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 #include "harness/compat.h"
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #if !defined (__APPLE__)
22 #include <CL/cl.h>
23 #endif
24
25 #include "procs.h"
26 #include "gl/setup.h"
27 #include "harness/testHarness.h"
28 #include "harness/parseParameters.h"
29
30 #if !defined(_WIN32)
31 #include <unistd.h>
32 #endif
33
34 static cl_context sCurrentContext = NULL;
35
36
37 #define TEST_FN_REDIRECT( fn ) ADD_TEST( redirect_##fn )
38 #define TEST_FN_REDIRECTOR( fn ) \
39 int test_redirect_##fn(cl_device_id device, cl_context context, cl_command_queue queue, int numElements ) \
40 { \
41 int error; \
42 clCommandQueueWrapper realQueue = clCreateCommandQueueWithProperties( sCurrentContext, device, 0, &error ); \
43 test_error( error, "Unable to create command queue" ); \
44 return test_##fn( device, sCurrentContext, realQueue, numElements ); \
45 }
46
47 // buffers:
48 TEST_FN_REDIRECTOR( buffers )
49 TEST_FN_REDIRECTOR( buffers_getinfo )
50
51 // 1D images:
52 TEST_FN_REDIRECTOR( images_read_1D )
53 TEST_FN_REDIRECTOR( images_write_1D )
54 TEST_FN_REDIRECTOR( images_1D_getinfo )
55
56 // 1D image arrays:
57 TEST_FN_REDIRECTOR( images_read_1Darray )
58 TEST_FN_REDIRECTOR( images_write_1Darray )
59 TEST_FN_REDIRECTOR( images_1Darray_getinfo )
60
61 // 2D images:
62 TEST_FN_REDIRECTOR( images_read_2D )
63 TEST_FN_REDIRECTOR( images_read_cube )
64 TEST_FN_REDIRECTOR( images_write )
65 TEST_FN_REDIRECTOR( images_write_cube )
66 TEST_FN_REDIRECTOR( images_2D_getinfo )
67 TEST_FN_REDIRECTOR( images_cube_getinfo )
68
69 // 2D image arrays:
70 TEST_FN_REDIRECTOR( images_read_2Darray )
71 TEST_FN_REDIRECTOR( images_write_2Darray )
72 TEST_FN_REDIRECTOR( images_2Darray_getinfo )
73
74 // 3D images:
75 TEST_FN_REDIRECTOR( images_read_3D )
76 TEST_FN_REDIRECTOR( images_write_3D )
77 TEST_FN_REDIRECTOR( images_3D_getinfo )
78
79 #ifdef GL_VERSION_3_2
80
81 TEST_FN_REDIRECTOR( images_read_texturebuffer )
82 TEST_FN_REDIRECTOR( images_write_texturebuffer )
83 TEST_FN_REDIRECTOR( images_texturebuffer_getinfo )
84
85 // depth textures
86 TEST_FN_REDIRECTOR( images_read_2D_depth )
87 TEST_FN_REDIRECTOR( images_write_2D_depth )
88 TEST_FN_REDIRECTOR( images_read_2Darray_depth )
89 TEST_FN_REDIRECTOR( images_write_2Darray_depth )
90
91 TEST_FN_REDIRECTOR( images_read_2D_multisample )
92 TEST_FN_REDIRECTOR( images_read_2Darray_multisample )
93 TEST_FN_REDIRECTOR( image_methods_depth )
94 TEST_FN_REDIRECTOR( image_methods_multisample )
95 #endif
96
97 // Renderbuffer-backed images:
98 TEST_FN_REDIRECTOR( renderbuffer_read )
99 TEST_FN_REDIRECTOR( renderbuffer_write )
100 TEST_FN_REDIRECTOR( renderbuffer_getinfo )
101
102 TEST_FN_REDIRECTOR( fence_sync )
103
104 test_definition test_list[] = {
105 TEST_FN_REDIRECT( buffers ),
106 TEST_FN_REDIRECT( buffers_getinfo ),
107
108 TEST_FN_REDIRECT( images_read_1D ),
109 TEST_FN_REDIRECT( images_write_1D ),
110 TEST_FN_REDIRECT( images_1D_getinfo ),
111
112 TEST_FN_REDIRECT( images_read_1Darray ),
113 TEST_FN_REDIRECT( images_write_1Darray ),
114 TEST_FN_REDIRECT( images_1Darray_getinfo ),
115
116 TEST_FN_REDIRECT( images_read_2D ),
117 TEST_FN_REDIRECT( images_write ),
118 TEST_FN_REDIRECT( images_2D_getinfo ),
119
120 TEST_FN_REDIRECT( images_read_cube ),
121 TEST_FN_REDIRECT( images_write_cube ),
122 TEST_FN_REDIRECT( images_cube_getinfo ),
123
124 TEST_FN_REDIRECT( images_read_2Darray ),
125 TEST_FN_REDIRECT( images_write_2Darray),
126 TEST_FN_REDIRECT( images_2Darray_getinfo ),
127
128 TEST_FN_REDIRECT( images_read_3D ),
129 TEST_FN_REDIRECT( images_write_3D ),
130 TEST_FN_REDIRECT( images_3D_getinfo ),
131
132 TEST_FN_REDIRECT( renderbuffer_read ),
133 TEST_FN_REDIRECT( renderbuffer_write ),
134 TEST_FN_REDIRECT( renderbuffer_getinfo )
135 };
136
137 test_definition test_list32[] = {
138 TEST_FN_REDIRECT( images_read_texturebuffer ),
139 TEST_FN_REDIRECT( images_write_texturebuffer ),
140 TEST_FN_REDIRECT( images_texturebuffer_getinfo ),
141
142 TEST_FN_REDIRECT( fence_sync ),
143 TEST_FN_REDIRECT( images_read_2D_depth ),
144 TEST_FN_REDIRECT( images_write_2D_depth ),
145 TEST_FN_REDIRECT( images_read_2Darray_depth ),
146 TEST_FN_REDIRECT( images_write_2Darray_depth ),
147 TEST_FN_REDIRECT( images_read_2D_multisample ),
148 TEST_FN_REDIRECT( images_read_2Darray_multisample ),
149 TEST_FN_REDIRECT( image_methods_depth ),
150 TEST_FN_REDIRECT( image_methods_multisample )
151 };
152
153 const int test_num = ARRAY_SIZE( test_list );
154 const int test_num32 = ARRAY_SIZE( test_list32 );
155
main(int argc,const char * argv[])156 int main(int argc, const char *argv[])
157 {
158 gTestRounding = true;
159 int error = 0;
160 int numErrors = 0;
161
162 test_start();
163 argc = parseCustomParam(argc, argv);
164 if (argc == -1)
165 {
166 return -1;
167 }
168
169 cl_device_type requestedDeviceType = CL_DEVICE_TYPE_DEFAULT;
170
171 /* Do we have a CPU/GPU specification? */
172 if( argc > 1 )
173 {
174 if( strcmp( argv[ argc - 1 ], "gpu" ) == 0 || strcmp( argv[ argc - 1 ], "CL_DEVICE_TYPE_GPU" ) == 0 )
175 {
176 requestedDeviceType = CL_DEVICE_TYPE_GPU;
177 argc--;
178 }
179 else if( strcmp( argv[ argc - 1 ], "cpu" ) == 0 || strcmp( argv[ argc - 1 ], "CL_DEVICE_TYPE_CPU" ) == 0 )
180 {
181 requestedDeviceType = CL_DEVICE_TYPE_CPU;
182 argc--;
183 }
184 else if( strcmp( argv[ argc - 1 ], "accelerator" ) == 0 || strcmp( argv[ argc - 1 ], "CL_DEVICE_TYPE_ACCELERATOR" ) == 0 )
185 {
186 requestedDeviceType = CL_DEVICE_TYPE_ACCELERATOR;
187 argc--;
188 }
189 else if( strcmp( argv[ argc - 1 ], "CL_DEVICE_TYPE_DEFAULT" ) == 0 )
190 {
191 requestedDeviceType = CL_DEVICE_TYPE_DEFAULT;
192 argc--;
193 }
194 }
195
196 if( argc > 1 && strcmp( argv[ 1 ], "-list" ) == 0 )
197 {
198 log_info( "Available 2.x tests:\n" );
199 for( int i = 0; i < test_num; i++ )
200 log_info( "\t%s\n", test_list[i].name );
201
202 log_info( "Available 3.2 tests:\n" );
203 for( int i = 0; i < test_num32; i++ )
204 log_info( "\t%s\n", test_list32[i].name );
205
206 log_info( "Note: Any 3.2 test names must follow 2.1 test names on the command line.\n" );
207 log_info( "Use environment variables to specify desired device.\n" );
208
209 return 0;
210 }
211
212 // Check to see if any 2.x or 3.2 test names were specified on the command line.
213 unsigned first_32_testname = 0;
214
215 for (int j=1; (j<argc) && (!first_32_testname); ++j)
216 for (int i = 0; i < test_num32; ++i)
217 if (strcmp(test_list32[i].name, argv[j]) == 0) {
218 first_32_testname = j;
219 break;
220 }
221
222 // Create the environment for the test.
223 GLEnvironment *glEnv = GLEnvironment::Instance();
224
225 // Check if any devices of the requested type support CL/GL interop.
226 int supported = glEnv->SupportsCLGLInterop( requestedDeviceType );
227 if( supported == 0 ) {
228 log_info("Test not run because GL-CL interop is not supported for any devices of the requested type.\n");
229 return 0;
230 } else if ( supported == -1 ) {
231 log_error("Unable to setup the test or failed to determine if CL-GL interop is supported.\n");
232 return -1;
233 }
234
235 // Initialize function pointers.
236 error = init_clgl_ext();
237 if (error < 0) {
238 return error;
239 }
240
241 // OpenGL tests for non-3.2 ////////////////////////////////////////////////////////
242 if ((argc == 1) || (first_32_testname != 1)) {
243
244 // At least one device supports CL-GL interop, so init the test.
245 if( glEnv->Init( &argc, (char **)argv, CL_FALSE ) ) {
246 log_error("Failed to initialize the GL environment for this test.\n");
247 return -1;
248 }
249
250 // Create a context to use and then grab a device (or devices) from it
251 sCurrentContext = glEnv->CreateCLContext();
252 if( sCurrentContext == NULL )
253 {
254 log_error( "ERROR: Unable to obtain CL context from GL\n" );
255 return -1;
256 }
257
258 size_t numDevices = 0;
259 cl_device_id *deviceIDs;
260
261 error = clGetContextInfo( sCurrentContext, CL_CONTEXT_DEVICES, 0, NULL, &numDevices);
262 if( error != CL_SUCCESS )
263 {
264 print_error( error, "Unable to get device count from context" );
265 return -1;
266 }
267 deviceIDs = (cl_device_id *)malloc(numDevices);
268 if (deviceIDs == NULL) {
269 print_error( error, "malloc failed" );
270 return -1;
271 }
272 error = clGetContextInfo( sCurrentContext, CL_CONTEXT_DEVICES, numDevices, deviceIDs, NULL);
273 if( error != CL_SUCCESS ) {
274 print_error( error, "Unable to get device list from context" );
275 return -1;
276 }
277
278 numDevices /= sizeof(cl_device_id);
279
280 if (numDevices < 1) {
281 log_error("No devices found.\n");
282 return -1;
283 }
284
285 // Execute tests.
286 int argc_ = (first_32_testname) ? first_32_testname : argc;
287
288 for( size_t i = 0; i < numDevices; i++ ) {
289 log_info( "\nTesting OpenGL 2.x\n" );
290 if( printDeviceHeader( deviceIDs[ i ] ) != CL_SUCCESS ) {
291 return -1;
292 }
293
294 // Note: don't use the entire harness, because we have a different way of obtaining the device (via the context)
295 error = parseAndCallCommandLineTests( argc_, argv, deviceIDs[i], test_num, test_list, true, 0, 1024 );
296 if( error != 0 )
297 break;
298 }
299
300 numErrors += error;
301
302 // Clean-up.
303 free(deviceIDs);
304 clReleaseContext( sCurrentContext );
305 //delete glEnv;
306 }
307
308 // OpenGL 3.2 tests. ////////////////////////////////////////////////////////
309 if ((argc==1) || first_32_testname) {
310
311 // At least one device supports CL-GL interop, so init the test.
312 if( glEnv->Init( &argc, (char **)argv, CL_TRUE ) ) {
313 log_error("Failed to initialize the GL environment for this test.\n");
314 return -1;
315 }
316
317 // Create a context to use and then grab a device (or devices) from it
318 sCurrentContext = glEnv->CreateCLContext();
319 if( sCurrentContext == NULL ) {
320 log_error( "ERROR: Unable to obtain CL context from GL\n" );
321 return -1;
322 }
323
324 size_t numDevices = 0;
325 cl_device_id *deviceIDs;
326
327 error = clGetContextInfo( sCurrentContext, CL_CONTEXT_DEVICES, 0, NULL, &numDevices);
328 if( error != CL_SUCCESS ) {
329 print_error( error, "Unable to get device count from context" );
330 return -1;
331 }
332 deviceIDs = (cl_device_id *)malloc(numDevices);
333 if (deviceIDs == NULL) {
334 print_error( error, "malloc failed" );
335 return -1;
336 }
337 error = clGetContextInfo( sCurrentContext, CL_CONTEXT_DEVICES, numDevices, deviceIDs, NULL);
338 if( error != CL_SUCCESS ) {
339 print_error( error, "Unable to get device list from context" );
340 return -1;
341 }
342
343 numDevices /= sizeof(cl_device_id);
344
345 if (numDevices < 1) {
346 log_error("No devices found.\n");
347 return -1;
348 }
349
350 int argc_ = (first_32_testname) ? 1 + (argc - first_32_testname) : argc;
351 const char** argv_ = (first_32_testname) ? &argv[first_32_testname-1] : argv;
352
353 // Execute the tests.
354 for( size_t i = 0; i < numDevices; i++ ) {
355 log_info( "\nTesting OpenGL 3.2\n" );
356 if( printDeviceHeader( deviceIDs[ i ] ) != CL_SUCCESS ) {
357 return -1;
358 }
359
360 // Note: don't use the entire harness, because we have a different way of obtaining the device (via the context)
361 error = parseAndCallCommandLineTests( argc_, argv_, deviceIDs[i], test_num32, test_list32, true, 0, 1024 );
362 if( error != 0 )
363 break;
364 }
365
366 numErrors += error;
367
368 // Clean-up.
369 free(deviceIDs);
370 clReleaseContext( sCurrentContext );
371 delete glEnv;
372
373 }
374
375 //All done.
376 return numErrors;
377 }
378
379