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
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
*device
= 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_IDXGIDevice1
)
44 || IsEqualGUID(riid
, &IID_IDXGIDevice2
)
45 || IsEqualGUID(riid
, &IID_IDXGIDevice3
)
46 || IsEqualGUID(riid
, &IID_IWineDXGIDevice
))
48 IUnknown_AddRef(iface
);
53 if (IsEqualGUID(riid
, &IID_IWineDXGISwapChainFactory
))
55 IUnknown_AddRef(iface
);
56 *object
= &device
->IWineDXGISwapChainFactory_iface
;
60 if (device
->child_layer
)
62 TRACE("Forwarding to child layer %p.\n", device
->child_layer
);
63 return IUnknown_QueryInterface(device
->child_layer
, riid
, object
);
66 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
72 static ULONG STDMETHODCALLTYPE
dxgi_device_AddRef(IWineDXGIDevice
*iface
)
74 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
75 ULONG refcount
= InterlockedIncrement(&device
->refcount
);
77 TRACE("%p increasing refcount to %u\n", device
, refcount
);
82 static ULONG STDMETHODCALLTYPE
dxgi_device_Release(IWineDXGIDevice
*iface
)
84 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
85 ULONG refcount
= InterlockedDecrement(&device
->refcount
);
87 TRACE("%p decreasing refcount to %u.\n", device
, refcount
);
91 if (device
->child_layer
)
92 IUnknown_Release(device
->child_layer
);
94 wined3d_swapchain_decref(device
->implicit_swapchain
);
95 wined3d_device_decref(device
->wined3d_device
);
96 wined3d_mutex_unlock();
97 IWineDXGIAdapter_Release(device
->adapter
);
98 wined3d_private_store_cleanup(&device
->private_store
);
105 /* IDXGIObject methods */
107 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateData(IWineDXGIDevice
*iface
,
108 REFGUID guid
, UINT data_size
, const void *data
)
110 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
112 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
114 return dxgi_set_private_data(&device
->private_store
, guid
, data_size
, data
);
117 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateDataInterface(IWineDXGIDevice
*iface
,
118 REFGUID guid
, const IUnknown
*object
)
120 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
122 TRACE("iface %p, guid %s, object %p.\n", iface
, debugstr_guid(guid
), object
);
124 return dxgi_set_private_data_interface(&device
->private_store
, guid
, object
);
127 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetPrivateData(IWineDXGIDevice
*iface
,
128 REFGUID guid
, UINT
*data_size
, void *data
)
130 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
132 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
134 return dxgi_get_private_data(&device
->private_store
, guid
, data_size
, data
);
137 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetParent(IWineDXGIDevice
*iface
, REFIID riid
, void **parent
)
139 IDXGIAdapter
*adapter
;
142 TRACE("iface %p, riid %s, parent %p.\n", iface
, debugstr_guid(riid
), parent
);
144 hr
= IWineDXGIDevice_GetAdapter(iface
, &adapter
);
147 ERR("Failed to get adapter, hr %#x.\n", hr
);
151 hr
= IDXGIAdapter_QueryInterface(adapter
, riid
, parent
);
152 IDXGIAdapter_Release(adapter
);
157 /* IDXGIDevice methods */
159 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetAdapter(IWineDXGIDevice
*iface
, IDXGIAdapter
**adapter
)
161 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
163 TRACE("iface %p, adapter %p.\n", iface
, adapter
);
165 *adapter
= (IDXGIAdapter
*)device
->adapter
;
166 IDXGIAdapter_AddRef(*adapter
);
170 static HRESULT STDMETHODCALLTYPE
dxgi_device_CreateSurface(IWineDXGIDevice
*iface
,
171 const DXGI_SURFACE_DESC
*desc
, UINT surface_count
, DXGI_USAGE usage
,
172 const DXGI_SHARED_RESOURCE
*shared_resource
, IDXGISurface
**surface
)
174 struct wined3d_device_parent
*device_parent
;
175 struct wined3d_resource_desc surface_desc
;
176 IWineDXGIDeviceParent
*dxgi_device_parent
;
181 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p.\n",
182 iface
, desc
, surface_count
, usage
, shared_resource
, surface
);
184 hr
= IWineDXGIDevice_QueryInterface(iface
, &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
);
187 ERR("Device should implement IWineDXGIDeviceParent.\n");
191 device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
193 surface_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
194 surface_desc
.format
= wined3dformat_from_dxgi_format(desc
->Format
);
195 wined3d_sample_desc_from_dxgi(&surface_desc
.multisample_type
,
196 &surface_desc
.multisample_quality
, &desc
->SampleDesc
);
197 surface_desc
.bind_flags
= wined3d_bind_flags_from_dxgi_usage(usage
);
198 surface_desc
.usage
= 0;
199 surface_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
200 surface_desc
.width
= desc
->Width
;
201 surface_desc
.height
= desc
->Height
;
202 surface_desc
.depth
= 1;
203 surface_desc
.size
= 0;
205 wined3d_mutex_lock();
206 memset(surface
, 0, surface_count
* sizeof(*surface
));
207 for (i
= 0; i
< surface_count
; ++i
)
209 struct wined3d_texture
*wined3d_texture
;
212 if (FAILED(hr
= device_parent
->ops
->create_swapchain_texture(device_parent
,
213 NULL
, &surface_desc
, 0, &wined3d_texture
)))
215 ERR("Failed to create surface, hr %#x.\n", hr
);
219 parent
= wined3d_texture_get_parent(wined3d_texture
);
220 hr
= IUnknown_QueryInterface(parent
, &IID_IDXGISurface
, (void **)&surface
[i
]);
221 wined3d_texture_decref(wined3d_texture
);
224 ERR("Surface should implement IDXGISurface.\n");
228 TRACE("Created IDXGISurface %p (%u/%u).\n", surface
[i
], i
+ 1, surface_count
);
230 wined3d_mutex_unlock();
231 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
236 wined3d_mutex_unlock();
237 for (j
= 0; j
< i
; ++j
)
239 IDXGISurface_Release(surface
[i
]);
241 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
245 static HRESULT STDMETHODCALLTYPE
dxgi_device_QueryResourceResidency(IWineDXGIDevice
*iface
,
246 IUnknown
*const *resources
, DXGI_RESIDENCY
*residency
, UINT resource_count
)
248 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
249 iface
, resources
, residency
, resource_count
);
254 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetGPUThreadPriority(IWineDXGIDevice
*iface
, INT priority
)
256 FIXME("iface %p, priority %d stub!\n", iface
, priority
);
261 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetGPUThreadPriority(IWineDXGIDevice
*iface
, INT
*priority
)
263 FIXME("iface %p, priority %p stub!\n", iface
, priority
);
268 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetMaximumFrameLatency(IWineDXGIDevice
*iface
, UINT max_latency
)
270 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
272 TRACE("iface %p, max_latency %u.\n", iface
, max_latency
);
274 if (max_latency
> DXGI_FRAME_LATENCY_MAX
)
275 return DXGI_ERROR_INVALID_CALL
;
277 wined3d_mutex_lock();
278 wined3d_device_set_max_frame_latency(device
->wined3d_device
, max_latency
);
279 wined3d_mutex_unlock();
284 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetMaximumFrameLatency(IWineDXGIDevice
*iface
, UINT
*max_latency
)
286 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
288 TRACE("iface %p, max_latency %p.\n", iface
, max_latency
);
291 return DXGI_ERROR_INVALID_CALL
;
293 wined3d_mutex_lock();
294 *max_latency
= wined3d_device_get_max_frame_latency(device
->wined3d_device
);
295 wined3d_mutex_unlock();
300 static HRESULT STDMETHODCALLTYPE
dxgi_device_OfferResources(IWineDXGIDevice
*iface
, UINT resource_count
,
301 IDXGIResource
* const *resources
, DXGI_OFFER_RESOURCE_PRIORITY priority
)
303 FIXME("iface %p, resource_count %u, resources %p, priority %u stub!\n", iface
, resource_count
,
304 resources
, priority
);
309 static HRESULT STDMETHODCALLTYPE
dxgi_device_ReclaimResources(IWineDXGIDevice
*iface
, UINT resource_count
,
310 IDXGIResource
* const *resources
, BOOL
*discarded
)
312 FIXME("iface %p, resource_count %u, resources %p, discarded %p stub!\n", iface
, resource_count
,
313 resources
, discarded
);
318 static HRESULT STDMETHODCALLTYPE
dxgi_device_EnqueueSetEvent(IWineDXGIDevice
*iface
, HANDLE event
)
320 FIXME("iface %p, event %p stub!\n", iface
, event
);
325 static void STDMETHODCALLTYPE
dxgi_device_Trim(IWineDXGIDevice
*iface
)
327 FIXME("iface %p stub!\n", iface
);
330 /* IWineDXGIDevice methods */
332 static HRESULT STDMETHODCALLTYPE
dxgi_device_create_surface(IWineDXGIDevice
*iface
,
333 struct wined3d_texture
*wined3d_texture
, DXGI_USAGE usage
,
334 const DXGI_SHARED_RESOURCE
*shared_resource
, IUnknown
*outer
, void **surface
)
336 struct dxgi_surface
*object
;
339 TRACE("iface %p, wined3d_texture %p, usage %#x, shared_resource %p, outer %p, surface %p.\n",
340 iface
, wined3d_texture
, usage
, shared_resource
, outer
, surface
);
342 if (!(object
= heap_alloc_zero(sizeof(*object
))))
344 ERR("Failed to allocate DXGI surface object memory.\n");
345 return E_OUTOFMEMORY
;
348 if (FAILED(hr
= dxgi_surface_init(object
, (IDXGIDevice
*)iface
, outer
, wined3d_texture
)))
350 WARN("Failed to initialize surface, hr %#x.\n", hr
);
355 TRACE("Created IDXGISurface %p.\n", object
);
356 *surface
= outer
? &object
->IUnknown_iface
: (IUnknown
*)&object
->IDXGISurface1_iface
;
361 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl
=
363 /* IUnknown methods */
364 dxgi_device_QueryInterface
,
367 /* IDXGIObject methods */
368 dxgi_device_SetPrivateData
,
369 dxgi_device_SetPrivateDataInterface
,
370 dxgi_device_GetPrivateData
,
371 dxgi_device_GetParent
,
372 /* IDXGIDevice methods */
373 dxgi_device_GetAdapter
,
374 dxgi_device_CreateSurface
,
375 dxgi_device_QueryResourceResidency
,
376 dxgi_device_SetGPUThreadPriority
,
377 dxgi_device_GetGPUThreadPriority
,
378 /* IDXGIDevice1 methods */
379 dxgi_device_SetMaximumFrameLatency
,
380 dxgi_device_GetMaximumFrameLatency
,
381 /* IDXGIDevice2 methods */
382 dxgi_device_OfferResources
,
383 dxgi_device_ReclaimResources
,
384 dxgi_device_EnqueueSetEvent
,
385 /* IDXGIDevice3 methods */
387 /* IWineDXGIDevice methods */
388 dxgi_device_create_surface
,
391 static inline struct dxgi_device
*impl_from_IWineDXGISwapChainFactory(IWineDXGISwapChainFactory
*iface
)
393 return CONTAINING_RECORD(iface
, struct dxgi_device
, IWineDXGISwapChainFactory_iface
);
396 static HRESULT STDMETHODCALLTYPE
dxgi_swapchain_factory_QueryInterface(IWineDXGISwapChainFactory
*iface
,
397 REFIID iid
, void **out
)
399 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
401 TRACE("iface %p, iid %s, out %p.\n", iface
, debugstr_guid(iid
), out
);
403 return dxgi_device_QueryInterface(&device
->IWineDXGIDevice_iface
, iid
, out
);
406 static ULONG STDMETHODCALLTYPE
dxgi_swapchain_factory_AddRef(IWineDXGISwapChainFactory
*iface
)
408 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
410 TRACE("iface %p.\n", iface
);
412 return dxgi_device_AddRef(&device
->IWineDXGIDevice_iface
);
415 static ULONG STDMETHODCALLTYPE
dxgi_swapchain_factory_Release(IWineDXGISwapChainFactory
*iface
)
417 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
419 TRACE("iface %p.\n", iface
);
421 return dxgi_device_Release(&device
->IWineDXGIDevice_iface
);
424 static HRESULT STDMETHODCALLTYPE
dxgi_swapchain_factory_create_swapchain(IWineDXGISwapChainFactory
*iface
,
425 IDXGIFactory
*factory
, HWND window
, const DXGI_SWAP_CHAIN_DESC1
*desc
,
426 const DXGI_SWAP_CHAIN_FULLSCREEN_DESC
*fullscreen_desc
, IDXGIOutput
*output
, IDXGISwapChain1
**swapchain
)
428 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
429 struct wined3d_swapchain_desc wined3d_desc
;
430 struct IDXGIOutput
*containing_output
;
431 struct d3d11_swapchain
*object
;
432 struct IDXGIAdapter
*adapter
;
435 TRACE("iface %p, factory %p, window %p, desc %p, fullscreen_desc %p, output %p, swapchain %p.\n",
436 iface
, factory
, window
, desc
, fullscreen_desc
, output
, swapchain
);
438 if (FAILED(hr
= dxgi_get_output_from_window(factory
, window
, &containing_output
)))
440 WARN("Failed to get output from window %p, hr %#x.\n", window
, hr
);
442 /* FIXME: As wined3d only supports one output currently, even if a window is on a
443 * non-primary output, the swapchain will use the primary output. Keep this behaviour
444 * until all outputs are correctly enumerated. Otherwise it will create a regression
445 * for applications that specify a device window on a non-primary output */
446 if (FAILED(hr
= IDXGIFactory_EnumAdapters(factory
, 0, &adapter
)))
449 hr
= IDXGIAdapter_EnumOutputs(adapter
, 0, &containing_output
);
450 IDXGIAdapter_Release(adapter
);
454 FIXME("Using the primary output for the device window that is on a non-primary output.\n");
457 hr
= wined3d_swapchain_desc_from_dxgi(&wined3d_desc
, containing_output
, window
, desc
,
459 IDXGIOutput_Release(containing_output
);
463 if (!(object
= heap_alloc_zero(sizeof(*object
))))
465 ERR("Failed to allocate swapchain memory.\n");
466 return E_OUTOFMEMORY
;
469 if (FAILED(hr
= d3d11_swapchain_init(object
, device
, &wined3d_desc
)))
471 WARN("Failed to initialise swapchain, hr %#x.\n", hr
);
476 TRACE("Created swapchain %p.\n", object
);
478 *swapchain
= &object
->IDXGISwapChain1_iface
;
483 static const struct IWineDXGISwapChainFactoryVtbl dxgi_swapchain_factory_vtbl
=
485 dxgi_swapchain_factory_QueryInterface
,
486 dxgi_swapchain_factory_AddRef
,
487 dxgi_swapchain_factory_Release
,
488 dxgi_swapchain_factory_create_swapchain
,
491 HRESULT
dxgi_device_init(struct dxgi_device
*device
, struct dxgi_device_layer
*layer
,
492 IDXGIFactory
*factory
, IDXGIAdapter
*adapter
,
493 const D3D_FEATURE_LEVEL
*feature_levels
, unsigned int level_count
)
495 struct wined3d_device_parent
*wined3d_device_parent
;
496 struct wined3d_swapchain_desc swapchain_desc
;
497 IWineDXGIDeviceParent
*dxgi_device_parent
;
498 struct d3d11_swapchain
*swapchain
;
499 struct dxgi_adapter
*dxgi_adapter
;
500 struct dxgi_factory
*dxgi_factory
;
501 struct dxgi_output
*dxgi_output
;
502 struct IDXGIOutput
*output
;
507 if (!(dxgi_factory
= unsafe_impl_from_IDXGIFactory(factory
)))
509 WARN("This is not the factory we're looking for.\n");
513 if (!(dxgi_adapter
= unsafe_impl_from_IDXGIAdapter(adapter
)))
515 WARN("This is not the adapter we're looking for.\n");
519 device
->IWineDXGIDevice_iface
.lpVtbl
= &dxgi_device_vtbl
;
520 device
->IWineDXGISwapChainFactory_iface
.lpVtbl
= &dxgi_swapchain_factory_vtbl
;
521 device
->refcount
= 1;
522 wined3d_mutex_lock();
523 wined3d_private_store_init(&device
->private_store
);
525 layer_base
= device
+ 1;
527 if (FAILED(hr
= layer
->create(layer
->id
, &layer_base
, 0,
528 device
, &IID_IUnknown
, (void **)&device
->child_layer
)))
530 WARN("Failed to create device, returning %#x.\n", hr
);
531 wined3d_private_store_cleanup(&device
->private_store
);
532 wined3d_mutex_unlock();
536 if (FAILED(hr
= IWineDXGIDevice_QueryInterface(&device
->IWineDXGIDevice_iface
,
537 &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
)))
539 ERR("DXGI device should implement IWineDXGIDeviceParent.\n");
540 IUnknown_Release(device
->child_layer
);
541 wined3d_private_store_cleanup(&device
->private_store
);
542 wined3d_mutex_unlock();
545 wined3d_device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
546 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
548 if (FAILED(hr
= wined3d_device_create(dxgi_factory
->wined3d
,
549 dxgi_adapter
->wined3d_adapter
, WINED3D_DEVICE_TYPE_HAL
, NULL
, 0, 4,
550 (const enum wined3d_feature_level
*)feature_levels
, level_count
,
551 wined3d_device_parent
, &device
->wined3d_device
)))
553 WARN("Failed to create a wined3d device, returning %#x.\n", hr
);
554 IUnknown_Release(device
->child_layer
);
555 wined3d_private_store_cleanup(&device
->private_store
);
556 wined3d_mutex_unlock();
560 window
= dxgi_factory_get_device_window(dxgi_factory
);
561 if (FAILED(hr
= dxgi_get_output_from_window(factory
, window
, &output
)))
563 ERR("Failed to get output from window %p.\n", window
);
564 wined3d_device_decref(device
->wined3d_device
);
565 IUnknown_Release(device
->child_layer
);
566 wined3d_private_store_cleanup(&device
->private_store
);
567 wined3d_mutex_unlock();
570 dxgi_output
= unsafe_impl_from_IDXGIOutput(output
);
572 memset(&swapchain_desc
, 0, sizeof(swapchain_desc
));
573 swapchain_desc
.swap_effect
= WINED3D_SWAP_EFFECT_DISCARD
;
574 swapchain_desc
.device_window
= window
;
575 swapchain_desc
.windowed
= TRUE
;
576 swapchain_desc
.flags
= WINED3D_SWAPCHAIN_IMPLICIT
;
577 swapchain_desc
.output
= dxgi_output
->wined3d_output
;
578 IDXGIOutput_Release(output
);
580 if (!(swapchain
= heap_alloc_zero(sizeof(*swapchain
))))
582 ERR("Failed to allocate swapchain memory.\n");
583 wined3d_device_decref(device
->wined3d_device
);
584 IUnknown_Release(device
->child_layer
);
585 wined3d_private_store_cleanup(&device
->private_store
);
586 wined3d_mutex_unlock();
587 return E_OUTOFMEMORY
;
590 if (FAILED(hr
= d3d11_swapchain_init(swapchain
, device
, &swapchain_desc
)))
592 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
593 heap_free(swapchain
);
594 wined3d_device_decref(device
->wined3d_device
);
595 IUnknown_Release(device
->child_layer
);
596 wined3d_private_store_cleanup(&device
->private_store
);
597 wined3d_mutex_unlock();
600 device
->implicit_swapchain
= swapchain
->wined3d_swapchain
;
602 TRACE("Created swapchain %p.\n", swapchain
);
604 wined3d_mutex_unlock();
606 device
->adapter
= &dxgi_adapter
->IWineDXGIAdapter_iface
;
607 IWineDXGIAdapter_AddRef(device
->adapter
);