Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:01

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
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 /// Creates a shared OpenCL/OpenGL context for the currently active
0035 /// OpenGL context.
0036 ///
0037 /// Once created, the shared context can be used to create OpenCL memory
0038 /// objects which can interact with OpenGL memory objects (e.g. VBOs).
0039 ///
0040 /// \throws unsupported_extension_error if no CL-GL sharing capable devices
0041 ///         are found.
0042 inline context opengl_create_shared_context()
0043 {
0044     // name of the OpenGL sharing extension for the system
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     // get OpenGL share group
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         // check whether this platform supports OpenCL/OpenGL sharing
0079         if (!platform.supports_extension(cl_gl_sharing_extension))
0080           continue;
0081 
0082         // load clGetGLContextInfoKHR() extension function
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         // create context properties listing the platform and current OpenGL display
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         // lookup current OpenCL device for current OpenGL context
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         // create device object for the GPU and ensure it supports CL-GL sharing
0120         device gpu(gpu_id, false);
0121         if(!gpu.supports_extension(cl_gl_sharing_extension)){
0122             continue;
0123         }
0124 
0125         // return CL-GL sharing context
0126         return context(gpu, properties);
0127     }
0128 #endif
0129 
0130     // no CL-GL sharing capable devices found
0131     BOOST_THROW_EXCEPTION(
0132         unsupported_extension_error(cl_gl_sharing_extension)
0133     );
0134 }
0135 
0136 } // end compute namespace
0137 } // end boost namespace
0138 
0139 #endif // BOOST_COMPUTE_INTEROP_OPENGL_CONTEXT_HPP