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