File indexing completed on 2025-01-18 09:30:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_INTEROP_OPENGL_CONTEXT_HPP
0012 #define BOOST_COMPUTE_INTEROP_OPENGL_CONTEXT_HPP
0013
0014 #include <boost/throw_exception.hpp>
0015
0016 #include <boost/compute/device.hpp>
0017 #include <boost/compute/system.hpp>
0018 #include <boost/compute/context.hpp>
0019 #include <boost/compute/exception/unsupported_extension_error.hpp>
0020 #include <boost/compute/interop/opengl/cl_gl.hpp>
0021
0022 #ifdef __APPLE__
0023 #include <OpenCL/cl_gl_ext.h>
0024 #include <OpenGL/OpenGL.h>
0025 #endif
0026
0027 #ifdef __linux__
0028 #include <GL/glx.h>
0029 #endif
0030
0031 namespace boost {
0032 namespace compute {
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 inline context opengl_create_shared_context()
0043 {
0044
0045 #if defined(__APPLE__)
0046 const char *cl_gl_sharing_extension = "cl_APPLE_gl_sharing";
0047 #else
0048 const char *cl_gl_sharing_extension = "cl_khr_gl_sharing";
0049 #endif
0050
0051 #if defined(__APPLE__)
0052
0053 CGLContextObj cgl_current_context = CGLGetCurrentContext();
0054 CGLShareGroupObj cgl_share_group = CGLGetShareGroup(cgl_current_context);
0055
0056 cl_context_properties properties[] = {
0057 CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE,
0058 (cl_context_properties) cgl_share_group,
0059 0
0060 };
0061
0062 cl_int error = 0;
0063 cl_context cl_gl_context = clCreateContext(properties, 0, 0, 0, 0, &error);
0064 if(!cl_gl_context){
0065 BOOST_THROW_EXCEPTION(opencl_error(error));
0066 }
0067
0068 return context(cl_gl_context, false);
0069 #else
0070 typedef cl_int(*GetGLContextInfoKHRFunction)(
0071 const cl_context_properties*, cl_gl_context_info, size_t, void *, size_t *
0072 );
0073
0074 std::vector<platform> platforms = system::platforms();
0075 for(size_t i = 0; i < platforms.size(); i++){
0076 const platform &platform = platforms[i];
0077
0078
0079 if (!platform.supports_extension(cl_gl_sharing_extension))
0080 continue;
0081
0082
0083 GetGLContextInfoKHRFunction GetGLContextInfoKHR =
0084 reinterpret_cast<GetGLContextInfoKHRFunction>(
0085 reinterpret_cast<size_t>(
0086 platform.get_extension_function_address("clGetGLContextInfoKHR")
0087 )
0088 );
0089 if(!GetGLContextInfoKHR){
0090 continue;
0091 }
0092
0093
0094 cl_context_properties properties[] = {
0095 CL_CONTEXT_PLATFORM, (cl_context_properties) platform.id(),
0096 #if defined(__linux__)
0097 CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(),
0098 CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(),
0099 #elif defined(_WIN32)
0100 CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(),
0101 CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(),
0102 #endif
0103 0
0104 };
0105
0106
0107 cl_device_id gpu_id;
0108 cl_int ret = GetGLContextInfoKHR(
0109 properties,
0110 CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
0111 sizeof(cl_device_id),
0112 &gpu_id,
0113 0
0114 );
0115 if(ret != CL_SUCCESS){
0116 continue;
0117 }
0118
0119
0120 device gpu(gpu_id, false);
0121 if(!gpu.supports_extension(cl_gl_sharing_extension)){
0122 continue;
0123 }
0124
0125
0126 return context(gpu, properties);
0127 }
0128 #endif
0129
0130
0131 BOOST_THROW_EXCEPTION(
0132 unsupported_extension_error(cl_gl_sharing_extension)
0133 );
0134 }
0135
0136 }
0137 }
0138
0139 #endif