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 if (IsEqualGUID(riid
, &IID_IUnknown
)
46 || IsEqualGUID(riid
, &IID_IDirect3D8
)) {
47 IUnknown_AddRef(iface
);
52 WARN("(%p)->(%s,%p),not found\n", This
, debugstr_guid(riid
),ppobj
);
57 static ULONG WINAPI
IDirect3D8Impl_AddRef(LPDIRECT3D8 iface
) {
58 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
59 ULONG ref
= InterlockedIncrement(&This
->ref
);
61 TRACE("(%p) : AddRef from %d\n", This
, ref
- 1);
66 static ULONG WINAPI
IDirect3D8Impl_Release(LPDIRECT3D8 iface
) {
67 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
68 ULONG ref
= InterlockedDecrement(&This
->ref
);
70 TRACE("(%p) : ReleaseRef to %d\n", This
, ref
);
73 TRACE("Releasing wined3d %p\n", This
->WineD3D
);
74 EnterCriticalSection(&d3d8_cs
);
75 IWineD3D_Release(This
->WineD3D
);
76 LeaveCriticalSection(&d3d8_cs
);
77 HeapFree(GetProcessHeap(), 0, This
);
83 /* IDirect3D8 Interface follow: */
84 static HRESULT WINAPI
IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface
, void* pInitializeFunction
) {
85 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
87 TRACE("(%p)->(%p)\n", This
, pInitializeFunction
);
89 EnterCriticalSection(&d3d8_cs
);
90 hr
= IWineD3D_RegisterSoftwareDevice(This
->WineD3D
, pInitializeFunction
);
91 LeaveCriticalSection(&d3d8_cs
);
95 static UINT WINAPI
IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface
) {
96 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
98 TRACE("(%p)\n", This
);
100 EnterCriticalSection(&d3d8_cs
);
101 hr
= IWineD3D_GetAdapterCount(This
->WineD3D
);
102 LeaveCriticalSection(&d3d8_cs
);
106 static HRESULT WINAPI
IDirect3D8Impl_GetAdapterIdentifier (LPDIRECT3D8 iface
,
107 UINT Adapter
, DWORD Flags
, D3DADAPTER_IDENTIFIER8
* pIdentifier
) {
108 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
109 WINED3DADAPTER_IDENTIFIER adapter_id
;
112 TRACE("(%p)->(%d,%08x, %p\n", This
, Adapter
, Flags
, pIdentifier
);
113 EnterCriticalSection(&d3d8_cs
);
114 /* dx8 and dx9 have different structures to be filled in, with incompatible
115 layouts so pass in pointers to the places to be filled via an internal
117 adapter_id
.Driver
= pIdentifier
->Driver
;
118 adapter_id
.Description
= pIdentifier
->Description
;
119 adapter_id
.DeviceName
= NULL
; /* d3d9 only */
120 adapter_id
.DriverVersion
= &pIdentifier
->DriverVersion
;
121 adapter_id
.VendorId
= &pIdentifier
->VendorId
;
122 adapter_id
.DeviceId
= &pIdentifier
->DeviceId
;
123 adapter_id
.SubSysId
= &pIdentifier
->SubSysId
;
124 adapter_id
.Revision
= &pIdentifier
->Revision
;
125 adapter_id
.DeviceIdentifier
= &pIdentifier
->DeviceIdentifier
;
126 adapter_id
.WHQLLevel
= &pIdentifier
->WHQLLevel
;
128 hr
= IWineD3D_GetAdapterIdentifier(This
->WineD3D
, Adapter
, Flags
, &adapter_id
);
129 LeaveCriticalSection(&d3d8_cs
);
133 static UINT WINAPI
IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface
,UINT Adapter
) {
134 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
136 TRACE("(%p)->(%d)\n", This
, Adapter
);
138 EnterCriticalSection(&d3d8_cs
);
139 hr
= IWineD3D_GetAdapterModeCount(This
->WineD3D
, Adapter
, 0 /* format */);
140 LeaveCriticalSection(&d3d8_cs
);
144 static HRESULT WINAPI
IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface
, UINT Adapter
, UINT Mode
, D3DDISPLAYMODE
* pMode
) {
145 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
147 TRACE("(%p)->(%d, %d, %p)\n", This
, Adapter
, Mode
, pMode
);
149 EnterCriticalSection(&d3d8_cs
);
150 hr
= IWineD3D_EnumAdapterModes(This
->WineD3D
, Adapter
, WINED3DFMT_UNKNOWN
, Mode
, (WINED3DDISPLAYMODE
*) pMode
);
151 LeaveCriticalSection(&d3d8_cs
);
153 if (SUCCEEDED(hr
)) pMode
->Format
= d3dformat_from_wined3dformat(pMode
->Format
);
158 static HRESULT WINAPI
IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface
, UINT Adapter
, D3DDISPLAYMODE
* pMode
) {
159 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
161 TRACE("(%p)->(%d,%p)\n", This
, Adapter
, pMode
);
163 EnterCriticalSection(&d3d8_cs
);
164 hr
= IWineD3D_GetAdapterDisplayMode(This
->WineD3D
, Adapter
, (WINED3DDISPLAYMODE
*) pMode
);
165 LeaveCriticalSection(&d3d8_cs
);
167 if (SUCCEEDED(hr
)) pMode
->Format
= d3dformat_from_wined3dformat(pMode
->Format
);
172 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface
,
173 UINT Adapter
, D3DDEVTYPE CheckType
, D3DFORMAT DisplayFormat
,
174 D3DFORMAT BackBufferFormat
, BOOL Windowed
) {
175 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
177 TRACE("(%p)->(%d, %d, %d, %d, %s)\n", This
, Adapter
, CheckType
, DisplayFormat
, BackBufferFormat
, Windowed
? "true" : "false");
179 EnterCriticalSection(&d3d8_cs
);
180 hr
= IWineD3D_CheckDeviceType(This
->WineD3D
, Adapter
, CheckType
, wined3dformat_from_d3dformat(DisplayFormat
),
181 wined3dformat_from_d3dformat(BackBufferFormat
), Windowed
);
182 LeaveCriticalSection(&d3d8_cs
);
186 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface
,
187 UINT Adapter
, D3DDEVTYPE DeviceType
, D3DFORMAT AdapterFormat
,
188 DWORD Usage
, D3DRESOURCETYPE RType
, D3DFORMAT CheckFormat
) {
189 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
191 WINED3DRESOURCETYPE WineD3DRType
;
192 TRACE("(%p)->(%d, %d, %d, %08x, %d, %d)\n", This
, Adapter
, DeviceType
, AdapterFormat
, Usage
, RType
, CheckFormat
);
195 case D3DRTYPE_VERTEXBUFFER
:
196 case D3DRTYPE_INDEXBUFFER
:
197 WineD3DRType
= WINED3DRTYPE_BUFFER
;
201 WineD3DRType
= RType
;
205 EnterCriticalSection(&d3d8_cs
);
206 hr
= IWineD3D_CheckDeviceFormat(This
->WineD3D
, Adapter
, DeviceType
, wined3dformat_from_d3dformat(AdapterFormat
),
207 Usage
, WineD3DRType
, wined3dformat_from_d3dformat(CheckFormat
), SURFACE_OPENGL
);
208 LeaveCriticalSection(&d3d8_cs
);
212 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface
,
213 UINT Adapter
, D3DDEVTYPE DeviceType
, D3DFORMAT SurfaceFormat
,
214 BOOL Windowed
, D3DMULTISAMPLE_TYPE MultiSampleType
) {
215 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
217 TRACE("(%p)-<(%d, %d, %d, %s, %d)\n", This
, Adapter
, DeviceType
, SurfaceFormat
, Windowed
? "true" : "false", MultiSampleType
);
219 EnterCriticalSection(&d3d8_cs
);
220 hr
= IWineD3D_CheckDeviceMultiSampleType(This
->WineD3D
, Adapter
, DeviceType
,
221 wined3dformat_from_d3dformat(SurfaceFormat
), Windowed
, (WINED3DMULTISAMPLE_TYPE
) MultiSampleType
, NULL
);
222 LeaveCriticalSection(&d3d8_cs
);
226 static HRESULT WINAPI
IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface
,
227 UINT Adapter
, D3DDEVTYPE DeviceType
, D3DFORMAT AdapterFormat
,
228 D3DFORMAT RenderTargetFormat
, D3DFORMAT DepthStencilFormat
) {
229 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
231 TRACE("(%p)-<(%d, %d, %d, %d, %d)\n", This
, Adapter
, DeviceType
, AdapterFormat
, RenderTargetFormat
, DepthStencilFormat
);
233 EnterCriticalSection(&d3d8_cs
);
234 hr
= IWineD3D_CheckDepthStencilMatch(This
->WineD3D
, Adapter
, DeviceType
,
235 wined3dformat_from_d3dformat(AdapterFormat
), wined3dformat_from_d3dformat(RenderTargetFormat
),
236 wined3dformat_from_d3dformat(DepthStencilFormat
));
237 LeaveCriticalSection(&d3d8_cs
);
241 static HRESULT WINAPI
IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface
, UINT Adapter
, D3DDEVTYPE DeviceType
, D3DCAPS8
* pCaps
) {
242 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
243 HRESULT hrc
= D3D_OK
;
244 WINED3DCAPS
*pWineCaps
;
246 TRACE("(%p) Relay %d %u %p\n", This
, Adapter
, DeviceType
, pCaps
);
249 return D3DERR_INVALIDCALL
;
251 pWineCaps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINED3DCAPS
));
252 if(pWineCaps
== NULL
){
253 return D3DERR_INVALIDCALL
; /*well this is what MSDN says to return*/
255 EnterCriticalSection(&d3d8_cs
);
256 hrc
= IWineD3D_GetDeviceCaps(This
->WineD3D
, Adapter
, DeviceType
, pWineCaps
);
257 LeaveCriticalSection(&d3d8_cs
);
258 WINECAPSTOD3D8CAPS(pCaps
, pWineCaps
)
259 HeapFree(GetProcessHeap(), 0, pWineCaps
);
261 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
262 if(pCaps
->PixelShaderVersion
> D3DPS_VERSION(1,4)){
263 pCaps
->PixelShaderVersion
= D3DPS_VERSION(1,4);
265 if(pCaps
->VertexShaderVersion
> D3DVS_VERSION(1,1)){
266 pCaps
->VertexShaderVersion
= D3DVS_VERSION(1,1);
269 TRACE("(%p) returning %p\n", This
, pCaps
);
273 static HMONITOR WINAPI
IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface
, UINT Adapter
) {
274 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
276 TRACE("(%p)->(%d)\n", This
, Adapter
);
278 EnterCriticalSection(&d3d8_cs
);
279 ret
= IWineD3D_GetAdapterMonitor(This
->WineD3D
, Adapter
);
280 LeaveCriticalSection(&d3d8_cs
);
284 ULONG WINAPI
D3D8CB_DestroyRenderTarget(IWineD3DSurface
*pSurface
) {
285 IDirect3DSurface8Impl
* surfaceParent
;
286 TRACE("(%p) call back\n", pSurface
);
288 IWineD3DSurface_GetParent(pSurface
, (IUnknown
**) &surfaceParent
);
289 surfaceParent
->isImplicit
= FALSE
;
290 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
291 return IDirect3DSurface8_Release((IDirect3DSurface8
*) surfaceParent
);
294 ULONG WINAPI
D3D8CB_DestroySwapChain(IWineD3DSwapChain
*pSwapChain
) {
295 IUnknown
* swapChainParent
;
296 TRACE("(%p) call back\n", pSwapChain
);
298 IWineD3DSwapChain_GetParent(pSwapChain
, &swapChainParent
);
299 IUnknown_Release(swapChainParent
);
300 return IUnknown_Release(swapChainParent
);
303 ULONG WINAPI
D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface
*pSurface
) {
304 IDirect3DSurface8Impl
* surfaceParent
;
305 TRACE("(%p) call back\n", pSurface
);
307 IWineD3DSurface_GetParent(pSurface
, (IUnknown
**) &surfaceParent
);
308 surfaceParent
->isImplicit
= FALSE
;
309 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
310 return IDirect3DSurface8_Release((IDirect3DSurface8
*) surfaceParent
);
313 static HRESULT WINAPI
IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface
, UINT Adapter
, D3DDEVTYPE DeviceType
, HWND hFocusWindow
,
314 DWORD BehaviourFlags
, D3DPRESENT_PARAMETERS
* pPresentationParameters
,
315 IDirect3DDevice8
** ppReturnedDeviceInterface
) {
317 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
318 IDirect3DDevice8Impl
*object
= NULL
;
319 WINED3DPRESENT_PARAMETERS localParameters
;
321 TRACE("(%p) Relay\n", This
);
323 /* Check the validity range of the adapter parameter */
324 if (Adapter
>= IDirect3D8Impl_GetAdapterCount(iface
)) {
325 *ppReturnedDeviceInterface
= NULL
;
326 return D3DERR_INVALIDCALL
;
329 /* Allocate the storage for the device object */
330 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(IDirect3DDevice8Impl
));
331 if (NULL
== object
) {
332 FIXME("Allocation of memory failed\n");
333 *ppReturnedDeviceInterface
= NULL
;
334 return D3DERR_OUTOFVIDEOMEMORY
;
337 object
->lpVtbl
= &Direct3DDevice8_Vtbl
;
338 object
->device_parent_vtbl
= &d3d8_wined3d_device_parent_vtbl
;
340 object
->handle_table
.entries
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
341 D3D8_INITIAL_HANDLE_TABLE_SIZE
* sizeof(*object
->handle_table
.entries
));
342 object
->handle_table
.table_size
= D3D8_INITIAL_HANDLE_TABLE_SIZE
;
343 *ppReturnedDeviceInterface
= (IDirect3DDevice8
*)object
;
345 /* Allocate an associated WineD3DDevice object */
346 EnterCriticalSection(&d3d8_cs
);
347 hr
= IWineD3D_CreateDevice(This
->WineD3D
, Adapter
, DeviceType
, hFocusWindow
, BehaviourFlags
,
348 (IUnknown
*)object
, (IWineD3DDeviceParent
*)&object
->device_parent_vtbl
, &object
->WineD3DDevice
);
351 HeapFree(GetProcessHeap(), 0, object
);
352 *ppReturnedDeviceInterface
= NULL
;
353 LeaveCriticalSection(&d3d8_cs
);
357 TRACE("(%p) : Created Device %p\n", This
, object
);
359 localParameters
.BackBufferWidth
= pPresentationParameters
->BackBufferWidth
;
360 localParameters
.BackBufferHeight
= pPresentationParameters
->BackBufferHeight
;
361 localParameters
.BackBufferFormat
= wined3dformat_from_d3dformat(pPresentationParameters
->BackBufferFormat
);
362 localParameters
.BackBufferCount
= pPresentationParameters
->BackBufferCount
;
363 localParameters
.MultiSampleType
= pPresentationParameters
->MultiSampleType
;
364 localParameters
.MultiSampleQuality
= 0; /* d3d9 only */
365 localParameters
.SwapEffect
= pPresentationParameters
->SwapEffect
;
366 localParameters
.hDeviceWindow
= pPresentationParameters
->hDeviceWindow
;
367 localParameters
.Windowed
= pPresentationParameters
->Windowed
;
368 localParameters
.EnableAutoDepthStencil
= pPresentationParameters
->EnableAutoDepthStencil
;
369 localParameters
.AutoDepthStencilFormat
= wined3dformat_from_d3dformat(pPresentationParameters
->AutoDepthStencilFormat
);
370 localParameters
.Flags
= pPresentationParameters
->Flags
;
371 localParameters
.FullScreen_RefreshRateInHz
= pPresentationParameters
->FullScreen_RefreshRateInHz
;
372 localParameters
.PresentationInterval
= pPresentationParameters
->FullScreen_PresentationInterval
;
373 localParameters
.AutoRestoreDisplayMode
= TRUE
;
375 if(BehaviourFlags
& D3DCREATE_MULTITHREADED
) {
376 IWineD3DDevice_SetMultithreaded(object
->WineD3DDevice
);
379 hr
= IWineD3DDevice_Init3D(object
->WineD3DDevice
, &localParameters
);
380 LeaveCriticalSection(&d3d8_cs
);
382 pPresentationParameters
->BackBufferWidth
= localParameters
.BackBufferWidth
;
383 pPresentationParameters
->BackBufferHeight
= localParameters
.BackBufferHeight
;
384 pPresentationParameters
->BackBufferFormat
= d3dformat_from_wined3dformat(localParameters
.BackBufferFormat
);
385 pPresentationParameters
->BackBufferCount
= localParameters
.BackBufferCount
;
386 pPresentationParameters
->MultiSampleType
= localParameters
.MultiSampleType
;
387 pPresentationParameters
->SwapEffect
= localParameters
.SwapEffect
;
388 pPresentationParameters
->hDeviceWindow
= localParameters
.hDeviceWindow
;
389 pPresentationParameters
->Windowed
= localParameters
.Windowed
;
390 pPresentationParameters
->EnableAutoDepthStencil
= localParameters
.EnableAutoDepthStencil
;
391 pPresentationParameters
->AutoDepthStencilFormat
= d3dformat_from_wined3dformat(localParameters
.AutoDepthStencilFormat
);
392 pPresentationParameters
->Flags
= localParameters
.Flags
;
393 pPresentationParameters
->FullScreen_RefreshRateInHz
= localParameters
.FullScreen_RefreshRateInHz
;
394 pPresentationParameters
->FullScreen_PresentationInterval
= localParameters
.PresentationInterval
;
397 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This
, object
->WineD3DDevice
);
398 HeapFree(GetProcessHeap(), 0, object
);
399 *ppReturnedDeviceInterface
= NULL
;
402 object
->declArraySize
= 16;
403 object
->decls
= HeapAlloc(GetProcessHeap(), 0, object
->declArraySize
* sizeof(*object
->decls
));
405 ERR("Out of memory\n");
406 EnterCriticalSection(&d3d8_cs
);
407 IWineD3DDevice_Release(object
->WineD3DDevice
);
408 LeaveCriticalSection(&d3d8_cs
);
409 HeapFree(GetProcessHeap(), 0, object
);
410 *ppReturnedDeviceInterface
= NULL
;
416 const IDirect3D8Vtbl Direct3D8_Vtbl
=
419 IDirect3D8Impl_QueryInterface
,
420 IDirect3D8Impl_AddRef
,
421 IDirect3D8Impl_Release
,
423 IDirect3D8Impl_RegisterSoftwareDevice
,
424 IDirect3D8Impl_GetAdapterCount
,
425 IDirect3D8Impl_GetAdapterIdentifier
,
426 IDirect3D8Impl_GetAdapterModeCount
,
427 IDirect3D8Impl_EnumAdapterModes
,
428 IDirect3D8Impl_GetAdapterDisplayMode
,
429 IDirect3D8Impl_CheckDeviceType
,
430 IDirect3D8Impl_CheckDeviceFormat
,
431 IDirect3D8Impl_CheckDeviceMultiSampleType
,
432 IDirect3D8Impl_CheckDepthStencilMatch
,
433 IDirect3D8Impl_GetDeviceCaps
,
434 IDirect3D8Impl_GetAdapterMonitor
,
435 IDirect3D8Impl_CreateDevice