d3d8: Get rid of some unused variables in IDirect3DDevice8Impl_CopyRects() (LLVM...
[wine] / dlls / d3d8 / device.c
1 /*
2  * IDirect3DDevice8 implementation
3  *
4  * Copyright 2002-2004 Jason Edmeades
5  * Copyright 2004 Christian Costa
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23
24 #include <math.h>
25 #include <stdarg.h>
26
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
34
35 #include "d3d8_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
38
39 D3DFORMAT d3dformat_from_wined3dformat(WINED3DFORMAT format)
40 {
41     BYTE *c = (BYTE *)&format;
42
43     /* Don't translate FOURCC formats */
44     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
45
46     switch(format)
47     {
48         case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
49         case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
50         case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
51         case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
52         case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
53         case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
54         case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
55         case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
56         case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
57         case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
58         case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
59         case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
60         case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
61         case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
62         case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63         case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64         case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65         case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66         case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67         case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68         case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69         case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70         case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71         case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72         case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
73         case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
74         case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
75         case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
76         case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
77         case WINED3DFMT_S8_UINT_D24_UNORM: return D3DFMT_D24S8;
78         case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
79         case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
80         case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
81         case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
82         case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
83         case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
84         default:
85             FIXME("Unhandled WINED3DFORMAT %#x\n", format);
86             return D3DFMT_UNKNOWN;
87     }
88 }
89
90 WINED3DFORMAT wined3dformat_from_d3dformat(D3DFORMAT format)
91 {
92     BYTE *c = (BYTE *)&format;
93
94     /* Don't translate FOURCC formats */
95     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
96
97     switch(format)
98     {
99         case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
100         case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
101         case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
102         case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
103         case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
104         case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
105         case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
106         case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
107         case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
108         case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
109         case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
110         case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
111         case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
112         case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
113         case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
114         case D3DFMT_P8: return WINED3DFMT_P8_UINT;
115         case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
116         case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
117         case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
118         case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
119         case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
120         case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
121         case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
122         case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
123         case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
124         case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
125         case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
126         case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
127         case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
128         case D3DFMT_D24S8: return WINED3DFMT_S8_UINT_D24_UNORM;
129         case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
130         case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
131         case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
132         case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
133         case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
134         case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
135         default:
136             FIXME("Unhandled D3DFORMAT %#x\n", format);
137             return WINED3DFMT_UNKNOWN;
138     }
139 }
140
141 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
142 {
143     switch(primitive_type)
144     {
145         case D3DPT_POINTLIST:
146             return primitive_count;
147
148         case D3DPT_LINELIST:
149             return primitive_count * 2;
150
151         case D3DPT_LINESTRIP:
152             return primitive_count + 1;
153
154         case D3DPT_TRIANGLELIST:
155             return primitive_count * 3;
156
157         case D3DPT_TRIANGLESTRIP:
158         case D3DPT_TRIANGLEFAN:
159             return primitive_count + 2;
160
161         default:
162             FIXME("Unhandled primitive type %#x\n", primitive_type);
163             return 0;
164     }
165 }
166
167 /* Handle table functions */
168 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
169 {
170     struct d3d8_handle_entry *entry;
171
172     if (t->free_entries)
173     {
174         /* Use a free handle */
175         entry = t->free_entries;
176         if (entry->type != D3D8_HANDLE_FREE)
177         {
178             ERR("Handle %u(%p) is in the free list, but has type %#x.\n", (entry - t->entries), entry, entry->type);
179             return D3D8_INVALID_HANDLE;
180         }
181         t->free_entries = entry->object;
182         entry->object = object;
183         entry->type = type;
184
185         return entry - t->entries;
186     }
187
188     if (!(t->entry_count < t->table_size))
189     {
190         /* Grow the table */
191         UINT new_size = t->table_size + (t->table_size >> 1);
192         struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
193                 0, t->entries, new_size * sizeof(*t->entries));
194         if (!new_entries)
195         {
196             ERR("Failed to grow the handle table.\n");
197             return D3D8_INVALID_HANDLE;
198         }
199         t->entries = new_entries;
200         t->table_size = new_size;
201     }
202
203     entry = &t->entries[t->entry_count];
204     entry->object = object;
205     entry->type = type;
206
207     return t->entry_count++;
208 }
209
210 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
211 {
212     struct d3d8_handle_entry *entry;
213     void *object;
214
215     if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
216     {
217         WARN("Invalid handle %u passed.\n", handle);
218         return NULL;
219     }
220
221     entry = &t->entries[handle];
222     if (entry->type != type)
223     {
224         WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
225         return NULL;
226     }
227
228     object = entry->object;
229     entry->object = t->free_entries;
230     entry->type = D3D8_HANDLE_FREE;
231     t->free_entries = entry;
232
233     return object;
234 }
235
236 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
237 {
238     struct d3d8_handle_entry *entry;
239
240     if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
241     {
242         WARN("Invalid handle %u passed.\n", handle);
243         return NULL;
244     }
245
246     entry = &t->entries[handle];
247     if (entry->type != type)
248     {
249         WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
250         return NULL;
251     }
252
253     return entry->object;
254 }
255
256 /* IDirect3D IUnknown parts follow: */
257 static HRESULT WINAPI IDirect3DDevice8Impl_QueryInterface(LPDIRECT3DDEVICE8 iface,REFIID riid,LPVOID *ppobj)
258 {
259     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
260
261     if (IsEqualGUID(riid, &IID_IUnknown)
262         || IsEqualGUID(riid, &IID_IDirect3DDevice8)) {
263         IUnknown_AddRef(iface);
264         *ppobj = This;
265         return S_OK;
266     }
267
268     if (IsEqualGUID(riid, &IID_IWineD3DDeviceParent))
269     {
270         IUnknown_AddRef((IUnknown *)&This->device_parent_vtbl);
271         *ppobj = &This->device_parent_vtbl;
272         return S_OK;
273     }
274
275     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
276     *ppobj = NULL;
277     return E_NOINTERFACE;
278 }
279
280 static ULONG WINAPI IDirect3DDevice8Impl_AddRef(LPDIRECT3DDEVICE8 iface) {
281     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
282     ULONG ref = InterlockedIncrement(&This->ref);
283
284     TRACE("(%p) : AddRef from %d\n", This, ref - 1);
285
286     return ref;
287 }
288
289 static ULONG WINAPI IDirect3DDevice8Impl_Release(LPDIRECT3DDEVICE8 iface) {
290     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
291     ULONG ref;
292
293     if (This->inDestruction) return 0;
294     ref = InterlockedDecrement(&This->ref);
295
296     TRACE("(%p) : ReleaseRef to %d\n", This, ref);
297
298     if (ref == 0) {
299         unsigned i;
300
301         TRACE("Releasing wined3d device %p\n", This->WineD3DDevice);
302
303         wined3d_mutex_lock();
304
305         This->inDestruction = TRUE;
306
307         for(i = 0; i < This->numConvertedDecls; i++) {
308             IDirect3DVertexDeclaration8_Release(This->decls[i].decl);
309         }
310         HeapFree(GetProcessHeap(), 0, This->decls);
311
312         IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D8CB_DestroySwapChain);
313         IWineD3DDevice_Release(This->WineD3DDevice);
314         HeapFree(GetProcessHeap(), 0, This->handle_table.entries);
315         HeapFree(GetProcessHeap(), 0, This);
316
317         wined3d_mutex_unlock();
318     }
319     return ref;
320 }
321
322 /* IDirect3DDevice Interface follow: */
323 static HRESULT WINAPI IDirect3DDevice8Impl_TestCooperativeLevel(LPDIRECT3DDEVICE8 iface) {
324     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
325     HRESULT hr;
326
327     TRACE("(%p) : Relay\n", This);
328
329     wined3d_mutex_lock();
330     hr = IWineD3DDevice_TestCooperativeLevel(This->WineD3DDevice);
331     wined3d_mutex_unlock();
332
333     return hr;
334 }
335
336 static UINT WINAPI  IDirect3DDevice8Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE8 iface) {
337     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
338     HRESULT hr;
339
340     TRACE("(%p) Relay\n", This);
341
342     wined3d_mutex_lock();
343     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
344     wined3d_mutex_unlock();
345
346     return hr;
347 }
348
349 static HRESULT WINAPI IDirect3DDevice8Impl_ResourceManagerDiscardBytes(LPDIRECT3DDEVICE8 iface, DWORD Bytes) {
350     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
351     HRESULT hr;
352
353     TRACE("(%p) : Relay bytes(%d)\n", This, Bytes);
354
355     wined3d_mutex_lock();
356     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
357     wined3d_mutex_unlock();
358
359     return hr;
360 }
361
362 static HRESULT WINAPI IDirect3DDevice8Impl_GetDirect3D(LPDIRECT3DDEVICE8 iface, IDirect3D8** ppD3D8) {
363     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
364     HRESULT hr = D3D_OK;
365     IWineD3D* pWineD3D;
366
367     TRACE("(%p) Relay\n", This);
368
369     if (NULL == ppD3D8) {
370         return D3DERR_INVALIDCALL;
371     }
372
373     wined3d_mutex_lock();
374     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
375     if (hr == D3D_OK && pWineD3D != NULL)
376     {
377         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D8);
378         IWineD3D_Release(pWineD3D);
379     } else {
380         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
381         *ppD3D8 = NULL;
382     }
383     wined3d_mutex_unlock();
384
385     TRACE("(%p) returning %p\n",This , *ppD3D8);
386
387     return hr;
388 }
389
390 static HRESULT WINAPI IDirect3DDevice8Impl_GetDeviceCaps(LPDIRECT3DDEVICE8 iface, D3DCAPS8* pCaps) {
391     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
392     HRESULT hrc = D3D_OK;
393     WINED3DCAPS *pWineCaps;
394
395     TRACE("(%p) : Relay pCaps %p\n", This, pCaps);
396     if(NULL == pCaps){
397         return D3DERR_INVALIDCALL;
398     }
399     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
400     if(pWineCaps == NULL){
401         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
402     }
403
404     wined3d_mutex_lock();
405     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
406     wined3d_mutex_unlock();
407
408     fixup_caps(pWineCaps);
409     WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
410     HeapFree(GetProcessHeap(), 0, pWineCaps);
411
412     TRACE("Returning %p %p\n", This, pCaps);
413     return hrc;
414 }
415
416 static HRESULT WINAPI IDirect3DDevice8Impl_GetDisplayMode(LPDIRECT3DDEVICE8 iface, D3DDISPLAYMODE* pMode) {
417     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
418     HRESULT hr;
419     TRACE("(%p) Relay\n", This);
420
421     wined3d_mutex_lock();
422     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, 0, (WINED3DDISPLAYMODE *) pMode);
423     wined3d_mutex_unlock();
424
425     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
426
427     return hr;
428 }
429
430 static HRESULT WINAPI IDirect3DDevice8Impl_GetCreationParameters(LPDIRECT3DDEVICE8 iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
431     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
432     HRESULT hr;
433     TRACE("(%p) Relay\n", This);
434
435     wined3d_mutex_lock();
436     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
437     wined3d_mutex_unlock();
438
439     return hr;
440 }
441
442 static HRESULT WINAPI IDirect3DDevice8Impl_SetCursorProperties(LPDIRECT3DDEVICE8 iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8* pCursorBitmap) {
443     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
444     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl*)pCursorBitmap;
445     HRESULT hr;
446     TRACE("(%p) Relay\n", This);
447     if(!pCursorBitmap) {
448         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
449         return WINED3DERR_INVALIDCALL;
450     }
451
452     wined3d_mutex_lock();
453     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice,XHotSpot,YHotSpot,pSurface->wineD3DSurface);
454     wined3d_mutex_unlock();
455
456     return hr;
457 }
458
459 static void WINAPI IDirect3DDevice8Impl_SetCursorPosition(LPDIRECT3DDEVICE8 iface, UINT XScreenSpace, UINT YScreenSpace, DWORD Flags) {
460     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
461     TRACE("(%p) Relay\n", This);
462
463     wined3d_mutex_lock();
464     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
465     wined3d_mutex_unlock();
466 }
467
468 static BOOL WINAPI IDirect3DDevice8Impl_ShowCursor(LPDIRECT3DDEVICE8 iface, BOOL bShow) {
469     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
470     BOOL ret;
471     TRACE("(%p) Relay\n", This);
472
473     wined3d_mutex_lock();
474     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
475     wined3d_mutex_unlock();
476
477     return ret;
478 }
479
480 static HRESULT WINAPI IDirect3DDevice8Impl_CreateAdditionalSwapChain(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters, IDirect3DSwapChain8** pSwapChain) {
481     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
482     IDirect3DSwapChain8Impl* object;
483     HRESULT hrc = D3D_OK;
484     WINED3DPRESENT_PARAMETERS localParameters;
485
486     TRACE("(%p) Relay\n", This);
487
488     /* Fix the back buffer count */
489     if(pPresentationParameters->BackBufferCount == 0) {
490         pPresentationParameters->BackBufferCount = 1;
491     }
492
493     object = HeapAlloc(GetProcessHeap(),  HEAP_ZERO_MEMORY, sizeof(*object));
494     if (NULL == object) {
495         FIXME("Allocation of memory failed\n");
496         *pSwapChain = NULL;
497         return D3DERR_OUTOFVIDEOMEMORY;
498     }
499     object->ref = 1;
500     object->lpVtbl = &Direct3DSwapChain8_Vtbl;
501
502     /* Allocate an associated WineD3DDevice object */
503     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
504     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
505     localParameters.BackBufferFormat                            = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
506     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
507     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
508     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
509     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
510     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
511     localParameters.Windowed                                    = pPresentationParameters->Windowed;
512     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
513     localParameters.AutoDepthStencilFormat                      = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
514     localParameters.Flags                                       = pPresentationParameters->Flags;
515     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
516     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
517     localParameters.AutoRestoreDisplayMode                      = TRUE;
518
519     wined3d_mutex_lock();
520     hrc = IWineD3DDevice_CreateSwapChain(This->WineD3DDevice, &localParameters,
521             &object->wineD3DSwapChain, (IUnknown *)object, SURFACE_OPENGL);
522     wined3d_mutex_unlock();
523
524     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
525     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
526     pPresentationParameters->BackBufferFormat                   = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
527     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
528     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
529     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
530     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
531     pPresentationParameters->Windowed                           = localParameters.Windowed;
532     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
533     pPresentationParameters->AutoDepthStencilFormat             = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
534     pPresentationParameters->Flags                              = localParameters.Flags;
535     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
536     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
537
538     if (hrc != D3D_OK) {
539         FIXME("(%p) call to IWineD3DDevice_CreateSwapChain failed\n", This);
540         HeapFree(GetProcessHeap(), 0 , object);
541         *pSwapChain = NULL;
542     }else{
543         IUnknown_AddRef(iface);
544         object->parentDevice = iface;
545         *pSwapChain = (IDirect3DSwapChain8 *)object;
546     }
547     TRACE("(%p) returning %p\n", This, *pSwapChain);
548     return hrc;
549 }
550
551 static HRESULT WINAPI IDirect3DDevice8Impl_Reset(LPDIRECT3DDEVICE8 iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
552     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
553     WINED3DPRESENT_PARAMETERS localParameters;
554     HRESULT hr;
555
556     TRACE("(%p) Relay pPresentationParameters(%p)\n", This, pPresentationParameters);
557
558     localParameters.BackBufferWidth                             = pPresentationParameters->BackBufferWidth;
559     localParameters.BackBufferHeight                            = pPresentationParameters->BackBufferHeight;
560     localParameters.BackBufferFormat                            = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
561     localParameters.BackBufferCount                             = pPresentationParameters->BackBufferCount;
562     localParameters.MultiSampleType                             = pPresentationParameters->MultiSampleType;
563     localParameters.MultiSampleQuality                          = 0; /* d3d9 only */
564     localParameters.SwapEffect                                  = pPresentationParameters->SwapEffect;
565     localParameters.hDeviceWindow                               = pPresentationParameters->hDeviceWindow;
566     localParameters.Windowed                                    = pPresentationParameters->Windowed;
567     localParameters.EnableAutoDepthStencil                      = pPresentationParameters->EnableAutoDepthStencil;
568     localParameters.AutoDepthStencilFormat                      = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
569     localParameters.Flags                                       = pPresentationParameters->Flags;
570     localParameters.FullScreen_RefreshRateInHz                  = pPresentationParameters->FullScreen_RefreshRateInHz;
571     localParameters.PresentationInterval                        = pPresentationParameters->FullScreen_PresentationInterval;
572     localParameters.AutoRestoreDisplayMode                      = TRUE;
573
574     wined3d_mutex_lock();
575     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
576     wined3d_mutex_unlock();
577
578     pPresentationParameters->BackBufferWidth                    = localParameters.BackBufferWidth;
579     pPresentationParameters->BackBufferHeight                   = localParameters.BackBufferHeight;
580     pPresentationParameters->BackBufferFormat                   = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
581     pPresentationParameters->BackBufferCount                    = localParameters.BackBufferCount;
582     pPresentationParameters->MultiSampleType                    = localParameters.MultiSampleType;
583     pPresentationParameters->SwapEffect                         = localParameters.SwapEffect;
584     pPresentationParameters->hDeviceWindow                      = localParameters.hDeviceWindow;
585     pPresentationParameters->Windowed                           = localParameters.Windowed;
586     pPresentationParameters->EnableAutoDepthStencil             = localParameters.EnableAutoDepthStencil;
587     pPresentationParameters->AutoDepthStencilFormat             = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
588     pPresentationParameters->Flags                              = localParameters.Flags;
589     pPresentationParameters->FullScreen_RefreshRateInHz         = localParameters.FullScreen_RefreshRateInHz;
590     pPresentationParameters->FullScreen_PresentationInterval    = localParameters.PresentationInterval;
591
592     return hr;
593 }
594
595 static HRESULT WINAPI IDirect3DDevice8Impl_Present(LPDIRECT3DDEVICE8 iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion) {
596     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
597     HRESULT hr;
598     TRACE("(%p) Relay\n", This);
599
600     wined3d_mutex_lock();
601     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
602     wined3d_mutex_unlock();
603
604     return hr;
605 }
606
607 static HRESULT WINAPI IDirect3DDevice8Impl_GetBackBuffer(LPDIRECT3DDEVICE8 iface, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8** ppBackBuffer) {
608     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
609     IWineD3DSurface *retSurface = NULL;
610     HRESULT rc = D3D_OK;
611
612     TRACE("(%p) Relay\n", This);
613
614     wined3d_mutex_lock();
615     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, 0, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &retSurface);
616     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
617         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
618         IWineD3DSurface_Release(retSurface);
619     }
620     wined3d_mutex_unlock();
621
622     return rc;
623 }
624
625 static HRESULT WINAPI IDirect3DDevice8Impl_GetRasterStatus(LPDIRECT3DDEVICE8 iface, D3DRASTER_STATUS* pRasterStatus) {
626     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
627     HRESULT hr;
628     TRACE("(%p) Relay\n", This);
629
630     wined3d_mutex_lock();
631     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, 0, (WINED3DRASTER_STATUS *) pRasterStatus);
632     wined3d_mutex_unlock();
633
634     return hr;
635 }
636
637 static void WINAPI IDirect3DDevice8Impl_SetGammaRamp(LPDIRECT3DDEVICE8 iface, DWORD Flags, CONST D3DGAMMARAMP* pRamp) {
638     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
639     TRACE("(%p) Relay\n", This);
640
641     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
642     wined3d_mutex_lock();
643     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, 0, Flags, (CONST WINED3DGAMMARAMP *) pRamp);
644     wined3d_mutex_unlock();
645 }
646
647 static void WINAPI IDirect3DDevice8Impl_GetGammaRamp(LPDIRECT3DDEVICE8 iface, D3DGAMMARAMP* pRamp) {
648     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
649     TRACE("(%p) Relay\n", This);
650
651     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
652     wined3d_mutex_lock();
653     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, 0, (WINED3DGAMMARAMP *) pRamp);
654     wined3d_mutex_unlock();
655 }
656
657 static HRESULT WINAPI IDirect3DDevice8Impl_CreateTexture(IDirect3DDevice8 *iface,
658         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
659         D3DPOOL pool, IDirect3DTexture8 **texture)
660 {
661     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
662     IDirect3DTexture8Impl *object;
663     HRESULT hr;
664
665     TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
666             iface, width, height, levels, usage, format, pool, texture);
667
668     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
669     if (!object)
670     {
671         ERR("Failed to allocate texture memory.\n");
672         return D3DERR_OUTOFVIDEOMEMORY;
673     }
674
675     hr = texture_init(object, This, width, height, levels, usage, format, pool);
676     if (FAILED(hr))
677     {
678         WARN("Failed to initialize texture, hr %#x.\n", hr);
679         HeapFree(GetProcessHeap(), 0, object);
680         return hr;
681     }
682
683     TRACE("Created texture %p.\n", object);
684     *texture = (IDirect3DTexture8 *)object;
685
686     return D3D_OK;
687 }
688
689 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVolumeTexture(IDirect3DDevice8 *iface,
690         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
691         D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
692 {
693     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
694     IDirect3DVolumeTexture8Impl *object;
695     HRESULT hr;
696
697     TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
698             iface, width, height, depth, levels, usage, format, pool, texture);
699
700     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
701     if (!object)
702     {
703         ERR("Failed to allocate volume texture memory.\n");
704         return D3DERR_OUTOFVIDEOMEMORY;
705     }
706
707     hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
708     if (FAILED(hr))
709     {
710         WARN("Failed to initialize volume texture, hr %#x.\n", hr);
711         HeapFree(GetProcessHeap(), 0, object);
712         return hr;
713     }
714
715     TRACE("Created volume texture %p.\n", object);
716     *texture = (IDirect3DVolumeTexture8 *)object;
717
718     return D3D_OK;
719 }
720
721 static HRESULT WINAPI IDirect3DDevice8Impl_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
722         UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
723 {
724     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
725     IDirect3DCubeTexture8Impl *object;
726     HRESULT hr;
727
728     TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
729             iface, edge_length, levels, usage, format, pool, texture);
730
731     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
732     if (!object)
733     {
734         ERR("Failed to allocate cube texture memory.\n");
735         return D3DERR_OUTOFVIDEOMEMORY;
736     }
737
738     hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
739     if (FAILED(hr))
740     {
741         WARN("Failed to initialize cube texture, hr %#x.\n", hr);
742         HeapFree(GetProcessHeap(), 0, object);
743         return hr;
744     }
745
746     TRACE("Created cube texture %p.\n", object);
747     *texture = (IDirect3DCubeTexture8 *)object;
748
749     return hr;
750 }
751
752 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size, DWORD usage,
753         DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
754 {
755     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
756     IDirect3DVertexBuffer8Impl *object;
757     HRESULT hr;
758
759     TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
760             iface, size, usage, fvf, pool, buffer);
761
762     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
763     if (!object)
764     {
765         ERR("Failed to allocate buffer memory.\n");
766         return D3DERR_OUTOFVIDEOMEMORY;
767     }
768
769     hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
770     if (FAILED(hr))
771     {
772         WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
773         HeapFree(GetProcessHeap(), 0, object);
774         return hr;
775     }
776
777     TRACE("Created vertex buffer %p.\n", object);
778     *buffer = (IDirect3DVertexBuffer8 *)object;
779
780     return D3D_OK;
781 }
782
783 static HRESULT WINAPI IDirect3DDevice8Impl_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size, DWORD usage,
784         D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
785 {
786     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
787     IDirect3DIndexBuffer8Impl *object;
788     HRESULT hr;
789
790     TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
791             iface, size, usage, format, pool, buffer);
792
793     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
794     if (!object)
795     {
796         ERR("Failed to allocate buffer memory.\n");
797         return D3DERR_OUTOFVIDEOMEMORY;
798     }
799
800     hr = indexbuffer_init(object, This, size, usage, format, pool);
801     if (FAILED(hr))
802     {
803         WARN("Failed to initialize index buffer, hr %#x.\n", hr);
804         HeapFree(GetProcessHeap(), 0, object);
805         return hr;
806     }
807
808     TRACE("Created index buffer %p.\n", object);
809     *buffer = (IDirect3DIndexBuffer8 *)object;
810
811     return D3D_OK;
812 }
813
814 static HRESULT IDirect3DDevice8Impl_CreateSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height,
815         D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level, IDirect3DSurface8 **ppSurface,
816         UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality)
817 {
818     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
819     IDirect3DSurface8Impl *object;
820     HRESULT hr;
821
822     TRACE("(%p) : w(%d) h(%d) fmt(%d) surf@%p\n", This, Width, Height, Format, *ppSurface);
823
824     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
825     if (!object)
826     {
827         FIXME("Failed to allocate surface memory.\n");
828         return D3DERR_OUTOFVIDEOMEMORY;
829     }
830
831     hr = surface_init(object, This, Width, Height, Format, Lockable, Discard,
832             Level, Usage, Pool, MultiSample, MultisampleQuality);
833     if (FAILED(hr))
834     {
835         WARN("Failed to initialize surface, hr %#x.\n", hr);
836         HeapFree(GetProcessHeap(), 0, object);
837         return hr;
838     }
839
840     TRACE("Created surface %p.\n", object);
841     *ppSurface = (IDirect3DSurface8 *)object;
842
843     return D3D_OK;
844 }
845
846 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable, IDirect3DSurface8** ppSurface) {
847     HRESULT hr;
848     TRACE("Relay\n");
849
850     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */,
851             0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, 0);
852
853     return hr;
854 }
855
856 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, IDirect3DSurface8** ppSurface) {
857     HRESULT hr;
858     TRACE("Relay\n");
859
860     /* TODO: Verify that Discard is false */
861     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE,
862             0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample, 0);
863
864     return hr;
865 }
866
867 /*  IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
868 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(LPDIRECT3DDEVICE8 iface, UINT Width, UINT Height, D3DFORMAT Format, IDirect3DSurface8** ppSurface) {
869     HRESULT hr;
870     TRACE("Relay\n");
871
872     hr = IDirect3DDevice8Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE /* Discard */,
873             0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE,
874             0 /* MultisampleQuality */);
875
876     return hr;
877 }
878
879 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8 *pSourceSurface, CONST RECT *pSourceRects, UINT cRects, IDirect3DSurface8 *pDestinationSurface, CONST POINT *pDestPoints) {
880     IDirect3DSurface8Impl *Source = (IDirect3DSurface8Impl *) pSourceSurface;
881     IDirect3DSurface8Impl *Dest = (IDirect3DSurface8Impl *) pDestinationSurface;
882
883     HRESULT              hr = WINED3D_OK;
884     WINED3DFORMAT        srcFormat, destFormat;
885     WINED3DSURFACE_DESC  winedesc;
886
887     TRACE("(%p) pSrcSur=%p, pSourceRects=%p, cRects=%d, pDstSur=%p, pDestPtsArr=%p\n", iface,
888           pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
889
890
891     /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the destination texture is in WINED3DPOOL_DEFAULT */
892
893     wined3d_mutex_lock();
894     IWineD3DSurface_GetDesc(Source->wineD3DSurface, &winedesc);
895     srcFormat = winedesc.format;
896
897     IWineD3DSurface_GetDesc(Dest->wineD3DSurface, &winedesc);
898     destFormat = winedesc.format;
899
900     /* Check that the source and destination formats match */
901     if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat) {
902         WARN("(%p) source %p format must match the dest %p format, returning WINED3DERR_INVALIDCALL\n", iface, pSourceSurface, pDestinationSurface);
903         wined3d_mutex_unlock();
904         return WINED3DERR_INVALIDCALL;
905     } else if (WINED3DFMT_UNKNOWN == destFormat) {
906         TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
907         IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
908     }
909
910     /* Quick if complete copy ... */
911     if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
912         IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
913     } else {
914         unsigned int i;
915         /* Copy rect by rect */
916         if (NULL != pSourceRects && NULL != pDestPoints) {
917             for (i = 0; i < cRects; ++i) {
918                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
919             }
920         } else {
921             for (i = 0; i < cRects; ++i) {
922                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
923             }
924         }
925     }
926     wined3d_mutex_unlock();
927
928     return hr;
929 }
930
931 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
932     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
933     HRESULT hr;
934     TRACE("(%p) Relay\n" , This);
935
936     wined3d_mutex_lock();
937     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
938     wined3d_mutex_unlock();
939
940     return hr;
941 }
942
943 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
944     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
945     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
946     HRESULT hr;
947
948     TRACE("(%p) Relay\n" , This);
949
950     if (pDestSurface == NULL) {
951         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
952         return D3DERR_INVALIDCALL;
953     }
954
955     wined3d_mutex_lock();
956     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
957     wined3d_mutex_unlock();
958
959     return hr;
960 }
961
962 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
963     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
964     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
965     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
966     IWineD3DSurface *original_ds = NULL;
967     HRESULT hr;
968     TRACE("(%p) Relay\n" , This);
969
970     wined3d_mutex_lock();
971
972     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &original_ds);
973     if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
974     {
975         hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, pZSurface ? pZSurface->wineD3DSurface : NULL);
976         if (SUCCEEDED(hr) && pSurface)
977             hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface->wineD3DSurface);
978         if (FAILED(hr)) IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, original_ds);
979     }
980     if (original_ds) IWineD3DSurface_Release(original_ds);
981
982     wined3d_mutex_unlock();
983
984     return hr;
985 }
986
987 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
988     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
989     HRESULT hr = D3D_OK;
990     IWineD3DSurface *pRenderTarget;
991
992     TRACE("(%p) Relay\n" , This);
993
994     if (ppRenderTarget == NULL) {
995         return D3DERR_INVALIDCALL;
996     }
997
998     wined3d_mutex_lock();
999     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
1000
1001     if (hr == D3D_OK && pRenderTarget != NULL) {
1002         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
1003         IWineD3DSurface_Release(pRenderTarget);
1004     } else {
1005         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
1006         *ppRenderTarget = NULL;
1007     }
1008     wined3d_mutex_unlock();
1009
1010     return hr;
1011 }
1012
1013 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
1014     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1015     HRESULT hr = D3D_OK;
1016     IWineD3DSurface *pZStencilSurface;
1017
1018     TRACE("(%p) Relay\n" , This);
1019     if(ppZStencilSurface == NULL){
1020         return D3DERR_INVALIDCALL;
1021     }
1022
1023     wined3d_mutex_lock();
1024     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
1025     if (hr == WINED3D_OK) {
1026         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
1027         IWineD3DSurface_Release(pZStencilSurface);
1028     }else{
1029         if (hr != WINED3DERR_NOTFOUND)
1030                 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1031         *ppZStencilSurface = NULL;
1032     }
1033     wined3d_mutex_unlock();
1034
1035     return hr;
1036 }
1037
1038 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
1039     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1040     HRESULT hr;
1041     TRACE("(%p) Relay\n" , This);
1042
1043     wined3d_mutex_lock();
1044     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1045     wined3d_mutex_unlock();
1046
1047     return hr;
1048 }
1049
1050 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
1051     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1052     HRESULT hr;
1053     TRACE("(%p) Relay\n" , This);
1054
1055     wined3d_mutex_lock();
1056     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1057     wined3d_mutex_unlock();
1058
1059     return hr;
1060 }
1061
1062 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
1063     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1064     HRESULT hr;
1065     TRACE("(%p) Relay\n" , This);
1066
1067     /* Note: D3DRECT is compatible with WINED3DRECT */
1068     wined3d_mutex_lock();
1069     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
1070     wined3d_mutex_unlock();
1071
1072     return hr;
1073 }
1074
1075 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
1076     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1077     HRESULT hr;
1078     TRACE("(%p) Relay\n" , This);
1079
1080     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1081     wined3d_mutex_lock();
1082     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1083     wined3d_mutex_unlock();
1084
1085     return hr;
1086 }
1087
1088 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
1089     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1090     HRESULT hr;
1091     TRACE("(%p) Relay\n" , This);
1092
1093     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1094     wined3d_mutex_lock();
1095     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1096     wined3d_mutex_unlock();
1097
1098     return hr;
1099 }
1100
1101 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
1102     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1103     HRESULT hr;
1104     TRACE("(%p) Relay\n" , This);
1105
1106     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1107     wined3d_mutex_lock();
1108     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1109     wined3d_mutex_unlock();
1110
1111     return hr;
1112 }
1113
1114 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
1115     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1116     HRESULT hr;
1117     TRACE("(%p) Relay\n" , This);
1118
1119     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1120     wined3d_mutex_lock();
1121     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1122     wined3d_mutex_unlock();
1123
1124     return hr;
1125 }
1126
1127 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
1128     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1129     HRESULT hr;
1130     TRACE("(%p) Relay\n" , This);
1131
1132     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1133     wined3d_mutex_lock();
1134     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1135     wined3d_mutex_unlock();
1136
1137     return hr;
1138 }
1139
1140 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
1141     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1142     HRESULT hr;
1143     TRACE("(%p) Relay\n" , This);
1144
1145     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1146     wined3d_mutex_lock();
1147     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1148     wined3d_mutex_unlock();
1149
1150     return hr;
1151 }
1152
1153 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
1154     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1155     HRESULT hr;
1156     TRACE("(%p) Relay\n" , This);
1157
1158     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1159     wined3d_mutex_lock();
1160     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1161     wined3d_mutex_unlock();
1162
1163     return hr;
1164 }
1165
1166 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
1167     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1168     HRESULT hr;
1169     TRACE("(%p) Relay\n" , This);
1170
1171     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1172     wined3d_mutex_lock();
1173     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1174     wined3d_mutex_unlock();
1175
1176     return hr;
1177 }
1178
1179 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
1180     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1181     HRESULT hr;
1182     TRACE("(%p) Relay\n" , This);
1183
1184     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1185     wined3d_mutex_lock();
1186     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1187     wined3d_mutex_unlock();
1188
1189     return hr;
1190 }
1191
1192 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
1193     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1194     HRESULT hr;
1195     TRACE("(%p) Relay\n" , This);
1196
1197     wined3d_mutex_lock();
1198     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1199     wined3d_mutex_unlock();
1200
1201     return hr;
1202 }
1203
1204 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
1205     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1206     HRESULT hr;
1207     TRACE("(%p) Relay\n" , This);
1208
1209     wined3d_mutex_lock();
1210     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1211     wined3d_mutex_unlock();
1212
1213     return hr;
1214 }
1215
1216 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
1217     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1218     HRESULT hr;
1219     TRACE("(%p) Relay\n" , This);
1220
1221     wined3d_mutex_lock();
1222     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1223     wined3d_mutex_unlock();
1224
1225     return hr;
1226 }
1227
1228 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
1229     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1230     HRESULT hr;
1231     TRACE("(%p) Relay\n" , This);
1232
1233     wined3d_mutex_lock();
1234     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1235     wined3d_mutex_unlock();
1236
1237     return hr;
1238 }
1239
1240 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
1241     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1242     HRESULT hr;
1243     TRACE("(%p) Relay\n" , This);
1244
1245     wined3d_mutex_lock();
1246     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1247     wined3d_mutex_unlock();
1248
1249     return hr;
1250 }
1251
1252 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
1253     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1254     HRESULT hr;
1255     TRACE("(%p) Relay\n" , This);
1256
1257     wined3d_mutex_lock();
1258     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1259     wined3d_mutex_unlock();
1260
1261     return hr;
1262 }
1263
1264 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
1265     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1266     HRESULT hr;
1267     TRACE("(%p)\n", This);
1268
1269     wined3d_mutex_lock();
1270     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1271     wined3d_mutex_unlock();
1272
1273     return hr;
1274 }
1275
1276 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
1277     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1278     HRESULT hr;
1279     IWineD3DStateBlock* wineD3DStateBlock;
1280     IDirect3DStateBlock8Impl* object;
1281
1282     TRACE("(%p) Relay\n", This);
1283
1284     /* Tell wineD3D to endstateblock before anything else (in case we run out
1285      * of memory later and cause locking problems)
1286      */
1287     wined3d_mutex_lock();
1288     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
1289     if (hr != D3D_OK) {
1290         WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1291         wined3d_mutex_unlock();
1292         return hr;
1293     }
1294
1295     /* allocate a new IDirectD3DStateBlock */
1296     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
1297     object->ref = 1;
1298     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1299
1300     object->wineD3DStateBlock = wineD3DStateBlock;
1301
1302     *pToken = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_SB);
1303     wined3d_mutex_unlock();
1304
1305     if (*pToken == D3D8_INVALID_HANDLE)
1306     {
1307         ERR("Failed to create a handle\n");
1308         IDirect3DStateBlock8_Release((IDirect3DStateBlock8 *)object);
1309         return E_FAIL;
1310     }
1311     ++*pToken;
1312
1313     TRACE("Returning %#x (%p).\n", *pToken, object);
1314
1315     return hr;
1316 }
1317
1318 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1319     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
1320     IDirect3DStateBlock8Impl *pSB;
1321     HRESULT hr;
1322
1323     TRACE("(%p) %#x Relay\n", This, Token);
1324
1325     wined3d_mutex_lock();
1326     pSB = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1327     if (!pSB)
1328     {
1329         WARN("Invalid handle (%#x) passed.\n", Token);
1330         wined3d_mutex_unlock();
1331         return D3DERR_INVALIDCALL;
1332     }
1333     hr = IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
1334     wined3d_mutex_unlock();
1335
1336     return hr;
1337 }
1338
1339 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1340     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1341     IDirect3DStateBlock8Impl *pSB;
1342     HRESULT hr;
1343
1344     TRACE("(%p) %#x Relay\n", This, Token);
1345
1346     wined3d_mutex_lock();
1347     pSB = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1348     if (!pSB)
1349     {
1350         WARN("Invalid handle (%#x) passed.\n", Token);
1351         wined3d_mutex_unlock();
1352         return D3DERR_INVALIDCALL;
1353     }
1354     hr = IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
1355     wined3d_mutex_unlock();
1356
1357     return hr;
1358 }
1359
1360 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1361     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1362     IDirect3DStateBlock8Impl *pSB;
1363
1364     TRACE("(%p) Relay\n", This);
1365
1366     wined3d_mutex_lock();
1367     pSB = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1368     wined3d_mutex_unlock();
1369
1370     if (!pSB)
1371     {
1372         WARN("Invalid handle (%#x) passed.\n", Token);
1373         return D3DERR_INVALIDCALL;
1374     }
1375
1376     if (IUnknown_Release((IUnknown *)pSB))
1377     {
1378         ERR("Stateblock %p has references left, this shouldn't happen.\n", pSB);
1379     }
1380
1381     return D3D_OK;
1382 }
1383
1384 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1385         D3DSTATEBLOCKTYPE Type, DWORD *handle)
1386 {
1387     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1388     IDirect3DStateBlock8Impl *object;
1389     HRESULT hr;
1390
1391     TRACE("(%p) Relay\n", This);
1392
1393     if (Type != D3DSBT_ALL
1394             && Type != D3DSBT_PIXELSTATE
1395             && Type != D3DSBT_VERTEXSTATE)
1396     {
1397         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1398         return D3DERR_INVALIDCALL;
1399     }
1400
1401     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
1402     if (!object)
1403     {
1404         ERR("Failed to allocate memory.\n");
1405         return E_OUTOFMEMORY;
1406     }
1407
1408     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1409     object->ref = 1;
1410
1411     wined3d_mutex_lock();
1412     hr = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type,
1413             &object->wineD3DStateBlock, (IUnknown *)object);
1414     if (FAILED(hr))
1415     {
1416         wined3d_mutex_unlock();
1417         ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1418         HeapFree(GetProcessHeap(), 0, object);
1419         return hr;
1420     }
1421
1422     *handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_SB);
1423     wined3d_mutex_unlock();
1424
1425     if (*handle == D3D8_INVALID_HANDLE)
1426     {
1427         ERR("Failed to allocate a handle.\n");
1428         IDirect3DStateBlock8_Release((IDirect3DStateBlock8 *)object);
1429         return E_FAIL;
1430     }
1431     ++*handle;
1432
1433     TRACE("Returning %#x (%p).\n", *handle, object);
1434
1435     return hr;
1436 }
1437
1438 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
1439     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1440     HRESULT hr;
1441     TRACE("(%p) Relay\n" , This);
1442 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1443
1444     wined3d_mutex_lock();
1445     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1446     wined3d_mutex_unlock();
1447
1448     return hr;
1449 }
1450
1451 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
1452     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1453     HRESULT hr;
1454     TRACE("(%p) Relay\n" , This);
1455
1456     wined3d_mutex_lock();
1457     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1458     wined3d_mutex_unlock();
1459
1460     return hr;
1461 }
1462
1463 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
1464     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1465     IWineD3DBaseTexture *retTexture;
1466     HRESULT hr;
1467
1468     TRACE("(%p) Relay\n" , This);
1469
1470     if(ppTexture == NULL){
1471         return D3DERR_INVALIDCALL;
1472     }
1473
1474     wined3d_mutex_lock();
1475     hr = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1476     if (FAILED(hr))
1477     {
1478         WARN("Failed to get texture for stage %u, hr %#x.\n", Stage, hr);
1479         wined3d_mutex_unlock();
1480         *ppTexture = NULL;
1481         return hr;
1482     }
1483
1484     if (retTexture)
1485     {
1486         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1487         IWineD3DBaseTexture_Release(retTexture);
1488     }
1489     else
1490     {
1491         *ppTexture = NULL;
1492     }
1493     wined3d_mutex_unlock();
1494
1495     return D3D_OK;
1496 }
1497
1498 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1499     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1500     HRESULT hr;
1501     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1502
1503     wined3d_mutex_lock();
1504     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1505                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1506     wined3d_mutex_unlock();
1507
1508     return hr;
1509 }
1510
1511 static const struct tss_lookup
1512 {
1513     BOOL sampler_state;
1514     DWORD state;
1515 }
1516 tss_lookup[] =
1517 {
1518     {FALSE, WINED3DTSS_FORCE_DWORD},            /*  0, unused */
1519     {FALSE, WINED3DTSS_COLOROP},                /*  1, D3DTSS_COLOROP */
1520     {FALSE, WINED3DTSS_COLORARG1},              /*  2, D3DTSS_COLORARG1 */
1521     {FALSE, WINED3DTSS_COLORARG2},              /*  3, D3DTSS_COLORARG2 */
1522     {FALSE, WINED3DTSS_ALPHAOP},                /*  4, D3DTSS_ALPHAOP */
1523     {FALSE, WINED3DTSS_ALPHAARG1},              /*  5, D3DTSS_ALPHAARG1 */
1524     {FALSE, WINED3DTSS_ALPHAARG2},              /*  6, D3DTSS_ALPHAARG2 */
1525     {FALSE, WINED3DTSS_BUMPENVMAT00},           /*  7, D3DTSS_BUMPENVMAT00 */
1526     {FALSE, WINED3DTSS_BUMPENVMAT01},           /*  8, D3DTSS_BUMPENVMAT01 */
1527     {FALSE, WINED3DTSS_BUMPENVMAT10},           /*  9, D3DTSS_BUMPENVMAT10 */
1528     {FALSE, WINED3DTSS_BUMPENVMAT11},           /* 10, D3DTSS_BUMPENVMAT11 */
1529     {FALSE, WINED3DTSS_TEXCOORDINDEX},          /* 11, D3DTSS_TEXCOORDINDEX */
1530     {FALSE, WINED3DTSS_FORCE_DWORD},            /* 12, unused */
1531     {TRUE,  WINED3DSAMP_ADDRESSU},              /* 13, D3DTSS_ADDRESSU */
1532     {TRUE,  WINED3DSAMP_ADDRESSV},              /* 14, D3DTSS_ADDRESSV */
1533     {TRUE,  WINED3DSAMP_BORDERCOLOR},           /* 15, D3DTSS_BORDERCOLOR */
1534     {TRUE,  WINED3DSAMP_MAGFILTER},             /* 16, D3DTSS_MAGFILTER */
1535     {TRUE,  WINED3DSAMP_MINFILTER},             /* 17, D3DTSS_MINFILTER */
1536     {TRUE,  WINED3DSAMP_MIPFILTER},             /* 18, D3DTSS_MIPFILTER */
1537     {TRUE,  WINED3DSAMP_MIPMAPLODBIAS},         /* 19, D3DTSS_MIPMAPLODBIAS */
1538     {TRUE,  WINED3DSAMP_MAXMIPLEVEL},           /* 20, D3DTSS_MAXMIPLEVEL */
1539     {TRUE,  WINED3DSAMP_MAXANISOTROPY},         /* 21, D3DTSS_MAXANISOTROPY */
1540     {FALSE, WINED3DTSS_BUMPENVLSCALE},          /* 22, D3DTSS_BUMPENVLSCALE */
1541     {FALSE, WINED3DTSS_BUMPENVLOFFSET},         /* 23, D3DTSS_BUMPENVLOFFSET */
1542     {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS},  /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1543     {TRUE,  WINED3DSAMP_ADDRESSW},              /* 25, D3DTSS_ADDRESSW */
1544     {FALSE, WINED3DTSS_COLORARG0},              /* 26, D3DTSS_COLORARG0 */
1545     {FALSE, WINED3DTSS_ALPHAARG0},              /* 27, D3DTSS_ALPHAARG0 */
1546     {FALSE, WINED3DTSS_RESULTARG},              /* 28, D3DTSS_RESULTARG */
1547 };
1548
1549 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1550     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1551     const struct tss_lookup *l = &tss_lookup[Type];
1552     HRESULT hr;
1553     TRACE("(%p) Relay\n" , This);
1554
1555     wined3d_mutex_lock();
1556     if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1557     else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1558     wined3d_mutex_unlock();
1559
1560     return hr;
1561 }
1562
1563 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1564     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1565     const struct tss_lookup *l = &tss_lookup[Type];
1566     HRESULT hr;
1567     TRACE("(%p) Relay\n" , This);
1568
1569     wined3d_mutex_lock();
1570     if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1571     else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1572     wined3d_mutex_unlock();
1573
1574     return hr;
1575 }
1576
1577 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1578     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1579     HRESULT hr;
1580     TRACE("(%p) Relay\n" , This);
1581
1582     wined3d_mutex_lock();
1583     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1584     wined3d_mutex_unlock();
1585
1586     return hr;
1587 }
1588
1589 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1590     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1591     FIXME("(%p) : stub\n", This);
1592     return D3D_OK;
1593 }
1594
1595 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1596     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1597     HRESULT hr;
1598     TRACE("(%p) Relay\n" , This);
1599
1600     wined3d_mutex_lock();
1601     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1602     wined3d_mutex_unlock();
1603
1604     return hr;
1605 }
1606
1607 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1608     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1609     HRESULT hr;
1610     TRACE("(%p) Relay\n" , This);
1611
1612     wined3d_mutex_lock();
1613     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1614     wined3d_mutex_unlock();
1615
1616     return hr;
1617 }
1618
1619 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1620     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1621     HRESULT hr;
1622     TRACE("(%p) Relay\n" , This);
1623
1624     wined3d_mutex_lock();
1625     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1626     wined3d_mutex_unlock();
1627
1628     return hr;
1629 }
1630
1631 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1632     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1633     HRESULT hr;
1634     TRACE("(%p) Relay\n" , This);
1635
1636     wined3d_mutex_lock();
1637     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1638     wined3d_mutex_unlock();
1639
1640     return hr;
1641 }
1642
1643 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface, D3DPRIMITIVETYPE PrimitiveType,
1644         UINT StartVertex, UINT PrimitiveCount)
1645 {
1646     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1647     HRESULT hr;
1648     TRACE("(%p) Relay\n" , This);
1649
1650     wined3d_mutex_lock();
1651     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1652     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1653             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1654     wined3d_mutex_unlock();
1655
1656     return hr;
1657 }
1658
1659 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1660                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1661     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1662     HRESULT hr;
1663     TRACE("(%p) Relay\n" , This);
1664
1665     wined3d_mutex_lock();
1666     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1667     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1668             vertex_count_from_primitive_count(PrimitiveType, primCount));
1669     wined3d_mutex_unlock();
1670
1671     return hr;
1672 }
1673
1674 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1675     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1676     HRESULT hr;
1677     TRACE("(%p) Relay\n" , This);
1678
1679     wined3d_mutex_lock();
1680     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1681     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1682             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1683             pVertexStreamZeroData, VertexStreamZeroStride);
1684     wined3d_mutex_unlock();
1685
1686     return hr;
1687 }
1688
1689 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1690                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1691                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1692                                                              UINT VertexStreamZeroStride) {
1693     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1694     HRESULT hr;
1695     TRACE("(%p) Relay\n" , This);
1696
1697     wined3d_mutex_lock();
1698     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1699     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1700             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1701             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1702     wined3d_mutex_unlock();
1703
1704     return hr;
1705 }
1706
1707 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1708     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1709     HRESULT hr;
1710     IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
1711     TRACE("(%p) Relay\n" , This);
1712
1713     wined3d_mutex_lock();
1714     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1715     wined3d_mutex_unlock();
1716
1717     return hr;
1718 }
1719
1720 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
1721         const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
1722 {
1723     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1724     IDirect3DVertexShader8Impl *object;
1725     DWORD shader_handle;
1726     DWORD handle;
1727     HRESULT hr;
1728
1729     TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
1730             iface, declaration, byte_code, shader, usage);
1731
1732     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1733     if (!object)
1734     {
1735         ERR("Failed to allocate vertex shader memory.\n");
1736         *shader = 0;
1737         return E_OUTOFMEMORY;
1738     }
1739
1740     wined3d_mutex_lock();
1741     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
1742     wined3d_mutex_unlock();
1743     if (handle == D3D8_INVALID_HANDLE)
1744     {
1745         ERR("Failed to allocate vertex shader handle.\n");
1746         HeapFree(GetProcessHeap(), 0, object);
1747         *shader = 0;
1748         return E_OUTOFMEMORY;
1749     }
1750
1751     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
1752
1753     hr = vertexshader_init(object, This, declaration, byte_code, shader_handle, usage);
1754     if (FAILED(hr))
1755     {
1756         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
1757         wined3d_mutex_lock();
1758         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
1759         wined3d_mutex_unlock();
1760         HeapFree(GetProcessHeap(), 0, object);
1761         *shader = 0;
1762         return hr;
1763     }
1764
1765     TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
1766     *shader = shader_handle;
1767
1768     return D3D_OK;
1769 }
1770
1771 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1772 {
1773     IDirect3DVertexDeclaration8Impl *d3d8_declaration;
1774     HRESULT hr;
1775     int p, low, high; /* deliberately signed */
1776     struct FvfToDecl *convertedDecls = This->decls;
1777
1778     TRACE("Searching for declaration for fvf %08x... ", fvf);
1779
1780     low = 0;
1781     high = This->numConvertedDecls - 1;
1782     while(low <= high) {
1783         p = (low + high) >> 1;
1784         TRACE("%d ", p);
1785         if(convertedDecls[p].fvf == fvf) {
1786             TRACE("found %p\n", convertedDecls[p].decl);
1787             return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
1788         } else if(convertedDecls[p].fvf < fvf) {
1789             low = p + 1;
1790         } else {
1791             high = p - 1;
1792         }
1793     }
1794     TRACE("not found. Creating and inserting at position %d.\n", low);
1795
1796     d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
1797     if (!d3d8_declaration)
1798     {
1799         ERR("Memory allocation failed.\n");
1800         return NULL;
1801     }
1802
1803     hr = vertexdeclaration_init_fvf(d3d8_declaration, This, fvf);
1804     if (FAILED(hr))
1805     {
1806         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
1807         HeapFree(GetProcessHeap(), 0, d3d8_declaration);
1808         return NULL;
1809     }
1810
1811     if(This->declArraySize == This->numConvertedDecls) {
1812         int grow = This->declArraySize / 2;
1813         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1814                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1815         if(!convertedDecls) {
1816             /* This will destroy it */
1817             IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
1818             return NULL;
1819         }
1820         This->decls = convertedDecls;
1821         This->declArraySize += grow;
1822     }
1823
1824     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
1825     convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
1826     convertedDecls[low].fvf = fvf;
1827     This->numConvertedDecls++;
1828
1829     TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
1830     return d3d8_declaration;
1831 }
1832
1833 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1834     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1835     IDirect3DVertexShader8Impl *shader;
1836     HRESULT hr;
1837
1838     TRACE("(%p) : Relay\n", This);
1839
1840     if (VS_HIGHESTFIXEDFXF >= pShader) {
1841         TRACE("Setting FVF, %#x\n", pShader);
1842
1843         wined3d_mutex_lock();
1844         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1845                 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
1846         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1847         wined3d_mutex_unlock();
1848
1849         return D3D_OK;
1850     }
1851
1852     TRACE("Setting shader\n");
1853
1854     wined3d_mutex_lock();
1855     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1856     if (!shader)
1857     {
1858         WARN("Invalid handle (%#x) passed.\n", pShader);
1859         wined3d_mutex_unlock();
1860
1861         return D3DERR_INVALIDCALL;
1862     }
1863
1864     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1865             ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
1866     if (SUCCEEDED(hr)) hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wineD3DVertexShader);
1867     wined3d_mutex_unlock();
1868
1869     TRACE("Returning hr %#x\n", hr);
1870
1871     return hr;
1872 }
1873
1874 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1875     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1876     IWineD3DVertexDeclaration *wined3d_declaration;
1877     IDirect3DVertexDeclaration8 *d3d8_declaration;
1878     HRESULT hrc;
1879
1880     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1881
1882     wined3d_mutex_lock();
1883     hrc = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
1884     if (FAILED(hrc))
1885     {
1886         wined3d_mutex_unlock();
1887         WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
1888                 This, hrc, This->WineD3DDevice);
1889         return hrc;
1890     }
1891
1892     if (!wined3d_declaration)
1893     {
1894         wined3d_mutex_unlock();
1895         *ppShader = 0;
1896         return D3D_OK;
1897     }
1898
1899     hrc = IWineD3DVertexDeclaration_GetParent(wined3d_declaration, (IUnknown **)&d3d8_declaration);
1900     IWineD3DVertexDeclaration_Release(wined3d_declaration);
1901     wined3d_mutex_unlock();
1902     if (SUCCEEDED(hrc))
1903     {
1904         *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
1905         IDirect3DVertexDeclaration8_Release(d3d8_declaration);
1906     }
1907
1908     TRACE("(%p) : returning %#x\n", This, *ppShader);
1909
1910     return hrc;
1911 }
1912
1913 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1914     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1915     IDirect3DVertexShader8Impl *shader;
1916     IWineD3DVertexShader *cur = NULL;
1917
1918     TRACE("(%p) : pShader %#x\n", This, pShader);
1919
1920     wined3d_mutex_lock();
1921     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1922     if (!shader)
1923     {
1924         WARN("Invalid handle (%#x) passed.\n", pShader);
1925         wined3d_mutex_unlock();
1926
1927         return D3DERR_INVALIDCALL;
1928     }
1929
1930     IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
1931
1932     if (cur)
1933     {
1934         if (cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
1935         IWineD3DVertexShader_Release(cur);
1936     }
1937
1938     wined3d_mutex_unlock();
1939
1940     if (IUnknown_Release((IUnknown *)shader))
1941     {
1942         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
1943     }
1944
1945     return D3D_OK;
1946 }
1947
1948 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1949     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1950     HRESULT hr;
1951     TRACE("(%p) : Relay\n", This);
1952
1953     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
1954         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
1955              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
1956         return D3DERR_INVALIDCALL;
1957     }
1958
1959     wined3d_mutex_lock();
1960     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
1961     wined3d_mutex_unlock();
1962
1963     return hr;
1964 }
1965
1966 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1967     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1968     HRESULT hr;
1969     TRACE("(%p) : Relay\n", This);
1970
1971     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
1972         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
1973              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
1974         return D3DERR_INVALIDCALL;
1975     }
1976
1977     wined3d_mutex_lock();
1978     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
1979     wined3d_mutex_unlock();
1980
1981     return hr;
1982 }
1983
1984 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1985     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1986     IDirect3DVertexDeclaration8Impl *declaration;
1987     IDirect3DVertexShader8Impl *shader;
1988
1989     TRACE("(%p) : pVertexShader 0x%08x, pData %p, *pSizeOfData %u\n", This, pVertexShader, pData, *pSizeOfData);
1990
1991     wined3d_mutex_lock();
1992     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1993     wined3d_mutex_unlock();
1994
1995     if (!shader)
1996     {
1997         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
1998         return D3DERR_INVALIDCALL;
1999     }
2000     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2001
2002     /* If pData is NULL, we just return the required size of the buffer. */
2003     if (!pData) {
2004         *pSizeOfData = declaration->elements_size;
2005         return D3D_OK;
2006     }
2007
2008     /* MSDN claims that if *pSizeOfData is smaller than the required size
2009      * we should write the required size and return D3DERR_MOREDATA.
2010      * That's not actually true. */
2011     if (*pSizeOfData < declaration->elements_size) {
2012         return D3DERR_INVALIDCALL;
2013     }
2014
2015     CopyMemory(pData, declaration->elements, declaration->elements_size);
2016
2017     return D3D_OK;
2018 }
2019
2020 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
2021     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2022     IDirect3DVertexShader8Impl *shader = NULL;
2023     HRESULT hr;
2024
2025     TRACE("(%p) : pVertexShader %#x, pData %p, pSizeOfData %p\n", This, pVertexShader, pData, pSizeOfData);
2026
2027     wined3d_mutex_lock();
2028     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2029     if (!shader)
2030     {
2031         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2032         wined3d_mutex_unlock();
2033
2034         return D3DERR_INVALIDCALL;
2035     }
2036
2037     if (!shader->wineD3DVertexShader)
2038     {
2039         wined3d_mutex_unlock();
2040         *pSizeOfData = 0;
2041         return D3D_OK;
2042     }
2043
2044     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, pSizeOfData);
2045     wined3d_mutex_unlock();
2046
2047     return hr;
2048 }
2049
2050 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
2051     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2052     HRESULT hr;
2053     IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2054     TRACE("(%p) Relay\n", This);
2055
2056     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2057      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2058      * vertex buffers can't be created to address them with an index that requires the 32nd bit
2059      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2060      * problem)
2061      */
2062     wined3d_mutex_lock();
2063     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2064     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2065             ib ? ib->wineD3DIndexBuffer : NULL,
2066             ib ? ib->format : WINED3DFMT_UNKNOWN);
2067     wined3d_mutex_unlock();
2068
2069     return hr;
2070 }
2071
2072 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
2073     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2074     IWineD3DBuffer *retIndexData = NULL;
2075     HRESULT rc = D3D_OK;
2076
2077     TRACE("(%p) Relay\n", This);
2078
2079     if(ppIndexData == NULL){
2080         return D3DERR_INVALIDCALL;
2081     }
2082
2083     /* The case from UINT to INT is safe because d3d8 will never set negative values */
2084     wined3d_mutex_lock();
2085     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2086     rc = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2087     if (SUCCEEDED(rc) && retIndexData) {
2088         IWineD3DBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
2089         IWineD3DBuffer_Release(retIndexData);
2090     } else {
2091         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
2092         *ppIndexData = NULL;
2093     }
2094     wined3d_mutex_unlock();
2095
2096     return rc;
2097 }
2098
2099 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(IDirect3DDevice8 *iface,
2100         const DWORD *byte_code, DWORD *shader)
2101 {
2102     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2103     IDirect3DPixelShader8Impl *object;
2104     DWORD shader_handle;
2105     DWORD handle;
2106     HRESULT hr;
2107
2108     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2109
2110     if (!shader)
2111     {
2112         TRACE("(%p) Invalid call\n", This);
2113         return D3DERR_INVALIDCALL;
2114     }
2115
2116     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2117     if (!object)
2118     {
2119         ERR("Failed to allocate pixel shader memmory.\n");
2120         return E_OUTOFMEMORY;
2121     }
2122
2123     wined3d_mutex_lock();
2124     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2125     wined3d_mutex_unlock();
2126     if (handle == D3D8_INVALID_HANDLE)
2127     {
2128         ERR("Failed to allocate pixel shader handle.\n");
2129         HeapFree(GetProcessHeap(), 0, object);
2130         return E_OUTOFMEMORY;
2131     }
2132
2133     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2134
2135     hr = pixelshader_init(object, This, byte_code, shader_handle);
2136     if (FAILED(hr))
2137     {
2138         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2139         wined3d_mutex_lock();
2140         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_PS);
2141         wined3d_mutex_unlock();
2142         HeapFree(GetProcessHeap(), 0, object);
2143         *shader = 0;
2144         return hr;
2145     }
2146
2147     TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2148     *shader = shader_handle;
2149
2150     return D3D_OK;
2151 }
2152
2153 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2154     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2155     IDirect3DPixelShader8Impl *shader;
2156     HRESULT hr;
2157
2158     TRACE("(%p) : pShader %#x\n", This, pShader);
2159
2160     wined3d_mutex_lock();
2161
2162     if (!pShader)
2163     {
2164         hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2165         wined3d_mutex_unlock();
2166         return hr;
2167     }
2168
2169     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2170     if (!shader)
2171     {
2172         WARN("Invalid handle (%#x) passed.\n", pShader);
2173         wined3d_mutex_unlock();
2174         return D3DERR_INVALIDCALL;
2175     }
2176
2177     TRACE("(%p) : Setting shader %p\n", This, shader);
2178     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wineD3DPixelShader);
2179     wined3d_mutex_unlock();
2180
2181     return hr;
2182 }
2183
2184 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
2185     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2186     IWineD3DPixelShader *object;
2187
2188     HRESULT hrc = D3D_OK;
2189     TRACE("(%p) Relay\n", This);
2190     if (NULL == ppShader) {
2191         TRACE("(%p) Invalid call\n", This);
2192         return D3DERR_INVALIDCALL;
2193     }
2194
2195     wined3d_mutex_lock();
2196     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
2197     if (D3D_OK == hrc && NULL != object) {
2198         IDirect3DPixelShader8Impl *d3d8_shader;
2199         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
2200         IWineD3DPixelShader_Release(object);
2201         *ppShader = d3d8_shader->handle;
2202         IDirect3DPixelShader8_Release((IDirect3DPixelShader8 *)d3d8_shader);
2203     } else {
2204         *ppShader = 0;
2205     }
2206     wined3d_mutex_unlock();
2207
2208     TRACE("(%p) : returning %#x\n", This, *ppShader);
2209
2210     return hrc;
2211 }
2212
2213 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2214     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2215     IDirect3DPixelShader8Impl *shader;
2216     IWineD3DPixelShader *cur = NULL;
2217
2218     TRACE("(%p) : pShader %#x\n", This, pShader);
2219
2220     wined3d_mutex_lock();
2221
2222     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2223     if (!shader)
2224     {
2225         WARN("Invalid handle (%#x) passed.\n", pShader);
2226         wined3d_mutex_unlock();
2227         return D3D_OK;
2228     }
2229
2230     IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
2231
2232     if (cur)
2233     {
2234         if (cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
2235         IWineD3DPixelShader_Release(cur);
2236     }
2237
2238     wined3d_mutex_unlock();
2239
2240     if (IUnknown_Release((IUnknown *)shader))
2241     {
2242         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2243     }
2244
2245     return D3D_OK;
2246 }
2247
2248 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
2249     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2250     HRESULT hr;
2251     TRACE("(%p) Relay\n", This);
2252
2253     wined3d_mutex_lock();
2254     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2255     wined3d_mutex_unlock();
2256
2257     return hr;
2258 }
2259
2260 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
2261     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2262     HRESULT hr;
2263     TRACE("(%p) Relay\n", This);
2264
2265     wined3d_mutex_lock();
2266     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2267     wined3d_mutex_unlock();
2268
2269     return hr;
2270 }
2271
2272 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
2273     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2274     IDirect3DPixelShader8Impl *shader = NULL;
2275     HRESULT hr;
2276
2277     TRACE("(%p) : pPixelShader %#x, pData %p, pSizeOfData %p\n", This, pPixelShader, pData, pSizeOfData);
2278
2279     wined3d_mutex_lock();
2280     shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2281     if (!shader)
2282     {
2283         WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2284         wined3d_mutex_unlock();
2285
2286         return D3DERR_INVALIDCALL;
2287     }
2288
2289     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, pSizeOfData);
2290     wined3d_mutex_unlock();
2291
2292     return hr;
2293 }
2294
2295 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2296     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2297     HRESULT hr;
2298     TRACE("(%p) Relay\n", This);
2299
2300     wined3d_mutex_lock();
2301     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2302     wined3d_mutex_unlock();
2303
2304     return hr;
2305 }
2306
2307 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2308     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2309     HRESULT hr;
2310     TRACE("(%p) Relay\n", This);
2311
2312     wined3d_mutex_lock();
2313     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2314     wined3d_mutex_unlock();
2315
2316     return hr;
2317 }
2318
2319 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
2320     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2321     HRESULT hr;
2322     TRACE("(%p) Relay\n", This);
2323
2324     wined3d_mutex_lock();
2325     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2326     wined3d_mutex_unlock();
2327
2328     return hr;
2329 }
2330
2331 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
2332     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2333     HRESULT hr;
2334     TRACE("(%p) Relay\n" , This);
2335
2336     wined3d_mutex_lock();
2337     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2338                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2339                                         0/* Offset in bytes */, Stride);
2340     wined3d_mutex_unlock();
2341
2342     return hr;
2343 }
2344
2345 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
2346     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2347     IWineD3DBuffer *retStream = NULL;
2348     HRESULT rc = D3D_OK;
2349
2350     TRACE("(%p) Relay\n" , This);
2351
2352     if(pStream == NULL){
2353         return D3DERR_INVALIDCALL;
2354     }
2355
2356     wined3d_mutex_lock();
2357     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, 0 /* Offset in bytes */, pStride);
2358     if (rc == D3D_OK  && NULL != retStream) {
2359         IWineD3DBuffer_GetParent(retStream, (IUnknown **)pStream);
2360         IWineD3DBuffer_Release(retStream);
2361     }else{
2362         if (rc != D3D_OK){
2363             FIXME("Call to GetStreamSource failed %p\n",  pStride);
2364         }
2365         *pStream = NULL;
2366     }
2367     wined3d_mutex_unlock();
2368
2369     return rc;
2370 }
2371
2372
2373 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2374 {
2375     IDirect3DDevice8Impl_QueryInterface,
2376     IDirect3DDevice8Impl_AddRef,
2377     IDirect3DDevice8Impl_Release,
2378     IDirect3DDevice8Impl_TestCooperativeLevel,
2379     IDirect3DDevice8Impl_GetAvailableTextureMem,
2380     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2381     IDirect3DDevice8Impl_GetDirect3D,
2382     IDirect3DDevice8Impl_GetDeviceCaps,
2383     IDirect3DDevice8Impl_GetDisplayMode,
2384     IDirect3DDevice8Impl_GetCreationParameters,
2385     IDirect3DDevice8Impl_SetCursorProperties,
2386     IDirect3DDevice8Impl_SetCursorPosition,
2387     IDirect3DDevice8Impl_ShowCursor,
2388     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2389     IDirect3DDevice8Impl_Reset,
2390     IDirect3DDevice8Impl_Present,
2391     IDirect3DDevice8Impl_GetBackBuffer,
2392     IDirect3DDevice8Impl_GetRasterStatus,
2393     IDirect3DDevice8Impl_SetGammaRamp,
2394     IDirect3DDevice8Impl_GetGammaRamp,
2395     IDirect3DDevice8Impl_CreateTexture,
2396     IDirect3DDevice8Impl_CreateVolumeTexture,
2397     IDirect3DDevice8Impl_CreateCubeTexture,
2398     IDirect3DDevice8Impl_CreateVertexBuffer,
2399     IDirect3DDevice8Impl_CreateIndexBuffer,
2400     IDirect3DDevice8Impl_CreateRenderTarget,
2401     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2402     IDirect3DDevice8Impl_CreateImageSurface,
2403     IDirect3DDevice8Impl_CopyRects,
2404     IDirect3DDevice8Impl_UpdateTexture,
2405     IDirect3DDevice8Impl_GetFrontBuffer,
2406     IDirect3DDevice8Impl_SetRenderTarget,
2407     IDirect3DDevice8Impl_GetRenderTarget,
2408     IDirect3DDevice8Impl_GetDepthStencilSurface,
2409     IDirect3DDevice8Impl_BeginScene,
2410     IDirect3DDevice8Impl_EndScene,
2411     IDirect3DDevice8Impl_Clear,
2412     IDirect3DDevice8Impl_SetTransform,
2413     IDirect3DDevice8Impl_GetTransform,
2414     IDirect3DDevice8Impl_MultiplyTransform,
2415     IDirect3DDevice8Impl_SetViewport,
2416     IDirect3DDevice8Impl_GetViewport,
2417     IDirect3DDevice8Impl_SetMaterial,
2418     IDirect3DDevice8Impl_GetMaterial,
2419     IDirect3DDevice8Impl_SetLight,
2420     IDirect3DDevice8Impl_GetLight,
2421     IDirect3DDevice8Impl_LightEnable,
2422     IDirect3DDevice8Impl_GetLightEnable,
2423     IDirect3DDevice8Impl_SetClipPlane,
2424     IDirect3DDevice8Impl_GetClipPlane,
2425     IDirect3DDevice8Impl_SetRenderState,
2426     IDirect3DDevice8Impl_GetRenderState,
2427     IDirect3DDevice8Impl_BeginStateBlock,
2428     IDirect3DDevice8Impl_EndStateBlock,
2429     IDirect3DDevice8Impl_ApplyStateBlock,
2430     IDirect3DDevice8Impl_CaptureStateBlock,
2431     IDirect3DDevice8Impl_DeleteStateBlock,
2432     IDirect3DDevice8Impl_CreateStateBlock,
2433     IDirect3DDevice8Impl_SetClipStatus,
2434     IDirect3DDevice8Impl_GetClipStatus,
2435     IDirect3DDevice8Impl_GetTexture,
2436     IDirect3DDevice8Impl_SetTexture,
2437     IDirect3DDevice8Impl_GetTextureStageState,
2438     IDirect3DDevice8Impl_SetTextureStageState,
2439     IDirect3DDevice8Impl_ValidateDevice,
2440     IDirect3DDevice8Impl_GetInfo,
2441     IDirect3DDevice8Impl_SetPaletteEntries,
2442     IDirect3DDevice8Impl_GetPaletteEntries,
2443     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2444     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2445     IDirect3DDevice8Impl_DrawPrimitive,
2446     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2447     IDirect3DDevice8Impl_DrawPrimitiveUP,
2448     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2449     IDirect3DDevice8Impl_ProcessVertices,
2450     IDirect3DDevice8Impl_CreateVertexShader,
2451     IDirect3DDevice8Impl_SetVertexShader,
2452     IDirect3DDevice8Impl_GetVertexShader,
2453     IDirect3DDevice8Impl_DeleteVertexShader,
2454     IDirect3DDevice8Impl_SetVertexShaderConstant,
2455     IDirect3DDevice8Impl_GetVertexShaderConstant,
2456     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2457     IDirect3DDevice8Impl_GetVertexShaderFunction,
2458     IDirect3DDevice8Impl_SetStreamSource,
2459     IDirect3DDevice8Impl_GetStreamSource,
2460     IDirect3DDevice8Impl_SetIndices,
2461     IDirect3DDevice8Impl_GetIndices,
2462     IDirect3DDevice8Impl_CreatePixelShader,
2463     IDirect3DDevice8Impl_SetPixelShader,
2464     IDirect3DDevice8Impl_GetPixelShader,
2465     IDirect3DDevice8Impl_DeletePixelShader,
2466     IDirect3DDevice8Impl_SetPixelShaderConstant,
2467     IDirect3DDevice8Impl_GetPixelShaderConstant,
2468     IDirect3DDevice8Impl_GetPixelShaderFunction,
2469     IDirect3DDevice8Impl_DrawRectPatch,
2470     IDirect3DDevice8Impl_DrawTriPatch,
2471     IDirect3DDevice8Impl_DeletePatch
2472 };
2473
2474 /* IWineD3DDeviceParent IUnknown methods */
2475
2476 static inline struct IDirect3DDevice8Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2477 {
2478     return (struct IDirect3DDevice8Impl *)((char*)iface
2479             - FIELD_OFFSET(struct IDirect3DDevice8Impl, device_parent_vtbl));
2480 }
2481
2482 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
2483 {
2484     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2485     return IDirect3DDevice8Impl_QueryInterface((IDirect3DDevice8 *)This, riid, object);
2486 }
2487
2488 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2489 {
2490     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2491     return IDirect3DDevice8Impl_AddRef((IDirect3DDevice8 *)This);
2492 }
2493
2494 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2495 {
2496     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2497     return IDirect3DDevice8Impl_Release((IDirect3DDevice8 *)This);
2498 }
2499
2500 /* IWineD3DDeviceParent methods */
2501
2502 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2503 {
2504     TRACE("iface %p, device %p\n", iface, device);
2505 }
2506
2507 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2508         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
2509         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2510 {
2511     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2512     IDirect3DSurface8Impl *d3d_surface;
2513     BOOL lockable = TRUE;
2514     HRESULT hr;
2515
2516     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
2517             "\tpool %#x, level %u, face %u, surface %p\n",
2518             iface, superior, width, height, format, usage, pool, level, face, surface);
2519
2520
2521     if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2522
2523     hr = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)This, width, height,
2524             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2525             (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2526     if (FAILED(hr))
2527     {
2528         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2529         return hr;
2530     }
2531
2532     *surface = d3d_surface->wineD3DSurface;
2533     IWineD3DSurface_AddRef(*surface);
2534
2535     d3d_surface->container = superior;
2536     IUnknown_Release(d3d_surface->parentDevice);
2537     d3d_surface->parentDevice = NULL;
2538
2539     IDirect3DSurface8_Release((IDirect3DSurface8 *)d3d_surface);
2540     d3d_surface->forwardReference = superior;
2541
2542     return hr;
2543 }
2544
2545 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2546         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2547         DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
2548 {
2549     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2550     IDirect3DSurface8Impl *d3d_surface;
2551     HRESULT hr;
2552
2553     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2554             "\tmultisample_quality %u, lockable %u, surface %p\n",
2555             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
2556
2557     hr = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)This, width, height,
2558             d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2559     if (FAILED(hr))
2560     {
2561         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2562         return hr;
2563     }
2564
2565     *surface = d3d_surface->wineD3DSurface;
2566     IWineD3DSurface_AddRef(*surface);
2567
2568     d3d_surface->container = (IUnknown *)This;
2569     /* Implicit surfaces are created with an refcount of 0 */
2570     IUnknown_Release((IUnknown *)d3d_surface);
2571
2572     return hr;
2573 }
2574
2575 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2576         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2577         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2578 {
2579     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2580     IDirect3DSurface8Impl *d3d_surface;
2581     HRESULT hr;
2582
2583     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2584             "\tmultisample_quality %u, discard %u, surface %p\n",
2585             iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
2586
2587     hr = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)This, width, height,
2588             d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2589     if (FAILED(hr))
2590     {
2591         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2592         return hr;
2593     }
2594
2595     *surface = d3d_surface->wineD3DSurface;
2596     IWineD3DSurface_AddRef(*surface);
2597
2598     d3d_surface->container = (IUnknown *)This;
2599     /* Implicit surfaces are created with an refcount of 0 */
2600     IUnknown_Release((IUnknown *)d3d_surface);
2601
2602     return hr;
2603 }
2604
2605 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2606         IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
2607         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2608 {
2609     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2610     IDirect3DVolume8Impl *object;
2611     HRESULT hr;
2612
2613     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2614             iface, superior, width, height, depth, format, pool, usage, volume);
2615
2616     /* Allocate the storage for the device */
2617     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2618     if (!object)
2619     {
2620         FIXME("Allocation of memory failed\n");
2621         *volume = NULL;
2622         return D3DERR_OUTOFVIDEOMEMORY;
2623     }
2624
2625     hr = volume_init(object, This, width, height, depth, usage, format, pool);
2626     if (FAILED(hr))
2627     {
2628         WARN("Failed to initialize volume, hr %#x.\n", hr);
2629         HeapFree(GetProcessHeap(), 0, object);
2630         return hr;
2631     }
2632
2633     *volume = object->wineD3DVolume;
2634     IWineD3DVolume_AddRef(*volume);
2635     IDirect3DVolume8_Release((IDirect3DVolume8 *)object);
2636
2637     object->container = superior;
2638     object->forwardReference = superior;
2639
2640     TRACE("(%p) Created volume %p\n", iface, object);
2641
2642     return hr;
2643 }
2644
2645 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2646         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2647 {
2648     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2649     IDirect3DSwapChain8Impl *d3d_swapchain;
2650     D3DPRESENT_PARAMETERS local_parameters;
2651     HRESULT hr;
2652
2653     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2654
2655     /* Copy the presentation parameters */
2656     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2657     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2658     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2659     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2660     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2661     local_parameters.SwapEffect = present_parameters->SwapEffect;
2662     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2663     local_parameters.Windowed = present_parameters->Windowed;
2664     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2665     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2666     local_parameters.Flags = present_parameters->Flags;
2667     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2668     local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
2669
2670     hr = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)This,
2671             &local_parameters, (IDirect3DSwapChain8 **)&d3d_swapchain);
2672     if (FAILED(hr))
2673     {
2674         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2675         *swapchain = NULL;
2676         return hr;
2677     }
2678
2679     *swapchain = d3d_swapchain->wineD3DSwapChain;
2680     IUnknown_Release(d3d_swapchain->parentDevice);
2681     d3d_swapchain->parentDevice = NULL;
2682
2683     /* Copy back the presentation parameters */
2684     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2685     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2686     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2687     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2688     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2689     present_parameters->SwapEffect = local_parameters.SwapEffect;
2690     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2691     present_parameters->Windowed = local_parameters.Windowed;
2692     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2693     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2694     present_parameters->Flags = local_parameters.Flags;
2695     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2696     present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
2697
2698     return hr;
2699 }
2700
2701 const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
2702 {
2703     /* IUnknown methods */
2704     device_parent_QueryInterface,
2705     device_parent_AddRef,
2706     device_parent_Release,
2707     /* IWineD3DDeviceParent methods */
2708     device_parent_WineD3DDeviceCreated,
2709     device_parent_CreateSurface,
2710     device_parent_CreateRenderTarget,
2711     device_parent_CreateDepthStencilSurface,
2712     device_parent_CreateVolume,
2713     device_parent_CreateSwapChain,
2714 };