-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPerfFramework.cpp
More file actions
303 lines (238 loc) · 7.17 KB
/
PerfFramework.cpp
File metadata and controls
303 lines (238 loc) · 7.17 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "PerfFramework.h"
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
#include <math.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <windows.h>
#include <psapi.h>
#include "PerfTimer.h"
#define GL_GPU_MEM_INFO_TOTAL_AVAILABLE_MEM_NVX 0x9048
#define GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX 0x9049
using namespace std;
using namespace glm;
static GLFWwindow* window = nullptr;
size_t GetFreeMemNvidia() {
// TODO: AMD version w/ GL_ATI_meminfo
GLint currMem = 0;
glGetIntegerv(GL_GPU_MEM_INFO_CURRENT_AVAILABLE_MEM_NVX, &currMem);
return (size_t)currMem * 1024;
}
size_t GetMainMemUsage() {
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
return pmc.PrivateUsage;
}
PerfRecord RunPerf(std::function<void()> setupFn, std::function<void()> drawFn, std::function<void()> teardownFn) {
int discardFrames = FRAMES_TO_DISCARD;
int frameCount = FRAMES_TO_RECORD;
if (!glfwInit()) {
exit(EXIT_FAILURE);
}
window = glfwCreateWindow(1920, 1080, "Voxel Perf", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glewInit();
glfwSwapInterval(0);
glEnable(GL_DEPTH_TEST);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
CheckGLErrors();
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClearColor(0.1f, 0.12f, 0.6f, 1.0f);
CheckGLErrors();
size_t gpuFreeBefore = GetFreeMemNvidia();
size_t memUsedBefore = GetMainMemUsage();
CheckGLErrors();
setupFn();
CheckGLErrors();
glFinish();
//size_t gpuFreeAfter = GetFreeMemNvidia();
//size_t memUsedAfter = GetMainMemUsage();
size_t gpuFreeAfter;
size_t memUsedAfter;
double totalFrameTime = 0.0;
double totalRecordedFrames = frameCount;
PerfTimer timer;
timer.Start();
while (frameCount > 0) {
double frameTime = timer.Stop();
timer.Start();
if (discardFrames == 0) {
if (frameCount > 0) {
totalFrameTime += frameTime * 1000.0f;
frameCount--;
}
} else {
discardFrames--;
if (discardFrames == 0) {
gpuFreeAfter = GetFreeMemNvidia();
memUsedAfter = GetMainMemUsage();
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawFn();
glfwSwapBuffers(window);
glfwPollEvents();
}
teardownFn();
glfwDestroyWindow(window);
glfwTerminate();
PerfRecord record;
record.averageFrameTimeMs = totalFrameTime / totalRecordedFrames;
record.gpuMemUsed = gpuFreeBefore - gpuFreeAfter;
record.mainMemUsed = memUsedAfter - memUsedBefore;
return record;
}
void CheckGLErrors() {
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR) {
cerr << "OpenGL error: " << err << endl;
}
}
glm::vec3 CameraPosition() {
return vec3(-16, 0, -16);
}
mat4 MakeModelView() {
mat4 view = lookAt(CameraPosition(), vec3(0, 0, 0), vec3(0, 1, 0));
if (ROTATE) {
view *= glm::rotate((float)glfwGetTime(), vec3(0, 1, 0));
}
return view;
}
mat4 MakeProjection() {
mat4 projection = perspective(GetFovY(), GetAspectRatio(), 1.0f, 10000.0f);
return projection;
}
mat4 MakeMvp() {
return MakeProjection() * MakeModelView();
}
float GetAspectRatio() {
int width, height;
glfwGetFramebufferSize(window, &width, &height);
return (float)width / (float)height;
}
float GetFovY() {
return radians(45.0f);
}
glm::vec3 GetNearPlane() {
float nearZ = 1.0f;
float top = nearZ / sin(GetFovY());
float bottom = -top;
float left = bottom * GetAspectRatio();
float right = top * GetAspectRatio();
vec3 nearPlane(right - left, top - bottom, nearZ);
return nearPlane;
}
void PrintMatrix(glm::mat4 matrix) {
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (j > 0) {
cout << ", ";
}
cout << matrix[i][j];
}
cout << endl;
}
}
GLuint MakeShaderProgram(std::vector<std::pair<std::string, GLenum>> shaders) {
GLuint program = glCreateProgram();
for (auto& s : shaders) {
string src = ReadTextFile(s.first);
GLuint shaderId = glCreateShader(s.second);
const GLchar* srcGlChar = (const GLchar*)src.c_str();
glShaderSource(shaderId, 1, &srcGlChar, NULL);
glCompileShader(shaderId);
GLint success = 0;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success);
if (success == GL_FALSE) {
GLint logSize = 0;
GLint maxLength = 0;
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &maxLength);
// The maxLength includes the NULL character
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(shaderId, maxLength, &maxLength, &errorLog[0]);
cerr << "*************************************************" << endl;
cerr << "Shader \"" << s.first << "\" failed to compile:" << endl;
cerr << &errorLog[0] << endl;
cerr << endl;
glDeleteShader(shaderId);
glDeleteProgram(program);
return 0;
}
glAttachShader(program, shaderId);
glDeleteShader(shaderId);
CheckGLErrors();
}
glLinkProgram(program);
return program;
}
std::string ReadTextFile(std::string filename) {
ifstream textStream(filename, std::ifstream::in);
if (textStream.fail()) {
cerr << "Failed to open file \"" << filename << "\"";
return{};
}
string str(static_cast<stringstream const&>(stringstream() << textStream.rdbuf()).str());
return str;
}
int Round10BitSigned(float value) {
if (value < 0) {
return (int)round(value * 512);
} else {
return (int)round(value * 511);
}
}
int Round2BitSigned(float value) {
if (value < 0) {
return (int)round(value * 2);
} else {
return (int)round(value * 1);
}
}
PackedVec PackVec3(vec3 v) {
PackedVec packed;
packed.w = 0;
packed.x = Round10BitSigned(v.x);
packed.y = Round10BitSigned(v.y);
packed.z = Round10BitSigned(v.z);
return packed;
}
PackedVec PackVec4(glm::vec4 v) {
PackedVec packed;
packed.w = Round2BitSigned(v.w);
packed.x = Round10BitSigned(v.x);
packed.y = Round10BitSigned(v.y);
packed.z = Round10BitSigned(v.z);
return packed;
}
PackedColor PackColor(vec3 color) {
PackedColor packed;
packed.a = 3;
packed.r = (unsigned int)(color.r * 1023);
packed.g = (unsigned int)(color.g * 1023);
packed.b = (unsigned int)(color.b * 1023);
return packed;
}
std::map<string, string> g_commandLineOptions;
bool IsOptionSet(std::string optionName) {
return g_commandLineOptions.count(optionName) > 0;
}
std::string GetOption(std::string optionName) {
if (g_commandLineOptions.count(optionName) == 0) {
return "";
} else {
return g_commandLineOptions[optionName];
}
}