rpcrt4: Now that there's a process-wide pool of connections we don't
[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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
629     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
630     return IWineD3DDevice_BeginScene(This->WineD3DDevice);
631 }
632
633 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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       FIXME("(%p)  Failed to allocate %d bytes\n", This, sizeof(IDirect3DStateBlock8Impl));
829       *pToken = 0;
830       return E_OUTOFMEMORY;
831    }
832    object->lpVtbl = &Direct3DStateBlock8_Vtbl;
833    object->ref = 1;
834
835    hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown *)object);
836    if(D3D_OK != hrc){
837        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
838        HeapFree(GetProcessHeap(), 0, object);
839        *pToken = 0;
840    } else {
841        *pToken = (DWORD)object;
842    }
843    TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
844
845    return hrc;
846 }
847
848 HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
849     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
850     TRACE("(%p) Relay\n" , This);
851 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
852     return IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
853 }
854
855 HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
856     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
857     TRACE("(%p) Relay\n" , This);
858
859     return IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
860 }
861
862 HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
863     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
864     IWineD3DBaseTexture *retTexture = NULL;
865     HRESULT rc = D3D_OK;
866
867     TRACE("(%p) Relay\n" , This);
868
869     if(ppTexture == NULL){
870         return D3DERR_INVALIDCALL;
871     }
872
873     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
874     if (rc == D3D_OK && NULL != retTexture) {
875         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
876         IWineD3DBaseTexture_Release(retTexture);
877     } else {
878         FIXME("Call to get texture  (%ld) failed (%p)\n", Stage, retTexture);
879         *ppTexture = NULL;
880     }
881
882     return rc;
883 }
884
885 HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
886     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
887     TRACE("(%p) Relay %ld %p\n" , This, Stage, pTexture);
888
889     return IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
890                                      pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
891 }
892
893 HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
894     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
895     TRACE("(%p) Relay\n" , This);
896
897     switch(Type) {
898     case D3DTSS_ADDRESSU:
899         Type = WINED3DSAMP_ADDRESSU;
900         break;
901     case D3DTSS_ADDRESSV:
902         Type = WINED3DSAMP_ADDRESSV;
903         break;
904     case D3DTSS_ADDRESSW:
905         Type = WINED3DSAMP_ADDRESSW;
906         break;
907     case D3DTSS_BORDERCOLOR:
908         Type = WINED3DSAMP_BORDERCOLOR;
909         break;
910     case D3DTSS_MAGFILTER:
911         Type = WINED3DSAMP_MAGFILTER;
912         break;
913     case D3DTSS_MAXANISOTROPY:
914         Type = WINED3DSAMP_MAXANISOTROPY;
915         break;
916     case D3DTSS_MAXMIPLEVEL:
917         Type = WINED3DSAMP_MAXMIPLEVEL;
918         break;
919     case D3DTSS_MINFILTER:
920         Type = WINED3DSAMP_MINFILTER;
921         break;
922     case D3DTSS_MIPFILTER:
923         Type = WINED3DSAMP_MIPFILTER;
924         break;
925     case D3DTSS_MIPMAPLODBIAS:
926         Type = WINED3DSAMP_MIPMAPLODBIAS;
927         break;
928     default:
929         return IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
930     }
931
932     return IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, Type, pValue);
933 }
934
935 HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
936     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
937     TRACE("(%p) Relay\n" , This);
938
939     switch(Type) {
940     case D3DTSS_ADDRESSU:
941         Type = WINED3DSAMP_ADDRESSU;
942         break;
943     case D3DTSS_ADDRESSV:
944         Type = WINED3DSAMP_ADDRESSV;
945         break;
946     case D3DTSS_ADDRESSW:
947         Type = WINED3DSAMP_ADDRESSW;
948         break;
949     case D3DTSS_BORDERCOLOR:
950         Type = WINED3DSAMP_BORDERCOLOR;
951         break;
952     case D3DTSS_MAGFILTER:
953         Type = WINED3DSAMP_MAGFILTER;
954         break;
955     case D3DTSS_MAXANISOTROPY:
956         Type = WINED3DSAMP_MAXANISOTROPY;
957         break;
958     case D3DTSS_MAXMIPLEVEL:
959         Type = WINED3DSAMP_MAXMIPLEVEL;
960         break;
961     case D3DTSS_MINFILTER:
962         Type = WINED3DSAMP_MINFILTER;
963         break;
964     case D3DTSS_MIPFILTER:
965         Type = WINED3DSAMP_MIPFILTER;
966         break;
967     case D3DTSS_MIPMAPLODBIAS:
968         Type = WINED3DSAMP_MIPMAPLODBIAS;
969         break;
970     default:
971         return IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
972     }
973
974     return IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, Type, Value);
975 }
976
977 HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
978     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
979     TRACE("(%p) Relay\n" , This);
980
981     return IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
982 }
983
984 HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
985     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
986     FIXME("(%p) : stub\n", This);
987     return D3D_OK;
988 }
989
990 HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
991     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
992     TRACE("(%p) Relay\n" , This);
993
994     return IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
995 }
996
997 HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
998     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
999     TRACE("(%p) Relay\n" , This);
1000
1001     return IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1002 }
1003
1004 HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1005     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1006     TRACE("(%p) Relay\n" , This);
1007
1008     return IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1009 }
1010
1011 HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1012     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1013     TRACE("(%p) Relay\n" , This);
1014
1015     return IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1016 }
1017
1018 HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1019     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface; 
1020     TRACE("(%p) Relay\n" , This);
1021
1022     return IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1023 }
1024
1025 HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1026                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1027     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1028     TRACE("(%p) Relay\n" , This);
1029
1030     return IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, This->baseVertexIndex, MinVertexIndex, NumVertices, startIndex, primCount);
1031 }
1032
1033 HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1034     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1035     TRACE("(%p) Relay\n" , This);
1036
1037     return IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1038 }
1039
1040 HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1041                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1042                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1043                                                              UINT VertexStreamZeroStride) {
1044     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1045     TRACE("(%p) Relay\n" , This);
1046
1047     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1048                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1049 }
1050
1051 HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1052     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1053     TRACE("(%p) Relay\n" , This);
1054
1055     return IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer8Impl *)pDestBuffer)->wineD3DVertexBuffer, NULL, Flags);
1056 }
1057
1058 HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pDeclaration, CONST DWORD* pFunction, DWORD* ppShader, DWORD Usage) {
1059     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1060     HRESULT hrc = D3D_OK;
1061     IDirect3DVertexShader8Impl *object;
1062
1063     /* Setup a stub object for now */
1064     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1065     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1066     if (NULL == object) {
1067         FIXME("Allocation of memory failed\n");
1068         *ppShader = 0;
1069         return D3DERR_OUTOFVIDEOMEMORY;
1070     }
1071
1072     object->ref = 1;
1073     object->lpVtbl = &Direct3DVertexShader8_Vtbl;
1074     /* Usage is missing ..*/
1075     hrc = IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, pDeclaration, pFunction, &object->wineD3DVertexShader, (IUnknown *)object);
1076
1077     if (FAILED(hrc)) {
1078         /* free up object */
1079         FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
1080         HeapFree(GetProcessHeap(), 0, object);
1081         *ppShader = 0;
1082     } else {
1083         /* TODO: Store the VS declarations locally so that they can be derefferenced with a value higher than VS_HIGHESTFIXEDFXF */
1084         DWORD i = 0;
1085         while(This->vShaders[i] != NULL && i < MAX_SHADERS) ++i;
1086         if (MAX_SHADERS == i) {
1087             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1088             hrc = E_OUTOFMEMORY;
1089         } else {
1090             This->vShaders[i] = object;
1091             *ppShader = i + VS_HIGHESTFIXEDFXF + 1;
1092         }
1093     }
1094     TRACE("(%p) : returning %p\n", This, object);
1095
1096     return hrc;
1097 }
1098
1099 HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1100     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1101     HRESULT hrc = D3D_OK;
1102
1103     TRACE("(%p) : Relay\n", This);
1104     if (VS_HIGHESTFIXEDFXF >= pShader) {
1105         TRACE("Setting FVF, %d %ld\n", VS_HIGHESTFIXEDFXF, pShader);
1106         IWineD3DDevice_SetFVF(This->WineD3DDevice, pShader);
1107
1108         /* Call SetVertexShader with a NULL shader to set the vertexshader in the stateblock to NULL. */
1109         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1110     } else {
1111         TRACE("Setting shader\n");
1112         if (MAX_SHADERS <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1113             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1114             hrc = D3DERR_INVALIDCALL;
1115         } else {
1116             IDirect3DVertexShader8Impl *shader = This->vShaders[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1117             hrc =  IWineD3DDevice_SetVertexShader(This->WineD3DDevice, 0 == shader ? NULL : shader->wineD3DVertexShader);
1118         }
1119     }
1120     TRACE("(%p) : returning hr(%lu)\n", This, hrc);
1121
1122     return hrc;
1123 }
1124
1125 HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1126     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1127     IWineD3DVertexShader *pShader;
1128     HRESULT hrc = D3D_OK;
1129
1130     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1131     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
1132     if (D3D_OK == hrc) {
1133         if(0 != pShader) {
1134             DWORD i = 0;
1135             hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)ppShader);
1136             IWineD3DVertexShader_Release(pShader);
1137             while(This->vShaders[i] != (IDirect3DVertexShader8Impl *)ppShader && i < MAX_SHADERS) ++i;
1138             if (i < MAX_SHADERS) {
1139                 *ppShader = i + VS_HIGHESTFIXEDFXF + 1;
1140             } else {
1141                 WARN("(%p) : Couldn't find math for shader %p in d3d7 shadres list\n", This, (IDirect3DVertexShader8Impl *)ppShader);
1142                 *ppShader = 0;
1143             }
1144         } else {
1145             WARN("(%p) : The shader has been set to NULL\n", This);
1146
1147             /* TODO: Find out what should be returned, e.g. the FVF */
1148             *ppShader = 0;
1149             hrc = D3DERR_INVALIDCALL;
1150         }
1151     } else {
1152         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %lu (device %p)\n", This, hrc, This->WineD3DDevice);
1153     }
1154     TRACE("(%p) : returning %p\n", This, (IDirect3DVertexShader8 *)*ppShader);
1155
1156     return hrc;
1157 }
1158
1159 HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1160     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1161     HRESULT hrc = D3D_OK;
1162     TRACE("(%p) Relay\n", This);
1163     if (pShader <= VS_HIGHESTFIXEDFXF) {
1164         WARN("(%p) : Caller passed a shader below the value of VS_HIGHESTFIXEDFXF\n", This);
1165         hrc = D3DERR_INVALIDCALL;
1166     } else if (MAX_SHADERS <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1167         FIXME("(%p) : Caller passed a shader greater than the maximum number of shaders\n", This);
1168         hrc = D3DERR_INVALIDCALL;
1169     } else {
1170         IDirect3DVertexShader8Impl *shader = This->vShaders[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1171         while(IUnknown_Release((IUnknown *)shader));
1172         This->vShaders[pShader - (VS_HIGHESTFIXEDFXF + 1)] = NULL;
1173         hrc = D3D_OK;
1174     }
1175
1176     return hrc;
1177 }
1178
1179 HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1180     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1181     TRACE("(%p) : Relay\n", This);
1182
1183     return IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1184 }
1185
1186 HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1187     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1188     TRACE("(%p) : Relay\n", This);
1189
1190     return IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1191 }
1192
1193 HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1194     IDirect3DVertexShader8Impl *This = (IDirect3DVertexShader8Impl *)pVertexShader;
1195
1196     TRACE("(%p) : Relay\n", This);
1197 /*    return IWineD3DVertexShader_GetDeclaration(This->wineD3DVertexShader, pData, (UINT *)pSizeOfData); */
1198     return D3DERR_INVALIDCALL;
1199 }
1200 HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1201     IDirect3DVertexShader8Impl *This = (IDirect3DVertexShader8Impl *)pVertexShader;
1202
1203     TRACE("(%p) : Relay\n", This);
1204     return IWineD3DVertexShader_GetFunction(This->wineD3DVertexShader, pData, (UINT *)pSizeOfData);
1205 }
1206
1207 HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
1208     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1209     TRACE("(%p) Relay\n", This);
1210 /* FIXME: store base vertex index properly */
1211     This->baseVertexIndex = baseVertexIndex;
1212     return IWineD3DDevice_SetIndices(This->WineD3DDevice,
1213                                      NULL == pIndexData ? NULL : ((IDirect3DIndexBuffer8Impl *)pIndexData)->wineD3DIndexBuffer,
1214                                      0);
1215 }
1216
1217 HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
1218     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1219     IWineD3DIndexBuffer *retIndexData = NULL;
1220     HRESULT rc = D3D_OK;
1221     UINT tmp;
1222
1223     TRACE("(%p) Relay\n", This);
1224
1225     if(ppIndexData == NULL){
1226         return D3DERR_INVALIDCALL;
1227     }
1228
1229     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData, &tmp);
1230     if (D3D_OK == rc && NULL != retIndexData) {
1231         IWineD3DVertexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1232         IWineD3DVertexBuffer_Release(retIndexData);
1233     } else {
1234         if(rc != D3D_OK)  FIXME("Call to GetIndices failed\n");
1235         *ppIndexData = NULL;
1236     }
1237 /* FIXME: store base vertex index properly */
1238     *pBaseVertexIndex = This->baseVertexIndex;
1239     return rc;
1240 }
1241 HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
1242     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1243     IDirect3DPixelShader8Impl *object;
1244     HRESULT hrc = D3D_OK;
1245
1246     if (NULL == ppShader) {
1247         TRACE("(%p) Invalid call\n", This);
1248         return D3DERR_INVALIDCALL;
1249     }
1250     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1251
1252     if (NULL == object) {
1253         return E_OUTOFMEMORY;
1254     } else {
1255
1256         object->ref    = 1;
1257         object->lpVtbl = &Direct3DPixelShader8_Vtbl;
1258         hrc = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction, &object->wineD3DPixelShader , (IUnknown *)object);
1259         if (D3D_OK != hrc) {
1260             FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
1261             HeapFree(GetProcessHeap(), 0 , object);
1262             *ppShader = 0;
1263         } else {
1264             *ppShader = (DWORD)object;
1265         }
1266
1267     }
1268
1269     TRACE("(%p) : returning %p\n", This, (void *)*ppShader);
1270     return hrc;
1271 }
1272
1273 HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1274     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1275     IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)pShader;
1276     TRACE("(%p) Relay\n", This);
1277
1278     return IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader == NULL ? NULL :shader->wineD3DPixelShader);
1279 }
1280
1281 HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1282     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1283     IWineD3DPixelShader *object;
1284
1285     HRESULT hrc = D3D_OK;
1286     TRACE("(%p) Relay\n", This);
1287     if (NULL == ppShader) {
1288         TRACE("(%p) Invalid call\n", This);
1289         return D3DERR_INVALIDCALL;
1290     }
1291
1292     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
1293     if (D3D_OK == hrc && NULL != object) {
1294        hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)ppShader);
1295        IWineD3DPixelShader_Release(object);
1296     } else {
1297         *ppShader = (DWORD)NULL;
1298     }
1299
1300     TRACE("(%p) : returning %p\n", This, (void *)*ppShader);
1301     return hrc;
1302 }
1303
1304 HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1305     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1306     IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)pShader;
1307     TRACE("(%p) Relay\n", This);
1308
1309     if (NULL != shader) {
1310         while(IUnknown_Release((IUnknown *)shader));
1311     }
1312
1313     return D3D_OK;
1314 }
1315
1316 HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1317     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1318     TRACE("(%p) Relay\n", This);
1319
1320     return IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1321 }
1322
1323 HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1324     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1325     TRACE("(%p) Relay\n", This);
1326
1327     return IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1328 }
1329
1330 HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
1331     IDirect3DPixelShader8Impl *This = (IDirect3DPixelShader8Impl *)pPixelShader;
1332
1333     TRACE("(%p) : Relay\n", This);
1334     return IWineD3DPixelShader_GetFunction(This->wineD3DPixelShader, pData, (UINT *)pSizeOfData);
1335 }
1336
1337 HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1338     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1339     TRACE("(%p) Relay\n", This);
1340
1341     return IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (WINED3DRECTPATCH_INFO *)pRectPatchInfo);
1342 }
1343
1344 HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
1345     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1346     TRACE("(%p) Relay\n", This);
1347
1348     return IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (WINED3DTRIPATCH_INFO *)pTriPatchInfo);
1349 }
1350
1351 HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
1352     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1353     TRACE("(%p) Relay\n", This);
1354
1355     return IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
1356 }
1357
1358 HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
1359     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1360     TRACE("(%p) Relay\n" , This);
1361
1362     return IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1363                                           NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
1364                                           0/* Offset in bytes */, Stride);
1365 }
1366
1367 HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
1368     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1369     IWineD3DVertexBuffer *retStream = NULL;
1370     HRESULT rc = D3D_OK;
1371
1372     TRACE("(%p) Relay\n" , This);
1373
1374     if(pStream == NULL){
1375         return D3DERR_INVALIDCALL;
1376     }
1377
1378     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, 0 /* Offset in bytes */, pStride);
1379     if (rc == D3D_OK  && NULL != retStream) {
1380         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
1381         IWineD3DVertexBuffer_Release(retStream);
1382     }else{
1383          FIXME("Call to GetStreamSource failed %p\n",  pStride);
1384         *pStream = NULL;
1385     }
1386
1387     return rc;
1388 }
1389
1390
1391 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
1392 {
1393     IDirect3DDevice8Impl_QueryInterface,
1394     IDirect3DDevice8Impl_AddRef,
1395     IDirect3DDevice8Impl_Release,
1396     IDirect3DDevice8Impl_TestCooperativeLevel,
1397     IDirect3DDevice8Impl_GetAvailableTextureMem,
1398     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
1399     IDirect3DDevice8Impl_GetDirect3D,
1400     IDirect3DDevice8Impl_GetDeviceCaps,
1401     IDirect3DDevice8Impl_GetDisplayMode,
1402     IDirect3DDevice8Impl_GetCreationParameters,
1403     IDirect3DDevice8Impl_SetCursorProperties,
1404     IDirect3DDevice8Impl_SetCursorPosition,
1405     IDirect3DDevice8Impl_ShowCursor,
1406     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
1407     IDirect3DDevice8Impl_Reset,
1408     IDirect3DDevice8Impl_Present,
1409     IDirect3DDevice8Impl_GetBackBuffer,
1410     IDirect3DDevice8Impl_GetRasterStatus,
1411     IDirect3DDevice8Impl_SetGammaRamp,
1412     IDirect3DDevice8Impl_GetGammaRamp,
1413     IDirect3DDevice8Impl_CreateTexture,
1414     IDirect3DDevice8Impl_CreateVolumeTexture,
1415     IDirect3DDevice8Impl_CreateCubeTexture,
1416     IDirect3DDevice8Impl_CreateVertexBuffer,
1417     IDirect3DDevice8Impl_CreateIndexBuffer,
1418     IDirect3DDevice8Impl_CreateRenderTarget,
1419     IDirect3DDevice8Impl_CreateDepthStencilSurface,
1420     IDirect3DDevice8Impl_CreateImageSurface,
1421     IDirect3DDevice8Impl_CopyRects,
1422     IDirect3DDevice8Impl_UpdateTexture,
1423     IDirect3DDevice8Impl_GetFrontBuffer,
1424     IDirect3DDevice8Impl_SetRenderTarget,
1425     IDirect3DDevice8Impl_GetRenderTarget,
1426     IDirect3DDevice8Impl_GetDepthStencilSurface,
1427     IDirect3DDevice8Impl_BeginScene,
1428     IDirect3DDevice8Impl_EndScene,
1429     IDirect3DDevice8Impl_Clear,
1430     IDirect3DDevice8Impl_SetTransform,
1431     IDirect3DDevice8Impl_GetTransform,
1432     IDirect3DDevice8Impl_MultiplyTransform,
1433     IDirect3DDevice8Impl_SetViewport,
1434     IDirect3DDevice8Impl_GetViewport,
1435     IDirect3DDevice8Impl_SetMaterial,
1436     IDirect3DDevice8Impl_GetMaterial,
1437     IDirect3DDevice8Impl_SetLight,
1438     IDirect3DDevice8Impl_GetLight,
1439     IDirect3DDevice8Impl_LightEnable,
1440     IDirect3DDevice8Impl_GetLightEnable,
1441     IDirect3DDevice8Impl_SetClipPlane,
1442     IDirect3DDevice8Impl_GetClipPlane,
1443     IDirect3DDevice8Impl_SetRenderState,
1444     IDirect3DDevice8Impl_GetRenderState,
1445     IDirect3DDevice8Impl_BeginStateBlock,
1446     IDirect3DDevice8Impl_EndStateBlock,
1447     IDirect3DDevice8Impl_ApplyStateBlock,
1448     IDirect3DDevice8Impl_CaptureStateBlock,
1449     IDirect3DDevice8Impl_DeleteStateBlock,
1450     IDirect3DDevice8Impl_CreateStateBlock,
1451     IDirect3DDevice8Impl_SetClipStatus,
1452     IDirect3DDevice8Impl_GetClipStatus,
1453     IDirect3DDevice8Impl_GetTexture,
1454     IDirect3DDevice8Impl_SetTexture,
1455     IDirect3DDevice8Impl_GetTextureStageState,
1456     IDirect3DDevice8Impl_SetTextureStageState,
1457     IDirect3DDevice8Impl_ValidateDevice,
1458     IDirect3DDevice8Impl_GetInfo,
1459     IDirect3DDevice8Impl_SetPaletteEntries,
1460     IDirect3DDevice8Impl_GetPaletteEntries,
1461     IDirect3DDevice8Impl_SetCurrentTexturePalette,
1462     IDirect3DDevice8Impl_GetCurrentTexturePalette,
1463     IDirect3DDevice8Impl_DrawPrimitive,
1464     IDirect3DDevice8Impl_DrawIndexedPrimitive,
1465     IDirect3DDevice8Impl_DrawPrimitiveUP,
1466     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
1467     IDirect3DDevice8Impl_ProcessVertices,
1468     IDirect3DDevice8Impl_CreateVertexShader,
1469     IDirect3DDevice8Impl_SetVertexShader,
1470     IDirect3DDevice8Impl_GetVertexShader,
1471     IDirect3DDevice8Impl_DeleteVertexShader,
1472     IDirect3DDevice8Impl_SetVertexShaderConstant,
1473     IDirect3DDevice8Impl_GetVertexShaderConstant,
1474     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
1475     IDirect3DDevice8Impl_GetVertexShaderFunction,
1476     IDirect3DDevice8Impl_SetStreamSource,
1477     IDirect3DDevice8Impl_GetStreamSource,
1478     IDirect3DDevice8Impl_SetIndices,
1479     IDirect3DDevice8Impl_GetIndices,
1480     IDirect3DDevice8Impl_CreatePixelShader,
1481     IDirect3DDevice8Impl_SetPixelShader,
1482     IDirect3DDevice8Impl_GetPixelShader,
1483     IDirect3DDevice8Impl_DeletePixelShader,
1484     IDirect3DDevice8Impl_SetPixelShaderConstant,
1485     IDirect3DDevice8Impl_GetPixelShaderConstant,
1486     IDirect3DDevice8Impl_GetPixelShaderFunction,
1487     IDirect3DDevice8Impl_DrawRectPatch,
1488     IDirect3DDevice8Impl_DrawTriPatch,
1489     IDirect3DDevice8Impl_DeletePatch
1490 };
1491
1492 /* Internal function called back during the CreateDevice to create a render target  */
1493 HRESULT WINAPI D3D8CB_CreateSurface(IUnknown *device, UINT Width, UINT Height, 
1494                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1495                                          IWineD3DSurface **ppSurface, HANDLE *pSharedHandle) {
1496
1497     HRESULT res = D3D_OK;
1498     IDirect3DSurface8Impl *d3dSurface = NULL;
1499     BOOL Lockable = TRUE;
1500
1501     if((WINED3DPOOL_DEFAULT == Pool && WINED3DUSAGE_DYNAMIC != Usage))
1502         Lockable = FALSE;
1503
1504     TRACE("relay\n");
1505     res = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)device, Width, Height, (D3DFORMAT)Format, Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface8 **)&d3dSurface, D3DRTYPE_SURFACE, Usage, Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1506
1507     if (res == D3D_OK) {
1508         *ppSurface = d3dSurface->wineD3DSurface;
1509     } else {
1510         FIXME("(%p) IDirect3DDevice8_CreateSurface failed\n", device);
1511     }
1512     return res;
1513 }