crypt32/tests: Fix some test failures on Win9x.
[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         IWineD3D_Release(This->WineD3D);
75         HeapFree(GetProcessHeap(), 0, This);
76     }
77
78     return ref;
79 }
80
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);
86
87     EnterCriticalSection(&d3d8_cs);
88     hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
89     LeaveCriticalSection(&d3d8_cs);
90     return hr;
91 }
92
93 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
94     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
95     HRESULT hr;
96     TRACE("(%p)\n", This);
97
98     EnterCriticalSection(&d3d8_cs);
99     hr = IWineD3D_GetAdapterCount(This->WineD3D);
100     LeaveCriticalSection(&d3d8_cs);
101     return hr;
102 }
103
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;
109
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;
125
126     hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
127     LeaveCriticalSection(&d3d8_cs);
128     return hr;
129 }
130
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);
135
136     EnterCriticalSection(&d3d8_cs);
137     hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
138     LeaveCriticalSection(&d3d8_cs);
139     return hr;
140 }
141
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);
146
147     EnterCriticalSection(&d3d8_cs);
148     hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
149     LeaveCriticalSection(&d3d8_cs);
150
151     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
152
153     return hr;
154 }
155
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);
160
161     EnterCriticalSection(&d3d8_cs);
162     hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
163     LeaveCriticalSection(&d3d8_cs);
164
165     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
166
167     return hr;
168 }
169
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");
176
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;
182 }
183
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);
190
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;
196 }
197
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);
204
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;
210 }
211
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);
218
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;
225 }
226
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;
231
232     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
233
234     if(NULL == pCaps){
235         return D3DERR_INVALIDCALL;
236     }
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*/
240     }
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);
246
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);
250     }
251     if(pCaps->VertexShaderVersion > D3DVS_VERSION(1,1)){
252         pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
253     }
254
255     TRACE("(%p) returning %p\n", This, pCaps);
256     return hrc;
257 }
258
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);
263
264     EnterCriticalSection(&d3d8_cs);
265     ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
266     LeaveCriticalSection(&d3d8_cs);
267     return ret;
268 }
269
270 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
271     IDirect3DSurface8Impl* surfaceParent;
272     TRACE("(%p) call back\n", pSurface);
273
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);
278 }
279
280 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
281     IUnknown* swapChainParent;
282     TRACE("(%p) call back\n", pSwapChain);
283
284     IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
285     IUnknown_Release(swapChainParent);
286     return IUnknown_Release(swapChainParent);
287 }
288
289 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
290     IDirect3DSurface8Impl* surfaceParent;
291     TRACE("(%p) call back\n", pSurface);
292
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);
297 }
298
299 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
300                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
301                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
302
303     IDirect3D8Impl       *This   = (IDirect3D8Impl *)iface;
304     IDirect3DDevice8Impl *object = NULL;
305     WINED3DPRESENT_PARAMETERS localParameters;
306     HRESULT hr;
307     TRACE("(%p) Relay\n", This);
308
309     /* Check the validity range of the adapter parameter */
310     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
311         *ppReturnedDeviceInterface = NULL;
312         return D3DERR_INVALIDCALL;
313     }
314
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;
321     }
322
323     object->lpVtbl = &Direct3DDevice8_Vtbl;
324     object->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
325     object->ref = 1;
326     object->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
327             D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*object->handle_table.entries));
328     object->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
329     *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
330
331     /* Allocate an associated WineD3DDevice object */
332     EnterCriticalSection(&d3d8_cs);
333     hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
334             (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
335
336     if (hr != D3D_OK) {
337         HeapFree(GetProcessHeap(), 0, object);
338         *ppReturnedDeviceInterface = NULL;
339         LeaveCriticalSection(&d3d8_cs);
340         return hr;
341     }
342
343     TRACE("(%p) : Created Device %p\n", This, object);
344
345     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
346     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
347     localParameters.BackBufferFormat                            = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
348     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
349     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
350     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
351     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
352     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
353     localParameters.Windowed                                    = pPresentationParameters->Windowed;
354     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
355     localParameters.AutoDepthStencilFormat                      = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
356     localParameters.Flags                                       = pPresentationParameters->Flags;
357     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
358     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
359     localParameters.AutoRestoreDisplayMode                      = TRUE;
360
361     if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
362         IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
363     }
364
365     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
366     LeaveCriticalSection(&d3d8_cs);
367
368     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
369     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
370     pPresentationParameters->BackBufferFormat                   = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
371     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
372     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
373     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
374     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
375     pPresentationParameters->Windowed                           = localParameters.Windowed;
376     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
377     pPresentationParameters->AutoDepthStencilFormat             = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
378     pPresentationParameters->Flags                              = localParameters.Flags;
379     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
380     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
381
382     if (hr != D3D_OK) {
383         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
384         HeapFree(GetProcessHeap(), 0, object);
385         *ppReturnedDeviceInterface = NULL;
386     }
387
388     object->declArraySize = 16;
389     object->decls = HeapAlloc(GetProcessHeap(), 0, object->declArraySize * sizeof(*object->decls));
390     if(!object->decls) {
391         ERR("Out of memory\n");
392         IWineD3DDevice_Release(object->WineD3DDevice);
393         HeapFree(GetProcessHeap(), 0, object);
394         *ppReturnedDeviceInterface = NULL;
395         hr = E_OUTOFMEMORY;
396     }
397     return hr;
398 }
399
400 const IDirect3D8Vtbl Direct3D8_Vtbl =
401 {
402     /* IUnknown */
403     IDirect3D8Impl_QueryInterface,
404     IDirect3D8Impl_AddRef,
405     IDirect3D8Impl_Release,
406     /* IDirect3D8 */
407     IDirect3D8Impl_RegisterSoftwareDevice,
408     IDirect3D8Impl_GetAdapterCount,
409     IDirect3D8Impl_GetAdapterIdentifier,
410     IDirect3D8Impl_GetAdapterModeCount,
411     IDirect3D8Impl_EnumAdapterModes,
412     IDirect3D8Impl_GetAdapterDisplayMode,
413     IDirect3D8Impl_CheckDeviceType,
414     IDirect3D8Impl_CheckDeviceFormat,
415     IDirect3D8Impl_CheckDeviceMultiSampleType,
416     IDirect3D8Impl_CheckDepthStencilMatch,
417     IDirect3D8Impl_GetDeviceCaps,
418     IDirect3D8Impl_GetAdapterMonitor,
419     IDirect3D8Impl_CreateDevice
420 };