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