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
*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
);
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
));
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
);
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
);
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 wined3d_private_store_cleanup(&This
->private_store
);
88 HeapFree(GetProcessHeap(), 0, This
);
94 /* IDXGIObject methods */
96 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateData(IWineDXGIDevice
*iface
,
97 REFGUID guid
, UINT data_size
, const void *data
)
99 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
101 TRACE("iface %p, guid %s, data_size %u, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
103 return dxgi_set_private_data(&device
->private_store
, guid
, data_size
, data
);
106 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetPrivateDataInterface(IWineDXGIDevice
*iface
,
107 REFGUID guid
, const IUnknown
*object
)
109 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
111 TRACE("iface %p, guid %s, object %p.\n", iface
, debugstr_guid(guid
), object
);
113 return dxgi_set_private_data_interface(&device
->private_store
, guid
, object
);
116 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetPrivateData(IWineDXGIDevice
*iface
,
117 REFGUID guid
, UINT
*data_size
, void *data
)
119 struct dxgi_device
*device
= impl_from_IWineDXGIDevice(iface
);
121 TRACE("iface %p, guid %s, data_size %p, data %p.\n", iface
, debugstr_guid(guid
), data_size
, data
);
123 return dxgi_get_private_data(&device
->private_store
, guid
, data_size
, data
);
126 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetParent(IWineDXGIDevice
*iface
, REFIID riid
, void **parent
)
128 IDXGIAdapter
*adapter
;
131 TRACE("iface %p, riid %s, parent %p.\n", iface
, debugstr_guid(riid
), parent
);
133 hr
= IWineDXGIDevice_GetAdapter(iface
, &adapter
);
136 ERR("Failed to get adapter, hr %#x.\n", hr
);
140 hr
= IDXGIAdapter_QueryInterface(adapter
, riid
, parent
);
141 IDXGIAdapter_Release(adapter
);
146 /* IDXGIDevice methods */
148 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetAdapter(IWineDXGIDevice
*iface
, IDXGIAdapter
**adapter
)
150 struct dxgi_device
*This
= impl_from_IWineDXGIDevice(iface
);
151 struct wined3d_device_creation_parameters create_parameters
;
153 TRACE("iface %p, adapter %p\n", iface
, adapter
);
155 EnterCriticalSection(&dxgi_cs
);
156 wined3d_device_get_creation_parameters(This
->wined3d_device
, &create_parameters
);
157 LeaveCriticalSection(&dxgi_cs
);
159 return IDXGIFactory1_EnumAdapters(This
->factory
, create_parameters
.adapter_idx
, adapter
);
162 static HRESULT STDMETHODCALLTYPE
dxgi_device_CreateSurface(IWineDXGIDevice
*iface
,
163 const DXGI_SURFACE_DESC
*desc
, UINT surface_count
, DXGI_USAGE usage
,
164 const DXGI_SHARED_RESOURCE
*shared_resource
, IDXGISurface
**surface
)
166 struct wined3d_device_parent
*device_parent
;
167 struct wined3d_resource_desc surface_desc
;
168 IWineDXGIDeviceParent
*dxgi_device_parent
;
173 TRACE("iface %p, desc %p, surface_count %u, usage %#x, shared_resource %p, surface %p\n",
174 iface
, desc
, surface_count
, usage
, shared_resource
, surface
);
176 hr
= IWineDXGIDevice_QueryInterface(iface
, &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
);
179 ERR("Device should implement IWineD3DDeviceParent\n");
183 device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
185 FIXME("Implement DXGI<->wined3d usage conversion\n");
186 surface_desc
.resource_type
= WINED3D_RTYPE_SURFACE
;
187 surface_desc
.format
= wined3dformat_from_dxgi_format(desc
->Format
);
188 wined3d_sample_desc_from_dxgi(&surface_desc
.multisample_type
,
189 &surface_desc
.multisample_quality
, &desc
->SampleDesc
);
190 surface_desc
.usage
= usage
;
191 surface_desc
.pool
= WINED3D_POOL_DEFAULT
;
192 surface_desc
.width
= desc
->Width
;
193 surface_desc
.height
= desc
->Height
;
194 surface_desc
.depth
= 1;
195 surface_desc
.size
= 0;
197 memset(surface
, 0, surface_count
* sizeof(*surface
));
198 for (i
= 0; i
< surface_count
; ++i
)
200 struct wined3d_surface
*wined3d_surface
;
203 if (FAILED(hr
= device_parent
->ops
->create_swapchain_surface(device_parent
,
204 NULL
, &surface_desc
, &wined3d_surface
)))
206 ERR("Failed to create surface, hr %#x.\n", hr
);
210 parent
= wined3d_surface_get_parent(wined3d_surface
);
211 hr
= IUnknown_QueryInterface(parent
, &IID_IDXGISurface
, (void **)&surface
[i
]);
212 wined3d_surface_decref(wined3d_surface
);
215 ERR("Surface should implement IDXGISurface\n");
219 TRACE("Created IDXGISurface %p (%u/%u)\n", surface
[i
], i
+ 1, surface_count
);
221 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
226 for (j
= 0; j
< i
; ++j
)
228 IDXGISurface_Release(surface
[i
]);
230 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
234 static HRESULT STDMETHODCALLTYPE
dxgi_device_QueryResourceResidency(IWineDXGIDevice
*iface
,
235 IUnknown
*const *resources
, DXGI_RESIDENCY
*residency
, UINT resource_count
)
237 FIXME("iface %p, resources %p, residency %p, resource_count %u stub!\n",
238 iface
, resources
, residency
, resource_count
);
243 static HRESULT STDMETHODCALLTYPE
dxgi_device_SetGPUThreadPriority(IWineDXGIDevice
*iface
, INT priority
)
245 FIXME("iface %p, priority %d stub!\n", iface
, priority
);
250 static HRESULT STDMETHODCALLTYPE
dxgi_device_GetGPUThreadPriority(IWineDXGIDevice
*iface
, INT
*priority
)
252 FIXME("iface %p, priority %p stub!\n", iface
, priority
);
257 /* IWineDXGIDevice methods */
259 static HRESULT STDMETHODCALLTYPE
dxgi_device_create_surface(IWineDXGIDevice
*iface
,
260 struct wined3d_resource
*wined3d_resource
, DXGI_USAGE usage
,
261 const DXGI_SHARED_RESOURCE
*shared_resource
, IUnknown
*outer
, void **surface
)
263 struct dxgi_surface
*object
;
266 TRACE("iface %p, wined3d_resource %p, usage %#x, shared_resource %p, outer %p, surface %p.\n",
267 iface
, wined3d_resource
, usage
, shared_resource
, outer
, surface
);
269 if (!(object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
))))
271 ERR("Failed to allocate DXGI surface object memory\n");
272 return E_OUTOFMEMORY
;
275 if (FAILED(hr
= dxgi_surface_init(object
, (IDXGIDevice
*)iface
, outer
, wined3d_resource
)))
277 WARN("Failed to initialize surface, hr %#x.\n", hr
);
278 HeapFree(GetProcessHeap(), 0, object
);
282 TRACE("Created IDXGISurface %p\n", object
);
283 *surface
= outer
? &object
->IUnknown_iface
: (IUnknown
*)&object
->IDXGISurface_iface
;
288 static HRESULT STDMETHODCALLTYPE
dxgi_device_create_swapchain(IWineDXGIDevice
*iface
,
289 struct wined3d_swapchain_desc
*desc
, struct wined3d_swapchain
**wined3d_swapchain
)
291 struct dxgi_device
*This
= impl_from_IWineDXGIDevice(iface
);
292 struct dxgi_swapchain
*object
;
295 TRACE("iface %p, desc %p, wined3d_swapchain %p.\n",
296 iface
, desc
, wined3d_swapchain
);
298 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
301 ERR("Failed to allocate DXGI swapchain object memory\n");
302 return E_OUTOFMEMORY
;
305 hr
= dxgi_swapchain_init(object
, This
, desc
);
308 WARN("Failed to initialize swapchain, hr %#x.\n", hr
);
309 HeapFree(GetProcessHeap(), 0, object
);
313 TRACE("Created IDXGISwapChain %p\n", object
);
314 *wined3d_swapchain
= object
->wined3d_swapchain
;
319 static const struct IWineDXGIDeviceVtbl dxgi_device_vtbl
=
321 /* IUnknown methods */
322 dxgi_device_QueryInterface
,
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 /* IWineDXGIDevice methods */
337 dxgi_device_create_surface
,
338 dxgi_device_create_swapchain
,
341 HRESULT
dxgi_device_init(struct dxgi_device
*device
, struct dxgi_device_layer
*layer
,
342 IDXGIFactory
*factory
, IDXGIAdapter
*adapter
)
344 struct wined3d_device_parent
*wined3d_device_parent
;
345 struct wined3d_swapchain_desc swapchain_desc
;
346 IWineDXGIDeviceParent
*dxgi_device_parent
;
347 struct dxgi_adapter
*dxgi_adapter
;
348 struct dxgi_factory
*dxgi_factory
;
353 if (!(dxgi_factory
= unsafe_impl_from_IDXGIFactory1((IDXGIFactory1
*)factory
)))
355 WARN("This is not the factory we're looking for.\n");
359 if (!(dxgi_adapter
= unsafe_impl_from_IDXGIAdapter1((IDXGIAdapter1
*)adapter
)))
361 WARN("This is not the adapter we're looking for.\n");
365 device
->IWineDXGIDevice_iface
.lpVtbl
= &dxgi_device_vtbl
;
366 device
->refcount
= 1;
367 wined3d_private_store_init(&device
->private_store
);
369 layer_base
= device
+ 1;
371 if (FAILED(hr
= layer
->create(layer
->id
, &layer_base
, 0,
372 device
, &IID_IUnknown
, (void **)&device
->child_layer
)))
374 WARN("Failed to create device, returning %#x.\n", hr
);
375 wined3d_private_store_cleanup(&device
->private_store
);
379 if (FAILED(hr
= IWineDXGIDevice_QueryInterface(&device
->IWineDXGIDevice_iface
,
380 &IID_IWineDXGIDeviceParent
, (void **)&dxgi_device_parent
)))
382 ERR("DXGI device should implement IWineD3DDeviceParent.\n");
383 IUnknown_Release(device
->child_layer
);
384 wined3d_private_store_cleanup(&device
->private_store
);
387 wined3d_device_parent
= IWineDXGIDeviceParent_get_wined3d_device_parent(dxgi_device_parent
);
388 IWineDXGIDeviceParent_Release(dxgi_device_parent
);
390 FIXME("Ignoring adapter type.\n");
392 hr
= wined3d_get_device_caps(dxgi_factory
->wined3d
, dxgi_adapter
->ordinal
, WINED3D_DEVICE_TYPE_HAL
, &caps
);
393 if (FAILED(hr
) || caps
.VertexShaderVersion
< 4 || caps
.PixelShaderVersion
< 4)
395 WARN("Direct3D 10 is not supported on this GPU with the current shader backend.\n");
398 IUnknown_Release(device
->child_layer
);
399 wined3d_private_store_cleanup(&device
->private_store
);
403 EnterCriticalSection(&dxgi_cs
);
404 hr
= wined3d_device_create(dxgi_factory
->wined3d
, dxgi_adapter
->ordinal
, WINED3D_DEVICE_TYPE_HAL
,
405 NULL
, 0, 4, wined3d_device_parent
, &device
->wined3d_device
);
406 LeaveCriticalSection(&dxgi_cs
);
409 WARN("Failed to create a wined3d device, returning %#x.\n", hr
);
410 IUnknown_Release(device
->child_layer
);
411 wined3d_private_store_cleanup(&device
->private_store
);
415 memset(&swapchain_desc
, 0, sizeof(swapchain_desc
));
416 swapchain_desc
.swap_effect
= WINED3D_SWAP_EFFECT_DISCARD
;
417 swapchain_desc
.device_window
= dxgi_factory_get_device_window(dxgi_factory
);
418 swapchain_desc
.windowed
= TRUE
;
419 if (FAILED(hr
= wined3d_device_init_3d(device
->wined3d_device
, &swapchain_desc
)))
421 ERR("Failed to initialize 3D, hr %#x.\n", hr
);
422 wined3d_device_decref(device
->wined3d_device
);
423 IUnknown_Release(device
->child_layer
);
424 wined3d_private_store_cleanup(&device
->private_store
);
428 device
->factory
= &dxgi_factory
->IDXGIFactory1_iface
;
429 IDXGIFactory1_AddRef(device
->factory
);