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