This commit is contained in:
Mario Hüttel 2019-09-30 22:29:42 +02:00
parent 4d26053a70
commit 49201ea8cf
2 changed files with 55 additions and 21 deletions

View File

@ -10,8 +10,8 @@ pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
pkg_check_modules(CAIRO REQUIRED cairo) pkg_check_modules(CAIRO REQUIRED cairo)
find_package(OpenCL REQUIRED) find_package(OpenCL REQUIRED)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
add_subdirectory(glade) add_subdirectory(glade)
@ -34,4 +34,3 @@ message(STATUS "OpenCL found: ${OPENCL_FOUND}")
message(STATUS "OpenCL includes: ${OPENCL_INCLUDE_DIRS}") message(STATUS "OpenCL includes: ${OPENCL_INCLUDE_DIRS}")
message(STATUS "OpenCL CXX includes: ${OPENCL_HAS_CPP_BINDINGS}") message(STATUS "OpenCL CXX includes: ${OPENCL_HAS_CPP_BINDINGS}")
message(STATUS "OpenCL libraries: ${OPENCL_LIBRARIES}") message(STATUS "OpenCL libraries: ${OPENCL_LIBRARIES}")
message(STATUS "OpenGL libraries: ${OPENGL_gl_LIBRARIES}")

View File

@ -30,7 +30,6 @@ static const char * const orange_framgment_shader = " \
FragColor = texture(ourTexture, texCoord);\n \ FragColor = texture(ourTexture, texCoord);\n \
}"; }";
struct canvas_buffer { struct canvas_buffer {
unsigned char *mandelbrot_buffer; unsigned char *mandelbrot_buffer;
unsigned int iterations; unsigned int iterations;
@ -48,13 +47,13 @@ static gboolean on_main_window_close(GtkWidget *window, gpointer data)
return FALSE; return FALSE;
} }
#define check_error(error) \ void check_error(cl_int error)
do { \ {
if (error != CL_SUCCESS) { \ if (error != CL_SUCCESS) {
printf("OpenCL call failed with error %d. %s:%d\n", (int)error, __func__, __LINE__); \ printf("OpenCL call failed with error %d\n", (int)error);
exit(error); \ exit(error);
} } while(0) }
}
void load_kernel_from_file(char *file, char **src) void load_kernel_from_file(char *file, char **src)
{ {
@ -84,6 +83,51 @@ cl_program create_program(char *src, cl_context context)
return program; return program;
} }
unsigned int *data_array = NULL;
static gboolean drawing_callback(GtkWidget *widget, cairo_t *cr, gpointer data)
{
struct canvas_buffer *buff = (struct canvas_buffer *)data;
int width;
int height;
GtkStyleContext *style_context;
int x, y;
double color_scale;
style_context = gtk_widget_get_style_context(widget);
width = gtk_widget_get_allocated_width(widget);
height = gtk_widget_get_allocated_height(widget);
cairo_save(cr);
/* Drawing code start */
cairo_scale(cr, 1, -1);
cairo_translate(cr, (double)width/2.0, -(double)height/2.0);
for (x = 0; x < buff->width; x++) {
for (y = 0; y < buff->height; y++) {
cairo_rectangle(cr,
((double)x - (double)(buff->width - 1) / 2),
((double)y - (double)(buff->height - 1) / 2),
1,
1);
if (data_array[y * buff->width + x] == buff->iterations) {
cairo_set_source_rgb (cr, 0, 0, 0);
} else {
color_scale = (double)data_array[y * buff->width + x] / buff->iterations;
cairo_set_source_rgb (cr, (sqrt(1-color_scale)), (color_scale*color_scale), color_scale);
}
cairo_fill(cr);
}
}
/* Drawing code end */
cairo_restore(cr);
return FALSE;
}
static int calculate_mandelbrot(struct canvas_buffer *buff) static int calculate_mandelbrot(struct canvas_buffer *buff)
{ {
@ -189,8 +233,6 @@ static int calculate_mandelbrot(struct canvas_buffer *buff)
clEnqueueReadBuffer(queue, dat_buff, CL_TRUE, 0, data_size*3, buff->mandelbrot_buffer, 0, NULL, NULL); clEnqueueReadBuffer(queue, dat_buff, CL_TRUE, 0, data_size*3, buff->mandelbrot_buffer, 0, NULL, NULL);
printf("Calculation finished!\n");
ret = clFlush(queue); ret = clFlush(queue);
ret = clFinish(queue); ret = clFinish(queue);
ret = clReleaseKernel(kernel); ret = clReleaseKernel(kernel);
@ -355,14 +397,7 @@ int main(int argc, char **argv)
mandelbrot_buff.height = 3000; mandelbrot_buff.height = 3000;
mandelbrot_buff.iterations = 200; mandelbrot_buff.iterations = 200;
mandelbrot_buff.mandelbrot_buffer = NULL; mandelbrot_buff.mandelbrot_buffer = NULL;
//calculate_mandelbrot(&mandelbrot_buff); calculate_mandelbrot(&mandelbrot_buff);
gtk_gl_area_make_current(glarea);
GError *error = NULL;
g_signal_connect(glarea, "render", G_CALLBACK(render), NULL);