msi: Don't crash if we can't find a control.
[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 %ld\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 = InterlockedDecrement(&This->ref);
93
94     TRACE("(%p) : ReleaseRef to %ld\n", This, ref);
95
96     if (ref == 0) {
97         TRACE("Releasing wined3d device %p\n", This->WineD3DDevice);
98         IWineD3DDevice_Uninit3D(This->WineD3DDevice);
99         IWineD3DDevice_Release(This->WineD3DDevice);
100         HeapFree(GetProcessHeap(), 0, This);
101     }
102     return ref;
103 }
104
105 /* IDirect3DDevice Interface follow: */
106 static HRESULT WINAPI IDirect3DDevice8Impl_TestCooperativeLevel(LPDIRECT3DDEVICE8 iface) {
107     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
108
109     TRACE("(%p) : Relay\n", This);
110     return IWineD3DDevice_TestCooperativeLevel(This->WineD3DDevice);
111 }
112
113 static UINT WINAPI  IDirect3DDevice8Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE8 iface) {
114     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
115
116     TRACE("(%p) Relay\n", This);
117     return IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
118 }
119
120 static HRESULT WINAPI IDirect3DDevice8Impl_ResourceManagerDiscardBytes(LPDIRECT3DDEVICE8 iface, DWORD Bytes) {
121     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
122
123     TRACE("(%p) : Relay bytes(%ld)\n", This, Bytes);
124     return IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
125 }
126
127 static HRESULT WINAPI IDirect3DDevice8Impl_GetDirect3D(LPDIRECT3DDEVICE8 iface, IDirect3D8** ppD3D8) {
128     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
129     HRESULT hr = D3D_OK;
130     IWineD3D* pWineD3D;
131
132     TRACE("(%p) Relay\n", This);
133
134     if (NULL == ppD3D8) {
135         return D3DERR_INVALIDCALL;
136     }
137     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
138     if (hr == D3D_OK && pWineD3D != NULL)
139     {
140         IWineD3DResource_GetParent((IWineD3DResource *)pWineD3D,(IUnknown **)ppD3D8);
141         IWineD3DResource_Release((IWineD3DResource *)pWineD3D);
142     } else {
143         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
144         *ppD3D8 = NULL;
145     }
146     TRACE("(%p) returning %p\b",This , *ppD3D8);
147     return hr;
148 }
149
150 static HRESULT WINAPI IDirect3DDevice8Impl_GetDeviceCaps(LPDIRECT3DDEVICE8 iface, D3DCAPS8* pCaps) {
151     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
152     HRESULT hrc = D3D_OK;
153     WINED3DCAPS *pWineCaps;
154
155     TRACE("(%p) : Relay pCaps %p\n", This, pCaps);
156     if(NULL == pCaps){
157         return D3DERR_INVALIDCALL;
158     }
159     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
160     if(pWineCaps == NULL){
161         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
162     }
163
164     D3D8CAPSTOWINECAPS(pCaps, pWineCaps)
165     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
166     HeapFree(GetProcessHeap(), 0, pWineCaps);
167     TRACE("Returning %p %p\n", This, pCaps);
168     return hrc;
169 }
170
171 static HRESULT WINAPI IDirect3DDevice8Impl_GetDisplayMode(LPDIRECT3DDEVICE8 iface, D3DDISPLAYMODE* pMode) {
172     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
173     TRACE("(%p) Relay\n", This);
174     return IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, 0, (WINED3DDISPLAYMODE *) pMode);
175 }
176
177 static HRESULT WINAPI IDirect3DDevice8Impl_GetCreationParameters(LPDIRECT3DDEVICE8 iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
178     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
179     TRACE("(%p) Relay\n", This);
180     return IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
181 }
182
183 static HRESULT WINAPI IDirect3DDevice8Impl_SetCursorProperties(LPDIRECT3DDEVICE8 iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8* pCursorBitmap) {
184     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
185     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl*)pCursorBitmap;
186     TRACE("(%p) Relay\n", This);
187     return IWineD3DDevice_SetCursorProperties(This->WineD3DDevice,XHotSpot,YHotSpot,(IWineD3DSurface*)pSurface->wineD3DSurface);
188 }
189
190 static void WINAPI IDirect3DDevice8Impl_SetCursorPosition(LPDIRECT3DDEVICE8 iface, UINT XScreenSpace, UINT YScreenSpace, DWORD Flags) {
191     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
192     TRACE("(%p) Relay\n", This);
193     return IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
194 }
195
196 static BOOL WINAPI IDirect3DDevice8Impl_ShowCursor(LPDIRECT3DDEVICE8 iface, BOOL bShow) {
197     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
198     TRACE("(%p) Relay\n", This);
199
200     return IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
201 }
202
203 static HRESULT WINAPI IDirect3DDevice8Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain8** pSwapChain) {
204     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
205     IDirect3DSwapChain8Impl* object;
206     HRESULT hrc = D3D_OK;
207     WINED3DPRESENT_PARAMETERS localParameters;
208
209     TRACE("(%p) Relay\n", This);
210
211     /* Fix the back buffer count */
212     if(pPresentationParameters->BackBufferCount == 0) {
213         pPresentationParameters->BackBufferCount = 1;
214     }
215
216     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
217     if (NULL == object) {
218         FIXME("Allocation of memory failed\n");
219         *pSwapChain = NULL;
220         return D3DERR_OUTOFVIDEOMEMORY;
221     }
222     object->ref = 1;
223     object->lpVtbl = &Direct3DSwapChain8_Vtbl;
224
225     /* Allocate an associated WineD3DDevice object */
226     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
227     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
228     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
229     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
230     localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
231     localParameters.MultiSampleQuality             = NULL; /* d3d9 only */
232     localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *) &pPresentationParameters->SwapEffect;
233     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
234     localParameters.Windowed                       = &pPresentationParameters->Windowed;
235     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
236     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
237     localParameters.Flags                          = &pPresentationParameters->Flags;
238     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz;
239     localParameters.PresentationInterval           = &pPresentationParameters->FullScreen_PresentationInterval;
240
241
242     hrc = IWineD3DDevice_CreateAdditionalSwapChain(This->WineD3DDevice, &localParameters, &object->wineD3DSwapChain, (IUnknown*)object, D3D8CB_CreateRenderTarget, D3D8CB_CreateDepthStencilSurface);
243     if (hrc != D3D_OK) {
244         FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
245         HeapFree(GetProcessHeap(), 0 , object);
246         *pSwapChain = NULL;
247     }else{
248         IUnknown_AddRef(iface);
249         object->parentDevice = iface;
250         *pSwapChain = (IDirect3DSwapChain8 *)object;
251     }
252     TRACE("(%p) returning %p\n", This, *pSwapChain);
253     return hrc;
254 }
255
256 static HRESULT WINAPI IDirect3DDevice8Impl_Reset(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
257     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
258     WINED3DPRESENT_PARAMETERS localParameters;
259     TRACE("(%p) Relay pPresentationParameters(%p)\n", This, pPresentationParameters);
260 /* FINDME: FIXME: */
261     localParameters.BackBufferWidth                = &pPresentationParameters->BackBufferWidth;
262     localParameters.BackBufferHeight               = &pPresentationParameters->BackBufferHeight;
263     localParameters.BackBufferFormat               = (WINED3DFORMAT *)&pPresentationParameters->BackBufferFormat;
264     localParameters.BackBufferCount                = &pPresentationParameters->BackBufferCount;
265     localParameters.MultiSampleType                = (WINED3DMULTISAMPLE_TYPE *) &pPresentationParameters->MultiSampleType;
266     localParameters.MultiSampleQuality             = NULL; /* D3d9 only */
267     localParameters.SwapEffect                     = (WINED3DSWAPEFFECT *) &pPresentationParameters->SwapEffect;
268     localParameters.hDeviceWindow                  = &pPresentationParameters->hDeviceWindow;
269     localParameters.Windowed                       = &pPresentationParameters->Windowed;
270     localParameters.EnableAutoDepthStencil         = &pPresentationParameters->EnableAutoDepthStencil;
271     localParameters.AutoDepthStencilFormat         = (WINED3DFORMAT *)&pPresentationParameters->AutoDepthStencilFormat;
272     localParameters.Flags                          = &pPresentationParameters->Flags;
273     localParameters.FullScreen_RefreshRateInHz     = &pPresentationParameters->FullScreen_RefreshRateInHz;
274     localParameters.PresentationInterval           = &pPresentationParameters->FullScreen_PresentationInterval;
275     return IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
276 }
277
278 static HRESULT WINAPI IDirect3DDevice8Impl_Present(LPDIRECT3DDEVICE8 iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) {
279     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
280     TRACE("(%p) Relay\n", This);
281     return IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
282 }
283
284 static HRESULT WINAPI IDirect3DDevice8Impl_GetBackBuffer(LPDIRECT3DDEVICE8 iface, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8** ppBackBuffer) {
285     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
286     IWineD3DSurface *retSurface = NULL;
287     HRESULT rc = D3D_OK;
288
289     TRACE("(%p) Relay\n", This);
290
291     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, 0, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, (IWineD3DSurface **)&retSurface);
292     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
293         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
294         IWineD3DSurface_Release(retSurface);
295     }
296     return rc;
297 }
298
299 static HRESULT WINAPI IDirect3DDevice8Impl_GetRasterStatus(LPDIRECT3DDEVICE8 iface, D3DRASTER_STATUS* pRasterStatus) {
300     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
301     TRACE("(%p) Relay\n", This);
302
303     return IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, 0, (WINED3DRASTER_STATUS *) pRasterStatus);
304 }
305
306 static void WINAPI IDirect3DDevice8Impl_SetGammaRamp(LPDIRECT3DDEVICE8 iface, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
307     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
308     TRACE("(%p) Relay\n", This);
309
310     return IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, 0, Flags, (WINED3DGAMMARAMP *) pRamp);
311 }
312
313 static void WINAPI IDirect3DDevice8Impl_GetGammaRamp(LPDIRECT3DDEVICE8 iface, D3DGAMMARAMP* pRamp) {
314     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
315     TRACE("(%p) Relay\n", This);
316
317     return IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, 0, (WINED3DGAMMARAMP *) pRamp);
318 }
319
320 static HRESULT WINAPI IDirect3DDevice8Impl_CreateTexture(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
321                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture8 **ppTexture) {
322     IDirect3DTexture8Impl *object;
323     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
324     HRESULT hrc = D3D_OK;
325
326     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%ld), Fmt(%u), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
327
328     /* Allocate the storage for the device */
329     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture8Impl));
330
331     if (NULL == object) {
332         FIXME("Allocation of memory failed\n");
333 /*        *ppTexture = NULL; */
334         return D3DERR_OUTOFVIDEOMEMORY;
335     }
336
337     object->lpVtbl = &Direct3DTexture8_Vtbl;
338     object->ref = 1;
339     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage,
340                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DTexture, NULL, (IUnknown *)object, D3D8CB_CreateSurface);
341
342     if (FAILED(hrc)) {
343         /* free up object */ 
344         FIXME("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
345         HeapFree(GetProcessHeap(), 0, object);
346 /*      *ppTexture = NULL; */
347    } else {
348         IUnknown_AddRef(iface);
349         object->parentDevice = iface;
350         *ppTexture = (LPDIRECT3DTEXTURE8) object;
351    }
352
353    TRACE("(%p) Created Texture %p, %p\n",This,object,object->wineD3DTexture);
354    return hrc;
355 }
356
357 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVolumeTexture(LPDIRECT3DDEVICE8 iface, 
358                                                           UINT Width, UINT Height, UINT Depth, UINT Levels, DWORD Usage, 
359                                                           D3DFORMAT Format, D3DPOOL Pool, IDirect3DVolumeTexture8** ppVolumeTexture) {
360
361     IDirect3DVolumeTexture8Impl *object;
362     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
363     HRESULT hrc = D3D_OK;
364
365     TRACE("(%p) Relay\n", This);
366
367     /* Allocate the storage for the device */
368     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVolumeTexture8Impl));
369     if (NULL == object) {
370         FIXME("(%p) allocation of memory failed\n", This);
371         *ppVolumeTexture = NULL;
372         return D3DERR_OUTOFVIDEOMEMORY;
373     }
374
375     object->lpVtbl = &Direct3DVolumeTexture8_Vtbl;
376     object->ref = 1;
377     hrc = IWineD3DDevice_CreateVolumeTexture(This->WineD3DDevice, Width, Height, Depth, Levels, Usage,
378                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DVolumeTexture, NULL,
379                                  (IUnknown *)object, D3D8CB_CreateVolume);
380
381     if (hrc != D3D_OK) {
382
383         /* free up object */
384         FIXME("(%p) call to IWineD3DDevice_CreateVolumeTexture failed\n", This);
385         HeapFree(GetProcessHeap(), 0, object);
386         *ppVolumeTexture = NULL;
387     } else {
388         IUnknown_AddRef(iface);
389         object->parentDevice = iface;
390         *ppVolumeTexture = (LPDIRECT3DVOLUMETEXTURE8) object;
391     }
392     TRACE("(%p)  returning %p\n", This , *ppVolumeTexture);
393     return hrc;
394 }
395
396 static HRESULT WINAPI IDirect3DDevice8Impl_CreateCubeTexture(LPDIRECT3DDEVICE8 iface, UINT EdgeLength, UINT Levels, DWORD Usage, 
397                                                         D3DFORMAT Format, D3DPOOL Pool, IDirect3DCubeTexture8** ppCubeTexture) {
398
399     IDirect3DCubeTexture8Impl *object;
400     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
401     HRESULT hr = D3D_OK;
402
403     TRACE("(%p) : ELen(%d) Lvl(%d) Usage(%ld) fmt(%u), Pool(%d)\n" , This, EdgeLength, Levels, Usage, Format, Pool);
404
405     /* Allocate the storage for the device */
406     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
407
408     if (NULL == object) {
409         FIXME("(%p) allocation of CubeTexture failed\n", This);
410         *ppCubeTexture = NULL;
411         return D3DERR_OUTOFVIDEOMEMORY;
412     }
413
414     object->lpVtbl = &Direct3DCubeTexture8_Vtbl;
415     object->ref = 1;
416     hr = IWineD3DDevice_CreateCubeTexture(This->WineD3DDevice, EdgeLength, Levels, Usage,
417                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DCubeTexture, NULL, (IUnknown*)object,
418                                  D3D8CB_CreateSurface);
419
420     if (hr != D3D_OK){
421
422         /* free up object */
423         FIXME("(%p) call to IWineD3DDevice_CreateCubeTexture failed\n", This);
424         HeapFree(GetProcessHeap(), 0, object);
425         *ppCubeTexture = NULL;
426     } else {
427         IUnknown_AddRef(iface);
428         object->parentDevice = iface;
429         *ppCubeTexture = (LPDIRECT3DCUBETEXTURE8) object;
430     }
431
432     TRACE("(%p) returning %p\n",This, *ppCubeTexture);
433     return hr;
434 }
435
436 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexBuffer(LPDIRECT3DDEVICE8 iface, UINT Size, DWORD Usage, DWORD FVF, D3DPOOL Pool, IDirect3DVertexBuffer8** ppVertexBuffer) {
437     IDirect3DVertexBuffer8Impl *object;
438     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
439     HRESULT hrc = D3D_OK;
440
441     TRACE("(%p) Relay\n", This);
442     /* Allocate the storage for the device */
443     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexBuffer8Impl));
444     if (NULL == object) {
445         FIXME("Allocation of memory failed\n");
446         *ppVertexBuffer = NULL;
447         return D3DERR_OUTOFVIDEOMEMORY;
448     }
449
450     object->lpVtbl = &Direct3DVertexBuffer8_Vtbl;
451     object->ref = 1;
452     hrc = IWineD3DDevice_CreateVertexBuffer(This->WineD3DDevice, Size, Usage, FVF, (WINED3DPOOL) Pool, &(object->wineD3DVertexBuffer), NULL, (IUnknown *)object);
453
454     if (D3D_OK != hrc) {
455
456         /* free up object */
457         FIXME("(%p) call to IWineD3DDevice_CreateVertexBuffer failed\n", This);
458         HeapFree(GetProcessHeap(), 0, object);
459         *ppVertexBuffer = NULL;
460     } else {
461         IUnknown_AddRef(iface);
462         object->parentDevice = iface;
463         *ppVertexBuffer = (LPDIRECT3DVERTEXBUFFER8) object;
464     }
465     return hrc;
466 }
467
468 static HRESULT WINAPI IDirect3DDevice8Impl_CreateIndexBuffer(LPDIRECT3DDEVICE8 iface, UINT Length, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DIndexBuffer8** ppIndexBuffer) {
469     IDirect3DIndexBuffer8Impl *object;
470     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
471     HRESULT hrc = D3D_OK;
472
473     TRACE("(%p) Relay\n", This);
474     /* Allocate the storage for the device */
475     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
476     if (NULL == object) {
477         FIXME("Allocation of memory failed\n");
478         *ppIndexBuffer = NULL;
479         return D3DERR_OUTOFVIDEOMEMORY;
480     }
481
482     object->lpVtbl = &Direct3DIndexBuffer8_Vtbl;
483     object->ref = 1;
484     TRACE("Calling wined3d create index buffer\n");
485     hrc = IWineD3DDevice_CreateIndexBuffer(This->WineD3DDevice, Length, Usage, Format, (WINED3DPOOL) Pool, &object->wineD3DIndexBuffer, NULL, (IUnknown *)object);
486
487     if (D3D_OK != hrc) {
488
489         /* free up object */
490         FIXME("(%p) call to IWineD3DDevice_CreateIndexBuffer failed\n", This);
491         HeapFree(GetProcessHeap(), 0, object);
492         *ppIndexBuffer = NULL;
493     } else {
494         IUnknown_AddRef(iface);
495         object->parentDevice = iface;
496         *ppIndexBuffer = (LPDIRECT3DINDEXBUFFER8)object;
497     }
498     return hrc;
499 }
500
501 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)  {
502     HRESULT hrc;
503     IDirect3DSurface8Impl *object;
504     IDirect3DDevice8Impl  *This = (IDirect3DDevice8Impl *)iface;
505     TRACE("(%p) Relay\n", This);
506     if(MultisampleQuality < 0) { 
507         FIXME("MultisampleQuality out of range %ld, substituting 0\n", MultisampleQuality);
508         /*FIXME: Find out what windows does with a MultisampleQuality < 0 */
509         MultisampleQuality=0;
510     }
511
512     if(MultisampleQuality > 0){
513         FIXME("MultisampleQuality set to %ld, substituting 0\n" , MultisampleQuality);
514         /*
515         MultisampleQuality
516         [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.
517         */
518         MultisampleQuality=0;
519     }
520     /*FIXME: Check MAX bounds of MultisampleQuality*/
521
522     /* Allocate the storage for the device */
523     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
524     if (NULL == object) {
525         FIXME("Allocation of memory failed\n");
526         *ppSurface = NULL;
527         return D3DERR_OUTOFVIDEOMEMORY;
528     }
529
530     object->lpVtbl = &Direct3DSurface8_Vtbl;
531     object->ref = 1;
532
533     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
534
535     hrc = IWineD3DDevice_CreateSurface(This->WineD3DDevice, Width, Height, Format, Lockable, Discard, Level,  &object->wineD3DSurface, Type, Usage, (WINED3DPOOL) Pool,MultiSample,MultisampleQuality, NULL, SURFACE_OPENGL, (IUnknown *)object);
536     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
537        /* free up object */
538         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
539         HeapFree(GetProcessHeap(), 0, object);
540         *ppSurface = NULL;
541     } else {
542         IUnknown_AddRef(iface);
543         object->parentDevice = iface;
544         *ppSurface = (LPDIRECT3DSURFACE8) object;
545     }
546     return hrc;
547 }
548
549 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8** ppSurface) {
550     TRACE("Relay\n");
551
552     return IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */, 0 /* Level */ , ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, 0);
553 }
554
555 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8** ppSurface) {
556     TRACE("Relay\n");
557     /* TODO: Verify that Discard is false */
558     return IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE, 0 /* Level */
559                                                ,ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_DEPTHSTENCIL,
560                                                 D3DPOOL_DEFAULT, MultiSample, 0);
561 }
562
563 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8** ppSurface) {
564     TRACE("Relay\n");
565
566     return IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Loackable */ , FALSE /*Discard*/ , 0 /* Level */ , ppSurface, D3DRTYPE_SURFACE, 0 /* Usage (undefined/none) */ , D3DPOOL_SCRATCH, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
567 }
568
569 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8 *pSourceSurface, CONST RECT *pSourceRects, UINT cRects, IDirect3DSurface8 *pDestinationSurface, CONST POINT *pDestPoints) {
570     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
571
572     TRACE("(%p) Relay\n" , This);
573
574     return IWineD3DDevice_CopyRects(This->WineD3DDevice, pSourceSurface == NULL ? NULL : ((IDirect3DSurface8Impl *)pSourceSurface)->wineD3DSurface,
575             pSourceRects, cRects, pDestinationSurface == NULL ? NULL : ((IDirect3DSurface8Impl *)pDestinationSurface)->wineD3DSurface, pDestPoints);
576 }
577
578 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
579     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
580     TRACE("(%p) Relay\n" , This);
581
582     return IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
583 }
584
585 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
586     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
587     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
588
589     TRACE("(%p) Relay\n" , This);
590
591     if (pDestSurface == NULL) {
592         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
593         return D3DERR_INVALIDCALL;
594     }
595
596     return IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
597 }
598
599 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
600     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
601     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
602     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
603     TRACE("(%p) Relay\n" , This);
604
605     IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL == pZSurface ? NULL : (IWineD3DSurface *)pZSurface->wineD3DSurface);
606
607     return IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface ? (IWineD3DSurface *)pSurface->wineD3DSurface : NULL);
608 }
609
610 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
611     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
612     HRESULT hr = D3D_OK;
613     IWineD3DSurface *pRenderTarget;
614
615     TRACE("(%p) Relay\n" , This);
616
617     if (ppRenderTarget == NULL) {
618         return D3DERR_INVALIDCALL;
619     }
620     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
621
622     if (hr == D3D_OK && pRenderTarget != NULL) {
623         IWineD3DResource_GetParent((IWineD3DResource *)pRenderTarget,(IUnknown**)ppRenderTarget);
624         IWineD3DResource_Release((IWineD3DResource *)pRenderTarget);
625     } else {
626         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
627         *ppRenderTarget = NULL;
628     }
629
630     return hr;
631 }
632
633 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
634     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
635     HRESULT hr = D3D_OK;
636     IWineD3DSurface *pZStencilSurface;
637
638     TRACE("(%p) Relay\n" , This);
639     if(ppZStencilSurface == NULL){
640         return D3DERR_INVALIDCALL;
641     }
642
643     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
644     if(hr == D3D_OK && pZStencilSurface != NULL){
645         IWineD3DResource_GetParent((IWineD3DResource *)pZStencilSurface,(IUnknown**)ppZStencilSurface);
646         IWineD3DResource_Release((IWineD3DResource *)pZStencilSurface);
647     }else{
648         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
649         *ppZStencilSurface = NULL;
650     }
651
652     return D3D_OK;
653 }
654
655 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
656     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
657     return IWineD3DDevice_BeginScene(This->WineD3DDevice);
658 }
659
660 static HRESULT WINAPI IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
661     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
662     TRACE("(%p) Relay\n" , This);
663
664     return IWineD3DDevice_EndScene(This->WineD3DDevice);
665 }
666
667 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
668     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
669     TRACE("(%p) Relay\n" , This);
670
671     return IWineD3DDevice_Clear(This->WineD3DDevice, Count, pRects, Flags, Color, Z, Stencil);
672 }
673
674 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
675     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
676     TRACE("(%p) Relay\n" , This);
677
678     return IWineD3DDevice_SetTransform(This->WineD3DDevice, State, lpMatrix);
679 }
680
681 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
682     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
683     TRACE("(%p) Relay\n" , This);
684
685     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, pMatrix);
686 }
687
688 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
689     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
690     TRACE("(%p) Relay\n" , This);
691
692     return IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, pMatrix);
693 }
694
695 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
696     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
697     TRACE("(%p) Relay\n" , This);
698
699     return IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
700 }
701
702 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
703     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
704     TRACE("(%p) Relay\n" , This);
705
706     return IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
707 }
708
709 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
710     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
711     TRACE("(%p) Relay\n" , This);
712 /* FIXME: Verify that D3DMATERIAL8 ~= WINED3DMATERIAL */
713     return IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
714 }
715
716 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
717     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
718     TRACE("(%p) Relay\n" , This);
719 /* FIXME: Verify that D3DMATERIAL8 ~= WINED3DMATERIAL */
720     return IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
721 }
722
723 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
724     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
725     TRACE("(%p) Relay\n" , This);
726 /* FIXME: Verify that D3DLIGHT8 ~= WINED3DLIGHT */
727     return IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
728 }
729
730 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
731     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
732     TRACE("(%p) Relay\n" , This);
733 /* FIXME: Verify that D3DLIGHT8 ~= WINED3DLIGHT */
734     return IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
735 }
736
737 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
738     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
739     TRACE("(%p) Relay\n" , This);
740
741     return IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
742 }
743
744 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
745     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
746     TRACE("(%p) Relay\n" , This);
747
748     return IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
749 }
750
751 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
752     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
753     TRACE("(%p) Relay\n" , This);
754
755     return IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
756 }
757
758 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
759     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
760     TRACE("(%p) Relay\n" , This);
761
762     return IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
763 }
764
765 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
766     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
767     TRACE("(%p) Relay\n" , This);
768
769     return IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
770 }
771
772 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
773     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
774     TRACE("(%p) Relay\n" , This);
775
776     return IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
777 }
778
779 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
780   IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
781
782   TRACE("(%p)\n", This);
783
784   return IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
785 }
786
787 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
788     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
789     HRESULT hr;
790     IWineD3DStateBlock* wineD3DStateBlock;
791     IDirect3DStateBlock8Impl* object;
792
793     TRACE("(%p) Relay\n", This);
794
795     /* Tell wineD3D to endstatablock before anything else (in case we run out
796      * of memory later and cause locking problems)
797      */
798     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
799     if (hr != D3D_OK) {
800        FIXME("IWineD3DDevice_EndStateBlock returned an error\n");
801        return hr;
802     }
803
804     /* allocate a new IDirectD3DStateBlock */
805     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
806     object->ref = 1;
807     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
808
809     object->wineD3DStateBlock = wineD3DStateBlock;
810
811     *pToken = (DWORD)object;
812     TRACE("(%p)Returning %p %p\n", This, object, wineD3DStateBlock);
813
814     return hr;
815 }
816
817 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
818     IDirect3DStateBlock8Impl *pSB  = (IDirect3DStateBlock8Impl*) Token;
819     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
820
821     TRACE("(%p) %p Relay\n", This, pSB);
822
823     return  IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
824 }
825
826 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
827     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
828     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
829
830     TRACE("(%p) %p Relay\n", This, pSB);
831
832     return IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
833 }
834
835 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
836     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
837     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
838
839     TRACE("(%p) Relay\n", This);
840
841     while(IUnknown_Release((IUnknown *)pSB));
842
843     return D3D_OK;
844 }
845
846 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(LPDIRECT3DDEVICE8 iface, D3DSTATEBLOCKTYPE Type, DWORD* pToken) {
847    IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
848    IDirect3DStateBlock8Impl *object;
849    HRESULT hrc = D3D_OK;
850
851    TRACE("(%p) Relay\n", This);
852
853    object  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
854    if (NULL == object) {
855       *pToken = 0;
856       return E_OUTOFMEMORY;
857    }
858    object->lpVtbl = &Direct3DStateBlock8_Vtbl;
859    object->ref = 1;
860
861    hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown *)object);
862    if(D3D_OK != hrc){
863        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
864        HeapFree(GetProcessHeap(), 0, object);
865        *pToken = 0;
866    } else {
867        *pToken = (DWORD)object;
868    }
869    TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
870
871    return hrc;
872 }
873
874 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
875     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
876     TRACE("(%p) Relay\n" , This);
877 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
878     return IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
879 }
880
881 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
882     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
883     TRACE("(%p) Relay\n" , This);
884
885     return IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
886 }
887
888 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
889     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
890     IWineD3DBaseTexture *retTexture = NULL;
891     HRESULT rc = D3D_OK;
892
893     TRACE("(%p) Relay\n" , This);
894
895     if(ppTexture == NULL){
896         return D3DERR_INVALIDCALL;
897     }
898
899     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
900     if (rc == D3D_OK && NULL != retTexture) {
901         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
902         IWineD3DBaseTexture_Release(retTexture);
903     } else {
904         FIXME("Call to get texture  (%ld) failed (%p)\n", Stage, retTexture);
905         *ppTexture = NULL;
906     }
907
908     return rc;
909 }
910
911 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
912     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
913     TRACE("(%p) Relay %ld %p\n" , This, Stage, pTexture);
914
915     return IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
916                                      pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
917 }
918
919 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
920     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
921     TRACE("(%p) Relay\n" , This);
922
923     switch(Type) {
924     case D3DTSS_ADDRESSU:
925         Type = WINED3DSAMP_ADDRESSU;
926         break;
927     case D3DTSS_ADDRESSV:
928         Type = WINED3DSAMP_ADDRESSV;
929         break;
930     case D3DTSS_ADDRESSW:
931         Type = WINED3DSAMP_ADDRESSW;
932         break;
933     case D3DTSS_BORDERCOLOR:
934         Type = WINED3DSAMP_BORDERCOLOR;
935         break;
936     case D3DTSS_MAGFILTER:
937         Type = WINED3DSAMP_MAGFILTER;
938         break;
939     case D3DTSS_MAXANISOTROPY:
940         Type = WINED3DSAMP_MAXANISOTROPY;
941         break;
942     case D3DTSS_MAXMIPLEVEL:
943         Type = WINED3DSAMP_MAXMIPLEVEL;
944         break;
945     case D3DTSS_MINFILTER:
946         Type = WINED3DSAMP_MINFILTER;
947         break;
948     case D3DTSS_MIPFILTER:
949         Type = WINED3DSAMP_MIPFILTER;
950         break;
951     case D3DTSS_MIPMAPLODBIAS:
952         Type = WINED3DSAMP_MIPMAPLODBIAS;
953         break;
954     default:
955         return IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
956     }
957
958     return IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, Type, pValue);
959 }
960
961 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
962     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
963     TRACE("(%p) Relay\n" , This);
964
965     switch(Type) {
966     case D3DTSS_ADDRESSU:
967         Type = WINED3DSAMP_ADDRESSU;
968         break;
969     case D3DTSS_ADDRESSV:
970         Type = WINED3DSAMP_ADDRESSV;
971         break;
972     case D3DTSS_ADDRESSW:
973         Type = WINED3DSAMP_ADDRESSW;
974         break;
975     case D3DTSS_BORDERCOLOR:
976         Type = WINED3DSAMP_BORDERCOLOR;
977         break;
978     case D3DTSS_MAGFILTER:
979         Type = WINED3DSAMP_MAGFILTER;
980         break;
981     case D3DTSS_MAXANISOTROPY:
982         Type = WINED3DSAMP_MAXANISOTROPY;
983         break;
984     case D3DTSS_MAXMIPLEVEL:
985         Type = WINED3DSAMP_MAXMIPLEVEL;
986         break;
987     case D3DTSS_MINFILTER:
988         Type = WINED3DSAMP_MINFILTER;
989         break;
990     case D3DTSS_MIPFILTER:
991         Type = WINED3DSAMP_MIPFILTER;
992         break;
993     case D3DTSS_MIPMAPLODBIAS:
994         Type = WINED3DSAMP_MIPMAPLODBIAS;
995         break;
996     default:
997         return IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
998     }
999
1000     return IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, Type, Value);
1001 }
1002
1003 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1004     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1005     TRACE("(%p) Relay\n" , This);
1006
1007     return IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1008 }
1009
1010 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1011     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1012     FIXME("(%p) : stub\n", This);
1013     return D3D_OK;
1014 }
1015
1016 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1017     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1018     TRACE("(%p) Relay\n" , This);
1019
1020     return IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1021 }
1022
1023 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1024     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1025     TRACE("(%p) Relay\n" , This);
1026
1027     return IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1028 }
1029
1030 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1031     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1032     TRACE("(%p) Relay\n" , This);
1033
1034     return IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1035 }
1036
1037 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1038     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1039     TRACE("(%p) Relay\n" , This);
1040
1041     return IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1042 }
1043
1044 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1045     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface; 
1046     TRACE("(%p) Relay\n" , This);
1047
1048     return IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1049 }
1050
1051 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1052                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1053     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1054     TRACE("(%p) Relay\n" , This);
1055
1056     return IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, This->baseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
1057 }
1058
1059 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1060     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1061     TRACE("(%p) Relay\n" , This);
1062
1063     return IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1064 }
1065
1066 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1067                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1068                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1069                                                              UINT VertexStreamZeroStride) {
1070     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1071     TRACE("(%p) Relay\n" , This);
1072
1073     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1074                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1075 }
1076
1077 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1078     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1079     TRACE("(%p) Relay\n" , This);
1080
1081     return IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer8Impl *)pDestBuffer)->wineD3DVertexBuffer, NULL, Flags);
1082 }
1083
1084 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pDeclaration, CONST DWORD* pFunction, DWORD* ppShader, DWORD Usage) {
1085     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1086     HRESULT hrc = D3D_OK;
1087     IDirect3DVertexShader8Impl *object;
1088
1089     /* Setup a stub object for now */
1090     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1091     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1092     if (NULL == object) {
1093         FIXME("Allocation of memory failed\n");
1094         *ppShader = 0;
1095         return D3DERR_OUTOFVIDEOMEMORY;
1096     }
1097
1098     object->ref = 1;
1099     object->lpVtbl = &Direct3DVertexShader8_Vtbl;
1100     /* Usage is missing ..*/
1101     hrc = IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, pDeclaration, pFunction, &object->wineD3DVertexShader, (IUnknown *)object);
1102
1103     if (FAILED(hrc)) {
1104         /* free up object */
1105         FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
1106         HeapFree(GetProcessHeap(), 0, object);
1107         *ppShader = 0;
1108     } else {
1109         /* TODO: Store the VS declarations locally so that they can be derefferenced with a value higher than VS_HIGHESTFIXEDFXF */
1110         shader_handle *handle = alloc_shader_handle(This);
1111         if (!handle) {
1112             ERR("Failed to allocate shader handle\n");
1113             IDirect3DVertexShader8_Release((IUnknown *)object);
1114             hrc = E_OUTOFMEMORY;
1115         } else {
1116             object->handle = handle;
1117             *handle = object;
1118             *ppShader = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1119         }
1120     }
1121     TRACE("(%p) : returning %p (handle %#lx)\n", This, object, *ppShader);
1122
1123     return hrc;
1124 }
1125
1126 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1127     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1128     HRESULT hrc = D3D_OK;
1129
1130     TRACE("(%p) : Relay\n", This);
1131     if (VS_HIGHESTFIXEDFXF >= pShader) {
1132         TRACE("Setting FVF, %d %ld\n", VS_HIGHESTFIXEDFXF, pShader);
1133         IWineD3DDevice_SetFVF(This->WineD3DDevice, pShader);
1134
1135         /* Call SetVertexShader with a NULL shader to set the vertexshader in the stateblock to NULL. */
1136         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1137     } else {
1138         TRACE("Setting shader\n");
1139         if (This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1140             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1141             hrc = D3DERR_INVALIDCALL;
1142         } else {
1143             IDirect3DVertexShader8Impl *shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1144             hrc =  IWineD3DDevice_SetVertexShader(This->WineD3DDevice, 0 == shader ? NULL : shader->wineD3DVertexShader);
1145         }
1146     }
1147     TRACE("(%p) : returning hr(%lu)\n", This, hrc);
1148
1149     return hrc;
1150 }
1151
1152 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1153     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1154     IWineD3DVertexShader *pShader;
1155     HRESULT hrc = D3D_OK;
1156
1157     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1158     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
1159     if (D3D_OK == hrc) {
1160         if(0 != pShader) {
1161             IDirect3DVertexShader8Impl *d3d8_shader;
1162             hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)&d3d8_shader);
1163             IWineD3DVertexShader_Release(pShader);
1164             *ppShader = (d3d8_shader->handle - This->shader_handles) + (VS_HIGHESTFIXEDFXF + 1);
1165         } else {
1166             WARN("(%p) : The shader has been set to NULL\n", This);
1167
1168             /* TODO: Find out what should be returned, e.g. the FVF */
1169             *ppShader = 0;
1170             hrc = D3DERR_INVALIDCALL;
1171         }
1172     } else {
1173         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %lu (device %p)\n", This, hrc, This->WineD3DDevice);
1174     }
1175     TRACE("(%p) : returning %#lx\n", This, *ppShader);
1176
1177     return hrc;
1178 }
1179
1180 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1181     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1182
1183     TRACE("(%p) : pShader %#lx\n", This, pShader);
1184
1185     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1186         ERR("(%p) : Trying to delete an invalid handle\n", This);
1187         return D3DERR_INVALIDCALL;
1188     } else {
1189         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1190         IDirect3DVertexShader8Impl *shader = *handle;
1191         while(IUnknown_Release((IUnknown *)shader));
1192         free_shader_handle(This, handle);
1193     }
1194
1195     return D3D_OK;
1196 }
1197
1198 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1199     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1200     TRACE("(%p) : Relay\n", This);
1201
1202     return IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1203 }
1204
1205 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1206     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1207     TRACE("(%p) : Relay\n", This);
1208
1209     return IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1210 }
1211
1212 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1213     IDirect3DVertexShader8Impl *This = (IDirect3DVertexShader8Impl *)pVertexShader;
1214
1215     TRACE("(%p) : Relay\n", This);
1216 /*    return IWineD3DVertexShader_GetDeclaration(This->wineD3DVertexShader, pData, (UINT *)pSizeOfData); */
1217     return D3DERR_INVALIDCALL;
1218 }
1219 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1220     IDirect3DVertexShader8Impl *This = (IDirect3DVertexShader8Impl *)pVertexShader;
1221
1222     TRACE("(%p) : Relay\n", This);
1223     return IWineD3DVertexShader_GetFunction(This->wineD3DVertexShader, pData, (UINT *)pSizeOfData);
1224 }
1225
1226 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
1227     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1228     TRACE("(%p) Relay\n", This);
1229 /* FIXME: store base vertex index properly */
1230     This->baseVertexIndex = baseVertexIndex;
1231     return IWineD3DDevice_SetIndices(This->WineD3DDevice,
1232                                      NULL == pIndexData ? NULL : ((IDirect3DIndexBuffer8Impl *)pIndexData)->wineD3DIndexBuffer,
1233                                      0);
1234 }
1235
1236 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
1237     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1238     IWineD3DIndexBuffer *retIndexData = NULL;
1239     HRESULT rc = D3D_OK;
1240     UINT tmp;
1241
1242     TRACE("(%p) Relay\n", This);
1243
1244     if(ppIndexData == NULL){
1245         return D3DERR_INVALIDCALL;
1246     }
1247
1248     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData, &tmp);
1249     if (D3D_OK == rc && NULL != retIndexData) {
1250         IWineD3DVertexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1251         IWineD3DVertexBuffer_Release(retIndexData);
1252     } else {
1253         if(rc != D3D_OK)  FIXME("Call to GetIndices failed\n");
1254         *ppIndexData = NULL;
1255     }
1256 /* FIXME: store base vertex index properly */
1257     *pBaseVertexIndex = This->baseVertexIndex;
1258     return rc;
1259 }
1260 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
1261     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1262     IDirect3DPixelShader8Impl *object;
1263     HRESULT hrc = D3D_OK;
1264
1265     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1266
1267     if (NULL == ppShader) {
1268         TRACE("(%p) Invalid call\n", This);
1269         return D3DERR_INVALIDCALL;
1270     }
1271     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1272
1273     if (NULL == object) {
1274         return E_OUTOFMEMORY;
1275     } else {
1276
1277         object->ref    = 1;
1278         object->lpVtbl = &Direct3DPixelShader8_Vtbl;
1279         hrc = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction, &object->wineD3DPixelShader , (IUnknown *)object);
1280         if (D3D_OK != hrc) {
1281             FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
1282             HeapFree(GetProcessHeap(), 0 , object);
1283             *ppShader = 0;
1284         } else {
1285             shader_handle *handle = alloc_shader_handle(This);
1286             if (!handle) {
1287                 ERR("Failed to allocate shader handle\n");
1288                 IDirect3DVertexShader8_Release((IUnknown *)object);
1289                 hrc = E_OUTOFMEMORY;
1290             } else {
1291                 object->handle = handle;
1292                 *handle = object;
1293                 *ppShader = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1294             }
1295         }
1296
1297     }
1298
1299     TRACE("(%p) : returning %p (handle %#lx)\n", This, object, *ppShader);
1300     return hrc;
1301 }
1302
1303 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1304     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1305     IDirect3DPixelShader8Impl *shader = NULL;
1306
1307     TRACE("(%p) : pShader %#lx\n", This, pShader);
1308
1309     if (pShader > VS_HIGHESTFIXEDFXF && This->allocated_shader_handles > pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1310         shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1311     } else if (pShader) {
1312         ERR("Trying to set an invalid handle.\n");
1313     }
1314
1315     TRACE("(%p) : Setting shader %p\n", This, shader);
1316     return IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader == NULL ? NULL :shader->wineD3DPixelShader);
1317 }
1318
1319 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1320     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1321     IWineD3DPixelShader *object;
1322
1323     HRESULT hrc = D3D_OK;
1324     TRACE("(%p) Relay\n", This);
1325     if (NULL == ppShader) {
1326         TRACE("(%p) Invalid call\n", This);
1327         return D3DERR_INVALIDCALL;
1328     }
1329
1330     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
1331     if (D3D_OK == hrc && NULL != object) {
1332         IDirect3DPixelShader8Impl *d3d8_shader;
1333         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
1334         IWineD3DPixelShader_Release(object);
1335         *ppShader = (d3d8_shader->handle - This->shader_handles) + (VS_HIGHESTFIXEDFXF + 1);
1336     } else {
1337         *ppShader = (DWORD)NULL;
1338     }
1339
1340     TRACE("(%p) : returning %#lx\n", This, *ppShader);
1341     return hrc;
1342 }
1343
1344 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1345     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1346
1347     TRACE("(%p) : pShader %#lx\n", This, pShader);
1348
1349     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1350         ERR("(%p) : Trying to delete an invalid handle\n", This);
1351         return D3DERR_INVALIDCALL;
1352     } else {
1353         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1354         IDirect3DPixelShader8Impl *shader = *handle;
1355         while(IUnknown_Release((IUnknown *)shader));
1356         free_shader_handle(This, handle);
1357     }
1358
1359     return D3D_OK;
1360 }
1361
1362 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1363     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1364     TRACE("(%p) Relay\n", This);
1365
1366     return IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1367 }
1368
1369 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1370     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1371     TRACE("(%p) Relay\n", This);
1372
1373     return IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1374 }
1375
1376 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
1377     IDirect3DPixelShader8Impl *This = (IDirect3DPixelShader8Impl *)pPixelShader;
1378
1379     TRACE("(%p) : Relay\n", This);
1380     return IWineD3DPixelShader_GetFunction(This->wineD3DPixelShader, pData, (UINT *)pSizeOfData);
1381 }
1382
1383 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1384     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1385     TRACE("(%p) Relay\n", This);
1386
1387     return IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (WINED3DRECTPATCH_INFO *)pRectPatchInfo);
1388 }
1389
1390 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
1391     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1392     TRACE("(%p) Relay\n", This);
1393
1394     return IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (WINED3DTRIPATCH_INFO *)pTriPatchInfo);
1395 }
1396
1397 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
1398     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1399     TRACE("(%p) Relay\n", This);
1400
1401     return IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
1402 }
1403
1404 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
1405     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1406     TRACE("(%p) Relay\n" , This);
1407
1408     return IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1409                                           NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
1410                                           0/* Offset in bytes */, Stride);
1411 }
1412
1413 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
1414     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1415     IWineD3DVertexBuffer *retStream = NULL;
1416     HRESULT rc = D3D_OK;
1417
1418     TRACE("(%p) Relay\n" , This);
1419
1420     if(pStream == NULL){
1421         return D3DERR_INVALIDCALL;
1422     }
1423
1424     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, 0 /* Offset in bytes */, pStride);
1425     if (rc == D3D_OK  && NULL != retStream) {
1426         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
1427         IWineD3DVertexBuffer_Release(retStream);
1428     }else{
1429          FIXME("Call to GetStreamSource failed %p\n",  pStride);
1430         *pStream = NULL;
1431     }
1432
1433     return rc;
1434 }
1435
1436
1437 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
1438 {
1439     IDirect3DDevice8Impl_QueryInterface,
1440     IDirect3DDevice8Impl_AddRef,
1441     IDirect3DDevice8Impl_Release,
1442     IDirect3DDevice8Impl_TestCooperativeLevel,
1443     IDirect3DDevice8Impl_GetAvailableTextureMem,
1444     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
1445     IDirect3DDevice8Impl_GetDirect3D,
1446     IDirect3DDevice8Impl_GetDeviceCaps,
1447     IDirect3DDevice8Impl_GetDisplayMode,
1448     IDirect3DDevice8Impl_GetCreationParameters,
1449     IDirect3DDevice8Impl_SetCursorProperties,
1450     IDirect3DDevice8Impl_SetCursorPosition,
1451     IDirect3DDevice8Impl_ShowCursor,
1452     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
1453     IDirect3DDevice8Impl_Reset,
1454     IDirect3DDevice8Impl_Present,
1455     IDirect3DDevice8Impl_GetBackBuffer,
1456     IDirect3DDevice8Impl_GetRasterStatus,
1457     IDirect3DDevice8Impl_SetGammaRamp,
1458     IDirect3DDevice8Impl_GetGammaRamp,
1459     IDirect3DDevice8Impl_CreateTexture,
1460     IDirect3DDevice8Impl_CreateVolumeTexture,
1461     IDirect3DDevice8Impl_CreateCubeTexture,
1462     IDirect3DDevice8Impl_CreateVertexBuffer,
1463     IDirect3DDevice8Impl_CreateIndexBuffer,
1464     IDirect3DDevice8Impl_CreateRenderTarget,
1465     IDirect3DDevice8Impl_CreateDepthStencilSurface,
1466     IDirect3DDevice8Impl_CreateImageSurface,
1467     IDirect3DDevice8Impl_CopyRects,
1468     IDirect3DDevice8Impl_UpdateTexture,
1469     IDirect3DDevice8Impl_GetFrontBuffer,
1470     IDirect3DDevice8Impl_SetRenderTarget,
1471     IDirect3DDevice8Impl_GetRenderTarget,
1472     IDirect3DDevice8Impl_GetDepthStencilSurface,
1473     IDirect3DDevice8Impl_BeginScene,
1474     IDirect3DDevice8Impl_EndScene,
1475     IDirect3DDevice8Impl_Clear,
1476     IDirect3DDevice8Impl_SetTransform,
1477     IDirect3DDevice8Impl_GetTransform,
1478     IDirect3DDevice8Impl_MultiplyTransform,
1479     IDirect3DDevice8Impl_SetViewport,
1480     IDirect3DDevice8Impl_GetViewport,
1481     IDirect3DDevice8Impl_SetMaterial,
1482     IDirect3DDevice8Impl_GetMaterial,
1483     IDirect3DDevice8Impl_SetLight,
1484     IDirect3DDevice8Impl_GetLight,
1485     IDirect3DDevice8Impl_LightEnable,
1486     IDirect3DDevice8Impl_GetLightEnable,
1487     IDirect3DDevice8Impl_SetClipPlane,
1488     IDirect3DDevice8Impl_GetClipPlane,
1489     IDirect3DDevice8Impl_SetRenderState,
1490     IDirect3DDevice8Impl_GetRenderState,
1491     IDirect3DDevice8Impl_BeginStateBlock,
1492     IDirect3DDevice8Impl_EndStateBlock,
1493     IDirect3DDevice8Impl_ApplyStateBlock,
1494     IDirect3DDevice8Impl_CaptureStateBlock,
1495     IDirect3DDevice8Impl_DeleteStateBlock,
1496     IDirect3DDevice8Impl_CreateStateBlock,
1497     IDirect3DDevice8Impl_SetClipStatus,
1498     IDirect3DDevice8Impl_GetClipStatus,
1499     IDirect3DDevice8Impl_GetTexture,
1500     IDirect3DDevice8Impl_SetTexture,
1501     IDirect3DDevice8Impl_GetTextureStageState,
1502     IDirect3DDevice8Impl_SetTextureStageState,
1503     IDirect3DDevice8Impl_ValidateDevice,
1504     IDirect3DDevice8Impl_GetInfo,
1505     IDirect3DDevice8Impl_SetPaletteEntries,
1506     IDirect3DDevice8Impl_GetPaletteEntries,
1507     IDirect3DDevice8Impl_SetCurrentTexturePalette,
1508     IDirect3DDevice8Impl_GetCurrentTexturePalette,
1509     IDirect3DDevice8Impl_DrawPrimitive,
1510     IDirect3DDevice8Impl_DrawIndexedPrimitive,
1511     IDirect3DDevice8Impl_DrawPrimitiveUP,
1512     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
1513     IDirect3DDevice8Impl_ProcessVertices,
1514     IDirect3DDevice8Impl_CreateVertexShader,
1515     IDirect3DDevice8Impl_SetVertexShader,
1516     IDirect3DDevice8Impl_GetVertexShader,
1517     IDirect3DDevice8Impl_DeleteVertexShader,
1518     IDirect3DDevice8Impl_SetVertexShaderConstant,
1519     IDirect3DDevice8Impl_GetVertexShaderConstant,
1520     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
1521     IDirect3DDevice8Impl_GetVertexShaderFunction,
1522     IDirect3DDevice8Impl_SetStreamSource,
1523     IDirect3DDevice8Impl_GetStreamSource,
1524     IDirect3DDevice8Impl_SetIndices,
1525     IDirect3DDevice8Impl_GetIndices,
1526     IDirect3DDevice8Impl_CreatePixelShader,
1527     IDirect3DDevice8Impl_SetPixelShader,
1528     IDirect3DDevice8Impl_GetPixelShader,
1529     IDirect3DDevice8Impl_DeletePixelShader,
1530     IDirect3DDevice8Impl_SetPixelShaderConstant,
1531     IDirect3DDevice8Impl_GetPixelShaderConstant,
1532     IDirect3DDevice8Impl_GetPixelShaderFunction,
1533     IDirect3DDevice8Impl_DrawRectPatch,
1534     IDirect3DDevice8Impl_DrawTriPatch,
1535     IDirect3DDevice8Impl_DeletePatch
1536 };
1537
1538 /* Internal function called back during the CreateDevice to create a render target  */
1539 HRESULT WINAPI D3D8CB_CreateSurface(IUnknown *device, UINT Width, UINT Height, 
1540                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1541                                          IWineD3DSurface **ppSurface, HANDLE *pSharedHandle) {
1542
1543     HRESULT res = D3D_OK;
1544     IDirect3DSurface8Impl *d3dSurface = NULL;
1545     BOOL Lockable = TRUE;
1546
1547     if((WINED3DPOOL_DEFAULT == Pool && WINED3DUSAGE_DYNAMIC != Usage))
1548         Lockable = FALSE;
1549
1550     TRACE("relay\n");
1551     res = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)device, Width, Height, (D3DFORMAT)Format, Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface8 **)&d3dSurface, D3DRTYPE_SURFACE, Usage, Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1552
1553     if (SUCCEEDED(res)) {
1554         *ppSurface = d3dSurface->wineD3DSurface;
1555         IUnknown_Release(d3dSurface->parentDevice);
1556         d3dSurface->parentDevice = NULL;
1557     } else {
1558         FIXME("(%p) IDirect3DDevice8_CreateSurface failed\n", device);
1559     }
1560     return res;
1561 }