dinput: Silence incorrect warning and move it to a valid place.
[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       unsigned 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, 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, &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     
355     if(MultisampleQuality > 0){
356         FIXME("MultisampleQuality set to %d, bstituting 0\n", MultisampleQuality);
357     /*
358     MultisampleQuality
359  [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.
360  */
361  
362         MultisampleQuality=0;
363     }
364     /*FIXME: Check MAX bounds of MultisampleQuality*/
365
366     /* Allocate the storage for the device */
367     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
368     if (NULL == object) {
369         FIXME("Allocation of memory failed\n");
370         return D3DERR_OUTOFVIDEOMEMORY;
371     }
372
373     object->lpVtbl = &Direct3DSurface9_Vtbl;
374     object->ref = 1;
375     
376     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
377            
378     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);
379     
380     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
381
382        /* free up object */
383         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
384         HeapFree(GetProcessHeap(), 0, object);
385     } else {
386         IUnknown_AddRef(iface);
387         object->parentDevice = iface;
388         TRACE("(%p) : Created surface %p\n", This, object);
389         *ppSurface = (LPDIRECT3DSURFACE9) object;
390     }
391     return hrc;
392 }
393
394
395
396 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateRenderTarget(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, 
397                                                          D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, 
398                                                          DWORD MultisampleQuality, BOOL Lockable, 
399                                                          IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
400     HRESULT hr;
401     TRACE("Relay\n");
402
403    /* Is this correct? */
404    EnterCriticalSection(&d3d9_cs);
405    hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,Lockable,FALSE/*Discard*/, 0/*Level*/, ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_RENDERTARGET,D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
406    LeaveCriticalSection(&d3d9_cs);
407    return hr;
408 }
409
410 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height,
411                                                                 D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
412                                                                 DWORD MultisampleQuality, BOOL Discard,
413                                                                 IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
414     HRESULT hr;
415     TRACE("Relay\n");
416
417      EnterCriticalSection(&d3d9_cs);
418      hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,TRUE/* Lockable */,Discard, 0/* Level */
419                                                ,ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_DEPTHSTENCIL,
420                                                 D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
421      LeaveCriticalSection(&d3d9_cs);
422      return hr;
423 }
424
425
426 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestinationSurface, CONST POINT* pDestPoint) {
427     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
428     HRESULT hr;
429     TRACE("(%p) Relay\n" , This);
430
431     EnterCriticalSection(&d3d9_cs);
432     hr = IWineD3DDevice_UpdateSurface(This->WineD3DDevice, ((IDirect3DSurface9Impl *)pSourceSurface)->wineD3DSurface, pSourceRect, ((IDirect3DSurface9Impl *)pDestinationSurface)->wineD3DSurface, pDestPoint);
433     LeaveCriticalSection(&d3d9_cs);
434     return hr;
435 }
436
437 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateTexture(LPDIRECT3DDEVICE9 iface, IDirect3DBaseTexture9* pSourceTexture, IDirect3DBaseTexture9* pDestinationTexture) {
438     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
439     HRESULT hr;
440     TRACE("(%p) Relay\n" , This);
441
442     EnterCriticalSection(&d3d9_cs);
443     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture9Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture9Impl *)pDestinationTexture)->wineD3DBaseTexture);
444     LeaveCriticalSection(&d3d9_cs);
445     return hr;
446 }
447
448 /* This isn't in MSDN!
449 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBuffer(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pDestSurface) {
450     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
451     FIXME("(%p) : stub\n", This);
452     return D3D_OK;
453 }
454 */
455
456 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTargetData(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pRenderTarget, IDirect3DSurface9* pDestSurface) {
457     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
458     IDirect3DSurface9Impl *renderTarget = (IDirect3DSurface9Impl *)pRenderTarget;
459     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
460     HRESULT hr;
461     TRACE("(%p)->(%p,%p)\n" , This, renderTarget, destSurface);
462
463     EnterCriticalSection(&d3d9_cs);
464     hr = IWineD3DSurface_BltFast(destSurface->wineD3DSurface, 0, 0, renderTarget->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
465     LeaveCriticalSection(&d3d9_cs);
466     return hr;
467 }
468
469 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBufferData(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, IDirect3DSurface9* pDestSurface) {
470     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
471     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
472     HRESULT hr;
473     TRACE("(%p) Relay\n" , This);
474
475     EnterCriticalSection(&d3d9_cs);
476     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, iSwapChain, destSurface->wineD3DSurface);
477     LeaveCriticalSection(&d3d9_cs);
478     return hr;
479 }
480
481 static HRESULT  WINAPI  IDirect3DDevice9Impl_StretchRect(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestSurface, CONST RECT* pDestRect, D3DTEXTUREFILTERTYPE Filter) {
482     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
483     IDirect3DSurface9Impl *src = (IDirect3DSurface9Impl *) pSourceSurface;
484     IDirect3DSurface9Impl *dst = (IDirect3DSurface9Impl *) pDestSurface;
485     HRESULT hr;
486
487     TRACE("(%p)->(%p,%p,%p,%p,%d)\n" , This, src, pSourceRect, dst, pDestRect, Filter);
488     EnterCriticalSection(&d3d9_cs);
489     hr = IWineD3DSurface_Blt(dst->wineD3DSurface, (RECT *) pDestRect, src->wineD3DSurface, (RECT *) pSourceRect, 0, NULL, Filter);
490     LeaveCriticalSection(&d3d9_cs);
491     return hr;
492 }
493
494 static HRESULT  WINAPI  IDirect3DDevice9Impl_ColorFill(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSurface, CONST RECT* pRect, D3DCOLOR color) {
495     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
496     IDirect3DSurface9Impl *surface = (IDirect3DSurface9Impl *)pSurface;
497     HRESULT hr;
498     TRACE("(%p) Relay\n" , This);
499
500     /* Note: D3DRECT is compatible with WINED3DRECT */
501     EnterCriticalSection(&d3d9_cs);
502     hr = IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, (CONST WINED3DRECT*)pRect, color);
503     LeaveCriticalSection(&d3d9_cs);
504     return hr;
505 }
506
507 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
508     HRESULT hr;
509     TRACE("Relay\n");
510     if(Pool == D3DPOOL_MANAGED ){
511         FIXME("Attempting to create a managed offscreen plain surface\n");
512         return D3DERR_INVALIDCALL;
513     }    
514         /*MSDN: D3DPOOL_SCRATCH will return a surface that has identical characteristics to a surface created by the Microsoft DirectX 8.x method CreateImageSurface.
515         
516         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
517         but then...
518         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
519         Why, their always lockable?
520         should I change the usage to dynamic?        
521         */
522     EnterCriticalSection(&d3d9_cs);
523     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);
524     LeaveCriticalSection(&d3d9_cs);
525     return hr;
526 }
527
528 /* TODO: move to wineD3D */
529 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
530     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
531     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
532     HRESULT hr;
533     TRACE("(%p) Relay\n" , This);
534
535     EnterCriticalSection(&d3d9_cs);
536     hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? pSurface->wineD3DSurface : NULL);
537     LeaveCriticalSection(&d3d9_cs);
538     return hr;
539 }
540
541 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget) {
542     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
543     HRESULT hr = D3D_OK;
544     IWineD3DSurface *pRenderTarget;
545
546     TRACE("(%p) Relay\n" , This);
547
548     if (ppRenderTarget == NULL) {
549         return D3DERR_INVALIDCALL;
550     }
551
552     EnterCriticalSection(&d3d9_cs);
553     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
554
555     if (hr == D3D_OK && pRenderTarget != NULL) {
556         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
557         IWineD3DSurface_Release(pRenderTarget);
558     } else {
559         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
560         *ppRenderTarget = NULL;
561     }
562     LeaveCriticalSection(&d3d9_cs);
563
564     return hr;
565 }
566
567 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pZStencilSurface) {
568     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
569     IDirect3DSurface9Impl *pSurface;
570     HRESULT hr;
571     TRACE("(%p) Relay\n" , This);
572
573     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
574     EnterCriticalSection(&d3d9_cs);
575     hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL==pSurface ? NULL : pSurface->wineD3DSurface);
576     LeaveCriticalSection(&d3d9_cs);
577     return hr;
578 }
579
580 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9 **ppZStencilSurface) {
581     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
582     HRESULT hr = D3D_OK;
583     IWineD3DSurface *pZStencilSurface;
584
585     TRACE("(%p) Relay\n" , This);
586     if(ppZStencilSurface == NULL){
587         return D3DERR_INVALIDCALL;
588     }
589
590     EnterCriticalSection(&d3d9_cs);
591     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
592     if(hr == D3D_OK) {
593         if(pZStencilSurface != NULL){
594             IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
595             IWineD3DSurface_Release(pZStencilSurface);
596         } else {
597             *ppZStencilSurface = NULL;
598         }
599     } else {
600         WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed\n");
601         *ppZStencilSurface = NULL;
602     }
603     LeaveCriticalSection(&d3d9_cs);
604     return hr;
605 }
606
607 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9 iface) {
608     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
609     HRESULT hr;
610     TRACE("(%p) Relay\n" , This);
611
612     EnterCriticalSection(&d3d9_cs);
613     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
614     LeaveCriticalSection(&d3d9_cs);
615     return hr;
616 }
617
618 static HRESULT  WINAPI  IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9 iface) {
619     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
620     HRESULT hr;
621     TRACE("(%p) Relay\n" , This);
622
623     EnterCriticalSection(&d3d9_cs);
624     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
625     LeaveCriticalSection(&d3d9_cs);
626     return hr;
627 }
628
629 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
630     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
631     HRESULT hr;
632     TRACE("(%p) Relay\n" , This);
633
634     /* Note: D3DRECT is compatible with WINED3DRECT */
635     EnterCriticalSection(&d3d9_cs);
636     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
637     LeaveCriticalSection(&d3d9_cs);
638     return hr;
639 }
640
641 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
642     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
643     HRESULT hr;
644     TRACE("(%p) Relay\n" , This);
645
646     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
647     EnterCriticalSection(&d3d9_cs);
648     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
649     LeaveCriticalSection(&d3d9_cs);
650     return hr;
651 }
652
653 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
654     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
655     TRACE("(%p) Relay\n" , This);
656
657     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
658     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
659 }
660
661 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
662     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
663     HRESULT hr;
664     TRACE("(%p) Relay\n" , This);
665
666     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
667     EnterCriticalSection(&d3d9_cs);
668     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
669     LeaveCriticalSection(&d3d9_cs);
670     return hr;
671 }
672
673 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9 iface, CONST D3DVIEWPORT9* pViewport) {
674     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
675     HRESULT hr;
676     TRACE("(%p) Relay\n" , This);
677
678     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
679     EnterCriticalSection(&d3d9_cs);
680     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
681     LeaveCriticalSection(&d3d9_cs);
682     return hr;
683 }
684
685 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9 iface, D3DVIEWPORT9* pViewport) {
686     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
687     HRESULT hr;
688     TRACE("(%p) Relay\n" , This);
689
690     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
691     EnterCriticalSection(&d3d9_cs);
692     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
693     LeaveCriticalSection(&d3d9_cs);
694     return hr;
695 }
696
697 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9 iface, CONST D3DMATERIAL9* pMaterial) {
698     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
699     HRESULT hr;
700     TRACE("(%p) Relay\n" , This);
701
702     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
703     EnterCriticalSection(&d3d9_cs);
704     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
705     LeaveCriticalSection(&d3d9_cs);
706     return hr;
707 }
708
709 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9 iface, D3DMATERIAL9* pMaterial) {
710     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
711     HRESULT hr;
712     TRACE("(%p) Relay\n" , This);
713
714     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
715     EnterCriticalSection(&d3d9_cs);
716     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
717     LeaveCriticalSection(&d3d9_cs);
718     return hr;
719 }
720
721 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST D3DLIGHT9* pLight) {
722     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
723     HRESULT hr;
724     TRACE("(%p) Relay\n" , This);
725
726     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
727     EnterCriticalSection(&d3d9_cs);
728     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
729     LeaveCriticalSection(&d3d9_cs);
730     return hr;
731 }
732
733 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, D3DLIGHT9* pLight) {
734     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
735     HRESULT hr;
736     TRACE("(%p) Relay\n" , This);
737
738     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
739     EnterCriticalSection(&d3d9_cs);
740     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
741     LeaveCriticalSection(&d3d9_cs);
742     return hr;
743 }
744
745 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL Enable) {
746     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
747     HRESULT hr;
748     TRACE("(%p) Relay\n" , This);
749
750     EnterCriticalSection(&d3d9_cs);
751     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
752     LeaveCriticalSection(&d3d9_cs);
753     return hr;
754 }
755
756 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL* pEnable) {
757     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
758     HRESULT hr;
759     TRACE("(%p) Relay\n" , This);
760
761     EnterCriticalSection(&d3d9_cs);
762     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
763     LeaveCriticalSection(&d3d9_cs);
764     return hr;
765 }
766
767 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST float* pPlane) {
768     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
769     HRESULT hr;
770     TRACE("(%p) Relay\n" , This);
771
772     EnterCriticalSection(&d3d9_cs);
773     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
774     LeaveCriticalSection(&d3d9_cs);
775     return hr;
776 }
777
778 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, float* pPlane) {
779     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
780     HRESULT hr;
781     TRACE("(%p) Relay\n" , This);
782
783     EnterCriticalSection(&d3d9_cs);
784     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
785     LeaveCriticalSection(&d3d9_cs);
786     return hr;
787 }
788
789 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD Value) {
790     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
791     HRESULT hr;
792     TRACE("(%p) Relay\n" , This);
793
794     EnterCriticalSection(&d3d9_cs);
795     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
796     LeaveCriticalSection(&d3d9_cs);
797     return hr;
798 }
799
800 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
801     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
802     HRESULT hr;
803     TRACE("(%p) Relay\n" , This);
804
805     EnterCriticalSection(&d3d9_cs);
806     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
807     LeaveCriticalSection(&d3d9_cs);
808     return hr;
809 }
810
811 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9 iface, CONST D3DCLIPSTATUS9* pClipStatus) {
812     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
813     HRESULT hr;
814     TRACE("(%p) Relay\n" , This);
815
816     EnterCriticalSection(&d3d9_cs);
817     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
818     LeaveCriticalSection(&d3d9_cs);
819     return hr;
820 }
821
822 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9 iface, D3DCLIPSTATUS9* pClipStatus) {
823     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
824     HRESULT hr;
825     TRACE("(%p) Relay\n" , This);
826
827     EnterCriticalSection(&d3d9_cs);
828     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
829     LeaveCriticalSection(&d3d9_cs);
830     return hr;
831 }
832
833 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9 **ppTexture) {
834     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
835     IWineD3DBaseTexture *retTexture = NULL;
836     HRESULT rc = D3D_OK;
837
838     TRACE("(%p) Relay\n" , This);
839
840     if(ppTexture == NULL){
841         return D3DERR_INVALIDCALL;
842     }
843
844     EnterCriticalSection(&d3d9_cs);
845     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
846     if (rc == D3D_OK && NULL != retTexture) {
847         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
848         IWineD3DBaseTexture_Release(retTexture);
849     }else{
850         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
851         *ppTexture = NULL;
852     }
853     LeaveCriticalSection(&d3d9_cs);
854
855     return rc;
856 }
857
858 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
859     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
860     HRESULT hr;
861     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
862
863     EnterCriticalSection(&d3d9_cs);
864     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
865                                    pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture);
866     LeaveCriticalSection(&d3d9_cs);
867     return hr;
868 }
869
870 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD* pValue) {
871     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
872     HRESULT hr;
873     TRACE("(%p) Relay\n" , This);
874
875     EnterCriticalSection(&d3d9_cs);
876     hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
877     LeaveCriticalSection(&d3d9_cs);
878     return hr;
879 }
880
881 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
882     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
883     HRESULT hr;
884     TRACE("(%p) Relay\n" , This);
885
886     EnterCriticalSection(&d3d9_cs);
887     hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
888     LeaveCriticalSection(&d3d9_cs);
889     return hr;
890 }
891
892 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD* pValue) {
893     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
894     HRESULT hr;
895     TRACE("(%p) Relay\n" , This);
896
897     EnterCriticalSection(&d3d9_cs);
898     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
899     LeaveCriticalSection(&d3d9_cs);
900     return hr;
901 }
902
903 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
904     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
905     HRESULT hr;
906     TRACE("(%p) Relay\n" , This);
907
908     EnterCriticalSection(&d3d9_cs);
909     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);
910     LeaveCriticalSection(&d3d9_cs);
911     return hr;
912 }
913
914 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9 iface, DWORD* pNumPasses) {
915     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
916     HRESULT hr;
917     TRACE("(%p) Relay\n" , This);
918
919     EnterCriticalSection(&d3d9_cs);
920     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
921     LeaveCriticalSection(&d3d9_cs);
922     return hr;
923 }
924
925 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
926     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
927     HRESULT hr;
928     TRACE("(%p) Relay\n" , This);
929
930     EnterCriticalSection(&d3d9_cs);
931     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
932     LeaveCriticalSection(&d3d9_cs);
933     return hr;
934 }
935
936 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
937     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
938     HRESULT hr;
939     TRACE("(%p) Relay\n" , This);
940
941     EnterCriticalSection(&d3d9_cs);
942     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
943     LeaveCriticalSection(&d3d9_cs);
944     return hr;
945 }
946
947 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber) {
948     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
949     HRESULT hr;
950     TRACE("(%p) Relay\n" , This);
951
952     EnterCriticalSection(&d3d9_cs);
953     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
954     LeaveCriticalSection(&d3d9_cs);
955     return hr;
956 }
957
958 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT* PaletteNumber) {
959     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
960     HRESULT hr;
961     TRACE("(%p) Relay\n" , This);
962
963     EnterCriticalSection(&d3d9_cs);
964     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
965     LeaveCriticalSection(&d3d9_cs);
966     return hr;
967 }
968
969 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9 iface, CONST RECT* pRect) {
970     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
971     HRESULT hr;
972     TRACE("(%p) Relay\n" , This);
973
974     EnterCriticalSection(&d3d9_cs);
975     hr = IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);
976     LeaveCriticalSection(&d3d9_cs);
977     return hr;
978 }
979
980 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9 iface, RECT* pRect) {
981     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
982     HRESULT hr;
983     TRACE("(%p) Relay\n" , This);
984
985     EnterCriticalSection(&d3d9_cs);
986     hr = IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);
987     LeaveCriticalSection(&d3d9_cs);
988     return hr;
989 }
990
991 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface, BOOL bSoftware) {
992     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
993     HRESULT hr;
994     TRACE("(%p) Relay\n" , This);
995
996     EnterCriticalSection(&d3d9_cs);
997     hr = IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
998     LeaveCriticalSection(&d3d9_cs);
999     return hr;
1000 }
1001
1002 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface) {
1003     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1004     HRESULT hr;
1005     TRACE("(%p) Relay\n" , This);
1006
1007     EnterCriticalSection(&d3d9_cs);
1008     hr = IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
1009     LeaveCriticalSection(&d3d9_cs);
1010     return hr;
1011 }
1012
1013 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9 iface, float nSegments) {
1014     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1015     HRESULT hr;
1016     TRACE("(%p) Relay\n" , This);
1017
1018     EnterCriticalSection(&d3d9_cs);
1019     hr = IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
1020     LeaveCriticalSection(&d3d9_cs);
1021     return hr;
1022 }
1023
1024 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9 iface) {
1025     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1026     HRESULT hr;
1027     TRACE("(%p) Relay\n" , This);
1028
1029     EnterCriticalSection(&d3d9_cs);
1030     hr = IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
1031     LeaveCriticalSection(&d3d9_cs);
1032     return hr;
1033 }
1034
1035 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1036     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1037     HRESULT hr;
1038     TRACE("(%p) Relay\n" , This);
1039
1040     EnterCriticalSection(&d3d9_cs);
1041     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1042     LeaveCriticalSection(&d3d9_cs);
1043     return hr;
1044 }
1045
1046 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType,
1047                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
1048     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1049     HRESULT hr;
1050     TRACE("(%p) Relay\n" , This);
1051
1052     /* D3D8 passes the baseVertexIndex in SetIndices, and due to the stateblock functions wined3d has to work that way */
1053     EnterCriticalSection(&d3d9_cs);
1054     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
1055     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1056     LeaveCriticalSection(&d3d9_cs);
1057     return hr;
1058 }
1059
1060 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1061     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1062     HRESULT hr;
1063     TRACE("(%p) Relay\n" , This);
1064
1065     EnterCriticalSection(&d3d9_cs);
1066     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1067     LeaveCriticalSection(&d3d9_cs);
1068     return hr;
1069 }
1070
1071 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
1072                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
1073                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1074     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1075     HRESULT hr;
1076     TRACE("(%p) Relay\n" , This);
1077
1078     EnterCriticalSection(&d3d9_cs);
1079     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1080                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1081     LeaveCriticalSection(&d3d9_cs);
1082     return hr;
1083 }
1084
1085 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9 iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
1086     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1087     IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
1088     HRESULT hr;
1089     TRACE("(%p) Relay\n" , This);
1090
1091     EnterCriticalSection(&d3d9_cs);
1092     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer9Impl *)pDestBuffer)->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags);
1093     LeaveCriticalSection(&d3d9_cs);
1094     return hr;
1095 }
1096
1097 IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
1098     HRESULT hr;
1099     D3DVERTEXELEMENT9* elements = NULL;
1100     IDirect3DVertexDeclaration9* pDecl = NULL;
1101     int p, low, high; /* deliberately signed */
1102     IDirect3DVertexDeclaration9  **convertedDecls = This->convertedDecls;
1103
1104     TRACE("Searching for declaration for fvf %08x... ", fvf);
1105
1106     low = 0;
1107     high = This->numConvertedDecls - 1;
1108     while(low <= high) {
1109         p = (low + high) >> 1;
1110         TRACE("%d ", p);
1111         if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
1112             TRACE("found %p\n", convertedDecls[p]);
1113             return convertedDecls[p];
1114         } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
1115             low = p + 1;
1116         } else {
1117             high = p - 1;
1118         }
1119     }
1120     TRACE("not found. Creating and inserting at position %d.\n", low);
1121
1122     hr = vdecl_convert_fvf(fvf, &elements);
1123     if (hr != S_OK) return NULL;
1124
1125     hr = IDirect3DDevice9Impl_CreateVertexDeclaration((IDirect3DDevice9 *) This, elements, &pDecl);
1126     HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
1127     if (hr != S_OK) return NULL;
1128
1129     if(This->declArraySize == This->numConvertedDecls) {
1130         int grow = max(This->declArraySize / 2, 8);
1131         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1132                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1133         if(!convertedDecls) {
1134             /* This will destroy it */
1135             IDirect3DVertexDeclaration9_Release(pDecl);
1136             return NULL;
1137         }
1138         This->convertedDecls = convertedDecls;
1139         This->declArraySize += grow;
1140     }
1141
1142     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
1143     convertedDecls[low] = pDecl;
1144     This->numConvertedDecls++;
1145
1146     /* Will prevent the decl from being destroyed */
1147     ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
1148     IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
1149
1150     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
1151     return pDecl;
1152 }
1153
1154 HRESULT  WINAPI  IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9 iface, DWORD FVF) {
1155     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1156     HRESULT hr;
1157     TRACE("(%p) Relay\n" , This);
1158
1159     EnterCriticalSection(&d3d9_cs);
1160     if (0 != FVF) {
1161          HRESULT hr;
1162          IDirect3DVertexDeclaration9* pDecl = getConvertedDecl(This, FVF);
1163
1164          if(!pDecl) {
1165              /* Any situation when this should happen, except out of memory? */
1166              ERR("Failed to create a converted vertex declaration\n");
1167              LeaveCriticalSection(&d3d9_cs);
1168              return D3DERR_DRIVERINTERNALERROR;
1169          }
1170
1171          hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, pDecl);
1172          if (hr != S_OK) {
1173              LeaveCriticalSection(&d3d9_cs);
1174              return hr;
1175          }
1176     }
1177     hr = IWineD3DDevice_SetFVF(This->WineD3DDevice, FVF);
1178     LeaveCriticalSection(&d3d9_cs);
1179
1180     return hr;
1181 }
1182
1183 HRESULT  WINAPI  IDirect3DDevice9Impl_GetFVF(LPDIRECT3DDEVICE9 iface, DWORD* pFVF) {
1184     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1185     HRESULT hr;
1186     TRACE("(%p) Relay\n" , This);
1187
1188     EnterCriticalSection(&d3d9_cs);
1189     hr = IWineD3DDevice_GetFVF(This->WineD3DDevice, pFVF);
1190     LeaveCriticalSection(&d3d9_cs);
1191     return hr;
1192 }
1193
1194 HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
1195     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1196     HRESULT hr;
1197     TRACE("(%p) Relay\n" , This);
1198
1199     EnterCriticalSection(&d3d9_cs);
1200     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1201                                           pStreamData==NULL ? NULL:((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer, 
1202                                           OffsetInBytes, Stride);
1203     LeaveCriticalSection(&d3d9_cs);
1204     return hr;
1205 }
1206
1207 HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride) {
1208     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1209     IWineD3DVertexBuffer *retStream = NULL;
1210     HRESULT rc = D3D_OK;
1211
1212     TRACE("(%p) Relay\n" , This);
1213
1214     if(pStream == NULL){
1215         return D3DERR_INVALIDCALL;
1216     }
1217
1218     EnterCriticalSection(&d3d9_cs);
1219     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, OffsetInBytes, pStride);
1220     if (rc == D3D_OK  && NULL != retStream) {
1221         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
1222         IWineD3DVertexBuffer_Release(retStream);
1223     }else{
1224         if (rc != D3D_OK){
1225             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
1226         }
1227         *pStream = NULL;
1228     }
1229     LeaveCriticalSection(&d3d9_cs);
1230
1231     return rc;
1232 }
1233
1234 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT Divider) {
1235     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1236     HRESULT hr;
1237     TRACE("(%p) Relay\n" , This);
1238
1239     EnterCriticalSection(&d3d9_cs);
1240     hr = IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
1241     LeaveCriticalSection(&d3d9_cs);
1242     return hr;
1243 }
1244
1245 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT* Divider) {
1246     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1247     HRESULT hr;
1248     TRACE("(%p) Relay\n" , This);
1249
1250     EnterCriticalSection(&d3d9_cs);
1251     hr = IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
1252     LeaveCriticalSection(&d3d9_cs);
1253     return hr;
1254 }
1255
1256 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9* pIndexData) {
1257     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1258     HRESULT hr;
1259     TRACE("(%p) Relay\n", This);
1260
1261     EnterCriticalSection(&d3d9_cs);
1262     hr = IWineD3DDevice_SetIndices(This->WineD3DDevice,
1263             pIndexData ? ((IDirect3DIndexBuffer9Impl *)pIndexData)->wineD3DIndexBuffer : NULL);
1264     LeaveCriticalSection(&d3d9_cs);
1265     return hr;
1266 }
1267
1268 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9 **ppIndexData) {
1269     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1270     IWineD3DIndexBuffer *retIndexData = NULL;
1271     HRESULT rc = D3D_OK;
1272
1273     TRACE("(%p) Relay\n", This);
1274
1275     if(ppIndexData == NULL){
1276         return D3DERR_INVALIDCALL;
1277     }
1278
1279     EnterCriticalSection(&d3d9_cs);
1280     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData);
1281     if (SUCCEEDED(rc) && retIndexData) {
1282         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1283         IWineD3DIndexBuffer_Release(retIndexData);
1284     } else {
1285         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
1286         *ppIndexData = NULL;
1287     }
1288     LeaveCriticalSection(&d3d9_cs);
1289     return rc;
1290 }
1291
1292 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1293     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1294     HRESULT hr;
1295     TRACE("(%p) Relay\n", This);
1296
1297     EnterCriticalSection(&d3d9_cs);
1298     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
1299     LeaveCriticalSection(&d3d9_cs);
1300     return hr;
1301 }
1302 /*http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/d3d/interfaces/idirect3ddevice9/DrawTriPatch.asp*/
1303 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
1304     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1305     HRESULT hr;
1306     TRACE("(%p) Relay\n", This);
1307
1308     EnterCriticalSection(&d3d9_cs);
1309     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
1310     LeaveCriticalSection(&d3d9_cs);
1311     return hr;
1312 }
1313
1314 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9 iface, UINT Handle) {
1315     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1316     HRESULT hr;
1317     TRACE("(%p) Relay\n", This);
1318
1319     EnterCriticalSection(&d3d9_cs);
1320     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
1321     LeaveCriticalSection(&d3d9_cs);
1322     return hr;
1323 }
1324
1325 const IDirect3DDevice9Vtbl Direct3DDevice9_Vtbl =
1326 {
1327     /* IUnknown */
1328     IDirect3DDevice9Impl_QueryInterface,
1329     IDirect3DDevice9Impl_AddRef,
1330     IDirect3DDevice9Impl_Release,
1331     /* IDirect3DDevice9 */
1332     IDirect3DDevice9Impl_TestCooperativeLevel,
1333     IDirect3DDevice9Impl_GetAvailableTextureMem,
1334     IDirect3DDevice9Impl_EvictManagedResources,
1335     IDirect3DDevice9Impl_GetDirect3D,
1336     IDirect3DDevice9Impl_GetDeviceCaps,
1337     IDirect3DDevice9Impl_GetDisplayMode,
1338     IDirect3DDevice9Impl_GetCreationParameters,
1339     IDirect3DDevice9Impl_SetCursorProperties,
1340     IDirect3DDevice9Impl_SetCursorPosition,
1341     IDirect3DDevice9Impl_ShowCursor,
1342     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
1343     IDirect3DDevice9Impl_GetSwapChain,
1344     IDirect3DDevice9Impl_GetNumberOfSwapChains,
1345     IDirect3DDevice9Impl_Reset,
1346     IDirect3DDevice9Impl_Present,
1347     IDirect3DDevice9Impl_GetBackBuffer,
1348     IDirect3DDevice9Impl_GetRasterStatus,
1349     IDirect3DDevice9Impl_SetDialogBoxMode,
1350     IDirect3DDevice9Impl_SetGammaRamp,
1351     IDirect3DDevice9Impl_GetGammaRamp,
1352     IDirect3DDevice9Impl_CreateTexture,
1353     IDirect3DDevice9Impl_CreateVolumeTexture,
1354     IDirect3DDevice9Impl_CreateCubeTexture,
1355     IDirect3DDevice9Impl_CreateVertexBuffer,
1356     IDirect3DDevice9Impl_CreateIndexBuffer,
1357     IDirect3DDevice9Impl_CreateRenderTarget,
1358     IDirect3DDevice9Impl_CreateDepthStencilSurface,
1359     IDirect3DDevice9Impl_UpdateSurface,
1360     IDirect3DDevice9Impl_UpdateTexture,
1361     IDirect3DDevice9Impl_GetRenderTargetData,
1362     IDirect3DDevice9Impl_GetFrontBufferData,
1363     IDirect3DDevice9Impl_StretchRect,
1364     IDirect3DDevice9Impl_ColorFill,
1365     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
1366     IDirect3DDevice9Impl_SetRenderTarget,
1367     IDirect3DDevice9Impl_GetRenderTarget,
1368     IDirect3DDevice9Impl_SetDepthStencilSurface,
1369     IDirect3DDevice9Impl_GetDepthStencilSurface,
1370     IDirect3DDevice9Impl_BeginScene,
1371     IDirect3DDevice9Impl_EndScene,
1372     IDirect3DDevice9Impl_Clear,
1373     IDirect3DDevice9Impl_SetTransform,
1374     IDirect3DDevice9Impl_GetTransform,
1375     IDirect3DDevice9Impl_MultiplyTransform,
1376     IDirect3DDevice9Impl_SetViewport,
1377     IDirect3DDevice9Impl_GetViewport,
1378     IDirect3DDevice9Impl_SetMaterial,
1379     IDirect3DDevice9Impl_GetMaterial,
1380     IDirect3DDevice9Impl_SetLight,
1381     IDirect3DDevice9Impl_GetLight,
1382     IDirect3DDevice9Impl_LightEnable,
1383     IDirect3DDevice9Impl_GetLightEnable,
1384     IDirect3DDevice9Impl_SetClipPlane,
1385     IDirect3DDevice9Impl_GetClipPlane,
1386     IDirect3DDevice9Impl_SetRenderState,
1387     IDirect3DDevice9Impl_GetRenderState,
1388     IDirect3DDevice9Impl_CreateStateBlock,
1389     IDirect3DDevice9Impl_BeginStateBlock,
1390     IDirect3DDevice9Impl_EndStateBlock,
1391     IDirect3DDevice9Impl_SetClipStatus,
1392     IDirect3DDevice9Impl_GetClipStatus,
1393     IDirect3DDevice9Impl_GetTexture,
1394     IDirect3DDevice9Impl_SetTexture,
1395     IDirect3DDevice9Impl_GetTextureStageState,
1396     IDirect3DDevice9Impl_SetTextureStageState,
1397     IDirect3DDevice9Impl_GetSamplerState,
1398     IDirect3DDevice9Impl_SetSamplerState,
1399     IDirect3DDevice9Impl_ValidateDevice,
1400     IDirect3DDevice9Impl_SetPaletteEntries,
1401     IDirect3DDevice9Impl_GetPaletteEntries,
1402     IDirect3DDevice9Impl_SetCurrentTexturePalette,
1403     IDirect3DDevice9Impl_GetCurrentTexturePalette,
1404     IDirect3DDevice9Impl_SetScissorRect,
1405     IDirect3DDevice9Impl_GetScissorRect,
1406     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
1407     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
1408     IDirect3DDevice9Impl_SetNPatchMode,
1409     IDirect3DDevice9Impl_GetNPatchMode,
1410     IDirect3DDevice9Impl_DrawPrimitive,
1411     IDirect3DDevice9Impl_DrawIndexedPrimitive,
1412     IDirect3DDevice9Impl_DrawPrimitiveUP,
1413     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
1414     IDirect3DDevice9Impl_ProcessVertices,
1415     IDirect3DDevice9Impl_CreateVertexDeclaration,
1416     IDirect3DDevice9Impl_SetVertexDeclaration,
1417     IDirect3DDevice9Impl_GetVertexDeclaration,
1418     IDirect3DDevice9Impl_SetFVF,
1419     IDirect3DDevice9Impl_GetFVF,
1420     IDirect3DDevice9Impl_CreateVertexShader,
1421     IDirect3DDevice9Impl_SetVertexShader,
1422     IDirect3DDevice9Impl_GetVertexShader,
1423     IDirect3DDevice9Impl_SetVertexShaderConstantF,
1424     IDirect3DDevice9Impl_GetVertexShaderConstantF,
1425     IDirect3DDevice9Impl_SetVertexShaderConstantI,
1426     IDirect3DDevice9Impl_GetVertexShaderConstantI,
1427     IDirect3DDevice9Impl_SetVertexShaderConstantB,
1428     IDirect3DDevice9Impl_GetVertexShaderConstantB,
1429     IDirect3DDevice9Impl_SetStreamSource,
1430     IDirect3DDevice9Impl_GetStreamSource,
1431     IDirect3DDevice9Impl_SetStreamSourceFreq,
1432     IDirect3DDevice9Impl_GetStreamSourceFreq,
1433     IDirect3DDevice9Impl_SetIndices,
1434     IDirect3DDevice9Impl_GetIndices,
1435     IDirect3DDevice9Impl_CreatePixelShader,
1436     IDirect3DDevice9Impl_SetPixelShader,
1437     IDirect3DDevice9Impl_GetPixelShader,
1438     IDirect3DDevice9Impl_SetPixelShaderConstantF,
1439     IDirect3DDevice9Impl_GetPixelShaderConstantF,
1440     IDirect3DDevice9Impl_SetPixelShaderConstantI,
1441     IDirect3DDevice9Impl_GetPixelShaderConstantI,
1442     IDirect3DDevice9Impl_SetPixelShaderConstantB,
1443     IDirect3DDevice9Impl_GetPixelShaderConstantB,
1444     IDirect3DDevice9Impl_DrawRectPatch,
1445     IDirect3DDevice9Impl_DrawTriPatch,
1446     IDirect3DDevice9Impl_DeletePatch,
1447     IDirect3DDevice9Impl_CreateQuery
1448 };
1449
1450
1451 /* Internal function called back during the CreateDevice to create a render target  */
1452 HRESULT WINAPI D3D9CB_CreateSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
1453                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1454                                          WINED3DCUBEMAP_FACES Face,IWineD3DSurface** ppSurface,
1455                                          HANDLE* pSharedHandle) {
1456
1457     HRESULT res = D3D_OK;
1458     IDirect3DSurface9Impl *d3dSurface = NULL;
1459     BOOL Lockable = TRUE;
1460     
1461     if((Pool == D3DPOOL_DEFAULT &&  Usage != D3DUSAGE_DYNAMIC)) 
1462         Lockable = FALSE;
1463         
1464     TRACE("relay\n");
1465     res = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9 *)device, Width, Height, (D3DFORMAT)Format,
1466                 Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface9 **)&d3dSurface, D3DRTYPE_SURFACE,
1467                 Usage, (D3DPOOL) Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */, pSharedHandle);  
1468
1469     if (SUCCEEDED(res)) {
1470         *ppSurface = d3dSurface->wineD3DSurface;
1471         d3dSurface->container = pSuperior;
1472         IUnknown_Release(d3dSurface->parentDevice);
1473         d3dSurface->parentDevice = NULL;
1474         d3dSurface->forwardReference = pSuperior;
1475     } else {
1476         FIXME("(%p) IDirect3DDevice9_CreateSurface failed\n", device);
1477     }
1478     return res;
1479 }
1480
1481 ULONG WINAPI D3D9CB_DestroySurface(IWineD3DSurface *pSurface) {
1482     IDirect3DSurface9Impl* surfaceParent;
1483     TRACE("(%p) call back\n", pSurface);
1484
1485     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
1486     /* GetParent's AddRef was forwarded to an object in destruction.
1487      * Releasing it here again would cause an endless recursion. */
1488     surfaceParent->forwardReference = NULL;
1489     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
1490 }