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