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