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