wined3d: Implicit depth/stencil surfaces don't have a container.
[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     hr = IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, pRect, &c);
1048
1049     wined3d_mutex_unlock();
1050
1051     return hr;
1052 }
1053
1054 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
1055     HRESULT hr;
1056
1057     TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1058             iface, Width, Height, Format, Pool, ppSurface, pSharedHandle);
1059
1060     if(Pool == D3DPOOL_MANAGED ){
1061         FIXME("Attempting to create a managed offscreen plain surface\n");
1062         return D3DERR_INVALIDCALL;
1063     }
1064         /*
1065         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
1066         but then...
1067         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
1068         Why, their always lockable?
1069         should I change the usage to dynamic?
1070         */
1071     hr = IDirect3DDevice9Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE /* Discard */,
1072             0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */, (WINED3DPOOL)Pool, D3DMULTISAMPLE_NONE,
1073             0 /* MultisampleQuality */);
1074
1075     return hr;
1076 }
1077
1078 /* TODO: move to wineD3D */
1079 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9EX iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
1080     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1081     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
1082     HRESULT hr;
1083
1084     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, pRenderTarget);
1085
1086     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1087     {
1088         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1089         return D3DERR_INVALIDCALL;
1090     }
1091
1092     wined3d_mutex_lock();
1093     hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? pSurface->wineD3DSurface : NULL, TRUE);
1094     wined3d_mutex_unlock();
1095
1096     return hr;
1097 }
1098
1099 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTarget(LPDIRECT3DDEVICE9EX iface, DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget) {
1100     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1101     IWineD3DSurface *pRenderTarget;
1102     HRESULT hr;
1103
1104     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, ppRenderTarget);
1105
1106     if (ppRenderTarget == NULL) {
1107         return D3DERR_INVALIDCALL;
1108     }
1109
1110     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1111     {
1112         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1113         return D3DERR_INVALIDCALL;
1114     }
1115
1116     wined3d_mutex_lock();
1117
1118     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
1119
1120     if (FAILED(hr))
1121     {
1122         FIXME("Call to IWineD3DDevice_GetRenderTarget failed, hr %#x\n", hr);
1123     }
1124     else if (!pRenderTarget)
1125     {
1126         *ppRenderTarget = NULL;
1127     }
1128     else
1129     {
1130         IWineD3DSurface_GetParent(pRenderTarget, (IUnknown **)ppRenderTarget);
1131         IWineD3DSurface_Release(pRenderTarget);
1132     }
1133
1134     wined3d_mutex_unlock();
1135
1136     return hr;
1137 }
1138
1139 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pZStencilSurface) {
1140     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1141     IDirect3DSurface9Impl *pSurface;
1142     HRESULT hr;
1143
1144     TRACE("iface %p, depth_stencil %p.\n", iface, pZStencilSurface);
1145
1146     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
1147
1148     wined3d_mutex_lock();
1149     hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL==pSurface ? NULL : pSurface->wineD3DSurface);
1150     wined3d_mutex_unlock();
1151
1152     return hr;
1153 }
1154
1155 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9 **ppZStencilSurface) {
1156     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1157     HRESULT hr = D3D_OK;
1158     IWineD3DSurface *pZStencilSurface;
1159
1160     TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1161
1162     if(ppZStencilSurface == NULL){
1163         return D3DERR_INVALIDCALL;
1164     }
1165
1166     wined3d_mutex_lock();
1167     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
1168     if (hr == WINED3D_OK) {
1169         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
1170         IWineD3DSurface_Release(pZStencilSurface);
1171     } else {
1172         if (hr != WINED3DERR_NOTFOUND)
1173                 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1174         *ppZStencilSurface = NULL;
1175     }
1176     wined3d_mutex_unlock();
1177
1178     return hr;
1179 }
1180
1181 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9EX iface) {
1182     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1183     HRESULT hr;
1184
1185     TRACE("iface %p.\n", iface);
1186
1187     wined3d_mutex_lock();
1188     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1189     wined3d_mutex_unlock();
1190
1191     return hr;
1192 }
1193
1194 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9EX iface) {
1195     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1196     HRESULT hr;
1197
1198     TRACE("iface %p.\n", iface);
1199
1200     wined3d_mutex_lock();
1201     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1202     wined3d_mutex_unlock();
1203
1204     return hr;
1205 }
1206
1207 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9EX iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
1208     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1209     HRESULT hr;
1210
1211     TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1212             iface, Count, pRects, Flags, Color, Z, Stencil);
1213
1214     /* Note: D3DRECT is compatible with WINED3DRECT */
1215     wined3d_mutex_lock();
1216     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (const RECT *)pRects, Flags, Color, Z, Stencil);
1217     wined3d_mutex_unlock();
1218
1219     return hr;
1220 }
1221
1222 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
1223     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1224     HRESULT hr;
1225
1226     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1227
1228     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1229     wined3d_mutex_lock();
1230     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1231     wined3d_mutex_unlock();
1232
1233     return hr;
1234 }
1235
1236 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
1237     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1238     HRESULT hr;
1239
1240     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1241
1242     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1243     wined3d_mutex_lock();
1244     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1245     wined3d_mutex_unlock();
1246
1247     return hr;
1248 }
1249
1250 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
1251     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1252     HRESULT hr;
1253
1254     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1255
1256     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1257     wined3d_mutex_lock();
1258     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1259     wined3d_mutex_unlock();
1260
1261     return hr;
1262 }
1263
1264 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9EX iface, CONST D3DVIEWPORT9* pViewport) {
1265     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1266     HRESULT hr;
1267
1268     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1269
1270     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
1271     wined3d_mutex_lock();
1272     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1273     wined3d_mutex_unlock();
1274
1275     return hr;
1276 }
1277
1278 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9EX iface, D3DVIEWPORT9* pViewport) {
1279     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1280     HRESULT hr;
1281
1282     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1283
1284     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
1285     wined3d_mutex_lock();
1286     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1287     wined3d_mutex_unlock();
1288
1289     return hr;
1290 }
1291
1292 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9EX iface, CONST D3DMATERIAL9* pMaterial) {
1293     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1294     HRESULT hr;
1295
1296     TRACE("iface %p, material %p.\n", iface, pMaterial);
1297
1298     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
1299     wined3d_mutex_lock();
1300     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1301     wined3d_mutex_unlock();
1302
1303     return hr;
1304 }
1305
1306 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9EX iface, D3DMATERIAL9* pMaterial) {
1307     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1308     HRESULT hr;
1309
1310     TRACE("iface %p, material %p.\n", iface, pMaterial);
1311
1312     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
1313     wined3d_mutex_lock();
1314     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1315     wined3d_mutex_unlock();
1316
1317     return hr;
1318 }
1319
1320 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST D3DLIGHT9* pLight) {
1321     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1322     HRESULT hr;
1323
1324     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1325
1326     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
1327     wined3d_mutex_lock();
1328     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1329     wined3d_mutex_unlock();
1330
1331     return hr;
1332 }
1333
1334 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, D3DLIGHT9* pLight) {
1335     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1336     HRESULT hr;
1337
1338     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1339
1340     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
1341     wined3d_mutex_lock();
1342     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1343     wined3d_mutex_unlock();
1344
1345     return hr;
1346 }
1347
1348 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL Enable) {
1349     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1350     HRESULT hr;
1351
1352     TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1353
1354     wined3d_mutex_lock();
1355     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1356     wined3d_mutex_unlock();
1357
1358     return hr;
1359 }
1360
1361 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL* pEnable) {
1362     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1363     HRESULT hr;
1364
1365     TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1366
1367     wined3d_mutex_lock();
1368     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1369     wined3d_mutex_unlock();
1370
1371     return hr;
1372 }
1373
1374 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST float* pPlane) {
1375     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1376     HRESULT hr;
1377
1378     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1379
1380     wined3d_mutex_lock();
1381     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1382     wined3d_mutex_unlock();
1383
1384     return hr;
1385 }
1386
1387 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, float* pPlane) {
1388     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1389     HRESULT hr;
1390
1391     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1392
1393     wined3d_mutex_lock();
1394     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1395     wined3d_mutex_unlock();
1396
1397     return hr;
1398 }
1399
1400 static HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD Value) {
1401     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1402     HRESULT hr;
1403
1404     TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1405
1406     wined3d_mutex_lock();
1407     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1408     wined3d_mutex_unlock();
1409
1410     return hr;
1411 }
1412
1413 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
1414     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1415     HRESULT hr;
1416
1417     TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1418
1419     wined3d_mutex_lock();
1420     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1421     wined3d_mutex_unlock();
1422
1423     return hr;
1424 }
1425
1426 static HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(IDirect3DDevice9Ex *iface,
1427         D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1428 {
1429     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1430     IDirect3DStateBlock9Impl *object;
1431     HRESULT hr;
1432
1433     TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1434
1435     if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1436     {
1437         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1438         return D3DERR_INVALIDCALL;
1439     }
1440
1441     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1442     if (!object)
1443     {
1444         ERR("Failed to allocate stateblock memory.\n");
1445         return E_OUTOFMEMORY;
1446     }
1447
1448     hr = stateblock_init(object, This, type, NULL);
1449     if (FAILED(hr))
1450     {
1451         WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1452         HeapFree(GetProcessHeap(), 0, object);
1453         return hr;
1454     }
1455
1456     TRACE("Created stateblock %p.\n", object);
1457     *stateblock = (IDirect3DStateBlock9 *)object;
1458
1459     return D3D_OK;
1460 }
1461
1462 static HRESULT WINAPI IDirect3DDevice9Impl_BeginStateBlock(IDirect3DDevice9Ex *iface)
1463 {
1464     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1465     HRESULT hr;
1466
1467     TRACE("iface %p.\n", iface);
1468
1469     wined3d_mutex_lock();
1470     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1471     wined3d_mutex_unlock();
1472
1473     return hr;
1474 }
1475
1476 static HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1477 {
1478     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1479     IWineD3DStateBlock *wined3d_stateblock;
1480     IDirect3DStateBlock9Impl *object;
1481     HRESULT hr;
1482
1483     TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1484
1485     wined3d_mutex_lock();
1486     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice, &wined3d_stateblock);
1487     wined3d_mutex_unlock();
1488     if (FAILED(hr))
1489     {
1490        WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1491        return hr;
1492     }
1493
1494     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1495     if (!object)
1496     {
1497         ERR("Failed to allocate stateblock memory.\n");
1498         IWineD3DStateBlock_Release(wined3d_stateblock);
1499         return E_OUTOFMEMORY;
1500     }
1501
1502     hr = stateblock_init(object, This, 0, wined3d_stateblock);
1503     if (FAILED(hr))
1504     {
1505         WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1506         IWineD3DStateBlock_Release(wined3d_stateblock);
1507         HeapFree(GetProcessHeap(), 0, object);
1508         return hr;
1509     }
1510
1511     TRACE("Created stateblock %p.\n", object);
1512     *stateblock = (IDirect3DStateBlock9 *)object;
1513
1514     return D3D_OK;
1515 }
1516
1517 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9EX iface, CONST D3DCLIPSTATUS9* pClipStatus) {
1518     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1519     HRESULT hr;
1520
1521     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1522
1523     wined3d_mutex_lock();
1524     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1525     wined3d_mutex_unlock();
1526
1527     return hr;
1528 }
1529
1530 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9EX iface, D3DCLIPSTATUS9* pClipStatus) {
1531     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1532     HRESULT hr;
1533
1534     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1535
1536     wined3d_mutex_lock();
1537     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1538     wined3d_mutex_unlock();
1539
1540     return hr;
1541 }
1542
1543 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTexture(LPDIRECT3DDEVICE9EX iface, DWORD Stage, IDirect3DBaseTexture9 **ppTexture) {
1544     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1545     IWineD3DBaseTexture *retTexture = NULL;
1546     HRESULT rc = D3D_OK;
1547
1548     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1549
1550     if(ppTexture == NULL){
1551         return D3DERR_INVALIDCALL;
1552     }
1553
1554     wined3d_mutex_lock();
1555     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1556     if (SUCCEEDED(rc) && NULL != retTexture) {
1557         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1558         IWineD3DBaseTexture_Release(retTexture);
1559     } else {
1560         if(FAILED(rc)) {
1561             WARN("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1562         }
1563         *ppTexture = NULL;
1564     }
1565     wined3d_mutex_unlock();
1566
1567     return rc;
1568 }
1569
1570 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9EX iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
1571     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1572     HRESULT hr;
1573
1574     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1575
1576     wined3d_mutex_lock();
1577     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1578                                    pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture);
1579     wined3d_mutex_unlock();
1580
1581     return hr;
1582 }
1583
1584 static const WINED3DTEXTURESTAGESTATETYPE tss_lookup[] =
1585 {
1586     WINED3DTSS_FORCE_DWORD,             /*  0, unused */
1587     WINED3DTSS_COLOROP,                 /*  1, D3DTSS_COLOROP */
1588     WINED3DTSS_COLORARG1,               /*  2, D3DTSS_COLORARG1 */
1589     WINED3DTSS_COLORARG2,               /*  3, D3DTSS_COLORARG2 */
1590     WINED3DTSS_ALPHAOP,                 /*  4, D3DTSS_ALPHAOP */
1591     WINED3DTSS_ALPHAARG1,               /*  5, D3DTSS_ALPHAARG1 */
1592     WINED3DTSS_ALPHAARG2,               /*  6, D3DTSS_ALPHAARG2 */
1593     WINED3DTSS_BUMPENVMAT00,            /*  7, D3DTSS_BUMPENVMAT00 */
1594     WINED3DTSS_BUMPENVMAT01,            /*  8, D3DTSS_BUMPENVMAT01 */
1595     WINED3DTSS_BUMPENVMAT10,            /*  9, D3DTSS_BUMPENVMAT10 */
1596     WINED3DTSS_BUMPENVMAT11,            /* 10, D3DTSS_BUMPENVMAT11 */
1597     WINED3DTSS_TEXCOORDINDEX,           /* 11, D3DTSS_TEXCOORDINDEX */
1598     WINED3DTSS_FORCE_DWORD,             /* 12, unused */
1599     WINED3DTSS_FORCE_DWORD,             /* 13, unused */
1600     WINED3DTSS_FORCE_DWORD,             /* 14, unused */
1601     WINED3DTSS_FORCE_DWORD,             /* 15, unused */
1602     WINED3DTSS_FORCE_DWORD,             /* 16, unused */
1603     WINED3DTSS_FORCE_DWORD,             /* 17, unused */
1604     WINED3DTSS_FORCE_DWORD,             /* 18, unused */
1605     WINED3DTSS_FORCE_DWORD,             /* 19, unused */
1606     WINED3DTSS_FORCE_DWORD,             /* 20, unused */
1607     WINED3DTSS_FORCE_DWORD,             /* 21, unused */
1608     WINED3DTSS_BUMPENVLSCALE,           /* 22, D3DTSS_BUMPENVLSCALE */
1609     WINED3DTSS_BUMPENVLOFFSET,          /* 23, D3DTSS_BUMPENVLOFFSET */
1610     WINED3DTSS_TEXTURETRANSFORMFLAGS,   /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1611     WINED3DTSS_FORCE_DWORD,             /* 25, unused */
1612     WINED3DTSS_COLORARG0,               /* 26, D3DTSS_COLORARG0 */
1613     WINED3DTSS_ALPHAARG0,               /* 27, D3DTSS_ALPHAARG0 */
1614     WINED3DTSS_RESULTARG,               /* 28, D3DTSS_RESULTARG */
1615     WINED3DTSS_FORCE_DWORD,             /* 29, unused */
1616     WINED3DTSS_FORCE_DWORD,             /* 30, unused */
1617     WINED3DTSS_FORCE_DWORD,             /* 31, unused */
1618     WINED3DTSS_CONSTANT,                /* 32, D3DTSS_CONSTANT */
1619 };
1620
1621 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD* pValue) {
1622     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1623     HRESULT hr;
1624
1625     TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1626
1627     wined3d_mutex_lock();
1628     hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], pValue);
1629     wined3d_mutex_unlock();
1630
1631     return hr;
1632 }
1633
1634 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1635     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1636     HRESULT hr;
1637
1638     TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1639
1640     wined3d_mutex_lock();
1641     hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], Value);
1642     wined3d_mutex_unlock();
1643
1644     return hr;
1645 }
1646
1647 static HRESULT WINAPI IDirect3DDevice9Impl_GetSamplerState(IDirect3DDevice9Ex *iface, DWORD Sampler,
1648         D3DSAMPLERSTATETYPE Type, DWORD *pValue)
1649 {
1650     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1651     HRESULT hr;
1652
1653     TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, Sampler, Type, pValue);
1654
1655     wined3d_mutex_lock();
1656     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
1657     wined3d_mutex_unlock();
1658
1659     return hr;
1660 }
1661
1662 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9EX iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
1663     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1664     HRESULT hr;
1665
1666     TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, Sampler, Type, Value);
1667
1668     wined3d_mutex_lock();
1669     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);
1670     wined3d_mutex_unlock();
1671
1672     return hr;
1673 }
1674
1675 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9EX iface, DWORD* pNumPasses) {
1676     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1677     HRESULT hr;
1678
1679     TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1680
1681     wined3d_mutex_lock();
1682     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1683     wined3d_mutex_unlock();
1684
1685     return hr;
1686 }
1687
1688 static HRESULT WINAPI IDirect3DDevice9Impl_SetPaletteEntries(IDirect3DDevice9Ex *iface, UINT PaletteNumber,
1689         const PALETTEENTRY *pEntries)
1690 {
1691     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1692     HRESULT hr;
1693
1694     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1695
1696     wined3d_mutex_lock();
1697     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1698     wined3d_mutex_unlock();
1699
1700     return hr;
1701 }
1702
1703 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1704     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1705     HRESULT hr;
1706
1707     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1708
1709     wined3d_mutex_lock();
1710     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1711     wined3d_mutex_unlock();
1712
1713     return hr;
1714 }
1715
1716 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber) {
1717     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1718     HRESULT hr;
1719
1720     TRACE("iface %p, palette_idx %u.\n", iface, PaletteNumber);
1721
1722     wined3d_mutex_lock();
1723     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1724     wined3d_mutex_unlock();
1725
1726     return hr;
1727 }
1728
1729 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT* PaletteNumber) {
1730     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1731     HRESULT hr;
1732
1733     TRACE("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1734
1735     wined3d_mutex_lock();
1736     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1737     wined3d_mutex_unlock();
1738
1739     return hr;
1740 }
1741
1742 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9EX iface, CONST RECT* pRect) {
1743     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1744     HRESULT hr;
1745
1746     TRACE("iface %p, rect %p.\n", iface, pRect);
1747
1748     wined3d_mutex_lock();
1749     hr = IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);
1750     wined3d_mutex_unlock();
1751
1752     return hr;
1753 }
1754
1755 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9EX iface, RECT* pRect) {
1756     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1757     HRESULT hr;
1758
1759     TRACE("iface %p, rect %p.\n", iface, pRect);
1760
1761     wined3d_mutex_lock();
1762     hr = IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);
1763     wined3d_mutex_unlock();
1764
1765     return hr;
1766 }
1767
1768 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface, BOOL bSoftware) {
1769     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1770     HRESULT hr;
1771
1772     TRACE("iface %p, software %#x.\n", iface, bSoftware);
1773
1774     wined3d_mutex_lock();
1775     hr = IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
1776     wined3d_mutex_unlock();
1777
1778     return hr;
1779 }
1780
1781 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface) {
1782     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1783     BOOL ret;
1784
1785     TRACE("iface %p.\n", iface);
1786
1787     wined3d_mutex_lock();
1788     ret = IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
1789     wined3d_mutex_unlock();
1790
1791     return ret;
1792 }
1793
1794 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9EX iface, float nSegments) {
1795     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1796     HRESULT hr;
1797
1798     TRACE("iface %p, segment_count %.8e.\n", iface, nSegments);
1799
1800     wined3d_mutex_lock();
1801     hr = IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
1802     wined3d_mutex_unlock();
1803
1804     return hr;
1805 }
1806
1807 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9EX iface) {
1808     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1809     float ret;
1810
1811     TRACE("iface %p.\n", iface);
1812
1813     wined3d_mutex_lock();
1814     ret = IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
1815     wined3d_mutex_unlock();
1816
1817     return ret;
1818 }
1819
1820 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(IDirect3DDevice9Ex *iface, D3DPRIMITIVETYPE PrimitiveType,
1821         UINT StartVertex, UINT PrimitiveCount)
1822 {
1823     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1824     HRESULT hr;
1825
1826     TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1827             iface, PrimitiveType, StartVertex, PrimitiveCount);
1828
1829     wined3d_mutex_lock();
1830     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1831     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1832             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1833     wined3d_mutex_unlock();
1834
1835     return hr;
1836 }
1837
1838 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType,
1839                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
1840     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1841     HRESULT hr;
1842
1843     TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
1844             "vertex_count %u, start_idx %u, primitive_count %u.\n",
1845             iface, PrimitiveType, BaseVertexIndex, MinVertexIndex,
1846             NumVertices, startIndex, primCount);
1847
1848     wined3d_mutex_lock();
1849     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
1850     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1851     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1852             vertex_count_from_primitive_count(PrimitiveType, primCount));
1853     wined3d_mutex_unlock();
1854
1855     return hr;
1856 }
1857
1858 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitiveUP(IDirect3DDevice9Ex *iface, D3DPRIMITIVETYPE PrimitiveType,
1859         UINT PrimitiveCount, const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
1860 {
1861     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1862     HRESULT hr;
1863
1864     TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1865             iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1866
1867     wined3d_mutex_lock();
1868     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1869     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1870             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1871             pVertexStreamZeroData, VertexStreamZeroStride);
1872     wined3d_mutex_unlock();
1873
1874     return hr;
1875 }
1876
1877 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
1878                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
1879                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1880     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1881     HRESULT hr;
1882
1883     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1884             "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1885             iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1886             pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1887
1888     wined3d_mutex_lock();
1889     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1890     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1891             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1892             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1893     wined3d_mutex_unlock();
1894
1895     return hr;
1896 }
1897
1898 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9EX iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
1899     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1900     IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
1901     HRESULT hr;
1902     IDirect3DVertexBuffer9Impl *dest = (IDirect3DVertexBuffer9Impl *) pDestBuffer;
1903
1904     TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
1905             iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags);
1906
1907     wined3d_mutex_lock();
1908     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags, dest->fvf);
1909     wined3d_mutex_unlock();
1910
1911     return hr;
1912 }
1913
1914 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
1915         const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
1916 {
1917     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1918     IDirect3DVertexDeclaration9Impl *object;
1919     HRESULT hr;
1920
1921     TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
1922
1923     if (!declaration)
1924     {
1925         WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
1926         return D3DERR_INVALIDCALL;
1927     }
1928
1929     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1930     if (!object)
1931     {
1932         ERR("Failed to allocate vertex declaration memory.\n");
1933         return E_OUTOFMEMORY;
1934     }
1935
1936     hr = vertexdeclaration_init(object, This, elements);
1937     if (FAILED(hr))
1938     {
1939         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
1940         HeapFree(GetProcessHeap(), 0, object);
1941         return hr;
1942     }
1943
1944     TRACE("Created vertex declaration %p.\n", object);
1945     *declaration = (IDirect3DVertexDeclaration9 *)object;
1946
1947     return D3D_OK;
1948 }
1949
1950 static IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
1951     HRESULT hr;
1952     D3DVERTEXELEMENT9* elements = NULL;
1953     IDirect3DVertexDeclaration9* pDecl = NULL;
1954     int p, low, high; /* deliberately signed */
1955     IDirect3DVertexDeclaration9  **convertedDecls = This->convertedDecls;
1956
1957     TRACE("Searching for declaration for fvf %08x... ", fvf);
1958
1959     low = 0;
1960     high = This->numConvertedDecls - 1;
1961     while(low <= high) {
1962         p = (low + high) >> 1;
1963         TRACE("%d ", p);
1964         if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
1965             TRACE("found %p\n", convertedDecls[p]);
1966             return convertedDecls[p];
1967         } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
1968             low = p + 1;
1969         } else {
1970             high = p - 1;
1971         }
1972     }
1973     TRACE("not found. Creating and inserting at position %d.\n", low);
1974
1975     hr = vdecl_convert_fvf(fvf, &elements);
1976     if (hr != S_OK) return NULL;
1977
1978     hr = IDirect3DDevice9Impl_CreateVertexDeclaration((IDirect3DDevice9Ex *) This, elements, &pDecl);
1979     HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
1980     if (hr != S_OK) return NULL;
1981
1982     if(This->declArraySize == This->numConvertedDecls) {
1983         int grow = max(This->declArraySize / 2, 8);
1984         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1985                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1986         if(!convertedDecls) {
1987             /* This will destroy it */
1988             IDirect3DVertexDeclaration9_Release(pDecl);
1989             return NULL;
1990         }
1991         This->convertedDecls = convertedDecls;
1992         This->declArraySize += grow;
1993     }
1994
1995     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
1996     convertedDecls[low] = pDecl;
1997     This->numConvertedDecls++;
1998
1999     /* Will prevent the decl from being destroyed */
2000     ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
2001     IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
2002
2003     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
2004     return pDecl;
2005 }
2006
2007 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9EX iface, DWORD FVF) {
2008     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2009     IDirect3DVertexDeclaration9 *decl;
2010     HRESULT hr;
2011
2012     TRACE("iface %p, fvf %#x.\n", iface, FVF);
2013
2014     if (!FVF)
2015     {
2016         WARN("%#x is not a valid FVF\n", FVF);
2017         return D3D_OK;
2018     }
2019
2020     wined3d_mutex_lock();
2021     decl = getConvertedDecl(This, FVF);
2022     wined3d_mutex_unlock();
2023
2024     if (!decl)
2025     {
2026          /* Any situation when this should happen, except out of memory? */
2027          ERR("Failed to create a converted vertex declaration\n");
2028          return D3DERR_DRIVERINTERNALERROR;
2029     }
2030
2031     hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, decl);
2032     if (FAILED(hr)) ERR("Failed to set vertex declaration\n");
2033
2034     return hr;
2035 }
2036
2037 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(IDirect3DDevice9Ex *iface, DWORD *pFVF)
2038 {
2039     IDirect3DVertexDeclaration9 *decl;
2040     HRESULT hr;
2041
2042     TRACE("iface %p, fvf %p.\n", iface, pFVF);
2043
2044     hr = IDirect3DDevice9_GetVertexDeclaration(iface, &decl);
2045     if (FAILED(hr))
2046     {
2047         WARN("Failed to get vertex declaration, %#x\n", hr);
2048         *pFVF = 0;
2049         return hr;
2050     }
2051
2052     if (decl)
2053     {
2054         *pFVF = ((IDirect3DVertexDeclaration9Impl *)decl)->convFVF;
2055         IDirect3DVertexDeclaration9_Release(decl);
2056     }
2057     else
2058     {
2059         *pFVF = 0;
2060     }
2061
2062     TRACE("Returning FVF %#x\n", *pFVF);
2063
2064     return hr;
2065 }
2066
2067 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(IDirect3DDevice9Ex *iface,
2068         const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2069 {
2070     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2071     IDirect3DVertexShader9Impl *object;
2072     HRESULT hr;
2073
2074     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2075
2076     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2077     if (!object)
2078     {
2079         ERR("Failed to allocate vertex shader memory.\n");
2080         return E_OUTOFMEMORY;
2081     }
2082
2083     hr = vertexshader_init(object, This, byte_code);
2084     if (FAILED(hr))
2085     {
2086         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2087         HeapFree(GetProcessHeap(), 0, object);
2088         return hr;
2089     }
2090
2091     TRACE("Created vertex shader %p.\n", object);
2092     *shader = (IDirect3DVertexShader9 *)object;
2093
2094     return D3D_OK;
2095 }
2096
2097 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
2098     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2099     HRESULT hr;
2100
2101     TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2102             iface, StreamNumber, pStreamData, OffsetInBytes, Stride);
2103
2104     wined3d_mutex_lock();
2105     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2106             pStreamData ? ((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer : NULL,
2107             OffsetInBytes, Stride);
2108     wined3d_mutex_unlock();
2109
2110     return hr;
2111 }
2112
2113 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride) {
2114     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2115     IWineD3DBuffer *retStream = NULL;
2116     HRESULT rc = D3D_OK;
2117
2118     TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2119             iface, StreamNumber, pStream, OffsetInBytes, pStride);
2120
2121     if(pStream == NULL){
2122         return D3DERR_INVALIDCALL;
2123     }
2124
2125     wined3d_mutex_lock();
2126     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, OffsetInBytes, pStride);
2127     if (rc == D3D_OK  && NULL != retStream) {
2128         IWineD3DBuffer_GetParent(retStream, (IUnknown **)pStream);
2129         IWineD3DBuffer_Release(retStream);
2130     }else{
2131         if (rc != D3D_OK){
2132             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
2133         }
2134         *pStream = NULL;
2135     }
2136     wined3d_mutex_unlock();
2137
2138     return rc;
2139 }
2140
2141 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT StreamNumber,
2142         UINT Divider)
2143 {
2144     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2145     HRESULT hr;
2146
2147     TRACE("iface %p, stream_idx %u, freq %u.\n", iface, StreamNumber, Divider);
2148
2149     wined3d_mutex_lock();
2150     hr = IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
2151     wined3d_mutex_unlock();
2152
2153     return hr;
2154 }
2155
2156 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, UINT* Divider) {
2157     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2158     HRESULT hr;
2159
2160     TRACE("iface %p, stream_idx %u, freq %p.\n", iface, StreamNumber, Divider);
2161
2162     wined3d_mutex_lock();
2163     hr = IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
2164     wined3d_mutex_unlock();
2165
2166     return hr;
2167 }
2168
2169 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9EX iface, IDirect3DIndexBuffer9* pIndexData) {
2170     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2171     HRESULT hr;
2172     IDirect3DIndexBuffer9Impl *ib = (IDirect3DIndexBuffer9Impl *) pIndexData;
2173
2174     TRACE("iface %p, buffer %p.\n", iface, pIndexData);
2175
2176     wined3d_mutex_lock();
2177     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2178             ib ? ib->wineD3DIndexBuffer : NULL,
2179             ib ? ib->format : WINED3DFMT_UNKNOWN);
2180     wined3d_mutex_unlock();
2181
2182     return hr;
2183 }
2184
2185 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetIndices(LPDIRECT3DDEVICE9EX iface, IDirect3DIndexBuffer9 **ppIndexData) {
2186     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2187     IWineD3DBuffer *retIndexData = NULL;
2188     HRESULT rc = D3D_OK;
2189
2190     TRACE("iface %p, buffer %p.\n", iface, ppIndexData);
2191
2192     if(ppIndexData == NULL){
2193         return D3DERR_INVALIDCALL;
2194     }
2195
2196     wined3d_mutex_lock();
2197     rc = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2198     if (SUCCEEDED(rc) && retIndexData) {
2199         IWineD3DBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
2200         IWineD3DBuffer_Release(retIndexData);
2201     } else {
2202         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
2203         *ppIndexData = NULL;
2204     }
2205     wined3d_mutex_unlock();
2206
2207     return rc;
2208 }
2209
2210 static HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(IDirect3DDevice9Ex *iface,
2211         const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2212 {
2213     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2214     IDirect3DPixelShader9Impl *object;
2215     HRESULT hr;
2216
2217     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2218
2219     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2220     if (!object)
2221     {
2222         FIXME("Failed to allocate pixel shader memory.\n");
2223         return E_OUTOFMEMORY;
2224     }
2225
2226     hr = pixelshader_init(object, This, byte_code);
2227     if (FAILED(hr))
2228     {
2229         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2230         HeapFree(GetProcessHeap(), 0, object);
2231         return hr;
2232     }
2233
2234     TRACE("Created pixel shader %p.\n", object);
2235     *shader = (IDirect3DPixelShader9 *)object;
2236
2237     return D3D_OK;
2238 }
2239
2240 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2241     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2242     HRESULT hr;
2243
2244     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2245             iface, Handle, pNumSegs, pRectPatchInfo);
2246
2247     wined3d_mutex_lock();
2248     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2249     wined3d_mutex_unlock();
2250
2251     return hr;
2252 }
2253
2254 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2255     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2256     HRESULT hr;
2257
2258     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2259             iface, Handle, pNumSegs, pTriPatchInfo);
2260
2261     wined3d_mutex_lock();
2262     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2263     wined3d_mutex_unlock();
2264
2265     return hr;
2266 }
2267
2268 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9EX iface, UINT Handle) {
2269     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2270     HRESULT hr;
2271
2272     TRACE("iface %p, handle %#x.\n", iface, Handle);
2273
2274     wined3d_mutex_lock();
2275     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2276     wined3d_mutex_unlock();
2277
2278     return hr;
2279 }
2280
2281 static HRESULT WINAPI IDirect3DDevice9Impl_CreateQuery(IDirect3DDevice9Ex *iface,
2282         D3DQUERYTYPE type, IDirect3DQuery9 **query)
2283 {
2284     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2285     IDirect3DQuery9Impl *object;
2286     HRESULT hr;
2287
2288     TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2289
2290     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2291     if (!object)
2292     {
2293         ERR("Failed to allocate query memory.\n");
2294         return E_OUTOFMEMORY;
2295     }
2296
2297     hr = query_init(object, This, type);
2298     if (FAILED(hr))
2299     {
2300         WARN("Failed to initialize query, hr %#x.\n", hr);
2301         HeapFree(GetProcessHeap(), 0, object);
2302         return hr;
2303     }
2304
2305     TRACE("Created query %p.\n", object);
2306     if (query) *query = (IDirect3DQuery9 *)object;
2307     else IDirect3DQuery9_Release((IDirect3DQuery9 *)object);
2308
2309     return D3D_OK;
2310 }
2311
2312 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2313         UINT width, UINT height, float *rows, float *columns)
2314 {
2315     FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2316             iface, width, height, rows, columns);
2317
2318     return WINED3DERR_INVALIDCALL;
2319 }
2320
2321 static HRESULT WINAPI IDirect3DDevice9ExImpl_ComposeRects(IDirect3DDevice9Ex *iface,
2322         IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2323         UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2324 {
2325     FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2326             "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2327             iface, src_surface, dst_surface, src_descs, rect_count,
2328             dst_descs, operation, offset_x, offset_y);
2329
2330     return WINED3DERR_INVALIDCALL;
2331 }
2332
2333 static HRESULT WINAPI IDirect3DDevice9ExImpl_PresentEx(IDirect3DDevice9Ex *iface,
2334         const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2335         const RGNDATA *dirty_region, DWORD flags)
2336 {
2337     FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2338             iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2339
2340     return WINED3DERR_INVALIDCALL;
2341 }
2342
2343 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2344 {
2345     FIXME("iface %p, priority %p stub!\n", iface, priority);
2346
2347     return WINED3DERR_INVALIDCALL;
2348 }
2349
2350 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2351 {
2352     FIXME("iface %p, priority %d stub!\n", iface, priority);
2353
2354     return WINED3DERR_INVALIDCALL;
2355 }
2356
2357 static HRESULT WINAPI IDirect3DDevice9ExImpl_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2358 {
2359     FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2360
2361     return WINED3DERR_INVALIDCALL;
2362 }
2363
2364 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2365         IDirect3DResource9 **resources, UINT32 resource_count)
2366 {
2367     FIXME("iface %p, resources %p, resource_count %u stub!\n",
2368             iface, resources, resource_count);
2369
2370     return WINED3DERR_INVALIDCALL;
2371 }
2372
2373 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2374 {
2375     FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2376
2377     return WINED3DERR_INVALIDCALL;
2378 }
2379
2380 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2381 {
2382     FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2383
2384     *max_latency = 2;
2385
2386     return WINED3DERR_INVALIDCALL;
2387 }
2388
2389 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2390 {
2391     FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2392
2393     return WINED3DERR_INVALIDCALL;
2394 }
2395
2396 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2397         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2398         BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2399 {
2400     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2401             "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2402             iface, width, height, format, multisample_type, multisample_quality,
2403             lockable, surface, shared_handle, usage);
2404
2405     return WINED3DERR_INVALIDCALL;
2406 }
2407
2408 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2409         UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2410         HANDLE *shared_handle, DWORD usage)
2411 {
2412     FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2413             iface, width, height, format, pool, surface, shared_handle, usage);
2414
2415     return WINED3DERR_INVALIDCALL;
2416 }
2417
2418 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2419         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2420         BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2421 {
2422     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2423             "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2424             iface, width, height, format, multisample_type, multisample_quality,
2425             discard, surface, shared_handle, usage);
2426
2427     return WINED3DERR_INVALIDCALL;
2428 }
2429
2430 static HRESULT WINAPI IDirect3DDevice9ExImpl_ResetEx(IDirect3DDevice9Ex *iface,
2431         D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2432 {
2433     FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2434
2435     return WINED3DERR_INVALIDCALL;
2436 }
2437
2438 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2439         UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2440 {
2441     FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2442
2443     return WINED3DERR_INVALIDCALL;
2444 }
2445
2446 static const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
2447 {
2448     /* IUnknown */
2449     IDirect3DDevice9Impl_QueryInterface,
2450     IDirect3DDevice9Impl_AddRef,
2451     IDirect3DDevice9Impl_Release,
2452     /* IDirect3DDevice9 */
2453     IDirect3DDevice9Impl_TestCooperativeLevel,
2454     IDirect3DDevice9Impl_GetAvailableTextureMem,
2455     IDirect3DDevice9Impl_EvictManagedResources,
2456     IDirect3DDevice9Impl_GetDirect3D,
2457     IDirect3DDevice9Impl_GetDeviceCaps,
2458     IDirect3DDevice9Impl_GetDisplayMode,
2459     IDirect3DDevice9Impl_GetCreationParameters,
2460     IDirect3DDevice9Impl_SetCursorProperties,
2461     IDirect3DDevice9Impl_SetCursorPosition,
2462     IDirect3DDevice9Impl_ShowCursor,
2463     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
2464     IDirect3DDevice9Impl_GetSwapChain,
2465     IDirect3DDevice9Impl_GetNumberOfSwapChains,
2466     IDirect3DDevice9Impl_Reset,
2467     IDirect3DDevice9Impl_Present,
2468     IDirect3DDevice9Impl_GetBackBuffer,
2469     IDirect3DDevice9Impl_GetRasterStatus,
2470     IDirect3DDevice9Impl_SetDialogBoxMode,
2471     IDirect3DDevice9Impl_SetGammaRamp,
2472     IDirect3DDevice9Impl_GetGammaRamp,
2473     IDirect3DDevice9Impl_CreateTexture,
2474     IDirect3DDevice9Impl_CreateVolumeTexture,
2475     IDirect3DDevice9Impl_CreateCubeTexture,
2476     IDirect3DDevice9Impl_CreateVertexBuffer,
2477     IDirect3DDevice9Impl_CreateIndexBuffer,
2478     IDirect3DDevice9Impl_CreateRenderTarget,
2479     IDirect3DDevice9Impl_CreateDepthStencilSurface,
2480     IDirect3DDevice9Impl_UpdateSurface,
2481     IDirect3DDevice9Impl_UpdateTexture,
2482     IDirect3DDevice9Impl_GetRenderTargetData,
2483     IDirect3DDevice9Impl_GetFrontBufferData,
2484     IDirect3DDevice9Impl_StretchRect,
2485     IDirect3DDevice9Impl_ColorFill,
2486     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
2487     IDirect3DDevice9Impl_SetRenderTarget,
2488     IDirect3DDevice9Impl_GetRenderTarget,
2489     IDirect3DDevice9Impl_SetDepthStencilSurface,
2490     IDirect3DDevice9Impl_GetDepthStencilSurface,
2491     IDirect3DDevice9Impl_BeginScene,
2492     IDirect3DDevice9Impl_EndScene,
2493     IDirect3DDevice9Impl_Clear,
2494     IDirect3DDevice9Impl_SetTransform,
2495     IDirect3DDevice9Impl_GetTransform,
2496     IDirect3DDevice9Impl_MultiplyTransform,
2497     IDirect3DDevice9Impl_SetViewport,
2498     IDirect3DDevice9Impl_GetViewport,
2499     IDirect3DDevice9Impl_SetMaterial,
2500     IDirect3DDevice9Impl_GetMaterial,
2501     IDirect3DDevice9Impl_SetLight,
2502     IDirect3DDevice9Impl_GetLight,
2503     IDirect3DDevice9Impl_LightEnable,
2504     IDirect3DDevice9Impl_GetLightEnable,
2505     IDirect3DDevice9Impl_SetClipPlane,
2506     IDirect3DDevice9Impl_GetClipPlane,
2507     IDirect3DDevice9Impl_SetRenderState,
2508     IDirect3DDevice9Impl_GetRenderState,
2509     IDirect3DDevice9Impl_CreateStateBlock,
2510     IDirect3DDevice9Impl_BeginStateBlock,
2511     IDirect3DDevice9Impl_EndStateBlock,
2512     IDirect3DDevice9Impl_SetClipStatus,
2513     IDirect3DDevice9Impl_GetClipStatus,
2514     IDirect3DDevice9Impl_GetTexture,
2515     IDirect3DDevice9Impl_SetTexture,
2516     IDirect3DDevice9Impl_GetTextureStageState,
2517     IDirect3DDevice9Impl_SetTextureStageState,
2518     IDirect3DDevice9Impl_GetSamplerState,
2519     IDirect3DDevice9Impl_SetSamplerState,
2520     IDirect3DDevice9Impl_ValidateDevice,
2521     IDirect3DDevice9Impl_SetPaletteEntries,
2522     IDirect3DDevice9Impl_GetPaletteEntries,
2523     IDirect3DDevice9Impl_SetCurrentTexturePalette,
2524     IDirect3DDevice9Impl_GetCurrentTexturePalette,
2525     IDirect3DDevice9Impl_SetScissorRect,
2526     IDirect3DDevice9Impl_GetScissorRect,
2527     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
2528     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
2529     IDirect3DDevice9Impl_SetNPatchMode,
2530     IDirect3DDevice9Impl_GetNPatchMode,
2531     IDirect3DDevice9Impl_DrawPrimitive,
2532     IDirect3DDevice9Impl_DrawIndexedPrimitive,
2533     IDirect3DDevice9Impl_DrawPrimitiveUP,
2534     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
2535     IDirect3DDevice9Impl_ProcessVertices,
2536     IDirect3DDevice9Impl_CreateVertexDeclaration,
2537     IDirect3DDevice9Impl_SetVertexDeclaration,
2538     IDirect3DDevice9Impl_GetVertexDeclaration,
2539     IDirect3DDevice9Impl_SetFVF,
2540     IDirect3DDevice9Impl_GetFVF,
2541     IDirect3DDevice9Impl_CreateVertexShader,
2542     IDirect3DDevice9Impl_SetVertexShader,
2543     IDirect3DDevice9Impl_GetVertexShader,
2544     IDirect3DDevice9Impl_SetVertexShaderConstantF,
2545     IDirect3DDevice9Impl_GetVertexShaderConstantF,
2546     IDirect3DDevice9Impl_SetVertexShaderConstantI,
2547     IDirect3DDevice9Impl_GetVertexShaderConstantI,
2548     IDirect3DDevice9Impl_SetVertexShaderConstantB,
2549     IDirect3DDevice9Impl_GetVertexShaderConstantB,
2550     IDirect3DDevice9Impl_SetStreamSource,
2551     IDirect3DDevice9Impl_GetStreamSource,
2552     IDirect3DDevice9Impl_SetStreamSourceFreq,
2553     IDirect3DDevice9Impl_GetStreamSourceFreq,
2554     IDirect3DDevice9Impl_SetIndices,
2555     IDirect3DDevice9Impl_GetIndices,
2556     IDirect3DDevice9Impl_CreatePixelShader,
2557     IDirect3DDevice9Impl_SetPixelShader,
2558     IDirect3DDevice9Impl_GetPixelShader,
2559     IDirect3DDevice9Impl_SetPixelShaderConstantF,
2560     IDirect3DDevice9Impl_GetPixelShaderConstantF,
2561     IDirect3DDevice9Impl_SetPixelShaderConstantI,
2562     IDirect3DDevice9Impl_GetPixelShaderConstantI,
2563     IDirect3DDevice9Impl_SetPixelShaderConstantB,
2564     IDirect3DDevice9Impl_GetPixelShaderConstantB,
2565     IDirect3DDevice9Impl_DrawRectPatch,
2566     IDirect3DDevice9Impl_DrawTriPatch,
2567     IDirect3DDevice9Impl_DeletePatch,
2568     IDirect3DDevice9Impl_CreateQuery,
2569     /* IDirect3DDevice9Ex */
2570     IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
2571     IDirect3DDevice9ExImpl_ComposeRects,
2572     IDirect3DDevice9ExImpl_PresentEx,
2573     IDirect3DDevice9ExImpl_GetGPUThreadPriority,
2574     IDirect3DDevice9ExImpl_SetGPUThreadPriority,
2575     IDirect3DDevice9ExImpl_WaitForVBlank,
2576     IDirect3DDevice9ExImpl_CheckResourceResidency,
2577     IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
2578     IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
2579     IDirect3DDevice9ExImpl_CheckDeviceState,
2580     IDirect3DDevice9ExImpl_CreateRenderTargetEx,
2581     IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
2582     IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
2583     IDirect3DDevice9ExImpl_ResetEx,
2584     IDirect3DDevice9ExImpl_GetDisplayModeEx
2585 };
2586
2587 /* IWineD3DDeviceParent IUnknown methods */
2588
2589 static inline struct IDirect3DDevice9Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2590 {
2591     return (struct IDirect3DDevice9Impl *)((char*)iface
2592             - FIELD_OFFSET(struct IDirect3DDevice9Impl, device_parent_vtbl));
2593 }
2594
2595 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
2596 {
2597     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2598     return IDirect3DDevice9Impl_QueryInterface((IDirect3DDevice9Ex *)This, riid, object);
2599 }
2600
2601 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2602 {
2603     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2604     return IDirect3DDevice9Impl_AddRef((IDirect3DDevice9Ex *)This);
2605 }
2606
2607 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2608 {
2609     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2610     return IDirect3DDevice9Impl_Release((IDirect3DDevice9Ex *)This);
2611 }
2612
2613 /* IWineD3DDeviceParent methods */
2614
2615 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2616 {
2617     TRACE("iface %p, device %p\n", iface, device);
2618 }
2619
2620 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2621         IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2622         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2623 {
2624     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2625     IDirect3DSurface9Impl *d3d_surface;
2626     BOOL lockable = TRUE;
2627     HRESULT hr;
2628
2629     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
2630             "\tpool %#x, level %u, face %u, surface %p\n",
2631             iface, superior, width, height, format, usage, pool, level, face, surface);
2632
2633     if (pool == WINED3DPOOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
2634         lockable = FALSE;
2635
2636     hr = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9Ex *)This, width, height,
2637             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2638             (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2639     if (FAILED(hr))
2640     {
2641         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2642         return hr;
2643     }
2644
2645     *surface = d3d_surface->wineD3DSurface;
2646     IWineD3DSurface_AddRef(*surface);
2647
2648     d3d_surface->container = superior;
2649     IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
2650     d3d_surface->parentDevice = NULL;
2651
2652     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
2653     d3d_surface->forwardReference = superior;
2654
2655     return hr;
2656 }
2657
2658 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2659         IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format,
2660         WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
2661         IWineD3DSurface **surface)
2662 {
2663     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2664     IDirect3DSurface9Impl *d3d_surface;
2665     HRESULT hr;
2666
2667     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2668             "\tmultisample_quality %u, lockable %u, surface %p\n",
2669             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
2670
2671     hr = IDirect3DDevice9Impl_CreateRenderTarget((IDirect3DDevice9Ex *)This, width, height,
2672             d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
2673             (IDirect3DSurface9 **)&d3d_surface, NULL);
2674     if (FAILED(hr))
2675     {
2676         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2677         return hr;
2678     }
2679
2680     *surface = d3d_surface->wineD3DSurface;
2681     IWineD3DSurface_AddRef(*surface);
2682
2683     d3d_surface->container = superior;
2684     /* Implicit surfaces are created with an refcount of 0 */
2685     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
2686
2687     return hr;
2688 }
2689
2690 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2691         UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
2692         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2693 {
2694     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2695     IDirect3DSurface9Impl *d3d_surface;
2696     HRESULT hr;
2697
2698     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2699             "\tmultisample_quality %u, discard %u, surface %p\n",
2700             iface, width, height, format, multisample_type, multisample_quality, discard, surface);
2701
2702     hr = IDirect3DDevice9Impl_CreateDepthStencilSurface((IDirect3DDevice9Ex *)This, width, height,
2703             d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, discard,
2704             (IDirect3DSurface9 **)&d3d_surface, NULL);
2705     if (FAILED(hr))
2706     {
2707         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2708         return hr;
2709     }
2710
2711     *surface = d3d_surface->wineD3DSurface;
2712     IWineD3DSurface_AddRef(*surface);
2713     d3d_surface->container = (IUnknown *)This;
2714     /* Implicit surfaces are created with an refcount of 0 */
2715     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
2716
2717     return hr;
2718 }
2719
2720 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2721         IUnknown *superior, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2722         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2723 {
2724     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2725     IDirect3DVolume9Impl *object;
2726     HRESULT hr;
2727
2728     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2729             iface, superior, width, height, depth, format, pool, usage, volume);
2730
2731     /* Allocate the storage for the device */
2732     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2733     if (!object)
2734     {
2735         FIXME("Allocation of memory failed\n");
2736         *volume = NULL;
2737         return D3DERR_OUTOFVIDEOMEMORY;
2738     }
2739
2740     hr = volume_init(object, This, width, height, depth, usage, format, pool);
2741     if (FAILED(hr))
2742     {
2743         WARN("Failed to initialize volume, hr %#x.\n", hr);
2744         HeapFree(GetProcessHeap(), 0, object);
2745         return hr;
2746     }
2747
2748     *volume = object->wineD3DVolume;
2749     IWineD3DVolume_AddRef(*volume);
2750     IDirect3DVolume9_Release((IDirect3DVolume9 *)object);
2751
2752     object->container = superior;
2753     object->forwardReference = superior;
2754
2755     TRACE("(%p) Created volume %p\n", iface, object);
2756
2757     return hr;
2758 }
2759
2760 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2761         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2762 {
2763     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2764     IDirect3DSwapChain9Impl *d3d_swapchain;
2765     D3DPRESENT_PARAMETERS local_parameters;
2766     HRESULT hr;
2767
2768     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2769
2770     /* Copy the presentation parameters */
2771     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2772     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2773     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2774     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2775     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2776     local_parameters.MultiSampleQuality = present_parameters->MultiSampleQuality;
2777     local_parameters.SwapEffect = present_parameters->SwapEffect;
2778     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2779     local_parameters.Windowed = present_parameters->Windowed;
2780     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2781     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2782     local_parameters.Flags = present_parameters->Flags;
2783     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2784     local_parameters.PresentationInterval = present_parameters->PresentationInterval;
2785
2786     hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain((IDirect3DDevice9Ex *)This,
2787             &local_parameters, (IDirect3DSwapChain9 **)&d3d_swapchain);
2788     if (FAILED(hr))
2789     {
2790         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2791         *swapchain = NULL;
2792         return hr;
2793     }
2794
2795     *swapchain = d3d_swapchain->wineD3DSwapChain;
2796     d3d_swapchain->isImplicit = TRUE;
2797     /* Implicit swap chains are created with an refcount of 0 */
2798     IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)d3d_swapchain);
2799
2800     /* Copy back the presentation parameters */
2801     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2802     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2803     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2804     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2805     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2806     present_parameters->MultiSampleQuality = local_parameters.MultiSampleQuality;
2807     present_parameters->SwapEffect = local_parameters.SwapEffect;
2808     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2809     present_parameters->Windowed = local_parameters.Windowed;
2810     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2811     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2812     present_parameters->Flags = local_parameters.Flags;
2813     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2814     present_parameters->PresentationInterval = local_parameters.PresentationInterval;
2815
2816     return hr;
2817 }
2818
2819 static const IWineD3DDeviceParentVtbl d3d9_wined3d_device_parent_vtbl =
2820 {
2821     /* IUnknown methods */
2822     device_parent_QueryInterface,
2823     device_parent_AddRef,
2824     device_parent_Release,
2825     /* IWineD3DDeviceParent methods */
2826     device_parent_WineD3DDeviceCreated,
2827     device_parent_CreateSurface,
2828     device_parent_CreateRenderTarget,
2829     device_parent_CreateDepthStencilSurface,
2830     device_parent_CreateVolume,
2831     device_parent_CreateSwapChain,
2832 };
2833
2834 static void setup_fpu(void)
2835 {
2836 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2837     WORD cw;
2838     __asm__ volatile ("fnstcw %0" : "=m" (cw));
2839     cw = (cw & ~0xf3f) | 0x3f;
2840     __asm__ volatile ("fldcw %0" : : "m" (cw));
2841 #else
2842     FIXME("FPU setup not implemented for this platform.\n");
2843 #endif
2844 }
2845
2846 HRESULT device_init(IDirect3DDevice9Impl *device, IWineD3D *wined3d, UINT adapter, D3DDEVTYPE device_type,
2847         HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
2848 {
2849     WINED3DPRESENT_PARAMETERS *wined3d_parameters;
2850     UINT i, count = 1;
2851     HRESULT hr;
2852
2853     device->lpVtbl = &Direct3DDevice9_Vtbl;
2854     device->device_parent_vtbl = &d3d9_wined3d_device_parent_vtbl;
2855     device->ref = 1;
2856
2857     if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
2858
2859     wined3d_mutex_lock();
2860     hr = IWineD3D_CreateDevice(wined3d, adapter, device_type, focus_window, flags, (IUnknown *)device,
2861             (IWineD3DDeviceParent *)&device->device_parent_vtbl, &device->WineD3DDevice);
2862     if (FAILED(hr))
2863     {
2864         WARN("Failed to create wined3d device, hr %#x.\n", hr);
2865         wined3d_mutex_unlock();
2866         return hr;
2867     }
2868
2869     if (!parameters->Windowed)
2870     {
2871         if (!focus_window) focus_window = parameters->hDeviceWindow;
2872         if (FAILED(hr = IWineD3DDevice_AcquireFocusWindow(device->WineD3DDevice, focus_window)))
2873         {
2874             ERR("Failed to acquire focus window, hr %#x.\n", hr);
2875             IWineD3DDevice_Release(device->WineD3DDevice);
2876             wined3d_mutex_unlock();
2877             return hr;
2878         }
2879     }
2880
2881     if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
2882     {
2883         WINED3DCAPS caps;
2884
2885         IWineD3D_GetDeviceCaps(wined3d, adapter, device_type, &caps);
2886         count = caps.NumberOfAdaptersInGroup;
2887     }
2888
2889     if (flags & D3DCREATE_MULTITHREADED) IWineD3DDevice_SetMultithreaded(device->WineD3DDevice);
2890
2891     wined3d_parameters = HeapAlloc(GetProcessHeap(), 0, sizeof(*wined3d_parameters) * count);
2892     if (!wined3d_parameters)
2893     {
2894         ERR("Failed to allocate wined3d parameters.\n");
2895         IWineD3DDevice_Release(device->WineD3DDevice);
2896         wined3d_mutex_unlock();
2897         return E_OUTOFMEMORY;
2898     }
2899
2900     for (i = 0; i < count; ++i)
2901     {
2902         wined3d_parameters[i].BackBufferWidth = parameters[i].BackBufferWidth;
2903         wined3d_parameters[i].BackBufferHeight = parameters[i].BackBufferHeight;
2904         wined3d_parameters[i].BackBufferFormat = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
2905         wined3d_parameters[i].BackBufferCount = parameters[i].BackBufferCount;
2906         wined3d_parameters[i].MultiSampleType = parameters[i].MultiSampleType;
2907         wined3d_parameters[i].MultiSampleQuality = parameters[i].MultiSampleQuality;
2908         wined3d_parameters[i].SwapEffect = parameters[i].SwapEffect;
2909         wined3d_parameters[i].hDeviceWindow = parameters[i].hDeviceWindow;
2910         wined3d_parameters[i].Windowed = parameters[i].Windowed;
2911         wined3d_parameters[i].EnableAutoDepthStencil = parameters[i].EnableAutoDepthStencil;
2912         wined3d_parameters[i].AutoDepthStencilFormat =
2913                 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
2914         wined3d_parameters[i].Flags = parameters[i].Flags;
2915         wined3d_parameters[i].FullScreen_RefreshRateInHz = parameters[i].FullScreen_RefreshRateInHz;
2916         wined3d_parameters[i].PresentationInterval = parameters[i].PresentationInterval;
2917         wined3d_parameters[i].AutoRestoreDisplayMode = TRUE;
2918     }
2919
2920     hr = IWineD3DDevice_Init3D(device->WineD3DDevice, wined3d_parameters);
2921     if (FAILED(hr))
2922     {
2923         WARN("Failed to initialize 3D, hr %#x.\n", hr);
2924         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
2925         HeapFree(GetProcessHeap(), 0, wined3d_parameters);
2926         IWineD3DDevice_Release(device->WineD3DDevice);
2927         wined3d_mutex_unlock();
2928         return hr;
2929     }
2930
2931     wined3d_mutex_unlock();
2932
2933     for (i = 0; i < count; ++i)
2934     {
2935         parameters[i].BackBufferWidth = wined3d_parameters[i].BackBufferWidth;
2936         parameters[i].BackBufferHeight = wined3d_parameters[i].BackBufferHeight;
2937         parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters[i].BackBufferFormat);
2938         parameters[i].BackBufferCount = wined3d_parameters[i].BackBufferCount;
2939         parameters[i].MultiSampleType = wined3d_parameters[i].MultiSampleType;
2940         parameters[i].MultiSampleQuality = wined3d_parameters[i].MultiSampleQuality;
2941         parameters[i].SwapEffect = wined3d_parameters[i].SwapEffect;
2942         parameters[i].hDeviceWindow = wined3d_parameters[i].hDeviceWindow;
2943         parameters[i].Windowed = wined3d_parameters[i].Windowed;
2944         parameters[i].EnableAutoDepthStencil = wined3d_parameters[i].EnableAutoDepthStencil;
2945         parameters[i].AutoDepthStencilFormat =
2946                 d3dformat_from_wined3dformat(wined3d_parameters[i].AutoDepthStencilFormat);
2947         parameters[i].Flags = wined3d_parameters[i].Flags;
2948         parameters[i].FullScreen_RefreshRateInHz = wined3d_parameters[i].FullScreen_RefreshRateInHz;
2949         parameters[i].PresentationInterval = wined3d_parameters[i].PresentationInterval;
2950     }
2951     HeapFree(GetProcessHeap(), 0, wined3d_parameters);
2952
2953     /* Initialize the converted declaration array. This creates a valid pointer
2954      * and when adding decls HeapReAlloc() can be used without further checking. */
2955     device->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
2956     if (!device->convertedDecls)
2957     {
2958         ERR("Failed to allocate FVF vertex declaration map memory.\n");
2959         wined3d_mutex_lock();
2960         IWineD3DDevice_Uninit3D(device->WineD3DDevice, D3D9CB_DestroySwapChain);
2961         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
2962         IWineD3DDevice_Release(device->WineD3DDevice);
2963         wined3d_mutex_unlock();
2964         return E_OUTOFMEMORY;
2965     }
2966
2967     return D3D_OK;
2968 }