Sync ddraw, d3d8 and d3d9 + wined3d to wine 1.1.28
[reactos.git] / reactos / dll / directx / wine / 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 static HRESULT WINAPI IDirect3D9Impl_QueryInterface(LPDIRECT3D9EX iface, REFIID riid, LPVOID* ppobj)
29 {
30 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
31
32 if (IsEqualGUID(riid, &IID_IUnknown)
33 || IsEqualGUID(riid, &IID_IDirect3D9)) {
34 IDirect3D9Ex_AddRef(iface);
35 *ppobj = This;
36 TRACE("Returning IDirect3D9 interface at %p\n", *ppobj);
37 return S_OK;
38 } else if(IsEqualGUID(riid, &IID_IDirect3D9Ex)) {
39 if(This->extended) {
40 *ppobj = This;
41 TRACE("Returning IDirect3D9Ex interface at %p\n", *ppobj);
42 IDirect3D9Ex_AddRef((IDirect3D9Ex *)*ppobj);
43 } else {
44 WARN("Application asks for IDirect3D9Ex, but this instance wasn't created with Direct3DCreate9Ex\n");
45 WARN("Returning E_NOINTERFACE\n");
46 *ppobj = NULL;
47 return E_NOINTERFACE;
48 }
49 }
50
51 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
52 *ppobj = NULL;
53 return E_NOINTERFACE;
54 }
55
56 static ULONG WINAPI IDirect3D9Impl_AddRef(LPDIRECT3D9EX iface) {
57 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
58 ULONG ref = InterlockedIncrement(&This->ref);
59
60 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
61
62 return ref;
63 }
64
65 static ULONG WINAPI IDirect3D9Impl_Release(LPDIRECT3D9EX iface) {
66 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
67 ULONG ref = InterlockedDecrement(&This->ref);
68
69 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
70
71 if (ref == 0) {
72 wined3d_mutex_lock();
73 IWineD3D_Release(This->WineD3D);
74 wined3d_mutex_unlock();
75
76 HeapFree(GetProcessHeap(), 0, This);
77 }
78
79 return ref;
80 }
81
82 /* IDirect3D9 Interface follow: */
83 static HRESULT WINAPI IDirect3D9Impl_RegisterSoftwareDevice(LPDIRECT3D9EX iface, void* pInitializeFunction) {
84 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
85 HRESULT hr;
86 TRACE("(%p)->(%p)\n", This, pInitializeFunction);
87
88 wined3d_mutex_lock();
89 hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
90 wined3d_mutex_unlock();
91
92 return hr;
93 }
94
95 static UINT WINAPI IDirect3D9Impl_GetAdapterCount(LPDIRECT3D9EX iface) {
96 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
97 HRESULT hr;
98 TRACE("%p\n", This);
99
100 wined3d_mutex_lock();
101 hr = IWineD3D_GetAdapterCount(This->WineD3D);
102 wined3d_mutex_unlock();
103
104 return hr;
105 }
106
107 static HRESULT WINAPI IDirect3D9Impl_GetAdapterIdentifier(LPDIRECT3D9EX iface, UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier) {
108 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
109 WINED3DADAPTER_IDENTIFIER adapter_id;
110 HRESULT hr;
111
112 adapter_id.driver = pIdentifier->Driver;
113 adapter_id.driver_size = sizeof(pIdentifier->Driver);
114 adapter_id.description = pIdentifier->Description;
115 adapter_id.description_size = sizeof(pIdentifier->Description);
116 adapter_id.device_name = pIdentifier->DeviceName;
117 adapter_id.device_name_size = sizeof(pIdentifier->DeviceName);
118
119 wined3d_mutex_lock();
120 hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
121 wined3d_mutex_unlock();
122
123 pIdentifier->DriverVersion = adapter_id.driver_version;
124 pIdentifier->VendorId = adapter_id.vendor_id;
125 pIdentifier->DeviceId = adapter_id.device_id;
126 pIdentifier->SubSysId = adapter_id.subsystem_id;
127 pIdentifier->Revision = adapter_id.revision;
128 memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
129 pIdentifier->WHQLLevel = adapter_id.whql_level;
130
131 return hr;
132 }
133
134 static UINT WINAPI IDirect3D9Impl_GetAdapterModeCount(LPDIRECT3D9EX iface, UINT Adapter, D3DFORMAT Format) {
135 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
136 HRESULT hr;
137 TRACE("(%p)->(%d, %d\n", This, Adapter, Format);
138
139 /* Others than that not supported by d3d9, but reported by wined3d for ddraw. Filter them out */
140 if(Format != D3DFMT_X8R8G8B8 && Format != D3DFMT_R5G6B5) {
141 return 0;
142 }
143
144 wined3d_mutex_lock();
145 hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, wined3dformat_from_d3dformat(Format));
146 wined3d_mutex_unlock();
147
148 return hr;
149 }
150
151 static HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(LPDIRECT3D9EX iface, UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
152 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
153 HRESULT hr;
154 TRACE("(%p)->(%d, %d, %d, %p)\n", This, Adapter, Format, Mode, pMode);
155 /* We can't pass this to WineD3D, otherwise it'll think it came from D3D8 or DDraw.
156 It's supposed to fail anyway, so no harm returning failure. */
157 if(Format != D3DFMT_X8R8G8B8 && Format != D3DFMT_R5G6B5)
158 return D3DERR_INVALIDCALL;
159
160 wined3d_mutex_lock();
161 hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, wined3dformat_from_d3dformat(Format),
162 Mode, (WINED3DDISPLAYMODE *) pMode);
163 wined3d_mutex_unlock();
164
165 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
166
167 return hr;
168 }
169
170 static HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9EX iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
171 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
172 HRESULT hr;
173
174 wined3d_mutex_lock();
175 hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
176 wined3d_mutex_unlock();
177
178 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
179
180 return hr;
181 }
182
183 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(IDirect3D9Ex *iface, UINT Adapter,
184 D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat, D3DFORMAT BackBufferFormat, BOOL Windowed)
185 {
186 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
187 HRESULT hr;
188 TRACE("(%p)->(%d, %d, %d, %d, %s\n", This, Adapter, CheckType, DisplayFormat,
189 BackBufferFormat, Windowed ? "true" : "false");
190
191 wined3d_mutex_lock();
192 hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
193 wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
194 wined3d_mutex_unlock();
195
196 return hr;
197 }
198
199 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormat(IDirect3D9Ex *iface, UINT Adapter, D3DDEVTYPE DeviceType,
200 D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat)
201 {
202 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
203 HRESULT hr;
204 WINED3DRESOURCETYPE WineD3DRType;
205 TRACE("%p\n", This);
206
207 /* This format is nothing special and it is supported perfectly.
208 * However, ati and nvidia driver on windows do not mark this format as
209 * supported (tested with the dxCapsViewer) and pretending to
210 * support this format uncovers a bug in Battlefield 1942 (fonts are missing)
211 * So do the same as Windows drivers and pretend not to support it on dx8 and 9
212 */
213 if(CheckFormat == D3DFMT_R8G8B8)
214 {
215 WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
216 return D3DERR_NOTAVAILABLE;
217 }
218
219 switch(RType) {
220 case D3DRTYPE_VERTEXBUFFER:
221 case D3DRTYPE_INDEXBUFFER:
222 WineD3DRType = WINED3DRTYPE_BUFFER;
223 break;
224
225 default:
226 WineD3DRType = RType;
227 break;
228 }
229
230 wined3d_mutex_lock();
231 hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
232 Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
233 wined3d_mutex_unlock();
234
235 return hr;
236 }
237
238 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(IDirect3D9Ex *iface, UINT Adapter,
239 D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType,
240 DWORD *pQualityLevels)
241 {
242 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
243 HRESULT hr;
244 TRACE("%p\n", This);
245
246 wined3d_mutex_lock();
247 hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
248 wined3dformat_from_d3dformat(SurfaceFormat), Windowed, MultiSampleType, pQualityLevels);
249 wined3d_mutex_unlock();
250
251 return hr;
252 }
253
254 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(IDirect3D9Ex *iface, UINT Adapter,
255 D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
256 {
257 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
258 HRESULT hr;
259 TRACE("%p\n", This);
260
261 wined3d_mutex_lock();
262 hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
263 wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
264 wined3dformat_from_d3dformat(DepthStencilFormat));
265 wined3d_mutex_unlock();
266
267 return hr;
268 }
269
270 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
271 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
272 HRESULT hr;
273 TRACE("%p\n", This);
274
275 wined3d_mutex_lock();
276 hr = IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType,
277 wined3dformat_from_d3dformat(SourceFormat), wined3dformat_from_d3dformat(TargetFormat));
278 wined3d_mutex_unlock();
279
280 return hr;
281 }
282
283 void filter_caps(D3DCAPS9* pCaps)
284 {
285
286 DWORD textureFilterCaps =
287 D3DPTFILTERCAPS_MINFPOINT | D3DPTFILTERCAPS_MINFLINEAR | D3DPTFILTERCAPS_MINFANISOTROPIC |
288 D3DPTFILTERCAPS_MINFPYRAMIDALQUAD | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
289 D3DPTFILTERCAPS_MIPFPOINT | D3DPTFILTERCAPS_MIPFLINEAR | D3DPTFILTERCAPS_MAGFPOINT |
290 D3DPTFILTERCAPS_MAGFLINEAR |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
291 D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
292 pCaps->TextureFilterCaps &= textureFilterCaps;
293 pCaps->CubeTextureFilterCaps &= textureFilterCaps;
294 pCaps->VolumeTextureFilterCaps &= textureFilterCaps;
295
296 pCaps->DevCaps &=
297 D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
298 D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY |
299 D3DDEVCAPS_DRAWPRIMTLVERTEX | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
300 D3DDEVCAPS_DRAWPRIMITIVES2 | D3DDEVCAPS_SEPARATETEXTUREMEMORIES |
301 D3DDEVCAPS_DRAWPRIMITIVES2EX | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL |
302 D3DDEVCAPS_HWRASTERIZATION | D3DDEVCAPS_PUREDEVICE | D3DDEVCAPS_QUINTICRTPATCHES |
303 D3DDEVCAPS_RTPATCHES | D3DDEVCAPS_RTPATCHHANDLEZERO | D3DDEVCAPS_NPATCHES;
304
305 pCaps->ShadeCaps &=
306 D3DPSHADECAPS_COLORGOURAUDRGB | D3DPSHADECAPS_SPECULARGOURAUDRGB |
307 D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
308
309 pCaps->RasterCaps &=
310 D3DPRASTERCAPS_DITHER | D3DPRASTERCAPS_ZTEST | D3DPRASTERCAPS_FOGVERTEX |
311 D3DPRASTERCAPS_FOGTABLE | D3DPRASTERCAPS_MIPMAPLODBIAS | D3DPRASTERCAPS_ZBUFFERLESSHSR |
312 D3DPRASTERCAPS_FOGRANGE | D3DPRASTERCAPS_ANISOTROPY | D3DPRASTERCAPS_WBUFFER |
313 D3DPRASTERCAPS_WFOG | D3DPRASTERCAPS_ZFOG | D3DPRASTERCAPS_COLORPERSPECTIVE |
314 D3DPRASTERCAPS_SCISSORTEST | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS |
315 D3DPRASTERCAPS_DEPTHBIAS | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
316
317 pCaps->DevCaps2 &=
318 D3DDEVCAPS2_STREAMOFFSET | D3DDEVCAPS2_DMAPNPATCH | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
319 D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES |
320 D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
321
322 pCaps->Caps2 &=
323 D3DCAPS2_FULLSCREENGAMMA | D3DCAPS2_CANCALIBRATEGAMMA | D3DCAPS2_RESERVED |
324 D3DCAPS2_CANMANAGERESOURCE | D3DCAPS2_DYNAMICTEXTURES | D3DCAPS2_CANAUTOGENMIPMAP;
325
326 pCaps->VertexProcessingCaps &=
327 D3DVTXPCAPS_TEXGEN | D3DVTXPCAPS_MATERIALSOURCE7 | D3DVTXPCAPS_DIRECTIONALLIGHTS |
328 D3DVTXPCAPS_POSITIONALLIGHTS | D3DVTXPCAPS_LOCALVIEWER | D3DVTXPCAPS_TWEENING |
329 D3DVTXPCAPS_TEXGEN_SPHEREMAP | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
330
331 pCaps->TextureCaps &=
332 D3DPTEXTURECAPS_PERSPECTIVE | D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_ALPHA |
333 D3DPTEXTURECAPS_SQUAREONLY | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE |
334 D3DPTEXTURECAPS_ALPHAPALETTE | D3DPTEXTURECAPS_NONPOW2CONDITIONAL |
335 D3DPTEXTURECAPS_PROJECTED | D3DPTEXTURECAPS_CUBEMAP | D3DPTEXTURECAPS_VOLUMEMAP |
336 D3DPTEXTURECAPS_MIPMAP | D3DPTEXTURECAPS_MIPVOLUMEMAP | D3DPTEXTURECAPS_MIPCUBEMAP |
337 D3DPTEXTURECAPS_CUBEMAP_POW2 | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
338
339 pCaps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, pCaps->MaxVertexShaderConst);
340 }
341
342 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
343 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
344 HRESULT hrc = D3D_OK;
345 WINED3DCAPS *pWineCaps;
346
347 TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
348
349 if(NULL == pCaps){
350 return D3DERR_INVALIDCALL;
351 }
352 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
353 if(pWineCaps == NULL){
354 return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
355 }
356 memset(pCaps, 0, sizeof(*pCaps));
357
358 wined3d_mutex_lock();
359 hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
360 wined3d_mutex_unlock();
361
362 WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
363 HeapFree(GetProcessHeap(), 0, pWineCaps);
364
365 /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
366 pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
367
368 filter_caps(pCaps);
369
370 TRACE("(%p) returning %p\n", This, pCaps);
371 return hrc;
372 }
373
374 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9EX iface, UINT Adapter) {
375 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
376 HMONITOR ret;
377 TRACE("%p\n", This);
378
379 wined3d_mutex_lock();
380 ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
381 wined3d_mutex_unlock();
382
383 return ret;
384 }
385
386 ULONG WINAPI D3D9CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
387 IDirect3DSurface9Impl* surfaceParent;
388 TRACE("(%p) call back\n", pSurface);
389
390 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
391 surfaceParent->isImplicit = FALSE;
392 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
393 return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
394 }
395
396 ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
397 IDirect3DSwapChain9Impl* swapChainParent;
398 TRACE("(%p) call back\n", pSwapChain);
399
400 IWineD3DSwapChain_GetParent(pSwapChain,(IUnknown **) &swapChainParent);
401 swapChainParent->isImplicit = FALSE;
402 /* Swap chain had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
403 return IDirect3DSwapChain9_Release((IDirect3DSwapChain9*) swapChainParent);
404 }
405
406 ULONG WINAPI D3D9CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
407 IDirect3DSurface9Impl* surfaceParent;
408 TRACE("(%p) call back\n", pSurface);
409
410 IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
411 surfaceParent->isImplicit = FALSE;
412 /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
413 return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
414 }
415
416 static HRESULT WINAPI IDirect3D9Impl_CreateDevice(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType,
417 HWND hFocusWindow, DWORD BehaviourFlags,
418 D3DPRESENT_PARAMETERS* pPresentationParameters,
419 IDirect3DDevice9** ppReturnedDeviceInterface) {
420
421 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
422 IDirect3DDevice9Impl *object = NULL;
423 WINED3DPRESENT_PARAMETERS *localParameters;
424 UINT i, count = 1;
425 HRESULT hr;
426 TRACE("(%p) Relay\n", This);
427
428 /* Check the validity range of the adapter parameter */
429 if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
430 *ppReturnedDeviceInterface = NULL;
431 return D3DERR_INVALIDCALL;
432 }
433
434 /* Allocate the storage for the device object */
435 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
436 if (NULL == object) {
437 FIXME("Allocation of memory failed\n");
438 *ppReturnedDeviceInterface = NULL;
439 return D3DERR_OUTOFVIDEOMEMORY;
440 }
441
442 object->lpVtbl = &Direct3DDevice9_Vtbl;
443 object->device_parent_vtbl = &d3d9_wined3d_device_parent_vtbl;
444 object->ref = 1;
445 *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
446
447 /* Allocate an associated WineD3DDevice object */
448 wined3d_mutex_lock();
449 hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
450 (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
451 if (hr != D3D_OK) {
452 HeapFree(GetProcessHeap(), 0, object);
453 *ppReturnedDeviceInterface = NULL;
454 wined3d_mutex_unlock();
455
456 return hr;
457 }
458
459 TRACE("(%p) : Created Device %p\n", This, object);
460
461 if (BehaviourFlags & D3DCREATE_ADAPTERGROUP_DEVICE)
462 {
463 WINED3DCAPS caps;
464
465 IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, &caps);
466 count = caps.NumberOfAdaptersInGroup;
467 }
468
469 if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
470 IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
471 }
472
473 localParameters = HeapAlloc(GetProcessHeap(), 0, sizeof(*localParameters) * count);
474 for (i = 0; i < count; ++i)
475 {
476 localParameters[i].BackBufferWidth = pPresentationParameters[i].BackBufferWidth;
477 localParameters[i].BackBufferHeight = pPresentationParameters[i].BackBufferHeight;
478 localParameters[i].BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters[i].BackBufferFormat);
479 localParameters[i].BackBufferCount = pPresentationParameters[i].BackBufferCount;
480 localParameters[i].MultiSampleType = pPresentationParameters[i].MultiSampleType;
481 localParameters[i].MultiSampleQuality = pPresentationParameters[i].MultiSampleQuality;
482 localParameters[i].SwapEffect = pPresentationParameters[i].SwapEffect;
483 localParameters[i].hDeviceWindow = pPresentationParameters[i].hDeviceWindow;
484 localParameters[i].Windowed = pPresentationParameters[i].Windowed;
485 localParameters[i].EnableAutoDepthStencil = pPresentationParameters[i].EnableAutoDepthStencil;
486 localParameters[i].AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters[i].AutoDepthStencilFormat);
487 localParameters[i].Flags = pPresentationParameters[i].Flags;
488 localParameters[i].FullScreen_RefreshRateInHz = pPresentationParameters[i].FullScreen_RefreshRateInHz;
489 localParameters[i].PresentationInterval = pPresentationParameters[i].PresentationInterval;
490 localParameters[i].AutoRestoreDisplayMode = TRUE;
491 }
492
493 hr = IWineD3DDevice_Init3D(object->WineD3DDevice, localParameters);
494 if (hr != D3D_OK) {
495 FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
496 HeapFree(GetProcessHeap(), 0, object);
497 *ppReturnedDeviceInterface = NULL;
498 }
499
500 for (i = 0; i < count; ++i)
501 {
502 pPresentationParameters[i].BackBufferWidth = localParameters[i].BackBufferWidth;
503 pPresentationParameters[i].BackBufferHeight = localParameters[i].BackBufferHeight;
504 pPresentationParameters[i].BackBufferFormat = d3dformat_from_wined3dformat(localParameters[i].BackBufferFormat);
505 pPresentationParameters[i].BackBufferCount = localParameters[i].BackBufferCount;
506 pPresentationParameters[i].MultiSampleType = localParameters[i].MultiSampleType;
507 pPresentationParameters[i].MultiSampleQuality = localParameters[i].MultiSampleQuality;
508 pPresentationParameters[i].SwapEffect = localParameters[i].SwapEffect;
509 pPresentationParameters[i].hDeviceWindow = localParameters[i].hDeviceWindow;
510 pPresentationParameters[i].Windowed = localParameters[i].Windowed;
511 pPresentationParameters[i].EnableAutoDepthStencil = localParameters[i].EnableAutoDepthStencil;
512 pPresentationParameters[i].AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters[i].AutoDepthStencilFormat);
513 pPresentationParameters[i].Flags = localParameters[i].Flags;
514 pPresentationParameters[i].FullScreen_RefreshRateInHz = localParameters[i].FullScreen_RefreshRateInHz;
515 pPresentationParameters[i].PresentationInterval = localParameters[i].PresentationInterval;
516 }
517 HeapFree(GetProcessHeap(), 0, localParameters);
518
519 /* Initialize the converted declaration array. This creates a valid pointer and when adding decls HeapReAlloc
520 * can be used without further checking
521 */
522 object->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
523 wined3d_mutex_unlock();
524
525 return hr;
526 }
527
528 static UINT WINAPI IDirect3D9ExImpl_GetAdapterModeCountEx(IDirect3D9Ex *iface, UINT Adapter, CONST D3DDISPLAYMODEFILTER *pFilter) {
529 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
530 FIXME("(%p)->(%d, %p): Stub!\n", This, Adapter, pFilter);
531 return D3DERR_DRIVERINTERNALERROR;
532 }
533
534 static HRESULT WINAPI IDirect3D9ExImpl_EnumAdapterModesEx(IDirect3D9Ex *iface, UINT Adapter, CONST D3DDISPLAYMODEFILTER *pFilter, UINT Mode, D3DDISPLAYMODEEX* pMode) {
535 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
536 FIXME("(%p)->(%d, %p, %p): Stub!\n", This, Adapter, pFilter, pMode);
537 return D3DERR_DRIVERINTERNALERROR;
538 }
539
540 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterDisplayModeEx(IDirect3D9Ex *iface, UINT Adapter, D3DDISPLAYMODEEX *pMode, D3DDISPLAYROTATION *pRotation) {
541 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
542 FIXME("(%p)->(%d, %p, %p): Stub!\n", This, Adapter, pMode, pRotation);
543 return D3DERR_DRIVERINTERNALERROR;
544 }
545
546 static HRESULT WINAPI IDirect3D9ExImpl_CreateDeviceEx(IDirect3D9Ex *iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, D3DDISPLAYMODEEX* pFullscreenDisplayMode, struct IDirect3DDevice9Ex **ppReturnedDeviceInterface) {
547 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
548 FIXME("(%p)->(%d, %d, %p, 0x%08x, %p, %p, %p): Stub!\n", This, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, pFullscreenDisplayMode, ppReturnedDeviceInterface);
549 *ppReturnedDeviceInterface = NULL;
550 return D3DERR_DRIVERINTERNALERROR;
551 }
552
553 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterLUID(IDirect3D9Ex *iface, UINT Adapter, LUID *pLUID) {
554 IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
555 FIXME("(%p)->(%d, %p)\n", This, Adapter, pLUID);
556 return D3DERR_DRIVERINTERNALERROR;
557 }
558
559
560 const IDirect3D9ExVtbl Direct3D9_Vtbl =
561 {
562 /* IUnknown */
563 IDirect3D9Impl_QueryInterface,
564 IDirect3D9Impl_AddRef,
565 IDirect3D9Impl_Release,
566 /* IDirect3D9 */
567 IDirect3D9Impl_RegisterSoftwareDevice,
568 IDirect3D9Impl_GetAdapterCount,
569 IDirect3D9Impl_GetAdapterIdentifier,
570 IDirect3D9Impl_GetAdapterModeCount,
571 IDirect3D9Impl_EnumAdapterModes,
572 IDirect3D9Impl_GetAdapterDisplayMode,
573 IDirect3D9Impl_CheckDeviceType,
574 IDirect3D9Impl_CheckDeviceFormat,
575 IDirect3D9Impl_CheckDeviceMultiSampleType,
576 IDirect3D9Impl_CheckDepthStencilMatch,
577 IDirect3D9Impl_CheckDeviceFormatConversion,
578 IDirect3D9Impl_GetDeviceCaps,
579 IDirect3D9Impl_GetAdapterMonitor,
580 IDirect3D9Impl_CreateDevice,
581 /* IDirect3D9Ex */
582 IDirect3D9ExImpl_GetAdapterModeCountEx,
583 IDirect3D9ExImpl_EnumAdapterModesEx,
584 IDirect3D9ExImpl_GetAdapterDisplayModeEx,
585 IDirect3D9ExImpl_CreateDeviceEx,
586 IDirect3D9ExImpl_GetAdapterLUID
587
588 };