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