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