opengl32: Avoid generating a wrapper for internal functions when we can call the...
[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     return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
85 }
86
87 static UINT WINAPI IDirect3D8Impl_GetAdapterCount (LPDIRECT3D8 iface) {
88     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
89     return IWineD3D_GetAdapterCount(This->WineD3D);
90 }
91
92 static HRESULT  WINAPI  IDirect3D8Impl_GetAdapterIdentifier       (LPDIRECT3D8 iface,
93                                                             UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER8* pIdentifier) {
94     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
95     WINED3DADAPTER_IDENTIFIER adapter_id;
96
97     /* dx8 and dx9 have different structures to be filled in, with incompatible 
98        layouts so pass in pointers to the places to be filled via an internal 
99        structure                                                                */
100     adapter_id.Driver           = pIdentifier->Driver;
101     adapter_id.Description      = pIdentifier->Description;
102     adapter_id.DeviceName       = NULL; /* d3d9 only */
103     adapter_id.DriverVersion    = &pIdentifier->DriverVersion;
104     adapter_id.VendorId         = &pIdentifier->VendorId;
105     adapter_id.DeviceId         = &pIdentifier->DeviceId;
106     adapter_id.SubSysId         = &pIdentifier->SubSysId;
107     adapter_id.Revision         = &pIdentifier->Revision;
108     adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
109     adapter_id.WHQLLevel        = &pIdentifier->WHQLLevel;
110
111     return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
112 }
113
114 static UINT WINAPI IDirect3D8Impl_GetAdapterModeCount (LPDIRECT3D8 iface,UINT Adapter) {
115     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
116     return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, 0 /* format */);
117 }
118
119 static HRESULT WINAPI IDirect3D8Impl_EnumAdapterModes (LPDIRECT3D8 iface, UINT Adapter, UINT Mode, D3DDISPLAYMODE* pMode) {
120     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
121     /* FIXME: USe a constant WINED3DFOTMAT_ANY, or something similar */
122     return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, 0 /* format */, Mode, (WINED3DDISPLAYMODE *) pMode);
123 }
124
125 static HRESULT WINAPI IDirect3D8Impl_GetAdapterDisplayMode (LPDIRECT3D8 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
126     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
127     return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
128 }
129
130 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceType            (LPDIRECT3D8 iface,
131                                                             UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
132                                                             D3DFORMAT BackBufferFormat, BOOL Windowed) {
133     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
134     return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
135                                     BackBufferFormat, Windowed);
136 }
137
138 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceFormat          (LPDIRECT3D8 iface,
139                                                             UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
140                                                             DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
141     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
142     return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
143                                     Usage, RType, CheckFormat);
144 }
145
146 static HRESULT  WINAPI  IDirect3D8Impl_CheckDeviceMultiSampleType(LPDIRECT3D8 iface,
147                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
148                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType) {
149     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
150     return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
151                                                Windowed, (WINED3DMULTISAMPLE_TYPE) MultiSampleType, NULL);
152 }
153
154 static HRESULT  WINAPI  IDirect3D8Impl_CheckDepthStencilMatch(LPDIRECT3D8 iface, 
155                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
156                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
157     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
158     return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
159                                            RenderTargetFormat, DepthStencilFormat);
160 }
161
162 static HRESULT  WINAPI  IDirect3D8Impl_GetDeviceCaps(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS8* pCaps) {
163     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
164     HRESULT hrc = D3D_OK;
165     WINED3DCAPS *pWineCaps;
166
167     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
168
169     if(NULL == pCaps){
170         return D3DERR_INVALIDCALL;
171     }
172     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
173     if(pWineCaps == NULL){
174         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
175     }
176     D3D8CAPSTOWINECAPS(pCaps, pWineCaps)
177     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
178     HeapFree(GetProcessHeap(), 0, pWineCaps);
179     TRACE("(%p) returning %p\n", This, pCaps);
180     return hrc;
181 }
182
183 static HMONITOR WINAPI  IDirect3D8Impl_GetAdapterMonitor(LPDIRECT3D8 iface, UINT Adapter) {
184     IDirect3D8Impl *This = (IDirect3D8Impl *)iface;
185     return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
186 }
187
188 /* Internal function called back during the CreateDevice to create a render target */
189 HRESULT WINAPI D3D8CB_CreateRenderTarget(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
190                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
191                                          DWORD MultisampleQuality, BOOL Lockable,
192                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
193     HRESULT res = D3D_OK;
194     IDirect3DSurface8Impl *d3dSurface = NULL;
195
196     TRACE("(%p) call back\n", device);
197     res = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)device, Width, Height, 
198                                          (D3DFORMAT)Format, MultiSample, Lockable, 
199                                          (IDirect3DSurface8 **)&d3dSurface);
200
201     if (SUCCEEDED(res)) {
202         *ppSurface = d3dSurface->wineD3DSurface;
203         d3dSurface->isImplicit = TRUE;
204         /* Implicit surfaces are created with an refcount of 0 */
205         IUnknown_Release((IUnknown *)d3dSurface);
206     } else {
207         *ppSurface = NULL;
208     }
209     return res;
210 }
211
212 ULONG WINAPI D3D8CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
213     IDirect3DSurface8Impl* surfaceParent;
214     TRACE("(%p) call back\n", pSurface);
215
216     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
217     surfaceParent->isImplicit = FALSE;
218     /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
219     return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
220 }
221
222 /* Callback for creating the inplicite swapchain when the device is created */
223 static HRESULT WINAPI D3D8CB_CreateAdditionalSwapChain(IUnknown *device,
224                                                 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
225                                                 IWineD3DSwapChain ** ppSwapChain){
226     HRESULT res = D3D_OK;
227     IDirect3DSwapChain8Impl *d3dSwapChain = NULL;
228     D3DPRESENT_PARAMETERS localParameters;
229     TRACE("(%p) call back\n", device);
230
231     localParameters.BackBufferWidth                = *(pPresentationParameters->BackBufferWidth);
232     localParameters.BackBufferHeight               = *(pPresentationParameters->BackBufferHeight);
233     localParameters.BackBufferFormat               = *(pPresentationParameters->BackBufferFormat);
234     localParameters.BackBufferCount                = *(pPresentationParameters->BackBufferCount);
235     localParameters.MultiSampleType                = *(pPresentationParameters->MultiSampleType);
236     /* d3d9 only */
237     /* localParameters.MultiSampleQuality             = *(pPresentationParameters->MultiSampleQuality); */
238     localParameters.SwapEffect                     = *(pPresentationParameters->SwapEffect);
239     localParameters.hDeviceWindow                  = *(pPresentationParameters->hDeviceWindow);
240     localParameters.Windowed                       = *(pPresentationParameters->Windowed);
241     localParameters.EnableAutoDepthStencil         = *(pPresentationParameters->EnableAutoDepthStencil);
242     localParameters.AutoDepthStencilFormat         = *(pPresentationParameters->AutoDepthStencilFormat);
243     localParameters.Flags                          = *(pPresentationParameters->Flags);
244     localParameters.FullScreen_RefreshRateInHz     = *(pPresentationParameters->FullScreen_RefreshRateInHz);
245     /* d3d9 only */
246     /*    localParameters.PresentationInterval           = *(pPresentationParameters->PresentationInterval); */
247
248     /*copy the presentation parameters*/
249     res = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)device, &localParameters, (IDirect3DSwapChain8 **)&d3dSwapChain);
250
251     if (SUCCEEDED(res)) {
252         *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
253         IUnknown_Release(d3dSwapChain->parentDevice);
254         d3dSwapChain->parentDevice = NULL;
255     } else {
256         *ppSwapChain = NULL;
257     }
258
259     /*Copy back the presentation parameters*/
260     *pPresentationParameters->BackBufferWidth               = localParameters.BackBufferWidth;
261     *pPresentationParameters->BackBufferHeight              = localParameters.BackBufferHeight;
262     *pPresentationParameters->BackBufferFormat              = localParameters.BackBufferFormat;
263     *pPresentationParameters->BackBufferCount               = localParameters.BackBufferCount;
264     *pPresentationParameters->MultiSampleType               = localParameters.MultiSampleType;
265     /* d3d9 only */
266     /* *pPresentationParameters->MultiSampleQuality            = localParameters.MultiSampleQuality; */
267     *pPresentationParameters->SwapEffect                    = localParameters.SwapEffect;
268     *pPresentationParameters->hDeviceWindow                 = localParameters.hDeviceWindow;
269     *pPresentationParameters->Windowed                      = localParameters.Windowed;
270     *pPresentationParameters->EnableAutoDepthStencil        = localParameters.EnableAutoDepthStencil;
271     *pPresentationParameters->AutoDepthStencilFormat        = localParameters.AutoDepthStencilFormat;
272     *pPresentationParameters->Flags                         = localParameters.Flags;
273     *pPresentationParameters->FullScreen_RefreshRateInHz    = localParameters.FullScreen_RefreshRateInHz;
274     /* d3d9 only */
275     /* *pPresentationParameters->PresentationInterval          = localParameters.PresentationInterval; */
276
277    return res;
278 }
279
280 /* Internal function called back during the CreateDevice to create a render target */
281 HRESULT WINAPI D3D8CB_CreateDepthStencilSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
282                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
283                                          DWORD MultisampleQuality, BOOL Discard,
284                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
285     HRESULT res = D3D_OK;
286     IDirect3DSurface8Impl *d3dSurface = NULL;
287     TRACE("(%p) call back\n", device);
288
289     res = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)device, Width, Height, 
290                                          (D3DFORMAT)Format, MultiSample, (IDirect3DSurface8 **)&d3dSurface);
291     if (SUCCEEDED(res)) {
292         *ppSurface = d3dSurface->wineD3DSurface;
293         d3dSurface->isImplicit = TRUE;
294         /* Implicit surfaces are created with an refcount of 0 */
295         IUnknown_Release((IUnknown *)d3dSurface);
296     }
297     return res;
298 }
299
300 ULONG WINAPI D3D8CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
301     IDirect3DSurface8Impl* surfaceParent;
302     TRACE("(%p) call back\n", pSurface);
303
304     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
305     surfaceParent->isImplicit = FALSE;
306     /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
307     return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
308 }
309
310 static HRESULT WINAPI IDirect3D8Impl_CreateDevice(LPDIRECT3D8 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
311                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
312                                             IDirect3DDevice8** ppReturnedDeviceInterface) {
313
314     IDirect3D8Impl       *This   = (IDirect3D8Impl *)iface;
315     IDirect3DDevice8Impl *object = NULL;
316     WINED3DPRESENT_PARAMETERS localParameters;
317     HRESULT hr;
318     TRACE("(%p) Relay\n", This);
319
320     /* Check the validity range of the adapter parameter */
321     if (Adapter >= IDirect3D8Impl_GetAdapterCount(iface)) {
322         *ppReturnedDeviceInterface = NULL;
323         return D3DERR_INVALIDCALL;
324     }
325
326     /* Allocate the storage for the device object */
327     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice8Impl));
328     if (NULL == object) {
329         FIXME("Allocation of memory failed\n");
330         *ppReturnedDeviceInterface = NULL;
331         return D3DERR_OUTOFVIDEOMEMORY;
332     }
333
334     object->lpVtbl = &Direct3DDevice8_Vtbl;
335     object->ref = 1;
336     object->shader_handles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, INITIAL_SHADER_HANDLE_TABLE_SIZE * sizeof(shader_handle));
337     object->shader_handle_table_size = INITIAL_SHADER_HANDLE_TABLE_SIZE;
338     *ppReturnedDeviceInterface = (IDirect3DDevice8 *)object;
339
340     /* Allocate an associated WineD3DDevice object */
341     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;             
342     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
343     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
344     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
345     localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
346     /* d3d9 only */
347     localParameters.MultiSampleQuality             = NULL;
348     localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *) &pPresentationParameters->SwapEffect;
349     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
350     localParameters.Windowed                       = &pPresentationParameters->Windowed;
351     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
352     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
353     localParameters.Flags                          = &pPresentationParameters->Flags;
354     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz;
355     localParameters.PresentationInterval           = &pPresentationParameters->FullScreen_PresentationInterval;/* Renamed in dx9 */
356
357     hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &object->WineD3DDevice, (IUnknown *)object);
358
359     if (hr != D3D_OK) {
360         HeapFree(GetProcessHeap(), 0, object);
361         *ppReturnedDeviceInterface = NULL;
362         return hr;
363     }
364
365     TRACE("(%p) : Created Device %p\n", This, object);
366
367     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters, D3D8CB_CreateAdditionalSwapChain);
368     if (hr != D3D_OK) {
369         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
370         HeapFree(GetProcessHeap(), 0, object);
371         *ppReturnedDeviceInterface = NULL;
372     }
373
374     return hr;
375 }
376
377 const IDirect3D8Vtbl Direct3D8_Vtbl =
378 {
379     /* IUnknown */
380     IDirect3D8Impl_QueryInterface,
381     IDirect3D8Impl_AddRef,
382     IDirect3D8Impl_Release,
383     /* IDirect3D8 */
384     IDirect3D8Impl_RegisterSoftwareDevice,
385     IDirect3D8Impl_GetAdapterCount,
386     IDirect3D8Impl_GetAdapterIdentifier,
387     IDirect3D8Impl_GetAdapterModeCount,
388     IDirect3D8Impl_EnumAdapterModes,
389     IDirect3D8Impl_GetAdapterDisplayMode,
390     IDirect3D8Impl_CheckDeviceType,
391     IDirect3D8Impl_CheckDeviceFormat,
392     IDirect3D8Impl_CheckDeviceMultiSampleType,
393     IDirect3D8Impl_CheckDepthStencilMatch,
394     IDirect3D8Impl_GetDeviceCaps,
395     IDirect3D8Impl_GetAdapterMonitor,
396     IDirect3D8Impl_CreateDevice
397 };