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