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 static inline IDirect3D8Impl
*impl_from_IDirect3D8(IDirect3D8
*iface
)
42 return CONTAINING_RECORD(iface
, IDirect3D8Impl
, IDirect3D8_iface
);
45 static HRESULT WINAPI
IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface
, REFIID riid
,LPVOID
*ppobj
)
47 TRACE("iface %p, riid %s, object %p.\n", iface
, debugstr_guid(riid
), ppobj
);
49 if (IsEqualGUID(riid
, &IID_IDirect3D8
)
50 || IsEqualGUID(riid
, &IID_IUnknown
))
52 IUnknown_AddRef(iface
);
57 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid
));
63 static ULONG WINAPI
IDirect3D8Impl_AddRef(LPDIRECT3D8 iface
)
65 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
66 ULONG ref
= InterlockedIncrement(&This
->ref
);
68 TRACE("%p increasing refcount to %u.\n", iface
, ref
);
73 static ULONG WINAPI
IDirect3D8Impl_Release(LPDIRECT3D8 iface
)
75 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
76 ULONG ref
= InterlockedDecrement(&This
->ref
);
78 TRACE("%p decreasing refcount to %u.\n", iface
, ref
);
81 TRACE("Releasing wined3d %p\n", This
->WineD3D
);
84 wined3d_decref(This
->WineD3D
);
85 wined3d_mutex_unlock();
87 HeapFree(GetProcessHeap(), 0, This
);
93 static HRESULT WINAPI
IDirect3D8Impl_RegisterSoftwareDevice(LPDIRECT3D8 iface
,
94 void* pInitializeFunction
)
96 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
99 TRACE("iface %p, init_function %p.\n", iface
, pInitializeFunction
);
101 wined3d_mutex_lock();
102 hr
= wined3d_register_software_device(This
->WineD3D
, pInitializeFunction
);
103 wined3d_mutex_unlock();
108 static UINT WINAPI
IDirect3D8Impl_GetAdapterCount(LPDIRECT3D8 iface
)
110 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
113 TRACE("iface %p.\n", iface
);
115 wined3d_mutex_lock();
116 hr
= wined3d_get_adapter_count(This
->WineD3D
);
117 wined3d_mutex_unlock();
122 static HRESULT WINAPI
IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface
, UINT Adapter
,
123 DWORD Flags
, D3DADAPTER_IDENTIFIER8
*pIdentifier
)
125 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
126 struct wined3d_adapter_identifier adapter_id
;
129 TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
130 iface
, Adapter
, Flags
, pIdentifier
);
132 adapter_id
.driver
= pIdentifier
->Driver
;
133 adapter_id
.driver_size
= sizeof(pIdentifier
->Driver
);
134 adapter_id
.description
= pIdentifier
->Description
;
135 adapter_id
.description_size
= sizeof(pIdentifier
->Description
);
136 adapter_id
.device_name
= NULL
; /* d3d9 only */
137 adapter_id
.device_name_size
= 0; /* d3d9 only */
139 wined3d_mutex_lock();
140 hr
= wined3d_get_adapter_identifier(This
->WineD3D
, Adapter
, Flags
, &adapter_id
);
141 wined3d_mutex_unlock();
143 pIdentifier
->DriverVersion
= adapter_id
.driver_version
;
144 pIdentifier
->VendorId
= adapter_id
.vendor_id
;
145 pIdentifier
->DeviceId
= adapter_id
.device_id
;
146 pIdentifier
->SubSysId
= adapter_id
.subsystem_id
;
147 pIdentifier
->Revision
= adapter_id
.revision
;
148 memcpy(&pIdentifier
->DeviceIdentifier
, &adapter_id
.device_identifier
, sizeof(pIdentifier
->DeviceIdentifier
));
149 pIdentifier
->WHQLLevel
= adapter_id
.whql_level
;
154 static UINT WINAPI
IDirect3D8Impl_GetAdapterModeCount(LPDIRECT3D8 iface
,UINT Adapter
)
156 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
159 TRACE("iface %p, adapter %u.\n", iface
, Adapter
);
161 wined3d_mutex_lock();
162 hr
= wined3d_get_adapter_mode_count(This
->WineD3D
, Adapter
, 0);
163 wined3d_mutex_unlock();
168 static HRESULT WINAPI
IDirect3D8Impl_EnumAdapterModes(LPDIRECT3D8 iface
, UINT Adapter
, UINT Mode
,
169 D3DDISPLAYMODE
* pMode
)
171 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
174 TRACE("iface %p, adapter %u, mode_idx %u, mode %p.\n",
175 iface
, Adapter
, Mode
, pMode
);
177 wined3d_mutex_lock();
178 hr
= wined3d_enum_adapter_modes(This
->WineD3D
, Adapter
, WINED3DFMT_UNKNOWN
,
179 Mode
, (struct wined3d_display_mode
*)pMode
);
180 wined3d_mutex_unlock();
182 if (SUCCEEDED(hr
)) pMode
->Format
= d3dformat_from_wined3dformat(pMode
->Format
);
187 static HRESULT WINAPI
IDirect3D8Impl_GetAdapterDisplayMode(LPDIRECT3D8 iface
, UINT Adapter
,
188 D3DDISPLAYMODE
* pMode
)
190 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
193 TRACE("iface %p, adapter %u, mode %p.\n",
194 iface
, Adapter
, pMode
);
196 wined3d_mutex_lock();
197 hr
= wined3d_get_adapter_display_mode(This
->WineD3D
, Adapter
, (struct wined3d_display_mode
*)pMode
);
198 wined3d_mutex_unlock();
200 if (SUCCEEDED(hr
)) pMode
->Format
= d3dformat_from_wined3dformat(pMode
->Format
);
205 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceType(LPDIRECT3D8 iface
, UINT Adapter
,
206 D3DDEVTYPE CheckType
, D3DFORMAT DisplayFormat
, D3DFORMAT BackBufferFormat
, BOOL Windowed
)
208 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
211 TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
212 iface
, Adapter
, CheckType
, DisplayFormat
, BackBufferFormat
, Windowed
);
214 wined3d_mutex_lock();
215 hr
= wined3d_check_device_type(This
->WineD3D
, Adapter
, CheckType
, wined3dformat_from_d3dformat(DisplayFormat
),
216 wined3dformat_from_d3dformat(BackBufferFormat
), Windowed
);
217 wined3d_mutex_unlock();
222 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceFormat(LPDIRECT3D8 iface
, UINT Adapter
,
223 D3DDEVTYPE DeviceType
, D3DFORMAT AdapterFormat
, DWORD Usage
, D3DRESOURCETYPE RType
,
224 D3DFORMAT CheckFormat
)
226 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
227 enum wined3d_resource_type wined3d_rtype
;
230 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
231 iface
, Adapter
, DeviceType
, AdapterFormat
, Usage
, RType
, CheckFormat
);
234 case D3DRTYPE_VERTEXBUFFER
:
235 case D3DRTYPE_INDEXBUFFER
:
236 wined3d_rtype
= WINED3D_RTYPE_BUFFER
;
240 wined3d_rtype
= RType
;
244 wined3d_mutex_lock();
245 hr
= wined3d_check_device_format(This
->WineD3D
, Adapter
, DeviceType
, wined3dformat_from_d3dformat(AdapterFormat
),
246 Usage
, wined3d_rtype
, wined3dformat_from_d3dformat(CheckFormat
), WINED3D_SURFACE_TYPE_OPENGL
);
247 wined3d_mutex_unlock();
252 static HRESULT WINAPI
IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8
*iface
, UINT Adapter
,
253 D3DDEVTYPE DeviceType
, D3DFORMAT SurfaceFormat
, BOOL Windowed
,
254 D3DMULTISAMPLE_TYPE MultiSampleType
)
256 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
259 TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x.\n",
260 iface
, Adapter
, DeviceType
, SurfaceFormat
, Windowed
, MultiSampleType
);
262 wined3d_mutex_lock();
263 hr
= wined3d_check_device_multisample_type(This
->WineD3D
, Adapter
, DeviceType
,
264 wined3dformat_from_d3dformat(SurfaceFormat
), Windowed
,
265 (enum wined3d_multisample_type
)MultiSampleType
, NULL
);
266 wined3d_mutex_unlock();
271 static HRESULT WINAPI
IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8
*iface
, UINT Adapter
,
272 D3DDEVTYPE DeviceType
, D3DFORMAT AdapterFormat
, D3DFORMAT RenderTargetFormat
,
273 D3DFORMAT DepthStencilFormat
)
275 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
278 TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
279 iface
, Adapter
, DeviceType
, AdapterFormat
, RenderTargetFormat
, DepthStencilFormat
);
281 wined3d_mutex_lock();
282 hr
= wined3d_check_depth_stencil_match(This
->WineD3D
, Adapter
, DeviceType
,
283 wined3dformat_from_d3dformat(AdapterFormat
), wined3dformat_from_d3dformat(RenderTargetFormat
),
284 wined3dformat_from_d3dformat(DepthStencilFormat
));
285 wined3d_mutex_unlock();
290 void fixup_caps(WINED3DCAPS
*caps
)
292 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
293 if (caps
->PixelShaderVersion
)
294 caps
->PixelShaderVersion
= D3DPS_VERSION(1,4);
296 caps
->PixelShaderVersion
= D3DPS_VERSION(0,0);
297 if (caps
->VertexShaderVersion
)
298 caps
->VertexShaderVersion
= D3DVS_VERSION(1,1);
300 caps
->VertexShaderVersion
= D3DVS_VERSION(0,0);
301 caps
->MaxVertexShaderConst
= min(D3D8_MAX_VERTEX_SHADER_CONSTANTF
, caps
->MaxVertexShaderConst
);
303 caps
->StencilCaps
&= ~WINED3DSTENCILCAPS_TWOSIDED
;
306 static HRESULT WINAPI
IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface
, UINT Adapter
,
307 D3DDEVTYPE DeviceType
, D3DCAPS8
* pCaps
)
309 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
310 HRESULT hrc
= D3D_OK
;
311 WINED3DCAPS
*pWineCaps
;
313 TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface
, Adapter
, DeviceType
, pCaps
);
316 return D3DERR_INVALIDCALL
;
318 pWineCaps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINED3DCAPS
));
319 if(pWineCaps
== NULL
){
320 return D3DERR_INVALIDCALL
; /*well this is what MSDN says to return*/
323 wined3d_mutex_lock();
324 hrc
= wined3d_get_device_caps(This
->WineD3D
, Adapter
, DeviceType
, pWineCaps
);
325 wined3d_mutex_unlock();
327 fixup_caps(pWineCaps
);
328 WINECAPSTOD3D8CAPS(pCaps
, pWineCaps
)
329 HeapFree(GetProcessHeap(), 0, pWineCaps
);
331 TRACE("(%p) returning %p\n", This
, pCaps
);
335 static HMONITOR WINAPI
IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface
, UINT Adapter
)
337 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
340 TRACE("iface %p, adapter %u.\n", iface
, Adapter
);
342 wined3d_mutex_lock();
343 ret
= wined3d_get_adapter_monitor(This
->WineD3D
, Adapter
);
344 wined3d_mutex_unlock();
349 static HRESULT WINAPI
IDirect3D8Impl_CreateDevice(IDirect3D8
*iface
, UINT adapter
,
350 D3DDEVTYPE device_type
, HWND focus_window
, DWORD flags
, D3DPRESENT_PARAMETERS
*parameters
,
351 IDirect3DDevice8
**device
)
353 IDirect3D8Impl
*This
= impl_from_IDirect3D8(iface
);
354 IDirect3DDevice8Impl
*object
;
357 TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
358 iface
, adapter
, device_type
, focus_window
, flags
, parameters
, device
);
360 object
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*object
));
363 ERR("Failed to allocate device memory.\n");
364 return E_OUTOFMEMORY
;
367 hr
= device_init(object
, This
, This
->WineD3D
, adapter
, device_type
, focus_window
, flags
, parameters
);
370 WARN("Failed to initialize device, hr %#x.\n", hr
);
371 HeapFree(GetProcessHeap(), 0, object
);
375 TRACE("Created device %p.\n", object
);
376 *device
= &object
->IDirect3DDevice8_iface
;
381 const IDirect3D8Vtbl Direct3D8_Vtbl
=
384 IDirect3D8Impl_QueryInterface
,
385 IDirect3D8Impl_AddRef
,
386 IDirect3D8Impl_Release
,
388 IDirect3D8Impl_RegisterSoftwareDevice
,
389 IDirect3D8Impl_GetAdapterCount
,
390 IDirect3D8Impl_GetAdapterIdentifier
,
391 IDirect3D8Impl_GetAdapterModeCount
,
392 IDirect3D8Impl_EnumAdapterModes
,
393 IDirect3D8Impl_GetAdapterDisplayMode
,
394 IDirect3D8Impl_CheckDeviceType
,
395 IDirect3D8Impl_CheckDeviceFormat
,
396 IDirect3D8Impl_CheckDeviceMultiSampleType
,
397 IDirect3D8Impl_CheckDepthStencilMatch
,
398 IDirect3D8Impl_GetDeviceCaps
,
399 IDirect3D8Impl_GetAdapterMonitor
,
400 IDirect3D8Impl_CreateDevice