comctl32: Constify some variables.
[wine] / dlls / d3d8 / directx.c
1 /*
2  * IDirect3D8 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2003-2004 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26
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"
35
36 #include "d3d8_private.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39
40 /* IDirect3D IUnknown parts follow: */
41 static HRESULT WINAPI IDirect3D8Impl_QueryInterface(LPDIRECT3D8 iface, REFIID riid,LPVOID *ppobj)
42 {
43     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
44
45     if (IsEqualGUID(riid, &IID_IUnknown)
46         || IsEqualGUID(riid, &IID_IDirect3D8)) {
47         IUnknown_AddRef(iface);
48         *ppobj = This;
49         return S_OK;
50     }
51
52     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid),ppobj);
53     *ppobj = NULL;
54     return E_NOINTERFACE;
55 }
56
57 static ULONG WINAPI IDirect3D8Impl_AddRef(LPDIRECT3D8 iface) {
58     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
59     ULONG ref = InterlockedIncrement(&This->ref);
60
61     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
62
63     return ref;
64 }
65
66 static ULONG WINAPI IDirect3D8Impl_Release(LPDIRECT3D8 iface) {
67     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
68     ULONG ref = InterlockedDecrement(&This->ref);
69
70     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
71
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);
78     }
79
80     return ref;
81 }
82
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);
88
89     EnterCriticalSection(&d3d8_cs);
90     hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
91     LeaveCriticalSection(&d3d8_cs);
92     return hr;
93 }
94
95 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
96     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
97     HRESULT hr;
98     TRACE("(%p)\n", This);
99
100     EnterCriticalSection(&d3d8_cs);
101     hr = IWineD3D_GetAdapterCount(This->WineD3D);
102     LeaveCriticalSection(&d3d8_cs);
103     return hr;
104 }
105
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;
111
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;
127
128     hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
129     LeaveCriticalSection(&d3d8_cs);
130     return hr;
131 }
132
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);
137
138     EnterCriticalSection(&d3d8_cs);
139     hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
140     LeaveCriticalSection(&d3d8_cs);
141     return hr;
142 }
143
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);
148
149     EnterCriticalSection(&d3d8_cs);
150     hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
151     LeaveCriticalSection(&d3d8_cs);
152
153     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
154
155     return hr;
156 }
157
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);
162
163     EnterCriticalSection(&d3d8_cs);
164     hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
165     LeaveCriticalSection(&d3d8_cs);
166
167     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
168
169     return hr;
170 }
171
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");
178
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;
184 }
185
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);
193
194     switch(RType) {
195         case D3DRTYPE_VERTEXBUFFER:
196         case D3DRTYPE_INDEXBUFFER:
197             WineD3DRType = WINED3DRTYPE_BUFFER;
198             break;
199
200         default:
201             WineD3DRType = RType;
202             break;
203     }
204
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;
210 }
211
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);
218
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;
224 }
225
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);
232
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;
239 }
240
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;
245
246     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
247
248     if(NULL == pCaps){
249         return D3DERR_INVALIDCALL;
250     }
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*/
254     }
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);
260
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);
264     }
265     if(pCaps->VertexShaderVersion > D3DVS_VERSION(1,1)){
266         pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
267     }
268     pCaps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, pCaps->MaxVertexShaderConst);
269
270     TRACE("(%p) returning %p\n", This, pCaps);
271     return hrc;
272 }
273
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);
278
279     EnterCriticalSection(&d3d8_cs);
280     ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
281     LeaveCriticalSection(&d3d8_cs);
282     return ret;
283 }
284
285 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
286     IDirect3DSurface8Impl* surfaceParent;
287     TRACE("(%p) call back\n", pSurface);
288
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);
293 }
294
295 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
296     IUnknown* swapChainParent;
297     TRACE("(%p) call back\n", pSwapChain);
298
299     IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
300     IUnknown_Release(swapChainParent);
301     return IUnknown_Release(swapChainParent);
302 }
303
304 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
305     IDirect3DSurface8Impl* surfaceParent;
306     TRACE("(%p) call back\n", pSurface);
307
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);
312 }
313
314 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
315                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
316                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
317
318     IDirect3D8Impl       *This   = (IDirect3D8Impl *)iface;
319     IDirect3DDevice8Impl *object = NULL;
320     WINED3DPRESENT_PARAMETERS localParameters;
321     HRESULT hr;
322     TRACE("(%p) Relay\n", This);
323
324     /* Check the validity range of the adapter parameter */
325     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
326         *ppReturnedDeviceInterface = NULL;
327         return D3DERR_INVALIDCALL;
328     }
329
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;
336     }
337
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;
345
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);
350
351     if (hr != D3D_OK) {
352         HeapFree(GetProcessHeap(), 0, object);
353         *ppReturnedDeviceInterface = NULL;
354         LeaveCriticalSection(&d3d8_cs);
355         return hr;
356     }
357
358     TRACE("(%p) : Created Device %p\n", This, object);
359
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;
375
376     if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
377         IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
378     }
379
380     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
381     LeaveCriticalSection(&d3d8_cs);
382
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;
396
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;
401     }
402
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;
413     }
414     return hr;
415 }
416
417 const IDirect3D8Vtbl Direct3D8_Vtbl =
418 {
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
437 };