Release 1.5.29.
[wine] / dlls / dxgi / tests / device.c
1 /*
2  * Copyright 2008 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #define COBJMACROS
20 #include "initguid.h"
21 #include "d3d10.h"
22 #include "wine/test.h"
23
24 HRESULT WINAPI DXGID3D10CreateDevice(HMODULE d3d10core, IDXGIFactory *factory,
25         IDXGIAdapter *adapter, UINT flags, void *unknown0, void **device);
26
27 static IDXGIDevice *create_device(HMODULE d3d10core)
28 {
29     IDXGIDevice *dxgi_device = NULL;
30     IDXGIFactory *factory = NULL;
31     IDXGIAdapter *adapter = NULL;
32     IUnknown *device = NULL;
33     HRESULT hr;
34
35     hr = CreateDXGIFactory(&IID_IDXGIFactory, (void *)&factory);
36     if (FAILED(hr)) goto cleanup;
37
38     hr = IDXGIFactory_EnumAdapters(factory, 0, &adapter);
39     if (SUCCEEDED(hr))
40     {
41         hr = DXGID3D10CreateDevice(d3d10core, factory, adapter, 0, NULL, (void **)&device);
42     }
43
44     if (FAILED(hr))
45     {
46         HMODULE d3d10ref;
47
48         trace("Failed to create a HW device, trying REF\n");
49         if (adapter) IDXGIAdapter_Release(adapter);
50         adapter = NULL;
51
52         d3d10ref = LoadLibraryA("d3d10ref.dll");
53         if (!d3d10ref)
54         {
55             trace("d3d10ref.dll not available, unable to create a REF device\n");
56             goto cleanup;
57         }
58
59         hr = IDXGIFactory_CreateSoftwareAdapter(factory, d3d10ref, &adapter);
60         FreeLibrary(d3d10ref);
61         ok(SUCCEEDED(hr), "CreateSoftwareAdapter failed, hr %#x\n", hr);
62         if (FAILED(hr)) goto cleanup;
63
64         hr = DXGID3D10CreateDevice(d3d10core, factory, adapter, 0, NULL, (void **)&device);
65         ok(SUCCEEDED(hr), "Failed to create a REF device, hr %#x\n", hr);
66         if (FAILED(hr)) goto cleanup;
67     }
68
69     hr = IUnknown_QueryInterface(device, &IID_IDXGIDevice, (void **)&dxgi_device);
70     ok(SUCCEEDED(hr), "Created device does not implement IDXGIDevice\n");
71     IUnknown_Release(device);
72
73 cleanup:
74     if (adapter) IDXGIAdapter_Release(adapter);
75     if (factory) IDXGIFactory_Release(factory);
76
77     return dxgi_device;
78 }
79
80 static void test_device_interfaces(IDXGIDevice *device)
81 {
82     IUnknown *obj;
83     HRESULT hr;
84
85     if (SUCCEEDED(hr = IDXGIDevice_QueryInterface(device, &IID_IUnknown, (void **)&obj)))
86         IUnknown_Release(obj);
87     ok(SUCCEEDED(hr), "IDXGIDevice does not implement IUnknown\n");
88
89     if (SUCCEEDED(hr = IDXGIDevice_QueryInterface(device, &IID_IDXGIObject, (void **)&obj)))
90         IUnknown_Release(obj);
91     ok(SUCCEEDED(hr), "IDXGIDevice does not implement IDXGIObject\n");
92
93     if (SUCCEEDED(hr = IDXGIDevice_QueryInterface(device, &IID_IDXGIDevice, (void **)&obj)))
94         IUnknown_Release(obj);
95     ok(SUCCEEDED(hr), "IDXGIDevice does not implement IDXGIDevice\n");
96
97     if (SUCCEEDED(hr = IDXGIDevice_QueryInterface(device, &IID_ID3D10Device, (void **)&obj)))
98         IUnknown_Release(obj);
99     ok(SUCCEEDED(hr), "IDXGIDevice does not implement ID3D10Device\n");
100 }
101
102 static void test_adapter_desc(IDXGIDevice *device)
103 {
104     DXGI_ADAPTER_DESC desc;
105     IDXGIAdapter *adapter;
106     HRESULT hr;
107
108     hr = IDXGIDevice_GetAdapter(device, &adapter);
109     ok(SUCCEEDED(hr), "GetAdapter failed, hr %#x.\n", hr);
110
111     hr = IDXGIAdapter_GetDesc(adapter, NULL);
112     ok(hr == E_INVALIDARG, "GetDesc returned %#x, expected %#x.\n",
113             hr, E_INVALIDARG);
114
115     hr = IDXGIAdapter_GetDesc(adapter, &desc);
116     ok(SUCCEEDED(hr), "GetDesc failed, hr %#x.\n", hr);
117
118     trace("%s.\n", wine_dbgstr_w(desc.Description));
119     trace("%04x: %04x:%04x (rev %02x).\n",
120             desc.SubSysId, desc.VendorId, desc.DeviceId, desc.Revision);
121     trace("Dedicated video memory: %lu (%lu MB).\n",
122             desc.DedicatedVideoMemory, desc.DedicatedVideoMemory / (1024 * 1024));
123     trace("Dedicated system memory: %lu (%lu MB).\n",
124             desc.DedicatedSystemMemory, desc.DedicatedSystemMemory / (1024 * 1024));
125     trace("Shared system memory: %lu (%lu MB).\n",
126             desc.SharedSystemMemory, desc.SharedSystemMemory / (1024 * 1024));
127     trace("LUID: %08x:%08x.\n", desc.AdapterLuid.HighPart, desc.AdapterLuid.LowPart);
128
129     IDXGIAdapter_Release(adapter);
130 }
131
132 static void test_create_surface(IDXGIDevice *device)
133 {
134     ID3D10Texture2D *texture;
135     IDXGISurface *surface;
136     DXGI_SURFACE_DESC desc;
137     HRESULT hr;
138
139     desc.Width = 512;
140     desc.Height = 512;
141     desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
142     desc.SampleDesc.Count = 1;
143     desc.SampleDesc.Quality = 0;
144
145     hr = IDXGIDevice_CreateSurface(device, &desc, 1, DXGI_USAGE_RENDER_TARGET_OUTPUT, NULL, &surface);
146     ok(SUCCEEDED(hr), "Failed to create a dxgi surface, hr %#x\n", hr);
147
148     hr = IDXGISurface_QueryInterface(surface, &IID_ID3D10Texture2D, (void **)&texture);
149     ok(SUCCEEDED(hr), "Surface should implement ID3D10Texture2D\n");
150     if (SUCCEEDED(hr)) ID3D10Texture2D_Release(texture);
151
152     IDXGISurface_Release(surface);
153 }
154
155 static void test_parents(IDXGIDevice *device)
156 {
157     DXGI_SURFACE_DESC surface_desc;
158     IDXGISurface *surface;
159     IDXGIFactory *factory;
160     IDXGIAdapter *adapter;
161     IDXGIOutput *output;
162     IUnknown *parent;
163     HRESULT hr;
164
165     surface_desc.Width = 512;
166     surface_desc.Height = 512;
167     surface_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
168     surface_desc.SampleDesc.Count = 1;
169     surface_desc.SampleDesc.Quality = 0;
170
171     hr = IDXGIDevice_CreateSurface(device, &surface_desc, 1, DXGI_USAGE_RENDER_TARGET_OUTPUT, NULL, &surface);
172     ok(SUCCEEDED(hr), "Failed to create a dxgi surface, hr %#x\n", hr);
173
174     hr = IDXGISurface_GetParent(surface, &IID_IDXGIDevice, (void **)&parent);
175     IDXGISurface_Release(surface);
176     ok(SUCCEEDED(hr), "GetParent failed, hr %#x.\n", hr);
177     ok(parent == (IUnknown *)device, "Got parent %p, expected %p.\n", parent, device);
178     IUnknown_Release(parent);
179
180     hr = IDXGIDevice_GetAdapter(device, &adapter);
181     ok(SUCCEEDED(hr), "GetAdapter failed, hr %#x.\n", hr);
182
183     hr = IDXGIAdapter_EnumOutputs(adapter, 0, &output);
184     if (hr == DXGI_ERROR_NOT_FOUND)
185     {
186         skip("Adapter has not outputs, skipping output tests.\n");
187     }
188     else
189     {
190         ok(SUCCEEDED(hr), "EnumOutputs failed, hr %#x.\n", hr);
191
192         hr = IDXGIOutput_GetParent(output, &IID_IDXGIAdapter, (void **)&parent);
193         IDXGIOutput_Release(output);
194         ok(SUCCEEDED(hr), "GetParent failed, hr %#x.\n", hr);
195         ok(parent == (IUnknown *)adapter, "Got parent %p, expected %p.\n", parent, adapter);
196         IUnknown_Release(parent);
197     }
198
199     hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
200     ok(SUCCEEDED(hr), "GetParent failed, hr %#x.\n", hr);
201
202     hr = IDXGIFactory_GetParent(factory, &IID_IUnknown, (void **)&parent);
203     ok(hr == E_NOINTERFACE, "GetParent returned %#x, expected %#x.\n", hr, E_NOINTERFACE);
204     ok(parent == NULL, "Got parent %p, expected %p.\n", parent, NULL);
205     IDXGIFactory_Release(factory);
206
207     hr = IDXGIDevice_GetParent(device, &IID_IDXGIAdapter, (void **)&parent);
208     ok(SUCCEEDED(hr), "GetParent failed, hr %#x.\n", hr);
209     ok(parent == (IUnknown *)adapter, "Got parent %p, expected %p.\n", parent, adapter);
210     IUnknown_Release(parent);
211
212     IDXGIAdapter_Release(adapter);
213 }
214
215 static void test_output(IDXGIDevice *device)
216 {
217     IDXGIAdapter *adapter;
218     HRESULT hr;
219     IDXGIOutput *output;
220     UINT mode_count, mode_count_comp, i;
221     DXGI_MODE_DESC *modes;
222
223     hr = IDXGIDevice_GetAdapter(device, &adapter);
224     ok(SUCCEEDED(hr), "GetAdapter failed, hr %#x.\n", hr);
225
226     hr = IDXGIAdapter_EnumOutputs(adapter, 0, &output);
227     if (hr == DXGI_ERROR_NOT_FOUND)
228     {
229         skip("Adapter has not outputs, skipping output tests.\n");
230         IDXGIAdapter_Release(adapter);
231         return;
232     }
233
234     ok(SUCCEEDED(hr), "EnumOutputs failed, hr %#x.\n", hr);
235
236     IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, 0, NULL, NULL);
237     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
238
239     IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, 0, &mode_count, NULL);
240     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
241     mode_count_comp = mode_count;
242
243     IDXGIOutput_GetDisplayModeList(output, 0, 0, &mode_count, NULL);
244     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
245     ok(!mode_count, "Expected 0 got %d\n", mode_count);
246
247     IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_SCALING, &mode_count, NULL);
248     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
249     ok(mode_count >= mode_count_comp, "Flag implies trying to enumerate more modes\n");
250     mode_count_comp = mode_count;
251
252     modes = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(DXGI_MODE_DESC) * mode_count+10);
253
254     IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_SCALING, NULL, modes);
255     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
256     ok(!modes[0].Height, "No output was expected\n");
257
258     mode_count = 0;
259     IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_SCALING, &mode_count, modes);
260     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
261     ok(!modes[0].Height, "No output was expected\n");
262
263     mode_count = mode_count_comp;
264     IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_SCALING, &mode_count, modes);
265     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
266     ok(mode_count == mode_count_comp, "Expected %d, got %d\n", mode_count_comp, mode_count);
267
268     for (i = 0; i < mode_count; i++)
269     {
270         ok(modes[i].Height && modes[i].Width, "Proper mode was expected\n");
271     }
272
273     mode_count += 5;
274     IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_SCALING, &mode_count, modes);
275     ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
276     ok(mode_count == mode_count_comp, "Expected %d, got %d\n", mode_count_comp, mode_count);
277
278     if (mode_count_comp)
279     {
280         mode_count = mode_count_comp - 1;
281         IDXGIOutput_GetDisplayModeList(output, DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_SCALING, &mode_count, modes);
282         ok(SUCCEEDED(hr), "Failed to list modes, hr %#x.\n", hr);
283         ok(mode_count == mode_count_comp -1, "Expected %d, got %d\n", mode_count_comp, mode_count);
284     }
285     else
286     {
287         skip("Not enough modes for test, skipping\n");
288     }
289
290     HeapFree(GetProcessHeap(), 0, modes);
291     IDXGIOutput_Release(output);
292     IDXGIAdapter_Release(adapter);
293 }
294
295 struct refresh_rates
296 {
297     UINT numerator;
298     UINT denominator;
299     BOOL numerator_should_pass;
300     BOOL denominator_should_pass;
301 };
302
303 static void test_createswapchain(IDXGIDevice *device)
304 {
305     IUnknown *obj;
306     IDXGIAdapter *adapter;
307     IDXGIFactory *factory;
308     IDXGISwapChain *swapchain;
309     DXGI_SWAP_CHAIN_DESC creation_desc, result_desc;
310     HRESULT hr;
311     WNDCLASS wc = {0};
312     UINT i;
313
314     const struct refresh_rates refresh_list[] =
315     {
316         {60, 60, FALSE, FALSE},
317         {60,  0,  TRUE, FALSE},
318         {60,  1,  TRUE,  TRUE},
319         { 0, 60,  TRUE, FALSE},
320         { 0,  0,  TRUE, FALSE},
321     };
322
323
324     wc.lpfnWndProc = DefWindowProc;
325     wc.lpszClassName = "dxgi_test_wc";
326
327     RegisterClass(&wc);
328
329     creation_desc.OutputWindow = 0;
330     creation_desc.BufferDesc.Width = 800;
331     creation_desc.BufferDesc.Height = 600;
332     creation_desc.BufferDesc.RefreshRate.Numerator = 60;
333     creation_desc.BufferDesc.RefreshRate.Denominator = 60;
334     creation_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
335     creation_desc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
336     creation_desc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
337     creation_desc.SampleDesc.Count = 1;
338     creation_desc.SampleDesc.Quality = 0;
339     creation_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
340     creation_desc.BufferCount = 1;
341     creation_desc.OutputWindow = CreateWindow("dxgi_test_wc", "dxgi_test", 0, 0, 0, 0, 0, 0, 0, 0, 0);
342     creation_desc.Windowed = TRUE;
343     creation_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
344     creation_desc.Flags = 0;
345
346     hr = IDXGIDevice_QueryInterface(device, &IID_IUnknown, (void **)&obj);
347     ok(SUCCEEDED(hr), "IDXGIDevice does not implement IUnknown\n");
348
349     hr = IDXGIDevice_GetAdapter(device, &adapter);
350     ok(SUCCEEDED(hr), "GetAdapter failed, hr %#x.\n", hr);
351
352     hr = IDXGIAdapter_GetParent(adapter, &IID_IDXGIFactory, (void **)&factory);
353     ok(SUCCEEDED(hr), "GetParent failed, hr %#x.\n", hr);
354
355     hr = IDXGIFactory_CreateSwapChain(factory, obj, &creation_desc, &swapchain);
356     ok(SUCCEEDED(hr), "CreateSwapChain failed, hr %#x.\n", hr);
357
358     hr = IDXGISwapChain_GetDesc(swapchain, NULL);
359     ok(hr == E_INVALIDARG, "GetDesc unexpectedly returned %#x.\n", hr);
360
361     IDXGISwapChain_Release(swapchain);
362
363     for (i = 0; i < sizeof(refresh_list)/sizeof(refresh_list[0]); i++)
364     {
365         creation_desc.BufferDesc.RefreshRate.Numerator = refresh_list[i].numerator;
366         creation_desc.BufferDesc.RefreshRate.Denominator = refresh_list[i].denominator;
367
368         hr = IDXGIFactory_CreateSwapChain(factory, obj, &creation_desc, &swapchain);
369         ok(SUCCEEDED(hr), "CreateSwapChain failed, hr %#x.\n", hr);
370
371         hr = IDXGISwapChain_GetDesc(swapchain, &result_desc);
372         ok(SUCCEEDED(hr), "GetDesc failed, hr %#x.\n", hr);
373
374         if (refresh_list[i].numerator_should_pass)
375             ok(result_desc.BufferDesc.RefreshRate.Numerator == refresh_list[i].numerator,
376                 "Numerator %u is %u.\n", i, result_desc.BufferDesc.RefreshRate.Numerator);
377         else
378             todo_wine ok(result_desc.BufferDesc.RefreshRate.Numerator == refresh_list[i].numerator,
379                 "Numerator %u is %u.\n", i, result_desc.BufferDesc.RefreshRate.Numerator);
380
381         if (refresh_list[i].denominator_should_pass)
382             ok(result_desc.BufferDesc.RefreshRate.Denominator == refresh_list[i].denominator,
383                     "Denominator %u is %u.\n", i ,result_desc.BufferDesc.RefreshRate.Denominator);
384         else
385             todo_wine ok(result_desc.BufferDesc.RefreshRate.Denominator == refresh_list[i].denominator,
386                     "Denominator %u is %u.\n", i, result_desc.BufferDesc.RefreshRate.Denominator);
387
388         IDXGISwapChain_Release(swapchain);
389     }
390
391     creation_desc.Windowed = FALSE;
392
393     for (i = 0; i < sizeof(refresh_list)/sizeof(refresh_list[0]); i++)
394     {
395         creation_desc.BufferDesc.RefreshRate.Numerator = refresh_list[i].numerator;
396         creation_desc.BufferDesc.RefreshRate.Denominator = refresh_list[i].denominator;
397
398         hr = IDXGIFactory_CreateSwapChain(factory, obj, &creation_desc, &swapchain);
399         ok(SUCCEEDED(hr), "CreateSwapChain failed, hr %#x.\n", hr);
400
401         hr = IDXGISwapChain_GetDesc(swapchain, &result_desc);
402         ok(SUCCEEDED(hr), "GetDesc failed, hr %#x.\n", hr);
403
404         if (refresh_list[i].numerator_should_pass)
405             ok(result_desc.BufferDesc.RefreshRate.Numerator == refresh_list[i].numerator,
406                     "Numerator %u is %u.\n", i, result_desc.BufferDesc.RefreshRate.Numerator);
407         else
408             todo_wine ok(result_desc.BufferDesc.RefreshRate.Numerator == refresh_list[i].numerator,
409                     "Numerator %u is %u.\n", i, result_desc.BufferDesc.RefreshRate.Numerator);
410
411         if (refresh_list[i].denominator_should_pass)
412             ok(result_desc.BufferDesc.RefreshRate.Denominator == refresh_list[i].denominator,
413                     "Denominator %u is %u.\n", i ,result_desc.BufferDesc.RefreshRate.Denominator);
414         else
415             todo_wine ok(result_desc.BufferDesc.RefreshRate.Denominator == refresh_list[i].denominator,
416                     "Denominator %u is %u.\n", i, result_desc.BufferDesc.RefreshRate.Denominator);
417
418         hr = IDXGISwapChain_SetFullscreenState(swapchain, FALSE, NULL);
419         todo_wine ok(SUCCEEDED(hr), "SetFullscreenState failed, hr %#x.\n", hr);
420
421         IDXGISwapChain_Release(swapchain);
422     }
423
424     IDXGIFactory_Release(factory);
425     IDXGIAdapter_Release(adapter);
426     IUnknown_Release(obj);
427 }
428
429 START_TEST(device)
430 {
431     HMODULE d3d10core = LoadLibraryA("d3d10core.dll");
432     IDXGIDevice *device;
433     ULONG refcount;
434
435     if (!d3d10core)
436     {
437         win_skip("d3d10core.dll not available, skipping tests\n");
438         return;
439     }
440
441     device = create_device(d3d10core);
442     if (!device)
443     {
444         skip("Failed to create device, skipping tests\n");
445         FreeLibrary(d3d10core);
446         return;
447     }
448
449     test_adapter_desc(device);
450     test_device_interfaces(device);
451     test_create_surface(device);
452     test_parents(device);
453     test_output(device);
454     test_createswapchain(device);
455
456     refcount = IDXGIDevice_Release(device);
457     ok(!refcount, "Device has %u references left\n", refcount);
458     FreeLibrary(d3d10core);
459 }