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