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