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