push cc8bc80451cc24f4d7cf75168b569f0ebfe19547
[wine/hacks.git] / dlls / d3d8 / directx.c
blobd39d3d647684e82c14ea328842341a9d4953b842
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 EnterCriticalSection(&d3d8_cs);
75 IWineD3D_Release(This->WineD3D);
76 LeaveCriticalSection(&d3d8_cs);
77 HeapFree(GetProcessHeap(), 0, This);
80 return ref;
83 /* IDirect3D8 Interface follow: */
84 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
85 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
86 HRESULT hr;
87 TRACE("(%p)->(%p)\n", This, pInitializeFunction);
89 EnterCriticalSection(&d3d8_cs);
90 hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
91 LeaveCriticalSection(&d3d8_cs);
92 return hr;
95 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
96 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
97 HRESULT hr;
98 TRACE("(%p)\n", This);
100 EnterCriticalSection(&d3d8_cs);
101 hr = IWineD3D_GetAdapterCount(This->WineD3D);
102 LeaveCriticalSection(&d3d8_cs);
103 return hr;
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;
110 HRESULT hr;
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
116 structure */
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);
130 return hr;
133 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
134 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
135 HRESULT hr;
136 TRACE("(%p)->(%d)\n", This, Adapter);
138 EnterCriticalSection(&d3d8_cs);
139 hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
140 LeaveCriticalSection(&d3d8_cs);
141 return hr;
144 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
145 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
146 HRESULT hr;
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);
155 return hr;
158 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
159 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
160 HRESULT hr;
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);
169 return hr;
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;
176 HRESULT hr;
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);
183 return hr;
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;
190 HRESULT hr;
191 WINED3DRESOURCETYPE WineD3DRType;
192 TRACE("(%p)->(%d, %d, %d, %08x, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
194 switch(RType) {
195 case D3DRTYPE_VERTEXBUFFER:
196 case D3DRTYPE_INDEXBUFFER:
197 WineD3DRType = WINED3DRTYPE_BUFFER;
198 break;
200 default:
201 WineD3DRType = RType;
202 break;
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);
209 return hr;
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;
216 HRESULT hr;
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);
223 return hr;
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;
230 HRESULT hr;
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);
238 return hr;
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);
248 if(NULL == 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);
268 pCaps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, pCaps->MaxVertexShaderConst);
270 TRACE("(%p) returning %p\n", This, pCaps);
271 return hrc;
274 static HMONITOR WINAPI IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
275 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
276 HMONITOR ret;
277 TRACE("(%p)->(%d)\n", This, Adapter);
279 EnterCriticalSection(&d3d8_cs);
280 ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
281 LeaveCriticalSection(&d3d8_cs);
282 return ret;
285 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
286 IDirect3DSurface8Impl* surfaceParent;
287 TRACE("(%p) call back\n", pSurface);
289 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
290 surfaceParent->isImplicit = FALSE;
291 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
292 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
295 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
296 IUnknown* swapChainParent;
297 TRACE("(%p) call back\n", pSwapChain);
299 IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
300 IUnknown_Release(swapChainParent);
301 return IUnknown_Release(swapChainParent);
304 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
305 IDirect3DSurface8Impl* surfaceParent;
306 TRACE("(%p) call back\n", pSurface);
308 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
309 surfaceParent->isImplicit = FALSE;
310 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
311 return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
314 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
315 DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
316 IDirect3DDevice8** ppReturnedDeviceInterface) {
318 IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
319 IDirect3DDevice8Impl *object = NULL;
320 WINED3DPRESENT_PARAMETERS localParameters;
321 HRESULT hr;
322 TRACE("(%p) Relay\n", This);
324 /* Check the validity range of the adapter parameter */
325 if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
326 *ppReturnedDeviceInterface = NULL;
327 return D3DERR_INVALIDCALL;
330 /* Allocate the storage for the device object */
331 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
332 if (NULL == object) {
333 FIXME("Allocation of memory failed\n");
334 *ppReturnedDeviceInterface = NULL;
335 return D3DERR_OUTOFVIDEOMEMORY;
338 object->lpVtbl = &Direct3DDevice8_Vtbl;
339 object->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
340 object->ref = 1;
341 object->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
342 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*object->handle_table.entries));
343 object->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
344 *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
346 /* Allocate an associated WineD3DDevice object */
347 EnterCriticalSection(&d3d8_cs);
348 hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
349 (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
351 if (hr != D3D_OK) {
352 HeapFree(GetProcessHeap(), 0, object);
353 *ppReturnedDeviceInterface = NULL;
354 LeaveCriticalSection(&d3d8_cs);
355 return hr;
358 TRACE("(%p) : Created Device %p\n", This, object);
360 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
361 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
362 localParameters.BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
363 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
364 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
365 localParameters.MultiSampleQuality = 0; /* d3d9 only */
366 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
367 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
368 localParameters.Windowed = pPresentationParameters->Windowed;
369 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
370 localParameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
371 localParameters.Flags = pPresentationParameters->Flags;
372 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
373 localParameters.PresentationInterval = pPresentationParameters->FullScreen_PresentationInterval;
374 localParameters.AutoRestoreDisplayMode = TRUE;
376 if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
377 IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
380 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
381 LeaveCriticalSection(&d3d8_cs);
383 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
384 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
385 pPresentationParameters->BackBufferFormat = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
386 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
387 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
388 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
389 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
390 pPresentationParameters->Windowed = localParameters.Windowed;
391 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
392 pPresentationParameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
393 pPresentationParameters->Flags = localParameters.Flags;
394 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
395 pPresentationParameters->FullScreen_PresentationInterval = localParameters.PresentationInterval;
397 if (hr != D3D_OK) {
398 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
399 HeapFree(GetProcessHeap(), 0, object);
400 *ppReturnedDeviceInterface = NULL;
403 object->declArraySize = 16;
404 object->decls = HeapAlloc(GetProcessHeap(), 0, object->declArraySize * sizeof(*object->decls));
405 if(!object->decls) {
406 ERR("Out of memory\n");
407 EnterCriticalSection(&d3d8_cs);
408 IWineD3DDevice_Release(object->WineD3DDevice);
409 LeaveCriticalSection(&d3d8_cs);
410 HeapFree(GetProcessHeap(), 0, object);
411 *ppReturnedDeviceInterface = NULL;
412 hr = E_OUTOFMEMORY;
414 return hr;
417 const IDirect3D8Vtbl Direct3D8_Vtbl =
419 /* IUnknown */
420 IDirect3D8Impl_QueryInterface,
421 IDirect3D8Impl_AddRef,
422 IDirect3D8Impl_Release,
423 /* IDirect3D8 */
424 IDirect3D8Impl_RegisterSoftwareDevice,
425 IDirect3D8Impl_GetAdapterCount,
426 IDirect3D8Impl_GetAdapterIdentifier,
427 IDirect3D8Impl_GetAdapterModeCount,
428 IDirect3D8Impl_EnumAdapterModes,
429 IDirect3D8Impl_GetAdapterDisplayMode,
430 IDirect3D8Impl_CheckDeviceType,
431 IDirect3D8Impl_CheckDeviceFormat,
432 IDirect3D8Impl_CheckDeviceMultiSampleType,
433 IDirect3D8Impl_CheckDepthStencilMatch,
434 IDirect3D8Impl_GetDeviceCaps,
435 IDirect3D8Impl_GetAdapterMonitor,
436 IDirect3D8Impl_CreateDevice