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