From 18e5b2554b4cd73207e936e76e32c9d376cd1ba8 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Thu, 1 Feb 2018 01:27:30 +0100 Subject: abstracted rendering, cpu currently crashing randomly while zooming --- src/render.c | 184 +++++++++++++++++++++++++++++++++++++++++++++------- src/render.h | 14 ++++ src/render_cpu.c | 104 +---------------------------- src/render_cpu.h | 11 +--- src/render_opencl.c | 110 ++++--------------------------- src/render_opencl.h | 11 +--- 6 files changed, 192 insertions(+), 242 deletions(-) (limited to 'src') diff --git a/src/render.c b/src/render.c index e10e44b..30f1b7a 100644 --- a/src/render.c +++ b/src/render.c @@ -11,6 +11,15 @@ void init_render(config_t *config) { + x_min_s = -2.0; + x_max_s = 1.0; + y_min_s = -1.0; + y_max_s = 1.0; + x_min = x_min_s; + x_max = x_max_s; + y_min = y_min_s; + y_max = y_max_s; + _config = config; s_arr = (u32 *) malloc((_config->width) * (_config->height) * sizeof(u32)); _config->arr = s_arr; @@ -40,34 +49,31 @@ void init_render(config_t *config) { case MODE_CPU: init_cpu(_config); - glutDisplayFunc(render_cpu); - if (_config->zoomSpecific) - { - glutIdleFunc(idle_cpu); - } - else - { - glutIdleFunc(idle_cpu_dummy); - glutKeyboardFunc(keyboard_cpu); - glutMouseFunc(mouse_cpu); - } + _render = render_cpu; + _idle = idle_cpu; break; case MODE_OPENCL: init_opencl(_config); - glutDisplayFunc(render_opencl); - if (_config->zoomSpecific) - { - glutIdleFunc(idle_opencl); - } - else - { - glutIdleFunc(idle_opencl_dummy); - glutKeyboardFunc(keyboard_opencl); - glutMouseFunc(mouse_opencl); - } + _render = render_opencl; + _idle = idle_opencl; break; } + if (_config->zoomSpecific) + { + glutIdleFunc(idle); + } + else + { + glutIdleFunc(idle_dummy); + glutKeyboardFunc(keyboard); + glutMouseFunc(mouse); + } + + glutDisplayFunc(render); + + t_old = glutGet(GLUT_ELAPSED_TIME); + glutMainLoop(); } @@ -81,3 +87,137 @@ d64 zoom_func(d64 ft, d64 s) { return (s - expl(-ft)); } + +void idle_dummy(void) +{ + double xmin = (double) x_min; + double ymin = (double) y_min; + double xmax = (double) x_max; + double ymax = (double) y_max; + printf("x_min: %lf, y_min: %lf, x_max: %lf, y_max: %lf\n", xmin, ymin, xmax, ymax); + glutPostRedisplay(); +} + +void idle(void) +{ + int t = 0, delta = 0; + do + { + t = glutGet(GLUT_ELAPSED_TIME); + delta = t - t_old; + } while (delta < 16); // TODO: Hardcoded FPS + t_old = t; + + _idle(); + + //glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, _config->tex); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _config->width, + _config->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, + _config->arr); + glBindTexture(GL_TEXTURE_2D, 0); + ft += (_config->speed * (delta / 1000.0)); + x_min = x_min_s + + _config->zoom_func(ft, + (double) 2.0 + _config->to_x); + y_min = y_min_s + + _config->zoom_func(ft, + (double) 1.0 + _config->to_y); + x_max = x_max_s + - _config->zoom_func(ft, + (double) 1.0 - _config->to_x); + y_max = y_max_s + - _config->zoom_func(ft, + (double) 1.0 - _config->to_y); + glutPostRedisplay(); +} + +void render(void) +{ + _render(x_min, y_min, x_max, y_max); + + glBindTexture(GL_TEXTURE_2D, _config->tex); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _config->width, + _config->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, + _config->arr); + glBindTexture(GL_TEXTURE_2D, 0); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBindTexture(GL_TEXTURE_2D, _config->tex); + glEnable(GL_TEXTURE_2D); + + glBegin(GL_QUADS); + glTexCoord2i(0, 0); + glVertex2i(0, 0); + glTexCoord2i(0, 1); + glVertex2i(0, _config->height); + glTexCoord2i(1, 1); + glVertex2i(_config->width, _config->height); + glTexCoord2i(1, 0); + glVertex2i(_config->width, 0); + glEnd(); + + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + glutSwapBuffers(); +} + +void keyboard(unsigned char key, int mouseX, int mouseY) +{ + d64 w = (x_max - x_min), h = (y_max - y_min); + switch (key) + { + case 'w': + y_max += (h / 10.0); + y_min += (h / 10.0); + break; + case 'a': + x_max -= (w / 10.0); + x_min -= (w / 10.0); + break; + case 's': + y_max -= (h / 10.0); + y_min -= (h / 10.0); + break; + case 'd': + x_max += (w / 10.0); + x_min += (w / 10.0); + break; + case 'i': + _config->iterations++; + break; + case 'k': + _config->iterations--; + break; + } +} + +void mouse(int button, int state, int x, int y) +{ + if (state == GLUT_DOWN) + { + switch (button) + { + case GLUT_LEFT_BUTTON: + if (_config->speed < 0) + _config->speed = (-1) * _config->speed; + break; + case GLUT_RIGHT_BUTTON: + if (_config->speed > 0) + _config->speed = (-1) * _config->speed; + break; + } + _config->to_x = x_min + + ((d64) x * (x_max - x_min)) / _config->width; + _config->to_y = y_min + + ((d64) y * (y_max - y_min)) / _config->height; + t_old = glutGet(GLUT_ELAPSED_TIME); + glutIdleFunc(idle); + } + else if (state == GLUT_UP) + { + glutIdleFunc(idle_dummy); + } +} diff --git a/src/render.h b/src/render.h index 5972200..730673c 100644 --- a/src/render.h +++ b/src/render.h @@ -18,8 +18,22 @@ config_t *_config; u32 *s_arr; GLuint tex; +d64 x_min, x_max, y_min, y_max; +d64 x_min_s, x_max_s, y_min_s, y_max_s; + +float ft; +int t_old; + d64 zoom_func(d64 ft, d64 s); +void (*_render)(d64 x_min, d64 y_min, d64 x_max, d64 y_max); +void (*_idle)(void); +void keyboard(unsigned char key, int mouseX, int mouseY); +void mouse(int button, int state, int x, int y); +void idle_dummy(void); +void idle(void); +void render(void); + void init_render(config_t *config); void show_render(); diff --git a/src/render_cpu.c b/src/render_cpu.c index ef66e37..d5e0c09 100644 --- a/src/render_cpu.c +++ b/src/render_cpu.c @@ -8,51 +8,15 @@ #include "render_cpu.h" #define HAVE_STRUCT_TIMESPEC -void idle_cpu_dummy(void) -{ -// glBindTexture(GL_TEXTURE_2D, config_cpu->tex); -// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_cpu->width, -// config_cpu->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_cpu->arr); -// glBindTexture(GL_TEXTURE_2D, 0); - glutPostRedisplay(); -} - void init_cpu(config_t *config) { config_cpu = config; - x_min_s = -2.0; - x_max_s = 1.0; - y_min_s = -1.0; - y_max_s = 1.0; - x_min = x_min_s; - x_max = x_max_s; - y_min = y_min_s; - y_max = y_max_s; - - calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->arr); - t_old_cpu = glutGet(GLUT_ELAPSED_TIME); } -void render_cpu(void) +void render_cpu(d64 x_min, d64 y_min, d64 x_max, d64 y_max) { - glBindTexture(GL_TEXTURE_2D, config_cpu->tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_cpu->width, - config_cpu->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_cpu->arr); - glBindTexture(GL_TEXTURE_2D, 0); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindTexture(GL_TEXTURE_2D, config_cpu->tex); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - glTexCoord2i(0, 0); glVertex2i(0, 0); - glTexCoord2i(0, 1); glVertex2i(0, config_cpu->height); - glTexCoord2i(1, 1); glVertex2i(config_cpu->width, config_cpu->height); - glTexCoord2i(1, 0); glVertex2i(config_cpu->width, 0); - glEnd(); - - glDisable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, 0); - glutSwapBuffers(); + calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->arr); + printf("end render cpu\n"); } void calculate(d64 x_min, d64 y_min, d64 x_max, d64 y_max, u32 (*sfunc) (d64, d64, u32), u32 *arr) @@ -90,67 +54,5 @@ void calculate_t(void *args) void idle_cpu(void) { - int t = 0, delta = 0; - do - { - t = glutGet(GLUT_ELAPSED_TIME); - delta = t - t_old_cpu; - } - while(delta < 16); // TODO: Hardcoded FPS - t_old_cpu = t; - - calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->arr); - //glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, config_cpu->tex); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_cpu->width, config_cpu->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_cpu->arr); - glBindTexture(GL_TEXTURE_2D, 0); - ft+=(config_cpu->speed*(delta/1000.0)); - x_min = x_min_s + config_cpu->zoom_func(ft, (d64)2.0 + config_cpu->to_x); - y_min = y_min_s + config_cpu->zoom_func(ft, (d64)1.0 + config_cpu->to_y); - x_max = x_max_s - config_cpu->zoom_func(ft, (d64)1.0 - config_cpu->to_x); - y_max = y_max_s - config_cpu->zoom_func(ft, (d64)1.0 - config_cpu->to_y); - glutPostRedisplay(); -} -void keyboard_cpu(unsigned char key, int mouseX, int mouseY) -{ - switch(key) - { - case 'i': - config_cpu->iterations++; - break; - case 'd': - config_cpu->iterations--; - break; - } -} - -void mouse_cpu(int button, int state, int x, int y) -{ - if (state == GLUT_DOWN) - { - switch (button) - { - case GLUT_LEFT_BUTTON: - if (config_cpu->speed < 0) - config_cpu->speed = (-1) * config_cpu->speed; - break; - case GLUT_RIGHT_BUTTON: - if (config_cpu->speed > 0) - config_cpu->speed = (-1) * config_cpu->speed; - break; - } - config_cpu->to_x = x_min - + ((d64) x * (x_max - x_min)) / config_cpu->width; - config_cpu->to_y = y_min - + ((d64) y * (y_max - y_min)) / config_cpu->height; - t_old_cpu = glutGet(GLUT_ELAPSED_TIME); - glutIdleFunc(idle_cpu); - } - else if (state == GLUT_UP) - { - glutIdleFunc(idle_cpu_dummy); - } } diff --git a/src/render_cpu.h b/src/render_cpu.h index 0fc75eb..e40a5ea 100644 --- a/src/render_cpu.h +++ b/src/render_cpu.h @@ -31,19 +31,10 @@ typedef struct t_args { } ThreadArgs; config_t *config_cpu; -u32 rendercnt; -float ft; -int t_old_cpu; - -d64 x_min, x_max, y_min, y_max; -d64 x_min_s, x_max_s, y_min_s, y_max_s; void init_cpu(config_t *config); -void render_cpu(void); +void render_cpu(d64 x_min, d64 y_min, d64 x_max, d64 y_max); void idle_cpu(void); -void idle_cpu_dummy(void); -void keyboard_cpu(unsigned char key, int mouseX, int mouseY); -void mouse_cpu(int button, int state, int x, int y); void calculate(d64 x_min, d64 y_min, d64 x_max, d64 y_max, u32 (*sfunc) (d64, d64, u32), u32 *arr); void calculate_t(void *args); diff --git a/src/render_opencl.c b/src/render_opencl.c index 7bc9620..aae7e41 100644 --- a/src/render_opencl.c +++ b/src/render_opencl.c @@ -7,26 +7,14 @@ #include "render_opencl.h" -void idle_opencl_dummy(void) -{ - glutPostRedisplay(); -} - void init_opencl(config_t *cfg) { - x_min_s_cl = -2.0; - x_max_s_cl = 1.0; - y_min_s_cl = -1.0; - y_max_s_cl = 1.0; - x_min_cl = x_min_s_cl; - x_max_cl = x_max_s_cl; - y_min_cl = y_min_s_cl; - y_max_cl = y_max_s_cl; config_opencl = cfg; output = (cl_uint *) malloc((config_opencl->width) * (config_opencl->height) * sizeof(cl_uchar4)); context = NULL; + width_cl = config_opencl->width; // Leave it in, cl needs different endianness cl_platform_id platform_id; @@ -140,7 +128,7 @@ void init_opencl(config_t *cfg) } } -void render_opencl(void) +void render_opencl(d64 x_min, d64 y_min, d64 x_max, d64 y_max) { cl_event events[MAX_DEVICES]; cl_int eventStatus = CL_QUEUED; @@ -149,15 +137,20 @@ void render_opencl(void) size_t kernelWorkGroupSize; cl_kernel kernel; + cl_double x_min_cl = (cl_double) x_min; + cl_double y_min_cl = (cl_double) y_min; + cl_double x_max_cl = (cl_double) x_max; + cl_double y_max_cl = (cl_double) y_max; + cl_double y_max_t; cl_float y_max_t_f; - cl_double x_delta = ((x_max_cl - x_min_cl) / (double) config_opencl->width); - cl_double y_delta = -((y_max_cl - y_min_cl) / (double) config_opencl->height); + cl_double x_delta = ((x_max_cl - x_min_cl) / (cl_double) config_opencl->width); + cl_double y_delta = -((y_max_cl - y_min_cl) / (cl_double) config_opencl->height); cl_float x_delta_f = (float) x_delta; cl_float y_delta_f = (float) y_delta; - cl_float x_min_f = (float) x_min_cl; + cl_float x_min_f = (float) x_min; globalThreads[0] = ((config_opencl->width) * (config_opencl->height)) / num_devices; @@ -176,7 +169,7 @@ void render_opencl(void) localThreads[0] = kernelWorkGroupSize; } - y_max_t = (((y_min_cl + y_max_cl) / 2.0) + (y_max_cl - y_min_cl) / 2.0 - ((double) i * (y_max_cl - y_min_cl)) / (double) num_devices); + y_max_t = (((y_min_cl + y_max_cl) / 2.0) + (y_max_cl - y_min_cl) / 2.0 - ((cl_double) i * (y_max_cl - y_min_cl)) / (cl_double) num_devices); ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &outputBuffer[i]); @@ -185,7 +178,6 @@ void render_opencl(void) //printf("x_delta: %f, y_delta: %f, x_delta_f: %f, y_delta_f: %f, x_min_f: %f, y_max_t: %f, y_max_t_f: %f\n", x_delta, y_delta, x_delta_f, y_delta_f, x_min_f, y_max_t, y_max_t_f); - printf("set args\n"); switch (config_opencl->config_opencl.fpu) { case OPENCL_FPU_32: // lel @@ -241,89 +233,9 @@ void render_opencl(void) ret = clWaitForEvents(1, &events[num_devices - i - 1]); ret = clReleaseEvent(events[num_devices - i - 1]); } - printf("ocl render2\n"); - - glBindTexture(GL_TEXTURE_2D, config_opencl->tex); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_opencl->width, - config_opencl->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_opencl->arr); - glBindTexture(GL_TEXTURE_2D, 0); - - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindTexture(GL_TEXTURE_2D, config_opencl->tex); - glEnable(GL_TEXTURE_2D); - - glBegin(GL_QUADS); - glTexCoord2i(0, 0); glVertex2i(0, 0); - glTexCoord2i(0, 1); glVertex2i(0, config_opencl->height); - glTexCoord2i(1, 1); glVertex2i(config_opencl->width, config_opencl->height); - glTexCoord2i(1, 0); glVertex2i(config_opencl->width, 0); - glEnd(); - - glDisable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, 0); - glutSwapBuffers(); } void idle_opencl(void) { - int t = 0, delta = 0; - do - { - t = glutGet(GLUT_ELAPSED_TIME); - delta = t - t_old_opencl; - } while (delta < 16); // TODO: Hardcoded FPS - t_old_opencl = t; - - //glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, config_opencl->tex); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, config_opencl->width, - config_opencl->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, config_opencl->arr); - glBindTexture(GL_TEXTURE_2D, 0); - cl_ft += (config_opencl->speed * (delta / 1000.0)); - x_min_cl = x_min_s_cl + config_opencl->zoom_func(cl_ft, (cl_double) 2.0 + config_opencl->to_x); - y_min_cl = y_min_s_cl + config_opencl->zoom_func(cl_ft, (cl_double) 1.0 + config_opencl->to_y); - x_max_cl = x_max_s_cl - config_opencl->zoom_func(cl_ft, (cl_double) 1.0 - config_opencl->to_x); - y_max_cl = y_max_s_cl - config_opencl->zoom_func(cl_ft, (cl_double) 1.0 - config_opencl->to_y); - glutPostRedisplay(); -} - -void keyboard_opencl(unsigned char key, int mouseX, int mouseY) -{ - switch (key) - { - case 'i': - config_opencl->iterations++; - break; - case 'd': - config_opencl->iterations--; - break; - } -} -void mouse_opencl(int button, int state, int x, int y) -{ - if (state == GLUT_DOWN) - { - switch (button) - { - case GLUT_LEFT_BUTTON: - if (config_opencl->speed < 0) - config_opencl->speed = (-1) * config_opencl->speed; - break; - case GLUT_RIGHT_BUTTON: - if (config_opencl->speed > 0) - config_opencl->speed = (-1) * config_opencl->speed; - break; - } - config_opencl->to_x = x_min_cl + ((d64) x * (x_max_cl - x_min_cl)) / config_opencl->width; - config_opencl->to_y = y_min_cl + ((d64) y * (y_max_cl - y_min_cl)) / config_opencl->height; - t_old_opencl = glutGet(GLUT_ELAPSED_TIME); - glutIdleFunc(idle_opencl); - } - else if(state == GLUT_UP) - { - glutIdleFunc(idle_opencl_dummy); - } } diff --git a/src/render_opencl.h b/src/render_opencl.h index 51945d5..9bfb4de 100644 --- a/src/render_opencl.h +++ b/src/render_opencl.h @@ -25,10 +25,6 @@ config_t *config_opencl; -cl_double x_min_cl, x_max_cl, y_min_cl, y_max_cl; -cl_double x_min_s_cl, x_max_s_cl, y_min_s_cl, y_max_s_cl; - -float cl_ft; cl_uint *output; cl_device_id device_id; cl_context context; @@ -41,15 +37,10 @@ cl_mem outputBuffer[MAX_DEVICES]; cl_int width_cl; -int t_old_opencl; - d64 zoom_func(d64 ft, d64 s); void init_opencl(config_t *cfg); -void render_opencl(void); +void render_opencl(d64 x_min, d64 y_min, d64 x_max, d64 y_max); void idle_opencl(void); -void idle_opencl_dummy(void); -void keyboard_opencl(unsigned char key, int mouseX, int mouseY); -void mouse_opencl(int button, int state, int x, int y); #endif /* RENDER_OPENCL_H_ */ -- cgit v1.2.1