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