crypt32: Add and use HCRYPTPROV_LEGACY and HCRYPTPROV_OR_NCRYPT_KEY_HANDLE.
[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         int i;
101
102         TRACE("Releasing wined3d device %p\n", This->WineD3DDevice);
103         EnterCriticalSection(&d3d8_cs);
104         This->inDestruction = TRUE;
105
106         for(i = 0; i < This->numConvertedDecls; i++) {
107             IWineD3DVertexDeclaration_Release(This->decls[i].decl);
108         }
109
110         IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D8CB_DestroyDepthStencilSurface, D3D8CB_DestroySwapChain);
111         IWineD3DDevice_Release(This->WineD3DDevice);
112         HeapFree(GetProcessHeap(), 0, This->shader_handles);
113         HeapFree(GetProcessHeap(), 0, This);
114         LeaveCriticalSection(&d3d8_cs);
115     }
116     return ref;
117 }
118
119 /* IDirect3DDevice Interface follow: */
120 static HRESULT WINAPI IDirect3DDevice8Impl_TestCooperativeLevel(LPDIRECT3DDEVICE8 iface) {
121     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
122     HRESULT hr;
123
124     TRACE("(%p) : Relay\n", This);
125     EnterCriticalSection(&d3d8_cs);
126     hr = IWineD3DDevice_TestCooperativeLevel(This->WineD3DDevice);
127     LeaveCriticalSection(&d3d8_cs);
128     return hr;
129 }
130
131 static UINT WINAPI  IDirect3DDevice8Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE8 iface) {
132     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
133     HRESULT hr;
134
135     TRACE("(%p) Relay\n", This);
136     EnterCriticalSection(&d3d8_cs);
137     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
138     LeaveCriticalSection(&d3d8_cs);
139     return hr;
140 }
141
142 static HRESULT WINAPI IDirect3DDevice8Impl_ResourceManagerDiscardBytes(LPDIRECT3DDEVICE8 iface, DWORD Bytes) {
143     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
144     HRESULT hr;
145
146     TRACE("(%p) : Relay bytes(%d)\n", This, Bytes);
147     EnterCriticalSection(&d3d8_cs);
148     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
149     LeaveCriticalSection(&d3d8_cs);
150     return hr;
151 }
152
153 static HRESULT WINAPI IDirect3DDevice8Impl_GetDirect3D(LPDIRECT3DDEVICE8 iface, IDirect3D8** ppD3D8) {
154     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
155     HRESULT hr = D3D_OK;
156     IWineD3D* pWineD3D;
157
158     TRACE("(%p) Relay\n", This);
159
160     if (NULL == ppD3D8) {
161         return D3DERR_INVALIDCALL;
162     }
163
164     EnterCriticalSection(&d3d8_cs);
165     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
166     if (hr == D3D_OK && pWineD3D != NULL)
167     {
168         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D8);
169         IWineD3D_Release(pWineD3D);
170     } else {
171         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
172         *ppD3D8 = NULL;
173     }
174     TRACE("(%p) returning %p\n",This , *ppD3D8);
175     LeaveCriticalSection(&d3d8_cs);
176
177     return hr;
178 }
179
180 static HRESULT WINAPI IDirect3DDevice8Impl_GetDeviceCaps(LPDIRECT3DDEVICE8 iface, D3DCAPS8* pCaps) {
181     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
182     HRESULT hrc = D3D_OK;
183     WINED3DCAPS *pWineCaps;
184
185     TRACE("(%p) : Relay pCaps %p\n", This, pCaps);
186     if(NULL == pCaps){
187         return D3DERR_INVALIDCALL;
188     }
189     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
190     if(pWineCaps == NULL){
191         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
192     }
193
194     D3D8CAPSTOWINECAPS(pCaps, pWineCaps)
195     EnterCriticalSection(&d3d8_cs);
196     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
197     LeaveCriticalSection(&d3d8_cs);
198     HeapFree(GetProcessHeap(), 0, pWineCaps);
199
200     /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
201     if(pCaps->PixelShaderVersion > D3DPS_VERSION(1,4)){
202         pCaps->PixelShaderVersion = D3DPS_VERSION(1,4);
203     }
204     if(pCaps->VertexShaderVersion > D3DVS_VERSION(1,1)){
205         pCaps->VertexShaderVersion = D3DVS_VERSION(1,1);
206     }
207
208     TRACE("Returning %p %p\n", This, pCaps);
209     return hrc;
210 }
211
212 static HRESULT WINAPI IDirect3DDevice8Impl_GetDisplayMode(LPDIRECT3DDEVICE8 iface, D3DDISPLAYMODE* pMode) {
213     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
214     HRESULT hr;
215     TRACE("(%p) Relay\n", This);
216
217     EnterCriticalSection(&d3d8_cs);
218     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, 0, (WINED3DDISPLAYMODE *) pMode);
219     LeaveCriticalSection(&d3d8_cs);
220     return hr;
221 }
222
223 static HRESULT WINAPI IDirect3DDevice8Impl_GetCreationParameters(LPDIRECT3DDEVICE8 iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
224     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
225     HRESULT hr;
226     TRACE("(%p) Relay\n", This);
227
228     EnterCriticalSection(&d3d8_cs);
229     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
230     LeaveCriticalSection(&d3d8_cs);
231     return hr;
232 }
233
234 static HRESULT WINAPI IDirect3DDevice8Impl_SetCursorProperties(LPDIRECT3DDEVICE8 iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8* pCursorBitmap) {
235     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
236     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl*)pCursorBitmap;
237     HRESULT hr;
238     TRACE("(%p) Relay\n", This);
239     if(!pCursorBitmap) {
240         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
241         return WINED3DERR_INVALIDCALL;
242     }
243
244     EnterCriticalSection(&d3d8_cs);
245     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice,XHotSpot,YHotSpot,(IWineD3DSurface*)pSurface->wineD3DSurface);
246     LeaveCriticalSection(&d3d8_cs);
247     return hr;
248 }
249
250 static void WINAPI IDirect3DDevice8Impl_SetCursorPosition(LPDIRECT3DDEVICE8 iface, UINT XScreenSpace, UINT YScreenSpace, DWORD Flags) {
251     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
252     TRACE("(%p) Relay\n", This);
253
254     EnterCriticalSection(&d3d8_cs);
255     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
256     LeaveCriticalSection(&d3d8_cs);
257 }
258
259 static BOOL WINAPI IDirect3DDevice8Impl_ShowCursor(LPDIRECT3DDEVICE8 iface, BOOL bShow) {
260     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
261     BOOL ret;
262     TRACE("(%p) Relay\n", This);
263
264     EnterCriticalSection(&d3d8_cs);
265     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
266     LeaveCriticalSection(&d3d8_cs);
267     return ret;
268 }
269
270 static HRESULT WINAPI IDirect3DDevice8Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain8** pSwapChain) {
271     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
272     IDirect3DSwapChain8Impl* object;
273     HRESULT hrc = D3D_OK;
274     WINED3DPRESENT_PARAMETERS localParameters;
275
276     TRACE("(%p) Relay\n", This);
277
278     /* Fix the back buffer count */
279     if(pPresentationParameters->BackBufferCount == 0) {
280         pPresentationParameters->BackBufferCount = 1;
281     }
282
283     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
284     if (NULL == object) {
285         FIXME("Allocation of memory failed\n");
286         *pSwapChain = NULL;
287         return D3DERR_OUTOFVIDEOMEMORY;
288     }
289     object->ref = 1;
290     object->lpVtbl = &Direct3DSwapChain8_Vtbl;
291
292     /* Allocate an associated WineD3DDevice object */
293     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
294     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
295     localParameters.BackBufferFormat                            = pPresentationParameters->BackBufferFormat;
296     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
297     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
298     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
299     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
300     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
301     localParameters.Windowed                                    = pPresentationParameters->Windowed;
302     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
303     localParameters.AutoDepthStencilFormat                      = pPresentationParameters->AutoDepthStencilFormat;
304     localParameters.Flags                                       = pPresentationParameters->Flags;
305     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
306     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
307
308     EnterCriticalSection(&d3d8_cs);
309     hrc = IWineD3DDevice_CreateAdditionalSwapChain(This->WineD3DDevice, &localParameters, &object->wineD3DSwapChain, (IUnknown*)object, D3D8CB_CreateRenderTarget, D3D8CB_CreateDepthStencilSurface);
310     LeaveCriticalSection(&d3d8_cs);
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     if (hrc != D3D_OK) {
327         FIXME("(%p) call to IWineD3DDevice_CreateAdditionalSwapChain failed\n", This);
328         HeapFree(GetProcessHeap(), 0 , object);
329         *pSwapChain = NULL;
330     }else{
331         IUnknown_AddRef(iface);
332         object->parentDevice = iface;
333         *pSwapChain = (IDirect3DSwapChain8 *)object;
334     }
335     TRACE("(%p) returning %p\n", This, *pSwapChain);
336     return hrc;
337 }
338
339 static HRESULT WINAPI IDirect3DDevice8Impl_Reset(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
340     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
341     WINED3DPRESENT_PARAMETERS localParameters;
342     HRESULT hr;
343
344     TRACE("(%p) Relay pPresentationParameters(%p)\n", This, pPresentationParameters);
345
346     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
347     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
348     localParameters.BackBufferFormat                            = pPresentationParameters->BackBufferFormat;
349     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
350     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
351     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
352     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
353     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
354     localParameters.Windowed                                    = pPresentationParameters->Windowed;
355     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
356     localParameters.AutoDepthStencilFormat                      = pPresentationParameters->AutoDepthStencilFormat;
357     localParameters.Flags                                       = pPresentationParameters->Flags;
358     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
359     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
360
361     EnterCriticalSection(&d3d8_cs);
362     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
363     LeaveCriticalSection(&d3d8_cs);
364
365     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
366     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
367     pPresentationParameters->BackBufferFormat                   = localParameters.BackBufferFormat;
368     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
369     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
370     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
371     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
372     pPresentationParameters->Windowed                           = localParameters.Windowed;
373     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
374     pPresentationParameters->AutoDepthStencilFormat             = localParameters.AutoDepthStencilFormat;
375     pPresentationParameters->Flags                              = localParameters.Flags;
376     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
377     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
378
379     return hr;
380 }
381
382 static HRESULT WINAPI IDirect3DDevice8Impl_Present(LPDIRECT3DDEVICE8 iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) {
383     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
384     HRESULT hr;
385     TRACE("(%p) Relay\n", This);
386
387     EnterCriticalSection(&d3d8_cs);
388     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
389     LeaveCriticalSection(&d3d8_cs);
390     return hr;
391 }
392
393 static HRESULT WINAPI IDirect3DDevice8Impl_GetBackBuffer(LPDIRECT3DDEVICE8 iface, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8** ppBackBuffer) {
394     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
395     IWineD3DSurface *retSurface = NULL;
396     HRESULT rc = D3D_OK;
397
398     TRACE("(%p) Relay\n", This);
399
400     EnterCriticalSection(&d3d8_cs);
401     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, 0, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, (IWineD3DSurface **)&retSurface);
402     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
403         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
404         IWineD3DSurface_Release(retSurface);
405     }
406     LeaveCriticalSection(&d3d8_cs);
407     return rc;
408 }
409
410 static HRESULT WINAPI IDirect3DDevice8Impl_GetRasterStatus(LPDIRECT3DDEVICE8 iface, D3DRASTER_STATUS* pRasterStatus) {
411     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
412     HRESULT hr;
413     TRACE("(%p) Relay\n", This);
414
415     EnterCriticalSection(&d3d8_cs);
416     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, 0, (WINED3DRASTER_STATUS *) pRasterStatus);
417     LeaveCriticalSection(&d3d8_cs);
418     return hr;
419 }
420
421 static void WINAPI IDirect3DDevice8Impl_SetGammaRamp(LPDIRECT3DDEVICE8 iface, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
422     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
423     TRACE("(%p) Relay\n", This);
424
425     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
426     EnterCriticalSection(&d3d8_cs);
427     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, 0, Flags, (CONST WINED3DGAMMARAMP *) pRamp);
428     LeaveCriticalSection(&d3d8_cs);
429 }
430
431 static void WINAPI IDirect3DDevice8Impl_GetGammaRamp(LPDIRECT3DDEVICE8 iface, D3DGAMMARAMP* pRamp) {
432     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
433     TRACE("(%p) Relay\n", This);
434
435     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
436     EnterCriticalSection(&d3d8_cs);
437     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, 0, (WINED3DGAMMARAMP *) pRamp);
438     LeaveCriticalSection(&d3d8_cs);
439 }
440
441 static HRESULT WINAPI IDirect3DDevice8Impl_CreateTexture(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, UINT Levels, DWORD Usage,
442                                                     D3DFORMAT Format, D3DPOOL Pool, IDirect3DTexture8 **ppTexture) {
443     IDirect3DTexture8Impl *object;
444     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
445     HRESULT hrc = D3D_OK;
446
447     TRACE("(%p) : W(%d) H(%d), Lvl(%d) d(%d), Fmt(%u), Pool(%d)\n", This, Width, Height, Levels, Usage, Format,  Pool);
448
449     /* Allocate the storage for the device */
450     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DTexture8Impl));
451
452     if (NULL == object) {
453         FIXME("Allocation of memory failed\n");
454 /*        *ppTexture = NULL; */
455         return D3DERR_OUTOFVIDEOMEMORY;
456     }
457
458     object->lpVtbl = &Direct3DTexture8_Vtbl;
459     object->ref = 1;
460     EnterCriticalSection(&d3d8_cs);
461     hrc = IWineD3DDevice_CreateTexture(This->WineD3DDevice, Width, Height, Levels, Usage & WINED3DUSAGE_MASK,
462                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DTexture, NULL, (IUnknown *)object, D3D8CB_CreateSurface);
463     LeaveCriticalSection(&d3d8_cs);
464
465     if (FAILED(hrc)) {
466         /* free up object */ 
467         FIXME("(%p) call to IWineD3DDevice_CreateTexture failed\n", This);
468         HeapFree(GetProcessHeap(), 0, object);
469 /*      *ppTexture = NULL; */
470    } else {
471         IUnknown_AddRef(iface);
472         object->parentDevice = iface;
473         *ppTexture = (LPDIRECT3DTEXTURE8) object;
474    }
475
476    TRACE("(%p) Created Texture %p, %p\n",This,object,object->wineD3DTexture);
477    return hrc;
478 }
479
480 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVolumeTexture(LPDIRECT3DDEVICE8 iface, 
481                                                           UINT Width, UINT Height, UINT Depth, UINT Levels, DWORD Usage, 
482                                                           D3DFORMAT Format, D3DPOOL Pool, IDirect3DVolumeTexture8** ppVolumeTexture) {
483
484     IDirect3DVolumeTexture8Impl *object;
485     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
486     HRESULT hrc = D3D_OK;
487
488     TRACE("(%p) Relay\n", This);
489
490     /* Allocate the storage for the device */
491     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVolumeTexture8Impl));
492     if (NULL == object) {
493         FIXME("(%p) allocation of memory failed\n", This);
494         *ppVolumeTexture = NULL;
495         return D3DERR_OUTOFVIDEOMEMORY;
496     }
497
498     object->lpVtbl = &Direct3DVolumeTexture8_Vtbl;
499     object->ref = 1;
500     EnterCriticalSection(&d3d8_cs);
501     hrc = IWineD3DDevice_CreateVolumeTexture(This->WineD3DDevice, Width, Height, Depth, Levels, Usage & WINED3DUSAGE_MASK,
502                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DVolumeTexture, NULL,
503                                  (IUnknown *)object, D3D8CB_CreateVolume);
504     LeaveCriticalSection(&d3d8_cs);
505
506     if (hrc != D3D_OK) {
507
508         /* free up object */
509         FIXME("(%p) call to IWineD3DDevice_CreateVolumeTexture failed\n", This);
510         HeapFree(GetProcessHeap(), 0, object);
511         *ppVolumeTexture = NULL;
512     } else {
513         IUnknown_AddRef(iface);
514         object->parentDevice = iface;
515         *ppVolumeTexture = (LPDIRECT3DVOLUMETEXTURE8) object;
516     }
517     TRACE("(%p)  returning %p\n", This , *ppVolumeTexture);
518     return hrc;
519 }
520
521 static HRESULT WINAPI IDirect3DDevice8Impl_CreateCubeTexture(LPDIRECT3DDEVICE8 iface, UINT EdgeLength, UINT Levels, DWORD Usage, 
522                                                         D3DFORMAT Format, D3DPOOL Pool, IDirect3DCubeTexture8** ppCubeTexture) {
523
524     IDirect3DCubeTexture8Impl *object;
525     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
526     HRESULT hr = D3D_OK;
527
528     TRACE("(%p) : ELen(%d) Lvl(%d) Usage(%d) fmt(%u), Pool(%d)\n" , This, EdgeLength, Levels, Usage, Format, Pool);
529
530     /* Allocate the storage for the device */
531     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
532
533     if (NULL == object) {
534         FIXME("(%p) allocation of CubeTexture failed\n", This);
535         *ppCubeTexture = NULL;
536         return D3DERR_OUTOFVIDEOMEMORY;
537     }
538
539     object->lpVtbl = &Direct3DCubeTexture8_Vtbl;
540     object->ref = 1;
541     EnterCriticalSection(&d3d8_cs);
542     hr = IWineD3DDevice_CreateCubeTexture(This->WineD3DDevice, EdgeLength, Levels, Usage & WINED3DUSAGE_MASK,
543                                  (WINED3DFORMAT)Format, (WINED3DPOOL) Pool, &object->wineD3DCubeTexture, NULL, (IUnknown*)object,
544                                  D3D8CB_CreateSurface);
545     LeaveCriticalSection(&d3d8_cs);
546
547     if (hr != D3D_OK){
548
549         /* free up object */
550         FIXME("(%p) call to IWineD3DDevice_CreateCubeTexture failed\n", This);
551         HeapFree(GetProcessHeap(), 0, object);
552         *ppCubeTexture = NULL;
553     } else {
554         IUnknown_AddRef(iface);
555         object->parentDevice = iface;
556         *ppCubeTexture = (LPDIRECT3DCUBETEXTURE8) object;
557     }
558
559     TRACE("(%p) returning %p\n",This, *ppCubeTexture);
560     return hr;
561 }
562
563 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexBuffer(LPDIRECT3DDEVICE8 iface, UINT Size, DWORD Usage, DWORD FVF, D3DPOOL Pool, IDirect3DVertexBuffer8** ppVertexBuffer) {
564     IDirect3DVertexBuffer8Impl *object;
565     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
566     HRESULT hrc = D3D_OK;
567
568     TRACE("(%p) Relay\n", This);
569     /* Allocate the storage for the device */
570     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexBuffer8Impl));
571     if (NULL == object) {
572         FIXME("Allocation of memory failed\n");
573         *ppVertexBuffer = NULL;
574         return D3DERR_OUTOFVIDEOMEMORY;
575     }
576
577     object->lpVtbl = &Direct3DVertexBuffer8_Vtbl;
578     object->ref = 1;
579     EnterCriticalSection(&d3d8_cs);
580     hrc = IWineD3DDevice_CreateVertexBuffer(This->WineD3DDevice, Size, Usage & WINED3DUSAGE_MASK, FVF, (WINED3DPOOL) Pool, &(object->wineD3DVertexBuffer), NULL, (IUnknown *)object);
581     LeaveCriticalSection(&d3d8_cs);
582
583     if (D3D_OK != hrc) {
584
585         /* free up object */
586         FIXME("(%p) call to IWineD3DDevice_CreateVertexBuffer failed\n", This);
587         HeapFree(GetProcessHeap(), 0, object);
588         *ppVertexBuffer = NULL;
589     } else {
590         IUnknown_AddRef(iface);
591         object->parentDevice = iface;
592         *ppVertexBuffer = (LPDIRECT3DVERTEXBUFFER8) object;
593     }
594     return hrc;
595 }
596
597 static HRESULT WINAPI IDirect3DDevice8Impl_CreateIndexBuffer(LPDIRECT3DDEVICE8 iface, UINT Length, DWORD Usage, D3DFORMAT Format, D3DPOOL Pool, IDirect3DIndexBuffer8** ppIndexBuffer) {
598     IDirect3DIndexBuffer8Impl *object;
599     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
600     HRESULT hrc = D3D_OK;
601
602     TRACE("(%p) Relay\n", This);
603     /* Allocate the storage for the device */
604     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
605     if (NULL == object) {
606         FIXME("Allocation of memory failed\n");
607         *ppIndexBuffer = NULL;
608         return D3DERR_OUTOFVIDEOMEMORY;
609     }
610
611     object->lpVtbl = &Direct3DIndexBuffer8_Vtbl;
612     object->ref = 1;
613     TRACE("Calling wined3d create index buffer\n");
614     EnterCriticalSection(&d3d8_cs);
615     hrc = IWineD3DDevice_CreateIndexBuffer(This->WineD3DDevice, Length, Usage & WINED3DUSAGE_MASK, Format, (WINED3DPOOL) Pool, &object->wineD3DIndexBuffer, NULL, (IUnknown *)object);
616     LeaveCriticalSection(&d3d8_cs);
617
618     if (D3D_OK != hrc) {
619
620         /* free up object */
621         FIXME("(%p) call to IWineD3DDevice_CreateIndexBuffer failed\n", This);
622         HeapFree(GetProcessHeap(), 0, object);
623         *ppIndexBuffer = NULL;
624     } else {
625         IUnknown_AddRef(iface);
626         object->parentDevice = iface;
627         *ppIndexBuffer = (LPDIRECT3DINDEXBUFFER8)object;
628     }
629     return hrc;
630 }
631
632 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)  {
633     HRESULT hrc;
634     IDirect3DSurface8Impl *object;
635     IDirect3DDevice8Impl  *This = (IDirect3DDevice8Impl *)iface;
636     TRACE("(%p) Relay\n", This);
637     if(MultisampleQuality < 0) { 
638         FIXME("MultisampleQuality out of range %d, substituting 0\n", MultisampleQuality);
639         /*FIXME: Find out what windows does with a MultisampleQuality < 0 */
640         MultisampleQuality=0;
641     }
642
643     if(MultisampleQuality > 0){
644         FIXME("MultisampleQuality set to %d, substituting 0\n" , MultisampleQuality);
645         /*
646         MultisampleQuality
647         [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.
648         */
649         MultisampleQuality=0;
650     }
651     /*FIXME: Check MAX bounds of MultisampleQuality*/
652
653     /* Allocate the storage for the device */
654     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
655     if (NULL == object) {
656         FIXME("Allocation of memory failed\n");
657         *ppSurface = NULL;
658         return D3DERR_OUTOFVIDEOMEMORY;
659     }
660
661     object->lpVtbl = &Direct3DSurface8_Vtbl;
662     object->ref = 1;
663
664     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
665
666     /* Not called from the VTable, no locking needed */
667     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);
668     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
669        /* free up object */
670         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
671         HeapFree(GetProcessHeap(), 0, object);
672         *ppSurface = NULL;
673     } else {
674         IUnknown_AddRef(iface);
675         object->parentDevice = iface;
676         *ppSurface = (LPDIRECT3DSURFACE8) object;
677     }
678     return hrc;
679 }
680
681 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8** ppSurface) {
682     HRESULT hr;
683     TRACE("Relay\n");
684
685     EnterCriticalSection(&d3d8_cs);
686     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */, 0 /* Level */ , ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, 0);
687     LeaveCriticalSection(&d3d8_cs);
688     return hr;
689 }
690
691 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8** ppSurface) {
692     HRESULT hr;
693     TRACE("Relay\n");
694
695     /* TODO: Verify that Discard is false */
696     EnterCriticalSection(&d3d8_cs);
697     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE, 0 /* Level */
698                                                ,ppSurface, D3DRTYPE_SURFACE, D3DUSAGE_DEPTHSTENCIL,
699                                                 D3DPOOL_DEFAULT, MultiSample, 0);
700     LeaveCriticalSection(&d3d8_cs);
701     return hr;
702 }
703
704 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8** ppSurface) {
705     HRESULT hr;
706     TRACE("Relay\n");
707
708     EnterCriticalSection(&d3d8_cs);
709     hr = 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 */);
710     LeaveCriticalSection(&d3d8_cs);
711     return hr;
712 }
713
714 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8 *pSourceSurface, CONST RECT *pSourceRects, UINT cRects, IDirect3DSurface8 *pDestinationSurface, CONST POINT *pDestPoints) {
715     IDirect3DSurface8Impl *Source = (IDirect3DSurface8Impl *) pSourceSurface;
716     IDirect3DSurface8Impl *Dest = (IDirect3DSurface8Impl *) pDestinationSurface;
717
718     HRESULT              hr = WINED3D_OK;
719     WINED3DFORMAT        srcFormat, destFormat;
720     UINT                 srcWidth,  destWidth;
721     UINT                 srcHeight, destHeight;
722     UINT                 srcSize;
723     WINED3DSURFACE_DESC  winedesc;
724
725     TRACE("(%p) pSrcSur=%p, pSourceRects=%p, cRects=%d, pDstSur=%p, pDestPtsArr=%p\n", iface,
726           pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
727
728
729     /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the destination texture is in WINED3DPOOL_DEFAULT */
730     memset(&winedesc, 0, sizeof(winedesc));
731
732     winedesc.Format = &srcFormat;
733     winedesc.Width  = &srcWidth;
734     winedesc.Height = &srcHeight;
735     winedesc.Size   = &srcSize;
736     IWineD3DSurface_GetDesc(Source->wineD3DSurface, &winedesc);
737
738     winedesc.Format = &destFormat;
739     winedesc.Width  = &destWidth;
740     winedesc.Height = &destHeight;
741     winedesc.Size   = NULL;
742     EnterCriticalSection(&d3d8_cs);
743     IWineD3DSurface_GetDesc(Dest->wineD3DSurface, &winedesc);
744
745     /* Check that the source and destination formats match */
746     if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat) {
747         WARN("(%p) source %p format must match the dest %p format, returning WINED3DERR_INVALIDCALL\n", iface, pSourceSurface, pDestinationSurface);
748         LeaveCriticalSection(&d3d8_cs);
749         return WINED3DERR_INVALIDCALL;
750     } else if (WINED3DFMT_UNKNOWN == destFormat) {
751         TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
752         IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
753         destFormat = srcFormat;
754     }
755
756     /* Quick if complete copy ... */
757     if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
758         IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
759     } else {
760         unsigned int i;
761         /* Copy rect by rect */
762         if (NULL != pSourceRects && NULL != pDestPoints) {
763             for (i = 0; i < cRects; ++i) {
764                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, (RECT *) &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
765             }
766         } else {
767             for (i = 0; i < cRects; ++i) {
768                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, (RECT *) &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
769             }
770         }
771     }
772     LeaveCriticalSection(&d3d8_cs);
773
774     return hr;
775 }
776
777 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
778     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
779     HRESULT hr;
780     TRACE("(%p) Relay\n" , This);
781
782     EnterCriticalSection(&d3d8_cs);
783     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
784     LeaveCriticalSection(&d3d8_cs);
785     return hr;
786 }
787
788 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
789     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
790     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
791     HRESULT hr;
792
793     TRACE("(%p) Relay\n" , This);
794
795     if (pDestSurface == NULL) {
796         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
797         return D3DERR_INVALIDCALL;
798     }
799
800     EnterCriticalSection(&d3d8_cs);
801     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
802     LeaveCriticalSection(&d3d8_cs);
803     return hr;
804 }
805
806 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
807     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
808     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
809     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
810     HRESULT hr;
811     TRACE("(%p) Relay\n" , This);
812
813     IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL == pZSurface ? NULL : (IWineD3DSurface *)pZSurface->wineD3DSurface);
814
815     EnterCriticalSection(&d3d8_cs);
816     hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface ? (IWineD3DSurface *)pSurface->wineD3DSurface : NULL);
817     LeaveCriticalSection(&d3d8_cs);
818     return hr;
819 }
820
821 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
822     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
823     HRESULT hr = D3D_OK;
824     IWineD3DSurface *pRenderTarget;
825
826     TRACE("(%p) Relay\n" , This);
827
828     if (ppRenderTarget == NULL) {
829         return D3DERR_INVALIDCALL;
830     }
831     EnterCriticalSection(&d3d8_cs);
832     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
833
834     if (hr == D3D_OK && pRenderTarget != NULL) {
835         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
836         IWineD3DSurface_Release(pRenderTarget);
837     } else {
838         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
839         *ppRenderTarget = NULL;
840     }
841     LeaveCriticalSection(&d3d8_cs);
842
843     return hr;
844 }
845
846 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
847     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
848     HRESULT hr = D3D_OK;
849     IWineD3DSurface *pZStencilSurface;
850
851     TRACE("(%p) Relay\n" , This);
852     if(ppZStencilSurface == NULL){
853         return D3DERR_INVALIDCALL;
854     }
855
856     EnterCriticalSection(&d3d8_cs);
857     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
858     if(hr == D3D_OK && pZStencilSurface != NULL){
859         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
860         IWineD3DSurface_Release(pZStencilSurface);
861     }else{
862         FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed\n");
863         *ppZStencilSurface = NULL;
864     }
865     LeaveCriticalSection(&d3d8_cs);
866
867     return D3D_OK;
868 }
869
870 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
871     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
872     HRESULT hr;
873     TRACE("(%p) Relay\n" , This);
874
875     EnterCriticalSection(&d3d8_cs);
876     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
877     LeaveCriticalSection(&d3d8_cs);
878     return hr;
879 }
880
881 static HRESULT WINAPI IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
882     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
883     HRESULT hr;
884     TRACE("(%p) Relay\n" , This);
885
886     EnterCriticalSection(&d3d8_cs);
887     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
888     LeaveCriticalSection(&d3d8_cs);
889     return hr;
890 }
891
892 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
893     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
894     HRESULT hr;
895     TRACE("(%p) Relay\n" , This);
896
897     /* Note: D3DRECT is compatible with WINED3DRECT */
898     EnterCriticalSection(&d3d8_cs);
899     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
900     LeaveCriticalSection(&d3d8_cs);
901     return hr;
902 }
903
904 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
905     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
906     HRESULT hr;
907     TRACE("(%p) Relay\n" , This);
908
909     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
910     EnterCriticalSection(&d3d8_cs);
911     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
912     LeaveCriticalSection(&d3d8_cs);
913     return hr;
914 }
915
916 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
917     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
918     HRESULT hr;
919     TRACE("(%p) Relay\n" , This);
920
921     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
922     EnterCriticalSection(&d3d8_cs);
923     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
924     LeaveCriticalSection(&d3d8_cs);
925     return hr;
926 }
927
928 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
929     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
930     HRESULT hr;
931     TRACE("(%p) Relay\n" , This);
932
933     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
934     EnterCriticalSection(&d3d8_cs);
935     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
936     LeaveCriticalSection(&d3d8_cs);
937     return hr;
938 }
939
940 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
941     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
942     HRESULT hr;
943     TRACE("(%p) Relay\n" , This);
944
945     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
946     EnterCriticalSection(&d3d8_cs);
947     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
948     LeaveCriticalSection(&d3d8_cs);
949     return hr;
950 }
951
952 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
953     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
954     HRESULT hr;
955     TRACE("(%p) Relay\n" , This);
956
957     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
958     EnterCriticalSection(&d3d8_cs);
959     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
960     LeaveCriticalSection(&d3d8_cs);
961     return hr;
962 }
963
964 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
965     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
966     HRESULT hr;
967     TRACE("(%p) Relay\n" , This);
968
969     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
970     EnterCriticalSection(&d3d8_cs);
971     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
972     LeaveCriticalSection(&d3d8_cs);
973     return hr;
974 }
975
976 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
977     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
978     HRESULT hr;
979     TRACE("(%p) Relay\n" , This);
980
981     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
982     EnterCriticalSection(&d3d8_cs);
983     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
984     LeaveCriticalSection(&d3d8_cs);
985     return hr;
986 }
987
988 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
989     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
990     HRESULT hr;
991     TRACE("(%p) Relay\n" , This);
992  
993     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
994     EnterCriticalSection(&d3d8_cs);
995     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
996     LeaveCriticalSection(&d3d8_cs);
997     return hr;
998 }
999
1000 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
1001     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1002     HRESULT hr;
1003     TRACE("(%p) Relay\n" , This);
1004
1005     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1006     EnterCriticalSection(&d3d8_cs);
1007     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1008     LeaveCriticalSection(&d3d8_cs);
1009     return hr;
1010 }
1011
1012 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
1013     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1014     HRESULT hr;
1015     TRACE("(%p) Relay\n" , This);
1016
1017     EnterCriticalSection(&d3d8_cs);
1018     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1019     LeaveCriticalSection(&d3d8_cs);
1020     return hr;
1021 }
1022
1023 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
1024     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1025     HRESULT hr;
1026     TRACE("(%p) Relay\n" , This);
1027
1028     EnterCriticalSection(&d3d8_cs);
1029     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1030     LeaveCriticalSection(&d3d8_cs);
1031     return hr;
1032 }
1033
1034 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
1035     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1036     HRESULT hr;
1037     TRACE("(%p) Relay\n" , This);
1038
1039     EnterCriticalSection(&d3d8_cs);
1040     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1041     LeaveCriticalSection(&d3d8_cs);
1042     return hr;
1043 }
1044
1045 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
1046     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1047     HRESULT hr;
1048     TRACE("(%p) Relay\n" , This);
1049
1050     EnterCriticalSection(&d3d8_cs);
1051     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1052     LeaveCriticalSection(&d3d8_cs);
1053     return hr;
1054 }
1055
1056 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
1057     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1058     HRESULT hr;
1059     TRACE("(%p) Relay\n" , This);
1060
1061     EnterCriticalSection(&d3d8_cs);
1062     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1063     LeaveCriticalSection(&d3d8_cs);
1064     return hr;
1065 }
1066
1067 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
1068     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1069     HRESULT hr;
1070     TRACE("(%p) Relay\n" , This);
1071
1072     EnterCriticalSection(&d3d8_cs);
1073     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1074     LeaveCriticalSection(&d3d8_cs);
1075     return hr;
1076 }
1077
1078 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
1079     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1080     HRESULT hr;
1081     TRACE("(%p)\n", This);
1082
1083     EnterCriticalSection(&d3d8_cs);
1084     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1085     LeaveCriticalSection(&d3d8_cs);
1086     return hr;
1087 }
1088
1089 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
1090     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1091     HRESULT hr;
1092     IWineD3DStateBlock* wineD3DStateBlock;
1093     IDirect3DStateBlock8Impl* object;
1094
1095     TRACE("(%p) Relay\n", This);
1096
1097     /* Tell wineD3D to endstatablock before anything else (in case we run out
1098      * of memory later and cause locking problems)
1099      */
1100     EnterCriticalSection(&d3d8_cs);
1101     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
1102     if (hr != D3D_OK) {
1103         FIXME("IWineD3DDevice_EndStateBlock returned an error\n");
1104         LeaveCriticalSection(&d3d8_cs);
1105         return hr;
1106     }
1107
1108     /* allocate a new IDirectD3DStateBlock */
1109     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
1110     object->ref = 1;
1111     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1112
1113     object->wineD3DStateBlock = wineD3DStateBlock;
1114
1115     *pToken = (DWORD)object;
1116     TRACE("(%p)Returning %p %p\n", This, object, wineD3DStateBlock);
1117
1118     LeaveCriticalSection(&d3d8_cs);
1119     return hr;
1120 }
1121
1122 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1123     IDirect3DStateBlock8Impl *pSB  = (IDirect3DStateBlock8Impl*) Token;
1124     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
1125     HRESULT hr;
1126
1127     TRACE("(%p) %p Relay\n", This, pSB);
1128
1129     EnterCriticalSection(&d3d8_cs);
1130     hr = IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
1131     LeaveCriticalSection(&d3d8_cs);
1132     return hr;
1133 }
1134
1135 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1136     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
1137     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1138     HRESULT hr;
1139
1140     TRACE("(%p) %p Relay\n", This, pSB);
1141
1142     EnterCriticalSection(&d3d8_cs);
1143     hr = IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
1144     LeaveCriticalSection(&d3d8_cs);
1145     return hr;
1146 }
1147
1148 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1149     IDirect3DStateBlock8Impl* pSB = (IDirect3DStateBlock8Impl *)Token;
1150     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1151
1152     TRACE("(%p) Relay\n", This);
1153
1154     EnterCriticalSection(&d3d8_cs);
1155     while(IUnknown_Release((IUnknown *)pSB));
1156     LeaveCriticalSection(&d3d8_cs);
1157
1158     return D3D_OK;
1159 }
1160
1161 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(LPDIRECT3DDEVICE8 iface, D3DSTATEBLOCKTYPE Type, DWORD* pToken) {
1162    IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1163    IDirect3DStateBlock8Impl *object;
1164    HRESULT hrc = D3D_OK;
1165
1166    TRACE("(%p) Relay\n", This);
1167
1168    object  = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
1169    if (NULL == object) {
1170       *pToken = 0;
1171       return E_OUTOFMEMORY;
1172    }
1173    object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1174    object->ref = 1;
1175
1176    EnterCriticalSection(&d3d8_cs);
1177    hrc = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &object->wineD3DStateBlock, (IUnknown *)object);
1178    LeaveCriticalSection(&d3d8_cs);
1179    if(D3D_OK != hrc){
1180        FIXME("(%p) Call to IWineD3DDevice_CreateStateBlock failed.\n", This);
1181        HeapFree(GetProcessHeap(), 0, object);
1182        *pToken = 0;
1183    } else {
1184        *pToken = (DWORD)object;
1185    }
1186    TRACE("(%p) returning token (ptr to stateblock) of %p\n", This, object);
1187
1188    return hrc;
1189 }
1190
1191 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
1192     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1193     HRESULT hr;
1194     TRACE("(%p) Relay\n" , This);
1195 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1196     EnterCriticalSection(&d3d8_cs);
1197     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1198     LeaveCriticalSection(&d3d8_cs);
1199     return hr;
1200 }
1201
1202 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
1203     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1204     HRESULT hr;
1205     TRACE("(%p) Relay\n" , This);
1206
1207     EnterCriticalSection(&d3d8_cs);
1208     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1209     LeaveCriticalSection(&d3d8_cs);
1210     return hr;
1211 }
1212
1213 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
1214     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1215     IWineD3DBaseTexture *retTexture = NULL;
1216     HRESULT rc = D3D_OK;
1217
1218     TRACE("(%p) Relay\n" , This);
1219
1220     if(ppTexture == NULL){
1221         return D3DERR_INVALIDCALL;
1222     }
1223
1224     EnterCriticalSection(&d3d8_cs);
1225     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
1226     if (rc == D3D_OK && NULL != retTexture) {
1227         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1228         IWineD3DBaseTexture_Release(retTexture);
1229     } else {
1230         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1231         *ppTexture = NULL;
1232     }
1233     LeaveCriticalSection(&d3d8_cs);
1234
1235     return rc;
1236 }
1237
1238 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1239     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1240     HRESULT hr;
1241     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1242
1243     EnterCriticalSection(&d3d8_cs);
1244     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1245                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1246     LeaveCriticalSection(&d3d8_cs);
1247     return hr;
1248 }
1249
1250 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1251     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1252     HRESULT hr;
1253     TRACE("(%p) Relay\n" , This);
1254
1255     switch(Type) {
1256     case D3DTSS_ADDRESSU:
1257         Type = WINED3DSAMP_ADDRESSU;
1258         break;
1259     case D3DTSS_ADDRESSV:
1260         Type = WINED3DSAMP_ADDRESSV;
1261         break;
1262     case D3DTSS_ADDRESSW:
1263         Type = WINED3DSAMP_ADDRESSW;
1264         break;
1265     case D3DTSS_BORDERCOLOR:
1266         Type = WINED3DSAMP_BORDERCOLOR;
1267         break;
1268     case D3DTSS_MAGFILTER:
1269         Type = WINED3DSAMP_MAGFILTER;
1270         break;
1271     case D3DTSS_MAXANISOTROPY:
1272         Type = WINED3DSAMP_MAXANISOTROPY;
1273         break;
1274     case D3DTSS_MAXMIPLEVEL:
1275         Type = WINED3DSAMP_MAXMIPLEVEL;
1276         break;
1277     case D3DTSS_MINFILTER:
1278         Type = WINED3DSAMP_MINFILTER;
1279         break;
1280     case D3DTSS_MIPFILTER:
1281         Type = WINED3DSAMP_MIPFILTER;
1282         break;
1283     case D3DTSS_MIPMAPLODBIAS:
1284         Type = WINED3DSAMP_MIPMAPLODBIAS;
1285         break;
1286     default:
1287         EnterCriticalSection(&d3d8_cs);
1288         hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
1289         LeaveCriticalSection(&d3d8_cs);
1290         return hr;
1291     }
1292
1293     EnterCriticalSection(&d3d8_cs);
1294     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, Type, pValue);
1295     LeaveCriticalSection(&d3d8_cs);
1296     return hr;
1297 }
1298
1299 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1300     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1301     HRESULT hr;
1302     TRACE("(%p) Relay\n" , This);
1303
1304     switch(Type) {
1305     case D3DTSS_ADDRESSU:
1306         Type = WINED3DSAMP_ADDRESSU;
1307         break;
1308     case D3DTSS_ADDRESSV:
1309         Type = WINED3DSAMP_ADDRESSV;
1310         break;
1311     case D3DTSS_ADDRESSW:
1312         Type = WINED3DSAMP_ADDRESSW;
1313         break;
1314     case D3DTSS_BORDERCOLOR:
1315         Type = WINED3DSAMP_BORDERCOLOR;
1316         break;
1317     case D3DTSS_MAGFILTER:
1318         Type = WINED3DSAMP_MAGFILTER;
1319         break;
1320     case D3DTSS_MAXANISOTROPY:
1321         Type = WINED3DSAMP_MAXANISOTROPY;
1322         break;
1323     case D3DTSS_MAXMIPLEVEL:
1324         Type = WINED3DSAMP_MAXMIPLEVEL;
1325         break;
1326     case D3DTSS_MINFILTER:
1327         Type = WINED3DSAMP_MINFILTER;
1328         break;
1329     case D3DTSS_MIPFILTER:
1330         Type = WINED3DSAMP_MIPFILTER;
1331         break;
1332     case D3DTSS_MIPMAPLODBIAS:
1333         Type = WINED3DSAMP_MIPMAPLODBIAS;
1334         break;
1335     default:
1336         EnterCriticalSection(&d3d8_cs);
1337         hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
1338         LeaveCriticalSection(&d3d8_cs);
1339         return hr;
1340     }
1341
1342     EnterCriticalSection(&d3d8_cs);
1343     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, Type, Value);
1344     LeaveCriticalSection(&d3d8_cs);
1345     return hr;
1346 }
1347
1348 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1349     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1350     HRESULT hr;
1351     TRACE("(%p) Relay\n" , This);
1352
1353     EnterCriticalSection(&d3d8_cs);
1354     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1355     LeaveCriticalSection(&d3d8_cs);
1356     return hr;
1357 }
1358
1359 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1360     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1361     FIXME("(%p) : stub\n", This);
1362     return D3D_OK;
1363 }
1364
1365 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1366     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1367     HRESULT hr;
1368     TRACE("(%p) Relay\n" , This);
1369
1370     EnterCriticalSection(&d3d8_cs);
1371     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1372     LeaveCriticalSection(&d3d8_cs);
1373     return hr;
1374 }
1375
1376 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1377     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1378     HRESULT hr;
1379     TRACE("(%p) Relay\n" , This);
1380
1381     EnterCriticalSection(&d3d8_cs);
1382     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1383     LeaveCriticalSection(&d3d8_cs);
1384     return hr;
1385 }
1386
1387 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1388     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1389     HRESULT hr;
1390     TRACE("(%p) Relay\n" , This);
1391
1392     EnterCriticalSection(&d3d8_cs);
1393     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1394     LeaveCriticalSection(&d3d8_cs);
1395     return hr;
1396 }
1397
1398 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1399     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1400     HRESULT hr;
1401     TRACE("(%p) Relay\n" , This);
1402
1403     EnterCriticalSection(&d3d8_cs);
1404     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1405     LeaveCriticalSection(&d3d8_cs);
1406     return hr;
1407 }
1408
1409 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1410     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface; 
1411     HRESULT hr;
1412     TRACE("(%p) Relay\n" , This);
1413
1414     EnterCriticalSection(&d3d8_cs);
1415     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1416     LeaveCriticalSection(&d3d8_cs);
1417     return hr;
1418 }
1419
1420 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1421                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1422     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1423     HRESULT hr;
1424     TRACE("(%p) Relay\n" , This);
1425
1426     EnterCriticalSection(&d3d8_cs);
1427     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1428     LeaveCriticalSection(&d3d8_cs);
1429     return hr;
1430 }
1431
1432 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1433     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1434     HRESULT hr;
1435     TRACE("(%p) Relay\n" , This);
1436
1437     EnterCriticalSection(&d3d8_cs);
1438     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1439     LeaveCriticalSection(&d3d8_cs);
1440     return hr;
1441 }
1442
1443 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1444                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1445                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1446                                                              UINT VertexStreamZeroStride) {
1447     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1448     HRESULT hr;
1449     TRACE("(%p) Relay\n" , This);
1450
1451     EnterCriticalSection(&d3d8_cs);
1452     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1453                                                pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1454     LeaveCriticalSection(&d3d8_cs);
1455     return hr;
1456 }
1457
1458 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1459     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1460     HRESULT hr;
1461     TRACE("(%p) Relay\n" , This);
1462
1463     EnterCriticalSection(&d3d8_cs);
1464     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer8Impl *)pDestBuffer)->wineD3DVertexBuffer, NULL, Flags);
1465     LeaveCriticalSection(&d3d8_cs);
1466     return hr;
1467 }
1468
1469 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexDeclaration(IDirect3DDevice8 *iface, CONST DWORD *declaration, IDirect3DVertexDeclaration8 **decl_ptr) {
1470     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1471     IDirect3DVertexDeclaration8Impl *object;
1472     WINED3DVERTEXELEMENT *wined3d_elements;
1473     size_t wined3d_element_count;
1474     HRESULT hr = D3D_OK;
1475
1476     TRACE("(%p) : declaration %p\n", This, declaration);
1477
1478     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1479     if (!object) {
1480         ERR("Memory allocation failed\n");
1481         *decl_ptr = NULL;
1482         return D3DERR_OUTOFVIDEOMEMORY;
1483     }
1484
1485     object->ref_count = 1;
1486     object->lpVtbl = &Direct3DVertexDeclaration8_Vtbl;
1487
1488     wined3d_element_count = convert_to_wined3d_declaration(declaration, &object->elements_size, &wined3d_elements);
1489     object->elements = HeapAlloc(GetProcessHeap(), 0, object->elements_size);
1490     if (!object->elements) {
1491         ERR("Memory allocation failed\n");
1492         HeapFree(GetProcessHeap(), 0, wined3d_elements);
1493         HeapFree(GetProcessHeap(), 0, object);
1494         *decl_ptr = NULL;
1495         return D3DERR_OUTOFVIDEOMEMORY;
1496     }
1497
1498     CopyMemory(object->elements, declaration, object->elements_size);
1499
1500     EnterCriticalSection(&d3d8_cs);
1501     hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wined3d_vertex_declaration,
1502             (IUnknown *)object, wined3d_elements, wined3d_element_count);
1503     LeaveCriticalSection(&d3d8_cs);
1504     HeapFree(GetProcessHeap(), 0, wined3d_elements);
1505
1506     if (FAILED(hr)) {
1507         ERR("(%p) : IWineD3DDevice_CreateVertexDeclaration call failed\n", This);
1508         HeapFree(GetProcessHeap(), 0, object->elements);
1509         HeapFree(GetProcessHeap(), 0, object);
1510     } else {
1511         *decl_ptr = (IDirect3DVertexDeclaration8 *)object;
1512         TRACE("(%p) : Created vertex declaration %p\n", This, object);
1513     }
1514
1515     return hr;
1516 }
1517
1518 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pDeclaration, CONST DWORD* pFunction, DWORD* ppShader, DWORD Usage) {
1519     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1520     HRESULT hrc = D3D_OK;
1521     IDirect3DVertexShader8Impl *object;
1522     IWineD3DVertexDeclaration *wined3d_vertex_declaration;
1523
1524     /* Setup a stub object for now */
1525     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1526     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1527     if (NULL == object) {
1528         FIXME("Allocation of memory failed\n");
1529         *ppShader = 0;
1530         return D3DERR_OUTOFVIDEOMEMORY;
1531     }
1532
1533     object->ref = 1;
1534     object->lpVtbl = &Direct3DVertexShader8_Vtbl;
1535
1536     EnterCriticalSection(&d3d8_cs);
1537     hrc = IDirect3DDevice8Impl_CreateVertexDeclaration(iface, pDeclaration, &object->vertex_declaration);
1538     if (FAILED(hrc)) {
1539         ERR("(%p) : IDirect3DDeviceImpl_CreateVertexDeclaration call failed\n", This);
1540         LeaveCriticalSection(&d3d8_cs);
1541         HeapFree(GetProcessHeap(), 0, object);
1542         *ppShader = 0;
1543         return D3DERR_INVALIDCALL;
1544     }
1545     wined3d_vertex_declaration = ((IDirect3DVertexDeclaration8Impl *)object->vertex_declaration)->wined3d_vertex_declaration;
1546
1547     /* Usage is missing ... Use SetRenderState to set the sw vp render state in SetVertexShader */
1548     hrc = IWineD3DDevice_CreateVertexShader(This->WineD3DDevice, wined3d_vertex_declaration, pFunction, &object->wineD3DVertexShader, (IUnknown *)object);
1549
1550     if (FAILED(hrc)) {
1551         /* free up object */
1552         FIXME("Call to IWineD3DDevice_CreateVertexShader failed\n");
1553         HeapFree(GetProcessHeap(), 0, object);
1554         *ppShader = 0;
1555     } else {
1556         /* TODO: Store the VS declarations locally so that they can be derefferenced with a value higher than VS_HIGHESTFIXEDFXF */
1557         shader_handle *handle = alloc_shader_handle(This);
1558         if (!handle) {
1559             ERR("Failed to allocate shader handle\n");
1560             IDirect3DVertexShader8_Release((IUnknown *)object);
1561             hrc = E_OUTOFMEMORY;
1562         } else {
1563             object->handle = handle;
1564             *handle = object;
1565             *ppShader = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1566
1567             load_local_constants(pDeclaration, object->wineD3DVertexShader);
1568         }
1569     }
1570     LeaveCriticalSection(&d3d8_cs);
1571     TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
1572
1573     return hrc;
1574 }
1575
1576 IWineD3DVertexDeclaration *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1577 {
1578     HRESULT hr;
1579     IWineD3DVertexDeclaration* pDecl = NULL;
1580     int p, low, high; /* deliberately signed */
1581     struct FvfToDecl *convertedDecls = This->decls;
1582
1583     TRACE("Searching for declaration for fvf %08x... ", fvf);
1584
1585     low = 0;
1586     high = This->numConvertedDecls - 1;
1587     while(low <= high) {
1588         p = (low + high) >> 1;
1589         TRACE("%d ", p);
1590         if(convertedDecls[p].fvf == fvf) {
1591             TRACE("found %p\n", convertedDecls[p].decl);
1592             return convertedDecls[p].decl;
1593         } else if(convertedDecls[p].fvf < fvf) {
1594             low = p + 1;
1595         } else {
1596             high = p - 1;
1597         }
1598     }
1599     TRACE("not found. Creating and inserting at position %d.\n", low);
1600
1601     hr = IWineD3DDevice_CreateVertexDeclarationFromFVF(This->WineD3DDevice,
1602                                                        &pDecl,
1603                                                        (IUnknown *) This,
1604                                                        fvf);
1605     if (FAILED(hr)) return NULL;
1606
1607     if(This->declArraySize == This->numConvertedDecls) {
1608         int grow = This->declArraySize / 2;
1609         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1610                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1611         if(!convertedDecls) {
1612             /* This will destroy it */
1613             IWineD3DVertexDeclaration_Release(pDecl);
1614             return NULL;
1615         }
1616         This->decls = convertedDecls;
1617         This->declArraySize += grow;
1618     }
1619
1620     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
1621     convertedDecls[low].decl = pDecl;
1622     convertedDecls[low].fvf = fvf;
1623     This->numConvertedDecls++;
1624
1625     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
1626     return pDecl;
1627 }
1628
1629 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1630     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1631     HRESULT hrc = D3D_OK;
1632
1633     TRACE("(%p) : Relay\n", This);
1634     EnterCriticalSection(&d3d8_cs);
1635     if (VS_HIGHESTFIXEDFXF >= pShader) {
1636         TRACE("Setting FVF, %d %d\n", VS_HIGHESTFIXEDFXF, pShader);
1637         IWineD3DDevice_SetFVF(This->WineD3DDevice, pShader);
1638         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, IDirect3DDevice8Impl_FindDecl(This, pShader));
1639         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1640     } else {
1641         TRACE("Setting shader\n");
1642         if (This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1643             FIXME("(%p) : Number of shaders exceeds the maximum number of possible shaders\n", This);
1644             hrc = D3DERR_INVALIDCALL;
1645         } else {
1646             IDirect3DVertexShader8Impl *shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1647             IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1648                     shader ? ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration : NULL);
1649             hrc = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, 0 == shader ? NULL : shader->wineD3DVertexShader);
1650         }
1651     }
1652     TRACE("(%p) : returning hr(%u)\n", This, hrc);
1653     LeaveCriticalSection(&d3d8_cs);
1654
1655     return hrc;
1656 }
1657
1658 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1659     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1660     IWineD3DVertexShader *pShader;
1661     HRESULT hrc = D3D_OK;
1662
1663     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1664     EnterCriticalSection(&d3d8_cs);
1665     hrc = IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &pShader);
1666     if (D3D_OK == hrc) {
1667         if(0 != pShader) {
1668             IDirect3DVertexShader8Impl *d3d8_shader;
1669             hrc = IWineD3DVertexShader_GetParent(pShader, (IUnknown **)&d3d8_shader);
1670             IWineD3DVertexShader_Release(pShader);
1671             *ppShader = (d3d8_shader->handle - This->shader_handles) + (VS_HIGHESTFIXEDFXF + 1);
1672         } else {
1673             *ppShader = 0;
1674             hrc = D3D_OK;
1675         }
1676     } else {
1677         WARN("(%p) : Call to IWineD3DDevice_GetVertexShader failed %u (device %p)\n", This, hrc, This->WineD3DDevice);
1678     }
1679     TRACE("(%p) : returning %#x\n", This, *ppShader);
1680     LeaveCriticalSection(&d3d8_cs);
1681
1682     return hrc;
1683 }
1684
1685 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1686     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1687
1688     TRACE("(%p) : pShader %#x\n", This, pShader);
1689
1690     EnterCriticalSection(&d3d8_cs);
1691     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1692         ERR("(%p) : Trying to delete an invalid handle\n", This);
1693         LeaveCriticalSection(&d3d8_cs);
1694         return D3DERR_INVALIDCALL;
1695     } else {
1696         IWineD3DVertexShader *cur = NULL;
1697         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1698         IDirect3DVertexShader8Impl *shader = *handle;
1699
1700         IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
1701         if(cur) {
1702             if(cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
1703             IWineD3DVertexShader_Release(cur);
1704         }
1705
1706         while(IUnknown_Release((IUnknown *)shader));
1707         free_shader_handle(This, handle);
1708     }
1709     LeaveCriticalSection(&d3d8_cs);
1710
1711     return D3D_OK;
1712 }
1713
1714 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1715     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1716     HRESULT hr;
1717     TRACE("(%p) : Relay\n", This);
1718
1719     EnterCriticalSection(&d3d8_cs);
1720     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1721     LeaveCriticalSection(&d3d8_cs);
1722     return hr;
1723 }
1724
1725 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1726     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1727     HRESULT hr;
1728     TRACE("(%p) : Relay\n", This);
1729
1730     EnterCriticalSection(&d3d8_cs);
1731     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1732     LeaveCriticalSection(&d3d8_cs);
1733     return hr;
1734 }
1735
1736 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1737     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1738     IDirect3DVertexDeclaration8Impl *declaration;
1739     IDirect3DVertexShader8Impl *shader = NULL;
1740
1741     TRACE("(%p) : pVertexShader 0x%08x, pData %p, *pSizeOfData %u\n", This, pVertexShader, pData, *pSizeOfData);
1742
1743     EnterCriticalSection(&d3d8_cs);
1744     if (pVertexShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pVertexShader - (VS_HIGHESTFIXEDFXF + 1)) {
1745         ERR("Passed an invalid shader handle.\n");
1746         LeaveCriticalSection(&d3d8_cs);
1747         return D3DERR_INVALIDCALL;
1748     }
1749
1750     shader = This->shader_handles[pVertexShader - (VS_HIGHESTFIXEDFXF + 1)];
1751     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
1752
1753     /* If pData is NULL, we just return the required size of the buffer. */
1754     if (!pData) {
1755         *pSizeOfData = declaration->elements_size;
1756         LeaveCriticalSection(&d3d8_cs);
1757         return D3D_OK;
1758     }
1759
1760     /* MSDN claims that if *pSizeOfData is smaller than the required size
1761      * we should write the required size and return D3DERR_MOREDATA.
1762      * That's not actually true. */
1763     if (*pSizeOfData < declaration->elements_size) {
1764         LeaveCriticalSection(&d3d8_cs);
1765         return D3DERR_INVALIDCALL;
1766     }
1767
1768     CopyMemory(pData, declaration->elements, declaration->elements_size);
1769     LeaveCriticalSection(&d3d8_cs);
1770
1771     return D3D_OK;
1772 }
1773
1774 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1775     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1776     IDirect3DVertexShader8Impl *shader = NULL;
1777     HRESULT hr;
1778
1779     TRACE("(%p) : pVertexShader %#x, pData %p, pSizeOfData %p\n", This, pVertexShader, pData, pSizeOfData);
1780
1781     EnterCriticalSection(&d3d8_cs);
1782     if (pVertexShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pVertexShader - (VS_HIGHESTFIXEDFXF + 1)) {
1783         ERR("Passed an invalid shader handle.\n");
1784         LeaveCriticalSection(&d3d8_cs);
1785         return D3DERR_INVALIDCALL;
1786     }
1787
1788     shader = This->shader_handles[pVertexShader - (VS_HIGHESTFIXEDFXF + 1)];
1789     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, (UINT *)pSizeOfData);
1790     LeaveCriticalSection(&d3d8_cs);
1791     return hr;
1792 }
1793
1794 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
1795     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1796     HRESULT hr;
1797     TRACE("(%p) Relay\n", This);
1798
1799     EnterCriticalSection(&d3d8_cs);
1800     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
1801     hr = IWineD3DDevice_SetIndices(This->WineD3DDevice,
1802             pIndexData ? ((IDirect3DIndexBuffer8Impl *)pIndexData)->wineD3DIndexBuffer : NULL);
1803     LeaveCriticalSection(&d3d8_cs);
1804     return hr;
1805 }
1806
1807 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
1808     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1809     IWineD3DIndexBuffer *retIndexData = NULL;
1810     HRESULT rc = D3D_OK;
1811
1812     TRACE("(%p) Relay\n", This);
1813
1814     if(ppIndexData == NULL){
1815         return D3DERR_INVALIDCALL;
1816     }
1817
1818     EnterCriticalSection(&d3d8_cs);
1819     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, pBaseVertexIndex);
1820     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData);
1821     if (SUCCEEDED(rc) && retIndexData) {
1822         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1823         IWineD3DIndexBuffer_Release(retIndexData);
1824     } else {
1825         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
1826         *ppIndexData = NULL;
1827     }
1828     LeaveCriticalSection(&d3d8_cs);
1829
1830     return rc;
1831 }
1832 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
1833     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1834     IDirect3DPixelShader8Impl *object;
1835     HRESULT hrc = D3D_OK;
1836
1837     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
1838
1839     if (NULL == ppShader) {
1840         TRACE("(%p) Invalid call\n", This);
1841         return D3DERR_INVALIDCALL;
1842     }
1843     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1844
1845     if (NULL == object) {
1846         return E_OUTOFMEMORY;
1847     } else {
1848         EnterCriticalSection(&d3d8_cs);
1849
1850         object->ref    = 1;
1851         object->lpVtbl = &Direct3DPixelShader8_Vtbl;
1852         hrc = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction, &object->wineD3DPixelShader , (IUnknown *)object);
1853         if (D3D_OK != hrc) {
1854             FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
1855             HeapFree(GetProcessHeap(), 0 , object);
1856             *ppShader = 0;
1857         } else {
1858             shader_handle *handle = alloc_shader_handle(This);
1859             if (!handle) {
1860                 ERR("Failed to allocate shader handle\n");
1861                 IDirect3DVertexShader8_Release((IUnknown *)object);
1862                 hrc = E_OUTOFMEMORY;
1863             } else {
1864                 object->handle = handle;
1865                 *handle = object;
1866                 *ppShader = (handle - This->shader_handles) + VS_HIGHESTFIXEDFXF + 1;
1867             }
1868         }
1869         LeaveCriticalSection(&d3d8_cs);
1870     }
1871
1872     TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
1873     return hrc;
1874 }
1875
1876 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1877     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1878     IDirect3DPixelShader8Impl *shader = NULL;
1879     HRESULT hr;
1880
1881     TRACE("(%p) : pShader %#x\n", This, pShader);
1882
1883     EnterCriticalSection(&d3d8_cs);
1884     if (pShader > VS_HIGHESTFIXEDFXF && This->allocated_shader_handles > pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1885         shader = This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1886     } else if (pShader) {
1887         ERR("Trying to set an invalid handle.\n");
1888     }
1889
1890     TRACE("(%p) : Setting shader %p\n", This, shader);
1891     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader == NULL ? NULL :shader->wineD3DPixelShader);
1892     LeaveCriticalSection(&d3d8_cs);
1893     return hr;
1894 }
1895
1896 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1897     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1898     IWineD3DPixelShader *object;
1899
1900     HRESULT hrc = D3D_OK;
1901     TRACE("(%p) Relay\n", This);
1902     if (NULL == ppShader) {
1903         TRACE("(%p) Invalid call\n", This);
1904         return D3DERR_INVALIDCALL;
1905     }
1906
1907     EnterCriticalSection(&d3d8_cs);
1908     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
1909     if (D3D_OK == hrc && NULL != object) {
1910         IDirect3DPixelShader8Impl *d3d8_shader;
1911         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
1912         IWineD3DPixelShader_Release(object);
1913         *ppShader = (d3d8_shader->handle - This->shader_handles) + (VS_HIGHESTFIXEDFXF + 1);
1914     } else {
1915         *ppShader = (DWORD)NULL;
1916     }
1917
1918     TRACE("(%p) : returning %#x\n", This, *ppShader);
1919     LeaveCriticalSection(&d3d8_cs);
1920     return hrc;
1921 }
1922
1923 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1924     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1925
1926     TRACE("(%p) : pShader %#x\n", This, pShader);
1927
1928     EnterCriticalSection(&d3d8_cs);
1929     if (pShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pShader - (VS_HIGHESTFIXEDFXF + 1)) {
1930         ERR("(%p) : Trying to delete an invalid handle\n", This);
1931         LeaveCriticalSection(&d3d8_cs);
1932         return D3DERR_INVALIDCALL;
1933     } else {
1934         IWineD3DPixelShader *cur = NULL;
1935         shader_handle *handle = &This->shader_handles[pShader - (VS_HIGHESTFIXEDFXF + 1)];
1936         IDirect3DPixelShader8Impl *shader = *handle;
1937
1938         IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
1939         if(cur) {
1940             if(cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
1941             IWineD3DPixelShader_Release(cur);
1942         }
1943
1944         while(IUnknown_Release((IUnknown *)shader));
1945         free_shader_handle(This, handle);
1946     }
1947     LeaveCriticalSection(&d3d8_cs);
1948
1949     return D3D_OK;
1950 }
1951
1952 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1953     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1954     HRESULT hr;
1955     TRACE("(%p) Relay\n", This);
1956
1957     EnterCriticalSection(&d3d8_cs);
1958     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, (CONST float *)pConstantData, ConstantCount);
1959     LeaveCriticalSection(&d3d8_cs);
1960     return hr;
1961 }
1962
1963 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1964     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1965     HRESULT hr;
1966     TRACE("(%p) Relay\n", This);
1967
1968     EnterCriticalSection(&d3d8_cs);
1969     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, (float *)pConstantData, ConstantCount);
1970     LeaveCriticalSection(&d3d8_cs);
1971     return hr;
1972 }
1973
1974 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
1975     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1976     IDirect3DPixelShader8Impl *shader = NULL;
1977     HRESULT hr;
1978
1979     TRACE("(%p) : pPixelShader %#x, pData %p, pSizeOfData %p\n", This, pPixelShader, pData, pSizeOfData);
1980
1981     EnterCriticalSection(&d3d8_cs);
1982     if (pPixelShader <= VS_HIGHESTFIXEDFXF || This->allocated_shader_handles <= pPixelShader - (VS_HIGHESTFIXEDFXF + 1)) {
1983         ERR("Passed an invalid shader handle.\n");
1984         LeaveCriticalSection(&d3d8_cs);
1985         return D3DERR_INVALIDCALL;
1986     }
1987
1988     shader = This->shader_handles[pPixelShader - (VS_HIGHESTFIXEDFXF + 1)];
1989     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, (UINT *)pSizeOfData);
1990     LeaveCriticalSection(&d3d8_cs);
1991     return hr;
1992 }
1993
1994 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1995     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1996     HRESULT hr;
1997     TRACE("(%p) Relay\n", This);
1998
1999     EnterCriticalSection(&d3d8_cs);
2000     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2001     LeaveCriticalSection(&d3d8_cs);
2002     return hr;
2003 }
2004
2005 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2006     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2007     HRESULT hr;
2008     TRACE("(%p) Relay\n", This);
2009
2010     EnterCriticalSection(&d3d8_cs);
2011     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2012     LeaveCriticalSection(&d3d8_cs);
2013     return hr;
2014 }
2015
2016 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
2017     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2018     HRESULT hr;
2019     TRACE("(%p) Relay\n", This);
2020
2021     EnterCriticalSection(&d3d8_cs);
2022     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2023     LeaveCriticalSection(&d3d8_cs);
2024     return hr;
2025 }
2026
2027 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
2028     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2029     HRESULT hr;
2030     TRACE("(%p) Relay\n" , This);
2031
2032     EnterCriticalSection(&d3d8_cs);
2033     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2034                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2035                                         0/* Offset in bytes */, Stride);
2036     LeaveCriticalSection(&d3d8_cs);
2037     return hr;
2038 }
2039
2040 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
2041     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2042     IWineD3DVertexBuffer *retStream = NULL;
2043     HRESULT rc = D3D_OK;
2044
2045     TRACE("(%p) Relay\n" , This);
2046
2047     if(pStream == NULL){
2048         return D3DERR_INVALIDCALL;
2049     }
2050
2051     EnterCriticalSection(&d3d8_cs);
2052     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, 0 /* Offset in bytes */, pStride);
2053     if (rc == D3D_OK  && NULL != retStream) {
2054         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
2055         IWineD3DVertexBuffer_Release(retStream);
2056     }else{
2057         if (rc != D3D_OK){
2058             FIXME("Call to GetStreamSource failed %p\n",  pStride);
2059         }
2060         *pStream = NULL;
2061     }
2062     LeaveCriticalSection(&d3d8_cs);
2063
2064     return rc;
2065 }
2066
2067
2068 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2069 {
2070     IDirect3DDevice8Impl_QueryInterface,
2071     IDirect3DDevice8Impl_AddRef,
2072     IDirect3DDevice8Impl_Release,
2073     IDirect3DDevice8Impl_TestCooperativeLevel,
2074     IDirect3DDevice8Impl_GetAvailableTextureMem,
2075     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2076     IDirect3DDevice8Impl_GetDirect3D,
2077     IDirect3DDevice8Impl_GetDeviceCaps,
2078     IDirect3DDevice8Impl_GetDisplayMode,
2079     IDirect3DDevice8Impl_GetCreationParameters,
2080     IDirect3DDevice8Impl_SetCursorProperties,
2081     IDirect3DDevice8Impl_SetCursorPosition,
2082     IDirect3DDevice8Impl_ShowCursor,
2083     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2084     IDirect3DDevice8Impl_Reset,
2085     IDirect3DDevice8Impl_Present,
2086     IDirect3DDevice8Impl_GetBackBuffer,
2087     IDirect3DDevice8Impl_GetRasterStatus,
2088     IDirect3DDevice8Impl_SetGammaRamp,
2089     IDirect3DDevice8Impl_GetGammaRamp,
2090     IDirect3DDevice8Impl_CreateTexture,
2091     IDirect3DDevice8Impl_CreateVolumeTexture,
2092     IDirect3DDevice8Impl_CreateCubeTexture,
2093     IDirect3DDevice8Impl_CreateVertexBuffer,
2094     IDirect3DDevice8Impl_CreateIndexBuffer,
2095     IDirect3DDevice8Impl_CreateRenderTarget,
2096     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2097     IDirect3DDevice8Impl_CreateImageSurface,
2098     IDirect3DDevice8Impl_CopyRects,
2099     IDirect3DDevice8Impl_UpdateTexture,
2100     IDirect3DDevice8Impl_GetFrontBuffer,
2101     IDirect3DDevice8Impl_SetRenderTarget,
2102     IDirect3DDevice8Impl_GetRenderTarget,
2103     IDirect3DDevice8Impl_GetDepthStencilSurface,
2104     IDirect3DDevice8Impl_BeginScene,
2105     IDirect3DDevice8Impl_EndScene,
2106     IDirect3DDevice8Impl_Clear,
2107     IDirect3DDevice8Impl_SetTransform,
2108     IDirect3DDevice8Impl_GetTransform,
2109     IDirect3DDevice8Impl_MultiplyTransform,
2110     IDirect3DDevice8Impl_SetViewport,
2111     IDirect3DDevice8Impl_GetViewport,
2112     IDirect3DDevice8Impl_SetMaterial,
2113     IDirect3DDevice8Impl_GetMaterial,
2114     IDirect3DDevice8Impl_SetLight,
2115     IDirect3DDevice8Impl_GetLight,
2116     IDirect3DDevice8Impl_LightEnable,
2117     IDirect3DDevice8Impl_GetLightEnable,
2118     IDirect3DDevice8Impl_SetClipPlane,
2119     IDirect3DDevice8Impl_GetClipPlane,
2120     IDirect3DDevice8Impl_SetRenderState,
2121     IDirect3DDevice8Impl_GetRenderState,
2122     IDirect3DDevice8Impl_BeginStateBlock,
2123     IDirect3DDevice8Impl_EndStateBlock,
2124     IDirect3DDevice8Impl_ApplyStateBlock,
2125     IDirect3DDevice8Impl_CaptureStateBlock,
2126     IDirect3DDevice8Impl_DeleteStateBlock,
2127     IDirect3DDevice8Impl_CreateStateBlock,
2128     IDirect3DDevice8Impl_SetClipStatus,
2129     IDirect3DDevice8Impl_GetClipStatus,
2130     IDirect3DDevice8Impl_GetTexture,
2131     IDirect3DDevice8Impl_SetTexture,
2132     IDirect3DDevice8Impl_GetTextureStageState,
2133     IDirect3DDevice8Impl_SetTextureStageState,
2134     IDirect3DDevice8Impl_ValidateDevice,
2135     IDirect3DDevice8Impl_GetInfo,
2136     IDirect3DDevice8Impl_SetPaletteEntries,
2137     IDirect3DDevice8Impl_GetPaletteEntries,
2138     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2139     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2140     IDirect3DDevice8Impl_DrawPrimitive,
2141     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2142     IDirect3DDevice8Impl_DrawPrimitiveUP,
2143     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2144     IDirect3DDevice8Impl_ProcessVertices,
2145     IDirect3DDevice8Impl_CreateVertexShader,
2146     IDirect3DDevice8Impl_SetVertexShader,
2147     IDirect3DDevice8Impl_GetVertexShader,
2148     IDirect3DDevice8Impl_DeleteVertexShader,
2149     IDirect3DDevice8Impl_SetVertexShaderConstant,
2150     IDirect3DDevice8Impl_GetVertexShaderConstant,
2151     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2152     IDirect3DDevice8Impl_GetVertexShaderFunction,
2153     IDirect3DDevice8Impl_SetStreamSource,
2154     IDirect3DDevice8Impl_GetStreamSource,
2155     IDirect3DDevice8Impl_SetIndices,
2156     IDirect3DDevice8Impl_GetIndices,
2157     IDirect3DDevice8Impl_CreatePixelShader,
2158     IDirect3DDevice8Impl_SetPixelShader,
2159     IDirect3DDevice8Impl_GetPixelShader,
2160     IDirect3DDevice8Impl_DeletePixelShader,
2161     IDirect3DDevice8Impl_SetPixelShaderConstant,
2162     IDirect3DDevice8Impl_GetPixelShaderConstant,
2163     IDirect3DDevice8Impl_GetPixelShaderFunction,
2164     IDirect3DDevice8Impl_DrawRectPatch,
2165     IDirect3DDevice8Impl_DrawTriPatch,
2166     IDirect3DDevice8Impl_DeletePatch
2167 };
2168
2169 /* Internal function called back during the CreateDevice to create a render target  */
2170 HRESULT WINAPI D3D8CB_CreateSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
2171                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
2172                                          WINED3DCUBEMAP_FACES Face, IWineD3DSurface **ppSurface,
2173                                          HANDLE *pSharedHandle) {
2174
2175     HRESULT res = D3D_OK;
2176     IDirect3DSurface8Impl *d3dSurface = NULL;
2177     BOOL Lockable = TRUE;
2178
2179     if((WINED3DPOOL_DEFAULT == Pool && WINED3DUSAGE_DYNAMIC != Usage))
2180         Lockable = FALSE;
2181
2182     TRACE("relay\n");
2183     res = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)device, Width, Height, (D3DFORMAT)Format, Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface8 **)&d3dSurface, D3DRTYPE_SURFACE, Usage, Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2184
2185     if (SUCCEEDED(res)) {
2186         *ppSurface = d3dSurface->wineD3DSurface;
2187         d3dSurface->container = pSuperior;
2188         IUnknown_Release(d3dSurface->parentDevice);
2189         d3dSurface->parentDevice = NULL;
2190         d3dSurface->forwardReference = pSuperior;
2191     } else {
2192         FIXME("(%p) IDirect3DDevice8_CreateSurface failed\n", device);
2193     }
2194     return res;
2195 }
2196
2197 ULONG WINAPI D3D8CB_DestroySurface(IWineD3DSurface *pSurface) {
2198     IDirect3DSurface8Impl* surfaceParent;
2199     TRACE("(%p) call back\n", pSurface);
2200
2201     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
2202     /* GetParent's AddRef was forwarded to an object in destruction.
2203      * Releasing it here again would cause an endless recursion. */
2204     surfaceParent->forwardReference = NULL;
2205     return IDirect3DSurface8_Release((IDirect3DSurface8*) surfaceParent);
2206 }