Repository navigation
Expand file tree
/
Copy pathSolutionRunner.cs
More file actions
160 lines (144 loc) · 4.62 KB
/
Copy pathSolutionRunner.cs
File metadata and controls
160 lines (144 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using Silk.NET.OpenGL;
using Silk.NET.Windowing;
using System.Diagnostics;
using static GraphicsApiBenchmark.GLHelper;
namespace GraphicsApiBenchmark
{
public abstract class SolutionRunner
{
private readonly int resolution;
private readonly int frameCountPerSolution;
private readonly IWindow window;
private readonly NoOpSolution noOpSolution = new();
private readonly List<ISolution> solutions;
protected Dictionary<string, string> textResources = [];
private int currentFrameIndex = 0;
private int currentSolutionIndex = -1;
private string gpuName = "Unknown renderer";
protected SolutionRunner(int resolution, int frameCountPerSolution)
{
this.resolution = resolution;
this.frameCountPerSolution = frameCountPerSolution;
window = CreateWindow();
LoadResources();
solutions = CreateSolutions();
}
public void Run()
{
RunSolutions();
PrintResults();
}
protected abstract void LoadResources();
protected abstract List<ISolution> CreateSolutions();
private void RunSolutions()
{
window.Run();
window.Dispose();
}
private IWindow CreateWindow()
{
#if DEBUG
var contextFlags = ContextFlags.Debug;
#else
var contextFlags = ContextFlags.ForwardCompatible;
#endif
currentFrameIndex = 0;
var options = WindowOptions.Default with
{
VSync = false,
Size = new(resolution),
Title = "Sandbox",
API = new(ContextAPI.OpenGL, ContextProfile.Core, contextFlags, new(4, 6))
};
var window = Window.Create(options);
window.Load += () =>
{
gl = window.CreateOpenGL();
InitializeLogging();
gpuName = GetRenderer();
};
window.Render += (double dt) =>
{
var solution = currentSolutionIndex == -1 ? noOpSolution : solutions[currentSolutionIndex];
if (currentFrameIndex == 0)
{
solution.Initialize();
UpdateProgress();
}
if (currentFrameIndex < frameCountPerSolution)
{
solution.Render((float)dt);
currentFrameIndex++;
}
if (currentFrameIndex >= frameCountPerSolution)
{
solution.Dispose();
currentFrameIndex = 0;
currentSolutionIndex++;
if (currentSolutionIndex >= solutions.Count)
{
window.Close();
}
}
};
window.Closing += () =>
{
gl.Dispose();
ResetCursor();
};
return window;
}
[Conditional("RELEASE")]
private void UpdateProgress()
{
if (currentSolutionIndex != -1)
{
Console.CursorLeft = 0;
Console.Write($"{currentSolutionIndex + 1} / {solutions.Count}");
}
}
[Conditional("RELEASE")]
private static void ResetCursor()
{
Console.CursorLeft = 0;
}
protected abstract void PrintResults();
protected void PrintGeneralInformations()
{
PrintBuildConfiguration();
Console.WriteLine($"GPU: {gpuName}");
Console.WriteLine($"Resolution: {resolution}x{resolution} px");
Console.WriteLine($"Rendered frames per solution: {frameCountPerSolution}");
}
protected static void PrintBuildConfiguration()
{
#if DEBUG
Console.ForegroundColor = ConsoleColor.Black;
Console.BackgroundColor = ConsoleColor.Red;
Console.WriteLine("DEBUG MODE");
Console.ResetColor();
#else
Console.WriteLine("RELEASE MODE");
#endif
}
protected void PrintSolutionResults()
{
foreach (var solution in solutions)
{
solution.PrintResults();
}
}
public IWindow GetWindow()
{
return window;
}
public int GetCurrentFrameIndex()
{
return currentFrameIndex;
}
public IReadOnlyDictionary<string, string> GetTextResources()
{
return textResources;
}
}
}