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