quartz: Use proper alloc/free functions for COM objects.
[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     return 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     return 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     return 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     if(Filter != D3DTEXF_NONE) ERR("Texture filters not supported yet\n");
387     return IWineD3DSurface_Blt(dst->wineD3DSurface, (RECT *) pDestRect, src->wineD3DSurface, (RECT *) pSourceRect, 0, NULL);
388 }
389
390 static HRESULT  WINAPI  IDirect3DDevice9Impl_ColorFill(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pSurface, CONST RECT* pRect, D3DCOLOR color) {
391     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
392     IDirect3DSurface9Impl *surface = (IDirect3DSurface9Impl *)pSurface;
393     TRACE("(%p) Relay\n" , This);
394
395     /* Note: D3DRECT is compatible with WINED3DRECT */
396     return IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, (CONST WINED3DRECT*)pRect, color);    
397 }
398
399 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
400     TRACE("Relay\n");
401     if(Pool == D3DPOOL_MANAGED ){
402         FIXME("Attempting to create a managed offscreen plain surface\n");
403         return D3DERR_INVALIDCALL;
404     }    
405         /*MSDN: D3DPOOL_SCRATCH will return a surface that has identical characteristics to a surface created by the Microsoft DirectX 8.x method CreateImageSurface.
406         
407         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
408         but then...
409         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
410         Why, their always lockable?
411         should I change the usage to dynamic?        
412         */
413     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);
414 }
415
416 /* TODO: move to wineD3D */
417 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
418     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
419     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
420     TRACE("(%p) Relay\n" , This);
421     return IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? (IWineD3DSurface*)pSurface->wineD3DSurface : NULL);
422 }
423
424 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTarget(LPDIRECT3DDEVICE9 iface, DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget) {
425     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
426     HRESULT hr = D3D_OK;
427     IWineD3DSurface *pRenderTarget;
428
429     TRACE("(%p) Relay\n" , This);
430
431     if (ppRenderTarget == NULL) {
432         return D3DERR_INVALIDCALL;
433     }
434     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
435
436     if (hr == D3D_OK && pRenderTarget != NULL) {
437         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
438         IWineD3DSurface_Release(pRenderTarget);
439     } else {
440         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
441         *ppRenderTarget = NULL;
442     }
443     return hr;
444 }
445
446 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9* pZStencilSurface) {
447     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
448     IDirect3DSurface9Impl *pSurface;
449
450     TRACE("(%p) Relay\n" , This);
451
452     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
453     return IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice,NULL==pSurface?NULL:(IWineD3DSurface*)pSurface->wineD3DSurface);
454 }
455
456 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE9 iface, IDirect3DSurface9 **ppZStencilSurface) {
457     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
458     HRESULT hr = D3D_OK;
459     IWineD3DSurface *pZStencilSurface;
460
461     TRACE("(%p) Relay\n" , This);
462     if(ppZStencilSurface == NULL){
463         return D3DERR_INVALIDCALL;
464     }
465
466     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
467     if(hr == D3D_OK && pZStencilSurface != NULL){
468         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
469         IWineD3DSurface_Release(pZStencilSurface);
470     }else{
471         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
472         *ppZStencilSurface = NULL;
473     }
474     return D3D_OK;
475 }
476
477 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9 iface) {
478     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
479     TRACE("(%p) Relay\n" , This);
480     return IWineD3DDevice_BeginScene(This->WineD3DDevice);
481 }
482
483 static HRESULT  WINAPI  IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9 iface) {
484     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
485     TRACE("(%p) Relay\n" , This);
486     return IWineD3DDevice_EndScene(This->WineD3DDevice);
487     
488 }
489
490 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
491     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
492     TRACE("(%p) Relay\n" , This);
493
494     /* Note: D3DRECT is compatible with WINED3DRECT */
495     return IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
496 }
497
498 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
499     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
500     TRACE("(%p) Relay\n" , This);
501
502     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
503     return IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
504 }
505
506 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
507     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
508     TRACE("(%p) Relay\n" , This);
509
510     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
511     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
512 }
513
514 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
515     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
516     TRACE("(%p) Relay\n" , This);
517
518     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
519     return IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
520 }
521
522 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9 iface, CONST D3DVIEWPORT9* pViewport) {
523     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
524     TRACE("(%p) Relay\n" , This);
525
526     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
527     return IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
528 }
529
530 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9 iface, D3DVIEWPORT9* pViewport) {
531     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
532     TRACE("(%p) Relay\n" , This);
533
534     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
535     return IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
536 }
537
538 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9 iface, CONST D3DMATERIAL9* pMaterial) {
539     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
540     TRACE("(%p) Relay\n" , This);
541
542     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
543     return IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
544 }
545
546 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9 iface, D3DMATERIAL9* pMaterial) {
547     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
548     TRACE("(%p) Relay\n" , This);
549
550     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
551     return IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
552 }
553
554 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST D3DLIGHT9* pLight) {
555     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
556     TRACE("(%p) Relay\n" , This);
557
558     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
559     return IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
560 }
561
562 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9 iface, DWORD Index, D3DLIGHT9* pLight) {
563     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
564     TRACE("(%p) Relay\n" , This);
565
566     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
567     return IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
568 }
569
570 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL Enable) {
571     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
572     TRACE("(%p) Relay\n" , This);
573     return IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
574 }
575
576 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9 iface, DWORD Index, BOOL* pEnable) {
577     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
578     TRACE("(%p) Relay\n" , This);
579     return IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
580 }
581
582 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, CONST float* pPlane) {
583     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
584     TRACE("(%p) Relay\n" , This);
585     return IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
586 }
587
588 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9 iface, DWORD Index, float* pPlane) {
589     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
590     TRACE("(%p) Relay\n" , This);
591     return IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
592 }
593
594 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD Value) {
595     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
596     TRACE("(%p) Relay\n" , This);
597     return IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
598 }
599
600 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9 iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
601     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
602     TRACE("(%p) Relay\n" , This);
603     return IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
604 }
605
606 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9 iface, CONST D3DCLIPSTATUS9* pClipStatus) {
607     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
608     TRACE("(%p) Relay\n" , This);
609     return IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
610 }
611
612 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9 iface, D3DCLIPSTATUS9* pClipStatus) {
613     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
614     TRACE("(%p) Relay\n" , This);
615     return IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
616 }
617
618 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9 **ppTexture) {
619     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
620     IWineD3DBaseTexture *retTexture = NULL;
621     HRESULT rc = D3D_OK;
622
623     TRACE("(%p) Relay\n" , This);
624
625     if(ppTexture == NULL){
626         return D3DERR_INVALIDCALL;
627     }
628
629     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, (IWineD3DBaseTexture **)&retTexture);
630     if (rc == D3D_OK && NULL != retTexture) {
631         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
632         IWineD3DBaseTexture_Release(retTexture);
633     }else{
634         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
635         *ppTexture = NULL;
636     }
637     return rc;
638 }
639
640 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9 iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
641     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
642     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
643     return IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
644                                      pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture); 
645 }
646
647 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD* pValue) {
648     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
649     TRACE("(%p) Relay\n" , This);
650     return IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, Type, pValue);
651 }
652
653 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
654     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
655     TRACE("(%p) Relay\n" , This);
656     return IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, Type, Value);
657 }
658
659 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD* pValue) {
660     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
661     TRACE("(%p) Relay\n" , This);
662     return IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
663 }
664
665 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9 iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
666     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
667     TRACE("(%p) Relay\n" , This);
668     return IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);    
669 }
670
671 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9 iface, DWORD* pNumPasses) {
672     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
673     TRACE("(%p) Relay\n" , This);
674     return IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
675 }
676
677 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
678     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
679     TRACE("(%p) Relay\n" , This);
680     return IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
681 }
682
683 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
684     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
685     TRACE("(%p) Relay\n" , This);
686     return IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
687 }
688
689 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT PaletteNumber) {
690     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
691     TRACE("(%p) Relay\n" , This);
692     return IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
693 }
694
695 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9 iface, UINT* PaletteNumber) {
696     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
697     TRACE("(%p) Relay\n" , This);
698     return IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
699 }
700
701 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9 iface, CONST RECT* pRect) {
702     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
703     TRACE("(%p) Relay\n" , This);
704     return IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);    
705 }
706
707 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9 iface, RECT* pRect) {
708     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
709     TRACE("(%p) Relay\n" , This);
710     return IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);   
711 }
712
713 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface, BOOL bSoftware) {
714     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
715     TRACE("(%p) Relay\n" , This);
716     return IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
717 }
718
719 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9 iface) {
720     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
721     TRACE("(%p) Relay\n" , This);
722     return IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
723 }
724
725 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9 iface, float nSegments) {
726     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
727     TRACE("(%p) Relay\n" , This);
728     return IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
729 }
730
731 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9 iface) {
732     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
733     TRACE("(%p) Relay\n" , This);
734     return IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
735 }
736
737 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
738     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
739     TRACE("(%p) Relay\n" , This);
740     return IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
741 }
742
743 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType,
744                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
745     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
746     TRACE("(%p) Relay\n" , This);
747
748     /* D3D8 passes the baseVertexIndex in SetIndices, and due to the stateblock functions wined3d has to work that way */
749     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
750     return IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
751 }
752
753 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
754     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
755     TRACE("(%p) Relay\n" , This);
756     return IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
757 }
758
759 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9 iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
760                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
761                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
762     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
763     TRACE("(%p) Relay\n" , This);
764     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
765                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
766 }
767
768 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9 iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
769     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
770     TRACE("(%p) Relay\n" , This);
771     return IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer9Impl *)pDestBuffer)->wineD3DVertexBuffer, ((IDirect3DVertexBuffer9Impl *)pVertexDecl)->wineD3DVertexBuffer, Flags);       
772 }
773
774 HRESULT  WINAPI  IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9 iface, DWORD FVF) {
775     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
776     TRACE("(%p) Relay\n" , This);
777
778     if (0 != FVF) {
779          HRESULT hr;
780          D3DVERTEXELEMENT9* elements = NULL;
781          IDirect3DVertexDeclaration9* pDecl = NULL;
782
783          hr = vdecl_convert_fvf(FVF, &elements);
784          if (hr != S_OK) goto exit;
785
786          hr = IDirect3DDevice9Impl_CreateVertexDeclaration(iface, elements, &pDecl);
787          if (hr != S_OK) goto exit;
788              
789          hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, pDecl);
790          if (hr != S_OK) goto exit;
791          This->convertedDecl = pDecl;
792          pDecl = NULL;
793
794          exit:
795          HeapFree(GetProcessHeap(), 0, elements);
796          if (pDecl) IUnknown_Release(pDecl);
797          if (hr != S_OK) return hr;
798     }
799
800     return IWineD3DDevice_SetFVF(This->WineD3DDevice, FVF);
801 }
802
803 HRESULT  WINAPI  IDirect3DDevice9Impl_GetFVF(LPDIRECT3DDEVICE9 iface, DWORD* pFVF) {
804     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
805     TRACE("(%p) Relay\n" , This);
806     return IWineD3DDevice_GetFVF(This->WineD3DDevice, pFVF);
807 }
808
809 HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
810     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
811     TRACE("(%p) Relay\n" , This);
812     return IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber, 
813                                           pStreamData==NULL ? NULL:((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer, 
814                                           OffsetInBytes, Stride);
815 }
816
817 HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSource(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride) {
818     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
819     IWineD3DVertexBuffer *retStream = NULL;
820     HRESULT rc = D3D_OK;
821
822     TRACE("(%p) Relay\n" , This);
823
824     if(pStream == NULL){
825         return D3DERR_INVALIDCALL;
826     }
827
828     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, (IWineD3DVertexBuffer **)&retStream, OffsetInBytes, pStride);
829     if (rc == D3D_OK  && NULL != retStream) {
830         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
831         IWineD3DVertexBuffer_Release(retStream);
832     }else{
833         if (rc != D3D_OK){
834             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
835         }
836         *pStream = NULL;
837     }
838     return rc;
839 }
840
841 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT Divider) {
842     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
843     TRACE("(%p) Relay\n" , This);
844     IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
845     return D3D_OK;
846 }
847
848 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9 iface, UINT StreamNumber, UINT* Divider) {
849     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
850     TRACE("(%p) Relay\n" , This);
851     return IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
852 }
853
854 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9* pIndexData) {
855     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
856     TRACE("(%p) Relay\n", This);
857     return IWineD3DDevice_SetIndices(This->WineD3DDevice, 
858                                      pIndexData==NULL ? NULL:((IDirect3DIndexBuffer9Impl *)pIndexData)->wineD3DIndexBuffer, 
859                                      0);
860 }
861
862 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetIndices(LPDIRECT3DDEVICE9 iface, IDirect3DIndexBuffer9 **ppIndexData) {
863     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
864     IWineD3DIndexBuffer *retIndexData = NULL;
865     HRESULT rc = D3D_OK;
866     UINT tmp;
867
868     TRACE("(%p) Relay\n", This);
869
870     if(ppIndexData == NULL){
871         return D3DERR_INVALIDCALL;
872     }
873
874     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData, &tmp);
875     if (rc == D3D_OK && NULL != retIndexData) {
876         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
877         IWineD3DIndexBuffer_Release(retIndexData);
878     }else{
879         if(rc != D3D_OK)  FIXME("Call to GetIndices failed\n");
880         *ppIndexData = NULL;
881     }
882     return rc;
883 }
884
885 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
886     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
887     TRACE("(%p) Relay\n", This);
888     return IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
889 }
890 /*http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/directx/graphics/reference/d3d/interfaces/idirect3ddevice9/DrawTriPatch.asp*/
891 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9 iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
892     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
893     TRACE("(%p) Relay\n", This);
894     return IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
895 }
896
897 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9 iface, UINT Handle) {
898     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
899     TRACE("(%p) Relay\n", This);
900     return IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
901 }
902
903 const IDirect3DDevice9Vtbl Direct3DDevice9_Vtbl =
904 {
905     /* IUnknown */
906     IDirect3DDevice9Impl_QueryInterface,
907     IDirect3DDevice9Impl_AddRef,
908     IDirect3DDevice9Impl_Release,
909     /* IDirect3DDevice9 */
910     IDirect3DDevice9Impl_TestCooperativeLevel,
911     IDirect3DDevice9Impl_GetAvailableTextureMem,
912     IDirect3DDevice9Impl_EvictManagedResources,
913     IDirect3DDevice9Impl_GetDirect3D,
914     IDirect3DDevice9Impl_GetDeviceCaps,
915     IDirect3DDevice9Impl_GetDisplayMode,
916     IDirect3DDevice9Impl_GetCreationParameters,
917     IDirect3DDevice9Impl_SetCursorProperties,
918     IDirect3DDevice9Impl_SetCursorPosition,
919     IDirect3DDevice9Impl_ShowCursor,
920     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
921     IDirect3DDevice9Impl_GetSwapChain,
922     IDirect3DDevice9Impl_GetNumberOfSwapChains,
923     IDirect3DDevice9Impl_Reset,
924     IDirect3DDevice9Impl_Present,
925     IDirect3DDevice9Impl_GetBackBuffer,
926     IDirect3DDevice9Impl_GetRasterStatus,
927     IDirect3DDevice9Impl_SetDialogBoxMode,
928     IDirect3DDevice9Impl_SetGammaRamp,
929     IDirect3DDevice9Impl_GetGammaRamp,
930     IDirect3DDevice9Impl_CreateTexture,
931     IDirect3DDevice9Impl_CreateVolumeTexture,
932     IDirect3DDevice9Impl_CreateCubeTexture,
933     IDirect3DDevice9Impl_CreateVertexBuffer,
934     IDirect3DDevice9Impl_CreateIndexBuffer,
935     IDirect3DDevice9Impl_CreateRenderTarget,
936     IDirect3DDevice9Impl_CreateDepthStencilSurface,
937     IDirect3DDevice9Impl_UpdateSurface,
938     IDirect3DDevice9Impl_UpdateTexture,
939     IDirect3DDevice9Impl_GetRenderTargetData,
940     IDirect3DDevice9Impl_GetFrontBufferData,
941     IDirect3DDevice9Impl_StretchRect,
942     IDirect3DDevice9Impl_ColorFill,
943     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
944     IDirect3DDevice9Impl_SetRenderTarget,
945     IDirect3DDevice9Impl_GetRenderTarget,
946     IDirect3DDevice9Impl_SetDepthStencilSurface,
947     IDirect3DDevice9Impl_GetDepthStencilSurface,
948     IDirect3DDevice9Impl_BeginScene,
949     IDirect3DDevice9Impl_EndScene,
950     IDirect3DDevice9Impl_Clear,
951     IDirect3DDevice9Impl_SetTransform,
952     IDirect3DDevice9Impl_GetTransform,
953     IDirect3DDevice9Impl_MultiplyTransform,
954     IDirect3DDevice9Impl_SetViewport,
955     IDirect3DDevice9Impl_GetViewport,
956     IDirect3DDevice9Impl_SetMaterial,
957     IDirect3DDevice9Impl_GetMaterial,
958     IDirect3DDevice9Impl_SetLight,
959     IDirect3DDevice9Impl_GetLight,
960     IDirect3DDevice9Impl_LightEnable,
961     IDirect3DDevice9Impl_GetLightEnable,
962     IDirect3DDevice9Impl_SetClipPlane,
963     IDirect3DDevice9Impl_GetClipPlane,
964     IDirect3DDevice9Impl_SetRenderState,
965     IDirect3DDevice9Impl_GetRenderState,
966     IDirect3DDevice9Impl_CreateStateBlock,
967     IDirect3DDevice9Impl_BeginStateBlock,
968     IDirect3DDevice9Impl_EndStateBlock,
969     IDirect3DDevice9Impl_SetClipStatus,
970     IDirect3DDevice9Impl_GetClipStatus,
971     IDirect3DDevice9Impl_GetTexture,
972     IDirect3DDevice9Impl_SetTexture,
973     IDirect3DDevice9Impl_GetTextureStageState,
974     IDirect3DDevice9Impl_SetTextureStageState,
975     IDirect3DDevice9Impl_GetSamplerState,
976     IDirect3DDevice9Impl_SetSamplerState,
977     IDirect3DDevice9Impl_ValidateDevice,
978     IDirect3DDevice9Impl_SetPaletteEntries,
979     IDirect3DDevice9Impl_GetPaletteEntries,
980     IDirect3DDevice9Impl_SetCurrentTexturePalette,
981     IDirect3DDevice9Impl_GetCurrentTexturePalette,
982     IDirect3DDevice9Impl_SetScissorRect,
983     IDirect3DDevice9Impl_GetScissorRect,
984     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
985     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
986     IDirect3DDevice9Impl_SetNPatchMode,
987     IDirect3DDevice9Impl_GetNPatchMode,
988     IDirect3DDevice9Impl_DrawPrimitive,
989     IDirect3DDevice9Impl_DrawIndexedPrimitive,
990     IDirect3DDevice9Impl_DrawPrimitiveUP,
991     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
992     IDirect3DDevice9Impl_ProcessVertices,
993     IDirect3DDevice9Impl_CreateVertexDeclaration,
994     IDirect3DDevice9Impl_SetVertexDeclaration,
995     IDirect3DDevice9Impl_GetVertexDeclaration,
996     IDirect3DDevice9Impl_SetFVF,
997     IDirect3DDevice9Impl_GetFVF,
998     IDirect3DDevice9Impl_CreateVertexShader,
999     IDirect3DDevice9Impl_SetVertexShader,
1000     IDirect3DDevice9Impl_GetVertexShader,
1001     IDirect3DDevice9Impl_SetVertexShaderConstantF,
1002     IDirect3DDevice9Impl_GetVertexShaderConstantF,
1003     IDirect3DDevice9Impl_SetVertexShaderConstantI,
1004     IDirect3DDevice9Impl_GetVertexShaderConstantI,
1005     IDirect3DDevice9Impl_SetVertexShaderConstantB,
1006     IDirect3DDevice9Impl_GetVertexShaderConstantB,
1007     IDirect3DDevice9Impl_SetStreamSource,
1008     IDirect3DDevice9Impl_GetStreamSource,
1009     IDirect3DDevice9Impl_SetStreamSourceFreq,
1010     IDirect3DDevice9Impl_GetStreamSourceFreq,
1011     IDirect3DDevice9Impl_SetIndices,
1012     IDirect3DDevice9Impl_GetIndices,
1013     IDirect3DDevice9Impl_CreatePixelShader,
1014     IDirect3DDevice9Impl_SetPixelShader,
1015     IDirect3DDevice9Impl_GetPixelShader,
1016     IDirect3DDevice9Impl_SetPixelShaderConstantF,
1017     IDirect3DDevice9Impl_GetPixelShaderConstantF,
1018     IDirect3DDevice9Impl_SetPixelShaderConstantI,
1019     IDirect3DDevice9Impl_GetPixelShaderConstantI,
1020     IDirect3DDevice9Impl_SetPixelShaderConstantB,
1021     IDirect3DDevice9Impl_GetPixelShaderConstantB,
1022     IDirect3DDevice9Impl_DrawRectPatch,
1023     IDirect3DDevice9Impl_DrawTriPatch,
1024     IDirect3DDevice9Impl_DeletePatch,
1025     IDirect3DDevice9Impl_CreateQuery
1026 };
1027
1028
1029 /* Internal function called back during the CreateDevice to create a render target  */
1030 HRESULT WINAPI D3D9CB_CreateSurface(IUnknown *device, IUnknown *pSuperior, UINT Width, UINT Height,
1031                                          WINED3DFORMAT Format, DWORD Usage, WINED3DPOOL Pool, UINT Level,
1032                                          IWineD3DSurface** ppSurface, HANDLE* pSharedHandle) {
1033
1034     HRESULT res = D3D_OK;
1035     IDirect3DSurface9Impl *d3dSurface = NULL;
1036     BOOL Lockable = TRUE;
1037     
1038     if((Pool == D3DPOOL_DEFAULT &&  Usage != D3DUSAGE_DYNAMIC)) 
1039         Lockable = FALSE;
1040         
1041     TRACE("relay\n");
1042     res = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9 *)device, Width, Height, (D3DFORMAT)Format,
1043                 Lockable, FALSE/*Discard*/, Level,  (IDirect3DSurface9 **)&d3dSurface, D3DRTYPE_SURFACE,
1044                 Usage, (D3DPOOL) Pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */, pSharedHandle);  
1045
1046     if (SUCCEEDED(res)) {
1047         *ppSurface = d3dSurface->wineD3DSurface;
1048         d3dSurface->container = pSuperior;
1049         IUnknown_Release(d3dSurface->parentDevice);
1050         d3dSurface->parentDevice = NULL;
1051         d3dSurface->forwardReference = pSuperior;
1052     } else {
1053         FIXME("(%p) IDirect3DDevice9_CreateSurface failed\n", device);
1054     }
1055     return res;
1056 }
1057
1058 ULONG WINAPI D3D9CB_DestroySurface(IWineD3DSurface *pSurface) {
1059     IDirect3DSurface9Impl* surfaceParent;
1060     TRACE("(%p) call back\n", pSurface);
1061
1062     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
1063     /* GetParent's AddRef was forwarded to an object in destruction.
1064      * Releasing it here again would cause an endless recursion. */
1065     surfaceParent->forwardReference = NULL;
1066     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
1067 }