-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMain.cpp
More file actions
121 lines (87 loc) · 3.08 KB
/
Main.cpp
File metadata and controls
121 lines (87 loc) · 3.08 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
#include <cstdio>
#include <cstdlib>
#include <Imgui/imgui.h>
#include "Raytracer.h"
#include "WorkerThread.h"
// Forces NVIDIA driver to be used
extern "C" { _declspec(dllexport) unsigned NvOptimusEnablement = true; }
#define TOTAL_TIMING_COUNT 100
float timings[TOTAL_TIMING_COUNT];
int current_frame = 0;
int main(int argument_count, char ** arguments) {
Window window(SCREEN_WIDTH, SCREEN_HEIGHT, "Raytracer");
#if _DEBUG
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(glMessageCallback, NULL);
#endif
Texture::init();
MaterialBuffer::init();
// Initialize timing stuff
Uint64 now = 0;
Uint64 last = 0;
float inv_perf_freq = 1.0f / (float)SDL_GetPerformanceFrequency();
float delta_time = 0;
float second = 0.0f;
int frames = 0;
int fps = 0;
// Initialize Scene
Scene scene;
scene.camera.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
Raytracer raytracer;
raytracer.scene = &scene;
// Initialize multi threading stuff
WorkerThreads::init(raytracer, window);
last = SDL_GetPerformanceCounter();
// Game loop
while (!window.is_closed) {
//window.clear();
scene.update(delta_time);
WorkerThreads::wake_up_worker_threads(window.tile_count_x * window.tile_count_y);
WorkerThreads::wait_on_worker_threads();
window.draw_quad();
// Perform frame timing
now = SDL_GetPerformanceCounter();
delta_time = float(now - last) * inv_perf_freq;
last = now;
// Calculate average of last TOTAL_TIMING_COUNT frames
timings[current_frame++ % TOTAL_TIMING_COUNT] = delta_time;
float avg = 0.0f;
int count = current_frame < TOTAL_TIMING_COUNT ? current_frame : TOTAL_TIMING_COUNT;
for (int i = 0; i < count; i++) {
avg += timings[i];
}
avg /= count;
// Calculate fps
frames++;
second += delta_time;
while (second >= 1.0f) {
second -= 1.0f;
fps = frames;
frames = 0;
}
PerformanceStats performance_stats = WorkerThreads::sum_performance_stats();
// Convert to MegaRays / Second
float num_primary_rays = float(performance_stats.num_primary_rays * fps) * 1e-6f;
float num_shadow_rays = float(performance_stats.num_shadow_rays * fps) * 1e-6f;
float num_reflection_rays = float(performance_stats.num_reflection_rays * fps) * 1e-6f;
float num_refraction_rays = float(performance_stats.num_refraction_rays * fps) * 1e-6f;
float num_total_rays = num_primary_rays + num_shadow_rays + num_reflection_rays + num_refraction_rays;
window.gui_begin();
ImGui::Begin("Raytracer");
ImGui::Text("Frame: %i", current_frame);
ImGui::Text("FPS: %i", fps);
ImGui::Text("Delta: %.2f ms", delta_time * 1000.0f);
ImGui::Text("Avg: %.2f ms", avg * 1000.0f);
if (ImGui::CollapsingHeader("Ray Counters", ImGuiTreeNodeFlags_DefaultOpen)) {
ImGui::Text("Total: %.2f MRays/s", num_total_rays);
ImGui::Text("Primary: %.2f MRays/s", num_primary_rays);
ImGui::Text("Shadow: %.2f MRays/s", num_shadow_rays);
ImGui::Text("Reflection: %.2f MRays/s", num_reflection_rays);
ImGui::Text("Refraction: %.2f MRays/s", num_refraction_rays);
}
ImGui::End();
window.gui_end();
window.swap();
}
return EXIT_SUCCESS;
}