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