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