mshtml: Improved more VARIANT debug traces.
[wine] / dlls / d3d9 / device.c
1 /*
2  * IDirect3DDevice9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
29 {
30     BYTE *c = (BYTE *)&format;
31
32     /* Don't translate FOURCC formats */
33     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
34
35     switch(format)
36     {
37         case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
38         case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
39         case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
40         case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
41         case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
42         case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
43         case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
44         case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
45         case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
46         case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
47         case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
48         case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
49         case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
50         case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
51         case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
52         case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
53         case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
54         case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
55         case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
56         case WINED3DFMT_P8_UINT: return D3DFMT_P8;
57         case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
58         case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
59         case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
60         case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
61         case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
62         case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
63         case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
64         case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
65         case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
66         case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
67         case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
68         case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
69         case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
70         case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
71         case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
72         case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
73         case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
74         case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
75         case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
76         case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
77         case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
78         case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
79         case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
80         case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
81         case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
82         case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
83         case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
84         case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
85         case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
86         case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
87         default:
88             FIXME("Unhandled wined3d format %#x.\n", format);
89             return D3DFMT_UNKNOWN;
90     }
91 }
92
93 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
94 {
95     BYTE *c = (BYTE *)&format;
96
97     /* Don't translate FOURCC formats */
98     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
99
100     switch(format)
101     {
102         case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
103         case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
104         case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
105         case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
106         case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
107         case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
108         case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
109         case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
110         case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
111         case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
112         case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
113         case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
114         case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
115         case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
116         case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
117         case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118         case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
119         case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
120         case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121         case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122         case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123         case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124         case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125         case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126         case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127         case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128         case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129         case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130         case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
131         case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
132         case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
133         case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
134         case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
135         case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
136         case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
137         case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
138         case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
139         case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
140         case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
141         case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
142         case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
143         case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
144         case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
145         case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
146         case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
147         case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
148         case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
149         case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
150         case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
151         case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
152         default:
153             FIXME("Unhandled D3DFORMAT %#x\n", format);
154             return WINED3DFMT_UNKNOWN;
155     }
156 }
157
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
159 {
160     switch(primitive_type)
161     {
162         case D3DPT_POINTLIST:
163             return primitive_count;
164
165         case D3DPT_LINELIST:
166             return primitive_count * 2;
167
168         case D3DPT_LINESTRIP:
169             return primitive_count + 1;
170
171         case D3DPT_TRIANGLELIST:
172             return primitive_count * 3;
173
174         case D3DPT_TRIANGLESTRIP:
175         case D3DPT_TRIANGLEFAN:
176             return primitive_count + 2;
177
178         default:
179             FIXME("Unhandled primitive type %#x\n", primitive_type);
180             return 0;
181     }
182 }
183
184 static inline IDirect3DDevice9Impl *impl_from_IDirect3DDevice9Ex(IDirect3DDevice9Ex *iface)
185 {
186     return CONTAINING_RECORD(iface, IDirect3DDevice9Impl, IDirect3DDevice9Ex_iface);
187 }
188
189 static HRESULT WINAPI IDirect3DDevice9Impl_QueryInterface(IDirect3DDevice9Ex *iface, REFIID riid,
190         void **ppobj)
191 {
192     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
193
194     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
195
196     if (IsEqualGUID(riid, &IID_IUnknown)
197         || IsEqualGUID(riid, &IID_IDirect3DDevice9)) {
198         IDirect3DDevice9Ex_AddRef(iface);
199         *ppobj = This;
200         TRACE("Returning IDirect3DDevice9 interface at %p\n", *ppobj);
201         return S_OK;
202     } else if(IsEqualGUID(riid, &IID_IDirect3DDevice9Ex)) {
203         /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
204          * It doesn't matter with which function the device was created. */
205
206         if (This->d3d_parent->extended)
207         {
208             *ppobj = iface;
209             IDirect3DDevice9Ex_AddRef((IDirect3DDevice9Ex *) *ppobj);
210             TRACE("Returning IDirect3DDevice9Ex interface at %p\n", *ppobj);
211             return S_OK;
212         } else {
213             WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE\n");
214             *ppobj = NULL;
215             return E_NOINTERFACE;
216         }
217     }
218
219     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
220     *ppobj = NULL;
221     return E_NOINTERFACE;
222 }
223
224 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(IDirect3DDevice9Ex *iface)
225 {
226     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
227     ULONG ref = InterlockedIncrement(&This->ref);
228
229     TRACE("%p increasing refcount to %u.\n", iface, ref);
230
231     return ref;
232 }
233
234 static ULONG WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Release(IDirect3DDevice9Ex *iface)
235 {
236     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
237     ULONG ref;
238
239     if (This->inDestruction) return 0;
240     ref = InterlockedDecrement(&This->ref);
241
242     TRACE("%p decreasing refcount to %u.\n", iface, ref);
243
244     if (ref == 0) {
245       unsigned i;
246       This->inDestruction = TRUE;
247
248       wined3d_mutex_lock();
249       for(i = 0; i < This->numConvertedDecls; i++) {
250           /* Unless Wine is buggy or the app has a bug the refcount will be 0, because decls hold a reference to the
251            * device
252            */
253           IDirect3DVertexDeclaration9Impl_Destroy(This->convertedDecls[i]);
254       }
255       HeapFree(GetProcessHeap(), 0, This->convertedDecls);
256
257       wined3d_device_uninit_3d(This->wined3d_device);
258       wined3d_device_release_focus_window(This->wined3d_device);
259       wined3d_device_decref(This->wined3d_device);
260       wined3d_mutex_unlock();
261
262       IDirect3D9Ex_Release(&This->d3d_parent->IDirect3D9Ex_iface);
263
264       HeapFree(GetProcessHeap(), 0, This);
265     }
266     return ref;
267 }
268
269 static HRESULT WINAPI IDirect3DDevice9Impl_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
270 {
271     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
272
273     TRACE("iface %p.\n", iface);
274
275     if (This->notreset)
276     {
277         TRACE("D3D9 device is marked not reset.\n");
278         return D3DERR_DEVICENOTRESET;
279     }
280
281     return D3D_OK;
282 }
283
284 static UINT WINAPI IDirect3DDevice9Impl_GetAvailableTextureMem(IDirect3DDevice9Ex *iface)
285 {
286     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
287     HRESULT hr;
288
289     TRACE("iface %p.\n", iface);
290
291     wined3d_mutex_lock();
292     hr = wined3d_device_get_available_texture_mem(This->wined3d_device);
293     wined3d_mutex_unlock();
294
295     return hr;
296 }
297
298 static HRESULT WINAPI IDirect3DDevice9Impl_EvictManagedResources(IDirect3DDevice9Ex *iface)
299 {
300     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
301
302     TRACE("iface %p.\n", iface);
303
304     wined3d_mutex_lock();
305     wined3d_device_evict_managed_resources(This->wined3d_device);
306     wined3d_mutex_unlock();
307
308     return D3D_OK;
309 }
310
311 static HRESULT WINAPI IDirect3DDevice9Impl_GetDirect3D(IDirect3DDevice9Ex *iface,
312         IDirect3D9 **ppD3D9)
313 {
314     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
315
316     TRACE("iface %p, d3d9 %p.\n", iface, ppD3D9);
317
318     if (NULL == ppD3D9) {
319         return D3DERR_INVALIDCALL;
320     }
321
322     return IDirect3D9Ex_QueryInterface(&This->d3d_parent->IDirect3D9Ex_iface,
323             &IID_IDirect3D9, (void **)ppD3D9);
324 }
325
326 static HRESULT WINAPI IDirect3DDevice9Impl_GetDeviceCaps(IDirect3DDevice9Ex *iface, D3DCAPS9 *pCaps)
327 {
328     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
329     HRESULT hrc = D3D_OK;
330     WINED3DCAPS *pWineCaps;
331
332     TRACE("iface %p, caps %p.\n", iface, pCaps);
333
334     if(NULL == pCaps){
335         return D3DERR_INVALIDCALL;
336     }
337     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
338     if(pWineCaps == NULL){
339         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
340     }
341
342     memset(pCaps, 0, sizeof(*pCaps));
343
344     wined3d_mutex_lock();
345     hrc = wined3d_device_get_device_caps(This->wined3d_device, pWineCaps);
346     wined3d_mutex_unlock();
347
348     WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
349     HeapFree(GetProcessHeap(), 0, pWineCaps);
350
351     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
352     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
353
354     filter_caps(pCaps);
355
356     TRACE("Returning %p %p\n", This, pCaps);
357     return hrc;
358 }
359
360 static HRESULT WINAPI IDirect3DDevice9Impl_GetDisplayMode(IDirect3DDevice9Ex *iface,
361         UINT iSwapChain, D3DDISPLAYMODE *pMode)
362 {
363     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
364     HRESULT hr;
365
366     TRACE("iface %p, swapchain %u, mode %p.\n", iface, iSwapChain, pMode);
367
368     wined3d_mutex_lock();
369     hr = wined3d_device_get_display_mode(This->wined3d_device, iSwapChain, (struct wined3d_display_mode *)pMode);
370     wined3d_mutex_unlock();
371
372     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
373
374     return hr;
375 }
376
377 static HRESULT WINAPI IDirect3DDevice9Impl_GetCreationParameters(IDirect3DDevice9Ex *iface,
378         D3DDEVICE_CREATION_PARAMETERS *pParameters)
379 {
380     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
381     HRESULT hr;
382
383     TRACE("iface %p, parameters %p.\n", iface, pParameters);
384
385     wined3d_mutex_lock();
386     hr = wined3d_device_get_creation_parameters(This->wined3d_device,
387             (struct wined3d_device_creation_parameters *)pParameters);
388     wined3d_mutex_unlock();
389
390     return hr;
391 }
392
393 static HRESULT WINAPI IDirect3DDevice9Impl_SetCursorProperties(IDirect3DDevice9Ex *iface,
394         UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9 *pCursorBitmap)
395 {
396     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
397     IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pCursorBitmap);
398     HRESULT hr;
399
400     TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
401             iface, XHotSpot, YHotSpot, pCursorBitmap);
402
403     if (!pCursorBitmap)
404     {
405         WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
406         return D3DERR_INVALIDCALL;
407     }
408
409     wined3d_mutex_lock();
410     hr = wined3d_device_set_cursor_properties(This->wined3d_device, XHotSpot, YHotSpot, pSurface->wined3d_surface);
411     wined3d_mutex_unlock();
412
413     return hr;
414 }
415
416 static void WINAPI IDirect3DDevice9Impl_SetCursorPosition(IDirect3DDevice9Ex *iface,
417         int XScreenSpace, int YScreenSpace, DWORD Flags)
418 {
419     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
420
421     TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, XScreenSpace, YScreenSpace, Flags);
422
423     wined3d_mutex_lock();
424     wined3d_device_set_cursor_position(This->wined3d_device, XScreenSpace, YScreenSpace, Flags);
425     wined3d_mutex_unlock();
426 }
427
428 static BOOL WINAPI IDirect3DDevice9Impl_ShowCursor(IDirect3DDevice9Ex *iface, BOOL bShow)
429 {
430     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
431     BOOL ret;
432
433     TRACE("iface %p, show %#x.\n", iface, bShow);
434
435     wined3d_mutex_lock();
436     ret = wined3d_device_show_cursor(This->wined3d_device, bShow);
437     wined3d_mutex_unlock();
438
439     return ret;
440 }
441
442 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
443         D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
444 {
445     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
446     IDirect3DSwapChain9Impl *object;
447     HRESULT hr;
448
449     TRACE("iface %p, present_parameters %p, swapchain %p.\n",
450             iface, present_parameters, swapchain);
451
452     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
453     if (!object)
454     {
455         ERR("Failed to allocate swapchain memory.\n");
456         return E_OUTOFMEMORY;
457     }
458
459     hr = swapchain_init(object, This, present_parameters);
460     if (FAILED(hr))
461     {
462         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
463         HeapFree(GetProcessHeap(), 0, object);
464         return hr;
465     }
466
467     TRACE("Created swapchain %p.\n", object);
468     *swapchain = (IDirect3DSwapChain9 *)object;
469
470     return D3D_OK;
471 }
472
473 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_GetSwapChain(IDirect3DDevice9Ex *iface,
474         UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
475 {
476     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
477     struct wined3d_swapchain *wined3d_swapchain = NULL;
478     HRESULT hr;
479
480     TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
481
482     wined3d_mutex_lock();
483     hr = wined3d_device_get_swapchain(This->wined3d_device, swapchain_idx, &wined3d_swapchain);
484     if (SUCCEEDED(hr) && wined3d_swapchain)
485     {
486        *swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
487        IDirect3DSwapChain9_AddRef(*swapchain);
488        wined3d_swapchain_decref(wined3d_swapchain);
489     }
490     else
491     {
492         *swapchain = NULL;
493     }
494     wined3d_mutex_unlock();
495
496     return hr;
497 }
498
499 static UINT WINAPI IDirect3DDevice9Impl_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
500 {
501     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
502     UINT count;
503
504     TRACE("iface %p.\n", iface);
505
506     wined3d_mutex_lock();
507     count = wined3d_device_get_swapchain_count(This->wined3d_device);
508     wined3d_mutex_unlock();
509
510     return count;
511 }
512
513 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
514 {
515     struct wined3d_resource_desc desc;
516
517     wined3d_resource_get_desc(resource, &desc);
518     if (desc.pool == WINED3D_POOL_DEFAULT)
519     {
520         IDirect3DSurface9 *surface;
521
522         if (desc.resource_type != WINED3D_RTYPE_SURFACE)
523         {
524             WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
525             return D3DERR_INVALIDCALL;
526         }
527
528         surface = wined3d_resource_get_parent(resource);
529
530         IDirect3DSurface9_AddRef(surface);
531         if (IDirect3DSurface9_Release(surface))
532         {
533             WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
534             return D3DERR_INVALIDCALL;
535         }
536
537         WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
538     }
539
540     return D3D_OK;
541 }
542
543 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Reset(IDirect3DDevice9Ex *iface,
544         D3DPRESENT_PARAMETERS *pPresentationParameters)
545 {
546     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
547     struct wined3d_swapchain_desc swapchain_desc;
548     HRESULT hr;
549
550     TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
551
552     /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
553      * such objects can still be used for rendering after their external d3d9 object has been destroyed.
554      * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
555      * make use of the hidden reference and destroys the objects.
556      *
557      * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
558      * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
559      */
560     wined3d_mutex_lock();
561
562     swapchain_desc.backbuffer_width = pPresentationParameters->BackBufferWidth;
563     swapchain_desc.backbuffer_height = pPresentationParameters->BackBufferHeight;
564     swapchain_desc.backbuffer_format = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
565     swapchain_desc.backbuffer_count = pPresentationParameters->BackBufferCount;
566     swapchain_desc.multisample_type = pPresentationParameters->MultiSampleType;
567     swapchain_desc.multisample_quality = pPresentationParameters->MultiSampleQuality;
568     swapchain_desc.swap_effect = pPresentationParameters->SwapEffect;
569     swapchain_desc.device_window = pPresentationParameters->hDeviceWindow;
570     swapchain_desc.windowed = pPresentationParameters->Windowed;
571     swapchain_desc.enable_auto_depth_stencil = pPresentationParameters->EnableAutoDepthStencil;
572     swapchain_desc.auto_depth_stencil_format = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
573     swapchain_desc.flags = pPresentationParameters->Flags;
574     swapchain_desc.refresh_rate = pPresentationParameters->FullScreen_RefreshRateInHz;
575     swapchain_desc.swap_interval = pPresentationParameters->PresentationInterval;
576     swapchain_desc.auto_restore_display_mode = TRUE;
577
578     hr = wined3d_device_reset(This->wined3d_device, &swapchain_desc, reset_enum_callback);
579     if (FAILED(hr))
580         This->notreset = TRUE;
581     else
582         This->notreset = FALSE;
583
584     wined3d_mutex_unlock();
585
586     return hr;
587 }
588
589 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Present(IDirect3DDevice9Ex *iface,
590         const RECT *pSourceRect, const RECT *pDestRect, HWND hDestWindowOverride,
591         const RGNDATA *pDirtyRegion)
592 {
593     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
594     HRESULT hr;
595
596     TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
597             iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
598
599     wined3d_mutex_lock();
600     hr = wined3d_device_present(This->wined3d_device, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
601     wined3d_mutex_unlock();
602
603     return hr;
604  }
605
606 static HRESULT WINAPI IDirect3DDevice9Impl_GetBackBuffer(IDirect3DDevice9Ex *iface,
607         UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 **ppBackBuffer)
608 {
609     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
610     struct wined3d_surface *wined3d_surface = NULL;
611     HRESULT hr;
612
613     TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
614             iface, iSwapChain, BackBuffer, Type, ppBackBuffer);
615
616     wined3d_mutex_lock();
617     hr = wined3d_device_get_back_buffer(This->wined3d_device, iSwapChain,
618             BackBuffer, (enum wined3d_backbuffer_type)Type, &wined3d_surface);
619     if (SUCCEEDED(hr) && wined3d_surface && ppBackBuffer)
620     {
621         *ppBackBuffer = wined3d_surface_get_parent(wined3d_surface);
622         IDirect3DSurface9_AddRef(*ppBackBuffer);
623         wined3d_surface_decref(wined3d_surface);
624     }
625     wined3d_mutex_unlock();
626
627     return hr;
628 }
629 static HRESULT WINAPI IDirect3DDevice9Impl_GetRasterStatus(IDirect3DDevice9Ex *iface,
630         UINT iSwapChain, D3DRASTER_STATUS *pRasterStatus)
631 {
632     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
633     HRESULT hr;
634
635     TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, iSwapChain, pRasterStatus);
636
637     wined3d_mutex_lock();
638     hr = wined3d_device_get_raster_status(This->wined3d_device,
639             iSwapChain, (struct wined3d_raster_status *)pRasterStatus);
640     wined3d_mutex_unlock();
641
642     return hr;
643 }
644
645 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(IDirect3DDevice9Ex *iface,
646         BOOL bEnableDialogs)
647 {
648     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
649     HRESULT hr;
650
651     TRACE("iface %p, enable %#x.\n", iface, bEnableDialogs);
652
653     wined3d_mutex_lock();
654     hr = wined3d_device_set_dialog_box_mode(This->wined3d_device, bEnableDialogs);
655     wined3d_mutex_unlock();
656
657     return hr;
658 }
659
660 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
661         DWORD Flags, const D3DGAMMARAMP *pRamp)
662 {
663     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
664
665     TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, iSwapChain, Flags, pRamp);
666
667     /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
668     wined3d_mutex_lock();
669     wined3d_device_set_gamma_ramp(This->wined3d_device, iSwapChain, Flags, (const struct wined3d_gamma_ramp *)pRamp);
670     wined3d_mutex_unlock();
671 }
672
673 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
674         D3DGAMMARAMP *pRamp)
675 {
676     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
677
678     TRACE("iface %p, swapchain %u, ramp %p.\n", iface, iSwapChain, pRamp);
679
680     /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
681     wined3d_mutex_lock();
682     wined3d_device_get_gamma_ramp(This->wined3d_device, iSwapChain, (struct wined3d_gamma_ramp *)pRamp);
683     wined3d_mutex_unlock();
684 }
685
686 static HRESULT WINAPI IDirect3DDevice9Impl_CreateTexture(IDirect3DDevice9Ex *iface,
687         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
688         D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
689 {
690     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
691     IDirect3DTexture9Impl *object;
692     BOOL set_mem = FALSE;
693     HRESULT hr;
694
695     TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
696             iface, width, height, levels, usage, format, pool, texture, shared_handle);
697
698     if (shared_handle)
699     {
700         if (pool == D3DPOOL_SYSTEMMEM)
701         {
702             if (levels != 1)
703                 return D3DERR_INVALIDCALL;
704             set_mem = TRUE;
705         }
706         else
707             FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
708     }
709
710     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
711     if (!object)
712     {
713         ERR("Failed to allocate texture memory.\n");
714         return D3DERR_OUTOFVIDEOMEMORY;
715     }
716
717     hr = texture_init(object, This, width, height, levels, usage, format, pool);
718     if (FAILED(hr))
719     {
720         WARN("Failed to initialize texture, hr %#x.\n", hr);
721         HeapFree(GetProcessHeap(), 0, object);
722         return hr;
723     }
724
725     if (set_mem)
726     {
727         struct wined3d_resource *resource;
728         IDirect3DSurface9Impl *surface;
729
730         resource = wined3d_texture_get_sub_resource(object->wined3d_texture, 0);
731         surface = wined3d_resource_get_parent(resource);
732         wined3d_surface_set_mem(surface->wined3d_surface, *shared_handle);
733     }
734
735     TRACE("Created texture %p.\n", object);
736     *texture = &object->IDirect3DTexture9_iface;
737
738     return D3D_OK;
739 }
740
741 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
742         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
743         D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
744 {
745     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
746     IDirect3DVolumeTexture9Impl *object;
747     HRESULT hr;
748
749     TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
750             iface, width, height, depth, levels);
751     TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
752             usage, format, pool, texture, shared_handle);
753
754     if (shared_handle)
755         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
756
757     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
758     if (!object)
759     {
760         ERR("Failed to allocate volume texture memory.\n");
761         return D3DERR_OUTOFVIDEOMEMORY;
762     }
763
764     hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
765     if (FAILED(hr))
766     {
767         WARN("Failed to initialize volume texture, hr %#x.\n", hr);
768         HeapFree(GetProcessHeap(), 0, object);
769         return hr;
770     }
771
772     TRACE("Created volume texture %p.\n", object);
773     *texture = &object->IDirect3DVolumeTexture9_iface;
774
775     return D3D_OK;
776 }
777
778 static HRESULT WINAPI IDirect3DDevice9Impl_CreateCubeTexture(IDirect3DDevice9Ex *iface,
779         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
780         IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
781 {
782     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
783     IDirect3DCubeTexture9Impl *object;
784     HRESULT hr;
785
786     TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
787             iface, edge_length, levels, usage, format, pool, texture, shared_handle);
788
789     if (shared_handle)
790         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
791
792     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
793     if (!object)
794     {
795         ERR("Failed to allocate cube texture memory.\n");
796         return D3DERR_OUTOFVIDEOMEMORY;
797     }
798
799     hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
800     if (FAILED(hr))
801     {
802         WARN("Failed to initialize cube texture, hr %#x.\n", hr);
803         HeapFree(GetProcessHeap(), 0, object);
804         return hr;
805     }
806
807     TRACE("Created cube texture %p.\n", object);
808     *texture = &object->IDirect3DCubeTexture9_iface;
809
810     return D3D_OK;
811 }
812
813 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size,
814         DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer,
815         HANDLE *shared_handle)
816 {
817     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
818     IDirect3DVertexBuffer9Impl *object;
819     HRESULT hr;
820
821     TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
822             iface, size, usage, fvf, pool, buffer, shared_handle);
823
824     if (shared_handle)
825         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
826
827     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
828     if (!object)
829     {
830         ERR("Failed to allocate buffer memory.\n");
831         return D3DERR_OUTOFVIDEOMEMORY;
832     }
833
834     hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
835     if (FAILED(hr))
836     {
837         WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
838         HeapFree(GetProcessHeap(), 0, object);
839         return hr;
840     }
841
842     TRACE("Created vertex buffer %p.\n", object);
843     *buffer = &object->IDirect3DVertexBuffer9_iface;
844
845     return D3D_OK;
846 }
847
848 static HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size,
849         DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer,
850         HANDLE *shared_handle)
851 {
852     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
853     IDirect3DIndexBuffer9Impl *object;
854     HRESULT hr;
855
856     TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
857             iface, size, usage, format, pool, buffer, shared_handle);
858
859     if (shared_handle)
860         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
861
862     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
863     if (!object)
864     {
865         ERR("Failed to allocate buffer memory.\n");
866         return D3DERR_OUTOFVIDEOMEMORY;
867     }
868
869     hr = indexbuffer_init(object, This, size, usage, format, pool);
870     if (FAILED(hr))
871     {
872         WARN("Failed to initialize index buffer, hr %#x.\n", hr);
873         HeapFree(GetProcessHeap(), 0, object);
874         return hr;
875     }
876
877     TRACE("Created index buffer %p.\n", object);
878     *buffer = &object->IDirect3DIndexBuffer9_iface;
879
880     return D3D_OK;
881 }
882
883 static HRESULT IDirect3DDevice9Impl_CreateSurface(IDirect3DDevice9Impl *device, UINT Width,
884         UINT Height, D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level,
885         IDirect3DSurface9 **ppSurface, UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample,
886         DWORD MultisampleQuality)
887 {
888     IDirect3DSurface9Impl *object;
889     HRESULT hr;
890
891     TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p.\n"
892             "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
893             device, Width, Height, Format, Lockable, Discard, Level, ppSurface, Usage, Pool,
894             MultiSample, MultisampleQuality);
895
896     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
897     if (!object)
898     {
899         FIXME("Failed to allocate surface memory.\n");
900         return D3DERR_OUTOFVIDEOMEMORY;
901     }
902
903     hr = surface_init(object, device, Width, Height, Format, Lockable, Discard, Level, Usage, Pool,
904             MultiSample, MultisampleQuality);
905     if (FAILED(hr))
906     {
907         WARN("Failed to initialize surface, hr %#x.\n", hr);
908         HeapFree(GetProcessHeap(), 0, object);
909         return hr;
910     }
911
912     TRACE("Created surface %p.\n", object);
913     *ppSurface = &object->IDirect3DSurface9_iface;
914
915     return D3D_OK;
916 }
917
918 static HRESULT WINAPI IDirect3DDevice9Impl_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT Width,
919         UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality,
920         BOOL Lockable, IDirect3DSurface9 **ppSurface, HANDLE *shared_handle)
921 {
922     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
923     HRESULT hr;
924
925     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
926             "lockable %#x, surface %p, shared_handle %p.\n",
927             iface, Width, Height, Format, MultiSample, MultisampleQuality,
928             Lockable, ppSurface, shared_handle);
929
930     if (shared_handle)
931         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
932
933     hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, Lockable,
934             FALSE /* Discard */, 0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,
935             MultiSample, MultisampleQuality);
936
937     return hr;
938 }
939
940 static HRESULT WINAPI IDirect3DDevice9Impl_CreateDepthStencilSurface(IDirect3DDevice9Ex *iface,
941         UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
942         DWORD MultisampleQuality, BOOL Discard, IDirect3DSurface9 **ppSurface,
943         HANDLE *shared_handle)
944 {
945     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
946     HRESULT hr;
947
948     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
949             "discard %#x, surface %p, shared_handle %p.\n",
950             iface, Width, Height, Format, MultiSample, MultisampleQuality,
951             Discard, ppSurface, shared_handle);
952
953     if (shared_handle)
954         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
955
956     hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
957             Discard, 0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample,
958             MultisampleQuality);
959
960     return hr;
961 }
962
963
964 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateSurface(IDirect3DDevice9Ex *iface,
965         IDirect3DSurface9 *pSourceSurface, const RECT *pSourceRect,
966         IDirect3DSurface9 *pDestinationSurface, const POINT *pDestPoint)
967 {
968     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
969     IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(pSourceSurface);
970     IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(pDestinationSurface);
971     HRESULT hr;
972
973     TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
974             iface, pSourceSurface, pSourceRect, pDestinationSurface, pDestPoint);
975
976     wined3d_mutex_lock();
977     hr = wined3d_device_update_surface(This->wined3d_device, src->wined3d_surface, pSourceRect,
978             dst->wined3d_surface, pDestPoint);
979     wined3d_mutex_unlock();
980
981     return hr;
982 }
983
984 static HRESULT WINAPI IDirect3DDevice9Impl_UpdateTexture(IDirect3DDevice9Ex *iface,
985         IDirect3DBaseTexture9 *src_texture, IDirect3DBaseTexture9 *dst_texture)
986 {
987     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
988     HRESULT hr;
989
990     TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
991
992     wined3d_mutex_lock();
993     hr = wined3d_device_update_texture(This->wined3d_device,
994             ((IDirect3DBaseTexture9Impl *)src_texture)->wined3d_texture,
995             ((IDirect3DBaseTexture9Impl *)dst_texture)->wined3d_texture);
996     wined3d_mutex_unlock();
997
998     return hr;
999 }
1000
1001 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1002         IDirect3DSurface9 *pRenderTarget, IDirect3DSurface9 *pDestSurface)
1003 {
1004     IDirect3DSurface9Impl *renderTarget = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
1005     IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1006     HRESULT hr;
1007
1008     TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, pRenderTarget, pDestSurface);
1009
1010     wined3d_mutex_lock();
1011     hr = wined3d_surface_get_render_target_data(destSurface->wined3d_surface, renderTarget->wined3d_surface);
1012     wined3d_mutex_unlock();
1013
1014     return hr;
1015 }
1016
1017 static HRESULT WINAPI IDirect3DDevice9Impl_GetFrontBufferData(IDirect3DDevice9Ex *iface,
1018         UINT iSwapChain, IDirect3DSurface9 *pDestSurface)
1019 {
1020     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1021     IDirect3DSurface9Impl *destSurface = unsafe_impl_from_IDirect3DSurface9(pDestSurface);
1022     HRESULT hr;
1023
1024     TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, iSwapChain, pDestSurface);
1025
1026     wined3d_mutex_lock();
1027     hr = wined3d_device_get_front_buffer_data(This->wined3d_device, iSwapChain, destSurface->wined3d_surface);
1028     wined3d_mutex_unlock();
1029
1030     return hr;
1031 }
1032
1033 static HRESULT WINAPI IDirect3DDevice9Impl_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *src_surface,
1034         const RECT *src_rect, IDirect3DSurface9 *dst_surface, const RECT *dst_rect, D3DTEXTUREFILTERTYPE filter)
1035 {
1036     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1037     IDirect3DSurface9Impl *src = unsafe_impl_from_IDirect3DSurface9(src_surface);
1038     IDirect3DSurface9Impl *dst = unsafe_impl_from_IDirect3DSurface9(dst_surface);
1039     HRESULT hr = D3DERR_INVALIDCALL;
1040     struct wined3d_resource_desc src_desc, dst_desc;
1041     struct wined3d_resource *wined3d_resource;
1042
1043     TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1044             iface, src_surface, src_rect, dst_surface, dst_rect, filter);
1045
1046     wined3d_mutex_lock();
1047     wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1048     wined3d_resource_get_desc(wined3d_resource, &dst_desc);
1049
1050     wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1051     wined3d_resource_get_desc(wined3d_resource, &src_desc);
1052
1053     if (src_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1054     {
1055         if (This->in_scene)
1056         {
1057             WARN("Rejecting depth / stencil blit while in scene.\n");
1058             goto done;
1059         }
1060
1061         if (src_rect)
1062         {
1063             if (src_rect->left || src_rect->top || src_rect->right != src_desc.width
1064                     || src_rect->bottom != src_desc.height)
1065             {
1066                 WARN("Rejecting depth / stencil blit with invalid source rect %s.\n",
1067                         wine_dbgstr_rect(src_rect));
1068                 goto done;
1069             }
1070         }
1071         if (dst_rect)
1072         {
1073             if (dst_rect->left || dst_rect->top || dst_rect->right != dst_desc.width
1074                     || dst_rect->bottom != dst_desc.height)
1075             {
1076                 WARN("Rejecting depth / stencil blit with invalid destination rect %s.\n",
1077                         wine_dbgstr_rect(dst_rect));
1078                 goto done;
1079             }
1080         }
1081         if (src_desc.width != dst_desc.width || src_desc.height != dst_desc.height)
1082         {
1083             WARN("Rejecting depth / stencil blit with mismatched surface sizes.\n");
1084             goto done;
1085         }
1086     }
1087
1088     hr = wined3d_surface_blt(dst->wined3d_surface, dst_rect, src->wined3d_surface, src_rect, 0, NULL, filter);
1089     if (hr == WINEDDERR_INVALIDRECT)
1090         hr = D3DERR_INVALIDCALL;
1091
1092 done:
1093     wined3d_mutex_unlock();
1094     return hr;
1095 }
1096
1097 static HRESULT WINAPI IDirect3DDevice9Impl_ColorFill(IDirect3DDevice9Ex *iface,
1098         IDirect3DSurface9 *pSurface, const RECT *pRect, D3DCOLOR color)
1099 {
1100     const struct wined3d_color c =
1101     {
1102         ((color >> 16) & 0xff) / 255.0f,
1103         ((color >>  8) & 0xff) / 255.0f,
1104         (color & 0xff) / 255.0f,
1105         ((color >> 24) & 0xff) / 255.0f,
1106     };
1107     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1108     IDirect3DSurface9Impl *surface = unsafe_impl_from_IDirect3DSurface9(pSurface);
1109     struct wined3d_resource *wined3d_resource;
1110     struct wined3d_resource_desc desc;
1111     HRESULT hr;
1112
1113     TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, pSurface, pRect, color);
1114
1115     wined3d_mutex_lock();
1116
1117     wined3d_resource = wined3d_surface_get_resource(surface->wined3d_surface);
1118     wined3d_resource_get_desc(wined3d_resource, &desc);
1119
1120     /* This method is only allowed with surfaces that are render targets, or
1121      * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1122     if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3D_POOL_DEFAULT)
1123     {
1124         wined3d_mutex_unlock();
1125         WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1126         return D3DERR_INVALIDCALL;
1127     }
1128
1129     /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1130     hr = wined3d_device_color_fill(This->wined3d_device, surface->wined3d_surface, pRect, &c);
1131
1132     wined3d_mutex_unlock();
1133
1134     return hr;
1135 }
1136
1137 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(IDirect3DDevice9Ex *iface,
1138         UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface,
1139         HANDLE *shared_handle)
1140 {
1141     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1142     HRESULT hr;
1143
1144     TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1145             iface, Width, Height, Format, Pool, ppSurface, shared_handle);
1146
1147     if (shared_handle)
1148         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
1149
1150     if (Pool == D3DPOOL_MANAGED)
1151     {
1152         FIXME("Attempting to create a managed offscreen plain surface\n");
1153         return D3DERR_INVALIDCALL;
1154     }
1155         /*
1156         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
1157         but then...
1158         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
1159         Why, their always lockable?
1160         should I change the usage to dynamic?
1161         */
1162     hr = IDirect3DDevice9Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
1163             FALSE /* Discard */, 0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */,
1164             Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1165
1166     return hr;
1167 }
1168
1169 /* TODO: move to wineD3D */
1170 static HRESULT WINAPI IDirect3DDevice9Impl_SetRenderTarget(IDirect3DDevice9Ex *iface,
1171         DWORD RenderTargetIndex, IDirect3DSurface9 *pRenderTarget)
1172 {
1173     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1174     IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pRenderTarget);
1175     HRESULT hr;
1176
1177     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, pRenderTarget);
1178
1179     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1180     {
1181         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1182         return D3DERR_INVALIDCALL;
1183     }
1184
1185     wined3d_mutex_lock();
1186     hr = wined3d_device_set_render_target(This->wined3d_device, RenderTargetIndex,
1187             pSurface ? pSurface->wined3d_surface : NULL, TRUE);
1188     wined3d_mutex_unlock();
1189
1190     return hr;
1191 }
1192
1193 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTarget(IDirect3DDevice9Ex *iface,
1194         DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget)
1195 {
1196     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1197     struct wined3d_surface *wined3d_surface;
1198     HRESULT hr;
1199
1200     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, ppRenderTarget);
1201
1202     if (ppRenderTarget == NULL) {
1203         return D3DERR_INVALIDCALL;
1204     }
1205
1206     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1207     {
1208         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1209         return D3DERR_INVALIDCALL;
1210     }
1211
1212     wined3d_mutex_lock();
1213     hr = wined3d_device_get_render_target(This->wined3d_device, RenderTargetIndex, &wined3d_surface);
1214     if (SUCCEEDED(hr))
1215     {
1216         *ppRenderTarget = wined3d_surface_get_parent(wined3d_surface);
1217         IDirect3DSurface9_AddRef(*ppRenderTarget);
1218         wined3d_surface_decref(wined3d_surface);
1219     }
1220     else
1221     {
1222         if (hr != WINED3DERR_NOTFOUND)
1223             WARN("Failed to get render target %u, hr %#x.\n", RenderTargetIndex, hr);
1224         *ppRenderTarget = NULL;
1225     }
1226     wined3d_mutex_unlock();
1227
1228     return hr;
1229 }
1230
1231 static HRESULT WINAPI IDirect3DDevice9Impl_SetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1232         IDirect3DSurface9 *pZStencilSurface)
1233 {
1234     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1235     IDirect3DSurface9Impl *pSurface = unsafe_impl_from_IDirect3DSurface9(pZStencilSurface);
1236     HRESULT hr;
1237
1238     TRACE("iface %p, depth_stencil %p.\n", iface, pZStencilSurface);
1239
1240     wined3d_mutex_lock();
1241     hr = wined3d_device_set_depth_stencil(This->wined3d_device, pSurface ? pSurface->wined3d_surface : NULL);
1242     wined3d_mutex_unlock();
1243
1244     return hr;
1245 }
1246
1247 static HRESULT WINAPI IDirect3DDevice9Impl_GetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1248         IDirect3DSurface9 **ppZStencilSurface)
1249 {
1250     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1251     struct wined3d_surface *wined3d_surface;
1252     HRESULT hr;
1253
1254     TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1255
1256     if(ppZStencilSurface == NULL){
1257         return D3DERR_INVALIDCALL;
1258     }
1259
1260     wined3d_mutex_lock();
1261     hr = wined3d_device_get_depth_stencil(This->wined3d_device, &wined3d_surface);
1262     if (SUCCEEDED(hr))
1263     {
1264         *ppZStencilSurface = wined3d_surface_get_parent(wined3d_surface);
1265         IDirect3DSurface9_AddRef(*ppZStencilSurface);
1266         wined3d_surface_decref(wined3d_surface);
1267     }
1268     else
1269     {
1270         if (hr != WINED3DERR_NOTFOUND)
1271                 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1272         *ppZStencilSurface = NULL;
1273     }
1274     wined3d_mutex_unlock();
1275
1276     return hr;
1277 }
1278
1279 static HRESULT WINAPI IDirect3DDevice9Impl_BeginScene(IDirect3DDevice9Ex *iface)
1280 {
1281     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1282     HRESULT hr;
1283
1284     TRACE("iface %p.\n", iface);
1285
1286     wined3d_mutex_lock();
1287     hr = wined3d_device_begin_scene(This->wined3d_device);
1288     if (SUCCEEDED(hr)) This->in_scene = TRUE;
1289     wined3d_mutex_unlock();
1290
1291     return hr;
1292 }
1293
1294 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_EndScene(IDirect3DDevice9Ex *iface)
1295 {
1296     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1297     HRESULT hr;
1298
1299     TRACE("iface %p.\n", iface);
1300
1301     wined3d_mutex_lock();
1302     hr = wined3d_device_end_scene(This->wined3d_device);
1303     if (SUCCEEDED(hr)) This->in_scene = FALSE;
1304     wined3d_mutex_unlock();
1305
1306     return hr;
1307 }
1308
1309 static HRESULT WINAPI IDirect3DDevice9Impl_Clear(IDirect3DDevice9Ex *iface, DWORD rect_count,
1310         const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1311 {
1312     const struct wined3d_color c =
1313     {
1314         ((color >> 16) & 0xff) / 255.0f,
1315         ((color >>  8) & 0xff) / 255.0f,
1316         (color & 0xff) / 255.0f,
1317         ((color >> 24) & 0xff) / 255.0f,
1318     };
1319     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1320     HRESULT hr;
1321
1322     TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1323             iface, rect_count, rects, flags, color, z, stencil);
1324
1325     wined3d_mutex_lock();
1326     hr = wined3d_device_clear(This->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1327     wined3d_mutex_unlock();
1328
1329     return hr;
1330 }
1331
1332 static HRESULT WINAPI IDirect3DDevice9Impl_SetTransform(IDirect3DDevice9Ex *iface,
1333         D3DTRANSFORMSTATETYPE State, const D3DMATRIX *lpMatrix)
1334 {
1335     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1336     HRESULT hr;
1337
1338     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1339
1340     /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1341     wined3d_mutex_lock();
1342     hr = wined3d_device_set_transform(This->wined3d_device, State, (const struct wined3d_matrix *)lpMatrix);
1343     wined3d_mutex_unlock();
1344
1345     return hr;
1346 }
1347
1348 static HRESULT WINAPI IDirect3DDevice9Impl_GetTransform(IDirect3DDevice9Ex *iface,
1349         D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
1350 {
1351     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1352     HRESULT hr;
1353
1354     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1355
1356     /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1357     wined3d_mutex_lock();
1358     hr = wined3d_device_get_transform(This->wined3d_device, State, (struct wined3d_matrix *)pMatrix);
1359     wined3d_mutex_unlock();
1360
1361     return hr;
1362 }
1363
1364 static HRESULT WINAPI IDirect3DDevice9Impl_MultiplyTransform(IDirect3DDevice9Ex *iface,
1365         D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
1366 {
1367     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1368     HRESULT hr;
1369
1370     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1371
1372     /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1373     wined3d_mutex_lock();
1374     hr = wined3d_device_multiply_transform(This->wined3d_device, State, (const struct wined3d_matrix *)pMatrix);
1375     wined3d_mutex_unlock();
1376
1377     return hr;
1378 }
1379
1380 static HRESULT WINAPI IDirect3DDevice9Impl_SetViewport(IDirect3DDevice9Ex *iface,
1381         const D3DVIEWPORT9 *pViewport)
1382 {
1383     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1384     HRESULT hr;
1385
1386     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1387
1388     /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1389     wined3d_mutex_lock();
1390     hr = wined3d_device_set_viewport(This->wined3d_device, (const struct wined3d_viewport *)pViewport);
1391     wined3d_mutex_unlock();
1392
1393     return hr;
1394 }
1395
1396 static HRESULT WINAPI IDirect3DDevice9Impl_GetViewport(IDirect3DDevice9Ex *iface,
1397         D3DVIEWPORT9 *pViewport)
1398 {
1399     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1400     HRESULT hr;
1401
1402     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1403
1404     /* Note: D3DVIEWPORT9 is compatible with struct wined3d_viewport. */
1405     wined3d_mutex_lock();
1406     hr = wined3d_device_get_viewport(This->wined3d_device, (struct wined3d_viewport *)pViewport);
1407     wined3d_mutex_unlock();
1408
1409     return hr;
1410 }
1411
1412 static HRESULT WINAPI IDirect3DDevice9Impl_SetMaterial(IDirect3DDevice9Ex *iface,
1413         const D3DMATERIAL9 *pMaterial)
1414 {
1415     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1416     HRESULT hr;
1417
1418     TRACE("iface %p, material %p.\n", iface, pMaterial);
1419
1420     /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1421     wined3d_mutex_lock();
1422     hr = wined3d_device_set_material(This->wined3d_device, (const struct wined3d_material *)pMaterial);
1423     wined3d_mutex_unlock();
1424
1425     return hr;
1426 }
1427
1428 static HRESULT WINAPI IDirect3DDevice9Impl_GetMaterial(IDirect3DDevice9Ex *iface,
1429         D3DMATERIAL9 *pMaterial)
1430 {
1431     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1432     HRESULT hr;
1433
1434     TRACE("iface %p, material %p.\n", iface, pMaterial);
1435
1436     /* Note: D3DMATERIAL9 is compatible with struct wined3d_material. */
1437     wined3d_mutex_lock();
1438     hr = wined3d_device_get_material(This->wined3d_device, (struct wined3d_material *)pMaterial);
1439     wined3d_mutex_unlock();
1440
1441     return hr;
1442 }
1443
1444 static HRESULT WINAPI IDirect3DDevice9Impl_SetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1445         const D3DLIGHT9 *pLight)
1446 {
1447     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1448     HRESULT hr;
1449
1450     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1451
1452     /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1453     wined3d_mutex_lock();
1454     hr = wined3d_device_set_light(This->wined3d_device, Index, (const struct wined3d_light *)pLight);
1455     wined3d_mutex_unlock();
1456
1457     return hr;
1458 }
1459
1460 static HRESULT WINAPI IDirect3DDevice9Impl_GetLight(IDirect3DDevice9Ex *iface, DWORD Index,
1461         D3DLIGHT9 *pLight)
1462 {
1463     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1464     HRESULT hr;
1465
1466     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1467
1468     /* Note: D3DLIGHT9 is compatible with struct wined3d_light. */
1469     wined3d_mutex_lock();
1470     hr = wined3d_device_get_light(This->wined3d_device, Index, (struct wined3d_light *)pLight);
1471     wined3d_mutex_unlock();
1472
1473     return hr;
1474 }
1475
1476 static HRESULT WINAPI IDirect3DDevice9Impl_LightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1477         BOOL Enable)
1478 {
1479     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1480     HRESULT hr;
1481
1482     TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1483
1484     wined3d_mutex_lock();
1485     hr = wined3d_device_set_light_enable(This->wined3d_device, Index, Enable);
1486     wined3d_mutex_unlock();
1487
1488     return hr;
1489 }
1490
1491 static HRESULT WINAPI IDirect3DDevice9Impl_GetLightEnable(IDirect3DDevice9Ex *iface, DWORD Index,
1492         BOOL *pEnable)
1493 {
1494     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1495     HRESULT hr;
1496
1497     TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1498
1499     wined3d_mutex_lock();
1500     hr = wined3d_device_get_light_enable(This->wined3d_device, Index, pEnable);
1501     wined3d_mutex_unlock();
1502
1503     return hr;
1504 }
1505
1506 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1507         const float *pPlane)
1508 {
1509     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1510     HRESULT hr;
1511
1512     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1513
1514     wined3d_mutex_lock();
1515     hr = wined3d_device_set_clip_plane(This->wined3d_device, Index, pPlane);
1516     wined3d_mutex_unlock();
1517
1518     return hr;
1519 }
1520
1521 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipPlane(IDirect3DDevice9Ex *iface, DWORD Index,
1522         float *pPlane)
1523 {
1524     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1525     HRESULT hr;
1526
1527     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1528
1529     wined3d_mutex_lock();
1530     hr = wined3d_device_get_clip_plane(This->wined3d_device, Index, pPlane);
1531     wined3d_mutex_unlock();
1532
1533     return hr;
1534 }
1535
1536 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetRenderState(IDirect3DDevice9Ex *iface,
1537         D3DRENDERSTATETYPE State, DWORD Value)
1538 {
1539     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1540     HRESULT hr;
1541
1542     TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1543
1544     wined3d_mutex_lock();
1545     hr = wined3d_device_set_render_state(This->wined3d_device, State, Value);
1546     wined3d_mutex_unlock();
1547
1548     return hr;
1549 }
1550
1551 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderState(IDirect3DDevice9Ex *iface,
1552         D3DRENDERSTATETYPE State, DWORD *pValue)
1553 {
1554     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1555     HRESULT hr;
1556
1557     TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1558
1559     wined3d_mutex_lock();
1560     hr = wined3d_device_get_render_state(This->wined3d_device, State, pValue);
1561     wined3d_mutex_unlock();
1562
1563     return hr;
1564 }
1565
1566 static HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(IDirect3DDevice9Ex *iface,
1567         D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1568 {
1569     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1570     IDirect3DStateBlock9Impl *object;
1571     HRESULT hr;
1572
1573     TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1574
1575     if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1576     {
1577         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1578         return D3DERR_INVALIDCALL;
1579     }
1580
1581     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1582     if (!object)
1583     {
1584         ERR("Failed to allocate stateblock memory.\n");
1585         return E_OUTOFMEMORY;
1586     }
1587
1588     hr = stateblock_init(object, This, type, NULL);
1589     if (FAILED(hr))
1590     {
1591         WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1592         HeapFree(GetProcessHeap(), 0, object);
1593         return hr;
1594     }
1595
1596     TRACE("Created stateblock %p.\n", object);
1597     *stateblock = &object->IDirect3DStateBlock9_iface;
1598
1599     return D3D_OK;
1600 }
1601
1602 static HRESULT WINAPI IDirect3DDevice9Impl_BeginStateBlock(IDirect3DDevice9Ex *iface)
1603 {
1604     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1605     HRESULT hr;
1606
1607     TRACE("iface %p.\n", iface);
1608
1609     wined3d_mutex_lock();
1610     hr = wined3d_device_begin_stateblock(This->wined3d_device);
1611     wined3d_mutex_unlock();
1612
1613     return hr;
1614 }
1615
1616 static HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface,
1617         IDirect3DStateBlock9 **stateblock)
1618 {
1619     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1620     struct wined3d_stateblock *wined3d_stateblock;
1621     IDirect3DStateBlock9Impl *object;
1622     HRESULT hr;
1623
1624     TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1625
1626     wined3d_mutex_lock();
1627     hr = wined3d_device_end_stateblock(This->wined3d_device, &wined3d_stateblock);
1628     wined3d_mutex_unlock();
1629     if (FAILED(hr))
1630     {
1631        WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1632        return hr;
1633     }
1634
1635     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1636     if (!object)
1637     {
1638         ERR("Failed to allocate stateblock memory.\n");
1639         wined3d_mutex_lock();
1640         wined3d_stateblock_decref(wined3d_stateblock);
1641         wined3d_mutex_unlock();
1642         return E_OUTOFMEMORY;
1643     }
1644
1645     hr = stateblock_init(object, This, 0, wined3d_stateblock);
1646     if (FAILED(hr))
1647     {
1648         WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1649         wined3d_mutex_lock();
1650         wined3d_stateblock_decref(wined3d_stateblock);
1651         wined3d_mutex_unlock();
1652         HeapFree(GetProcessHeap(), 0, object);
1653         return hr;
1654     }
1655
1656     TRACE("Created stateblock %p.\n", object);
1657     *stateblock = &object->IDirect3DStateBlock9_iface;
1658
1659     return D3D_OK;
1660 }
1661
1662 static HRESULT WINAPI IDirect3DDevice9Impl_SetClipStatus(IDirect3DDevice9Ex *iface,
1663         const D3DCLIPSTATUS9 *pClipStatus)
1664 {
1665     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1666     HRESULT hr;
1667
1668     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1669
1670     wined3d_mutex_lock();
1671     hr = wined3d_device_set_clip_status(This->wined3d_device, (const struct wined3d_clip_status *)pClipStatus);
1672     wined3d_mutex_unlock();
1673
1674     return hr;
1675 }
1676
1677 static HRESULT WINAPI IDirect3DDevice9Impl_GetClipStatus(IDirect3DDevice9Ex *iface,
1678         D3DCLIPSTATUS9 *pClipStatus)
1679 {
1680     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1681     HRESULT hr;
1682
1683     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1684
1685     wined3d_mutex_lock();
1686     hr = wined3d_device_get_clip_status(This->wined3d_device, (struct wined3d_clip_status *)pClipStatus);
1687     wined3d_mutex_unlock();
1688
1689     return hr;
1690 }
1691
1692 static HRESULT WINAPI IDirect3DDevice9Impl_GetTexture(IDirect3DDevice9Ex *iface, DWORD Stage,
1693         IDirect3DBaseTexture9 **ppTexture)
1694 {
1695     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1696     struct wined3d_texture *wined3d_texture = NULL;
1697     HRESULT hr;
1698
1699     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1700
1701     if(ppTexture == NULL){
1702         return D3DERR_INVALIDCALL;
1703     }
1704
1705     wined3d_mutex_lock();
1706     hr = wined3d_device_get_texture(This->wined3d_device, Stage, &wined3d_texture);
1707     if (SUCCEEDED(hr) && wined3d_texture)
1708     {
1709         *ppTexture = wined3d_texture_get_parent(wined3d_texture);
1710         IDirect3DBaseTexture9_AddRef(*ppTexture);
1711         wined3d_texture_decref(wined3d_texture);
1712     }
1713     else
1714     {
1715         if (FAILED(hr))
1716         {
1717             WARN("Call to get texture (%u) failed (%p).\n", Stage, wined3d_texture);
1718         }
1719         *ppTexture = NULL;
1720     }
1721     wined3d_mutex_unlock();
1722
1723     return hr;
1724 }
1725
1726 static HRESULT WINAPI IDirect3DDevice9Impl_SetTexture(IDirect3DDevice9Ex *iface, DWORD stage,
1727         IDirect3DBaseTexture9 *texture)
1728 {
1729     IDirect3DDevice9Impl *device = impl_from_IDirect3DDevice9Ex(iface);
1730     HRESULT hr;
1731
1732     TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1733
1734     wined3d_mutex_lock();
1735     hr = wined3d_device_set_texture(device->wined3d_device, stage,
1736             texture ? ((IDirect3DBaseTexture9Impl *)texture)->wined3d_texture : NULL);
1737     wined3d_mutex_unlock();
1738
1739     return hr;
1740 }
1741
1742 static const enum wined3d_texture_stage_state tss_lookup[] =
1743 {
1744     WINED3D_TSS_INVALID,                    /*  0, unused */
1745     WINED3D_TSS_COLOR_OP,                   /*  1, D3DTSS_COLOROP */
1746     WINED3D_TSS_COLOR_ARG1,                 /*  2, D3DTSS_COLORARG1 */
1747     WINED3D_TSS_COLOR_ARG2,                 /*  3, D3DTSS_COLORARG2 */
1748     WINED3D_TSS_ALPHA_OP,                   /*  4, D3DTSS_ALPHAOP */
1749     WINED3D_TSS_ALPHA_ARG1,                 /*  5, D3DTSS_ALPHAARG1 */
1750     WINED3D_TSS_ALPHA_ARG2,                 /*  6, D3DTSS_ALPHAARG2 */
1751     WINED3D_TSS_BUMPENV_MAT00,              /*  7, D3DTSS_BUMPENVMAT00 */
1752     WINED3D_TSS_BUMPENV_MAT01,              /*  8, D3DTSS_BUMPENVMAT01 */
1753     WINED3D_TSS_BUMPENV_MAT10,              /*  9, D3DTSS_BUMPENVMAT10 */
1754     WINED3D_TSS_BUMPENV_MAT11,              /* 10, D3DTSS_BUMPENVMAT11 */
1755     WINED3D_TSS_TEXCOORD_INDEX,             /* 11, D3DTSS_TEXCOORDINDEX */
1756     WINED3D_TSS_INVALID,                    /* 12, unused */
1757     WINED3D_TSS_INVALID,                    /* 13, unused */
1758     WINED3D_TSS_INVALID,                    /* 14, unused */
1759     WINED3D_TSS_INVALID,                    /* 15, unused */
1760     WINED3D_TSS_INVALID,                    /* 16, unused */
1761     WINED3D_TSS_INVALID,                    /* 17, unused */
1762     WINED3D_TSS_INVALID,                    /* 18, unused */
1763     WINED3D_TSS_INVALID,                    /* 19, unused */
1764     WINED3D_TSS_INVALID,                    /* 20, unused */
1765     WINED3D_TSS_INVALID,                    /* 21, unused */
1766     WINED3D_TSS_BUMPENV_LSCALE,             /* 22, D3DTSS_BUMPENVLSCALE */
1767     WINED3D_TSS_BUMPENV_LOFFSET,            /* 23, D3DTSS_BUMPENVLOFFSET */
1768     WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS,    /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1769     WINED3D_TSS_INVALID,                    /* 25, unused */
1770     WINED3D_TSS_COLOR_ARG0,                 /* 26, D3DTSS_COLORARG0 */
1771     WINED3D_TSS_ALPHA_ARG0,                 /* 27, D3DTSS_ALPHAARG0 */
1772     WINED3D_TSS_RESULT_ARG,                 /* 28, D3DTSS_RESULTARG */
1773     WINED3D_TSS_INVALID,                    /* 29, unused */
1774     WINED3D_TSS_INVALID,                    /* 30, unused */
1775     WINED3D_TSS_INVALID,                    /* 31, unused */
1776     WINED3D_TSS_CONSTANT,                   /* 32, D3DTSS_CONSTANT */
1777 };
1778
1779 static HRESULT WINAPI IDirect3DDevice9Impl_GetTextureStageState(IDirect3DDevice9Ex *iface,
1780         DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1781 {
1782     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1783     HRESULT hr;
1784
1785     TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1786
1787     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1788     {
1789         WARN("Invalid Type %#x passed.\n", Type);
1790         return D3D_OK;
1791     }
1792
1793     wined3d_mutex_lock();
1794     hr = wined3d_device_get_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], pValue);
1795     wined3d_mutex_unlock();
1796
1797     return hr;
1798 }
1799
1800 static HRESULT WINAPI IDirect3DDevice9Impl_SetTextureStageState(IDirect3DDevice9Ex *iface,
1801         DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1802 {
1803     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1804     HRESULT hr;
1805
1806     TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1807
1808     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1809     {
1810         WARN("Invalid Type %#x passed.\n", Type);
1811         return D3D_OK;
1812     }
1813
1814     wined3d_mutex_lock();
1815     hr = wined3d_device_set_texture_stage_state(This->wined3d_device, Stage, tss_lookup[Type], Value);
1816     wined3d_mutex_unlock();
1817
1818     return hr;
1819 }
1820
1821 static HRESULT WINAPI IDirect3DDevice9Impl_GetSamplerState(IDirect3DDevice9Ex *iface, DWORD Sampler,
1822         D3DSAMPLERSTATETYPE Type, DWORD *pValue)
1823 {
1824     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1825     HRESULT hr;
1826
1827     TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, Sampler, Type, pValue);
1828
1829     wined3d_mutex_lock();
1830     hr = wined3d_device_get_sampler_state(This->wined3d_device, Sampler, Type, pValue);
1831     wined3d_mutex_unlock();
1832
1833     return hr;
1834 }
1835
1836 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetSamplerState(IDirect3DDevice9Ex *iface,
1837         DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value)
1838 {
1839     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1840     HRESULT hr;
1841
1842     TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, Sampler, Type, Value);
1843
1844     wined3d_mutex_lock();
1845     hr = wined3d_device_set_sampler_state(This->wined3d_device, Sampler, Type, Value);
1846     wined3d_mutex_unlock();
1847
1848     return hr;
1849 }
1850
1851 static HRESULT WINAPI IDirect3DDevice9Impl_ValidateDevice(IDirect3DDevice9Ex *iface,
1852         DWORD *pNumPasses)
1853 {
1854     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1855     HRESULT hr;
1856
1857     TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1858
1859     wined3d_mutex_lock();
1860     hr = wined3d_device_validate_device(This->wined3d_device, pNumPasses);
1861     wined3d_mutex_unlock();
1862
1863     return hr;
1864 }
1865
1866 static HRESULT WINAPI IDirect3DDevice9Impl_SetPaletteEntries(IDirect3DDevice9Ex *iface,
1867         UINT PaletteNumber, const PALETTEENTRY *pEntries)
1868 {
1869     FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1870
1871     return D3DERR_INVALIDCALL;
1872 }
1873
1874 static HRESULT WINAPI IDirect3DDevice9Impl_GetPaletteEntries(IDirect3DDevice9Ex *iface,
1875         UINT PaletteNumber, PALETTEENTRY *pEntries)
1876 {
1877     FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, PaletteNumber, pEntries);
1878
1879     return D3DERR_INVALIDCALL;
1880 }
1881
1882 static HRESULT WINAPI IDirect3DDevice9Impl_SetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1883         UINT PaletteNumber)
1884 {
1885     FIXME("iface %p, palette_idx %u unimplemented.\n", iface, PaletteNumber);
1886
1887     return D3DERR_INVALIDCALL;
1888 }
1889
1890 static HRESULT WINAPI IDirect3DDevice9Impl_GetCurrentTexturePalette(IDirect3DDevice9Ex *iface,
1891         UINT *PaletteNumber)
1892 {
1893     FIXME("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1894
1895     return D3DERR_INVALIDCALL;
1896 }
1897
1898 static HRESULT WINAPI IDirect3DDevice9Impl_SetScissorRect(IDirect3DDevice9Ex *iface,
1899         const RECT *pRect)
1900 {
1901     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1902     HRESULT hr;
1903
1904     TRACE("iface %p, rect %p.\n", iface, pRect);
1905
1906     wined3d_mutex_lock();
1907     hr = wined3d_device_set_scissor_rect(This->wined3d_device, pRect);
1908     wined3d_mutex_unlock();
1909
1910     return hr;
1911 }
1912
1913 static HRESULT WINAPI IDirect3DDevice9Impl_GetScissorRect(IDirect3DDevice9Ex *iface, RECT *pRect)
1914 {
1915     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1916     HRESULT hr;
1917
1918     TRACE("iface %p, rect %p.\n", iface, pRect);
1919
1920     wined3d_mutex_lock();
1921     hr = wined3d_device_get_scissor_rect(This->wined3d_device, pRect);
1922     wined3d_mutex_unlock();
1923
1924     return hr;
1925 }
1926
1927 static HRESULT WINAPI IDirect3DDevice9Impl_SetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface,
1928         BOOL bSoftware)
1929 {
1930     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1931     HRESULT hr;
1932
1933     TRACE("iface %p, software %#x.\n", iface, bSoftware);
1934
1935     wined3d_mutex_lock();
1936     hr = wined3d_device_set_software_vertex_processing(This->wined3d_device, bSoftware);
1937     wined3d_mutex_unlock();
1938
1939     return hr;
1940 }
1941
1942 static BOOL WINAPI IDirect3DDevice9Impl_GetSoftwareVertexProcessing(IDirect3DDevice9Ex *iface)
1943 {
1944     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1945     BOOL ret;
1946
1947     TRACE("iface %p.\n", iface);
1948
1949     wined3d_mutex_lock();
1950     ret = wined3d_device_get_software_vertex_processing(This->wined3d_device);
1951     wined3d_mutex_unlock();
1952
1953     return ret;
1954 }
1955
1956 static HRESULT WINAPI IDirect3DDevice9Impl_SetNPatchMode(IDirect3DDevice9Ex *iface, float nSegments)
1957 {
1958     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1959     HRESULT hr;
1960
1961     TRACE("iface %p, segment_count %.8e.\n", iface, nSegments);
1962
1963     wined3d_mutex_lock();
1964     hr = wined3d_device_set_npatch_mode(This->wined3d_device, nSegments);
1965     wined3d_mutex_unlock();
1966
1967     return hr;
1968 }
1969
1970 static float WINAPI IDirect3DDevice9Impl_GetNPatchMode(IDirect3DDevice9Ex *iface)
1971 {
1972     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1973     float ret;
1974
1975     TRACE("iface %p.\n", iface);
1976
1977     wined3d_mutex_lock();
1978     ret = wined3d_device_get_npatch_mode(This->wined3d_device);
1979     wined3d_mutex_unlock();
1980
1981     return ret;
1982 }
1983
1984 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(IDirect3DDevice9Ex *iface,
1985         D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1986 {
1987     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
1988     HRESULT hr;
1989
1990     TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1991             iface, PrimitiveType, StartVertex, PrimitiveCount);
1992
1993     wined3d_mutex_lock();
1994     wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
1995     hr = wined3d_device_draw_primitive(This->wined3d_device, StartVertex,
1996             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1997     wined3d_mutex_unlock();
1998
1999     return hr;
2000 }
2001
2002 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitive(IDirect3DDevice9Ex *iface,
2003         D3DPRIMITIVETYPE PrimitiveType, INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices,
2004         UINT startIndex, UINT primCount)
2005 {
2006     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2007     HRESULT hr;
2008
2009     TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
2010             "vertex_count %u, start_idx %u, primitive_count %u.\n",
2011             iface, PrimitiveType, BaseVertexIndex, MinVertexIndex,
2012             NumVertices, startIndex, primCount);
2013
2014     wined3d_mutex_lock();
2015     wined3d_device_set_base_vertex_index(This->wined3d_device, BaseVertexIndex);
2016     wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2017     hr = wined3d_device_draw_indexed_primitive(This->wined3d_device, startIndex,
2018             vertex_count_from_primitive_count(PrimitiveType, primCount));
2019     wined3d_mutex_unlock();
2020
2021     return hr;
2022 }
2023
2024 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitiveUP(IDirect3DDevice9Ex *iface,
2025         D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
2026         UINT VertexStreamZeroStride)
2027 {
2028     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2029     HRESULT hr;
2030
2031     TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2032             iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
2033
2034     wined3d_mutex_lock();
2035     wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2036     hr = wined3d_device_draw_primitive_up(This->wined3d_device,
2037             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
2038             pVertexStreamZeroData, VertexStreamZeroStride);
2039     wined3d_mutex_unlock();
2040
2041     return hr;
2042 }
2043
2044 static HRESULT WINAPI IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(IDirect3DDevice9Ex *iface,
2045         D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
2046         UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
2047         const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
2048 {
2049     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2050     HRESULT hr;
2051
2052     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
2053             "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2054             iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
2055             pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
2056
2057     wined3d_mutex_lock();
2058     wined3d_device_set_primitive_type(This->wined3d_device, PrimitiveType);
2059     hr = wined3d_device_draw_indexed_primitive_up(This->wined3d_device,
2060             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
2061             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
2062     wined3d_mutex_unlock();
2063
2064     return hr;
2065 }
2066
2067 static HRESULT WINAPI IDirect3DDevice9Impl_ProcessVertices(IDirect3DDevice9Ex *iface,
2068         UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9 *pDestBuffer,
2069         IDirect3DVertexDeclaration9 *pVertexDecl, DWORD Flags)
2070 {
2071     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2072     IDirect3DVertexBuffer9Impl *dest = unsafe_impl_from_IDirect3DVertexBuffer9(pDestBuffer);
2073     IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
2074     HRESULT hr;
2075
2076     TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
2077             iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags);
2078
2079     wined3d_mutex_lock();
2080     hr = wined3d_device_process_vertices(This->wined3d_device, SrcStartIndex, DestIndex, VertexCount,
2081             dest->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags, dest->fvf);
2082     wined3d_mutex_unlock();
2083
2084     return hr;
2085 }
2086
2087 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
2088         const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
2089 {
2090     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2091     IDirect3DVertexDeclaration9Impl *object;
2092     HRESULT hr;
2093
2094     TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2095
2096     if (!declaration)
2097     {
2098         WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2099         return D3DERR_INVALIDCALL;
2100     }
2101
2102     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2103     if (!object)
2104     {
2105         ERR("Failed to allocate vertex declaration memory.\n");
2106         return E_OUTOFMEMORY;
2107     }
2108
2109     hr = vertexdeclaration_init(object, This, elements);
2110     if (FAILED(hr))
2111     {
2112         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2113         HeapFree(GetProcessHeap(), 0, object);
2114         return hr;
2115     }
2116
2117     TRACE("Created vertex declaration %p.\n", object);
2118     *declaration = (IDirect3DVertexDeclaration9 *)object;
2119
2120     return D3D_OK;
2121 }
2122
2123 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2124         IDirect3DVertexDeclaration9 *declaration)
2125 {
2126     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2127     HRESULT hr;
2128
2129     TRACE("iface %p, declaration %p.\n", iface, declaration);
2130
2131     wined3d_mutex_lock();
2132     hr = wined3d_device_set_vertex_declaration(This->wined3d_device,
2133             declaration ? ((IDirect3DVertexDeclaration9Impl *)declaration)->wineD3DVertexDeclaration : NULL);
2134     wined3d_mutex_unlock();
2135
2136     return hr;
2137 }
2138
2139 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2140         IDirect3DVertexDeclaration9 **declaration)
2141 {
2142     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2143     struct wined3d_vertex_declaration *wined3d_declaration = NULL;
2144     HRESULT hr;
2145
2146     TRACE("iface %p, declaration %p.\n", iface, declaration);
2147
2148     if (!declaration) return D3DERR_INVALIDCALL;
2149
2150     wined3d_mutex_lock();
2151     hr = wined3d_device_get_vertex_declaration(This->wined3d_device, &wined3d_declaration);
2152     if (SUCCEEDED(hr) && wined3d_declaration)
2153     {
2154         *declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2155         IDirect3DVertexDeclaration9_AddRef(*declaration);
2156         wined3d_vertex_declaration_decref(wined3d_declaration);
2157     }
2158     else
2159     {
2160         *declaration = NULL;
2161     }
2162     wined3d_mutex_unlock();
2163
2164     TRACE("Returning %p.\n", *declaration);
2165     return hr;
2166 }
2167
2168 static IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
2169     HRESULT hr;
2170     D3DVERTEXELEMENT9* elements = NULL;
2171     IDirect3DVertexDeclaration9* pDecl = NULL;
2172     int p, low, high; /* deliberately signed */
2173     IDirect3DVertexDeclaration9  **convertedDecls = This->convertedDecls;
2174
2175     TRACE("Searching for declaration for fvf %08x... ", fvf);
2176
2177     low = 0;
2178     high = This->numConvertedDecls - 1;
2179     while(low <= high) {
2180         p = (low + high) >> 1;
2181         TRACE("%d ", p);
2182         if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
2183             TRACE("found %p\n", convertedDecls[p]);
2184             return convertedDecls[p];
2185         } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
2186             low = p + 1;
2187         } else {
2188             high = p - 1;
2189         }
2190     }
2191     TRACE("not found. Creating and inserting at position %d.\n", low);
2192
2193     hr = vdecl_convert_fvf(fvf, &elements);
2194     if (hr != S_OK) return NULL;
2195
2196     hr = IDirect3DDevice9Impl_CreateVertexDeclaration(&This->IDirect3DDevice9Ex_iface, elements,
2197             &pDecl);
2198     HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
2199     if (hr != S_OK) return NULL;
2200
2201     if(This->declArraySize == This->numConvertedDecls) {
2202         int grow = max(This->declArraySize / 2, 8);
2203         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2204                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2205         if(!convertedDecls) {
2206             /* This will destroy it */
2207             IDirect3DVertexDeclaration9_Release(pDecl);
2208             return NULL;
2209         }
2210         This->convertedDecls = convertedDecls;
2211         This->declArraySize += grow;
2212     }
2213
2214     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
2215     convertedDecls[low] = pDecl;
2216     This->numConvertedDecls++;
2217
2218     /* Will prevent the decl from being destroyed */
2219     ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
2220     IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
2221
2222     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
2223     return pDecl;
2224 }
2225
2226 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(IDirect3DDevice9Ex *iface, DWORD FVF)
2227 {
2228     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2229     IDirect3DVertexDeclaration9 *decl;
2230     HRESULT hr;
2231
2232     TRACE("iface %p, fvf %#x.\n", iface, FVF);
2233
2234     if (!FVF)
2235     {
2236         WARN("%#x is not a valid FVF\n", FVF);
2237         return D3D_OK;
2238     }
2239
2240     wined3d_mutex_lock();
2241     decl = getConvertedDecl(This, FVF);
2242     wined3d_mutex_unlock();
2243
2244     if (!decl)
2245     {
2246          /* Any situation when this should happen, except out of memory? */
2247          ERR("Failed to create a converted vertex declaration\n");
2248          return D3DERR_DRIVERINTERNALERROR;
2249     }
2250
2251     hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, decl);
2252     if (FAILED(hr)) ERR("Failed to set vertex declaration\n");
2253
2254     return hr;
2255 }
2256
2257 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(IDirect3DDevice9Ex *iface, DWORD *pFVF)
2258 {
2259     IDirect3DVertexDeclaration9 *decl;
2260     HRESULT hr;
2261
2262     TRACE("iface %p, fvf %p.\n", iface, pFVF);
2263
2264     hr = IDirect3DDevice9_GetVertexDeclaration(iface, &decl);
2265     if (FAILED(hr))
2266     {
2267         WARN("Failed to get vertex declaration, %#x\n", hr);
2268         *pFVF = 0;
2269         return hr;
2270     }
2271
2272     if (decl)
2273     {
2274         *pFVF = ((IDirect3DVertexDeclaration9Impl *)decl)->convFVF;
2275         IDirect3DVertexDeclaration9_Release(decl);
2276     }
2277     else
2278     {
2279         *pFVF = 0;
2280     }
2281
2282     TRACE("Returning FVF %#x\n", *pFVF);
2283
2284     return hr;
2285 }
2286
2287 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(IDirect3DDevice9Ex *iface,
2288         const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2289 {
2290     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2291     IDirect3DVertexShader9Impl *object;
2292     HRESULT hr;
2293
2294     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2295
2296     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2297     if (!object)
2298     {
2299         ERR("Failed to allocate vertex shader memory.\n");
2300         return E_OUTOFMEMORY;
2301     }
2302
2303     hr = vertexshader_init(object, This, byte_code);
2304     if (FAILED(hr))
2305     {
2306         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2307         HeapFree(GetProcessHeap(), 0, object);
2308         return hr;
2309     }
2310
2311     TRACE("Created vertex shader %p.\n", object);
2312     *shader = &object->IDirect3DVertexShader9_iface;
2313
2314     return D3D_OK;
2315 }
2316
2317 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShader(IDirect3DDevice9Ex *iface,
2318         IDirect3DVertexShader9 *shader)
2319 {
2320     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2321     IDirect3DVertexShader9Impl *shader_obj = unsafe_impl_from_IDirect3DVertexShader9(shader);
2322     HRESULT hr;
2323
2324     TRACE("iface %p, shader %p.\n", iface, shader);
2325
2326     wined3d_mutex_lock();
2327     hr =  wined3d_device_set_vertex_shader(This->wined3d_device,
2328             shader_obj ? shader_obj->wined3d_shader : NULL);
2329     wined3d_mutex_unlock();
2330
2331     return hr;
2332 }
2333
2334 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShader(IDirect3DDevice9Ex *iface,
2335         IDirect3DVertexShader9 **shader)
2336 {
2337     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2338     struct wined3d_shader *wined3d_shader;
2339
2340     TRACE("iface %p, shader %p.\n", iface, shader);
2341
2342     wined3d_mutex_lock();
2343     wined3d_shader = wined3d_device_get_vertex_shader(This->wined3d_device);
2344     if (wined3d_shader)
2345     {
2346         *shader = wined3d_shader_get_parent(wined3d_shader);
2347         IDirect3DVertexShader9_AddRef(*shader);
2348         wined3d_shader_decref(wined3d_shader);
2349     }
2350     else
2351     {
2352         *shader = NULL;
2353     }
2354     wined3d_mutex_unlock();
2355
2356     TRACE("Returning %p.\n", *shader);
2357
2358     return D3D_OK;
2359 }
2360
2361 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2362         UINT reg_idx, const float *data, UINT count)
2363 {
2364     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2365     HRESULT hr;
2366
2367     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2368
2369     if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2370     {
2371         WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2372              reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2373         return D3DERR_INVALIDCALL;
2374     }
2375
2376     wined3d_mutex_lock();
2377     hr = wined3d_device_set_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2378     wined3d_mutex_unlock();
2379
2380     return hr;
2381 }
2382
2383 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2384         UINT reg_idx, float *data, UINT count)
2385 {
2386     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2387     HRESULT hr;
2388
2389     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2390
2391     if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2392     {
2393         WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2394              reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2395         return D3DERR_INVALIDCALL;
2396     }
2397
2398     wined3d_mutex_lock();
2399     hr = wined3d_device_get_vs_consts_f(This->wined3d_device, reg_idx, data, count);
2400     wined3d_mutex_unlock();
2401
2402     return hr;
2403 }
2404
2405 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2406         UINT reg_idx, const int *data, UINT count)
2407 {
2408     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2409     HRESULT hr;
2410
2411     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2412
2413     wined3d_mutex_lock();
2414     hr = wined3d_device_set_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2415     wined3d_mutex_unlock();
2416
2417     return hr;
2418 }
2419
2420 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2421         UINT reg_idx, int *data, UINT count)
2422 {
2423     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2424     HRESULT hr;
2425
2426     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2427
2428     wined3d_mutex_lock();
2429     hr = wined3d_device_get_vs_consts_i(This->wined3d_device, reg_idx, data, count);
2430     wined3d_mutex_unlock();
2431
2432     return hr;
2433 }
2434
2435 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2436         UINT reg_idx, const BOOL *data, UINT count)
2437 {
2438     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2439     HRESULT hr;
2440
2441     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2442
2443     wined3d_mutex_lock();
2444     hr = wined3d_device_set_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2445     wined3d_mutex_unlock();
2446
2447     return hr;
2448 }
2449
2450 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2451         UINT reg_idx, BOOL *data, UINT count)
2452 {
2453     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2454     HRESULT hr;
2455
2456     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2457
2458     wined3d_mutex_lock();
2459     hr = wined3d_device_get_vs_consts_b(This->wined3d_device, reg_idx, data, count);
2460     wined3d_mutex_unlock();
2461
2462     return hr;
2463 }
2464
2465 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(IDirect3DDevice9Ex *iface,
2466         UINT StreamNumber, IDirect3DVertexBuffer9 *pStreamData, UINT OffsetInBytes, UINT Stride)
2467 {
2468     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2469     IDirect3DVertexBuffer9Impl *streamdata = unsafe_impl_from_IDirect3DVertexBuffer9(pStreamData);
2470     HRESULT hr;
2471
2472     TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2473             iface, StreamNumber, pStreamData, OffsetInBytes, Stride);
2474
2475     wined3d_mutex_lock();
2476     hr = wined3d_device_set_stream_source(This->wined3d_device, StreamNumber,
2477             streamdata ? streamdata->wineD3DVertexBuffer : NULL,
2478             OffsetInBytes, Stride);
2479     wined3d_mutex_unlock();
2480
2481     return hr;
2482 }
2483
2484 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(IDirect3DDevice9Ex *iface,
2485         UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT *OffsetInBytes, UINT *pStride)
2486 {
2487     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2488     struct wined3d_buffer *retStream = NULL;
2489     HRESULT hr;
2490
2491     TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2492             iface, StreamNumber, pStream, OffsetInBytes, pStride);
2493
2494     if(pStream == NULL){
2495         return D3DERR_INVALIDCALL;
2496     }
2497
2498     wined3d_mutex_lock();
2499     hr = wined3d_device_get_stream_source(This->wined3d_device, StreamNumber, &retStream, OffsetInBytes, pStride);
2500     if (SUCCEEDED(hr) && retStream)
2501     {
2502         *pStream = wined3d_buffer_get_parent(retStream);
2503         IDirect3DVertexBuffer9_AddRef(*pStream);
2504         wined3d_buffer_decref(retStream);
2505     }
2506     else
2507     {
2508         if (FAILED(hr))
2509         {
2510             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
2511         }
2512         *pStream = NULL;
2513     }
2514     wined3d_mutex_unlock();
2515
2516     return hr;
2517 }
2518
2519 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2520         UINT StreamNumber, UINT Divider)
2521 {
2522     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2523     HRESULT hr;
2524
2525     TRACE("iface %p, stream_idx %u, freq %u.\n", iface, StreamNumber, Divider);
2526
2527     wined3d_mutex_lock();
2528     hr = wined3d_device_set_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2529     wined3d_mutex_unlock();
2530
2531     return hr;
2532 }
2533
2534 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSourceFreq(IDirect3DDevice9Ex *iface,
2535         UINT StreamNumber, UINT *Divider)
2536 {
2537     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2538     HRESULT hr;
2539
2540     TRACE("iface %p, stream_idx %u, freq %p.\n", iface, StreamNumber, Divider);
2541
2542     wined3d_mutex_lock();
2543     hr = wined3d_device_get_stream_source_freq(This->wined3d_device, StreamNumber, Divider);
2544     wined3d_mutex_unlock();
2545
2546     return hr;
2547 }
2548
2549 static HRESULT WINAPI IDirect3DDevice9Impl_SetIndices(IDirect3DDevice9Ex *iface,
2550         IDirect3DIndexBuffer9 *pIndexData)
2551 {
2552     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2553     IDirect3DIndexBuffer9Impl *ib = unsafe_impl_from_IDirect3DIndexBuffer9(pIndexData);
2554     HRESULT hr;
2555
2556     TRACE("iface %p, buffer %p.\n", iface, pIndexData);
2557
2558     wined3d_mutex_lock();
2559     hr = wined3d_device_set_index_buffer(This->wined3d_device,
2560             ib ? ib->wineD3DIndexBuffer : NULL,
2561             ib ? ib->format : WINED3DFMT_UNKNOWN);
2562     wined3d_mutex_unlock();
2563
2564     return hr;
2565 }
2566
2567 static HRESULT WINAPI IDirect3DDevice9Impl_GetIndices(IDirect3DDevice9Ex *iface,
2568         IDirect3DIndexBuffer9 **ppIndexData)
2569 {
2570     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2571     struct wined3d_buffer *retIndexData = NULL;
2572     HRESULT hr;
2573
2574     TRACE("iface %p, buffer %p.\n", iface, ppIndexData);
2575
2576     if(ppIndexData == NULL){
2577         return D3DERR_INVALIDCALL;
2578     }
2579
2580     wined3d_mutex_lock();
2581     hr = wined3d_device_get_index_buffer(This->wined3d_device, &retIndexData);
2582     if (SUCCEEDED(hr) && retIndexData)
2583     {
2584         *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2585         IDirect3DIndexBuffer9_AddRef(*ppIndexData);
2586         wined3d_buffer_decref(retIndexData);
2587     }
2588     else
2589     {
2590         if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2591         *ppIndexData = NULL;
2592     }
2593     wined3d_mutex_unlock();
2594
2595     return hr;
2596 }
2597
2598 static HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(IDirect3DDevice9Ex *iface,
2599         const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2600 {
2601     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2602     IDirect3DPixelShader9Impl *object;
2603     HRESULT hr;
2604
2605     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2606
2607     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2608     if (!object)
2609     {
2610         FIXME("Failed to allocate pixel shader memory.\n");
2611         return E_OUTOFMEMORY;
2612     }
2613
2614     hr = pixelshader_init(object, This, byte_code);
2615     if (FAILED(hr))
2616     {
2617         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2618         HeapFree(GetProcessHeap(), 0, object);
2619         return hr;
2620     }
2621
2622     TRACE("Created pixel shader %p.\n", object);
2623     *shader = &object->IDirect3DPixelShader9_iface;
2624
2625     return D3D_OK;
2626 }
2627
2628 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShader(IDirect3DDevice9Ex *iface,
2629         IDirect3DPixelShader9 *shader)
2630 {
2631     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2632     IDirect3DPixelShader9Impl *shader_obj = unsafe_impl_from_IDirect3DPixelShader9(shader);
2633     HRESULT hr;
2634
2635     TRACE("iface %p, shader %p.\n", iface, shader);
2636
2637     wined3d_mutex_lock();
2638     hr = wined3d_device_set_pixel_shader(This->wined3d_device,
2639             shader_obj ? shader_obj->wined3d_shader : NULL);
2640     wined3d_mutex_unlock();
2641
2642     return hr;
2643 }
2644
2645 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShader(IDirect3DDevice9Ex *iface,
2646         IDirect3DPixelShader9 **shader)
2647 {
2648     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2649     struct wined3d_shader *wined3d_shader;
2650
2651     TRACE("iface %p, shader %p.\n", iface, shader);
2652
2653     if (!shader) return D3DERR_INVALIDCALL;
2654
2655     wined3d_mutex_lock();
2656     wined3d_shader = wined3d_device_get_pixel_shader(This->wined3d_device);
2657     if (wined3d_shader)
2658     {
2659         *shader = wined3d_shader_get_parent(wined3d_shader);
2660         IDirect3DPixelShader9_AddRef(*shader);
2661         wined3d_shader_decref(wined3d_shader);
2662     }
2663     else
2664     {
2665         *shader = NULL;
2666     }
2667     wined3d_mutex_unlock();
2668
2669     TRACE("Returning %p.\n", *shader);
2670
2671     return D3D_OK;
2672 }
2673
2674 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2675         UINT reg_idx, const float *data, UINT count)
2676 {
2677     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2678     HRESULT hr;
2679
2680     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2681
2682     wined3d_mutex_lock();
2683     hr = wined3d_device_set_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2684     wined3d_mutex_unlock();
2685
2686     return hr;
2687 }
2688
2689 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2690         UINT reg_idx, float *data, UINT count)
2691 {
2692     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2693     HRESULT hr;
2694
2695     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2696
2697     wined3d_mutex_lock();
2698     hr = wined3d_device_get_ps_consts_f(This->wined3d_device, reg_idx, data, count);
2699     wined3d_mutex_unlock();
2700
2701     return hr;
2702 }
2703
2704 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2705         UINT reg_idx, const int *data, UINT count)
2706 {
2707     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2708     HRESULT hr;
2709
2710     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2711
2712     wined3d_mutex_lock();
2713     hr = wined3d_device_set_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2714     wined3d_mutex_unlock();
2715
2716     return hr;
2717 }
2718
2719 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2720         UINT reg_idx, int *data, UINT count)
2721 {
2722     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2723     HRESULT hr;
2724
2725     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2726
2727     wined3d_mutex_lock();
2728     hr = wined3d_device_get_ps_consts_i(This->wined3d_device, reg_idx, data, count);
2729     wined3d_mutex_unlock();
2730
2731     return hr;
2732 }
2733
2734 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2735         UINT reg_idx, const BOOL *data, UINT count)
2736 {
2737     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2738     HRESULT hr;
2739
2740     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2741
2742     wined3d_mutex_lock();
2743     hr = wined3d_device_set_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2744     wined3d_mutex_unlock();
2745
2746     return hr;
2747 }
2748
2749 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2750         UINT reg_idx, BOOL *data, UINT count)
2751 {
2752     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2753     HRESULT hr;
2754
2755     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2756
2757     wined3d_mutex_lock();
2758     hr = wined3d_device_get_ps_consts_b(This->wined3d_device, reg_idx, data, count);
2759     wined3d_mutex_unlock();
2760
2761     return hr;
2762 }
2763
2764 static HRESULT WINAPI IDirect3DDevice9Impl_DrawRectPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2765         const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2766 {
2767     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2768     HRESULT hr;
2769
2770     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2771             iface, Handle, pNumSegs, pRectPatchInfo);
2772
2773     wined3d_mutex_lock();
2774     hr = wined3d_device_draw_rect_patch(This->wined3d_device, Handle,
2775             pNumSegs, (const struct wined3d_rect_patch_info *)pRectPatchInfo);
2776     wined3d_mutex_unlock();
2777
2778     return hr;
2779 }
2780
2781 static HRESULT WINAPI IDirect3DDevice9Impl_DrawTriPatch(IDirect3DDevice9Ex *iface, UINT Handle,
2782         const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2783 {
2784     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2785     HRESULT hr;
2786
2787     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2788             iface, Handle, pNumSegs, pTriPatchInfo);
2789
2790     wined3d_mutex_lock();
2791     hr = wined3d_device_draw_tri_patch(This->wined3d_device, Handle,
2792             pNumSegs, (const struct wined3d_tri_patch_info *)pTriPatchInfo);
2793     wined3d_mutex_unlock();
2794
2795     return hr;
2796 }
2797
2798 static HRESULT WINAPI IDirect3DDevice9Impl_DeletePatch(IDirect3DDevice9Ex *iface, UINT Handle)
2799 {
2800     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2801     HRESULT hr;
2802
2803     TRACE("iface %p, handle %#x.\n", iface, Handle);
2804
2805     wined3d_mutex_lock();
2806     hr = wined3d_device_delete_patch(This->wined3d_device, Handle);
2807     wined3d_mutex_unlock();
2808
2809     return hr;
2810 }
2811
2812 static HRESULT WINAPI IDirect3DDevice9Impl_CreateQuery(IDirect3DDevice9Ex *iface, D3DQUERYTYPE type,
2813         IDirect3DQuery9 **query)
2814 {
2815     IDirect3DDevice9Impl *This = impl_from_IDirect3DDevice9Ex(iface);
2816     IDirect3DQuery9Impl *object;
2817     HRESULT hr;
2818
2819     TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2820
2821     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2822     if (!object)
2823     {
2824         ERR("Failed to allocate query memory.\n");
2825         return E_OUTOFMEMORY;
2826     }
2827
2828     hr = query_init(object, This, type);
2829     if (FAILED(hr))
2830     {
2831         WARN("Failed to initialize query, hr %#x.\n", hr);
2832         HeapFree(GetProcessHeap(), 0, object);
2833         return hr;
2834     }
2835
2836     TRACE("Created query %p.\n", object);
2837     if (query) *query = &object->IDirect3DQuery9_iface;
2838     else IDirect3DQuery9_Release(&object->IDirect3DQuery9_iface);
2839
2840     return D3D_OK;
2841 }
2842
2843 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2844         UINT width, UINT height, float *rows, float *columns)
2845 {
2846     FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2847             iface, width, height, rows, columns);
2848
2849     return E_NOTIMPL;
2850 }
2851
2852 static HRESULT WINAPI IDirect3DDevice9ExImpl_ComposeRects(IDirect3DDevice9Ex *iface,
2853         IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2854         UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2855 {
2856     FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2857             "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2858             iface, src_surface, dst_surface, src_descs, rect_count,
2859             dst_descs, operation, offset_x, offset_y);
2860
2861     return E_NOTIMPL;
2862 }
2863
2864 static HRESULT WINAPI IDirect3DDevice9ExImpl_PresentEx(IDirect3DDevice9Ex *iface,
2865         const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2866         const RGNDATA *dirty_region, DWORD flags)
2867 {
2868     FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2869             iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2870
2871     return E_NOTIMPL;
2872 }
2873
2874 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2875 {
2876     FIXME("iface %p, priority %p stub!\n", iface, priority);
2877
2878     return E_NOTIMPL;
2879 }
2880
2881 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2882 {
2883     FIXME("iface %p, priority %d stub!\n", iface, priority);
2884
2885     return E_NOTIMPL;
2886 }
2887
2888 static HRESULT WINAPI IDirect3DDevice9ExImpl_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2889 {
2890     FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2891
2892     return E_NOTIMPL;
2893 }
2894
2895 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2896         IDirect3DResource9 **resources, UINT32 resource_count)
2897 {
2898     FIXME("iface %p, resources %p, resource_count %u stub!\n",
2899             iface, resources, resource_count);
2900
2901     return E_NOTIMPL;
2902 }
2903
2904 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2905 {
2906     FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2907
2908     return E_NOTIMPL;
2909 }
2910
2911 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2912 {
2913     FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2914
2915     *max_latency = 2;
2916
2917     return E_NOTIMPL;
2918 }
2919
2920 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2921 {
2922     static int i;
2923
2924     TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
2925
2926     if (!i++)
2927         FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2928
2929     return D3D_OK;
2930 }
2931
2932 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2933         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2934         BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2935 {
2936     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2937             "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2938             iface, width, height, format, multisample_type, multisample_quality,
2939             lockable, surface, shared_handle, usage);
2940
2941     if (shared_handle)
2942         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2943
2944     return E_NOTIMPL;
2945 }
2946
2947 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2948         UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2949         HANDLE *shared_handle, DWORD usage)
2950 {
2951     FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2952             iface, width, height, format, pool, surface, shared_handle, usage);
2953
2954     return E_NOTIMPL;
2955 }
2956
2957 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2958         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2959         BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2960 {
2961     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2962             "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2963             iface, width, height, format, multisample_type, multisample_quality,
2964             discard, surface, shared_handle, usage);
2965
2966     if (shared_handle)
2967         FIXME("Resource sharing not implemented, *shared_handle %p.\n", *shared_handle);
2968
2969     return E_NOTIMPL;
2970 }
2971
2972 static HRESULT WINAPI IDirect3DDevice9ExImpl_ResetEx(IDirect3DDevice9Ex *iface,
2973         D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2974 {
2975     FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2976
2977     return E_NOTIMPL;
2978 }
2979
2980 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2981         UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2982 {
2983     FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2984
2985     return E_NOTIMPL;
2986 }
2987
2988 static const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
2989 {
2990     /* IUnknown */
2991     IDirect3DDevice9Impl_QueryInterface,
2992     IDirect3DDevice9Impl_AddRef,
2993     IDirect3DDevice9Impl_Release,
2994     /* IDirect3DDevice9 */
2995     IDirect3DDevice9Impl_TestCooperativeLevel,
2996     IDirect3DDevice9Impl_GetAvailableTextureMem,
2997     IDirect3DDevice9Impl_EvictManagedResources,
2998     IDirect3DDevice9Impl_GetDirect3D,
2999     IDirect3DDevice9Impl_GetDeviceCaps,
3000     IDirect3DDevice9Impl_GetDisplayMode,
3001     IDirect3DDevice9Impl_GetCreationParameters,
3002     IDirect3DDevice9Impl_SetCursorProperties,
3003     IDirect3DDevice9Impl_SetCursorPosition,
3004     IDirect3DDevice9Impl_ShowCursor,
3005     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
3006     IDirect3DDevice9Impl_GetSwapChain,
3007     IDirect3DDevice9Impl_GetNumberOfSwapChains,
3008     IDirect3DDevice9Impl_Reset,
3009     IDirect3DDevice9Impl_Present,
3010     IDirect3DDevice9Impl_GetBackBuffer,
3011     IDirect3DDevice9Impl_GetRasterStatus,
3012     IDirect3DDevice9Impl_SetDialogBoxMode,
3013     IDirect3DDevice9Impl_SetGammaRamp,
3014     IDirect3DDevice9Impl_GetGammaRamp,
3015     IDirect3DDevice9Impl_CreateTexture,
3016     IDirect3DDevice9Impl_CreateVolumeTexture,
3017     IDirect3DDevice9Impl_CreateCubeTexture,
3018     IDirect3DDevice9Impl_CreateVertexBuffer,
3019     IDirect3DDevice9Impl_CreateIndexBuffer,
3020     IDirect3DDevice9Impl_CreateRenderTarget,
3021     IDirect3DDevice9Impl_CreateDepthStencilSurface,
3022     IDirect3DDevice9Impl_UpdateSurface,
3023     IDirect3DDevice9Impl_UpdateTexture,
3024     IDirect3DDevice9Impl_GetRenderTargetData,
3025     IDirect3DDevice9Impl_GetFrontBufferData,
3026     IDirect3DDevice9Impl_StretchRect,
3027     IDirect3DDevice9Impl_ColorFill,
3028     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
3029     IDirect3DDevice9Impl_SetRenderTarget,
3030     IDirect3DDevice9Impl_GetRenderTarget,
3031     IDirect3DDevice9Impl_SetDepthStencilSurface,
3032     IDirect3DDevice9Impl_GetDepthStencilSurface,
3033     IDirect3DDevice9Impl_BeginScene,
3034     IDirect3DDevice9Impl_EndScene,
3035     IDirect3DDevice9Impl_Clear,
3036     IDirect3DDevice9Impl_SetTransform,
3037     IDirect3DDevice9Impl_GetTransform,
3038     IDirect3DDevice9Impl_MultiplyTransform,
3039     IDirect3DDevice9Impl_SetViewport,
3040     IDirect3DDevice9Impl_GetViewport,
3041     IDirect3DDevice9Impl_SetMaterial,
3042     IDirect3DDevice9Impl_GetMaterial,
3043     IDirect3DDevice9Impl_SetLight,
3044     IDirect3DDevice9Impl_GetLight,
3045     IDirect3DDevice9Impl_LightEnable,
3046     IDirect3DDevice9Impl_GetLightEnable,
3047     IDirect3DDevice9Impl_SetClipPlane,
3048     IDirect3DDevice9Impl_GetClipPlane,
3049     IDirect3DDevice9Impl_SetRenderState,
3050     IDirect3DDevice9Impl_GetRenderState,
3051     IDirect3DDevice9Impl_CreateStateBlock,
3052     IDirect3DDevice9Impl_BeginStateBlock,
3053     IDirect3DDevice9Impl_EndStateBlock,
3054     IDirect3DDevice9Impl_SetClipStatus,
3055     IDirect3DDevice9Impl_GetClipStatus,
3056     IDirect3DDevice9Impl_GetTexture,
3057     IDirect3DDevice9Impl_SetTexture,
3058     IDirect3DDevice9Impl_GetTextureStageState,
3059     IDirect3DDevice9Impl_SetTextureStageState,
3060     IDirect3DDevice9Impl_GetSamplerState,
3061     IDirect3DDevice9Impl_SetSamplerState,
3062     IDirect3DDevice9Impl_ValidateDevice,
3063     IDirect3DDevice9Impl_SetPaletteEntries,
3064     IDirect3DDevice9Impl_GetPaletteEntries,
3065     IDirect3DDevice9Impl_SetCurrentTexturePalette,
3066     IDirect3DDevice9Impl_GetCurrentTexturePalette,
3067     IDirect3DDevice9Impl_SetScissorRect,
3068     IDirect3DDevice9Impl_GetScissorRect,
3069     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
3070     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
3071     IDirect3DDevice9Impl_SetNPatchMode,
3072     IDirect3DDevice9Impl_GetNPatchMode,
3073     IDirect3DDevice9Impl_DrawPrimitive,
3074     IDirect3DDevice9Impl_DrawIndexedPrimitive,
3075     IDirect3DDevice9Impl_DrawPrimitiveUP,
3076     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
3077     IDirect3DDevice9Impl_ProcessVertices,
3078     IDirect3DDevice9Impl_CreateVertexDeclaration,
3079     IDirect3DDevice9Impl_SetVertexDeclaration,
3080     IDirect3DDevice9Impl_GetVertexDeclaration,
3081     IDirect3DDevice9Impl_SetFVF,
3082     IDirect3DDevice9Impl_GetFVF,
3083     IDirect3DDevice9Impl_CreateVertexShader,
3084     IDirect3DDevice9Impl_SetVertexShader,
3085     IDirect3DDevice9Impl_GetVertexShader,
3086     IDirect3DDevice9Impl_SetVertexShaderConstantF,
3087     IDirect3DDevice9Impl_GetVertexShaderConstantF,
3088     IDirect3DDevice9Impl_SetVertexShaderConstantI,
3089     IDirect3DDevice9Impl_GetVertexShaderConstantI,
3090     IDirect3DDevice9Impl_SetVertexShaderConstantB,
3091     IDirect3DDevice9Impl_GetVertexShaderConstantB,
3092     IDirect3DDevice9Impl_SetStreamSource,
3093     IDirect3DDevice9Impl_GetStreamSource,
3094     IDirect3DDevice9Impl_SetStreamSourceFreq,
3095     IDirect3DDevice9Impl_GetStreamSourceFreq,
3096     IDirect3DDevice9Impl_SetIndices,
3097     IDirect3DDevice9Impl_GetIndices,
3098     IDirect3DDevice9Impl_CreatePixelShader,
3099     IDirect3DDevice9Impl_SetPixelShader,
3100     IDirect3DDevice9Impl_GetPixelShader,
3101     IDirect3DDevice9Impl_SetPixelShaderConstantF,
3102     IDirect3DDevice9Impl_GetPixelShaderConstantF,
3103     IDirect3DDevice9Impl_SetPixelShaderConstantI,
3104     IDirect3DDevice9Impl_GetPixelShaderConstantI,
3105     IDirect3DDevice9Impl_SetPixelShaderConstantB,
3106     IDirect3DDevice9Impl_GetPixelShaderConstantB,
3107     IDirect3DDevice9Impl_DrawRectPatch,
3108     IDirect3DDevice9Impl_DrawTriPatch,
3109     IDirect3DDevice9Impl_DeletePatch,
3110     IDirect3DDevice9Impl_CreateQuery,
3111     /* IDirect3DDevice9Ex */
3112     IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
3113     IDirect3DDevice9ExImpl_ComposeRects,
3114     IDirect3DDevice9ExImpl_PresentEx,
3115     IDirect3DDevice9ExImpl_GetGPUThreadPriority,
3116     IDirect3DDevice9ExImpl_SetGPUThreadPriority,
3117     IDirect3DDevice9ExImpl_WaitForVBlank,
3118     IDirect3DDevice9ExImpl_CheckResourceResidency,
3119     IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
3120     IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
3121     IDirect3DDevice9ExImpl_CheckDeviceState,
3122     IDirect3DDevice9ExImpl_CreateRenderTargetEx,
3123     IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
3124     IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
3125     IDirect3DDevice9ExImpl_ResetEx,
3126     IDirect3DDevice9ExImpl_GetDisplayModeEx
3127 };
3128
3129 static inline struct IDirect3DDevice9Impl *device_from_device_parent(struct wined3d_device_parent *device_parent)
3130 {
3131     return CONTAINING_RECORD(device_parent, struct IDirect3DDevice9Impl, device_parent);
3132 }
3133
3134 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3135         struct wined3d_device *device)
3136 {
3137     TRACE("device_parent %p, device %p.\n", device_parent, device);
3138 }
3139
3140 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3141 {
3142     TRACE("device_parent %p.\n", device_parent);
3143 }
3144
3145 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
3146         void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
3147         enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
3148 {
3149     struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3150     IDirect3DSurface9Impl *d3d_surface;
3151     BOOL lockable = TRUE;
3152     HRESULT hr;
3153
3154     TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
3155             "\tpool %#x, level %u, face %u, surface %p.\n",
3156             device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
3157
3158     if (pool == WINED3D_POOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
3159         lockable = FALSE;
3160
3161     hr = IDirect3DDevice9Impl_CreateSurface(device, width, height,
3162             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
3163             (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
3164     if (FAILED(hr))
3165     {
3166         WARN("Failed to create surface, hr %#x.\n", hr);
3167         return hr;
3168     }
3169
3170     *surface = d3d_surface->wined3d_surface;
3171     wined3d_surface_incref(*surface);
3172
3173     d3d_surface->container = container_parent;
3174     IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
3175     d3d_surface->parentDevice = NULL;
3176
3177     IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3178     d3d_surface->forwardReference = container_parent;
3179
3180     return hr;
3181 }
3182
3183 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
3184         void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
3185         enum wined3d_multisample_type multisample_type, DWORD multisample_quality, BOOL lockable,
3186         struct wined3d_surface **surface)
3187 {
3188     struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3189     IDirect3DSurface9Impl *d3d_surface;
3190     HRESULT hr;
3191
3192     TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3193             "\tmultisample_quality %u, lockable %u, surface %p.\n",
3194             device_parent, container_parent, width, height, format, multisample_type,
3195             multisample_quality, lockable, surface);
3196
3197     hr = IDirect3DDevice9Impl_CreateRenderTarget(&device->IDirect3DDevice9Ex_iface, width, height,
3198             d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
3199             (IDirect3DSurface9 **)&d3d_surface, NULL);
3200     if (FAILED(hr))
3201     {
3202         WARN("Failed to create rendertarget, hr %#x.\n", hr);
3203         return hr;
3204     }
3205
3206     *surface = d3d_surface->wined3d_surface;
3207     wined3d_surface_incref(*surface);
3208
3209     d3d_surface->container = container_parent;
3210     /* Implicit surfaces are created with an refcount of 0 */
3211     IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3212
3213     return hr;
3214 }
3215
3216 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
3217         UINT width, UINT height, enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
3218         DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
3219 {
3220     struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3221     IDirect3DSurface9Impl *d3d_surface;
3222     HRESULT hr;
3223
3224     TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3225             "\tmultisample_quality %u, discard %u, surface %p.\n",
3226             device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
3227
3228     hr = IDirect3DDevice9Impl_CreateDepthStencilSurface(&device->IDirect3DDevice9Ex_iface, width,
3229             height, d3dformat_from_wined3dformat(format), multisample_type, multisample_quality,
3230             discard, (IDirect3DSurface9 **)&d3d_surface, NULL);
3231     if (FAILED(hr))
3232     {
3233         WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
3234         return hr;
3235     }
3236
3237     *surface = d3d_surface->wined3d_surface;
3238     wined3d_surface_incref(*surface);
3239     d3d_surface->container = (IUnknown *)&device->IDirect3DDevice9Ex_iface;
3240     /* Implicit surfaces are created with an refcount of 0 */
3241     IDirect3DSurface9_Release(&d3d_surface->IDirect3DSurface9_iface);
3242
3243     return hr;
3244 }
3245
3246 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
3247         void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
3248         enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
3249 {
3250     struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3251     IDirect3DVolume9Impl *object;
3252     HRESULT hr;
3253
3254     TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
3255             "format %#x, pool %#x, usage %#x, volume %p\n",
3256             device_parent, container_parent, width, height, depth,
3257             format, pool, usage, volume);
3258
3259     /* Allocate the storage for the device */
3260     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3261     if (!object)
3262     {
3263         FIXME("Allocation of memory failed\n");
3264         *volume = NULL;
3265         return D3DERR_OUTOFVIDEOMEMORY;
3266     }
3267
3268     hr = volume_init(object, device, width, height, depth, usage, format, pool);
3269     if (FAILED(hr))
3270     {
3271         WARN("Failed to initialize volume, hr %#x.\n", hr);
3272         HeapFree(GetProcessHeap(), 0, object);
3273         return hr;
3274     }
3275
3276     *volume = object->wined3d_volume;
3277     wined3d_volume_incref(*volume);
3278     IDirect3DVolume9_Release(&object->IDirect3DVolume9_iface);
3279
3280     object->container = container_parent;
3281     object->forwardReference = container_parent;
3282
3283     TRACE("Created volume %p.\n", object);
3284
3285     return hr;
3286 }
3287
3288 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3289         struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3290 {
3291     struct IDirect3DDevice9Impl *device = device_from_device_parent(device_parent);
3292     D3DPRESENT_PARAMETERS local_parameters;
3293     IDirect3DSwapChain9 *d3d_swapchain;
3294     HRESULT hr;
3295
3296     TRACE("device_parent %p, desc %p, swapchain %p\n", device_parent, desc, swapchain);
3297
3298     /* Copy the presentation parameters */
3299     local_parameters.BackBufferWidth = desc->backbuffer_width;
3300     local_parameters.BackBufferHeight = desc->backbuffer_height;
3301     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(desc->backbuffer_format);
3302     local_parameters.BackBufferCount = desc->backbuffer_count;
3303     local_parameters.MultiSampleType = desc->multisample_type;
3304     local_parameters.MultiSampleQuality = desc->multisample_quality;
3305     local_parameters.SwapEffect = desc->swap_effect;
3306     local_parameters.hDeviceWindow = desc->device_window;
3307     local_parameters.Windowed = desc->windowed;
3308     local_parameters.EnableAutoDepthStencil = desc->enable_auto_depth_stencil;
3309     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc->auto_depth_stencil_format);
3310     local_parameters.Flags = desc->flags;
3311     local_parameters.FullScreen_RefreshRateInHz = desc->refresh_rate;
3312     local_parameters.PresentationInterval = desc->swap_interval;
3313
3314     hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain(&device->IDirect3DDevice9Ex_iface,
3315             &local_parameters, &d3d_swapchain);
3316     if (FAILED(hr))
3317     {
3318         WARN("Failed to create swapchain, hr %#x.\n", hr);
3319         *swapchain = NULL;
3320         return hr;
3321     }
3322
3323     *swapchain = ((IDirect3DSwapChain9Impl *)d3d_swapchain)->wined3d_swapchain;
3324     wined3d_swapchain_incref(*swapchain);
3325     IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)d3d_swapchain);
3326
3327     /* Copy back the presentation parameters */
3328     desc->backbuffer_width = local_parameters.BackBufferWidth;
3329     desc->backbuffer_height = local_parameters.BackBufferHeight;
3330     desc->backbuffer_format = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3331     desc->backbuffer_count = local_parameters.BackBufferCount;
3332     desc->multisample_type = local_parameters.MultiSampleType;
3333     desc->multisample_quality = local_parameters.MultiSampleQuality;
3334     desc->swap_effect = local_parameters.SwapEffect;
3335     desc->device_window = local_parameters.hDeviceWindow;
3336     desc->windowed = local_parameters.Windowed;
3337     desc->enable_auto_depth_stencil = local_parameters.EnableAutoDepthStencil;
3338     desc->auto_depth_stencil_format = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3339     desc->flags = local_parameters.Flags;
3340     desc->refresh_rate = local_parameters.FullScreen_RefreshRateInHz;
3341     desc->swap_interval = local_parameters.PresentationInterval;
3342
3343     return hr;
3344 }
3345
3346 static const struct wined3d_device_parent_ops d3d9_wined3d_device_parent_ops =
3347 {
3348     device_parent_wined3d_device_created,
3349     device_parent_mode_changed,
3350     device_parent_create_surface,
3351     device_parent_create_rendertarget,
3352     device_parent_create_depth_stencil,
3353     device_parent_create_volume,
3354     device_parent_create_swapchain,
3355 };
3356
3357 static void setup_fpu(void)
3358 {
3359 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3360     WORD cw;
3361     __asm__ volatile ("fnstcw %0" : "=m" (cw));
3362     cw = (cw & ~0xf3f) | 0x3f;
3363     __asm__ volatile ("fldcw %0" : : "m" (cw));
3364 #elif defined(__i386__) && defined(_MSC_VER)
3365     WORD cw;
3366     __asm fnstcw cw;
3367     cw = (cw & ~0xf3f) | 0x3f;
3368     __asm fldcw cw;
3369 #else
3370     FIXME("FPU setup not implemented for this platform.\n");
3371 #endif
3372 }
3373
3374 HRESULT device_init(IDirect3DDevice9Impl *device, IDirect3D9Impl *parent, struct wined3d *wined3d,
3375         UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
3376         D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3377 {
3378     struct wined3d_swapchain_desc *swapchain_desc;
3379     UINT i, count = 1;
3380     HRESULT hr;
3381
3382     if (mode)
3383         FIXME("Ignoring display mode.\n");
3384
3385     device->IDirect3DDevice9Ex_iface.lpVtbl = &Direct3DDevice9_Vtbl;
3386     device->device_parent.ops = &d3d9_wined3d_device_parent_ops;
3387     device->ref = 1;
3388
3389     if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3390
3391     wined3d_mutex_lock();
3392     hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3393             &device->device_parent, &device->wined3d_device);
3394     if (FAILED(hr))
3395     {
3396         WARN("Failed to create wined3d device, hr %#x.\n", hr);
3397         wined3d_mutex_unlock();
3398         return hr;
3399     }
3400
3401     if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3402     {
3403         WINED3DCAPS caps;
3404
3405         wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3406         count = caps.NumberOfAdaptersInGroup;
3407     }
3408
3409     if (flags & D3DCREATE_MULTITHREADED)
3410         wined3d_device_set_multithreaded(device->wined3d_device);
3411
3412     if (!parameters->Windowed)
3413     {
3414         if (!focus_window)
3415             focus_window = parameters->hDeviceWindow;
3416         if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3417         {
3418             ERR("Failed to acquire focus window, hr %#x.\n", hr);
3419             wined3d_device_decref(device->wined3d_device);
3420             wined3d_mutex_unlock();
3421             return hr;
3422         }
3423
3424         for (i = 0; i < count; ++i)
3425         {
3426             HWND device_window = parameters[i].hDeviceWindow;
3427
3428             if (!device_window) device_window = focus_window;
3429             wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3430                     parameters[i].BackBufferWidth,
3431                     parameters[i].BackBufferHeight);
3432         }
3433     }
3434
3435     swapchain_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*swapchain_desc) * count);
3436     if (!swapchain_desc)
3437     {
3438         ERR("Failed to allocate wined3d parameters.\n");
3439         wined3d_device_decref(device->wined3d_device);
3440         wined3d_mutex_unlock();
3441         return E_OUTOFMEMORY;
3442     }
3443
3444     for (i = 0; i < count; ++i)
3445     {
3446         swapchain_desc[i].backbuffer_width = parameters[i].BackBufferWidth;
3447         swapchain_desc[i].backbuffer_height = parameters[i].BackBufferHeight;
3448         swapchain_desc[i].backbuffer_format = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
3449         swapchain_desc[i].backbuffer_count = parameters[i].BackBufferCount;
3450         swapchain_desc[i].multisample_type = parameters[i].MultiSampleType;
3451         swapchain_desc[i].multisample_quality = parameters[i].MultiSampleQuality;
3452         swapchain_desc[i].swap_effect = parameters[i].SwapEffect;
3453         swapchain_desc[i].device_window = parameters[i].hDeviceWindow;
3454         swapchain_desc[i].windowed = parameters[i].Windowed;
3455         swapchain_desc[i].enable_auto_depth_stencil = parameters[i].EnableAutoDepthStencil;
3456         swapchain_desc[i].auto_depth_stencil_format =
3457                 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
3458         swapchain_desc[i].flags = parameters[i].Flags;
3459         swapchain_desc[i].refresh_rate = parameters[i].FullScreen_RefreshRateInHz;
3460         swapchain_desc[i].swap_interval = parameters[i].PresentationInterval;
3461         swapchain_desc[i].auto_restore_display_mode = TRUE;
3462     }
3463
3464     hr = wined3d_device_init_3d(device->wined3d_device, swapchain_desc);
3465     if (FAILED(hr))
3466     {
3467         WARN("Failed to initialize 3D, hr %#x.\n", hr);
3468         wined3d_device_release_focus_window(device->wined3d_device);
3469         HeapFree(GetProcessHeap(), 0, swapchain_desc);
3470         wined3d_device_decref(device->wined3d_device);
3471         wined3d_mutex_unlock();
3472         return hr;
3473     }
3474
3475     wined3d_mutex_unlock();
3476
3477     for (i = 0; i < count; ++i)
3478     {
3479         parameters[i].BackBufferWidth = swapchain_desc[i].backbuffer_width;
3480         parameters[i].BackBufferHeight = swapchain_desc[i].backbuffer_height;
3481         parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc[i].backbuffer_format);
3482         parameters[i].BackBufferCount = swapchain_desc[i].backbuffer_count;
3483         parameters[i].MultiSampleType = swapchain_desc[i].multisample_type;
3484         parameters[i].MultiSampleQuality = swapchain_desc[i].multisample_quality;
3485         parameters[i].SwapEffect = swapchain_desc[i].swap_effect;
3486         parameters[i].hDeviceWindow = swapchain_desc[i].device_window;
3487         parameters[i].Windowed = swapchain_desc[i].windowed;
3488         parameters[i].EnableAutoDepthStencil = swapchain_desc[i].enable_auto_depth_stencil;
3489         parameters[i].AutoDepthStencilFormat =
3490                 d3dformat_from_wined3dformat(swapchain_desc[i].auto_depth_stencil_format);
3491         parameters[i].Flags = swapchain_desc[i].flags;
3492         parameters[i].FullScreen_RefreshRateInHz = swapchain_desc[i].refresh_rate;
3493         parameters[i].PresentationInterval = swapchain_desc[i].swap_interval;
3494     }
3495     HeapFree(GetProcessHeap(), 0, swapchain_desc);
3496
3497     /* Initialize the converted declaration array. This creates a valid pointer
3498      * and when adding decls HeapReAlloc() can be used without further checking. */
3499     device->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
3500     if (!device->convertedDecls)
3501     {
3502         ERR("Failed to allocate FVF vertex declaration map memory.\n");
3503         wined3d_mutex_lock();
3504         wined3d_device_uninit_3d(device->wined3d_device);
3505         wined3d_device_release_focus_window(device->wined3d_device);
3506         wined3d_device_decref(device->wined3d_device);
3507         wined3d_mutex_unlock();
3508         return E_OUTOFMEMORY;
3509     }
3510
3511     IDirect3D9Ex_AddRef(&parent->IDirect3D9Ex_iface);
3512     device->d3d_parent = parent;
3513
3514     return D3D_OK;
3515 }