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 static HRESULT WINAPI
IDirect3D8Impl_CreateDevice(IDirect3D8
*iface
, UINT adapter
, D3DDEVTYPE device_type
,
335 HWND focus_window
, DWORD flags
, D3DPRESENT_PARAMETERS
*parameters
, IDirect3DDevice8
**device
)
337 IDirect3D8Impl
*This
= (IDirect3D8Impl
*)iface
;
338 IDirect3DDevice8Impl
*object
;
341 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
342 iface
, adapter
, device_type
, focus_window
, flags
, parameters
, device
);
344 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
347 ERR("Failed to allocate device memory.\n");
348 return E_OUTOFMEMORY
;
351 hr
= device_init(object
, This
->WineD3D
, adapter
, device_type
, focus_window
, flags
, parameters
);
354 WARN("Failed to initialize device, hr %#x.\n", hr
);
355 HeapFree(GetProcessHeap(), 0, object
);
359 TRACE("Created device %p.\n", object
);
360 *device
= (IDirect3DDevice8
*)object
;
365 const IDirect3D8Vtbl Direct3D8_Vtbl
=
368 IDirect3D8Impl_QueryInterface
,
369 IDirect3D8Impl_AddRef
,
370 IDirect3D8Impl_Release
,
372 IDirect3D8Impl_RegisterSoftwareDevice
,
373 IDirect3D8Impl_GetAdapterCount
,
374 IDirect3D8Impl_GetAdapterIdentifier
,
375 IDirect3D8Impl_GetAdapterModeCount
,
376 IDirect3D8Impl_EnumAdapterModes
,
377 IDirect3D8Impl_GetAdapterDisplayMode
,
378 IDirect3D8Impl_CheckDeviceType
,
379 IDirect3D8Impl_CheckDeviceFormat
,
380 IDirect3D8Impl_CheckDeviceMultiSampleType
,
381 IDirect3D8Impl_CheckDepthStencilMatch
,
382 IDirect3D8Impl_GetDeviceCaps
,
383 IDirect3D8Impl_GetAdapterMonitor
,
384 IDirect3D8Impl_CreateDevice