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