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