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