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