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