dxgi: Fix a comment typo.
[wine/multimedia.git] / dlls / dxgi / device.c
bloba6f72cae375260350a5dd2d2e3413676179d213a
1 /*
2 * Copyright 2008 Henri Verbeet for CodeWeavers
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.
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.
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
20 #include "config.h"
21 #include "wine/port.h"
23 #include "dxgi_private.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(dxgi);
27 static inline struct dxgi_device *impl_from_IWineDXGIDevice(IWineDXGIDevice *iface)
29 return CONTAINING_RECORD(iface, struct dxgi_device, IWineDXGIDevice_iface);
32 /* IUnknown methods */
34 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryInterface(IWineDXGIDevice *iface, REFIID riid, void **object)
36 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
38 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
40 if (IsEqualGUID(riid, &IID_IUnknown)
41 || IsEqualGUID(riid, &IID_IDXGIObject)
42 || IsEqualGUID(riid, &IID_IDXGIDevice)
43 || IsEqualGUID(riid, &IID_IWineDXGIDevice))
45 IUnknown_AddRef(iface);
46 *object = iface;
47 return S_OK;
50 if (This->child_layer)
52 TRACE("forwarding to child layer %p\n", This->child_layer);
53 return IUnknown_QueryInterface(This->child_layer, riid, object);
56 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
58 *object = NULL;
59 return E_NOINTERFACE;
62 static ULONG STDMETHODCALLTYPE dxgi_device_AddRef(IWineDXGIDevice *iface)
64 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
65 ULONG refcount = InterlockedIncrement(&This->refcount);
67 TRACE("%p increasing refcount to %u\n", This, refcount);
69 return refcount;
72 static ULONG STDMETHODCALLTYPE dxgi_device_Release(IWineDXGIDevice *iface)
74 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
75 ULONG refcount = InterlockedDecrement(&This->refcount);
77 TRACE("%p decreasing refcount to %u\n", This, refcount);
79 if (!refcount)
81 if (This->child_layer) IUnknown_Release(This->child_layer);
82 EnterCriticalSection(&dxgi_cs);
83 wined3d_device_uninit_3d(This->wined3d_device);
84 wined3d_device_decref(This->wined3d_device);
85 LeaveCriticalSection(&dxgi_cs);
86 IDXGIFactory1_Release(This->factory);
87 HeapFree(GetProcessHeap(), 0, This);
90 return refcount;
93 /* IDXGIObject methods */
95 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateData(IWineDXGIDevice *iface,
96 REFGUID guid, UINT data_size, const void *data)
98 FIXME("iface %p, guid %s, data_size %u, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
100 return E_NOTIMPL;
103 static HRESULT STDMETHODCALLTYPE dxgi_device_SetPrivateDataInterface(IWineDXGIDevice *iface,
104 REFGUID guid, const IUnknown *object)
106 FIXME("iface %p, guid %s, object %p stub!\n", iface, debugstr_guid(guid), object);
108 return E_NOTIMPL;
111 static HRESULT STDMETHODCALLTYPE dxgi_device_GetPrivateData(IWineDXGIDevice *iface,
112 REFGUID guid, UINT *data_size, void *data)
114 FIXME("iface %p, guid %s, data_size %p, data %p stub!\n", iface, debugstr_guid(guid), data_size, data);
116 return E_NOTIMPL;
119 static HRESULT STDMETHODCALLTYPE dxgi_device_GetParent(IWineDXGIDevice *iface, REFIID riid, void **parent)
121 IDXGIAdapter *adapter;
122 HRESULT hr;
124 TRACE("iface %p, riid %s, parent %p.\n", iface, debugstr_guid(riid), parent);
126 hr = IWineDXGIDevice_GetAdapter(iface, &adapter);
127 if (FAILED(hr))
129 ERR("Failed to get adapter, hr %#x.\n", hr);
130 return hr;
133 hr = IDXGIAdapter_QueryInterface(adapter, riid, parent);
134 IDXGIAdapter_Release(adapter);
136 return hr;
139 /* IDXGIDevice methods */
141 static HRESULT STDMETHODCALLTYPE dxgi_device_GetAdapter(IWineDXGIDevice *iface, IDXGIAdapter **adapter)
143 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
144 struct wined3d_device_creation_parameters create_parameters;
146 TRACE("iface %p, adapter %p\n", iface, adapter);
148 EnterCriticalSection(&dxgi_cs);
149 wined3d_device_get_creation_parameters(This->wined3d_device, &create_parameters);
150 LeaveCriticalSection(&dxgi_cs);
152 return IDXGIFactory1_EnumAdapters(This->factory, create_parameters.adapter_idx, adapter);
155 static HRESULT STDMETHODCALLTYPE dxgi_device_CreateSurface(IWineDXGIDevice *iface,
156 const DXGI_SURFACE_DESC *desc, UINT surface_count, DXGI_USAGE usage,
157 const DXGI_SHARED_RESOURCE *shared_resource, IDXGISurface **surface)
159 struct wined3d_device_parent *device_parent;
160 struct wined3d_resource_desc surface_desc;
161 IWineDXGIDeviceParent *dxgi_device_parent;
162 HRESULT hr;
163 UINT i;
164 UINT j;
166 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
167 iface, desc, surface_count, usage, shared_resource, surface);
169 hr = IWineDXGIDevice_QueryInterface(iface, &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent);
170 if (FAILED(hr))
172 ERR("Device should implement IWineD3DDeviceParent\n");
173 return E_FAIL;
176 device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
178 FIXME("Implement DXGI<->wined3d usage conversion\n");
179 surface_desc.resource_type = WINED3D_RTYPE_SURFACE;
180 surface_desc.format = wined3dformat_from_dxgi_format(desc->Format);
181 surface_desc.multisample_type = desc->SampleDesc.Count > 1 ? desc->SampleDesc.Count : WINED3D_MULTISAMPLE_NONE;
182 surface_desc.multisample_quality = desc->SampleDesc.Quality;
183 surface_desc.usage = usage;
184 surface_desc.pool = WINED3D_POOL_DEFAULT;
185 surface_desc.width = desc->Width;
186 surface_desc.height = desc->Height;
187 surface_desc.depth = 1;
188 surface_desc.size = 0;
190 memset(surface, 0, surface_count * sizeof(*surface));
191 for (i = 0; i < surface_count; ++i)
193 struct wined3d_surface *wined3d_surface;
194 IUnknown *parent;
196 if (FAILED(hr = device_parent->ops->create_swapchain_surface(device_parent,
197 NULL, &surface_desc, &wined3d_surface)))
199 ERR("Failed to create surface, hr %#x.\n", hr);
200 goto fail;
203 parent = wined3d_surface_get_parent(wined3d_surface);
204 hr = IUnknown_QueryInterface(parent, &IID_IDXGISurface, (void **)&surface[i]);
205 wined3d_surface_decref(wined3d_surface);
206 if (FAILED(hr))
208 ERR("Surface should implement IDXGISurface\n");
209 goto fail;
212 TRACE("Created IDXGISurface %p (%u/%u)\n", surface[i], i + 1, surface_count);
214 IWineDXGIDeviceParent_Release(dxgi_device_parent);
216 return S_OK;
218 fail:
219 for (j = 0; j < i; ++j)
221 IDXGISurface_Release(surface[i]);
223 IWineDXGIDeviceParent_Release(dxgi_device_parent);
224 return hr;
227 static HRESULT STDMETHODCALLTYPE dxgi_device_QueryResourceResidency(IWineDXGIDevice *iface,
228 IUnknown *const *resources, DXGI_RESIDENCY *residency, UINT resource_count)
230 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
231 iface, resources, residency, resource_count);
233 return E_NOTIMPL;
236 static HRESULT STDMETHODCALLTYPE dxgi_device_SetGPUThreadPriority(IWineDXGIDevice *iface, INT priority)
238 FIXME("iface %p, priority %d stub!\n", iface, priority);
240 return E_NOTIMPL;
243 static HRESULT STDMETHODCALLTYPE dxgi_device_GetGPUThreadPriority(IWineDXGIDevice *iface, INT *priority)
245 FIXME("iface %p, priority %p stub!\n", iface, priority);
247 return E_NOTIMPL;
250 /* IWineDXGIDevice methods */
252 static HRESULT STDMETHODCALLTYPE dxgi_device_create_surface(IWineDXGIDevice *iface, const DXGI_SURFACE_DESC *desc,
253 DXGI_USAGE usage, const DXGI_SHARED_RESOURCE *shared_resource, IUnknown *outer, void **surface)
255 struct dxgi_surface *object;
256 HRESULT hr;
258 FIXME("iface %p, desc %p, usage %#x, shared_resource %p, outer %p, surface %p partial stub!\n",
259 iface, desc, usage, shared_resource, outer, surface);
261 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
262 if (!object)
264 ERR("Failed to allocate DXGI surface object memory\n");
265 return E_OUTOFMEMORY;
268 if (FAILED(hr = dxgi_surface_init(object, (IDXGIDevice *)iface, outer, desc)))
270 WARN("Failed to initialize surface, hr %#x.\n", hr);
271 HeapFree(GetProcessHeap(), 0, object);
272 return hr;
275 TRACE("Created IDXGISurface %p\n", object);
276 *surface = outer ? &object->IUnknown_iface : (IUnknown *)&object->IDXGISurface_iface;
278 return S_OK;
281 static HRESULT STDMETHODCALLTYPE dxgi_device_create_swapchain(IWineDXGIDevice *iface,
282 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **wined3d_swapchain)
284 struct dxgi_device *This = impl_from_IWineDXGIDevice(iface);
285 struct dxgi_swapchain *object;
286 HRESULT hr;
288 TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
289 iface, desc, wined3d_swapchain);
291 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
292 if (!object)
294 ERR("Failed to allocate DXGI swapchain object memory\n");
295 return E_OUTOFMEMORY;
298 hr = dxgi_swapchain_init(object, This, desc);
299 if (FAILED(hr))
301 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
302 HeapFree(GetProcessHeap(), 0, object);
303 return hr;
306 TRACE("Created IDXGISwapChain %p\n", object);
307 *wined3d_swapchain = object->wined3d_swapchain;
309 return S_OK;
312 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl =
314 /* IUnknown methods */
315 dxgi_device_QueryInterface,
316 dxgi_device_AddRef,
317 dxgi_device_Release,
318 /* IDXGIObject methods */
319 dxgi_device_SetPrivateData,
320 dxgi_device_SetPrivateDataInterface,
321 dxgi_device_GetPrivateData,
322 dxgi_device_GetParent,
323 /* IDXGIDevice methods */
324 dxgi_device_GetAdapter,
325 dxgi_device_CreateSurface,
326 dxgi_device_QueryResourceResidency,
327 dxgi_device_SetGPUThreadPriority,
328 dxgi_device_GetGPUThreadPriority,
329 /* IWineDXGIDevice methods */
330 dxgi_device_create_surface,
331 dxgi_device_create_swapchain,
334 HRESULT dxgi_device_init(struct dxgi_device *device, struct dxgi_device_layer *layer,
335 IDXGIFactory *factory, IDXGIAdapter *adapter)
337 struct wined3d_device_parent *wined3d_device_parent;
338 struct wined3d_swapchain_desc swapchain_desc;
339 IWineDXGIDeviceParent *dxgi_device_parent;
340 struct dxgi_adapter *dxgi_adapter;
341 struct dxgi_factory *dxgi_factory;
342 void *layer_base;
343 HRESULT hr;
344 WINED3DCAPS caps;
346 if (!(dxgi_factory = unsafe_impl_from_IDXGIFactory1((IDXGIFactory1 *)factory)))
348 WARN("This is not the factory we're looking for.\n");
349 return E_FAIL;
352 if (!(dxgi_adapter = unsafe_impl_from_IDXGIAdapter1((IDXGIAdapter1 *)adapter)))
354 WARN("This is not the adapter we're looking for.\n");
355 return E_FAIL;
358 device->IWineDXGIDevice_iface.lpVtbl = &dxgi_device_vtbl;
359 device->refcount = 1;
361 layer_base = device + 1;
363 if (FAILED(hr = layer->create(layer->id, &layer_base, 0,
364 device, &IID_IUnknown, (void **)&device->child_layer)))
366 WARN("Failed to create device, returning %#x.\n", hr);
367 return hr;
370 if (FAILED(hr = IWineDXGIDevice_QueryInterface(&device->IWineDXGIDevice_iface,
371 &IID_IWineDXGIDeviceParent, (void **)&dxgi_device_parent)))
373 ERR("DXGI device should implement IWineD3DDeviceParent.\n");
374 IUnknown_Release(device->child_layer);
375 return hr;
377 wined3d_device_parent = IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent);
378 IWineDXGIDeviceParent_Release(dxgi_device_parent);
380 FIXME("Ignoring adapter type.\n");
382 hr = wined3d_get_device_caps(dxgi_factory->wined3d, dxgi_adapter->ordinal, WINED3D_DEVICE_TYPE_HAL, &caps);
383 if (FAILED(hr) || caps.VertexShaderVersion < 4 || caps.PixelShaderVersion < 4)
385 WARN("Direct3D 10 is not supported on this GPU with the current shader backend.\n");
386 if (SUCCEEDED(hr))
387 hr = E_FAIL;
388 IUnknown_Release(device->child_layer);
389 return hr;
392 EnterCriticalSection(&dxgi_cs);
393 hr = wined3d_device_create(dxgi_factory->wined3d, dxgi_adapter->ordinal, WINED3D_DEVICE_TYPE_HAL,
394 NULL, 0, 4, wined3d_device_parent, &device->wined3d_device);
395 LeaveCriticalSection(&dxgi_cs);
396 if (FAILED(hr))
398 WARN("Failed to create a wined3d device, returning %#x.\n", hr);
399 IUnknown_Release(device->child_layer);
400 return hr;
403 memset(&swapchain_desc, 0, sizeof(swapchain_desc));
404 swapchain_desc.swap_effect = WINED3D_SWAP_EFFECT_DISCARD;
405 swapchain_desc.device_window = dxgi_factory_get_device_window(dxgi_factory);
406 swapchain_desc.windowed = TRUE;
407 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc)))
409 ERR("Failed to initialize 3D, hr %#x.\n", hr);
410 wined3d_device_decref(device->wined3d_device);
411 IUnknown_Release(device->child_layer);
412 return hr;
415 device->factory = &dxgi_factory->IDXGIFactory1_iface;
416 IDXGIFactory1_AddRef(device->factory);
418 return S_OK;