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_cpu.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 src/render_cpu.c (limited to 'src/render_cpu.c') diff --git a/src/render_cpu.c b/src/render_cpu.c new file mode 100644 index 0000000..9370461 --- /dev/null +++ b/src/render_cpu.c @@ -0,0 +1,86 @@ +/* + * render.c + * + * Created on: 15.01.2018 + * Author: Superleo1810 + */ + +#include "render.h" +#define HAVE_STRUCT_TIMESPEC + +void init_cpu(CpuConfig *config) +{ + config_cpu = config; +} + +void render_cpu(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); + glutSwapBuffers(); +} + +void calculate(d64 x_min, d64 y_min, d64 x_max, d64 y_max, u32 (*sfunc) (d64, d64, u32), u32 *arr) +{ + pthread_t thread; + ThreadArgs *args = (ThreadArgs *) malloc(config_cpu->threads * sizeof(ThreadArgs)); + for(u8 i = 0; i < config_cpu->threads; i++) + { + args[i] = (ThreadArgs) { .tc = config_cpu->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; + d64 x_math, y_math; + u32 iterations; + for (u32 y = (config_cpu->height/_args->tc)*(_args->tid); y < config_cpu->height; y++) + { + for (u32 x = 0; x < _config->width; x++) + { + x_math = _args->x_min + ((d64) x * (_args->x_max - _args->x_min)) / config_cpu->width; + y_math = _args->y_min + ((d64) (config_cpu->height - y) * (_args->y_max - _args->y_min)) / config_cpu->height; + iterations = _args->sfunc(x_math, y_math, config_cpu->iterations); + _args->arr[COORDS(x, y, config_cpu->width)] = (((1<<24)-1)*iterations)/config_cpu->iterations; + } + } +} + +long double zoom_func(d64 ft, d64 s) +{ + return (s - expl(-ft)); +} + +void idle_cpu(void) +{ + calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->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_cpu->width, config_cpu->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_cpu->speed*dt); + x_min = x_min_s + zoom_func(ft, (d64)2.0 + config_cpu->to_x); + y_min = y_min_s + zoom_func(ft, (d64)1.0 + config_cpu->to_y); + x_max = x_max_s - zoom_func(ft, (d64)1.0 - config_cpu->to_x); + y_max = y_max_s - zoom_func(ft, (d64)1.0 - config_cpu->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_cpu.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'src/render_cpu.c') diff --git a/src/render_cpu.c b/src/render_cpu.c index 9370461..f3bf28e 100644 --- a/src/render_cpu.c +++ b/src/render_cpu.c @@ -5,12 +5,23 @@ * Author: Superleo1810 */ -#include "render.h" +#include "render_cpu.h" #define HAVE_STRUCT_TIMESPEC void init_cpu(CpuConfig *config) { config_cpu = 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; + + calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->arr); } void render_cpu(void) @@ -21,9 +32,9 @@ void render_cpu(void) 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); + 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); @@ -50,7 +61,7 @@ void calculate_t(void *args) u32 iterations; for (u32 y = (config_cpu->height/_args->tc)*(_args->tid); y < config_cpu->height; y++) { - for (u32 x = 0; x < _config->width; x++) + for (u32 x = 0; x < config_cpu->width; x++) { x_math = _args->x_min + ((d64) x * (_args->x_max - _args->x_min)) / config_cpu->width; y_math = _args->y_min + ((d64) (config_cpu->height - y) * (_args->y_max - _args->y_min)) / config_cpu->height; @@ -72,7 +83,7 @@ void idle_cpu(void) 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_cpu->width, config_cpu->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, s_arr); + 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); int t = glutGet(GLUT_ELAPSED_TIME); dt = (t - delta) / 1000.0; -- 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_cpu.c | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) (limited to 'src/render_cpu.c') diff --git a/src/render_cpu.c b/src/render_cpu.c index f3bf28e..5065d4d 100644 --- a/src/render_cpu.c +++ b/src/render_cpu.c @@ -11,10 +11,9 @@ void init_cpu(CpuConfig *config) { config_cpu = config; - delta = glutGet(GLUT_ELAPSED_TIME); x_min_s = -2.0; x_max_s = 1.0; - y_min_s= -1.0; + y_min_s = -1.0; y_max_s = 1.0; x_min = x_min_s; x_max = x_max_s; @@ -27,7 +26,7 @@ void init_cpu(CpuConfig *config) void render_cpu(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glBindTexture(GL_TEXTURE_2D, tex); + glBindTexture(GL_TEXTURE_2D, config_cpu->tex); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); @@ -52,6 +51,7 @@ void calculate(d64 x_min, d64 y_min, d64 x_max, d64 y_max, u32 (*sfunc) (d64, d6 pthread_create(&thread, NULL, calculate_t, (void *)&args[i]); } pthread_join(thread, NULL); + free(args); } void calculate_t(void *args) @@ -71,27 +71,29 @@ void calculate_t(void *args) } } -long double zoom_func(d64 ft, d64 s) -{ - return (s - expl(-ft)); -} - void idle_cpu(void) { + static int t_old; + int t = 0, delta = 0; + do + { + t = glutGet(GLUT_ELAPSED_TIME); + delta = t - t_old; + } + while(delta < 16); // TODO: Hardcoded FPS + t_old = t; + calculate(x_min, y_min, x_max, y_max, config_cpu->set_func, config_cpu->arr); //glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, 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); - int t = glutGet(GLUT_ELAPSED_TIME); - dt = (t - delta) / 1000.0; - delta = t; - ft+=(config_cpu->speed*dt); - x_min = x_min_s + zoom_func(ft, (d64)2.0 + config_cpu->to_x); - y_min = y_min_s + zoom_func(ft, (d64)1.0 + config_cpu->to_y); - x_max = x_max_s - zoom_func(ft, (d64)1.0 - config_cpu->to_x); - y_max = y_max_s - zoom_func(ft, (d64)1.0 - config_cpu->to_y); + 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(); } -- cgit v1.2.1