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