From fe0fbc04e18b4def7129bed96620304bdc0eb0d5 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Thu, 22 Sep 2016 22:18:30 -0700 Subject: [PATCH] little edit to get rid of some cl boilerplate --- include/util.hpp | 58 +++++++++++++++++++++++++++++----------------- src/CL_Wrapper.cpp | 17 +++++++++++--- src/main.cpp | 13 ----------- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/include/util.hpp b/include/util.hpp index e5bba3b..532ff14 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -10,33 +10,49 @@ const float PI_F = 3.14159265358979f; struct fps_counter { public: - fps_counter(){ - if(!f.loadFromFile("../assets/fonts/Arial.ttf")){ - std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl; - } else { - t.setFont(f); - } + fps_counter(){ + if(!f.loadFromFile("../assets/fonts/Arial.ttf")){ + std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl; + } else { + t.setFont(f); } + } - void frame(double delta_time){ - if (frame_count == 100){ - frame_count = 0; - fps_average = 0; - } - frame_count++; - fps_average += (delta_time - fps_average) / frame_count; + void frame(double delta_time){ + if (frame_count == 100){ + frame_count = 0; + fps_average = 0; } + frame_count++; + fps_average += (delta_time - fps_average) / frame_count; + } + + void flip_units() { + if (milliseconds) + milliseconds = false; + else + milliseconds = true; + } - void draw(sf::RenderWindow *r){ - t.setString(std::to_string(fps_average)); - r->draw(t); - } + void draw(sf::RenderWindow *r){ + + std::string out; + + if (milliseconds) + out = std::to_string(fps_average); + else + out = std::to_string(floor(1 / fps_average)); + + t.setString(out); + r->draw(t); + } private: - sf::Font f; - sf::Text t; - int frame_count = 0; - double fps_average = 0; + bool milliseconds = false; + sf::Font f; + sf::Text t; + int frame_count = 0; + double fps_average = 0; }; struct debug_text { diff --git a/src/CL_Wrapper.cpp b/src/CL_Wrapper.cpp index 4881c96..7fdbe47 100644 --- a/src/CL_Wrapper.cpp +++ b/src/CL_Wrapper.cpp @@ -276,7 +276,11 @@ int CL_Wrapper::run_kernel(std::string kernel_name, const int work_size){ size_t global_work_size[1] = { static_cast(work_size) }; - cl_kernel kernel = kernel_map.at(kernel_name); + cl_kernel kernel = kernel_map.at(kernel_name); + + error = clEnqueueAcquireGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, 0, 0); + if (assert(error, "clEnqueueAcquireGLObjects")) + return -1; //error = clEnqueueTask(command_queue, kernel, 0, NULL, NULL); error = clEnqueueNDRangeKernel( @@ -284,8 +288,15 @@ int CL_Wrapper::run_kernel(std::string kernel_name, const int work_size){ 1, NULL, global_work_size, NULL, 0, NULL, NULL); - if (assert(error, "clEnqueueNDRangeKernel")) - return -1; + if (assert(error, "clEnqueueNDRangeKernel")) + return -1; + + // What if errors out and gl objects are never released? + error = clEnqueueReleaseGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, NULL, NULL); + if (assert(error, "clEnqueueReleaseGLObjects")) + return -1; + + return 1; } diff --git a/src/main.cpp b/src/main.cpp index 505d368..10ab7b2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -311,20 +311,9 @@ int main() { // ==== FPS LOCKED ==== camera.update(delta_time); - // Run the raycast - error = clEnqueueAcquireGLObjects(c.getCommandQueue(), 1, &image_buff, 0, 0, 0); - if (c.assert(error, "clEnqueueAcquireGLObjects")) - return -1; - c.run_kernel("min_kern", WORK_SIZE); - clFinish(c.getCommandQueue()); - - error = clEnqueueReleaseGLObjects(c.getCommandQueue(), 1, &image_buff, 0, NULL, NULL); - if (c.assert(error, "clEnqueueReleaseGLObjects")) - return -1; - // ==== RENDER ==== @@ -342,8 +331,6 @@ int main() { cam_text_mov_x.draw(&window); cam_text_mov_y.draw(&window); cam_text_mov_z.draw(&window); - - //cam_text_z.draw(&window); window.display();