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