user32/tests: Move shell test to the end - killing explorer affects lots of tests.
[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 /* Shader handle functions */
40 static shader_handle *alloc_shader_handle(IDirect3DDevice8Impl *This) {
41     if (This->free_shader_handles) {
42         /* Use a free handle */
43         shader_handle *handle = This->free_shader_handles;
44         This->free_shader_handles = *handle;
45         return handle;
46     }
47     if (!(This->allocated_shader_handles < This->shader_handle_table_size)) {
48         /* Grow the table */
49         DWORD new_size = This->shader_handle_table_size + (This->shader_handle_table_size >> 1);
50         shader_handle *new_handles = HeapReAlloc(GetProcessHeap(), 0, This->shader_handles, new_size * sizeof(shader_handle));
51         if (!new_handles) return NULL;
52         This->shader_handles = new_handles;
53         This->shader_handle_table_size = new_size;
54     }
55
56     return &This->shader_handles[This->allocated_shader_handles++];
57 }
58
59 static void free_shader_handle(IDirect3DDevice8Impl *This, shader_handle *handle) {
60     *handle = This->free_shader_handles;
61     This->free_shader_handles = handle;
62 }
63
64 /* IDirect3D IUnknown parts follow: */
65 static HRESULT WINAPI IDirect3DDevice8Impl_QueryInterface(LPDIRECT3DDEVICE8 iface,REFIID riid,LPVOID *ppobj)
66 {
67     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
68
69     if (IsEqualGUID(riid, &IID_IUnknown)
70         || IsEqualGUID(riid, &IID_IDirect3DDevice8)) {
71         IUnknown_AddRef(iface);
72         *ppobj = This;
73         return S_OK;
74     }
75
76     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
77     *ppobj = NULL;
78     return E_NOINTERFACE;
79 }
80
81 static ULONG WINAPI IDirect3DDevice8Impl_AddRef(LPDIRECT3DDEVICE8 iface) {
82     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
83     ULONG ref = InterlockedIncrement(&This->ref);
84
85     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
86
87     return ref;
88 }
89
90 static ULONG WINAPI IDirect3DDevice8Impl_Release(LPDIRECT3DDEVICE8 iface) {
91     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
92     ULONG ref;
93
94     if (This->inDestruction) return 0;
95     ref = InterlockedDecrement(&This->ref);
96
97     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
98
99     if (ref == 0) {
100         unsigned i;
101
102         TRACE("Releasing wined3d device %p\n", This->WineD3DDevice);
103         EnterCriticalSection(&d3d8_cs);
104         This->inDestruction = TRUE;
105
106         for(i = 0; i < This->numConvertedDecls; i++) {
107             IWineD3DVertexDeclaration_Release(This->decls[i].decl);
108         }
109         HeapFree(GetProcessHeap(), 0, This->decls);
110
111         IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D8CB_DestroyDepthStencilSurface, D3D8CB_DestroySwapChain);
112         IWineD3DDevice_Release(This->WineD3DDevice);
113         HeapFree(GetProcessHeap(), 0, This->shader_handles);
114         HeapFree(GetProcessHeap(), 0, This);
115         LeaveCriticalSection(&d3d8_cs);
116     }
117     return ref;
118 }
119
120 /* IDirect3DDevice Interface follow: */
121 static HRESULT WINAPI IDirect3DDevice8Impl_TestCooperativeLevel(LPDIRECT3DDEVICE8 iface) {
122     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
123     HRESULT hr;
124
125     TRACE("(%p) : Relay\n", This);
126     EnterCriticalSection(&d3d8_cs);
127     hr = IWineD3DDevice_TestCooperativeLevel(This->WineD3DDevice);
128     LeaveCriticalSection(&d3d8_cs);
129     return hr;
130 }
131
132 static UINT WINAPI  IDirect3DDevice8Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE8 iface) {
133     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
134     HRESULT hr;
135
136     TRACE("(%p) Relay\n", This);
137     EnterCriticalSection(&d3d8_cs);
138     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
139     LeaveCriticalSection(&d3d8_cs);
140     return hr;
141 }
142
143 static HRESULT WINAPI IDirect3DDevice8Impl_ResourceManagerDiscardBytes(LPDIRECT3DDEVICE8 iface, DWORD Bytes) {
144     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
145     HRESULT hr;
146
147     TRACE("(%p) : Relay bytes(%d)\n", This, Bytes);
148     EnterCriticalSection(&d3d8_cs);
149     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
150     LeaveCriticalSection(&d3d8_cs);
151     return hr;
152 }
153
154 static HRESULT WINAPI IDirect3DDevice8Impl_GetDirect3D(LPDIRECT3DDEVICE8 iface, IDirect3D8** ppD3D8) {
155     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
156     HRESULT hr = D3D_OK;
157     IWineD3D* pWineD3D;
158
159     TRACE("(%p) Relay\n", This);
160
161     if (NULL == ppD3D8) {
162         return D3DERR_INVALIDCALL;
163     }
164
165     EnterCriticalSection(&d3d8_cs);
166     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
167     if (hr == D3D_OK && pWineD3D != NULL)
168     {
169         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D8);
170         IWineD3D_Release(pWineD3D);
171     } else {
172         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
173         *ppD3D8 = NULL;
174     }
175     TRACE("(%p) returning %p\n",This , *ppD3D8);
176     LeaveCriticalSection(&d3d8_cs);
177
178     return hr;
179 }
180
181 static HRESULT WINAPI IDirect3DDevice8Impl_GetDeviceCaps(LPDIRECT3DDEVICE8 iface, D3DCAPS8* pCaps) {
182     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
183     HRESULT hrc = D3D_OK;
184     WINED3DCAPS *pWineCaps;
185
186     TRACE("(%p) : Relay pCaps %p\n", This, pCaps);
187     if(NULL == pCaps){
188         return D3DERR_INVALIDCALL;
189     }
190     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
191     if(pWineCaps == NULL){
192         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
193     }
194
195     EnterCriticalSection(&d3d8_cs);
196     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
197     LeaveCriticalSection(&d3d8_cs);
198     WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
199     HeapFree(GetProcessHeap(), 0, pWineCaps);
200
201     /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
202     if(pCaps->PixelShaderVersion > D3DPS_VERSION(1,4)){
203         pCaps->PixelShaderVersion = D3DPS_VERSION(1,4);
204     }
205     if(pCaps->VertexShaderVersion > D3DVS_VERSION(1,1)){
206         pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
207     }
208
209     TRACE("Returning %p %p\n", This, pCaps);
210     return hrc;
211 }
212
213 static HRESULT WINAPI IDirect3DDevice8Impl_GetDisplayMode(LPDIRECT3DDEVICE8 iface, D3DDISPLAYMODE* pMode) {
214     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
215     HRESULT hr;
216     TRACE("(%p) Relay\n", This);
217
218     EnterCriticalSection(&d3d8_cs);
219     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, 0, (WINED3DDISPLAYMODE *) pMode);
220     LeaveCriticalSection(&d3d8_cs);
221     return hr;
222 }
223
224 static HRESULT WINAPI IDirect3DDevice8Impl_GetCreationParameters(LPDIRECT3DDEVICE8 iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
225     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
226     HRESULT hr;
227     TRACE("(%p) Relay\n", This);
228
229     EnterCriticalSection(&d3d8_cs);
230     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
231     LeaveCriticalSection(&d3d8_cs);
232     return hr;
233 }
234
235 static HRESULT WINAPI IDirect3DDevice8Impl_SetCursorProperties(LPDIRECT3DDEVICE8 iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8* pCursorBitmap) {
236     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
237     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl*)pCursorBitmap;
238     HRESULT hr;
239     TRACE("(%p) Relay\n", This);
240     if(!pCursorBitmap) {
241         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
242         return WINED3DERR_INVALIDCALL;
243     }
244
245     EnterCriticalSection(&d3d8_cs);
246     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice,XHotSpot,YHotSpot,pSurface->wineD3DSurface);
247     LeaveCriticalSection(&d3d8_cs);
248     return hr;
249 }
250
251 static void WINAPI IDirect3DDevice8Impl_SetCursorPosition(LPDIRECT3DDEVICE8 iface, UINT XScreenSpace, UINT YScreenSpace, DWORD Flags) {
252     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
253     TRACE("(%p) Relay\n", This);
254
255     EnterCriticalSection(&d3d8_cs);
256     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
257     LeaveCriticalSection(&d3d8_cs);
258 }
259
260 static BOOL WINAPI IDirect3DDevice8Impl_ShowCursor(LPDIRECT3DDEVICE8 iface, BOOL bShow) {
261     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
262     BOOL ret;
263     TRACE("(%p) Relay\n", This);
264
265     EnterCriticalSection(&d3d8_cs);
266     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
267     LeaveCriticalSection(&d3d8_cs);
268     return ret;
269 }
270
271 static HRESULT WINAPI IDirect3DDevice8Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain8** pSwapChain) {
272     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
273     IDirect3DSwapChain8Impl* object;
274     HRESULT hrc = D3D_OK;
275     WINED3DPRESENT_PARAMETERS localParameters;
276
277     TRACE("(%p) Relay\n", This);
278
279     /* Fix the back buffer count */
280     if(pPresentationParameters->BackBufferCount == 0) {
281         pPresentationParameters->BackBufferCount = 1;
282     }
283
284     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
285     if (NULL == object) {
286         FIXME("Allocation of memory failed\n");
287         *pSwapChain = NULL;
288         return D3DERR_OUTOFVIDEOMEMORY;
289     }
290     object->ref = 1;
291     object->lpVtbl = &Direct3DSwapChain8_Vtbl;
292
293     /* Allocate an associated WineD3DDevice object */
294     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
295     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
296     localParameters.BackBufferFormat                            = pPresentationParameters->BackBufferFormat;
297     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
298     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
299     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
300     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
301     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
302     localParameters.Windowed                                    = pPresentationParameters->Windowed;
303     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
304     localParameters.AutoDepthStencilFormat                      = pPresentationParameters->AutoDepthStencilFormat;
305     localParameters.Flags                                       = pPresentationParameters->Flags;
306     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
307     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
308
309     EnterCriticalSection(&d3d8_cs);
310     hrc = IWineD3DDevice_CreateAdditionalSwapChain(This->WineD3DDevice, &localParameters, &object->wineD3DSwapChain, (IUnknown*)object, D3D8CB_CreateRenderTarget, D3D8CB_CreateDepthStencilSurface, SURFACE_OPENGL);
311     LeaveCriticalSection(&d3d8_cs);
312
313     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
314     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
315     pPresentationParameters->BackBufferFormat                   = localParameters.BackBufferFormat;
316     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
317     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
318     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
319     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
320     pPresentationParameters->Windowed                           = localParameters.Windowed;
321     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
322     pPresentationParameters->AutoDepthStencilFormat             = localParameters.AutoDepthStencilFormat;
323     pPresentationParameters->Flags                              = localParameters.Flags;
324     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
325     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
326
327     if (hrc != D3D_OK) {
328         FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
329         HeapFree(GetProcessHeap(), 0 , object);
330         *pSwapChain = NULL;
331     }else{
332         IUnknown_AddRef(iface);
333         object->parentDevice = iface;
334         *pSwapChain = (IDirect3DSwapChain8 *)object;
335     }
336     TRACE("(%p) returning %p\n", This, *pSwapChain);
337     return hrc;
338 }
339
340 static HRESULT WINAPI IDirect3DDevice8Impl_Reset(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
341     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
342     WINED3DPRESENT_PARAMETERS localParameters;
343     HRESULT hr;
344
345     TRACE("(%p) Relay pPresentationParameters(%p)\n", This, pPresentationParameters);
346
347     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
348     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
349     localParameters.BackBufferFormat                            = pPresentationParameters->BackBufferFormat;
350     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
351     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
352     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
353     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
354     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
355     localParameters.Windowed                                    = pPresentationParameters->Windowed;
356     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
357     localParameters.AutoDepthStencilFormat                      = pPresentationParameters->AutoDepthStencilFormat;
358     localParameters.Flags                                       = pPresentationParameters->Flags;
359     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
360     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
361
362     EnterCriticalSection(&d3d8_cs);
363     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
364     LeaveCriticalSection(&d3d8_cs);
365
366     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
367     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
368     pPresentationParameters->BackBufferFormat                   = localParameters.BackBufferFormat;
369     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
370     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
371     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
372     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
373     pPresentationParameters->Windowed                           = localParameters.Windowed;
374     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
375     pPresentationParameters->AutoDepthStencilFormat             = localParameters.AutoDepthStencilFormat;
376     pPresentationParameters->Flags                              = localParameters.Flags;
377     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
378     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
379
380     return hr;
381 }
382
383 static HRESULT WINAPI IDirect3DDevice8Impl_Present(LPDIRECT3DDEVICE8 iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) {
384     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
385     HRESULT hr;
386     TRACE("(%p) Relay\n", This);
387
388     EnterCriticalSection(&d3d8_cs);
389     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
390     LeaveCriticalSection(&d3d8_cs);
391     return hr;
392 }
393
394 static HRESULT WINAPI IDirect3DDevice8Impl_GetBackBuffer(LPDIRECT3DDEVICE8 iface, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8** ppBackBuffer) {
395     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
396     IWineD3DSurface *retSurface = NULL;
397     HRESULT rc = D3D_OK;
398
399     TRACE("(%p) Relay\n", This);
400
401     EnterCriticalSection(&d3d8_cs);
402     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, 0, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &retSurface);
403     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
404         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
405         IWineD3DSurface_Release(retSurface);
406     }
407     LeaveCriticalSection(&d3d8_cs);
408     return rc;
409 }
410
411 static HRESULT WINAPI IDirect3DDevice8Impl_GetRasterStatus(LPDIRECT3DDEVICE8 iface, D3DRASTER_STATUS* pRasterStatus) {
412     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
413     HRESULT hr;
414     TRACE("(%p) Relay\n", This);
415
416     EnterCriticalSection(&d3d8_cs);
417     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, 0, (WINED3DRASTER_STATUS *) pRasterStatus);
418     LeaveCriticalSection(&d3d8_cs);
419     return hr;
420 }
421
422 static void WINAPI IDirect3DDevice8Impl_SetGammaRamp(LPDIRECT3DDEVICE8 iface, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
423     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
424     TRACE("(%p) Relay\n", This);
425
426     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
427     EnterCriticalSection(&d3d8_cs);
428     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, 0, Flags, (CONST WINED3DGAMMARAMP *) pRamp);
429     LeaveCriticalSection(&d3d8_cs);
430 }
431
432 static void WINAPI IDirect3DDevice8Impl_GetGammaRamp(LPDIRECT3DDEVICE8 iface, D3DGAMMARAMP* pRamp) {
433     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
434     TRACE("(%p) Relay\n", This);
435
436     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
437     EnterCriticalSection(&d3d8_cs);
438     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, 0, (WINED3DGAMMARAMP *) pRamp);
439     LeaveCriticalSection(&d3d8_cs);
440 }
441
442 static HRESULT WINAPI IDirect3DDevice8Impl_CreateTexture(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
443                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture8 **ppTexture) {
444     IDirect3DTexture8Impl *object;
445     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
446     HRESULT hrc = D3D_OK;
447
448     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%d), Fmt(%u), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
449
450     /* Allocate the storage for the device */
451     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture8Impl));
452
453     if (NULL == object) {
454         FIXME("Allocation of memory failed\n");
455 /*        *ppTexture = NULL; */
456         return D3DERR_OUTOFVIDEOMEMORY;
457     }
458
459     object->lpVtbl = &Direct3DTexture8_Vtbl;
460     object->ref = 1;
461     EnterCriticalSection(&d3d8_cs);
462     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage & WINED3DUSAGE_MASK,
463                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DTexture, NULL, (IUnknown *)object, D3D8CB_CreateSurface);
464     LeaveCriticalSection(&d3d8_cs);
465
466     if (FAILED(hrc)) {
467         /* free up object */ 
468         FIXME("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
469         HeapFree(GetProcessHeap(), 0, object);
470 /*      *ppTexture = NULL; */
471    } else {
472         IUnknown_AddRef(iface);
473         object->parentDevice = iface;
474         *ppTexture = (LPDIRECT3DTEXTURE8) object;
475         TRACE("(%p) Created Texture %p, %p\n",This,object,object->wineD3DTexture);
476    }
477
478    return hrc;
479 }
480
481 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVolumeTexture(LPDIRECT3DDEVICE8 iface, 
482                                                           UINT Width, UINT Height, UINT Depth, UINT Levels, DWORD Usage, 
483                                                           D3DFORMAT Format, D3DPOOL Pool, IDirect3DVolumeTexture8** ppVolumeTexture) {
484
485     IDirect3DVolumeTexture8Impl *object;
486     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
487     HRESULT hrc = D3D_OK;
488
489     TRACE("(%p) Relay\n", This);
490
491     /* Allocate the storage for the device */
492     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVolumeTexture8Impl));
493     if (NULL == object) {
494         FIXME("(%p) allocation of memory failed\n", This);
495         *ppVolumeTexture = NULL;
496         return D3DERR_OUTOFVIDEOMEMORY;
497     }
498
499     object->lpVtbl = &Direct3DVolumeTexture8_Vtbl;
500     object->ref = 1;
501     EnterCriticalSection(&d3d8_cs);
502     hrc = IWineD3DDevice_CreateVolumeTexture(This->WineD3DDevice, Width, Height, Depth, Levels, Usage & WINED3DUSAGE_MASK,
503                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DVolumeTexture, NULL,
504                                  (IUnknown *)object, D3D8CB_CreateVolume);
505     LeaveCriticalSection(&d3d8_cs);
506
507     if (hrc != D3D_OK) {
508
509         /* free up object */
510         FIXME("(%p) call to IWineD3DDevice_CreateVolumeTexture failed\n", This);
511         HeapFree(GetProcessHeap(), 0, object);
512         *ppVolumeTexture = NULL;
513     } else {
514         IUnknown_AddRef(iface);
515         object->parentDevice = iface;
516         *ppVolumeTexture = (LPDIRECT3DVOLUMETEXTURE8) object;
517     }
518     TRACE("(%p)  returning %p\n", This , *ppVolumeTexture);
519     return hrc;
520 }
521
522 static HRESULT WINAPI IDirect3DDevice8Impl_CreateCubeTexture(LPDIRECT3DDEVICE8 iface, UINT EdgeLength, UINT Levels, DWORD Usage, 
523                                                         D3DFORMAT Format, D3DPOOL Pool, IDirect3DCubeTexture8** ppCubeTexture) {
524
525     IDirect3DCubeTexture8Impl *object;
526     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
527     HRESULT hr = D3D_OK;
528
529     TRACE("(%p) : ELen(%d) Lvl(%d) Usage(%d) fmt(%u), Pool(%d)\n" , This, EdgeLength, Levels, Usage, Format, Pool);
530
531     /* Allocate the storage for the device */
532     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
533
534     if (NULL == object) {
535         FIXME("(%p) allocation of CubeTexture failed\n", This);
536         *ppCubeTexture = NULL;
537         return D3DERR_OUTOFVIDEOMEMORY;
538     }
539
540     object->lpVtbl = &Direct3DCubeTexture8_Vtbl;
541     object->ref = 1;
542     EnterCriticalSection(&d3d8_cs);
543     hr = IWineD3DDevice_CreateCubeTexture(This->WineD3DDevice, EdgeLength, Levels, Usage & WINED3DUSAGE_MASK,
544                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DCubeTexture, NULL, (IUnknown*)object,
545                                  D3D8CB_CreateSurface);
546     LeaveCriticalSection(&d3d8_cs);
547
548     if (hr != D3D_OK){
549
550         /* free up object */
551         FIXME("(%p) call to IWineD3DDevice_CreateCubeTexture failed\n", This);
552         HeapFree(GetProcessHeap(), 0, object);
553         *ppCubeTexture = NULL;
554     } else {
555         IUnknown_AddRef(iface);
556         object->parentDevice = iface;
557         *ppCubeTexture = (LPDIRECT3DCUBETEXTURE8) object;
558     }
559
560     TRACE("(%p) returning %p\n",This, *ppCubeTexture);
561     return hr;
562 }
563
564 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexBuffer(LPDIRECT3DDEVICE8 iface, UINT Size, DWORD Usage, DWORD FVF, D3DPOOL Pool, IDirect3DVertexBuffer8** ppVertexBuffer) {
565     IDirect3DVertexBuffer8Impl *object;
566     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
567     HRESULT hrc = D3D_OK;
568
569     TRACE("(%p) Relay\n", This);
570     /* Allocate the storage for the device */
571     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexBuffer8Impl));
572     if (NULL == object) {
573         FIXME("Allocation of memory failed\n");
574         *ppVertexBuffer = NULL;
575         return D3DERR_OUTOFVIDEOMEMORY;
576     }
577
578     object->lpVtbl = &Direct3DVertexBuffer8_Vtbl;
579     object->ref = 1;
580     EnterCriticalSection(&d3d8_cs);
581     hrc = IWineD3DDevice_CreateVertexBuffer(This->WineD3DDevice, Size, Usage & WINED3DUSAGE_MASK, FVF, (WINED3DPOOL) Pool, &(object->wineD3DVertexBuffer), NULL, (IUnknown *)object);
582     LeaveCriticalSection(&d3d8_cs);
583
584     if (D3D_OK != hrc) {
585
586         /* free up object */
587         FIXME("(%p) call to IWineD3DDevice_CreateVertexBuffer failed\n", This);
588         HeapFree(GetProcessHeap(), 0, object);
589         *ppVertexBuffer = NULL;
590     } else {
591         IUnknown_AddRef(iface);
592         object->parentDevice = iface;
593         *ppVertexBuffer = (LPDIRECT3DVERTEXBUFFER8) object;
594     }
595     return hrc;
596 }
597
598 static HRESULT WINAPI IDirect3DDevice8Impl_CreateIndexBuffer(LPDIRECT3DDEVICE8 iface, UINT Length, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DIndexBuffer8** ppIndexBuffer) {
599     IDirect3DIndexBuffer8Impl *object;
600     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
601     HRESULT hrc = D3D_OK;
602
603     TRACE("(%p) Relay\n", This);
604     /* Allocate the storage for the device */
605     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
606     if (NULL == object) {
607         FIXME("Allocation of memory failed\n");
608         *ppIndexBuffer = NULL;
609         return D3DERR_OUTOFVIDEOMEMORY;
610     }
611
612     object->lpVtbl = &Direct3DIndexBuffer8_Vtbl;
613     object->ref = 1;
614     TRACE("Calling wined3d create index buffer\n");
615     EnterCriticalSection(&d3d8_cs);
616     hrc = IWineD3DDevice_CreateIndexBuffer(This->WineD3DDevice, Length, Usage & WINED3DUSAGE_MASK, Format, (WINED3DPOOL) Pool, &object->wineD3DIndexBuffer, NULL, (IUnknown *)object);
617     LeaveCriticalSection(&d3d8_cs);
618
619     if (D3D_OK != hrc) {
620
621         /* free up object */
622         FIXME("(%p) call to IWineD3DDevice_CreateIndexBuffer failed\n", This);
623         HeapFree(GetProcessHeap(), 0, object);
624         *ppIndexBuffer = NULL;
625     } else {
626         IUnknown_AddRef(iface);
627         object->parentDevice = iface;
628         *ppIndexBuffer = (LPDIRECT3DINDEXBUFFER8)object;
629     }
630     return hrc;
631 }
632
633 static HRESULT WINAPI 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)  {
634     HRESULT hrc;
635     IDirect3DSurface8Impl *object;
636     IDirect3DDevice8Impl  *This = (IDirect3DDevice8Impl *)iface;
637     TRACE("(%p) Relay\n", This);
638
639     if(MultisampleQuality > 0){
640         FIXME("MultisampleQuality set to %d, substituting 0\n" , MultisampleQuality);
641         /*
642         MultisampleQuality
643         [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.
644         */
645         MultisampleQuality=0;
646     }
647     /*FIXME: Check MAX bounds of MultisampleQuality*/
648
649     /* Allocate the storage for the device */
650     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
651     if (NULL == object) {
652         FIXME("Allocation of memory failed\n");
653         *ppSurface = NULL;
654         return D3DERR_OUTOFVIDEOMEMORY;
655     }
656
657     object->lpVtbl = &Direct3DSurface8_Vtbl;
658     object->ref = 1;
659
660     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
661
662     /* Not called from the VTable, no locking needed */
663     hrc = IWineD3DDevice_CreateSurface(This->WineD3DDevice, Width, Height, Format, Lockable, Discard, Level,  &object->wineD3DSurface, Type, Usage & WINED3DUSAGE_MASK, (WINED3DPOOL) Pool,MultiSample,MultisampleQuality, NULL, SURFACE_OPENGL, (IUnknown *)object);
664     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
665        /* free up object */
666         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
667         HeapFree(GetProcessHeap(), 0, object);
668         *ppSurface = NULL;
669     } else {
670         IUnknown_AddRef(iface);
671         object->parentDevice = iface;
672         *ppSurface = (LPDIRECT3DSURFACE8) object;
673     }
674     return hrc;
675 }
676
677 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8** ppSurface) {
678     HRESULT hr;
679     TRACE("Relay\n");
680
681     EnterCriticalSection(&d3d8_cs);
682     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */, 0 /* Level */ , ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, 0);
683     LeaveCriticalSection(&d3d8_cs);
684     return hr;
685 }
686
687 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8** ppSurface) {
688     HRESULT hr;
689     TRACE("Relay\n");
690
691     /* TODO: Verify that Discard is false */
692     EnterCriticalSection(&d3d8_cs);
693     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE, 0 /* Level */
694                                                ,ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_DEPTHSTENCIL,
695                                                 D3DPOOL_DEFAULT, MultiSample, 0);
696     LeaveCriticalSection(&d3d8_cs);
697     return hr;
698 }
699
700 /*  IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
701 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8** ppSurface) {
702     HRESULT hr;
703     TRACE("Relay\n");
704
705     EnterCriticalSection(&d3d8_cs);
706     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Loackable */ , FALSE /*Discard*/ , 0 /* Level */ , ppSurface,
707                                             D3DRTYPE_SURFACE, 0 /* Usage (undefined/none) */ , D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
708     LeaveCriticalSection(&d3d8_cs);
709     return hr;
710 }
711
712 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8 *pSourceSurface, CONST RECT *pSourceRects, UINT cRects, IDirect3DSurface8 *pDestinationSurface, CONST POINT *pDestPoints) {
713     IDirect3DSurface8Impl *Source = (IDirect3DSurface8Impl *) pSourceSurface;
714     IDirect3DSurface8Impl *Dest = (IDirect3DSurface8Impl *) pDestinationSurface;
715
716     HRESULT              hr = WINED3D_OK;
717     WINED3DFORMAT        srcFormat, destFormat;
718     UINT                 srcWidth,  destWidth;
719     UINT                 srcHeight, destHeight;
720     UINT                 srcSize;
721     WINED3DSURFACE_DESC  winedesc;
722
723     TRACE("(%p) pSrcSur=%p, pSourceRects=%p, cRects=%d, pDstSur=%p, pDestPtsArr=%p\n", iface,
724           pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
725
726
727     /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the destination texture is in WINED3DPOOL_DEFAULT */
728     memset(&winedesc, 0, sizeof(winedesc));
729
730     winedesc.Format = &srcFormat;
731     winedesc.Width  = &srcWidth;
732     winedesc.Height = &srcHeight;
733     winedesc.Size   = &srcSize;
734     IWineD3DSurface_GetDesc(Source->wineD3DSurface, &winedesc);
735
736     winedesc.Format = &destFormat;
737     winedesc.Width  = &destWidth;
738     winedesc.Height = &destHeight;
739     winedesc.Size   = NULL;
740     EnterCriticalSection(&d3d8_cs);
741     IWineD3DSurface_GetDesc(Dest->wineD3DSurface, &winedesc);
742
743     /* Check that the source and destination formats match */
744     if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat) {
745         WARN("(%p) source %p format must match the dest %p format, returning WINED3DERR_INVALIDCALL\n", iface, pSourceSurface, pDestinationSurface);
746         LeaveCriticalSection(&d3d8_cs);
747         return WINED3DERR_INVALIDCALL;
748     } else if (WINED3DFMT_UNKNOWN == destFormat) {
749         TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
750         IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
751         destFormat = srcFormat;
752     }
753
754     /* Quick if complete copy ... */
755     if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
756         IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
757     } else {
758         unsigned int i;
759         /* Copy rect by rect */
760         if (NULL != pSourceRects && NULL != pDestPoints) {
761             for (i = 0; i < cRects; ++i) {
762                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, (RECT *) &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
763             }
764         } else {
765             for (i = 0; i < cRects; ++i) {
766                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, (RECT *) &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
767             }
768         }
769     }
770     LeaveCriticalSection(&d3d8_cs);
771
772     return hr;
773 }
774
775 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
776     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
777     HRESULT hr;
778     TRACE("(%p) Relay\n" , This);
779
780     EnterCriticalSection(&d3d8_cs);
781     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
782     LeaveCriticalSection(&d3d8_cs);
783     return hr;
784 }
785
786 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
787     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
788     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
789     HRESULT hr;
790
791     TRACE("(%p) Relay\n" , This);
792
793     if (pDestSurface == NULL) {
794         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
795         return D3DERR_INVALIDCALL;
796     }
797
798     EnterCriticalSection(&d3d8_cs);
799     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
800     LeaveCriticalSection(&d3d8_cs);
801     return hr;
802 }
803
804 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
805     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
806     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
807     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
808     IWineD3DSurface *original_ds = NULL;
809     HRESULT hr;
810     TRACE("(%p) Relay\n" , This);
811
812     EnterCriticalSection(&d3d8_cs);
813
814     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &original_ds);
815     if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
816     {
817         hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, pZSurface ? pZSurface->wineD3DSurface : NULL);
818         if (SUCCEEDED(hr) && pSurface)
819             hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface->wineD3DSurface);
820         if (FAILED(hr)) IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, original_ds);
821     }
822     if (original_ds) IWineD3DSurface_Release(original_ds);
823
824     LeaveCriticalSection(&d3d8_cs);
825     return hr;
826 }
827
828 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
829     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
830     HRESULT hr = D3D_OK;
831     IWineD3DSurface *pRenderTarget;
832
833     TRACE("(%p) Relay\n" , This);
834
835     if (ppRenderTarget == NULL) {
836         return D3DERR_INVALIDCALL;
837     }
838     EnterCriticalSection(&d3d8_cs);
839     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
840
841     if (hr == D3D_OK && pRenderTarget != NULL) {
842         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
843         IWineD3DSurface_Release(pRenderTarget);
844     } else {
845         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
846         *ppRenderTarget = NULL;
847     }
848     LeaveCriticalSection(&d3d8_cs);
849
850     return hr;
851 }
852
853 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
854     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
855     HRESULT hr = D3D_OK;
856     IWineD3DSurface *pZStencilSurface;
857
858     TRACE("(%p) Relay\n" , This);
859     if(ppZStencilSurface == NULL){
860         return D3DERR_INVALIDCALL;
861     }
862
863     EnterCriticalSection(&d3d8_cs);
864     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
865     if (hr == WINED3D_OK) {
866         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
867         IWineD3DSurface_Release(pZStencilSurface);
868     }else{
869         if (hr != WINED3DERR_NOTFOUND)
870                 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
871         *ppZStencilSurface = NULL;
872     }
873     LeaveCriticalSection(&d3d8_cs);
874
875     return hr;
876 }
877
878 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
879     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
880     HRESULT hr;
881     TRACE("(%p) Relay\n" , This);
882
883     EnterCriticalSection(&d3d8_cs);
884     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
885     LeaveCriticalSection(&d3d8_cs);
886     return hr;
887 }
888
889 static HRESULT WINAPI IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
890     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
891     HRESULT hr;
892     TRACE("(%p) Relay\n" , This);
893
894     EnterCriticalSection(&d3d8_cs);
895     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
896     LeaveCriticalSection(&d3d8_cs);
897     return hr;
898 }
899
900 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
901     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
902     HRESULT hr;
903     TRACE("(%p) Relay\n" , This);
904
905     /* Note: D3DRECT is compatible with WINED3DRECT */
906     EnterCriticalSection(&d3d8_cs);
907     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
908     LeaveCriticalSection(&d3d8_cs);
909     return hr;
910 }
911
912 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
913     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
914     HRESULT hr;
915     TRACE("(%p) Relay\n" , This);
916
917     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
918     EnterCriticalSection(&d3d8_cs);
919     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
920     LeaveCriticalSection(&d3d8_cs);
921     return hr;
922 }
923
924 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
925     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
926     HRESULT hr;
927     TRACE("(%p) Relay\n" , This);
928
929     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
930     EnterCriticalSection(&d3d8_cs);
931     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
932     LeaveCriticalSection(&d3d8_cs);
933     return hr;
934 }
935
936 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
937     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
938     HRESULT hr;
939     TRACE("(%p) Relay\n" , This);
940
941     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
942     EnterCriticalSection(&d3d8_cs);
943     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
944     LeaveCriticalSection(&d3d8_cs);
945     return hr;
946 }
947
948 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
949     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
950     HRESULT hr;
951     TRACE("(%p) Relay\n" , This);
952
953     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
954     EnterCriticalSection(&d3d8_cs);
955     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
956     LeaveCriticalSection(&d3d8_cs);
957     return hr;
958 }
959
960 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
961     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
962     HRESULT hr;
963     TRACE("(%p) Relay\n" , This);
964
965     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
966     EnterCriticalSection(&d3d8_cs);
967     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
968     LeaveCriticalSection(&d3d8_cs);
969     return hr;
970 }
971
972 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
973     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
974     HRESULT hr;
975     TRACE("(%p) Relay\n" , This);
976
977     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
978     EnterCriticalSection(&d3d8_cs);
979     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
980     LeaveCriticalSection(&d3d8_cs);
981     return hr;
982 }
983
984 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
985     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
986     HRESULT hr;
987     TRACE("(%p) Relay\n" , This);
988
989     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
990     EnterCriticalSection(&d3d8_cs);
991     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
992     LeaveCriticalSection(&d3d8_cs);
993     return hr;
994 }
995
996 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
997     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
998     HRESULT hr;
999     TRACE("(%p) Relay\n" , This);
1000  
1001     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1002     EnterCriticalSection(&d3d8_cs);
1003     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1004     LeaveCriticalSection(&d3d8_cs);
1005     return hr;
1006 }
1007
1008 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
1009     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1010     HRESULT hr;
1011     TRACE("(%p) Relay\n" , This);
1012
1013     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1014     EnterCriticalSection(&d3d8_cs);
1015     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1016     LeaveCriticalSection(&d3d8_cs);
1017     return hr;
1018 }
1019
1020 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
1021     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1022     HRESULT hr;
1023     TRACE("(%p) Relay\n" , This);
1024
1025     EnterCriticalSection(&d3d8_cs);
1026     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1027     LeaveCriticalSection(&d3d8_cs);
1028     return hr;
1029 }
1030
1031 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
1032     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1033     HRESULT hr;
1034     TRACE("(%p) Relay\n" , This);
1035
1036     EnterCriticalSection(&d3d8_cs);
1037     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1038     LeaveCriticalSection(&d3d8_cs);
1039     return hr;
1040 }
1041
1042 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
1043     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1044     HRESULT hr;
1045     TRACE("(%p) Relay\n" , This);
1046
1047     EnterCriticalSection(&d3d8_cs);
1048     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1049     LeaveCriticalSection(&d3d8_cs);
1050     return hr;
1051 }
1052
1053 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
1054     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1055     HRESULT hr;
1056     TRACE("(%p) Relay\n" , This);
1057
1058     EnterCriticalSection(&d3d8_cs);
1059     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1060     LeaveCriticalSection(&d3d8_cs);
1061     return hr;
1062 }
1063
1064 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
1065     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1066     HRESULT hr;
1067     TRACE("(%p) Relay\n" , This);
1068
1069     EnterCriticalSection(&d3d8_cs);
1070     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1071     LeaveCriticalSection(&d3d8_cs);
1072     return hr;
1073 }
1074
1075 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
1076     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1077     HRESULT hr;
1078     TRACE("(%p) Relay\n" , This);
1079
1080     EnterCriticalSection(&d3d8_cs);
1081     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1082     LeaveCriticalSection(&d3d8_cs);
1083     return hr;
1084 }
1085
1086 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
1087     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1088     HRESULT hr;
1089     TRACE("(%p)\n", This);
1090
1091     EnterCriticalSection(&d3d8_cs);
1092     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1093     LeaveCriticalSection(&d3d8_cs);
1094     return hr;
1095 }
1096
1097 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
1098     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1099     HRESULT hr;
1100     IWineD3DStateBlock* wineD3DStateBlock;
1101     IDirect3DStateBlock8Impl* object;
1102
1103     TRACE("(%p) Relay\n", This);
1104
1105     /* Tell wineD3D to endstateblock before anything else (in case we run out
1106      * of memory later and cause locking problems)
1107      */
1108     EnterCriticalSection(&d3d8_cs);
1109     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
1110     if (hr != D3D_OK) {
1111         FIXME("IWineD3DDevice_EndStateBlock returned an error\n");
1112         LeaveCriticalSection(&d3d8_cs);
1113         return hr;
1114     }
1115
1116     /* allocate a new IDirectD3DStateBlock */
1117     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
1118     object->ref = 1;
1119     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1120
1121     object->wineD3DStateBlock = wineD3DStateBlock;
1122
1123     *pToken = (DWORD)object;
1124     TRACE("(%p)Returning %p %p\n", This, object, wineD3DStateBlock);
1125
1126     LeaveCriticalSection(&d3d8_cs);
1127     return hr;
1128 }
1129
1130 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1131     IDirect3DStateBlock8Impl *pSB  = (IDirect3DStateBlock8Impl*) Token;
1132     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
1133     HRESULT hr;
1134
1135     TRACE("(%p) %p Relay\n", This, pSB);
1136
1137     EnterCriticalSection(&d3d8_cs);
1138     hr = IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
1139     LeaveCriticalSection(&d3d8_cs);
1140     return hr;
1141 }
1142
1143 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1144     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
1145     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1146     HRESULT hr;
1147
1148     TRACE("(%p) %p Relay\n", This, pSB);
1149
1150     EnterCriticalSection(&d3d8_cs);
1151     hr = IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
1152     LeaveCriticalSection(&d3d8_cs);
1153     return hr;
1154 }
1155
1156 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1157     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
1158     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1159
1160     TRACE("(%p) Relay\n", This);
1161
1162     EnterCriticalSection(&d3d8_cs);
1163     while(IUnknown_Release((IUnknown *)pSB));
1164     LeaveCriticalSection(&d3d8_cs);
1165
1166     return D3D_OK;
1167 }
1168
1169 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(LPDIRECT3DDEVICE8 iface, D3DSTATEBLOCKTYPE Type, DWORD* pToken) {
1170    IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1171    IDirect3DStateBlock8Impl *object;
1172    HRESULT hrc = D3D_OK;
1173
1174    TRACE("(%p) Relay\n", This);
1175
1176    if(Type != D3DSBT_ALL         && Type != D3DSBT_PIXELSTATE &&
1177       Type != D3DSBT_VERTEXSTATE                              ) {
1178        WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1179        return D3DERR_INVALIDCALL;
1180    }
1181
1182    object  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
1183    if (NULL == object) {
1184       *pToken = 0;
1185       return E_OUTOFMEMORY;
1186    }
1187    object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1188    object->ref = 1;
1189
1190    EnterCriticalSection(&d3d8_cs);
1191    hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown *)object);
1192    LeaveCriticalSection(&d3d8_cs);
1193    if(D3D_OK != hrc){
1194        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
1195        HeapFree(GetProcessHeap(), 0, object);
1196        *pToken = 0;
1197    } else {
1198        *pToken = (DWORD)object;
1199        TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
1200    }
1201
1202    return hrc;
1203 }
1204
1205 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
1206     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1207     HRESULT hr;
1208     TRACE("(%p) Relay\n" , This);
1209 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1210     EnterCriticalSection(&d3d8_cs);
1211     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1212     LeaveCriticalSection(&d3d8_cs);
1213     return hr;
1214 }
1215
1216 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
1217     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1218     HRESULT hr;
1219     TRACE("(%p) Relay\n" , This);
1220
1221     EnterCriticalSection(&d3d8_cs);
1222     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1223     LeaveCriticalSection(&d3d8_cs);
1224     return hr;
1225 }
1226
1227 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
1228     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1229     IWineD3DBaseTexture *retTexture = NULL;
1230     HRESULT rc = D3D_OK;
1231
1232     TRACE("(%p) Relay\n" , This);
1233
1234     if(ppTexture == NULL){
1235         return D3DERR_INVALIDCALL;
1236     }
1237
1238     EnterCriticalSection(&d3d8_cs);
1239     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1240     if (rc == D3D_OK && NULL != retTexture) {
1241         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1242         IWineD3DBaseTexture_Release(retTexture);
1243     } else {
1244         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1245         *ppTexture = NULL;
1246     }
1247     LeaveCriticalSection(&d3d8_cs);
1248
1249     return rc;
1250 }
1251
1252 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1253     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1254     HRESULT hr;
1255     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1256
1257     EnterCriticalSection(&d3d8_cs);
1258     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1259                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1260     LeaveCriticalSection(&d3d8_cs);
1261     return hr;
1262 }
1263
1264 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1265     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1266     HRESULT hr;
1267     TRACE("(%p) Relay\n" , This);
1268
1269     switch(Type) {
1270     case D3DTSS_ADDRESSU:
1271         Type = WINED3DSAMP_ADDRESSU;
1272         break;
1273     case D3DTSS_ADDRESSV:
1274         Type = WINED3DSAMP_ADDRESSV;
1275         break;
1276     case D3DTSS_ADDRESSW:
1277         Type = WINED3DSAMP_ADDRESSW;
1278         break;
1279     case D3DTSS_BORDERCOLOR:
1280         Type = WINED3DSAMP_BORDERCOLOR;
1281         break;
1282     case D3DTSS_MAGFILTER:
1283         Type = WINED3DSAMP_MAGFILTER;
1284         break;
1285     case D3DTSS_MAXANISOTROPY:
1286         Type = WINED3DSAMP_MAXANISOTROPY;
1287         break;
1288     case D3DTSS_MAXMIPLEVEL:
1289         Type = WINED3DSAMP_MAXMIPLEVEL;
1290         break;
1291     case D3DTSS_MINFILTER:
1292         Type = WINED3DSAMP_MINFILTER;
1293         break;
1294     case D3DTSS_MIPFILTER:
1295         Type = WINED3DSAMP_MIPFILTER;
1296         break;
1297     case D3DTSS_MIPMAPLODBIAS:
1298         Type = WINED3DSAMP_MIPMAPLODBIAS;
1299         break;
1300     default:
1301         EnterCriticalSection(&d3d8_cs);
1302         hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
1303         LeaveCriticalSection(&d3d8_cs);
1304         return hr;
1305     }
1306
1307     EnterCriticalSection(&d3d8_cs);
1308     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, Type, pValue);
1309     LeaveCriticalSection(&d3d8_cs);
1310     return hr;
1311 }
1312
1313 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1314     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1315     HRESULT hr;
1316     TRACE("(%p) Relay\n" , This);
1317
1318     switch(Type) {
1319     case D3DTSS_ADDRESSU:
1320         Type = WINED3DSAMP_ADDRESSU;
1321         break;
1322     case D3DTSS_ADDRESSV:
1323         Type = WINED3DSAMP_ADDRESSV;
1324         break;
1325     case D3DTSS_ADDRESSW:
1326         Type = WINED3DSAMP_ADDRESSW;
1327         break;
1328     case D3DTSS_BORDERCOLOR:
1329         Type = WINED3DSAMP_BORDERCOLOR;
1330         break;
1331     case D3DTSS_MAGFILTER:
1332         Type = WINED3DSAMP_MAGFILTER;
1333         break;
1334     case D3DTSS_MAXANISOTROPY:
1335         Type = WINED3DSAMP_MAXANISOTROPY;
1336         break;
1337     case D3DTSS_MAXMIPLEVEL:
1338         Type = WINED3DSAMP_MAXMIPLEVEL;
1339         break;
1340     case D3DTSS_MINFILTER:
1341         Type = WINED3DSAMP_MINFILTER;
1342         break;
1343     case D3DTSS_MIPFILTER:
1344         Type = WINED3DSAMP_MIPFILTER;
1345         break;
1346     case D3DTSS_MIPMAPLODBIAS:
1347         Type = WINED3DSAMP_MIPMAPLODBIAS;
1348         break;
1349     default:
1350         EnterCriticalSection(&d3d8_cs);
1351         hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
1352         LeaveCriticalSection(&d3d8_cs);
1353         return hr;
1354     }
1355
1356     EnterCriticalSection(&d3d8_cs);
1357     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, Type, Value);
1358     LeaveCriticalSection(&d3d8_cs);
1359     return hr;
1360 }
1361
1362 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1363     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1364     HRESULT hr;
1365     TRACE("(%p) Relay\n" , This);
1366
1367     EnterCriticalSection(&d3d8_cs);
1368     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1369     LeaveCriticalSection(&d3d8_cs);
1370     return hr;
1371 }
1372
1373 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1374     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1375     FIXME("(%p) : stub\n", This);
1376     return D3D_OK;
1377 }
1378
1379 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1380     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1381     HRESULT hr;
1382     TRACE("(%p) Relay\n" , This);
1383
1384     EnterCriticalSection(&d3d8_cs);
1385     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1386     LeaveCriticalSection(&d3d8_cs);
1387     return hr;
1388 }
1389
1390 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1391     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1392     HRESULT hr;
1393     TRACE("(%p) Relay\n" , This);
1394
1395     EnterCriticalSection(&d3d8_cs);
1396     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1397     LeaveCriticalSection(&d3d8_cs);
1398     return hr;
1399 }
1400
1401 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1402     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1403     HRESULT hr;
1404     TRACE("(%p) Relay\n" , This);
1405
1406     EnterCriticalSection(&d3d8_cs);
1407     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1408     LeaveCriticalSection(&d3d8_cs);
1409     return hr;
1410 }
1411
1412 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1413     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1414     HRESULT hr;
1415     TRACE("(%p) Relay\n" , This);
1416
1417     EnterCriticalSection(&d3d8_cs);
1418     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1419     LeaveCriticalSection(&d3d8_cs);
1420     return hr;
1421 }
1422
1423 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1424     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface; 
1425     HRESULT hr;
1426     TRACE("(%p) Relay\n" , This);
1427
1428     EnterCriticalSection(&d3d8_cs);
1429     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1430     LeaveCriticalSection(&d3d8_cs);
1431     return hr;
1432 }
1433
1434 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1435                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1436     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1437     HRESULT hr;
1438     TRACE("(%p) Relay\n" , This);
1439
1440     EnterCriticalSection(&d3d8_cs);
1441     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1442     LeaveCriticalSection(&d3d8_cs);
1443     return hr;
1444 }
1445
1446 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1447     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1448     HRESULT hr;
1449     TRACE("(%p) Relay\n" , This);
1450
1451     EnterCriticalSection(&d3d8_cs);
1452     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1453     LeaveCriticalSection(&d3d8_cs);
1454     return hr;
1455 }
1456
1457 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1458                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1459                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1460                                                              UINT VertexStreamZeroStride) {
1461     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1462     HRESULT hr;
1463     TRACE("(%p) Relay\n" , This);
1464
1465     EnterCriticalSection(&d3d8_cs);
1466     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1467                                                pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1468     LeaveCriticalSection(&d3d8_cs);
1469     return hr;
1470 }
1471
1472 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1473     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1474     HRESULT hr;
1475     TRACE("(%p) Relay\n" , This);
1476
1477     EnterCriticalSection(&d3d8_cs);
1478     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer8Impl *)pDestBuffer)->wineD3DVertexBuffer, NULL, Flags);
1479     LeaveCriticalSection(&d3d8_cs);
1480     return hr;
1481 }
1482
1483 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexDeclaration(IDirect3DDevice8 *iface, CONST DWORD *declaration, IDirect3DVertexDeclaration8 **decl_ptr) {
1484     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1485     IDirect3DVertexDeclaration8Impl *object;
1486     WINED3DVERTEXELEMENT *wined3d_elements;
1487     UINT wined3d_element_count;
1488     HRESULT hr = D3D_OK;
1489
1490     TRACE("(%p) : declaration %p\n", This, declaration);
1491
1492     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1493     if (!object) {
1494         ERR("Memory allocation failed\n");
1495         *decl_ptr = NULL;
1496         return D3DERR_OUTOFVIDEOMEMORY;
1497     }
1498
1499     object->ref_count = 1;
1500     object->lpVtbl = &Direct3DVertexDeclaration8_Vtbl;
1501
1502     wined3d_element_count = convert_to_wined3d_declaration(declaration, &object->elements_size, &wined3d_elements);
1503     object->elements = HeapAlloc(GetProcessHeap(), 0, object->elements_size);
1504     if (!object->elements) {
1505         ERR("Memory allocation failed\n");
1506         HeapFree(GetProcessHeap(), 0, wined3d_elements);
1507         HeapFree(GetProcessHeap(), 0, object);
1508         *decl_ptr = NULL;
1509         return D3DERR_OUTOFVIDEOMEMORY;
1510     }
1511
1512     CopyMemory(object->elements, declaration, object->elements_size);
1513
1514     EnterCriticalSection(&d3d8_cs);
1515     hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wined3d_vertex_declaration,
1516             (IUnknown *)object, wined3d_elements, wined3d_element_count);
1517     LeaveCriticalSection(&d3d8_cs);
1518     HeapFree(GetProcessHeap(), 0, wined3d_elements);
1519
1520     if (FAILED(hr)) {
1521         ERR("(%p) : IWineD3DDevice_CreateVertexDeclaration call failed\n", This);
1522         HeapFree(GetProcessHeap(), 0, object->elements);
1523         HeapFree(GetProcessHeap(), 0, object);
1524     } else {
1525         *decl_ptr = (IDirect3DVertexDeclaration8 *)object;
1526         TRACE("(%p) : Created vertex declaration %p\n", This, object);
1527     }
1528
1529     return hr;
1530 }
1531
1532 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pDeclaration, CONST DWORD* pFunction, DWORD* ppShader, DWORD Usage) {
1533     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1534     HRESULT hrc = D3D_OK;
1535     IDirect3DVertexShader8Impl *object;
1536     IWineD3DVertexDeclaration *wined3d_vertex_declaration;
1537     const DWORD *token = pDeclaration;
1538
1539     /* Test if the vertex declaration is valid */
1540     while (D3DVSD_END() != *token) {
1541         D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
1542
1543         if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000)) {
1544             DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
1545             DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
1546
1547             if(reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !pFunction) {
1548                 WARN("Attempt to use a non-FLOAT3 normal with the fixed function function\n");
1549                 return D3DERR_INVALIDCALL;
1550             }
1551         }
1552         token += parse_token(token);
1553     }
1554
1555     /* Setup a stub object for now */
1556     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1557     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1558     if (NULL == object) {
1559         FIXME("Allocation of memory failed\n");
1560         *ppShader = 0;
1561         return D3DERR_OUTOFVIDEOMEMORY;
1562     }
1563
1564     object->ref = 1;
1565     object->lpVtbl = &Direct3DVertexShader8_Vtbl;
1566
1567     EnterCriticalSection(&d3d8_cs);
1568     hrc = IDirect3DDevice8Impl_CreateVertexDeclaration(iface, pDeclaration, &object->vertex_declaration);
1569     if (FAILED(hrc)) {
1570         ERR("(%p) : IDirect3DDeviceImpl_CreateVertexDeclaration call failed\n", This);
1571         LeaveCriticalSection(&d3d8_cs);
1572         HeapFree(GetProcessHeap(), 0, object);
1573         *ppShader = 0;
1574         return D3DERR_INVALIDCALL;
1575     }
1576     wined3d_vertex_declaration = ((IDirect3DVertexDeclaration8Impl *)object->vertex_declaration)->wined3d_vertex_declaration;
1577
1578     /* Usage is missing ... Use SetRenderState to set the sw vp render state in SetVertexShader */
1579     hrc = IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, wined3d_vertex_declaration, pFunction, &object->wineD3DVertexShader, (IUnknown *)object);
1580
1581     if (FAILED(hrc)) {
1582         /* free up object */
1583         FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
1584         HeapFree(GetProcessHeap(), 0, object);
1585         *ppShader = 0;
1586     } else {
1587         /* TODO: Store the VS declarations locally so that they can be dereferenced with a value higher than VS_HIGHESTFIXEDFXF */
1588         shader_handle *handle = alloc_shader_handle(This);
1589         if (!handle) {
1590             ERR("Failed to allocate shader handle\n");
1591             IDirect3DVertexShader8_Release((IUnknown *)object);
1592             hrc = E_OUTOFMEMORY;
1593         } else {
1594             *handle = object;
1595             object->handle = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1596             *ppShader = object->handle;
1597
1598             load_local_constants(pDeclaration, object->wineD3DVertexShader);
1599             TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
1600         }
1601     }
1602     LeaveCriticalSection(&d3d8_cs);
1603
1604     return hrc;
1605 }
1606
1607 IWineD3DVertexDeclaration *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1608 {
1609     HRESULT hr;
1610     IWineD3DVertexDeclaration* pDecl = NULL;
1611     int p, low, high; /* deliberately signed */
1612     struct FvfToDecl *convertedDecls = This->decls;
1613
1614     TRACE("Searching for declaration for fvf %08x... ", fvf);
1615
1616     low = 0;
1617     high = This->numConvertedDecls - 1;
1618     while(low <= high) {
1619         p = (low + high) >> 1;
1620         TRACE("%d ", p);
1621         if(convertedDecls[p].fvf == fvf) {
1622             TRACE("found %p\n", convertedDecls[p].decl);
1623             return convertedDecls[p].decl;
1624         } else if(convertedDecls[p].fvf < fvf) {
1625             low = p + 1;
1626         } else {
1627             high = p - 1;
1628         }
1629     }
1630     TRACE("not found. Creating and inserting at position %d.\n", low);
1631
1632     hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->WineD3DDevice,
1633                                                        &pDecl,
1634                                                        (IUnknown *) This,
1635                                                        fvf);
1636     if (FAILED(hr)) return NULL;
1637
1638     if(This->declArraySize == This->numConvertedDecls) {
1639         int grow = This->declArraySize / 2;
1640         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1641                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1642         if(!convertedDecls) {
1643             /* This will destroy it */
1644             IWineD3DVertexDeclaration_Release(pDecl);
1645             return NULL;
1646         }
1647         This->decls = convertedDecls;
1648         This->declArraySize += grow;
1649     }
1650
1651     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
1652     convertedDecls[low].decl = pDecl;
1653     convertedDecls[low].fvf = fvf;
1654     This->numConvertedDecls++;
1655
1656     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
1657     return pDecl;
1658 }
1659
1660 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1661     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1662     HRESULT hrc = D3D_OK;
1663
1664     TRACE("(%p) : Relay\n", This);
1665     EnterCriticalSection(&d3d8_cs);
1666     if (VS_HIGHESTFIXEDFXF >= pShader) {
1667         TRACE("Setting FVF, %d %d\n", VS_HIGHESTFIXEDFXF, pShader);
1668         IWineD3DDevice_SetFVF(This->WineD3DDevice, pShader);
1669         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, IDirect3DDevice8Impl_FindDecl(This, pShader));
1670         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1671     } else {
1672         TRACE("Setting shader\n");
1673         if (This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1674             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1675             hrc = D3DERR_INVALIDCALL;
1676         } else {
1677             IDirect3DVertexShader8Impl *shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1678             IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1679                     shader ? ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration : NULL);
1680             hrc = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, 0 == shader ? NULL : shader->wineD3DVertexShader);
1681         }
1682     }
1683     TRACE("(%p) : returning hr(%u)\n", This, hrc);
1684     LeaveCriticalSection(&d3d8_cs);
1685
1686     return hrc;
1687 }
1688
1689 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1690     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1691     IWineD3DVertexShader *pShader;
1692     HRESULT hrc = D3D_OK;
1693
1694     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1695     EnterCriticalSection(&d3d8_cs);
1696     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
1697     if (D3D_OK == hrc) {
1698         if(0 != pShader) {
1699             IDirect3DVertexShader8Impl *d3d8_shader;
1700             hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)&d3d8_shader);
1701             IWineD3DVertexShader_Release(pShader);
1702             *ppShader = d3d8_shader->handle;
1703         } else {
1704             *ppShader = 0;
1705             hrc = D3D_OK;
1706         }
1707     } else {
1708         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %u (device %p)\n", This, hrc, This->WineD3DDevice);
1709     }
1710     TRACE("(%p) : returning %#x\n", This, *ppShader);
1711     LeaveCriticalSection(&d3d8_cs);
1712
1713     return hrc;
1714 }
1715
1716 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1717     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1718
1719     TRACE("(%p) : pShader %#x\n", This, pShader);
1720
1721     EnterCriticalSection(&d3d8_cs);
1722     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1723         ERR("(%p) : Trying to delete an invalid handle\n", This);
1724         LeaveCriticalSection(&d3d8_cs);
1725         return D3DERR_INVALIDCALL;
1726     } else {
1727         IWineD3DVertexShader *cur = NULL;
1728         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1729         IDirect3DVertexShader8Impl *shader = *handle;
1730
1731         IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
1732         if(cur) {
1733             if(cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
1734             IWineD3DVertexShader_Release(cur);
1735         }
1736
1737         while(IUnknown_Release((IUnknown *)shader));
1738         free_shader_handle(This, handle);
1739     }
1740     LeaveCriticalSection(&d3d8_cs);
1741
1742     return D3D_OK;
1743 }
1744
1745 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1746     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1747     HRESULT hr;
1748     TRACE("(%p) : Relay\n", This);
1749
1750     EnterCriticalSection(&d3d8_cs);
1751     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1752     LeaveCriticalSection(&d3d8_cs);
1753     return hr;
1754 }
1755
1756 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1757     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1758     HRESULT hr;
1759     TRACE("(%p) : Relay\n", This);
1760
1761     EnterCriticalSection(&d3d8_cs);
1762     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1763     LeaveCriticalSection(&d3d8_cs);
1764     return hr;
1765 }
1766
1767 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1768     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1769     IDirect3DVertexDeclaration8Impl *declaration;
1770     IDirect3DVertexShader8Impl *shader = NULL;
1771
1772     TRACE("(%p) : pVertexShader 0x%08x, pData %p, *pSizeOfData %u\n", This, pVertexShader, pData, *pSizeOfData);
1773
1774     EnterCriticalSection(&d3d8_cs);
1775     if (pVertexShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pVertexShader - (VS_HIGHESTFIXEDFXF + 1)) {
1776         ERR("Passed an invalid shader handle.\n");
1777         LeaveCriticalSection(&d3d8_cs);
1778         return D3DERR_INVALIDCALL;
1779     }
1780
1781     shader = This->shader_handles[pVertexShader - (VS_HIGHESTFIXEDFXF + 1)];
1782     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
1783
1784     /* If pData is NULL, we just return the required size of the buffer. */
1785     if (!pData) {
1786         *pSizeOfData = declaration->elements_size;
1787         LeaveCriticalSection(&d3d8_cs);
1788         return D3D_OK;
1789     }
1790
1791     /* MSDN claims that if *pSizeOfData is smaller than the required size
1792      * we should write the required size and return D3DERR_MOREDATA.
1793      * That's not actually true. */
1794     if (*pSizeOfData < declaration->elements_size) {
1795         LeaveCriticalSection(&d3d8_cs);
1796         return D3DERR_INVALIDCALL;
1797     }
1798
1799     CopyMemory(pData, declaration->elements, declaration->elements_size);
1800     LeaveCriticalSection(&d3d8_cs);
1801
1802     return D3D_OK;
1803 }
1804
1805 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1806     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1807     IDirect3DVertexShader8Impl *shader = NULL;
1808     HRESULT hr;
1809
1810     TRACE("(%p) : pVertexShader %#x, pData %p, pSizeOfData %p\n", This, pVertexShader, pData, pSizeOfData);
1811
1812     EnterCriticalSection(&d3d8_cs);
1813     if (pVertexShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pVertexShader - (VS_HIGHESTFIXEDFXF + 1)) {
1814         ERR("Passed an invalid shader handle.\n");
1815         LeaveCriticalSection(&d3d8_cs);
1816         return D3DERR_INVALIDCALL;
1817     }
1818
1819     shader = This->shader_handles[pVertexShader - (VS_HIGHESTFIXEDFXF + 1)];
1820     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, pSizeOfData);
1821     LeaveCriticalSection(&d3d8_cs);
1822     return hr;
1823 }
1824
1825 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
1826     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1827     HRESULT hr;
1828     TRACE("(%p) Relay\n", This);
1829
1830     EnterCriticalSection(&d3d8_cs);
1831     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
1832      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
1833      * vertex buffers can't be created to address them with an index that requires the 32nd bit
1834      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
1835      * problem)
1836      */
1837     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
1838     hr = IWineD3DDevice_SetIndices(This->WineD3DDevice,
1839             pIndexData ? ((IDirect3DIndexBuffer8Impl *)pIndexData)->wineD3DIndexBuffer : NULL);
1840     LeaveCriticalSection(&d3d8_cs);
1841     return hr;
1842 }
1843
1844 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
1845     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1846     IWineD3DIndexBuffer *retIndexData = NULL;
1847     HRESULT rc = D3D_OK;
1848
1849     TRACE("(%p) Relay\n", This);
1850
1851     if(ppIndexData == NULL){
1852         return D3DERR_INVALIDCALL;
1853     }
1854
1855     EnterCriticalSection(&d3d8_cs);
1856     /* The case from UINT to INT is safe because d3d8 will never set negative values */
1857     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
1858     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData);
1859     if (SUCCEEDED(rc) && retIndexData) {
1860         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1861         IWineD3DIndexBuffer_Release(retIndexData);
1862     } else {
1863         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
1864         *ppIndexData = NULL;
1865     }
1866     LeaveCriticalSection(&d3d8_cs);
1867
1868     return rc;
1869 }
1870 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
1871     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1872     IDirect3DPixelShader8Impl *object;
1873     HRESULT hrc = D3D_OK;
1874
1875     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1876
1877     if (NULL == ppShader) {
1878         TRACE("(%p) Invalid call\n", This);
1879         return D3DERR_INVALIDCALL;
1880     }
1881     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1882
1883     if (NULL == object) {
1884         return E_OUTOFMEMORY;
1885     } else {
1886         EnterCriticalSection(&d3d8_cs);
1887
1888         object->ref    = 1;
1889         object->lpVtbl = &Direct3DPixelShader8_Vtbl;
1890         hrc = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction, &object->wineD3DPixelShader , (IUnknown *)object);
1891         if (D3D_OK != hrc) {
1892             FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
1893             HeapFree(GetProcessHeap(), 0 , object);
1894             *ppShader = 0;
1895         } else {
1896             shader_handle *handle = alloc_shader_handle(This);
1897             if (!handle) {
1898                 ERR("Failed to allocate shader handle\n");
1899                 IDirect3DVertexShader8_Release((IUnknown *)object);
1900                 hrc = E_OUTOFMEMORY;
1901             } else {
1902                 *handle = object;
1903                 object->handle = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1904                 *ppShader = object->handle;
1905                 TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
1906             }
1907         }
1908         LeaveCriticalSection(&d3d8_cs);
1909     }
1910
1911     return hrc;
1912 }
1913
1914 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1915     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1916     IDirect3DPixelShader8Impl *shader = NULL;
1917     HRESULT hr;
1918
1919     TRACE("(%p) : pShader %#x\n", This, pShader);
1920
1921     EnterCriticalSection(&d3d8_cs);
1922     if (pShader > VS_HIGHESTFIXEDFXF && This->allocated_shader_handles > pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1923         shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1924     } else if (pShader) {
1925         ERR("Trying to set an invalid handle.\n");
1926     }
1927
1928     TRACE("(%p) : Setting shader %p\n", This, shader);
1929     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader == NULL ? NULL :shader->wineD3DPixelShader);
1930     LeaveCriticalSection(&d3d8_cs);
1931     return hr;
1932 }
1933
1934 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1935     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1936     IWineD3DPixelShader *object;
1937
1938     HRESULT hrc = D3D_OK;
1939     TRACE("(%p) Relay\n", This);
1940     if (NULL == ppShader) {
1941         TRACE("(%p) Invalid call\n", This);
1942         return D3DERR_INVALIDCALL;
1943     }
1944
1945     EnterCriticalSection(&d3d8_cs);
1946     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
1947     if (D3D_OK == hrc && NULL != object) {
1948         IDirect3DPixelShader8Impl *d3d8_shader;
1949         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
1950         IWineD3DPixelShader_Release(object);
1951         *ppShader = d3d8_shader->handle;
1952     } else {
1953         *ppShader = (DWORD)NULL;
1954     }
1955
1956     TRACE("(%p) : returning %#x\n", This, *ppShader);
1957     LeaveCriticalSection(&d3d8_cs);
1958     return hrc;
1959 }
1960
1961 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1962     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1963
1964     TRACE("(%p) : pShader %#x\n", This, pShader);
1965
1966     EnterCriticalSection(&d3d8_cs);
1967     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1968         ERR("(%p) : Trying to delete an invalid handle\n", This);
1969         LeaveCriticalSection(&d3d8_cs);
1970         return D3DERR_INVALIDCALL;
1971     } else {
1972         IWineD3DPixelShader *cur = NULL;
1973         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1974         IDirect3DPixelShader8Impl *shader = *handle;
1975
1976         IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
1977         if(cur) {
1978             if(cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
1979             IWineD3DPixelShader_Release(cur);
1980         }
1981
1982         while(IUnknown_Release((IUnknown *)shader));
1983         free_shader_handle(This, handle);
1984     }
1985     LeaveCriticalSection(&d3d8_cs);
1986
1987     return D3D_OK;
1988 }
1989
1990 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1991     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1992     HRESULT hr;
1993     TRACE("(%p) Relay\n", This);
1994
1995     EnterCriticalSection(&d3d8_cs);
1996     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1997     LeaveCriticalSection(&d3d8_cs);
1998     return hr;
1999 }
2000
2001 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
2002     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2003     HRESULT hr;
2004     TRACE("(%p) Relay\n", This);
2005
2006     EnterCriticalSection(&d3d8_cs);
2007     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
2008     LeaveCriticalSection(&d3d8_cs);
2009     return hr;
2010 }
2011
2012 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
2013     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2014     IDirect3DPixelShader8Impl *shader = NULL;
2015     HRESULT hr;
2016
2017     TRACE("(%p) : pPixelShader %#x, pData %p, pSizeOfData %p\n", This, pPixelShader, pData, pSizeOfData);
2018
2019     EnterCriticalSection(&d3d8_cs);
2020     if (pPixelShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pPixelShader - (VS_HIGHESTFIXEDFXF + 1)) {
2021         ERR("Passed an invalid shader handle.\n");
2022         LeaveCriticalSection(&d3d8_cs);
2023         return D3DERR_INVALIDCALL;
2024     }
2025
2026     shader = This->shader_handles[pPixelShader - (VS_HIGHESTFIXEDFXF + 1)];
2027     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, pSizeOfData);
2028     LeaveCriticalSection(&d3d8_cs);
2029     return hr;
2030 }
2031
2032 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2033     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2034     HRESULT hr;
2035     TRACE("(%p) Relay\n", This);
2036
2037     EnterCriticalSection(&d3d8_cs);
2038     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2039     LeaveCriticalSection(&d3d8_cs);
2040     return hr;
2041 }
2042
2043 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2044     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2045     HRESULT hr;
2046     TRACE("(%p) Relay\n", This);
2047
2048     EnterCriticalSection(&d3d8_cs);
2049     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2050     LeaveCriticalSection(&d3d8_cs);
2051     return hr;
2052 }
2053
2054 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
2055     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2056     HRESULT hr;
2057     TRACE("(%p) Relay\n", This);
2058
2059     EnterCriticalSection(&d3d8_cs);
2060     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2061     LeaveCriticalSection(&d3d8_cs);
2062     return hr;
2063 }
2064
2065 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
2066     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2067     HRESULT hr;
2068     TRACE("(%p) Relay\n" , This);
2069
2070     EnterCriticalSection(&d3d8_cs);
2071     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2072                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2073                                         0/* Offset in bytes */, Stride);
2074     LeaveCriticalSection(&d3d8_cs);
2075     return hr;
2076 }
2077
2078 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
2079     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2080     IWineD3DVertexBuffer *retStream = NULL;
2081     HRESULT rc = D3D_OK;
2082
2083     TRACE("(%p) Relay\n" , This);
2084
2085     if(pStream == NULL){
2086         return D3DERR_INVALIDCALL;
2087     }
2088
2089     EnterCriticalSection(&d3d8_cs);
2090     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, 0 /* Offset in bytes */, pStride);
2091     if (rc == D3D_OK  && NULL != retStream) {
2092         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
2093         IWineD3DVertexBuffer_Release(retStream);
2094     }else{
2095         if (rc != D3D_OK){
2096             FIXME("Call to GetStreamSource failed %p\n",  pStride);
2097         }
2098         *pStream = NULL;
2099     }
2100     LeaveCriticalSection(&d3d8_cs);
2101
2102     return rc;
2103 }
2104
2105
2106 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2107 {
2108     IDirect3DDevice8Impl_QueryInterface,
2109     IDirect3DDevice8Impl_AddRef,
2110     IDirect3DDevice8Impl_Release,
2111     IDirect3DDevice8Impl_TestCooperativeLevel,
2112     IDirect3DDevice8Impl_GetAvailableTextureMem,
2113     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2114     IDirect3DDevice8Impl_GetDirect3D,
2115     IDirect3DDevice8Impl_GetDeviceCaps,
2116     IDirect3DDevice8Impl_GetDisplayMode,
2117     IDirect3DDevice8Impl_GetCreationParameters,
2118     IDirect3DDevice8Impl_SetCursorProperties,
2119     IDirect3DDevice8Impl_SetCursorPosition,
2120     IDirect3DDevice8Impl_ShowCursor,
2121     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2122     IDirect3DDevice8Impl_Reset,
2123     IDirect3DDevice8Impl_Present,
2124     IDirect3DDevice8Impl_GetBackBuffer,
2125     IDirect3DDevice8Impl_GetRasterStatus,
2126     IDirect3DDevice8Impl_SetGammaRamp,
2127     IDirect3DDevice8Impl_GetGammaRamp,
2128     IDirect3DDevice8Impl_CreateTexture,
2129     IDirect3DDevice8Impl_CreateVolumeTexture,
2130     IDirect3DDevice8Impl_CreateCubeTexture,
2131     IDirect3DDevice8Impl_CreateVertexBuffer,
2132     IDirect3DDevice8Impl_CreateIndexBuffer,
2133     IDirect3DDevice8Impl_CreateRenderTarget,
2134     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2135     IDirect3DDevice8Impl_CreateImageSurface,
2136     IDirect3DDevice8Impl_CopyRects,
2137     IDirect3DDevice8Impl_UpdateTexture,
2138     IDirect3DDevice8Impl_GetFrontBuffer,
2139     IDirect3DDevice8Impl_SetRenderTarget,
2140     IDirect3DDevice8Impl_GetRenderTarget,
2141     IDirect3DDevice8Impl_GetDepthStencilSurface,
2142     IDirect3DDevice8Impl_BeginScene,
2143     IDirect3DDevice8Impl_EndScene,
2144     IDirect3DDevice8Impl_Clear,
2145     IDirect3DDevice8Impl_SetTransform,
2146     IDirect3DDevice8Impl_GetTransform,
2147     IDirect3DDevice8Impl_MultiplyTransform,
2148     IDirect3DDevice8Impl_SetViewport,
2149     IDirect3DDevice8Impl_GetViewport,
2150     IDirect3DDevice8Impl_SetMaterial,
2151     IDirect3DDevice8Impl_GetMaterial,
2152     IDirect3DDevice8Impl_SetLight,
2153     IDirect3DDevice8Impl_GetLight,
2154     IDirect3DDevice8Impl_LightEnable,
2155     IDirect3DDevice8Impl_GetLightEnable,
2156     IDirect3DDevice8Impl_SetClipPlane,
2157     IDirect3DDevice8Impl_GetClipPlane,
2158     IDirect3DDevice8Impl_SetRenderState,
2159     IDirect3DDevice8Impl_GetRenderState,
2160     IDirect3DDevice8Impl_BeginStateBlock,
2161     IDirect3DDevice8Impl_EndStateBlock,
2162     IDirect3DDevice8Impl_ApplyStateBlock,
2163     IDirect3DDevice8Impl_CaptureStateBlock,
2164     IDirect3DDevice8Impl_DeleteStateBlock,
2165     IDirect3DDevice8Impl_CreateStateBlock,
2166     IDirect3DDevice8Impl_SetClipStatus,
2167     IDirect3DDevice8Impl_GetClipStatus,
2168     IDirect3DDevice8Impl_GetTexture,
2169     IDirect3DDevice8Impl_SetTexture,
2170     IDirect3DDevice8Impl_GetTextureStageState,
2171     IDirect3DDevice8Impl_SetTextureStageState,
2172     IDirect3DDevice8Impl_ValidateDevice,
2173     IDirect3DDevice8Impl_GetInfo,
2174     IDirect3DDevice8Impl_SetPaletteEntries,
2175     IDirect3DDevice8Impl_GetPaletteEntries,
2176     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2177     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2178     IDirect3DDevice8Impl_DrawPrimitive,
2179     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2180     IDirect3DDevice8Impl_DrawPrimitiveUP,
2181     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2182     IDirect3DDevice8Impl_ProcessVertices,
2183     IDirect3DDevice8Impl_CreateVertexShader,
2184     IDirect3DDevice8Impl_SetVertexShader,
2185     IDirect3DDevice8Impl_GetVertexShader,
2186     IDirect3DDevice8Impl_DeleteVertexShader,
2187     IDirect3DDevice8Impl_SetVertexShaderConstant,
2188     IDirect3DDevice8Impl_GetVertexShaderConstant,
2189     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2190     IDirect3DDevice8Impl_GetVertexShaderFunction,
2191     IDirect3DDevice8Impl_SetStreamSource,
2192     IDirect3DDevice8Impl_GetStreamSource,
2193     IDirect3DDevice8Impl_SetIndices,
2194     IDirect3DDevice8Impl_GetIndices,
2195     IDirect3DDevice8Impl_CreatePixelShader,
2196     IDirect3DDevice8Impl_SetPixelShader,
2197     IDirect3DDevice8Impl_GetPixelShader,
2198     IDirect3DDevice8Impl_DeletePixelShader,
2199     IDirect3DDevice8Impl_SetPixelShaderConstant,
2200     IDirect3DDevice8Impl_GetPixelShaderConstant,
2201     IDirect3DDevice8Impl_GetPixelShaderFunction,
2202     IDirect3DDevice8Impl_DrawRectPatch,
2203     IDirect3DDevice8Impl_DrawTriPatch,
2204     IDirect3DDevice8Impl_DeletePatch
2205 };
2206
2207 /* Internal function called back during the CreateDevice to create a render target  */
2208 HRESULT WINAPI D3D8CB_CreateSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
2209                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
2210                                          WINED3DCUBEMAP_FACES Face, IWineD3DSurface **ppSurface,
2211                                          HANDLE *pSharedHandle) {
2212
2213     HRESULT res = D3D_OK;
2214     IDirect3DSurface8Impl *d3dSurface = NULL;
2215     BOOL Lockable = TRUE;
2216
2217     if((WINED3DPOOL_DEFAULT == Pool && WINED3DUSAGE_DYNAMIC != Usage))
2218         Lockable = FALSE;
2219
2220     TRACE("relay\n");
2221     res = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)device, Width, Height, (D3DFORMAT)Format, Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface8 **)&d3dSurface, D3DRTYPE_SURFACE, Usage, Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2222
2223     if (SUCCEEDED(res)) {
2224         *ppSurface = d3dSurface->wineD3DSurface;
2225         d3dSurface->container = pSuperior;
2226         IUnknown_Release(d3dSurface->parentDevice);
2227         d3dSurface->parentDevice = NULL;
2228         d3dSurface->forwardReference = pSuperior;
2229     } else {
2230         FIXME("(%p) IDirect3DDevice8_CreateSurface failed\n", device);
2231     }
2232     return res;
2233 }
2234
2235 ULONG WINAPI D3D8CB_DestroySurface(IWineD3DSurface *pSurface) {
2236     IDirect3DSurface8Impl* surfaceParent;
2237     TRACE("(%p) call back\n", pSurface);
2238
2239     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
2240     /* GetParent's AddRef was forwarded to an object in destruction.
2241      * Releasing it here again would cause an endless recursion. */
2242     surfaceParent->forwardReference = NULL;
2243     return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
2244 }