comctl32/monthcal: Add parameter validation to MCM_HITTEST handler.
[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     UINT                 srcWidth,  destWidth;
886     UINT                 srcHeight, destHeight;
887     UINT                 srcSize;
888     WINED3DSURFACE_DESC  winedesc;
889
890     TRACE("(%p) pSrcSur=%p, pSourceRects=%p, cRects=%d, pDstSur=%p, pDestPtsArr=%p\n", iface,
891           pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
892
893
894     /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the destination texture is in WINED3DPOOL_DEFAULT */
895
896     wined3d_mutex_lock();
897     IWineD3DSurface_GetDesc(Source->wineD3DSurface, &winedesc);
898     srcFormat = winedesc.format;
899     srcWidth = winedesc.width;
900     srcHeight = winedesc.height;
901     srcSize = winedesc.size;
902
903     IWineD3DSurface_GetDesc(Dest->wineD3DSurface, &winedesc);
904     destFormat = winedesc.format;
905     destWidth = winedesc.width;
906     destHeight = winedesc.height;
907
908     /* Check that the source and destination formats match */
909     if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat) {
910         WARN("(%p) source %p format must match the dest %p format, returning WINED3DERR_INVALIDCALL\n", iface, pSourceSurface, pDestinationSurface);
911         wined3d_mutex_unlock();
912         return WINED3DERR_INVALIDCALL;
913     } else if (WINED3DFMT_UNKNOWN == destFormat) {
914         TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
915         IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
916         destFormat = srcFormat;
917     }
918
919     /* Quick if complete copy ... */
920     if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
921         IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
922     } else {
923         unsigned int i;
924         /* Copy rect by rect */
925         if (NULL != pSourceRects && NULL != pDestPoints) {
926             for (i = 0; i < cRects; ++i) {
927                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
928             }
929         } else {
930             for (i = 0; i < cRects; ++i) {
931                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
932             }
933         }
934     }
935     wined3d_mutex_unlock();
936
937     return hr;
938 }
939
940 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(LPDIRECT3DDEVICE8 iface, IDirect3DBaseTexture8* pSourceTexture, IDirect3DBaseTexture8* pDestinationTexture) {
941     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
942     HRESULT hr;
943     TRACE("(%p) Relay\n" , This);
944
945     wined3d_mutex_lock();
946     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
947     wined3d_mutex_unlock();
948
949     return hr;
950 }
951
952 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pDestSurface) {
953     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
954     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
955     HRESULT hr;
956
957     TRACE("(%p) Relay\n" , This);
958
959     if (pDestSurface == NULL) {
960         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
961         return D3DERR_INVALIDCALL;
962     }
963
964     wined3d_mutex_lock();
965     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
966     wined3d_mutex_unlock();
967
968     return hr;
969 }
970
971 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8* pRenderTarget, IDirect3DSurface8* pNewZStencil) {
972     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
973     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
974     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
975     IWineD3DSurface *original_ds = NULL;
976     HRESULT hr;
977     TRACE("(%p) Relay\n" , This);
978
979     wined3d_mutex_lock();
980
981     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &original_ds);
982     if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
983     {
984         hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, pZSurface ? pZSurface->wineD3DSurface : NULL);
985         if (SUCCEEDED(hr) && pSurface)
986             hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface->wineD3DSurface);
987         if (FAILED(hr)) IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, original_ds);
988     }
989     if (original_ds) IWineD3DSurface_Release(original_ds);
990
991     wined3d_mutex_unlock();
992
993     return hr;
994 }
995
996 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetRenderTarget(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppRenderTarget) {
997     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
998     HRESULT hr = D3D_OK;
999     IWineD3DSurface *pRenderTarget;
1000
1001     TRACE("(%p) Relay\n" , This);
1002
1003     if (ppRenderTarget == NULL) {
1004         return D3DERR_INVALIDCALL;
1005     }
1006
1007     wined3d_mutex_lock();
1008     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
1009
1010     if (hr == D3D_OK && pRenderTarget != NULL) {
1011         IWineD3DSurface_GetParent(pRenderTarget,(IUnknown**)ppRenderTarget);
1012         IWineD3DSurface_Release(pRenderTarget);
1013     } else {
1014         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
1015         *ppRenderTarget = NULL;
1016     }
1017     wined3d_mutex_unlock();
1018
1019     return hr;
1020 }
1021
1022 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE8 iface, IDirect3DSurface8** ppZStencilSurface) {
1023     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1024     HRESULT hr = D3D_OK;
1025     IWineD3DSurface *pZStencilSurface;
1026
1027     TRACE("(%p) Relay\n" , This);
1028     if(ppZStencilSurface == NULL){
1029         return D3DERR_INVALIDCALL;
1030     }
1031
1032     wined3d_mutex_lock();
1033     hr=IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
1034     if (hr == WINED3D_OK) {
1035         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
1036         IWineD3DSurface_Release(pZStencilSurface);
1037     }else{
1038         if (hr != WINED3DERR_NOTFOUND)
1039                 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1040         *ppZStencilSurface = NULL;
1041     }
1042     wined3d_mutex_unlock();
1043
1044     return hr;
1045 }
1046
1047 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(LPDIRECT3DDEVICE8 iface) {
1048     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1049     HRESULT hr;
1050     TRACE("(%p) Relay\n" , This);
1051
1052     wined3d_mutex_lock();
1053     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1054     wined3d_mutex_unlock();
1055
1056     return hr;
1057 }
1058
1059 static HRESULT WINAPI IDirect3DDevice8Impl_EndScene(LPDIRECT3DDEVICE8 iface) {
1060     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1061     HRESULT hr;
1062     TRACE("(%p) Relay\n" , This);
1063
1064     wined3d_mutex_lock();
1065     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1066     wined3d_mutex_unlock();
1067
1068     return hr;
1069 }
1070
1071 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(LPDIRECT3DDEVICE8 iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
1072     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1073     HRESULT hr;
1074     TRACE("(%p) Relay\n" , This);
1075
1076     /* Note: D3DRECT is compatible with WINED3DRECT */
1077     wined3d_mutex_lock();
1078     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
1079     wined3d_mutex_unlock();
1080
1081     return hr;
1082 }
1083
1084 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
1085     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1086     HRESULT hr;
1087     TRACE("(%p) Relay\n" , This);
1088
1089     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1090     wined3d_mutex_lock();
1091     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1092     wined3d_mutex_unlock();
1093
1094     return hr;
1095 }
1096
1097 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State,D3DMATRIX* pMatrix) {
1098     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1099     HRESULT hr;
1100     TRACE("(%p) Relay\n" , This);
1101
1102     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1103     wined3d_mutex_lock();
1104     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1105     wined3d_mutex_unlock();
1106
1107     return hr;
1108 }
1109
1110 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(LPDIRECT3DDEVICE8 iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
1111     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1112     HRESULT hr;
1113     TRACE("(%p) Relay\n" , This);
1114
1115     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1116     wined3d_mutex_lock();
1117     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1118     wined3d_mutex_unlock();
1119
1120     return hr;
1121 }
1122
1123 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(LPDIRECT3DDEVICE8 iface, CONST D3DVIEWPORT8* pViewport) {
1124     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1125     HRESULT hr;
1126     TRACE("(%p) Relay\n" , This);
1127
1128     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1129     wined3d_mutex_lock();
1130     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1131     wined3d_mutex_unlock();
1132
1133     return hr;
1134 }
1135
1136 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(LPDIRECT3DDEVICE8 iface, D3DVIEWPORT8* pViewport) {
1137     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1138     HRESULT hr;
1139     TRACE("(%p) Relay\n" , This);
1140
1141     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1142     wined3d_mutex_lock();
1143     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1144     wined3d_mutex_unlock();
1145
1146     return hr;
1147 }
1148
1149 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(LPDIRECT3DDEVICE8 iface, CONST D3DMATERIAL8* pMaterial) {
1150     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1151     HRESULT hr;
1152     TRACE("(%p) Relay\n" , This);
1153
1154     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1155     wined3d_mutex_lock();
1156     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1157     wined3d_mutex_unlock();
1158
1159     return hr;
1160 }
1161
1162 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(LPDIRECT3DDEVICE8 iface, D3DMATERIAL8* pMaterial) {
1163     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1164     HRESULT hr;
1165     TRACE("(%p) Relay\n" , This);
1166
1167     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1168     wined3d_mutex_lock();
1169     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1170     wined3d_mutex_unlock();
1171
1172     return hr;
1173 }
1174
1175 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(LPDIRECT3DDEVICE8 iface, DWORD Index, CONST D3DLIGHT8* pLight) {
1176     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1177     HRESULT hr;
1178     TRACE("(%p) Relay\n" , This);
1179
1180     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1181     wined3d_mutex_lock();
1182     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1183     wined3d_mutex_unlock();
1184
1185     return hr;
1186 }
1187
1188 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(LPDIRECT3DDEVICE8 iface, DWORD Index,D3DLIGHT8* pLight) {
1189     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1190     HRESULT hr;
1191     TRACE("(%p) Relay\n" , This);
1192
1193     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1194     wined3d_mutex_lock();
1195     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1196     wined3d_mutex_unlock();
1197
1198     return hr;
1199 }
1200
1201 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL Enable) {
1202     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1203     HRESULT hr;
1204     TRACE("(%p) Relay\n" , This);
1205
1206     wined3d_mutex_lock();
1207     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1208     wined3d_mutex_unlock();
1209
1210     return hr;
1211 }
1212
1213 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(LPDIRECT3DDEVICE8 iface, DWORD Index,BOOL* pEnable) {
1214     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1215     HRESULT hr;
1216     TRACE("(%p) Relay\n" , This);
1217
1218     wined3d_mutex_lock();
1219     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1220     wined3d_mutex_unlock();
1221
1222     return hr;
1223 }
1224
1225 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,CONST float* pPlane) {
1226     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1227     HRESULT hr;
1228     TRACE("(%p) Relay\n" , This);
1229
1230     wined3d_mutex_lock();
1231     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1232     wined3d_mutex_unlock();
1233
1234     return hr;
1235 }
1236
1237 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(LPDIRECT3DDEVICE8 iface, DWORD Index,float* pPlane) {
1238     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1239     HRESULT hr;
1240     TRACE("(%p) Relay\n" , This);
1241
1242     wined3d_mutex_lock();
1243     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1244     wined3d_mutex_unlock();
1245
1246     return hr;
1247 }
1248
1249 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD Value) {
1250     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1251     HRESULT hr;
1252     TRACE("(%p) Relay\n" , This);
1253
1254     wined3d_mutex_lock();
1255     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1256     wined3d_mutex_unlock();
1257
1258     return hr;
1259 }
1260
1261 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(LPDIRECT3DDEVICE8 iface, D3DRENDERSTATETYPE State,DWORD* pValue) {
1262     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1263     HRESULT hr;
1264     TRACE("(%p) Relay\n" , This);
1265
1266     wined3d_mutex_lock();
1267     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1268     wined3d_mutex_unlock();
1269
1270     return hr;
1271 }
1272
1273 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(LPDIRECT3DDEVICE8 iface) {
1274     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1275     HRESULT hr;
1276     TRACE("(%p)\n", This);
1277
1278     wined3d_mutex_lock();
1279     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1280     wined3d_mutex_unlock();
1281
1282     return hr;
1283 }
1284
1285 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(LPDIRECT3DDEVICE8 iface, DWORD* pToken) {
1286     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1287     HRESULT hr;
1288     IWineD3DStateBlock* wineD3DStateBlock;
1289     IDirect3DStateBlock8Impl* object;
1290
1291     TRACE("(%p) Relay\n", This);
1292
1293     /* Tell wineD3D to endstateblock before anything else (in case we run out
1294      * of memory later and cause locking problems)
1295      */
1296     wined3d_mutex_lock();
1297     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &wineD3DStateBlock);
1298     if (hr != D3D_OK) {
1299         WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1300         wined3d_mutex_unlock();
1301         return hr;
1302     }
1303
1304     /* allocate a new IDirectD3DStateBlock */
1305     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY ,sizeof(IDirect3DStateBlock8Impl));
1306     object->ref = 1;
1307     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1308
1309     object->wineD3DStateBlock = wineD3DStateBlock;
1310
1311     *pToken = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_SB);
1312     wined3d_mutex_unlock();
1313
1314     if (*pToken == D3D8_INVALID_HANDLE)
1315     {
1316         ERR("Failed to create a handle\n");
1317         IDirect3DStateBlock8_Release((IDirect3DStateBlock8 *)object);
1318         return E_FAIL;
1319     }
1320     ++*pToken;
1321
1322     TRACE("Returning %#x (%p).\n", *pToken, object);
1323
1324     return hr;
1325 }
1326
1327 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1328     IDirect3DDevice8Impl     *This = (IDirect3DDevice8Impl *)iface;
1329     IDirect3DStateBlock8Impl *pSB;
1330     HRESULT hr;
1331
1332     TRACE("(%p) %#x Relay\n", This, Token);
1333
1334     wined3d_mutex_lock();
1335     pSB = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1336     if (!pSB)
1337     {
1338         WARN("Invalid handle (%#x) passed.\n", Token);
1339         wined3d_mutex_unlock();
1340         return D3DERR_INVALIDCALL;
1341     }
1342     hr = IWineD3DStateBlock_Apply(pSB->wineD3DStateBlock);
1343     wined3d_mutex_unlock();
1344
1345     return hr;
1346 }
1347
1348 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1349     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1350     IDirect3DStateBlock8Impl *pSB;
1351     HRESULT hr;
1352
1353     TRACE("(%p) %#x Relay\n", This, Token);
1354
1355     wined3d_mutex_lock();
1356     pSB = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1357     if (!pSB)
1358     {
1359         WARN("Invalid handle (%#x) passed.\n", Token);
1360         wined3d_mutex_unlock();
1361         return D3DERR_INVALIDCALL;
1362     }
1363     hr = IWineD3DStateBlock_Capture(pSB->wineD3DStateBlock);
1364     wined3d_mutex_unlock();
1365
1366     return hr;
1367 }
1368
1369 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(LPDIRECT3DDEVICE8 iface, DWORD Token) {
1370     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1371     IDirect3DStateBlock8Impl *pSB;
1372
1373     TRACE("(%p) Relay\n", This);
1374
1375     wined3d_mutex_lock();
1376     pSB = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1377     wined3d_mutex_unlock();
1378
1379     if (!pSB)
1380     {
1381         WARN("Invalid handle (%#x) passed.\n", Token);
1382         return D3DERR_INVALIDCALL;
1383     }
1384
1385     if (IUnknown_Release((IUnknown *)pSB))
1386     {
1387         ERR("Stateblock %p has references left, this shouldn't happen.\n", pSB);
1388     }
1389
1390     return D3D_OK;
1391 }
1392
1393 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1394         D3DSTATEBLOCKTYPE Type, DWORD *handle)
1395 {
1396     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1397     IDirect3DStateBlock8Impl *object;
1398     HRESULT hr;
1399
1400     TRACE("(%p) Relay\n", This);
1401
1402     if (Type != D3DSBT_ALL
1403             && Type != D3DSBT_PIXELSTATE
1404             && Type != D3DSBT_VERTEXSTATE)
1405     {
1406         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1407         return D3DERR_INVALIDCALL;
1408     }
1409
1410     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DStateBlock8Impl));
1411     if (!object)
1412     {
1413         ERR("Failed to allocate memory.\n");
1414         return E_OUTOFMEMORY;
1415     }
1416
1417     object->lpVtbl = &Direct3DStateBlock8_Vtbl;
1418     object->ref = 1;
1419
1420     wined3d_mutex_lock();
1421     hr = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type,
1422             &object->wineD3DStateBlock, (IUnknown *)object);
1423     if (FAILED(hr))
1424     {
1425         wined3d_mutex_unlock();
1426         ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1427         HeapFree(GetProcessHeap(), 0, object);
1428         return hr;
1429     }
1430
1431     *handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_SB);
1432     wined3d_mutex_unlock();
1433
1434     if (*handle == D3D8_INVALID_HANDLE)
1435     {
1436         ERR("Failed to allocate a handle.\n");
1437         IDirect3DStateBlock8_Release((IDirect3DStateBlock8 *)object);
1438         return E_FAIL;
1439     }
1440     ++*handle;
1441
1442     TRACE("Returning %#x (%p).\n", *handle, object);
1443
1444     return hr;
1445 }
1446
1447 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(LPDIRECT3DDEVICE8 iface, CONST D3DCLIPSTATUS8* pClipStatus) {
1448     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1449     HRESULT hr;
1450     TRACE("(%p) Relay\n" , This);
1451 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1452
1453     wined3d_mutex_lock();
1454     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1455     wined3d_mutex_unlock();
1456
1457     return hr;
1458 }
1459
1460 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(LPDIRECT3DDEVICE8 iface, D3DCLIPSTATUS8* pClipStatus) {
1461     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1462     HRESULT hr;
1463     TRACE("(%p) Relay\n" , This);
1464
1465     wined3d_mutex_lock();
1466     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1467     wined3d_mutex_unlock();
1468
1469     return hr;
1470 }
1471
1472 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage,IDirect3DBaseTexture8** ppTexture) {
1473     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1474     IWineD3DBaseTexture *retTexture;
1475     HRESULT hr;
1476
1477     TRACE("(%p) Relay\n" , This);
1478
1479     if(ppTexture == NULL){
1480         return D3DERR_INVALIDCALL;
1481     }
1482
1483     wined3d_mutex_lock();
1484     hr = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1485     if (FAILED(hr))
1486     {
1487         WARN("Failed to get texture for stage %u, hr %#x.\n", Stage, hr);
1488         wined3d_mutex_unlock();
1489         *ppTexture = NULL;
1490         return hr;
1491     }
1492
1493     if (retTexture)
1494     {
1495         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1496         IWineD3DBaseTexture_Release(retTexture);
1497     }
1498     else
1499     {
1500         *ppTexture = NULL;
1501     }
1502     wined3d_mutex_unlock();
1503
1504     return D3D_OK;
1505 }
1506
1507 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1508     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1509     HRESULT hr;
1510     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1511
1512     wined3d_mutex_lock();
1513     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1514                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1515     wined3d_mutex_unlock();
1516
1517     return hr;
1518 }
1519
1520 static const struct tss_lookup
1521 {
1522     BOOL sampler_state;
1523     DWORD state;
1524 }
1525 tss_lookup[] =
1526 {
1527     {FALSE, WINED3DTSS_FORCE_DWORD},            /*  0, unused */
1528     {FALSE, WINED3DTSS_COLOROP},                /*  1, D3DTSS_COLOROP */
1529     {FALSE, WINED3DTSS_COLORARG1},              /*  2, D3DTSS_COLORARG1 */
1530     {FALSE, WINED3DTSS_COLORARG2},              /*  3, D3DTSS_COLORARG2 */
1531     {FALSE, WINED3DTSS_ALPHAOP},                /*  4, D3DTSS_ALPHAOP */
1532     {FALSE, WINED3DTSS_ALPHAARG1},              /*  5, D3DTSS_ALPHAARG1 */
1533     {FALSE, WINED3DTSS_ALPHAARG2},              /*  6, D3DTSS_ALPHAARG2 */
1534     {FALSE, WINED3DTSS_BUMPENVMAT00},           /*  7, D3DTSS_BUMPENVMAT00 */
1535     {FALSE, WINED3DTSS_BUMPENVMAT01},           /*  8, D3DTSS_BUMPENVMAT01 */
1536     {FALSE, WINED3DTSS_BUMPENVMAT10},           /*  9, D3DTSS_BUMPENVMAT10 */
1537     {FALSE, WINED3DTSS_BUMPENVMAT11},           /* 10, D3DTSS_BUMPENVMAT11 */
1538     {FALSE, WINED3DTSS_TEXCOORDINDEX},          /* 11, D3DTSS_TEXCOORDINDEX */
1539     {FALSE, WINED3DTSS_FORCE_DWORD},            /* 12, unused */
1540     {TRUE,  WINED3DSAMP_ADDRESSU},              /* 13, D3DTSS_ADDRESSU */
1541     {TRUE,  WINED3DSAMP_ADDRESSV},              /* 14, D3DTSS_ADDRESSV */
1542     {TRUE,  WINED3DSAMP_BORDERCOLOR},           /* 15, D3DTSS_BORDERCOLOR */
1543     {TRUE,  WINED3DSAMP_MAGFILTER},             /* 16, D3DTSS_MAGFILTER */
1544     {TRUE,  WINED3DSAMP_MINFILTER},             /* 17, D3DTSS_MINFILTER */
1545     {TRUE,  WINED3DSAMP_MIPFILTER},             /* 18, D3DTSS_MIPFILTER */
1546     {TRUE,  WINED3DSAMP_MIPMAPLODBIAS},         /* 19, D3DTSS_MIPMAPLODBIAS */
1547     {TRUE,  WINED3DSAMP_MAXMIPLEVEL},           /* 20, D3DTSS_MAXMIPLEVEL */
1548     {TRUE,  WINED3DSAMP_MAXANISOTROPY},         /* 21, D3DTSS_MAXANISOTROPY */
1549     {FALSE, WINED3DTSS_BUMPENVLSCALE},          /* 22, D3DTSS_BUMPENVLSCALE */
1550     {FALSE, WINED3DTSS_BUMPENVLOFFSET},         /* 23, D3DTSS_BUMPENVLOFFSET */
1551     {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS},  /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1552     {TRUE,  WINED3DSAMP_ADDRESSW},              /* 25, D3DTSS_ADDRESSW */
1553     {FALSE, WINED3DTSS_COLORARG0},              /* 26, D3DTSS_COLORARG0 */
1554     {FALSE, WINED3DTSS_ALPHAARG0},              /* 27, D3DTSS_ALPHAARG0 */
1555     {FALSE, WINED3DTSS_RESULTARG},              /* 28, D3DTSS_RESULTARG */
1556 };
1557
1558 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1559     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1560     const struct tss_lookup *l = &tss_lookup[Type];
1561     HRESULT hr;
1562     TRACE("(%p) Relay\n" , This);
1563
1564     wined3d_mutex_lock();
1565     if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1566     else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1567     wined3d_mutex_unlock();
1568
1569     return hr;
1570 }
1571
1572 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1573     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1574     const struct tss_lookup *l = &tss_lookup[Type];
1575     HRESULT hr;
1576     TRACE("(%p) Relay\n" , This);
1577
1578     wined3d_mutex_lock();
1579     if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1580     else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1581     wined3d_mutex_unlock();
1582
1583     return hr;
1584 }
1585
1586 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1587     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1588     HRESULT hr;
1589     TRACE("(%p) Relay\n" , This);
1590
1591     wined3d_mutex_lock();
1592     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1593     wined3d_mutex_unlock();
1594
1595     return hr;
1596 }
1597
1598 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1599     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1600     FIXME("(%p) : stub\n", This);
1601     return D3D_OK;
1602 }
1603
1604 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1605     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1606     HRESULT hr;
1607     TRACE("(%p) Relay\n" , This);
1608
1609     wined3d_mutex_lock();
1610     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1611     wined3d_mutex_unlock();
1612
1613     return hr;
1614 }
1615
1616 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1617     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1618     HRESULT hr;
1619     TRACE("(%p) Relay\n" , This);
1620
1621     wined3d_mutex_lock();
1622     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1623     wined3d_mutex_unlock();
1624
1625     return hr;
1626 }
1627
1628 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1629     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1630     HRESULT hr;
1631     TRACE("(%p) Relay\n" , This);
1632
1633     wined3d_mutex_lock();
1634     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1635     wined3d_mutex_unlock();
1636
1637     return hr;
1638 }
1639
1640 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1641     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1642     HRESULT hr;
1643     TRACE("(%p) Relay\n" , This);
1644
1645     wined3d_mutex_lock();
1646     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1647     wined3d_mutex_unlock();
1648
1649     return hr;
1650 }
1651
1652 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface, D3DPRIMITIVETYPE PrimitiveType,
1653         UINT StartVertex, UINT PrimitiveCount)
1654 {
1655     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1656     HRESULT hr;
1657     TRACE("(%p) Relay\n" , This);
1658
1659     wined3d_mutex_lock();
1660     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1661     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1662             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1663     wined3d_mutex_unlock();
1664
1665     return hr;
1666 }
1667
1668 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1669                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1670     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1671     HRESULT hr;
1672     TRACE("(%p) Relay\n" , This);
1673
1674     wined3d_mutex_lock();
1675     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1676     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1677             vertex_count_from_primitive_count(PrimitiveType, primCount));
1678     wined3d_mutex_unlock();
1679
1680     return hr;
1681 }
1682
1683 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1684     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1685     HRESULT hr;
1686     TRACE("(%p) Relay\n" , This);
1687
1688     wined3d_mutex_lock();
1689     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1690     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1691             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1692             pVertexStreamZeroData, VertexStreamZeroStride);
1693     wined3d_mutex_unlock();
1694
1695     return hr;
1696 }
1697
1698 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1699                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1700                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1701                                                              UINT VertexStreamZeroStride) {
1702     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1703     HRESULT hr;
1704     TRACE("(%p) Relay\n" , This);
1705
1706     wined3d_mutex_lock();
1707     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1708     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1709             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1710             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1711     wined3d_mutex_unlock();
1712
1713     return hr;
1714 }
1715
1716 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1717     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1718     HRESULT hr;
1719     IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
1720     TRACE("(%p) Relay\n" , This);
1721
1722     wined3d_mutex_lock();
1723     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1724     wined3d_mutex_unlock();
1725
1726     return hr;
1727 }
1728
1729 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
1730         const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
1731 {
1732     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1733     IDirect3DVertexShader8Impl *object;
1734     DWORD shader_handle;
1735     DWORD handle;
1736     HRESULT hr;
1737
1738     TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
1739             iface, declaration, byte_code, shader, usage);
1740
1741     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1742     if (!object)
1743     {
1744         ERR("Failed to allocate vertex shader memory.\n");
1745         *shader = 0;
1746         return E_OUTOFMEMORY;
1747     }
1748
1749     wined3d_mutex_lock();
1750     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
1751     wined3d_mutex_unlock();
1752     if (handle == D3D8_INVALID_HANDLE)
1753     {
1754         ERR("Failed to allocate vertex shader handle.\n");
1755         HeapFree(GetProcessHeap(), 0, object);
1756         *shader = 0;
1757         return E_OUTOFMEMORY;
1758     }
1759
1760     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
1761
1762     hr = vertexshader_init(object, This, declaration, byte_code, shader_handle, usage);
1763     if (FAILED(hr))
1764     {
1765         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
1766         wined3d_mutex_lock();
1767         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
1768         wined3d_mutex_unlock();
1769         HeapFree(GetProcessHeap(), 0, object);
1770         *shader = 0;
1771         return hr;
1772     }
1773
1774     TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
1775     *shader = shader_handle;
1776
1777     return D3D_OK;
1778 }
1779
1780 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1781 {
1782     IDirect3DVertexDeclaration8Impl *d3d8_declaration;
1783     HRESULT hr;
1784     int p, low, high; /* deliberately signed */
1785     struct FvfToDecl *convertedDecls = This->decls;
1786
1787     TRACE("Searching for declaration for fvf %08x... ", fvf);
1788
1789     low = 0;
1790     high = This->numConvertedDecls - 1;
1791     while(low <= high) {
1792         p = (low + high) >> 1;
1793         TRACE("%d ", p);
1794         if(convertedDecls[p].fvf == fvf) {
1795             TRACE("found %p\n", convertedDecls[p].decl);
1796             return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
1797         } else if(convertedDecls[p].fvf < fvf) {
1798             low = p + 1;
1799         } else {
1800             high = p - 1;
1801         }
1802     }
1803     TRACE("not found. Creating and inserting at position %d.\n", low);
1804
1805     d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
1806     if (!d3d8_declaration)
1807     {
1808         ERR("Memory allocation failed.\n");
1809         return NULL;
1810     }
1811
1812     hr = vertexdeclaration_init_fvf(d3d8_declaration, This, fvf);
1813     if (FAILED(hr))
1814     {
1815         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
1816         HeapFree(GetProcessHeap(), 0, d3d8_declaration);
1817         return NULL;
1818     }
1819
1820     if(This->declArraySize == This->numConvertedDecls) {
1821         int grow = This->declArraySize / 2;
1822         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1823                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1824         if(!convertedDecls) {
1825             /* This will destroy it */
1826             IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
1827             return NULL;
1828         }
1829         This->decls = convertedDecls;
1830         This->declArraySize += grow;
1831     }
1832
1833     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
1834     convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
1835     convertedDecls[low].fvf = fvf;
1836     This->numConvertedDecls++;
1837
1838     TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
1839     return d3d8_declaration;
1840 }
1841
1842 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1843     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1844     IDirect3DVertexShader8Impl *shader;
1845     HRESULT hr;
1846
1847     TRACE("(%p) : Relay\n", This);
1848
1849     if (VS_HIGHESTFIXEDFXF >= pShader) {
1850         TRACE("Setting FVF, %#x\n", pShader);
1851
1852         wined3d_mutex_lock();
1853         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1854                 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
1855         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1856         wined3d_mutex_unlock();
1857
1858         return D3D_OK;
1859     }
1860
1861     TRACE("Setting shader\n");
1862
1863     wined3d_mutex_lock();
1864     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1865     if (!shader)
1866     {
1867         WARN("Invalid handle (%#x) passed.\n", pShader);
1868         wined3d_mutex_unlock();
1869
1870         return D3DERR_INVALIDCALL;
1871     }
1872
1873     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1874             ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
1875     if (SUCCEEDED(hr)) hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wineD3DVertexShader);
1876     wined3d_mutex_unlock();
1877
1878     TRACE("Returning hr %#x\n", hr);
1879
1880     return hr;
1881 }
1882
1883 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1884     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1885     IWineD3DVertexDeclaration *wined3d_declaration;
1886     IDirect3DVertexDeclaration8 *d3d8_declaration;
1887     HRESULT hrc;
1888
1889     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1890
1891     wined3d_mutex_lock();
1892     hrc = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
1893     if (FAILED(hrc))
1894     {
1895         wined3d_mutex_unlock();
1896         WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
1897                 This, hrc, This->WineD3DDevice);
1898         return hrc;
1899     }
1900
1901     if (!wined3d_declaration)
1902     {
1903         wined3d_mutex_unlock();
1904         *ppShader = 0;
1905         return D3D_OK;
1906     }
1907
1908     hrc = IWineD3DVertexDeclaration_GetParent(wined3d_declaration, (IUnknown **)&d3d8_declaration);
1909     IWineD3DVertexDeclaration_Release(wined3d_declaration);
1910     wined3d_mutex_unlock();
1911     if (SUCCEEDED(hrc))
1912     {
1913         *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
1914         IDirect3DVertexDeclaration8_Release(d3d8_declaration);
1915     }
1916
1917     TRACE("(%p) : returning %#x\n", This, *ppShader);
1918
1919     return hrc;
1920 }
1921
1922 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1923     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1924     IDirect3DVertexShader8Impl *shader;
1925     IWineD3DVertexShader *cur = NULL;
1926
1927     TRACE("(%p) : pShader %#x\n", This, pShader);
1928
1929     wined3d_mutex_lock();
1930     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1931     if (!shader)
1932     {
1933         WARN("Invalid handle (%#x) passed.\n", pShader);
1934         wined3d_mutex_unlock();
1935
1936         return D3DERR_INVALIDCALL;
1937     }
1938
1939     IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
1940
1941     if (cur)
1942     {
1943         if (cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
1944         IWineD3DVertexShader_Release(cur);
1945     }
1946
1947     wined3d_mutex_unlock();
1948
1949     if (IUnknown_Release((IUnknown *)shader))
1950     {
1951         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
1952     }
1953
1954     return D3D_OK;
1955 }
1956
1957 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1958     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1959     HRESULT hr;
1960     TRACE("(%p) : Relay\n", This);
1961
1962     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
1963         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
1964              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
1965         return D3DERR_INVALIDCALL;
1966     }
1967
1968     wined3d_mutex_lock();
1969     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
1970     wined3d_mutex_unlock();
1971
1972     return hr;
1973 }
1974
1975 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1976     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1977     HRESULT hr;
1978     TRACE("(%p) : Relay\n", This);
1979
1980     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
1981         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
1982              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
1983         return D3DERR_INVALIDCALL;
1984     }
1985
1986     wined3d_mutex_lock();
1987     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
1988     wined3d_mutex_unlock();
1989
1990     return hr;
1991 }
1992
1993 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1994     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1995     IDirect3DVertexDeclaration8Impl *declaration;
1996     IDirect3DVertexShader8Impl *shader;
1997
1998     TRACE("(%p) : pVertexShader 0x%08x, pData %p, *pSizeOfData %u\n", This, pVertexShader, pData, *pSizeOfData);
1999
2000     wined3d_mutex_lock();
2001     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2002     wined3d_mutex_unlock();
2003
2004     if (!shader)
2005     {
2006         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2007         return D3DERR_INVALIDCALL;
2008     }
2009     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2010
2011     /* If pData is NULL, we just return the required size of the buffer. */
2012     if (!pData) {
2013         *pSizeOfData = declaration->elements_size;
2014         return D3D_OK;
2015     }
2016
2017     /* MSDN claims that if *pSizeOfData is smaller than the required size
2018      * we should write the required size and return D3DERR_MOREDATA.
2019      * That's not actually true. */
2020     if (*pSizeOfData < declaration->elements_size) {
2021         return D3DERR_INVALIDCALL;
2022     }
2023
2024     CopyMemory(pData, declaration->elements, declaration->elements_size);
2025
2026     return D3D_OK;
2027 }
2028
2029 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
2030     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2031     IDirect3DVertexShader8Impl *shader = NULL;
2032     HRESULT hr;
2033
2034     TRACE("(%p) : pVertexShader %#x, pData %p, pSizeOfData %p\n", This, pVertexShader, pData, pSizeOfData);
2035
2036     wined3d_mutex_lock();
2037     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2038     if (!shader)
2039     {
2040         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2041         wined3d_mutex_unlock();
2042
2043         return D3DERR_INVALIDCALL;
2044     }
2045
2046     if (!shader->wineD3DVertexShader)
2047     {
2048         wined3d_mutex_unlock();
2049         *pSizeOfData = 0;
2050         return D3D_OK;
2051     }
2052
2053     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, pSizeOfData);
2054     wined3d_mutex_unlock();
2055
2056     return hr;
2057 }
2058
2059 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
2060     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2061     HRESULT hr;
2062     IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2063     TRACE("(%p) Relay\n", This);
2064
2065     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2066      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2067      * vertex buffers can't be created to address them with an index that requires the 32nd bit
2068      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2069      * problem)
2070      */
2071     wined3d_mutex_lock();
2072     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2073     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2074             ib ? ib->wineD3DIndexBuffer : NULL,
2075             ib ? ib->format : WINED3DFMT_UNKNOWN);
2076     wined3d_mutex_unlock();
2077
2078     return hr;
2079 }
2080
2081 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
2082     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2083     IWineD3DBuffer *retIndexData = NULL;
2084     HRESULT rc = D3D_OK;
2085
2086     TRACE("(%p) Relay\n", This);
2087
2088     if(ppIndexData == NULL){
2089         return D3DERR_INVALIDCALL;
2090     }
2091
2092     /* The case from UINT to INT is safe because d3d8 will never set negative values */
2093     wined3d_mutex_lock();
2094     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2095     rc = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2096     if (SUCCEEDED(rc) && retIndexData) {
2097         IWineD3DBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
2098         IWineD3DBuffer_Release(retIndexData);
2099     } else {
2100         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
2101         *ppIndexData = NULL;
2102     }
2103     wined3d_mutex_unlock();
2104
2105     return rc;
2106 }
2107
2108 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(IDirect3DDevice8 *iface,
2109         const DWORD *byte_code, DWORD *shader)
2110 {
2111     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2112     IDirect3DPixelShader8Impl *object;
2113     DWORD shader_handle;
2114     DWORD handle;
2115     HRESULT hr;
2116
2117     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2118
2119     if (!shader)
2120     {
2121         TRACE("(%p) Invalid call\n", This);
2122         return D3DERR_INVALIDCALL;
2123     }
2124
2125     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2126     if (!object)
2127     {
2128         ERR("Failed to allocate pixel shader memmory.\n");
2129         return E_OUTOFMEMORY;
2130     }
2131
2132     wined3d_mutex_lock();
2133     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2134     wined3d_mutex_unlock();
2135     if (handle == D3D8_INVALID_HANDLE)
2136     {
2137         ERR("Failed to allocate pixel shader handle.\n");
2138         HeapFree(GetProcessHeap(), 0, object);
2139         return E_OUTOFMEMORY;
2140     }
2141
2142     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2143
2144     hr = pixelshader_init(object, This, byte_code, shader_handle);
2145     if (FAILED(hr))
2146     {
2147         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2148         wined3d_mutex_lock();
2149         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_PS);
2150         wined3d_mutex_unlock();
2151         HeapFree(GetProcessHeap(), 0, object);
2152         *shader = 0;
2153         return hr;
2154     }
2155
2156     TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2157     *shader = shader_handle;
2158
2159     return D3D_OK;
2160 }
2161
2162 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2163     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2164     IDirect3DPixelShader8Impl *shader;
2165     HRESULT hr;
2166
2167     TRACE("(%p) : pShader %#x\n", This, pShader);
2168
2169     wined3d_mutex_lock();
2170
2171     if (!pShader)
2172     {
2173         hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2174         wined3d_mutex_unlock();
2175         return hr;
2176     }
2177
2178     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2179     if (!shader)
2180     {
2181         WARN("Invalid handle (%#x) passed.\n", pShader);
2182         wined3d_mutex_unlock();
2183         return D3DERR_INVALIDCALL;
2184     }
2185
2186     TRACE("(%p) : Setting shader %p\n", This, shader);
2187     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wineD3DPixelShader);
2188     wined3d_mutex_unlock();
2189
2190     return hr;
2191 }
2192
2193 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
2194     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2195     IWineD3DPixelShader *object;
2196
2197     HRESULT hrc = D3D_OK;
2198     TRACE("(%p) Relay\n", This);
2199     if (NULL == ppShader) {
2200         TRACE("(%p) Invalid call\n", This);
2201         return D3DERR_INVALIDCALL;
2202     }
2203
2204     wined3d_mutex_lock();
2205     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
2206     if (D3D_OK == hrc && NULL != object) {
2207         IDirect3DPixelShader8Impl *d3d8_shader;
2208         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
2209         IWineD3DPixelShader_Release(object);
2210         *ppShader = d3d8_shader->handle;
2211         IDirect3DPixelShader8_Release((IDirect3DPixelShader8 *)d3d8_shader);
2212     } else {
2213         *ppShader = 0;
2214     }
2215     wined3d_mutex_unlock();
2216
2217     TRACE("(%p) : returning %#x\n", This, *ppShader);
2218
2219     return hrc;
2220 }
2221
2222 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2223     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2224     IDirect3DPixelShader8Impl *shader;
2225     IWineD3DPixelShader *cur = NULL;
2226
2227     TRACE("(%p) : pShader %#x\n", This, pShader);
2228
2229     wined3d_mutex_lock();
2230
2231     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2232     if (!shader)
2233     {
2234         WARN("Invalid handle (%#x) passed.\n", pShader);
2235         wined3d_mutex_unlock();
2236         return D3D_OK;
2237     }
2238
2239     IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
2240
2241     if (cur)
2242     {
2243         if (cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
2244         IWineD3DPixelShader_Release(cur);
2245     }
2246
2247     wined3d_mutex_unlock();
2248
2249     if (IUnknown_Release((IUnknown *)shader))
2250     {
2251         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2252     }
2253
2254     return D3D_OK;
2255 }
2256
2257 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
2258     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2259     HRESULT hr;
2260     TRACE("(%p) Relay\n", This);
2261
2262     wined3d_mutex_lock();
2263     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2264     wined3d_mutex_unlock();
2265
2266     return hr;
2267 }
2268
2269 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
2270     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2271     HRESULT hr;
2272     TRACE("(%p) Relay\n", This);
2273
2274     wined3d_mutex_lock();
2275     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2276     wined3d_mutex_unlock();
2277
2278     return hr;
2279 }
2280
2281 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
2282     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2283     IDirect3DPixelShader8Impl *shader = NULL;
2284     HRESULT hr;
2285
2286     TRACE("(%p) : pPixelShader %#x, pData %p, pSizeOfData %p\n", This, pPixelShader, pData, pSizeOfData);
2287
2288     wined3d_mutex_lock();
2289     shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2290     if (!shader)
2291     {
2292         WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2293         wined3d_mutex_unlock();
2294
2295         return D3DERR_INVALIDCALL;
2296     }
2297
2298     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, pSizeOfData);
2299     wined3d_mutex_unlock();
2300
2301     return hr;
2302 }
2303
2304 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2305     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2306     HRESULT hr;
2307     TRACE("(%p) Relay\n", This);
2308
2309     wined3d_mutex_lock();
2310     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2311     wined3d_mutex_unlock();
2312
2313     return hr;
2314 }
2315
2316 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2317     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2318     HRESULT hr;
2319     TRACE("(%p) Relay\n", This);
2320
2321     wined3d_mutex_lock();
2322     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2323     wined3d_mutex_unlock();
2324
2325     return hr;
2326 }
2327
2328 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
2329     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2330     HRESULT hr;
2331     TRACE("(%p) Relay\n", This);
2332
2333     wined3d_mutex_lock();
2334     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2335     wined3d_mutex_unlock();
2336
2337     return hr;
2338 }
2339
2340 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
2341     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2342     HRESULT hr;
2343     TRACE("(%p) Relay\n" , This);
2344
2345     wined3d_mutex_lock();
2346     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2347                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2348                                         0/* Offset in bytes */, Stride);
2349     wined3d_mutex_unlock();
2350
2351     return hr;
2352 }
2353
2354 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
2355     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2356     IWineD3DBuffer *retStream = NULL;
2357     HRESULT rc = D3D_OK;
2358
2359     TRACE("(%p) Relay\n" , This);
2360
2361     if(pStream == NULL){
2362         return D3DERR_INVALIDCALL;
2363     }
2364
2365     wined3d_mutex_lock();
2366     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, 0 /* Offset in bytes */, pStride);
2367     if (rc == D3D_OK  && NULL != retStream) {
2368         IWineD3DBuffer_GetParent(retStream, (IUnknown **)pStream);
2369         IWineD3DBuffer_Release(retStream);
2370     }else{
2371         if (rc != D3D_OK){
2372             FIXME("Call to GetStreamSource failed %p\n",  pStride);
2373         }
2374         *pStream = NULL;
2375     }
2376     wined3d_mutex_unlock();
2377
2378     return rc;
2379 }
2380
2381
2382 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2383 {
2384     IDirect3DDevice8Impl_QueryInterface,
2385     IDirect3DDevice8Impl_AddRef,
2386     IDirect3DDevice8Impl_Release,
2387     IDirect3DDevice8Impl_TestCooperativeLevel,
2388     IDirect3DDevice8Impl_GetAvailableTextureMem,
2389     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2390     IDirect3DDevice8Impl_GetDirect3D,
2391     IDirect3DDevice8Impl_GetDeviceCaps,
2392     IDirect3DDevice8Impl_GetDisplayMode,
2393     IDirect3DDevice8Impl_GetCreationParameters,
2394     IDirect3DDevice8Impl_SetCursorProperties,
2395     IDirect3DDevice8Impl_SetCursorPosition,
2396     IDirect3DDevice8Impl_ShowCursor,
2397     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2398     IDirect3DDevice8Impl_Reset,
2399     IDirect3DDevice8Impl_Present,
2400     IDirect3DDevice8Impl_GetBackBuffer,
2401     IDirect3DDevice8Impl_GetRasterStatus,
2402     IDirect3DDevice8Impl_SetGammaRamp,
2403     IDirect3DDevice8Impl_GetGammaRamp,
2404     IDirect3DDevice8Impl_CreateTexture,
2405     IDirect3DDevice8Impl_CreateVolumeTexture,
2406     IDirect3DDevice8Impl_CreateCubeTexture,
2407     IDirect3DDevice8Impl_CreateVertexBuffer,
2408     IDirect3DDevice8Impl_CreateIndexBuffer,
2409     IDirect3DDevice8Impl_CreateRenderTarget,
2410     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2411     IDirect3DDevice8Impl_CreateImageSurface,
2412     IDirect3DDevice8Impl_CopyRects,
2413     IDirect3DDevice8Impl_UpdateTexture,
2414     IDirect3DDevice8Impl_GetFrontBuffer,
2415     IDirect3DDevice8Impl_SetRenderTarget,
2416     IDirect3DDevice8Impl_GetRenderTarget,
2417     IDirect3DDevice8Impl_GetDepthStencilSurface,
2418     IDirect3DDevice8Impl_BeginScene,
2419     IDirect3DDevice8Impl_EndScene,
2420     IDirect3DDevice8Impl_Clear,
2421     IDirect3DDevice8Impl_SetTransform,
2422     IDirect3DDevice8Impl_GetTransform,
2423     IDirect3DDevice8Impl_MultiplyTransform,
2424     IDirect3DDevice8Impl_SetViewport,
2425     IDirect3DDevice8Impl_GetViewport,
2426     IDirect3DDevice8Impl_SetMaterial,
2427     IDirect3DDevice8Impl_GetMaterial,
2428     IDirect3DDevice8Impl_SetLight,
2429     IDirect3DDevice8Impl_GetLight,
2430     IDirect3DDevice8Impl_LightEnable,
2431     IDirect3DDevice8Impl_GetLightEnable,
2432     IDirect3DDevice8Impl_SetClipPlane,
2433     IDirect3DDevice8Impl_GetClipPlane,
2434     IDirect3DDevice8Impl_SetRenderState,
2435     IDirect3DDevice8Impl_GetRenderState,
2436     IDirect3DDevice8Impl_BeginStateBlock,
2437     IDirect3DDevice8Impl_EndStateBlock,
2438     IDirect3DDevice8Impl_ApplyStateBlock,
2439     IDirect3DDevice8Impl_CaptureStateBlock,
2440     IDirect3DDevice8Impl_DeleteStateBlock,
2441     IDirect3DDevice8Impl_CreateStateBlock,
2442     IDirect3DDevice8Impl_SetClipStatus,
2443     IDirect3DDevice8Impl_GetClipStatus,
2444     IDirect3DDevice8Impl_GetTexture,
2445     IDirect3DDevice8Impl_SetTexture,
2446     IDirect3DDevice8Impl_GetTextureStageState,
2447     IDirect3DDevice8Impl_SetTextureStageState,
2448     IDirect3DDevice8Impl_ValidateDevice,
2449     IDirect3DDevice8Impl_GetInfo,
2450     IDirect3DDevice8Impl_SetPaletteEntries,
2451     IDirect3DDevice8Impl_GetPaletteEntries,
2452     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2453     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2454     IDirect3DDevice8Impl_DrawPrimitive,
2455     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2456     IDirect3DDevice8Impl_DrawPrimitiveUP,
2457     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2458     IDirect3DDevice8Impl_ProcessVertices,
2459     IDirect3DDevice8Impl_CreateVertexShader,
2460     IDirect3DDevice8Impl_SetVertexShader,
2461     IDirect3DDevice8Impl_GetVertexShader,
2462     IDirect3DDevice8Impl_DeleteVertexShader,
2463     IDirect3DDevice8Impl_SetVertexShaderConstant,
2464     IDirect3DDevice8Impl_GetVertexShaderConstant,
2465     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2466     IDirect3DDevice8Impl_GetVertexShaderFunction,
2467     IDirect3DDevice8Impl_SetStreamSource,
2468     IDirect3DDevice8Impl_GetStreamSource,
2469     IDirect3DDevice8Impl_SetIndices,
2470     IDirect3DDevice8Impl_GetIndices,
2471     IDirect3DDevice8Impl_CreatePixelShader,
2472     IDirect3DDevice8Impl_SetPixelShader,
2473     IDirect3DDevice8Impl_GetPixelShader,
2474     IDirect3DDevice8Impl_DeletePixelShader,
2475     IDirect3DDevice8Impl_SetPixelShaderConstant,
2476     IDirect3DDevice8Impl_GetPixelShaderConstant,
2477     IDirect3DDevice8Impl_GetPixelShaderFunction,
2478     IDirect3DDevice8Impl_DrawRectPatch,
2479     IDirect3DDevice8Impl_DrawTriPatch,
2480     IDirect3DDevice8Impl_DeletePatch
2481 };
2482
2483 /* IWineD3DDeviceParent IUnknown methods */
2484
2485 static inline struct IDirect3DDevice8Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2486 {
2487     return (struct IDirect3DDevice8Impl *)((char*)iface
2488             - FIELD_OFFSET(struct IDirect3DDevice8Impl, device_parent_vtbl));
2489 }
2490
2491 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
2492 {
2493     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2494     return IDirect3DDevice8Impl_QueryInterface((IDirect3DDevice8 *)This, riid, object);
2495 }
2496
2497 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2498 {
2499     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2500     return IDirect3DDevice8Impl_AddRef((IDirect3DDevice8 *)This);
2501 }
2502
2503 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2504 {
2505     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2506     return IDirect3DDevice8Impl_Release((IDirect3DDevice8 *)This);
2507 }
2508
2509 /* IWineD3DDeviceParent methods */
2510
2511 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2512 {
2513     TRACE("iface %p, device %p\n", iface, device);
2514 }
2515
2516 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2517         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
2518         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2519 {
2520     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2521     IDirect3DSurface8Impl *d3d_surface;
2522     BOOL lockable = TRUE;
2523     HRESULT hr;
2524
2525     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
2526             "\tpool %#x, level %u, face %u, surface %p\n",
2527             iface, superior, width, height, format, usage, pool, level, face, surface);
2528
2529
2530     if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2531
2532     hr = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)This, width, height,
2533             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2534             (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2535     if (FAILED(hr))
2536     {
2537         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2538         return hr;
2539     }
2540
2541     *surface = d3d_surface->wineD3DSurface;
2542     IWineD3DSurface_AddRef(*surface);
2543
2544     d3d_surface->container = superior;
2545     IUnknown_Release(d3d_surface->parentDevice);
2546     d3d_surface->parentDevice = NULL;
2547
2548     IDirect3DSurface8_Release((IDirect3DSurface8 *)d3d_surface);
2549     d3d_surface->forwardReference = superior;
2550
2551     return hr;
2552 }
2553
2554 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2555         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2556         DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
2557 {
2558     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2559     IDirect3DSurface8Impl *d3d_surface;
2560     HRESULT hr;
2561
2562     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2563             "\tmultisample_quality %u, lockable %u, surface %p\n",
2564             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
2565
2566     hr = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)This, width, height,
2567             d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2568     if (FAILED(hr))
2569     {
2570         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2571         return hr;
2572     }
2573
2574     *surface = d3d_surface->wineD3DSurface;
2575     IWineD3DSurface_AddRef(*surface);
2576
2577     d3d_surface->container = (IUnknown *)This;
2578     /* Implicit surfaces are created with an refcount of 0 */
2579     IUnknown_Release((IUnknown *)d3d_surface);
2580
2581     return hr;
2582 }
2583
2584 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2585         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2586         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2587 {
2588     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2589     IDirect3DSurface8Impl *d3d_surface;
2590     HRESULT hr;
2591
2592     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2593             "\tmultisample_quality %u, discard %u, surface %p\n",
2594             iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
2595
2596     hr = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)This, width, height,
2597             d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2598     if (FAILED(hr))
2599     {
2600         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2601         return hr;
2602     }
2603
2604     *surface = d3d_surface->wineD3DSurface;
2605     IWineD3DSurface_AddRef(*surface);
2606
2607     d3d_surface->container = (IUnknown *)This;
2608     /* Implicit surfaces are created with an refcount of 0 */
2609     IUnknown_Release((IUnknown *)d3d_surface);
2610
2611     return hr;
2612 }
2613
2614 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2615         IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
2616         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2617 {
2618     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2619     IDirect3DVolume8Impl *object;
2620     HRESULT hr;
2621
2622     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2623             iface, superior, width, height, depth, format, pool, usage, volume);
2624
2625     /* Allocate the storage for the device */
2626     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2627     if (!object)
2628     {
2629         FIXME("Allocation of memory failed\n");
2630         *volume = NULL;
2631         return D3DERR_OUTOFVIDEOMEMORY;
2632     }
2633
2634     hr = volume_init(object, This, width, height, depth, usage, format, pool);
2635     if (FAILED(hr))
2636     {
2637         WARN("Failed to initialize volume, hr %#x.\n", hr);
2638         HeapFree(GetProcessHeap(), 0, object);
2639         return hr;
2640     }
2641
2642     *volume = object->wineD3DVolume;
2643     IWineD3DVolume_AddRef(*volume);
2644     IDirect3DVolume8_Release((IDirect3DVolume8 *)object);
2645
2646     object->container = superior;
2647     object->forwardReference = superior;
2648
2649     TRACE("(%p) Created volume %p\n", iface, object);
2650
2651     return hr;
2652 }
2653
2654 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2655         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2656 {
2657     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2658     IDirect3DSwapChain8Impl *d3d_swapchain;
2659     D3DPRESENT_PARAMETERS local_parameters;
2660     HRESULT hr;
2661
2662     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2663
2664     /* Copy the presentation parameters */
2665     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2666     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2667     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2668     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2669     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2670     local_parameters.SwapEffect = present_parameters->SwapEffect;
2671     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2672     local_parameters.Windowed = present_parameters->Windowed;
2673     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2674     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2675     local_parameters.Flags = present_parameters->Flags;
2676     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2677     local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
2678
2679     hr = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)This,
2680             &local_parameters, (IDirect3DSwapChain8 **)&d3d_swapchain);
2681     if (FAILED(hr))
2682     {
2683         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2684         *swapchain = NULL;
2685         return hr;
2686     }
2687
2688     *swapchain = d3d_swapchain->wineD3DSwapChain;
2689     IUnknown_Release(d3d_swapchain->parentDevice);
2690     d3d_swapchain->parentDevice = NULL;
2691
2692     /* Copy back the presentation parameters */
2693     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2694     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2695     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2696     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2697     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2698     present_parameters->SwapEffect = local_parameters.SwapEffect;
2699     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2700     present_parameters->Windowed = local_parameters.Windowed;
2701     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2702     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2703     present_parameters->Flags = local_parameters.Flags;
2704     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2705     present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
2706
2707     return hr;
2708 }
2709
2710 const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
2711 {
2712     /* IUnknown methods */
2713     device_parent_QueryInterface,
2714     device_parent_AddRef,
2715     device_parent_Release,
2716     /* IWineD3DDeviceParent methods */
2717     device_parent_WineD3DDeviceCreated,
2718     device_parent_CreateSurface,
2719     device_parent_CreateRenderTarget,
2720     device_parent_CreateDepthStencilSurface,
2721     device_parent_CreateVolume,
2722     device_parent_CreateSwapChain,
2723 };