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