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