-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgpu_timers.cpp
More file actions
357 lines (286 loc) · 10.3 KB
/
gpu_timers.cpp
File metadata and controls
357 lines (286 loc) · 10.3 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/random.hpp"
#include "glm/gtx/string_cast.hpp"
#include "glm/gtx/rotate_vector.hpp"
#include "common/debug.h"
#include "common/programobject.h"
#include "common/objloader.h"
#include "common/meshbuffer.h"
#include "common/meshobject.h"
#include "common/cubegenerator.h"
#include "common/renderable.h"
#include "common/memory_info.h"
#define CHECKEXTENSIONS_IMPLEMENTATION
#include "common/checkextensions.h"
#include <pnglite.h>
using namespace std;
using namespace ogle;
std::string DataDirectory; // ends with a forward slash
int WINDOW_WIDTH = 1024;
int WINDOW_HEIGHT = 1024;
GLFWwindow* glfwWindow;
glm::mat4x4 Camera;
glm::mat4x4 SceneBase;
glm::mat4x4 Projection;
glm::mat4x4 ProjectionView;
glm::vec3 CameraTarget;
glm::vec3 CameraPosition;
glm::vec3 CameraUp;
bool MousePositionCapture = false;
glm::vec2 PrevMousePos;
Renderable Cube;
ProgramObject CubeShader;
const unsigned int CubeCount = 5;
glm::mat4x4 Models[CubeCount];
glm::vec4 Colors[CubeCount];
GLuint NormalMaps[CubeCount];
void errorCallback(int error, const char* description)
{
cerr << description << endl;
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (mods == GLFW_MOD_CONTROL && action == GLFW_RELEASE){
MousePositionCapture = false;
glfwSetCursorPosCallback(glfwWindow, nullptr);
}
if (key == GLFW_KEY_P && action == GLFW_RELEASE){
png_t out_img = {0};
int error = 0;
error = png_open_file_write(&out_img, "screen_shot.png");
int width = WINDOW_WIDTH;
int height = WINDOW_HEIGHT;
int comp_count = 4;
int bytes_per = 1;
unsigned char* data = new unsigned char[width * height * comp_count * bytes_per];
glReadPixels(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_RGBA, GL_UNSIGNED_BYTE, data);
int bits_per_pixel = 8;
error = png_set_data(&out_img, width, height, bits_per_pixel, PNG_TRUECOLOR_ALPHA, data);
error = png_close_file(&out_img);
delete [] data; data = 0;
}
}
void cursorCallback(GLFWwindow* window, double x, double y)
{
if (MousePositionCapture){
glm::vec2 curr = glm::vec2(float(x), float(y));
glm::vec2 diff = curr - PrevMousePos;
PrevMousePos = curr;
glm::vec3 rotation_axis = glm::vec3(diff.y, -diff.x, 0.f);
float mag = glm::length(rotation_axis);
if (mag == 0) return;
rotation_axis = rotation_axis / mag; // normalize
glm::vec3 view_vec = CameraPosition - CameraTarget;
view_vec = glm::rotate(view_vec, mag * 0.001f, rotation_axis);
CameraPosition = CameraTarget + view_vec;
glm::vec3 view_dir = glm::normalize(view_vec);
glm::vec3 tmp = glm::cross(CameraUp, view_dir);
tmp = glm::normalize(tmp);
CameraUp = glm::normalize(glm::cross(view_dir, tmp));
Camera = glm::lookAt(CameraPosition, CameraTarget, CameraUp);
}
}
void mouseCallback(GLFWwindow* window, int btn, int action, int mods)
{
if (mods == GLFW_MOD_ALT){
if (btn == GLFW_MOUSE_BUTTON_1) {
if(action == GLFW_PRESS) {
double x,y;
glfwGetCursorPos(glfwWindow, &x, &y);
PrevMousePos = glm::vec2(float(x), float(y));
MousePositionCapture = true;
glfwSetCursorPosCallback(glfwWindow, cursorCallback);
}
if(action == GLFW_RELEASE) {
MousePositionCapture = false;
glfwSetCursorPosCallback(glfwWindow, nullptr);
}
}
}
}
void scrollCallback(GLFWwindow* window, double xoffset, double yoffset)
{
glm::vec3 view_vec = CameraPosition - CameraTarget;
float dist = glm::length(view_vec);
view_vec /= dist;
dist -= 10.f * float(yoffset);
CameraPosition = CameraTarget + view_vec * dist;
Camera = glm::lookAt(CameraPosition, CameraTarget, CameraUp);
}
void initGLFW(){
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
glfwWindow = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "GpuTimers", NULL, NULL);
if (!glfwWindow)
{
fprintf(stderr, "Failed to create GLFW glfwWindow\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(glfwWindow);
glfwSwapInterval( 0 ); // Turn off vsync for benchmarking.
glfwSetTime( 0.0 );
glfwSetKeyCallback(glfwWindow, keyCallback);
glfwSetErrorCallback(errorCallback);
glfwSetMouseButtonCallback(glfwWindow, mouseCallback);
glfwSetScrollCallback(glfwWindow, scrollCallback);
}
void initGLAD(){
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
{
std::cout << "initGLAD: Failed to initialize OpenGL context" << std::endl;
exit(EXIT_FAILURE);
}
int width,height;
glfwGetFramebufferSize(glfwWindow, &width, &height);
glViewport( 0, 0, (GLsizei)width, (GLsizei)height );
}
void setDataDir(int argc, char *argv[]){
// get base directory for reading in files
DataDirectory = "./data/";
}
void initView(){
float fovy = glm::radians(30.f);
Projection = glm::perspective<float>(fovy, WINDOW_WIDTH/(float)WINDOW_HEIGHT, 0.1f, 1000.0f );
float opposite = 3.f;
float adjacent = atan(fovy*.5f) / opposite;
adjacent = 1.f / adjacent;
CameraUp = glm::vec3(0,1,0);
CameraTarget = glm::vec3(0);
glm::vec3 direction(0,0,1);
CameraPosition = direction * adjacent;
Camera = glm::lookAt(CameraPosition, CameraTarget, CameraUp);
}
void initCubes(){
ogle::CubeGenerator shape;
shape.scale(1.f);
shape.tessellation_density(1024);
shape.generate();
MeshBuffer buffer;
buffer.setVerts(shape.Positions.size(), (const float*)shape.Positions.data());
buffer.setNorms(shape.Positions.size(), (const float*)shape.Normals.data());
buffer.setTexCoords(0, shape.Positions.size(), (const float*)shape.TexCoords.data());
buffer.setIndices(shape.Indices.size(), shape.Indices.data());
Cube.init(buffer);
std::map<unsigned int, std::string> shaders;
shaders[GL_VERTEX_SHADER] = DataDirectory + "cube.vert";
shaders[GL_FRAGMENT_SHADER] = DataDirectory + "cube.frag";
CubeShader.init(shaders);
for (unsigned int i=0; i<CubeCount; ++i)
{
float randval = 2;
glm::vec4 color(glm::linearRand(0.f,1.f),glm::linearRand(0.f,1.f),glm::linearRand(0.f,1.f), 1.f);
glm::vec4 position(glm::linearRand(-randval,randval),glm::linearRand(-randval,randval),glm::linearRand(-randval,randval), 1);
Models[i][3] = position;
Colors[i] = color;
}
png_t img = {0};
png_open_file_read(&img, (DataDirectory + "skyline-buildings-new-york-skyscrapers.png").c_str());
unsigned char* img_data;
img_data = new unsigned char[img.width * img.height * img.bpp];
std::cout << "---------------------------------" << std::endl;
std::cout << "Image size: " << ((img.width * img.height * img.bpp) / 1024 / 1024) << endl;
std::cout << "---------------------------------" << std::endl;
png_get_data(&img, img_data);
png_close_file(&img);
glActiveTexture(GL_TEXTURE0);
glGenTextures(CubeCount, NormalMaps);
for (unsigned int i=0; i<CubeCount; ++i){
// std::cout << "Loading number: " << i << " out of " << CubeCount << std::endl;
glBindTexture(GL_TEXTURE_2D, NormalMaps[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.width, img.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (const GLvoid*)img_data);
glFlush();
memory_info_print(memory_info_report_limits(), memory_info_report_eviction());
}
glBindTexture(GL_TEXTURE_2D, 0);
delete [] img_data;
}
void init(int argc, char* argv[]){
setDataDir(argc, argv);
initGLFW();
initGLAD();
ogle::Debug::init();
png_init(0,0);
memory_info_init();
std::cout << "initial" << std::endl;
MemoryLimits ml = memory_info_report_limits();
MemoryEvictionInfo mei = memory_info_report_eviction();
memory_info_print(ml);
memory_info_print(mei);
initCubes();
initView();
std::cout << "============ Loading complete ========" << std::endl;
}
void update(){
float deltaTime = (float)glfwGetTime(); // get's the amount of time since last setTime
glfwSetTime(0);
ProjectionView = Projection * Camera;
}
void renderCubes(){
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glActiveTexture(GL_TEXTURE0);
CubeShader.bind();
CubeShader.setMatrix44((const float*)&Camera, "View");
CubeShader.setMatrix44((const float*)&Projection, "Projection");
CubeShader.setFloat(10, "Shininess");
for (int i=0; i<CubeCount; ++i)
{
glBindTexture(GL_TEXTURE_2D, NormalMaps[i]);
CubeShader.setMatrix44((const float*)&Models[i], "Model");
CubeShader.setVec4((const float*)&Colors[i], "Diffuse");
Cube.render();
}
}
void render(){
static int i =0;
glClearColor( 0,0,0,0 );
glClearDepth( 1 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
renderCubes();
memory_info_print(memory_info_report_limits(), memory_info_report_eviction());
if (i > 10) exit(1);
i += 1;
}
void runloop(){
glfwSetTime(0); // init timer
while (!glfwWindowShouldClose(glfwWindow)){
glfwPollEvents();
update();
render();
glfwSwapBuffers(glfwWindow);
}
}
void shutdown(){
glDeleteTextures(CubeCount, NormalMaps);
CubeShader.shutdown();
Cube.shutdown();
ogle::Debug::shutdown();
glfwDestroyWindow(glfwWindow);
glfwTerminate();
}
int main(int argc, char* argv[]){
init(argc, argv);
runloop();
shutdown();
exit(EXIT_SUCCESS);
}