diff --git a/include/OpenCL.h b/include/OpenCL.h index 687ba3a..8865467 100644 --- a/include/OpenCL.h +++ b/include/OpenCL.h @@ -23,6 +23,7 @@ #include #include #include +#include "Vector4.hpp" struct device { @@ -47,7 +48,7 @@ public: OpenCL(sf::Vector2i resolution); ~OpenCL(); - bool init(); + bool init(sf::Vector4f *range); void run_kernel(std::string kernel_name); @@ -95,9 +96,11 @@ private: // Create a buffer with CL_MEM_READ_ONLY and CL_MEM_COPY_HOST_PTR int create_buffer(std::string buffer_name, cl_uint size, void* data); + // Create a buffer with user defined data access flags int create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags); + // Store a cl_mem object in the buffer map int store_buffer(cl_mem buffer, std::string buffer_name); diff --git a/kernels/mandlebrot.cl b/kernels/mandlebrot.cl index 1441f60..96a8666 100644 --- a/kernels/mandlebrot.cl +++ b/kernels/mandlebrot.cl @@ -21,7 +21,7 @@ __kernel void mandlebrot ( float y = 0.0; int iteration_count = 0; - int interation_threshold = 1000; + int interation_threshold = 2000; while (x*x + y*y < 4 && iteration_count < interation_threshold) { float x_temp = x*x - y*y + x0; @@ -31,8 +31,15 @@ __kernel void mandlebrot ( } int val = scale(iteration_count, 0, 1000, 0, 16777216); + //printf("%i", ((val >> 8) & 0xff)); - write_imagef(image, pixel, (float4)(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, 200)); + float r = scale((val & 0xff), 0, 255, 0, 1); + float g = scale((val >> 8) & 0xff, 0, 255, 0, 1); + float b = scale((val >> 16) & 0xff, 0, 255, 0, 1); + + +// write_imagei(image, pixel, (int4)((val & 0xff), ((val >> 8) & 0xff), ((val >> 16) & 0xff), 200)); + write_imagef(image, pixel, (float4)(r, g, b, 200)); return; diff --git a/src/OpenCL.cpp b/src/OpenCL.cpp index e0f8b0d..a4146f9 100644 --- a/src/OpenCL.cpp +++ b/src/OpenCL.cpp @@ -442,7 +442,8 @@ OpenCL::~OpenCL() { } -bool OpenCL::init() { +bool OpenCL::init(sf::Vector4f *range) +{ // Initialize opencl up to the point where we start assigning buffers aquire_hardware(); @@ -457,9 +458,8 @@ bool OpenCL::init() { create_image_buffer("viewport_image", viewport_texture.getSize().x * viewport_texture.getSize().x * 4 * sizeof(float), &viewport_texture, CL_MEM_WRITE_ONLY); create_buffer("image_res", sizeof(sf::Vector2i), &viewport_resolution); + create_buffer("range", sizeof(sf::Vector4f), range, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR); - sf::Vector4i range(-1.0f, 1.0f, -1.0f, 1.0f); - create_buffer("range", sizeof(sf::Vector4i), &range); set_kernel_arg("mandlebrot", 0, "image_res"); set_kernel_arg("mandlebrot", 1, "viewport_image"); diff --git a/src/main.cpp b/src/main.cpp index d8e491e..d31678e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -79,33 +79,11 @@ int main() { fps_counter fps; OpenCL cl(sf::Vector2i(WINDOW_X, WINDOW_Y)); - cl.init(); + sf::Vector4f range(-1.0f, 1.0f, -1.0f, 1.0f); + cl.init(&range); - - - sf::Uint8 *pixels = new sf::Uint8[WINDOW_X * WINDOW_Y * 4]; - - sf::Sprite viewport_sprite; - sf::Texture viewport_texture; - viewport_texture.create(WINDOW_X, WINDOW_Y); - viewport_texture.update(pixels); - viewport_sprite.setTexture(viewport_texture); - - - std::vector thread_pool; - - for (int i = 0; i < 10; i++) { - thread_pool.emplace_back(std::thread(func, i, 10, pixels)); - } - - for (auto &t: thread_pool) { - t.join(); - } - - viewport_texture.update(pixels); - while (window.isOpen()) { sf::Event event; // Handle input @@ -113,6 +91,36 @@ int main() { if (event.type == sf::Event::Closed) { window.close(); } + if (event.type == sf::Event::KeyPressed) { + if (event.key.code == sf::Keyboard::Down) { + range.z += 0.001; + range.w += 0.001; + } + if (event.key.code == sf::Keyboard::Up) { + range.z -= 0.001; + range.w -= 0.001; + } + if (event.key.code == sf::Keyboard::Right) { + range.x += 0.001; + range.y += 0.001; + } + if (event.key.code == sf::Keyboard::Left) { + range.x -= 0.001; + range.y -= 0.001; + } + if (event.key.code == sf::Keyboard::Equal) { + range.x *= 1.02; + range.y *= 1.02; + range.z *= 1.02; + range.w *= 1.02; + } + if (event.key.code == sf::Keyboard::Dash) { + range.x *= 0.98; + range.y *= 0.98; + range.z *= 0.98; + range.w *= 0.98; + } + } } elapsed_time = elap_time(); // Handle time @@ -130,9 +138,13 @@ int main() { // Do physics at 60fps } + cl.run_kernel("mandlebrot"); + window.clear(sf::Color::White); - window.draw(viewport_sprite); + cl.draw(&window); + + //window.draw(viewport_sprite); fps.draw(&window); fps.frame(delta_time);