• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (c) 2017 The Khronos Group Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //    http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 #define GL_GLEXT_PROTOTYPES
17 
18 #include "setup.h"
19 #include "testBase.h"
20 #include "harness/errorHelpers.h"
21 
22 #include <GL/gl.h>
23 #include <GL/glut.h>
24 #include <GL/glext.h>
25 #include <GL/freeglut.h>
26 #include <GL/glx.h>
27 #include <CL/cl_ext.h>
28 
29 class X11GLEnvironment : public GLEnvironment
30 {
31 private:
32     cl_device_id m_devices[64];
33     cl_uint m_device_count;
34 
35 public:
X11GLEnvironment()36     X11GLEnvironment()
37     {
38         m_device_count = 0;
39     }
Init(int * argc,char ** argv,int use_opencl_32)40     virtual int Init( int *argc, char **argv, int use_opencl_32 )
41     {
42          // Create a GLUT window to render into
43         glutInit( argc, argv );
44         glutInitWindowSize( 512, 512 );
45         glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
46         glutCreateWindow( "OpenCL <-> OpenGL Test" );
47         glewInit();
48         return 0;
49     }
50 
CreateCLContext(void)51     virtual cl_context CreateCLContext( void )
52     {
53         GLXContext context = glXGetCurrentContext();
54         Display *dpy = glXGetCurrentDisplay();
55 
56         cl_context_properties properties[] = {
57             CL_GL_CONTEXT_KHR,  (cl_context_properties) context,
58             CL_GLX_DISPLAY_KHR, (cl_context_properties) dpy,
59             0
60         };
61         cl_int status;
62 
63         if (!context || !dpy) {
64             print_error(CL_INVALID_CONTEXT, "No GL context bound");
65             return 0;
66         }
67 
68         return clCreateContext(properties, 1, m_devices, NULL, NULL, &status);
69     }
70 
SupportsCLGLInterop(cl_device_type device_type)71     virtual int SupportsCLGLInterop( cl_device_type device_type )
72     {
73         int found_valid_device = 0;
74         cl_platform_id platform;
75         cl_device_id devices[64];
76         cl_uint num_of_devices;
77         int error;
78         error = clGetPlatformIDs(1, &platform, NULL);
79         if (error) {
80             print_error(error, "clGetPlatformIDs failed");
81             return -1;
82         }
83         error = clGetDeviceIDs(platform, device_type, 64, devices, &num_of_devices);
84         // If this platform doesn't have any of the requested device_type (namely GPUs) then return 0
85         if (error == CL_DEVICE_NOT_FOUND)
86           return 0;
87         if (error) {
88             print_error(error, "clGetDeviceIDs failed");
89             return -1;
90         }
91 
92         for (int i=0; i<(int)num_of_devices; i++) {
93             if (!is_extension_available(devices[i], "cl_khr_gl_sharing ")) {
94                 log_info("Device %d of %d does not support required extension cl_khr_gl_sharing.\n", i+1, num_of_devices);
95             } else {
96                 log_info("Device %d of %d supports required extension cl_khr_gl_sharing.\n", i+1, num_of_devices);
97                 found_valid_device = 1;
98                 m_devices[m_device_count++] = devices[i];
99             }
100         }
101         return found_valid_device;
102     }
103 
~X11GLEnvironment()104     virtual ~X11GLEnvironment()
105     {
106     }
107 };
108 
Instance(void)109 GLEnvironment * GLEnvironment::Instance( void )
110 {
111     static X11GLEnvironment * env = NULL;
112     if( env == NULL )
113         env = new X11GLEnvironment();
114     return env;
115 }
116