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