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