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 #if defined( _WIN32 )
17
18 #define _CRT_SECURE_NO_WARNINGS
19 #include <vector>
20 #include <algorithm>
21 #include "harness.h"
22 #include "harness/testHarness.h"
23 #include "harness/parseParameters.h"
24
main(int argc,const char * argv[])25 int main(int argc, const char* argv[])
26 {
27 cl_int result;
28 cl_platform_id platform = NULL;
29 cl_uint num_devices_tested = 0;
30
31 argc = parseCustomParam(argc, argv);
32
33 // get the platforms to test
34 result = clGetPlatformIDs(1, &platform, NULL); NonTestRequire(result == CL_SUCCESS, "Failed to get any platforms.");
35
36 HarnessD3D11_Initialize(platform);
37
38 // for each adapter...
39 IDXGIFactory* pFactory = NULL;
40 HRESULT hr = CreateDXGIFactory(IID_IDXGIFactory, (void**)(&pFactory) );
41 NonTestRequire(SUCCEEDED(hr), "Failed to create DXGI factory.");
42 for (UINT adapter = 0;; ++adapter)
43 {
44 IDXGIAdapter* pAdapter = NULL;
45 ID3D11Device* pDevice = NULL;
46 ID3D11DeviceContext* pDC = NULL;
47 HRESULT hr = pFactory->EnumAdapters(adapter, &pAdapter);
48 if (FAILED(hr))
49 {
50 break;
51 }
52
53 // print data about the adapter
54 DXGI_ADAPTER_DESC desc;
55 hr = pAdapter->GetDesc(&desc);
56 NonTestRequire(SUCCEEDED(hr), "IDXGIAdapter::GetDesc failed.");
57
58 TestPrint("=====================================\n");
59 TestPrint("Testing DXGI Adapter and D3D11 Device\n");
60 TestPrint("Description=%ls, VendorID=%x, DeviceID=%x\n", desc.Description, desc.VendorId, desc.DeviceId);
61 TestPrint("=====================================\n");
62
63 // run the test on the adapter
64 HarnessD3D11_CreateDevice(pAdapter, &pDevice, &pDC);
65
66 cl_uint num_devices = 0;
67
68 // test adapter and device enumeration
69 TestAdapterEnumeration(platform, pAdapter, pDevice, &num_devices);
70
71 // if there were any devices found in enumeration, run the tests on them
72 if (num_devices)
73 {
74 TestAdapterDevices(platform, pAdapter, pDevice, pDC, num_devices);
75 }
76 num_devices_tested += num_devices;
77
78 // destroy the D3D11 device
79 if (pDevice)
80 {
81 HarnessD3D11_DestroyDevice();
82 }
83
84 pAdapter->Release();
85 }
86 pFactory->Release();
87
88 // allow the test to be waived in automation
89 // NonTestRequire(num_devices_tested, "No D3D11 compatible cl_device_ids were found.");
90
91 HarnessD3D11_TestStats();
92 }
93
TestAdapterEnumeration(cl_platform_id platform,IDXGIAdapter * pAdapter,ID3D11Device * pDevice,cl_uint * num_devices)94 void TestAdapterEnumeration(
95 cl_platform_id platform,
96 IDXGIAdapter* pAdapter,
97 ID3D11Device* pDevice,
98 cl_uint* num_devices)
99 {
100 cl_uint num_adapter_devices = 0;
101 cl_device_id* adapter_devices = NULL;
102
103 cl_uint num_device_devices = 0;
104 cl_device_id* device_devices = NULL;
105
106 cl_int result;
107
108 HarnessD3D11_TestBegin("cl_device_id Enumeration");
109
110 // get the cl_device_ids for the adapter
111 {
112 result = clGetDeviceIDsFromD3D11KHR(
113 platform,
114 CL_D3D11_DXGI_ADAPTER_KHR,
115 pAdapter,
116 CL_ALL_DEVICES_FOR_D3D11_KHR,
117 0,
118 NULL,
119 &num_adapter_devices);
120 TestRequire(
121 (result == CL_SUCCESS || result == CL_DEVICE_NOT_FOUND),
122 "clGetDeviceIDsFromD3D11KHR failed.");
123
124 if (result == CL_DEVICE_NOT_FOUND)
125 {
126 TestPrint("No devices found for adapter.\n");
127 }
128 else
129 {
130 // if there were devices, query them
131 adapter_devices = new cl_device_id[num_adapter_devices];
132 result = clGetDeviceIDsFromD3D11KHR(
133 platform,
134 CL_D3D11_DXGI_ADAPTER_KHR,
135 pAdapter,
136 CL_ALL_DEVICES_FOR_D3D11_KHR,
137 num_adapter_devices,
138 adapter_devices,
139 NULL);
140 TestRequire(
141 (result == CL_SUCCESS),
142 "clGetDeviceIDsFromD3D11KHR failed.");
143 }
144 }
145
146 // get the cl_device_ids for the device (if it was successfully created)
147 if (pDevice)
148 {
149 result = clGetDeviceIDsFromD3D11KHR(
150 platform,
151 CL_D3D11_DEVICE_KHR,
152 pDevice,
153 CL_ALL_DEVICES_FOR_D3D11_KHR,
154 0,
155 NULL,
156 &num_device_devices);
157 TestRequire(
158 (result == CL_SUCCESS || result == CL_DEVICE_NOT_FOUND),
159 "clGetDeviceIDsFromD3D11KHR failed.");
160
161 if (result == CL_DEVICE_NOT_FOUND)
162 {
163 TestPrint("No devices found for D3D device.\n");
164 }
165 else
166 {
167 // if there were devices, query them
168 device_devices = new cl_device_id[num_device_devices];
169 result = clGetDeviceIDsFromD3D11KHR(
170 platform,
171 CL_D3D11_DEVICE_KHR,
172 pDevice,
173 CL_ALL_DEVICES_FOR_D3D11_KHR,
174 num_device_devices,
175 device_devices,
176 NULL);
177 TestRequire(
178 (result == CL_SUCCESS),
179 "clGetDeviceIDsFromD3D11KHR failed.");
180 }
181
182 }
183
184 Cleanup:
185
186 if (adapter_devices)
187 {
188 delete[] adapter_devices;
189 }
190 if (device_devices)
191 {
192 delete[] device_devices;
193 }
194
195 *num_devices = num_device_devices;
196
197 HarnessD3D11_TestEnd();
198 }
199
TestAdapterDevices(cl_platform_id platform,IDXGIAdapter * pAdapter,ID3D11Device * pDevice,ID3D11DeviceContext * pDC,cl_uint num_devices_expected)200 void TestAdapterDevices(
201 cl_platform_id platform,
202 IDXGIAdapter* pAdapter,
203 ID3D11Device* pDevice,
204 ID3D11DeviceContext* pDC,
205 cl_uint num_devices_expected)
206 {
207 cl_int result;
208 cl_uint num_devices = 0;
209 cl_device_id* devices = NULL;
210
211 devices = new cl_device_id[num_devices_expected];
212 NonTestRequire(
213 devices,
214 "Memory allocation failure.");
215
216 result = clGetDeviceIDsFromD3D11KHR(
217 platform,
218 CL_D3D11_DEVICE_KHR,
219 pDevice,
220 CL_ALL_DEVICES_FOR_D3D11_KHR,
221 num_devices_expected,
222 devices,
223 &num_devices);
224 NonTestRequire(
225 (result == CL_SUCCESS),
226 "clGetDeviceIDsFromD3D11KHR failed.");
227 NonTestRequire(
228 (num_devices == num_devices_expected),
229 "clGetDeviceIDsFromD3D11KHR returned an unexpected number of devices.");
230
231 for (cl_uint i = 0; i < num_devices; ++i)
232 {
233 TestDevice(devices[i], pDevice, pDC);
234 }
235 }
236
TestDevice(cl_device_id device,ID3D11Device * pDevice,ID3D11DeviceContext * pDC)237 void TestDevice(
238 cl_device_id device,
239 ID3D11Device* pDevice,
240 ID3D11DeviceContext* pDC)
241 {
242 char device_name[1024];
243 cl_int result = CL_SUCCESS;
244 cl_context context = NULL;
245 cl_command_queue command_queue = NULL;
246 ID3D11Device* clDevice = NULL;
247 cl_uint prefer_shared_resources;
248
249 result = clGetDeviceInfo(
250 device,
251 CL_DEVICE_NAME,
252 sizeof(device_name),
253 device_name,
254 NULL);
255 NonTestRequire(CL_SUCCESS == result, "clGetDeviceInfo with CL_DEVICE_NAME failed");
256 TestPrint("--------------------\n");
257 TestPrint("Testing cl_device_id\n");
258 TestPrint("Name=%s\n", device_name);
259 TestPrint("--------------------\n");
260
261 if (!TestDeviceContextCreate(device, pDevice, &context, &command_queue) )
262 {
263 return;
264 }
265
266 // make sure that we can query the shared resource preference
267 result = clGetContextInfo(
268 context,
269 CL_CONTEXT_D3D11_PREFER_SHARED_RESOURCES_KHR,
270 sizeof(prefer_shared_resources),
271 &prefer_shared_resources,
272 NULL);
273 NonTestRequire(CL_SUCCESS == result, "clGetContextInfo with CL_CONTEXT_D3D10_PREFER_SHARED_RESOURCES_KHR failed");
274
275 // run buffer tests
276 TestDeviceBuffer(
277 context,
278 command_queue,
279 pDevice,
280 pDC);
281
282 // run 2D texture tests
283 TestDeviceTexture2D(
284 device,
285 context,
286 command_queue,
287 pDevice,
288 pDC);
289
290 // run 3D texture tests
291 TestDeviceTexture3D(
292 device,
293 context,
294 command_queue,
295 pDevice,
296 pDC);
297
298 // run misc tests
299 TestDeviceMisc(
300 device,
301 context,
302 command_queue,
303 pDevice);
304
305 clReleaseContext(context);
306 clReleaseCommandQueue(command_queue);
307 }
308
TestDeviceContextCreate(cl_device_id device,ID3D11Device * pDevice,cl_context * out_context,cl_command_queue * out_command_queue)309 bool TestDeviceContextCreate(
310 cl_device_id device,
311 ID3D11Device* pDevice,
312 cl_context* out_context,
313 cl_command_queue* out_command_queue)
314 {
315 cl_int result = CL_SUCCESS;
316 cl_context context = NULL;
317 cl_command_queue command_queue = NULL;
318
319 ID3D11Device* clDevice = NULL;
320
321 bool succeeded = false;
322
323 HarnessD3D11_TestBegin("Context creation");
324
325 cl_context_properties properties[5];
326
327 // create the context
328 properties[0] = (cl_context_properties)CL_CONTEXT_D3D11_DEVICE_KHR;
329 properties[1] = (cl_context_properties)pDevice;
330 properties[2] = (cl_context_properties)CL_CONTEXT_INTEROP_USER_SYNC;
331 properties[3] = (cl_context_properties)CL_TRUE;
332 properties[4] = (cl_context_properties)0;
333 context = clCreateContext(
334 properties,
335 1,
336 &device,
337 NULL,
338 NULL,
339 &result);
340 TestRequire(
341 (result == CL_SUCCESS),
342 "clCreateContext with CL_CONTEXT_D3D11_DEVICE_KHR failed");
343 result = clReleaseContext(context);
344 TestRequire(
345 (result == CL_SUCCESS),
346 "clReleaseContext with CL_CONTEXT_D3D11_DEVICE_KHR failed");
347
348 // create the context
349 properties[0] = (cl_context_properties)CL_CONTEXT_D3D11_DEVICE_KHR;
350 properties[1] = (cl_context_properties)pDevice;
351 properties[2] = (cl_context_properties)CL_CONTEXT_INTEROP_USER_SYNC;
352 properties[3] = (cl_context_properties)CL_FALSE;
353 properties[4] = (cl_context_properties)0;
354 context = clCreateContext(
355 properties,
356 1,
357 &device,
358 NULL,
359 NULL,
360 &result);
361 TestRequire(
362 (result == CL_SUCCESS),
363 "clCreateContext with CL_CONTEXT_D3D11_DEVICE_KHR failed");
364 result = clReleaseContext(context);
365 TestRequire(
366 (result == CL_SUCCESS),
367 "clReleaseContext with CL_CONTEXT_D3D11_DEVICE_KHR failed");
368
369 // create the context
370 properties[0] = (cl_context_properties)CL_CONTEXT_D3D11_DEVICE_KHR;
371 properties[1] = (cl_context_properties)pDevice;
372 properties[2] = (cl_context_properties)0;
373 context = clCreateContext(
374 properties,
375 1,
376 &device,
377 NULL,
378 NULL,
379 &result);
380 TestRequire(
381 (result == CL_SUCCESS),
382 "clCreateContext with CL_CONTEXT_D3D11_DEVICE_KHR failed");
383
384 // check CL_CONTEXT_D3D11_DEVICE_KHR
385 {
386 size_t param_value_size_ret;
387 result = clGetContextInfo(context, CL_CONTEXT_PROPERTIES, 0, NULL, ¶m_value_size_ret);
388 TestRequire(
389 (result == CL_SUCCESS),
390 "clGetContextInfo with CL_CONTEXT_PROPERTIES failed");
391
392 TestRequire(
393 ((param_value_size_ret % sizeof(cl_context_properties)) == 0),
394 "param_value_size_ret is not a multiple of sizeof(cl_context_properties)");
395
396 std::vector<cl_context_properties> contextProperties(param_value_size_ret / sizeof(cl_context_properties));
397 result = clGetContextInfo(context, CL_CONTEXT_PROPERTIES, param_value_size_ret, &contextProperties[0], NULL);
398 TestRequire(
399 (result == CL_SUCCESS),
400 "clGetContextInfo with CL_CONTEXT_PROPERTIES failed");
401
402 TestRequire(contextProperties.size() % 2 == 1, "Property list size is not odd.");
403 TestRequire(contextProperties[contextProperties.size() - 1] == 0, "last property is not zero");
404
405 std::vector<cl_context_properties>::const_iterator iter;
406 for (iter = contextProperties.begin(); *iter != 0; iter+=2)
407 {
408 if (CL_CONTEXT_D3D11_DEVICE_KHR == *iter)
409 {
410 TestRequire((ID3D11Device*)*(iter+1) == pDevice, "CL_CONTEXT_D3D11_DEVICE_KHR returned invalid value");
411 break;
412 }
413 }
414
415 TestRequire((iter != contextProperties.end()), "CL_CONTEXT_PROPERTIES doesn't include CL_CONTEXT_D3D11_DEVICE_KHR");
416 }
417
418
419 // create the command queue
420 TestPrint("Creating a command queue.\n");
421 command_queue = clCreateCommandQueueWithProperties(
422 context,
423 device,
424 NULL,
425 &result);
426 TestRequire(
427 (result == CL_SUCCESS),
428 "clCreateContext with CL_CONTEXT_D3D11_DEVICE_KHR failed");
429
430 succeeded = true;
431
432 Cleanup:
433
434 if (succeeded)
435 {
436 *out_context = context;
437 *out_command_queue = command_queue;
438 }
439 else
440 {
441 if (context)
442 {
443 clReleaseContext(context);
444 }
445 if (command_queue)
446 {
447 clReleaseCommandQueue(command_queue);
448 }
449 }
450 HarnessD3D11_TestEnd();
451 return succeeded;
452 }
453
454 #else
455
456 #include "errorHelpers.h"
457
main(int argc,char * argv[])458 int main(int argc, char* argv[])
459 {
460 log_info( "Windows-specific test skipped.\n" );
461 return 0;
462 }
463
464 #endif
465