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