d3d9: Surfaces always have resource type WINED3DRTYPE_SURFACE.
[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(enum wined3d_format_id format)
29 {
30     BYTE *c = (BYTE *)&format;
31
32     /* Don't translate FOURCC formats */
33     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
34
35     switch(format)
36     {
37         case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
38         case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
39         case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
40         case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
41         case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
42         case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
43         case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
44         case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
45         case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
46         case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
47         case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
48         case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
49         case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
50         case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
51         case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
52         case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
53         case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
54         case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
55         case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
56         case WINED3DFMT_P8_UINT: return D3DFMT_P8;
57         case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
58         case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
59         case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
60         case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
61         case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
62         case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
63         case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
64         case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
65         case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
66         case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
67         case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
68         case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
69         case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
70         case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
71         case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
72         case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
73         case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
74         case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
75         case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
76         case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
77         case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
78         case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
79         case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
80         case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
81         case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
82         case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
83         case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
84         case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
85         case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
86         case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
87         default:
88             FIXME("Unhandled wined3d format %#x.\n", format);
89             return D3DFMT_UNKNOWN;
90     }
91 }
92
93 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
94 {
95     BYTE *c = (BYTE *)&format;
96
97     /* Don't translate FOURCC formats */
98     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
99
100     switch(format)
101     {
102         case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
103         case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
104         case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
105         case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
106         case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
107         case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
108         case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
109         case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
110         case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
111         case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
112         case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
113         case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
114         case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
115         case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
116         case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
117         case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118         case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
119         case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
120         case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121         case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122         case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123         case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124         case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125         case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126         case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127         case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128         case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129         case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130         case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
131         case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
132         case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
133         case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
134         case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
135         case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
136         case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
137         case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
138         case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
139         case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
140         case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
141         case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
142         case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
143         case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
144         case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
145         case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
146         case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
147         case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
148         case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
149         case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
150         case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
151         case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
152         default:
153             FIXME("Unhandled D3DFORMAT %#x\n", format);
154             return WINED3DFMT_UNKNOWN;
155     }
156 }
157
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
159 {
160     switch(primitive_type)
161     {
162         case D3DPT_POINTLIST:
163             return primitive_count;
164
165         case D3DPT_LINELIST:
166             return primitive_count * 2;
167
168         case D3DPT_LINESTRIP:
169             return primitive_count + 1;
170
171         case D3DPT_TRIANGLELIST:
172             return primitive_count * 3;
173
174         case D3DPT_TRIANGLESTRIP:
175         case D3DPT_TRIANGLEFAN:
176             return primitive_count + 2;
177
178         default:
179             FIXME("Unhandled primitive type %#x\n", primitive_type);
180             return 0;
181     }
182 }
183
184 static ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *swapchain)
185 {
186     IDirect3DSwapChain9Impl *parent;
187
188     TRACE("swapchain %p.\n", swapchain);
189
190     parent = IWineD3DSwapChain_GetParent(swapchain);
191     parent->isImplicit = FALSE;
192     IDirect3DSwapChain9_AddRef((IDirect3DSwapChain9 *)parent);
193     return IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)parent);
194 }
195
196 /* IDirect3D IUnknown parts follow: */
197 static HRESULT WINAPI IDirect3DDevice9Impl_QueryInterface(LPDIRECT3DDEVICE9EX iface, REFIID riid, LPVOID* ppobj) {
198     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
199     IDirect3D9 *d3d;
200     IDirect3D9Impl *d3dimpl;
201
202     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
203
204     if (IsEqualGUID(riid, &IID_IUnknown)
205         || IsEqualGUID(riid, &IID_IDirect3DDevice9)) {
206         IDirect3DDevice9Ex_AddRef(iface);
207         *ppobj = This;
208         TRACE("Returning IDirect3DDevice9 interface at %p\n", *ppobj);
209         return S_OK;
210     } else if(IsEqualGUID(riid, &IID_IDirect3DDevice9Ex)) {
211         /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
212          * It doesn't matter with which function the device was created.
213          */
214         IDirect3DDevice9_GetDirect3D(iface, &d3d);
215         d3dimpl = (IDirect3D9Impl *) d3d;
216
217         if(d3dimpl->extended) {
218             *ppobj = iface;
219             IDirect3DDevice9Ex_AddRef((IDirect3DDevice9Ex *) *ppobj);
220             IDirect3D9_Release(d3d);
221             TRACE("Returning IDirect3DDevice9Ex interface at %p\n", *ppobj);
222             return S_OK;
223         } else {
224             WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE\n");
225             IDirect3D9_Release(d3d);
226             *ppobj = NULL;
227             return E_NOINTERFACE;
228         }
229     }
230
231     if (IsEqualGUID(riid, &IID_IWineD3DDeviceParent))
232     {
233         IUnknown_AddRef((IUnknown *)&This->device_parent_vtbl);
234         *ppobj = &This->device_parent_vtbl;
235         return S_OK;
236     }
237
238     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
239     *ppobj = NULL;
240     return E_NOINTERFACE;
241 }
242
243 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(LPDIRECT3DDEVICE9EX iface) {
244     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
245     ULONG ref = InterlockedIncrement(&This->ref);
246
247     TRACE("%p increasing refcount to %u.\n", iface, ref);
248
249     return ref;
250 }
251
252 static ULONG WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Release(LPDIRECT3DDEVICE9EX iface) {
253     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
254     ULONG ref;
255
256     if (This->inDestruction) return 0;
257     ref = InterlockedDecrement(&This->ref);
258
259     TRACE("%p decreasing refcount to %u.\n", iface, ref);
260
261     if (ref == 0) {
262       unsigned i;
263       This->inDestruction = TRUE;
264
265       wined3d_mutex_lock();
266       for(i = 0; i < This->numConvertedDecls; i++) {
267           /* Unless Wine is buggy or the app has a bug the refcount will be 0, because decls hold a reference to the
268            * device
269            */
270           IDirect3DVertexDeclaration9Impl_Destroy(This->convertedDecls[i]);
271       }
272       HeapFree(GetProcessHeap(), 0, This->convertedDecls);
273
274       IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D9CB_DestroySwapChain);
275       IWineD3DDevice_ReleaseFocusWindow(This->WineD3DDevice);
276       IWineD3DDevice_Release(This->WineD3DDevice);
277       wined3d_mutex_unlock();
278
279       HeapFree(GetProcessHeap(), 0, This);
280     }
281     return ref;
282 }
283
284 /* IDirect3DDevice Interface follow: */
285 static HRESULT WINAPI IDirect3DDevice9Impl_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
286 {
287     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
288
289     TRACE("iface %p.\n", iface);
290
291     if (This->notreset)
292     {
293         TRACE("D3D9 device is marked not reset.\n");
294         return D3DERR_DEVICENOTRESET;
295     }
296
297     return D3D_OK;
298 }
299
300 static UINT     WINAPI  IDirect3DDevice9Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE9EX iface) {
301     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
302     HRESULT hr;
303
304     TRACE("iface %p.\n", iface);
305
306     wined3d_mutex_lock();
307     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
308     wined3d_mutex_unlock();
309
310     return hr;
311 }
312
313 static HRESULT  WINAPI  IDirect3DDevice9Impl_EvictManagedResources(LPDIRECT3DDEVICE9EX iface) {
314     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
315     HRESULT hr;
316
317     TRACE("iface %p.\n", iface);
318
319     wined3d_mutex_lock();
320     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
321     wined3d_mutex_unlock();
322
323     return hr;
324 }
325
326 static HRESULT WINAPI IDirect3DDevice9Impl_GetDirect3D(IDirect3DDevice9Ex *iface, IDirect3D9 **ppD3D9)
327 {
328     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
329     struct wined3d *wined3d;
330     HRESULT hr = D3D_OK;
331
332     TRACE("iface %p, d3d9 %p.\n", iface, ppD3D9);
333
334     if (NULL == ppD3D9) {
335         return D3DERR_INVALIDCALL;
336     }
337
338     wined3d_mutex_lock();
339     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &wined3d);
340     if (hr == D3D_OK && wined3d)
341     {
342         *ppD3D9 = wined3d_get_parent(wined3d);
343         IDirect3D9_AddRef(*ppD3D9);
344         wined3d_decref(wined3d);
345     }
346     else
347     {
348         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
349         *ppD3D9 = NULL;
350     }
351     TRACE("(%p) returning %p\n", This, *ppD3D9);
352     wined3d_mutex_unlock();
353
354     return hr;
355 }
356
357 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDeviceCaps(LPDIRECT3DDEVICE9EX iface, D3DCAPS9* pCaps) {
358     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
359     HRESULT hrc = D3D_OK;
360     WINED3DCAPS *pWineCaps;
361
362     TRACE("iface %p, caps %p.\n", iface, pCaps);
363
364     if(NULL == pCaps){
365         return D3DERR_INVALIDCALL;
366     }
367     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
368     if(pWineCaps == NULL){
369         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
370     }
371
372     memset(pCaps, 0, sizeof(*pCaps));
373
374     wined3d_mutex_lock();
375     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
376     wined3d_mutex_unlock();
377
378     WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
379     HeapFree(GetProcessHeap(), 0, pWineCaps);
380
381     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
382     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
383
384     filter_caps(pCaps);
385
386     TRACE("Returning %p %p\n", This, pCaps);
387     return hrc;
388 }
389
390 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDisplayMode(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DDISPLAYMODE* pMode) {
391     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
392     HRESULT hr;
393
394     TRACE("iface %p, swapchain %u, mode %p.\n", iface, iSwapChain, pMode);
395
396     wined3d_mutex_lock();
397     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, iSwapChain, (WINED3DDISPLAYMODE *) pMode);
398     wined3d_mutex_unlock();
399
400     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
401
402     return hr;
403 }
404
405 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCreationParameters(LPDIRECT3DDEVICE9EX iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
406     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
407     HRESULT hr;
408
409     TRACE("iface %p, parameters %p.\n", iface, pParameters);
410
411     wined3d_mutex_lock();
412     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
413     wined3d_mutex_unlock();
414
415     return hr;
416 }
417
418 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCursorProperties(LPDIRECT3DDEVICE9EX iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9* pCursorBitmap) {
419     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
420     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pCursorBitmap;
421     HRESULT hr;
422
423     TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
424             iface, XHotSpot, YHotSpot, pCursorBitmap);
425
426     if (!pCursorBitmap)
427     {
428         WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
429         return D3DERR_INVALIDCALL;
430     }
431
432     wined3d_mutex_lock();
433     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice, XHotSpot, YHotSpot, pSurface->wineD3DSurface);
434     wined3d_mutex_unlock();
435
436     return hr;
437 }
438
439 static void     WINAPI  IDirect3DDevice9Impl_SetCursorPosition(LPDIRECT3DDEVICE9EX iface, int XScreenSpace, int YScreenSpace, DWORD Flags) {
440     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
441
442     TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, XScreenSpace, YScreenSpace, Flags);
443
444     wined3d_mutex_lock();
445     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
446     wined3d_mutex_unlock();
447 }
448
449 static BOOL     WINAPI  IDirect3DDevice9Impl_ShowCursor(LPDIRECT3DDEVICE9EX iface, BOOL bShow) {
450     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
451     BOOL ret;
452
453     TRACE("iface %p, show %#x.\n", iface, bShow);
454
455     wined3d_mutex_lock();
456     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
457     wined3d_mutex_unlock();
458
459     return ret;
460 }
461
462 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_CreateAdditionalSwapChain(IDirect3DDevice9Ex *iface,
463         D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain9 **swapchain)
464 {
465     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
466     IDirect3DSwapChain9Impl *object;
467     HRESULT hr;
468
469     TRACE("iface %p, present_parameters %p, swapchain %p.\n",
470             iface, present_parameters, swapchain);
471
472     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
473     if (!object)
474     {
475         ERR("Failed to allocate swapchain memory.\n");
476         return E_OUTOFMEMORY;
477     }
478
479     hr = swapchain_init(object, This, present_parameters);
480     if (FAILED(hr))
481     {
482         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
483         HeapFree(GetProcessHeap(), 0, object);
484         return hr;
485     }
486
487     TRACE("Created swapchain %p.\n", object);
488     *swapchain = (IDirect3DSwapChain9 *)object;
489
490     return D3D_OK;
491 }
492
493 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_GetSwapChain(IDirect3DDevice9Ex *iface,
494         UINT swapchain_idx, IDirect3DSwapChain9 **swapchain)
495 {
496     IWineD3DSwapChain *wined3d_swapchain = NULL;
497     HRESULT hr;
498
499     TRACE("iface %p, swapchain_idx %u, swapchain %p.\n", iface, swapchain_idx, swapchain);
500
501     wined3d_mutex_lock();
502     hr = IWineD3DDevice_GetSwapChain(((IDirect3DDevice9Impl *)iface)->WineD3DDevice,
503             swapchain_idx, &wined3d_swapchain);
504     if (SUCCEEDED(hr) && wined3d_swapchain)
505     {
506        *swapchain = IWineD3DSwapChain_GetParent(wined3d_swapchain);
507        IDirect3DSwapChain9_AddRef(*swapchain);
508        IWineD3DSwapChain_Release(wined3d_swapchain);
509     }
510     else
511     {
512         *swapchain = NULL;
513     }
514     wined3d_mutex_unlock();
515
516     return hr;
517 }
518
519 static UINT WINAPI IDirect3DDevice9Impl_GetNumberOfSwapChains(IDirect3DDevice9Ex *iface)
520 {
521     UINT count;
522
523     TRACE("iface %p.\n", iface);
524
525     wined3d_mutex_lock();
526     count = IWineD3DDevice_GetNumberOfSwapChains(((IDirect3DDevice9Impl *)iface)->WineD3DDevice);
527     wined3d_mutex_unlock();
528
529     return count;
530 }
531
532 static HRESULT WINAPI reset_enum_callback(struct wined3d_resource *resource, void *data)
533 {
534     BOOL *resources_ok = data;
535     D3DRESOURCETYPE type;
536     HRESULT ret = S_OK;
537     D3DSURFACE_DESC surface_desc;
538     D3DVOLUME_DESC volume_desc;
539     D3DINDEXBUFFER_DESC index_desc;
540     D3DVERTEXBUFFER_DESC vertex_desc;
541     WINED3DPOOL pool;
542     IDirect3DResource9 *parent;
543
544     parent = wined3d_resource_get_parent(resource);
545     type = IDirect3DResource9_GetType(parent);
546     switch(type) {
547         case D3DRTYPE_SURFACE:
548             IDirect3DSurface9_GetDesc((IDirect3DSurface9 *)parent, &surface_desc);
549             pool = surface_desc.Pool;
550             break;
551
552         case D3DRTYPE_VOLUME:
553             IDirect3DVolume9_GetDesc((IDirect3DVolume9 *)parent, &volume_desc);
554             pool = volume_desc.Pool;
555             break;
556
557         case D3DRTYPE_INDEXBUFFER:
558             IDirect3DIndexBuffer9_GetDesc((IDirect3DIndexBuffer9 *) parent, &index_desc);
559             pool = index_desc.Pool;
560             break;
561
562         case D3DRTYPE_VERTEXBUFFER:
563             IDirect3DVertexBuffer9_GetDesc((IDirect3DVertexBuffer9 *)parent, &vertex_desc);
564             pool = vertex_desc.Pool;
565             break;
566
567         /* No need to check for textures. If there is a D3DPOOL_DEFAULT texture, there
568          * is a D3DPOOL_DEFAULT surface or volume as well
569          */
570         default:
571             pool = WINED3DPOOL_SCRATCH; /* a harmless pool */
572             break;
573     }
574
575     if (pool == WINED3DPOOL_DEFAULT)
576     {
577         IDirect3DResource9_AddRef(parent);
578         if (IUnknown_Release(parent) == 0)
579         {
580             TRACE("Parent %p is an implicit resource with ref 0\n", parent);
581         } else {
582             WARN("Resource %p(wineD3D %p) with pool D3DPOOL_DEFAULT blocks the Reset call\n", parent, resource);
583             ret = S_FALSE;
584             *resources_ok = FALSE;
585         }
586     }
587
588     return ret;
589 }
590
591 static HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Reset(LPDIRECT3DDEVICE9EX iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
592     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
593     WINED3DPRESENT_PARAMETERS localParameters;
594     HRESULT hr;
595     BOOL resources_ok = TRUE;
596     UINT i;
597
598     TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
599
600     /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
601      * such objects can still be used for rendering after their external d3d9 object has been destroyed.
602      * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
603      * make use of the hidden reference and destroys the objects.
604      *
605      * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
606      * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
607      */
608     wined3d_mutex_lock();
609     IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice, NULL, WINED3DFMT_UNKNOWN);
610     for(i = 0; i < 16; i++) {
611         IWineD3DDevice_SetStreamSource(This->WineD3DDevice, i, NULL, 0, 0);
612     }
613     for(i = 0; i < 16; i++) {
614         IWineD3DDevice_SetTexture(This->WineD3DDevice, i, NULL);
615     }
616
617     IWineD3DDevice_EnumResources(This->WineD3DDevice, reset_enum_callback, &resources_ok);
618     if(!resources_ok) {
619         WARN("The application is holding D3DPOOL_DEFAULT resources, rejecting reset\n");
620         This->notreset = TRUE;
621         wined3d_mutex_unlock();
622
623         return D3DERR_INVALIDCALL;
624     }
625
626     localParameters.BackBufferWidth                     = pPresentationParameters->BackBufferWidth;
627     localParameters.BackBufferHeight                    = pPresentationParameters->BackBufferHeight;
628     localParameters.BackBufferFormat                    = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
629     localParameters.BackBufferCount                     = pPresentationParameters->BackBufferCount;
630     localParameters.MultiSampleType                     = pPresentationParameters->MultiSampleType;
631     localParameters.MultiSampleQuality                  = pPresentationParameters->MultiSampleQuality;
632     localParameters.SwapEffect                          = pPresentationParameters->SwapEffect;
633     localParameters.hDeviceWindow                       = pPresentationParameters->hDeviceWindow;
634     localParameters.Windowed                            = pPresentationParameters->Windowed;
635     localParameters.EnableAutoDepthStencil              = pPresentationParameters->EnableAutoDepthStencil;
636     localParameters.AutoDepthStencilFormat              = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
637     localParameters.Flags                               = pPresentationParameters->Flags;
638     localParameters.FullScreen_RefreshRateInHz          = pPresentationParameters->FullScreen_RefreshRateInHz;
639     localParameters.PresentationInterval                = pPresentationParameters->PresentationInterval;
640     localParameters.AutoRestoreDisplayMode              = TRUE;
641
642     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
643     if(FAILED(hr)) {
644         This->notreset = TRUE;
645
646         pPresentationParameters->BackBufferWidth            = localParameters.BackBufferWidth;
647         pPresentationParameters->BackBufferHeight           = localParameters.BackBufferHeight;
648         pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
649         pPresentationParameters->BackBufferCount            = localParameters.BackBufferCount;
650         pPresentationParameters->MultiSampleType            = localParameters.MultiSampleType;
651         pPresentationParameters->MultiSampleQuality         = localParameters.MultiSampleQuality;
652         pPresentationParameters->SwapEffect                 = localParameters.SwapEffect;
653         pPresentationParameters->hDeviceWindow              = localParameters.hDeviceWindow;
654         pPresentationParameters->Windowed                   = localParameters.Windowed;
655         pPresentationParameters->EnableAutoDepthStencil     = localParameters.EnableAutoDepthStencil;
656         pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
657         pPresentationParameters->Flags                      = localParameters.Flags;
658         pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
659         pPresentationParameters->PresentationInterval       = localParameters.PresentationInterval;
660     } else {
661         This->notreset = FALSE;
662     }
663
664     wined3d_mutex_unlock();
665
666     return hr;
667 }
668
669 static HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Present(LPDIRECT3DDEVICE9EX iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA*
670  pDirtyRegion) {
671     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
672     HRESULT hr;
673
674     TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
675             iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
676
677     wined3d_mutex_lock();
678     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
679     wined3d_mutex_unlock();
680
681     return hr;
682  }
683
684 static HRESULT WINAPI IDirect3DDevice9Impl_GetBackBuffer(IDirect3DDevice9Ex *iface,
685         UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 **ppBackBuffer)
686 {
687     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
688     IWineD3DSurface *retSurface = NULL;
689     HRESULT hr;
690
691     TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
692             iface, iSwapChain, BackBuffer, Type, ppBackBuffer);
693
694     wined3d_mutex_lock();
695     hr = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, iSwapChain,
696             BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &retSurface);
697     if (SUCCEEDED(hr) && retSurface && ppBackBuffer)
698     {
699         *ppBackBuffer = IWineD3DSurface_GetParent(retSurface);
700         IDirect3DSurface9_AddRef(*ppBackBuffer);
701         IWineD3DSurface_Release(retSurface);
702     }
703     wined3d_mutex_unlock();
704
705     return hr;
706 }
707 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRasterStatus(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DRASTER_STATUS* pRasterStatus) {
708     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
709     HRESULT hr;
710
711     TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, iSwapChain, pRasterStatus);
712
713     wined3d_mutex_lock();
714     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, iSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
715     wined3d_mutex_unlock();
716
717     return hr;
718 }
719
720 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(LPDIRECT3DDEVICE9EX iface, BOOL bEnableDialogs) {
721     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
722     HRESULT hr;
723
724     TRACE("iface %p, enable %#x.\n", iface, bEnableDialogs);
725
726     wined3d_mutex_lock();
727     hr = IWineD3DDevice_SetDialogBoxMode(This->WineD3DDevice, bEnableDialogs);
728     wined3d_mutex_unlock();
729
730     return hr;
731 }
732
733 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
734         DWORD Flags, const D3DGAMMARAMP *pRamp)
735 {
736     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
737
738     TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, iSwapChain, Flags, pRamp);
739
740     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
741     wined3d_mutex_lock();
742     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, iSwapChain, Flags, (CONST WINED3DGAMMARAMP *)pRamp);
743     wined3d_mutex_unlock();
744 }
745
746 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DGAMMARAMP* pRamp) {
747     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
748
749     TRACE("iface %p, swapchain %u, ramp %p.\n", iface, iSwapChain, pRamp);
750
751     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
752     wined3d_mutex_lock();
753     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, iSwapChain, (WINED3DGAMMARAMP *) pRamp);
754     wined3d_mutex_unlock();
755 }
756
757 static HRESULT WINAPI IDirect3DDevice9Impl_CreateTexture(IDirect3DDevice9Ex *iface,
758         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
759         D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
760 {
761     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
762     IDirect3DTexture9Impl *object;
763     HRESULT hr;
764
765     TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
766             iface, width, height, levels, usage, format, pool, texture, shared_handle);
767
768     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
769     if (!object)
770     {
771         ERR("Failed to allocate texture memory.\n");
772         return D3DERR_OUTOFVIDEOMEMORY;
773     }
774
775     hr = texture_init(object, This, width, height, levels, usage, format, pool);
776     if (FAILED(hr))
777     {
778         WARN("Failed to initialize texture, hr %#x.\n", hr);
779         HeapFree(GetProcessHeap(), 0, object);
780         return hr;
781     }
782
783     TRACE("Created texture %p.\n", object);
784     *texture = (IDirect3DTexture9 *)object;
785
786     return D3D_OK;
787 }
788
789 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
790         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
791         D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
792 {
793     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
794     IDirect3DVolumeTexture9Impl *object;
795     HRESULT hr;
796
797     TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
798             iface, width, height, depth, levels);
799     TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
800             usage, format, pool, texture, shared_handle);
801
802     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
803     if (!object)
804     {
805         ERR("Failed to allocate volume texture memory.\n");
806         return D3DERR_OUTOFVIDEOMEMORY;
807     }
808
809     hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
810     if (FAILED(hr))
811     {
812         WARN("Failed to initialize volume texture, hr %#x.\n", hr);
813         HeapFree(GetProcessHeap(), 0, object);
814         return hr;
815     }
816
817     TRACE("Created volume texture %p.\n", object);
818     *texture = (IDirect3DVolumeTexture9 *)object;
819
820     return D3D_OK;
821 }
822
823 static HRESULT WINAPI IDirect3DDevice9Impl_CreateCubeTexture(IDirect3DDevice9Ex *iface,
824         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
825         IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
826 {
827     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
828     IDirect3DCubeTexture9Impl *object;
829     HRESULT hr;
830
831     TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
832             iface, edge_length, levels, usage, format, pool, texture, shared_handle);
833
834     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
835     if (!object)
836     {
837         ERR("Failed to allocate cube texture memory.\n");
838         return D3DERR_OUTOFVIDEOMEMORY;
839     }
840
841     hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
842     if (FAILED(hr))
843     {
844         WARN("Failed to initialize cube texture, hr %#x.\n", hr);
845         HeapFree(GetProcessHeap(), 0, object);
846         return hr;
847     }
848
849     TRACE("Created cube texture %p.\n", object);
850     *texture = (IDirect3DCubeTexture9 *)object;
851
852     return D3D_OK;
853 }
854
855 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size, DWORD usage,
856         DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer, HANDLE *shared_handle)
857 {
858     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
859     IDirect3DVertexBuffer9Impl *object;
860     HRESULT hr;
861
862     TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
863             iface, size, usage, fvf, pool, buffer, shared_handle);
864
865     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
866     if (!object)
867     {
868         ERR("Failed to allocate buffer memory.\n");
869         return D3DERR_OUTOFVIDEOMEMORY;
870     }
871
872     hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
873     if (FAILED(hr))
874     {
875         WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
876         HeapFree(GetProcessHeap(), 0, object);
877         return hr;
878     }
879
880     TRACE("Created vertex buffer %p.\n", object);
881     *buffer = (IDirect3DVertexBuffer9 *)object;
882
883     return D3D_OK;
884 }
885
886 static HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size, DWORD usage,
887         D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer, HANDLE *shared_handle)
888 {
889     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
890     IDirect3DIndexBuffer9Impl *object;
891     HRESULT hr;
892
893     TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
894             iface, size, usage, format, pool, buffer, shared_handle);
895
896     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
897     if (!object)
898     {
899         ERR("Failed to allocate buffer memory.\n");
900         return D3DERR_OUTOFVIDEOMEMORY;
901     }
902
903     hr = indexbuffer_init(object, This, size, usage, format, pool);
904     if (FAILED(hr))
905     {
906         WARN("Failed to initialize index buffer, hr %#x.\n", hr);
907         HeapFree(GetProcessHeap(), 0, object);
908         return hr;
909     }
910
911     TRACE("Created index buffer %p.\n", object);
912     *buffer = (IDirect3DIndexBuffer9 *)object;
913
914     return D3D_OK;
915 }
916
917 static HRESULT IDirect3DDevice9Impl_CreateSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height,
918         D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level, IDirect3DSurface9 **ppSurface,
919         UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality)
920 {
921     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
922     IDirect3DSurface9Impl *object;
923     HRESULT hr;
924
925     TRACE("iface %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p.\n"
926             "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
927             iface, Width, Height, Format, Lockable, Discard, Level, ppSurface,
928             Usage, Pool, MultiSample, MultisampleQuality);
929
930     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
931     if (!object)
932     {
933         FIXME("Failed to allocate surface memory.\n");
934         return D3DERR_OUTOFVIDEOMEMORY;
935     }
936
937     hr = surface_init(object, This, Width, Height, Format, Lockable, Discard,
938             Level, Usage, Pool, MultiSample, MultisampleQuality);
939     if (FAILED(hr))
940     {
941         WARN("Failed to initialize surface, hr %#x.\n", hr);
942         HeapFree(GetProcessHeap(), 0, object);
943         return hr;
944     }
945
946     TRACE("Created surface %p.\n", object);
947     *ppSurface = (IDirect3DSurface9 *)object;
948
949     return D3D_OK;
950 }
951
952 static HRESULT WINAPI IDirect3DDevice9Impl_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT Width, UINT Height,
953         D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality, BOOL Lockable,
954         IDirect3DSurface9 **ppSurface, HANDLE *pSharedHandle)
955 {
956     HRESULT hr;
957
958     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
959             "lockable %#x, surface %p, shared_handle %p.\n",
960             iface, Width, Height, Format, MultiSample, MultisampleQuality,
961             Lockable, ppSurface, pSharedHandle);
962
963     hr = IDirect3DDevice9Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */,
964             0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, MultisampleQuality);
965
966     return hr;
967 }
968
969 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height,
970                                                                 D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
971                                                                 DWORD MultisampleQuality, BOOL Discard,
972                                                                 IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
973     HRESULT hr;
974
975     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
976             "discard %#x, surface %p, shared_handle %p.\n",
977             iface, Width, Height, Format, MultiSample, MultisampleQuality,
978             Discard, ppSurface, pSharedHandle);
979
980     hr = IDirect3DDevice9Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, Discard,
981             0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample, MultisampleQuality);
982
983     return hr;
984 }
985
986
987 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestinationSurface, CONST POINT* pDestPoint) {
988     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
989     HRESULT hr;
990
991     TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
992             iface, pSourceSurface, pSourceRect, pDestinationSurface, pDestPoint);
993
994     wined3d_mutex_lock();
995     hr = IWineD3DDevice_UpdateSurface(This->WineD3DDevice, ((IDirect3DSurface9Impl *)pSourceSurface)->wineD3DSurface, pSourceRect, ((IDirect3DSurface9Impl *)pDestinationSurface)->wineD3DSurface, pDestPoint);
996     wined3d_mutex_unlock();
997
998     return hr;
999 }
1000
1001 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateTexture(LPDIRECT3DDEVICE9EX iface, IDirect3DBaseTexture9* pSourceTexture, IDirect3DBaseTexture9* pDestinationTexture) {
1002     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1003     HRESULT hr;
1004
1005     TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, pSourceTexture, pDestinationTexture);
1006
1007     wined3d_mutex_lock();
1008     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture9Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture9Impl *)pDestinationTexture)->wineD3DBaseTexture);
1009     wined3d_mutex_unlock();
1010
1011     return hr;
1012 }
1013
1014 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTargetData(IDirect3DDevice9Ex *iface,
1015         IDirect3DSurface9 *pRenderTarget, IDirect3DSurface9 *pDestSurface)
1016 {
1017     IDirect3DSurface9Impl *renderTarget = (IDirect3DSurface9Impl *)pRenderTarget;
1018     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
1019     HRESULT hr;
1020
1021     TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, pRenderTarget, pDestSurface);
1022
1023     wined3d_mutex_lock();
1024     hr = IWineD3DSurface_BltFast(destSurface->wineD3DSurface, 0, 0, renderTarget->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
1025     wined3d_mutex_unlock();
1026
1027     return hr;
1028 }
1029
1030 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBufferData(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSurface9* pDestSurface) {
1031     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1032     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
1033     HRESULT hr;
1034
1035     TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, iSwapChain, pDestSurface);
1036
1037     wined3d_mutex_lock();
1038     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, iSwapChain, destSurface->wineD3DSurface);
1039     wined3d_mutex_unlock();
1040
1041     return hr;
1042 }
1043
1044 static HRESULT WINAPI IDirect3DDevice9Impl_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *pSourceSurface,
1045         const RECT *pSourceRect, IDirect3DSurface9 *pDestSurface, const RECT *pDestRect, D3DTEXTUREFILTERTYPE Filter)
1046 {
1047     IDirect3DSurface9Impl *src = (IDirect3DSurface9Impl *) pSourceSurface;
1048     IDirect3DSurface9Impl *dst = (IDirect3DSurface9Impl *) pDestSurface;
1049     HRESULT hr;
1050
1051     TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
1052             iface, pSourceSurface, pSourceRect, pDestSurface, pDestRect, Filter);
1053
1054     wined3d_mutex_lock();
1055     hr = IWineD3DSurface_Blt(dst->wineD3DSurface, pDestRect, src->wineD3DSurface, pSourceRect, 0, NULL, Filter);
1056     wined3d_mutex_unlock();
1057
1058     return hr;
1059 }
1060
1061 static HRESULT WINAPI IDirect3DDevice9Impl_ColorFill(IDirect3DDevice9Ex *iface,
1062         IDirect3DSurface9 *pSurface, const RECT *pRect, D3DCOLOR color)
1063 {
1064     const WINED3DCOLORVALUE c =
1065     {
1066         ((color >> 16) & 0xff) / 255.0f,
1067         ((color >>  8) & 0xff) / 255.0f,
1068         (color & 0xff) / 255.0f,
1069         ((color >> 24) & 0xff) / 255.0f,
1070     };
1071     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1072     IDirect3DSurface9Impl *surface = (IDirect3DSurface9Impl *)pSurface;
1073     struct wined3d_resource_desc desc;
1074     HRESULT hr;
1075
1076     TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, pSurface, pRect, color);
1077
1078     wined3d_mutex_lock();
1079
1080     IWineD3DSurface_GetDesc(surface->wineD3DSurface, &desc);
1081
1082     /* This method is only allowed with surfaces that are render targets, or
1083      * offscreen plain surfaces in D3DPOOL_DEFAULT. */
1084     if (!(desc.usage & WINED3DUSAGE_RENDERTARGET) && desc.pool != WINED3DPOOL_DEFAULT)
1085     {
1086         wined3d_mutex_unlock();
1087         WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1088         return D3DERR_INVALIDCALL;
1089     }
1090
1091     /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1092     hr = IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, pRect, &c);
1093
1094     wined3d_mutex_unlock();
1095
1096     return hr;
1097 }
1098
1099 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
1100     HRESULT hr;
1101
1102     TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1103             iface, Width, Height, Format, Pool, ppSurface, pSharedHandle);
1104
1105     if(Pool == D3DPOOL_MANAGED ){
1106         FIXME("Attempting to create a managed offscreen plain surface\n");
1107         return D3DERR_INVALIDCALL;
1108     }
1109         /*
1110         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
1111         but then...
1112         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
1113         Why, their always lockable?
1114         should I change the usage to dynamic?
1115         */
1116     hr = IDirect3DDevice9Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE /* Discard */,
1117             0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */, (WINED3DPOOL)Pool, D3DMULTISAMPLE_NONE,
1118             0 /* MultisampleQuality */);
1119
1120     return hr;
1121 }
1122
1123 /* TODO: move to wineD3D */
1124 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9EX iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
1125     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1126     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
1127     HRESULT hr;
1128
1129     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, pRenderTarget);
1130
1131     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1132     {
1133         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1134         return D3DERR_INVALIDCALL;
1135     }
1136
1137     wined3d_mutex_lock();
1138     hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? pSurface->wineD3DSurface : NULL, TRUE);
1139     wined3d_mutex_unlock();
1140
1141     return hr;
1142 }
1143
1144 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTarget(IDirect3DDevice9Ex *iface,
1145         DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget)
1146 {
1147     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1148     IWineD3DSurface *pRenderTarget;
1149     HRESULT hr;
1150
1151     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, ppRenderTarget);
1152
1153     if (ppRenderTarget == NULL) {
1154         return D3DERR_INVALIDCALL;
1155     }
1156
1157     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1158     {
1159         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1160         return D3DERR_INVALIDCALL;
1161     }
1162
1163     wined3d_mutex_lock();
1164
1165     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
1166
1167     if (FAILED(hr))
1168     {
1169         FIXME("Call to IWineD3DDevice_GetRenderTarget failed, hr %#x\n", hr);
1170     }
1171     else if (!pRenderTarget)
1172     {
1173         *ppRenderTarget = NULL;
1174     }
1175     else
1176     {
1177         *ppRenderTarget = IWineD3DSurface_GetParent(pRenderTarget);
1178         IDirect3DSurface9_AddRef(*ppRenderTarget);
1179         IWineD3DSurface_Release(pRenderTarget);
1180     }
1181
1182     wined3d_mutex_unlock();
1183
1184     return hr;
1185 }
1186
1187 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pZStencilSurface) {
1188     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1189     IDirect3DSurface9Impl *pSurface;
1190     HRESULT hr;
1191
1192     TRACE("iface %p, depth_stencil %p.\n", iface, pZStencilSurface);
1193
1194     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
1195
1196     wined3d_mutex_lock();
1197     hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL==pSurface ? NULL : pSurface->wineD3DSurface);
1198     wined3d_mutex_unlock();
1199
1200     return hr;
1201 }
1202
1203 static HRESULT WINAPI IDirect3DDevice9Impl_GetDepthStencilSurface(IDirect3DDevice9Ex *iface,
1204         IDirect3DSurface9 **ppZStencilSurface)
1205 {
1206     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1207     IWineD3DSurface *pZStencilSurface;
1208     HRESULT hr;
1209
1210     TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1211
1212     if(ppZStencilSurface == NULL){
1213         return D3DERR_INVALIDCALL;
1214     }
1215
1216     wined3d_mutex_lock();
1217     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
1218     if (SUCCEEDED(hr))
1219     {
1220         *ppZStencilSurface = IWineD3DSurface_GetParent(pZStencilSurface);
1221         IDirect3DSurface9_AddRef(*ppZStencilSurface);
1222         IWineD3DSurface_Release(pZStencilSurface);
1223     }
1224     else
1225     {
1226         if (hr != WINED3DERR_NOTFOUND)
1227                 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1228         *ppZStencilSurface = NULL;
1229     }
1230     wined3d_mutex_unlock();
1231
1232     return hr;
1233 }
1234
1235 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9EX iface) {
1236     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1237     HRESULT hr;
1238
1239     TRACE("iface %p.\n", iface);
1240
1241     wined3d_mutex_lock();
1242     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1243     wined3d_mutex_unlock();
1244
1245     return hr;
1246 }
1247
1248 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9EX iface) {
1249     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1250     HRESULT hr;
1251
1252     TRACE("iface %p.\n", iface);
1253
1254     wined3d_mutex_lock();
1255     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1256     wined3d_mutex_unlock();
1257
1258     return hr;
1259 }
1260
1261 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9EX iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
1262     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1263     HRESULT hr;
1264
1265     TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1266             iface, Count, pRects, Flags, Color, Z, Stencil);
1267
1268     /* Note: D3DRECT is compatible with WINED3DRECT */
1269     wined3d_mutex_lock();
1270     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (const RECT *)pRects, Flags, Color, Z, Stencil);
1271     wined3d_mutex_unlock();
1272
1273     return hr;
1274 }
1275
1276 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
1277     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1278     HRESULT hr;
1279
1280     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1281
1282     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1283     wined3d_mutex_lock();
1284     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1285     wined3d_mutex_unlock();
1286
1287     return hr;
1288 }
1289
1290 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
1291     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1292     HRESULT hr;
1293
1294     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1295
1296     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1297     wined3d_mutex_lock();
1298     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1299     wined3d_mutex_unlock();
1300
1301     return hr;
1302 }
1303
1304 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
1305     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1306     HRESULT hr;
1307
1308     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1309
1310     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1311     wined3d_mutex_lock();
1312     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1313     wined3d_mutex_unlock();
1314
1315     return hr;
1316 }
1317
1318 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9EX iface, CONST D3DVIEWPORT9* pViewport) {
1319     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1320     HRESULT hr;
1321
1322     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1323
1324     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
1325     wined3d_mutex_lock();
1326     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1327     wined3d_mutex_unlock();
1328
1329     return hr;
1330 }
1331
1332 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9EX iface, D3DVIEWPORT9* pViewport) {
1333     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1334     HRESULT hr;
1335
1336     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1337
1338     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
1339     wined3d_mutex_lock();
1340     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1341     wined3d_mutex_unlock();
1342
1343     return hr;
1344 }
1345
1346 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9EX iface, CONST D3DMATERIAL9* pMaterial) {
1347     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1348     HRESULT hr;
1349
1350     TRACE("iface %p, material %p.\n", iface, pMaterial);
1351
1352     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
1353     wined3d_mutex_lock();
1354     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1355     wined3d_mutex_unlock();
1356
1357     return hr;
1358 }
1359
1360 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9EX iface, D3DMATERIAL9* pMaterial) {
1361     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1362     HRESULT hr;
1363
1364     TRACE("iface %p, material %p.\n", iface, pMaterial);
1365
1366     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
1367     wined3d_mutex_lock();
1368     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1369     wined3d_mutex_unlock();
1370
1371     return hr;
1372 }
1373
1374 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST D3DLIGHT9* pLight) {
1375     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1376     HRESULT hr;
1377
1378     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1379
1380     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
1381     wined3d_mutex_lock();
1382     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1383     wined3d_mutex_unlock();
1384
1385     return hr;
1386 }
1387
1388 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, D3DLIGHT9* pLight) {
1389     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1390     HRESULT hr;
1391
1392     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1393
1394     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
1395     wined3d_mutex_lock();
1396     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1397     wined3d_mutex_unlock();
1398
1399     return hr;
1400 }
1401
1402 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL Enable) {
1403     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1404     HRESULT hr;
1405
1406     TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1407
1408     wined3d_mutex_lock();
1409     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1410     wined3d_mutex_unlock();
1411
1412     return hr;
1413 }
1414
1415 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL* pEnable) {
1416     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1417     HRESULT hr;
1418
1419     TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1420
1421     wined3d_mutex_lock();
1422     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1423     wined3d_mutex_unlock();
1424
1425     return hr;
1426 }
1427
1428 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST float* pPlane) {
1429     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1430     HRESULT hr;
1431
1432     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1433
1434     wined3d_mutex_lock();
1435     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1436     wined3d_mutex_unlock();
1437
1438     return hr;
1439 }
1440
1441 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, float* pPlane) {
1442     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1443     HRESULT hr;
1444
1445     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1446
1447     wined3d_mutex_lock();
1448     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1449     wined3d_mutex_unlock();
1450
1451     return hr;
1452 }
1453
1454 static HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD Value) {
1455     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1456     HRESULT hr;
1457
1458     TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1459
1460     wined3d_mutex_lock();
1461     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1462     wined3d_mutex_unlock();
1463
1464     return hr;
1465 }
1466
1467 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
1468     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1469     HRESULT hr;
1470
1471     TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1472
1473     wined3d_mutex_lock();
1474     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1475     wined3d_mutex_unlock();
1476
1477     return hr;
1478 }
1479
1480 static HRESULT WINAPI IDirect3DDevice9Impl_CreateStateBlock(IDirect3DDevice9Ex *iface,
1481         D3DSTATEBLOCKTYPE type, IDirect3DStateBlock9 **stateblock)
1482 {
1483     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1484     IDirect3DStateBlock9Impl *object;
1485     HRESULT hr;
1486
1487     TRACE("iface %p, type %#x, stateblock %p.\n", iface, type, stateblock);
1488
1489     if (type != D3DSBT_ALL && type != D3DSBT_PIXELSTATE && type != D3DSBT_VERTEXSTATE)
1490     {
1491         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL.\n");
1492         return D3DERR_INVALIDCALL;
1493     }
1494
1495     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1496     if (!object)
1497     {
1498         ERR("Failed to allocate stateblock memory.\n");
1499         return E_OUTOFMEMORY;
1500     }
1501
1502     hr = stateblock_init(object, This, type, NULL);
1503     if (FAILED(hr))
1504     {
1505         WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1506         HeapFree(GetProcessHeap(), 0, object);
1507         return hr;
1508     }
1509
1510     TRACE("Created stateblock %p.\n", object);
1511     *stateblock = (IDirect3DStateBlock9 *)object;
1512
1513     return D3D_OK;
1514 }
1515
1516 static HRESULT WINAPI IDirect3DDevice9Impl_BeginStateBlock(IDirect3DDevice9Ex *iface)
1517 {
1518     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1519     HRESULT hr;
1520
1521     TRACE("iface %p.\n", iface);
1522
1523     wined3d_mutex_lock();
1524     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1525     wined3d_mutex_unlock();
1526
1527     return hr;
1528 }
1529
1530 static HRESULT WINAPI IDirect3DDevice9Impl_EndStateBlock(IDirect3DDevice9Ex *iface, IDirect3DStateBlock9 **stateblock)
1531 {
1532     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1533     struct wined3d_stateblock *wined3d_stateblock;
1534     IDirect3DStateBlock9Impl *object;
1535     HRESULT hr;
1536
1537     TRACE("iface %p, stateblock %p.\n", iface, stateblock);
1538
1539     wined3d_mutex_lock();
1540     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice, &wined3d_stateblock);
1541     wined3d_mutex_unlock();
1542     if (FAILED(hr))
1543     {
1544        WARN("IWineD3DDevice_EndStateBlock() failed, hr %#x.\n", hr);
1545        return hr;
1546     }
1547
1548     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1549     if (!object)
1550     {
1551         ERR("Failed to allocate stateblock memory.\n");
1552         wined3d_mutex_lock();
1553         wined3d_stateblock_decref(wined3d_stateblock);
1554         wined3d_mutex_unlock();
1555         return E_OUTOFMEMORY;
1556     }
1557
1558     hr = stateblock_init(object, This, 0, wined3d_stateblock);
1559     if (FAILED(hr))
1560     {
1561         WARN("Failed to initialize stateblock, hr %#x.\n", hr);
1562         wined3d_mutex_lock();
1563         wined3d_stateblock_decref(wined3d_stateblock);
1564         wined3d_mutex_unlock();
1565         HeapFree(GetProcessHeap(), 0, object);
1566         return hr;
1567     }
1568
1569     TRACE("Created stateblock %p.\n", object);
1570     *stateblock = (IDirect3DStateBlock9 *)object;
1571
1572     return D3D_OK;
1573 }
1574
1575 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9EX iface, CONST D3DCLIPSTATUS9* pClipStatus) {
1576     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1577     HRESULT hr;
1578
1579     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1580
1581     wined3d_mutex_lock();
1582     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1583     wined3d_mutex_unlock();
1584
1585     return hr;
1586 }
1587
1588 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9EX iface, D3DCLIPSTATUS9* pClipStatus) {
1589     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1590     HRESULT hr;
1591
1592     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1593
1594     wined3d_mutex_lock();
1595     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1596     wined3d_mutex_unlock();
1597
1598     return hr;
1599 }
1600
1601 static HRESULT WINAPI IDirect3DDevice9Impl_GetTexture(IDirect3DDevice9Ex *iface,
1602         DWORD Stage, IDirect3DBaseTexture9 **ppTexture)
1603 {
1604     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1605     IWineD3DBaseTexture *retTexture = NULL;
1606     HRESULT hr;
1607
1608     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1609
1610     if(ppTexture == NULL){
1611         return D3DERR_INVALIDCALL;
1612     }
1613
1614     wined3d_mutex_lock();
1615     hr = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1616     if (SUCCEEDED(hr) && retTexture)
1617     {
1618         *ppTexture = IWineD3DBaseTexture_GetParent(retTexture);
1619         IWineD3DBaseTexture_AddRef(*ppTexture);
1620         IWineD3DBaseTexture_Release(retTexture);
1621     }
1622     else
1623     {
1624         if (FAILED(hr))
1625         {
1626             WARN("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1627         }
1628         *ppTexture = NULL;
1629     }
1630     wined3d_mutex_unlock();
1631
1632     return hr;
1633 }
1634
1635 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9EX iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
1636     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1637     HRESULT hr;
1638
1639     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1640
1641     wined3d_mutex_lock();
1642     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1643                                    pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture);
1644     wined3d_mutex_unlock();
1645
1646     return hr;
1647 }
1648
1649 static const WINED3DTEXTURESTAGESTATETYPE tss_lookup[] =
1650 {
1651     WINED3DTSS_FORCE_DWORD,             /*  0, unused */
1652     WINED3DTSS_COLOROP,                 /*  1, D3DTSS_COLOROP */
1653     WINED3DTSS_COLORARG1,               /*  2, D3DTSS_COLORARG1 */
1654     WINED3DTSS_COLORARG2,               /*  3, D3DTSS_COLORARG2 */
1655     WINED3DTSS_ALPHAOP,                 /*  4, D3DTSS_ALPHAOP */
1656     WINED3DTSS_ALPHAARG1,               /*  5, D3DTSS_ALPHAARG1 */
1657     WINED3DTSS_ALPHAARG2,               /*  6, D3DTSS_ALPHAARG2 */
1658     WINED3DTSS_BUMPENVMAT00,            /*  7, D3DTSS_BUMPENVMAT00 */
1659     WINED3DTSS_BUMPENVMAT01,            /*  8, D3DTSS_BUMPENVMAT01 */
1660     WINED3DTSS_BUMPENVMAT10,            /*  9, D3DTSS_BUMPENVMAT10 */
1661     WINED3DTSS_BUMPENVMAT11,            /* 10, D3DTSS_BUMPENVMAT11 */
1662     WINED3DTSS_TEXCOORDINDEX,           /* 11, D3DTSS_TEXCOORDINDEX */
1663     WINED3DTSS_FORCE_DWORD,             /* 12, unused */
1664     WINED3DTSS_FORCE_DWORD,             /* 13, unused */
1665     WINED3DTSS_FORCE_DWORD,             /* 14, unused */
1666     WINED3DTSS_FORCE_DWORD,             /* 15, unused */
1667     WINED3DTSS_FORCE_DWORD,             /* 16, unused */
1668     WINED3DTSS_FORCE_DWORD,             /* 17, unused */
1669     WINED3DTSS_FORCE_DWORD,             /* 18, unused */
1670     WINED3DTSS_FORCE_DWORD,             /* 19, unused */
1671     WINED3DTSS_FORCE_DWORD,             /* 20, unused */
1672     WINED3DTSS_FORCE_DWORD,             /* 21, unused */
1673     WINED3DTSS_BUMPENVLSCALE,           /* 22, D3DTSS_BUMPENVLSCALE */
1674     WINED3DTSS_BUMPENVLOFFSET,          /* 23, D3DTSS_BUMPENVLOFFSET */
1675     WINED3DTSS_TEXTURETRANSFORMFLAGS,   /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1676     WINED3DTSS_FORCE_DWORD,             /* 25, unused */
1677     WINED3DTSS_COLORARG0,               /* 26, D3DTSS_COLORARG0 */
1678     WINED3DTSS_ALPHAARG0,               /* 27, D3DTSS_ALPHAARG0 */
1679     WINED3DTSS_RESULTARG,               /* 28, D3DTSS_RESULTARG */
1680     WINED3DTSS_FORCE_DWORD,             /* 29, unused */
1681     WINED3DTSS_FORCE_DWORD,             /* 30, unused */
1682     WINED3DTSS_FORCE_DWORD,             /* 31, unused */
1683     WINED3DTSS_CONSTANT,                /* 32, D3DTSS_CONSTANT */
1684 };
1685
1686 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1687 {
1688     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1689     HRESULT hr;
1690
1691     TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1692
1693     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1694     {
1695         WARN("Invalid Type %#x passed.\n", Type);
1696         return D3D_OK;
1697     }
1698
1699     wined3d_mutex_lock();
1700     hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], pValue);
1701     wined3d_mutex_unlock();
1702
1703     return hr;
1704 }
1705
1706 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1707 {
1708     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1709     HRESULT hr;
1710
1711     TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1712
1713     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1714     {
1715         WARN("Invalid Type %#x passed.\n", Type);
1716         return D3D_OK;
1717     }
1718
1719     wined3d_mutex_lock();
1720     hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], Value);
1721     wined3d_mutex_unlock();
1722
1723     return hr;
1724 }
1725
1726 static HRESULT WINAPI IDirect3DDevice9Impl_GetSamplerState(IDirect3DDevice9Ex *iface, DWORD Sampler,
1727         D3DSAMPLERSTATETYPE Type, DWORD *pValue)
1728 {
1729     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1730     HRESULT hr;
1731
1732     TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, Sampler, Type, pValue);
1733
1734     wined3d_mutex_lock();
1735     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
1736     wined3d_mutex_unlock();
1737
1738     return hr;
1739 }
1740
1741 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9EX iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
1742     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1743     HRESULT hr;
1744
1745     TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, Sampler, Type, Value);
1746
1747     wined3d_mutex_lock();
1748     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);
1749     wined3d_mutex_unlock();
1750
1751     return hr;
1752 }
1753
1754 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9EX iface, DWORD* pNumPasses) {
1755     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1756     HRESULT hr;
1757
1758     TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1759
1760     wined3d_mutex_lock();
1761     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1762     wined3d_mutex_unlock();
1763
1764     return hr;
1765 }
1766
1767 static HRESULT WINAPI IDirect3DDevice9Impl_SetPaletteEntries(IDirect3DDevice9Ex *iface, UINT PaletteNumber,
1768         const PALETTEENTRY *pEntries)
1769 {
1770     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1771     HRESULT hr;
1772
1773     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1774
1775     wined3d_mutex_lock();
1776     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1777     wined3d_mutex_unlock();
1778
1779     return hr;
1780 }
1781
1782 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1783     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1784     HRESULT hr;
1785
1786     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1787
1788     wined3d_mutex_lock();
1789     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1790     wined3d_mutex_unlock();
1791
1792     return hr;
1793 }
1794
1795 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber) {
1796     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1797     HRESULT hr;
1798
1799     TRACE("iface %p, palette_idx %u.\n", iface, PaletteNumber);
1800
1801     wined3d_mutex_lock();
1802     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1803     wined3d_mutex_unlock();
1804
1805     return hr;
1806 }
1807
1808 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT* PaletteNumber) {
1809     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1810     HRESULT hr;
1811
1812     TRACE("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1813
1814     wined3d_mutex_lock();
1815     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1816     wined3d_mutex_unlock();
1817
1818     return hr;
1819 }
1820
1821 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9EX iface, CONST RECT* pRect) {
1822     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1823     HRESULT hr;
1824
1825     TRACE("iface %p, rect %p.\n", iface, pRect);
1826
1827     wined3d_mutex_lock();
1828     hr = IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);
1829     wined3d_mutex_unlock();
1830
1831     return hr;
1832 }
1833
1834 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9EX iface, RECT* pRect) {
1835     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1836     HRESULT hr;
1837
1838     TRACE("iface %p, rect %p.\n", iface, pRect);
1839
1840     wined3d_mutex_lock();
1841     hr = IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);
1842     wined3d_mutex_unlock();
1843
1844     return hr;
1845 }
1846
1847 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface, BOOL bSoftware) {
1848     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1849     HRESULT hr;
1850
1851     TRACE("iface %p, software %#x.\n", iface, bSoftware);
1852
1853     wined3d_mutex_lock();
1854     hr = IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
1855     wined3d_mutex_unlock();
1856
1857     return hr;
1858 }
1859
1860 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface) {
1861     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1862     BOOL ret;
1863
1864     TRACE("iface %p.\n", iface);
1865
1866     wined3d_mutex_lock();
1867     ret = IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
1868     wined3d_mutex_unlock();
1869
1870     return ret;
1871 }
1872
1873 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9EX iface, float nSegments) {
1874     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1875     HRESULT hr;
1876
1877     TRACE("iface %p, segment_count %.8e.\n", iface, nSegments);
1878
1879     wined3d_mutex_lock();
1880     hr = IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
1881     wined3d_mutex_unlock();
1882
1883     return hr;
1884 }
1885
1886 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9EX iface) {
1887     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1888     float ret;
1889
1890     TRACE("iface %p.\n", iface);
1891
1892     wined3d_mutex_lock();
1893     ret = IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
1894     wined3d_mutex_unlock();
1895
1896     return ret;
1897 }
1898
1899 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(IDirect3DDevice9Ex *iface, D3DPRIMITIVETYPE PrimitiveType,
1900         UINT StartVertex, UINT PrimitiveCount)
1901 {
1902     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1903     HRESULT hr;
1904
1905     TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1906             iface, PrimitiveType, StartVertex, PrimitiveCount);
1907
1908     wined3d_mutex_lock();
1909     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1910     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1911             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1912     wined3d_mutex_unlock();
1913
1914     return hr;
1915 }
1916
1917 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType,
1918                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
1919     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1920     HRESULT hr;
1921
1922     TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
1923             "vertex_count %u, start_idx %u, primitive_count %u.\n",
1924             iface, PrimitiveType, BaseVertexIndex, MinVertexIndex,
1925             NumVertices, startIndex, primCount);
1926
1927     wined3d_mutex_lock();
1928     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
1929     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1930     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1931             vertex_count_from_primitive_count(PrimitiveType, primCount));
1932     wined3d_mutex_unlock();
1933
1934     return hr;
1935 }
1936
1937 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitiveUP(IDirect3DDevice9Ex *iface, D3DPRIMITIVETYPE PrimitiveType,
1938         UINT PrimitiveCount, const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
1939 {
1940     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1941     HRESULT hr;
1942
1943     TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1944             iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1945
1946     wined3d_mutex_lock();
1947     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1948     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1949             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1950             pVertexStreamZeroData, VertexStreamZeroStride);
1951     wined3d_mutex_unlock();
1952
1953     return hr;
1954 }
1955
1956 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
1957                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
1958                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1959     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1960     HRESULT hr;
1961
1962     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1963             "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1964             iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1965             pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1966
1967     wined3d_mutex_lock();
1968     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1969     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1970             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1971             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1972     wined3d_mutex_unlock();
1973
1974     return hr;
1975 }
1976
1977 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9EX iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
1978     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1979     IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
1980     HRESULT hr;
1981     IDirect3DVertexBuffer9Impl *dest = (IDirect3DVertexBuffer9Impl *) pDestBuffer;
1982
1983     TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
1984             iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags);
1985
1986     wined3d_mutex_lock();
1987     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags, dest->fvf);
1988     wined3d_mutex_unlock();
1989
1990     return hr;
1991 }
1992
1993 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
1994         const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
1995 {
1996     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1997     IDirect3DVertexDeclaration9Impl *object;
1998     HRESULT hr;
1999
2000     TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
2001
2002     if (!declaration)
2003     {
2004         WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
2005         return D3DERR_INVALIDCALL;
2006     }
2007
2008     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2009     if (!object)
2010     {
2011         ERR("Failed to allocate vertex declaration memory.\n");
2012         return E_OUTOFMEMORY;
2013     }
2014
2015     hr = vertexdeclaration_init(object, This, elements);
2016     if (FAILED(hr))
2017     {
2018         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2019         HeapFree(GetProcessHeap(), 0, object);
2020         return hr;
2021     }
2022
2023     TRACE("Created vertex declaration %p.\n", object);
2024     *declaration = (IDirect3DVertexDeclaration9 *)object;
2025
2026     return D3D_OK;
2027 }
2028
2029 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(IDirect3DDevice9Ex *iface,
2030         IDirect3DVertexDeclaration9 *declaration)
2031 {
2032     HRESULT hr;
2033
2034     TRACE("iface %p, declaration %p.\n", iface, declaration);
2035
2036     wined3d_mutex_lock();
2037     hr = IWineD3DDevice_SetVertexDeclaration(((IDirect3DDevice9Impl *)iface)->WineD3DDevice,
2038             declaration ? ((IDirect3DVertexDeclaration9Impl *)declaration)->wineD3DVertexDeclaration : NULL);
2039     wined3d_mutex_unlock();
2040
2041     return hr;
2042 }
2043
2044 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(IDirect3DDevice9Ex *iface,
2045         IDirect3DVertexDeclaration9 **declaration)
2046 {
2047     struct wined3d_vertex_declaration *wined3d_declaration = NULL;
2048     HRESULT hr;
2049
2050     TRACE("iface %p, declaration %p.\n", iface, declaration);
2051
2052     if (!declaration) return D3DERR_INVALIDCALL;
2053
2054     wined3d_mutex_lock();
2055     hr = IWineD3DDevice_GetVertexDeclaration(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, &wined3d_declaration);
2056     if (SUCCEEDED(hr) && wined3d_declaration)
2057     {
2058         *declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2059         IDirect3DVertexDeclaration9_AddRef(*declaration);
2060         wined3d_vertex_declaration_decref(wined3d_declaration);
2061     }
2062     else
2063     {
2064         *declaration = NULL;
2065     }
2066     wined3d_mutex_unlock();
2067
2068     TRACE("Returning %p.\n", *declaration);
2069     return hr;
2070 }
2071
2072 static IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
2073     HRESULT hr;
2074     D3DVERTEXELEMENT9* elements = NULL;
2075     IDirect3DVertexDeclaration9* pDecl = NULL;
2076     int p, low, high; /* deliberately signed */
2077     IDirect3DVertexDeclaration9  **convertedDecls = This->convertedDecls;
2078
2079     TRACE("Searching for declaration for fvf %08x... ", fvf);
2080
2081     low = 0;
2082     high = This->numConvertedDecls - 1;
2083     while(low <= high) {
2084         p = (low + high) >> 1;
2085         TRACE("%d ", p);
2086         if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
2087             TRACE("found %p\n", convertedDecls[p]);
2088             return convertedDecls[p];
2089         } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
2090             low = p + 1;
2091         } else {
2092             high = p - 1;
2093         }
2094     }
2095     TRACE("not found. Creating and inserting at position %d.\n", low);
2096
2097     hr = vdecl_convert_fvf(fvf, &elements);
2098     if (hr != S_OK) return NULL;
2099
2100     hr = IDirect3DDevice9Impl_CreateVertexDeclaration((IDirect3DDevice9Ex *) This, elements, &pDecl);
2101     HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
2102     if (hr != S_OK) return NULL;
2103
2104     if(This->declArraySize == This->numConvertedDecls) {
2105         int grow = max(This->declArraySize / 2, 8);
2106         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2107                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2108         if(!convertedDecls) {
2109             /* This will destroy it */
2110             IDirect3DVertexDeclaration9_Release(pDecl);
2111             return NULL;
2112         }
2113         This->convertedDecls = convertedDecls;
2114         This->declArraySize += grow;
2115     }
2116
2117     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
2118     convertedDecls[low] = pDecl;
2119     This->numConvertedDecls++;
2120
2121     /* Will prevent the decl from being destroyed */
2122     ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
2123     IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
2124
2125     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
2126     return pDecl;
2127 }
2128
2129 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9EX iface, DWORD FVF) {
2130     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2131     IDirect3DVertexDeclaration9 *decl;
2132     HRESULT hr;
2133
2134     TRACE("iface %p, fvf %#x.\n", iface, FVF);
2135
2136     if (!FVF)
2137     {
2138         WARN("%#x is not a valid FVF\n", FVF);
2139         return D3D_OK;
2140     }
2141
2142     wined3d_mutex_lock();
2143     decl = getConvertedDecl(This, FVF);
2144     wined3d_mutex_unlock();
2145
2146     if (!decl)
2147     {
2148          /* Any situation when this should happen, except out of memory? */
2149          ERR("Failed to create a converted vertex declaration\n");
2150          return D3DERR_DRIVERINTERNALERROR;
2151     }
2152
2153     hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, decl);
2154     if (FAILED(hr)) ERR("Failed to set vertex declaration\n");
2155
2156     return hr;
2157 }
2158
2159 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(IDirect3DDevice9Ex *iface, DWORD *pFVF)
2160 {
2161     IDirect3DVertexDeclaration9 *decl;
2162     HRESULT hr;
2163
2164     TRACE("iface %p, fvf %p.\n", iface, pFVF);
2165
2166     hr = IDirect3DDevice9_GetVertexDeclaration(iface, &decl);
2167     if (FAILED(hr))
2168     {
2169         WARN("Failed to get vertex declaration, %#x\n", hr);
2170         *pFVF = 0;
2171         return hr;
2172     }
2173
2174     if (decl)
2175     {
2176         *pFVF = ((IDirect3DVertexDeclaration9Impl *)decl)->convFVF;
2177         IDirect3DVertexDeclaration9_Release(decl);
2178     }
2179     else
2180     {
2181         *pFVF = 0;
2182     }
2183
2184     TRACE("Returning FVF %#x\n", *pFVF);
2185
2186     return hr;
2187 }
2188
2189 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(IDirect3DDevice9Ex *iface,
2190         const DWORD *byte_code, IDirect3DVertexShader9 **shader)
2191 {
2192     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2193     IDirect3DVertexShader9Impl *object;
2194     HRESULT hr;
2195
2196     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2197
2198     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2199     if (!object)
2200     {
2201         ERR("Failed to allocate vertex shader memory.\n");
2202         return E_OUTOFMEMORY;
2203     }
2204
2205     hr = vertexshader_init(object, This, byte_code);
2206     if (FAILED(hr))
2207     {
2208         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2209         HeapFree(GetProcessHeap(), 0, object);
2210         return hr;
2211     }
2212
2213     TRACE("Created vertex shader %p.\n", object);
2214     *shader = (IDirect3DVertexShader9 *)object;
2215
2216     return D3D_OK;
2217 }
2218
2219 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShader(IDirect3DDevice9Ex *iface,
2220         IDirect3DVertexShader9 *shader)
2221 {
2222     HRESULT hr;
2223
2224     TRACE("iface %p, shader %p.\n", iface, shader);
2225
2226     wined3d_mutex_lock();
2227     hr =  IWineD3DDevice_SetVertexShader(((IDirect3DDevice9Impl *)iface)->WineD3DDevice,
2228             shader ? ((IDirect3DVertexShader9Impl *)shader)->wineD3DVertexShader : NULL);
2229     wined3d_mutex_unlock();
2230
2231     return hr;
2232 }
2233
2234 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShader(IDirect3DDevice9Ex *iface,
2235         IDirect3DVertexShader9 **shader)
2236 {
2237     IWineD3DVertexShader *wined3d_shader;
2238
2239     TRACE("iface %p, shader %p.\n", iface, shader);
2240
2241     wined3d_mutex_lock();
2242     wined3d_shader = IWineD3DDevice_GetVertexShader(((IDirect3DDevice9Impl *)iface)->WineD3DDevice);
2243     if (wined3d_shader)
2244     {
2245         *shader = IWineD3DVertexShader_GetParent(wined3d_shader);
2246         IDirect3DVertexShader9_AddRef(*shader);
2247         IWineD3DVertexShader_Release(wined3d_shader);
2248     }
2249     else
2250     {
2251         *shader = NULL;
2252     }
2253     wined3d_mutex_unlock();
2254
2255     TRACE("Returning %p.\n", *shader);
2256
2257     return D3D_OK;
2258 }
2259
2260 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2261         UINT reg_idx, const float *data, UINT count)
2262 {
2263     HRESULT hr;
2264
2265     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2266
2267     if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2268     {
2269         WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2270              reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2271         return D3DERR_INVALIDCALL;
2272     }
2273
2274     wined3d_mutex_lock();
2275     hr = IWineD3DDevice_SetVertexShaderConstantF(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2276     wined3d_mutex_unlock();
2277
2278     return hr;
2279 }
2280
2281 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantF(IDirect3DDevice9Ex *iface,
2282         UINT reg_idx, float *data, UINT count)
2283 {
2284     HRESULT hr;
2285
2286     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2287
2288     if (reg_idx + count > D3D9_MAX_VERTEX_SHADER_CONSTANTF)
2289     {
2290         WARN("Trying to access %u constants, but d3d9 only supports %u\n",
2291              reg_idx + count, D3D9_MAX_VERTEX_SHADER_CONSTANTF);
2292         return D3DERR_INVALIDCALL;
2293     }
2294
2295     wined3d_mutex_lock();
2296     hr = IWineD3DDevice_GetVertexShaderConstantF(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2297     wined3d_mutex_unlock();
2298
2299     return hr;
2300 }
2301
2302 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2303         UINT reg_idx, const int *data, UINT count)
2304 {
2305     HRESULT hr;
2306
2307     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2308
2309     wined3d_mutex_lock();
2310     hr = IWineD3DDevice_SetVertexShaderConstantI(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2311     wined3d_mutex_unlock();
2312
2313     return hr;
2314 }
2315
2316 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantI(IDirect3DDevice9Ex *iface,
2317         UINT reg_idx, int *data, UINT count)
2318 {
2319     HRESULT hr;
2320
2321     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2322
2323     wined3d_mutex_lock();
2324     hr = IWineD3DDevice_GetVertexShaderConstantI(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2325     wined3d_mutex_unlock();
2326
2327     return hr;
2328 }
2329
2330 static HRESULT WINAPI IDirect3DDevice9Impl_SetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2331         UINT reg_idx, const BOOL *data, UINT count)
2332 {
2333     HRESULT hr;
2334
2335     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2336
2337     wined3d_mutex_lock();
2338     hr = IWineD3DDevice_SetVertexShaderConstantB(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2339     wined3d_mutex_unlock();
2340
2341     return hr;
2342 }
2343
2344 static HRESULT WINAPI IDirect3DDevice9Impl_GetVertexShaderConstantB(IDirect3DDevice9Ex *iface,
2345         UINT reg_idx, BOOL *data, UINT count)
2346 {
2347     HRESULT hr;
2348
2349     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2350
2351     wined3d_mutex_lock();
2352     hr = IWineD3DDevice_GetVertexShaderConstantB(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2353     wined3d_mutex_unlock();
2354
2355     return hr;
2356 }
2357
2358 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
2359     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2360     HRESULT hr;
2361
2362     TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
2363             iface, StreamNumber, pStreamData, OffsetInBytes, Stride);
2364
2365     wined3d_mutex_lock();
2366     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2367             pStreamData ? ((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer : NULL,
2368             OffsetInBytes, Stride);
2369     wined3d_mutex_unlock();
2370
2371     return hr;
2372 }
2373
2374 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(IDirect3DDevice9Ex *iface,
2375         UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride)
2376 {
2377     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2378     struct wined3d_buffer *retStream = NULL;
2379     HRESULT hr;
2380
2381     TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
2382             iface, StreamNumber, pStream, OffsetInBytes, pStride);
2383
2384     if(pStream == NULL){
2385         return D3DERR_INVALIDCALL;
2386     }
2387
2388     wined3d_mutex_lock();
2389     hr = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, OffsetInBytes, pStride);
2390     if (SUCCEEDED(hr) && retStream)
2391     {
2392         *pStream = wined3d_buffer_get_parent(retStream);
2393         IDirect3DVertexBuffer9_AddRef(*pStream);
2394         wined3d_buffer_decref(retStream);
2395     }
2396     else
2397     {
2398         if (FAILED(hr))
2399         {
2400             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
2401         }
2402         *pStream = NULL;
2403     }
2404     wined3d_mutex_unlock();
2405
2406     return hr;
2407 }
2408
2409 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT StreamNumber,
2410         UINT Divider)
2411 {
2412     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2413     HRESULT hr;
2414
2415     TRACE("iface %p, stream_idx %u, freq %u.\n", iface, StreamNumber, Divider);
2416
2417     wined3d_mutex_lock();
2418     hr = IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
2419     wined3d_mutex_unlock();
2420
2421     return hr;
2422 }
2423
2424 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, UINT* Divider) {
2425     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2426     HRESULT hr;
2427
2428     TRACE("iface %p, stream_idx %u, freq %p.\n", iface, StreamNumber, Divider);
2429
2430     wined3d_mutex_lock();
2431     hr = IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
2432     wined3d_mutex_unlock();
2433
2434     return hr;
2435 }
2436
2437 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9EX iface, IDirect3DIndexBuffer9* pIndexData) {
2438     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2439     HRESULT hr;
2440     IDirect3DIndexBuffer9Impl *ib = (IDirect3DIndexBuffer9Impl *) pIndexData;
2441
2442     TRACE("iface %p, buffer %p.\n", iface, pIndexData);
2443
2444     wined3d_mutex_lock();
2445     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2446             ib ? ib->wineD3DIndexBuffer : NULL,
2447             ib ? ib->format : WINED3DFMT_UNKNOWN);
2448     wined3d_mutex_unlock();
2449
2450     return hr;
2451 }
2452
2453 static HRESULT WINAPI IDirect3DDevice9Impl_GetIndices(IDirect3DDevice9Ex *iface, IDirect3DIndexBuffer9 **ppIndexData)
2454 {
2455     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2456     struct wined3d_buffer *retIndexData = NULL;
2457     HRESULT hr;
2458
2459     TRACE("iface %p, buffer %p.\n", iface, ppIndexData);
2460
2461     if(ppIndexData == NULL){
2462         return D3DERR_INVALIDCALL;
2463     }
2464
2465     wined3d_mutex_lock();
2466     hr = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2467     if (SUCCEEDED(hr) && retIndexData)
2468     {
2469         *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2470         IDirect3DIndexBuffer9_AddRef(*ppIndexData);
2471         wined3d_buffer_decref(retIndexData);
2472     }
2473     else
2474     {
2475         if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2476         *ppIndexData = NULL;
2477     }
2478     wined3d_mutex_unlock();
2479
2480     return hr;
2481 }
2482
2483 static HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(IDirect3DDevice9Ex *iface,
2484         const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2485 {
2486     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2487     IDirect3DPixelShader9Impl *object;
2488     HRESULT hr;
2489
2490     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2491
2492     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2493     if (!object)
2494     {
2495         FIXME("Failed to allocate pixel shader memory.\n");
2496         return E_OUTOFMEMORY;
2497     }
2498
2499     hr = pixelshader_init(object, This, byte_code);
2500     if (FAILED(hr))
2501     {
2502         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2503         HeapFree(GetProcessHeap(), 0, object);
2504         return hr;
2505     }
2506
2507     TRACE("Created pixel shader %p.\n", object);
2508     *shader = (IDirect3DPixelShader9 *)object;
2509
2510     return D3D_OK;
2511 }
2512
2513 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShader(IDirect3DDevice9Ex *iface,
2514         IDirect3DPixelShader9 *shader)
2515 {
2516     HRESULT hr;
2517
2518     TRACE("iface %p, shader %p.\n", iface, shader);
2519
2520     wined3d_mutex_lock();
2521     hr = IWineD3DDevice_SetPixelShader(((IDirect3DDevice9Impl *)iface)->WineD3DDevice,
2522             shader ? ((IDirect3DPixelShader9Impl *)shader)->wineD3DPixelShader : NULL);
2523     wined3d_mutex_unlock();
2524
2525     return hr;
2526 }
2527
2528 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShader(IDirect3DDevice9Ex *iface,
2529         IDirect3DPixelShader9 **shader)
2530 {
2531     IWineD3DPixelShader *wined3d_shader;
2532
2533     TRACE("iface %p, shader %p.\n", iface, shader);
2534
2535     if (!shader) return D3DERR_INVALIDCALL;
2536
2537     wined3d_mutex_lock();
2538     wined3d_shader = IWineD3DDevice_GetPixelShader(((IDirect3DDevice9Impl *)iface)->WineD3DDevice);
2539     if (wined3d_shader)
2540     {
2541         *shader = IWineD3DPixelShader_GetParent(wined3d_shader);
2542         IDirect3DPixelShader9_AddRef(*shader);
2543         IWineD3DPixelShader_Release(wined3d_shader);
2544     }
2545     else
2546     {
2547         *shader = NULL;
2548     }
2549     wined3d_mutex_unlock();
2550
2551     TRACE("Returning %p.\n", *shader);
2552
2553     return D3D_OK;
2554 }
2555
2556 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2557         UINT reg_idx, const float *data, UINT count)
2558 {
2559     HRESULT hr;
2560
2561     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2562
2563     wined3d_mutex_lock();
2564     hr = IWineD3DDevice_SetPixelShaderConstantF(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2565     wined3d_mutex_unlock();
2566
2567     return hr;
2568 }
2569
2570 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantF(IDirect3DDevice9Ex *iface,
2571         UINT reg_idx, float *data, UINT count)
2572 {
2573     HRESULT hr;
2574
2575     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2576
2577     wined3d_mutex_lock();
2578     hr = IWineD3DDevice_GetPixelShaderConstantF(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2579     wined3d_mutex_unlock();
2580
2581     return hr;
2582 }
2583
2584 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2585         UINT reg_idx, const int *data, UINT count)
2586 {
2587     HRESULT hr;
2588
2589     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2590
2591     wined3d_mutex_lock();
2592     hr = IWineD3DDevice_SetPixelShaderConstantI(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2593     wined3d_mutex_unlock();
2594
2595     return hr;
2596 }
2597
2598 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantI(IDirect3DDevice9Ex *iface,
2599         UINT reg_idx, int *data, UINT count)
2600 {
2601     HRESULT hr;
2602
2603     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2604
2605     wined3d_mutex_lock();
2606     hr = IWineD3DDevice_GetPixelShaderConstantI(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2607     wined3d_mutex_unlock();
2608
2609     return hr;
2610 }
2611
2612 static HRESULT WINAPI IDirect3DDevice9Impl_SetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2613         UINT reg_idx, const BOOL *data, UINT count)
2614 {
2615     HRESULT hr;
2616
2617     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2618
2619     wined3d_mutex_lock();
2620     hr = IWineD3DDevice_SetPixelShaderConstantB(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2621     wined3d_mutex_unlock();
2622
2623     return hr;
2624 }
2625
2626 static HRESULT WINAPI IDirect3DDevice9Impl_GetPixelShaderConstantB(IDirect3DDevice9Ex *iface,
2627         UINT reg_idx, BOOL *data, UINT count)
2628 {
2629     HRESULT hr;
2630
2631     TRACE("iface %p, reg_idx %u, data %p, count %u.\n", iface, reg_idx, data, count);
2632
2633     wined3d_mutex_lock();
2634     hr = IWineD3DDevice_GetPixelShaderConstantB(((IDirect3DDevice9Impl *)iface)->WineD3DDevice, reg_idx, data, count);
2635     wined3d_mutex_unlock();
2636
2637     return hr;
2638 }
2639
2640 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2641     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2642     HRESULT hr;
2643
2644     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2645             iface, Handle, pNumSegs, pRectPatchInfo);
2646
2647     wined3d_mutex_lock();
2648     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2649     wined3d_mutex_unlock();
2650
2651     return hr;
2652 }
2653
2654 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2655     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2656     HRESULT hr;
2657
2658     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2659             iface, Handle, pNumSegs, pTriPatchInfo);
2660
2661     wined3d_mutex_lock();
2662     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2663     wined3d_mutex_unlock();
2664
2665     return hr;
2666 }
2667
2668 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9EX iface, UINT Handle) {
2669     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2670     HRESULT hr;
2671
2672     TRACE("iface %p, handle %#x.\n", iface, Handle);
2673
2674     wined3d_mutex_lock();
2675     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2676     wined3d_mutex_unlock();
2677
2678     return hr;
2679 }
2680
2681 static HRESULT WINAPI IDirect3DDevice9Impl_CreateQuery(IDirect3DDevice9Ex *iface,
2682         D3DQUERYTYPE type, IDirect3DQuery9 **query)
2683 {
2684     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2685     IDirect3DQuery9Impl *object;
2686     HRESULT hr;
2687
2688     TRACE("iface %p, type %#x, query %p.\n", iface, type, query);
2689
2690     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2691     if (!object)
2692     {
2693         ERR("Failed to allocate query memory.\n");
2694         return E_OUTOFMEMORY;
2695     }
2696
2697     hr = query_init(object, This, type);
2698     if (FAILED(hr))
2699     {
2700         WARN("Failed to initialize query, hr %#x.\n", hr);
2701         HeapFree(GetProcessHeap(), 0, object);
2702         return hr;
2703     }
2704
2705     TRACE("Created query %p.\n", object);
2706     if (query) *query = (IDirect3DQuery9 *)object;
2707     else IDirect3DQuery9_Release((IDirect3DQuery9 *)object);
2708
2709     return D3D_OK;
2710 }
2711
2712 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2713         UINT width, UINT height, float *rows, float *columns)
2714 {
2715     FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2716             iface, width, height, rows, columns);
2717
2718     return E_NOTIMPL;
2719 }
2720
2721 static HRESULT WINAPI IDirect3DDevice9ExImpl_ComposeRects(IDirect3DDevice9Ex *iface,
2722         IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2723         UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2724 {
2725     FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2726             "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2727             iface, src_surface, dst_surface, src_descs, rect_count,
2728             dst_descs, operation, offset_x, offset_y);
2729
2730     return E_NOTIMPL;
2731 }
2732
2733 static HRESULT WINAPI IDirect3DDevice9ExImpl_PresentEx(IDirect3DDevice9Ex *iface,
2734         const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2735         const RGNDATA *dirty_region, DWORD flags)
2736 {
2737     FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2738             iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2739
2740     return E_NOTIMPL;
2741 }
2742
2743 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2744 {
2745     FIXME("iface %p, priority %p stub!\n", iface, priority);
2746
2747     return E_NOTIMPL;
2748 }
2749
2750 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2751 {
2752     FIXME("iface %p, priority %d stub!\n", iface, priority);
2753
2754     return E_NOTIMPL;
2755 }
2756
2757 static HRESULT WINAPI IDirect3DDevice9ExImpl_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2758 {
2759     FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2760
2761     return E_NOTIMPL;
2762 }
2763
2764 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2765         IDirect3DResource9 **resources, UINT32 resource_count)
2766 {
2767     FIXME("iface %p, resources %p, resource_count %u stub!\n",
2768             iface, resources, resource_count);
2769
2770     return E_NOTIMPL;
2771 }
2772
2773 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2774 {
2775     FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2776
2777     return E_NOTIMPL;
2778 }
2779
2780 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2781 {
2782     FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2783
2784     *max_latency = 2;
2785
2786     return E_NOTIMPL;
2787 }
2788
2789 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2790 {
2791     static int i;
2792
2793     TRACE("iface %p, dst_window %p stub!\n", iface, dst_window);
2794
2795     if (!i++)
2796         FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2797
2798     return D3D_OK;
2799 }
2800
2801 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2802         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2803         BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2804 {
2805     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2806             "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2807             iface, width, height, format, multisample_type, multisample_quality,
2808             lockable, surface, shared_handle, usage);
2809
2810     return E_NOTIMPL;
2811 }
2812
2813 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2814         UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2815         HANDLE *shared_handle, DWORD usage)
2816 {
2817     FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2818             iface, width, height, format, pool, surface, shared_handle, usage);
2819
2820     return E_NOTIMPL;
2821 }
2822
2823 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2824         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2825         BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2826 {
2827     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2828             "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2829             iface, width, height, format, multisample_type, multisample_quality,
2830             discard, surface, shared_handle, usage);
2831
2832     return E_NOTIMPL;
2833 }
2834
2835 static HRESULT WINAPI IDirect3DDevice9ExImpl_ResetEx(IDirect3DDevice9Ex *iface,
2836         D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2837 {
2838     FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2839
2840     return E_NOTIMPL;
2841 }
2842
2843 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2844         UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2845 {
2846     FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2847
2848     return E_NOTIMPL;
2849 }
2850
2851 static const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
2852 {
2853     /* IUnknown */
2854     IDirect3DDevice9Impl_QueryInterface,
2855     IDirect3DDevice9Impl_AddRef,
2856     IDirect3DDevice9Impl_Release,
2857     /* IDirect3DDevice9 */
2858     IDirect3DDevice9Impl_TestCooperativeLevel,
2859     IDirect3DDevice9Impl_GetAvailableTextureMem,
2860     IDirect3DDevice9Impl_EvictManagedResources,
2861     IDirect3DDevice9Impl_GetDirect3D,
2862     IDirect3DDevice9Impl_GetDeviceCaps,
2863     IDirect3DDevice9Impl_GetDisplayMode,
2864     IDirect3DDevice9Impl_GetCreationParameters,
2865     IDirect3DDevice9Impl_SetCursorProperties,
2866     IDirect3DDevice9Impl_SetCursorPosition,
2867     IDirect3DDevice9Impl_ShowCursor,
2868     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
2869     IDirect3DDevice9Impl_GetSwapChain,
2870     IDirect3DDevice9Impl_GetNumberOfSwapChains,
2871     IDirect3DDevice9Impl_Reset,
2872     IDirect3DDevice9Impl_Present,
2873     IDirect3DDevice9Impl_GetBackBuffer,
2874     IDirect3DDevice9Impl_GetRasterStatus,
2875     IDirect3DDevice9Impl_SetDialogBoxMode,
2876     IDirect3DDevice9Impl_SetGammaRamp,
2877     IDirect3DDevice9Impl_GetGammaRamp,
2878     IDirect3DDevice9Impl_CreateTexture,
2879     IDirect3DDevice9Impl_CreateVolumeTexture,
2880     IDirect3DDevice9Impl_CreateCubeTexture,
2881     IDirect3DDevice9Impl_CreateVertexBuffer,
2882     IDirect3DDevice9Impl_CreateIndexBuffer,
2883     IDirect3DDevice9Impl_CreateRenderTarget,
2884     IDirect3DDevice9Impl_CreateDepthStencilSurface,
2885     IDirect3DDevice9Impl_UpdateSurface,
2886     IDirect3DDevice9Impl_UpdateTexture,
2887     IDirect3DDevice9Impl_GetRenderTargetData,
2888     IDirect3DDevice9Impl_GetFrontBufferData,
2889     IDirect3DDevice9Impl_StretchRect,
2890     IDirect3DDevice9Impl_ColorFill,
2891     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
2892     IDirect3DDevice9Impl_SetRenderTarget,
2893     IDirect3DDevice9Impl_GetRenderTarget,
2894     IDirect3DDevice9Impl_SetDepthStencilSurface,
2895     IDirect3DDevice9Impl_GetDepthStencilSurface,
2896     IDirect3DDevice9Impl_BeginScene,
2897     IDirect3DDevice9Impl_EndScene,
2898     IDirect3DDevice9Impl_Clear,
2899     IDirect3DDevice9Impl_SetTransform,
2900     IDirect3DDevice9Impl_GetTransform,
2901     IDirect3DDevice9Impl_MultiplyTransform,
2902     IDirect3DDevice9Impl_SetViewport,
2903     IDirect3DDevice9Impl_GetViewport,
2904     IDirect3DDevice9Impl_SetMaterial,
2905     IDirect3DDevice9Impl_GetMaterial,
2906     IDirect3DDevice9Impl_SetLight,
2907     IDirect3DDevice9Impl_GetLight,
2908     IDirect3DDevice9Impl_LightEnable,
2909     IDirect3DDevice9Impl_GetLightEnable,
2910     IDirect3DDevice9Impl_SetClipPlane,
2911     IDirect3DDevice9Impl_GetClipPlane,
2912     IDirect3DDevice9Impl_SetRenderState,
2913     IDirect3DDevice9Impl_GetRenderState,
2914     IDirect3DDevice9Impl_CreateStateBlock,
2915     IDirect3DDevice9Impl_BeginStateBlock,
2916     IDirect3DDevice9Impl_EndStateBlock,
2917     IDirect3DDevice9Impl_SetClipStatus,
2918     IDirect3DDevice9Impl_GetClipStatus,
2919     IDirect3DDevice9Impl_GetTexture,
2920     IDirect3DDevice9Impl_SetTexture,
2921     IDirect3DDevice9Impl_GetTextureStageState,
2922     IDirect3DDevice9Impl_SetTextureStageState,
2923     IDirect3DDevice9Impl_GetSamplerState,
2924     IDirect3DDevice9Impl_SetSamplerState,
2925     IDirect3DDevice9Impl_ValidateDevice,
2926     IDirect3DDevice9Impl_SetPaletteEntries,
2927     IDirect3DDevice9Impl_GetPaletteEntries,
2928     IDirect3DDevice9Impl_SetCurrentTexturePalette,
2929     IDirect3DDevice9Impl_GetCurrentTexturePalette,
2930     IDirect3DDevice9Impl_SetScissorRect,
2931     IDirect3DDevice9Impl_GetScissorRect,
2932     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
2933     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
2934     IDirect3DDevice9Impl_SetNPatchMode,
2935     IDirect3DDevice9Impl_GetNPatchMode,
2936     IDirect3DDevice9Impl_DrawPrimitive,
2937     IDirect3DDevice9Impl_DrawIndexedPrimitive,
2938     IDirect3DDevice9Impl_DrawPrimitiveUP,
2939     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
2940     IDirect3DDevice9Impl_ProcessVertices,
2941     IDirect3DDevice9Impl_CreateVertexDeclaration,
2942     IDirect3DDevice9Impl_SetVertexDeclaration,
2943     IDirect3DDevice9Impl_GetVertexDeclaration,
2944     IDirect3DDevice9Impl_SetFVF,
2945     IDirect3DDevice9Impl_GetFVF,
2946     IDirect3DDevice9Impl_CreateVertexShader,
2947     IDirect3DDevice9Impl_SetVertexShader,
2948     IDirect3DDevice9Impl_GetVertexShader,
2949     IDirect3DDevice9Impl_SetVertexShaderConstantF,
2950     IDirect3DDevice9Impl_GetVertexShaderConstantF,
2951     IDirect3DDevice9Impl_SetVertexShaderConstantI,
2952     IDirect3DDevice9Impl_GetVertexShaderConstantI,
2953     IDirect3DDevice9Impl_SetVertexShaderConstantB,
2954     IDirect3DDevice9Impl_GetVertexShaderConstantB,
2955     IDirect3DDevice9Impl_SetStreamSource,
2956     IDirect3DDevice9Impl_GetStreamSource,
2957     IDirect3DDevice9Impl_SetStreamSourceFreq,
2958     IDirect3DDevice9Impl_GetStreamSourceFreq,
2959     IDirect3DDevice9Impl_SetIndices,
2960     IDirect3DDevice9Impl_GetIndices,
2961     IDirect3DDevice9Impl_CreatePixelShader,
2962     IDirect3DDevice9Impl_SetPixelShader,
2963     IDirect3DDevice9Impl_GetPixelShader,
2964     IDirect3DDevice9Impl_SetPixelShaderConstantF,
2965     IDirect3DDevice9Impl_GetPixelShaderConstantF,
2966     IDirect3DDevice9Impl_SetPixelShaderConstantI,
2967     IDirect3DDevice9Impl_GetPixelShaderConstantI,
2968     IDirect3DDevice9Impl_SetPixelShaderConstantB,
2969     IDirect3DDevice9Impl_GetPixelShaderConstantB,
2970     IDirect3DDevice9Impl_DrawRectPatch,
2971     IDirect3DDevice9Impl_DrawTriPatch,
2972     IDirect3DDevice9Impl_DeletePatch,
2973     IDirect3DDevice9Impl_CreateQuery,
2974     /* IDirect3DDevice9Ex */
2975     IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
2976     IDirect3DDevice9ExImpl_ComposeRects,
2977     IDirect3DDevice9ExImpl_PresentEx,
2978     IDirect3DDevice9ExImpl_GetGPUThreadPriority,
2979     IDirect3DDevice9ExImpl_SetGPUThreadPriority,
2980     IDirect3DDevice9ExImpl_WaitForVBlank,
2981     IDirect3DDevice9ExImpl_CheckResourceResidency,
2982     IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
2983     IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
2984     IDirect3DDevice9ExImpl_CheckDeviceState,
2985     IDirect3DDevice9ExImpl_CreateRenderTargetEx,
2986     IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
2987     IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
2988     IDirect3DDevice9ExImpl_ResetEx,
2989     IDirect3DDevice9ExImpl_GetDisplayModeEx
2990 };
2991
2992 /* IWineD3DDeviceParent IUnknown methods */
2993
2994 static inline struct IDirect3DDevice9Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2995 {
2996     return (struct IDirect3DDevice9Impl *)((char*)iface
2997             - FIELD_OFFSET(struct IDirect3DDevice9Impl, device_parent_vtbl));
2998 }
2999
3000 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
3001 {
3002     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3003     return IDirect3DDevice9Impl_QueryInterface((IDirect3DDevice9Ex *)This, riid, object);
3004 }
3005
3006 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
3007 {
3008     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3009     return IDirect3DDevice9Impl_AddRef((IDirect3DDevice9Ex *)This);
3010 }
3011
3012 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
3013 {
3014     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3015     return IDirect3DDevice9Impl_Release((IDirect3DDevice9Ex *)This);
3016 }
3017
3018 /* IWineD3DDeviceParent methods */
3019
3020 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
3021 {
3022     TRACE("iface %p, device %p\n", iface, device);
3023 }
3024
3025 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
3026         void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
3027         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
3028 {
3029     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3030     IDirect3DSurface9Impl *d3d_surface;
3031     BOOL lockable = TRUE;
3032     HRESULT hr;
3033
3034     TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
3035             "\tpool %#x, level %u, face %u, surface %p\n",
3036             iface, container_parent, width, height, format, usage, pool, level, face, surface);
3037
3038     if (pool == WINED3DPOOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
3039         lockable = FALSE;
3040
3041     hr = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9Ex *)This, width, height,
3042             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
3043             (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
3044     if (FAILED(hr))
3045     {
3046         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
3047         return hr;
3048     }
3049
3050     *surface = d3d_surface->wineD3DSurface;
3051     IWineD3DSurface_AddRef(*surface);
3052
3053     d3d_surface->container = container_parent;
3054     IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
3055     d3d_surface->parentDevice = NULL;
3056
3057     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
3058     d3d_surface->forwardReference = container_parent;
3059
3060     return hr;
3061 }
3062
3063 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
3064         void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
3065         WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
3066         IWineD3DSurface **surface)
3067 {
3068     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3069     IDirect3DSurface9Impl *d3d_surface;
3070     HRESULT hr;
3071
3072     TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3073             "\tmultisample_quality %u, lockable %u, surface %p\n",
3074             iface, container_parent, width, height, format, multisample_type, multisample_quality, lockable, surface);
3075
3076     hr = IDirect3DDevice9Impl_CreateRenderTarget((IDirect3DDevice9Ex *)This, width, height,
3077             d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
3078             (IDirect3DSurface9 **)&d3d_surface, NULL);
3079     if (FAILED(hr))
3080     {
3081         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
3082         return hr;
3083     }
3084
3085     *surface = d3d_surface->wineD3DSurface;
3086     IWineD3DSurface_AddRef(*surface);
3087
3088     d3d_surface->container = container_parent;
3089     /* Implicit surfaces are created with an refcount of 0 */
3090     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
3091
3092     return hr;
3093 }
3094
3095 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
3096         UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
3097         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
3098 {
3099     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3100     IDirect3DSurface9Impl *d3d_surface;
3101     HRESULT hr;
3102
3103     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
3104             "\tmultisample_quality %u, discard %u, surface %p\n",
3105             iface, width, height, format, multisample_type, multisample_quality, discard, surface);
3106
3107     hr = IDirect3DDevice9Impl_CreateDepthStencilSurface((IDirect3DDevice9Ex *)This, width, height,
3108             d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, discard,
3109             (IDirect3DSurface9 **)&d3d_surface, NULL);
3110     if (FAILED(hr))
3111     {
3112         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
3113         return hr;
3114     }
3115
3116     *surface = d3d_surface->wineD3DSurface;
3117     IWineD3DSurface_AddRef(*surface);
3118     d3d_surface->container = (IUnknown *)This;
3119     /* Implicit surfaces are created with an refcount of 0 */
3120     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
3121
3122     return hr;
3123 }
3124
3125 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
3126         void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
3127         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
3128 {
3129     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3130     IDirect3DVolume9Impl *object;
3131     HRESULT hr;
3132
3133     TRACE("iface %p, container_parent %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
3134             iface, container_parent, width, height, depth, format, pool, usage, volume);
3135
3136     /* Allocate the storage for the device */
3137     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3138     if (!object)
3139     {
3140         FIXME("Allocation of memory failed\n");
3141         *volume = NULL;
3142         return D3DERR_OUTOFVIDEOMEMORY;
3143     }
3144
3145     hr = volume_init(object, This, width, height, depth, usage, format, pool);
3146     if (FAILED(hr))
3147     {
3148         WARN("Failed to initialize volume, hr %#x.\n", hr);
3149         HeapFree(GetProcessHeap(), 0, object);
3150         return hr;
3151     }
3152
3153     *volume = object->wineD3DVolume;
3154     IWineD3DVolume_AddRef(*volume);
3155     IDirect3DVolume9_Release((IDirect3DVolume9 *)object);
3156
3157     object->container = container_parent;
3158     object->forwardReference = container_parent;
3159
3160     TRACE("(%p) Created volume %p\n", iface, object);
3161
3162     return hr;
3163 }
3164
3165 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3166         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
3167 {
3168     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
3169     IDirect3DSwapChain9Impl *d3d_swapchain;
3170     D3DPRESENT_PARAMETERS local_parameters;
3171     HRESULT hr;
3172
3173     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3174
3175     /* Copy the presentation parameters */
3176     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
3177     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
3178     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
3179     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
3180     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
3181     local_parameters.MultiSampleQuality = present_parameters->MultiSampleQuality;
3182     local_parameters.SwapEffect = present_parameters->SwapEffect;
3183     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
3184     local_parameters.Windowed = present_parameters->Windowed;
3185     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
3186     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
3187     local_parameters.Flags = present_parameters->Flags;
3188     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
3189     local_parameters.PresentationInterval = present_parameters->PresentationInterval;
3190
3191     hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain((IDirect3DDevice9Ex *)This,
3192             &local_parameters, (IDirect3DSwapChain9 **)&d3d_swapchain);
3193     if (FAILED(hr))
3194     {
3195         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
3196         *swapchain = NULL;
3197         return hr;
3198     }
3199
3200     *swapchain = d3d_swapchain->wineD3DSwapChain;
3201     d3d_swapchain->isImplicit = TRUE;
3202     /* Implicit swap chains are created with an refcount of 0 */
3203     IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)d3d_swapchain);
3204
3205     /* Copy back the presentation parameters */
3206     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
3207     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
3208     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3209     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
3210     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
3211     present_parameters->MultiSampleQuality = local_parameters.MultiSampleQuality;
3212     present_parameters->SwapEffect = local_parameters.SwapEffect;
3213     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
3214     present_parameters->Windowed = local_parameters.Windowed;
3215     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
3216     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3217     present_parameters->Flags = local_parameters.Flags;
3218     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
3219     present_parameters->PresentationInterval = local_parameters.PresentationInterval;
3220
3221     return hr;
3222 }
3223
3224 static const IWineD3DDeviceParentVtbl d3d9_wined3d_device_parent_vtbl =
3225 {
3226     /* IUnknown methods */
3227     device_parent_QueryInterface,
3228     device_parent_AddRef,
3229     device_parent_Release,
3230     /* IWineD3DDeviceParent methods */
3231     device_parent_WineD3DDeviceCreated,
3232     device_parent_CreateSurface,
3233     device_parent_CreateRenderTarget,
3234     device_parent_CreateDepthStencilSurface,
3235     device_parent_CreateVolume,
3236     device_parent_CreateSwapChain,
3237 };
3238
3239 static void setup_fpu(void)
3240 {
3241 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3242     WORD cw;
3243     __asm__ volatile ("fnstcw %0" : "=m" (cw));
3244     cw = (cw & ~0xf3f) | 0x3f;
3245     __asm__ volatile ("fldcw %0" : : "m" (cw));
3246 #else
3247     FIXME("FPU setup not implemented for this platform.\n");
3248 #endif
3249 }
3250
3251 HRESULT device_init(IDirect3DDevice9Impl *device, struct wined3d *wined3d, UINT adapter, D3DDEVTYPE device_type,
3252         HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode)
3253 {
3254     WINED3DPRESENT_PARAMETERS *wined3d_parameters;
3255     UINT i, count = 1;
3256     HRESULT hr;
3257
3258     if (mode)
3259         FIXME("Ignoring display mode.\n");
3260
3261     device->lpVtbl = &Direct3DDevice9_Vtbl;
3262     device->device_parent_vtbl = &d3d9_wined3d_device_parent_vtbl;
3263     device->ref = 1;
3264
3265     if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3266
3267     wined3d_mutex_lock();
3268     hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags,
3269             (IWineD3DDeviceParent *)&device->device_parent_vtbl, &device->WineD3DDevice);
3270     if (FAILED(hr))
3271     {
3272         WARN("Failed to create wined3d device, hr %#x.\n", hr);
3273         wined3d_mutex_unlock();
3274         return hr;
3275     }
3276
3277     if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
3278     {
3279         WINED3DCAPS caps;
3280
3281         wined3d_get_device_caps(wined3d, adapter, device_type, &caps);
3282         count = caps.NumberOfAdaptersInGroup;
3283     }
3284
3285     if (flags & D3DCREATE_MULTITHREADED) IWineD3DDevice_SetMultithreaded(device->WineD3DDevice);
3286
3287     if (!parameters->Windowed)
3288     {
3289         if (!focus_window) focus_window = parameters->hDeviceWindow;
3290         if (FAILED(hr = IWineD3DDevice_AcquireFocusWindow(device->WineD3DDevice, focus_window)))
3291         {
3292             ERR("Failed to acquire focus window, hr %#x.\n", hr);
3293             IWineD3DDevice_Release(device->WineD3DDevice);
3294             wined3d_mutex_unlock();
3295             return hr;
3296         }
3297
3298         for (i = 0; i < count; ++i)
3299         {
3300             HWND device_window = parameters[i].hDeviceWindow;
3301
3302             if (!device_window) device_window = focus_window;
3303             IWineD3DDevice_SetupFullscreenWindow(device->WineD3DDevice, device_window,
3304                     parameters[i].BackBufferWidth,
3305                     parameters[i].BackBufferHeight);
3306         }
3307     }
3308
3309     wined3d_parameters = HeapAlloc(GetProcessHeap(), 0, sizeof(*wined3d_parameters) * count);
3310     if (!wined3d_parameters)
3311     {
3312         ERR("Failed to allocate wined3d parameters.\n");
3313         IWineD3DDevice_Release(device->WineD3DDevice);
3314         wined3d_mutex_unlock();
3315         return E_OUTOFMEMORY;
3316     }
3317
3318     for (i = 0; i < count; ++i)
3319     {
3320         wined3d_parameters[i].BackBufferWidth = parameters[i].BackBufferWidth;
3321         wined3d_parameters[i].BackBufferHeight = parameters[i].BackBufferHeight;
3322         wined3d_parameters[i].BackBufferFormat = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
3323         wined3d_parameters[i].BackBufferCount = parameters[i].BackBufferCount;
3324         wined3d_parameters[i].MultiSampleType = parameters[i].MultiSampleType;
3325         wined3d_parameters[i].MultiSampleQuality = parameters[i].MultiSampleQuality;
3326         wined3d_parameters[i].SwapEffect = parameters[i].SwapEffect;
3327         wined3d_parameters[i].hDeviceWindow = parameters[i].hDeviceWindow;
3328         wined3d_parameters[i].Windowed = parameters[i].Windowed;
3329         wined3d_parameters[i].EnableAutoDepthStencil = parameters[i].EnableAutoDepthStencil;
3330         wined3d_parameters[i].AutoDepthStencilFormat =
3331                 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
3332         wined3d_parameters[i].Flags = parameters[i].Flags;
3333         wined3d_parameters[i].FullScreen_RefreshRateInHz = parameters[i].FullScreen_RefreshRateInHz;
3334         wined3d_parameters[i].PresentationInterval = parameters[i].PresentationInterval;
3335         wined3d_parameters[i].AutoRestoreDisplayMode = TRUE;
3336     }
3337
3338     hr = IWineD3DDevice_Init3D(device->WineD3DDevice, wined3d_parameters);
3339     if (FAILED(hr))
3340     {
3341         WARN("Failed to initialize 3D, hr %#x.\n", hr);
3342         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3343         HeapFree(GetProcessHeap(), 0, wined3d_parameters);
3344         IWineD3DDevice_Release(device->WineD3DDevice);
3345         wined3d_mutex_unlock();
3346         return hr;
3347     }
3348
3349     wined3d_mutex_unlock();
3350
3351     for (i = 0; i < count; ++i)
3352     {
3353         parameters[i].BackBufferWidth = wined3d_parameters[i].BackBufferWidth;
3354         parameters[i].BackBufferHeight = wined3d_parameters[i].BackBufferHeight;
3355         parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters[i].BackBufferFormat);
3356         parameters[i].BackBufferCount = wined3d_parameters[i].BackBufferCount;
3357         parameters[i].MultiSampleType = wined3d_parameters[i].MultiSampleType;
3358         parameters[i].MultiSampleQuality = wined3d_parameters[i].MultiSampleQuality;
3359         parameters[i].SwapEffect = wined3d_parameters[i].SwapEffect;
3360         parameters[i].hDeviceWindow = wined3d_parameters[i].hDeviceWindow;
3361         parameters[i].Windowed = wined3d_parameters[i].Windowed;
3362         parameters[i].EnableAutoDepthStencil = wined3d_parameters[i].EnableAutoDepthStencil;
3363         parameters[i].AutoDepthStencilFormat =
3364                 d3dformat_from_wined3dformat(wined3d_parameters[i].AutoDepthStencilFormat);
3365         parameters[i].Flags = wined3d_parameters[i].Flags;
3366         parameters[i].FullScreen_RefreshRateInHz = wined3d_parameters[i].FullScreen_RefreshRateInHz;
3367         parameters[i].PresentationInterval = wined3d_parameters[i].PresentationInterval;
3368     }
3369     HeapFree(GetProcessHeap(), 0, wined3d_parameters);
3370
3371     /* Initialize the converted declaration array. This creates a valid pointer
3372      * and when adding decls HeapReAlloc() can be used without further checking. */
3373     device->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
3374     if (!device->convertedDecls)
3375     {
3376         ERR("Failed to allocate FVF vertex declaration map memory.\n");
3377         wined3d_mutex_lock();
3378         IWineD3DDevice_Uninit3D(device->WineD3DDevice, D3D9CB_DestroySwapChain);
3379         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3380         IWineD3DDevice_Release(device->WineD3DDevice);
3381         wined3d_mutex_unlock();
3382         return E_OUTOFMEMORY;
3383     }
3384
3385     return D3D_OK;
3386 }