[DXGI]
[reactos.git] / reactos / dll / directx / wine / dxgi / 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
20 #include "dxgi_private.h"
21
22 static inline struct dxgi_device *impl_from_IWineDXGIDevice(IWineDXGIDevice *iface)
23 {
24 return CONTAINING_RECORD(iface, struct dxgi_device, IWineDXGIDevice_iface);
25 }
26
27 /* IUnknown methods */
28
29 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
30 {
31 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
32
33 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
34
35 if (IsEqualGUID(riid, &IID_IUnknown)
36 || IsEqualGUID(riid, &IID_IDXGIObject)
37 || IsEqualGUID(riid, &IID_IDXGIDevice)
38 || IsEqualGUID(riid, &IID_IWineDXGIDevice))
39 {
40 IUnknown_AddRef(iface);
41 *object = iface;
42 return S_OK;
43 }
44
45 if (This->child_layer)
46 {
47 TRACE("forwarding to child layer %p\n", This->child_layer);
48 return IUnknown_QueryInterface(This->child_layer, riid, object);
49 }
50
51 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
52
53 *object = NULL;
54 return E_NOINTERFACE;
55 }
56
57 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
58 {
59 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
60 ULONG refcount = InterlockedIncrement(&This->refcount);
61
62 TRACE("%p increasing refcount to %u\n", This, refcount);
63
64 return refcount;
65 }
66
67 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
68 {
69 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
70 ULONG refcount = InterlockedDecrement(&This->refcount);
71
72 TRACE("%p decreasing refcount to %u\n", This, refcount);
73
74 if (!refcount)
75 {
76 if (This->child_layer) IUnknown_Release(This->child_layer);
77 EnterCriticalSection(&dxgi_cs);
78 wined3d_device_decref(This->wined3d_device);
79 LeaveCriticalSection(&dxgi_cs);
80 IWineDXGIFactory_Release(This->factory);
81 HeapFree(GetProcessHeap(), 0, This);
82 }
83
84 return refcount;
85 }
86
87 /* IDXGIObject methods */
88
89 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
90 REFGUID guid, UINT data_size, const void *data)
91 {
92 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
93
94 return E_NOTIMPL;
95 }
96
97 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
98 REFGUID guid, const IUnknown *object)
99 {
100 FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
101
102 return E_NOTIMPL;
103 }
104
105 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
106 REFGUID guid, UINT *data_size, void *data)
107 {
108 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
109
110 return E_NOTIMPL;
111 }
112
113 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
114 {
115 IDXGIAdapter *adapter;
116 HRESULT hr;
117
118 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
119
120 hr = IWineDXGIDevice_GetAdapter(iface, &adapter);
121 if (FAILED(hr))
122 {
123 ERR("Failed to get adapter, hr %#x.\n", hr);
124 return hr;
125 }
126
127 hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
128 IDXGIAdapter_Release(adapter);
129
130 return hr;
131 }
132
133 /* IDXGIDevice methods */
134
135 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
136 {
137 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
138 struct wined3d_device_creation_parameters create_parameters;
139
140 TRACE("iface %p, adapter %p\n", iface, adapter);
141
142 EnterCriticalSection(&dxgi_cs);
143 wined3d_device_get_creation_parameters(This->wined3d_device, &create_parameters);
144 LeaveCriticalSection(&dxgi_cs);
145
146 return IWineDXGIFactory_EnumAdapters(This->factory, create_parameters.adapter_idx, adapter);
147 }
148
149 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
150 const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
151 const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
152 {
153 struct wined3d_device_parent *device_parent;
154 struct wined3d_resource_desc surface_desc;
155 IWineDXGIDeviceParent *dxgi_device_parent;
156 HRESULT hr;
157 UINT i;
158 UINT j;
159
160 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
161 iface, desc, surface_count, usage, shared_resource, surface);
162
163 hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
164 if (FAILED(hr))
165 {
166 ERR("Device should implement IWineD3DDeviceParent\n");
167 return E_FAIL;
168 }
169
170 device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
171
172 FIXME("Implement DXGI<->wined3d usage conversion\n");
173 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
174 surface_desc.format = wined3dformat_from_dxgi_format(desc->Format);
175 surface_desc.multisample_type = desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3D_MULTISAMPLE_NONE;
176 surface_desc.multisample_quality = desc->SampleDesc.Quality;
177 surface_desc.usage = usage;
178 surface_desc.pool = WINED3D_POOL_DEFAULT;
179 surface_desc.width = desc->Width;
180 surface_desc.height = desc->Height;
181 surface_desc.depth = 1;
182 surface_desc.size = 0;
183
184 memset(surface, 0, surface_count * sizeof(*surface));
185 for (i = 0; i < surface_count; ++i)
186 {
187 struct wined3d_surface *wined3d_surface;
188 IUnknown *parent;
189
190 if (FAILED(hr = device_parent->ops->create_swapchain_surface(device_parent,
191 NULL, &surface_desc, &wined3d_surface)))
192 {
193 ERR("Failed to create surface, hr %#x.\n", hr);
194 goto fail;
195 }
196
197 parent = wined3d_surface_get_parent(wined3d_surface);
198 hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
199 wined3d_surface_decref(wined3d_surface);
200 if (FAILED(hr))
201 {
202 ERR("Surface should implement IDXGISurface\n");
203 goto fail;
204 }
205
206 TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
207 }
208 IWineDXGIDeviceParent_Release(dxgi_device_parent);
209
210 return S_OK;
211
212 fail:
213 for (j = 0; j < i; ++j)
214 {
215 IDXGISurface_Release(surface[i]);
216 }
217 IWineDXGIDeviceParent_Release(dxgi_device_parent);
218 return hr;
219 }
220
221 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
222 IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
223 {
224 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
225 iface, resources, residency, resource_count);
226
227 return E_NOTIMPL;
228 }
229
230 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
231 {
232 FIXME("iface %p, priority %d stub!\n", iface, priority);
233
234 return E_NOTIMPL;
235 }
236
237 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
238 {
239 FIXME("iface %p, priority %p stub!\n", iface, priority);
240
241 return E_NOTIMPL;
242 }
243
244 /* IWineDXGIDevice methods */
245
246 static struct wined3d_device * STDMETHODCALLTYPE dxgi_device_get_wined3d_device(IWineDXGIDevice *iface)
247 {
248 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
249
250 TRACE("iface %p\n", iface);
251
252 EnterCriticalSection(&dxgi_cs);
253 wined3d_device_incref(This->wined3d_device);
254 LeaveCriticalSection(&dxgi_cs);
255 return This->wined3d_device;
256 }
257
258 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
259 DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
260 {
261 struct dxgi_surface *object;
262 HRESULT hr;
263
264 FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
265 iface, desc, usage, shared_resource, outer, surface);
266
267 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
268 if (!object)
269 {
270 ERR("Failed to allocate DXGI surface object memory\n");
271 return E_OUTOFMEMORY;
272 }
273
274 hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer);
275 if (FAILED(hr))
276 {
277 WARN("Failed to initialize surface, hr %#x.\n", hr);
278 HeapFree(GetProcessHeap(), 0, object);
279 return hr;
280 }
281
282 TRACE("Created IDXGISurface %p\n", object);
283 *surface = outer ? &object->IUnknown_iface : (IUnknown *)&object->IDXGISurface_iface;
284
285 return S_OK;
286 }
287
288 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
289 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **wined3d_swapchain)
290 {
291 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
292 struct dxgi_swapchain *object;
293 HRESULT hr;
294
295 TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
296 iface, desc, wined3d_swapchain);
297
298 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
299 if (!object)
300 {
301 ERR("Failed to allocate DXGI swapchain object memory\n");
302 return E_OUTOFMEMORY;
303 }
304
305 hr = dxgi_swapchain_init(object, This, desc);
306 if (FAILED(hr))
307 {
308 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
309 HeapFree(GetProcessHeap(), 0, object);
310 return hr;
311 }
312
313 TRACE("Created IDXGISwapChain %p\n", object);
314 *wined3d_swapchain = object->wined3d_swapchain;
315
316 return S_OK;
317 }
318
319 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
320 {
321 /* IUnknown methods */
322 dxgi_device_QueryInterface,
323 dxgi_device_AddRef,
324 dxgi_device_Release,
325 /* IDXGIObject methods */
326 dxgi_device_SetPrivateData,
327 dxgi_device_SetPrivateDataInterface,
328 dxgi_device_GetPrivateData,
329 dxgi_device_GetParent,
330 /* IDXGIDevice methods */
331 dxgi_device_GetAdapter,
332 dxgi_device_CreateSurface,
333 dxgi_device_QueryResourceResidency,
334 dxgi_device_SetGPUThreadPriority,
335 dxgi_device_GetGPUThreadPriority,
336 /* IWineDXGIAdapter methods */
337 dxgi_device_get_wined3d_device,
338 dxgi_device_create_surface,
339 dxgi_device_create_swapchain,
340 };
341
342 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
343 IDXGIFactory *factory, IDXGIAdapter *adapter)
344 {
345 struct wined3d_device_parent *wined3d_device_parent;
346 IWineDXGIDeviceParent *dxgi_device_parent;
347 IWineDXGIAdapter *wine_adapter;
348 UINT adapter_ordinal;
349 struct wined3d *wined3d;
350 void *layer_base;
351 HRESULT hr;
352 WINED3DCAPS caps;
353
354 device->IWineDXGIDevice_iface.lpVtbl = &dxgi_device_vtbl;
355 device->refcount = 1;
356
357 layer_base = device + 1;
358
359 hr = layer->create(layer->id, &layer_base, 0,
360 device, &IID_IUnknown, (void **)&device->child_layer);
361 if (FAILED(hr))
362 {
363 WARN("Failed to create device, returning %#x.\n", hr);
364 goto fail;
365 }
366
367 hr = IDXGIFactory_QueryInterface(factory, &IID_IWineDXGIFactory, (void **)&device->factory);
368 if (FAILED(hr))
369 {
370 WARN("This is not the factory we're looking for, returning %#x.\n", hr);
371 goto fail;
372 }
373 wined3d = IWineDXGIFactory_get_wined3d(device->factory);
374
375 hr = IDXGIAdapter_QueryInterface(adapter, &IID_IWineDXGIAdapter, (void **)&wine_adapter);
376 if (FAILED(hr))
377 {
378 WARN("This is not the adapter we're looking for, returning %#x.\n", hr);
379 EnterCriticalSection(&dxgi_cs);
380 wined3d_decref(wined3d);
381 LeaveCriticalSection(&dxgi_cs);
382 goto fail;
383 }
384 adapter_ordinal = IWineDXGIAdapter_get_ordinal(wine_adapter);
385 IWineDXGIAdapter_Release(wine_adapter);
386
387 hr = IWineDXGIDevice_QueryInterface(&device->IWineDXGIDevice_iface, &IID_IWineDXGIDeviceParent,
388 (void **)&dxgi_device_parent);
389 if (FAILED(hr))
390 {
391 ERR("DXGI device should implement IWineD3DDeviceParent.\n");
392 goto fail;
393 }
394
395 wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
396
397 FIXME("Ignoring adapter type.\n");
398
399 hr = wined3d_get_device_caps(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, &caps);
400 if (FAILED(hr) || caps.VertexShaderVersion < 4 || caps.PixelShaderVersion < 4)
401 {
402 WARN("Direct3D 10 is not supported on this GPU with the current shader backend.\n");
403 if (SUCCEEDED(hr))
404 hr = E_FAIL;
405 goto fail;
406 }
407
408 EnterCriticalSection(&dxgi_cs);
409 hr = wined3d_device_create(wined3d, adapter_ordinal, WINED3D_DEVICE_TYPE_HAL, NULL, 0, 4,
410 wined3d_device_parent, &device->wined3d_device);
411 IWineDXGIDeviceParent_Release(dxgi_device_parent);
412 wined3d_decref(wined3d);
413 LeaveCriticalSection(&dxgi_cs);
414 if (FAILED(hr))
415 {
416 WARN("Failed to create a wined3d device, returning %#x.\n", hr);
417 goto fail;
418 }
419
420 return S_OK;
421
422 fail:
423 if (device->wined3d_device)
424 {
425 EnterCriticalSection(&dxgi_cs);
426 wined3d_device_decref(device->wined3d_device);
427 LeaveCriticalSection(&dxgi_cs);
428 }
429 if (device->factory) IWineDXGIFactory_Release(device->factory);
430 if (device->child_layer) IUnknown_Release(device->child_layer);
431 return hr;
432 }