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