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