d3d8: Remove trailing spaces.
[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_DestroyDepthStencilSurface, 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     HRESULT hrc;
862     IDirect3DSurface8Impl *object;
863     IDirect3DDevice8Impl  *This = (IDirect3DDevice8Impl *)iface;
864     TRACE("(%p) Relay\n", This);
865
866     if(MultisampleQuality > 0){
867         FIXME("MultisampleQuality set to %d, substituting 0\n" , MultisampleQuality);
868         /*
869         MultisampleQuality
870         [in] Quality level. The valid range is between zero and one less than the level returned by pQualityLevels used by IDirect3D8::CheckDeviceMultiSampleType. Passing a larger value returns the error D3DERR_INVALIDCALL. The MultisampleQuality values of paired render targets, depth stencil surfaces, and the MultiSample type must all match.
871         */
872         MultisampleQuality=0;
873     }
874     /*FIXME: Check MAX bounds of MultisampleQuality*/
875
876     /* Allocate the storage for the device */
877     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
878     if (NULL == object) {
879         FIXME("Allocation of memory failed\n");
880         *ppSurface = NULL;
881         return D3DERR_OUTOFVIDEOMEMORY;
882     }
883
884     object->lpVtbl = &Direct3DSurface8_Vtbl;
885     object->ref = 1;
886
887     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
888
889     wined3d_mutex_lock();
890     hrc = IWineD3DDevice_CreateSurface(This->WineD3DDevice, Width, Height, wined3dformat_from_d3dformat(Format),
891             Lockable, Discard, Level, &object->wineD3DSurface, Usage & WINED3DUSAGE_MASK, (WINED3DPOOL)Pool,
892             MultiSample, MultisampleQuality, SURFACE_OPENGL, (IUnknown *)object);
893     wined3d_mutex_unlock();
894
895     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
896        /* free up object */
897         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
898         HeapFree(GetProcessHeap(), 0, object);
899         *ppSurface = NULL;
900     } else {
901         IUnknown_AddRef(iface);
902         object->parentDevice = iface;
903         *ppSurface = (LPDIRECT3DSURFACE8) object;
904     }
905     return hrc;
906 }
907
908 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8** ppSurface) {
909     HRESULT hr;
910     TRACE("Relay\n");
911
912     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */,
913             0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, 0);
914
915     return hr;
916 }
917
918 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8** ppSurface) {
919     HRESULT hr;
920     TRACE("Relay\n");
921
922     /* TODO: Verify that Discard is false */
923     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE,
924             0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample, 0);
925
926     return hr;
927 }
928
929 /*  IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
930 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8** ppSurface) {
931     HRESULT hr;
932     TRACE("Relay\n");
933
934     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE /* Discard */,
935             0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE,
936             0 /* MultisampleQuality */);
937
938     return hr;
939 }
940
941 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8 *pSourceSurface, CONST RECT *pSourceRects, UINT cRects, IDirect3DSurface8 *pDestinationSurface, CONST POINT *pDestPoints) {
942     IDirect3DSurface8Impl *Source = (IDirect3DSurface8Impl *) pSourceSurface;
943     IDirect3DSurface8Impl *Dest = (IDirect3DSurface8Impl *) pDestinationSurface;
944
945     HRESULT              hr = WINED3D_OK;
946     WINED3DFORMAT        srcFormat, destFormat;
947     UINT                 srcWidth,  destWidth;
948     UINT                 srcHeight, destHeight;
949     UINT                 srcSize;
950     WINED3DSURFACE_DESC  winedesc;
951
952     TRACE("(%p) pSrcSur=%p, pSourceRects=%p, cRects=%d, pDstSur=%p, pDestPtsArr=%p\n", iface,
953           pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
954
955
956     /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the destination texture is in WINED3DPOOL_DEFAULT */
957
958     wined3d_mutex_lock();
959     IWineD3DSurface_GetDesc(Source->wineD3DSurface, &winedesc);
960     srcFormat = winedesc.format;
961     srcWidth = winedesc.width;
962     srcHeight = winedesc.height;
963     srcSize = winedesc.size;
964
965     IWineD3DSurface_GetDesc(Dest->wineD3DSurface, &winedesc);
966     destFormat = winedesc.format;
967     destWidth = winedesc.width;
968     destHeight = winedesc.height;
969
970     /* Check that the source and destination formats match */
971     if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat) {
972         WARN("(%p) source %p format must match the dest %p format, returning WINED3DERR_INVALIDCALL\n", iface, pSourceSurface, pDestinationSurface);
973         wined3d_mutex_unlock();
974         return WINED3DERR_INVALIDCALL;
975     } else if (WINED3DFMT_UNKNOWN == destFormat) {
976         TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
977         IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
978         destFormat = srcFormat;
979     }
980
981     /* Quick if complete copy ... */
982     if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
983         IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
984     } else {
985         unsigned int i;
986         /* Copy rect by rect */
987         if (NULL != pSourceRects && NULL != pDestPoints) {
988             for (i = 0; i < cRects; ++i) {
989                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
990             }
991         } else {
992             for (i = 0; i < cRects; ++i) {
993                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
994             }
995         }
996     }
997     wined3d_mutex_unlock();
998
999     return hr;
1000 }
1001
1002 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
1003     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1004     HRESULT hr;
1005     TRACE("(%p) Relay\n" , This);
1006
1007     wined3d_mutex_lock();
1008     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
1009     wined3d_mutex_unlock();
1010
1011     return hr;
1012 }
1013
1014 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
1015     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1016     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
1017     HRESULT hr;
1018
1019     TRACE("(%p) Relay\n" , This);
1020
1021     if (pDestSurface == NULL) {
1022         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
1023         return D3DERR_INVALIDCALL;
1024     }
1025
1026     wined3d_mutex_lock();
1027     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
1028     wined3d_mutex_unlock();
1029
1030     return hr;
1031 }
1032
1033 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
1034     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1035     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
1036     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
1037     IWineD3DSurface *original_ds = NULL;
1038     HRESULT hr;
1039     TRACE("(%p) Relay\n" , This);
1040
1041     wined3d_mutex_lock();
1042
1043     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &original_ds);
1044     if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
1045     {
1046         hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, pZSurface ? pZSurface->wineD3DSurface : NULL);
1047         if (SUCCEEDED(hr) && pSurface)
1048             hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface->wineD3DSurface);
1049         if (FAILED(hr)) IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, original_ds);
1050     }
1051     if (original_ds) IWineD3DSurface_Release(original_ds);
1052
1053     wined3d_mutex_unlock();
1054
1055     return hr;
1056 }
1057
1058 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
1059     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1060     HRESULT hr = D3D_OK;
1061     IWineD3DSurface *pRenderTarget;
1062
1063     TRACE("(%p) Relay\n" , This);
1064
1065     if (ppRenderTarget == NULL) {
1066         return D3DERR_INVALIDCALL;
1067     }
1068
1069     wined3d_mutex_lock();
1070     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
1071
1072     if (hr == D3D_OK && pRenderTarget != NULL) {
1073         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
1074         IWineD3DSurface_Release(pRenderTarget);
1075     } else {
1076         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
1077         *ppRenderTarget = NULL;
1078     }
1079     wined3d_mutex_unlock();
1080
1081     return hr;
1082 }
1083
1084 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
1085     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1086     HRESULT hr = D3D_OK;
1087     IWineD3DSurface *pZStencilSurface;
1088
1089     TRACE("(%p) Relay\n" , This);
1090     if(ppZStencilSurface == NULL){
1091         return D3DERR_INVALIDCALL;
1092     }
1093
1094     wined3d_mutex_lock();
1095     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
1096     if (hr == WINED3D_OK) {
1097         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
1098         IWineD3DSurface_Release(pZStencilSurface);
1099     }else{
1100         if (hr != WINED3DERR_NOTFOUND)
1101                 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1102         *ppZStencilSurface = NULL;
1103     }
1104     wined3d_mutex_unlock();
1105
1106     return hr;
1107 }
1108
1109 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
1110     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1111     HRESULT hr;
1112     TRACE("(%p) Relay\n" , This);
1113
1114     wined3d_mutex_lock();
1115     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1116     wined3d_mutex_unlock();
1117
1118     return hr;
1119 }
1120
1121 static HRESULT WINAPI IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
1122     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1123     HRESULT hr;
1124     TRACE("(%p) Relay\n" , This);
1125
1126     wined3d_mutex_lock();
1127     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1128     wined3d_mutex_unlock();
1129
1130     return hr;
1131 }
1132
1133 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
1134     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1135     HRESULT hr;
1136     TRACE("(%p) Relay\n" , This);
1137
1138     /* Note: D3DRECT is compatible with WINED3DRECT */
1139     wined3d_mutex_lock();
1140     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
1141     wined3d_mutex_unlock();
1142
1143     return hr;
1144 }
1145
1146 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
1147     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1148     HRESULT hr;
1149     TRACE("(%p) Relay\n" , This);
1150
1151     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1152     wined3d_mutex_lock();
1153     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1154     wined3d_mutex_unlock();
1155
1156     return hr;
1157 }
1158
1159 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
1160     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1161     HRESULT hr;
1162     TRACE("(%p) Relay\n" , This);
1163
1164     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1165     wined3d_mutex_lock();
1166     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1167     wined3d_mutex_unlock();
1168
1169     return hr;
1170 }
1171
1172 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
1173     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1174     HRESULT hr;
1175     TRACE("(%p) Relay\n" , This);
1176
1177     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1178     wined3d_mutex_lock();
1179     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1180     wined3d_mutex_unlock();
1181
1182     return hr;
1183 }
1184
1185 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
1186     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1187     HRESULT hr;
1188     TRACE("(%p) Relay\n" , This);
1189
1190     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1191     wined3d_mutex_lock();
1192     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1193     wined3d_mutex_unlock();
1194
1195     return hr;
1196 }
1197
1198 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
1199     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1200     HRESULT hr;
1201     TRACE("(%p) Relay\n" , This);
1202
1203     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1204     wined3d_mutex_lock();
1205     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1206     wined3d_mutex_unlock();
1207
1208     return hr;
1209 }
1210
1211 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
1212     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1213     HRESULT hr;
1214     TRACE("(%p) Relay\n" , This);
1215
1216     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1217     wined3d_mutex_lock();
1218     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1219     wined3d_mutex_unlock();
1220
1221     return hr;
1222 }
1223
1224 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
1225     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1226     HRESULT hr;
1227     TRACE("(%p) Relay\n" , This);
1228
1229     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1230     wined3d_mutex_lock();
1231     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1232     wined3d_mutex_unlock();
1233
1234     return hr;
1235 }
1236
1237 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
1238     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1239     HRESULT hr;
1240     TRACE("(%p) Relay\n" , This);
1241
1242     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1243     wined3d_mutex_lock();
1244     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1245     wined3d_mutex_unlock();
1246
1247     return hr;
1248 }
1249
1250 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
1251     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1252     HRESULT hr;
1253     TRACE("(%p) Relay\n" , This);
1254
1255     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1256     wined3d_mutex_lock();
1257     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1258     wined3d_mutex_unlock();
1259
1260     return hr;
1261 }
1262
1263 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
1264     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1265     HRESULT hr;
1266     TRACE("(%p) Relay\n" , This);
1267
1268     wined3d_mutex_lock();
1269     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1270     wined3d_mutex_unlock();
1271
1272     return hr;
1273 }
1274
1275 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
1276     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1277     HRESULT hr;
1278     TRACE("(%p) Relay\n" , This);
1279
1280     wined3d_mutex_lock();
1281     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1282     wined3d_mutex_unlock();
1283
1284     return hr;
1285 }
1286
1287 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
1288     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1289     HRESULT hr;
1290     TRACE("(%p) Relay\n" , This);
1291
1292     wined3d_mutex_lock();
1293     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1294     wined3d_mutex_unlock();
1295
1296     return hr;
1297 }
1298
1299 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
1300     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1301     HRESULT hr;
1302     TRACE("(%p) Relay\n" , This);
1303
1304     wined3d_mutex_lock();
1305     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1306     wined3d_mutex_unlock();
1307
1308     return hr;
1309 }
1310
1311 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
1312     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1313     HRESULT hr;
1314     TRACE("(%p) Relay\n" , This);
1315
1316     wined3d_mutex_lock();
1317     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1318     wined3d_mutex_unlock();
1319
1320     return hr;
1321 }
1322
1323 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
1324     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1325     HRESULT hr;
1326     TRACE("(%p) Relay\n" , This);
1327
1328     wined3d_mutex_lock();
1329     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1330     wined3d_mutex_unlock();
1331
1332     return hr;
1333 }
1334
1335 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
1336     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1337     HRESULT hr;
1338     TRACE("(%p)\n", This);
1339
1340     wined3d_mutex_lock();
1341     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1342     wined3d_mutex_unlock();
1343
1344     return hr;
1345 }
1346
1347 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
1348     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1349     HRESULT hr;
1350     IWineD3DStateBlock* wineD3DStateBlock;
1351     IDirect3DStateBlock8Impl* object;
1352
1353     TRACE("(%p) Relay\n", This);
1354
1355     /* Tell wineD3D to endstateblock before anything else (in case we run out
1356      * of memory later and cause locking problems)
1357      */
1358     wined3d_mutex_lock();
1359     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
1360     if (hr != D3D_OK) {
1361         WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1362         wined3d_mutex_unlock();
1363         return hr;
1364     }
1365
1366     /* allocate a new IDirectD3DStateBlock */
1367     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
1368     object->ref = 1;
1369     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1370
1371     object->wineD3DStateBlock = wineD3DStateBlock;
1372
1373     *pToken = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_SB);
1374     wined3d_mutex_unlock();
1375
1376     if (*pToken == D3D8_INVALID_HANDLE)
1377     {
1378         ERR("Failed to create a handle\n");
1379         IDirect3DStateBlock8_Release((IDirect3DStateBlock8 *)object);
1380         return E_FAIL;
1381     }
1382     ++*pToken;
1383
1384     TRACE("Returning %#x (%p).\n", *pToken, object);
1385
1386     return hr;
1387 }
1388
1389 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1390     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
1391     IDirect3DStateBlock8Impl *pSB;
1392     HRESULT hr;
1393
1394     TRACE("(%p) %#x Relay\n", This, Token);
1395
1396     wined3d_mutex_lock();
1397     pSB = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1398     if (!pSB)
1399     {
1400         WARN("Invalid handle (%#x) passed.\n", Token);
1401         wined3d_mutex_unlock();
1402         return D3DERR_INVALIDCALL;
1403     }
1404     hr = IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
1405     wined3d_mutex_unlock();
1406
1407     return hr;
1408 }
1409
1410 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1411     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1412     IDirect3DStateBlock8Impl *pSB;
1413     HRESULT hr;
1414
1415     TRACE("(%p) %#x Relay\n", This, Token);
1416
1417     wined3d_mutex_lock();
1418     pSB = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1419     if (!pSB)
1420     {
1421         WARN("Invalid handle (%#x) passed.\n", Token);
1422         wined3d_mutex_unlock();
1423         return D3DERR_INVALIDCALL;
1424     }
1425     hr = IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
1426     wined3d_mutex_unlock();
1427
1428     return hr;
1429 }
1430
1431 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1432     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1433     IDirect3DStateBlock8Impl *pSB;
1434
1435     TRACE("(%p) Relay\n", This);
1436
1437     wined3d_mutex_lock();
1438     pSB = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1439     wined3d_mutex_unlock();
1440
1441     if (!pSB)
1442     {
1443         WARN("Invalid handle (%#x) passed.\n", Token);
1444         return D3DERR_INVALIDCALL;
1445     }
1446
1447     if (IUnknown_Release((IUnknown *)pSB))
1448     {
1449         ERR("Stateblock %p has references left, this shouldn't happen.\n", pSB);
1450     }
1451
1452     return D3D_OK;
1453 }
1454
1455 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1456         D3DSTATEBLOCKTYPE Type, DWORD *handle)
1457 {
1458     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1459     IDirect3DStateBlock8Impl *object;
1460     HRESULT hr;
1461
1462     TRACE("(%p) Relay\n", This);
1463
1464     if (Type != D3DSBT_ALL
1465             && Type != D3DSBT_PIXELSTATE
1466             && Type != D3DSBT_VERTEXSTATE)
1467     {
1468         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1469         return D3DERR_INVALIDCALL;
1470     }
1471
1472     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
1473     if (!object)
1474     {
1475         ERR("Failed to allocate memory.\n");
1476         return E_OUTOFMEMORY;
1477     }
1478
1479     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1480     object->ref = 1;
1481
1482     wined3d_mutex_lock();
1483     hr = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type,
1484             &object->wineD3DStateBlock, (IUnknown *)object);
1485     if (FAILED(hr))
1486     {
1487         wined3d_mutex_unlock();
1488         ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1489         HeapFree(GetProcessHeap(), 0, object);
1490         return hr;
1491     }
1492
1493     *handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_SB);
1494     wined3d_mutex_unlock();
1495
1496     if (*handle == D3D8_INVALID_HANDLE)
1497     {
1498         ERR("Failed to allocate a handle.\n");
1499         IDirect3DStateBlock8_Release((IDirect3DStateBlock8 *)object);
1500         return E_FAIL;
1501     }
1502     ++*handle;
1503
1504     TRACE("Returning %#x (%p).\n", *handle, object);
1505
1506     return hr;
1507 }
1508
1509 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
1510     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1511     HRESULT hr;
1512     TRACE("(%p) Relay\n" , This);
1513 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1514
1515     wined3d_mutex_lock();
1516     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1517     wined3d_mutex_unlock();
1518
1519     return hr;
1520 }
1521
1522 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
1523     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1524     HRESULT hr;
1525     TRACE("(%p) Relay\n" , This);
1526
1527     wined3d_mutex_lock();
1528     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1529     wined3d_mutex_unlock();
1530
1531     return hr;
1532 }
1533
1534 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
1535     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1536     IWineD3DBaseTexture *retTexture = NULL;
1537     HRESULT rc = D3D_OK;
1538
1539     TRACE("(%p) Relay\n" , This);
1540
1541     if(ppTexture == NULL){
1542         return D3DERR_INVALIDCALL;
1543     }
1544
1545     wined3d_mutex_lock();
1546     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1547     if (rc == D3D_OK && NULL != retTexture) {
1548         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1549         IWineD3DBaseTexture_Release(retTexture);
1550     } else {
1551         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1552         *ppTexture = NULL;
1553     }
1554     wined3d_mutex_unlock();
1555
1556     return rc;
1557 }
1558
1559 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1560     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1561     HRESULT hr;
1562     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1563
1564     wined3d_mutex_lock();
1565     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1566                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1567     wined3d_mutex_unlock();
1568
1569     return hr;
1570 }
1571
1572 static const struct tss_lookup
1573 {
1574     BOOL sampler_state;
1575     DWORD state;
1576 }
1577 tss_lookup[] =
1578 {
1579     {FALSE, WINED3DTSS_FORCE_DWORD},            /*  0, unused */
1580     {FALSE, WINED3DTSS_COLOROP},                /*  1, D3DTSS_COLOROP */
1581     {FALSE, WINED3DTSS_COLORARG1},              /*  2, D3DTSS_COLORARG1 */
1582     {FALSE, WINED3DTSS_COLORARG2},              /*  3, D3DTSS_COLORARG2 */
1583     {FALSE, WINED3DTSS_ALPHAOP},                /*  4, D3DTSS_ALPHAOP */
1584     {FALSE, WINED3DTSS_ALPHAARG1},              /*  5, D3DTSS_ALPHAARG1 */
1585     {FALSE, WINED3DTSS_ALPHAARG2},              /*  6, D3DTSS_ALPHAARG2 */
1586     {FALSE, WINED3DTSS_BUMPENVMAT00},           /*  7, D3DTSS_BUMPENVMAT00 */
1587     {FALSE, WINED3DTSS_BUMPENVMAT01},           /*  8, D3DTSS_BUMPENVMAT01 */
1588     {FALSE, WINED3DTSS_BUMPENVMAT10},           /*  9, D3DTSS_BUMPENVMAT10 */
1589     {FALSE, WINED3DTSS_BUMPENVMAT11},           /* 10, D3DTSS_BUMPENVMAT11 */
1590     {FALSE, WINED3DTSS_TEXCOORDINDEX},          /* 11, D3DTSS_TEXCOORDINDEX */
1591     {FALSE, WINED3DTSS_FORCE_DWORD},            /* 12, unused */
1592     {TRUE,  WINED3DSAMP_ADDRESSU},              /* 13, D3DTSS_ADDRESSU */
1593     {TRUE,  WINED3DSAMP_ADDRESSV},              /* 14, D3DTSS_ADDRESSV */
1594     {TRUE,  WINED3DSAMP_BORDERCOLOR},           /* 15, D3DTSS_BORDERCOLOR */
1595     {TRUE,  WINED3DSAMP_MAGFILTER},             /* 16, D3DTSS_MAGFILTER */
1596     {TRUE,  WINED3DSAMP_MINFILTER},             /* 17, D3DTSS_MINFILTER */
1597     {TRUE,  WINED3DSAMP_MIPFILTER},             /* 18, D3DTSS_MIPFILTER */
1598     {TRUE,  WINED3DSAMP_MIPMAPLODBIAS},         /* 19, D3DTSS_MIPMAPLODBIAS */
1599     {TRUE,  WINED3DSAMP_MAXMIPLEVEL},           /* 20, D3DTSS_MAXMIPLEVEL */
1600     {TRUE,  WINED3DSAMP_MAXANISOTROPY},         /* 21, D3DTSS_MAXANISOTROPY */
1601     {FALSE, WINED3DTSS_BUMPENVLSCALE},          /* 22, D3DTSS_BUMPENVLSCALE */
1602     {FALSE, WINED3DTSS_BUMPENVLOFFSET},         /* 23, D3DTSS_BUMPENVLOFFSET */
1603     {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS},  /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1604     {TRUE,  WINED3DSAMP_ADDRESSW},              /* 25, D3DTSS_ADDRESSW */
1605     {FALSE, WINED3DTSS_COLORARG0},              /* 26, D3DTSS_COLORARG0 */
1606     {FALSE, WINED3DTSS_ALPHAARG0},              /* 27, D3DTSS_ALPHAARG0 */
1607     {FALSE, WINED3DTSS_RESULTARG},              /* 28, D3DTSS_RESULTARG */
1608 };
1609
1610 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1611     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1612     const struct tss_lookup *l = &tss_lookup[Type];
1613     HRESULT hr;
1614     TRACE("(%p) Relay\n" , This);
1615
1616     wined3d_mutex_lock();
1617     if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1618     else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1619     wined3d_mutex_unlock();
1620
1621     return hr;
1622 }
1623
1624 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1625     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1626     const struct tss_lookup *l = &tss_lookup[Type];
1627     HRESULT hr;
1628     TRACE("(%p) Relay\n" , This);
1629
1630     wined3d_mutex_lock();
1631     if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1632     else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1633     wined3d_mutex_unlock();
1634
1635     return hr;
1636 }
1637
1638 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1639     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1640     HRESULT hr;
1641     TRACE("(%p) Relay\n" , This);
1642
1643     wined3d_mutex_lock();
1644     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1645     wined3d_mutex_unlock();
1646
1647     return hr;
1648 }
1649
1650 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1651     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1652     FIXME("(%p) : stub\n", This);
1653     return D3D_OK;
1654 }
1655
1656 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1657     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1658     HRESULT hr;
1659     TRACE("(%p) Relay\n" , This);
1660
1661     wined3d_mutex_lock();
1662     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1663     wined3d_mutex_unlock();
1664
1665     return hr;
1666 }
1667
1668 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1669     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1670     HRESULT hr;
1671     TRACE("(%p) Relay\n" , This);
1672
1673     wined3d_mutex_lock();
1674     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1675     wined3d_mutex_unlock();
1676
1677     return hr;
1678 }
1679
1680 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1681     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1682     HRESULT hr;
1683     TRACE("(%p) Relay\n" , This);
1684
1685     wined3d_mutex_lock();
1686     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1687     wined3d_mutex_unlock();
1688
1689     return hr;
1690 }
1691
1692 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1693     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1694     HRESULT hr;
1695     TRACE("(%p) Relay\n" , This);
1696
1697     wined3d_mutex_lock();
1698     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1699     wined3d_mutex_unlock();
1700
1701     return hr;
1702 }
1703
1704 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface, D3DPRIMITIVETYPE PrimitiveType,
1705         UINT StartVertex, UINT PrimitiveCount)
1706 {
1707     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1708     HRESULT hr;
1709     TRACE("(%p) Relay\n" , This);
1710
1711     wined3d_mutex_lock();
1712     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1713     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1714             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1715     wined3d_mutex_unlock();
1716
1717     return hr;
1718 }
1719
1720 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1721                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1722     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1723     HRESULT hr;
1724     TRACE("(%p) Relay\n" , This);
1725
1726     wined3d_mutex_lock();
1727     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1728     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, MinVertexIndex, NumVertices,
1729             startIndex, vertex_count_from_primitive_count(PrimitiveType, primCount));
1730     wined3d_mutex_unlock();
1731
1732     return hr;
1733 }
1734
1735 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1736     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1737     HRESULT hr;
1738     TRACE("(%p) Relay\n" , This);
1739
1740     wined3d_mutex_lock();
1741     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1742     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1743             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1744             pVertexStreamZeroData, VertexStreamZeroStride);
1745     wined3d_mutex_unlock();
1746
1747     return hr;
1748 }
1749
1750 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1751                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1752                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1753                                                              UINT VertexStreamZeroStride) {
1754     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1755     HRESULT hr;
1756     TRACE("(%p) Relay\n" , This);
1757
1758     wined3d_mutex_lock();
1759     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1760     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, MinVertexIndex, NumVertexIndices,
1761             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1762             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1763     wined3d_mutex_unlock();
1764
1765     return hr;
1766 }
1767
1768 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1769     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1770     HRESULT hr;
1771     IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
1772     TRACE("(%p) Relay\n" , This);
1773
1774     wined3d_mutex_lock();
1775     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1776     wined3d_mutex_unlock();
1777
1778     return hr;
1779 }
1780
1781 static HRESULT IDirect3DDevice8Impl_CreateVertexDeclaration(IDirect3DDevice8 *iface, CONST DWORD *declaration, IDirect3DVertexDeclaration8 **decl_ptr) {
1782     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1783     IDirect3DVertexDeclaration8Impl *object;
1784     WINED3DVERTEXELEMENT *wined3d_elements;
1785     UINT wined3d_element_count;
1786     HRESULT hr = D3D_OK;
1787
1788     TRACE("(%p) : declaration %p\n", This, declaration);
1789
1790     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1791     if (!object) {
1792         ERR("Memory allocation failed\n");
1793         *decl_ptr = NULL;
1794         return D3DERR_OUTOFVIDEOMEMORY;
1795     }
1796
1797     object->ref_count = 1;
1798     object->lpVtbl = &Direct3DVertexDeclaration8_Vtbl;
1799
1800     wined3d_element_count = convert_to_wined3d_declaration(declaration, &object->elements_size, &wined3d_elements);
1801     object->elements = HeapAlloc(GetProcessHeap(), 0, object->elements_size);
1802     if (!object->elements) {
1803         ERR("Memory allocation failed\n");
1804         HeapFree(GetProcessHeap(), 0, wined3d_elements);
1805         HeapFree(GetProcessHeap(), 0, object);
1806         *decl_ptr = NULL;
1807         return D3DERR_OUTOFVIDEOMEMORY;
1808     }
1809
1810     CopyMemory(object->elements, declaration, object->elements_size);
1811
1812     wined3d_mutex_lock();
1813     hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wined3d_vertex_declaration,
1814             (IUnknown *)object, wined3d_elements, wined3d_element_count);
1815     wined3d_mutex_unlock();
1816
1817     HeapFree(GetProcessHeap(), 0, wined3d_elements);
1818
1819     if (FAILED(hr)) {
1820         ERR("(%p) : IWineD3DDevice_CreateVertexDeclaration call failed\n", This);
1821         HeapFree(GetProcessHeap(), 0, object->elements);
1822         HeapFree(GetProcessHeap(), 0, object);
1823     } else {
1824         *decl_ptr = (IDirect3DVertexDeclaration8 *)object;
1825         TRACE("(%p) : Created vertex declaration %p\n", This, object);
1826     }
1827
1828     return hr;
1829 }
1830
1831 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pDeclaration, CONST DWORD* pFunction, DWORD* ppShader, DWORD Usage) {
1832     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1833     HRESULT hrc = D3D_OK;
1834     IDirect3DVertexShader8Impl *object;
1835     const DWORD *token = pDeclaration;
1836     DWORD handle;
1837
1838     /* Test if the vertex declaration is valid */
1839     while (D3DVSD_END() != *token) {
1840         D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
1841
1842         if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000)) {
1843             DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
1844             DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
1845
1846             if(reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !pFunction) {
1847                 WARN("Attempt to use a non-FLOAT3 normal with the fixed function function\n");
1848                 return D3DERR_INVALIDCALL;
1849             }
1850         }
1851         token += parse_token(token);
1852     }
1853
1854     /* Setup a stub object for now */
1855     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1856     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1857     if (NULL == object) {
1858         FIXME("Allocation of memory failed\n");
1859         *ppShader = 0;
1860         return D3DERR_OUTOFVIDEOMEMORY;
1861     }
1862
1863     object->ref = 1;
1864     object->lpVtbl = &Direct3DVertexShader8_Vtbl;
1865
1866     hrc = IDirect3DDevice8Impl_CreateVertexDeclaration(iface, pDeclaration, &object->vertex_declaration);
1867     if (FAILED(hrc)) {
1868         ERR("(%p) : IDirect3DDeviceImpl_CreateVertexDeclaration call failed\n", This);
1869         HeapFree(GetProcessHeap(), 0, object);
1870         *ppShader = 0;
1871         return D3DERR_INVALIDCALL;
1872     }
1873
1874     wined3d_mutex_lock();
1875     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
1876     if (handle == D3D8_INVALID_HANDLE)
1877     {
1878         ERR("Failed to allocate shader handle\n");
1879         wined3d_mutex_unlock();
1880         IDirect3DVertexDeclaration8_Release(object->vertex_declaration);
1881         HeapFree(GetProcessHeap(), 0, object);
1882         *ppShader = 0;
1883         return E_OUTOFMEMORY;
1884     }
1885     else
1886     {
1887         DWORD shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
1888         *ppShader = ((IDirect3DVertexDeclaration8Impl *)object->vertex_declaration)->shader_handle = shader_handle;
1889     }
1890
1891     if (pFunction)
1892     {
1893         /* Usage is missing ... Use SetRenderState to set the sw vp render state in SetVertexShader */
1894         hrc = IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, pFunction,
1895                 NULL /* output signature */, &object->wineD3DVertexShader, (IUnknown *)object);
1896
1897         if (FAILED(hrc))
1898         {
1899             /* free up object */
1900             FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
1901             d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
1902             IDirect3DVertexDeclaration8_Release(object->vertex_declaration);
1903             HeapFree(GetProcessHeap(), 0, object);
1904             *ppShader = 0;
1905         }
1906         else
1907         {
1908             load_local_constants(pDeclaration, object->wineD3DVertexShader);
1909             TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
1910         }
1911     }
1912
1913     wined3d_mutex_unlock();
1914
1915     return hrc;
1916 }
1917
1918 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1919 {
1920     IDirect3DVertexDeclaration8Impl *d3d8_declaration;
1921     HRESULT hr;
1922     int p, low, high; /* deliberately signed */
1923     struct FvfToDecl *convertedDecls = This->decls;
1924
1925     TRACE("Searching for declaration for fvf %08x... ", fvf);
1926
1927     low = 0;
1928     high = This->numConvertedDecls - 1;
1929     while(low <= high) {
1930         p = (low + high) >> 1;
1931         TRACE("%d ", p);
1932         if(convertedDecls[p].fvf == fvf) {
1933             TRACE("found %p\n", convertedDecls[p].decl);
1934             return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
1935         } else if(convertedDecls[p].fvf < fvf) {
1936             low = p + 1;
1937         } else {
1938             high = p - 1;
1939         }
1940     }
1941     TRACE("not found. Creating and inserting at position %d.\n", low);
1942
1943     d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
1944     if (!d3d8_declaration)
1945     {
1946         ERR("Memory allocation failed.\n");
1947         return NULL;
1948     }
1949
1950     d3d8_declaration->ref_count = 1;
1951     d3d8_declaration->lpVtbl = &Direct3DVertexDeclaration8_Vtbl;
1952     d3d8_declaration->elements = NULL;
1953     d3d8_declaration->elements_size = 0;
1954     d3d8_declaration->shader_handle = fvf;
1955
1956     hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->WineD3DDevice,
1957             &d3d8_declaration->wined3d_vertex_declaration, (IUnknown *)d3d8_declaration, fvf);
1958     if (FAILED(hr))
1959     {
1960         ERR("Failed to create wined3d vertex declaration.\n");
1961         HeapFree(GetProcessHeap(), 0, d3d8_declaration);
1962         return NULL;
1963     }
1964
1965     if(This->declArraySize == This->numConvertedDecls) {
1966         int grow = This->declArraySize / 2;
1967         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1968                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1969         if(!convertedDecls) {
1970             /* This will destroy it */
1971             IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
1972             return NULL;
1973         }
1974         This->decls = convertedDecls;
1975         This->declArraySize += grow;
1976     }
1977
1978     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
1979     convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
1980     convertedDecls[low].fvf = fvf;
1981     This->numConvertedDecls++;
1982
1983     TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
1984     return d3d8_declaration;
1985 }
1986
1987 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1988     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1989     IDirect3DVertexShader8Impl *shader;
1990     HRESULT hr;
1991
1992     TRACE("(%p) : Relay\n", This);
1993
1994     if (VS_HIGHESTFIXEDFXF >= pShader) {
1995         TRACE("Setting FVF, %#x\n", pShader);
1996
1997         wined3d_mutex_lock();
1998         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1999                 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
2000         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
2001         wined3d_mutex_unlock();
2002
2003         return D3D_OK;
2004     }
2005
2006     TRACE("Setting shader\n");
2007
2008     wined3d_mutex_lock();
2009     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2010     if (!shader)
2011     {
2012         WARN("Invalid handle (%#x) passed.\n", pShader);
2013         wined3d_mutex_unlock();
2014
2015         return D3DERR_INVALIDCALL;
2016     }
2017
2018     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
2019             ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
2020     if (SUCCEEDED(hr)) hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wineD3DVertexShader);
2021     wined3d_mutex_unlock();
2022
2023     TRACE("Returning hr %#x\n", hr);
2024
2025     return hr;
2026 }
2027
2028 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
2029     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2030     IWineD3DVertexDeclaration *wined3d_declaration;
2031     IDirect3DVertexDeclaration8 *d3d8_declaration;
2032     HRESULT hrc;
2033
2034     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
2035
2036     wined3d_mutex_lock();
2037     hrc = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
2038     if (FAILED(hrc))
2039     {
2040         wined3d_mutex_unlock();
2041         WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
2042                 This, hrc, This->WineD3DDevice);
2043         return hrc;
2044     }
2045
2046     if (!wined3d_declaration)
2047     {
2048         wined3d_mutex_unlock();
2049         *ppShader = 0;
2050         return D3D_OK;
2051     }
2052
2053     hrc = IWineD3DVertexDeclaration_GetParent(wined3d_declaration, (IUnknown **)&d3d8_declaration);
2054     IWineD3DVertexDeclaration_Release(wined3d_declaration);
2055     wined3d_mutex_unlock();
2056     if (SUCCEEDED(hrc))
2057     {
2058         *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
2059         IDirect3DVertexDeclaration8_Release(d3d8_declaration);
2060     }
2061
2062     TRACE("(%p) : returning %#x\n", This, *ppShader);
2063
2064     return hrc;
2065 }
2066
2067 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2068     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2069     IDirect3DVertexShader8Impl *shader;
2070     IWineD3DVertexShader *cur = NULL;
2071
2072     TRACE("(%p) : pShader %#x\n", This, pShader);
2073
2074     wined3d_mutex_lock();
2075     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2076     if (!shader)
2077     {
2078         WARN("Invalid handle (%#x) passed.\n", pShader);
2079         wined3d_mutex_unlock();
2080
2081         return D3DERR_INVALIDCALL;
2082     }
2083
2084     IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
2085
2086     if (cur)
2087     {
2088         if (cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
2089         IWineD3DVertexShader_Release(cur);
2090     }
2091
2092     wined3d_mutex_unlock();
2093
2094     if (IUnknown_Release((IUnknown *)shader))
2095     {
2096         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2097     }
2098
2099     return D3D_OK;
2100 }
2101
2102 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
2103     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2104     HRESULT hr;
2105     TRACE("(%p) : Relay\n", This);
2106
2107     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2108         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2109              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2110         return D3DERR_INVALIDCALL;
2111     }
2112
2113     wined3d_mutex_lock();
2114     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2115     wined3d_mutex_unlock();
2116
2117     return hr;
2118 }
2119
2120 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
2121     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2122     HRESULT hr;
2123     TRACE("(%p) : Relay\n", This);
2124
2125     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2126         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2127              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2128         return D3DERR_INVALIDCALL;
2129     }
2130
2131     wined3d_mutex_lock();
2132     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2133     wined3d_mutex_unlock();
2134
2135     return hr;
2136 }
2137
2138 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
2139     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2140     IDirect3DVertexDeclaration8Impl *declaration;
2141     IDirect3DVertexShader8Impl *shader;
2142
2143     TRACE("(%p) : pVertexShader 0x%08x, pData %p, *pSizeOfData %u\n", This, pVertexShader, pData, *pSizeOfData);
2144
2145     wined3d_mutex_lock();
2146     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2147     wined3d_mutex_unlock();
2148
2149     if (!shader)
2150     {
2151         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2152         return D3DERR_INVALIDCALL;
2153     }
2154     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2155
2156     /* If pData is NULL, we just return the required size of the buffer. */
2157     if (!pData) {
2158         *pSizeOfData = declaration->elements_size;
2159         return D3D_OK;
2160     }
2161
2162     /* MSDN claims that if *pSizeOfData is smaller than the required size
2163      * we should write the required size and return D3DERR_MOREDATA.
2164      * That's not actually true. */
2165     if (*pSizeOfData < declaration->elements_size) {
2166         return D3DERR_INVALIDCALL;
2167     }
2168
2169     CopyMemory(pData, declaration->elements, declaration->elements_size);
2170
2171     return D3D_OK;
2172 }
2173
2174 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
2175     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2176     IDirect3DVertexShader8Impl *shader = NULL;
2177     HRESULT hr;
2178
2179     TRACE("(%p) : pVertexShader %#x, pData %p, pSizeOfData %p\n", This, pVertexShader, pData, pSizeOfData);
2180
2181     wined3d_mutex_lock();
2182     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2183     if (!shader)
2184     {
2185         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2186         wined3d_mutex_unlock();
2187
2188         return D3DERR_INVALIDCALL;
2189     }
2190
2191     if (!shader->wineD3DVertexShader)
2192     {
2193         wined3d_mutex_unlock();
2194         *pSizeOfData = 0;
2195         return D3D_OK;
2196     }
2197
2198     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, pSizeOfData);
2199     wined3d_mutex_unlock();
2200
2201     return hr;
2202 }
2203
2204 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
2205     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2206     HRESULT hr;
2207     IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2208     TRACE("(%p) Relay\n", This);
2209
2210     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2211      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2212      * vertex buffers can't be created to address them with an index that requires the 32nd bit
2213      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2214      * problem)
2215      */
2216     wined3d_mutex_lock();
2217     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2218     hr = IWineD3DDevice_SetIndices(This->WineD3DDevice,
2219             ib ? ib->wineD3DIndexBuffer : NULL,
2220             ib ? ib->format : WINED3DFMT_UNKNOWN);
2221     wined3d_mutex_unlock();
2222
2223     return hr;
2224 }
2225
2226 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
2227     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2228     IWineD3DBuffer *retIndexData = NULL;
2229     HRESULT rc = D3D_OK;
2230
2231     TRACE("(%p) Relay\n", This);
2232
2233     if(ppIndexData == NULL){
2234         return D3DERR_INVALIDCALL;
2235     }
2236
2237     /* The case from UINT to INT is safe because d3d8 will never set negative values */
2238     wined3d_mutex_lock();
2239     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2240     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData);
2241     if (SUCCEEDED(rc) && retIndexData) {
2242         IWineD3DBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
2243         IWineD3DBuffer_Release(retIndexData);
2244     } else {
2245         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
2246         *ppIndexData = NULL;
2247     }
2248     wined3d_mutex_unlock();
2249
2250     return rc;
2251 }
2252 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
2253     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2254     IDirect3DPixelShader8Impl *object;
2255     DWORD handle;
2256     HRESULT hr;
2257
2258     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
2259
2260     if (NULL == ppShader) {
2261         TRACE("(%p) Invalid call\n", This);
2262         return D3DERR_INVALIDCALL;
2263     }
2264
2265     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2266     if (!object)
2267     {
2268         ERR("Failed to allocate memmory.\n");
2269         return E_OUTOFMEMORY;
2270     }
2271
2272     object->ref    = 1;
2273     object->lpVtbl = &Direct3DPixelShader8_Vtbl;
2274
2275     wined3d_mutex_lock();
2276     hr = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction,
2277             NULL, &object->wineD3DPixelShader, (IUnknown *)object);
2278     if (FAILED(hr))
2279     {
2280         wined3d_mutex_unlock();
2281         FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
2282         HeapFree(GetProcessHeap(), 0 , object);
2283         *ppShader = 0;
2284         return hr;
2285     }
2286
2287     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2288     wined3d_mutex_unlock();
2289
2290     if (handle == D3D8_INVALID_HANDLE)
2291     {
2292         ERR("Failed to allocate shader handle\n");
2293         IDirect3DVertexShader8_Release((IUnknown *)object);
2294         return E_OUTOFMEMORY;
2295     }
2296
2297     *ppShader = object->handle = handle + VS_HIGHESTFIXEDFXF + 1;
2298     TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
2299
2300     return hr;
2301 }
2302
2303 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2304     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2305     IDirect3DPixelShader8Impl *shader;
2306     HRESULT hr;
2307
2308     TRACE("(%p) : pShader %#x\n", This, pShader);
2309
2310     wined3d_mutex_lock();
2311
2312     if (!pShader)
2313     {
2314         hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2315         wined3d_mutex_unlock();
2316         return hr;
2317     }
2318
2319     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2320     if (!shader)
2321     {
2322         WARN("Invalid handle (%#x) passed.\n", pShader);
2323         wined3d_mutex_unlock();
2324         return D3DERR_INVALIDCALL;
2325     }
2326
2327     TRACE("(%p) : Setting shader %p\n", This, shader);
2328     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wineD3DPixelShader);
2329     wined3d_mutex_unlock();
2330
2331     return hr;
2332 }
2333
2334 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
2335     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2336     IWineD3DPixelShader *object;
2337
2338     HRESULT hrc = D3D_OK;
2339     TRACE("(%p) Relay\n", This);
2340     if (NULL == ppShader) {
2341         TRACE("(%p) Invalid call\n", This);
2342         return D3DERR_INVALIDCALL;
2343     }
2344
2345     wined3d_mutex_lock();
2346     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
2347     if (D3D_OK == hrc && NULL != object) {
2348         IDirect3DPixelShader8Impl *d3d8_shader;
2349         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
2350         IWineD3DPixelShader_Release(object);
2351         *ppShader = d3d8_shader->handle;
2352         IDirect3DPixelShader8_Release((IDirect3DPixelShader8 *)d3d8_shader);
2353     } else {
2354         *ppShader = 0;
2355     }
2356     wined3d_mutex_unlock();
2357
2358     TRACE("(%p) : returning %#x\n", This, *ppShader);
2359
2360     return hrc;
2361 }
2362
2363 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2364     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2365     IDirect3DPixelShader8Impl *shader;
2366     IWineD3DPixelShader *cur = NULL;
2367
2368     TRACE("(%p) : pShader %#x\n", This, pShader);
2369
2370     wined3d_mutex_lock();
2371
2372     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2373     if (!shader)
2374     {
2375         WARN("Invalid handle (%#x) passed.\n", pShader);
2376         wined3d_mutex_unlock();
2377         return D3D_OK;
2378     }
2379
2380     IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
2381
2382     if (cur)
2383     {
2384         if (cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
2385         IWineD3DPixelShader_Release(cur);
2386     }
2387
2388     wined3d_mutex_unlock();
2389
2390     if (IUnknown_Release((IUnknown *)shader))
2391     {
2392         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2393     }
2394
2395     return D3D_OK;
2396 }
2397
2398 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
2399     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2400     HRESULT hr;
2401     TRACE("(%p) Relay\n", This);
2402
2403     wined3d_mutex_lock();
2404     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2405     wined3d_mutex_unlock();
2406
2407     return hr;
2408 }
2409
2410 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
2411     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2412     HRESULT hr;
2413     TRACE("(%p) Relay\n", This);
2414
2415     wined3d_mutex_lock();
2416     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2417     wined3d_mutex_unlock();
2418
2419     return hr;
2420 }
2421
2422 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
2423     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2424     IDirect3DPixelShader8Impl *shader = NULL;
2425     HRESULT hr;
2426
2427     TRACE("(%p) : pPixelShader %#x, pData %p, pSizeOfData %p\n", This, pPixelShader, pData, pSizeOfData);
2428
2429     wined3d_mutex_lock();
2430     shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2431     if (!shader)
2432     {
2433         WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2434         wined3d_mutex_unlock();
2435
2436         return D3DERR_INVALIDCALL;
2437     }
2438
2439     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, pSizeOfData);
2440     wined3d_mutex_unlock();
2441
2442     return hr;
2443 }
2444
2445 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2446     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2447     HRESULT hr;
2448     TRACE("(%p) Relay\n", This);
2449
2450     wined3d_mutex_lock();
2451     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2452     wined3d_mutex_unlock();
2453
2454     return hr;
2455 }
2456
2457 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2458     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2459     HRESULT hr;
2460     TRACE("(%p) Relay\n", This);
2461
2462     wined3d_mutex_lock();
2463     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2464     wined3d_mutex_unlock();
2465
2466     return hr;
2467 }
2468
2469 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
2470     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2471     HRESULT hr;
2472     TRACE("(%p) Relay\n", This);
2473
2474     wined3d_mutex_lock();
2475     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2476     wined3d_mutex_unlock();
2477
2478     return hr;
2479 }
2480
2481 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
2482     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2483     HRESULT hr;
2484     TRACE("(%p) Relay\n" , This);
2485
2486     wined3d_mutex_lock();
2487     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2488                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2489                                         0/* Offset in bytes */, Stride);
2490     wined3d_mutex_unlock();
2491
2492     return hr;
2493 }
2494
2495 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
2496     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2497     IWineD3DBuffer *retStream = NULL;
2498     HRESULT rc = D3D_OK;
2499
2500     TRACE("(%p) Relay\n" , This);
2501
2502     if(pStream == NULL){
2503         return D3DERR_INVALIDCALL;
2504     }
2505
2506     wined3d_mutex_lock();
2507     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, 0 /* Offset in bytes */, pStride);
2508     if (rc == D3D_OK  && NULL != retStream) {
2509         IWineD3DBuffer_GetParent(retStream, (IUnknown **)pStream);
2510         IWineD3DBuffer_Release(retStream);
2511     }else{
2512         if (rc != D3D_OK){
2513             FIXME("Call to GetStreamSource failed %p\n",  pStride);
2514         }
2515         *pStream = NULL;
2516     }
2517     wined3d_mutex_unlock();
2518
2519     return rc;
2520 }
2521
2522
2523 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2524 {
2525     IDirect3DDevice8Impl_QueryInterface,
2526     IDirect3DDevice8Impl_AddRef,
2527     IDirect3DDevice8Impl_Release,
2528     IDirect3DDevice8Impl_TestCooperativeLevel,
2529     IDirect3DDevice8Impl_GetAvailableTextureMem,
2530     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2531     IDirect3DDevice8Impl_GetDirect3D,
2532     IDirect3DDevice8Impl_GetDeviceCaps,
2533     IDirect3DDevice8Impl_GetDisplayMode,
2534     IDirect3DDevice8Impl_GetCreationParameters,
2535     IDirect3DDevice8Impl_SetCursorProperties,
2536     IDirect3DDevice8Impl_SetCursorPosition,
2537     IDirect3DDevice8Impl_ShowCursor,
2538     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2539     IDirect3DDevice8Impl_Reset,
2540     IDirect3DDevice8Impl_Present,
2541     IDirect3DDevice8Impl_GetBackBuffer,
2542     IDirect3DDevice8Impl_GetRasterStatus,
2543     IDirect3DDevice8Impl_SetGammaRamp,
2544     IDirect3DDevice8Impl_GetGammaRamp,
2545     IDirect3DDevice8Impl_CreateTexture,
2546     IDirect3DDevice8Impl_CreateVolumeTexture,
2547     IDirect3DDevice8Impl_CreateCubeTexture,
2548     IDirect3DDevice8Impl_CreateVertexBuffer,
2549     IDirect3DDevice8Impl_CreateIndexBuffer,
2550     IDirect3DDevice8Impl_CreateRenderTarget,
2551     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2552     IDirect3DDevice8Impl_CreateImageSurface,
2553     IDirect3DDevice8Impl_CopyRects,
2554     IDirect3DDevice8Impl_UpdateTexture,
2555     IDirect3DDevice8Impl_GetFrontBuffer,
2556     IDirect3DDevice8Impl_SetRenderTarget,
2557     IDirect3DDevice8Impl_GetRenderTarget,
2558     IDirect3DDevice8Impl_GetDepthStencilSurface,
2559     IDirect3DDevice8Impl_BeginScene,
2560     IDirect3DDevice8Impl_EndScene,
2561     IDirect3DDevice8Impl_Clear,
2562     IDirect3DDevice8Impl_SetTransform,
2563     IDirect3DDevice8Impl_GetTransform,
2564     IDirect3DDevice8Impl_MultiplyTransform,
2565     IDirect3DDevice8Impl_SetViewport,
2566     IDirect3DDevice8Impl_GetViewport,
2567     IDirect3DDevice8Impl_SetMaterial,
2568     IDirect3DDevice8Impl_GetMaterial,
2569     IDirect3DDevice8Impl_SetLight,
2570     IDirect3DDevice8Impl_GetLight,
2571     IDirect3DDevice8Impl_LightEnable,
2572     IDirect3DDevice8Impl_GetLightEnable,
2573     IDirect3DDevice8Impl_SetClipPlane,
2574     IDirect3DDevice8Impl_GetClipPlane,
2575     IDirect3DDevice8Impl_SetRenderState,
2576     IDirect3DDevice8Impl_GetRenderState,
2577     IDirect3DDevice8Impl_BeginStateBlock,
2578     IDirect3DDevice8Impl_EndStateBlock,
2579     IDirect3DDevice8Impl_ApplyStateBlock,
2580     IDirect3DDevice8Impl_CaptureStateBlock,
2581     IDirect3DDevice8Impl_DeleteStateBlock,
2582     IDirect3DDevice8Impl_CreateStateBlock,
2583     IDirect3DDevice8Impl_SetClipStatus,
2584     IDirect3DDevice8Impl_GetClipStatus,
2585     IDirect3DDevice8Impl_GetTexture,
2586     IDirect3DDevice8Impl_SetTexture,
2587     IDirect3DDevice8Impl_GetTextureStageState,
2588     IDirect3DDevice8Impl_SetTextureStageState,
2589     IDirect3DDevice8Impl_ValidateDevice,
2590     IDirect3DDevice8Impl_GetInfo,
2591     IDirect3DDevice8Impl_SetPaletteEntries,
2592     IDirect3DDevice8Impl_GetPaletteEntries,
2593     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2594     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2595     IDirect3DDevice8Impl_DrawPrimitive,
2596     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2597     IDirect3DDevice8Impl_DrawPrimitiveUP,
2598     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2599     IDirect3DDevice8Impl_ProcessVertices,
2600     IDirect3DDevice8Impl_CreateVertexShader,
2601     IDirect3DDevice8Impl_SetVertexShader,
2602     IDirect3DDevice8Impl_GetVertexShader,
2603     IDirect3DDevice8Impl_DeleteVertexShader,
2604     IDirect3DDevice8Impl_SetVertexShaderConstant,
2605     IDirect3DDevice8Impl_GetVertexShaderConstant,
2606     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2607     IDirect3DDevice8Impl_GetVertexShaderFunction,
2608     IDirect3DDevice8Impl_SetStreamSource,
2609     IDirect3DDevice8Impl_GetStreamSource,
2610     IDirect3DDevice8Impl_SetIndices,
2611     IDirect3DDevice8Impl_GetIndices,
2612     IDirect3DDevice8Impl_CreatePixelShader,
2613     IDirect3DDevice8Impl_SetPixelShader,
2614     IDirect3DDevice8Impl_GetPixelShader,
2615     IDirect3DDevice8Impl_DeletePixelShader,
2616     IDirect3DDevice8Impl_SetPixelShaderConstant,
2617     IDirect3DDevice8Impl_GetPixelShaderConstant,
2618     IDirect3DDevice8Impl_GetPixelShaderFunction,
2619     IDirect3DDevice8Impl_DrawRectPatch,
2620     IDirect3DDevice8Impl_DrawTriPatch,
2621     IDirect3DDevice8Impl_DeletePatch
2622 };
2623
2624 ULONG WINAPI D3D8CB_DestroySurface(IWineD3DSurface *pSurface) {
2625     IDirect3DSurface8Impl* surfaceParent;
2626     TRACE("(%p) call back\n", pSurface);
2627
2628     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
2629     /* GetParent's AddRef was forwarded to an object in destruction.
2630      * Releasing it here again would cause an endless recursion. */
2631     surfaceParent->forwardReference = NULL;
2632     return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
2633 }
2634
2635 /* IWineD3DDeviceParent IUnknown methods */
2636
2637 static inline struct IDirect3DDevice8Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2638 {
2639     return (struct IDirect3DDevice8Impl *)((char*)iface
2640             - FIELD_OFFSET(struct IDirect3DDevice8Impl, device_parent_vtbl));
2641 }
2642
2643 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
2644 {
2645     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2646     return IDirect3DDevice8Impl_QueryInterface((IDirect3DDevice8 *)This, riid, object);
2647 }
2648
2649 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2650 {
2651     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2652     return IDirect3DDevice8Impl_AddRef((IDirect3DDevice8 *)This);
2653 }
2654
2655 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2656 {
2657     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2658     return IDirect3DDevice8Impl_Release((IDirect3DDevice8 *)This);
2659 }
2660
2661 /* IWineD3DDeviceParent methods */
2662
2663 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2664 {
2665     TRACE("iface %p, device %p\n", iface, device);
2666 }
2667
2668 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2669         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
2670         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2671 {
2672     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2673     IDirect3DSurface8Impl *d3d_surface;
2674     BOOL lockable = TRUE;
2675     HRESULT hr;
2676
2677     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
2678             "\tpool %#x, level %u, face %u, surface %p\n",
2679             iface, superior, width, height, format, usage, pool, level, face, surface);
2680
2681
2682     if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2683
2684     hr = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)This, width, height,
2685             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2686             (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2687     if (FAILED(hr))
2688     {
2689         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2690         return hr;
2691     }
2692
2693     *surface = d3d_surface->wineD3DSurface;
2694     d3d_surface->container = superior;
2695     IUnknown_Release(d3d_surface->parentDevice);
2696     d3d_surface->parentDevice = NULL;
2697     d3d_surface->forwardReference = superior;
2698
2699     return hr;
2700 }
2701
2702 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2703         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2704         DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
2705 {
2706     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2707     IDirect3DSurface8Impl *d3d_surface;
2708     HRESULT hr;
2709
2710     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2711             "\tmultisample_quality %u, lockable %u, surface %p\n",
2712             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
2713
2714     hr = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)This, width, height,
2715             d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2716     if (FAILED(hr))
2717     {
2718         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2719         return hr;
2720     }
2721
2722     *surface = d3d_surface->wineD3DSurface;
2723     d3d_surface->container = (IUnknown *)This;
2724     d3d_surface->isImplicit = TRUE;
2725     /* Implicit surfaces are created with an refcount of 0 */
2726     IUnknown_Release((IUnknown *)d3d_surface);
2727
2728     return hr;
2729 }
2730
2731 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2732         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2733         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2734 {
2735     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2736     IDirect3DSurface8Impl *d3d_surface;
2737     HRESULT hr;
2738
2739     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2740             "\tmultisample_quality %u, discard %u, surface %p\n",
2741             iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
2742
2743     hr = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)This, width, height,
2744             d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2745     if (FAILED(hr))
2746     {
2747         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2748         return hr;
2749     }
2750
2751     *surface = d3d_surface->wineD3DSurface;
2752     d3d_surface->container = (IUnknown *)This;
2753     d3d_surface->isImplicit = TRUE;
2754     /* Implicit surfaces are created with an refcount of 0 */
2755     IUnknown_Release((IUnknown *)d3d_surface);
2756
2757     return hr;
2758 }
2759
2760 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2761         IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
2762         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2763 {
2764     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2765     IDirect3DVolume8Impl *object;
2766     HRESULT hr;
2767
2768     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2769             iface, superior, width, height, depth, format, pool, usage, volume);
2770
2771     /* Allocate the storage for the device */
2772     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2773     if (!object)
2774     {
2775         FIXME("Allocation of memory failed\n");
2776         *volume = NULL;
2777         return D3DERR_OUTOFVIDEOMEMORY;
2778     }
2779
2780     object->lpVtbl = &Direct3DVolume8_Vtbl;
2781     object->ref = 1;
2782     hr = IWineD3DDevice_CreateVolume(This->WineD3DDevice, width, height, depth, usage,
2783             format, pool, &object->wineD3DVolume, (IUnknown *)object);
2784     if (FAILED(hr))
2785     {
2786         ERR("(%p) CreateVolume failed, returning %#x\n", iface, hr);
2787         HeapFree(GetProcessHeap(), 0, object);
2788         *volume = NULL;
2789         return hr;
2790     }
2791
2792     *volume = object->wineD3DVolume;
2793     object->container = superior;
2794     object->forwardReference = superior;
2795
2796     TRACE("(%p) Created volume %p\n", iface, *volume);
2797
2798     return hr;
2799 }
2800
2801 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2802         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2803 {
2804     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2805     IDirect3DSwapChain8Impl *d3d_swapchain;
2806     D3DPRESENT_PARAMETERS local_parameters;
2807     HRESULT hr;
2808
2809     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2810
2811     /* Copy the presentation parameters */
2812     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2813     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2814     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2815     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2816     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2817     local_parameters.SwapEffect = present_parameters->SwapEffect;
2818     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2819     local_parameters.Windowed = present_parameters->Windowed;
2820     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2821     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2822     local_parameters.Flags = present_parameters->Flags;
2823     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2824     local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
2825
2826     hr = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)This,
2827             &local_parameters, (IDirect3DSwapChain8 **)&d3d_swapchain);
2828     if (FAILED(hr))
2829     {
2830         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2831         *swapchain = NULL;
2832         return hr;
2833     }
2834
2835     *swapchain = d3d_swapchain->wineD3DSwapChain;
2836     IUnknown_Release(d3d_swapchain->parentDevice);
2837     d3d_swapchain->parentDevice = NULL;
2838
2839     /* Copy back the presentation parameters */
2840     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2841     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2842     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2843     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2844     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2845     present_parameters->SwapEffect = local_parameters.SwapEffect;
2846     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2847     present_parameters->Windowed = local_parameters.Windowed;
2848     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2849     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2850     present_parameters->Flags = local_parameters.Flags;
2851     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2852     present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
2853
2854     return hr;
2855 }
2856
2857 const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
2858 {
2859     /* IUnknown methods */
2860     device_parent_QueryInterface,
2861     device_parent_AddRef,
2862     device_parent_Release,
2863     /* IWineD3DDeviceParent methods */
2864     device_parent_WineD3DDeviceCreated,
2865     device_parent_CreateSurface,
2866     device_parent_CreateRenderTarget,
2867     device_parent_CreateDepthStencilSurface,
2868     device_parent_CreateVolume,
2869     device_parent_CreateSwapChain,
2870 };