wined3d: Enabling too many lights is silently ignored.
[wine] / dlls / d3d9 / device.c
1 /*
2  * IDirect3DDevice9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28
29 /* IDirect3D IUnknown parts follow: */
30 static HRESULT WINAPI IDirect3DDevice9Impl_QueryInterface(LPDIRECT3DDEVICE9 iface, REFIID riid, LPVOID* ppobj) {
31     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
32
33     if (IsEqualGUID(riid, &IID_IUnknown)
34         || IsEqualGUID(riid, &IID_IDirect3DDevice9)) {
35         IUnknown_AddRef(iface);
36         *ppobj = This;
37         return S_OK;
38     }
39
40     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
41     *ppobj = NULL;
42     return E_NOINTERFACE;
43 }
44
45 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(LPDIRECT3DDEVICE9 iface) {
46     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
47     ULONG ref = InterlockedIncrement(&This->ref);
48
49     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
50
51     return ref;
52 }
53
54 static ULONG WINAPI IDirect3DDevice9Impl_Release(LPDIRECT3DDEVICE9 iface) {
55     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
56     ULONG ref;
57
58     if (This->inDestruction) return 0;
59     ref = InterlockedDecrement(&This->ref);
60
61     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
62
63     if (ref == 0) {
64       int i;
65       This->inDestruction = TRUE;
66
67       EnterCriticalSection(&d3d9_cs);
68       for(i = 0; i < This->numConvertedDecls; i++) {
69           /* Unless Wine is buggy or the app has a bug the refcount will be 0, because decls hold a reference to the
70            * device
71            */
72           IDirect3DVertexDeclaration9Impl_Destroy(This->convertedDecls[i]);
73       }
74       HeapFree(GetProcessHeap(), 0, This->convertedDecls);
75
76       IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D9CB_DestroyDepthStencilSurface, D3D9CB_DestroySwapChain);
77       IWineD3DDevice_Release(This->WineD3DDevice);
78       LeaveCriticalSection(&d3d9_cs);
79       HeapFree(GetProcessHeap(), 0, This);
80     }
81     return ref;
82 }
83
84 /* IDirect3DDevice Interface follow: */
85 static HRESULT  WINAPI  IDirect3DDevice9Impl_TestCooperativeLevel(LPDIRECT3DDEVICE9 iface) {
86     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
87     HRESULT hr;
88     TRACE("(%p) : Relay\n", This);
89
90     EnterCriticalSection(&d3d9_cs);
91     hr = IWineD3DDevice_TestCooperativeLevel(This->WineD3DDevice);
92     LeaveCriticalSection(&d3d9_cs);
93     return hr;
94 }
95
96 static UINT     WINAPI  IDirect3DDevice9Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE9 iface) {
97     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
98     HRESULT hr;
99     TRACE("(%p) Relay\n", This);
100
101     EnterCriticalSection(&d3d9_cs);
102     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
103     LeaveCriticalSection(&d3d9_cs);
104     return hr;
105 }
106
107 static HRESULT  WINAPI  IDirect3DDevice9Impl_EvictManagedResources(LPDIRECT3DDEVICE9 iface) {
108     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
109     HRESULT hr;
110     TRACE("(%p) : Relay\n", This);
111
112     EnterCriticalSection(&d3d9_cs);
113     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
114     LeaveCriticalSection(&d3d9_cs);
115     return hr;
116 }
117
118 HRESULT  WINAPI  IDirect3DDevice9Impl_GetDirect3D(LPDIRECT3DDEVICE9 iface, IDirect3D9** ppD3D9) {
119     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
120     HRESULT hr = D3D_OK;
121     IWineD3D* pWineD3D;
122
123     TRACE("(%p) Relay\n", This);
124
125     if (NULL == ppD3D9) {
126         return D3DERR_INVALIDCALL;
127     }
128
129     EnterCriticalSection(&d3d9_cs);
130     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
131     if (hr == D3D_OK && pWineD3D != NULL)
132     {
133         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D9);
134         IWineD3D_Release(pWineD3D);
135     } else {
136         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
137         *ppD3D9 = NULL;
138     }
139     TRACE("(%p) returning %p\n", This, *ppD3D9);
140     LeaveCriticalSection(&d3d9_cs);
141     return hr;
142 }
143
144 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDeviceCaps(LPDIRECT3DDEVICE9 iface, D3DCAPS9* pCaps) {
145     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
146     HRESULT hrc = D3D_OK;
147     WINED3DCAPS *pWineCaps;
148
149     TRACE("(%p) : Relay pCaps %p\n", This, pCaps);
150     if(NULL == pCaps){
151         return D3DERR_INVALIDCALL;
152     }
153     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
154     if(pWineCaps == NULL){
155         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
156     }
157
158     memset(pCaps, 0, sizeof(*pCaps));
159     D3D9CAPSTOWINECAPS(pCaps, pWineCaps)
160     EnterCriticalSection(&d3d9_cs);
161     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
162     LeaveCriticalSection(&d3d9_cs);
163     HeapFree(GetProcessHeap(), 0, pWineCaps);
164
165     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
166     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
167
168     filter_caps(pCaps);
169
170     TRACE("Returning %p %p\n", This, pCaps);
171     return hrc;
172 }
173
174 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDisplayMode(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, D3DDISPLAYMODE* pMode) {
175     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
176     HRESULT hr;
177     TRACE("(%p) Relay\n", This);
178
179     EnterCriticalSection(&d3d9_cs);
180     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, iSwapChain, (WINED3DDISPLAYMODE *) pMode);
181     LeaveCriticalSection(&d3d9_cs);
182     return hr;
183 }
184
185 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCreationParameters(LPDIRECT3DDEVICE9 iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
186     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
187     HRESULT hr;
188     TRACE("(%p) Relay\n", This);
189
190     EnterCriticalSection(&d3d9_cs);
191     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
192     LeaveCriticalSection(&d3d9_cs);
193     return hr;
194 }
195
196 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCursorProperties(LPDIRECT3DDEVICE9 iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9* pCursorBitmap) {
197     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
198     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pCursorBitmap;
199     HRESULT hr;
200
201     TRACE("(%p) Relay\n", This);
202     if(!pCursorBitmap) {
203         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
204         return WINED3DERR_INVALIDCALL;
205     }
206
207     EnterCriticalSection(&d3d9_cs);
208     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice, XHotSpot, YHotSpot, (IWineD3DSurface*)pSurface->wineD3DSurface);
209     LeaveCriticalSection(&d3d9_cs);
210     return hr;
211 }
212
213 static void     WINAPI  IDirect3DDevice9Impl_SetCursorPosition(LPDIRECT3DDEVICE9 iface, int XScreenSpace, int YScreenSpace, DWORD Flags) {
214     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
215     TRACE("(%p) Relay\n", This);
216
217     EnterCriticalSection(&d3d9_cs);
218     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
219     LeaveCriticalSection(&d3d9_cs);
220 }
221
222 static BOOL     WINAPI  IDirect3DDevice9Impl_ShowCursor(LPDIRECT3DDEVICE9 iface, BOOL bShow) {
223     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
224     BOOL ret;
225     TRACE("(%p) Relay\n", This);
226
227     EnterCriticalSection(&d3d9_cs);
228     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
229     LeaveCriticalSection(&d3d9_cs);
230     return ret;
231 }
232
233 static HRESULT  WINAPI  IDirect3DDevice9Impl_Reset(LPDIRECT3DDEVICE9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
234     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
235     WINED3DPRESENT_PARAMETERS localParameters;
236     HRESULT hr;
237
238     TRACE("(%p) Relay pPresentationParameters(%p)\n", This, pPresentationParameters);
239
240     localParameters.BackBufferWidth                     = pPresentationParameters->BackBufferWidth;
241     localParameters.BackBufferHeight                    = pPresentationParameters->BackBufferHeight;
242     localParameters.BackBufferFormat                    = pPresentationParameters->BackBufferFormat;
243     localParameters.BackBufferCount                     = pPresentationParameters->BackBufferCount;
244     localParameters.MultiSampleType                     = pPresentationParameters->MultiSampleType;
245     localParameters.MultiSampleQuality                  = pPresentationParameters->MultiSampleQuality;
246     localParameters.SwapEffect                          = pPresentationParameters->SwapEffect;
247     localParameters.hDeviceWindow                       = pPresentationParameters->hDeviceWindow;
248     localParameters.Windowed                            = pPresentationParameters->Windowed;
249     localParameters.EnableAutoDepthStencil              = pPresentationParameters->EnableAutoDepthStencil;
250     localParameters.AutoDepthStencilFormat              = pPresentationParameters->AutoDepthStencilFormat;
251     localParameters.Flags                               = pPresentationParameters->Flags;
252     localParameters.FullScreen_RefreshRateInHz          = pPresentationParameters->FullScreen_RefreshRateInHz;
253     localParameters.PresentationInterval                = pPresentationParameters->PresentationInterval;
254
255     EnterCriticalSection(&d3d9_cs);
256     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
257     LeaveCriticalSection(&d3d9_cs);
258
259     pPresentationParameters->BackBufferWidth            = localParameters.BackBufferWidth;
260     pPresentationParameters->BackBufferHeight           = localParameters.BackBufferHeight;
261     pPresentationParameters->BackBufferFormat           = localParameters.BackBufferFormat;
262     pPresentationParameters->BackBufferCount            = localParameters.BackBufferCount;
263     pPresentationParameters->MultiSampleType            = localParameters.MultiSampleType;
264     pPresentationParameters->MultiSampleQuality         = localParameters.MultiSampleQuality;
265     pPresentationParameters->SwapEffect                 = localParameters.SwapEffect;
266     pPresentationParameters->hDeviceWindow              = localParameters.hDeviceWindow;
267     pPresentationParameters->Windowed                   = localParameters.Windowed;
268     pPresentationParameters->EnableAutoDepthStencil     = localParameters.EnableAutoDepthStencil;
269     pPresentationParameters->AutoDepthStencilFormat     = localParameters.AutoDepthStencilFormat;
270     pPresentationParameters->Flags                      = localParameters.Flags;
271     pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
272     pPresentationParameters->PresentationInterval       = localParameters.PresentationInterval;
273
274     return hr;
275 }
276
277 static HRESULT  WINAPI  IDirect3DDevice9Impl_Present(LPDIRECT3DDEVICE9 iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA*
278  pDirtyRegion) {
279     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
280     HRESULT hr;
281     TRACE("(%p) Relay\n", This);
282
283     EnterCriticalSection(&d3d9_cs);
284     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
285     LeaveCriticalSection(&d3d9_cs);
286     return hr;
287  }
288
289 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetBackBuffer(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 ** ppBackBuffer) {
290     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
291     IWineD3DSurface *retSurface = NULL;
292     HRESULT rc = D3D_OK;
293
294     TRACE("(%p) Relay\n", This);
295
296     EnterCriticalSection(&d3d9_cs);
297     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, iSwapChain, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, (IWineD3DSurface **)&retSurface);
298     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
299         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
300         IWineD3DSurface_Release(retSurface);
301     }
302     LeaveCriticalSection(&d3d9_cs);
303     return rc;
304 }
305 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRasterStatus(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, D3DRASTER_STATUS* pRasterStatus) {
306     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
307     HRESULT hr;
308     TRACE("(%p) Relay\n", This);
309
310     EnterCriticalSection(&d3d9_cs);
311     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, iSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
312     LeaveCriticalSection(&d3d9_cs);
313     return hr;
314 }
315
316 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(LPDIRECT3DDEVICE9 iface, BOOL bEnableDialogs) {
317     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
318     HRESULT hr;
319     TRACE("(%p) Relay\n", This);
320
321     EnterCriticalSection(&d3d9_cs);
322     hr = IWineD3DDevice_SetDialogBoxMode(This->WineD3DDevice, bEnableDialogs);
323     LeaveCriticalSection(&d3d9_cs);
324     return hr;
325 }
326
327 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
328     
329     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
330     TRACE("(%p) Relay\n", This);
331
332     EnterCriticalSection(&d3d9_cs);
333     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
334     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, iSwapChain, Flags, (CONST WINED3DGAMMARAMP *)pRamp);
335     LeaveCriticalSection(&d3d9_cs);
336 }
337
338 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, D3DGAMMARAMP* pRamp) {    
339     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
340     TRACE("(%p) Relay\n", This);
341
342     EnterCriticalSection(&d3d9_cs);
343     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
344     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, iSwapChain, (WINED3DGAMMARAMP *) pRamp);
345     LeaveCriticalSection(&d3d9_cs);
346 }
347
348
349 static HRESULT  WINAPI IDirect3DDevice9Impl_CreateSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level, IDirect3DSurface9 **ppSurface,D3DRESOURCETYPE Type, UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality,HANDLE* pSharedHandle )  {
350     HRESULT hrc;
351     IDirect3DSurface9Impl *object;
352     IDirect3DDevice9Impl  *This = (IDirect3DDevice9Impl *)iface;
353     TRACE("(%p) Relay\n", This);
354     if(MultisampleQuality < 0) { 
355         FIXME("MultisampleQuality out of range %d, substituting 0\n", MultisampleQuality);
356     /*FIXME: Find out what windows does with a MultisampleQuality < 0 */
357         MultisampleQuality=0;
358     }
359     
360     if(MultisampleQuality > 0){
361         FIXME("MultisampleQuality set to %d, bstituting 0\n", MultisampleQuality);
362     /*
363     MultisampleQuality
364  [in] Quality level. The valid range is between zero and one less than the level returned by pQualityLevels used by IDirect3D9::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.
365  */
366  
367         MultisampleQuality=0;
368     }
369     /*FIXME: Check MAX bounds of MultisampleQuality*/
370
371     /* Allocate the storage for the device */
372     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
373     if (NULL == object) {
374         FIXME("Allocation of memory failed\n");
375         return D3DERR_OUTOFVIDEOMEMORY;
376     }
377
378     object->lpVtbl = &Direct3DSurface9_Vtbl;
379     object->ref = 1;
380     
381     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
382            
383     hrc = IWineD3DDevice_CreateSurface(This->WineD3DDevice, Width, Height, Format, Lockable, Discard, Level,  &object->wineD3DSurface, Type, Usage & WINED3DUSAGE_MASK, (WINED3DPOOL) Pool,MultiSample,MultisampleQuality,pSharedHandle,SURFACE_OPENGL,(IUnknown *)object);
384     
385     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
386
387        /* free up object */
388         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
389         HeapFree(GetProcessHeap(), 0, object);
390     } else {
391         IUnknown_AddRef(iface);
392         object->parentDevice = iface;
393         TRACE("(%p) : Created surface %p\n", This, object);
394         *ppSurface = (LPDIRECT3DSURFACE9) object;
395     }
396     return hrc;
397 }
398
399
400
401 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateRenderTarget(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, 
402                                                          D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, 
403                                                          DWORD MultisampleQuality, BOOL Lockable, 
404                                                          IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
405     HRESULT hr;
406     TRACE("Relay\n");
407
408    /* Is this correct? */
409    EnterCriticalSection(&d3d9_cs);
410    hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,Lockable,FALSE/*Discard*/, 0/*Level*/, ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_RENDERTARGET,D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
411    LeaveCriticalSection(&d3d9_cs);
412    return hr;
413 }
414
415 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height,
416                                                                 D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
417                                                                 DWORD MultisampleQuality, BOOL Discard,
418                                                                 IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
419     HRESULT hr;
420     TRACE("Relay\n");
421
422      EnterCriticalSection(&d3d9_cs);
423      hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,TRUE/* Lockable */,Discard, 0/* Level */
424                                                ,ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_DEPTHSTENCIL,
425                                                 D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
426      LeaveCriticalSection(&d3d9_cs);
427      return hr;
428 }
429
430
431 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestinationSurface, CONST POINT* pDestPoint) {
432     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
433     HRESULT hr;
434     TRACE("(%p) Relay\n" , This);
435
436     EnterCriticalSection(&d3d9_cs);
437     hr = IWineD3DDevice_UpdateSurface(This->WineD3DDevice, ((IDirect3DSurface9Impl *)pSourceSurface)->wineD3DSurface, pSourceRect, ((IDirect3DSurface9Impl *)pDestinationSurface)->wineD3DSurface, pDestPoint);
438     LeaveCriticalSection(&d3d9_cs);
439     return hr;
440 }
441
442 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateTexture(LPDIRECT3DDEVICE9 iface, IDirect3DBaseTexture9* pSourceTexture, IDirect3DBaseTexture9* pDestinationTexture) {
443     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
444     HRESULT hr;
445     TRACE("(%p) Relay\n" , This);
446
447     EnterCriticalSection(&d3d9_cs);
448     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture9Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture9Impl *)pDestinationTexture)->wineD3DBaseTexture);
449     LeaveCriticalSection(&d3d9_cs);
450     return hr;
451 }
452
453 /* This isn't in MSDN!
454 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBuffer(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pDestSurface) {
455     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
456     FIXME("(%p) : stub\n", This);
457     return D3D_OK;
458 }
459 */
460
461 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTargetData(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pRenderTarget, IDirect3DSurface9* pDestSurface) {
462     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
463     IDirect3DSurface9Impl *renderTarget = (IDirect3DSurface9Impl *)pRenderTarget;
464     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
465     HRESULT hr;
466     TRACE("(%p)->(%p,%p)\n" , This, renderTarget, destSurface);
467
468     EnterCriticalSection(&d3d9_cs);
469     hr = IWineD3DSurface_BltFast(destSurface->wineD3DSurface, 0, 0, renderTarget->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
470     LeaveCriticalSection(&d3d9_cs);
471     return hr;
472 }
473
474 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBufferData(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, IDirect3DSurface9* pDestSurface) {
475     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
476     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
477     HRESULT hr;
478     TRACE("(%p) Relay\n" , This);
479
480     EnterCriticalSection(&d3d9_cs);
481     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, iSwapChain, destSurface->wineD3DSurface);
482     LeaveCriticalSection(&d3d9_cs);
483     return hr;
484 }
485
486 static HRESULT  WINAPI  IDirect3DDevice9Impl_StretchRect(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestSurface, CONST RECT* pDestRect, D3DTEXTUREFILTERTYPE Filter) {
487     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
488     IDirect3DSurface9Impl *src = (IDirect3DSurface9Impl *) pSourceSurface;
489     IDirect3DSurface9Impl *dst = (IDirect3DSurface9Impl *) pDestSurface;
490     HRESULT hr;
491
492     TRACE("(%p)->(%p,%p,%p,%p,%d)\n" , This, src, pSourceRect, dst, pDestRect, Filter);
493     EnterCriticalSection(&d3d9_cs);
494     hr = IWineD3DSurface_Blt(dst->wineD3DSurface, (RECT *) pDestRect, src->wineD3DSurface, (RECT *) pSourceRect, 0, NULL, Filter);
495     LeaveCriticalSection(&d3d9_cs);
496     return hr;
497 }
498
499 static HRESULT  WINAPI  IDirect3DDevice9Impl_ColorFill(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSurface, CONST RECT* pRect, D3DCOLOR color) {
500     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
501     IDirect3DSurface9Impl *surface = (IDirect3DSurface9Impl *)pSurface;
502     HRESULT hr;
503     TRACE("(%p) Relay\n" , This);
504
505     /* Note: D3DRECT is compatible with WINED3DRECT */
506     EnterCriticalSection(&d3d9_cs);
507     hr = IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, (CONST WINED3DRECT*)pRect, color);
508     LeaveCriticalSection(&d3d9_cs);
509     return hr;
510 }
511
512 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
513     HRESULT hr;
514     TRACE("Relay\n");
515     if(Pool == D3DPOOL_MANAGED ){
516         FIXME("Attempting to create a managed offscreen plain surface\n");
517         return D3DERR_INVALIDCALL;
518     }    
519         /*MSDN: D3DPOOL_SCRATCH will return a surface that has identical characteristics to a surface created by the Microsoft DirectX 8.x method CreateImageSurface.
520         
521         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
522         but then...
523         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
524         Why, their always lockable?
525         should I change the usage to dynamic?        
526         */
527     EnterCriticalSection(&d3d9_cs);
528     hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,TRUE/*Loackable*/,FALSE/*Discard*/,0/*Level*/ , ppSurface,D3DRTYPE_SURFACE, 0/*Usage (undefined/none)*/,(WINED3DPOOL) Pool,D3DMULTISAMPLE_NONE,0/*MultisampleQuality*/,pSharedHandle);
529     LeaveCriticalSection(&d3d9_cs);
530     return hr;
531 }
532
533 /* TODO: move to wineD3D */
534 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
535     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
536     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
537     HRESULT hr;
538     TRACE("(%p) Relay\n" , This);
539
540     EnterCriticalSection(&d3d9_cs);
541     hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? (IWineD3DSurface*)pSurface->wineD3DSurface : NULL);
542     LeaveCriticalSection(&d3d9_cs);
543     return hr;
544 }
545
546 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget) {
547     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
548     HRESULT hr = D3D_OK;
549     IWineD3DSurface *pRenderTarget;
550
551     TRACE("(%p) Relay\n" , This);
552
553     if (ppRenderTarget == NULL) {
554         return D3DERR_INVALIDCALL;
555     }
556
557     EnterCriticalSection(&d3d9_cs);
558     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
559
560     if (hr == D3D_OK && pRenderTarget != NULL) {
561         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
562         IWineD3DSurface_Release(pRenderTarget);
563     } else {
564         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
565         *ppRenderTarget = NULL;
566     }
567     LeaveCriticalSection(&d3d9_cs);
568
569     return hr;
570 }
571
572 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pZStencilSurface) {
573     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
574     IDirect3DSurface9Impl *pSurface;
575     HRESULT hr;
576     TRACE("(%p) Relay\n" , This);
577
578     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
579     EnterCriticalSection(&d3d9_cs);
580     hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL==pSurface ? NULL : pSurface->wineD3DSurface);
581     LeaveCriticalSection(&d3d9_cs);
582     return hr;
583 }
584
585 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9 **ppZStencilSurface) {
586     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
587     HRESULT hr = D3D_OK;
588     IWineD3DSurface *pZStencilSurface;
589
590     TRACE("(%p) Relay\n" , This);
591     if(ppZStencilSurface == NULL){
592         return D3DERR_INVALIDCALL;
593     }
594
595     EnterCriticalSection(&d3d9_cs);
596     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
597     if(hr == D3D_OK) {
598         if(pZStencilSurface != NULL){
599             IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
600             IWineD3DSurface_Release(pZStencilSurface);
601         } else {
602             *ppZStencilSurface = NULL;
603         }
604     } else {
605         WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed\n");
606         *ppZStencilSurface = NULL;
607     }
608     LeaveCriticalSection(&d3d9_cs);
609     return hr;
610 }
611
612 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9 iface) {
613     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
614     HRESULT hr;
615     TRACE("(%p) Relay\n" , This);
616
617     EnterCriticalSection(&d3d9_cs);
618     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
619     LeaveCriticalSection(&d3d9_cs);
620     return hr;
621 }
622
623 static HRESULT  WINAPI  IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9 iface) {
624     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
625     HRESULT hr;
626     TRACE("(%p) Relay\n" , This);
627
628     EnterCriticalSection(&d3d9_cs);
629     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
630     LeaveCriticalSection(&d3d9_cs);
631     return hr;
632 }
633
634 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
635     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
636     HRESULT hr;
637     TRACE("(%p) Relay\n" , This);
638
639     /* Note: D3DRECT is compatible with WINED3DRECT */
640     EnterCriticalSection(&d3d9_cs);
641     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
642     LeaveCriticalSection(&d3d9_cs);
643     return hr;
644 }
645
646 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
647     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
648     HRESULT hr;
649     TRACE("(%p) Relay\n" , This);
650
651     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
652     EnterCriticalSection(&d3d9_cs);
653     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
654     LeaveCriticalSection(&d3d9_cs);
655     return hr;
656 }
657
658 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
659     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
660     TRACE("(%p) Relay\n" , This);
661
662     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
663     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
664 }
665
666 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
667     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
668     HRESULT hr;
669     TRACE("(%p) Relay\n" , This);
670
671     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
672     EnterCriticalSection(&d3d9_cs);
673     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
674     LeaveCriticalSection(&d3d9_cs);
675     return hr;
676 }
677
678 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9 iface, CONST D3DVIEWPORT9* pViewport) {
679     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
680     HRESULT hr;
681     TRACE("(%p) Relay\n" , This);
682
683     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
684     EnterCriticalSection(&d3d9_cs);
685     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
686     LeaveCriticalSection(&d3d9_cs);
687     return hr;
688 }
689
690 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9 iface, D3DVIEWPORT9* pViewport) {
691     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
692     HRESULT hr;
693     TRACE("(%p) Relay\n" , This);
694
695     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
696     EnterCriticalSection(&d3d9_cs);
697     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
698     LeaveCriticalSection(&d3d9_cs);
699     return hr;
700 }
701
702 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9 iface, CONST D3DMATERIAL9* pMaterial) {
703     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
704     HRESULT hr;
705     TRACE("(%p) Relay\n" , This);
706
707     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
708     EnterCriticalSection(&d3d9_cs);
709     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
710     LeaveCriticalSection(&d3d9_cs);
711     return hr;
712 }
713
714 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9 iface, D3DMATERIAL9* pMaterial) {
715     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
716     HRESULT hr;
717     TRACE("(%p) Relay\n" , This);
718
719     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
720     EnterCriticalSection(&d3d9_cs);
721     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
722     LeaveCriticalSection(&d3d9_cs);
723     return hr;
724 }
725
726 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST D3DLIGHT9* pLight) {
727     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
728     HRESULT hr;
729     TRACE("(%p) Relay\n" , This);
730
731     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
732     EnterCriticalSection(&d3d9_cs);
733     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
734     LeaveCriticalSection(&d3d9_cs);
735     return hr;
736 }
737
738 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, D3DLIGHT9* pLight) {
739     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
740     HRESULT hr;
741     TRACE("(%p) Relay\n" , This);
742
743     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
744     EnterCriticalSection(&d3d9_cs);
745     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
746     LeaveCriticalSection(&d3d9_cs);
747     return hr;
748 }
749
750 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL Enable) {
751     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
752     HRESULT hr;
753     TRACE("(%p) Relay\n" , This);
754
755     EnterCriticalSection(&d3d9_cs);
756     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
757     LeaveCriticalSection(&d3d9_cs);
758     return hr;
759 }
760
761 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL* pEnable) {
762     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
763     HRESULT hr;
764     TRACE("(%p) Relay\n" , This);
765
766     EnterCriticalSection(&d3d9_cs);
767     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
768     LeaveCriticalSection(&d3d9_cs);
769     return hr;
770 }
771
772 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST float* pPlane) {
773     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
774     HRESULT hr;
775     TRACE("(%p) Relay\n" , This);
776
777     EnterCriticalSection(&d3d9_cs);
778     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
779     LeaveCriticalSection(&d3d9_cs);
780     return hr;
781 }
782
783 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, float* pPlane) {
784     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
785     HRESULT hr;
786     TRACE("(%p) Relay\n" , This);
787
788     EnterCriticalSection(&d3d9_cs);
789     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
790     LeaveCriticalSection(&d3d9_cs);
791     return hr;
792 }
793
794 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD Value) {
795     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
796     HRESULT hr;
797     TRACE("(%p) Relay\n" , This);
798
799     EnterCriticalSection(&d3d9_cs);
800     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
801     LeaveCriticalSection(&d3d9_cs);
802     return hr;
803 }
804
805 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
806     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
807     HRESULT hr;
808     TRACE("(%p) Relay\n" , This);
809
810     EnterCriticalSection(&d3d9_cs);
811     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
812     LeaveCriticalSection(&d3d9_cs);
813     return hr;
814 }
815
816 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9 iface, CONST D3DCLIPSTATUS9* pClipStatus) {
817     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
818     HRESULT hr;
819     TRACE("(%p) Relay\n" , This);
820
821     EnterCriticalSection(&d3d9_cs);
822     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
823     LeaveCriticalSection(&d3d9_cs);
824     return hr;
825 }
826
827 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9 iface, D3DCLIPSTATUS9* pClipStatus) {
828     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
829     HRESULT hr;
830     TRACE("(%p) Relay\n" , This);
831
832     EnterCriticalSection(&d3d9_cs);
833     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
834     LeaveCriticalSection(&d3d9_cs);
835     return hr;
836 }
837
838 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9 **ppTexture) {
839     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
840     IWineD3DBaseTexture *retTexture = NULL;
841     HRESULT rc = D3D_OK;
842
843     TRACE("(%p) Relay\n" , This);
844
845     if(ppTexture == NULL){
846         return D3DERR_INVALIDCALL;
847     }
848
849     EnterCriticalSection(&d3d9_cs);
850     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
851     if (rc == D3D_OK && NULL != retTexture) {
852         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
853         IWineD3DBaseTexture_Release(retTexture);
854     }else{
855         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
856         *ppTexture = NULL;
857     }
858     LeaveCriticalSection(&d3d9_cs);
859
860     return rc;
861 }
862
863 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
864     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
865     HRESULT hr;
866     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
867
868     EnterCriticalSection(&d3d9_cs);
869     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
870                                    pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture);
871     LeaveCriticalSection(&d3d9_cs);
872     return hr;
873 }
874
875 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD* pValue) {
876     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
877     HRESULT hr;
878     TRACE("(%p) Relay\n" , This);
879
880     EnterCriticalSection(&d3d9_cs);
881     hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
882     LeaveCriticalSection(&d3d9_cs);
883     return hr;
884 }
885
886 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
887     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
888     HRESULT hr;
889     TRACE("(%p) Relay\n" , This);
890
891     EnterCriticalSection(&d3d9_cs);
892     hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
893     LeaveCriticalSection(&d3d9_cs);
894     return hr;
895 }
896
897 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD* pValue) {
898     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
899     HRESULT hr;
900     TRACE("(%p) Relay\n" , This);
901
902     EnterCriticalSection(&d3d9_cs);
903     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
904     LeaveCriticalSection(&d3d9_cs);
905     return hr;
906 }
907
908 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
909     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
910     HRESULT hr;
911     TRACE("(%p) Relay\n" , This);
912
913     EnterCriticalSection(&d3d9_cs);
914     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);
915     LeaveCriticalSection(&d3d9_cs);
916     return hr;
917 }
918
919 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9 iface, DWORD* pNumPasses) {
920     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
921     HRESULT hr;
922     TRACE("(%p) Relay\n" , This);
923
924     EnterCriticalSection(&d3d9_cs);
925     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
926     LeaveCriticalSection(&d3d9_cs);
927     return hr;
928 }
929
930 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
931     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
932     HRESULT hr;
933     TRACE("(%p) Relay\n" , This);
934
935     EnterCriticalSection(&d3d9_cs);
936     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
937     LeaveCriticalSection(&d3d9_cs);
938     return hr;
939 }
940
941 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
942     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
943     HRESULT hr;
944     TRACE("(%p) Relay\n" , This);
945
946     EnterCriticalSection(&d3d9_cs);
947     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
948     LeaveCriticalSection(&d3d9_cs);
949     return hr;
950 }
951
952 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber) {
953     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
954     HRESULT hr;
955     TRACE("(%p) Relay\n" , This);
956
957     EnterCriticalSection(&d3d9_cs);
958     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
959     LeaveCriticalSection(&d3d9_cs);
960     return hr;
961 }
962
963 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT* PaletteNumber) {
964     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
965     HRESULT hr;
966     TRACE("(%p) Relay\n" , This);
967
968     EnterCriticalSection(&d3d9_cs);
969     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
970     LeaveCriticalSection(&d3d9_cs);
971     return hr;
972 }
973
974 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9 iface, CONST RECT* pRect) {
975     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
976     HRESULT hr;
977     TRACE("(%p) Relay\n" , This);
978
979     EnterCriticalSection(&d3d9_cs);
980     hr = IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);
981     LeaveCriticalSection(&d3d9_cs);
982     return hr;
983 }
984
985 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9 iface, RECT* pRect) {
986     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
987     HRESULT hr;
988     TRACE("(%p) Relay\n" , This);
989
990     EnterCriticalSection(&d3d9_cs);
991     hr = IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);
992     LeaveCriticalSection(&d3d9_cs);
993     return hr;
994 }
995
996 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface, BOOL bSoftware) {
997     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
998     HRESULT hr;
999     TRACE("(%p) Relay\n" , This);
1000
1001     EnterCriticalSection(&d3d9_cs);
1002     hr = IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
1003     LeaveCriticalSection(&d3d9_cs);
1004     return hr;
1005 }
1006
1007 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface) {
1008     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1009     HRESULT hr;
1010     TRACE("(%p) Relay\n" , This);
1011
1012     EnterCriticalSection(&d3d9_cs);
1013     hr = IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
1014     LeaveCriticalSection(&d3d9_cs);
1015     return hr;
1016 }
1017
1018 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9 iface, float nSegments) {
1019     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1020     HRESULT hr;
1021     TRACE("(%p) Relay\n" , This);
1022
1023     EnterCriticalSection(&d3d9_cs);
1024     hr = IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
1025     LeaveCriticalSection(&d3d9_cs);
1026     return hr;
1027 }
1028
1029 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9 iface) {
1030     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1031     HRESULT hr;
1032     TRACE("(%p) Relay\n" , This);
1033
1034     EnterCriticalSection(&d3d9_cs);
1035     hr = IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
1036     LeaveCriticalSection(&d3d9_cs);
1037     return hr;
1038 }
1039
1040 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1041     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1042     HRESULT hr;
1043     TRACE("(%p) Relay\n" , This);
1044
1045     EnterCriticalSection(&d3d9_cs);
1046     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1047     LeaveCriticalSection(&d3d9_cs);
1048     return hr;
1049 }
1050
1051 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType,
1052                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
1053     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1054     HRESULT hr;
1055     TRACE("(%p) Relay\n" , This);
1056
1057     /* D3D8 passes the baseVertexIndex in SetIndices, and due to the stateblock functions wined3d has to work that way */
1058     EnterCriticalSection(&d3d9_cs);
1059     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
1060     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1061     LeaveCriticalSection(&d3d9_cs);
1062     return hr;
1063 }
1064
1065 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1066     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1067     HRESULT hr;
1068     TRACE("(%p) Relay\n" , This);
1069
1070     EnterCriticalSection(&d3d9_cs);
1071     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1072     LeaveCriticalSection(&d3d9_cs);
1073     return hr;
1074 }
1075
1076 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
1077                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
1078                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1079     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1080     HRESULT hr;
1081     TRACE("(%p) Relay\n" , This);
1082
1083     EnterCriticalSection(&d3d9_cs);
1084     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1085                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1086     LeaveCriticalSection(&d3d9_cs);
1087     return hr;
1088 }
1089
1090 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9 iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
1091     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1092     IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
1093     HRESULT hr;
1094     TRACE("(%p) Relay\n" , This);
1095
1096     EnterCriticalSection(&d3d9_cs);
1097     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer9Impl *)pDestBuffer)->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags);
1098     LeaveCriticalSection(&d3d9_cs);
1099     return hr;
1100 }
1101
1102 IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
1103     HRESULT hr;
1104     D3DVERTEXELEMENT9* elements = NULL;
1105     IDirect3DVertexDeclaration9* pDecl = NULL;
1106     int p, low, high; /* deliberately signed */
1107     IDirect3DVertexDeclaration9  **convertedDecls = This->convertedDecls;
1108
1109     TRACE("Searching for declaration for fvf %08x... ", fvf);
1110
1111     low = 0;
1112     high = This->numConvertedDecls - 1;
1113     while(low <= high) {
1114         p = (low + high) >> 1;
1115         TRACE("%d ", p);
1116         if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
1117             TRACE("found %p\n", convertedDecls[p]);
1118             return convertedDecls[p];
1119         } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
1120             low = p + 1;
1121         } else {
1122             high = p - 1;
1123         }
1124     }
1125     TRACE("not found. Creating and inserting at position %d.\n", low);
1126
1127     hr = vdecl_convert_fvf(fvf, &elements);
1128     if (hr != S_OK) return NULL;
1129
1130     hr = IDirect3DDevice9Impl_CreateVertexDeclaration((IDirect3DDevice9 *) This, elements, &pDecl);
1131     HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
1132     if (hr != S_OK) return NULL;
1133
1134     if(This->declArraySize == This->numConvertedDecls) {
1135         int grow = max(This->declArraySize / 2, 8);
1136         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1137                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1138         if(!convertedDecls) {
1139             /* This will destroy it */
1140             IDirect3DVertexDeclaration9_Release(pDecl);
1141             return NULL;
1142         }
1143         This->convertedDecls = convertedDecls;
1144         This->declArraySize += grow;
1145     }
1146
1147     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
1148     convertedDecls[low] = pDecl;
1149     This->numConvertedDecls++;
1150
1151     /* Will prevent the decl from being destroyed */
1152     ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
1153     IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
1154
1155     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
1156     return pDecl;
1157 }
1158
1159 HRESULT  WINAPI  IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9 iface, DWORD FVF) {
1160     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1161     HRESULT hr;
1162     TRACE("(%p) Relay\n" , This);
1163
1164     EnterCriticalSection(&d3d9_cs);
1165     if (0 != FVF) {
1166          HRESULT hr;
1167          IDirect3DVertexDeclaration9* pDecl = getConvertedDecl(This, FVF);
1168
1169          if(!pDecl) {
1170              /* Any situation when this should happen, except out of memory? */
1171              ERR("Failed to create a converted vertex declaration\n");
1172              LeaveCriticalSection(&d3d9_cs);
1173              return D3DERR_DRIVERINTERNALERROR;
1174          }
1175
1176          hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, pDecl);
1177          if (hr != S_OK) {
1178              LeaveCriticalSection(&d3d9_cs);
1179              return hr;
1180          }
1181     }
1182     hr = IWineD3DDevice_SetFVF(This->WineD3DDevice, FVF);
1183     LeaveCriticalSection(&d3d9_cs);
1184
1185     return hr;
1186 }
1187
1188 HRESULT  WINAPI  IDirect3DDevice9Impl_GetFVF(LPDIRECT3DDEVICE9 iface, DWORD* pFVF) {
1189     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1190     HRESULT hr;
1191     TRACE("(%p) Relay\n" , This);
1192
1193     EnterCriticalSection(&d3d9_cs);
1194     hr = IWineD3DDevice_GetFVF(This->WineD3DDevice, pFVF);
1195     LeaveCriticalSection(&d3d9_cs);
1196     return hr;
1197 }
1198
1199 HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
1200     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1201     HRESULT hr;
1202     TRACE("(%p) Relay\n" , This);
1203
1204     EnterCriticalSection(&d3d9_cs);
1205     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1206                                           pStreamData==NULL ? NULL:((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer, 
1207                                           OffsetInBytes, Stride);
1208     LeaveCriticalSection(&d3d9_cs);
1209     return hr;
1210 }
1211
1212 HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride) {
1213     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1214     IWineD3DVertexBuffer *retStream = NULL;
1215     HRESULT rc = D3D_OK;
1216
1217     TRACE("(%p) Relay\n" , This);
1218
1219     if(pStream == NULL){
1220         return D3DERR_INVALIDCALL;
1221     }
1222
1223     EnterCriticalSection(&d3d9_cs);
1224     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, OffsetInBytes, pStride);
1225     if (rc == D3D_OK  && NULL != retStream) {
1226         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
1227         IWineD3DVertexBuffer_Release(retStream);
1228     }else{
1229         if (rc != D3D_OK){
1230             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
1231         }
1232         *pStream = NULL;
1233     }
1234     LeaveCriticalSection(&d3d9_cs);
1235
1236     return rc;
1237 }
1238
1239 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT Divider) {
1240     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1241     HRESULT hr;
1242     TRACE("(%p) Relay\n" , This);
1243
1244     EnterCriticalSection(&d3d9_cs);
1245     hr = IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
1246     LeaveCriticalSection(&d3d9_cs);
1247     return hr;
1248 }
1249
1250 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT* Divider) {
1251     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1252     HRESULT hr;
1253     TRACE("(%p) Relay\n" , This);
1254
1255     EnterCriticalSection(&d3d9_cs);
1256     hr = IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
1257     LeaveCriticalSection(&d3d9_cs);
1258     return hr;
1259 }
1260
1261 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9* pIndexData) {
1262     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1263     HRESULT hr;
1264     TRACE("(%p) Relay\n", This);
1265
1266     EnterCriticalSection(&d3d9_cs);
1267     hr = IWineD3DDevice_SetIndices(This->WineD3DDevice,
1268             pIndexData ? ((IDirect3DIndexBuffer9Impl *)pIndexData)->wineD3DIndexBuffer : NULL);
1269     LeaveCriticalSection(&d3d9_cs);
1270     return hr;
1271 }
1272
1273 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9 **ppIndexData) {
1274     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1275     IWineD3DIndexBuffer *retIndexData = NULL;
1276     HRESULT rc = D3D_OK;
1277
1278     TRACE("(%p) Relay\n", This);
1279
1280     if(ppIndexData == NULL){
1281         return D3DERR_INVALIDCALL;
1282     }
1283
1284     EnterCriticalSection(&d3d9_cs);
1285     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData);
1286     if (SUCCEEDED(rc) && retIndexData) {
1287         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1288         IWineD3DIndexBuffer_Release(retIndexData);
1289     } else {
1290         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
1291         *ppIndexData = NULL;
1292     }
1293     LeaveCriticalSection(&d3d9_cs);
1294     return rc;
1295 }
1296
1297 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1298     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1299     HRESULT hr;
1300     TRACE("(%p) Relay\n", This);
1301
1302     EnterCriticalSection(&d3d9_cs);
1303     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
1304     LeaveCriticalSection(&d3d9_cs);
1305     return hr;
1306 }
1307 /*http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/d3d/interfaces/idirect3ddevice9/DrawTriPatch.asp*/
1308 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
1309     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1310     HRESULT hr;
1311     TRACE("(%p) Relay\n", This);
1312
1313     EnterCriticalSection(&d3d9_cs);
1314     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
1315     LeaveCriticalSection(&d3d9_cs);
1316     return hr;
1317 }
1318
1319 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9 iface, UINT Handle) {
1320     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1321     HRESULT hr;
1322     TRACE("(%p) Relay\n", This);
1323
1324     EnterCriticalSection(&d3d9_cs);
1325     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
1326     LeaveCriticalSection(&d3d9_cs);
1327     return hr;
1328 }
1329
1330 const IDirect3DDevice9Vtbl Direct3DDevice9_Vtbl =
1331 {
1332     /* IUnknown */
1333     IDirect3DDevice9Impl_QueryInterface,
1334     IDirect3DDevice9Impl_AddRef,
1335     IDirect3DDevice9Impl_Release,
1336     /* IDirect3DDevice9 */
1337     IDirect3DDevice9Impl_TestCooperativeLevel,
1338     IDirect3DDevice9Impl_GetAvailableTextureMem,
1339     IDirect3DDevice9Impl_EvictManagedResources,
1340     IDirect3DDevice9Impl_GetDirect3D,
1341     IDirect3DDevice9Impl_GetDeviceCaps,
1342     IDirect3DDevice9Impl_GetDisplayMode,
1343     IDirect3DDevice9Impl_GetCreationParameters,
1344     IDirect3DDevice9Impl_SetCursorProperties,
1345     IDirect3DDevice9Impl_SetCursorPosition,
1346     IDirect3DDevice9Impl_ShowCursor,
1347     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
1348     IDirect3DDevice9Impl_GetSwapChain,
1349     IDirect3DDevice9Impl_GetNumberOfSwapChains,
1350     IDirect3DDevice9Impl_Reset,
1351     IDirect3DDevice9Impl_Present,
1352     IDirect3DDevice9Impl_GetBackBuffer,
1353     IDirect3DDevice9Impl_GetRasterStatus,
1354     IDirect3DDevice9Impl_SetDialogBoxMode,
1355     IDirect3DDevice9Impl_SetGammaRamp,
1356     IDirect3DDevice9Impl_GetGammaRamp,
1357     IDirect3DDevice9Impl_CreateTexture,
1358     IDirect3DDevice9Impl_CreateVolumeTexture,
1359     IDirect3DDevice9Impl_CreateCubeTexture,
1360     IDirect3DDevice9Impl_CreateVertexBuffer,
1361     IDirect3DDevice9Impl_CreateIndexBuffer,
1362     IDirect3DDevice9Impl_CreateRenderTarget,
1363     IDirect3DDevice9Impl_CreateDepthStencilSurface,
1364     IDirect3DDevice9Impl_UpdateSurface,
1365     IDirect3DDevice9Impl_UpdateTexture,
1366     IDirect3DDevice9Impl_GetRenderTargetData,
1367     IDirect3DDevice9Impl_GetFrontBufferData,
1368     IDirect3DDevice9Impl_StretchRect,
1369     IDirect3DDevice9Impl_ColorFill,
1370     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
1371     IDirect3DDevice9Impl_SetRenderTarget,
1372     IDirect3DDevice9Impl_GetRenderTarget,
1373     IDirect3DDevice9Impl_SetDepthStencilSurface,
1374     IDirect3DDevice9Impl_GetDepthStencilSurface,
1375     IDirect3DDevice9Impl_BeginScene,
1376     IDirect3DDevice9Impl_EndScene,
1377     IDirect3DDevice9Impl_Clear,
1378     IDirect3DDevice9Impl_SetTransform,
1379     IDirect3DDevice9Impl_GetTransform,
1380     IDirect3DDevice9Impl_MultiplyTransform,
1381     IDirect3DDevice9Impl_SetViewport,
1382     IDirect3DDevice9Impl_GetViewport,
1383     IDirect3DDevice9Impl_SetMaterial,
1384     IDirect3DDevice9Impl_GetMaterial,
1385     IDirect3DDevice9Impl_SetLight,
1386     IDirect3DDevice9Impl_GetLight,
1387     IDirect3DDevice9Impl_LightEnable,
1388     IDirect3DDevice9Impl_GetLightEnable,
1389     IDirect3DDevice9Impl_SetClipPlane,
1390     IDirect3DDevice9Impl_GetClipPlane,
1391     IDirect3DDevice9Impl_SetRenderState,
1392     IDirect3DDevice9Impl_GetRenderState,
1393     IDirect3DDevice9Impl_CreateStateBlock,
1394     IDirect3DDevice9Impl_BeginStateBlock,
1395     IDirect3DDevice9Impl_EndStateBlock,
1396     IDirect3DDevice9Impl_SetClipStatus,
1397     IDirect3DDevice9Impl_GetClipStatus,
1398     IDirect3DDevice9Impl_GetTexture,
1399     IDirect3DDevice9Impl_SetTexture,
1400     IDirect3DDevice9Impl_GetTextureStageState,
1401     IDirect3DDevice9Impl_SetTextureStageState,
1402     IDirect3DDevice9Impl_GetSamplerState,
1403     IDirect3DDevice9Impl_SetSamplerState,
1404     IDirect3DDevice9Impl_ValidateDevice,
1405     IDirect3DDevice9Impl_SetPaletteEntries,
1406     IDirect3DDevice9Impl_GetPaletteEntries,
1407     IDirect3DDevice9Impl_SetCurrentTexturePalette,
1408     IDirect3DDevice9Impl_GetCurrentTexturePalette,
1409     IDirect3DDevice9Impl_SetScissorRect,
1410     IDirect3DDevice9Impl_GetScissorRect,
1411     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
1412     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
1413     IDirect3DDevice9Impl_SetNPatchMode,
1414     IDirect3DDevice9Impl_GetNPatchMode,
1415     IDirect3DDevice9Impl_DrawPrimitive,
1416     IDirect3DDevice9Impl_DrawIndexedPrimitive,
1417     IDirect3DDevice9Impl_DrawPrimitiveUP,
1418     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
1419     IDirect3DDevice9Impl_ProcessVertices,
1420     IDirect3DDevice9Impl_CreateVertexDeclaration,
1421     IDirect3DDevice9Impl_SetVertexDeclaration,
1422     IDirect3DDevice9Impl_GetVertexDeclaration,
1423     IDirect3DDevice9Impl_SetFVF,
1424     IDirect3DDevice9Impl_GetFVF,
1425     IDirect3DDevice9Impl_CreateVertexShader,
1426     IDirect3DDevice9Impl_SetVertexShader,
1427     IDirect3DDevice9Impl_GetVertexShader,
1428     IDirect3DDevice9Impl_SetVertexShaderConstantF,
1429     IDirect3DDevice9Impl_GetVertexShaderConstantF,
1430     IDirect3DDevice9Impl_SetVertexShaderConstantI,
1431     IDirect3DDevice9Impl_GetVertexShaderConstantI,
1432     IDirect3DDevice9Impl_SetVertexShaderConstantB,
1433     IDirect3DDevice9Impl_GetVertexShaderConstantB,
1434     IDirect3DDevice9Impl_SetStreamSource,
1435     IDirect3DDevice9Impl_GetStreamSource,
1436     IDirect3DDevice9Impl_SetStreamSourceFreq,
1437     IDirect3DDevice9Impl_GetStreamSourceFreq,
1438     IDirect3DDevice9Impl_SetIndices,
1439     IDirect3DDevice9Impl_GetIndices,
1440     IDirect3DDevice9Impl_CreatePixelShader,
1441     IDirect3DDevice9Impl_SetPixelShader,
1442     IDirect3DDevice9Impl_GetPixelShader,
1443     IDirect3DDevice9Impl_SetPixelShaderConstantF,
1444     IDirect3DDevice9Impl_GetPixelShaderConstantF,
1445     IDirect3DDevice9Impl_SetPixelShaderConstantI,
1446     IDirect3DDevice9Impl_GetPixelShaderConstantI,
1447     IDirect3DDevice9Impl_SetPixelShaderConstantB,
1448     IDirect3DDevice9Impl_GetPixelShaderConstantB,
1449     IDirect3DDevice9Impl_DrawRectPatch,
1450     IDirect3DDevice9Impl_DrawTriPatch,
1451     IDirect3DDevice9Impl_DeletePatch,
1452     IDirect3DDevice9Impl_CreateQuery
1453 };
1454
1455
1456 /* Internal function called back during the CreateDevice to create a render target  */
1457 HRESULT WINAPI D3D9CB_CreateSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
1458                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1459                                          WINED3DCUBEMAP_FACES Face,IWineD3DSurface** ppSurface,
1460                                          HANDLE* pSharedHandle) {
1461
1462     HRESULT res = D3D_OK;
1463     IDirect3DSurface9Impl *d3dSurface = NULL;
1464     BOOL Lockable = TRUE;
1465     
1466     if((Pool == D3DPOOL_DEFAULT &&  Usage != D3DUSAGE_DYNAMIC)) 
1467         Lockable = FALSE;
1468         
1469     TRACE("relay\n");
1470     res = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9 *)device, Width, Height, (D3DFORMAT)Format,
1471                 Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface9 **)&d3dSurface, D3DRTYPE_SURFACE,
1472                 Usage, (D3DPOOL) Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */, pSharedHandle);  
1473
1474     if (SUCCEEDED(res)) {
1475         *ppSurface = d3dSurface->wineD3DSurface;
1476         d3dSurface->container = pSuperior;
1477         IUnknown_Release(d3dSurface->parentDevice);
1478         d3dSurface->parentDevice = NULL;
1479         d3dSurface->forwardReference = pSuperior;
1480     } else {
1481         FIXME("(%p) IDirect3DDevice9_CreateSurface failed\n", device);
1482     }
1483     return res;
1484 }
1485
1486 ULONG WINAPI D3D9CB_DestroySurface(IWineD3DSurface *pSurface) {
1487     IDirect3DSurface9Impl* surfaceParent;
1488     TRACE("(%p) call back\n", pSurface);
1489
1490     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
1491     /* GetParent's AddRef was forwarded to an object in destruction.
1492      * Releasing it here again would cause an endless recursion. */
1493     surfaceParent->forwardReference = NULL;
1494     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
1495 }