rpcrt4: Fix the argument size of conformant arrays.
[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         *ppSurface = (LPDIRECT3DSURFACE8) object;
518     }
519     return hrc;
520 }
521
522 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8** ppSurface) {
523     TRACE("Relay\n");
524
525     return IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */, 0 /* Level */ , ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, 0);
526 }
527
528 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8** ppSurface) {
529     TRACE("Relay\n");
530     /* TODO: Verify that Discard is false */
531     return IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE, 0 /* Level */
532                                                ,ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_DEPTHSTENCIL,
533                                                 D3DPOOL_DEFAULT, MultiSample, 0);
534 }
535
536 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8** ppSurface) {
537     TRACE("Relay\n");
538
539     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 */);
540 }
541
542 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8 *pSourceSurface, CONST RECT *pSourceRects, UINT cRects, IDirect3DSurface8 *pDestinationSurface, CONST POINT *pDestPoints) {
543     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
544
545     TRACE("(%p) Relay\n" , This);
546
547     return IWineD3DDevice_CopyRects(This->WineD3DDevice, pSourceSurface == NULL ? NULL : ((IDirect3DSurface8Impl *)pSourceSurface)->wineD3DSurface,
548             pSourceRects, cRects, pDestinationSurface == NULL ? NULL : ((IDirect3DSurface8Impl *)pDestinationSurface)->wineD3DSurface, pDestPoints);
549 }
550
551 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
552     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
553     TRACE("(%p) Relay\n" , This);
554
555     return IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
556 }
557
558 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
559     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
560     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
561
562     TRACE("(%p) Relay\n" , This);
563
564     if (pDestSurface == NULL) {
565         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
566         return D3DERR_INVALIDCALL;
567     }
568
569     return IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
570 }
571
572 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
573     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
574     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
575     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
576     TRACE("(%p) Relay\n" , This);
577
578     IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL == pZSurface ? NULL : (IWineD3DSurface *)pZSurface->wineD3DSurface);
579
580     return IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, (IWineD3DSurface *)pSurface->wineD3DSurface);
581 }
582
583 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
584     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
585     HRESULT hr = D3D_OK;
586     IWineD3DSurface *pRenderTarget;
587
588     TRACE("(%p) Relay\n" , This);
589
590     if (ppRenderTarget == NULL) {
591         return D3DERR_INVALIDCALL;
592     }
593     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
594
595     if (hr == D3D_OK && pRenderTarget != NULL) {
596         IWineD3DResource_GetParent((IWineD3DResource *)pRenderTarget,(IUnknown**)ppRenderTarget);
597         IWineD3DResource_Release((IWineD3DResource *)pRenderTarget);
598     } else {
599         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
600         *ppRenderTarget = NULL;
601     }
602
603     return hr;
604 }
605
606 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
607     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
608     HRESULT hr = D3D_OK;
609     IWineD3DSurface *pZStencilSurface;
610
611     TRACE("(%p) Relay\n" , This);
612     if(ppZStencilSurface == NULL){
613         return D3DERR_INVALIDCALL;
614     }
615
616     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
617     if(hr == D3D_OK && pZStencilSurface != NULL){
618         IWineD3DResource_GetParent((IWineD3DResource *)pZStencilSurface,(IUnknown**)ppZStencilSurface);
619         IWineD3DResource_Release((IWineD3DResource *)pZStencilSurface);
620     }else{
621         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
622         *ppZStencilSurface = NULL;
623     }
624
625     return D3D_OK;
626 }
627
628 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
629     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
630     return IWineD3DDevice_BeginScene(This->WineD3DDevice);
631 }
632
633 static HRESULT WINAPI IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
634     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
635     TRACE("(%p) Relay\n" , This);
636
637     return IWineD3DDevice_EndScene(This->WineD3DDevice);
638 }
639
640 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
641     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
642     TRACE("(%p) Relay\n" , This);
643
644     return IWineD3DDevice_Clear(This->WineD3DDevice, Count, pRects, Flags, Color, Z, Stencil);
645 }
646
647 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
648     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
649     TRACE("(%p) Relay\n" , This);
650
651     return IWineD3DDevice_SetTransform(This->WineD3DDevice, State, lpMatrix);
652 }
653
654 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
655     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
656     TRACE("(%p) Relay\n" , This);
657
658     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, pMatrix);
659 }
660
661 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
662     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
663     TRACE("(%p) Relay\n" , This);
664
665     return IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, pMatrix);
666 }
667
668 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
669     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
670     TRACE("(%p) Relay\n" , This);
671
672     return IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
673 }
674
675 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
676     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
677     TRACE("(%p) Relay\n" , This);
678
679     return IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
680 }
681
682 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
683     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
684     TRACE("(%p) Relay\n" , This);
685 /* FIXME: Verify that D3DMATERIAL8 ~= WINED3DMATERIAL */
686     return IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
687 }
688
689 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
690     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
691     TRACE("(%p) Relay\n" , This);
692 /* FIXME: Verify that D3DMATERIAL8 ~= WINED3DMATERIAL */
693     return IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
694 }
695
696 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
697     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
698     TRACE("(%p) Relay\n" , This);
699 /* FIXME: Verify that D3DLIGHT8 ~= WINED3DLIGHT */
700     return IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
701 }
702
703 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
704     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
705     TRACE("(%p) Relay\n" , This);
706 /* FIXME: Verify that D3DLIGHT8 ~= WINED3DLIGHT */
707     return IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
708 }
709
710 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
711     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
712     TRACE("(%p) Relay\n" , This);
713
714     return IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
715 }
716
717 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
718     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
719     TRACE("(%p) Relay\n" , This);
720
721     return IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
722 }
723
724 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
725     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
726     TRACE("(%p) Relay\n" , This);
727
728     return IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
729 }
730
731 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
732     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
733     TRACE("(%p) Relay\n" , This);
734
735     return IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
736 }
737
738 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
739     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
740     TRACE("(%p) Relay\n" , This);
741
742     return IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
743 }
744
745 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
746     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
747     TRACE("(%p) Relay\n" , This);
748
749     return IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
750 }
751
752 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
753   IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
754
755   TRACE("(%p)\n", This);
756
757   return IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
758 }
759
760 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
761     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
762     HRESULT hr;
763     IWineD3DStateBlock* wineD3DStateBlock;
764     IDirect3DStateBlock8Impl* object;
765
766     TRACE("(%p) Relay\n", This);
767
768     /* Tell wineD3D to endstatablock before anything else (in case we run out
769      * of memory later and cause locking problems)
770      */
771     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
772     if (hr != D3D_OK) {
773        FIXME("IWineD3DDevice_EndStateBlock returned an error\n");
774        return hr;
775     }
776
777     /* allocate a new IDirectD3DStateBlock */
778     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
779     object->ref = 1;
780     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
781
782     object->wineD3DStateBlock = wineD3DStateBlock;
783
784     *pToken = (DWORD)object;
785     TRACE("(%p)Returning %p %p\n", This, object, wineD3DStateBlock);
786
787     return hr;
788 }
789
790 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
791     IDirect3DStateBlock8Impl *pSB  = (IDirect3DStateBlock8Impl*) Token;
792     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
793
794     TRACE("(%p) %p Relay\n", This, pSB);
795
796     return  IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
797 }
798
799 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
800     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
801     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
802
803     TRACE("(%p) %p Relay\n", This, pSB);
804
805     return IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
806 }
807
808 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
809     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
810     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
811
812     TRACE("(%p) Relay\n", This);
813
814     while(IUnknown_Release((IUnknown *)pSB));
815
816     return D3D_OK;
817 }
818
819 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(LPDIRECT3DDEVICE8 iface, D3DSTATEBLOCKTYPE Type, DWORD* pToken) {
820    IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
821    IDirect3DStateBlock8Impl *object;
822    HRESULT hrc = D3D_OK;
823
824    TRACE("(%p) Relay\n", This);
825
826    object  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
827    if (NULL == object) {
828       *pToken = 0;
829       return E_OUTOFMEMORY;
830    }
831    object->lpVtbl = &Direct3DStateBlock8_Vtbl;
832    object->ref = 1;
833
834    hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown *)object);
835    if(D3D_OK != hrc){
836        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
837        HeapFree(GetProcessHeap(), 0, object);
838        *pToken = 0;
839    } else {
840        *pToken = (DWORD)object;
841    }
842    TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
843
844    return hrc;
845 }
846
847 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
848     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
849     TRACE("(%p) Relay\n" , This);
850 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
851     return IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
852 }
853
854 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
855     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
856     TRACE("(%p) Relay\n" , This);
857
858     return IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
859 }
860
861 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
862     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
863     IWineD3DBaseTexture *retTexture = NULL;
864     HRESULT rc = D3D_OK;
865
866     TRACE("(%p) Relay\n" , This);
867
868     if(ppTexture == NULL){
869         return D3DERR_INVALIDCALL;
870     }
871
872     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
873     if (rc == D3D_OK && NULL != retTexture) {
874         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
875         IWineD3DBaseTexture_Release(retTexture);
876     } else {
877         FIXME("Call to get texture  (%ld) failed (%p)\n", Stage, retTexture);
878         *ppTexture = NULL;
879     }
880
881     return rc;
882 }
883
884 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
885     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
886     TRACE("(%p) Relay %ld %p\n" , This, Stage, pTexture);
887
888     return IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
889                                      pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
890 }
891
892 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
893     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
894     TRACE("(%p) Relay\n" , This);
895
896     switch(Type) {
897     case D3DTSS_ADDRESSU:
898         Type = WINED3DSAMP_ADDRESSU;
899         break;
900     case D3DTSS_ADDRESSV:
901         Type = WINED3DSAMP_ADDRESSV;
902         break;
903     case D3DTSS_ADDRESSW:
904         Type = WINED3DSAMP_ADDRESSW;
905         break;
906     case D3DTSS_BORDERCOLOR:
907         Type = WINED3DSAMP_BORDERCOLOR;
908         break;
909     case D3DTSS_MAGFILTER:
910         Type = WINED3DSAMP_MAGFILTER;
911         break;
912     case D3DTSS_MAXANISOTROPY:
913         Type = WINED3DSAMP_MAXANISOTROPY;
914         break;
915     case D3DTSS_MAXMIPLEVEL:
916         Type = WINED3DSAMP_MAXMIPLEVEL;
917         break;
918     case D3DTSS_MINFILTER:
919         Type = WINED3DSAMP_MINFILTER;
920         break;
921     case D3DTSS_MIPFILTER:
922         Type = WINED3DSAMP_MIPFILTER;
923         break;
924     case D3DTSS_MIPMAPLODBIAS:
925         Type = WINED3DSAMP_MIPMAPLODBIAS;
926         break;
927     default:
928         return IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
929     }
930
931     return IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, Type, pValue);
932 }
933
934 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
935     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
936     TRACE("(%p) Relay\n" , This);
937
938     switch(Type) {
939     case D3DTSS_ADDRESSU:
940         Type = WINED3DSAMP_ADDRESSU;
941         break;
942     case D3DTSS_ADDRESSV:
943         Type = WINED3DSAMP_ADDRESSV;
944         break;
945     case D3DTSS_ADDRESSW:
946         Type = WINED3DSAMP_ADDRESSW;
947         break;
948     case D3DTSS_BORDERCOLOR:
949         Type = WINED3DSAMP_BORDERCOLOR;
950         break;
951     case D3DTSS_MAGFILTER:
952         Type = WINED3DSAMP_MAGFILTER;
953         break;
954     case D3DTSS_MAXANISOTROPY:
955         Type = WINED3DSAMP_MAXANISOTROPY;
956         break;
957     case D3DTSS_MAXMIPLEVEL:
958         Type = WINED3DSAMP_MAXMIPLEVEL;
959         break;
960     case D3DTSS_MINFILTER:
961         Type = WINED3DSAMP_MINFILTER;
962         break;
963     case D3DTSS_MIPFILTER:
964         Type = WINED3DSAMP_MIPFILTER;
965         break;
966     case D3DTSS_MIPMAPLODBIAS:
967         Type = WINED3DSAMP_MIPMAPLODBIAS;
968         break;
969     default:
970         return IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
971     }
972
973     return IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, Type, Value);
974 }
975
976 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
977     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
978     TRACE("(%p) Relay\n" , This);
979
980     return IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
981 }
982
983 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
984     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
985     FIXME("(%p) : stub\n", This);
986     return D3D_OK;
987 }
988
989 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
990     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
991     TRACE("(%p) Relay\n" , This);
992
993     return IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
994 }
995
996 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
997     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
998     TRACE("(%p) Relay\n" , This);
999
1000     return IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1001 }
1002
1003 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1004     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1005     TRACE("(%p) Relay\n" , This);
1006
1007     return IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1008 }
1009
1010 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1011     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1012     TRACE("(%p) Relay\n" , This);
1013
1014     return IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1015 }
1016
1017 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1018     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface; 
1019     TRACE("(%p) Relay\n" , This);
1020
1021     return IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1022 }
1023
1024 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1025                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1026     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1027     TRACE("(%p) Relay\n" , This);
1028
1029     return IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, This->baseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
1030 }
1031
1032 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1033     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1034     TRACE("(%p) Relay\n" , This);
1035
1036     return IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1037 }
1038
1039 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1040                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1041                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1042                                                              UINT VertexStreamZeroStride) {
1043     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1044     TRACE("(%p) Relay\n" , This);
1045
1046     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1047                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1048 }
1049
1050 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1051     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1052     TRACE("(%p) Relay\n" , This);
1053
1054     return IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer8Impl *)pDestBuffer)->wineD3DVertexBuffer, NULL, Flags);
1055 }
1056
1057 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pDeclaration, CONST DWORD* pFunction, DWORD* ppShader, DWORD Usage) {
1058     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1059     HRESULT hrc = D3D_OK;
1060     IDirect3DVertexShader8Impl *object;
1061
1062     /* Setup a stub object for now */
1063     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1064     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1065     if (NULL == object) {
1066         FIXME("Allocation of memory failed\n");
1067         *ppShader = 0;
1068         return D3DERR_OUTOFVIDEOMEMORY;
1069     }
1070
1071     object->ref = 1;
1072     object->lpVtbl = &Direct3DVertexShader8_Vtbl;
1073     /* Usage is missing ..*/
1074     hrc = IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, pDeclaration, pFunction, &object->wineD3DVertexShader, (IUnknown *)object);
1075
1076     if (FAILED(hrc)) {
1077         /* free up object */
1078         FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
1079         HeapFree(GetProcessHeap(), 0, object);
1080         *ppShader = 0;
1081     } else {
1082         /* TODO: Store the VS declarations locally so that they can be derefferenced with a value higher than VS_HIGHESTFIXEDFXF */
1083         DWORD i = 0;
1084         while(This->vShaders[i] != NULL && i < MAX_SHADERS) ++i;
1085         if (MAX_SHADERS == i) {
1086             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1087             hrc = E_OUTOFMEMORY;
1088         } else {
1089             This->vShaders[i] = object;
1090             *ppShader = i + VS_HIGHESTFIXEDFXF + 1;
1091         }
1092     }
1093     TRACE("(%p) : returning %p\n", This, object);
1094
1095     return hrc;
1096 }
1097
1098 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1099     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1100     HRESULT hrc = D3D_OK;
1101
1102     TRACE("(%p) : Relay\n", This);
1103     if (VS_HIGHESTFIXEDFXF >= pShader) {
1104         TRACE("Setting FVF, %d %ld\n", VS_HIGHESTFIXEDFXF, pShader);
1105         IWineD3DDevice_SetFVF(This->WineD3DDevice, pShader);
1106
1107         /* Call SetVertexShader with a NULL shader to set the vertexshader in the stateblock to NULL. */
1108         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1109     } else {
1110         TRACE("Setting shader\n");
1111         if (MAX_SHADERS <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1112             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1113             hrc = D3DERR_INVALIDCALL;
1114         } else {
1115             IDirect3DVertexShader8Impl *shader = This->vShaders[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1116             hrc =  IWineD3DDevice_SetVertexShader(This->WineD3DDevice, 0 == shader ? NULL : shader->wineD3DVertexShader);
1117         }
1118     }
1119     TRACE("(%p) : returning hr(%lu)\n", This, hrc);
1120
1121     return hrc;
1122 }
1123
1124 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1125     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1126     IWineD3DVertexShader *pShader;
1127     HRESULT hrc = D3D_OK;
1128
1129     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1130     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
1131     if (D3D_OK == hrc) {
1132         if(0 != pShader) {
1133             DWORD i = 0;
1134             hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)ppShader);
1135             IWineD3DVertexShader_Release(pShader);
1136             while(This->vShaders[i] != (IDirect3DVertexShader8Impl *)ppShader && i < MAX_SHADERS) ++i;
1137             if (i < MAX_SHADERS) {
1138                 *ppShader = i + VS_HIGHESTFIXEDFXF + 1;
1139             } else {
1140                 WARN("(%p) : Couldn't find math for shader %p in d3d7 shadres list\n", This, (IDirect3DVertexShader8Impl *)ppShader);
1141                 *ppShader = 0;
1142             }
1143         } else {
1144             WARN("(%p) : The shader has been set to NULL\n", This);
1145
1146             /* TODO: Find out what should be returned, e.g. the FVF */
1147             *ppShader = 0;
1148             hrc = D3DERR_INVALIDCALL;
1149         }
1150     } else {
1151         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %lu (device %p)\n", This, hrc, This->WineD3DDevice);
1152     }
1153     TRACE("(%p) : returning %p\n", This, (IDirect3DVertexShader8 *)*ppShader);
1154
1155     return hrc;
1156 }
1157
1158 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1159     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1160     HRESULT hrc = D3D_OK;
1161     TRACE("(%p) Relay\n", This);
1162     if (pShader <= VS_HIGHESTFIXEDFXF) {
1163         WARN("(%p) : Caller passed a shader below the value of VS_HIGHESTFIXEDFXF\n", This);
1164         hrc = D3DERR_INVALIDCALL;
1165     } else if (MAX_SHADERS <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1166         FIXME("(%p) : Caller passed a shader greater than the maximum number of shaders\n", This);
1167         hrc = D3DERR_INVALIDCALL;
1168     } else {
1169         IDirect3DVertexShader8Impl *shader = This->vShaders[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1170         while(IUnknown_Release((IUnknown *)shader));
1171         This->vShaders[pShader - (VS_HIGHESTFIXEDFXF + 1)] = NULL;
1172         hrc = D3D_OK;
1173     }
1174
1175     return hrc;
1176 }
1177
1178 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1179     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1180     TRACE("(%p) : Relay\n", This);
1181
1182     return IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1183 }
1184
1185 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1186     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1187     TRACE("(%p) : Relay\n", This);
1188
1189     return IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1190 }
1191
1192 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1193     IDirect3DVertexShader8Impl *This = (IDirect3DVertexShader8Impl *)pVertexShader;
1194
1195     TRACE("(%p) : Relay\n", This);
1196 /*    return IWineD3DVertexShader_GetDeclaration(This->wineD3DVertexShader, pData, (UINT *)pSizeOfData); */
1197     return D3DERR_INVALIDCALL;
1198 }
1199 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1200     IDirect3DVertexShader8Impl *This = (IDirect3DVertexShader8Impl *)pVertexShader;
1201
1202     TRACE("(%p) : Relay\n", This);
1203     return IWineD3DVertexShader_GetFunction(This->wineD3DVertexShader, pData, (UINT *)pSizeOfData);
1204 }
1205
1206 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
1207     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1208     TRACE("(%p) Relay\n", This);
1209 /* FIXME: store base vertex index properly */
1210     This->baseVertexIndex = baseVertexIndex;
1211     return IWineD3DDevice_SetIndices(This->WineD3DDevice,
1212                                      NULL == pIndexData ? NULL : ((IDirect3DIndexBuffer8Impl *)pIndexData)->wineD3DIndexBuffer,
1213                                      0);
1214 }
1215
1216 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
1217     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1218     IWineD3DIndexBuffer *retIndexData = NULL;
1219     HRESULT rc = D3D_OK;
1220     UINT tmp;
1221
1222     TRACE("(%p) Relay\n", This);
1223
1224     if(ppIndexData == NULL){
1225         return D3DERR_INVALIDCALL;
1226     }
1227
1228     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData, &tmp);
1229     if (D3D_OK == rc && NULL != retIndexData) {
1230         IWineD3DVertexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1231         IWineD3DVertexBuffer_Release(retIndexData);
1232     } else {
1233         if(rc != D3D_OK)  FIXME("Call to GetIndices failed\n");
1234         *ppIndexData = NULL;
1235     }
1236 /* FIXME: store base vertex index properly */
1237     *pBaseVertexIndex = This->baseVertexIndex;
1238     return rc;
1239 }
1240 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
1241     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1242     IDirect3DPixelShader8Impl *object;
1243     HRESULT hrc = D3D_OK;
1244
1245     if (NULL == ppShader) {
1246         TRACE("(%p) Invalid call\n", This);
1247         return D3DERR_INVALIDCALL;
1248     }
1249     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1250
1251     if (NULL == object) {
1252         return E_OUTOFMEMORY;
1253     } else {
1254
1255         object->ref    = 1;
1256         object->lpVtbl = &Direct3DPixelShader8_Vtbl;
1257         hrc = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction, &object->wineD3DPixelShader , (IUnknown *)object);
1258         if (D3D_OK != hrc) {
1259             FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
1260             HeapFree(GetProcessHeap(), 0 , object);
1261             *ppShader = 0;
1262         } else {
1263             *ppShader = (DWORD)object;
1264         }
1265
1266     }
1267
1268     TRACE("(%p) : returning %p\n", This, (void *)*ppShader);
1269     return hrc;
1270 }
1271
1272 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1273     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1274     IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)pShader;
1275     TRACE("(%p) Relay\n", This);
1276
1277     return IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader == NULL ? NULL :shader->wineD3DPixelShader);
1278 }
1279
1280 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1281     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1282     IWineD3DPixelShader *object;
1283
1284     HRESULT hrc = D3D_OK;
1285     TRACE("(%p) Relay\n", This);
1286     if (NULL == ppShader) {
1287         TRACE("(%p) Invalid call\n", This);
1288         return D3DERR_INVALIDCALL;
1289     }
1290
1291     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
1292     if (D3D_OK == hrc && NULL != object) {
1293        hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)ppShader);
1294        IWineD3DPixelShader_Release(object);
1295     } else {
1296         *ppShader = (DWORD)NULL;
1297     }
1298
1299     TRACE("(%p) : returning %p\n", This, (void *)*ppShader);
1300     return hrc;
1301 }
1302
1303 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1304     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1305     IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)pShader;
1306     TRACE("(%p) Relay\n", This);
1307
1308     if (NULL != shader) {
1309         while(IUnknown_Release((IUnknown *)shader));
1310     }
1311
1312     return D3D_OK;
1313 }
1314
1315 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1316     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1317     TRACE("(%p) Relay\n", This);
1318
1319     return IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1320 }
1321
1322 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1323     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1324     TRACE("(%p) Relay\n", This);
1325
1326     return IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1327 }
1328
1329 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
1330     IDirect3DPixelShader8Impl *This = (IDirect3DPixelShader8Impl *)pPixelShader;
1331
1332     TRACE("(%p) : Relay\n", This);
1333     return IWineD3DPixelShader_GetFunction(This->wineD3DPixelShader, pData, (UINT *)pSizeOfData);
1334 }
1335
1336 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1337     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1338     TRACE("(%p) Relay\n", This);
1339
1340     return IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (WINED3DRECTPATCH_INFO *)pRectPatchInfo);
1341 }
1342
1343 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
1344     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1345     TRACE("(%p) Relay\n", This);
1346
1347     return IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (WINED3DTRIPATCH_INFO *)pTriPatchInfo);
1348 }
1349
1350 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
1351     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1352     TRACE("(%p) Relay\n", This);
1353
1354     return IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
1355 }
1356
1357 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
1358     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1359     TRACE("(%p) Relay\n" , This);
1360
1361     return IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1362                                           NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
1363                                           0/* Offset in bytes */, Stride);
1364 }
1365
1366 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
1367     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1368     IWineD3DVertexBuffer *retStream = NULL;
1369     HRESULT rc = D3D_OK;
1370
1371     TRACE("(%p) Relay\n" , This);
1372
1373     if(pStream == NULL){
1374         return D3DERR_INVALIDCALL;
1375     }
1376
1377     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, 0 /* Offset in bytes */, pStride);
1378     if (rc == D3D_OK  && NULL != retStream) {
1379         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
1380         IWineD3DVertexBuffer_Release(retStream);
1381     }else{
1382          FIXME("Call to GetStreamSource failed %p\n",  pStride);
1383         *pStream = NULL;
1384     }
1385
1386     return rc;
1387 }
1388
1389
1390 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
1391 {
1392     IDirect3DDevice8Impl_QueryInterface,
1393     IDirect3DDevice8Impl_AddRef,
1394     IDirect3DDevice8Impl_Release,
1395     IDirect3DDevice8Impl_TestCooperativeLevel,
1396     IDirect3DDevice8Impl_GetAvailableTextureMem,
1397     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
1398     IDirect3DDevice8Impl_GetDirect3D,
1399     IDirect3DDevice8Impl_GetDeviceCaps,
1400     IDirect3DDevice8Impl_GetDisplayMode,
1401     IDirect3DDevice8Impl_GetCreationParameters,
1402     IDirect3DDevice8Impl_SetCursorProperties,
1403     IDirect3DDevice8Impl_SetCursorPosition,
1404     IDirect3DDevice8Impl_ShowCursor,
1405     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
1406     IDirect3DDevice8Impl_Reset,
1407     IDirect3DDevice8Impl_Present,
1408     IDirect3DDevice8Impl_GetBackBuffer,
1409     IDirect3DDevice8Impl_GetRasterStatus,
1410     IDirect3DDevice8Impl_SetGammaRamp,
1411     IDirect3DDevice8Impl_GetGammaRamp,
1412     IDirect3DDevice8Impl_CreateTexture,
1413     IDirect3DDevice8Impl_CreateVolumeTexture,
1414     IDirect3DDevice8Impl_CreateCubeTexture,
1415     IDirect3DDevice8Impl_CreateVertexBuffer,
1416     IDirect3DDevice8Impl_CreateIndexBuffer,
1417     IDirect3DDevice8Impl_CreateRenderTarget,
1418     IDirect3DDevice8Impl_CreateDepthStencilSurface,
1419     IDirect3DDevice8Impl_CreateImageSurface,
1420     IDirect3DDevice8Impl_CopyRects,
1421     IDirect3DDevice8Impl_UpdateTexture,
1422     IDirect3DDevice8Impl_GetFrontBuffer,
1423     IDirect3DDevice8Impl_SetRenderTarget,
1424     IDirect3DDevice8Impl_GetRenderTarget,
1425     IDirect3DDevice8Impl_GetDepthStencilSurface,
1426     IDirect3DDevice8Impl_BeginScene,
1427     IDirect3DDevice8Impl_EndScene,
1428     IDirect3DDevice8Impl_Clear,
1429     IDirect3DDevice8Impl_SetTransform,
1430     IDirect3DDevice8Impl_GetTransform,
1431     IDirect3DDevice8Impl_MultiplyTransform,
1432     IDirect3DDevice8Impl_SetViewport,
1433     IDirect3DDevice8Impl_GetViewport,
1434     IDirect3DDevice8Impl_SetMaterial,
1435     IDirect3DDevice8Impl_GetMaterial,
1436     IDirect3DDevice8Impl_SetLight,
1437     IDirect3DDevice8Impl_GetLight,
1438     IDirect3DDevice8Impl_LightEnable,
1439     IDirect3DDevice8Impl_GetLightEnable,
1440     IDirect3DDevice8Impl_SetClipPlane,
1441     IDirect3DDevice8Impl_GetClipPlane,
1442     IDirect3DDevice8Impl_SetRenderState,
1443     IDirect3DDevice8Impl_GetRenderState,
1444     IDirect3DDevice8Impl_BeginStateBlock,
1445     IDirect3DDevice8Impl_EndStateBlock,
1446     IDirect3DDevice8Impl_ApplyStateBlock,
1447     IDirect3DDevice8Impl_CaptureStateBlock,
1448     IDirect3DDevice8Impl_DeleteStateBlock,
1449     IDirect3DDevice8Impl_CreateStateBlock,
1450     IDirect3DDevice8Impl_SetClipStatus,
1451     IDirect3DDevice8Impl_GetClipStatus,
1452     IDirect3DDevice8Impl_GetTexture,
1453     IDirect3DDevice8Impl_SetTexture,
1454     IDirect3DDevice8Impl_GetTextureStageState,
1455     IDirect3DDevice8Impl_SetTextureStageState,
1456     IDirect3DDevice8Impl_ValidateDevice,
1457     IDirect3DDevice8Impl_GetInfo,
1458     IDirect3DDevice8Impl_SetPaletteEntries,
1459     IDirect3DDevice8Impl_GetPaletteEntries,
1460     IDirect3DDevice8Impl_SetCurrentTexturePalette,
1461     IDirect3DDevice8Impl_GetCurrentTexturePalette,
1462     IDirect3DDevice8Impl_DrawPrimitive,
1463     IDirect3DDevice8Impl_DrawIndexedPrimitive,
1464     IDirect3DDevice8Impl_DrawPrimitiveUP,
1465     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
1466     IDirect3DDevice8Impl_ProcessVertices,
1467     IDirect3DDevice8Impl_CreateVertexShader,
1468     IDirect3DDevice8Impl_SetVertexShader,
1469     IDirect3DDevice8Impl_GetVertexShader,
1470     IDirect3DDevice8Impl_DeleteVertexShader,
1471     IDirect3DDevice8Impl_SetVertexShaderConstant,
1472     IDirect3DDevice8Impl_GetVertexShaderConstant,
1473     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
1474     IDirect3DDevice8Impl_GetVertexShaderFunction,
1475     IDirect3DDevice8Impl_SetStreamSource,
1476     IDirect3DDevice8Impl_GetStreamSource,
1477     IDirect3DDevice8Impl_SetIndices,
1478     IDirect3DDevice8Impl_GetIndices,
1479     IDirect3DDevice8Impl_CreatePixelShader,
1480     IDirect3DDevice8Impl_SetPixelShader,
1481     IDirect3DDevice8Impl_GetPixelShader,
1482     IDirect3DDevice8Impl_DeletePixelShader,
1483     IDirect3DDevice8Impl_SetPixelShaderConstant,
1484     IDirect3DDevice8Impl_GetPixelShaderConstant,
1485     IDirect3DDevice8Impl_GetPixelShaderFunction,
1486     IDirect3DDevice8Impl_DrawRectPatch,
1487     IDirect3DDevice8Impl_DrawTriPatch,
1488     IDirect3DDevice8Impl_DeletePatch
1489 };
1490
1491 /* Internal function called back during the CreateDevice to create a render target  */
1492 HRESULT WINAPI D3D8CB_CreateSurface(IUnknown *device, UINT Width, UINT Height, 
1493                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1494                                          IWineD3DSurface **ppSurface, HANDLE *pSharedHandle) {
1495
1496     HRESULT res = D3D_OK;
1497     IDirect3DSurface8Impl *d3dSurface = NULL;
1498     BOOL Lockable = TRUE;
1499
1500     if((WINED3DPOOL_DEFAULT == Pool && WINED3DUSAGE_DYNAMIC != Usage))
1501         Lockable = FALSE;
1502
1503     TRACE("relay\n");
1504     res = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)device, Width, Height, (D3DFORMAT)Format, Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface8 **)&d3dSurface, D3DRTYPE_SURFACE, Usage, Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1505
1506     if (res == D3D_OK) {
1507         *ppSurface = d3dSurface->wineD3DSurface;
1508     } else {
1509         FIXME("(%p) IDirect3DDevice8_CreateSurface failed\n", device);
1510     }
1511     return res;
1512 }