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