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