forked from CIS565-Fall-2021/Project1-CUDA-Flocking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
319 lines (254 loc) · 8.84 KB
/
main.cpp
File metadata and controls
319 lines (254 loc) · 8.84 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
/**
* @file main.cpp
* @brief Example Boids flocking simulation for CIS 565
* @authors Liam Boone, Kai Ninomiya, Kangning (Gary) Li
* @date 2013-2017
* @copyright University of Pennsylvania
*/
#include "main.hpp"
// ================
// Configuration
// ================
// LOOK-2.1 LOOK-2.3 - toggles for UNIFORM_GRID and COHERENT_GRID
#define VISUALIZE 1
#define UNIFORM_GRID 1
#define COHERENT_GRID 1
// LOOK-1.2 - change this to adjust particle count in the simulation
const int N_FOR_VIS = 300000;
const float DT = 0.2f;
/**
* C main function.
*/
int main(int argc, char* argv[]) {
projectName = "565 CUDA Intro: Boids";
if (init(argc, argv)) {
mainLoop();
Boids::endSimulation();
return 0;
} else {
return 1;
}
}
//-------------------------------
//---------RUNTIME STUFF---------
//-------------------------------
std::string deviceName;
GLFWwindow *window;
/**
* Initialization of CUDA and GLFW.
*/
bool init(int argc, char **argv) {
// Set window title to "Student Name: [SM 2.0] GPU Name"
cudaDeviceProp deviceProp;
int gpuDevice = 0;
int device_count = 0;
cudaGetDeviceCount(&device_count);
if (gpuDevice > device_count) {
std::cout
<< "Error: GPU device number is greater than the number of devices!"
<< " Perhaps a CUDA-capable GPU is not installed?"
<< std::endl;
return false;
}
cudaGetDeviceProperties(&deviceProp, gpuDevice);
int major = deviceProp.major;
int minor = deviceProp.minor;
std::ostringstream ss;
ss << projectName << " [SM " << major << "." << minor << " " << deviceProp.name << "]";
deviceName = ss.str();
// Window setup stuff
glfwSetErrorCallback(errorCallback);
if (!glfwInit()) {
std::cout
<< "Error: Could not initialize GLFW!"
<< " Perhaps OpenGL 3.3 isn't available?"
<< std::endl;
return false;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, deviceName.c_str(), NULL, NULL);
if (!window) {
glfwTerminate();
return false;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback);
glfwSetCursorPosCallback(window, mousePositionCallback);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
return false;
}
// Initialize drawing state
initVAO();
// Default to device ID 0. If you have more than one GPU and want to test a non-default one,
// change the device ID.
cudaGLSetGLDevice(0);
cudaGLRegisterBufferObject(boidVBO_positions);
cudaGLRegisterBufferObject(boidVBO_velocities);
// Initialize N-body simulation
Boids::initSimulation(N_FOR_VIS);
updateCamera();
initShaders(program);
glEnable(GL_DEPTH_TEST);
return true;
}
void initVAO() {
std::unique_ptr<GLfloat[]> bodies{ new GLfloat[4 * (N_FOR_VIS)] };
std::unique_ptr<GLuint[]> bindices{ new GLuint[N_FOR_VIS] };
glm::vec4 ul(-1.0, -1.0, 1.0, 1.0);
glm::vec4 lr(1.0, 1.0, 0.0, 0.0);
for (int i = 0; i < N_FOR_VIS; i++) {
bodies[4 * i + 0] = 0.0f;
bodies[4 * i + 1] = 0.0f;
bodies[4 * i + 2] = 0.0f;
bodies[4 * i + 3] = 1.0f;
bindices[i] = i;
}
glGenVertexArrays(1, &boidVAO); // Attach everything needed to draw a particle to this
glGenBuffers(1, &boidVBO_positions);
glGenBuffers(1, &boidVBO_velocities);
glGenBuffers(1, &boidIBO);
glBindVertexArray(boidVAO);
// Bind the positions array to the boidVAO by way of the boidVBO_positions
glBindBuffer(GL_ARRAY_BUFFER, boidVBO_positions); // bind the buffer
glBufferData(GL_ARRAY_BUFFER, 4 * (N_FOR_VIS) * sizeof(GLfloat), bodies.get(), GL_DYNAMIC_DRAW); // transfer data
glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer((GLuint)positionLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
// Bind the velocities array to the boidVAO by way of the boidVBO_velocities
glBindBuffer(GL_ARRAY_BUFFER, boidVBO_velocities);
glBufferData(GL_ARRAY_BUFFER, 4 * (N_FOR_VIS) * sizeof(GLfloat), bodies.get(), GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(velocitiesLocation);
glVertexAttribPointer((GLuint)velocitiesLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, boidIBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, (N_FOR_VIS) * sizeof(GLuint), bindices.get(), GL_STATIC_DRAW);
glBindVertexArray(0);
}
void initShaders(GLuint * program) {
GLint location;
program[PROG_BOID] = glslUtility::createProgram(
"shaders/boid.vert.glsl",
"shaders/boid.geom.glsl",
"shaders/boid.frag.glsl", attributeLocations, 2);
glUseProgram(program[PROG_BOID]);
if ((location = glGetUniformLocation(program[PROG_BOID], "u_projMatrix")) != -1) {
glUniformMatrix4fv(location, 1, GL_FALSE, &projection[0][0]);
}
if ((location = glGetUniformLocation(program[PROG_BOID], "u_cameraPos")) != -1) {
glUniform3fv(location, 1, &cameraPosition[0]);
}
}
//====================================
// Main loop
//====================================
void runCUDA() {
// Map OpenGL buffer object for writing from CUDA on a single GPU
// No data is moved (Win & Linux). When mapped to CUDA, OpenGL should not
// use this buffer
float4 *dptr = NULL;
float *dptrVertPositions = NULL;
float *dptrVertVelocities = NULL;
cudaGLMapBufferObject((void**)&dptrVertPositions, boidVBO_positions);
cudaGLMapBufferObject((void**)&dptrVertVelocities, boidVBO_velocities);
// execute the kernel
#if UNIFORM_GRID && COHERENT_GRID
Boids::stepSimulationCoherentGrid(DT);
#elif UNIFORM_GRID
Boids::stepSimulationScatteredGrid(DT);
#else
Boids::stepSimulationNaive(DT);
#endif
#if VISUALIZE
Boids::copyBoidsToVBO(dptrVertPositions, dptrVertVelocities);
#endif
// unmap buffer object
cudaGLUnmapBufferObject(boidVBO_positions);
cudaGLUnmapBufferObject(boidVBO_velocities);
}
void mainLoop() {
double fps = 0;
double timebase = 0;
int frame = 0;
int print = 0;
Boids::unitTest(); // LOOK-1.2 We run some basic example code to make sure
// your CUDA development setup is ready to go.
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
frame++;
double time = glfwGetTime();
if (time - timebase > 1.0) {
fps = frame / (time - timebase);
timebase = time;
frame = 0;
}
runCUDA();
std::ostringstream ss;
ss << "[";
ss.precision(1);
ss << std::fixed << fps;
ss << " fps] " << deviceName;
glfwSetWindowTitle(window, ss.str().c_str());
if (time > print) {
std::cout << fps << std::endl;
print++;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#if VISUALIZE
glUseProgram(program[PROG_BOID]);
glBindVertexArray(boidVAO);
glPointSize((GLfloat)pointSize);
glDrawElements(GL_POINTS, N_FOR_VIS + 1, GL_UNSIGNED_INT, 0);
glPointSize(1.0f);
glUseProgram(0);
glBindVertexArray(0);
glfwSwapBuffers(window);
#endif
}
glfwDestroyWindow(window);
glfwTerminate();
}
void errorCallback(int error, const char *description) {
fprintf(stderr, "error %d: %s\n", error, description);
}
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
leftMousePressed = (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS);
rightMousePressed = (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS);
}
void mousePositionCallback(GLFWwindow* window, double xpos, double ypos) {
if (leftMousePressed) {
// compute new camera parameters
phi += (xpos - lastX) / width;
theta -= (ypos - lastY) / height;
theta = std::fmax(0.01f, std::fmin(theta, 3.14f));
updateCamera();
}
else if (rightMousePressed) {
zoom += (ypos - lastY) / height;
zoom = std::fmax(0.1f, std::fmin(zoom, 5.0f));
updateCamera();
}
lastX = xpos;
lastY = ypos;
}
void updateCamera() {
cameraPosition.x = zoom * sin(phi) * sin(theta);
cameraPosition.z = zoom * cos(theta);
cameraPosition.y = zoom * cos(phi) * sin(theta);
cameraPosition += lookAt;
projection = glm::perspective(fovy, float(width) / float(height), zNear, zFar);
glm::mat4 view = glm::lookAt(cameraPosition, lookAt, glm::vec3(0, 0, 1));
projection = projection * view;
GLint location;
glUseProgram(program[PROG_BOID]);
if ((location = glGetUniformLocation(program[PROG_BOID], "u_projMatrix")) != -1) {
glUniformMatrix4fv(location, 1, GL_FALSE, &projection[0][0]);
}
}