From 1c3ea27064257d8cf7b9f36b0388b8cf2e94ab08 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Fri, 26 Jan 2018 01:12:49 +0100 Subject: Skeleton for OpenCL rendering, cleaned up alot, WIP and crashing --- src/render.c | 108 +++++++++-------------------------------------------------- 1 file changed, 15 insertions(+), 93 deletions(-) (limited to 'src/render.c') diff --git a/src/render.c b/src/render.c index 55d514b..69b46e0 100644 --- a/src/render.c +++ b/src/render.c @@ -9,28 +9,27 @@ #define HAVE_STRUCT_TIMESPEC #include -void render_init(Config *config, u32 (*sfunc) (long double, long double, u32)) +void init_render(Config *config) { - delta = glutGet(GLUT_ELAPSED_TIME); - 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; - _sfunc = sfunc; s_arr = (u32 *) malloc((_config->width) * (_config->height) * sizeof(u32)); - calculate(x_min, y_min, x_max, y_max, _sfunc, s_arr); //glutInit(0, NULL); glutInitWindowPosition(0, 0); glutInitWindowSize(_config->width, _config->height); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Renderer"); - glutDisplayFunc(gl_render); - glutIdleFunc(gl_idle); + + if (_config->mode) + { + glutDisplayFunc(render_cpu); + glutIdleFunc(idle_cpu); + init_cpu(&_config->config_cpu); + } + else + { + glutDisplayFunc(render_opencl); + glutIdleFunc(idle_opencl); + init_opencl(&_config->config_opencl); + } glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); @@ -46,84 +45,7 @@ void render_init(Config *config, u32 (*sfunc) (long double, long double, u32)) glutMainLoop(); } -void render_show() -{ - -} - -void gl_render(void) -{ - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindTexture(GL_TEXTURE_2D, 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); - -// glBegin(GL_TRIANGLES); -// glVertex3f(-0.5,-0.5,0.0); -// glVertex3f(0.5,0.0,0.0); -// glVertex3f(0.0,0.5,0.0); -// glEnd(); - - glutSwapBuffers(); -} - -void calculate(long double x_min, long double y_min, long double x_max, long double y_max, u32 (*sfunc) (long double, long double, u32), u32 *arr) +void show_render() { - pthread_t thread; - ThreadArgs *args = (ThreadArgs *) malloc(_config->threads * sizeof(ThreadArgs)); - for(u8 i = 0; i < _config->threads; i++) - { - args[i] = (ThreadArgs) { .tc = _config->threads, .tid = i, .x_min = x_min, .y_min = y_min, .x_max = x_max, .y_max = y_max, .sfunc = sfunc, .arr = arr }; - pthread_create(&thread, NULL, calculate_t, (void *)&args[i]); - } - pthread_join(thread, NULL); -} - -void calculate_t(void *args) -{ - ThreadArgs *_args = (ThreadArgs *)args; - long double x_math, y_math; - u32 iterations; - for (u32 y = (_config->height/_args->tc)*(_args->tid); y < _config->height; y++) - { - for (u32 x = 0; x < _config->width; x++) - { - x_math = _args->x_min + ((long double) x * (_args->x_max - _args->x_min)) / _config->width; - y_math = _args->y_min + ((long double) (_config->height - y) * (_args->y_max - _args->y_min)) / _config->height; - iterations = _args->sfunc(x_math, y_math, _config->iterations); - _args->arr[COORDS(x, y, _config->width)] = (((1<<24)-1)*iterations)/_config->iterations; - } - } -} -long double zoom_func(long double ft, long double s) -{ - return (s - expl(-ft)); -} - -void gl_idle(void) -{ - calculate(x_min, y_min, x_max, y_max, _sfunc, s_arr); - //glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, 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, s_arr); - glBindTexture(GL_TEXTURE_2D, 0); - int t = glutGet(GLUT_ELAPSED_TIME); - dt = (t - delta) / 1000.0; - delta = t; - ft+=(_config->speed*dt); - x_min = x_min_s + zoom_func(ft, (long double)2.0 + _config->to_x); - y_min = y_min_s + zoom_func(ft, (long double)1.0 + _config->to_y); - x_max = x_max_s - zoom_func(ft, (long double)1.0 - _config->to_x); - y_max = y_max_s - zoom_func(ft, (long double)1.0 - _config->to_y); - glutPostRedisplay(); } -- cgit v1.2.1 From 3d162bbcc92b22e6b00ee80194214c393770adb7 Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Fri, 26 Jan 2018 15:34:06 +0100 Subject: fixed crash --- src/render.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'src/render.c') diff --git a/src/render.c b/src/render.c index 69b46e0..b9ad5e0 100644 --- a/src/render.c +++ b/src/render.c @@ -11,24 +11,28 @@ void init_render(Config *config) { + _config = config; s_arr = (u32 *) malloc((_config->width) * (_config->height) * sizeof(u32)); + _config->config_cpu.arr = s_arr; + _config->config_opencl.arr = s_arr; //glutInit(0, NULL); glutInitWindowPosition(0, 0); glutInitWindowSize(_config->width, _config->height); glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Renderer"); - if (_config->mode) + switch(_config->mode) { + case MODE_CPU: + init_cpu(&_config->config_cpu); glutDisplayFunc(render_cpu); glutIdleFunc(idle_cpu); - init_cpu(&_config->config_cpu); - } - else - { + break; + case MODE_OPENCL: + init_opencl(&_config->config_opencl); glutDisplayFunc(render_opencl); glutIdleFunc(idle_opencl); - init_opencl(&_config->config_opencl); + break; } glGenTextures(1, &tex); @@ -43,6 +47,7 @@ void init_render(Config *config) glOrtho(0, _config->width, 0, _config->height, -1, 1); glMatrixMode(GL_MODELVIEW); glutMainLoop(); + } void show_render() -- cgit v1.2.1 From c8a38bd8ad66eb0b35f08a4733fdee37a888b83c Mon Sep 17 00:00:00 2001 From: Leonard Kugis Date: Sat, 27 Jan 2018 02:03:31 +0100 Subject: OpenCL rendering added, not working properly --- src/render.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'src/render.c') diff --git a/src/render.c b/src/render.c index b9ad5e0..191dcd9 100644 --- a/src/render.c +++ b/src/render.c @@ -21,6 +21,24 @@ void init_render(Config *config) glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); glutCreateWindow("Renderer"); + glGenTextures(1, &tex); + glBindTexture(GL_TEXTURE_2D, 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, s_arr); + glBindTexture(GL_TEXTURE_2D, 0); + //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _config->width, _config->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, s_arr); + + glMatrixMode(GL_PROJECTION); + glOrtho(0, _config->width, 0, _config->height, -1, 1); + glMatrixMode(GL_MODELVIEW); + + _config->config_cpu.tex = tex; + _config->config_opencl.tex = tex; + _config->config_cpu.zoom_func = zoom_func; + _config->config_opencl.zoom_func = zoom_func; + switch(_config->mode) { case MODE_CPU: @@ -35,17 +53,6 @@ void init_render(Config *config) break; } - glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, 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, s_arr); - glBindTexture(GL_TEXTURE_2D, 0); - //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _config->width, _config->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, s_arr); - - glMatrixMode(GL_PROJECTION); - glOrtho(0, _config->width, 0, _config->height, -1, 1); - glMatrixMode(GL_MODELVIEW); glutMainLoop(); } @@ -54,3 +61,8 @@ void show_render() { } + +d64 zoom_func(d64 ft, d64 s) +{ + return (s - expl(-ft)); +} -- cgit v1.2.1