quartz: Exclude unused headers.
[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     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     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     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, WINEDDBLTFAST_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], WINEDDBLTFAST_NOCOLORKEY);
671             }
672         } else {
673             for (i = 0; i < cRects; ++i) {
674                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, (RECT *) &pSourceRects[i], WINEDDBLTFAST_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     TRACE("(%p) Relay\n" , This);
762
763     return IWineD3DDevice_BeginScene(This->WineD3DDevice);
764 }
765
766 static HRESULT WINAPI IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
767     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
768     TRACE("(%p) Relay\n" , This);
769
770     return IWineD3DDevice_EndScene(This->WineD3DDevice);
771 }
772
773 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
774     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
775     TRACE("(%p) Relay\n" , This);
776
777     /* Note: D3DRECT is compatible with WINED3DRECT */
778     return IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
779 }
780
781 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
782     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
783     TRACE("(%p) Relay\n" , This);
784
785     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
786     return IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
787 }
788
789 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
790     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
791     TRACE("(%p) Relay\n" , This);
792
793     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
794     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
795 }
796
797 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
798     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
799     TRACE("(%p) Relay\n" , This);
800
801     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
802     return IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
803 }
804
805 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
806     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
807     TRACE("(%p) Relay\n" , This);
808
809     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
810     return IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
811 }
812
813 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
814     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
815     TRACE("(%p) Relay\n" , This);
816
817     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
818     return IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
819 }
820
821 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
822     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
823     TRACE("(%p) Relay\n" , This);
824
825     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
826     return IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
827 }
828
829 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
830     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
831     TRACE("(%p) Relay\n" , This);
832
833     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
834     return IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
835 }
836
837 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
838     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
839     TRACE("(%p) Relay\n" , This);
840  
841     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
842     return IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
843 }
844
845 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
846     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
847     TRACE("(%p) Relay\n" , This);
848
849     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
850     return IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
851 }
852
853 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
854     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
855     TRACE("(%p) Relay\n" , This);
856
857     return IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
858 }
859
860 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
861     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
862     TRACE("(%p) Relay\n" , This);
863
864     return IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
865 }
866
867 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
868     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
869     TRACE("(%p) Relay\n" , This);
870
871     return IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
872 }
873
874 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
875     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
876     TRACE("(%p) Relay\n" , This);
877
878     return IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
879 }
880
881 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
882     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
883     TRACE("(%p) Relay\n" , This);
884
885     return IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
886 }
887
888 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
889     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
890     TRACE("(%p) Relay\n" , This);
891
892     return IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
893 }
894
895 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
896   IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
897
898   TRACE("(%p)\n", This);
899
900   return IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
901 }
902
903 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
904     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
905     HRESULT hr;
906     IWineD3DStateBlock* wineD3DStateBlock;
907     IDirect3DStateBlock8Impl* object;
908
909     TRACE("(%p) Relay\n", This);
910
911     /* Tell wineD3D to endstatablock before anything else (in case we run out
912      * of memory later and cause locking problems)
913      */
914     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
915     if (hr != D3D_OK) {
916        FIXME("IWineD3DDevice_EndStateBlock returned an error\n");
917        return hr;
918     }
919
920     /* allocate a new IDirectD3DStateBlock */
921     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
922     object->ref = 1;
923     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
924
925     object->wineD3DStateBlock = wineD3DStateBlock;
926
927     *pToken = (DWORD)object;
928     TRACE("(%p)Returning %p %p\n", This, object, wineD3DStateBlock);
929
930     return hr;
931 }
932
933 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
934     IDirect3DStateBlock8Impl *pSB  = (IDirect3DStateBlock8Impl*) Token;
935     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
936
937     TRACE("(%p) %p Relay\n", This, pSB);
938
939     return  IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
940 }
941
942 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
943     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
944     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
945
946     TRACE("(%p) %p Relay\n", This, pSB);
947
948     return IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
949 }
950
951 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
952     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
953     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
954
955     TRACE("(%p) Relay\n", This);
956
957     while(IUnknown_Release((IUnknown *)pSB));
958
959     return D3D_OK;
960 }
961
962 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(LPDIRECT3DDEVICE8 iface, D3DSTATEBLOCKTYPE Type, DWORD* pToken) {
963    IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
964    IDirect3DStateBlock8Impl *object;
965    HRESULT hrc = D3D_OK;
966
967    TRACE("(%p) Relay\n", This);
968
969    object  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
970    if (NULL == object) {
971       *pToken = 0;
972       return E_OUTOFMEMORY;
973    }
974    object->lpVtbl = &Direct3DStateBlock8_Vtbl;
975    object->ref = 1;
976
977    hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown *)object);
978    if(D3D_OK != hrc){
979        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
980        HeapFree(GetProcessHeap(), 0, object);
981        *pToken = 0;
982    } else {
983        *pToken = (DWORD)object;
984    }
985    TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
986
987    return hrc;
988 }
989
990 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
991     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
992     TRACE("(%p) Relay\n" , This);
993 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
994     return IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
995 }
996
997 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
998     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
999     TRACE("(%p) Relay\n" , This);
1000
1001     return IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1002 }
1003
1004 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
1005     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1006     IWineD3DBaseTexture *retTexture = NULL;
1007     HRESULT rc = D3D_OK;
1008
1009     TRACE("(%p) Relay\n" , This);
1010
1011     if(ppTexture == NULL){
1012         return D3DERR_INVALIDCALL;
1013     }
1014
1015     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
1016     if (rc == D3D_OK && NULL != retTexture) {
1017         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1018         IWineD3DBaseTexture_Release(retTexture);
1019     } else {
1020         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1021         *ppTexture = NULL;
1022     }
1023
1024     return rc;
1025 }
1026
1027 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1028     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1029     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1030
1031     return IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1032                                      pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1033 }
1034
1035 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1036     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1037     TRACE("(%p) Relay\n" , This);
1038
1039     switch(Type) {
1040     case D3DTSS_ADDRESSU:
1041         Type = WINED3DSAMP_ADDRESSU;
1042         break;
1043     case D3DTSS_ADDRESSV:
1044         Type = WINED3DSAMP_ADDRESSV;
1045         break;
1046     case D3DTSS_ADDRESSW:
1047         Type = WINED3DSAMP_ADDRESSW;
1048         break;
1049     case D3DTSS_BORDERCOLOR:
1050         Type = WINED3DSAMP_BORDERCOLOR;
1051         break;
1052     case D3DTSS_MAGFILTER:
1053         Type = WINED3DSAMP_MAGFILTER;
1054         break;
1055     case D3DTSS_MAXANISOTROPY:
1056         Type = WINED3DSAMP_MAXANISOTROPY;
1057         break;
1058     case D3DTSS_MAXMIPLEVEL:
1059         Type = WINED3DSAMP_MAXMIPLEVEL;
1060         break;
1061     case D3DTSS_MINFILTER:
1062         Type = WINED3DSAMP_MINFILTER;
1063         break;
1064     case D3DTSS_MIPFILTER:
1065         Type = WINED3DSAMP_MIPFILTER;
1066         break;
1067     case D3DTSS_MIPMAPLODBIAS:
1068         Type = WINED3DSAMP_MIPMAPLODBIAS;
1069         break;
1070     default:
1071         return IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
1072     }
1073
1074     return IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, Type, pValue);
1075 }
1076
1077 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1078     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1079     TRACE("(%p) Relay\n" , This);
1080
1081     switch(Type) {
1082     case D3DTSS_ADDRESSU:
1083         Type = WINED3DSAMP_ADDRESSU;
1084         break;
1085     case D3DTSS_ADDRESSV:
1086         Type = WINED3DSAMP_ADDRESSV;
1087         break;
1088     case D3DTSS_ADDRESSW:
1089         Type = WINED3DSAMP_ADDRESSW;
1090         break;
1091     case D3DTSS_BORDERCOLOR:
1092         Type = WINED3DSAMP_BORDERCOLOR;
1093         break;
1094     case D3DTSS_MAGFILTER:
1095         Type = WINED3DSAMP_MAGFILTER;
1096         break;
1097     case D3DTSS_MAXANISOTROPY:
1098         Type = WINED3DSAMP_MAXANISOTROPY;
1099         break;
1100     case D3DTSS_MAXMIPLEVEL:
1101         Type = WINED3DSAMP_MAXMIPLEVEL;
1102         break;
1103     case D3DTSS_MINFILTER:
1104         Type = WINED3DSAMP_MINFILTER;
1105         break;
1106     case D3DTSS_MIPFILTER:
1107         Type = WINED3DSAMP_MIPFILTER;
1108         break;
1109     case D3DTSS_MIPMAPLODBIAS:
1110         Type = WINED3DSAMP_MIPMAPLODBIAS;
1111         break;
1112     default:
1113         return IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
1114     }
1115
1116     return IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, Type, Value);
1117 }
1118
1119 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1120     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1121     TRACE("(%p) Relay\n" , This);
1122
1123     return IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1124 }
1125
1126 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1127     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1128     FIXME("(%p) : stub\n", This);
1129     return D3D_OK;
1130 }
1131
1132 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1133     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1134     TRACE("(%p) Relay\n" , This);
1135
1136     return IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1137 }
1138
1139 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1140     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1141     TRACE("(%p) Relay\n" , This);
1142
1143     return IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1144 }
1145
1146 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1147     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1148     TRACE("(%p) Relay\n" , This);
1149
1150     return IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1151 }
1152
1153 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1154     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1155     TRACE("(%p) Relay\n" , This);
1156
1157     return IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1158 }
1159
1160 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1161     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface; 
1162     TRACE("(%p) Relay\n" , This);
1163
1164     return IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1165 }
1166
1167 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1168                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1169     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1170     TRACE("(%p) Relay\n" , This);
1171
1172     return IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1173 }
1174
1175 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1176     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1177     TRACE("(%p) Relay\n" , This);
1178
1179     return IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1180 }
1181
1182 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1183                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1184                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1185                                                              UINT VertexStreamZeroStride) {
1186     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1187     TRACE("(%p) Relay\n" , This);
1188
1189     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1190                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1191 }
1192
1193 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1194     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1195     TRACE("(%p) Relay\n" , This);
1196
1197     return IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer8Impl *)pDestBuffer)->wineD3DVertexBuffer, NULL, Flags);
1198 }
1199
1200 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexDeclaration(IDirect3DDevice8 *iface, CONST DWORD *declaration, IDirect3DVertexDeclaration8 **decl_ptr) {
1201     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1202     IDirect3DVertexDeclaration8Impl *object;
1203     WINED3DVERTEXELEMENT *wined3d_elements;
1204     size_t wined3d_element_count;
1205     HRESULT hr = D3D_OK;
1206
1207     TRACE("(%p) : declaration %p\n", This, declaration);
1208
1209     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1210     if (!object) {
1211         ERR("Memory allocation failed\n");
1212         *decl_ptr = NULL;
1213         return D3DERR_OUTOFVIDEOMEMORY;
1214     }
1215
1216     object->ref_count = 1;
1217     object->lpVtbl = &Direct3DVertexDeclaration8_Vtbl;
1218
1219     wined3d_element_count = convert_to_wined3d_declaration(declaration, &object->elements_size, &wined3d_elements);
1220     object->elements = HeapAlloc(GetProcessHeap(), 0, object->elements_size);
1221     if (!object->elements) {
1222         ERR("Memory allocation failed\n");
1223         HeapFree(GetProcessHeap(), 0, wined3d_elements);
1224         HeapFree(GetProcessHeap(), 0, object);
1225         *decl_ptr = NULL;
1226         return D3DERR_OUTOFVIDEOMEMORY;
1227     }
1228
1229     CopyMemory(object->elements, declaration, object->elements_size);
1230
1231     hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wined3d_vertex_declaration,
1232             (IUnknown *)object, wined3d_elements, wined3d_element_count);
1233     HeapFree(GetProcessHeap(), 0, wined3d_elements);
1234
1235     if (FAILED(hr)) {
1236         ERR("(%p) : IWineD3DDevice_CreateVertexDeclaration call failed\n", This);
1237         HeapFree(GetProcessHeap(), 0, object->elements);
1238         HeapFree(GetProcessHeap(), 0, object);
1239     } else {
1240         *decl_ptr = (IDirect3DVertexDeclaration8 *)object;
1241         TRACE("(%p) : Created vertex declaration %p\n", This, object);
1242     }
1243
1244     return hr;
1245 }
1246
1247 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pDeclaration, CONST DWORD* pFunction, DWORD* ppShader, DWORD Usage) {
1248     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1249     HRESULT hrc = D3D_OK;
1250     IDirect3DVertexShader8Impl *object;
1251     IWineD3DVertexDeclaration *wined3d_vertex_declaration;
1252
1253     /* Setup a stub object for now */
1254     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1255     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1256     if (NULL == object) {
1257         FIXME("Allocation of memory failed\n");
1258         *ppShader = 0;
1259         return D3DERR_OUTOFVIDEOMEMORY;
1260     }
1261
1262     object->ref = 1;
1263     object->lpVtbl = &Direct3DVertexShader8_Vtbl;
1264
1265     hrc = IDirect3DDevice8Impl_CreateVertexDeclaration(iface, pDeclaration, &object->vertex_declaration);
1266     if (FAILED(hrc)) {
1267         ERR("(%p) : IDirect3DDeviceImpl_CreateVertexDeclaration call failed\n", This);
1268         HeapFree(GetProcessHeap(), 0, object);
1269         *ppShader = 0;
1270         return D3DERR_INVALIDCALL;
1271     }
1272     wined3d_vertex_declaration = ((IDirect3DVertexDeclaration8Impl *)object->vertex_declaration)->wined3d_vertex_declaration;
1273
1274     /* Usage is missing ... Use SetRenderState to set the sw vp render state in SetVertexShader */
1275     hrc = IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, wined3d_vertex_declaration, pFunction, &object->wineD3DVertexShader, (IUnknown *)object);
1276
1277     if (FAILED(hrc)) {
1278         /* free up object */
1279         FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
1280         HeapFree(GetProcessHeap(), 0, object);
1281         *ppShader = 0;
1282     } else {
1283         /* TODO: Store the VS declarations locally so that they can be derefferenced with a value higher than VS_HIGHESTFIXEDFXF */
1284         shader_handle *handle = alloc_shader_handle(This);
1285         if (!handle) {
1286             ERR("Failed to allocate shader handle\n");
1287             IDirect3DVertexShader8_Release((IUnknown *)object);
1288             hrc = E_OUTOFMEMORY;
1289         } else {
1290             object->handle = handle;
1291             *handle = object;
1292             *ppShader = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1293
1294             load_local_constants(pDeclaration, object->wineD3DVertexShader);
1295         }
1296     }
1297     TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
1298
1299     return hrc;
1300 }
1301
1302 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1303     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1304     HRESULT hrc = D3D_OK;
1305
1306     TRACE("(%p) : Relay\n", This);
1307     if (VS_HIGHESTFIXEDFXF >= pShader) {
1308         TRACE("Setting FVF, %d %d\n", VS_HIGHESTFIXEDFXF, pShader);
1309         IWineD3DDevice_SetFVF(This->WineD3DDevice, pShader);
1310
1311         /* Call SetVertexShader with a NULL shader to set the vertexshader in the stateblock to NULL. */
1312         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, NULL);
1313         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1314     } else {
1315         TRACE("Setting shader\n");
1316         if (This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1317             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1318             hrc = D3DERR_INVALIDCALL;
1319         } else {
1320             IDirect3DVertexShader8Impl *shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1321             IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1322                     shader ? ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration : NULL);
1323             hrc = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, 0 == shader ? NULL : shader->wineD3DVertexShader);
1324         }
1325     }
1326     TRACE("(%p) : returning hr(%u)\n", This, hrc);
1327
1328     return hrc;
1329 }
1330
1331 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1332     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1333     IWineD3DVertexShader *pShader;
1334     HRESULT hrc = D3D_OK;
1335
1336     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1337     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
1338     if (D3D_OK == hrc) {
1339         if(0 != pShader) {
1340             IDirect3DVertexShader8Impl *d3d8_shader;
1341             hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)&d3d8_shader);
1342             IWineD3DVertexShader_Release(pShader);
1343             *ppShader = (d3d8_shader->handle - This->shader_handles) + (VS_HIGHESTFIXEDFXF + 1);
1344         } else {
1345             *ppShader = 0;
1346             hrc = D3D_OK;
1347         }
1348     } else {
1349         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %u (device %p)\n", This, hrc, This->WineD3DDevice);
1350     }
1351     TRACE("(%p) : returning %#x\n", This, *ppShader);
1352
1353     return hrc;
1354 }
1355
1356 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1357     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1358
1359     TRACE("(%p) : pShader %#x\n", This, pShader);
1360
1361     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1362         ERR("(%p) : Trying to delete an invalid handle\n", This);
1363         return D3DERR_INVALIDCALL;
1364     } else {
1365         IWineD3DVertexShader *cur = NULL;
1366         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1367         IDirect3DVertexShader8Impl *shader = *handle;
1368
1369         IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
1370         if(cur) {
1371             if(cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
1372             IWineD3DVertexShader_Release(cur);
1373         }
1374
1375         while(IUnknown_Release((IUnknown *)shader));
1376         free_shader_handle(This, handle);
1377     }
1378
1379     return D3D_OK;
1380 }
1381
1382 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1383     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1384     TRACE("(%p) : Relay\n", This);
1385
1386     return IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1387 }
1388
1389 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1390     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1391     TRACE("(%p) : Relay\n", This);
1392
1393     return IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1394 }
1395
1396 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1397     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1398     IDirect3DVertexDeclaration8Impl *declaration;
1399     IDirect3DVertexShader8Impl *shader = NULL;
1400
1401     TRACE("(%p) : pVertexShader 0x%08x, pData %p, *pSizeOfData %u\n", This, pVertexShader, pData, *pSizeOfData);
1402
1403     if (pVertexShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pVertexShader - (VS_HIGHESTFIXEDFXF + 1)) {
1404         ERR("Passed an invalid shader handle.\n");
1405         return D3DERR_INVALIDCALL;
1406     }
1407
1408     shader = This->shader_handles[pVertexShader - (VS_HIGHESTFIXEDFXF + 1)];
1409     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
1410
1411     /* If pData is NULL, we just return the required size of the buffer. */
1412     if (!pData) {
1413         *pSizeOfData = declaration->elements_size;
1414         return D3D_OK;
1415     }
1416
1417     /* MSDN claims that if *pSizeOfData is smaller than the required size
1418      * we should write the required size and return D3DERR_MOREDATA.
1419      * That's not actually true. */
1420     if (*pSizeOfData < declaration->elements_size) {
1421         return D3DERR_INVALIDCALL;
1422     }
1423
1424     CopyMemory(pData, declaration->elements, declaration->elements_size);
1425
1426     return D3D_OK;
1427 }
1428
1429 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1430     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1431     IDirect3DVertexShader8Impl *shader = NULL;
1432
1433     TRACE("(%p) : pVertexShader %#x, pData %p, pSizeOfData %p\n", This, pVertexShader, pData, pSizeOfData);
1434
1435     if (pVertexShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pVertexShader - (VS_HIGHESTFIXEDFXF + 1)) {
1436         ERR("Passed an invalid shader handle.\n");
1437         return D3DERR_INVALIDCALL;
1438     }
1439
1440     shader = This->shader_handles[pVertexShader - (VS_HIGHESTFIXEDFXF + 1)];
1441     return IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, (UINT *)pSizeOfData);
1442 }
1443
1444 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
1445     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1446     TRACE("(%p) Relay\n", This);
1447     return IWineD3DDevice_SetIndices(This->WineD3DDevice,
1448                                      NULL == pIndexData ? NULL : ((IDirect3DIndexBuffer8Impl *)pIndexData)->wineD3DIndexBuffer,
1449                                      baseVertexIndex);
1450 }
1451
1452 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
1453     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1454     IWineD3DIndexBuffer *retIndexData = NULL;
1455     HRESULT rc = D3D_OK;
1456
1457     TRACE("(%p) Relay\n", This);
1458
1459     if(ppIndexData == NULL){
1460         return D3DERR_INVALIDCALL;
1461     }
1462
1463     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData, pBaseVertexIndex);
1464     if (D3D_OK == rc && NULL != retIndexData) {
1465         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1466         IWineD3DIndexBuffer_Release(retIndexData);
1467     } else {
1468         if(rc != D3D_OK)  FIXME("Call to GetIndices failed\n");
1469         *ppIndexData = NULL;
1470     }
1471     return rc;
1472 }
1473 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
1474     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1475     IDirect3DPixelShader8Impl *object;
1476     HRESULT hrc = D3D_OK;
1477
1478     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1479
1480     if (NULL == ppShader) {
1481         TRACE("(%p) Invalid call\n", This);
1482         return D3DERR_INVALIDCALL;
1483     }
1484     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1485
1486     if (NULL == object) {
1487         return E_OUTOFMEMORY;
1488     } else {
1489
1490         object->ref    = 1;
1491         object->lpVtbl = &Direct3DPixelShader8_Vtbl;
1492         hrc = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction, &object->wineD3DPixelShader , (IUnknown *)object);
1493         if (D3D_OK != hrc) {
1494             FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
1495             HeapFree(GetProcessHeap(), 0 , object);
1496             *ppShader = 0;
1497         } else {
1498             shader_handle *handle = alloc_shader_handle(This);
1499             if (!handle) {
1500                 ERR("Failed to allocate shader handle\n");
1501                 IDirect3DVertexShader8_Release((IUnknown *)object);
1502                 hrc = E_OUTOFMEMORY;
1503             } else {
1504                 object->handle = handle;
1505                 *handle = object;
1506                 *ppShader = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1507             }
1508         }
1509
1510     }
1511
1512     TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
1513     return hrc;
1514 }
1515
1516 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1517     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1518     IDirect3DPixelShader8Impl *shader = NULL;
1519
1520     TRACE("(%p) : pShader %#x\n", This, pShader);
1521
1522     if (pShader > VS_HIGHESTFIXEDFXF && This->allocated_shader_handles > pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1523         shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1524     } else if (pShader) {
1525         ERR("Trying to set an invalid handle.\n");
1526     }
1527
1528     TRACE("(%p) : Setting shader %p\n", This, shader);
1529     return IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader == NULL ? NULL :shader->wineD3DPixelShader);
1530 }
1531
1532 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1533     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1534     IWineD3DPixelShader *object;
1535
1536     HRESULT hrc = D3D_OK;
1537     TRACE("(%p) Relay\n", This);
1538     if (NULL == ppShader) {
1539         TRACE("(%p) Invalid call\n", This);
1540         return D3DERR_INVALIDCALL;
1541     }
1542
1543     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
1544     if (D3D_OK == hrc && NULL != object) {
1545         IDirect3DPixelShader8Impl *d3d8_shader;
1546         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
1547         IWineD3DPixelShader_Release(object);
1548         *ppShader = (d3d8_shader->handle - This->shader_handles) + (VS_HIGHESTFIXEDFXF + 1);
1549     } else {
1550         *ppShader = (DWORD)NULL;
1551     }
1552
1553     TRACE("(%p) : returning %#x\n", This, *ppShader);
1554     return hrc;
1555 }
1556
1557 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1558     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1559
1560     TRACE("(%p) : pShader %#x\n", This, pShader);
1561
1562     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1563         ERR("(%p) : Trying to delete an invalid handle\n", This);
1564         return D3DERR_INVALIDCALL;
1565     } else {
1566         IWineD3DPixelShader *cur = NULL;
1567         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1568         IDirect3DPixelShader8Impl *shader = *handle;
1569
1570         IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
1571         if(cur) {
1572             if(cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
1573             IWineD3DPixelShader_Release(cur);
1574         }
1575
1576         while(IUnknown_Release((IUnknown *)shader));
1577         free_shader_handle(This, handle);
1578     }
1579
1580     return D3D_OK;
1581 }
1582
1583 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1584     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1585     TRACE("(%p) Relay\n", This);
1586
1587     return IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1588 }
1589
1590 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1591     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1592     TRACE("(%p) Relay\n", This);
1593
1594     return IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1595 }
1596
1597 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
1598     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1599     IDirect3DPixelShader8Impl *shader = NULL;
1600
1601     TRACE("(%p) : pPixelShader %#x, pData %p, pSizeOfData %p\n", This, pPixelShader, pData, pSizeOfData);
1602
1603     if (pPixelShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pPixelShader - (VS_HIGHESTFIXEDFXF + 1)) {
1604         ERR("Passed an invalid shader handle.\n");
1605         return D3DERR_INVALIDCALL;
1606     }
1607
1608     shader = This->shader_handles[pPixelShader - (VS_HIGHESTFIXEDFXF + 1)];
1609     return IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, (UINT *)pSizeOfData);
1610 }
1611
1612 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1613     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1614     TRACE("(%p) Relay\n", This);
1615
1616     return IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
1617 }
1618
1619 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
1620     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1621     TRACE("(%p) Relay\n", This);
1622
1623     return IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
1624 }
1625
1626 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
1627     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1628     TRACE("(%p) Relay\n", This);
1629
1630     return IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
1631 }
1632
1633 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
1634     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1635     TRACE("(%p) Relay\n" , This);
1636
1637     return IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1638                                           NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
1639                                           0/* Offset in bytes */, Stride);
1640 }
1641
1642 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
1643     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1644     IWineD3DVertexBuffer *retStream = NULL;
1645     HRESULT rc = D3D_OK;
1646
1647     TRACE("(%p) Relay\n" , This);
1648
1649     if(pStream == NULL){
1650         return D3DERR_INVALIDCALL;
1651     }
1652
1653     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, 0 /* Offset in bytes */, pStride);
1654     if (rc == D3D_OK  && NULL != retStream) {
1655         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
1656         IWineD3DVertexBuffer_Release(retStream);
1657     }else{
1658         if (rc != D3D_OK){
1659             FIXME("Call to GetStreamSource failed %p\n",  pStride);
1660         }
1661         *pStream = NULL;
1662     }
1663
1664     return rc;
1665 }
1666
1667
1668 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
1669 {
1670     IDirect3DDevice8Impl_QueryInterface,
1671     IDirect3DDevice8Impl_AddRef,
1672     IDirect3DDevice8Impl_Release,
1673     IDirect3DDevice8Impl_TestCooperativeLevel,
1674     IDirect3DDevice8Impl_GetAvailableTextureMem,
1675     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
1676     IDirect3DDevice8Impl_GetDirect3D,
1677     IDirect3DDevice8Impl_GetDeviceCaps,
1678     IDirect3DDevice8Impl_GetDisplayMode,
1679     IDirect3DDevice8Impl_GetCreationParameters,
1680     IDirect3DDevice8Impl_SetCursorProperties,
1681     IDirect3DDevice8Impl_SetCursorPosition,
1682     IDirect3DDevice8Impl_ShowCursor,
1683     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
1684     IDirect3DDevice8Impl_Reset,
1685     IDirect3DDevice8Impl_Present,
1686     IDirect3DDevice8Impl_GetBackBuffer,
1687     IDirect3DDevice8Impl_GetRasterStatus,
1688     IDirect3DDevice8Impl_SetGammaRamp,
1689     IDirect3DDevice8Impl_GetGammaRamp,
1690     IDirect3DDevice8Impl_CreateTexture,
1691     IDirect3DDevice8Impl_CreateVolumeTexture,
1692     IDirect3DDevice8Impl_CreateCubeTexture,
1693     IDirect3DDevice8Impl_CreateVertexBuffer,
1694     IDirect3DDevice8Impl_CreateIndexBuffer,
1695     IDirect3DDevice8Impl_CreateRenderTarget,
1696     IDirect3DDevice8Impl_CreateDepthStencilSurface,
1697     IDirect3DDevice8Impl_CreateImageSurface,
1698     IDirect3DDevice8Impl_CopyRects,
1699     IDirect3DDevice8Impl_UpdateTexture,
1700     IDirect3DDevice8Impl_GetFrontBuffer,
1701     IDirect3DDevice8Impl_SetRenderTarget,
1702     IDirect3DDevice8Impl_GetRenderTarget,
1703     IDirect3DDevice8Impl_GetDepthStencilSurface,
1704     IDirect3DDevice8Impl_BeginScene,
1705     IDirect3DDevice8Impl_EndScene,
1706     IDirect3DDevice8Impl_Clear,
1707     IDirect3DDevice8Impl_SetTransform,
1708     IDirect3DDevice8Impl_GetTransform,
1709     IDirect3DDevice8Impl_MultiplyTransform,
1710     IDirect3DDevice8Impl_SetViewport,
1711     IDirect3DDevice8Impl_GetViewport,
1712     IDirect3DDevice8Impl_SetMaterial,
1713     IDirect3DDevice8Impl_GetMaterial,
1714     IDirect3DDevice8Impl_SetLight,
1715     IDirect3DDevice8Impl_GetLight,
1716     IDirect3DDevice8Impl_LightEnable,
1717     IDirect3DDevice8Impl_GetLightEnable,
1718     IDirect3DDevice8Impl_SetClipPlane,
1719     IDirect3DDevice8Impl_GetClipPlane,
1720     IDirect3DDevice8Impl_SetRenderState,
1721     IDirect3DDevice8Impl_GetRenderState,
1722     IDirect3DDevice8Impl_BeginStateBlock,
1723     IDirect3DDevice8Impl_EndStateBlock,
1724     IDirect3DDevice8Impl_ApplyStateBlock,
1725     IDirect3DDevice8Impl_CaptureStateBlock,
1726     IDirect3DDevice8Impl_DeleteStateBlock,
1727     IDirect3DDevice8Impl_CreateStateBlock,
1728     IDirect3DDevice8Impl_SetClipStatus,
1729     IDirect3DDevice8Impl_GetClipStatus,
1730     IDirect3DDevice8Impl_GetTexture,
1731     IDirect3DDevice8Impl_SetTexture,
1732     IDirect3DDevice8Impl_GetTextureStageState,
1733     IDirect3DDevice8Impl_SetTextureStageState,
1734     IDirect3DDevice8Impl_ValidateDevice,
1735     IDirect3DDevice8Impl_GetInfo,
1736     IDirect3DDevice8Impl_SetPaletteEntries,
1737     IDirect3DDevice8Impl_GetPaletteEntries,
1738     IDirect3DDevice8Impl_SetCurrentTexturePalette,
1739     IDirect3DDevice8Impl_GetCurrentTexturePalette,
1740     IDirect3DDevice8Impl_DrawPrimitive,
1741     IDirect3DDevice8Impl_DrawIndexedPrimitive,
1742     IDirect3DDevice8Impl_DrawPrimitiveUP,
1743     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
1744     IDirect3DDevice8Impl_ProcessVertices,
1745     IDirect3DDevice8Impl_CreateVertexShader,
1746     IDirect3DDevice8Impl_SetVertexShader,
1747     IDirect3DDevice8Impl_GetVertexShader,
1748     IDirect3DDevice8Impl_DeleteVertexShader,
1749     IDirect3DDevice8Impl_SetVertexShaderConstant,
1750     IDirect3DDevice8Impl_GetVertexShaderConstant,
1751     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
1752     IDirect3DDevice8Impl_GetVertexShaderFunction,
1753     IDirect3DDevice8Impl_SetStreamSource,
1754     IDirect3DDevice8Impl_GetStreamSource,
1755     IDirect3DDevice8Impl_SetIndices,
1756     IDirect3DDevice8Impl_GetIndices,
1757     IDirect3DDevice8Impl_CreatePixelShader,
1758     IDirect3DDevice8Impl_SetPixelShader,
1759     IDirect3DDevice8Impl_GetPixelShader,
1760     IDirect3DDevice8Impl_DeletePixelShader,
1761     IDirect3DDevice8Impl_SetPixelShaderConstant,
1762     IDirect3DDevice8Impl_GetPixelShaderConstant,
1763     IDirect3DDevice8Impl_GetPixelShaderFunction,
1764     IDirect3DDevice8Impl_DrawRectPatch,
1765     IDirect3DDevice8Impl_DrawTriPatch,
1766     IDirect3DDevice8Impl_DeletePatch
1767 };
1768
1769 /* Internal function called back during the CreateDevice to create a render target  */
1770 HRESULT WINAPI D3D8CB_CreateSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
1771                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1772                                          WINED3DCUBEMAP_FACES Face, IWineD3DSurface **ppSurface,
1773                                          HANDLE *pSharedHandle) {
1774
1775     HRESULT res = D3D_OK;
1776     IDirect3DSurface8Impl *d3dSurface = NULL;
1777     BOOL Lockable = TRUE;
1778
1779     if((WINED3DPOOL_DEFAULT == Pool && WINED3DUSAGE_DYNAMIC != Usage))
1780         Lockable = FALSE;
1781
1782     TRACE("relay\n");
1783     res = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)device, Width, Height, (D3DFORMAT)Format, Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface8 **)&d3dSurface, D3DRTYPE_SURFACE, Usage, Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
1784
1785     if (SUCCEEDED(res)) {
1786         *ppSurface = d3dSurface->wineD3DSurface;
1787         d3dSurface->container = pSuperior;
1788         IUnknown_Release(d3dSurface->parentDevice);
1789         d3dSurface->parentDevice = NULL;
1790         d3dSurface->forwardReference = pSuperior;
1791     } else {
1792         FIXME("(%p) IDirect3DDevice8_CreateSurface failed\n", device);
1793     }
1794     return res;
1795 }
1796
1797 ULONG WINAPI D3D8CB_DestroySurface(IWineD3DSurface *pSurface) {
1798     IDirect3DSurface8Impl* surfaceParent;
1799     TRACE("(%p) call back\n", pSurface);
1800
1801     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
1802     /* GetParent's AddRef was forwarded to an object in destruction.
1803      * Releasing it here again would cause an endless recursion. */
1804     surfaceParent->forwardReference = NULL;
1805     return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
1806 }