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