From c734614e5f7a4013955740d27f3f461161ff1b12 Mon Sep 17 00:00:00 2001 From: MitchellHansen Date: Mon, 10 Oct 2016 23:14:45 -0700 Subject: [PATCH] cleaned up an unused file, added check for sharing --- include/CL_Wrapper.h | 16 +++-- include/Map.h | 10 ++-- src/CL_Wrapper.cpp | 85 ++++++++++++++++++++------ src/Map.cpp | 39 ++++-------- src/TestPlatform.cpp | 138 ------------------------------------------- src/main.cpp | 26 +++----- 6 files changed, 104 insertions(+), 210 deletions(-) delete mode 100644 src/TestPlatform.cpp diff --git a/include/CL_Wrapper.h b/include/CL_Wrapper.h index 1cff709..d3b2ac3 100644 --- a/include/CL_Wrapper.h +++ b/include/CL_Wrapper.h @@ -2,10 +2,12 @@ #include #include "util.hpp" #include +#include #ifdef linux #include #include +#include #elif defined _WIN32 #include @@ -35,15 +37,21 @@ public: ~CL_Wrapper(); int acquire_platform_and_device(); - int create_shared_context(); + + int create_shared_context(); + int create_cl_context(); + int create_command_queue(); + int compile_kernel(std::string kernel_source, bool is_path, std::string kernel_name); + int create_buffer(std::string buffer_name, cl_uint size, void* data); int create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags); - int set_kernel_arg(std::string kernel_name, int index, std::string buffer_name); int store_buffer(cl_mem, std::string buffer_name); + + int set_kernel_arg(std::string kernel_name, int index, std::string buffer_name); int run_kernel(std::string kernel_name, const int work_size); - + bool assert(int error_code, std::string function_name); cl_device_id getDeviceID(); @@ -56,7 +64,7 @@ private: int error = 0; bool initialized = false; - + bool cl_khr_gl_sharing_fallback = false; cl_platform_id platform_id; cl_device_id device_id; diff --git a/include/Map.h b/include/Map.h index 6cf46db..c2615ea 100644 --- a/include/Map.h +++ b/include/Map.h @@ -2,16 +2,18 @@ #include #include #include +#include +#include #include #include #include #include -#define _USE_MATH_DEFINES -#include +#include "util.hpp" +#include #include - -#include +#define _USE_MATH_DEFINES +#include #define CHUNK_DIM 32 diff --git a/src/CL_Wrapper.cpp b/src/CL_Wrapper.cpp index b2ba706..356aac4 100644 --- a/src/CL_Wrapper.cpp +++ b/src/CL_Wrapper.cpp @@ -1,12 +1,24 @@ #include "CL_Wrapper.h" CL_Wrapper::CL_Wrapper() { + + acquire_platform_and_device(); + + if (cl_khr_gl_sharing_fallback){ + create_cl_context(); + } else { + create_shared_context(); + } + + create_command_queue(); + } CL_Wrapper::~CL_Wrapper() { } + int CL_Wrapper::acquire_platform_and_device(){ // Get the number of platforms @@ -88,37 +100,72 @@ int CL_Wrapper::acquire_platform_and_device(){ platform_id = current_best_device.platform; device_id = current_best_device.id; + // Test for sharing + size_t ext_str_size = 1024; + char *ext_str = new char[ext_str_size]; + clGetDeviceInfo(device_id, CL_DEVICE_EXTENSIONS, ext_str_size, ext_str, &ext_str_size); + + if (std::string(ext_str).find("cl_khr_gl_sharing") == std::string::npos){ + std::cout << "No support for the cl_khr_gl_sharing extension"; + cl_khr_gl_sharing_fallback = true; + } + + delete ext_str; + return 1; }; + +int CL_Wrapper::create_cl_context() { + + // If the device doesn't support sharing then we have to create a regular context + cl_context_properties context_properties[] = { + CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id + }; + + context = clCreateContext( + context_properties, + 1, + &device_id, + nullptr, nullptr, + &error + ); + + if (assert(error, "clCreateContext")) + return -1; + + return 0; +} + + int CL_Wrapper::create_shared_context() { // Hurray for standards! // Setup the context properties to grab the current GL context #ifdef linux + cl_context_properties context_properties[] = { CL_GL_CONTEXT_KHR, (cl_context_properties)glXGetCurrentContext(), CL_GLX_DISPLAY_KHR, (cl_context_properties)glXGetCurrentDisplay(), - CL_CONTEXT_PLATFORM, (cl_context_properties)platform, + CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, 0 }; #elif defined _WIN32 - // TODO: Clean this up next time I'm on a windows machine - //cl_context_properties context_properties[] = { - // CL_CONTEXT_PLATFORM, (cl_context_properties) platformIds[0], - // CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), - // CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(), - // 0 - //}; HGLRC hGLRC = wglGetCurrentContext(); HDC hDC = wglGetCurrentDC(); - cl_context_properties context_properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC, CL_WGL_HDC_KHR, (cl_context_properties)hDC, 0 }; + cl_context_properties context_properties[] = { + CL_CONTEXT_PLATFORM, (cl_context_properties)platform_id, + CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC, + CL_WGL_HDC_KHR, (cl_context_properties)hDC, + 0 + }; #elif defined TARGET_OS_MAC + CGLContextObj glContext = CGLGetCurrentContext(); CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext); cl_context_properties context_properties[] = { @@ -237,9 +284,9 @@ int CL_Wrapper::set_kernel_arg( int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data, cl_mem_flags flags) { - cl_mem buff = clCreateBuffer( - getContext(), flags, - size, data, &error + cl_mem buff = clCreateBuffer( + getContext(), flags, + size, data, &error ); if (assert(error, "clCreateBuffer")) @@ -253,9 +300,9 @@ int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data, int CL_Wrapper::create_buffer(std::string buffer_name, cl_uint size, void* data) { - cl_mem buff = clCreateBuffer( - getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, - size, data, &error + cl_mem buff = clCreateBuffer( + getContext(), CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, + size, data, &error ); if (assert(error, "clCreateBuffer")) @@ -278,8 +325,8 @@ int CL_Wrapper::run_kernel(std::string kernel_name, const int work_size){ cl_kernel kernel = kernel_map.at(kernel_name); - error = clEnqueueAcquireGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, 0, 0); - if (assert(error, "clEnqueueAcquireGLObjects")) + 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); @@ -294,8 +341,8 @@ int CL_Wrapper::run_kernel(std::string kernel_name, const int work_size){ clFinish(getCommandQueue()); // 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")) + error = clEnqueueReleaseGLObjects(getCommandQueue(), 1, &buffer_map.at("image_buffer"), 0, NULL, NULL); + if (assert(error, "clEnqueueReleaseGLObjects")) return -1; return 1; diff --git a/src/Map.cpp b/src/Map.cpp index 8b5933a..7c024d8 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -1,9 +1,4 @@ -#pragma once #include "Map.h" -#include -#include -#include -#include "util.hpp" Map::Map(sf::Vector3i position) { @@ -136,24 +131,24 @@ void Map::generate_octree() { // ////// for (int y = 0; y < dim.y; y++) { // //// double height = 0; // -// //// z += -x*2 * std::sin(std::sqrt(abs(x*2 - y*2 - 47))) - -// //// (y*2 + 47) * std::sin(std::sqrt(std::abs(y*2 + 47 + x*2 / 2))); +// //// z += -x*2 * std::sin(std::sqrt(abs(x*2 - y*2 - 47))) - +// //// (y*2 + 47) * std::sin(std::sqrt(std::abs(y*2 + 47 + x*2 / 2))); // //// // -// //// //z += x * std::sin(std::sqrt(std::abs(y - x + 1))) * -// //// // std::cos(std::sqrt(std::abs(y + x + 1))) + -// //// // (y + 1) * -// //// // std::cos(std::sqrt(std::abs(y - x + 1))) * -// //// // std::sin(std::sqrt(std::abs(y + x + 1))); +// //// //z += x * std::sin(std::sqrt(std::abs(y - x + 1))) * +// //// // std::cos(std::sqrt(std::abs(y + x + 1))) + +// //// // (y + 1) * +// //// // std::cos(std::sqrt(std::abs(y - x + 1))) * +// //// // std::sin(std::sqrt(std::abs(y + x + 1))); // //// // //// // Pathological -// //// //z += 0.5 + -// //// // (std::pow(std::sin(std::sqrt(100 * std::pow(x/20, 2) + std::pow(y/20, 2))), 2) - 0.5) / +// //// //z += 0.5 + +// //// // (std::pow(std::sin(std::sqrt(100 * std::pow(x/20, 2) + std::pow(y/20, 2))), 2) - 0.5) / // //// // (1 + 0.001 * std::pow(std::pow(x/20, 2) - 2 * x/20 * y/20 + std::pow(y/20, 2), 2)); // // //// // Ackleys -// //// //z += 20 + M_E - -// //// // (20 / (std::pow(M_E, 0.2) * std::sqrt((std::pow(x / 16.0, 2) + std::pow(y / 16.0, 2) + 1) / 2))) - +// //// //z += 20 + M_E - +// //// // (20 / (std::pow(M_E, 0.2) * std::sqrt((std::pow(x / 16.0, 2) + std::pow(y / 16.0, 2) + 1) / 2))) - // //// // std::pow(M_E, 0.5 * std::cos(2 * M_PI * x / 16.0) + cos(2 * M_PI * y / 16.0)); // // //// // @@ -365,18 +360,6 @@ void Map::setVoxel(sf::Vector3i world_position, int val) { } -void Map::moveLight(sf::Vector2f in) { -// -// sf::Vector3f light_spherical = CartToSphere(global_light); -// -// light_spherical.y += in.y; -// light_spherical.x += in.x; -// -// global_light = SphereToCart(light_spherical); -// -// return; -} - void Chunk::set(int type) { for (int i = 0; i < CHUNK_DIM * CHUNK_DIM * CHUNK_DIM; i++) { voxel_data[i] = 0; diff --git a/src/TestPlatform.cpp b/src/TestPlatform.cpp deleted file mode 100644 index e4d046c..0000000 --- a/src/TestPlatform.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#pragma once -#include -#include -#include -#include - -#ifdef linux - -#elif defined _WIN32 -#define CL_USE_DEPRECATED_OPENCL_1_2_APIS -#include -#include -#include - -#elif defined TARGET_OS_MAC -# include -# include -#endif - - - -#ifdef TARGET_OS_MAC -int IsExtensionSupported( - const char* support_str, - const char* ext_string, - size_t ext_buffer_size) { - - size_t offset = 0; - - const char* space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset); - - size_t space_pos = space_substr ? space_substr - ext_string : 0; - - while (space_pos < ext_buffer_size) { - - if( strncmp(support_str, ext_string + offset, space_pos) == 0 ) { - // Device supports requested extension! - printf("Info: Found extension support ā€˜%sā€™!\n", support_str); - return 1; - } - - // Keep searching -- skip to next token string - offset = space_pos + 1; - space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset); - space_pos = space_substr ? space_substr - ext_string : 0; - } - - std::cout << "Warning: Extension not supported " << support_str << std::endl; - return 0; -} -#endif - -inline int test_for_gl_cl_sharing() { - - - int err = 0; - -#if defined (__APPLE__) || defined(MACOSX) - static const char *CL_GL_SHARING_EXT = "cl_APPLE_gl_sharing"; -#else - static const char* CL_GL_SHARING_EXT = "cl_khr_gl_sharing"; -#endif - - cl_uint num_devices; - clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices); - - cl_device_id *devices = (cl_device_id *) calloc(sizeof(cl_device_id), num_devices); - clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); - - // Get string containing supported device extensions - size_t ext_size = 1024; - char *ext_string = (char *) malloc(ext_size); - err = clGetDeviceInfo(devices[0], CL_DEVICE_EXTENSIONS, ext_size, ext_string, &ext_size); - - free(devices); - - // Search for GL support in extension string (space delimited) - //int supported = IsExtensionSupported(CL_GL_SHARING_EXT, ext_string, ext_size); - int supported = 0; - if (supported) { - // Device supports context sharing with OpenGL - printf("Found GL Sharing Support!\n"); - return 1; - } - return -1; -} - - -inline int query_platform_devices() { - - int error = 0; - // Get the number of platforms - cl_uint platform_count = 0; - clGetPlatformIDs(0, nullptr, &platform_count); - - // Fetch the platforms - std::vector platformIds(platform_count); - clGetPlatformIDs(platform_count, platformIds.data(), nullptr); - - - for (unsigned int q = 0; q < platform_count; q++) { - - // From stackoverflow, gets and lists the compute devices - cl_uint num_devices, i; - error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices); - - cl_device_id *devices = (cl_device_id *)calloc(sizeof(cl_device_id), num_devices); - error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); - - char buf[128]; - for (i = 0; i < num_devices; i++) { - clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL); - fprintf(stdout, "Device %s supports ", buf); - - clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, 128, buf, NULL); - fprintf(stdout, "%s\n", buf); - - - //cl_device_type a; - //clGetDeviceInfo(devices[i], CL_DEVICE_TYPE, 128, &a, NULL); - //std::cout << a << std::endl; - - //cl_uint b; - //clGetDeviceInfo(devices[i], CL_DEVICE_MAX_CLOCK_FREQUENCY, 128, &b, NULL); - //std::cout << b << std::endl; - - //cl_uint c; - //clGetDeviceInfo(devices[i], CL_DEVICE_MAX_COMPUTE_UNITS, 128, &c, NULL); - //std::cout << c << std::endl; - - std::cout << devices[i] << std::endl; - } - - free(devices); - } - - return 1; -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 482bee7..6e3f52c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,7 +25,6 @@ #include #endif -#include "TestPlatform.cpp" #include "Map.h" #include "Curses.h" #include "util.hpp" @@ -65,21 +64,14 @@ sf::Texture window_texture; int main() { - + CL_Wrapper c; - Map m(sf::Vector3i (50, 50, 50)); - m.generate_octree(); + //Map m(sf::Vector3i (50, 50, 50)); + //m.generate_octree(); return 1; //sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); - //// Setup CL, instantiate and pass in values to the kernel - //CL_Wrapper c; - //query_platform_devices(); - //c.acquire_platform_and_device(); - //c.create_shared_context(); - //c.create_command_queue(); - //if (c.compile_kernel("../kernels/ray_caster_kernel.cl", true, "min_kern") < 0) { // std::cin.get(); // return -1; @@ -320,15 +312,15 @@ int main() { // // ==== DELTA TIME LOCKED ==== // } - // float l[] = { - // light[9] * sin(delta_time / 1) + light[7] * cos(delta_time / 1), - // light[8], - // light[9] * cos(delta_time / 1) - light[7] * sin(delta_time / 1) + // float l[] = { + // light[9] * sin(delta_time / 1) + light[7] * cos(delta_time / 1), + // light[8], + // light[9] * cos(delta_time / 1) - light[7] * sin(delta_time / 1) // }; // float l2[] = { - // l[0] * cos(delta_time) - l[2] * sin(delta_time), - // l[0] * sin(delta_time) + l[2] * cos(delta_time), + // l[0] * cos(delta_time) - l[2] * sin(delta_time), + // l[0] * sin(delta_time) + l[2] * cos(delta_time), // l[2] // };