diff options
Diffstat (limited to 'intern/opensubdiv/internal/opensubdiv_device_context_opencl.cc')
-rw-r--r-- | intern/opensubdiv/internal/opensubdiv_device_context_opencl.cc | 165 |
1 files changed, 75 insertions, 90 deletions
diff --git a/intern/opensubdiv/internal/opensubdiv_device_context_opencl.cc b/intern/opensubdiv/internal/opensubdiv_device_context_opencl.cc index f6f1458b189..12f6ced4bbb 100644 --- a/intern/opensubdiv/internal/opensubdiv_device_context_opencl.cc +++ b/intern/opensubdiv/internal/opensubdiv_device_context_opencl.cc @@ -26,26 +26,27 @@ #ifdef OPENSUBDIV_HAS_OPENCL -#if defined(_WIN32) -# include <windows.h> -#elif defined(__APPLE__) -# include <OpenGL/OpenGL.h> -#else -# include <GL/glx.h> -#endif +# if defined(_WIN32) +# include <windows.h> +# elif defined(__APPLE__) +# include <OpenGL/OpenGL.h> +# else +# include <GL/glx.h> +# endif -#include <cstdio> -#include <cstring> +# include <cstdio> +# include <cstring> -#include "internal/opensubdiv_util.h" +# include "internal/opensubdiv_util.h" -#define message(...) // fprintf(stderr, __VA_ARGS__) -#define error(...) fprintf(stderr, __VA_ARGS__) +# define message(...) // fprintf(stderr, __VA_ARGS__) +# define error(...) fprintf(stderr, __VA_ARGS__) namespace { // Returns the first found platform. -cl_platform_id findPlatform() { +cl_platform_id findPlatform() +{ cl_uint num_platforms; cl_int ci_error_number = clGetPlatformIDs(0, NULL, &num_platforms); if (ci_error_number != CL_SUCCESS) { @@ -60,11 +61,8 @@ cl_platform_id findPlatform() { ci_error_number = clGetPlatformIDs(num_platforms, &cl_platform_ids[0], NULL); char ch_buffer[1024]; for (cl_uint i = 0; i < num_platforms; ++i) { - ci_error_number = clGetPlatformInfo(cl_platform_ids[i], - CL_PLATFORM_NAME, - sizeof(ch_buffer), - ch_buffer, - NULL); + ci_error_number = clGetPlatformInfo( + cl_platform_ids[i], CL_PLATFORM_NAME, sizeof(ch_buffer), ch_buffer, NULL); if (ci_error_number == CL_SUCCESS) { cl_platform_id platform_id = cl_platform_ids[i]; return platform_id; @@ -74,20 +72,18 @@ cl_platform_id findPlatform() { } // Return the device in cl_devices which supports the extension. -int findExtensionSupportedDevice(cl_device_id* cl_devices, +int findExtensionSupportedDevice(cl_device_id *cl_devices, int num_devices, - const char* extension_name) { + const char *extension_name) +{ // Find a device that supports sharing with GL/D3D11 // (SLI / X-fire configurations) cl_int cl_error_number; for (int i = 0; i < num_devices; ++i) { // Get extensions string size. size_t extensions_size; - cl_error_number = clGetDeviceInfo(cl_devices[i], - CL_DEVICE_EXTENSIONS, - 0, - NULL, - &extensions_size); + cl_error_number = clGetDeviceInfo( + cl_devices[i], CL_DEVICE_EXTENSIONS, 0, NULL, &extensions_size); if (cl_error_number != CL_SUCCESS) { error("Error %d in clGetDeviceInfo\n", cl_error_number); return -1; @@ -95,11 +91,8 @@ int findExtensionSupportedDevice(cl_device_id* cl_devices, if (extensions_size > 0) { // Get extensions string. string extensions('\0', extensions_size); - cl_error_number = clGetDeviceInfo(cl_devices[i], - CL_DEVICE_EXTENSIONS, - extensions_size, - &extensions[0], - &extensions_size); + cl_error_number = clGetDeviceInfo( + cl_devices[i], CL_DEVICE_EXTENSIONS, extensions_size, &extensions[0], &extensions_size); if (cl_error_number != CL_SUCCESS) { error("Error %d in clGetDeviceInfo\n", cl_error_number); continue; @@ -119,12 +112,12 @@ int findExtensionSupportedDevice(cl_device_id* cl_devices, } // namespace -CLDeviceContext::CLDeviceContext() - : cl_context_(NULL), - cl_command_queue_(NULL) { +CLDeviceContext::CLDeviceContext() : cl_context_(NULL), cl_command_queue_(NULL) +{ } -CLDeviceContext::~CLDeviceContext() { +CLDeviceContext::~CLDeviceContext() +{ if (cl_command_queue_) { clReleaseCommandQueue(cl_command_queue_); } @@ -133,8 +126,9 @@ CLDeviceContext::~CLDeviceContext() { } } -bool CLDeviceContext::HAS_CL_VERSION_1_1() { -#ifdef OPENSUBDIV_HAS_CLEW +bool CLDeviceContext::HAS_CL_VERSION_1_1() +{ +# ifdef OPENSUBDIV_HAS_CLEW static bool clew_initialized = false; static bool clew_load_success; if (!clew_initialized) { @@ -145,55 +139,53 @@ bool CLDeviceContext::HAS_CL_VERSION_1_1() { } } return clew_load_success; -#endif +# endif return true; } -bool CLDeviceContext::Initialize() { -#ifdef OPENSUBDIV_HAS_CLEW +bool CLDeviceContext::Initialize() +{ +# ifdef OPENSUBDIV_HAS_CLEW if (!clGetPlatformIDs) { error("Error clGetPlatformIDs function not bound.\n"); return false; } -#endif +# endif cl_int cl_error_number; cl_platform_id cp_platform = findPlatform(); -#if defined(_WIN32) - cl_context_properties props[] = { - CL_GL_CONTEXT_KHR, - (cl_context_properties)wglGetCurrentContext(), - CL_WGL_HDC_KHR, - (cl_context_properties)wglGetCurrentDC(), - CL_CONTEXT_PLATFORM, - (cl_context_properties)cp_platform, - 0}; -#elif defined(__APPLE__) +# if defined(_WIN32) + cl_context_properties props[] = {CL_GL_CONTEXT_KHR, + (cl_context_properties)wglGetCurrentContext(), + CL_WGL_HDC_KHR, + (cl_context_properties)wglGetCurrentDC(), + CL_CONTEXT_PLATFORM, + (cl_context_properties)cp_platform, + 0}; +# elif defined(__APPLE__) CGLContextObj kCGLContext = CGLGetCurrentContext(); CGLShareGroupObj kCGLShareGroup = CGLGetShareGroup(kCGLContext); - cl_context_properties props[] = {CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, - (cl_context_properties)kCGLShareGroup, - 0}; -#else cl_context_properties props[] = { - CL_GL_CONTEXT_KHR, - (cl_context_properties)glXGetCurrentContext(), - CL_GLX_DISPLAY_KHR, - (cl_context_properties)glXGetCurrentDisplay(), - CL_CONTEXT_PLATFORM, - (cl_context_properties)cp_platform, - 0}; -#endif + CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, (cl_context_properties)kCGLShareGroup, 0}; +# else + cl_context_properties props[] = {CL_GL_CONTEXT_KHR, + (cl_context_properties)glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, + (cl_context_properties)glXGetCurrentDisplay(), + CL_CONTEXT_PLATFORM, + (cl_context_properties)cp_platform, + 0}; +# endif -#if defined(__APPLE__) - _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE, NULL, - &cl_error_number); +# if defined(__APPLE__) + _clContext = clCreateContext(props, 0, NULL, clLogMessagesToStdoutAPPLE, NULL, &cl_error_number); if (cl_error_number != CL_SUCCESS) { error("Error %d in clCreateContext\n", cl_error_number); return false; } size_t devices_size = 0; - clGetGLContextInfoAPPLE(_clContext, kCGLContext, + clGetGLContextInfoAPPLE(_clContext, + kCGLContext, CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE, 0, NULL, @@ -204,13 +196,14 @@ bool CLDeviceContext::Initialize() { return false; } vector<cl_device_id> cl_devices(num_devices); - clGetGLContextInfoAPPLE(_clContext, kCGLContext, + clGetGLContextInfoAPPLE(_clContext, + kCGLContext, CL_CGL_DEVICES_FOR_SUPPORTED_VIRTUAL_SCREENS_APPLE, num_devices * sizeof(cl_device_id), &cl_devices[0], NULL); int cl_device_used = 0; -#else // not __APPLE__ +# else // not __APPLE__ // Get the number of GPU devices available to the platform. cl_uint num_devices = 0; clGetDeviceIDs(cp_platform, CL_DEVICE_TYPE_GPU, 0, NULL, &num_devices); @@ -220,33 +213,22 @@ bool CLDeviceContext::Initialize() { } // Create the device list. vector<cl_device_id> cl_devices(num_devices); - clGetDeviceIDs(cp_platform, - CL_DEVICE_TYPE_GPU, - num_devices, - &cl_devices[0], - NULL); - const char* extension = "cl_khr_gl_sharing"; - int cl_device_used = findExtensionSupportedDevice(&cl_devices[0], - num_devices, - extension); + clGetDeviceIDs(cp_platform, CL_DEVICE_TYPE_GPU, num_devices, &cl_devices[0], NULL); + const char *extension = "cl_khr_gl_sharing"; + int cl_device_used = findExtensionSupportedDevice(&cl_devices[0], num_devices, extension); if (cl_device_used < 0) { error("No device found that supports CL/GL context sharing\n"); return false; } - cl_context_ = clCreateContext(props, - 1, - &cl_devices[cl_device_used], - NULL, NULL, - &cl_error_number); -#endif // not __APPLE__ + cl_context_ = clCreateContext( + props, 1, &cl_devices[cl_device_used], NULL, NULL, &cl_error_number); +# endif // not __APPLE__ if (cl_error_number != CL_SUCCESS) { error("Error %d in clCreateContext\n", cl_error_number); return false; } - cl_command_queue_ = clCreateCommandQueue(cl_context_, - cl_devices[cl_device_used], - 0, - &cl_error_number); + cl_command_queue_ = clCreateCommandQueue( + cl_context_, cl_devices[cl_device_used], 0, &cl_error_number); if (cl_error_number != CL_SUCCESS) { error("Error %d in clCreateCommandQueue\n", cl_error_number); return false; @@ -254,15 +236,18 @@ bool CLDeviceContext::Initialize() { return true; } -bool CLDeviceContext::IsInitialized() const { +bool CLDeviceContext::IsInitialized() const +{ return (cl_context_ != NULL); } -cl_context CLDeviceContext::GetContext() const { +cl_context CLDeviceContext::GetContext() const +{ return cl_context_; } -cl_command_queue CLDeviceContext::GetCommandQueue() const { +cl_command_queue CLDeviceContext::GetCommandQueue() const +{ return cl_command_queue_; } |