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