msctf: Add ITfSource interface to Context.
[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(LPDIRECT3DDEVICE9EX iface, REFIID riid, LPVOID* ppobj) {
31     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
32     IDirect3D9 *d3d;
33     IDirect3D9Impl *d3dimpl;
34
35     if (IsEqualGUID(riid, &IID_IUnknown)
36         || IsEqualGUID(riid, &IID_IDirect3DDevice9)) {
37         IDirect3DDevice9Ex_AddRef(iface);
38         *ppobj = This;
39         TRACE("Returning IDirect3DDevice9 interface at %p\n", *ppobj);
40         return S_OK;
41     } else if(IsEqualGUID(riid, &IID_IDirect3DDevice9Ex)) {
42         /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
43          * It doesn't matter with which function the device was created.
44          */
45         IDirect3DDevice9_GetDirect3D(iface, &d3d);
46         d3dimpl = (IDirect3D9Impl *) d3d;
47
48         if(d3dimpl->extended) {
49             *ppobj = iface;
50             IDirect3DDevice9Ex_AddRef((IDirect3DDevice9Ex *) *ppobj);
51             IDirect3D9_Release(d3d);
52             TRACE("Returning IDirect3DDevice9Ex interface at %p\n", *ppobj);
53             return S_OK;
54         } else {
55             WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE\n");
56             IDirect3D9_Release(d3d);
57             *ppobj = NULL;
58             return E_NOINTERFACE;
59         }
60     }
61
62     if (IsEqualGUID(riid, &IID_IWineD3DDeviceParent))
63     {
64         IUnknown_AddRef((IUnknown *)&This->device_parent_vtbl);
65         *ppobj = &This->device_parent_vtbl;
66         return S_OK;
67     }
68
69     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
70     *ppobj = NULL;
71     return E_NOINTERFACE;
72 }
73
74 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(LPDIRECT3DDEVICE9EX iface) {
75     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
76     ULONG ref = InterlockedIncrement(&This->ref);
77
78     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
79
80     return ref;
81 }
82
83 static ULONG WINAPI IDirect3DDevice9Impl_Release(LPDIRECT3DDEVICE9EX iface) {
84     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
85     ULONG ref;
86
87     if (This->inDestruction) return 0;
88     ref = InterlockedDecrement(&This->ref);
89
90     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
91
92     if (ref == 0) {
93       unsigned i;
94       This->inDestruction = TRUE;
95
96       EnterCriticalSection(&d3d9_cs);
97       for(i = 0; i < This->numConvertedDecls; i++) {
98           /* Unless Wine is buggy or the app has a bug the refcount will be 0, because decls hold a reference to the
99            * device
100            */
101           IDirect3DVertexDeclaration9Impl_Destroy(This->convertedDecls[i]);
102       }
103       HeapFree(GetProcessHeap(), 0, This->convertedDecls);
104
105       IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D9CB_DestroyDepthStencilSurface, D3D9CB_DestroySwapChain);
106       IWineD3DDevice_Release(This->WineD3DDevice);
107       LeaveCriticalSection(&d3d9_cs);
108       HeapFree(GetProcessHeap(), 0, This);
109     }
110     return ref;
111 }
112
113 /* IDirect3DDevice Interface follow: */
114 static HRESULT  WINAPI  IDirect3DDevice9Impl_TestCooperativeLevel(LPDIRECT3DDEVICE9EX iface) {
115     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
116     HRESULT hr;
117     TRACE("(%p)\n", This);
118
119     EnterCriticalSection(&d3d9_cs);
120     hr = IWineD3DDevice_TestCooperativeLevel(This->WineD3DDevice);
121     LeaveCriticalSection(&d3d9_cs);
122     if(hr == WINED3D_OK && This->notreset) {
123         TRACE("D3D9 Device is marked not reset\n");
124         hr = D3DERR_DEVICENOTRESET;
125     }
126
127     return hr;
128 }
129
130 static UINT     WINAPI  IDirect3DDevice9Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE9EX iface) {
131     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
132     HRESULT hr;
133     TRACE("(%p) Relay\n", This);
134
135     EnterCriticalSection(&d3d9_cs);
136     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
137     LeaveCriticalSection(&d3d9_cs);
138     return hr;
139 }
140
141 static HRESULT  WINAPI  IDirect3DDevice9Impl_EvictManagedResources(LPDIRECT3DDEVICE9EX iface) {
142     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
143     HRESULT hr;
144     TRACE("(%p) : Relay\n", This);
145
146     EnterCriticalSection(&d3d9_cs);
147     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
148     LeaveCriticalSection(&d3d9_cs);
149     return hr;
150 }
151
152 static HRESULT WINAPI IDirect3DDevice9Impl_GetDirect3D(LPDIRECT3DDEVICE9EX iface, IDirect3D9** ppD3D9) {
153     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
154     HRESULT hr = D3D_OK;
155     IWineD3D* pWineD3D;
156
157     TRACE("(%p) Relay\n", This);
158
159     if (NULL == ppD3D9) {
160         return D3DERR_INVALIDCALL;
161     }
162
163     EnterCriticalSection(&d3d9_cs);
164     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
165     if (hr == D3D_OK && pWineD3D != NULL)
166     {
167         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D9);
168         IWineD3D_Release(pWineD3D);
169     } else {
170         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
171         *ppD3D9 = NULL;
172     }
173     TRACE("(%p) returning %p\n", This, *ppD3D9);
174     LeaveCriticalSection(&d3d9_cs);
175     return hr;
176 }
177
178 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDeviceCaps(LPDIRECT3DDEVICE9EX iface, D3DCAPS9* pCaps) {
179     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
180     HRESULT hrc = D3D_OK;
181     WINED3DCAPS *pWineCaps;
182
183     TRACE("(%p) : Relay pCaps %p\n", This, pCaps);
184     if(NULL == pCaps){
185         return D3DERR_INVALIDCALL;
186     }
187     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
188     if(pWineCaps == NULL){
189         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
190     }
191
192     memset(pCaps, 0, sizeof(*pCaps));
193     EnterCriticalSection(&d3d9_cs);
194     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
195     LeaveCriticalSection(&d3d9_cs);
196     WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
197     HeapFree(GetProcessHeap(), 0, pWineCaps);
198
199     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
200     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
201
202     filter_caps(pCaps);
203
204     TRACE("Returning %p %p\n", This, pCaps);
205     return hrc;
206 }
207
208 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDisplayMode(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DDISPLAYMODE* pMode) {
209     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
210     HRESULT hr;
211     TRACE("(%p) Relay\n", This);
212
213     EnterCriticalSection(&d3d9_cs);
214     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, iSwapChain, (WINED3DDISPLAYMODE *) pMode);
215     LeaveCriticalSection(&d3d9_cs);
216     return hr;
217 }
218
219 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCreationParameters(LPDIRECT3DDEVICE9EX iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
220     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
221     HRESULT hr;
222     TRACE("(%p) Relay\n", This);
223
224     EnterCriticalSection(&d3d9_cs);
225     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
226     LeaveCriticalSection(&d3d9_cs);
227     return hr;
228 }
229
230 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCursorProperties(LPDIRECT3DDEVICE9EX iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9* pCursorBitmap) {
231     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
232     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pCursorBitmap;
233     HRESULT hr;
234
235     TRACE("(%p) Relay\n", This);
236     if(!pCursorBitmap) {
237         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
238         return WINED3DERR_INVALIDCALL;
239     }
240
241     EnterCriticalSection(&d3d9_cs);
242     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice, XHotSpot, YHotSpot, pSurface->wineD3DSurface);
243     LeaveCriticalSection(&d3d9_cs);
244     return hr;
245 }
246
247 static void     WINAPI  IDirect3DDevice9Impl_SetCursorPosition(LPDIRECT3DDEVICE9EX iface, int XScreenSpace, int YScreenSpace, DWORD Flags) {
248     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
249     TRACE("(%p) Relay\n", This);
250
251     EnterCriticalSection(&d3d9_cs);
252     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
253     LeaveCriticalSection(&d3d9_cs);
254 }
255
256 static BOOL     WINAPI  IDirect3DDevice9Impl_ShowCursor(LPDIRECT3DDEVICE9EX iface, BOOL bShow) {
257     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
258     BOOL ret;
259     TRACE("(%p) Relay\n", This);
260
261     EnterCriticalSection(&d3d9_cs);
262     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
263     LeaveCriticalSection(&d3d9_cs);
264     return ret;
265 }
266
267 static HRESULT WINAPI reset_enum_callback(IWineD3DResource *resource, void *data) {
268     BOOL *resources_ok = data;
269     WINED3DRESOURCETYPE type;
270     HRESULT ret = S_OK;
271     WINED3DSURFACE_DESC surface_desc;
272     WINED3DVOLUME_DESC volume_desc;
273     WINED3DINDEXBUFFER_DESC index_desc;
274     WINED3DVERTEXBUFFER_DESC vertex_desc;
275     WINED3DFORMAT dummy_format;
276     WINED3DMULTISAMPLE_TYPE dummy_multisampletype;
277     DWORD dummy_dword;
278     WINED3DPOOL pool = WINED3DPOOL_SCRATCH; /* a harmless pool */
279     IUnknown *parent;
280
281     type = IWineD3DResource_GetType(resource);
282     switch(type) {
283         case WINED3DRTYPE_SURFACE:
284             surface_desc.Format = &dummy_format;
285             surface_desc.Type = &type;
286             surface_desc.Usage = &dummy_dword;
287             surface_desc.Pool = &pool;
288             surface_desc.Size = &dummy_dword;
289             surface_desc.MultiSampleType = &dummy_multisampletype;
290             surface_desc.MultiSampleQuality = &dummy_dword;
291             surface_desc.Width = &dummy_dword;
292             surface_desc.Height = &dummy_dword;
293
294             IWineD3DSurface_GetDesc((IWineD3DSurface *) resource, &surface_desc);
295             break;
296
297         case WINED3DRTYPE_VOLUME:
298             volume_desc.Format = &dummy_format;
299             volume_desc.Type = &type;
300             volume_desc.Usage = &dummy_dword;
301             volume_desc.Pool = &pool;
302             volume_desc.Size = &dummy_dword;
303             volume_desc.Width = &dummy_dword;
304             volume_desc.Height = &dummy_dword;
305             volume_desc.Depth = &dummy_dword;
306             IWineD3DVolume_GetDesc((IWineD3DVolume *) resource, &volume_desc);
307             break;
308
309         case WINED3DRTYPE_INDEXBUFFER:
310             IWineD3DIndexBuffer_GetDesc((IWineD3DIndexBuffer *) resource, &index_desc);
311             pool = index_desc.Pool;
312             break;
313
314         case WINED3DRTYPE_VERTEXBUFFER:
315             IWineD3DVertexBuffer_GetDesc((IWineD3DVertexBuffer *) resource, &vertex_desc);
316             pool = vertex_desc.Pool;
317             break;
318
319         /* No need to check for textures. If there is a D3DPOOL_DEFAULT texture, there
320          * is a D3DPOOL_DEFAULT surface or volume as well
321          */
322         default:
323             break;
324     }
325
326     if(pool == WINED3DPOOL_DEFAULT) {
327         IWineD3DResource_GetParent(resource, &parent);
328         if(IUnknown_Release(parent) == 0) {
329             TRACE("Parent %p is an implicit resource with ref 0\n", parent);
330         } else {
331             WARN("Resource %p(wineD3D %p) with pool D3DPOOL_DEFAULT blocks the Reset call\n", parent, resource);
332             ret = S_FALSE;
333             *resources_ok = FALSE;
334         }
335     }
336     IWineD3DResource_Release(resource);
337
338     return ret;
339 }
340
341 static HRESULT  WINAPI  IDirect3DDevice9Impl_Reset(LPDIRECT3DDEVICE9EX iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
342     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
343     WINED3DPRESENT_PARAMETERS localParameters;
344     HRESULT hr;
345     BOOL resources_ok = TRUE;
346     UINT i;
347
348     TRACE("(%p) Relay pPresentationParameters(%p)\n", This, pPresentationParameters);
349
350     /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
351      * such objects can still be used for rendering after their external d3d9 object has been destroyed.
352      * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
353      * make use of the hidden reference and destroys the objects.
354      *
355      * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
356      * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
357      */
358     IWineD3DDevice_SetIndices(This->WineD3DDevice, NULL);
359     for(i = 0; i < 16; i++) {
360         IWineD3DDevice_SetStreamSource(This->WineD3DDevice, i, NULL, 0, 0);
361     }
362     for(i = 0; i < 16; i++) {
363         IWineD3DDevice_SetTexture(This->WineD3DDevice, i, NULL);
364     }
365
366     IWineD3DDevice_EnumResources(This->WineD3DDevice, reset_enum_callback, &resources_ok);
367     if(!resources_ok) {
368         WARN("The application is holding D3DPOOL_DEFAULT resources, rejecting reset\n");
369         This->notreset = TRUE;
370         return WINED3DERR_INVALIDCALL;
371     }
372
373     localParameters.BackBufferWidth                     = pPresentationParameters->BackBufferWidth;
374     localParameters.BackBufferHeight                    = pPresentationParameters->BackBufferHeight;
375     localParameters.BackBufferFormat                    = pPresentationParameters->BackBufferFormat;
376     localParameters.BackBufferCount                     = pPresentationParameters->BackBufferCount;
377     localParameters.MultiSampleType                     = pPresentationParameters->MultiSampleType;
378     localParameters.MultiSampleQuality                  = pPresentationParameters->MultiSampleQuality;
379     localParameters.SwapEffect                          = pPresentationParameters->SwapEffect;
380     localParameters.hDeviceWindow                       = pPresentationParameters->hDeviceWindow;
381     localParameters.Windowed                            = pPresentationParameters->Windowed;
382     localParameters.EnableAutoDepthStencil              = pPresentationParameters->EnableAutoDepthStencil;
383     localParameters.AutoDepthStencilFormat              = pPresentationParameters->AutoDepthStencilFormat;
384     localParameters.Flags                               = pPresentationParameters->Flags;
385     localParameters.FullScreen_RefreshRateInHz          = pPresentationParameters->FullScreen_RefreshRateInHz;
386     localParameters.PresentationInterval                = pPresentationParameters->PresentationInterval;
387     localParameters.AutoRestoreDisplayMode              = TRUE;
388
389     EnterCriticalSection(&d3d9_cs);
390     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
391     LeaveCriticalSection(&d3d9_cs);
392     if(FAILED(hr)) {
393         This->notreset = TRUE;
394
395         pPresentationParameters->BackBufferWidth            = localParameters.BackBufferWidth;
396         pPresentationParameters->BackBufferHeight           = localParameters.BackBufferHeight;
397         pPresentationParameters->BackBufferFormat           = localParameters.BackBufferFormat;
398         pPresentationParameters->BackBufferCount            = localParameters.BackBufferCount;
399         pPresentationParameters->MultiSampleType            = localParameters.MultiSampleType;
400         pPresentationParameters->MultiSampleQuality         = localParameters.MultiSampleQuality;
401         pPresentationParameters->SwapEffect                 = localParameters.SwapEffect;
402         pPresentationParameters->hDeviceWindow              = localParameters.hDeviceWindow;
403         pPresentationParameters->Windowed                   = localParameters.Windowed;
404         pPresentationParameters->EnableAutoDepthStencil     = localParameters.EnableAutoDepthStencil;
405         pPresentationParameters->AutoDepthStencilFormat     = localParameters.AutoDepthStencilFormat;
406         pPresentationParameters->Flags                      = localParameters.Flags;
407         pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
408         pPresentationParameters->PresentationInterval       = localParameters.PresentationInterval;
409     } else {
410         This->notreset = FALSE;
411     }
412
413     return hr;
414 }
415
416 static HRESULT  WINAPI  IDirect3DDevice9Impl_Present(LPDIRECT3DDEVICE9EX iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA*
417  pDirtyRegion) {
418     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
419     HRESULT hr;
420     TRACE("(%p) Relay\n", This);
421
422     EnterCriticalSection(&d3d9_cs);
423     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
424     LeaveCriticalSection(&d3d9_cs);
425     return hr;
426  }
427
428 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetBackBuffer(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 ** ppBackBuffer) {
429     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
430     IWineD3DSurface *retSurface = NULL;
431     HRESULT rc = D3D_OK;
432
433     TRACE("(%p) Relay\n", This);
434
435     EnterCriticalSection(&d3d9_cs);
436     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, iSwapChain, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &retSurface);
437     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
438         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
439         IWineD3DSurface_Release(retSurface);
440     }
441     LeaveCriticalSection(&d3d9_cs);
442     return rc;
443 }
444 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRasterStatus(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DRASTER_STATUS* pRasterStatus) {
445     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
446     HRESULT hr;
447     TRACE("(%p) Relay\n", This);
448
449     EnterCriticalSection(&d3d9_cs);
450     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, iSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
451     LeaveCriticalSection(&d3d9_cs);
452     return hr;
453 }
454
455 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(LPDIRECT3DDEVICE9EX iface, BOOL bEnableDialogs) {
456     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
457     HRESULT hr;
458     TRACE("(%p) Relay\n", This);
459
460     EnterCriticalSection(&d3d9_cs);
461     hr = IWineD3DDevice_SetDialogBoxMode(This->WineD3DDevice, bEnableDialogs);
462     LeaveCriticalSection(&d3d9_cs);
463     return hr;
464 }
465
466 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
467     
468     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
469     TRACE("(%p) Relay\n", This);
470
471     EnterCriticalSection(&d3d9_cs);
472     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
473     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, iSwapChain, Flags, (CONST WINED3DGAMMARAMP *)pRamp);
474     LeaveCriticalSection(&d3d9_cs);
475 }
476
477 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DGAMMARAMP* pRamp) {
478     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
479     TRACE("(%p) Relay\n", This);
480
481     EnterCriticalSection(&d3d9_cs);
482     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
483     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, iSwapChain, (WINED3DGAMMARAMP *) pRamp);
484     LeaveCriticalSection(&d3d9_cs);
485 }
486
487
488 static HRESULT IDirect3DDevice9Impl_CreateSurface(LPDIRECT3DDEVICE9EX 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 )  {
489     HRESULT hrc;
490     IDirect3DSurface9Impl *object;
491     IDirect3DDevice9Impl  *This = (IDirect3DDevice9Impl *)iface;
492     TRACE("(%p) Relay\n", This);
493     
494     if(MultisampleQuality > 0){
495         FIXME("MultisampleQuality set to %d, bstituting 0\n", MultisampleQuality);
496     /*
497     MultisampleQuality
498  [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.
499  */
500  
501         MultisampleQuality=0;
502     }
503     /*FIXME: Check MAX bounds of MultisampleQuality*/
504
505     /* Allocate the storage for the device */
506     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
507     if (NULL == object) {
508         FIXME("Allocation of memory failed\n");
509         return D3DERR_OUTOFVIDEOMEMORY;
510     }
511
512     object->lpVtbl = &Direct3DSurface9_Vtbl;
513     object->ref = 1;
514     
515     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
516            
517     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);
518     
519     if (hrc != D3D_OK || NULL == object->wineD3DSurface) {
520
521        /* free up object */
522         FIXME("(%p) call to IWineD3DDevice_CreateSurface failed\n", This);
523         HeapFree(GetProcessHeap(), 0, object);
524     } else {
525         IDirect3DDevice9Ex_AddRef(iface);
526         object->parentDevice = iface;
527         TRACE("(%p) : Created surface %p\n", This, object);
528         *ppSurface = (LPDIRECT3DSURFACE9) object;
529     }
530     return hrc;
531 }
532
533
534
535 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateRenderTarget(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height,
536                                                          D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, 
537                                                          DWORD MultisampleQuality, BOOL Lockable, 
538                                                          IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
539     HRESULT hr;
540     TRACE("Relay\n");
541
542    /* Is this correct? */
543    EnterCriticalSection(&d3d9_cs);
544    hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,Lockable,FALSE/*Discard*/, 0/*Level*/, ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_RENDERTARGET,D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
545    LeaveCriticalSection(&d3d9_cs);
546    return hr;
547 }
548
549 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height,
550                                                                 D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
551                                                                 DWORD MultisampleQuality, BOOL Discard,
552                                                                 IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
553     HRESULT hr;
554     TRACE("Relay\n");
555
556      EnterCriticalSection(&d3d9_cs);
557      hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,TRUE/* Lockable */,Discard, 0/* Level */
558                                                ,ppSurface,D3DRTYPE_SURFACE,D3DUSAGE_DEPTHSTENCIL,
559                                                 D3DPOOL_DEFAULT,MultiSample,MultisampleQuality,pSharedHandle);
560      LeaveCriticalSection(&d3d9_cs);
561      return hr;
562 }
563
564
565 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestinationSurface, CONST POINT* pDestPoint) {
566     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
567     HRESULT hr;
568     TRACE("(%p) Relay\n" , This);
569
570     EnterCriticalSection(&d3d9_cs);
571     hr = IWineD3DDevice_UpdateSurface(This->WineD3DDevice, ((IDirect3DSurface9Impl *)pSourceSurface)->wineD3DSurface, pSourceRect, ((IDirect3DSurface9Impl *)pDestinationSurface)->wineD3DSurface, pDestPoint);
572     LeaveCriticalSection(&d3d9_cs);
573     return hr;
574 }
575
576 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateTexture(LPDIRECT3DDEVICE9EX iface, IDirect3DBaseTexture9* pSourceTexture, IDirect3DBaseTexture9* pDestinationTexture) {
577     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
578     HRESULT hr;
579     TRACE("(%p) Relay\n" , This);
580
581     EnterCriticalSection(&d3d9_cs);
582     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture9Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture9Impl *)pDestinationTexture)->wineD3DBaseTexture);
583     LeaveCriticalSection(&d3d9_cs);
584     return hr;
585 }
586
587 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTargetData(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pRenderTarget, IDirect3DSurface9* pDestSurface) {
588     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
589     IDirect3DSurface9Impl *renderTarget = (IDirect3DSurface9Impl *)pRenderTarget;
590     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
591     HRESULT hr;
592     TRACE("(%p)->(%p,%p)\n" , This, renderTarget, destSurface);
593
594     EnterCriticalSection(&d3d9_cs);
595     hr = IWineD3DSurface_BltFast(destSurface->wineD3DSurface, 0, 0, renderTarget->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
596     LeaveCriticalSection(&d3d9_cs);
597     return hr;
598 }
599
600 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBufferData(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSurface9* pDestSurface) {
601     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
602     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
603     HRESULT hr;
604     TRACE("(%p) Relay\n" , This);
605
606     EnterCriticalSection(&d3d9_cs);
607     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, iSwapChain, destSurface->wineD3DSurface);
608     LeaveCriticalSection(&d3d9_cs);
609     return hr;
610 }
611
612 static HRESULT  WINAPI  IDirect3DDevice9Impl_StretchRect(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestSurface, CONST RECT* pDestRect, D3DTEXTUREFILTERTYPE Filter) {
613     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
614     IDirect3DSurface9Impl *src = (IDirect3DSurface9Impl *) pSourceSurface;
615     IDirect3DSurface9Impl *dst = (IDirect3DSurface9Impl *) pDestSurface;
616     HRESULT hr;
617
618     TRACE("(%p)->(%p,%p,%p,%p,%d)\n" , This, src, pSourceRect, dst, pDestRect, Filter);
619     EnterCriticalSection(&d3d9_cs);
620     hr = IWineD3DSurface_Blt(dst->wineD3DSurface, pDestRect, src->wineD3DSurface, pSourceRect, 0, NULL, Filter);
621     LeaveCriticalSection(&d3d9_cs);
622     return hr;
623 }
624
625 static HRESULT  WINAPI  IDirect3DDevice9Impl_ColorFill(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pSurface, CONST RECT* pRect, D3DCOLOR color) {
626     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
627     IDirect3DSurface9Impl *surface = (IDirect3DSurface9Impl *)pSurface;
628     WINED3DPOOL pool;
629     WINED3DRESOURCETYPE restype;
630     DWORD usage;
631     WINED3DSURFACE_DESC desc;
632     HRESULT hr;
633     TRACE("(%p) Relay\n" , This);
634
635     memset(&desc, 0, sizeof(desc));
636     desc.Usage = &usage;
637     desc.Pool = &pool;
638     desc.Type = &restype;
639     IWineD3DSurface_GetDesc(surface->wineD3DSurface, &desc);
640
641     /* This method is only allowed with surfaces that are render targets, or offscreen plain surfaces
642      * in D3DPOOL_DEFAULT
643      */
644     if(!(usage & WINED3DUSAGE_RENDERTARGET) && (pool != D3DPOOL_DEFAULT || restype != D3DRTYPE_SURFACE)) {
645         WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
646         return D3DERR_INVALIDCALL;
647     }
648
649     /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
650     /* Note: D3DRECT is compatible with WINED3DRECT */
651     EnterCriticalSection(&d3d9_cs);
652     hr = IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, (CONST WINED3DRECT*)pRect, color);
653     LeaveCriticalSection(&d3d9_cs);
654     return hr;
655 }
656
657 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
658     HRESULT hr;
659     TRACE("Relay\n");
660     if(Pool == D3DPOOL_MANAGED ){
661         FIXME("Attempting to create a managed offscreen plain surface\n");
662         return D3DERR_INVALIDCALL;
663     }    
664         /*
665         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
666         but then...
667         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
668         Why, their always lockable?
669         should I change the usage to dynamic?        
670         */
671     EnterCriticalSection(&d3d9_cs);
672     hr = IDirect3DDevice9Impl_CreateSurface(iface,Width,Height,Format,TRUE/*Loackable*/,FALSE/*Discard*/,0/*Level*/ , ppSurface,D3DRTYPE_SURFACE, 0/*Usage (undefined/none)*/,(WINED3DPOOL) Pool,D3DMULTISAMPLE_NONE,0/*MultisampleQuality*/,pSharedHandle);
673     LeaveCriticalSection(&d3d9_cs);
674     return hr;
675 }
676
677 /* TODO: move to wineD3D */
678 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9EX iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
679     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
680     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
681     HRESULT hr;
682     TRACE("(%p) Relay\n" , This);
683
684     EnterCriticalSection(&d3d9_cs);
685     hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? pSurface->wineD3DSurface : NULL);
686     LeaveCriticalSection(&d3d9_cs);
687     return hr;
688 }
689
690 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTarget(LPDIRECT3DDEVICE9EX iface, DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget) {
691     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
692     HRESULT hr = D3D_OK;
693     IWineD3DSurface *pRenderTarget;
694
695     TRACE("(%p) Relay\n" , This);
696
697     if (ppRenderTarget == NULL) {
698         return D3DERR_INVALIDCALL;
699     }
700
701     EnterCriticalSection(&d3d9_cs);
702     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
703
704     if (hr == D3D_OK && pRenderTarget != NULL) {
705         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
706         IWineD3DSurface_Release(pRenderTarget);
707     } else {
708         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
709         *ppRenderTarget = NULL;
710     }
711     LeaveCriticalSection(&d3d9_cs);
712
713     return hr;
714 }
715
716 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pZStencilSurface) {
717     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
718     IDirect3DSurface9Impl *pSurface;
719     HRESULT hr;
720     TRACE("(%p) Relay\n" , This);
721
722     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
723     EnterCriticalSection(&d3d9_cs);
724     hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL==pSurface ? NULL : pSurface->wineD3DSurface);
725     LeaveCriticalSection(&d3d9_cs);
726     return hr;
727 }
728
729 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9 **ppZStencilSurface) {
730     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
731     HRESULT hr = D3D_OK;
732     IWineD3DSurface *pZStencilSurface;
733
734     TRACE("(%p) Relay\n" , This);
735     if(ppZStencilSurface == NULL){
736         return D3DERR_INVALIDCALL;
737     }
738
739     EnterCriticalSection(&d3d9_cs);
740     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
741     if (hr == WINED3D_OK) {
742         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
743         IWineD3DSurface_Release(pZStencilSurface);
744     } else {
745         if (hr != WINED3DERR_NOTFOUND)
746                 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
747         *ppZStencilSurface = NULL;
748     }
749     LeaveCriticalSection(&d3d9_cs);
750     return hr;
751 }
752
753 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9EX iface) {
754     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
755     HRESULT hr;
756     TRACE("(%p) Relay\n" , This);
757
758     EnterCriticalSection(&d3d9_cs);
759     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
760     LeaveCriticalSection(&d3d9_cs);
761     return hr;
762 }
763
764 static HRESULT  WINAPI  IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9EX iface) {
765     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
766     HRESULT hr;
767     TRACE("(%p) Relay\n" , This);
768
769     EnterCriticalSection(&d3d9_cs);
770     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
771     LeaveCriticalSection(&d3d9_cs);
772     return hr;
773 }
774
775 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9EX iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
776     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
777     HRESULT hr;
778     TRACE("(%p) Relay\n" , This);
779
780     /* Note: D3DRECT is compatible with WINED3DRECT */
781     EnterCriticalSection(&d3d9_cs);
782     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
783     LeaveCriticalSection(&d3d9_cs);
784     return hr;
785 }
786
787 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
788     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
789     HRESULT hr;
790     TRACE("(%p) Relay\n" , This);
791
792     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
793     EnterCriticalSection(&d3d9_cs);
794     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
795     LeaveCriticalSection(&d3d9_cs);
796     return hr;
797 }
798
799 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
800     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
801     TRACE("(%p) Relay\n" , This);
802
803     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
804     return IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
805 }
806
807 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
808     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
809     HRESULT hr;
810     TRACE("(%p) Relay\n" , This);
811
812     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
813     EnterCriticalSection(&d3d9_cs);
814     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
815     LeaveCriticalSection(&d3d9_cs);
816     return hr;
817 }
818
819 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9EX iface, CONST D3DVIEWPORT9* pViewport) {
820     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
821     HRESULT hr;
822     TRACE("(%p) Relay\n" , This);
823
824     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
825     EnterCriticalSection(&d3d9_cs);
826     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
827     LeaveCriticalSection(&d3d9_cs);
828     return hr;
829 }
830
831 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9EX iface, D3DVIEWPORT9* pViewport) {
832     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
833     HRESULT hr;
834     TRACE("(%p) Relay\n" , This);
835
836     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
837     EnterCriticalSection(&d3d9_cs);
838     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
839     LeaveCriticalSection(&d3d9_cs);
840     return hr;
841 }
842
843 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9EX iface, CONST D3DMATERIAL9* pMaterial) {
844     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
845     HRESULT hr;
846     TRACE("(%p) Relay\n" , This);
847
848     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
849     EnterCriticalSection(&d3d9_cs);
850     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
851     LeaveCriticalSection(&d3d9_cs);
852     return hr;
853 }
854
855 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9EX iface, D3DMATERIAL9* pMaterial) {
856     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
857     HRESULT hr;
858     TRACE("(%p) Relay\n" , This);
859
860     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
861     EnterCriticalSection(&d3d9_cs);
862     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
863     LeaveCriticalSection(&d3d9_cs);
864     return hr;
865 }
866
867 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST D3DLIGHT9* pLight) {
868     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
869     HRESULT hr;
870     TRACE("(%p) Relay\n" , This);
871
872     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
873     EnterCriticalSection(&d3d9_cs);
874     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
875     LeaveCriticalSection(&d3d9_cs);
876     return hr;
877 }
878
879 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, D3DLIGHT9* pLight) {
880     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
881     HRESULT hr;
882     TRACE("(%p) Relay\n" , This);
883
884     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
885     EnterCriticalSection(&d3d9_cs);
886     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
887     LeaveCriticalSection(&d3d9_cs);
888     return hr;
889 }
890
891 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL Enable) {
892     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
893     HRESULT hr;
894     TRACE("(%p) Relay\n" , This);
895
896     EnterCriticalSection(&d3d9_cs);
897     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
898     LeaveCriticalSection(&d3d9_cs);
899     return hr;
900 }
901
902 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL* pEnable) {
903     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
904     HRESULT hr;
905     TRACE("(%p) Relay\n" , This);
906
907     EnterCriticalSection(&d3d9_cs);
908     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
909     LeaveCriticalSection(&d3d9_cs);
910     return hr;
911 }
912
913 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST float* pPlane) {
914     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
915     HRESULT hr;
916     TRACE("(%p) Relay\n" , This);
917
918     EnterCriticalSection(&d3d9_cs);
919     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
920     LeaveCriticalSection(&d3d9_cs);
921     return hr;
922 }
923
924 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, float* pPlane) {
925     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
926     HRESULT hr;
927     TRACE("(%p) Relay\n" , This);
928
929     EnterCriticalSection(&d3d9_cs);
930     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
931     LeaveCriticalSection(&d3d9_cs);
932     return hr;
933 }
934
935 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD Value) {
936     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
937     HRESULT hr;
938     TRACE("(%p) Relay\n" , This);
939
940     EnterCriticalSection(&d3d9_cs);
941     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
942     LeaveCriticalSection(&d3d9_cs);
943     return hr;
944 }
945
946 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
947     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
948     HRESULT hr;
949     TRACE("(%p) Relay\n" , This);
950
951     EnterCriticalSection(&d3d9_cs);
952     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
953     LeaveCriticalSection(&d3d9_cs);
954     return hr;
955 }
956
957 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9EX iface, CONST D3DCLIPSTATUS9* pClipStatus) {
958     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
959     HRESULT hr;
960     TRACE("(%p) Relay\n" , This);
961
962     EnterCriticalSection(&d3d9_cs);
963     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
964     LeaveCriticalSection(&d3d9_cs);
965     return hr;
966 }
967
968 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9EX iface, D3DCLIPSTATUS9* pClipStatus) {
969     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
970     HRESULT hr;
971     TRACE("(%p) Relay\n" , This);
972
973     EnterCriticalSection(&d3d9_cs);
974     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
975     LeaveCriticalSection(&d3d9_cs);
976     return hr;
977 }
978
979 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTexture(LPDIRECT3DDEVICE9EX iface, DWORD Stage, IDirect3DBaseTexture9 **ppTexture) {
980     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
981     IWineD3DBaseTexture *retTexture = NULL;
982     HRESULT rc = D3D_OK;
983
984     TRACE("(%p) Relay\n" , This);
985
986     if(ppTexture == NULL){
987         return D3DERR_INVALIDCALL;
988     }
989
990     EnterCriticalSection(&d3d9_cs);
991     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
992     if (SUCCEEDED(rc) && NULL != retTexture) {
993         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
994         IWineD3DBaseTexture_Release(retTexture);
995     } else {
996         if(FAILED(rc)) {
997             WARN("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
998         }
999         *ppTexture = NULL;
1000     }
1001     LeaveCriticalSection(&d3d9_cs);
1002
1003     return rc;
1004 }
1005
1006 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9EX iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
1007     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1008     HRESULT hr;
1009     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1010
1011     EnterCriticalSection(&d3d9_cs);
1012     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1013                                    pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture);
1014     LeaveCriticalSection(&d3d9_cs);
1015     return hr;
1016 }
1017
1018 static const WINED3DTEXTURESTAGESTATETYPE tss_lookup[] =
1019 {
1020     WINED3DTSS_FORCE_DWORD,             /*  0, unused */
1021     WINED3DTSS_COLOROP,                 /*  1, D3DTSS_COLOROP */
1022     WINED3DTSS_COLORARG1,               /*  2, D3DTSS_COLORARG1 */
1023     WINED3DTSS_COLORARG2,               /*  3, D3DTSS_COLORARG2 */
1024     WINED3DTSS_ALPHAOP,                 /*  4, D3DTSS_ALPHAOP */
1025     WINED3DTSS_ALPHAARG1,               /*  5, D3DTSS_ALPHAARG1 */
1026     WINED3DTSS_ALPHAARG2,               /*  6, D3DTSS_ALPHAARG2 */
1027     WINED3DTSS_BUMPENVMAT00,            /*  7, D3DTSS_BUMPENVMAT00 */
1028     WINED3DTSS_BUMPENVMAT01,            /*  8, D3DTSS_BUMPENVMAT01 */
1029     WINED3DTSS_BUMPENVMAT10,            /*  9, D3DTSS_BUMPENVMAT10 */
1030     WINED3DTSS_BUMPENVMAT11,            /* 10, D3DTSS_BUMPENVMAT11 */
1031     WINED3DTSS_TEXCOORDINDEX,           /* 11, D3DTSS_TEXCOORDINDEX */
1032     WINED3DTSS_FORCE_DWORD,             /* 12, unused */
1033     WINED3DTSS_FORCE_DWORD,             /* 13, unused */
1034     WINED3DTSS_FORCE_DWORD,             /* 14, unused */
1035     WINED3DTSS_FORCE_DWORD,             /* 15, unused */
1036     WINED3DTSS_FORCE_DWORD,             /* 16, unused */
1037     WINED3DTSS_FORCE_DWORD,             /* 17, unused */
1038     WINED3DTSS_FORCE_DWORD,             /* 18, unused */
1039     WINED3DTSS_FORCE_DWORD,             /* 19, unused */
1040     WINED3DTSS_FORCE_DWORD,             /* 20, unused */
1041     WINED3DTSS_FORCE_DWORD,             /* 21, unused */
1042     WINED3DTSS_BUMPENVLSCALE,           /* 22, D3DTSS_BUMPENVLSCALE */
1043     WINED3DTSS_BUMPENVLOFFSET,          /* 23, D3DTSS_BUMPENVLOFFSET */
1044     WINED3DTSS_TEXTURETRANSFORMFLAGS,   /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1045     WINED3DTSS_FORCE_DWORD,             /* 25, unused */
1046     WINED3DTSS_COLORARG0,               /* 26, D3DTSS_COLORARG0 */
1047     WINED3DTSS_ALPHAARG0,               /* 27, D3DTSS_ALPHAARG0 */
1048     WINED3DTSS_RESULTARG,               /* 28, D3DTSS_RESULTARG */
1049     WINED3DTSS_FORCE_DWORD,             /* 29, unused */
1050     WINED3DTSS_FORCE_DWORD,             /* 30, unused */
1051     WINED3DTSS_FORCE_DWORD,             /* 31, unused */
1052     WINED3DTSS_CONSTANT,                /* 32, D3DTSS_CONSTANT */
1053 };
1054
1055 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD* pValue) {
1056     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1057     HRESULT hr;
1058     TRACE("(%p) Relay\n" , This);
1059
1060     EnterCriticalSection(&d3d9_cs);
1061     hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], pValue);
1062     LeaveCriticalSection(&d3d9_cs);
1063     return hr;
1064 }
1065
1066 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1067     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1068     HRESULT hr;
1069     TRACE("(%p) Relay\n" , This);
1070
1071     EnterCriticalSection(&d3d9_cs);
1072     hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], Value);
1073     LeaveCriticalSection(&d3d9_cs);
1074     return hr;
1075 }
1076
1077 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetSamplerState(LPDIRECT3DDEVICE9EX iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD* pValue) {
1078     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1079     HRESULT hr;
1080     TRACE("(%p) Relay\n" , This);
1081
1082     EnterCriticalSection(&d3d9_cs);
1083     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
1084     LeaveCriticalSection(&d3d9_cs);
1085     return hr;
1086 }
1087
1088 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9EX iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
1089     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1090     HRESULT hr;
1091     TRACE("(%p) Relay\n" , This);
1092
1093     EnterCriticalSection(&d3d9_cs);
1094     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);
1095     LeaveCriticalSection(&d3d9_cs);
1096     return hr;
1097 }
1098
1099 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9EX iface, DWORD* pNumPasses) {
1100     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1101     HRESULT hr;
1102     TRACE("(%p) Relay\n" , This);
1103
1104     EnterCriticalSection(&d3d9_cs);
1105     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1106     LeaveCriticalSection(&d3d9_cs);
1107     return hr;
1108 }
1109
1110 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetPaletteEntries(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1111     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1112     HRESULT hr;
1113     TRACE("(%p) Relay\n" , This);
1114
1115     EnterCriticalSection(&d3d9_cs);
1116     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1117     LeaveCriticalSection(&d3d9_cs);
1118     return hr;
1119 }
1120
1121 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1122     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1123     HRESULT hr;
1124     TRACE("(%p) Relay\n" , This);
1125
1126     EnterCriticalSection(&d3d9_cs);
1127     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1128     LeaveCriticalSection(&d3d9_cs);
1129     return hr;
1130 }
1131
1132 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber) {
1133     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1134     HRESULT hr;
1135     TRACE("(%p) Relay\n" , This);
1136
1137     EnterCriticalSection(&d3d9_cs);
1138     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1139     LeaveCriticalSection(&d3d9_cs);
1140     return hr;
1141 }
1142
1143 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT* PaletteNumber) {
1144     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1145     HRESULT hr;
1146     TRACE("(%p) Relay\n" , This);
1147
1148     EnterCriticalSection(&d3d9_cs);
1149     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1150     LeaveCriticalSection(&d3d9_cs);
1151     return hr;
1152 }
1153
1154 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9EX iface, CONST RECT* pRect) {
1155     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1156     HRESULT hr;
1157     TRACE("(%p) Relay\n" , This);
1158
1159     EnterCriticalSection(&d3d9_cs);
1160     hr = IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);
1161     LeaveCriticalSection(&d3d9_cs);
1162     return hr;
1163 }
1164
1165 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9EX iface, RECT* pRect) {
1166     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1167     HRESULT hr;
1168     TRACE("(%p) Relay\n" , This);
1169
1170     EnterCriticalSection(&d3d9_cs);
1171     hr = IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);
1172     LeaveCriticalSection(&d3d9_cs);
1173     return hr;
1174 }
1175
1176 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface, BOOL bSoftware) {
1177     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1178     HRESULT hr;
1179     TRACE("(%p) Relay\n" , This);
1180
1181     EnterCriticalSection(&d3d9_cs);
1182     hr = IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
1183     LeaveCriticalSection(&d3d9_cs);
1184     return hr;
1185 }
1186
1187 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface) {
1188     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1189     BOOL ret;
1190     TRACE("(%p) Relay\n" , This);
1191
1192     EnterCriticalSection(&d3d9_cs);
1193     ret = IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
1194     LeaveCriticalSection(&d3d9_cs);
1195     return ret;
1196 }
1197
1198 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9EX iface, float nSegments) {
1199     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1200     HRESULT hr;
1201     TRACE("(%p) Relay\n" , This);
1202
1203     EnterCriticalSection(&d3d9_cs);
1204     hr = IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
1205     LeaveCriticalSection(&d3d9_cs);
1206     return hr;
1207 }
1208
1209 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9EX iface) {
1210     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1211     float ret;
1212     TRACE("(%p) Relay\n" , This);
1213
1214     EnterCriticalSection(&d3d9_cs);
1215     ret = IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
1216     LeaveCriticalSection(&d3d9_cs);
1217     return ret;
1218 }
1219
1220 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount) {
1221     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1222     HRESULT hr;
1223     TRACE("(%p) Relay\n" , This);
1224
1225     EnterCriticalSection(&d3d9_cs);
1226     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, PrimitiveType, StartVertex, PrimitiveCount);
1227     LeaveCriticalSection(&d3d9_cs);
1228     return hr;
1229 }
1230
1231 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType,
1232                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
1233     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1234     HRESULT hr;
1235     TRACE("(%p) Relay\n" , This);
1236
1237     /* D3D8 passes the baseVertexIndex in SetIndices, and due to the stateblock functions wined3d has to work that way */
1238     EnterCriticalSection(&d3d9_cs);
1239     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
1240     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1241     LeaveCriticalSection(&d3d9_cs);
1242     return hr;
1243 }
1244
1245 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1246     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1247     HRESULT hr;
1248     TRACE("(%p) Relay\n" , This);
1249
1250     EnterCriticalSection(&d3d9_cs);
1251     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1252     LeaveCriticalSection(&d3d9_cs);
1253     return hr;
1254 }
1255
1256 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
1257                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
1258                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1259     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1260     HRESULT hr;
1261     TRACE("(%p) Relay\n" , This);
1262
1263     EnterCriticalSection(&d3d9_cs);
1264     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1265                                                  pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1266     LeaveCriticalSection(&d3d9_cs);
1267     return hr;
1268 }
1269
1270 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9EX iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
1271     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1272     IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
1273     HRESULT hr;
1274     TRACE("(%p) Relay\n" , This);
1275
1276     EnterCriticalSection(&d3d9_cs);
1277     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, ((IDirect3DVertexBuffer9Impl *)pDestBuffer)->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags);
1278     LeaveCriticalSection(&d3d9_cs);
1279     return hr;
1280 }
1281
1282 static IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
1283     HRESULT hr;
1284     D3DVERTEXELEMENT9* elements = NULL;
1285     IDirect3DVertexDeclaration9* pDecl = NULL;
1286     int p, low, high; /* deliberately signed */
1287     IDirect3DVertexDeclaration9  **convertedDecls = This->convertedDecls;
1288
1289     TRACE("Searching for declaration for fvf %08x... ", fvf);
1290
1291     low = 0;
1292     high = This->numConvertedDecls - 1;
1293     while(low <= high) {
1294         p = (low + high) >> 1;
1295         TRACE("%d ", p);
1296         if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
1297             TRACE("found %p\n", convertedDecls[p]);
1298             return convertedDecls[p];
1299         } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
1300             low = p + 1;
1301         } else {
1302             high = p - 1;
1303         }
1304     }
1305     TRACE("not found. Creating and inserting at position %d.\n", low);
1306
1307     hr = vdecl_convert_fvf(fvf, &elements);
1308     if (hr != S_OK) return NULL;
1309
1310     hr = IDirect3DDevice9Impl_CreateVertexDeclaration((IDirect3DDevice9Ex *) This, elements, &pDecl);
1311     HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
1312     if (hr != S_OK) return NULL;
1313
1314     if(This->declArraySize == This->numConvertedDecls) {
1315         int grow = max(This->declArraySize / 2, 8);
1316         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1317                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1318         if(!convertedDecls) {
1319             /* This will destroy it */
1320             IDirect3DVertexDeclaration9_Release(pDecl);
1321             return NULL;
1322         }
1323         This->convertedDecls = convertedDecls;
1324         This->declArraySize += grow;
1325     }
1326
1327     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
1328     convertedDecls[low] = pDecl;
1329     This->numConvertedDecls++;
1330
1331     /* Will prevent the decl from being destroyed */
1332     ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
1333     IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
1334
1335     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
1336     return pDecl;
1337 }
1338
1339 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9EX iface, DWORD FVF) {
1340     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1341     HRESULT hr = S_OK;
1342     TRACE("(%p) Relay\n" , This);
1343
1344     EnterCriticalSection(&d3d9_cs);
1345     if (0 != FVF) {
1346          IDirect3DVertexDeclaration9* pDecl = getConvertedDecl(This, FVF);
1347
1348          if(!pDecl) {
1349              /* Any situation when this should happen, except out of memory? */
1350              ERR("Failed to create a converted vertex declaration\n");
1351              LeaveCriticalSection(&d3d9_cs);
1352              return D3DERR_DRIVERINTERNALERROR;
1353          }
1354
1355          hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, pDecl);
1356          if (hr != S_OK) {
1357              LeaveCriticalSection(&d3d9_cs);
1358              return hr;
1359          }
1360     }
1361     LeaveCriticalSection(&d3d9_cs);
1362
1363     return hr;
1364 }
1365
1366 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(LPDIRECT3DDEVICE9EX iface, DWORD* pFVF) {
1367     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1368     IDirect3DVertexDeclaration9 *decl;
1369     HRESULT hr;
1370     TRACE("(%p) Relay\n" , This);
1371
1372     hr = IDirect3DDevice9_GetVertexDeclaration(iface, &decl);
1373     if (FAILED(hr))
1374     {
1375         WARN("Failed to get vertex declaration, %#x\n", hr);
1376         *pFVF = 0;
1377         return hr;
1378     }
1379
1380     if (decl)
1381     {
1382         *pFVF = ((IDirect3DVertexDeclaration9Impl *)decl)->convFVF;
1383         IDirect3DVertexDeclaration9_Release(decl);
1384     }
1385     else
1386     {
1387         *pFVF = 0;
1388     }
1389
1390     TRACE("Returning FVF %#x\n", *pFVF);
1391
1392     return hr;
1393 }
1394
1395 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
1396     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1397     HRESULT hr;
1398     TRACE("(%p) Relay\n" , This);
1399
1400     EnterCriticalSection(&d3d9_cs);
1401     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1402                                           pStreamData==NULL ? NULL:((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer, 
1403                                           OffsetInBytes, Stride);
1404     LeaveCriticalSection(&d3d9_cs);
1405     return hr;
1406 }
1407
1408 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride) {
1409     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1410     IWineD3DVertexBuffer *retStream = NULL;
1411     HRESULT rc = D3D_OK;
1412
1413     TRACE("(%p) Relay\n" , This);
1414
1415     if(pStream == NULL){
1416         return D3DERR_INVALIDCALL;
1417     }
1418
1419     EnterCriticalSection(&d3d9_cs);
1420     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, OffsetInBytes, pStride);
1421     if (rc == D3D_OK  && NULL != retStream) {
1422         IWineD3DVertexBuffer_GetParent(retStream, (IUnknown **)pStream);
1423         IWineD3DVertexBuffer_Release(retStream);
1424     }else{
1425         if (rc != D3D_OK){
1426             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
1427         }
1428         *pStream = NULL;
1429     }
1430     LeaveCriticalSection(&d3d9_cs);
1431
1432     return rc;
1433 }
1434
1435 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetStreamSourceFreq(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, UINT Divider) {
1436     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;    
1437     HRESULT hr;
1438     TRACE("(%p) Relay\n" , This);
1439
1440     EnterCriticalSection(&d3d9_cs);
1441     hr = IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
1442     LeaveCriticalSection(&d3d9_cs);
1443     return hr;
1444 }
1445
1446 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, UINT* Divider) {
1447     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1448     HRESULT hr;
1449     TRACE("(%p) Relay\n" , This);
1450
1451     EnterCriticalSection(&d3d9_cs);
1452     hr = IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
1453     LeaveCriticalSection(&d3d9_cs);
1454     return hr;
1455 }
1456
1457 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9EX iface, IDirect3DIndexBuffer9* pIndexData) {
1458     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1459     HRESULT hr;
1460     TRACE("(%p) Relay\n", This);
1461
1462     EnterCriticalSection(&d3d9_cs);
1463     hr = IWineD3DDevice_SetIndices(This->WineD3DDevice,
1464             pIndexData ? ((IDirect3DIndexBuffer9Impl *)pIndexData)->wineD3DIndexBuffer : NULL);
1465     LeaveCriticalSection(&d3d9_cs);
1466     return hr;
1467 }
1468
1469 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetIndices(LPDIRECT3DDEVICE9EX iface, IDirect3DIndexBuffer9 **ppIndexData) {
1470     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1471     IWineD3DIndexBuffer *retIndexData = NULL;
1472     HRESULT rc = D3D_OK;
1473
1474     TRACE("(%p) Relay\n", This);
1475
1476     if(ppIndexData == NULL){
1477         return D3DERR_INVALIDCALL;
1478     }
1479
1480     EnterCriticalSection(&d3d9_cs);
1481     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData);
1482     if (SUCCEEDED(rc) && retIndexData) {
1483         IWineD3DIndexBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
1484         IWineD3DIndexBuffer_Release(retIndexData);
1485     } else {
1486         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
1487         *ppIndexData = NULL;
1488     }
1489     LeaveCriticalSection(&d3d9_cs);
1490     return rc;
1491 }
1492
1493 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
1494     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1495     HRESULT hr;
1496     TRACE("(%p) Relay\n", This);
1497
1498     EnterCriticalSection(&d3d9_cs);
1499     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
1500     LeaveCriticalSection(&d3d9_cs);
1501     return hr;
1502 }
1503
1504 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
1505     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1506     HRESULT hr;
1507     TRACE("(%p) Relay\n", This);
1508
1509     EnterCriticalSection(&d3d9_cs);
1510     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
1511     LeaveCriticalSection(&d3d9_cs);
1512     return hr;
1513 }
1514
1515 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9EX iface, UINT Handle) {
1516     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1517     HRESULT hr;
1518     TRACE("(%p) Relay\n", This);
1519
1520     EnterCriticalSection(&d3d9_cs);
1521     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
1522     LeaveCriticalSection(&d3d9_cs);
1523     return hr;
1524 }
1525
1526 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(LPDIRECT3DDEVICE9EX iface, UINT width, UINT height, float *rows, float *columns) {
1527     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1528     FIXME("(%p)->(%d, %d, %p, %p) Stub!\n", This, width, height, rows, columns);
1529     return WINED3DERR_INVALIDCALL;
1530 }
1531
1532 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_ComposeRects(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9 *pSrc, IDirect3DDevice9 *pDst, IDirect3DVertexBuffer9 *pSrcRectDescs, UINT NumRects, IDirect3DVertexBuffer9 *pDstRectDescs, D3DCOMPOSERECTSOP Operation, int Xoffset, int Yoffset) {
1533     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1534     FIXME("(%p)->(%p, %p, %p, %d, %p, %d, %d, %d) Stub!\n", This, pSrc, pDst, pSrcRectDescs, NumRects, pDstRectDescs, Operation, Xoffset, Yoffset);
1535     return WINED3DERR_INVALIDCALL;
1536 }
1537
1538 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_PresentEx(LPDIRECT3DDEVICE9EX iface, CONST RECT *pSourceRect, CONST RECT *pDestRect, HWND hDestWindowOverride, CONST RGNDATA *pDirtyRegion, DWORD dwFlags) {
1539     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1540     FIXME("(%p= -> (%p), %p, %p, %p, 0x%08x) Stub!\n", This, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion, dwFlags);
1541     return WINED3DERR_INVALIDCALL;
1542 }
1543
1544 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_GetGPUThreadPriority(LPDIRECT3DDEVICE9EX iface, INT *pPriority) {
1545     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1546     FIXME("(%p)->(%p) Stub!\n", This, pPriority);
1547     return WINED3DERR_INVALIDCALL;
1548 }
1549
1550 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_SetGPUThreadPriority(LPDIRECT3DDEVICE9EX iface, INT Priority) {
1551     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1552     FIXME("(%p)->(%d) Stub!\n", This, Priority);
1553     return WINED3DERR_INVALIDCALL;
1554 }
1555
1556 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_WaitForVBlank(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain) {
1557     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1558     FIXME("(%p)->(%d) Stub!\n", This, iSwapChain);
1559     return WINED3DERR_INVALIDCALL;
1560 }
1561
1562 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_CheckresourceResidency(LPDIRECT3DDEVICE9EX iface, IDirect3DResource9 ** pResourceArray, UINT32 Numresources) {
1563     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1564     FIXME("(%p)->(%p, %d) Stub!\n", This, pResourceArray, Numresources);
1565     return WINED3DERR_INVALIDCALL;
1566 }
1567
1568 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_SetMaximumFrameLatency(LPDIRECT3DDEVICE9EX iface, UINT MaxLatency) {
1569     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1570     FIXME("(%p)->(%d) Stub!\n", This, MaxLatency);
1571     return WINED3DERR_INVALIDCALL;
1572 }
1573
1574 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_GetMaximumFrameLatency(LPDIRECT3DDEVICE9EX iface, UINT *pMaxLatency) {
1575     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1576     FIXME("(%p)->(%p) Stub!\n", This, pMaxLatency);
1577     *pMaxLatency = 2;
1578     return WINED3DERR_INVALIDCALL;
1579 }
1580
1581 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_CheckdeviceState(LPDIRECT3DDEVICE9EX iface, HWND hDestinationWindow) {
1582     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1583     FIXME("(%p)->(%p) Stub!\n", This, hDestinationWindow);
1584     return WINED3DERR_INVALIDCALL;
1585 }
1586
1587 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_CreateRenderTargetEx(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultiSampleQuality, BOOL Lockable, IDirect3DSurface9 ** ppSurface, HANDLE *pSharedHandle, DWORD Usage) {
1588     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1589     FIXME("(%p)->(%d, %d, %d, %d, %d, %s, %p, %p, 0x%08x) Stub!\n", This, Width, Height, Format, MultiSample, MultiSampleQuality, Lockable ? "true" : "false", ppSurface, pSharedHandle, Usage);
1590     return WINED3DERR_INVALIDCALL;
1591 }
1592
1593 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE *pSharedHandle, DWORD Usage) {
1594     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1595     FIXME("(%p)->(%d, %d, %d, %d, %p, %p, 0x%08x) Stub!\n", This, Width, Height, Format, Pool, ppSurface, pSharedHandle, Usage);
1596     return WINED3DERR_INVALIDCALL;
1597 }
1598
1599 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultiSampleQuality, BOOL Discard, IDirect3DSurface9 **ppSurface, HANDLE *pSharedHandle, DWORD Usage) {
1600     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1601     FIXME("(%p)->(%d, %d, %d, %d, %d, %s, %p, %p, 0x%08x) Stub!\n", This, Width, Height, Format, MultiSample, MultiSampleQuality, Discard ? "true" : "false", ppSurface, pSharedHandle, Usage);
1602     return WINED3DERR_INVALIDCALL;
1603 }
1604
1605 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_ResetEx(LPDIRECT3DDEVICE9EX iface, D3DPRESENT_PARAMETERS *pPresentationParameters, D3DDISPLAYMODEEX *pFullscreenDisplayMode) {
1606     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1607     FIXME("(%p)->(%p) Stub!\n", This, pPresentationParameters);
1608     return WINED3DERR_INVALIDCALL;
1609 }
1610
1611 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_GetDisplayModeEx(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DDISPLAYMODEEX *pMode, D3DDISPLAYROTATION *pRotation) {
1612     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *) iface;
1613     FIXME("(%p)->(%d, %p, %p) Stub!\n", This, iSwapChain, pMode, pRotation);
1614     return WINED3DERR_INVALIDCALL;
1615 }
1616
1617 const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
1618 {
1619     /* IUnknown */
1620     IDirect3DDevice9Impl_QueryInterface,
1621     IDirect3DDevice9Impl_AddRef,
1622     IDirect3DDevice9Impl_Release,
1623     /* IDirect3DDevice9 */
1624     IDirect3DDevice9Impl_TestCooperativeLevel,
1625     IDirect3DDevice9Impl_GetAvailableTextureMem,
1626     IDirect3DDevice9Impl_EvictManagedResources,
1627     IDirect3DDevice9Impl_GetDirect3D,
1628     IDirect3DDevice9Impl_GetDeviceCaps,
1629     IDirect3DDevice9Impl_GetDisplayMode,
1630     IDirect3DDevice9Impl_GetCreationParameters,
1631     IDirect3DDevice9Impl_SetCursorProperties,
1632     IDirect3DDevice9Impl_SetCursorPosition,
1633     IDirect3DDevice9Impl_ShowCursor,
1634     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
1635     IDirect3DDevice9Impl_GetSwapChain,
1636     IDirect3DDevice9Impl_GetNumberOfSwapChains,
1637     IDirect3DDevice9Impl_Reset,
1638     IDirect3DDevice9Impl_Present,
1639     IDirect3DDevice9Impl_GetBackBuffer,
1640     IDirect3DDevice9Impl_GetRasterStatus,
1641     IDirect3DDevice9Impl_SetDialogBoxMode,
1642     IDirect3DDevice9Impl_SetGammaRamp,
1643     IDirect3DDevice9Impl_GetGammaRamp,
1644     IDirect3DDevice9Impl_CreateTexture,
1645     IDirect3DDevice9Impl_CreateVolumeTexture,
1646     IDirect3DDevice9Impl_CreateCubeTexture,
1647     IDirect3DDevice9Impl_CreateVertexBuffer,
1648     IDirect3DDevice9Impl_CreateIndexBuffer,
1649     IDirect3DDevice9Impl_CreateRenderTarget,
1650     IDirect3DDevice9Impl_CreateDepthStencilSurface,
1651     IDirect3DDevice9Impl_UpdateSurface,
1652     IDirect3DDevice9Impl_UpdateTexture,
1653     IDirect3DDevice9Impl_GetRenderTargetData,
1654     IDirect3DDevice9Impl_GetFrontBufferData,
1655     IDirect3DDevice9Impl_StretchRect,
1656     IDirect3DDevice9Impl_ColorFill,
1657     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
1658     IDirect3DDevice9Impl_SetRenderTarget,
1659     IDirect3DDevice9Impl_GetRenderTarget,
1660     IDirect3DDevice9Impl_SetDepthStencilSurface,
1661     IDirect3DDevice9Impl_GetDepthStencilSurface,
1662     IDirect3DDevice9Impl_BeginScene,
1663     IDirect3DDevice9Impl_EndScene,
1664     IDirect3DDevice9Impl_Clear,
1665     IDirect3DDevice9Impl_SetTransform,
1666     IDirect3DDevice9Impl_GetTransform,
1667     IDirect3DDevice9Impl_MultiplyTransform,
1668     IDirect3DDevice9Impl_SetViewport,
1669     IDirect3DDevice9Impl_GetViewport,
1670     IDirect3DDevice9Impl_SetMaterial,
1671     IDirect3DDevice9Impl_GetMaterial,
1672     IDirect3DDevice9Impl_SetLight,
1673     IDirect3DDevice9Impl_GetLight,
1674     IDirect3DDevice9Impl_LightEnable,
1675     IDirect3DDevice9Impl_GetLightEnable,
1676     IDirect3DDevice9Impl_SetClipPlane,
1677     IDirect3DDevice9Impl_GetClipPlane,
1678     IDirect3DDevice9Impl_SetRenderState,
1679     IDirect3DDevice9Impl_GetRenderState,
1680     IDirect3DDevice9Impl_CreateStateBlock,
1681     IDirect3DDevice9Impl_BeginStateBlock,
1682     IDirect3DDevice9Impl_EndStateBlock,
1683     IDirect3DDevice9Impl_SetClipStatus,
1684     IDirect3DDevice9Impl_GetClipStatus,
1685     IDirect3DDevice9Impl_GetTexture,
1686     IDirect3DDevice9Impl_SetTexture,
1687     IDirect3DDevice9Impl_GetTextureStageState,
1688     IDirect3DDevice9Impl_SetTextureStageState,
1689     IDirect3DDevice9Impl_GetSamplerState,
1690     IDirect3DDevice9Impl_SetSamplerState,
1691     IDirect3DDevice9Impl_ValidateDevice,
1692     IDirect3DDevice9Impl_SetPaletteEntries,
1693     IDirect3DDevice9Impl_GetPaletteEntries,
1694     IDirect3DDevice9Impl_SetCurrentTexturePalette,
1695     IDirect3DDevice9Impl_GetCurrentTexturePalette,
1696     IDirect3DDevice9Impl_SetScissorRect,
1697     IDirect3DDevice9Impl_GetScissorRect,
1698     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
1699     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
1700     IDirect3DDevice9Impl_SetNPatchMode,
1701     IDirect3DDevice9Impl_GetNPatchMode,
1702     IDirect3DDevice9Impl_DrawPrimitive,
1703     IDirect3DDevice9Impl_DrawIndexedPrimitive,
1704     IDirect3DDevice9Impl_DrawPrimitiveUP,
1705     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
1706     IDirect3DDevice9Impl_ProcessVertices,
1707     IDirect3DDevice9Impl_CreateVertexDeclaration,
1708     IDirect3DDevice9Impl_SetVertexDeclaration,
1709     IDirect3DDevice9Impl_GetVertexDeclaration,
1710     IDirect3DDevice9Impl_SetFVF,
1711     IDirect3DDevice9Impl_GetFVF,
1712     IDirect3DDevice9Impl_CreateVertexShader,
1713     IDirect3DDevice9Impl_SetVertexShader,
1714     IDirect3DDevice9Impl_GetVertexShader,
1715     IDirect3DDevice9Impl_SetVertexShaderConstantF,
1716     IDirect3DDevice9Impl_GetVertexShaderConstantF,
1717     IDirect3DDevice9Impl_SetVertexShaderConstantI,
1718     IDirect3DDevice9Impl_GetVertexShaderConstantI,
1719     IDirect3DDevice9Impl_SetVertexShaderConstantB,
1720     IDirect3DDevice9Impl_GetVertexShaderConstantB,
1721     IDirect3DDevice9Impl_SetStreamSource,
1722     IDirect3DDevice9Impl_GetStreamSource,
1723     IDirect3DDevice9Impl_SetStreamSourceFreq,
1724     IDirect3DDevice9Impl_GetStreamSourceFreq,
1725     IDirect3DDevice9Impl_SetIndices,
1726     IDirect3DDevice9Impl_GetIndices,
1727     IDirect3DDevice9Impl_CreatePixelShader,
1728     IDirect3DDevice9Impl_SetPixelShader,
1729     IDirect3DDevice9Impl_GetPixelShader,
1730     IDirect3DDevice9Impl_SetPixelShaderConstantF,
1731     IDirect3DDevice9Impl_GetPixelShaderConstantF,
1732     IDirect3DDevice9Impl_SetPixelShaderConstantI,
1733     IDirect3DDevice9Impl_GetPixelShaderConstantI,
1734     IDirect3DDevice9Impl_SetPixelShaderConstantB,
1735     IDirect3DDevice9Impl_GetPixelShaderConstantB,
1736     IDirect3DDevice9Impl_DrawRectPatch,
1737     IDirect3DDevice9Impl_DrawTriPatch,
1738     IDirect3DDevice9Impl_DeletePatch,
1739     IDirect3DDevice9Impl_CreateQuery,
1740     /* IDirect3DDevice9Ex */
1741     IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
1742     IDirect3DDevice9ExImpl_ComposeRects,
1743     IDirect3DDevice9ExImpl_PresentEx,
1744     IDirect3DDevice9ExImpl_GetGPUThreadPriority,
1745     IDirect3DDevice9ExImpl_SetGPUThreadPriority,
1746     IDirect3DDevice9ExImpl_WaitForVBlank,
1747     IDirect3DDevice9ExImpl_CheckresourceResidency,
1748     IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
1749     IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
1750     IDirect3DDevice9ExImpl_CheckdeviceState,
1751     IDirect3DDevice9ExImpl_CreateRenderTargetEx,
1752     IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
1753     IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
1754     IDirect3DDevice9ExImpl_ResetEx,
1755     IDirect3DDevice9ExImpl_GetDisplayModeEx
1756 };
1757
1758 ULONG WINAPI D3D9CB_DestroySurface(IWineD3DSurface *pSurface) {
1759     IDirect3DSurface9Impl* surfaceParent;
1760     TRACE("(%p) call back\n", pSurface);
1761
1762     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
1763     /* GetParent's AddRef was forwarded to an object in destruction.
1764      * Releasing it here again would cause an endless recursion. */
1765     surfaceParent->forwardReference = NULL;
1766     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
1767 }
1768
1769 /* IWineD3DDeviceParent IUnknown methods */
1770
1771 static inline struct IDirect3DDevice9Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
1772 {
1773     return (struct IDirect3DDevice9Impl *)((char*)iface
1774             - FIELD_OFFSET(struct IDirect3DDevice9Impl, device_parent_vtbl));
1775 }
1776
1777 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
1778 {
1779     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1780     return IDirect3DDevice9Impl_QueryInterface((IDirect3DDevice9Ex *)This, riid, object);
1781 }
1782
1783 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
1784 {
1785     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1786     return IDirect3DDevice9Impl_AddRef((IDirect3DDevice9Ex *)This);
1787 }
1788
1789 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
1790 {
1791     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1792     return IDirect3DDevice9Impl_Release((IDirect3DDevice9Ex *)This);
1793 }
1794
1795 /* IWineD3DDeviceParent methods */
1796
1797 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
1798         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
1799         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
1800 {
1801     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1802     IDirect3DSurface9Impl *d3d_surface;
1803     BOOL lockable = TRUE;
1804     HRESULT hr;
1805
1806     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
1807             "\tpool %#x, level %u, face %u, surface %p\n",
1808             iface, superior, width, height, format, usage, pool, level, face, surface);
1809
1810     if (pool == D3DPOOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC)) lockable = FALSE;
1811
1812     hr = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9Ex *)This, width, height,
1813             format, lockable, FALSE /* Discard */, level, (IDirect3DSurface9 **)&d3d_surface,
1814             D3DRTYPE_SURFACE, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */, NULL);
1815     if (FAILED(hr))
1816     {
1817         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
1818         return hr;
1819     }
1820
1821     *surface = d3d_surface->wineD3DSurface;
1822     d3d_surface->container = superior;
1823     IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
1824     d3d_surface->parentDevice = NULL;
1825     d3d_surface->forwardReference = superior;
1826
1827     return hr;
1828 }
1829
1830 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
1831         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
1832         DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
1833 {
1834     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1835     IDirect3DSurface9Impl *d3d_surface;
1836     HRESULT hr;
1837
1838     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
1839             "\tmultisample_quality %u, lockable %u, surface %p\n",
1840             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
1841
1842     hr = IDirect3DDevice9Impl_CreateRenderTarget((IDirect3DDevice9Ex *)This, width, height, format,
1843             multisample_type, multisample_quality, lockable, (IDirect3DSurface9 **)&d3d_surface, NULL);
1844     if (FAILED(hr))
1845     {
1846         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
1847         return hr;
1848     }
1849
1850     *surface = d3d_surface->wineD3DSurface;
1851     d3d_surface->container = superior;
1852     d3d_surface->isImplicit = TRUE;
1853     /* Implicit surfaces are created with an refcount of 0 */
1854     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
1855
1856     return hr;
1857 }
1858
1859 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
1860         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
1861         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
1862 {
1863     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1864     IDirect3DSurface9Impl *d3d_surface;
1865     HRESULT hr;
1866
1867     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
1868             "\tmultisample_quality %u, discard %u, surface %p\n",
1869             iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
1870
1871     hr = IDirect3DDevice9Impl_CreateDepthStencilSurface((IDirect3DDevice9Ex *)This, width, height, format,
1872             multisample_type, multisample_quality, discard, (IDirect3DSurface9 **)&d3d_surface, NULL);
1873     if (FAILED(hr))
1874     {
1875         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
1876         return hr;
1877     }
1878
1879     *surface = d3d_surface->wineD3DSurface;
1880     d3d_surface->container = (IUnknown *)This;
1881     d3d_surface->isImplicit = TRUE;
1882     /* Implicit surfaces are created with an refcount of 0 */
1883     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
1884
1885     return hr;
1886 }
1887
1888 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
1889         IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
1890         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
1891 {
1892     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1893     IDirect3DVolume9Impl *object;
1894     HRESULT hr;
1895
1896     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
1897             iface, superior, width, height, depth, format, pool, usage, volume);
1898
1899     /* Allocate the storage for the device */
1900     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1901     if (!object)
1902     {
1903         FIXME("Allocation of memory failed\n");
1904         *volume = NULL;
1905         return D3DERR_OUTOFVIDEOMEMORY;
1906     }
1907
1908     object->lpVtbl = &Direct3DVolume9_Vtbl;
1909     object->ref = 1;
1910     hr = IWineD3DDevice_CreateVolume(This->WineD3DDevice, width, height, depth, usage & WINED3DUSAGE_MASK,
1911             format, pool, &object->wineD3DVolume, NULL, (IUnknown *)object);
1912     if (FAILED(hr))
1913     {
1914         ERR("(%p) CreateVolume failed, returning %#x\n", iface, hr);
1915         HeapFree(GetProcessHeap(), 0, object);
1916         *volume = NULL;
1917         return hr;
1918     }
1919
1920     *volume = object->wineD3DVolume;
1921     object->container = superior;
1922     object->forwardReference = superior;
1923
1924     TRACE("(%p) Created volume %p\n", iface, *volume);
1925
1926     return hr;
1927 }
1928
1929 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
1930         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
1931 {
1932     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
1933     IDirect3DSwapChain9Impl *d3d_swapchain;
1934     D3DPRESENT_PARAMETERS local_parameters;
1935     HRESULT hr;
1936
1937     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
1938
1939     /* Copy the presentation parameters */
1940     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
1941     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
1942     local_parameters.BackBufferFormat = present_parameters->BackBufferFormat;
1943     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
1944     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
1945     local_parameters.MultiSampleQuality = present_parameters->MultiSampleQuality;
1946     local_parameters.SwapEffect = present_parameters->SwapEffect;
1947     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
1948     local_parameters.Windowed = present_parameters->Windowed;
1949     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
1950     local_parameters.AutoDepthStencilFormat = present_parameters->AutoDepthStencilFormat;
1951     local_parameters.Flags = present_parameters->Flags;
1952     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
1953     local_parameters.PresentationInterval = present_parameters->PresentationInterval;
1954
1955     hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain((IDirect3DDevice9Ex *)This,
1956             &local_parameters, (IDirect3DSwapChain9 **)&d3d_swapchain);
1957     if (FAILED(hr))
1958     {
1959         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
1960         *swapchain = NULL;
1961         return hr;
1962     }
1963
1964     *swapchain = d3d_swapchain->wineD3DSwapChain;
1965     d3d_swapchain->isImplicit = TRUE;
1966     /* Implicit swap chains are created with an refcount of 0 */
1967     IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)d3d_swapchain);
1968
1969     /* Copy back the presentation parameters */
1970     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
1971     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
1972     present_parameters->BackBufferFormat = local_parameters.BackBufferFormat;
1973     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
1974     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
1975     present_parameters->MultiSampleQuality = local_parameters.MultiSampleQuality;
1976     present_parameters->SwapEffect = local_parameters.SwapEffect;
1977     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
1978     present_parameters->Windowed = local_parameters.Windowed;
1979     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
1980     present_parameters->AutoDepthStencilFormat = local_parameters.AutoDepthStencilFormat;
1981     present_parameters->Flags = local_parameters.Flags;
1982     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
1983     present_parameters->PresentationInterval = local_parameters.PresentationInterval;
1984
1985     return hr;
1986 }
1987
1988 const IWineD3DDeviceParentVtbl d3d9_wined3d_device_parent_vtbl =
1989 {
1990     /* IUnknown methods */
1991     device_parent_QueryInterface,
1992     device_parent_AddRef,
1993     device_parent_Release,
1994     /* IWineD3DDeviceParent methods */
1995     device_parent_CreateSurface,
1996     device_parent_CreateRenderTarget,
1997     device_parent_CreateDepthStencilSurface,
1998     device_parent_CreateVolume,
1999     device_parent_CreateSwapChain,
2000 };