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 <CL/cl_ext.h>
25
26 typedef CL_API_ENTRY cl_int (CL_API_CALL *clGetGLContextInfoKHR_fn)(
27 const cl_context_properties *properties,
28 cl_gl_context_info param_name,
29 size_t param_value_size,
30 void *param_value,
31 size_t *param_value_size_ret);
32
33 // Rename references to this dynamically linked function to avoid
34 // collision with static link version
35 #define clGetGLContextInfoKHR clGetGLContextInfoKHR_proc
36 static clGetGLContextInfoKHR_fn clGetGLContextInfoKHR;
37
38 #define MAX_DEVICES 32
39
40 class WGLEnvironment : public GLEnvironment
41 {
42 private:
43 cl_device_id m_devices[MAX_DEVICES];
44 int m_device_count;
45 cl_platform_id m_platform;
46 bool m_is_glut_init;
47
48 public:
WGLEnvironment()49 WGLEnvironment()
50 {
51 m_device_count = 0;
52 m_platform = 0;
53 m_is_glut_init = false;
54 }
Init(int * argc,char ** argv,int use_opengl_32)55 virtual int Init( int *argc, char **argv, int use_opengl_32 )
56 {
57 if (!m_is_glut_init)
58 {
59 // Create a GLUT window to render into
60 glutInit( argc, argv );
61 glutInitWindowSize( 512, 512 );
62 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
63 glutCreateWindow( "OpenCL <-> OpenGL Test" );
64 glewInit();
65 m_is_glut_init = true;
66 }
67 return 0;
68 }
69
CreateCLContext(void)70 virtual cl_context CreateCLContext( void )
71 {
72 HGLRC hGLRC = wglGetCurrentContext();
73 HDC hDC = wglGetCurrentDC();
74 cl_context_properties properties[] = {
75 CL_CONTEXT_PLATFORM, (cl_context_properties) m_platform,
76 CL_GL_CONTEXT_KHR, (cl_context_properties) hGLRC,
77 CL_WGL_HDC_KHR, (cl_context_properties) hDC,
78 0
79 };
80 cl_device_id devices[MAX_DEVICES];
81 size_t dev_size;
82 cl_int status;
83
84 if (!hGLRC || !hDC) {
85 print_error(CL_INVALID_CONTEXT, "No GL context bound");
86 return 0;
87 }
88
89 if (!clGetGLContextInfoKHR) {
90 // As OpenCL for the platforms. Warn if more than one platform found,
91 // since this might not be the platform we want. By default, we simply
92 // use the first returned platform.
93
94 cl_uint nplatforms;
95 cl_platform_id platform;
96 clGetPlatformIDs(0, NULL, &nplatforms);
97 clGetPlatformIDs(1, &platform, NULL);
98
99 if (nplatforms > 1) {
100 log_info("clGetPlatformIDs returned multiple values. This is not "
101 "an error, but might result in obtaining incorrect function "
102 "pointers if you do not want the first returned platform.\n");
103
104 // Show them the platform name, in case it is a problem.
105
106 size_t size;
107 char *name;
108
109 clGetPlatformInfo(platform, CL_PLATFORM_NAME, 0, NULL, &size);
110 name = (char*)malloc(size);
111 clGetPlatformInfo(platform, CL_PLATFORM_NAME, size, name, NULL);
112
113 log_info("Using platform with name: %s \n", name);
114 free(name);
115 }
116
117 clGetGLContextInfoKHR = (clGetGLContextInfoKHR_fn) clGetExtensionFunctionAddressForPlatform(platform, "clGetGLContextInfoKHR");
118 if (!clGetGLContextInfoKHR) {
119 print_error(CL_INVALID_PLATFORM, "Failed to query proc address for clGetGLContextInfoKHR");
120 }
121 }
122
123 status = clGetGLContextInfoKHR(properties,
124 CL_DEVICES_FOR_GL_CONTEXT_KHR,
125 sizeof(devices),
126 devices,
127 &dev_size);
128 if (status != CL_SUCCESS) {
129 print_error(status, "clGetGLContextInfoKHR failed");
130 return 0;
131 }
132 dev_size /= sizeof(cl_device_id);
133 log_info("GL context supports %d compute devices\n", dev_size);
134
135 status = clGetGLContextInfoKHR(properties,
136 CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
137 sizeof(devices),
138 devices,
139 &dev_size);
140 if (status != CL_SUCCESS) {
141 print_error(status, "clGetGLContextInfoKHR failed");
142 return 0;
143 }
144
145 cl_device_id ctxDevice = m_devices[0];
146 if (dev_size > 0) {
147 log_info("GL context current device: 0x%x\n", devices[0]);
148 for (int i = 0; i < m_device_count; i++) {
149 if (m_devices[i] == devices[0]) {
150 ctxDevice = devices[0];
151 break;
152 }
153 }
154 } else {
155 log_info("GL context current device is not a CL device, using device %d.\n", ctxDevice);
156 }
157
158 return clCreateContext(properties, 1, &ctxDevice, NULL, NULL, &status);
159 }
160
SupportsCLGLInterop(cl_device_type device_type)161 virtual int SupportsCLGLInterop( cl_device_type device_type )
162 {
163 cl_device_id devices[MAX_DEVICES];
164 cl_uint num_of_devices;
165 int error;
166 error = clGetPlatformIDs(1, &m_platform, NULL);
167 if (error) {
168 print_error(error, "clGetPlatformIDs failed");
169 return -1;
170 }
171 error = clGetDeviceIDs(m_platform, device_type, MAX_DEVICES, devices, &num_of_devices);
172 if (error) {
173 print_error(error, "clGetDeviceIDs failed");
174 return -1;
175 }
176
177 // Check all devices, search for one that supports cl_khr_gl_sharing
178 for (int i=0; i<(int)num_of_devices; i++) {
179 if (!is_extension_available(devices[i], "cl_khr_gl_sharing")) {
180 log_info("Device %d of %d does not support required extension cl_khr_gl_sharing.\n", i+1, num_of_devices);
181 } else {
182 log_info("Device %d of %d supports required extension cl_khr_gl_sharing.\n", i+1, num_of_devices);
183 m_devices[m_device_count++] = devices[i];
184 }
185 }
186 return m_device_count > 0;
187 }
188
~WGLEnvironment()189 virtual ~WGLEnvironment()
190 {
191 }
192 };
193
Instance(void)194 GLEnvironment * GLEnvironment::Instance( void )
195 {
196 static WGLEnvironment * env = NULL;
197 if( env == NULL )
198 env = new WGLEnvironment();
199 return env;
200 }
201