2 * IDirect3D8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2003-2004 Raphael Junqueira
6 * Copyright 2004 Christian Costa
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 #include "d3d8_private.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8
);
40 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI
IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface
, REFIID riid
,LPVOID
*ppobj
)
43 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
45 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), ppobj
);
47 if (IsEqualGUID(riid
, &IID_IUnknown
)
48 || IsEqualGUID(riid
, &IID_IDirect3D8
)) {
49 IUnknown_AddRef(iface
);
54 WARN("(%p)->(%s,%p),not found\n", This
, debugstr_guid(riid
),ppobj
);
59 static ULONG WINAPI
IDirect3D8Impl_AddRef(LPDIRECT3D8 iface
) {
60 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
61 ULONG ref
= InterlockedIncrement(&This
->ref
);
63 TRACE("%p increasing refcount to %u.\n", iface
, ref
);
68 static ULONG WINAPI
IDirect3D8Impl_Release(LPDIRECT3D8 iface
) {
69 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
70 ULONG ref
= InterlockedDecrement(&This
->ref
);
72 TRACE("%p decreasing refcount to %u.\n", iface
, ref
);
75 TRACE("Releasing wined3d %p\n", This
->WineD3D
);
78 IWineD3D_Release(This
->WineD3D
);
79 wined3d_mutex_unlock();
81 HeapFree(GetProcessHeap(), 0, This
);
87 /* IDirect3D8 Interface follow: */
88 static HRESULT WINAPI
IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface
, void* pInitializeFunction
) {
89 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
92 TRACE("iface %p, init_function %p.\n", iface
, pInitializeFunction
);
95 hr
= IWineD3D_RegisterSoftwareDevice(This
->WineD3D
, pInitializeFunction
);
96 wined3d_mutex_unlock();
101 static UINT WINAPI
IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface
) {
102 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
105 TRACE("iface %p.\n", iface
);
107 wined3d_mutex_lock();
108 hr
= IWineD3D_GetAdapterCount(This
->WineD3D
);
109 wined3d_mutex_unlock();
114 static HRESULT WINAPI
IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface
,
115 UINT Adapter
, DWORD Flags
, D3DADAPTER_IDENTIFIER8
*pIdentifier
)
117 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
118 WINED3DADAPTER_IDENTIFIER adapter_id
;
121 TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
122 iface
, Adapter
, Flags
, pIdentifier
);
124 adapter_id
.driver
= pIdentifier
->Driver
;
125 adapter_id
.driver_size
= sizeof(pIdentifier
->Driver
);
126 adapter_id
.description
= pIdentifier
->Description
;
127 adapter_id
.description_size
= sizeof(pIdentifier
->Description
);
128 adapter_id
.device_name
= NULL
; /* d3d9 only */
129 adapter_id
.device_name_size
= 0; /* d3d9 only */
131 wined3d_mutex_lock();
132 hr
= IWineD3D_GetAdapterIdentifier(This
->WineD3D
, Adapter
, Flags
, &adapter_id
);
133 wined3d_mutex_unlock();
135 pIdentifier
->DriverVersion
= adapter_id
.driver_version
;
136 pIdentifier
->VendorId
= adapter_id
.vendor_id
;
137 pIdentifier
->DeviceId
= adapter_id
.device_id
;
138 pIdentifier
->SubSysId
= adapter_id
.subsystem_id
;
139 pIdentifier
->Revision
= adapter_id
.revision
;
140 memcpy(&pIdentifier
->DeviceIdentifier
, &adapter_id
.device_identifier
, sizeof(pIdentifier
->DeviceIdentifier
));
141 pIdentifier
->WHQLLevel
= adapter_id
.whql_level
;
146 static UINT WINAPI
IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface
,UINT Adapter
) {
147 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
150 TRACE("iface %p, adapter %u.\n", iface
, Adapter
);
152 wined3d_mutex_lock();
153 hr
= IWineD3D_GetAdapterModeCount(This
->WineD3D
, Adapter
, 0 /* format */);
154 wined3d_mutex_unlock();
159 static HRESULT WINAPI
IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface
, UINT Adapter
, UINT Mode
, D3DDISPLAYMODE
* pMode
) {
160 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
163 TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
164 iface
, Adapter
, Mode
, pMode
);
166 wined3d_mutex_lock();
167 hr
= IWineD3D_EnumAdapterModes(This
->WineD3D
, Adapter
, WINED3DFMT_UNKNOWN
, Mode
, (WINED3DDISPLAYMODE
*) pMode
);
168 wined3d_mutex_unlock();
170 if (SUCCEEDED(hr
)) pMode
->Format
= d3dformat_from_wined3dformat(pMode
->Format
);
175 static HRESULT WINAPI
IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface
, UINT Adapter
, D3DDISPLAYMODE
* pMode
) {
176 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
179 TRACE("iface %p, adapter %u, mode %p.\n",
180 iface
, Adapter
, pMode
);
182 wined3d_mutex_lock();
183 hr
= IWineD3D_GetAdapterDisplayMode(This
->WineD3D
, Adapter
, (WINED3DDISPLAYMODE
*) pMode
);
184 wined3d_mutex_unlock();
186 if (SUCCEEDED(hr
)) pMode
->Format
= d3dformat_from_wined3dformat(pMode
->Format
);
191 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface
,
192 UINT Adapter
, D3DDEVTYPE CheckType
, D3DFORMAT DisplayFormat
,
193 D3DFORMAT BackBufferFormat
, BOOL Windowed
) {
194 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
197 TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
198 iface
, Adapter
, CheckType
, DisplayFormat
, BackBufferFormat
, Windowed
);
200 wined3d_mutex_lock();
201 hr
= IWineD3D_CheckDeviceType(This
->WineD3D
, Adapter
, CheckType
, wined3dformat_from_d3dformat(DisplayFormat
),
202 wined3dformat_from_d3dformat(BackBufferFormat
), Windowed
);
203 wined3d_mutex_unlock();
208 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface
,
209 UINT Adapter
, D3DDEVTYPE DeviceType
, D3DFORMAT AdapterFormat
,
210 DWORD Usage
, D3DRESOURCETYPE RType
, D3DFORMAT CheckFormat
) {
211 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
213 WINED3DRESOURCETYPE WineD3DRType
;
215 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
216 iface
, Adapter
, DeviceType
, AdapterFormat
, Usage
, RType
, CheckFormat
);
218 if(CheckFormat
== D3DFMT_R8G8B8
)
220 /* See comment in dlls/d3d9/directx.c, IDirect3D9Impl_CheckDeviceFormat for details */
221 WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
222 return D3DERR_NOTAVAILABLE
;
227 case D3DRTYPE_VERTEXBUFFER
:
228 case D3DRTYPE_INDEXBUFFER
:
229 WineD3DRType
= WINED3DRTYPE_BUFFER
;
233 WineD3DRType
= RType
;
237 wined3d_mutex_lock();
238 hr
= IWineD3D_CheckDeviceFormat(This
->WineD3D
, Adapter
, DeviceType
, wined3dformat_from_d3dformat(AdapterFormat
),
239 Usage
, WineD3DRType
, wined3dformat_from_d3dformat(CheckFormat
), SURFACE_OPENGL
);
240 wined3d_mutex_unlock();
245 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8
*iface
, UINT Adapter
,
246 D3DDEVTYPE DeviceType
, D3DFORMAT SurfaceFormat
, BOOL Windowed
, D3DMULTISAMPLE_TYPE MultiSampleType
)
248 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
251 TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
252 iface
, Adapter
, DeviceType
, SurfaceFormat
, Windowed
, MultiSampleType
);
254 wined3d_mutex_lock();
255 hr
= IWineD3D_CheckDeviceMultiSampleType(This
->WineD3D
, Adapter
, DeviceType
,
256 wined3dformat_from_d3dformat(SurfaceFormat
), Windowed
, (WINED3DMULTISAMPLE_TYPE
) MultiSampleType
, NULL
);
257 wined3d_mutex_unlock();
262 static HRESULT WINAPI
IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8
*iface
, UINT Adapter
, D3DDEVTYPE DeviceType
,
263 D3DFORMAT AdapterFormat
, D3DFORMAT RenderTargetFormat
, D3DFORMAT DepthStencilFormat
)
265 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
268 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
269 iface
, Adapter
, DeviceType
, AdapterFormat
, RenderTargetFormat
, DepthStencilFormat
);
271 wined3d_mutex_lock();
272 hr
= IWineD3D_CheckDepthStencilMatch(This
->WineD3D
, Adapter
, DeviceType
,
273 wined3dformat_from_d3dformat(AdapterFormat
), wined3dformat_from_d3dformat(RenderTargetFormat
),
274 wined3dformat_from_d3dformat(DepthStencilFormat
));
275 wined3d_mutex_unlock();
280 void fixup_caps(WINED3DCAPS
*caps
)
282 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
283 if (caps
->PixelShaderVersion
> D3DPS_VERSION(1,4)) {
284 caps
->PixelShaderVersion
= D3DPS_VERSION(1,4);
286 if (caps
->VertexShaderVersion
> D3DVS_VERSION(1,1)) {
287 caps
->VertexShaderVersion
= D3DVS_VERSION(1,1);
289 caps
->MaxVertexShaderConst
= min(D3D8_MAX_VERTEX_SHADER_CONSTANTF
, caps
->MaxVertexShaderConst
);
291 caps
->StencilCaps
&= ~WINED3DSTENCILCAPS_TWOSIDED
;
294 static HRESULT WINAPI
IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface
, UINT Adapter
, D3DDEVTYPE DeviceType
, D3DCAPS8
* pCaps
) {
295 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
296 HRESULT hrc
= D3D_OK
;
297 WINED3DCAPS
*pWineCaps
;
299 TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface
, Adapter
, DeviceType
, pCaps
);
302 return D3DERR_INVALIDCALL
;
304 pWineCaps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINED3DCAPS
));
305 if(pWineCaps
== NULL
){
306 return D3DERR_INVALIDCALL
; /*well this is what MSDN says to return*/
309 wined3d_mutex_lock();
310 hrc
= IWineD3D_GetDeviceCaps(This
->WineD3D
, Adapter
, DeviceType
, pWineCaps
);
311 wined3d_mutex_unlock();
313 fixup_caps(pWineCaps
);
314 WINECAPSTOD3D8CAPS(pCaps
, pWineCaps
)
315 HeapFree(GetProcessHeap(), 0, pWineCaps
);
317 TRACE("(%p) returning %p\n", This
, pCaps
);
321 static HMONITOR WINAPI
IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface
, UINT Adapter
) {
322 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
325 TRACE("iface %p, adapter %u.\n", iface
, Adapter
);
327 wined3d_mutex_lock();
328 ret
= IWineD3D_GetAdapterMonitor(This
->WineD3D
, Adapter
);
329 wined3d_mutex_unlock();
334 ULONG WINAPI
D3D8CB_DestroySwapChain(IWineD3DSwapChain
*pSwapChain
) {
335 IUnknown
* swapChainParent
;
337 TRACE("swapchain %p.\n", pSwapChain
);
339 IWineD3DSwapChain_GetParent(pSwapChain
, &swapChainParent
);
340 IUnknown_Release(swapChainParent
);
341 return IUnknown_Release(swapChainParent
);
344 static HRESULT WINAPI
IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface
, UINT Adapter
, D3DDEVTYPE DeviceType
, HWND hFocusWindow
,
345 DWORD BehaviourFlags
, D3DPRESENT_PARAMETERS
* pPresentationParameters
,
346 IDirect3DDevice8
** ppReturnedDeviceInterface
) {
348 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
349 IDirect3DDevice8Impl
*object
= NULL
;
350 WINED3DPRESENT_PARAMETERS localParameters
;
353 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
354 iface
, Adapter
, DeviceType
, hFocusWindow
, BehaviourFlags
, pPresentationParameters
,
355 ppReturnedDeviceInterface
);
357 /* Check the validity range of the adapter parameter */
358 if (Adapter
>= IDirect3D8Impl_GetAdapterCount(iface
)) {
359 *ppReturnedDeviceInterface
= NULL
;
360 return D3DERR_INVALIDCALL
;
363 /* Allocate the storage for the device object */
364 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirect3DDevice8Impl
));
365 if (NULL
== object
) {
366 FIXME("Allocation of memory failed\n");
367 *ppReturnedDeviceInterface
= NULL
;
368 return D3DERR_OUTOFVIDEOMEMORY
;
371 object
->lpVtbl
= &Direct3DDevice8_Vtbl
;
372 object
->device_parent_vtbl
= &d3d8_wined3d_device_parent_vtbl
;
374 object
->handle_table
.entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
375 D3D8_INITIAL_HANDLE_TABLE_SIZE
* sizeof(*object
->handle_table
.entries
));
376 object
->handle_table
.table_size
= D3D8_INITIAL_HANDLE_TABLE_SIZE
;
377 *ppReturnedDeviceInterface
= (IDirect3DDevice8
*)object
;
379 /* Allocate an associated WineD3DDevice object */
380 wined3d_mutex_lock();
381 hr
= IWineD3D_CreateDevice(This
->WineD3D
, Adapter
, DeviceType
, hFocusWindow
, BehaviourFlags
,
382 (IUnknown
*)object
, (IWineD3DDeviceParent
*)&object
->device_parent_vtbl
, &object
->WineD3DDevice
);
385 HeapFree(GetProcessHeap(), 0, object
);
386 *ppReturnedDeviceInterface
= NULL
;
387 wined3d_mutex_unlock();
392 TRACE("(%p) : Created Device %p\n", This
, object
);
394 localParameters
.BackBufferWidth
= pPresentationParameters
->BackBufferWidth
;
395 localParameters
.BackBufferHeight
= pPresentationParameters
->BackBufferHeight
;
396 localParameters
.BackBufferFormat
= wined3dformat_from_d3dformat(pPresentationParameters
->BackBufferFormat
);
397 localParameters
.BackBufferCount
= pPresentationParameters
->BackBufferCount
;
398 localParameters
.MultiSampleType
= pPresentationParameters
->MultiSampleType
;
399 localParameters
.MultiSampleQuality
= 0; /* d3d9 only */
400 localParameters
.SwapEffect
= pPresentationParameters
->SwapEffect
;
401 localParameters
.hDeviceWindow
= pPresentationParameters
->hDeviceWindow
;
402 localParameters
.Windowed
= pPresentationParameters
->Windowed
;
403 localParameters
.EnableAutoDepthStencil
= pPresentationParameters
->EnableAutoDepthStencil
;
404 localParameters
.AutoDepthStencilFormat
= wined3dformat_from_d3dformat(pPresentationParameters
->AutoDepthStencilFormat
);
405 localParameters
.Flags
= pPresentationParameters
->Flags
;
406 localParameters
.FullScreen_RefreshRateInHz
= pPresentationParameters
->FullScreen_RefreshRateInHz
;
407 localParameters
.PresentationInterval
= pPresentationParameters
->FullScreen_PresentationInterval
;
408 localParameters
.AutoRestoreDisplayMode
= TRUE
;
410 if(BehaviourFlags
& D3DCREATE_MULTITHREADED
) {
411 IWineD3DDevice_SetMultithreaded(object
->WineD3DDevice
);
414 hr
= IWineD3DDevice_Init3D(object
->WineD3DDevice
, &localParameters
);
415 wined3d_mutex_unlock();
417 pPresentationParameters
->BackBufferWidth
= localParameters
.BackBufferWidth
;
418 pPresentationParameters
->BackBufferHeight
= localParameters
.BackBufferHeight
;
419 pPresentationParameters
->BackBufferFormat
= d3dformat_from_wined3dformat(localParameters
.BackBufferFormat
);
420 pPresentationParameters
->BackBufferCount
= localParameters
.BackBufferCount
;
421 pPresentationParameters
->MultiSampleType
= localParameters
.MultiSampleType
;
422 pPresentationParameters
->SwapEffect
= localParameters
.SwapEffect
;
423 pPresentationParameters
->hDeviceWindow
= localParameters
.hDeviceWindow
;
424 pPresentationParameters
->Windowed
= localParameters
.Windowed
;
425 pPresentationParameters
->EnableAutoDepthStencil
= localParameters
.EnableAutoDepthStencil
;
426 pPresentationParameters
->AutoDepthStencilFormat
= d3dformat_from_wined3dformat(localParameters
.AutoDepthStencilFormat
);
427 pPresentationParameters
->Flags
= localParameters
.Flags
;
428 pPresentationParameters
->FullScreen_RefreshRateInHz
= localParameters
.FullScreen_RefreshRateInHz
;
429 pPresentationParameters
->FullScreen_PresentationInterval
= localParameters
.PresentationInterval
;
432 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This
, object
->WineD3DDevice
);
433 HeapFree(GetProcessHeap(), 0, object
);
434 *ppReturnedDeviceInterface
= NULL
;
437 object
->declArraySize
= 16;
438 object
->decls
= HeapAlloc(GetProcessHeap(), 0, object
->declArraySize
* sizeof(*object
->decls
));
440 ERR("Out of memory\n");
442 wined3d_mutex_lock();
443 IWineD3DDevice_Release(object
->WineD3DDevice
);
444 wined3d_mutex_unlock();
446 HeapFree(GetProcessHeap(), 0, object
);
447 *ppReturnedDeviceInterface
= NULL
;
453 const IDirect3D8Vtbl Direct3D8_Vtbl
=
456 IDirect3D8Impl_QueryInterface
,
457 IDirect3D8Impl_AddRef
,
458 IDirect3D8Impl_Release
,
460 IDirect3D8Impl_RegisterSoftwareDevice
,
461 IDirect3D8Impl_GetAdapterCount
,
462 IDirect3D8Impl_GetAdapterIdentifier
,
463 IDirect3D8Impl_GetAdapterModeCount
,
464 IDirect3D8Impl_EnumAdapterModes
,
465 IDirect3D8Impl_GetAdapterDisplayMode
,
466 IDirect3D8Impl_CheckDeviceType
,
467 IDirect3D8Impl_CheckDeviceFormat
,
468 IDirect3D8Impl_CheckDeviceMultiSampleType
,
469 IDirect3D8Impl_CheckDepthStencilMatch
,
470 IDirect3D8Impl_GetDeviceCaps
,
471 IDirect3D8Impl_GetAdapterMonitor
,
472 IDirect3D8Impl_CreateDevice