ntdll: Use the gettid system call on all Linux platforms.
[wine] / dlls / d3d10core / 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 D3D10CoreCreateDevice(IDXGIFactory *factory, IDXGIAdapter *adapter,
25         UINT flags, void *unknown0, ID3D10Device **device);
26
27 static ID3D10Device *create_device(void)
28 {
29     IDXGIFactory *factory = NULL;
30     IDXGIAdapter *adapter = NULL;
31     ID3D10Device *device = NULL;
32     HRESULT hr;
33
34     hr = CreateDXGIFactory(&IID_IDXGIFactory, (void *)&factory);
35     if (FAILED(hr)) goto cleanup;
36
37     hr = IDXGIFactory_EnumAdapters(factory, 0, &adapter);
38     ok(SUCCEEDED(hr) || hr == DXGI_ERROR_NOT_FOUND, /* Some VMware and VirtualBox */
39             "EnumAdapters failed, hr %#x.\n", hr);
40     if (SUCCEEDED(hr))
41     {
42         hr = D3D10CoreCreateDevice(factory, adapter, 0, NULL, &device);
43     }
44
45     if (FAILED(hr))
46     {
47         HMODULE d3d10ref;
48
49         trace("Failed to create a HW device, trying REF\n");
50         if (adapter) IDXGIAdapter_Release(adapter);
51         adapter = NULL;
52
53         d3d10ref = LoadLibraryA("d3d10ref.dll");
54         if (!d3d10ref)
55         {
56             trace("d3d10ref.dll not available, unable to create a REF device\n");
57             goto cleanup;
58         }
59
60         hr = IDXGIFactory_CreateSoftwareAdapter(factory, d3d10ref, &adapter);
61         FreeLibrary(d3d10ref);
62         ok(SUCCEEDED(hr), "CreateSoftwareAdapter failed, hr %#x\n", hr);
63         if (FAILED(hr)) goto cleanup;
64
65         hr = D3D10CoreCreateDevice(factory, adapter, 0, NULL, &device);
66         ok(SUCCEEDED(hr), "Failed to create a REF device, hr %#x\n", hr);
67         if (FAILED(hr)) goto cleanup;
68     }
69
70 cleanup:
71     if (adapter) IDXGIAdapter_Release(adapter);
72     if (factory) IDXGIFactory_Release(factory);
73
74     return device;
75 }
76
77 static void test_device_interfaces(ID3D10Device *device)
78 {
79     IUnknown *obj;
80     HRESULT hr;
81
82     if (SUCCEEDED(hr = ID3D10Device_QueryInterface(device, &IID_IUnknown, (void **)&obj)))
83         IUnknown_Release(obj);
84     ok(SUCCEEDED(hr), "ID3D10Device does not implement IUnknown\n");
85
86     if (SUCCEEDED(hr = ID3D10Device_QueryInterface(device, &IID_IDXGIObject, (void **)&obj)))
87         IUnknown_Release(obj);
88     ok(SUCCEEDED(hr), "ID3D10Device does not implement IDXGIObject\n");
89
90     if (SUCCEEDED(hr = ID3D10Device_QueryInterface(device, &IID_IDXGIDevice, (void **)&obj)))
91         IUnknown_Release(obj);
92     ok(SUCCEEDED(hr), "ID3D10Device does not implement IDXGIDevice\n");
93
94     if (SUCCEEDED(hr = ID3D10Device_QueryInterface(device, &IID_ID3D10Device, (void **)&obj)))
95         IUnknown_Release(obj);
96     ok(SUCCEEDED(hr), "ID3D10Device does not implement ID3D10Device\n");
97 }
98
99 static void test_create_texture2d(ID3D10Device *device)
100 {
101     D3D10_TEXTURE2D_DESC desc;
102     ID3D10Texture2D *texture;
103     IDXGISurface *surface;
104     HRESULT hr;
105
106     desc.Width = 512;
107     desc.Height = 512;
108     desc.MipLevels = 1;
109     desc.ArraySize = 1;
110     desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
111     desc.SampleDesc.Count = 1;
112     desc.SampleDesc.Quality = 0;
113     desc.Usage = D3D10_USAGE_DEFAULT;
114     desc.BindFlags = D3D10_BIND_RENDER_TARGET;
115     desc.CPUAccessFlags = 0;
116     desc.MiscFlags = 0;
117
118     hr = ID3D10Device_CreateTexture2D(device, &desc, NULL, &texture);
119     ok(SUCCEEDED(hr), "Failed to create a 2d texture, hr %#x\n", hr);
120
121     hr = ID3D10Texture2D_QueryInterface(texture, &IID_IDXGISurface, (void **)&surface);
122     ok(SUCCEEDED(hr), "Texture should implement IDXGISurface\n");
123     if (SUCCEEDED(hr)) IDXGISurface_Release(surface);
124     ID3D10Texture2D_Release(texture);
125
126     desc.MipLevels = 0;
127     hr = ID3D10Device_CreateTexture2D(device, &desc, NULL, &texture);
128     ok(SUCCEEDED(hr), "Failed to create a 2d texture, hr %#x\n", hr);
129
130     hr = ID3D10Texture2D_QueryInterface(texture, &IID_IDXGISurface, (void **)&surface);
131     ok(FAILED(hr), "Texture should not implement IDXGISurface\n");
132     if (SUCCEEDED(hr)) IDXGISurface_Release(surface);
133     ID3D10Texture2D_Release(texture);
134
135     desc.MipLevels = 1;
136     desc.ArraySize = 2;
137     hr = ID3D10Device_CreateTexture2D(device, &desc, NULL, &texture);
138     ok(SUCCEEDED(hr), "Failed to create a 2d texture, hr %#x\n", hr);
139
140     hr = ID3D10Texture2D_QueryInterface(texture, &IID_IDXGISurface, (void **)&surface);
141     ok(FAILED(hr), "Texture should not implement IDXGISurface\n");
142     if (SUCCEEDED(hr)) IDXGISurface_Release(surface);
143     ID3D10Texture2D_Release(texture);
144 }
145
146 static void test_create_texture3d(ID3D10Device *device)
147 {
148     D3D10_TEXTURE3D_DESC desc;
149     ID3D10Texture3D *texture;
150     IDXGISurface *surface;
151     HRESULT hr;
152
153     desc.Width = 64;
154     desc.Height = 64;
155     desc.Depth = 64;
156     desc.MipLevels = 1;
157     desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
158     desc.Usage = D3D10_USAGE_DEFAULT;
159     desc.BindFlags = D3D10_BIND_RENDER_TARGET;
160     desc.CPUAccessFlags = 0;
161     desc.MiscFlags = 0;
162
163     hr = ID3D10Device_CreateTexture3D(device, &desc, NULL, &texture);
164     ok(SUCCEEDED(hr), "Failed to create a 3d texture, hr %#x.\n", hr);
165
166     hr = ID3D10Texture3D_QueryInterface(texture, &IID_IDXGISurface, (void **)&surface);
167     ok(FAILED(hr), "Texture should not implement IDXGISurface.\n");
168     if (SUCCEEDED(hr)) IDXGISurface_Release(surface);
169     ID3D10Texture3D_Release(texture);
170
171     desc.MipLevels = 0;
172     hr = ID3D10Device_CreateTexture3D(device, &desc, NULL, &texture);
173     ok(SUCCEEDED(hr), "Failed to create a 3d texture, hr %#x.\n", hr);
174
175     hr = ID3D10Texture3D_QueryInterface(texture, &IID_IDXGISurface, (void **)&surface);
176     ok(FAILED(hr), "Texture should not implement IDXGISurface.\n");
177     if (SUCCEEDED(hr)) IDXGISurface_Release(surface);
178     ID3D10Texture3D_Release(texture);
179 }
180
181 static void test_create_rendertarget_view(ID3D10Device *device)
182 {
183     D3D10_RENDER_TARGET_VIEW_DESC rtv_desc;
184     D3D10_TEXTURE2D_DESC texture_desc;
185     D3D10_BUFFER_DESC buffer_desc;
186     ID3D10RenderTargetView *rtview;
187     ID3D10Texture2D *texture;
188     ID3D10Buffer *buffer;
189     HRESULT hr;
190
191     buffer_desc.ByteWidth = 1024;
192     buffer_desc.Usage = D3D10_USAGE_DEFAULT;
193     buffer_desc.BindFlags = D3D10_BIND_RENDER_TARGET;
194     buffer_desc.CPUAccessFlags = 0;
195     buffer_desc.MiscFlags = 0;
196
197     hr = ID3D10Device_CreateBuffer(device, &buffer_desc, NULL, &buffer);
198     ok(SUCCEEDED(hr), "Failed to create a buffer, hr %#x\n", hr);
199
200     rtv_desc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
201     rtv_desc.ViewDimension = D3D10_RTV_DIMENSION_BUFFER;
202     U(rtv_desc).Buffer.ElementOffset = 0;
203     U(rtv_desc).Buffer.ElementWidth = 64;
204
205     hr = ID3D10Device_CreateRenderTargetView(device, (ID3D10Resource *)buffer, &rtv_desc, &rtview);
206     ok(SUCCEEDED(hr), "Failed to create a rendertarget view, hr %#x\n", hr);
207
208     ID3D10RenderTargetView_Release(rtview);
209     ID3D10Buffer_Release(buffer);
210
211     texture_desc.Width = 512;
212     texture_desc.Height = 512;
213     texture_desc.MipLevels = 1;
214     texture_desc.ArraySize = 1;
215     texture_desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
216     texture_desc.SampleDesc.Count = 1;
217     texture_desc.SampleDesc.Quality = 0;
218     texture_desc.Usage = D3D10_USAGE_DEFAULT;
219     texture_desc.BindFlags = D3D10_BIND_RENDER_TARGET;
220     texture_desc.CPUAccessFlags = 0;
221     texture_desc.MiscFlags = 0;
222
223     hr = ID3D10Device_CreateTexture2D(device, &texture_desc, NULL, &texture);
224     ok(SUCCEEDED(hr), "Failed to create a 2d texture, hr %#x\n", hr);
225
226     /* For texture resources it's allowed to specify NULL as desc */
227     hr = ID3D10Device_CreateRenderTargetView(device, (ID3D10Resource *)texture, NULL, &rtview);
228     ok(SUCCEEDED(hr), "Failed to create a rendertarget view, hr %#x\n", hr);
229
230     ID3D10RenderTargetView_GetDesc(rtview, &rtv_desc);
231     ok(rtv_desc.Format == texture_desc.Format, "Expected format %#x, got %#x\n", texture_desc.Format, rtv_desc.Format);
232     ok(rtv_desc.ViewDimension == D3D10_RTV_DIMENSION_TEXTURE2D,
233             "Expected view dimension D3D10_RTV_DIMENSION_TEXTURE2D, got %#x\n", rtv_desc.ViewDimension);
234     ok(U(rtv_desc).Texture2D.MipSlice == 0, "Expected mip slice 0, got %#x\n", U(rtv_desc).Texture2D.MipSlice);
235
236     ID3D10RenderTargetView_Release(rtview);
237     ID3D10Texture2D_Release(texture);
238 }
239
240 START_TEST(device)
241 {
242     ID3D10Device *device;
243     ULONG refcount;
244
245     device = create_device();
246     if (!device)
247     {
248         skip("Failed to create device, skipping tests\n");
249         return;
250     }
251
252     test_device_interfaces(device);
253     test_create_texture2d(device);
254     test_create_texture3d(device);
255     test_create_rendertarget_view(device);
256
257     refcount = ID3D10Device_Release(device);
258     ok(!refcount, "Device has %u references left\n", refcount);
259 }