-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.hpp
161 lines (128 loc) · 4.5 KB
/
util.hpp
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
//glfw includes
#include <GLFW/glfw3.h>
#include <iostream>
//imgui includes
#include "./imgui/imgui.h"
#include "./imgui/imgui_impl_glfw.h"
#include "./imgui/imgui_impl_opengl3.h"
#ifndef NDEBUG
#define checkCudaErrors(err) __checkCudaErrors(err, __FILE__, __LINE__)
inline void __checkCudaErrors(cudaError err, const char* file, const int line) {
if (err != cudaSuccess)
{
fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",
file, line, (int)err, cudaGetErrorString(err));
system("pause");
exit(1);
}
}
inline void checkKernelErrors() {
cudaDeviceSynchronize();
checkCudaErrors(cudaGetLastError());
}
#else //disable GPU debuging
#define checkCudaErrors(err) err
inline void checkKernelErrors() {}
#endif
#define CPU_MODE 1
#define GPU_MODE 2
#define FPS_UPDATE_DELAY 0.5
//mandatory forward declaration
void reinit();
//global variables
namespace gbl{
int SCREEN_X=640;
int SCREEN_Y=480;
float4* pixels;
float4* d_pixels;
//display callback for drawing pixels
void (*display)();
int mode = GPU_MODE;
bool paused = false; //stop displaying pixel (prevent unallowed memory access when processing callback)
bool needResize = false; //if pixel buffer needs to be reallocated (prevent too many calls to free/malloc)
bool otherWindow = true; //display or not other window
int frameAcc = 0; //number of frame since last FPS calculation
double prevUpdt = 0.0; //time at previous FPS evaluation
int currentFPS = 0.0f;
void resizePixelsBuffer(){
reinit();
}
//handle fps computation, and reallocating buffer if needed(to avoid too many call to mallloc/free)
void calculate(GLFWwindow* window){
frameAcc++;
double timeCurr = glfwGetTime();
float elapsedTime = timeCurr-prevUpdt;
if(elapsedTime>FPS_UPDATE_DELAY){
currentFPS = frameAcc / elapsedTime ;
frameAcc = 0;
prevUpdt = timeCurr;
if(needResize){
glfwGetWindowSize(window, &SCREEN_X, &SCREEN_Y);
resizePixelsBuffer();
needResize = false;
paused = false;
}
}
}
}//end namespace gbl
void clean(); void init();
namespace utl{
void initImGui(GLFWwindow* window){
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // IF using Docking Branch
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; //enables multi window
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true); // Second param install_callback=true will install GLFW callbacks and chain to existing ones.
ImGui_ImplOpenGL3_Init();
}
void newframeImGui(){
// (Your code calls glfwPollEvents())
// ...
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
}
void toggleMode(int m){
gbl::paused = true;
clean();
init();
gbl::paused = false;
}
void wdw_info(int mode, int sx, int sy, int fps){
ImGui::Begin("Base info");
ImGui::Text("Mode : ");
ImGui::SameLine(); if(ImGui::RadioButton("CPU", &gbl::mode, CPU_MODE)){toggleMode(CPU_MODE);}
ImGui::SameLine(); if(ImGui::RadioButton("GPU", &gbl::mode, GPU_MODE)){toggleMode(GPU_MODE);}
ImGui::Text("Current Window size : %d x %d", sx, sy);
ImGui::Text("FPS : %d", fps);
ImGui::End();
}
void multiViewportImGui(GLFWwindow* window){
ImGuiIO& io = ImGui::GetIO();
if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable){
// Update and Render additional Platform Windows
ImGui::UpdatePlatformWindows();
ImGui::RenderPlatformWindowsDefault();
// for OpenGL: restore current GL context.
glfwMakeContextCurrent(window);
}
}
void endframeImGui(){
// Rendering
// (Your code clears your framebuffer, renders your other stuff etc.)
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// (Your code calls glfwSwapBuffers() etc.)
}
void shutdownImGui(){
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
}
} //end namespace utl