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