push 43f03fe87c2254c6df67b2de3c08b5b20fd64327
[wine/hacks.git] / dlls / d3d8 / directx.c
blobea2dc884e8ded70ba13664463b22e89ca616168e
1 /*
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
23 #include "config.h"
25 #include <stdarg.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
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);
48 *ppobj = This;
49 return S_OK;
52 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
53 *ppobj = NULL;
54 return E_NOINTERFACE;
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);
63 return ref;
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);
72 if (ref == 0) {
73 TRACE("Releasing wined3d %p\n", This->WineD3D);
74 IWineD3D_Release(This->WineD3D);
75 HeapFree(GetProcessHeap(), 0, This);
78 return ref;
81 /* IDirect3D8 Interface follow: */
82 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
83 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
84 HRESULT hr;
85 TRACE("(%p)->(%p)\n", This, pInitializeFunction);
87 EnterCriticalSection(&d3d8_cs);
88 hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
89 LeaveCriticalSection(&d3d8_cs);
90 return hr;
93 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
94 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
95 HRESULT hr;
96 TRACE("(%p)\n", This);
98 EnterCriticalSection(&d3d8_cs);
99 hr = IWineD3D_GetAdapterCount(This->WineD3D);
100 LeaveCriticalSection(&d3d8_cs);
101 return hr;
104 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier (LPDIRECT3D8 iface,
105 UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
106 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
107 WINED3DADAPTER_IDENTIFIER adapter_id;
108 HRESULT hr;
110 TRACE("(%p)->(%d,%08x, %p\n", This, Adapter, Flags, pIdentifier);
111 EnterCriticalSection(&d3d8_cs);
112 /* dx8 and dx9 have different structures to be filled in, with incompatible
113 layouts so pass in pointers to the places to be filled via an internal
114 structure */
115 adapter_id.Driver = pIdentifier->Driver;
116 adapter_id.Description = pIdentifier->Description;
117 adapter_id.DeviceName = NULL; /* d3d9 only */
118 adapter_id.DriverVersion = &pIdentifier->DriverVersion;
119 adapter_id.VendorId = &pIdentifier->VendorId;
120 adapter_id.DeviceId = &pIdentifier->DeviceId;
121 adapter_id.SubSysId = &pIdentifier->SubSysId;
122 adapter_id.Revision = &pIdentifier->Revision;
123 adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
124 adapter_id.WHQLLevel = &pIdentifier->WHQLLevel;
126 hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
127 LeaveCriticalSection(&d3d8_cs);
128 return hr;
131 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
132 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
133 HRESULT hr;
134 TRACE("(%p)->(%d)\n", This, Adapter);
136 EnterCriticalSection(&d3d8_cs);
137 hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
138 LeaveCriticalSection(&d3d8_cs);
139 return hr;
142 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
143 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
144 HRESULT hr;
145 TRACE("(%p)->(%d, %d, %p)\n", This, Adapter, Mode, pMode);
147 EnterCriticalSection(&d3d8_cs);
148 hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
149 LeaveCriticalSection(&d3d8_cs);
151 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
153 return hr;
156 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
157 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
158 HRESULT hr;
159 TRACE("(%p)->(%d,%p)\n", This, Adapter, pMode);
161 EnterCriticalSection(&d3d8_cs);
162 hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
163 LeaveCriticalSection(&d3d8_cs);
165 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
167 return hr;
170 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceType (LPDIRECT3D8 iface,
171 UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
172 D3DFORMAT BackBufferFormat, BOOL Windowed) {
173 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
174 HRESULT hr;
175 TRACE("(%p)->(%d, %d, %d, %d, %s)\n", This, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed ? "true" : "false");
177 EnterCriticalSection(&d3d8_cs);
178 hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
179 wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
180 LeaveCriticalSection(&d3d8_cs);
181 return hr;
184 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceFormat (LPDIRECT3D8 iface,
185 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
186 DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
187 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
188 HRESULT hr;
189 TRACE("(%p)->(%d, %d, %d, %08x, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
191 EnterCriticalSection(&d3d8_cs);
192 hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
193 Usage, RType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
194 LeaveCriticalSection(&d3d8_cs);
195 return hr;
198 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
199 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
200 BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
201 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
202 HRESULT hr;
203 TRACE("(%p)-<(%d, %d, %d, %s, %d)\n", This, Adapter, DeviceType, SurfaceFormat, Windowed ? "true" : "false", MultiSampleType);
205 EnterCriticalSection(&d3d8_cs);
206 hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
207 wined3dformat_from_d3dformat(SurfaceFormat), Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
208 LeaveCriticalSection(&d3d8_cs);
209 return hr;
212 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface,
213 UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
214 D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
215 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
216 HRESULT hr;
217 TRACE("(%p)-<(%d, %d, %d, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
219 EnterCriticalSection(&d3d8_cs);
220 hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
221 wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
222 wined3dformat_from_d3dformat(DepthStencilFormat));
223 LeaveCriticalSection(&d3d8_cs);
224 return hr;
227 static HRESULT WINAPI IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
228 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
229 HRESULT hrc = D3D_OK;
230 WINED3DCAPS *pWineCaps;
232 TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
234 if(NULL == pCaps){
235 return D3DERR_INVALIDCALL;
237 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
238 if(pWineCaps == NULL){
239 return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
241 EnterCriticalSection(&d3d8_cs);
242 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
243 LeaveCriticalSection(&d3d8_cs);
244 WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
245 HeapFree(GetProcessHeap(), 0, pWineCaps);
247 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
248 if(pCaps->PixelShaderVersion > D3DPS_VERSION(1,4)){
249 pCaps->PixelShaderVersion = D3DPS_VERSION(1,4);
251 if(pCaps->VertexShaderVersion > D3DVS_VERSION(1,1)){
252 pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
255 TRACE("(%p) returning %p\n", This, pCaps);
256 return hrc;
259 static HMONITOR WINAPI IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
260 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
261 HMONITOR ret;
262 TRACE("(%p)->(%d)\n", This, Adapter);
264 EnterCriticalSection(&d3d8_cs);
265 ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
266 LeaveCriticalSection(&d3d8_cs);
267 return ret;
270 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
271 IDirect3DSurface8Impl* surfaceParent;
272 TRACE("(%p) call back\n", pSurface);
274 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
275 surfaceParent->isImplicit = FALSE;
276 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
277 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
280 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
281 IUnknown* swapChainParent;
282 TRACE("(%p) call back\n", pSwapChain);
284 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
285 IUnknown_Release(swapChainParent);
286 return IUnknown_Release(swapChainParent);
289 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
290 IDirect3DSurface8Impl* surfaceParent;
291 TRACE("(%p) call back\n", pSurface);
293 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
294 surfaceParent->isImplicit = FALSE;
295 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
296 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
299 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
300 DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
301 IDirect3DDevice8** ppReturnedDeviceInterface) {
303 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
304 IDirect3DDevice8Impl *object = NULL;
305 WINED3DPRESENT_PARAMETERS localParameters;
306 HRESULT hr;
307 TRACE("(%p) Relay\n", This);
309 /* Check the validity range of the adapter parameter */
310 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
311 *ppReturnedDeviceInterface = NULL;
312 return D3DERR_INVALIDCALL;
315 /* Allocate the storage for the device object */
316 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
317 if (NULL == object) {
318 FIXME("Allocation of memory failed\n");
319 *ppReturnedDeviceInterface = NULL;
320 return D3DERR_OUTOFVIDEOMEMORY;
323 object->lpVtbl = &Direct3DDevice8_Vtbl;
324 object->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
325 object->ref = 1;
326 object->shader_handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, INITIAL_SHADER_HANDLE_TABLE_SIZE * sizeof(shader_handle));
327 object->shader_handle_table_size = INITIAL_SHADER_HANDLE_TABLE_SIZE;
328 *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
330 /* Allocate an associated WineD3DDevice object */
331 EnterCriticalSection(&d3d8_cs);
332 hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
333 (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
335 if (hr != D3D_OK) {
336 HeapFree(GetProcessHeap(), 0, object);
337 *ppReturnedDeviceInterface = NULL;
338 LeaveCriticalSection(&d3d8_cs);
339 return hr;
342 TRACE("(%p) : Created Device %p\n", This, object);
344 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
345 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
346 localParameters.BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
347 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
348 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
349 localParameters.MultiSampleQuality = 0; /* d3d9 only */
350 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
351 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
352 localParameters.Windowed = pPresentationParameters->Windowed;
353 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
354 localParameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
355 localParameters.Flags = pPresentationParameters->Flags;
356 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
357 localParameters.PresentationInterval = pPresentationParameters->FullScreen_PresentationInterval;
358 localParameters.AutoRestoreDisplayMode = TRUE;
360 if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
361 IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
364 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
365 LeaveCriticalSection(&d3d8_cs);
367 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
368 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
369 pPresentationParameters->BackBufferFormat = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
370 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
371 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
372 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
373 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
374 pPresentationParameters->Windowed = localParameters.Windowed;
375 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
376 pPresentationParameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
377 pPresentationParameters->Flags = localParameters.Flags;
378 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
379 pPresentationParameters->FullScreen_PresentationInterval = localParameters.PresentationInterval;
381 if (hr != D3D_OK) {
382 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
383 HeapFree(GetProcessHeap(), 0, object);
384 *ppReturnedDeviceInterface = NULL;
387 object->declArraySize = 16;
388 object->decls = HeapAlloc(GetProcessHeap(), 0, object->declArraySize * sizeof(*object->decls));
389 if(!object->decls) {
390 ERR("Out of memory\n");
391 IWineD3DDevice_Release(object->WineD3DDevice);
392 HeapFree(GetProcessHeap(), 0, object);
393 *ppReturnedDeviceInterface = NULL;
394 hr = E_OUTOFMEMORY;
396 return hr;
399 const IDirect3D8Vtbl Direct3D8_Vtbl =
401 /* IUnknown */
402 IDirect3D8Impl_QueryInterface,
403 IDirect3D8Impl_AddRef,
404 IDirect3D8Impl_Release,
405 /* IDirect3D8 */
406 IDirect3D8Impl_RegisterSoftwareDevice,
407 IDirect3D8Impl_GetAdapterCount,
408 IDirect3D8Impl_GetAdapterIdentifier,
409 IDirect3D8Impl_GetAdapterModeCount,
410 IDirect3D8Impl_EnumAdapterModes,
411 IDirect3D8Impl_GetAdapterDisplayMode,
412 IDirect3D8Impl_CheckDeviceType,
413 IDirect3D8Impl_CheckDeviceFormat,
414 IDirect3D8Impl_CheckDeviceMultiSampleType,
415 IDirect3D8Impl_CheckDepthStencilMatch,
416 IDirect3D8Impl_GetDeviceCaps,
417 IDirect3D8Impl_GetAdapterMonitor,
418 IDirect3D8Impl_CreateDevice