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