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