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