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