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