forked from bgasiorzewski/engine
/
engine.c
332 lines (275 loc) · 10.2 KB
/
engine.c
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <GLFW/glfw3.h>
#include "cl.h"
#include "camera.h"
#include "error.h"
#include "geom.h"
#include "globals.h"
#include "render.h"
#include "types.h"
// TODO create proper file for haskell stuff
#include "HsFFI.h"
void load_file(HsPtr name);
void push_oct_tree_empty(void);
void push_oct_tree_solid(float r, float g, float b);
void push_oct_tree_partial(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7);
static void key_callback(GLFWwindow *windows, int key, int scancode, int action, int mods);
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
static void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos);
static void initOctTree(void);
#define SOFT_CHECK_CL(status, msg) do {if (status != CL_SUCCESS) {num_platforms = 0; fprintf(stderr, "WARNING: %s (%d)\n", msg, status); return;}} while (0)
static void init_cl(void)
{
cl_int status;
cl_platform_id platform_id;
cl_context context;
cl_program program;
char *kernel_src = malloc(10240);
check_nn(kernel_src, "kernel_src");
status = clGetPlatformIDs(1, &platform_id, &num_platforms);
SOFT_CHECK_CL(status, "get platform ids");
fprintf(stderr, "#platforms: %u\n", num_platforms);
if (num_platforms == 0)
return;
static char info[4][128];
status = clGetPlatformInfo(platform_id, CL_PLATFORM_PROFILE, 128, info[0], NULL);
SOFT_CHECK_CL(status, "get platform profile");
status = clGetPlatformInfo(platform_id, CL_PLATFORM_VERSION, 128, info[1], NULL);
SOFT_CHECK_CL(status, "get platform version");
status = clGetPlatformInfo(platform_id, CL_PLATFORM_NAME, 128, info[2], NULL);
SOFT_CHECK_CL(status, "get platform name");
status = clGetPlatformInfo(platform_id, CL_PLATFORM_VENDOR, 128, info[3], NULL);
SOFT_CHECK_CL(status, "get platform vendor");
fprintf(stderr, "profile: %s\n", info[0]);
fprintf(stderr, "version: %s\n", info[1]);
fprintf(stderr, "name: %s\n", info[2]);
fprintf(stderr, "vendor: %s\n", info[3]);
cl_context_properties *props = getContextProperties(platform_id);
context = clCreateContextFromType(props, CL_DEVICE_TYPE_GPU, NULL, NULL, &status);
SOFT_CHECK_CL(status, "create context");
// create a command queue
cl_device_id device_id;
status = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
SOFT_CHECK_CL(status, "get device ids");
queue = clCreateCommandQueue(context, device_id, 0, &status);
SOFT_CHECK_CL(status, "create command queue");
// allocate memory objects
mainOctCL = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, octTreeLength*sizeof(OctTreeNode), mainOctTree, &status);
SOFT_CHECK_CL(status, "create buffer");
glGenTextures(1, &texture);
check_gl();
glBindTexture(GL_TEXTURE_2D, texture);
check_gl();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
check_gl();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
check_gl();
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_FLOAT, NULL);
check_gl();
glFinish();
check_gl();
image = clCreateFromGLTexture(context, CL_MEM_WRITE_ONLY, GL_TEXTURE_2D, 0, texture, &status);
SOFT_CHECK_CL(status, "create image");
// create the compute program
FILE *kernel_handle = fopen("ray.cl", "rb");
check_nn(kernel_handle, "fopen ray.cl");
size_t n_bytes = fread(kernel_src, 1, 10239, kernel_handle);
kernel_src[n_bytes] = '\0';
check_ferror(kernel_handle, "fread");
program = clCreateProgramWithSource(context, 1, (const char **)&kernel_src, NULL, &status);
SOFT_CHECK_CL(status, "create program");
// build the compute program executable
status = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (status != CL_BUILD_PROGRAM_FAILURE && status != CL_SUCCESS) {
SOFT_CHECK_CL(status, "build program");
} else {
size_t log_size;
status = clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
SOFT_CHECK_CL(status, "get program build log size");
char *log = malloc(log_size);
check_nn(log, "log");
status = clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, log_size, log, NULL);
SOFT_CHECK_CL(status, "get program build log");
fprintf(stderr, "build kernel log:\n%s\n", log);
}
// create the compute kernel
kernel = clCreateKernel(program, "ray_cl", &status);
SOFT_CHECK_CL(status, "create kernel");
status = clReleaseProgram(program);
SOFT_CHECK_CL(status, "release program");
status = clReleaseContext(context);
SOFT_CHECK_CL(status, "release context");
fprintf(stderr, "OpenCL initialization successful\n");
render_method = TracerCL;
}
int main(int argc, char **argv)
{
glfwSetErrorCallback(error_callback);
/* Initialize the library */
if (!glfwInit()){
fprintf(stderr, "Initialization failed.\n");
return 1;
}
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(width, height, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
fprintf(stderr, "Error creating window.\n");
return 1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, 1);
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetCursorPosCallback(window, cursor_pos_callback);
//**************************** generowanie przykładowych piksli
hs_init(&argc, &argv);
initOctTree();
hs_exit();
float *piksele = malloc(height*width*3*sizeof(*piksele));
printf("sizeof(OctTreeNode)=%d\n", (int)sizeof(OctTreeNode));
//****************************
init_cl();
turnCamera(0.f,0.f,0.f); // Calculates initial camera direction
fflush(stderr);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
for (int i = 0; i < height * width * 3; i++)
piksele[i] = 0.0;
clock_t start = clock();
captureOctTree(camera_pos, camera_target, up, width, height, piksele);
clock_t end = clock();
// show render time in window title
char title[16];
snprintf(title, 16, "%d ms", (int)((end - start) / (CLOCKS_PER_SEC / 1000)));
glfwSetWindowTitle(window, title);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
if (num_platforms > 0) {
clReleaseMemObject(mainOctCL);
clReleaseMemObject(image);
clReleaseKernel(kernel);
clReleaseCommandQueue(queue);
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
if (action == GLFW_PRESS || action == GLFW_REPEAT)
{
switch (key) {
case GLFW_KEY_W:
moveCamera(1.f, 0.f, 0.f);
break;
case GLFW_KEY_S:
moveCamera(-1.f, 0.f, 0.f);
break;
case GLFW_KEY_A:
moveCamera(0.f, -1.f, 0.f);
break;
case GLFW_KEY_D:
moveCamera(0.f, 1.f, 0.f);
break;
case GLFW_KEY_Z:
moveCameraGlobal(0.f, 0.f, -1.f);
break;
case GLFW_KEY_Q:
moveCameraGlobal(0.f, 0.f, 1.f);
break;
case GLFW_KEY_I:
turnCamera(1.f, 0.f, 0.f);
break;
case GLFW_KEY_K:
turnCamera(-1.f, 0.f, 0.f);
break;
case GLFW_KEY_L:
turnCamera(0.f, -1.f, 0.f);
break;
case GLFW_KEY_J:
turnCamera(0.f, 1.f, 0.f);
break;
case GLFW_KEY_T:
changeFOV(-1.f);
break;
case GLFW_KEY_Y:
changeFOV(1.f);
break;
case GLFW_KEY_M:
light = camera_pos;
break;
case GLFW_KEY_P:
nextRenderMethod();
break;
/*default:*/
}
}
}
static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_PRESS) {
camera_movement_active = 1;
}
if (button == GLFW_MOUSE_BUTTON_1 && action == GLFW_RELEASE) {
camera_movement_active = 0;
}
}
static void cursor_pos_callback(GLFWwindow *window, double xpos, double ypos)
{
static double last_xpos = 0, last_ypos = 0;
xpos -= last_xpos;
ypos -= last_ypos;
last_xpos += xpos;
last_ypos += ypos;
if (camera_movement_active) {
turnCamera(ypos * cursor_turn_speed, xpos * cursor_turn_speed, 0.f);
}
}
void push_oct_tree_partial(int c0, int c1, int c2, int c3, int c4, int c5, int c6, int c7)
{
int child_arr[8] = {c0, c1, c2, c3, c4, c5, c6, c7};
mainOctTree[octTreeLength].type = Partial;
for (int i = 0; i < 8; i++) {
int x = (i / 4) % 2;
int y = (i / 2) % 2;
int z = i % 2;
mainOctTree[octTreeLength].nodes[x][y][z] = child_arr[i];
mainOctTree[child_arr[i]].parent = octTreeLength;
mainOctTree[child_arr[i]].x = x;
mainOctTree[child_arr[i]].y = y;
mainOctTree[child_arr[i]].z = z;
}
octTreeLength++;
}
void push_oct_tree_solid(float r, float g, float b)
{
mainOctTree[octTreeLength].type = Solid;
mainOctTree[octTreeLength].color = (Color4f) {r,g,b,0};
octTreeLength++;
}
void push_oct_tree_empty(void)
{
mainOctTree[octTreeLength].type = Empty;
octTreeLength++;
}
static void initOctTree(void)
{
mainOctTree = malloc(1024 * 1024 * sizeof(*mainOctTree));
octTreeLength = 0;
load_file("model.json");
mainOctTree[0].parent = -1;
fprintf(stderr, "Done loading.\n");
fflush(stderr);
}