ntdsapi: Add DsMakeSpnA to the spec file.
[wine] / dlls / d3d9 / directx.c
1 /*
2  * IDirect3D9 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  * Copyright 2005 Oliver Stieber
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3D9 IUnknown parts follow: */
28 HRESULT WINAPI IDirect3D9Impl_QueryInterface(LPDIRECT3D9 iface, REFIID riid, LPVOID* ppobj)
29 {
30     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
31
32     if (IsEqualGUID(riid, &IID_IUnknown)
33         || IsEqualGUID(riid, &IID_IDirect3D9)) {
34         IUnknown_AddRef(iface);
35         *ppobj = This;
36         return D3D_OK;
37     }
38
39     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
40     return E_NOINTERFACE;
41 }
42
43 ULONG WINAPI IDirect3D9Impl_AddRef(LPDIRECT3D9 iface) {
44     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
45     ULONG ref = InterlockedIncrement(&This->ref);
46
47     TRACE("(%p) : AddRef from %ld\n", This, ref - 1);
48
49     return ref;
50 }
51
52 ULONG WINAPI IDirect3D9Impl_Release(LPDIRECT3D9 iface) {
53     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
54     ULONG ref = InterlockedDecrement(&This->ref);
55
56     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
57
58     if (ref == 0) {
59         IWineD3D_Release(This->WineD3D);
60         HeapFree(GetProcessHeap(), 0, This);
61     }
62
63     return ref;
64 }
65
66 /* IDirect3D9 Interface follow: */
67 HRESULT  WINAPI  IDirect3D9Impl_RegisterSoftwareDevice(LPDIRECT3D9 iface, void* pInitializeFunction) {
68     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
69     return IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
70 }
71
72 UINT     WINAPI  IDirect3D9Impl_GetAdapterCount(LPDIRECT3D9 iface) {
73     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
74     return IWineD3D_GetAdapterCount(This->WineD3D);
75 }
76
77 HRESULT WINAPI IDirect3D9Impl_GetAdapterIdentifier(LPDIRECT3D9 iface, UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier) {
78     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
79     WINED3DADAPTER_IDENTIFIER adapter_id;
80
81     /* dx8 and dx9 have different structures to be filled in, with incompatible 
82        layouts so pass in pointers to the places to be filled via an internal 
83        structure                                                                */
84     adapter_id.Driver           = pIdentifier->Driver;          
85     adapter_id.Description      = pIdentifier->Description;     
86     adapter_id.DeviceName       = pIdentifier->DeviceName;      
87     adapter_id.DriverVersion    = &pIdentifier->DriverVersion;   
88     adapter_id.VendorId         = &pIdentifier->VendorId;        
89     adapter_id.DeviceId         = &pIdentifier->DeviceId;        
90     adapter_id.SubSysId         = &pIdentifier->SubSysId;        
91     adapter_id.Revision         = &pIdentifier->Revision;        
92     adapter_id.DeviceIdentifier = &pIdentifier->DeviceIdentifier;
93     adapter_id.WHQLLevel        = &pIdentifier->WHQLLevel;       
94
95     return IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
96 }
97
98 UINT WINAPI IDirect3D9Impl_GetAdapterModeCount(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format) {
99     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
100     return IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, Format);
101 }
102
103 HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(LPDIRECT3D9 iface, UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
104     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
105     return IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, Format, Mode, (WINED3DDISPLAYMODE *) pMode);
106 }
107
108 HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9 iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
109     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
110     return IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
111 }
112
113 HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(LPDIRECT3D9 iface,
114                                               UINT Adapter, D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat,
115                                               D3DFORMAT BackBufferFormat, BOOL Windowed) {
116     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
117     return IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, DisplayFormat,
118                                     BackBufferFormat, Windowed);
119 }
120
121 HRESULT  WINAPI  IDirect3D9Impl_CheckDeviceFormat(LPDIRECT3D9 iface,
122                                                   UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
123                                                   DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat) {
124     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
125     return IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, AdapterFormat,
126                                     Usage, RType, CheckFormat);
127 }
128
129 HRESULT  WINAPI  IDirect3D9Impl_CheckDeviceMultiSampleType(LPDIRECT3D9 iface,
130                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
131                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels) {
132     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
133     return IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType, SurfaceFormat,
134                                                Windowed, MultiSampleType, pQualityLevels);
135 }
136
137 HRESULT  WINAPI  IDirect3D9Impl_CheckDepthStencilMatch(LPDIRECT3D9 iface, 
138                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
139                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
140     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
141     return IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType, AdapterFormat,
142                                            RenderTargetFormat, DepthStencilFormat);
143 }
144
145 HRESULT  WINAPI  IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
146     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
147     return IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType, SourceFormat,
148                                                 TargetFormat);
149 }
150
151 HRESULT  WINAPI  IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
152     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
153     HRESULT hrc = D3D_OK;
154     WINED3DCAPS *pWineCaps;
155
156     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
157
158     if(NULL == pCaps){
159         return D3DERR_INVALIDCALL;
160     }
161     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
162     if(pWineCaps == NULL){
163         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
164     }
165     D3D9CAPSTOWINECAPS(pCaps, pWineCaps)
166     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
167     HeapFree(GetProcessHeap(), 0, pWineCaps);
168     TRACE("(%p) returning %p\n", This, pCaps);
169     return hrc;
170 }
171
172 HMONITOR WINAPI  IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9 iface, UINT Adapter) {
173     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
174     return IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
175 }
176
177 /* Internal function called back during the CreateDevice to create a render target */
178 HRESULT WINAPI D3D9CB_CreateRenderTarget(IUnknown *device, UINT Width, UINT Height,
179                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
180                                          DWORD MultisampleQuality, BOOL Lockable,
181                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
182     HRESULT res = D3D_OK;
183     IDirect3DSurface9Impl *d3dSurface = NULL;
184     TRACE("(%p) call back\n", device);
185     res = IDirect3DDevice9_CreateRenderTarget((IDirect3DDevice9 *)device, Width, Height, 
186                                          (D3DFORMAT)Format, MultiSample, MultisampleQuality, Lockable,
187                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
188
189     if (SUCCEEDED(res)) {
190         *ppSurface = d3dSurface->wineD3DSurface;
191     } else {
192         *ppSurface = NULL;
193     }
194     return res;
195 }
196
197 HRESULT WINAPI D3D9CB_CreateAdditionalSwapChain(IUnknown *device,
198                                                 WINED3DPRESENT_PARAMETERS* pPresentationParameters,
199                                                 IWineD3DSwapChain ** ppSwapChain) {
200     HRESULT res = D3D_OK;
201     IDirect3DSwapChain9Impl *d3dSwapChain = NULL;
202     D3DPRESENT_PARAMETERS localParameters;
203     TRACE("(%p) call back\n", device);
204
205     localParameters.BackBufferWidth                = *(pPresentationParameters->BackBufferWidth);
206     localParameters.BackBufferHeight               = *(pPresentationParameters->BackBufferHeight);
207     localParameters.BackBufferFormat               = *(pPresentationParameters->BackBufferFormat);
208     localParameters.BackBufferCount                = *(pPresentationParameters->BackBufferCount);
209     localParameters.MultiSampleType                = *(pPresentationParameters->MultiSampleType);
210     localParameters.MultiSampleQuality             = *(pPresentationParameters->MultiSampleQuality);
211     localParameters.SwapEffect                     = *(pPresentationParameters->SwapEffect);
212     localParameters.hDeviceWindow                  = *(pPresentationParameters->hDeviceWindow);
213     localParameters.Windowed                       = *(pPresentationParameters->Windowed);
214     localParameters.EnableAutoDepthStencil         = *(pPresentationParameters->EnableAutoDepthStencil);
215     localParameters.AutoDepthStencilFormat         = *(pPresentationParameters->AutoDepthStencilFormat);
216     localParameters.Flags                          = *(pPresentationParameters->Flags);
217     localParameters.FullScreen_RefreshRateInHz     = *(pPresentationParameters->FullScreen_RefreshRateInHz);
218     localParameters.PresentationInterval           = *(pPresentationParameters->PresentationInterval);
219
220     /*copy the presentation parameters*/
221     res = IDirect3DDevice9_CreateAdditionalSwapChain((IDirect3DDevice9 *)device, &localParameters, (IDirect3DSwapChain9 **)&d3dSwapChain);
222
223     if (res == D3D_OK && d3dSwapChain != NULL) {
224         *ppSwapChain = d3dSwapChain->wineD3DSwapChain;
225     } else {
226         *ppSwapChain = NULL;
227     }
228     /*Copy back the presentation parameters*/
229    *pPresentationParameters->BackBufferWidth               = localParameters.BackBufferWidth;
230    *pPresentationParameters->BackBufferHeight              = localParameters.BackBufferHeight;
231    *pPresentationParameters->BackBufferFormat              = localParameters.BackBufferFormat;
232    *pPresentationParameters->BackBufferCount               = localParameters.BackBufferCount;
233    *pPresentationParameters->MultiSampleType               = localParameters.MultiSampleType;
234    *pPresentationParameters->MultiSampleQuality            = localParameters.MultiSampleQuality;
235    *pPresentationParameters->SwapEffect                    = localParameters.SwapEffect;
236    *pPresentationParameters->hDeviceWindow                 = localParameters.hDeviceWindow;
237    *pPresentationParameters->Windowed                      = localParameters.Windowed;
238    *pPresentationParameters->EnableAutoDepthStencil        = localParameters.EnableAutoDepthStencil;
239    *pPresentationParameters->AutoDepthStencilFormat        = localParameters.AutoDepthStencilFormat;
240    *pPresentationParameters->Flags                         = localParameters.Flags;
241    *pPresentationParameters->FullScreen_RefreshRateInHz    = localParameters.FullScreen_RefreshRateInHz;
242    *pPresentationParameters->PresentationInterval          = localParameters.PresentationInterval;
243
244    return res;
245 }
246
247 /* Internal function called back during the CreateDevice to create a render target */
248 HRESULT WINAPI D3D9CB_CreateDepthStencilSurface(IUnknown *device, UINT Width, UINT Height, 
249                                          WINED3DFORMAT Format, WINED3DMULTISAMPLE_TYPE MultiSample,
250                                          DWORD MultisampleQuality, BOOL Discard,
251                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
252     HRESULT res = D3D_OK;
253     IDirect3DSurface9Impl *d3dSurface = NULL;
254     TRACE("(%p) call back\n", device);
255
256     res = IDirect3DDevice9_CreateDepthStencilSurface((IDirect3DDevice9 *)device, Width, Height, 
257                                          (D3DFORMAT)Format, MultiSample, MultisampleQuality, Discard, 
258                                          (IDirect3DSurface9 **)&d3dSurface, pSharedHandle);
259     if (res == D3D_OK) {
260         *ppSurface = d3dSurface->wineD3DSurface;
261     }
262     return res;
263 }
264
265
266 HRESULT  WINAPI  IDirect3D9Impl_CreateDevice(LPDIRECT3D9 iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow,
267                                             DWORD BehaviourFlags, D3DPRESENT_PARAMETERS* pPresentationParameters,
268                                             IDirect3DDevice9** ppReturnedDeviceInterface) {
269
270     IDirect3D9Impl       *This   = (IDirect3D9Impl *)iface;
271     IDirect3DDevice9Impl *object = NULL;
272     WINED3DPRESENT_PARAMETERS localParameters;
273     HRESULT hr;
274     TRACE("(%p) Relay\n", This);
275
276     /* Check the validity range of the adapter parameter */
277     if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
278         *ppReturnedDeviceInterface = NULL;
279         return D3DERR_INVALIDCALL;
280     }
281
282     /* Allocate the storage for the device object */
283     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
284     if (NULL == object) {
285         FIXME("Allocation of memory failed\n");
286         *ppReturnedDeviceInterface = NULL;
287         return D3DERR_OUTOFVIDEOMEMORY;
288     }
289
290     object->lpVtbl = &Direct3DDevice9_Vtbl;
291     object->ref = 1;
292     *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
293
294     /* Allocate an associated WineD3DDevice object */
295     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
296     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
297     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
298     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
299     localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
300     localParameters.MultiSampleQuality             = &pPresentationParameters->MultiSampleQuality;
301     localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *) &pPresentationParameters->SwapEffect;
302     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
303     localParameters.Windowed                       = &pPresentationParameters->Windowed;
304     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
305     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
306     localParameters.Flags                          = &pPresentationParameters->Flags;
307     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz; 
308     localParameters.PresentationInterval           = &pPresentationParameters->PresentationInterval;
309
310     hr =IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags, &object->WineD3DDevice, (IUnknown *)object);
311
312     if (hr != D3D_OK) {
313         HeapFree(GetProcessHeap(), 0, object);
314         *ppReturnedDeviceInterface = NULL;
315         return hr;
316     }
317
318     TRACE("(%p) : Created Device %p\n", This, object);
319
320     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, &localParameters, D3D9CB_CreateAdditionalSwapChain);
321     if (hr != D3D_OK) {
322         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
323         HeapFree(GetProcessHeap(), 0, object);
324         *ppReturnedDeviceInterface = NULL;
325     }
326     return hr;
327 }
328
329
330
331 const IDirect3D9Vtbl Direct3D9_Vtbl =
332 {
333     /* IUnknown */
334     IDirect3D9Impl_QueryInterface,
335     IDirect3D9Impl_AddRef,
336     IDirect3D9Impl_Release,
337     /* IDirect3D9 */
338     IDirect3D9Impl_RegisterSoftwareDevice,
339     IDirect3D9Impl_GetAdapterCount,
340     IDirect3D9Impl_GetAdapterIdentifier,
341     IDirect3D9Impl_GetAdapterModeCount,
342     IDirect3D9Impl_EnumAdapterModes,
343     IDirect3D9Impl_GetAdapterDisplayMode,
344     IDirect3D9Impl_CheckDeviceType,
345     IDirect3D9Impl_CheckDeviceFormat,
346     IDirect3D9Impl_CheckDeviceMultiSampleType,
347     IDirect3D9Impl_CheckDepthStencilMatch,
348     IDirect3D9Impl_CheckDeviceFormatConversion,
349     IDirect3D9Impl_GetDeviceCaps,
350     IDirect3D9Impl_GetAdapterMonitor,
351     IDirect3D9Impl_CreateDevice
352 };