wined3d: Devices don't need parents.
[wine] / dlls / d3d8 / device.c
1 /*
2  * IDirect3DDevice8 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2004 Christian Costa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <math.h>
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
34
35 #include "d3d8_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
38
39 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
40 {
41     BYTE *c = (BYTE *)&format;
42
43     /* Don't translate FOURCC formats */
44     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
45
46     switch(format)
47     {
48         case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
49         case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
50         case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
51         case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
52         case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
53         case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
54         case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
55         case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
56         case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
57         case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
58         case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
59         case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
60         case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
61         case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
62         case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63         case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64         case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65         case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66         case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67         case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68         case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69         case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70         case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71         case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72         case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
73         case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
74         case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
75         case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
76         case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
77         case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
78         case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
79         case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
80         case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
81         case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
82         case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
83         case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
84         default:
85             FIXME("Unhandled wined3d format %#x.\n", format);
86             return D3DFMT_UNKNOWN;
87     }
88 }
89
90 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
91 {
92     BYTE *c = (BYTE *)&format;
93
94     /* Don't translate FOURCC formats */
95     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
96
97     switch(format)
98     {
99         case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
100         case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
101         case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
102         case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
103         case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
104         case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
105         case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
106         case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
107         case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
108         case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
109         case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
110         case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
111         case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
112         case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
113         case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
114         case D3DFMT_P8: return WINED3DFMT_P8_UINT;
115         case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
116         case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
117         case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
118         case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
119         case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
120         case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
121         case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
122         case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
123         case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
124         case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
125         case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
126         case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
127         case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
128         case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
129         case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
130         case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
131         case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
132         case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
133         case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
134         case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
135         default:
136             FIXME("Unhandled D3DFORMAT %#x\n", format);
137             return WINED3DFMT_UNKNOWN;
138     }
139 }
140
141 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
142 {
143     switch(primitive_type)
144     {
145         case D3DPT_POINTLIST:
146             return primitive_count;
147
148         case D3DPT_LINELIST:
149             return primitive_count * 2;
150
151         case D3DPT_LINESTRIP:
152             return primitive_count + 1;
153
154         case D3DPT_TRIANGLELIST:
155             return primitive_count * 3;
156
157         case D3DPT_TRIANGLESTRIP:
158         case D3DPT_TRIANGLEFAN:
159             return primitive_count + 2;
160
161         default:
162             FIXME("Unhandled primitive type %#x\n", primitive_type);
163             return 0;
164     }
165 }
166
167 /* Handle table functions */
168 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
169 {
170     struct d3d8_handle_entry *entry;
171
172     if (t->free_entries)
173     {
174         DWORD index = t->free_entries - t->entries;
175         /* Use a free handle */
176         entry = t->free_entries;
177         if (entry->type != D3D8_HANDLE_FREE)
178         {
179             ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
180             return D3D8_INVALID_HANDLE;
181         }
182         t->free_entries = entry->object;
183         entry->object = object;
184         entry->type = type;
185
186         return index;
187     }
188
189     if (!(t->entry_count < t->table_size))
190     {
191         /* Grow the table */
192         UINT new_size = t->table_size + (t->table_size >> 1);
193         struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
194                 0, t->entries, new_size * sizeof(*t->entries));
195         if (!new_entries)
196         {
197             ERR("Failed to grow the handle table.\n");
198             return D3D8_INVALID_HANDLE;
199         }
200         t->entries = new_entries;
201         t->table_size = new_size;
202     }
203
204     entry = &t->entries[t->entry_count];
205     entry->object = object;
206     entry->type = type;
207
208     return t->entry_count++;
209 }
210
211 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
212 {
213     struct d3d8_handle_entry *entry;
214     void *object;
215
216     if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
217     {
218         WARN("Invalid handle %u passed.\n", handle);
219         return NULL;
220     }
221
222     entry = &t->entries[handle];
223     if (entry->type != type)
224     {
225         WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
226         return NULL;
227     }
228
229     object = entry->object;
230     entry->object = t->free_entries;
231     entry->type = D3D8_HANDLE_FREE;
232     t->free_entries = entry;
233
234     return object;
235 }
236
237 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
238 {
239     struct d3d8_handle_entry *entry;
240
241     if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
242     {
243         WARN("Invalid handle %u passed.\n", handle);
244         return NULL;
245     }
246
247     entry = &t->entries[handle];
248     if (entry->type != type)
249     {
250         WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
251         return NULL;
252     }
253
254     return entry->object;
255 }
256
257 static ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *swapchain)
258 {
259     IUnknown *parent;
260
261     TRACE("swapchain %p.\n", swapchain);
262
263     IWineD3DSwapChain_GetParent(swapchain, &parent);
264     IUnknown_Release(parent);
265     return IUnknown_Release(parent);
266 }
267
268 /* IDirect3D IUnknown parts follow: */
269 static HRESULT WINAPI IDirect3DDevice8Impl_QueryInterface(LPDIRECT3DDEVICE8 iface,REFIID riid,LPVOID *ppobj)
270 {
271     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
272
273     TRACE("iface %p, riid %s, object %p.\n",
274             iface, debugstr_guid(riid), ppobj);
275
276     if (IsEqualGUID(riid, &IID_IUnknown)
277         || IsEqualGUID(riid, &IID_IDirect3DDevice8)) {
278         IUnknown_AddRef(iface);
279         *ppobj = This;
280         return S_OK;
281     }
282
283     if (IsEqualGUID(riid, &IID_IWineD3DDeviceParent))
284     {
285         IUnknown_AddRef((IUnknown *)&This->device_parent_vtbl);
286         *ppobj = &This->device_parent_vtbl;
287         return S_OK;
288     }
289
290     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
291     *ppobj = NULL;
292     return E_NOINTERFACE;
293 }
294
295 static ULONG WINAPI IDirect3DDevice8Impl_AddRef(LPDIRECT3DDEVICE8 iface) {
296     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
297     ULONG ref = InterlockedIncrement(&This->ref);
298
299     TRACE("%p increasing refcount to %u.\n", iface, ref);
300
301     return ref;
302 }
303
304 static ULONG WINAPI IDirect3DDevice8Impl_Release(LPDIRECT3DDEVICE8 iface) {
305     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
306     ULONG ref;
307
308     if (This->inDestruction) return 0;
309     ref = InterlockedDecrement(&This->ref);
310
311     TRACE("%p decreasing refcount to %u.\n", iface, ref);
312
313     if (ref == 0) {
314         unsigned i;
315
316         TRACE("Releasing wined3d device %p\n", This->WineD3DDevice);
317
318         wined3d_mutex_lock();
319
320         This->inDestruction = TRUE;
321
322         for(i = 0; i < This->numConvertedDecls; i++) {
323             IDirect3DVertexDeclaration8_Release(This->decls[i].decl);
324         }
325         HeapFree(GetProcessHeap(), 0, This->decls);
326
327         IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D8CB_DestroySwapChain);
328         IWineD3DDevice_ReleaseFocusWindow(This->WineD3DDevice);
329         IWineD3DDevice_Release(This->WineD3DDevice);
330         HeapFree(GetProcessHeap(), 0, This->handle_table.entries);
331         HeapFree(GetProcessHeap(), 0, This);
332
333         wined3d_mutex_unlock();
334     }
335     return ref;
336 }
337
338 /* IDirect3DDevice Interface follow: */
339 static HRESULT WINAPI IDirect3DDevice8Impl_TestCooperativeLevel(IDirect3DDevice8 *iface)
340 {
341     TRACE("iface %p.\n", iface);
342
343     return D3D_OK;
344 }
345
346 static UINT WINAPI  IDirect3DDevice8Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE8 iface) {
347     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
348     HRESULT hr;
349
350     TRACE("iface %p.\n", iface);
351
352     wined3d_mutex_lock();
353     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
354     wined3d_mutex_unlock();
355
356     return hr;
357 }
358
359 static HRESULT WINAPI IDirect3DDevice8Impl_ResourceManagerDiscardBytes(LPDIRECT3DDEVICE8 iface, DWORD Bytes) {
360     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
361     HRESULT hr;
362
363     TRACE("iface %p, byte_count %u.\n", iface, Bytes);
364     FIXME("Byte count ignored.\n");
365
366     wined3d_mutex_lock();
367     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
368     wined3d_mutex_unlock();
369
370     return hr;
371 }
372
373 static HRESULT WINAPI IDirect3DDevice8Impl_GetDirect3D(LPDIRECT3DDEVICE8 iface, IDirect3D8** ppD3D8) {
374     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
375     HRESULT hr = D3D_OK;
376     IWineD3D* pWineD3D;
377
378     TRACE("iface %p, d3d8 %p.\n", iface, ppD3D8);
379
380     if (NULL == ppD3D8) {
381         return D3DERR_INVALIDCALL;
382     }
383
384     wined3d_mutex_lock();
385     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
386     if (hr == D3D_OK && pWineD3D != NULL)
387     {
388         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D8);
389         IWineD3D_Release(pWineD3D);
390     } else {
391         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
392         *ppD3D8 = NULL;
393     }
394     wined3d_mutex_unlock();
395
396     TRACE("(%p) returning %p\n",This , *ppD3D8);
397
398     return hr;
399 }
400
401 static HRESULT WINAPI IDirect3DDevice8Impl_GetDeviceCaps(LPDIRECT3DDEVICE8 iface, D3DCAPS8* pCaps) {
402     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
403     HRESULT hrc = D3D_OK;
404     WINED3DCAPS *pWineCaps;
405
406     TRACE("iface %p, caps %p.\n", iface, pCaps);
407
408     if(NULL == pCaps){
409         return D3DERR_INVALIDCALL;
410     }
411     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
412     if(pWineCaps == NULL){
413         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
414     }
415
416     wined3d_mutex_lock();
417     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
418     wined3d_mutex_unlock();
419
420     fixup_caps(pWineCaps);
421     WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
422     HeapFree(GetProcessHeap(), 0, pWineCaps);
423
424     TRACE("Returning %p %p\n", This, pCaps);
425     return hrc;
426 }
427
428 static HRESULT WINAPI IDirect3DDevice8Impl_GetDisplayMode(LPDIRECT3DDEVICE8 iface, D3DDISPLAYMODE* pMode) {
429     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
430     HRESULT hr;
431
432     TRACE("iface %p, mode %p.\n", iface, pMode);
433
434     wined3d_mutex_lock();
435     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, 0, (WINED3DDISPLAYMODE *) pMode);
436     wined3d_mutex_unlock();
437
438     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
439
440     return hr;
441 }
442
443 static HRESULT WINAPI IDirect3DDevice8Impl_GetCreationParameters(LPDIRECT3DDEVICE8 iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
444     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
445     HRESULT hr;
446
447     TRACE("iface %p, parameters %p.\n", iface, pParameters);
448
449     wined3d_mutex_lock();
450     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
451     wined3d_mutex_unlock();
452
453     return hr;
454 }
455
456 static HRESULT WINAPI IDirect3DDevice8Impl_SetCursorProperties(LPDIRECT3DDEVICE8 iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8* pCursorBitmap) {
457     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
458     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl*)pCursorBitmap;
459     HRESULT hr;
460
461     TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
462             iface, XHotSpot, YHotSpot, pCursorBitmap);
463
464     if(!pCursorBitmap) {
465         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
466         return WINED3DERR_INVALIDCALL;
467     }
468
469     wined3d_mutex_lock();
470     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice,XHotSpot,YHotSpot,pSurface->wineD3DSurface);
471     wined3d_mutex_unlock();
472
473     return hr;
474 }
475
476 static void WINAPI IDirect3DDevice8Impl_SetCursorPosition(LPDIRECT3DDEVICE8 iface, UINT XScreenSpace, UINT YScreenSpace, DWORD Flags) {
477     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
478
479     TRACE("iface %p, x %u, y %u, flags %#x.\n",
480             iface, XScreenSpace, YScreenSpace, Flags);
481
482     wined3d_mutex_lock();
483     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
484     wined3d_mutex_unlock();
485 }
486
487 static BOOL WINAPI IDirect3DDevice8Impl_ShowCursor(LPDIRECT3DDEVICE8 iface, BOOL bShow) {
488     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
489     BOOL ret;
490
491     TRACE("iface %p, show %#x.\n", iface, bShow);
492
493     wined3d_mutex_lock();
494     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
495     wined3d_mutex_unlock();
496
497     return ret;
498 }
499
500 static HRESULT WINAPI IDirect3DDevice8Impl_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
501         D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
502 {
503     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
504     IDirect3DSwapChain8Impl *object;
505     HRESULT hr;
506
507     TRACE("iface %p, present_parameters %p, swapchain %p.\n",
508             iface, present_parameters, swapchain);
509
510     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
511     if (!object)
512     {
513         ERR("Failed to allocate swapchain memory.\n");
514         return E_OUTOFMEMORY;
515     }
516
517     hr = swapchain_init(object, This, present_parameters);
518     if (FAILED(hr))
519     {
520         WARN("Failed to initialize swapchain, hr %#x.\n", hr);
521         HeapFree(GetProcessHeap(), 0, object);
522         return hr;
523     }
524
525     TRACE("Created swapchain %p.\n", object);
526     *swapchain = (IDirect3DSwapChain8 *)object;
527
528     return D3D_OK;
529 }
530
531 static HRESULT WINAPI IDirect3DDevice8Impl_Reset(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
532     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
533     WINED3DPRESENT_PARAMETERS localParameters;
534     HRESULT hr;
535
536     TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
537
538     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
539     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
540     localParameters.BackBufferFormat                            = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
541     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
542     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
543     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
544     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
545     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
546     localParameters.Windowed                                    = pPresentationParameters->Windowed;
547     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
548     localParameters.AutoDepthStencilFormat                      = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
549     localParameters.Flags                                       = pPresentationParameters->Flags;
550     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
551     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
552     localParameters.AutoRestoreDisplayMode                      = TRUE;
553
554     wined3d_mutex_lock();
555     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
556     if(SUCCEEDED(hr)) {
557         hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, WINED3DRS_POINTSIZE_MIN, 0);
558     }
559     wined3d_mutex_unlock();
560
561     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
562     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
563     pPresentationParameters->BackBufferFormat                   = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
564     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
565     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
566     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
567     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
568     pPresentationParameters->Windowed                           = localParameters.Windowed;
569     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
570     pPresentationParameters->AutoDepthStencilFormat             = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
571     pPresentationParameters->Flags                              = localParameters.Flags;
572     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
573     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
574
575     return hr;
576 }
577
578 static HRESULT WINAPI IDirect3DDevice8Impl_Present(LPDIRECT3DDEVICE8 iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) {
579     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
580     HRESULT hr;
581
582     TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
583             iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
584
585     wined3d_mutex_lock();
586     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
587     wined3d_mutex_unlock();
588
589     return hr;
590 }
591
592 static HRESULT WINAPI IDirect3DDevice8Impl_GetBackBuffer(LPDIRECT3DDEVICE8 iface, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8** ppBackBuffer) {
593     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
594     IWineD3DSurface *retSurface = NULL;
595     HRESULT rc = D3D_OK;
596
597     TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
598             iface, BackBuffer, Type, ppBackBuffer);
599
600     wined3d_mutex_lock();
601     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, 0, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &retSurface);
602     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
603         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
604         IWineD3DSurface_Release(retSurface);
605     }
606     wined3d_mutex_unlock();
607
608     return rc;
609 }
610
611 static HRESULT WINAPI IDirect3DDevice8Impl_GetRasterStatus(LPDIRECT3DDEVICE8 iface, D3DRASTER_STATUS* pRasterStatus) {
612     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
613     HRESULT hr;
614
615     TRACE("iface %p, raster_status %p.\n", iface, pRasterStatus);
616
617     wined3d_mutex_lock();
618     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, 0, (WINED3DRASTER_STATUS *) pRasterStatus);
619     wined3d_mutex_unlock();
620
621     return hr;
622 }
623
624 static void WINAPI IDirect3DDevice8Impl_SetGammaRamp(LPDIRECT3DDEVICE8 iface, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
625     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
626
627     TRACE("iface %p, flags %#x, ramp %p.\n", iface, Flags, pRamp);
628
629     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
630     wined3d_mutex_lock();
631     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, 0, Flags, (CONST WINED3DGAMMARAMP *) pRamp);
632     wined3d_mutex_unlock();
633 }
634
635 static void WINAPI IDirect3DDevice8Impl_GetGammaRamp(LPDIRECT3DDEVICE8 iface, D3DGAMMARAMP* pRamp) {
636     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
637
638     TRACE("iface %p, ramp %p.\n", iface, pRamp);
639
640     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
641     wined3d_mutex_lock();
642     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, 0, (WINED3DGAMMARAMP *) pRamp);
643     wined3d_mutex_unlock();
644 }
645
646 static HRESULT WINAPI IDirect3DDevice8Impl_CreateTexture(IDirect3DDevice8 *iface,
647         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
648         D3DPOOL pool, IDirect3DTexture8 **texture)
649 {
650     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
651     IDirect3DTexture8Impl *object;
652     HRESULT hr;
653
654     TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
655             iface, width, height, levels, usage, format, pool, texture);
656
657     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
658     if (!object)
659     {
660         ERR("Failed to allocate texture memory.\n");
661         return D3DERR_OUTOFVIDEOMEMORY;
662     }
663
664     hr = texture_init(object, This, width, height, levels, usage, format, pool);
665     if (FAILED(hr))
666     {
667         WARN("Failed to initialize texture, hr %#x.\n", hr);
668         HeapFree(GetProcessHeap(), 0, object);
669         return hr;
670     }
671
672     TRACE("Created texture %p.\n", object);
673     *texture = (IDirect3DTexture8 *)object;
674
675     return D3D_OK;
676 }
677
678 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVolumeTexture(IDirect3DDevice8 *iface,
679         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
680         D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
681 {
682     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
683     IDirect3DVolumeTexture8Impl *object;
684     HRESULT hr;
685
686     TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
687             iface, width, height, depth, levels, usage, format, pool, texture);
688
689     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
690     if (!object)
691     {
692         ERR("Failed to allocate volume texture memory.\n");
693         return D3DERR_OUTOFVIDEOMEMORY;
694     }
695
696     hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
697     if (FAILED(hr))
698     {
699         WARN("Failed to initialize volume texture, hr %#x.\n", hr);
700         HeapFree(GetProcessHeap(), 0, object);
701         return hr;
702     }
703
704     TRACE("Created volume texture %p.\n", object);
705     *texture = (IDirect3DVolumeTexture8 *)object;
706
707     return D3D_OK;
708 }
709
710 static HRESULT WINAPI IDirect3DDevice8Impl_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
711         UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
712 {
713     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
714     IDirect3DCubeTexture8Impl *object;
715     HRESULT hr;
716
717     TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
718             iface, edge_length, levels, usage, format, pool, texture);
719
720     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
721     if (!object)
722     {
723         ERR("Failed to allocate cube texture memory.\n");
724         return D3DERR_OUTOFVIDEOMEMORY;
725     }
726
727     hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
728     if (FAILED(hr))
729     {
730         WARN("Failed to initialize cube texture, hr %#x.\n", hr);
731         HeapFree(GetProcessHeap(), 0, object);
732         return hr;
733     }
734
735     TRACE("Created cube texture %p.\n", object);
736     *texture = (IDirect3DCubeTexture8 *)object;
737
738     return hr;
739 }
740
741 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size, DWORD usage,
742         DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
743 {
744     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
745     IDirect3DVertexBuffer8Impl *object;
746     HRESULT hr;
747
748     TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
749             iface, size, usage, fvf, pool, buffer);
750
751     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
752     if (!object)
753     {
754         ERR("Failed to allocate buffer memory.\n");
755         return D3DERR_OUTOFVIDEOMEMORY;
756     }
757
758     hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
759     if (FAILED(hr))
760     {
761         WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
762         HeapFree(GetProcessHeap(), 0, object);
763         return hr;
764     }
765
766     TRACE("Created vertex buffer %p.\n", object);
767     *buffer = (IDirect3DVertexBuffer8 *)object;
768
769     return D3D_OK;
770 }
771
772 static HRESULT WINAPI IDirect3DDevice8Impl_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size, DWORD usage,
773         D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
774 {
775     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
776     IDirect3DIndexBuffer8Impl *object;
777     HRESULT hr;
778
779     TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
780             iface, size, usage, format, pool, buffer);
781
782     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
783     if (!object)
784     {
785         ERR("Failed to allocate buffer memory.\n");
786         return D3DERR_OUTOFVIDEOMEMORY;
787     }
788
789     hr = indexbuffer_init(object, This, size, usage, format, pool);
790     if (FAILED(hr))
791     {
792         WARN("Failed to initialize index buffer, hr %#x.\n", hr);
793         HeapFree(GetProcessHeap(), 0, object);
794         return hr;
795     }
796
797     TRACE("Created index buffer %p.\n", object);
798     *buffer = (IDirect3DIndexBuffer8 *)object;
799
800     return D3D_OK;
801 }
802
803 static HRESULT IDirect3DDevice8Impl_CreateSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height,
804         D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level, IDirect3DSurface8 **ppSurface,
805         UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality)
806 {
807     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
808     IDirect3DSurface8Impl *object;
809     HRESULT hr;
810
811     TRACE("iface %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p,\n"
812             "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
813             iface, Width, Height, Format, Lockable, Discard, Level, ppSurface,
814             Usage, Pool, MultiSample, MultisampleQuality);
815
816     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
817     if (!object)
818     {
819         FIXME("Failed to allocate surface memory.\n");
820         return D3DERR_OUTOFVIDEOMEMORY;
821     }
822
823     hr = surface_init(object, This, Width, Height, Format, Lockable, Discard,
824             Level, Usage, Pool, MultiSample, MultisampleQuality);
825     if (FAILED(hr))
826     {
827         WARN("Failed to initialize surface, hr %#x.\n", hr);
828         HeapFree(GetProcessHeap(), 0, object);
829         return hr;
830     }
831
832     TRACE("Created surface %p.\n", object);
833     *ppSurface = (IDirect3DSurface8 *)object;
834
835     return D3D_OK;
836 }
837
838 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8** ppSurface) {
839     HRESULT hr;
840
841     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
842             iface, Width, Height, Format, MultiSample, Lockable, ppSurface);
843
844     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */,
845             0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, 0);
846
847     return hr;
848 }
849
850 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8** ppSurface) {
851     HRESULT hr;
852
853     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
854             iface, Width, Height, Format, MultiSample, ppSurface);
855
856     /* TODO: Verify that Discard is false */
857     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE,
858             0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample, 0);
859
860     return hr;
861 }
862
863 /*  IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
864 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8** ppSurface) {
865     HRESULT hr;
866
867     TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
868             iface, Width, Height, Format, ppSurface);
869
870     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE /* Discard */,
871             0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE,
872             0 /* MultisampleQuality */);
873
874     return hr;
875 }
876
877 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8 *pSourceSurface, CONST RECT *pSourceRects, UINT cRects, IDirect3DSurface8 *pDestinationSurface, CONST POINT *pDestPoints) {
878     IDirect3DSurface8Impl *Source = (IDirect3DSurface8Impl *) pSourceSurface;
879     IDirect3DSurface8Impl *Dest = (IDirect3DSurface8Impl *) pDestinationSurface;
880
881     HRESULT              hr = WINED3D_OK;
882     enum wined3d_format_id srcFormat, destFormat;
883     WINED3DSURFACE_DESC  winedesc;
884
885     TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
886             iface, pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
887
888     /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the
889      * destination texture is in WINED3DPOOL_DEFAULT. */
890
891     wined3d_mutex_lock();
892     IWineD3DSurface_GetDesc(Source->wineD3DSurface, &winedesc);
893     srcFormat = winedesc.format;
894
895     IWineD3DSurface_GetDesc(Dest->wineD3DSurface, &winedesc);
896     destFormat = winedesc.format;
897
898     /* Check that the source and destination formats match */
899     if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat) {
900         WARN("(%p) source %p format must match the dest %p format, returning WINED3DERR_INVALIDCALL\n", iface, pSourceSurface, pDestinationSurface);
901         wined3d_mutex_unlock();
902         return WINED3DERR_INVALIDCALL;
903     } else if (WINED3DFMT_UNKNOWN == destFormat) {
904         TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
905         IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
906     }
907
908     /* Quick if complete copy ... */
909     if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
910         IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
911     } else {
912         unsigned int i;
913         /* Copy rect by rect */
914         if (NULL != pSourceRects && NULL != pDestPoints) {
915             for (i = 0; i < cRects; ++i) {
916                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
917             }
918         } else {
919             for (i = 0; i < cRects; ++i) {
920                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
921             }
922         }
923     }
924     wined3d_mutex_unlock();
925
926     return hr;
927 }
928
929 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
930     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
931     HRESULT hr;
932
933     TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, pSourceTexture, pDestinationTexture);
934
935     wined3d_mutex_lock();
936     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
937     wined3d_mutex_unlock();
938
939     return hr;
940 }
941
942 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
943     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
944     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
945     HRESULT hr;
946
947     TRACE("iface %p, dst_surface %p.\n", iface, pDestSurface);
948
949     if (pDestSurface == NULL) {
950         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
951         return D3DERR_INVALIDCALL;
952     }
953
954     wined3d_mutex_lock();
955     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
956     wined3d_mutex_unlock();
957
958     return hr;
959 }
960
961 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
962     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
963     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
964     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
965     IWineD3DSurface *original_ds = NULL;
966     HRESULT hr;
967
968     TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, pRenderTarget, pNewZStencil);
969
970     wined3d_mutex_lock();
971
972     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &original_ds);
973     if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
974     {
975         hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, pZSurface ? pZSurface->wineD3DSurface : NULL);
976         if (SUCCEEDED(hr) && pSurface)
977             hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface->wineD3DSurface, TRUE);
978         if (FAILED(hr)) IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, original_ds);
979     }
980     if (original_ds) IWineD3DSurface_Release(original_ds);
981
982     wined3d_mutex_unlock();
983
984     return hr;
985 }
986
987 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
988     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
989     HRESULT hr = D3D_OK;
990     IWineD3DSurface *pRenderTarget;
991
992     TRACE("iface %p, render_target %p.\n", iface, ppRenderTarget);
993
994     if (ppRenderTarget == NULL) {
995         return D3DERR_INVALIDCALL;
996     }
997
998     wined3d_mutex_lock();
999     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
1000
1001     if (hr == D3D_OK && pRenderTarget != NULL) {
1002         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
1003         IWineD3DSurface_Release(pRenderTarget);
1004     } else {
1005         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
1006         *ppRenderTarget = NULL;
1007     }
1008     wined3d_mutex_unlock();
1009
1010     return hr;
1011 }
1012
1013 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
1014     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1015     HRESULT hr = D3D_OK;
1016     IWineD3DSurface *pZStencilSurface;
1017
1018     TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1019
1020     if(ppZStencilSurface == NULL){
1021         return D3DERR_INVALIDCALL;
1022     }
1023
1024     wined3d_mutex_lock();
1025     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
1026     if (hr == WINED3D_OK) {
1027         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
1028         IWineD3DSurface_Release(pZStencilSurface);
1029     }else{
1030         if (hr != WINED3DERR_NOTFOUND)
1031                 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1032         *ppZStencilSurface = NULL;
1033     }
1034     wined3d_mutex_unlock();
1035
1036     return hr;
1037 }
1038
1039 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
1040     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1041     HRESULT hr;
1042
1043     TRACE("iface %p.\n", iface);
1044
1045     wined3d_mutex_lock();
1046     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1047     wined3d_mutex_unlock();
1048
1049     return hr;
1050 }
1051
1052 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
1053     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1054     HRESULT hr;
1055
1056     TRACE("iface %p.\n", iface);
1057
1058     wined3d_mutex_lock();
1059     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1060     wined3d_mutex_unlock();
1061
1062     return hr;
1063 }
1064
1065 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
1066     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1067     HRESULT hr;
1068
1069     TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1070             iface, Count, pRects, Flags, Color, Z, Stencil);
1071
1072     wined3d_mutex_lock();
1073     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (const RECT *)pRects, Flags, Color, Z, Stencil);
1074     wined3d_mutex_unlock();
1075
1076     return hr;
1077 }
1078
1079 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
1080     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1081     HRESULT hr;
1082
1083     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1084
1085     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1086     wined3d_mutex_lock();
1087     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1088     wined3d_mutex_unlock();
1089
1090     return hr;
1091 }
1092
1093 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
1094     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1095     HRESULT hr;
1096
1097     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1098
1099     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1100     wined3d_mutex_lock();
1101     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1102     wined3d_mutex_unlock();
1103
1104     return hr;
1105 }
1106
1107 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
1108     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1109     HRESULT hr;
1110
1111     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1112
1113     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1114     wined3d_mutex_lock();
1115     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1116     wined3d_mutex_unlock();
1117
1118     return hr;
1119 }
1120
1121 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
1122     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1123     HRESULT hr;
1124
1125     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1126
1127     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1128     wined3d_mutex_lock();
1129     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1130     wined3d_mutex_unlock();
1131
1132     return hr;
1133 }
1134
1135 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
1136     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1137     HRESULT hr;
1138
1139     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1140
1141     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1142     wined3d_mutex_lock();
1143     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1144     wined3d_mutex_unlock();
1145
1146     return hr;
1147 }
1148
1149 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
1150     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1151     HRESULT hr;
1152
1153     TRACE("iface %p, material %p.\n", iface, pMaterial);
1154
1155     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1156     wined3d_mutex_lock();
1157     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1158     wined3d_mutex_unlock();
1159
1160     return hr;
1161 }
1162
1163 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
1164     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1165     HRESULT hr;
1166
1167     TRACE("iface %p, material %p.\n", iface, pMaterial);
1168
1169     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1170     wined3d_mutex_lock();
1171     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1172     wined3d_mutex_unlock();
1173
1174     return hr;
1175 }
1176
1177 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
1178     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1179     HRESULT hr;
1180
1181     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1182
1183     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1184     wined3d_mutex_lock();
1185     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1186     wined3d_mutex_unlock();
1187
1188     return hr;
1189 }
1190
1191 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
1192     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1193     HRESULT hr;
1194
1195     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1196
1197     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1198     wined3d_mutex_lock();
1199     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1200     wined3d_mutex_unlock();
1201
1202     return hr;
1203 }
1204
1205 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
1206     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1207     HRESULT hr;
1208
1209     TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1210
1211     wined3d_mutex_lock();
1212     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1213     wined3d_mutex_unlock();
1214
1215     return hr;
1216 }
1217
1218 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
1219     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1220     HRESULT hr;
1221
1222     TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1223
1224     wined3d_mutex_lock();
1225     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1226     wined3d_mutex_unlock();
1227
1228     return hr;
1229 }
1230
1231 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
1232     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1233     HRESULT hr;
1234
1235     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1236
1237     wined3d_mutex_lock();
1238     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1239     wined3d_mutex_unlock();
1240
1241     return hr;
1242 }
1243
1244 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
1245     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1246     HRESULT hr;
1247
1248     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1249
1250     wined3d_mutex_lock();
1251     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1252     wined3d_mutex_unlock();
1253
1254     return hr;
1255 }
1256
1257 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
1258     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1259     HRESULT hr;
1260
1261     TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1262
1263     wined3d_mutex_lock();
1264     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1265     wined3d_mutex_unlock();
1266
1267     return hr;
1268 }
1269
1270 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
1271     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1272     HRESULT hr;
1273
1274     TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1275
1276     wined3d_mutex_lock();
1277     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1278     wined3d_mutex_unlock();
1279
1280     return hr;
1281 }
1282
1283 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
1284     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1285     HRESULT hr;
1286
1287     TRACE("iface %p.\n", iface);
1288
1289     wined3d_mutex_lock();
1290     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1291     wined3d_mutex_unlock();
1292
1293     return hr;
1294 }
1295
1296 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
1297     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1298     IWineD3DStateBlock *stateblock;
1299     HRESULT hr;
1300
1301     TRACE("iface %p, token %p.\n", iface, pToken);
1302
1303     /* Tell wineD3D to endstateblock before anything else (in case we run out
1304      * of memory later and cause locking problems)
1305      */
1306     wined3d_mutex_lock();
1307     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &stateblock);
1308     if (hr != D3D_OK) {
1309         WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1310         wined3d_mutex_unlock();
1311         return hr;
1312     }
1313
1314     *pToken = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1315     wined3d_mutex_unlock();
1316
1317     if (*pToken == D3D8_INVALID_HANDLE)
1318     {
1319         ERR("Failed to create a handle\n");
1320         wined3d_mutex_lock();
1321         IWineD3DStateBlock_Release(stateblock);
1322         wined3d_mutex_unlock();
1323         return E_FAIL;
1324     }
1325     ++*pToken;
1326
1327     TRACE("Returning %#x (%p).\n", *pToken, stateblock);
1328
1329     return hr;
1330 }
1331
1332 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1333     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
1334     IWineD3DStateBlock *stateblock;
1335     HRESULT hr;
1336
1337     TRACE("iface %p, token %#x.\n", iface, Token);
1338
1339     wined3d_mutex_lock();
1340     stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1341     if (!stateblock)
1342     {
1343         WARN("Invalid handle (%#x) passed.\n", Token);
1344         wined3d_mutex_unlock();
1345         return D3DERR_INVALIDCALL;
1346     }
1347     hr = IWineD3DStateBlock_Apply(stateblock);
1348     wined3d_mutex_unlock();
1349
1350     return hr;
1351 }
1352
1353 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1354     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1355     IWineD3DStateBlock *stateblock;
1356     HRESULT hr;
1357
1358     TRACE("iface %p, token %#x.\n", iface, Token);
1359
1360     wined3d_mutex_lock();
1361     stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1362     if (!stateblock)
1363     {
1364         WARN("Invalid handle (%#x) passed.\n", Token);
1365         wined3d_mutex_unlock();
1366         return D3DERR_INVALIDCALL;
1367     }
1368     hr = IWineD3DStateBlock_Capture(stateblock);
1369     wined3d_mutex_unlock();
1370
1371     return hr;
1372 }
1373
1374 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1375     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1376     IWineD3DStateBlock *stateblock;
1377
1378     TRACE("iface %p, token %#x.\n", iface, Token);
1379
1380     wined3d_mutex_lock();
1381     stateblock = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1382
1383     if (!stateblock)
1384     {
1385         WARN("Invalid handle (%#x) passed.\n", Token);
1386         wined3d_mutex_unlock();
1387         return D3DERR_INVALIDCALL;
1388     }
1389
1390     if (IWineD3DStateBlock_Release((IUnknown *)stateblock))
1391     {
1392         ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1393     }
1394     wined3d_mutex_unlock();
1395
1396     return D3D_OK;
1397 }
1398
1399 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1400         D3DSTATEBLOCKTYPE Type, DWORD *handle)
1401 {
1402     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1403     IWineD3DStateBlock *stateblock;
1404     HRESULT hr;
1405
1406     TRACE("iface %p, type %#x, handle %p.\n", iface, Type, handle);
1407
1408     if (Type != D3DSBT_ALL
1409             && Type != D3DSBT_PIXELSTATE
1410             && Type != D3DSBT_VERTEXSTATE)
1411     {
1412         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1413         return D3DERR_INVALIDCALL;
1414     }
1415
1416     wined3d_mutex_lock();
1417     hr = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &stateblock);
1418     if (FAILED(hr))
1419     {
1420         wined3d_mutex_unlock();
1421         ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1422         return hr;
1423     }
1424
1425     *handle = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1426     wined3d_mutex_unlock();
1427
1428     if (*handle == D3D8_INVALID_HANDLE)
1429     {
1430         ERR("Failed to allocate a handle.\n");
1431         wined3d_mutex_lock();
1432         IWineD3DStateBlock_Release(stateblock);
1433         wined3d_mutex_unlock();
1434         return E_FAIL;
1435     }
1436     ++*handle;
1437
1438     TRACE("Returning %#x (%p).\n", *handle, stateblock);
1439
1440     return hr;
1441 }
1442
1443 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
1444     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1445     HRESULT hr;
1446
1447     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1448 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1449
1450     wined3d_mutex_lock();
1451     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1452     wined3d_mutex_unlock();
1453
1454     return hr;
1455 }
1456
1457 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
1458     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1459     HRESULT hr;
1460
1461     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1462
1463     wined3d_mutex_lock();
1464     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1465     wined3d_mutex_unlock();
1466
1467     return hr;
1468 }
1469
1470 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
1471     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1472     IWineD3DBaseTexture *retTexture;
1473     HRESULT hr;
1474
1475     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1476
1477     if(ppTexture == NULL){
1478         return D3DERR_INVALIDCALL;
1479     }
1480
1481     wined3d_mutex_lock();
1482     hr = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1483     if (FAILED(hr))
1484     {
1485         WARN("Failed to get texture for stage %u, hr %#x.\n", Stage, hr);
1486         wined3d_mutex_unlock();
1487         *ppTexture = NULL;
1488         return hr;
1489     }
1490
1491     if (retTexture)
1492     {
1493         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1494         IWineD3DBaseTexture_Release(retTexture);
1495     }
1496     else
1497     {
1498         *ppTexture = NULL;
1499     }
1500     wined3d_mutex_unlock();
1501
1502     return D3D_OK;
1503 }
1504
1505 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1506     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1507     HRESULT hr;
1508
1509     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1510
1511     wined3d_mutex_lock();
1512     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1513                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1514     wined3d_mutex_unlock();
1515
1516     return hr;
1517 }
1518
1519 static const struct tss_lookup
1520 {
1521     BOOL sampler_state;
1522     DWORD state;
1523 }
1524 tss_lookup[] =
1525 {
1526     {FALSE, WINED3DTSS_FORCE_DWORD},            /*  0, unused */
1527     {FALSE, WINED3DTSS_COLOROP},                /*  1, D3DTSS_COLOROP */
1528     {FALSE, WINED3DTSS_COLORARG1},              /*  2, D3DTSS_COLORARG1 */
1529     {FALSE, WINED3DTSS_COLORARG2},              /*  3, D3DTSS_COLORARG2 */
1530     {FALSE, WINED3DTSS_ALPHAOP},                /*  4, D3DTSS_ALPHAOP */
1531     {FALSE, WINED3DTSS_ALPHAARG1},              /*  5, D3DTSS_ALPHAARG1 */
1532     {FALSE, WINED3DTSS_ALPHAARG2},              /*  6, D3DTSS_ALPHAARG2 */
1533     {FALSE, WINED3DTSS_BUMPENVMAT00},           /*  7, D3DTSS_BUMPENVMAT00 */
1534     {FALSE, WINED3DTSS_BUMPENVMAT01},           /*  8, D3DTSS_BUMPENVMAT01 */
1535     {FALSE, WINED3DTSS_BUMPENVMAT10},           /*  9, D3DTSS_BUMPENVMAT10 */
1536     {FALSE, WINED3DTSS_BUMPENVMAT11},           /* 10, D3DTSS_BUMPENVMAT11 */
1537     {FALSE, WINED3DTSS_TEXCOORDINDEX},          /* 11, D3DTSS_TEXCOORDINDEX */
1538     {FALSE, WINED3DTSS_FORCE_DWORD},            /* 12, unused */
1539     {TRUE,  WINED3DSAMP_ADDRESSU},              /* 13, D3DTSS_ADDRESSU */
1540     {TRUE,  WINED3DSAMP_ADDRESSV},              /* 14, D3DTSS_ADDRESSV */
1541     {TRUE,  WINED3DSAMP_BORDERCOLOR},           /* 15, D3DTSS_BORDERCOLOR */
1542     {TRUE,  WINED3DSAMP_MAGFILTER},             /* 16, D3DTSS_MAGFILTER */
1543     {TRUE,  WINED3DSAMP_MINFILTER},             /* 17, D3DTSS_MINFILTER */
1544     {TRUE,  WINED3DSAMP_MIPFILTER},             /* 18, D3DTSS_MIPFILTER */
1545     {TRUE,  WINED3DSAMP_MIPMAPLODBIAS},         /* 19, D3DTSS_MIPMAPLODBIAS */
1546     {TRUE,  WINED3DSAMP_MAXMIPLEVEL},           /* 20, D3DTSS_MAXMIPLEVEL */
1547     {TRUE,  WINED3DSAMP_MAXANISOTROPY},         /* 21, D3DTSS_MAXANISOTROPY */
1548     {FALSE, WINED3DTSS_BUMPENVLSCALE},          /* 22, D3DTSS_BUMPENVLSCALE */
1549     {FALSE, WINED3DTSS_BUMPENVLOFFSET},         /* 23, D3DTSS_BUMPENVLOFFSET */
1550     {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS},  /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1551     {TRUE,  WINED3DSAMP_ADDRESSW},              /* 25, D3DTSS_ADDRESSW */
1552     {FALSE, WINED3DTSS_COLORARG0},              /* 26, D3DTSS_COLORARG0 */
1553     {FALSE, WINED3DTSS_ALPHAARG0},              /* 27, D3DTSS_ALPHAARG0 */
1554     {FALSE, WINED3DTSS_RESULTARG},              /* 28, D3DTSS_RESULTARG */
1555 };
1556
1557 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1558     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1559     const struct tss_lookup *l = &tss_lookup[Type];
1560     HRESULT hr;
1561
1562     TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1563
1564     wined3d_mutex_lock();
1565     if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1566     else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1567     wined3d_mutex_unlock();
1568
1569     return hr;
1570 }
1571
1572 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1573     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1574     const struct tss_lookup *l = &tss_lookup[Type];
1575     HRESULT hr;
1576
1577     TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1578
1579     wined3d_mutex_lock();
1580     if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1581     else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1582     wined3d_mutex_unlock();
1583
1584     return hr;
1585 }
1586
1587 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1588     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1589     HRESULT hr;
1590
1591     TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1592
1593     wined3d_mutex_lock();
1594     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1595     wined3d_mutex_unlock();
1596
1597     return hr;
1598 }
1599
1600 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(IDirect3DDevice8 *iface,
1601         DWORD info_id, void *info, DWORD info_size)
1602 {
1603     FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1604
1605     return D3D_OK;
1606 }
1607
1608 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1609     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1610     HRESULT hr;
1611
1612     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1613
1614     wined3d_mutex_lock();
1615     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1616     wined3d_mutex_unlock();
1617
1618     return hr;
1619 }
1620
1621 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1622     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1623     HRESULT hr;
1624
1625     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1626
1627     wined3d_mutex_lock();
1628     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1629     wined3d_mutex_unlock();
1630
1631     return hr;
1632 }
1633
1634 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1635     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1636     HRESULT hr;
1637
1638     TRACE("iface %p, palette_idx %u.\n", iface, PaletteNumber);
1639
1640     wined3d_mutex_lock();
1641     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1642     wined3d_mutex_unlock();
1643
1644     return hr;
1645 }
1646
1647 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1648     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1649     HRESULT hr;
1650
1651     TRACE("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1652
1653     wined3d_mutex_lock();
1654     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1655     wined3d_mutex_unlock();
1656
1657     return hr;
1658 }
1659
1660 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface, D3DPRIMITIVETYPE PrimitiveType,
1661         UINT StartVertex, UINT PrimitiveCount)
1662 {
1663     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1664     HRESULT hr;
1665
1666     TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1667             iface, PrimitiveType, StartVertex, PrimitiveCount);
1668
1669     wined3d_mutex_lock();
1670     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1671     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1672             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1673     wined3d_mutex_unlock();
1674
1675     return hr;
1676 }
1677
1678 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1679                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1680     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1681     HRESULT hr;
1682
1683     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1684             iface, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1685
1686     wined3d_mutex_lock();
1687     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1688     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1689             vertex_count_from_primitive_count(PrimitiveType, primCount));
1690     wined3d_mutex_unlock();
1691
1692     return hr;
1693 }
1694
1695 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1696     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1697     HRESULT hr;
1698
1699     TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1700             iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1701
1702     wined3d_mutex_lock();
1703     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1704     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1705             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1706             pVertexStreamZeroData, VertexStreamZeroStride);
1707     wined3d_mutex_unlock();
1708
1709     return hr;
1710 }
1711
1712 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1713                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1714                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1715                                                              UINT VertexStreamZeroStride) {
1716     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1717     HRESULT hr;
1718
1719     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1720             "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1721             iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1722             pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1723
1724     wined3d_mutex_lock();
1725     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1726     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1727             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1728             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1729     wined3d_mutex_unlock();
1730
1731     return hr;
1732 }
1733
1734 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1735     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1736     HRESULT hr;
1737     IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
1738
1739     TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
1740             iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, Flags);
1741
1742     wined3d_mutex_lock();
1743     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1744     wined3d_mutex_unlock();
1745
1746     return hr;
1747 }
1748
1749 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
1750         const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
1751 {
1752     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1753     IDirect3DVertexShader8Impl *object;
1754     DWORD shader_handle;
1755     DWORD handle;
1756     HRESULT hr;
1757
1758     TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
1759             iface, declaration, byte_code, shader, usage);
1760
1761     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1762     if (!object)
1763     {
1764         ERR("Failed to allocate vertex shader memory.\n");
1765         *shader = 0;
1766         return E_OUTOFMEMORY;
1767     }
1768
1769     wined3d_mutex_lock();
1770     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
1771     wined3d_mutex_unlock();
1772     if (handle == D3D8_INVALID_HANDLE)
1773     {
1774         ERR("Failed to allocate vertex shader handle.\n");
1775         HeapFree(GetProcessHeap(), 0, object);
1776         *shader = 0;
1777         return E_OUTOFMEMORY;
1778     }
1779
1780     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
1781
1782     hr = vertexshader_init(object, This, declaration, byte_code, shader_handle, usage);
1783     if (FAILED(hr))
1784     {
1785         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
1786         wined3d_mutex_lock();
1787         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
1788         wined3d_mutex_unlock();
1789         HeapFree(GetProcessHeap(), 0, object);
1790         *shader = 0;
1791         return hr;
1792     }
1793
1794     TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
1795     *shader = shader_handle;
1796
1797     return D3D_OK;
1798 }
1799
1800 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1801 {
1802     IDirect3DVertexDeclaration8Impl *d3d8_declaration;
1803     HRESULT hr;
1804     int p, low, high; /* deliberately signed */
1805     struct FvfToDecl *convertedDecls = This->decls;
1806
1807     TRACE("Searching for declaration for fvf %08x... ", fvf);
1808
1809     low = 0;
1810     high = This->numConvertedDecls - 1;
1811     while(low <= high) {
1812         p = (low + high) >> 1;
1813         TRACE("%d ", p);
1814         if(convertedDecls[p].fvf == fvf) {
1815             TRACE("found %p\n", convertedDecls[p].decl);
1816             return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
1817         } else if(convertedDecls[p].fvf < fvf) {
1818             low = p + 1;
1819         } else {
1820             high = p - 1;
1821         }
1822     }
1823     TRACE("not found. Creating and inserting at position %d.\n", low);
1824
1825     d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
1826     if (!d3d8_declaration)
1827     {
1828         ERR("Memory allocation failed.\n");
1829         return NULL;
1830     }
1831
1832     hr = vertexdeclaration_init_fvf(d3d8_declaration, This, fvf);
1833     if (FAILED(hr))
1834     {
1835         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
1836         HeapFree(GetProcessHeap(), 0, d3d8_declaration);
1837         return NULL;
1838     }
1839
1840     if(This->declArraySize == This->numConvertedDecls) {
1841         int grow = This->declArraySize / 2;
1842         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1843                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1844         if(!convertedDecls) {
1845             /* This will destroy it */
1846             IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
1847             return NULL;
1848         }
1849         This->decls = convertedDecls;
1850         This->declArraySize += grow;
1851     }
1852
1853     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
1854     convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
1855     convertedDecls[low].fvf = fvf;
1856     This->numConvertedDecls++;
1857
1858     TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
1859     return d3d8_declaration;
1860 }
1861
1862 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1863     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1864     IDirect3DVertexShader8Impl *shader;
1865     HRESULT hr;
1866
1867     TRACE("iface %p, shader %#x.\n", iface, pShader);
1868
1869     if (VS_HIGHESTFIXEDFXF >= pShader) {
1870         TRACE("Setting FVF, %#x\n", pShader);
1871
1872         wined3d_mutex_lock();
1873         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1874                 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
1875         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1876         wined3d_mutex_unlock();
1877
1878         return D3D_OK;
1879     }
1880
1881     TRACE("Setting shader\n");
1882
1883     wined3d_mutex_lock();
1884     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1885     if (!shader)
1886     {
1887         WARN("Invalid handle (%#x) passed.\n", pShader);
1888         wined3d_mutex_unlock();
1889
1890         return D3DERR_INVALIDCALL;
1891     }
1892
1893     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1894             ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
1895     if (SUCCEEDED(hr)) hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wineD3DVertexShader);
1896     wined3d_mutex_unlock();
1897
1898     TRACE("Returning hr %#x\n", hr);
1899
1900     return hr;
1901 }
1902
1903 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1904     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1905     IWineD3DVertexDeclaration *wined3d_declaration;
1906     IDirect3DVertexDeclaration8 *d3d8_declaration;
1907     HRESULT hrc;
1908
1909     TRACE("iface %p, shader %p.\n", iface, ppShader);
1910
1911     wined3d_mutex_lock();
1912     hrc = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
1913     if (FAILED(hrc))
1914     {
1915         wined3d_mutex_unlock();
1916         WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
1917                 This, hrc, This->WineD3DDevice);
1918         return hrc;
1919     }
1920
1921     if (!wined3d_declaration)
1922     {
1923         wined3d_mutex_unlock();
1924         *ppShader = 0;
1925         return D3D_OK;
1926     }
1927
1928     hrc = IWineD3DVertexDeclaration_GetParent(wined3d_declaration, (IUnknown **)&d3d8_declaration);
1929     IWineD3DVertexDeclaration_Release(wined3d_declaration);
1930     wined3d_mutex_unlock();
1931     if (SUCCEEDED(hrc))
1932     {
1933         *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
1934         IDirect3DVertexDeclaration8_Release(d3d8_declaration);
1935     }
1936
1937     TRACE("(%p) : returning %#x\n", This, *ppShader);
1938
1939     return hrc;
1940 }
1941
1942 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1943     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1944     IDirect3DVertexShader8Impl *shader;
1945     IWineD3DVertexShader *cur = NULL;
1946
1947     TRACE("iface %p, shader %#x.\n", iface, pShader);
1948
1949     wined3d_mutex_lock();
1950     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1951     if (!shader)
1952     {
1953         WARN("Invalid handle (%#x) passed.\n", pShader);
1954         wined3d_mutex_unlock();
1955
1956         return D3DERR_INVALIDCALL;
1957     }
1958
1959     IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
1960
1961     if (cur)
1962     {
1963         if (cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
1964         IWineD3DVertexShader_Release(cur);
1965     }
1966
1967     wined3d_mutex_unlock();
1968
1969     if (IUnknown_Release((IUnknown *)shader))
1970     {
1971         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
1972     }
1973
1974     return D3D_OK;
1975 }
1976
1977 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1978     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1979     HRESULT hr;
1980
1981     TRACE("iface %p, register %u, data %p, count %u.\n",
1982             iface, Register, pConstantData, ConstantCount);
1983
1984     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
1985         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
1986              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
1987         return D3DERR_INVALIDCALL;
1988     }
1989
1990     wined3d_mutex_lock();
1991     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
1992     wined3d_mutex_unlock();
1993
1994     return hr;
1995 }
1996
1997 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1998     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1999     HRESULT hr;
2000
2001     TRACE("iface %p, register %u, data %p, count %u.\n",
2002             iface, Register, pConstantData, ConstantCount);
2003
2004     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2005         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2006              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2007         return D3DERR_INVALIDCALL;
2008     }
2009
2010     wined3d_mutex_lock();
2011     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2012     wined3d_mutex_unlock();
2013
2014     return hr;
2015 }
2016
2017 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
2018     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2019     IDirect3DVertexDeclaration8Impl *declaration;
2020     IDirect3DVertexShader8Impl *shader;
2021
2022     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2023             iface, pVertexShader, pData, pSizeOfData);
2024
2025     wined3d_mutex_lock();
2026     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2027     wined3d_mutex_unlock();
2028
2029     if (!shader)
2030     {
2031         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2032         return D3DERR_INVALIDCALL;
2033     }
2034     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2035
2036     /* If pData is NULL, we just return the required size of the buffer. */
2037     if (!pData) {
2038         *pSizeOfData = declaration->elements_size;
2039         return D3D_OK;
2040     }
2041
2042     /* MSDN claims that if *pSizeOfData is smaller than the required size
2043      * we should write the required size and return D3DERR_MOREDATA.
2044      * That's not actually true. */
2045     if (*pSizeOfData < declaration->elements_size) {
2046         return D3DERR_INVALIDCALL;
2047     }
2048
2049     CopyMemory(pData, declaration->elements, declaration->elements_size);
2050
2051     return D3D_OK;
2052 }
2053
2054 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
2055     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2056     IDirect3DVertexShader8Impl *shader = NULL;
2057     HRESULT hr;
2058
2059     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2060             iface, pVertexShader, pData, pSizeOfData);
2061
2062     wined3d_mutex_lock();
2063     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2064     if (!shader)
2065     {
2066         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2067         wined3d_mutex_unlock();
2068
2069         return D3DERR_INVALIDCALL;
2070     }
2071
2072     if (!shader->wineD3DVertexShader)
2073     {
2074         wined3d_mutex_unlock();
2075         *pSizeOfData = 0;
2076         return D3D_OK;
2077     }
2078
2079     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, pSizeOfData);
2080     wined3d_mutex_unlock();
2081
2082     return hr;
2083 }
2084
2085 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
2086     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2087     HRESULT hr;
2088     IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2089
2090     TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, pIndexData, baseVertexIndex);
2091
2092     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2093      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2094      * vertex buffers can't be created to address them with an index that requires the 32nd bit
2095      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2096      * problem)
2097      */
2098     wined3d_mutex_lock();
2099     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2100     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2101             ib ? ib->wineD3DIndexBuffer : NULL,
2102             ib ? ib->format : WINED3DFMT_UNKNOWN);
2103     wined3d_mutex_unlock();
2104
2105     return hr;
2106 }
2107
2108 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
2109     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2110     IWineD3DBuffer *retIndexData = NULL;
2111     HRESULT rc = D3D_OK;
2112
2113     TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, ppIndexData, pBaseVertexIndex);
2114
2115     if(ppIndexData == NULL){
2116         return D3DERR_INVALIDCALL;
2117     }
2118
2119     /* The case from UINT to INT is safe because d3d8 will never set negative values */
2120     wined3d_mutex_lock();
2121     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2122     rc = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2123     if (SUCCEEDED(rc) && retIndexData) {
2124         IWineD3DBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
2125         IWineD3DBuffer_Release(retIndexData);
2126     } else {
2127         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
2128         *ppIndexData = NULL;
2129     }
2130     wined3d_mutex_unlock();
2131
2132     return rc;
2133 }
2134
2135 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(IDirect3DDevice8 *iface,
2136         const DWORD *byte_code, DWORD *shader)
2137 {
2138     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2139     IDirect3DPixelShader8Impl *object;
2140     DWORD shader_handle;
2141     DWORD handle;
2142     HRESULT hr;
2143
2144     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2145
2146     if (!shader)
2147     {
2148         TRACE("(%p) Invalid call\n", This);
2149         return D3DERR_INVALIDCALL;
2150     }
2151
2152     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2153     if (!object)
2154     {
2155         ERR("Failed to allocate pixel shader memmory.\n");
2156         return E_OUTOFMEMORY;
2157     }
2158
2159     wined3d_mutex_lock();
2160     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2161     wined3d_mutex_unlock();
2162     if (handle == D3D8_INVALID_HANDLE)
2163     {
2164         ERR("Failed to allocate pixel shader handle.\n");
2165         HeapFree(GetProcessHeap(), 0, object);
2166         return E_OUTOFMEMORY;
2167     }
2168
2169     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2170
2171     hr = pixelshader_init(object, This, byte_code, shader_handle);
2172     if (FAILED(hr))
2173     {
2174         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2175         wined3d_mutex_lock();
2176         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_PS);
2177         wined3d_mutex_unlock();
2178         HeapFree(GetProcessHeap(), 0, object);
2179         *shader = 0;
2180         return hr;
2181     }
2182
2183     TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2184     *shader = shader_handle;
2185
2186     return D3D_OK;
2187 }
2188
2189 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2190     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2191     IDirect3DPixelShader8Impl *shader;
2192     HRESULT hr;
2193
2194     TRACE("iface %p, shader %#x.\n", iface, pShader);
2195
2196     wined3d_mutex_lock();
2197
2198     if (!pShader)
2199     {
2200         hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2201         wined3d_mutex_unlock();
2202         return hr;
2203     }
2204
2205     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2206     if (!shader)
2207     {
2208         WARN("Invalid handle (%#x) passed.\n", pShader);
2209         wined3d_mutex_unlock();
2210         return D3DERR_INVALIDCALL;
2211     }
2212
2213     TRACE("(%p) : Setting shader %p\n", This, shader);
2214     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wineD3DPixelShader);
2215     wined3d_mutex_unlock();
2216
2217     return hr;
2218 }
2219
2220 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
2221     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2222     IWineD3DPixelShader *object;
2223     HRESULT hrc = D3D_OK;
2224
2225     TRACE("iface %p, shader %p.\n", iface, ppShader);
2226
2227     if (NULL == ppShader) {
2228         TRACE("(%p) Invalid call\n", This);
2229         return D3DERR_INVALIDCALL;
2230     }
2231
2232     wined3d_mutex_lock();
2233     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
2234     if (D3D_OK == hrc && NULL != object) {
2235         IDirect3DPixelShader8Impl *d3d8_shader;
2236         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
2237         IWineD3DPixelShader_Release(object);
2238         *ppShader = d3d8_shader->handle;
2239         IDirect3DPixelShader8_Release((IDirect3DPixelShader8 *)d3d8_shader);
2240     } else {
2241         *ppShader = 0;
2242     }
2243     wined3d_mutex_unlock();
2244
2245     TRACE("(%p) : returning %#x\n", This, *ppShader);
2246
2247     return hrc;
2248 }
2249
2250 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2251     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2252     IDirect3DPixelShader8Impl *shader;
2253     IWineD3DPixelShader *cur = NULL;
2254
2255     TRACE("iface %p, shader %#x.\n", iface, pShader);
2256
2257     wined3d_mutex_lock();
2258
2259     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2260     if (!shader)
2261     {
2262         WARN("Invalid handle (%#x) passed.\n", pShader);
2263         wined3d_mutex_unlock();
2264         return D3DERR_INVALIDCALL;
2265     }
2266
2267     IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
2268
2269     if (cur)
2270     {
2271         if (cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
2272         IWineD3DPixelShader_Release(cur);
2273     }
2274
2275     wined3d_mutex_unlock();
2276
2277     if (IUnknown_Release((IUnknown *)shader))
2278     {
2279         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2280     }
2281
2282     return D3D_OK;
2283 }
2284
2285 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
2286     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2287     HRESULT hr;
2288
2289     TRACE("iface %p, register %u, data %p, count %u.\n",
2290             iface, Register, pConstantData, ConstantCount);
2291
2292     wined3d_mutex_lock();
2293     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2294     wined3d_mutex_unlock();
2295
2296     return hr;
2297 }
2298
2299 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
2300     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2301     HRESULT hr;
2302
2303     TRACE("iface %p, register %u, data %p, count %u.\n",
2304             iface, Register, pConstantData, ConstantCount);
2305
2306     wined3d_mutex_lock();
2307     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2308     wined3d_mutex_unlock();
2309
2310     return hr;
2311 }
2312
2313 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
2314     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2315     IDirect3DPixelShader8Impl *shader = NULL;
2316     HRESULT hr;
2317
2318     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2319             iface, pPixelShader, pData, pSizeOfData);
2320
2321     wined3d_mutex_lock();
2322     shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2323     if (!shader)
2324     {
2325         WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2326         wined3d_mutex_unlock();
2327
2328         return D3DERR_INVALIDCALL;
2329     }
2330
2331     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, pSizeOfData);
2332     wined3d_mutex_unlock();
2333
2334     return hr;
2335 }
2336
2337 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2338     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2339     HRESULT hr;
2340
2341     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2342             iface, Handle, pNumSegs, pRectPatchInfo);
2343
2344     wined3d_mutex_lock();
2345     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2346     wined3d_mutex_unlock();
2347
2348     return hr;
2349 }
2350
2351 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2352     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2353     HRESULT hr;
2354
2355     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2356             iface, Handle, pNumSegs, pTriPatchInfo);
2357
2358     wined3d_mutex_lock();
2359     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2360     wined3d_mutex_unlock();
2361
2362     return hr;
2363 }
2364
2365 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
2366     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2367     HRESULT hr;
2368
2369     TRACE("iface %p, handle %#x.\n", iface, Handle);
2370
2371     wined3d_mutex_lock();
2372     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2373     wined3d_mutex_unlock();
2374
2375     return hr;
2376 }
2377
2378 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
2379     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2380     HRESULT hr;
2381
2382     TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2383             iface, StreamNumber, pStreamData, Stride);
2384
2385     wined3d_mutex_lock();
2386     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2387                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2388                                         0/* Offset in bytes */, Stride);
2389     wined3d_mutex_unlock();
2390
2391     return hr;
2392 }
2393
2394 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
2395     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2396     IWineD3DBuffer *retStream = NULL;
2397     HRESULT rc = D3D_OK;
2398
2399     TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2400             iface, StreamNumber, pStream, pStride);
2401
2402     if(pStream == NULL){
2403         return D3DERR_INVALIDCALL;
2404     }
2405
2406     wined3d_mutex_lock();
2407     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, 0 /* Offset in bytes */, pStride);
2408     if (rc == D3D_OK  && NULL != retStream) {
2409         IWineD3DBuffer_GetParent(retStream, (IUnknown **)pStream);
2410         IWineD3DBuffer_Release(retStream);
2411     }else{
2412         if (rc != D3D_OK){
2413             FIXME("Call to GetStreamSource failed %p\n",  pStride);
2414         }
2415         *pStream = NULL;
2416     }
2417     wined3d_mutex_unlock();
2418
2419     return rc;
2420 }
2421
2422 static const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2423 {
2424     IDirect3DDevice8Impl_QueryInterface,
2425     IDirect3DDevice8Impl_AddRef,
2426     IDirect3DDevice8Impl_Release,
2427     IDirect3DDevice8Impl_TestCooperativeLevel,
2428     IDirect3DDevice8Impl_GetAvailableTextureMem,
2429     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2430     IDirect3DDevice8Impl_GetDirect3D,
2431     IDirect3DDevice8Impl_GetDeviceCaps,
2432     IDirect3DDevice8Impl_GetDisplayMode,
2433     IDirect3DDevice8Impl_GetCreationParameters,
2434     IDirect3DDevice8Impl_SetCursorProperties,
2435     IDirect3DDevice8Impl_SetCursorPosition,
2436     IDirect3DDevice8Impl_ShowCursor,
2437     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2438     IDirect3DDevice8Impl_Reset,
2439     IDirect3DDevice8Impl_Present,
2440     IDirect3DDevice8Impl_GetBackBuffer,
2441     IDirect3DDevice8Impl_GetRasterStatus,
2442     IDirect3DDevice8Impl_SetGammaRamp,
2443     IDirect3DDevice8Impl_GetGammaRamp,
2444     IDirect3DDevice8Impl_CreateTexture,
2445     IDirect3DDevice8Impl_CreateVolumeTexture,
2446     IDirect3DDevice8Impl_CreateCubeTexture,
2447     IDirect3DDevice8Impl_CreateVertexBuffer,
2448     IDirect3DDevice8Impl_CreateIndexBuffer,
2449     IDirect3DDevice8Impl_CreateRenderTarget,
2450     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2451     IDirect3DDevice8Impl_CreateImageSurface,
2452     IDirect3DDevice8Impl_CopyRects,
2453     IDirect3DDevice8Impl_UpdateTexture,
2454     IDirect3DDevice8Impl_GetFrontBuffer,
2455     IDirect3DDevice8Impl_SetRenderTarget,
2456     IDirect3DDevice8Impl_GetRenderTarget,
2457     IDirect3DDevice8Impl_GetDepthStencilSurface,
2458     IDirect3DDevice8Impl_BeginScene,
2459     IDirect3DDevice8Impl_EndScene,
2460     IDirect3DDevice8Impl_Clear,
2461     IDirect3DDevice8Impl_SetTransform,
2462     IDirect3DDevice8Impl_GetTransform,
2463     IDirect3DDevice8Impl_MultiplyTransform,
2464     IDirect3DDevice8Impl_SetViewport,
2465     IDirect3DDevice8Impl_GetViewport,
2466     IDirect3DDevice8Impl_SetMaterial,
2467     IDirect3DDevice8Impl_GetMaterial,
2468     IDirect3DDevice8Impl_SetLight,
2469     IDirect3DDevice8Impl_GetLight,
2470     IDirect3DDevice8Impl_LightEnable,
2471     IDirect3DDevice8Impl_GetLightEnable,
2472     IDirect3DDevice8Impl_SetClipPlane,
2473     IDirect3DDevice8Impl_GetClipPlane,
2474     IDirect3DDevice8Impl_SetRenderState,
2475     IDirect3DDevice8Impl_GetRenderState,
2476     IDirect3DDevice8Impl_BeginStateBlock,
2477     IDirect3DDevice8Impl_EndStateBlock,
2478     IDirect3DDevice8Impl_ApplyStateBlock,
2479     IDirect3DDevice8Impl_CaptureStateBlock,
2480     IDirect3DDevice8Impl_DeleteStateBlock,
2481     IDirect3DDevice8Impl_CreateStateBlock,
2482     IDirect3DDevice8Impl_SetClipStatus,
2483     IDirect3DDevice8Impl_GetClipStatus,
2484     IDirect3DDevice8Impl_GetTexture,
2485     IDirect3DDevice8Impl_SetTexture,
2486     IDirect3DDevice8Impl_GetTextureStageState,
2487     IDirect3DDevice8Impl_SetTextureStageState,
2488     IDirect3DDevice8Impl_ValidateDevice,
2489     IDirect3DDevice8Impl_GetInfo,
2490     IDirect3DDevice8Impl_SetPaletteEntries,
2491     IDirect3DDevice8Impl_GetPaletteEntries,
2492     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2493     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2494     IDirect3DDevice8Impl_DrawPrimitive,
2495     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2496     IDirect3DDevice8Impl_DrawPrimitiveUP,
2497     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2498     IDirect3DDevice8Impl_ProcessVertices,
2499     IDirect3DDevice8Impl_CreateVertexShader,
2500     IDirect3DDevice8Impl_SetVertexShader,
2501     IDirect3DDevice8Impl_GetVertexShader,
2502     IDirect3DDevice8Impl_DeleteVertexShader,
2503     IDirect3DDevice8Impl_SetVertexShaderConstant,
2504     IDirect3DDevice8Impl_GetVertexShaderConstant,
2505     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2506     IDirect3DDevice8Impl_GetVertexShaderFunction,
2507     IDirect3DDevice8Impl_SetStreamSource,
2508     IDirect3DDevice8Impl_GetStreamSource,
2509     IDirect3DDevice8Impl_SetIndices,
2510     IDirect3DDevice8Impl_GetIndices,
2511     IDirect3DDevice8Impl_CreatePixelShader,
2512     IDirect3DDevice8Impl_SetPixelShader,
2513     IDirect3DDevice8Impl_GetPixelShader,
2514     IDirect3DDevice8Impl_DeletePixelShader,
2515     IDirect3DDevice8Impl_SetPixelShaderConstant,
2516     IDirect3DDevice8Impl_GetPixelShaderConstant,
2517     IDirect3DDevice8Impl_GetPixelShaderFunction,
2518     IDirect3DDevice8Impl_DrawRectPatch,
2519     IDirect3DDevice8Impl_DrawTriPatch,
2520     IDirect3DDevice8Impl_DeletePatch
2521 };
2522
2523 /* IWineD3DDeviceParent IUnknown methods */
2524
2525 static inline struct IDirect3DDevice8Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2526 {
2527     return (struct IDirect3DDevice8Impl *)((char*)iface
2528             - FIELD_OFFSET(struct IDirect3DDevice8Impl, device_parent_vtbl));
2529 }
2530
2531 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
2532 {
2533     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2534     return IDirect3DDevice8Impl_QueryInterface((IDirect3DDevice8 *)This, riid, object);
2535 }
2536
2537 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2538 {
2539     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2540     return IDirect3DDevice8Impl_AddRef((IDirect3DDevice8 *)This);
2541 }
2542
2543 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2544 {
2545     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2546     return IDirect3DDevice8Impl_Release((IDirect3DDevice8 *)This);
2547 }
2548
2549 /* IWineD3DDeviceParent methods */
2550
2551 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2552 {
2553     TRACE("iface %p, device %p\n", iface, device);
2554 }
2555
2556 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2557         IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2558         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2559 {
2560     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2561     IDirect3DSurface8Impl *d3d_surface;
2562     BOOL lockable = TRUE;
2563     HRESULT hr;
2564
2565     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
2566             "\tpool %#x, level %u, face %u, surface %p\n",
2567             iface, superior, width, height, format, usage, pool, level, face, surface);
2568
2569
2570     if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2571
2572     hr = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)This, width, height,
2573             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2574             (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2575     if (FAILED(hr))
2576     {
2577         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2578         return hr;
2579     }
2580
2581     *surface = d3d_surface->wineD3DSurface;
2582     IWineD3DSurface_AddRef(*surface);
2583
2584     d3d_surface->container = superior;
2585     IUnknown_Release(d3d_surface->parentDevice);
2586     d3d_surface->parentDevice = NULL;
2587
2588     IDirect3DSurface8_Release((IDirect3DSurface8 *)d3d_surface);
2589     d3d_surface->forwardReference = superior;
2590
2591     return hr;
2592 }
2593
2594 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2595         IUnknown *superior, UINT width, UINT height, enum wined3d_format_id format,
2596         WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
2597         IWineD3DSurface **surface)
2598 {
2599     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2600     IDirect3DSurface8Impl *d3d_surface;
2601     HRESULT hr;
2602
2603     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2604             "\tmultisample_quality %u, lockable %u, surface %p\n",
2605             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
2606
2607     hr = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)This, width, height,
2608             d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2609     if (FAILED(hr))
2610     {
2611         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2612         return hr;
2613     }
2614
2615     *surface = d3d_surface->wineD3DSurface;
2616     IWineD3DSurface_AddRef(*surface);
2617
2618     d3d_surface->container = (IUnknown *)This;
2619     /* Implicit surfaces are created with an refcount of 0 */
2620     IUnknown_Release((IUnknown *)d3d_surface);
2621
2622     return hr;
2623 }
2624
2625 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2626         UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
2627         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2628 {
2629     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2630     IDirect3DSurface8Impl *d3d_surface;
2631     HRESULT hr;
2632
2633     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2634             "\tmultisample_quality %u, discard %u, surface %p\n",
2635             iface, width, height, format, multisample_type, multisample_quality, discard, surface);
2636
2637     hr = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)This, width, height,
2638             d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2639     if (FAILED(hr))
2640     {
2641         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2642         return hr;
2643     }
2644
2645     *surface = d3d_surface->wineD3DSurface;
2646     IWineD3DSurface_AddRef(*surface);
2647
2648     d3d_surface->container = (IUnknown *)This;
2649     /* Implicit surfaces are created with an refcount of 0 */
2650     IUnknown_Release((IUnknown *)d3d_surface);
2651
2652     return hr;
2653 }
2654
2655 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2656         IUnknown *superior, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2657         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2658 {
2659     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2660     IDirect3DVolume8Impl *object;
2661     HRESULT hr;
2662
2663     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2664             iface, superior, width, height, depth, format, pool, usage, volume);
2665
2666     /* Allocate the storage for the device */
2667     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2668     if (!object)
2669     {
2670         FIXME("Allocation of memory failed\n");
2671         *volume = NULL;
2672         return D3DERR_OUTOFVIDEOMEMORY;
2673     }
2674
2675     hr = volume_init(object, This, width, height, depth, usage, format, pool);
2676     if (FAILED(hr))
2677     {
2678         WARN("Failed to initialize volume, hr %#x.\n", hr);
2679         HeapFree(GetProcessHeap(), 0, object);
2680         return hr;
2681     }
2682
2683     *volume = object->wineD3DVolume;
2684     IWineD3DVolume_AddRef(*volume);
2685     IDirect3DVolume8_Release((IDirect3DVolume8 *)object);
2686
2687     object->container = superior;
2688     object->forwardReference = superior;
2689
2690     TRACE("(%p) Created volume %p\n", iface, object);
2691
2692     return hr;
2693 }
2694
2695 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2696         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2697 {
2698     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2699     IDirect3DSwapChain8Impl *d3d_swapchain;
2700     D3DPRESENT_PARAMETERS local_parameters;
2701     HRESULT hr;
2702
2703     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2704
2705     /* Copy the presentation parameters */
2706     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2707     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2708     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2709     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2710     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2711     local_parameters.SwapEffect = present_parameters->SwapEffect;
2712     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2713     local_parameters.Windowed = present_parameters->Windowed;
2714     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2715     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2716     local_parameters.Flags = present_parameters->Flags;
2717     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2718     local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
2719
2720     hr = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)This,
2721             &local_parameters, (IDirect3DSwapChain8 **)&d3d_swapchain);
2722     if (FAILED(hr))
2723     {
2724         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2725         *swapchain = NULL;
2726         return hr;
2727     }
2728
2729     *swapchain = d3d_swapchain->wineD3DSwapChain;
2730     IUnknown_Release(d3d_swapchain->parentDevice);
2731     d3d_swapchain->parentDevice = NULL;
2732
2733     /* Copy back the presentation parameters */
2734     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2735     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2736     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2737     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2738     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2739     present_parameters->SwapEffect = local_parameters.SwapEffect;
2740     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2741     present_parameters->Windowed = local_parameters.Windowed;
2742     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2743     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2744     present_parameters->Flags = local_parameters.Flags;
2745     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2746     present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
2747
2748     return hr;
2749 }
2750
2751 static const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
2752 {
2753     /* IUnknown methods */
2754     device_parent_QueryInterface,
2755     device_parent_AddRef,
2756     device_parent_Release,
2757     /* IWineD3DDeviceParent methods */
2758     device_parent_WineD3DDeviceCreated,
2759     device_parent_CreateSurface,
2760     device_parent_CreateRenderTarget,
2761     device_parent_CreateDepthStencilSurface,
2762     device_parent_CreateVolume,
2763     device_parent_CreateSwapChain,
2764 };
2765
2766 static void setup_fpu(void)
2767 {
2768 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2769     WORD cw;
2770     __asm__ volatile ("fnstcw %0" : "=m" (cw));
2771     cw = (cw & ~0xf3f) | 0x3f;
2772     __asm__ volatile ("fldcw %0" : : "m" (cw));
2773 #else
2774     FIXME("FPU setup not implemented for this platform.\n");
2775 #endif
2776 }
2777
2778 HRESULT device_init(IDirect3DDevice8Impl *device, IWineD3D *wined3d, UINT adapter,
2779         D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
2780 {
2781     WINED3DPRESENT_PARAMETERS wined3d_parameters;
2782     HRESULT hr;
2783
2784     device->lpVtbl = &Direct3DDevice8_Vtbl;
2785     device->device_parent_vtbl = &d3d8_wined3d_device_parent_vtbl;
2786     device->ref = 1;
2787     device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2788             D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
2789     if (!device->handle_table.entries)
2790     {
2791         ERR("Failed to allocate handle table memory.\n");
2792         return E_OUTOFMEMORY;
2793     }
2794     device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
2795
2796     if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
2797
2798     wined3d_mutex_lock();
2799     hr = IWineD3D_CreateDevice(wined3d, adapter, device_type, focus_window, flags,
2800             (IWineD3DDeviceParent *)&device->device_parent_vtbl, &device->WineD3DDevice);
2801     if (FAILED(hr))
2802     {
2803         WARN("Failed to create wined3d device, hr %#x.\n", hr);
2804         wined3d_mutex_unlock();
2805         HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
2806         return hr;
2807     }
2808
2809     if (!parameters->Windowed)
2810     {
2811         if (!focus_window) focus_window = parameters->hDeviceWindow;
2812         if (FAILED(hr = IWineD3DDevice_AcquireFocusWindow(device->WineD3DDevice, focus_window)))
2813         {
2814             ERR("Failed to acquire focus window, hr %#x.\n", hr);
2815             IWineD3DDevice_Release(device->WineD3DDevice);
2816             wined3d_mutex_unlock();
2817             HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
2818             return hr;
2819         }
2820     }
2821
2822     if (flags & D3DCREATE_MULTITHREADED) IWineD3DDevice_SetMultithreaded(device->WineD3DDevice);
2823
2824     wined3d_parameters.BackBufferWidth = parameters->BackBufferWidth;
2825     wined3d_parameters.BackBufferHeight = parameters->BackBufferHeight;
2826     wined3d_parameters.BackBufferFormat = wined3dformat_from_d3dformat(parameters->BackBufferFormat);
2827     wined3d_parameters.BackBufferCount = parameters->BackBufferCount;
2828     wined3d_parameters.MultiSampleType = parameters->MultiSampleType;
2829     wined3d_parameters.MultiSampleQuality = 0; /* d3d9 only */
2830     wined3d_parameters.SwapEffect = parameters->SwapEffect;
2831     wined3d_parameters.hDeviceWindow = parameters->hDeviceWindow;
2832     wined3d_parameters.Windowed = parameters->Windowed;
2833     wined3d_parameters.EnableAutoDepthStencil = parameters->EnableAutoDepthStencil;
2834     wined3d_parameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(parameters->AutoDepthStencilFormat);
2835     wined3d_parameters.Flags = parameters->Flags;
2836     wined3d_parameters.FullScreen_RefreshRateInHz = parameters->FullScreen_RefreshRateInHz;
2837     wined3d_parameters.PresentationInterval = parameters->FullScreen_PresentationInterval;
2838     wined3d_parameters.AutoRestoreDisplayMode = TRUE;
2839
2840     hr = IWineD3DDevice_Init3D(device->WineD3DDevice, &wined3d_parameters);
2841     if (FAILED(hr))
2842     {
2843         WARN("Failed to initialize 3D, hr %#x.\n", hr);
2844         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
2845         IWineD3DDevice_Release(device->WineD3DDevice);
2846         wined3d_mutex_unlock();
2847         HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
2848         return hr;
2849     }
2850
2851     hr = IWineD3DDevice_SetRenderState(device->WineD3DDevice, WINED3DRS_POINTSIZE_MIN, 0);
2852     wined3d_mutex_unlock();
2853     if (FAILED(hr))
2854     {
2855         ERR("Failed to set minimum pointsize, hr %#x.\n", hr);
2856         goto err;
2857     }
2858
2859     parameters->BackBufferWidth = wined3d_parameters.BackBufferWidth;
2860     parameters->BackBufferHeight = wined3d_parameters.BackBufferHeight;
2861     parameters->BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters.BackBufferFormat);
2862     parameters->BackBufferCount = wined3d_parameters.BackBufferCount;
2863     parameters->MultiSampleType = wined3d_parameters.MultiSampleType;
2864     parameters->SwapEffect = wined3d_parameters.SwapEffect;
2865     parameters->hDeviceWindow = wined3d_parameters.hDeviceWindow;
2866     parameters->Windowed = wined3d_parameters.Windowed;
2867     parameters->EnableAutoDepthStencil = wined3d_parameters.EnableAutoDepthStencil;
2868     parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(wined3d_parameters.AutoDepthStencilFormat);
2869     parameters->Flags = wined3d_parameters.Flags;
2870     parameters->FullScreen_RefreshRateInHz = wined3d_parameters.FullScreen_RefreshRateInHz;
2871     parameters->FullScreen_PresentationInterval = wined3d_parameters.PresentationInterval;
2872
2873     device->declArraySize = 16;
2874     device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
2875     if (!device->decls)
2876     {
2877         ERR("Failed to allocate FVF vertex delcaration map memory.\n");
2878         hr = E_OUTOFMEMORY;
2879         goto err;
2880     }
2881
2882     return D3D_OK;
2883
2884 err:
2885     wined3d_mutex_lock();
2886     IWineD3DDevice_Uninit3D(device->WineD3DDevice, D3D8CB_DestroySwapChain);
2887     IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
2888     IWineD3DDevice_Release(device->WineD3DDevice);
2889     wined3d_mutex_unlock();
2890     HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
2891     return hr;
2892 }