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