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