wined3d: Get rid of the WINED3DADAPTER_IDENTIFIER typedef.
[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., 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 static inline IDirect3D9Impl *impl_from_IDirect3D9Ex(IDirect3D9Ex *iface)
28 {
29     return CONTAINING_RECORD(iface, IDirect3D9Impl, IDirect3D9Ex_iface);
30 }
31
32 static HRESULT WINAPI IDirect3D9Impl_QueryInterface(IDirect3D9Ex *iface, REFIID riid, void **ppobj)
33 {
34     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
35
36     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
37
38     if (IsEqualGUID(riid, &IID_IUnknown)
39         || IsEqualGUID(riid, &IID_IDirect3D9)) {
40         IDirect3D9Ex_AddRef(iface);
41         *ppobj = This;
42         TRACE("Returning IDirect3D9 interface at %p\n", *ppobj);
43         return S_OK;
44     } else if(IsEqualGUID(riid, &IID_IDirect3D9Ex)) {
45         if(This->extended) {
46             *ppobj = This;
47             TRACE("Returning IDirect3D9Ex interface at %p\n", *ppobj);
48             IDirect3D9Ex_AddRef((IDirect3D9Ex *)*ppobj);
49         } else {
50             WARN("Application asks for IDirect3D9Ex, but this instance wasn't created with Direct3DCreate9Ex\n");
51             WARN("Returning E_NOINTERFACE\n");
52             *ppobj = NULL;
53             return E_NOINTERFACE;
54         }
55     }
56
57     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
58     *ppobj = NULL;
59     return E_NOINTERFACE;
60 }
61
62 static ULONG WINAPI IDirect3D9Impl_AddRef(IDirect3D9Ex *iface)
63 {
64     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
65     ULONG ref = InterlockedIncrement(&This->ref);
66
67     TRACE("%p increasing refcount to %u.\n", iface, ref);
68
69     return ref;
70 }
71
72 static ULONG WINAPI IDirect3D9Impl_Release(IDirect3D9Ex *iface)
73 {
74     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
75     ULONG ref = InterlockedDecrement(&This->ref);
76
77     TRACE("%p decreasing refcount to %u.\n", iface, ref);
78
79     if (ref == 0) {
80         wined3d_mutex_lock();
81         wined3d_decref(This->WineD3D);
82         wined3d_mutex_unlock();
83
84         HeapFree(GetProcessHeap(), 0, This);
85     }
86
87     return ref;
88 }
89
90 /* IDirect3D9 Interface follow: */
91 static HRESULT  WINAPI  IDirect3D9Impl_RegisterSoftwareDevice(IDirect3D9Ex *iface,
92         void *pInitializeFunction)
93 {
94     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
95     HRESULT hr;
96
97     TRACE("iface %p, init_function %p.\n", iface, pInitializeFunction);
98
99     wined3d_mutex_lock();
100     hr = wined3d_register_software_device(This->WineD3D, pInitializeFunction);
101     wined3d_mutex_unlock();
102
103     return hr;
104 }
105
106 static UINT WINAPI IDirect3D9Impl_GetAdapterCount(IDirect3D9Ex *iface)
107 {
108     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
109     UINT ret;
110
111     TRACE("iface %p.\n", iface);
112
113     wined3d_mutex_lock();
114     ret = wined3d_get_adapter_count(This->WineD3D);
115     wined3d_mutex_unlock();
116
117     return ret;
118 }
119
120 static HRESULT WINAPI IDirect3D9Impl_GetAdapterIdentifier(IDirect3D9Ex *iface, UINT Adapter,
121         DWORD Flags, D3DADAPTER_IDENTIFIER9 *pIdentifier)
122 {
123     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
124     struct wined3d_adapter_identifier adapter_id;
125     HRESULT hr;
126
127     TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
128             iface, Adapter, Flags, pIdentifier);
129
130     adapter_id.driver = pIdentifier->Driver;
131     adapter_id.driver_size = sizeof(pIdentifier->Driver);
132     adapter_id.description = pIdentifier->Description;
133     adapter_id.description_size = sizeof(pIdentifier->Description);
134     adapter_id.device_name = pIdentifier->DeviceName;
135     adapter_id.device_name_size = sizeof(pIdentifier->DeviceName);
136
137     wined3d_mutex_lock();
138     hr = wined3d_get_adapter_identifier(This->WineD3D, Adapter, Flags, &adapter_id);
139     wined3d_mutex_unlock();
140
141     pIdentifier->DriverVersion = adapter_id.driver_version;
142     pIdentifier->VendorId = adapter_id.vendor_id;
143     pIdentifier->DeviceId = adapter_id.device_id;
144     pIdentifier->SubSysId = adapter_id.subsystem_id;
145     pIdentifier->Revision = adapter_id.revision;
146     memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
147     pIdentifier->WHQLLevel = adapter_id.whql_level;
148
149     return hr;
150 }
151
152 static UINT WINAPI IDirect3D9Impl_GetAdapterModeCount(IDirect3D9Ex *iface, UINT Adapter,
153         D3DFORMAT Format)
154 {
155     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
156     UINT ret;
157
158     TRACE("iface %p, adapter %u, format %#x.\n", iface, Adapter, Format);
159
160     /* Others than that not supported by d3d9, but reported by wined3d for ddraw. Filter them out */
161     if(Format != D3DFMT_X8R8G8B8 && Format != D3DFMT_R5G6B5) {
162         return 0;
163     }
164
165     wined3d_mutex_lock();
166     ret = wined3d_get_adapter_mode_count(This->WineD3D, Adapter, wined3dformat_from_d3dformat(Format));
167     wined3d_mutex_unlock();
168
169     return ret;
170 }
171
172 static HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(IDirect3D9Ex *iface, UINT Adapter,
173         D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE *pMode)
174 {
175     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
176     HRESULT hr;
177
178     TRACE("iface %p, adapter %u, format %#x, mode_idx %u, mode %p.\n",
179             iface, Adapter, Format, Mode, pMode);
180
181     /* We can't pass this to WineD3D, otherwise it'll think it came from D3D8 or DDraw.
182        It's supposed to fail anyway, so no harm returning failure. */
183     if(Format != D3DFMT_X8R8G8B8 && Format != D3DFMT_R5G6B5)
184         return D3DERR_INVALIDCALL;
185
186     wined3d_mutex_lock();
187     hr = wined3d_enum_adapter_modes(This->WineD3D, Adapter, wined3dformat_from_d3dformat(Format),
188             Mode, (struct wined3d_display_mode *)pMode);
189     wined3d_mutex_unlock();
190
191     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
192
193     return hr;
194 }
195
196 static HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(IDirect3D9Ex *iface, UINT Adapter,
197         D3DDISPLAYMODE *pMode)
198 {
199     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
200     HRESULT hr;
201
202     TRACE("iface %p, adapter %u, mode %p.\n", iface, Adapter, pMode);
203
204     wined3d_mutex_lock();
205     hr = wined3d_get_adapter_display_mode(This->WineD3D, Adapter, (struct wined3d_display_mode *)pMode);
206     wined3d_mutex_unlock();
207
208     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
209
210     return hr;
211 }
212
213 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(IDirect3D9Ex *iface, UINT Adapter,
214         D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat, D3DFORMAT BackBufferFormat, BOOL Windowed)
215 {
216     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
217     HRESULT hr;
218
219     TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
220             iface, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed);
221
222     wined3d_mutex_lock();
223     hr = wined3d_check_device_type(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
224             wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
225     wined3d_mutex_unlock();
226
227     return hr;
228 }
229
230 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormat(IDirect3D9Ex *iface, UINT Adapter,
231         D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType,
232         D3DFORMAT CheckFormat)
233 {
234     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
235     HRESULT hr;
236     WINED3DRESOURCETYPE WineD3DRType;
237
238     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
239             iface, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
240
241     switch(RType) {
242         case D3DRTYPE_VERTEXBUFFER:
243         case D3DRTYPE_INDEXBUFFER:
244             WineD3DRType = WINED3DRTYPE_BUFFER;
245             break;
246
247         default:
248             WineD3DRType = RType;
249             break;
250     }
251
252     wined3d_mutex_lock();
253     hr = wined3d_check_device_format(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
254             Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
255     wined3d_mutex_unlock();
256
257     return hr;
258 }
259
260 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(IDirect3D9Ex *iface, UINT Adapter,
261         D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed,
262         D3DMULTISAMPLE_TYPE MultiSampleType, DWORD *pQualityLevels)
263 {
264     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
265     HRESULT hr;
266
267     TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x, levels %p.\n",
268             iface, Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType, pQualityLevels);
269
270     wined3d_mutex_lock();
271     hr = wined3d_check_device_multisample_type(This->WineD3D, Adapter, DeviceType,
272             wined3dformat_from_d3dformat(SurfaceFormat), Windowed, MultiSampleType, pQualityLevels);
273     wined3d_mutex_unlock();
274
275     return hr;
276 }
277
278 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(IDirect3D9Ex *iface, UINT Adapter,
279         D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat,
280         D3DFORMAT DepthStencilFormat)
281 {
282     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
283     HRESULT hr;
284
285     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
286             iface, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
287
288     wined3d_mutex_lock();
289     hr = wined3d_check_depth_stencil_match(This->WineD3D, Adapter, DeviceType,
290             wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
291             wined3dformat_from_d3dformat(DepthStencilFormat));
292     wined3d_mutex_unlock();
293
294     return hr;
295 }
296
297 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(IDirect3D9Ex *iface, UINT Adapter,
298         D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat)
299 {
300     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
301     HRESULT hr;
302
303     TRACE("iface %p, adapter %u, device_type %#x, src_format %#x, dst_format %#x.\n",
304             iface, Adapter, DeviceType, SourceFormat, TargetFormat);
305
306     wined3d_mutex_lock();
307     hr = wined3d_check_device_format_conversion(This->WineD3D, Adapter, DeviceType,
308             wined3dformat_from_d3dformat(SourceFormat), wined3dformat_from_d3dformat(TargetFormat));
309     wined3d_mutex_unlock();
310
311     return hr;
312 }
313
314 void filter_caps(D3DCAPS9* pCaps)
315 {
316     DWORD ps_minor_version[] = {0, 4, 0, 0};
317     DWORD vs_minor_version[] = {0, 1, 0, 0};
318     DWORD textureFilterCaps =
319         D3DPTFILTERCAPS_MINFPOINT      | D3DPTFILTERCAPS_MINFLINEAR    | D3DPTFILTERCAPS_MINFANISOTROPIC |
320         D3DPTFILTERCAPS_MINFPYRAMIDALQUAD                              | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
321         D3DPTFILTERCAPS_MIPFPOINT      | D3DPTFILTERCAPS_MIPFLINEAR    | D3DPTFILTERCAPS_MAGFPOINT       |
322         D3DPTFILTERCAPS_MAGFLINEAR     |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
323         D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
324     pCaps->TextureFilterCaps &= textureFilterCaps;
325     pCaps->CubeTextureFilterCaps &= textureFilterCaps;
326     pCaps->VolumeTextureFilterCaps &= textureFilterCaps;
327
328     pCaps->DevCaps &=
329         D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
330         D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY   |
331         D3DDEVCAPS_DRAWPRIMTLVERTEX    | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
332         D3DDEVCAPS_DRAWPRIMITIVES2     | D3DDEVCAPS_SEPARATETEXTUREMEMORIES                              |
333         D3DDEVCAPS_DRAWPRIMITIVES2EX   | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL  |
334         D3DDEVCAPS_HWRASTERIZATION     | D3DDEVCAPS_PUREDEVICE         | D3DDEVCAPS_QUINTICRTPATCHES     |
335         D3DDEVCAPS_RTPATCHES           | D3DDEVCAPS_RTPATCHHANDLEZERO  | D3DDEVCAPS_NPATCHES;
336
337     pCaps->ShadeCaps &=
338         D3DPSHADECAPS_COLORGOURAUDRGB  | D3DPSHADECAPS_SPECULARGOURAUDRGB |
339         D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
340
341     pCaps->RasterCaps &=
342         D3DPRASTERCAPS_DITHER          | D3DPRASTERCAPS_ZTEST          | D3DPRASTERCAPS_FOGVERTEX        |
343         D3DPRASTERCAPS_FOGTABLE        | D3DPRASTERCAPS_MIPMAPLODBIAS  | D3DPRASTERCAPS_ZBUFFERLESSHSR   |
344         D3DPRASTERCAPS_FOGRANGE        | D3DPRASTERCAPS_ANISOTROPY     | D3DPRASTERCAPS_WBUFFER          |
345         D3DPRASTERCAPS_WFOG            | D3DPRASTERCAPS_ZFOG           | D3DPRASTERCAPS_COLORPERSPECTIVE |
346         D3DPRASTERCAPS_SCISSORTEST     | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS                              |
347         D3DPRASTERCAPS_DEPTHBIAS       | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
348
349     pCaps->DevCaps2 &=
350         D3DDEVCAPS2_STREAMOFFSET       | D3DDEVCAPS2_DMAPNPATCH        | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
351         D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES                       |
352         D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
353
354     pCaps->Caps2 &=
355         D3DCAPS2_FULLSCREENGAMMA       | D3DCAPS2_CANCALIBRATEGAMMA    | D3DCAPS2_RESERVED               |
356         D3DCAPS2_CANMANAGERESOURCE     | D3DCAPS2_DYNAMICTEXTURES      | D3DCAPS2_CANAUTOGENMIPMAP;
357
358     pCaps->VertexProcessingCaps &=
359         D3DVTXPCAPS_TEXGEN             | D3DVTXPCAPS_MATERIALSOURCE7   | D3DVTXPCAPS_DIRECTIONALLIGHTS   |
360         D3DVTXPCAPS_POSITIONALLIGHTS   | D3DVTXPCAPS_LOCALVIEWER       | D3DVTXPCAPS_TWEENING            |
361         D3DVTXPCAPS_TEXGEN_SPHEREMAP   | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
362
363     pCaps->TextureCaps &=
364         D3DPTEXTURECAPS_PERSPECTIVE    | D3DPTEXTURECAPS_POW2          | D3DPTEXTURECAPS_ALPHA           |
365         D3DPTEXTURECAPS_SQUAREONLY     | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE                        |
366         D3DPTEXTURECAPS_ALPHAPALETTE   | D3DPTEXTURECAPS_NONPOW2CONDITIONAL                              |
367         D3DPTEXTURECAPS_PROJECTED      | D3DPTEXTURECAPS_CUBEMAP       | D3DPTEXTURECAPS_VOLUMEMAP       |
368         D3DPTEXTURECAPS_MIPMAP         | D3DPTEXTURECAPS_MIPVOLUMEMAP  | D3DPTEXTURECAPS_MIPCUBEMAP      |
369         D3DPTEXTURECAPS_CUBEMAP_POW2   | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
370
371     pCaps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, pCaps->MaxVertexShaderConst);
372     pCaps->NumSimultaneousRTs = min(D3D9_MAX_SIMULTANEOUS_RENDERTARGETS, pCaps->NumSimultaneousRTs);
373
374     if (pCaps->PixelShaderVersion > 3)
375         pCaps->PixelShaderVersion = D3DPS_VERSION(3,0);
376     else
377     {
378         DWORD major = pCaps->PixelShaderVersion;
379         pCaps->PixelShaderVersion = D3DPS_VERSION(major,ps_minor_version[major]);
380     }
381
382     if (pCaps->VertexShaderVersion > 3)
383         pCaps->VertexShaderVersion = D3DVS_VERSION(3,0);
384     else
385     {
386         DWORD major = pCaps->VertexShaderVersion;
387         pCaps->VertexShaderVersion = D3DVS_VERSION(major,vs_minor_version[major]);
388     }
389 }
390
391 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(IDirect3D9Ex *iface, UINT Adapter,
392         D3DDEVTYPE DeviceType, D3DCAPS9 *pCaps)
393 {
394     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
395     HRESULT hrc = D3D_OK;
396     WINED3DCAPS *pWineCaps;
397
398     TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, Adapter, DeviceType, pCaps);
399
400     if(NULL == pCaps){
401         return D3DERR_INVALIDCALL;
402     }
403     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
404     if(pWineCaps == NULL){
405         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
406     }
407     memset(pCaps, 0, sizeof(*pCaps));
408
409     wined3d_mutex_lock();
410     hrc = wined3d_get_device_caps(This->WineD3D, Adapter, DeviceType, pWineCaps);
411     wined3d_mutex_unlock();
412
413     WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
414     HeapFree(GetProcessHeap(), 0, pWineCaps);
415
416     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
417     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
418
419     filter_caps(pCaps);
420
421     TRACE("(%p) returning %p\n", This, pCaps);
422     return hrc;
423 }
424
425 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(IDirect3D9Ex *iface, UINT Adapter)
426 {
427     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
428     HMONITOR ret;
429
430     TRACE("iface %p, adapter %u.\n", iface, Adapter);
431
432     wined3d_mutex_lock();
433     ret = wined3d_get_adapter_monitor(This->WineD3D, Adapter);
434     wined3d_mutex_unlock();
435
436     return ret;
437 }
438
439 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3D9Impl_CreateDevice(IDirect3D9Ex *iface, UINT adapter,
440         D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters,
441         IDirect3DDevice9 **device)
442 {
443     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
444     IDirect3DDevice9Impl *object;
445     HRESULT hr;
446
447     TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
448             iface, adapter, device_type, focus_window, flags, parameters, device);
449
450     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
451     if (!object)
452     {
453         ERR("Failed to allocate device memory.\n");
454         return E_OUTOFMEMORY;
455     }
456
457     hr = device_init(object, This, This->WineD3D, adapter, device_type, focus_window, flags, parameters, NULL);
458     if (FAILED(hr))
459     {
460         WARN("Failed to initialize device, hr %#x.\n", hr);
461         HeapFree(GetProcessHeap(), 0, object);
462         return hr;
463     }
464
465     TRACE("Created device %p.\n", object);
466     *device = (IDirect3DDevice9 *)object;
467
468     return D3D_OK;
469 }
470
471 static UINT WINAPI IDirect3D9ExImpl_GetAdapterModeCountEx(IDirect3D9Ex *iface,
472         UINT adapter, const D3DDISPLAYMODEFILTER *filter)
473 {
474     FIXME("iface %p, adapter %u, filter %p stub!\n", iface, adapter, filter);
475
476     return 0;
477 }
478
479 static HRESULT WINAPI IDirect3D9ExImpl_EnumAdapterModesEx(IDirect3D9Ex *iface,
480         UINT adapter, const D3DDISPLAYMODEFILTER *filter, UINT mode_idx, D3DDISPLAYMODEEX *mode)
481 {
482     FIXME("iface %p, adapter %u, filter %p, mode_idx %u, mode %p stub!\n",
483             iface, adapter, filter, mode_idx, mode);
484
485     return E_NOTIMPL;
486 }
487
488 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterDisplayModeEx(IDirect3D9Ex *iface,
489         UINT adapter, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
490 {
491     FIXME("iface %p, adapter %u, mode %p, rotation %p stub!\n",
492             iface, adapter, mode, rotation);
493
494     return E_NOTIMPL;
495 }
496
497 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3D9ExImpl_CreateDeviceEx(IDirect3D9Ex *iface,
498         UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
499         D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode, IDirect3DDevice9Ex **device)
500 {
501     IDirect3D9Impl *d3d9 = impl_from_IDirect3D9Ex(iface);
502     IDirect3DDevice9Impl *object;
503     HRESULT hr;
504
505     TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, mode %p, device %p.\n",
506             iface, adapter, device_type, focus_window, flags, parameters, mode, device);
507
508     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
509     if (!object)
510     {
511         ERR("Failed to allocate device memory.\n");
512         return E_OUTOFMEMORY;
513     }
514
515     hr = device_init(object, d3d9, d3d9->WineD3D, adapter, device_type, focus_window, flags, parameters, mode);
516     if (FAILED(hr))
517     {
518         WARN("Failed to initialize device, hr %#x.\n", hr);
519         HeapFree(GetProcessHeap(), 0, object);
520         return hr;
521     }
522
523     TRACE("Created device %p.\n", object);
524     *device = &object->IDirect3DDevice9Ex_iface;
525
526     return D3D_OK;
527 }
528
529 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterLUID(IDirect3D9Ex *iface, UINT adapter, LUID *luid)
530 {
531     IDirect3D9Impl *This = impl_from_IDirect3D9Ex(iface);
532     struct wined3d_adapter_identifier adapter_id;
533     HRESULT hr;
534
535     TRACE("iface %p, adapter %u, luid %p.\n", iface, adapter, luid);
536
537     adapter_id.driver_size = 0;
538     adapter_id.description_size = 0;
539     adapter_id.device_name_size = 0;
540
541     wined3d_mutex_lock();
542     hr = wined3d_get_adapter_identifier(This->WineD3D, adapter, 0, &adapter_id);
543     wined3d_mutex_unlock();
544
545     memcpy(luid, &adapter_id.adapter_luid, sizeof(*luid));
546
547     return hr;
548 }
549
550
551 const IDirect3D9ExVtbl Direct3D9_Vtbl =
552 {
553     /* IUnknown */
554     IDirect3D9Impl_QueryInterface,
555     IDirect3D9Impl_AddRef,
556     IDirect3D9Impl_Release,
557     /* IDirect3D9 */
558     IDirect3D9Impl_RegisterSoftwareDevice,
559     IDirect3D9Impl_GetAdapterCount,
560     IDirect3D9Impl_GetAdapterIdentifier,
561     IDirect3D9Impl_GetAdapterModeCount,
562     IDirect3D9Impl_EnumAdapterModes,
563     IDirect3D9Impl_GetAdapterDisplayMode,
564     IDirect3D9Impl_CheckDeviceType,
565     IDirect3D9Impl_CheckDeviceFormat,
566     IDirect3D9Impl_CheckDeviceMultiSampleType,
567     IDirect3D9Impl_CheckDepthStencilMatch,
568     IDirect3D9Impl_CheckDeviceFormatConversion,
569     IDirect3D9Impl_GetDeviceCaps,
570     IDirect3D9Impl_GetAdapterMonitor,
571     IDirect3D9Impl_CreateDevice,
572     /* IDirect3D9Ex */
573     IDirect3D9ExImpl_GetAdapterModeCountEx,
574     IDirect3D9ExImpl_EnumAdapterModesEx,
575     IDirect3D9ExImpl_GetAdapterDisplayModeEx,
576     IDirect3D9ExImpl_CreateDeviceEx,
577     IDirect3D9ExImpl_GetAdapterLUID
578
579 };