setupapi: Implement SetupGetFileCompressionInfo on top of SetupGetFileCompressionInfoEx.
[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       This->inDestruction = TRUE;
65       if (This->convertedDecl != NULL)
66           IUnknown_Release(This->convertedDecl);
67       IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D9CB_DestroyDepthStencilSurface, D3D9CB_DestroySwapChain);
68       IWineD3DDevice_Release(This->WineD3DDevice);
69       HeapFree(GetProcessHeap(), 0, This);
70     }
71     return ref;
72 }
73
74 /* IDirect3DDevice Interface follow: */
75 static HRESULT  WINAPI  IDirect3DDevice9Impl_TestCooperativeLevel(LPDIRECT3DDEVICE9 iface) {
76     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
77     
78     TRACE("(%p) : Relay\n", This);
79     return IWineD3DDevice_TestCooperativeLevel(This->WineD3DDevice);
80 }
81
82 static UINT     WINAPI  IDirect3DDevice9Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE9 iface) {
83     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
84     
85     TRACE("(%p) Relay\n", This);
86     return IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);    
87 }
88
89 static HRESULT  WINAPI  IDirect3DDevice9Impl_EvictManagedResources(LPDIRECT3DDEVICE9 iface) {
90     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
91
92     TRACE("(%p) : Relay\n", This);
93     return IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
94 }
95
96 HRESULT  WINAPI  IDirect3DDevice9Impl_GetDirect3D(LPDIRECT3DDEVICE9 iface, IDirect3D9** ppD3D9) {
97     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
98     HRESULT hr = D3D_OK;
99     IWineD3D* pWineD3D;
100
101     TRACE("(%p) Relay\n", This);
102
103     if (NULL == ppD3D9) {
104         return D3DERR_INVALIDCALL;
105     }
106     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
107     if (hr == D3D_OK && pWineD3D != NULL)
108     {
109         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D9);
110         IWineD3D_Release(pWineD3D);
111     } else {
112         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
113         *ppD3D9 = NULL;
114     }
115     TRACE("(%p) returning %p\n", This, *ppD3D9);
116     return hr;
117 }
118
119 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDeviceCaps(LPDIRECT3DDEVICE9 iface, D3DCAPS9* pCaps) {
120     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
121     HRESULT hrc = D3D_OK;
122     WINED3DCAPS *pWineCaps;
123
124     TRACE("(%p) : Relay pCaps %p\n", This, pCaps);
125     if(NULL == pCaps){
126         return D3DERR_INVALIDCALL;
127     }
128     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
129     if(pWineCaps == NULL){
130         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
131     }
132
133     D3D9CAPSTOWINECAPS(pCaps, pWineCaps)
134     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
135     HeapFree(GetProcessHeap(), 0, pWineCaps);
136     TRACE("Returning %p %p\n", This, pCaps);
137     return hrc;
138 }
139
140 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDisplayMode(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, D3DDISPLAYMODE* pMode) {
141     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
142     TRACE("(%p) Relay\n", This);
143     return IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, iSwapChain, (WINED3DDISPLAYMODE *) pMode);
144 }
145
146 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCreationParameters(LPDIRECT3DDEVICE9 iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
147     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
148     TRACE("(%p) Relay\n", This);
149     return IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
150 }
151
152 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCursorProperties(LPDIRECT3DDEVICE9 iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9* pCursorBitmap) {
153     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
154     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pCursorBitmap;
155     TRACE("(%p) Relay\n", This);
156     if(!pCursorBitmap) {
157         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
158         return WINED3DERR_INVALIDCALL;
159     }
160     return IWineD3DDevice_SetCursorProperties(This->WineD3DDevice,XHotSpot,YHotSpot,(IWineD3DSurface*)pSurface->wineD3DSurface);
161 }
162
163 static void     WINAPI  IDirect3DDevice9Impl_SetCursorPosition(LPDIRECT3DDEVICE9 iface, int XScreenSpace, int YScreenSpace, DWORD Flags) {
164     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
165     TRACE("(%p) Relay\n", This);
166     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
167 }
168
169 static BOOL     WINAPI  IDirect3DDevice9Impl_ShowCursor(LPDIRECT3DDEVICE9 iface, BOOL bShow) {
170     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
171     TRACE("(%p) Relay\n", This);
172
173     return IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
174 }
175
176 static HRESULT  WINAPI  IDirect3DDevice9Impl_Reset(LPDIRECT3DDEVICE9 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
177     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
178     WINED3DPRESENT_PARAMETERS localParameters;
179     HRESULT hr;
180
181     TRACE("(%p) Relay pPresentationParameters(%p)\n", This, pPresentationParameters);
182
183     localParameters.BackBufferWidth                     = pPresentationParameters->BackBufferWidth;
184     localParameters.BackBufferHeight                    = pPresentationParameters->BackBufferHeight;
185     localParameters.BackBufferFormat                    = pPresentationParameters->BackBufferFormat;
186     localParameters.BackBufferCount                     = pPresentationParameters->BackBufferCount;
187     localParameters.MultiSampleType                     = pPresentationParameters->MultiSampleType;
188     localParameters.MultiSampleQuality                  = pPresentationParameters->MultiSampleQuality;
189     localParameters.SwapEffect                          = pPresentationParameters->SwapEffect;
190     localParameters.hDeviceWindow                       = pPresentationParameters->hDeviceWindow;
191     localParameters.Windowed                            = pPresentationParameters->Windowed;
192     localParameters.EnableAutoDepthStencil              = pPresentationParameters->EnableAutoDepthStencil;
193     localParameters.AutoDepthStencilFormat              = pPresentationParameters->AutoDepthStencilFormat;
194     localParameters.Flags                               = pPresentationParameters->Flags;
195     localParameters.FullScreen_RefreshRateInHz          = pPresentationParameters->FullScreen_RefreshRateInHz;
196     localParameters.PresentationInterval                = pPresentationParameters->PresentationInterval;
197
198     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
199
200     pPresentationParameters->BackBufferWidth            = localParameters.BackBufferWidth;
201     pPresentationParameters->BackBufferHeight           = localParameters.BackBufferHeight;
202     pPresentationParameters->BackBufferFormat           = localParameters.BackBufferFormat;
203     pPresentationParameters->BackBufferCount            = localParameters.BackBufferCount;
204     pPresentationParameters->MultiSampleType            = localParameters.MultiSampleType;
205     pPresentationParameters->MultiSampleQuality         = localParameters.MultiSampleQuality;
206     pPresentationParameters->SwapEffect                 = localParameters.SwapEffect;
207     pPresentationParameters->hDeviceWindow              = localParameters.hDeviceWindow;
208     pPresentationParameters->Windowed                   = localParameters.Windowed;
209     pPresentationParameters->EnableAutoDepthStencil     = localParameters.EnableAutoDepthStencil;
210     pPresentationParameters->AutoDepthStencilFormat     = localParameters.AutoDepthStencilFormat;
211     pPresentationParameters->Flags                      = localParameters.Flags;
212     pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
213     pPresentationParameters->PresentationInterval       = localParameters.PresentationInterval;
214
215     return hr;
216 }
217
218 static HRESULT  WINAPI  IDirect3DDevice9Impl_Present(LPDIRECT3DDEVICE9 iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA*
219  pDirtyRegion) {
220     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
221     TRACE("(%p) Relay\n", This);
222     return IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
223 }
224
225 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetBackBuffer(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 ** ppBackBuffer) {
226     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
227     IWineD3DSurface *retSurface = NULL;
228     HRESULT rc = D3D_OK;
229
230     TRACE("(%p) Relay\n", This);
231
232     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, iSwapChain, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, (IWineD3DSurface **)&retSurface);
233     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
234         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
235         IWineD3DSurface_Release(retSurface);
236     }
237     return rc;
238 }
239 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRasterStatus(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, D3DRASTER_STATUS* pRasterStatus) {
240     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
241     TRACE("(%p) Relay\n", This);
242     
243     return IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, iSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);        
244 }
245
246 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(LPDIRECT3DDEVICE9 iface, BOOL bEnableDialogs) {
247     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
248     TRACE("(%p) Relay\n", This);
249     
250     return IWineD3DDevice_SetDialogBoxMode(This->WineD3DDevice, bEnableDialogs);
251 }
252
253 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
254     
255     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
256     TRACE("(%p) Relay\n", This);
257    
258     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */ 
259     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, iSwapChain, Flags, (CONST WINED3DGAMMARAMP *)pRamp);
260 }
261
262 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, D3DGAMMARAMP* pRamp) {    
263     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
264     TRACE("(%p) Relay\n", This);
265     
266     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
267     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, iSwapChain, (WINED3DGAMMARAMP *) pRamp);
268 }
269
270
271 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 )  {
272     HRESULT hrc;
273     IDirect3DSurface9Impl *object;
274     IDirect3DDevice9Impl  *This = (IDirect3DDevice9Impl *)iface;
275     TRACE("(%p) Relay\n", This);
276     if(MultisampleQuality < 0) { 
277         FIXME("MultisampleQuality out of range %d, substituting 0\n", MultisampleQuality);
278     /*FIXME: Find out what windows does with a MultisampleQuality < 0 */
279         MultisampleQuality=0;
280     }
281     
282     if(MultisampleQuality > 0){
283         FIXME("MultisampleQuality set to %d, bstituting 0\n", MultisampleQuality);
284     /*
285     MultisampleQuality
286  [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.
287  */
288  
289         MultisampleQuality=0;
290     }
291     /*FIXME: Check MAX bounds of MultisampleQuality*/
292
293     /* Allocate the storage for the device */
294     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
295     if (NULL == object) {
296         FIXME("Allocation of memory failed\n");
297         return D3DERR_OUTOFVIDEOMEMORY;
298     }
299
300     object->lpVtbl = &Direct3DSurface9_Vtbl;
301     object->ref = 1;
302     
303     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
304            
305     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);
306     
307     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
308
309        /* free up object */
310         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
311         HeapFree(GetProcessHeap(), 0, object);
312     } else {
313         IUnknown_AddRef(iface);
314         object->parentDevice = iface;
315         TRACE("(%p) : Created surface %p\n", This, object);
316         *ppSurface = (LPDIRECT3DSURFACE9) object;
317     }
318     return hrc;
319 }
320
321
322
323 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateRenderTarget(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, 
324                                                          D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, 
325                                                          DWORD MultisampleQuality, BOOL Lockable, 
326                                                          IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
327    TRACE("Relay\n");
328    /* Is this correct? */
329    return IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,Lockable,FALSE/*Discard*/, 0/*Level*/, ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_RENDERTARGET,D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
330      
331
332 }
333
334 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height,
335                                                                 D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
336                                                                 DWORD MultisampleQuality, BOOL Discard,
337                                                                 IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
338      TRACE("Relay\n");
339      return IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,TRUE/* Lockable */,Discard, 0/* Level */
340                                                ,ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_DEPTHSTENCIL,
341                                                 D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
342 }
343
344
345 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestinationSurface, CONST POINT* pDestPoint) {
346     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;     
347     TRACE("(%p) Relay\n" , This);
348     return IWineD3DDevice_UpdateSurface(This->WineD3DDevice, ((IDirect3DSurface9Impl *)pSourceSurface)->wineD3DSurface, pSourceRect, ((IDirect3DSurface9Impl *)pDestinationSurface)->wineD3DSurface, pDestPoint);
349 }
350
351 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateTexture(LPDIRECT3DDEVICE9 iface, IDirect3DBaseTexture9* pSourceTexture, IDirect3DBaseTexture9* pDestinationTexture) {
352     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
353     TRACE("(%p) Relay\n" , This);
354     return IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture9Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture9Impl *)pDestinationTexture)->wineD3DBaseTexture);
355 }
356
357 /* This isn't in MSDN!
358 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBuffer(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pDestSurface) {
359     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
360     FIXME("(%p) : stub\n", This);
361     return D3D_OK;
362 }
363 */
364
365 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTargetData(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pRenderTarget, IDirect3DSurface9* pDestSurface) {
366     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
367     IDirect3DSurface9Impl *renderTarget = (IDirect3DSurface9Impl *)pRenderTarget;
368     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
369     TRACE("(%p)->(%p,%p)\n" , This, renderTarget, destSurface);
370     return IWineD3DSurface_BltFast(destSurface->wineD3DSurface, 0, 0, renderTarget->wineD3DSurface, NULL, DDBLTFAST_NOCOLORKEY);
371 }
372
373 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBufferData(LPDIRECT3DDEVICE9 iface, UINT iSwapChain, IDirect3DSurface9* pDestSurface) {
374     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
375     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
376     TRACE("(%p) Relay\n" , This);
377     return IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, iSwapChain, destSurface->wineD3DSurface);    
378 }
379
380 static HRESULT  WINAPI  IDirect3DDevice9Impl_StretchRect(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestSurface, CONST RECT* pDestRect, D3DTEXTUREFILTERTYPE Filter) {
381     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
382     IDirect3DSurface9Impl *src = (IDirect3DSurface9Impl *) pSourceSurface;
383     IDirect3DSurface9Impl *dst = (IDirect3DSurface9Impl *) pDestSurface;
384
385     TRACE("(%p)->(%p,%p,%p,%p,%d)\n" , This, src, pSourceRect, dst, pDestRect, Filter);
386     return IWineD3DSurface_Blt(dst->wineD3DSurface, (RECT *) pDestRect, src->wineD3DSurface, (RECT *) pSourceRect, 0, NULL, Filter);
387 }
388
389 static HRESULT  WINAPI  IDirect3DDevice9Impl_ColorFill(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSurface, CONST RECT* pRect, D3DCOLOR color) {
390     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
391     IDirect3DSurface9Impl *surface = (IDirect3DSurface9Impl *)pSurface;
392     TRACE("(%p) Relay\n" , This);
393
394     /* Note: D3DRECT is compatible with WINED3DRECT */
395     return IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, (CONST WINED3DRECT*)pRect, color);    
396 }
397
398 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
399     TRACE("Relay\n");
400     if(Pool == D3DPOOL_MANAGED ){
401         FIXME("Attempting to create a managed offscreen plain surface\n");
402         return D3DERR_INVALIDCALL;
403     }    
404         /*MSDN: D3DPOOL_SCRATCH will return a surface that has identical characteristics to a surface created by the Microsoft DirectX 8.x method CreateImageSurface.
405         
406         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
407         but then...
408         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
409         Why, their always lockable?
410         should I change the usage to dynamic?        
411         */
412     return 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);
413 }
414
415 /* TODO: move to wineD3D */
416 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
417     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
418     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
419     TRACE("(%p) Relay\n" , This);
420     return IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? (IWineD3DSurface*)pSurface->wineD3DSurface : NULL);
421 }
422
423 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget) {
424     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
425     HRESULT hr = D3D_OK;
426     IWineD3DSurface *pRenderTarget;
427
428     TRACE("(%p) Relay\n" , This);
429
430     if (ppRenderTarget == NULL) {
431         return D3DERR_INVALIDCALL;
432     }
433     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
434
435     if (hr == D3D_OK && pRenderTarget != NULL) {
436         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
437         IWineD3DSurface_Release(pRenderTarget);
438     } else {
439         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
440         *ppRenderTarget = NULL;
441     }
442     return hr;
443 }
444
445 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pZStencilSurface) {
446     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
447     IDirect3DSurface9Impl *pSurface;
448
449     TRACE("(%p) Relay\n" , This);
450
451     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
452     return IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice,NULL==pSurface?NULL:(IWineD3DSurface*)pSurface->wineD3DSurface);
453 }
454
455 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9 **ppZStencilSurface) {
456     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
457     HRESULT hr = D3D_OK;
458     IWineD3DSurface *pZStencilSurface;
459
460     TRACE("(%p) Relay\n" , This);
461     if(ppZStencilSurface == NULL){
462         return D3DERR_INVALIDCALL;
463     }
464
465     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
466     if(hr == D3D_OK && pZStencilSurface != NULL){
467         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
468         IWineD3DSurface_Release(pZStencilSurface);
469     }else{
470         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
471         *ppZStencilSurface = NULL;
472     }
473     return D3D_OK;
474 }
475
476 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9 iface) {
477     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
478     TRACE("(%p) Relay\n" , This);
479     return IWineD3DDevice_BeginScene(This->WineD3DDevice);
480 }
481
482 static HRESULT  WINAPI  IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9 iface) {
483     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
484     TRACE("(%p) Relay\n" , This);
485     return IWineD3DDevice_EndScene(This->WineD3DDevice);
486     
487 }
488
489 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
490     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
491     TRACE("(%p) Relay\n" , This);
492
493     /* Note: D3DRECT is compatible with WINED3DRECT */
494     return IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
495 }
496
497 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
498     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
499     TRACE("(%p) Relay\n" , This);
500
501     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
502     return IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
503 }
504
505 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
506     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
507     TRACE("(%p) Relay\n" , This);
508
509     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
510     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
511 }
512
513 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
514     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
515     TRACE("(%p) Relay\n" , This);
516
517     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
518     return IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
519 }
520
521 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9 iface, CONST D3DVIEWPORT9* pViewport) {
522     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
523     TRACE("(%p) Relay\n" , This);
524
525     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
526     return IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
527 }
528
529 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9 iface, D3DVIEWPORT9* pViewport) {
530     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
531     TRACE("(%p) Relay\n" , This);
532
533     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
534     return IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
535 }
536
537 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9 iface, CONST D3DMATERIAL9* pMaterial) {
538     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
539     TRACE("(%p) Relay\n" , This);
540
541     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
542     return IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
543 }
544
545 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9 iface, D3DMATERIAL9* pMaterial) {
546     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
547     TRACE("(%p) Relay\n" , This);
548
549     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
550     return IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
551 }
552
553 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST D3DLIGHT9* pLight) {
554     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
555     TRACE("(%p) Relay\n" , This);
556
557     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
558     return IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
559 }
560
561 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, D3DLIGHT9* pLight) {
562     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
563     TRACE("(%p) Relay\n" , This);
564
565     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
566     return IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
567 }
568
569 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL Enable) {
570     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
571     TRACE("(%p) Relay\n" , This);
572     return IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
573 }
574
575 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL* pEnable) {
576     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
577     TRACE("(%p) Relay\n" , This);
578     return IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
579 }
580
581 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST float* pPlane) {
582     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
583     TRACE("(%p) Relay\n" , This);
584     return IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
585 }
586
587 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, float* pPlane) {
588     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
589     TRACE("(%p) Relay\n" , This);
590     return IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
591 }
592
593 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD Value) {
594     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
595     TRACE("(%p) Relay\n" , This);
596     return IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
597 }
598
599 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
600     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
601     TRACE("(%p) Relay\n" , This);
602     return IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
603 }
604
605 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9 iface, CONST D3DCLIPSTATUS9* pClipStatus) {
606     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
607     TRACE("(%p) Relay\n" , This);
608     return IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
609 }
610
611 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9 iface, D3DCLIPSTATUS9* pClipStatus) {
612     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
613     TRACE("(%p) Relay\n" , This);
614     return IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
615 }
616
617 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9 **ppTexture) {
618     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
619     IWineD3DBaseTexture *retTexture = NULL;
620     HRESULT rc = D3D_OK;
621
622     TRACE("(%p) Relay\n" , This);
623
624     if(ppTexture == NULL){
625         return D3DERR_INVALIDCALL;
626     }
627
628     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
629     if (rc == D3D_OK && NULL != retTexture) {
630         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
631         IWineD3DBaseTexture_Release(retTexture);
632     }else{
633         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
634         *ppTexture = NULL;
635     }
636     return rc;
637 }
638
639 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
640     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
641     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
642     return IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
643                                      pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture); 
644 }
645
646 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD* pValue) {
647     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
648     TRACE("(%p) Relay\n" , This);
649     return IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
650 }
651
652 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
653     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
654     TRACE("(%p) Relay\n" , This);
655     return IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
656 }
657
658 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD* pValue) {
659     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
660     TRACE("(%p) Relay\n" , This);
661     return IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
662 }
663
664 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
665     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
666     TRACE("(%p) Relay\n" , This);
667     return IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);    
668 }
669
670 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9 iface, DWORD* pNumPasses) {
671     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
672     TRACE("(%p) Relay\n" , This);
673     return IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
674 }
675
676 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
677     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
678     TRACE("(%p) Relay\n" , This);
679     return IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
680 }
681
682 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
683     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
684     TRACE("(%p) Relay\n" , This);
685     return IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
686 }
687
688 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber) {
689     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
690     TRACE("(%p) Relay\n" , This);
691     return IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
692 }
693
694 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT* PaletteNumber) {
695     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
696     TRACE("(%p) Relay\n" , This);
697     return IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
698 }
699
700 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9 iface, CONST RECT* pRect) {
701     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
702     TRACE("(%p) Relay\n" , This);
703     return IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);    
704 }
705
706 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9 iface, RECT* pRect) {
707     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
708     TRACE("(%p) Relay\n" , This);
709     return IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);   
710 }
711
712 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface, BOOL bSoftware) {
713     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
714     TRACE("(%p) Relay\n" , This);
715     return IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
716 }
717
718 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface) {
719     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
720     TRACE("(%p) Relay\n" , This);
721     return IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
722 }
723
724 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9 iface, float nSegments) {
725     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
726     TRACE("(%p) Relay\n" , This);
727     return IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
728 }
729
730 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9 iface) {
731     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
732     TRACE("(%p) Relay\n" , This);
733     return IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
734 }
735
736 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
737     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
738     TRACE("(%p) Relay\n" , This);
739     return IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
740 }
741
742 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType,
743                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
744     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
745     TRACE("(%p) Relay\n" , This);
746
747     /* D3D8 passes the baseVertexIndex in SetIndices, and due to the stateblock functions wined3d has to work that way */
748     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
749     return IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
750 }
751
752 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
753     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
754     TRACE("(%p) Relay\n" , This);
755     return IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
756 }
757
758 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
759                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
760                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
761     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
762     TRACE("(%p) Relay\n" , This);
763     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
764                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
765 }
766
767 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9 iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
768     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
769     TRACE("(%p) Relay\n" , This);
770     return IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer9Impl *)pDestBuffer)->wineD3DVertexBuffer, ((IDirect3DVertexBuffer9Impl *)pVertexDecl)->wineD3DVertexBuffer, Flags);       
771 }
772
773 HRESULT  WINAPI  IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9 iface, DWORD FVF) {
774     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
775     TRACE("(%p) Relay\n" , This);
776
777     if (0 != FVF) {
778          HRESULT hr;
779          D3DVERTEXELEMENT9* elements = NULL;
780          IDirect3DVertexDeclaration9* pDecl = NULL;
781
782          hr = vdecl_convert_fvf(FVF, &elements);
783          if (hr != S_OK) goto exit;
784
785          hr = IDirect3DDevice9Impl_CreateVertexDeclaration(iface, elements, &pDecl);
786          if (hr != S_OK) goto exit;
787              
788          hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, pDecl);
789          if (hr != S_OK) goto exit;
790          This->convertedDecl = pDecl;
791          pDecl = NULL;
792
793          exit:
794          HeapFree(GetProcessHeap(), 0, elements);
795          if (pDecl) IUnknown_Release(pDecl);
796          if (hr != S_OK) return hr;
797     }
798
799     return IWineD3DDevice_SetFVF(This->WineD3DDevice, FVF);
800 }
801
802 HRESULT  WINAPI  IDirect3DDevice9Impl_GetFVF(LPDIRECT3DDEVICE9 iface, DWORD* pFVF) {
803     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
804     TRACE("(%p) Relay\n" , This);
805     return IWineD3DDevice_GetFVF(This->WineD3DDevice, pFVF);
806 }
807
808 HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
809     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
810     TRACE("(%p) Relay\n" , This);
811     return IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber, 
812                                           pStreamData==NULL ? NULL:((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer, 
813                                           OffsetInBytes, Stride);
814 }
815
816 HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride) {
817     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
818     IWineD3DVertexBuffer *retStream = NULL;
819     HRESULT rc = D3D_OK;
820
821     TRACE("(%p) Relay\n" , This);
822
823     if(pStream == NULL){
824         return D3DERR_INVALIDCALL;
825     }
826
827     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, OffsetInBytes, pStride);
828     if (rc == D3D_OK  && NULL != retStream) {
829         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
830         IWineD3DVertexBuffer_Release(retStream);
831     }else{
832         if (rc != D3D_OK){
833             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
834         }
835         *pStream = NULL;
836     }
837     return rc;
838 }
839
840 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT Divider) {
841     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
842     TRACE("(%p) Relay\n" , This);
843     IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
844     return D3D_OK;
845 }
846
847 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT* Divider) {
848     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
849     TRACE("(%p) Relay\n" , This);
850     return IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
851 }
852
853 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9* pIndexData) {
854     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
855     TRACE("(%p) Relay\n", This);
856     return IWineD3DDevice_SetIndices(This->WineD3DDevice, 
857                                      pIndexData==NULL ? NULL:((IDirect3DIndexBuffer9Impl *)pIndexData)->wineD3DIndexBuffer, 
858                                      0);
859 }
860
861 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9 **ppIndexData) {
862     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
863     IWineD3DIndexBuffer *retIndexData = NULL;
864     HRESULT rc = D3D_OK;
865     UINT tmp;
866
867     TRACE("(%p) Relay\n", This);
868
869     if(ppIndexData == NULL){
870         return D3DERR_INVALIDCALL;
871     }
872
873     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData, &tmp);
874     if (rc == D3D_OK && NULL != retIndexData) {
875         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
876         IWineD3DIndexBuffer_Release(retIndexData);
877     }else{
878         if(rc != D3D_OK)  FIXME("Call to GetIndices failed\n");
879         *ppIndexData = NULL;
880     }
881     return rc;
882 }
883
884 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
885     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
886     TRACE("(%p) Relay\n", This);
887     return IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
888 }
889 /*http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/d3d/interfaces/idirect3ddevice9/DrawTriPatch.asp*/
890 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
891     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
892     TRACE("(%p) Relay\n", This);
893     return IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
894 }
895
896 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9 iface, UINT Handle) {
897     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
898     TRACE("(%p) Relay\n", This);
899     return IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
900 }
901
902 const IDirect3DDevice9Vtbl Direct3DDevice9_Vtbl =
903 {
904     /* IUnknown */
905     IDirect3DDevice9Impl_QueryInterface,
906     IDirect3DDevice9Impl_AddRef,
907     IDirect3DDevice9Impl_Release,
908     /* IDirect3DDevice9 */
909     IDirect3DDevice9Impl_TestCooperativeLevel,
910     IDirect3DDevice9Impl_GetAvailableTextureMem,
911     IDirect3DDevice9Impl_EvictManagedResources,
912     IDirect3DDevice9Impl_GetDirect3D,
913     IDirect3DDevice9Impl_GetDeviceCaps,
914     IDirect3DDevice9Impl_GetDisplayMode,
915     IDirect3DDevice9Impl_GetCreationParameters,
916     IDirect3DDevice9Impl_SetCursorProperties,
917     IDirect3DDevice9Impl_SetCursorPosition,
918     IDirect3DDevice9Impl_ShowCursor,
919     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
920     IDirect3DDevice9Impl_GetSwapChain,
921     IDirect3DDevice9Impl_GetNumberOfSwapChains,
922     IDirect3DDevice9Impl_Reset,
923     IDirect3DDevice9Impl_Present,
924     IDirect3DDevice9Impl_GetBackBuffer,
925     IDirect3DDevice9Impl_GetRasterStatus,
926     IDirect3DDevice9Impl_SetDialogBoxMode,
927     IDirect3DDevice9Impl_SetGammaRamp,
928     IDirect3DDevice9Impl_GetGammaRamp,
929     IDirect3DDevice9Impl_CreateTexture,
930     IDirect3DDevice9Impl_CreateVolumeTexture,
931     IDirect3DDevice9Impl_CreateCubeTexture,
932     IDirect3DDevice9Impl_CreateVertexBuffer,
933     IDirect3DDevice9Impl_CreateIndexBuffer,
934     IDirect3DDevice9Impl_CreateRenderTarget,
935     IDirect3DDevice9Impl_CreateDepthStencilSurface,
936     IDirect3DDevice9Impl_UpdateSurface,
937     IDirect3DDevice9Impl_UpdateTexture,
938     IDirect3DDevice9Impl_GetRenderTargetData,
939     IDirect3DDevice9Impl_GetFrontBufferData,
940     IDirect3DDevice9Impl_StretchRect,
941     IDirect3DDevice9Impl_ColorFill,
942     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
943     IDirect3DDevice9Impl_SetRenderTarget,
944     IDirect3DDevice9Impl_GetRenderTarget,
945     IDirect3DDevice9Impl_SetDepthStencilSurface,
946     IDirect3DDevice9Impl_GetDepthStencilSurface,
947     IDirect3DDevice9Impl_BeginScene,
948     IDirect3DDevice9Impl_EndScene,
949     IDirect3DDevice9Impl_Clear,
950     IDirect3DDevice9Impl_SetTransform,
951     IDirect3DDevice9Impl_GetTransform,
952     IDirect3DDevice9Impl_MultiplyTransform,
953     IDirect3DDevice9Impl_SetViewport,
954     IDirect3DDevice9Impl_GetViewport,
955     IDirect3DDevice9Impl_SetMaterial,
956     IDirect3DDevice9Impl_GetMaterial,
957     IDirect3DDevice9Impl_SetLight,
958     IDirect3DDevice9Impl_GetLight,
959     IDirect3DDevice9Impl_LightEnable,
960     IDirect3DDevice9Impl_GetLightEnable,
961     IDirect3DDevice9Impl_SetClipPlane,
962     IDirect3DDevice9Impl_GetClipPlane,
963     IDirect3DDevice9Impl_SetRenderState,
964     IDirect3DDevice9Impl_GetRenderState,
965     IDirect3DDevice9Impl_CreateStateBlock,
966     IDirect3DDevice9Impl_BeginStateBlock,
967     IDirect3DDevice9Impl_EndStateBlock,
968     IDirect3DDevice9Impl_SetClipStatus,
969     IDirect3DDevice9Impl_GetClipStatus,
970     IDirect3DDevice9Impl_GetTexture,
971     IDirect3DDevice9Impl_SetTexture,
972     IDirect3DDevice9Impl_GetTextureStageState,
973     IDirect3DDevice9Impl_SetTextureStageState,
974     IDirect3DDevice9Impl_GetSamplerState,
975     IDirect3DDevice9Impl_SetSamplerState,
976     IDirect3DDevice9Impl_ValidateDevice,
977     IDirect3DDevice9Impl_SetPaletteEntries,
978     IDirect3DDevice9Impl_GetPaletteEntries,
979     IDirect3DDevice9Impl_SetCurrentTexturePalette,
980     IDirect3DDevice9Impl_GetCurrentTexturePalette,
981     IDirect3DDevice9Impl_SetScissorRect,
982     IDirect3DDevice9Impl_GetScissorRect,
983     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
984     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
985     IDirect3DDevice9Impl_SetNPatchMode,
986     IDirect3DDevice9Impl_GetNPatchMode,
987     IDirect3DDevice9Impl_DrawPrimitive,
988     IDirect3DDevice9Impl_DrawIndexedPrimitive,
989     IDirect3DDevice9Impl_DrawPrimitiveUP,
990     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
991     IDirect3DDevice9Impl_ProcessVertices,
992     IDirect3DDevice9Impl_CreateVertexDeclaration,
993     IDirect3DDevice9Impl_SetVertexDeclaration,
994     IDirect3DDevice9Impl_GetVertexDeclaration,
995     IDirect3DDevice9Impl_SetFVF,
996     IDirect3DDevice9Impl_GetFVF,
997     IDirect3DDevice9Impl_CreateVertexShader,
998     IDirect3DDevice9Impl_SetVertexShader,
999     IDirect3DDevice9Impl_GetVertexShader,
1000     IDirect3DDevice9Impl_SetVertexShaderConstantF,
1001     IDirect3DDevice9Impl_GetVertexShaderConstantF,
1002     IDirect3DDevice9Impl_SetVertexShaderConstantI,
1003     IDirect3DDevice9Impl_GetVertexShaderConstantI,
1004     IDirect3DDevice9Impl_SetVertexShaderConstantB,
1005     IDirect3DDevice9Impl_GetVertexShaderConstantB,
1006     IDirect3DDevice9Impl_SetStreamSource,
1007     IDirect3DDevice9Impl_GetStreamSource,
1008     IDirect3DDevice9Impl_SetStreamSourceFreq,
1009     IDirect3DDevice9Impl_GetStreamSourceFreq,
1010     IDirect3DDevice9Impl_SetIndices,
1011     IDirect3DDevice9Impl_GetIndices,
1012     IDirect3DDevice9Impl_CreatePixelShader,
1013     IDirect3DDevice9Impl_SetPixelShader,
1014     IDirect3DDevice9Impl_GetPixelShader,
1015     IDirect3DDevice9Impl_SetPixelShaderConstantF,
1016     IDirect3DDevice9Impl_GetPixelShaderConstantF,
1017     IDirect3DDevice9Impl_SetPixelShaderConstantI,
1018     IDirect3DDevice9Impl_GetPixelShaderConstantI,
1019     IDirect3DDevice9Impl_SetPixelShaderConstantB,
1020     IDirect3DDevice9Impl_GetPixelShaderConstantB,
1021     IDirect3DDevice9Impl_DrawRectPatch,
1022     IDirect3DDevice9Impl_DrawTriPatch,
1023     IDirect3DDevice9Impl_DeletePatch,
1024     IDirect3DDevice9Impl_CreateQuery
1025 };
1026
1027
1028 /* Internal function called back during the CreateDevice to create a render target  */
1029 HRESULT WINAPI D3D9CB_CreateSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
1030                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1031                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
1032
1033     HRESULT res = D3D_OK;
1034     IDirect3DSurface9Impl *d3dSurface = NULL;
1035     BOOL Lockable = TRUE;
1036     
1037     if((Pool == D3DPOOL_DEFAULT &&  Usage != D3DUSAGE_DYNAMIC)) 
1038         Lockable = FALSE;
1039         
1040     TRACE("relay\n");
1041     res = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9 *)device, Width, Height, (D3DFORMAT)Format,
1042                 Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface9 **)&d3dSurface, D3DRTYPE_SURFACE,
1043                 Usage, (D3DPOOL) Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */, pSharedHandle);  
1044
1045     if (SUCCEEDED(res)) {
1046         *ppSurface = d3dSurface->wineD3DSurface;
1047         d3dSurface->container = pSuperior;
1048         IUnknown_Release(d3dSurface->parentDevice);
1049         d3dSurface->parentDevice = NULL;
1050         d3dSurface->forwardReference = pSuperior;
1051     } else {
1052         FIXME("(%p) IDirect3DDevice9_CreateSurface failed\n", device);
1053     }
1054     return res;
1055 }
1056
1057 ULONG WINAPI D3D9CB_DestroySurface(IWineD3DSurface *pSurface) {
1058     IDirect3DSurface9Impl* surfaceParent;
1059     TRACE("(%p) call back\n", pSurface);
1060
1061     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
1062     /* GetParent's AddRef was forwarded to an object in destruction.
1063      * Releasing it here again would cause an endless recursion. */
1064     surfaceParent->forwardReference = NULL;
1065     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
1066 }