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