fltlib: Add a stub dll.
[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
75         wined3d_mutex_lock();
76         IWineD3D_Release(This->WineD3D);
77         wined3d_mutex_unlock();
78
79         HeapFree(GetProcessHeap(), 0, This);
80     }
81
82     return ref;
83 }
84
85 /* IDirect3D8 Interface follow: */
86 static HRESULT WINAPI IDirect3D8Impl_RegisterSoftwareDevice (LPDIRECT3D8 iface, void* pInitializeFunction) {
87     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
88     HRESULT hr;
89     TRACE("(%p)->(%p)\n", This, pInitializeFunction);
90
91     wined3d_mutex_lock();
92     hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
93     wined3d_mutex_unlock();
94
95     return hr;
96 }
97
98 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
99     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
100     HRESULT hr;
101     TRACE("(%p)\n", This);
102
103     wined3d_mutex_lock();
104     hr = IWineD3D_GetAdapterCount(This->WineD3D);
105     wined3d_mutex_unlock();
106
107     return hr;
108 }
109
110 static HRESULT WINAPI IDirect3D8Impl_GetAdapterIdentifier(LPDIRECT3D8 iface,
111         UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8 *pIdentifier)
112 {
113     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
114     WINED3DADAPTER_IDENTIFIER adapter_id;
115     HRESULT hr;
116
117     TRACE("(%p)->(%d,%08x, %p\n", This, Adapter, Flags, pIdentifier);
118
119     adapter_id.driver = pIdentifier->Driver;
120     adapter_id.driver_size = sizeof(pIdentifier->Driver);
121     adapter_id.description = pIdentifier->Description;
122     adapter_id.description_size = sizeof(pIdentifier->Description);
123     adapter_id.device_name = NULL; /* d3d9 only */
124     adapter_id.device_name_size = 0; /* d3d9 only */
125
126     wined3d_mutex_lock();
127     hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
128     wined3d_mutex_unlock();
129
130     pIdentifier->DriverVersion = adapter_id.driver_version;
131     pIdentifier->VendorId = adapter_id.vendor_id;
132     pIdentifier->DeviceId = adapter_id.device_id;
133     pIdentifier->SubSysId = adapter_id.subsystem_id;
134     pIdentifier->Revision = adapter_id.revision;
135     memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
136     pIdentifier->WHQLLevel = adapter_id.whql_level;
137
138     return hr;
139 }
140
141 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
142     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
143     HRESULT hr;
144     TRACE("(%p)->(%d)\n", This, Adapter);
145
146     wined3d_mutex_lock();
147     hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
148     wined3d_mutex_unlock();
149
150     return hr;
151 }
152
153 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
154     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
155     HRESULT hr;
156     TRACE("(%p)->(%d, %d, %p)\n", This, Adapter, Mode, pMode);
157
158     wined3d_mutex_lock();
159     hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, WINED3DFMT_UNKNOWN, Mode, (WINED3DDISPLAYMODE *) pMode);
160     wined3d_mutex_unlock();
161
162     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
163
164     return hr;
165 }
166
167 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
168     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
169     HRESULT hr;
170     TRACE("(%p)->(%d,%p)\n", This, Adapter, pMode);
171
172     wined3d_mutex_lock();
173     hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
174     wined3d_mutex_unlock();
175
176     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
177
178     return hr;
179 }
180
181 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
182                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
183                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
184     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
185     HRESULT hr;
186     TRACE("(%p)->(%d, %d, %d, %d, %s)\n", This, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed ? "true" : "false");
187
188     wined3d_mutex_lock();
189     hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
190             wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
191     wined3d_mutex_unlock();
192
193     return hr;
194 }
195
196 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
197                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
198                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
199     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
200     HRESULT hr;
201     WINED3DRESOURCETYPE WineD3DRType;
202     TRACE("(%p)->(%d, %d, %d, %08x, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
203
204     if(CheckFormat == D3DFMT_R8G8B8)
205     {
206         /* See comment in dlls/d3d9/directx.c, IDirect3D9Impl_CheckDeviceFormat for details */
207         WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
208         return D3DERR_NOTAVAILABLE;
209     }
210
211
212     switch(RType) {
213         case D3DRTYPE_VERTEXBUFFER:
214         case D3DRTYPE_INDEXBUFFER:
215             WineD3DRType = WINED3DRTYPE_BUFFER;
216             break;
217
218         default:
219             WineD3DRType = RType;
220             break;
221     }
222
223     wined3d_mutex_lock();
224     hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
225             Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
226     wined3d_mutex_unlock();
227
228     return hr;
229 }
230
231 static HRESULT WINAPI IDirect3D8Impl_CheckDeviceMultiSampleType(IDirect3D8 *iface, UINT Adapter,
232         D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType)
233 {
234     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
235     HRESULT hr;
236     TRACE("(%p)-<(%d, %d, %d, %s, %d)\n", This, Adapter, DeviceType, SurfaceFormat, Windowed ? "true" : "false", MultiSampleType);
237
238     wined3d_mutex_lock();
239     hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
240             wined3dformat_from_d3dformat(SurfaceFormat), Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
241     wined3d_mutex_unlock();
242
243     return hr;
244 }
245
246 static HRESULT WINAPI IDirect3D8Impl_CheckDepthStencilMatch(IDirect3D8 *iface, UINT Adapter, D3DDEVTYPE DeviceType,
247         D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
248 {
249     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
250     HRESULT hr;
251     TRACE("(%p)-<(%d, %d, %d, %d, %d)\n", This, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
252
253     wined3d_mutex_lock();
254     hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
255             wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
256             wined3dformat_from_d3dformat(DepthStencilFormat));
257     wined3d_mutex_unlock();
258
259     return hr;
260 }
261
262 void fixup_caps(WINED3DCAPS *caps)
263 {
264     /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
265     if (caps->PixelShaderVersion > D3DPS_VERSION(1,4)) {
266         caps->PixelShaderVersion = D3DPS_VERSION(1,4);
267     }
268     if (caps->VertexShaderVersion > D3DVS_VERSION(1,1)) {
269         caps->VertexShaderVersion = D3DVS_VERSION(1,1);
270     }
271     caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
272
273     caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
274 }
275
276 static HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
277     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
278     HRESULT hrc = D3D_OK;
279     WINED3DCAPS *pWineCaps;
280
281     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
282
283     if(NULL == pCaps){
284         return D3DERR_INVALIDCALL;
285     }
286     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
287     if(pWineCaps == NULL){
288         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
289     }
290
291     wined3d_mutex_lock();
292     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
293     wined3d_mutex_unlock();
294
295     fixup_caps(pWineCaps);
296     WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
297     HeapFree(GetProcessHeap(), 0, pWineCaps);
298
299     TRACE("(%p) returning %p\n", This, pCaps);
300     return hrc;
301 }
302
303 static HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
304     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
305     HMONITOR ret;
306     TRACE("(%p)->(%d)\n", This, Adapter);
307
308     wined3d_mutex_lock();
309     ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
310     wined3d_mutex_unlock();
311
312     return ret;
313 }
314
315 ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
316     IUnknown* swapChainParent;
317     TRACE("(%p) call back\n", pSwapChain);
318
319     IWineD3DSwapChain_GetParent(pSwapChain, &swapChainParent);
320     IUnknown_Release(swapChainParent);
321     return IUnknown_Release(swapChainParent);
322 }
323
324 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
325                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
326                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
327
328     IDirect3D8Impl       *This   = (IDirect3D8Impl *)iface;
329     IDirect3DDevice8Impl *object = NULL;
330     WINED3DPRESENT_PARAMETERS localParameters;
331     HRESULT hr;
332     TRACE("(%p) Relay\n", This);
333
334     /* Check the validity range of the adapter parameter */
335     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
336         *ppReturnedDeviceInterface = NULL;
337         return D3DERR_INVALIDCALL;
338     }
339
340     /* Allocate the storage for the device object */
341     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
342     if (NULL == object) {
343         FIXME("Allocation of memory failed\n");
344         *ppReturnedDeviceInterface = NULL;
345         return D3DERR_OUTOFVIDEOMEMORY;
346     }
347
348     object->lpVtbl = &Direct3DDevice8_Vtbl;
349     object->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
350     object->ref = 1;
351     object->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
352             D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*object->handle_table.entries));
353     object->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
354     *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
355
356     /* Allocate an associated WineD3DDevice object */
357     wined3d_mutex_lock();
358     hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
359             (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
360
361     if (hr != D3D_OK) {
362         HeapFree(GetProcessHeap(), 0, object);
363         *ppReturnedDeviceInterface = NULL;
364         wined3d_mutex_unlock();
365
366         return hr;
367     }
368
369     TRACE("(%p) : Created Device %p\n", This, object);
370
371     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
372     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
373     localParameters.BackBufferFormat                            = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
374     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
375     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
376     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
377     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
378     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
379     localParameters.Windowed                                    = pPresentationParameters->Windowed;
380     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
381     localParameters.AutoDepthStencilFormat                      = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
382     localParameters.Flags                                       = pPresentationParameters->Flags;
383     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
384     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
385     localParameters.AutoRestoreDisplayMode                      = TRUE;
386
387     if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
388         IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
389     }
390
391     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters);
392     wined3d_mutex_unlock();
393
394     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
395     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
396     pPresentationParameters->BackBufferFormat                   = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
397     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
398     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
399     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
400     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
401     pPresentationParameters->Windowed                           = localParameters.Windowed;
402     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
403     pPresentationParameters->AutoDepthStencilFormat             = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
404     pPresentationParameters->Flags                              = localParameters.Flags;
405     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
406     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
407
408     if (hr != D3D_OK) {
409         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
410         HeapFree(GetProcessHeap(), 0, object);
411         *ppReturnedDeviceInterface = NULL;
412     }
413
414     object->declArraySize = 16;
415     object->decls = HeapAlloc(GetProcessHeap(), 0, object->declArraySize * sizeof(*object->decls));
416     if(!object->decls) {
417         ERR("Out of memory\n");
418
419         wined3d_mutex_lock();
420         IWineD3DDevice_Release(object->WineD3DDevice);
421         wined3d_mutex_unlock();
422
423         HeapFree(GetProcessHeap(), 0, object);
424         *ppReturnedDeviceInterface = NULL;
425         hr = E_OUTOFMEMORY;
426     }
427     return hr;
428 }
429
430 const IDirect3D8Vtbl Direct3D8_Vtbl =
431 {
432     /* IUnknown */
433     IDirect3D8Impl_QueryInterface,
434     IDirect3D8Impl_AddRef,
435     IDirect3D8Impl_Release,
436     /* IDirect3D8 */
437     IDirect3D8Impl_RegisterSoftwareDevice,
438     IDirect3D8Impl_GetAdapterCount,
439     IDirect3D8Impl_GetAdapterIdentifier,
440     IDirect3D8Impl_GetAdapterModeCount,
441     IDirect3D8Impl_EnumAdapterModes,
442     IDirect3D8Impl_GetAdapterDisplayMode,
443     IDirect3D8Impl_CheckDeviceType,
444     IDirect3D8Impl_CheckDeviceFormat,
445     IDirect3D8Impl_CheckDeviceMultiSampleType,
446     IDirect3D8Impl_CheckDepthStencilMatch,
447     IDirect3D8Impl_GetDeviceCaps,
448     IDirect3D8Impl_GetAdapterMonitor,
449     IDirect3D8Impl_CreateDevice
450 };