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