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