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
);
26 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
28 static inline struct dxgi_device
*impl_from_IWineDXGIDevice(IWineDXGIDevice
*iface
)
30 return CONTAINING_RECORD(iface
, struct dxgi_device
, IWineDXGIDevice_iface
);
33 /* IUnknown methods */
35 static HRESULT STDMETHODCALLTYPE
dxgi_device_QueryInterface(IWineDXGIDevice
*iface
, REFIID riid
, void **object
)
37 struct dxgi_device
*This
= impl_from_IWineDXGIDevice(iface
);
39 TRACE("iface %p, riid %s, object %p\n", iface
, debugstr_guid(riid
), object
);
41 if (IsEqualGUID(riid
, &IID_IUnknown
)
42 || IsEqualGUID(riid
, &IID_IDXGIObject
)
43 || IsEqualGUID(riid
, &IID_IDXGIDevice
)
44 || IsEqualGUID(riid
, &IID_IWineDXGIDevice
))
46 IUnknown_AddRef(iface
);
51 if (This
->child_layer
)
53 TRACE("forwarding to child layer %p\n", This
->child_layer
);
54 return IUnknown_QueryInterface(This
->child_layer
, riid
, object
);
57 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid
));
63 static ULONG STDMETHODCALLTYPE
dxgi_device_AddRef(IWineDXGIDevice
*iface
)
65 struct dxgi_device
*This
= impl_from_IWineDXGIDevice(iface
);
66 ULONG refcount
= InterlockedIncrement(&This
->refcount
);
68 TRACE("%p increasing refcount to %u\n", This
, refcount
);
73 static ULONG STDMETHODCALLTYPE
dxgi_device_Release(IWineDXGIDevice
*iface
)
75 struct dxgi_device
*This
= impl_from_IWineDXGIDevice(iface
);
76 ULONG refcount
= InterlockedDecrement(&This
->refcount
);
78 TRACE("%p decreasing refcount to %u\n", This
, refcount
);
82 if (This
->child_layer
) IUnknown_Release(This
->child_layer
);
84 wined3d_device_uninit_3d(This
->wined3d_device
);
85 wined3d_device_decref(This
->wined3d_device
);
86 wined3d_mutex_unlock();
87 IDXGIFactory1_Release(This
->factory
);
88 wined3d_private_store_cleanup(&This
->private_store
);
89 HeapFree(GetProcessHeap(), 0, This
);
95 /* IDXGIObject methods */
97 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateData(IWineDXGIDevice
*iface
,
98 REFGUID guid
, UINT data_size
, const void *data
)
100 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
102 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
104 return dxgi_set_private_data(&device
->private_store
, guid
, data_size
, data
);
107 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateDataInterface(IWineDXGIDevice
*iface
,
108 REFGUID guid
, const IUnknown
*object
)
110 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
112 TRACE("iface %p, guid %s, object %p.\n", iface
, debugstr_guid(guid
), object
);
114 return dxgi_set_private_data_interface(&device
->private_store
, guid
, object
);
117 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetPrivateData(IWineDXGIDevice
*iface
,
118 REFGUID guid
, UINT
*data_size
, void *data
)
120 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
122 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
124 return dxgi_get_private_data(&device
->private_store
, guid
, data_size
, data
);
127 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetParent(IWineDXGIDevice
*iface
, REFIID riid
, void **parent
)
129 IDXGIAdapter
*adapter
;
132 TRACE("iface %p, riid %s, parent %p.\n", iface
, debugstr_guid(riid
), parent
);
134 hr
= IWineDXGIDevice_GetAdapter(iface
, &adapter
);
137 ERR("Failed to get adapter, hr %#x.\n", hr
);
141 hr
= IDXGIAdapter_QueryInterface(adapter
, riid
, parent
);
142 IDXGIAdapter_Release(adapter
);
147 /* IDXGIDevice methods */
149 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetAdapter(IWineDXGIDevice
*iface
, IDXGIAdapter
**adapter
)
151 struct dxgi_device
*This
= impl_from_IWineDXGIDevice(iface
);
152 struct wined3d_device_creation_parameters create_parameters
;
154 TRACE("iface %p, adapter %p\n", iface
, adapter
);
156 wined3d_mutex_lock();
157 wined3d_device_get_creation_parameters(This
->wined3d_device
, &create_parameters
);
158 wined3d_mutex_unlock();
160 return IDXGIFactory1_EnumAdapters(This
->factory
, create_parameters
.adapter_idx
, adapter
);
163 static HRESULT STDMETHODCALLTYPE
dxgi_device_CreateSurface(IWineDXGIDevice
*iface
,
164 const DXGI_SURFACE_DESC
*desc
, UINT surface_count
, DXGI_USAGE usage
,
165 const DXGI_SHARED_RESOURCE
*shared_resource
, IDXGISurface
**surface
)
167 struct wined3d_device_parent
*device_parent
;
168 struct wined3d_resource_desc surface_desc
;
169 IWineDXGIDeviceParent
*dxgi_device_parent
;
174 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
175 iface
, desc
, surface_count
, usage
, shared_resource
, surface
);
177 hr
= IWineDXGIDevice_QueryInterface(iface
, &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
);
180 ERR("Device should implement IWineD3DDeviceParent\n");
184 device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
186 FIXME("Implement DXGI<->wined3d usage conversion\n");
187 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
188 surface_desc
.format
= wined3dformat_from_dxgi_format(desc
->Format
);
189 wined3d_sample_desc_from_dxgi(&surface_desc
.multisample_type
,
190 &surface_desc
.multisample_quality
, &desc
->SampleDesc
);
191 surface_desc
.usage
= usage
;
192 surface_desc
.pool
= WINED3D_POOL_DEFAULT
;
193 surface_desc
.width
= desc
->Width
;
194 surface_desc
.height
= desc
->Height
;
195 surface_desc
.depth
= 1;
196 surface_desc
.size
= 0;
198 wined3d_mutex_lock();
199 memset(surface
, 0, surface_count
* sizeof(*surface
));
200 for (i
= 0; i
< surface_count
; ++i
)
202 struct wined3d_texture
*wined3d_texture
;
205 if (FAILED(hr
= device_parent
->ops
->create_swapchain_texture(device_parent
,
206 NULL
, &surface_desc
, &wined3d_texture
)))
208 ERR("Failed to create surface, hr %#x.\n", hr
);
212 parent
= wined3d_texture_get_parent(wined3d_texture
);
213 hr
= IUnknown_QueryInterface(parent
, &IID_IDXGISurface
, (void **)&surface
[i
]);
214 wined3d_texture_decref(wined3d_texture
);
217 ERR("Surface should implement IDXGISurface\n");
221 TRACE("Created IDXGISurface %p (%u/%u)\n", surface
[i
], i
+ 1, surface_count
);
223 wined3d_mutex_unlock();
224 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
229 wined3d_mutex_unlock();
230 for (j
= 0; j
< i
; ++j
)
232 IDXGISurface_Release(surface
[i
]);
234 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
238 static HRESULT STDMETHODCALLTYPE
dxgi_device_QueryResourceResidency(IWineDXGIDevice
*iface
,
239 IUnknown
*const *resources
, DXGI_RESIDENCY
*residency
, UINT resource_count
)
241 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
242 iface
, resources
, residency
, resource_count
);
247 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetGPUThreadPriority(IWineDXGIDevice
*iface
, INT priority
)
249 FIXME("iface %p, priority %d stub!\n", iface
, priority
);
254 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetGPUThreadPriority(IWineDXGIDevice
*iface
, INT
*priority
)
256 FIXME("iface %p, priority %p stub!\n", iface
, priority
);
261 /* IWineDXGIDevice methods */
263 static HRESULT STDMETHODCALLTYPE
dxgi_device_create_surface(IWineDXGIDevice
*iface
,
264 struct wined3d_resource
*wined3d_resource
, DXGI_USAGE usage
,
265 const DXGI_SHARED_RESOURCE
*shared_resource
, IUnknown
*outer
, void **surface
)
267 struct dxgi_surface
*object
;
270 TRACE("iface %p, wined3d_resource %p, usage %#x, shared_resource %p, outer %p, surface %p.\n",
271 iface
, wined3d_resource
, usage
, shared_resource
, outer
, surface
);
273 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
275 ERR("Failed to allocate DXGI surface object memory\n");
276 return E_OUTOFMEMORY
;
279 if (FAILED(hr
= dxgi_surface_init(object
, (IDXGIDevice
*)iface
, outer
, wined3d_resource
)))
281 WARN("Failed to initialize surface, hr %#x.\n", hr
);
282 HeapFree(GetProcessHeap(), 0, object
);
286 TRACE("Created IDXGISurface %p\n", object
);
287 *surface
= outer
? &object
->IUnknown_iface
: (IUnknown
*)&object
->IDXGISurface_iface
;
292 static HRESULT STDMETHODCALLTYPE
dxgi_device_create_swapchain(IWineDXGIDevice
*iface
,
293 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain
**wined3d_swapchain
)
295 struct dxgi_device
*This
= impl_from_IWineDXGIDevice(iface
);
296 struct dxgi_swapchain
*object
;
299 TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
300 iface
, desc
, wined3d_swapchain
);
302 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
305 ERR("Failed to allocate DXGI swapchain object memory\n");
306 return E_OUTOFMEMORY
;
309 hr
= dxgi_swapchain_init(object
, This
, desc
);
312 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
313 HeapFree(GetProcessHeap(), 0, object
);
317 TRACE("Created IDXGISwapChain %p\n", object
);
318 *wined3d_swapchain
= object
->wined3d_swapchain
;
323 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl
=
325 /* IUnknown methods */
326 dxgi_device_QueryInterface
,
329 /* IDXGIObject methods */
330 dxgi_device_SetPrivateData
,
331 dxgi_device_SetPrivateDataInterface
,
332 dxgi_device_GetPrivateData
,
333 dxgi_device_GetParent
,
334 /* IDXGIDevice methods */
335 dxgi_device_GetAdapter
,
336 dxgi_device_CreateSurface
,
337 dxgi_device_QueryResourceResidency
,
338 dxgi_device_SetGPUThreadPriority
,
339 dxgi_device_GetGPUThreadPriority
,
340 /* IWineDXGIDevice methods */
341 dxgi_device_create_surface
,
342 dxgi_device_create_swapchain
,
345 HRESULT
dxgi_device_init(struct dxgi_device
*device
, struct dxgi_device_layer
*layer
,
346 IDXGIFactory
*factory
, IDXGIAdapter
*adapter
)
348 struct wined3d_device_parent
*wined3d_device_parent
;
349 struct wined3d_swapchain_desc swapchain_desc
;
350 IWineDXGIDeviceParent
*dxgi_device_parent
;
351 struct dxgi_adapter
*dxgi_adapter
;
352 struct dxgi_factory
*dxgi_factory
;
357 if (!(dxgi_factory
= unsafe_impl_from_IDXGIFactory1((IDXGIFactory1
*)factory
)))
359 WARN("This is not the factory we're looking for.\n");
363 if (!(dxgi_adapter
= unsafe_impl_from_IDXGIAdapter1((IDXGIAdapter1
*)adapter
)))
365 WARN("This is not the adapter we're looking for.\n");
369 device
->IWineDXGIDevice_iface
.lpVtbl
= &dxgi_device_vtbl
;
370 device
->refcount
= 1;
371 wined3d_mutex_lock();
372 wined3d_private_store_init(&device
->private_store
);
374 layer_base
= device
+ 1;
376 if (FAILED(hr
= layer
->create(layer
->id
, &layer_base
, 0,
377 device
, &IID_IUnknown
, (void **)&device
->child_layer
)))
379 WARN("Failed to create device, returning %#x.\n", hr
);
380 wined3d_private_store_cleanup(&device
->private_store
);
381 wined3d_mutex_unlock();
385 if (FAILED(hr
= IWineDXGIDevice_QueryInterface(&device
->IWineDXGIDevice_iface
,
386 &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
)))
388 ERR("DXGI device should implement IWineD3DDeviceParent.\n");
389 IUnknown_Release(device
->child_layer
);
390 wined3d_private_store_cleanup(&device
->private_store
);
391 wined3d_mutex_unlock();
394 wined3d_device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
395 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
397 FIXME("Ignoring adapter type.\n");
399 hr
= wined3d_get_device_caps(dxgi_factory
->wined3d
, dxgi_adapter
->ordinal
, WINED3D_DEVICE_TYPE_HAL
, &caps
);
400 if (FAILED(hr
) || caps
.VertexShaderVersion
< 4 || caps
.PixelShaderVersion
< 4)
402 FIXME_(winediag
)("Direct3D 10 is not supported on this GPU with the current shader backend.\n");
405 IUnknown_Release(device
->child_layer
);
406 wined3d_private_store_cleanup(&device
->private_store
);
407 wined3d_mutex_unlock();
411 hr
= wined3d_device_create(dxgi_factory
->wined3d
, dxgi_adapter
->ordinal
, WINED3D_DEVICE_TYPE_HAL
,
412 NULL
, 0, 4, wined3d_device_parent
, &device
->wined3d_device
);
415 WARN("Failed to create a wined3d device, returning %#x.\n", hr
);
416 IUnknown_Release(device
->child_layer
);
417 wined3d_private_store_cleanup(&device
->private_store
);
418 wined3d_mutex_unlock();
422 memset(&swapchain_desc
, 0, sizeof(swapchain_desc
));
423 swapchain_desc
.swap_effect
= WINED3D_SWAP_EFFECT_DISCARD
;
424 swapchain_desc
.device_window
= dxgi_factory_get_device_window(dxgi_factory
);
425 swapchain_desc
.windowed
= TRUE
;
426 if (FAILED(hr
= wined3d_device_init_3d(device
->wined3d_device
, &swapchain_desc
)))
428 ERR("Failed to initialize 3D, hr %#x.\n", hr
);
429 wined3d_device_decref(device
->wined3d_device
);
430 IUnknown_Release(device
->child_layer
);
431 wined3d_private_store_cleanup(&device
->private_store
);
432 wined3d_mutex_unlock();
435 wined3d_mutex_unlock();
437 device
->factory
= &dxgi_factory
->IDXGIFactory1_iface
;
438 IDXGIFactory1_AddRef(device
->factory
);