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