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 "dxgi_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(dxgi
);
24 static inline struct dxgi_device
*impl_from_IWineDXGIDevice(IWineDXGIDevice
*iface
)
26 return CONTAINING_RECORD(iface
, struct dxgi_device
, IWineDXGIDevice_iface
);
29 /* IUnknown methods */
31 static HRESULT STDMETHODCALLTYPE
dxgi_device_QueryInterface(IWineDXGIDevice
*iface
, REFIID riid
, void **object
)
33 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
35 TRACE("iface %p, riid %s, object %p\n", iface
, debugstr_guid(riid
), object
);
37 if (IsEqualGUID(riid
, &IID_IUnknown
)
38 || IsEqualGUID(riid
, &IID_IDXGIObject
)
39 || IsEqualGUID(riid
, &IID_IDXGIDevice
)
40 || IsEqualGUID(riid
, &IID_IDXGIDevice1
)
41 || IsEqualGUID(riid
, &IID_IDXGIDevice2
)
42 || IsEqualGUID(riid
, &IID_IDXGIDevice3
)
43 || IsEqualGUID(riid
, &IID_IWineDXGIDevice
))
45 IUnknown_AddRef(iface
);
50 if (IsEqualGUID(riid
, &IID_IWineDXGISwapChainFactory
))
52 IUnknown_AddRef(iface
);
53 *object
= &device
->IWineDXGISwapChainFactory_iface
;
57 if (device
->child_layer
)
59 TRACE("Forwarding to child layer %p.\n", device
->child_layer
);
60 return IUnknown_QueryInterface(device
->child_layer
, riid
, object
);
63 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
69 static ULONG STDMETHODCALLTYPE
dxgi_device_AddRef(IWineDXGIDevice
*iface
)
71 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
72 ULONG refcount
= InterlockedIncrement(&device
->refcount
);
74 TRACE("%p increasing refcount to %lu\n", device
, refcount
);
79 static ULONG STDMETHODCALLTYPE
dxgi_device_Release(IWineDXGIDevice
*iface
)
81 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
82 ULONG refcount
= InterlockedDecrement(&device
->refcount
);
84 TRACE("%p decreasing refcount to %lu.\n", device
, refcount
);
88 if (device
->child_layer
)
89 IUnknown_Release(device
->child_layer
);
91 wined3d_swapchain_decref(device
->implicit_swapchain
);
92 wined3d_device_decref(device
->wined3d_device
);
93 wined3d_mutex_unlock();
94 IWineDXGIAdapter_Release(device
->adapter
);
95 wined3d_private_store_cleanup(&device
->private_store
);
102 /* IDXGIObject methods */
104 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateData(IWineDXGIDevice
*iface
,
105 REFGUID guid
, UINT data_size
, const void *data
)
107 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
109 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
111 return dxgi_set_private_data(&device
->private_store
, guid
, data_size
, data
);
114 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateDataInterface(IWineDXGIDevice
*iface
,
115 REFGUID guid
, const IUnknown
*object
)
117 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
119 TRACE("iface %p, guid %s, object %p.\n", iface
, debugstr_guid(guid
), object
);
121 return dxgi_set_private_data_interface(&device
->private_store
, guid
, object
);
124 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetPrivateData(IWineDXGIDevice
*iface
,
125 REFGUID guid
, UINT
*data_size
, void *data
)
127 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
129 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
131 return dxgi_get_private_data(&device
->private_store
, guid
, data_size
, data
);
134 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetParent(IWineDXGIDevice
*iface
, REFIID riid
, void **parent
)
136 IDXGIAdapter
*adapter
;
139 TRACE("iface %p, riid %s, parent %p.\n", iface
, debugstr_guid(riid
), parent
);
141 hr
= IWineDXGIDevice_GetAdapter(iface
, &adapter
);
144 ERR("Failed to get adapter, hr %#lx.\n", hr
);
148 hr
= IDXGIAdapter_QueryInterface(adapter
, riid
, parent
);
149 IDXGIAdapter_Release(adapter
);
154 /* IDXGIDevice methods */
156 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetAdapter(IWineDXGIDevice
*iface
, IDXGIAdapter
**adapter
)
158 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
160 TRACE("iface %p, adapter %p.\n", iface
, adapter
);
162 *adapter
= (IDXGIAdapter
*)device
->adapter
;
163 IDXGIAdapter_AddRef(*adapter
);
167 static HRESULT STDMETHODCALLTYPE
dxgi_device_CreateSurface(IWineDXGIDevice
*iface
,
168 const DXGI_SURFACE_DESC
*desc
, UINT surface_count
, DXGI_USAGE usage
,
169 const DXGI_SHARED_RESOURCE
*shared_resource
, IDXGISurface
**surface
)
171 struct wined3d_device_parent
*device_parent
;
172 struct wined3d_resource_desc surface_desc
;
173 IWineDXGIDeviceParent
*dxgi_device_parent
;
178 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p.\n",
179 iface
, desc
, surface_count
, usage
, shared_resource
, surface
);
181 hr
= IWineDXGIDevice_QueryInterface(iface
, &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
);
184 ERR("Device should implement IWineDXGIDeviceParent.\n");
188 device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
190 surface_desc
.resource_type
= WINED3D_RTYPE_TEXTURE_2D
;
191 surface_desc
.format
= wined3dformat_from_dxgi_format(desc
->Format
);
192 wined3d_sample_desc_from_dxgi(&surface_desc
.multisample_type
,
193 &surface_desc
.multisample_quality
, &desc
->SampleDesc
);
194 surface_desc
.bind_flags
= wined3d_bind_flags_from_dxgi_usage(usage
);
195 surface_desc
.usage
= 0;
196 surface_desc
.access
= WINED3D_RESOURCE_ACCESS_GPU
;
197 surface_desc
.width
= desc
->Width
;
198 surface_desc
.height
= desc
->Height
;
199 surface_desc
.depth
= 1;
200 surface_desc
.size
= 0;
202 wined3d_mutex_lock();
203 memset(surface
, 0, surface_count
* sizeof(*surface
));
204 for (i
= 0; i
< surface_count
; ++i
)
206 struct wined3d_texture
*wined3d_texture
;
209 if (FAILED(hr
= device_parent
->ops
->create_swapchain_texture(device_parent
,
210 NULL
, &surface_desc
, 0, &wined3d_texture
)))
212 ERR("Failed to create surface, hr %#lx.\n", hr
);
216 parent
= wined3d_texture_get_parent(wined3d_texture
);
217 hr
= IUnknown_QueryInterface(parent
, &IID_IDXGISurface
, (void **)&surface
[i
]);
218 wined3d_texture_decref(wined3d_texture
);
221 ERR("Surface should implement IDXGISurface.\n");
225 TRACE("Created IDXGISurface %p (%u/%u).\n", surface
[i
], i
+ 1, surface_count
);
227 wined3d_mutex_unlock();
228 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
233 wined3d_mutex_unlock();
234 for (j
= 0; j
< i
; ++j
)
236 IDXGISurface_Release(surface
[i
]);
238 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
242 static HRESULT STDMETHODCALLTYPE
dxgi_device_QueryResourceResidency(IWineDXGIDevice
*iface
,
243 IUnknown
*const *resources
, DXGI_RESIDENCY
*residency
, UINT resource_count
)
245 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
246 iface
, resources
, residency
, resource_count
);
251 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetGPUThreadPriority(IWineDXGIDevice
*iface
, INT priority
)
253 FIXME("iface %p, priority %d stub!\n", iface
, priority
);
258 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetGPUThreadPriority(IWineDXGIDevice
*iface
, INT
*priority
)
260 FIXME("iface %p, priority %p stub!\n", iface
, priority
);
265 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetMaximumFrameLatency(IWineDXGIDevice
*iface
, UINT max_latency
)
267 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
269 TRACE("iface %p, max_latency %u.\n", iface
, max_latency
);
271 if (max_latency
> DXGI_FRAME_LATENCY_MAX
)
272 return DXGI_ERROR_INVALID_CALL
;
274 wined3d_mutex_lock();
275 wined3d_device_set_max_frame_latency(device
->wined3d_device
, max_latency
);
276 wined3d_mutex_unlock();
281 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetMaximumFrameLatency(IWineDXGIDevice
*iface
, UINT
*max_latency
)
283 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
285 TRACE("iface %p, max_latency %p.\n", iface
, max_latency
);
288 return DXGI_ERROR_INVALID_CALL
;
290 wined3d_mutex_lock();
291 *max_latency
= wined3d_device_get_max_frame_latency(device
->wined3d_device
);
292 wined3d_mutex_unlock();
297 static HRESULT STDMETHODCALLTYPE
dxgi_device_OfferResources(IWineDXGIDevice
*iface
, UINT resource_count
,
298 IDXGIResource
* const *resources
, DXGI_OFFER_RESOURCE_PRIORITY priority
)
300 FIXME("iface %p, resource_count %u, resources %p, priority %u stub!\n", iface
, resource_count
,
301 resources
, priority
);
306 static HRESULT STDMETHODCALLTYPE
dxgi_device_ReclaimResources(IWineDXGIDevice
*iface
, UINT resource_count
,
307 IDXGIResource
* const *resources
, BOOL
*discarded
)
309 FIXME("iface %p, resource_count %u, resources %p, discarded %p stub!\n", iface
, resource_count
,
310 resources
, discarded
);
315 static HRESULT STDMETHODCALLTYPE
dxgi_device_EnqueueSetEvent(IWineDXGIDevice
*iface
, HANDLE event
)
317 FIXME("iface %p, event %p stub!\n", iface
, event
);
322 static void STDMETHODCALLTYPE
dxgi_device_Trim(IWineDXGIDevice
*iface
)
324 FIXME("iface %p stub!\n", iface
);
327 /* IWineDXGIDevice methods */
329 static HRESULT STDMETHODCALLTYPE
dxgi_device_create_resource(IWineDXGIDevice
*iface
,
330 struct wined3d_resource
*wined3d_resource
, DXGI_USAGE usage
,
331 const DXGI_SHARED_RESOURCE
*shared_resource
, IUnknown
*outer
, BOOL needs_surface
, void **resource
)
333 struct dxgi_resource
*object
;
336 TRACE("iface %p, wined3d_resource %p, usage %#x, shared_resource %p, outer %p, surface %p.\n",
337 iface
, wined3d_resource
, usage
, shared_resource
, outer
, resource
);
339 if (!(object
= heap_alloc_zero(sizeof(*object
))))
341 ERR("Failed to allocate DXGI resource object memory.\n");
342 return E_OUTOFMEMORY
;
345 if (FAILED(hr
= dxgi_resource_init(object
, (IDXGIDevice
*)iface
, outer
, needs_surface
, wined3d_resource
)))
347 WARN("Failed to initialize resource, hr %#lx.\n", hr
);
352 TRACE("Created resource %p.\n", object
);
353 *resource
= outer
? &object
->IUnknown_iface
: (IUnknown
*)&object
->IDXGIResource_iface
;
358 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl
=
360 /* IUnknown methods */
361 dxgi_device_QueryInterface
,
364 /* IDXGIObject methods */
365 dxgi_device_SetPrivateData
,
366 dxgi_device_SetPrivateDataInterface
,
367 dxgi_device_GetPrivateData
,
368 dxgi_device_GetParent
,
369 /* IDXGIDevice methods */
370 dxgi_device_GetAdapter
,
371 dxgi_device_CreateSurface
,
372 dxgi_device_QueryResourceResidency
,
373 dxgi_device_SetGPUThreadPriority
,
374 dxgi_device_GetGPUThreadPriority
,
375 /* IDXGIDevice1 methods */
376 dxgi_device_SetMaximumFrameLatency
,
377 dxgi_device_GetMaximumFrameLatency
,
378 /* IDXGIDevice2 methods */
379 dxgi_device_OfferResources
,
380 dxgi_device_ReclaimResources
,
381 dxgi_device_EnqueueSetEvent
,
382 /* IDXGIDevice3 methods */
384 /* IWineDXGIDevice methods */
385 dxgi_device_create_resource
,
388 static inline struct dxgi_device
*impl_from_IWineDXGISwapChainFactory(IWineDXGISwapChainFactory
*iface
)
390 return CONTAINING_RECORD(iface
, struct dxgi_device
, IWineDXGISwapChainFactory_iface
);
393 static HRESULT STDMETHODCALLTYPE
dxgi_swapchain_factory_QueryInterface(IWineDXGISwapChainFactory
*iface
,
394 REFIID iid
, void **out
)
396 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
398 TRACE("iface %p, iid %s, out %p.\n", iface
, debugstr_guid(iid
), out
);
400 return dxgi_device_QueryInterface(&device
->IWineDXGIDevice_iface
, iid
, out
);
403 static ULONG STDMETHODCALLTYPE
dxgi_swapchain_factory_AddRef(IWineDXGISwapChainFactory
*iface
)
405 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
407 TRACE("iface %p.\n", iface
);
409 return dxgi_device_AddRef(&device
->IWineDXGIDevice_iface
);
412 static ULONG STDMETHODCALLTYPE
dxgi_swapchain_factory_Release(IWineDXGISwapChainFactory
*iface
)
414 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
416 TRACE("iface %p.\n", iface
);
418 return dxgi_device_Release(&device
->IWineDXGIDevice_iface
);
421 static HRESULT STDMETHODCALLTYPE
dxgi_swapchain_factory_create_swapchain(IWineDXGISwapChainFactory
*iface
,
422 IDXGIFactory
*factory
, HWND window
, const DXGI_SWAP_CHAIN_DESC1
*desc
,
423 const DXGI_SWAP_CHAIN_FULLSCREEN_DESC
*fullscreen_desc
, IDXGIOutput
*output
, IDXGISwapChain1
**swapchain
)
425 struct dxgi_device
*device
= impl_from_IWineDXGISwapChainFactory(iface
);
426 struct wined3d_swapchain_desc wined3d_desc
;
427 struct IDXGIOutput
*containing_output
;
428 struct dxgi_factory
*dxgi_factory
;
429 struct d3d11_swapchain
*object
;
432 TRACE("iface %p, factory %p, window %p, desc %p, fullscreen_desc %p, output %p, swapchain %p.\n",
433 iface
, factory
, window
, desc
, fullscreen_desc
, output
, swapchain
);
435 if (!(dxgi_factory
= unsafe_impl_from_IDXGIFactory(factory
)))
437 WARN("Factory %p is not a valid dxgi factory.\n", factory
);
441 if (FAILED(hr
= dxgi_get_output_from_window(&dxgi_factory
->IWineDXGIFactory_iface
, window
, &containing_output
)))
443 WARN("Failed to get output from window %p, hr %#lx.\n", window
, hr
);
447 hr
= wined3d_swapchain_desc_from_dxgi(&wined3d_desc
, containing_output
, window
, desc
,
449 IDXGIOutput_Release(containing_output
);
453 if (!(object
= heap_alloc_zero(sizeof(*object
))))
455 ERR("Failed to allocate swapchain memory.\n");
456 return E_OUTOFMEMORY
;
459 if (FAILED(hr
= d3d11_swapchain_init(object
, device
, &wined3d_desc
)))
461 WARN("Failed to initialise swapchain, hr %#lx.\n", hr
);
466 TRACE("Created swapchain %p.\n", object
);
468 *swapchain
= &object
->IDXGISwapChain1_iface
;
473 static const struct IWineDXGISwapChainFactoryVtbl dxgi_swapchain_factory_vtbl
=
475 dxgi_swapchain_factory_QueryInterface
,
476 dxgi_swapchain_factory_AddRef
,
477 dxgi_swapchain_factory_Release
,
478 dxgi_swapchain_factory_create_swapchain
,
481 HRESULT
dxgi_device_init(struct dxgi_device
*device
, struct dxgi_device_layer
*layer
,
482 IDXGIFactory
*factory
, IDXGIAdapter
*adapter
,
483 const D3D_FEATURE_LEVEL
*feature_levels
, unsigned int level_count
)
485 struct wined3d_device_parent
*wined3d_device_parent
;
486 struct wined3d_swapchain_desc swapchain_desc
;
487 IWineDXGIDeviceParent
*dxgi_device_parent
;
488 struct d3d11_swapchain
*swapchain
;
489 struct dxgi_adapter
*dxgi_adapter
;
490 struct dxgi_factory
*dxgi_factory
;
491 struct dxgi_output
*dxgi_output
;
492 struct IDXGIOutput
*output
;
497 if (!(dxgi_factory
= unsafe_impl_from_IDXGIFactory(factory
)))
499 WARN("This is not the factory we're looking for.\n");
503 if (!(dxgi_adapter
= unsafe_impl_from_IDXGIAdapter(adapter
)))
505 WARN("This is not the adapter we're looking for.\n");
509 device
->IWineDXGIDevice_iface
.lpVtbl
= &dxgi_device_vtbl
;
510 device
->IWineDXGISwapChainFactory_iface
.lpVtbl
= &dxgi_swapchain_factory_vtbl
;
511 device
->refcount
= 1;
512 wined3d_mutex_lock();
513 wined3d_private_store_init(&device
->private_store
);
515 layer_base
= device
+ 1;
517 if (FAILED(hr
= layer
->create(layer
->id
, &layer_base
, 0,
518 device
, &IID_IUnknown
, (void **)&device
->child_layer
)))
520 WARN("Failed to create device, returning %#lx.\n", hr
);
521 wined3d_private_store_cleanup(&device
->private_store
);
522 wined3d_mutex_unlock();
526 if (FAILED(hr
= IWineDXGIDevice_QueryInterface(&device
->IWineDXGIDevice_iface
,
527 &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
)))
529 ERR("DXGI device should implement IWineDXGIDeviceParent.\n");
530 IUnknown_Release(device
->child_layer
);
531 wined3d_private_store_cleanup(&device
->private_store
);
532 wined3d_mutex_unlock();
535 wined3d_device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
536 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
538 if (FAILED(hr
= wined3d_device_create(dxgi_factory
->wined3d
,
539 dxgi_adapter
->wined3d_adapter
, WINED3D_DEVICE_TYPE_HAL
, NULL
, 0, 4,
540 (const enum wined3d_feature_level
*)feature_levels
, level_count
,
541 wined3d_device_parent
, &device
->wined3d_device
)))
543 WARN("Failed to create a wined3d device, returning %#lx.\n", hr
);
544 IUnknown_Release(device
->child_layer
);
545 wined3d_private_store_cleanup(&device
->private_store
);
546 wined3d_mutex_unlock();
550 window
= dxgi_factory_get_device_window(dxgi_factory
);
551 if (FAILED(hr
= dxgi_get_output_from_window(&dxgi_factory
->IWineDXGIFactory_iface
, window
, &output
)))
553 ERR("Failed to get output from window %p.\n", window
);
554 wined3d_device_decref(device
->wined3d_device
);
555 IUnknown_Release(device
->child_layer
);
556 wined3d_private_store_cleanup(&device
->private_store
);
557 wined3d_mutex_unlock();
560 dxgi_output
= unsafe_impl_from_IDXGIOutput(output
);
562 memset(&swapchain_desc
, 0, sizeof(swapchain_desc
));
563 swapchain_desc
.swap_effect
= WINED3D_SWAP_EFFECT_DISCARD
;
564 swapchain_desc
.device_window
= window
;
565 swapchain_desc
.windowed
= TRUE
;
566 swapchain_desc
.flags
= WINED3D_SWAPCHAIN_IMPLICIT
;
567 swapchain_desc
.output
= dxgi_output
->wined3d_output
;
568 IDXGIOutput_Release(output
);
570 if (!(swapchain
= heap_alloc_zero(sizeof(*swapchain
))))
572 ERR("Failed to allocate swapchain memory.\n");
573 wined3d_device_decref(device
->wined3d_device
);
574 IUnknown_Release(device
->child_layer
);
575 wined3d_private_store_cleanup(&device
->private_store
);
576 wined3d_mutex_unlock();
577 return E_OUTOFMEMORY
;
580 if (FAILED(hr
= d3d11_swapchain_init(swapchain
, device
, &swapchain_desc
)))
582 WARN("Failed to initialize swapchain, hr %#lx.\n", hr
);
583 heap_free(swapchain
);
584 wined3d_device_decref(device
->wined3d_device
);
585 IUnknown_Release(device
->child_layer
);
586 wined3d_private_store_cleanup(&device
->private_store
);
587 wined3d_mutex_unlock();
590 device
->implicit_swapchain
= swapchain
->wined3d_swapchain
;
592 TRACE("Created swapchain %p.\n", swapchain
);
594 wined3d_mutex_unlock();
596 device
->adapter
= &dxgi_adapter
->IWineDXGIAdapter_iface
;
597 IWineDXGIAdapter_AddRef(device
->adapter
);