d3d8: Add a separate function for vertex shader initialization.
[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_R8G8B8: return D3DFMT_R8G8B8;
50         case WINED3DFMT_A8R8G8B8: return D3DFMT_A8R8G8B8;
51         case WINED3DFMT_X8R8G8B8: return D3DFMT_X8R8G8B8;
52         case WINED3DFMT_R5G6B5: return D3DFMT_R5G6B5;
53         case WINED3DFMT_X1R5G5B5: return D3DFMT_X1R5G5B5;
54         case WINED3DFMT_A1R5G5B5: return D3DFMT_A1R5G5B5;
55         case WINED3DFMT_A4R4G4B4: return D3DFMT_A4R4G4B4;
56         case WINED3DFMT_R3G3B2: return D3DFMT_R3G3B2;
57         case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
58         case WINED3DFMT_A8R3G3B2: return D3DFMT_A8R3G3B2;
59         case WINED3DFMT_X4R4G4B4: return D3DFMT_X4R4G4B4;
60         case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
61         case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
62         case WINED3DFMT_A8P8: return D3DFMT_A8P8;
63         case WINED3DFMT_P8: return D3DFMT_P8;
64         case WINED3DFMT_L8: return D3DFMT_L8;
65         case WINED3DFMT_A8L8: return D3DFMT_A8L8;
66         case WINED3DFMT_A4L4: return D3DFMT_A4L4;
67         case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68         case WINED3DFMT_L6V5U5: return D3DFMT_L6V5U5;
69         case WINED3DFMT_X8L8V8U8: return D3DFMT_X8L8V8U8;
70         case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71         case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72         case WINED3DFMT_W11V11U10: return D3DFMT_W11V11U10;
73         case WINED3DFMT_A2W10V10U10: return D3DFMT_A2W10V10U10;
74         case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
75         case WINED3DFMT_D32: return D3DFMT_D32;
76         case WINED3DFMT_D15S1: return D3DFMT_D15S1;
77         case WINED3DFMT_D24S8: return D3DFMT_D24S8;
78         case WINED3DFMT_D24X8: return D3DFMT_D24X8;
79         case WINED3DFMT_D24X4S4: 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_R8G8B8;
101         case D3DFMT_A8R8G8B8: return WINED3DFMT_A8R8G8B8;
102         case D3DFMT_X8R8G8B8: return WINED3DFMT_X8R8G8B8;
103         case D3DFMT_R5G6B5: return WINED3DFMT_R5G6B5;
104         case D3DFMT_X1R5G5B5: return WINED3DFMT_X1R5G5B5;
105         case D3DFMT_A1R5G5B5: return WINED3DFMT_A1R5G5B5;
106         case D3DFMT_A4R4G4B4: return WINED3DFMT_A4R4G4B4;
107         case D3DFMT_R3G3B2: return WINED3DFMT_R3G3B2;
108         case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
109         case D3DFMT_A8R3G3B2: return WINED3DFMT_A8R3G3B2;
110         case D3DFMT_X4R4G4B4: return WINED3DFMT_X4R4G4B4;
111         case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
112         case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
113         case D3DFMT_A8P8: return WINED3DFMT_A8P8;
114         case D3DFMT_P8: return WINED3DFMT_P8;
115         case D3DFMT_L8: return WINED3DFMT_L8;
116         case D3DFMT_A8L8: return WINED3DFMT_A8L8;
117         case D3DFMT_A4L4: return WINED3DFMT_A4L4;
118         case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
119         case D3DFMT_L6V5U5: return WINED3DFMT_L6V5U5;
120         case D3DFMT_X8L8V8U8: return WINED3DFMT_X8L8V8U8;
121         case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
122         case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
123         case D3DFMT_W11V11U10: return WINED3DFMT_W11V11U10;
124         case D3DFMT_A2W10V10U10: return WINED3DFMT_A2W10V10U10;
125         case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
126         case D3DFMT_D32: return WINED3DFMT_D32;
127         case D3DFMT_D15S1: return WINED3DFMT_D15S1;
128         case D3DFMT_D24S8: return WINED3DFMT_D24S8;
129         case D3DFMT_D24X8: return WINED3DFMT_D24X8;
130         case D3DFMT_D24X4S4: return WINED3DFMT_D24X4S4;
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 = NULL;
1475     HRESULT rc = D3D_OK;
1476
1477     TRACE("(%p) Relay\n" , This);
1478
1479     if(ppTexture == NULL){
1480         return D3DERR_INVALIDCALL;
1481     }
1482
1483     wined3d_mutex_lock();
1484     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1485     if (rc == D3D_OK && NULL != retTexture) {
1486         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1487         IWineD3DBaseTexture_Release(retTexture);
1488     } else {
1489         FIXME("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1490         *ppTexture = NULL;
1491     }
1492     wined3d_mutex_unlock();
1493
1494     return rc;
1495 }
1496
1497 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(LPDIRECT3DDEVICE8 iface, DWORD Stage, IDirect3DBaseTexture8* pTexture) {
1498     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1499     HRESULT hr;
1500     TRACE("(%p) Relay %d %p\n" , This, Stage, pTexture);
1501
1502     wined3d_mutex_lock();
1503     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1504                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1505     wined3d_mutex_unlock();
1506
1507     return hr;
1508 }
1509
1510 static const struct tss_lookup
1511 {
1512     BOOL sampler_state;
1513     DWORD state;
1514 }
1515 tss_lookup[] =
1516 {
1517     {FALSE, WINED3DTSS_FORCE_DWORD},            /*  0, unused */
1518     {FALSE, WINED3DTSS_COLOROP},                /*  1, D3DTSS_COLOROP */
1519     {FALSE, WINED3DTSS_COLORARG1},              /*  2, D3DTSS_COLORARG1 */
1520     {FALSE, WINED3DTSS_COLORARG2},              /*  3, D3DTSS_COLORARG2 */
1521     {FALSE, WINED3DTSS_ALPHAOP},                /*  4, D3DTSS_ALPHAOP */
1522     {FALSE, WINED3DTSS_ALPHAARG1},              /*  5, D3DTSS_ALPHAARG1 */
1523     {FALSE, WINED3DTSS_ALPHAARG2},              /*  6, D3DTSS_ALPHAARG2 */
1524     {FALSE, WINED3DTSS_BUMPENVMAT00},           /*  7, D3DTSS_BUMPENVMAT00 */
1525     {FALSE, WINED3DTSS_BUMPENVMAT01},           /*  8, D3DTSS_BUMPENVMAT01 */
1526     {FALSE, WINED3DTSS_BUMPENVMAT10},           /*  9, D3DTSS_BUMPENVMAT10 */
1527     {FALSE, WINED3DTSS_BUMPENVMAT11},           /* 10, D3DTSS_BUMPENVMAT11 */
1528     {FALSE, WINED3DTSS_TEXCOORDINDEX},          /* 11, D3DTSS_TEXCOORDINDEX */
1529     {FALSE, WINED3DTSS_FORCE_DWORD},            /* 12, unused */
1530     {TRUE,  WINED3DSAMP_ADDRESSU},              /* 13, D3DTSS_ADDRESSU */
1531     {TRUE,  WINED3DSAMP_ADDRESSV},              /* 14, D3DTSS_ADDRESSV */
1532     {TRUE,  WINED3DSAMP_BORDERCOLOR},           /* 15, D3DTSS_BORDERCOLOR */
1533     {TRUE,  WINED3DSAMP_MAGFILTER},             /* 16, D3DTSS_MAGFILTER */
1534     {TRUE,  WINED3DSAMP_MINFILTER},             /* 17, D3DTSS_MINFILTER */
1535     {TRUE,  WINED3DSAMP_MIPFILTER},             /* 18, D3DTSS_MIPFILTER */
1536     {TRUE,  WINED3DSAMP_MIPMAPLODBIAS},         /* 19, D3DTSS_MIPMAPLODBIAS */
1537     {TRUE,  WINED3DSAMP_MAXMIPLEVEL},           /* 20, D3DTSS_MAXMIPLEVEL */
1538     {TRUE,  WINED3DSAMP_MAXANISOTROPY},         /* 21, D3DTSS_MAXANISOTROPY */
1539     {FALSE, WINED3DTSS_BUMPENVLSCALE},          /* 22, D3DTSS_BUMPENVLSCALE */
1540     {FALSE, WINED3DTSS_BUMPENVLOFFSET},         /* 23, D3DTSS_BUMPENVLOFFSET */
1541     {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS},  /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1542     {TRUE,  WINED3DSAMP_ADDRESSW},              /* 25, D3DTSS_ADDRESSW */
1543     {FALSE, WINED3DTSS_COLORARG0},              /* 26, D3DTSS_COLORARG0 */
1544     {FALSE, WINED3DTSS_ALPHAARG0},              /* 27, D3DTSS_ALPHAARG0 */
1545     {FALSE, WINED3DTSS_RESULTARG},              /* 28, D3DTSS_RESULTARG */
1546 };
1547
1548 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage,D3DTEXTURESTAGESTATETYPE Type,DWORD* pValue) {
1549     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1550     const struct tss_lookup *l = &tss_lookup[Type];
1551     HRESULT hr;
1552     TRACE("(%p) Relay\n" , This);
1553
1554     wined3d_mutex_lock();
1555     if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1556     else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1557     wined3d_mutex_unlock();
1558
1559     return hr;
1560 }
1561
1562 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(LPDIRECT3DDEVICE8 iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1563     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1564     const struct tss_lookup *l = &tss_lookup[Type];
1565     HRESULT hr;
1566     TRACE("(%p) Relay\n" , This);
1567
1568     wined3d_mutex_lock();
1569     if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1570     else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1571     wined3d_mutex_unlock();
1572
1573     return hr;
1574 }
1575
1576 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(LPDIRECT3DDEVICE8 iface, DWORD* pNumPasses) {
1577     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1578     HRESULT hr;
1579     TRACE("(%p) Relay\n" , This);
1580
1581     wined3d_mutex_lock();
1582     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1583     wined3d_mutex_unlock();
1584
1585     return hr;
1586 }
1587
1588 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(LPDIRECT3DDEVICE8 iface, DWORD DevInfoID, void* pDevInfoStruct, DWORD DevInfoStructSize) {
1589     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1590     FIXME("(%p) : stub\n", This);
1591     return D3D_OK;
1592 }
1593
1594 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, CONST PALETTEENTRY* pEntries) {
1595     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1596     HRESULT hr;
1597     TRACE("(%p) Relay\n" , This);
1598
1599     wined3d_mutex_lock();
1600     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1601     wined3d_mutex_unlock();
1602
1603     return hr;
1604 }
1605
1606 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1607     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1608     HRESULT hr;
1609     TRACE("(%p) Relay\n" , This);
1610
1611     wined3d_mutex_lock();
1612     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1613     wined3d_mutex_unlock();
1614
1615     return hr;
1616 }
1617
1618 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT PaletteNumber) {
1619     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1620     HRESULT hr;
1621     TRACE("(%p) Relay\n" , This);
1622
1623     wined3d_mutex_lock();
1624     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1625     wined3d_mutex_unlock();
1626
1627     return hr;
1628 }
1629
1630 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE8 iface, UINT *PaletteNumber) {
1631     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1632     HRESULT hr;
1633     TRACE("(%p) Relay\n" , This);
1634
1635     wined3d_mutex_lock();
1636     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1637     wined3d_mutex_unlock();
1638
1639     return hr;
1640 }
1641
1642 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface, D3DPRIMITIVETYPE PrimitiveType,
1643         UINT StartVertex, UINT PrimitiveCount)
1644 {
1645     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1646     HRESULT hr;
1647     TRACE("(%p) Relay\n" , This);
1648
1649     wined3d_mutex_lock();
1650     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1651     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1652             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1653     wined3d_mutex_unlock();
1654
1655     return hr;
1656 }
1657
1658 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,
1659                                                            UINT MinVertexIndex,UINT NumVertices,UINT startIndex,UINT primCount) {
1660     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1661     HRESULT hr;
1662     TRACE("(%p) Relay\n" , This);
1663
1664     wined3d_mutex_lock();
1665     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1666     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1667             vertex_count_from_primitive_count(PrimitiveType, primCount));
1668     wined3d_mutex_unlock();
1669
1670     return hr;
1671 }
1672
1673 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT PrimitiveCount,CONST void* pVertexStreamZeroData,UINT VertexStreamZeroStride) {
1674     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1675     HRESULT hr;
1676     TRACE("(%p) Relay\n" , This);
1677
1678     wined3d_mutex_lock();
1679     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1680     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1681             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1682             pVertexStreamZeroData, VertexStreamZeroStride);
1683     wined3d_mutex_unlock();
1684
1685     return hr;
1686 }
1687
1688 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE8 iface, D3DPRIMITIVETYPE PrimitiveType,UINT MinVertexIndex,
1689                                                              UINT NumVertexIndices,UINT PrimitiveCount,CONST void* pIndexData,
1690                                                              D3DFORMAT IndexDataFormat,CONST void* pVertexStreamZeroData,
1691                                                              UINT VertexStreamZeroStride) {
1692     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1693     HRESULT hr;
1694     TRACE("(%p) Relay\n" , This);
1695
1696     wined3d_mutex_lock();
1697     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1698     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1699             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1700             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1701     wined3d_mutex_unlock();
1702
1703     return hr;
1704 }
1705
1706 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(LPDIRECT3DDEVICE8 iface, UINT SrcStartIndex,UINT DestIndex,UINT VertexCount,IDirect3DVertexBuffer8* pDestBuffer,DWORD Flags) {
1707     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1708     HRESULT hr;
1709     IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
1710     TRACE("(%p) Relay\n" , This);
1711
1712     wined3d_mutex_lock();
1713     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1714     wined3d_mutex_unlock();
1715
1716     return hr;
1717 }
1718
1719 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
1720         const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
1721 {
1722     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1723     IDirect3DVertexShader8Impl *object;
1724     DWORD shader_handle;
1725     DWORD handle;
1726     HRESULT hr;
1727
1728     TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
1729             iface, declaration, byte_code, shader, usage);
1730
1731     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1732     if (!object)
1733     {
1734         ERR("Failed to allocate vertex shader memory.\n");
1735         *shader = 0;
1736         return E_OUTOFMEMORY;
1737     }
1738
1739     wined3d_mutex_lock();
1740     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
1741     wined3d_mutex_unlock();
1742     if (handle == D3D8_INVALID_HANDLE)
1743     {
1744         ERR("Failed to allocate vertex shader handle.\n");
1745         HeapFree(GetProcessHeap(), 0, object);
1746         *shader = 0;
1747         return E_OUTOFMEMORY;
1748     }
1749
1750     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
1751
1752     hr = vertexshader_init(object, This, declaration, byte_code, shader_handle, usage);
1753     if (FAILED(hr))
1754     {
1755         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
1756         wined3d_mutex_lock();
1757         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
1758         wined3d_mutex_unlock();
1759         HeapFree(GetProcessHeap(), 0, object);
1760         *shader = 0;
1761         return hr;
1762     }
1763
1764     TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
1765     *shader = shader_handle;
1766
1767     return D3D_OK;
1768 }
1769
1770 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1771 {
1772     IDirect3DVertexDeclaration8Impl *d3d8_declaration;
1773     HRESULT hr;
1774     int p, low, high; /* deliberately signed */
1775     struct FvfToDecl *convertedDecls = This->decls;
1776
1777     TRACE("Searching for declaration for fvf %08x... ", fvf);
1778
1779     low = 0;
1780     high = This->numConvertedDecls - 1;
1781     while(low <= high) {
1782         p = (low + high) >> 1;
1783         TRACE("%d ", p);
1784         if(convertedDecls[p].fvf == fvf) {
1785             TRACE("found %p\n", convertedDecls[p].decl);
1786             return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
1787         } else if(convertedDecls[p].fvf < fvf) {
1788             low = p + 1;
1789         } else {
1790             high = p - 1;
1791         }
1792     }
1793     TRACE("not found. Creating and inserting at position %d.\n", low);
1794
1795     d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
1796     if (!d3d8_declaration)
1797     {
1798         ERR("Memory allocation failed.\n");
1799         return NULL;
1800     }
1801
1802     hr = vertexdeclaration_init_fvf(d3d8_declaration, This, fvf);
1803     if (FAILED(hr))
1804     {
1805         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
1806         HeapFree(GetProcessHeap(), 0, d3d8_declaration);
1807         return NULL;
1808     }
1809
1810     if(This->declArraySize == This->numConvertedDecls) {
1811         int grow = This->declArraySize / 2;
1812         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1813                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1814         if(!convertedDecls) {
1815             /* This will destroy it */
1816             IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
1817             return NULL;
1818         }
1819         This->decls = convertedDecls;
1820         This->declArraySize += grow;
1821     }
1822
1823     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
1824     convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
1825     convertedDecls[low].fvf = fvf;
1826     This->numConvertedDecls++;
1827
1828     TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
1829     return d3d8_declaration;
1830 }
1831
1832 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1833     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1834     IDirect3DVertexShader8Impl *shader;
1835     HRESULT hr;
1836
1837     TRACE("(%p) : Relay\n", This);
1838
1839     if (VS_HIGHESTFIXEDFXF >= pShader) {
1840         TRACE("Setting FVF, %#x\n", pShader);
1841
1842         wined3d_mutex_lock();
1843         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1844                 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
1845         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
1846         wined3d_mutex_unlock();
1847
1848         return D3D_OK;
1849     }
1850
1851     TRACE("Setting shader\n");
1852
1853     wined3d_mutex_lock();
1854     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1855     if (!shader)
1856     {
1857         WARN("Invalid handle (%#x) passed.\n", pShader);
1858         wined3d_mutex_unlock();
1859
1860         return D3DERR_INVALIDCALL;
1861     }
1862
1863     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
1864             ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
1865     if (SUCCEEDED(hr)) hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wineD3DVertexShader);
1866     wined3d_mutex_unlock();
1867
1868     TRACE("Returning hr %#x\n", hr);
1869
1870     return hr;
1871 }
1872
1873 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
1874     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1875     IWineD3DVertexDeclaration *wined3d_declaration;
1876     IDirect3DVertexDeclaration8 *d3d8_declaration;
1877     HRESULT hrc;
1878
1879     TRACE("(%p) : Relay  device@%p\n", This, This->WineD3DDevice);
1880
1881     wined3d_mutex_lock();
1882     hrc = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
1883     if (FAILED(hrc))
1884     {
1885         wined3d_mutex_unlock();
1886         WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
1887                 This, hrc, This->WineD3DDevice);
1888         return hrc;
1889     }
1890
1891     if (!wined3d_declaration)
1892     {
1893         wined3d_mutex_unlock();
1894         *ppShader = 0;
1895         return D3D_OK;
1896     }
1897
1898     hrc = IWineD3DVertexDeclaration_GetParent(wined3d_declaration, (IUnknown **)&d3d8_declaration);
1899     IWineD3DVertexDeclaration_Release(wined3d_declaration);
1900     wined3d_mutex_unlock();
1901     if (SUCCEEDED(hrc))
1902     {
1903         *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
1904         IDirect3DVertexDeclaration8_Release(d3d8_declaration);
1905     }
1906
1907     TRACE("(%p) : returning %#x\n", This, *ppShader);
1908
1909     return hrc;
1910 }
1911
1912 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
1913     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1914     IDirect3DVertexShader8Impl *shader;
1915     IWineD3DVertexShader *cur = NULL;
1916
1917     TRACE("(%p) : pShader %#x\n", This, pShader);
1918
1919     wined3d_mutex_lock();
1920     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1921     if (!shader)
1922     {
1923         WARN("Invalid handle (%#x) passed.\n", pShader);
1924         wined3d_mutex_unlock();
1925
1926         return D3DERR_INVALIDCALL;
1927     }
1928
1929     IWineD3DDevice_GetVertexShader(This->WineD3DDevice, &cur);
1930
1931     if (cur)
1932     {
1933         if (cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
1934         IWineD3DVertexShader_Release(cur);
1935     }
1936
1937     wined3d_mutex_unlock();
1938
1939     if (IUnknown_Release((IUnknown *)shader))
1940     {
1941         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
1942     }
1943
1944     return D3D_OK;
1945 }
1946
1947 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
1948     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1949     HRESULT hr;
1950     TRACE("(%p) : Relay\n", This);
1951
1952     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
1953         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
1954              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
1955         return D3DERR_INVALIDCALL;
1956     }
1957
1958     wined3d_mutex_lock();
1959     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
1960     wined3d_mutex_unlock();
1961
1962     return hr;
1963 }
1964
1965 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
1966     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1967     HRESULT hr;
1968     TRACE("(%p) : Relay\n", This);
1969
1970     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
1971         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
1972              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
1973         return D3DERR_INVALIDCALL;
1974     }
1975
1976     wined3d_mutex_lock();
1977     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
1978     wined3d_mutex_unlock();
1979
1980     return hr;
1981 }
1982
1983 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
1984     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
1985     IDirect3DVertexDeclaration8Impl *declaration;
1986     IDirect3DVertexShader8Impl *shader;
1987
1988     TRACE("(%p) : pVertexShader 0x%08x, pData %p, *pSizeOfData %u\n", This, pVertexShader, pData, *pSizeOfData);
1989
1990     wined3d_mutex_lock();
1991     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
1992     wined3d_mutex_unlock();
1993
1994     if (!shader)
1995     {
1996         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
1997         return D3DERR_INVALIDCALL;
1998     }
1999     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2000
2001     /* If pData is NULL, we just return the required size of the buffer. */
2002     if (!pData) {
2003         *pSizeOfData = declaration->elements_size;
2004         return D3D_OK;
2005     }
2006
2007     /* MSDN claims that if *pSizeOfData is smaller than the required size
2008      * we should write the required size and return D3DERR_MOREDATA.
2009      * That's not actually true. */
2010     if (*pSizeOfData < declaration->elements_size) {
2011         return D3DERR_INVALIDCALL;
2012     }
2013
2014     CopyMemory(pData, declaration->elements, declaration->elements_size);
2015
2016     return D3D_OK;
2017 }
2018
2019 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pVertexShader, void* pData, DWORD* pSizeOfData) {
2020     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2021     IDirect3DVertexShader8Impl *shader = NULL;
2022     HRESULT hr;
2023
2024     TRACE("(%p) : pVertexShader %#x, pData %p, pSizeOfData %p\n", This, pVertexShader, pData, pSizeOfData);
2025
2026     wined3d_mutex_lock();
2027     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2028     if (!shader)
2029     {
2030         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2031         wined3d_mutex_unlock();
2032
2033         return D3DERR_INVALIDCALL;
2034     }
2035
2036     if (!shader->wineD3DVertexShader)
2037     {
2038         wined3d_mutex_unlock();
2039         *pSizeOfData = 0;
2040         return D3D_OK;
2041     }
2042
2043     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, pSizeOfData);
2044     wined3d_mutex_unlock();
2045
2046     return hr;
2047 }
2048
2049 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8* pIndexData, UINT baseVertexIndex) {
2050     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2051     HRESULT hr;
2052     IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2053     TRACE("(%p) Relay\n", This);
2054
2055     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2056      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2057      * vertex buffers can't be created to address them with an index that requires the 32nd bit
2058      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2059      * problem)
2060      */
2061     wined3d_mutex_lock();
2062     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2063     hr = IWineD3DDevice_SetIndices(This->WineD3DDevice,
2064             ib ? ib->wineD3DIndexBuffer : NULL,
2065             ib ? ib->format : WINED3DFMT_UNKNOWN);
2066     wined3d_mutex_unlock();
2067
2068     return hr;
2069 }
2070
2071 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(LPDIRECT3DDEVICE8 iface, IDirect3DIndexBuffer8** ppIndexData,UINT* pBaseVertexIndex) {
2072     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2073     IWineD3DBuffer *retIndexData = NULL;
2074     HRESULT rc = D3D_OK;
2075
2076     TRACE("(%p) Relay\n", This);
2077
2078     if(ppIndexData == NULL){
2079         return D3DERR_INVALIDCALL;
2080     }
2081
2082     /* The case from UINT to INT is safe because d3d8 will never set negative values */
2083     wined3d_mutex_lock();
2084     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2085     rc = IWineD3DDevice_GetIndices(This->WineD3DDevice, &retIndexData);
2086     if (SUCCEEDED(rc) && retIndexData) {
2087         IWineD3DBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
2088         IWineD3DBuffer_Release(retIndexData);
2089     } else {
2090         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
2091         *ppIndexData = NULL;
2092     }
2093     wined3d_mutex_unlock();
2094
2095     return rc;
2096 }
2097 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(LPDIRECT3DDEVICE8 iface, CONST DWORD* pFunction, DWORD* ppShader) {
2098     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2099     IDirect3DPixelShader8Impl *object;
2100     DWORD handle;
2101     HRESULT hr;
2102
2103     TRACE("(%p) : pFunction(%p), ppShader(%p)\n", This, pFunction, ppShader);
2104
2105     if (NULL == ppShader) {
2106         TRACE("(%p) Invalid call\n", This);
2107         return D3DERR_INVALIDCALL;
2108     }
2109
2110     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2111     if (!object)
2112     {
2113         ERR("Failed to allocate memmory.\n");
2114         return E_OUTOFMEMORY;
2115     }
2116
2117     object->ref    = 1;
2118     object->lpVtbl = &Direct3DPixelShader8_Vtbl;
2119
2120     wined3d_mutex_lock();
2121     hr = IWineD3DDevice_CreatePixelShader(This->WineD3DDevice, pFunction,
2122             NULL, &object->wineD3DPixelShader, (IUnknown *)object);
2123     if (FAILED(hr))
2124     {
2125         wined3d_mutex_unlock();
2126         FIXME("(%p) call to IWineD3DDevice_CreatePixelShader failed\n", This);
2127         HeapFree(GetProcessHeap(), 0 , object);
2128         *ppShader = 0;
2129         return hr;
2130     }
2131
2132     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2133     wined3d_mutex_unlock();
2134
2135     if (handle == D3D8_INVALID_HANDLE)
2136     {
2137         ERR("Failed to allocate shader handle\n");
2138         IDirect3DVertexShader8_Release((IUnknown *)object);
2139         return E_OUTOFMEMORY;
2140     }
2141
2142     *ppShader = object->handle = handle + VS_HIGHESTFIXEDFXF + 1;
2143     TRACE("(%p) : returning %p (handle %#x)\n", This, object, *ppShader);
2144
2145     return hr;
2146 }
2147
2148 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2149     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2150     IDirect3DPixelShader8Impl *shader;
2151     HRESULT hr;
2152
2153     TRACE("(%p) : pShader %#x\n", This, pShader);
2154
2155     wined3d_mutex_lock();
2156
2157     if (!pShader)
2158     {
2159         hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2160         wined3d_mutex_unlock();
2161         return hr;
2162     }
2163
2164     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2165     if (!shader)
2166     {
2167         WARN("Invalid handle (%#x) passed.\n", pShader);
2168         wined3d_mutex_unlock();
2169         return D3DERR_INVALIDCALL;
2170     }
2171
2172     TRACE("(%p) : Setting shader %p\n", This, shader);
2173     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wineD3DPixelShader);
2174     wined3d_mutex_unlock();
2175
2176     return hr;
2177 }
2178
2179 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(LPDIRECT3DDEVICE8 iface, DWORD* ppShader) {
2180     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2181     IWineD3DPixelShader *object;
2182
2183     HRESULT hrc = D3D_OK;
2184     TRACE("(%p) Relay\n", This);
2185     if (NULL == ppShader) {
2186         TRACE("(%p) Invalid call\n", This);
2187         return D3DERR_INVALIDCALL;
2188     }
2189
2190     wined3d_mutex_lock();
2191     hrc = IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &object);
2192     if (D3D_OK == hrc && NULL != object) {
2193         IDirect3DPixelShader8Impl *d3d8_shader;
2194         hrc = IWineD3DPixelShader_GetParent(object, (IUnknown **)&d3d8_shader);
2195         IWineD3DPixelShader_Release(object);
2196         *ppShader = d3d8_shader->handle;
2197         IDirect3DPixelShader8_Release((IDirect3DPixelShader8 *)d3d8_shader);
2198     } else {
2199         *ppShader = 0;
2200     }
2201     wined3d_mutex_unlock();
2202
2203     TRACE("(%p) : returning %#x\n", This, *ppShader);
2204
2205     return hrc;
2206 }
2207
2208 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(LPDIRECT3DDEVICE8 iface, DWORD pShader) {
2209     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2210     IDirect3DPixelShader8Impl *shader;
2211     IWineD3DPixelShader *cur = NULL;
2212
2213     TRACE("(%p) : pShader %#x\n", This, pShader);
2214
2215     wined3d_mutex_lock();
2216
2217     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2218     if (!shader)
2219     {
2220         WARN("Invalid handle (%#x) passed.\n", pShader);
2221         wined3d_mutex_unlock();
2222         return D3D_OK;
2223     }
2224
2225     IWineD3DDevice_GetPixelShader(This->WineD3DDevice, &cur);
2226
2227     if (cur)
2228     {
2229         if (cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
2230         IWineD3DPixelShader_Release(cur);
2231     }
2232
2233     wined3d_mutex_unlock();
2234
2235     if (IUnknown_Release((IUnknown *)shader))
2236     {
2237         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2238     }
2239
2240     return D3D_OK;
2241 }
2242
2243 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, CONST void* pConstantData, DWORD ConstantCount) {
2244     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2245     HRESULT hr;
2246     TRACE("(%p) Relay\n", This);
2247
2248     wined3d_mutex_lock();
2249     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2250     wined3d_mutex_unlock();
2251
2252     return hr;
2253 }
2254
2255 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(LPDIRECT3DDEVICE8 iface, DWORD Register, void* pConstantData, DWORD ConstantCount) {
2256     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2257     HRESULT hr;
2258     TRACE("(%p) Relay\n", This);
2259
2260     wined3d_mutex_lock();
2261     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2262     wined3d_mutex_unlock();
2263
2264     return hr;
2265 }
2266
2267 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(LPDIRECT3DDEVICE8 iface, DWORD pPixelShader, void* pData, DWORD* pSizeOfData) {
2268     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2269     IDirect3DPixelShader8Impl *shader = NULL;
2270     HRESULT hr;
2271
2272     TRACE("(%p) : pPixelShader %#x, pData %p, pSizeOfData %p\n", This, pPixelShader, pData, pSizeOfData);
2273
2274     wined3d_mutex_lock();
2275     shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2276     if (!shader)
2277     {
2278         WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2279         wined3d_mutex_unlock();
2280
2281         return D3DERR_INVALIDCALL;
2282     }
2283
2284     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, pSizeOfData);
2285     wined3d_mutex_unlock();
2286
2287     return hr;
2288 }
2289
2290 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2291     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2292     HRESULT hr;
2293     TRACE("(%p) Relay\n", This);
2294
2295     wined3d_mutex_lock();
2296     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2297     wined3d_mutex_unlock();
2298
2299     return hr;
2300 }
2301
2302 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(LPDIRECT3DDEVICE8 iface, UINT Handle,CONST float* pNumSegs,CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2303     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2304     HRESULT hr;
2305     TRACE("(%p) Relay\n", This);
2306
2307     wined3d_mutex_lock();
2308     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2309     wined3d_mutex_unlock();
2310
2311     return hr;
2312 }
2313
2314 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(LPDIRECT3DDEVICE8 iface, UINT Handle) {
2315     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2316     HRESULT hr;
2317     TRACE("(%p) Relay\n", This);
2318
2319     wined3d_mutex_lock();
2320     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2321     wined3d_mutex_unlock();
2322
2323     return hr;
2324 }
2325
2326 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8* pStreamData,UINT Stride) {
2327     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2328     HRESULT hr;
2329     TRACE("(%p) Relay\n" , This);
2330
2331     wined3d_mutex_lock();
2332     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2333                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2334                                         0/* Offset in bytes */, Stride);
2335     wined3d_mutex_unlock();
2336
2337     return hr;
2338 }
2339
2340 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(LPDIRECT3DDEVICE8 iface, UINT StreamNumber,IDirect3DVertexBuffer8** pStream,UINT* pStride) {
2341     IDirect3DDevice8Impl *This = (IDirect3DDevice8Impl *)iface;
2342     IWineD3DBuffer *retStream = NULL;
2343     HRESULT rc = D3D_OK;
2344
2345     TRACE("(%p) Relay\n" , This);
2346
2347     if(pStream == NULL){
2348         return D3DERR_INVALIDCALL;
2349     }
2350
2351     wined3d_mutex_lock();
2352     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, 0 /* Offset in bytes */, pStride);
2353     if (rc == D3D_OK  && NULL != retStream) {
2354         IWineD3DBuffer_GetParent(retStream, (IUnknown **)pStream);
2355         IWineD3DBuffer_Release(retStream);
2356     }else{
2357         if (rc != D3D_OK){
2358             FIXME("Call to GetStreamSource failed %p\n",  pStride);
2359         }
2360         *pStream = NULL;
2361     }
2362     wined3d_mutex_unlock();
2363
2364     return rc;
2365 }
2366
2367
2368 const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2369 {
2370     IDirect3DDevice8Impl_QueryInterface,
2371     IDirect3DDevice8Impl_AddRef,
2372     IDirect3DDevice8Impl_Release,
2373     IDirect3DDevice8Impl_TestCooperativeLevel,
2374     IDirect3DDevice8Impl_GetAvailableTextureMem,
2375     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2376     IDirect3DDevice8Impl_GetDirect3D,
2377     IDirect3DDevice8Impl_GetDeviceCaps,
2378     IDirect3DDevice8Impl_GetDisplayMode,
2379     IDirect3DDevice8Impl_GetCreationParameters,
2380     IDirect3DDevice8Impl_SetCursorProperties,
2381     IDirect3DDevice8Impl_SetCursorPosition,
2382     IDirect3DDevice8Impl_ShowCursor,
2383     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2384     IDirect3DDevice8Impl_Reset,
2385     IDirect3DDevice8Impl_Present,
2386     IDirect3DDevice8Impl_GetBackBuffer,
2387     IDirect3DDevice8Impl_GetRasterStatus,
2388     IDirect3DDevice8Impl_SetGammaRamp,
2389     IDirect3DDevice8Impl_GetGammaRamp,
2390     IDirect3DDevice8Impl_CreateTexture,
2391     IDirect3DDevice8Impl_CreateVolumeTexture,
2392     IDirect3DDevice8Impl_CreateCubeTexture,
2393     IDirect3DDevice8Impl_CreateVertexBuffer,
2394     IDirect3DDevice8Impl_CreateIndexBuffer,
2395     IDirect3DDevice8Impl_CreateRenderTarget,
2396     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2397     IDirect3DDevice8Impl_CreateImageSurface,
2398     IDirect3DDevice8Impl_CopyRects,
2399     IDirect3DDevice8Impl_UpdateTexture,
2400     IDirect3DDevice8Impl_GetFrontBuffer,
2401     IDirect3DDevice8Impl_SetRenderTarget,
2402     IDirect3DDevice8Impl_GetRenderTarget,
2403     IDirect3DDevice8Impl_GetDepthStencilSurface,
2404     IDirect3DDevice8Impl_BeginScene,
2405     IDirect3DDevice8Impl_EndScene,
2406     IDirect3DDevice8Impl_Clear,
2407     IDirect3DDevice8Impl_SetTransform,
2408     IDirect3DDevice8Impl_GetTransform,
2409     IDirect3DDevice8Impl_MultiplyTransform,
2410     IDirect3DDevice8Impl_SetViewport,
2411     IDirect3DDevice8Impl_GetViewport,
2412     IDirect3DDevice8Impl_SetMaterial,
2413     IDirect3DDevice8Impl_GetMaterial,
2414     IDirect3DDevice8Impl_SetLight,
2415     IDirect3DDevice8Impl_GetLight,
2416     IDirect3DDevice8Impl_LightEnable,
2417     IDirect3DDevice8Impl_GetLightEnable,
2418     IDirect3DDevice8Impl_SetClipPlane,
2419     IDirect3DDevice8Impl_GetClipPlane,
2420     IDirect3DDevice8Impl_SetRenderState,
2421     IDirect3DDevice8Impl_GetRenderState,
2422     IDirect3DDevice8Impl_BeginStateBlock,
2423     IDirect3DDevice8Impl_EndStateBlock,
2424     IDirect3DDevice8Impl_ApplyStateBlock,
2425     IDirect3DDevice8Impl_CaptureStateBlock,
2426     IDirect3DDevice8Impl_DeleteStateBlock,
2427     IDirect3DDevice8Impl_CreateStateBlock,
2428     IDirect3DDevice8Impl_SetClipStatus,
2429     IDirect3DDevice8Impl_GetClipStatus,
2430     IDirect3DDevice8Impl_GetTexture,
2431     IDirect3DDevice8Impl_SetTexture,
2432     IDirect3DDevice8Impl_GetTextureStageState,
2433     IDirect3DDevice8Impl_SetTextureStageState,
2434     IDirect3DDevice8Impl_ValidateDevice,
2435     IDirect3DDevice8Impl_GetInfo,
2436     IDirect3DDevice8Impl_SetPaletteEntries,
2437     IDirect3DDevice8Impl_GetPaletteEntries,
2438     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2439     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2440     IDirect3DDevice8Impl_DrawPrimitive,
2441     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2442     IDirect3DDevice8Impl_DrawPrimitiveUP,
2443     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2444     IDirect3DDevice8Impl_ProcessVertices,
2445     IDirect3DDevice8Impl_CreateVertexShader,
2446     IDirect3DDevice8Impl_SetVertexShader,
2447     IDirect3DDevice8Impl_GetVertexShader,
2448     IDirect3DDevice8Impl_DeleteVertexShader,
2449     IDirect3DDevice8Impl_SetVertexShaderConstant,
2450     IDirect3DDevice8Impl_GetVertexShaderConstant,
2451     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2452     IDirect3DDevice8Impl_GetVertexShaderFunction,
2453     IDirect3DDevice8Impl_SetStreamSource,
2454     IDirect3DDevice8Impl_GetStreamSource,
2455     IDirect3DDevice8Impl_SetIndices,
2456     IDirect3DDevice8Impl_GetIndices,
2457     IDirect3DDevice8Impl_CreatePixelShader,
2458     IDirect3DDevice8Impl_SetPixelShader,
2459     IDirect3DDevice8Impl_GetPixelShader,
2460     IDirect3DDevice8Impl_DeletePixelShader,
2461     IDirect3DDevice8Impl_SetPixelShaderConstant,
2462     IDirect3DDevice8Impl_GetPixelShaderConstant,
2463     IDirect3DDevice8Impl_GetPixelShaderFunction,
2464     IDirect3DDevice8Impl_DrawRectPatch,
2465     IDirect3DDevice8Impl_DrawTriPatch,
2466     IDirect3DDevice8Impl_DeletePatch
2467 };
2468
2469 /* IWineD3DDeviceParent IUnknown methods */
2470
2471 static inline struct IDirect3DDevice8Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2472 {
2473     return (struct IDirect3DDevice8Impl *)((char*)iface
2474             - FIELD_OFFSET(struct IDirect3DDevice8Impl, device_parent_vtbl));
2475 }
2476
2477 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
2478 {
2479     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2480     return IDirect3DDevice8Impl_QueryInterface((IDirect3DDevice8 *)This, riid, object);
2481 }
2482
2483 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2484 {
2485     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2486     return IDirect3DDevice8Impl_AddRef((IDirect3DDevice8 *)This);
2487 }
2488
2489 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2490 {
2491     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2492     return IDirect3DDevice8Impl_Release((IDirect3DDevice8 *)This);
2493 }
2494
2495 /* IWineD3DDeviceParent methods */
2496
2497 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2498 {
2499     TRACE("iface %p, device %p\n", iface, device);
2500 }
2501
2502 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2503         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
2504         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2505 {
2506     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2507     IDirect3DSurface8Impl *d3d_surface;
2508     BOOL lockable = TRUE;
2509     HRESULT hr;
2510
2511     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
2512             "\tpool %#x, level %u, face %u, surface %p\n",
2513             iface, superior, width, height, format, usage, pool, level, face, surface);
2514
2515
2516     if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2517
2518     hr = IDirect3DDevice8Impl_CreateSurface((IDirect3DDevice8 *)This, width, height,
2519             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2520             (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2521     if (FAILED(hr))
2522     {
2523         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2524         return hr;
2525     }
2526
2527     *surface = d3d_surface->wineD3DSurface;
2528     IWineD3DSurface_AddRef(*surface);
2529
2530     d3d_surface->container = superior;
2531     IUnknown_Release(d3d_surface->parentDevice);
2532     d3d_surface->parentDevice = NULL;
2533
2534     IDirect3DSurface8_Release((IDirect3DSurface8 *)d3d_surface);
2535     d3d_surface->forwardReference = superior;
2536
2537     return hr;
2538 }
2539
2540 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2541         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2542         DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
2543 {
2544     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2545     IDirect3DSurface8Impl *d3d_surface;
2546     HRESULT hr;
2547
2548     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2549             "\tmultisample_quality %u, lockable %u, surface %p\n",
2550             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
2551
2552     hr = IDirect3DDevice8_CreateRenderTarget((IDirect3DDevice8 *)This, width, height,
2553             d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2554     if (FAILED(hr))
2555     {
2556         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2557         return hr;
2558     }
2559
2560     *surface = d3d_surface->wineD3DSurface;
2561     IWineD3DSurface_AddRef(*surface);
2562
2563     d3d_surface->container = (IUnknown *)This;
2564     /* Implicit surfaces are created with an refcount of 0 */
2565     IUnknown_Release((IUnknown *)d3d_surface);
2566
2567     return hr;
2568 }
2569
2570 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2571         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2572         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2573 {
2574     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2575     IDirect3DSurface8Impl *d3d_surface;
2576     HRESULT hr;
2577
2578     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2579             "\tmultisample_quality %u, discard %u, surface %p\n",
2580             iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
2581
2582     hr = IDirect3DDevice8_CreateDepthStencilSurface((IDirect3DDevice8 *)This, width, height,
2583             d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2584     if (FAILED(hr))
2585     {
2586         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2587         return hr;
2588     }
2589
2590     *surface = d3d_surface->wineD3DSurface;
2591     IWineD3DSurface_AddRef(*surface);
2592
2593     d3d_surface->container = (IUnknown *)This;
2594     /* Implicit surfaces are created with an refcount of 0 */
2595     IUnknown_Release((IUnknown *)d3d_surface);
2596
2597     return hr;
2598 }
2599
2600 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2601         IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
2602         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2603 {
2604     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2605     IDirect3DVolume8Impl *object;
2606     HRESULT hr;
2607
2608     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2609             iface, superior, width, height, depth, format, pool, usage, volume);
2610
2611     /* Allocate the storage for the device */
2612     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2613     if (!object)
2614     {
2615         FIXME("Allocation of memory failed\n");
2616         *volume = NULL;
2617         return D3DERR_OUTOFVIDEOMEMORY;
2618     }
2619
2620     hr = volume_init(object, This, width, height, depth, usage, format, pool);
2621     if (FAILED(hr))
2622     {
2623         WARN("Failed to initialize volume, hr %#x.\n", hr);
2624         HeapFree(GetProcessHeap(), 0, object);
2625         return hr;
2626     }
2627
2628     *volume = object->wineD3DVolume;
2629     IWineD3DVolume_AddRef(*volume);
2630     IDirect3DVolume8_Release((IDirect3DVolume8 *)object);
2631
2632     object->container = superior;
2633     object->forwardReference = superior;
2634
2635     TRACE("(%p) Created volume %p\n", iface, object);
2636
2637     return hr;
2638 }
2639
2640 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2641         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2642 {
2643     struct IDirect3DDevice8Impl *This = device_from_device_parent(iface);
2644     IDirect3DSwapChain8Impl *d3d_swapchain;
2645     D3DPRESENT_PARAMETERS local_parameters;
2646     HRESULT hr;
2647
2648     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2649
2650     /* Copy the presentation parameters */
2651     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2652     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2653     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2654     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2655     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2656     local_parameters.SwapEffect = present_parameters->SwapEffect;
2657     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2658     local_parameters.Windowed = present_parameters->Windowed;
2659     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2660     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2661     local_parameters.Flags = present_parameters->Flags;
2662     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2663     local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
2664
2665     hr = IDirect3DDevice8_CreateAdditionalSwapChain((IDirect3DDevice8 *)This,
2666             &local_parameters, (IDirect3DSwapChain8 **)&d3d_swapchain);
2667     if (FAILED(hr))
2668     {
2669         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2670         *swapchain = NULL;
2671         return hr;
2672     }
2673
2674     *swapchain = d3d_swapchain->wineD3DSwapChain;
2675     IUnknown_Release(d3d_swapchain->parentDevice);
2676     d3d_swapchain->parentDevice = NULL;
2677
2678     /* Copy back the presentation parameters */
2679     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2680     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2681     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2682     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2683     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2684     present_parameters->SwapEffect = local_parameters.SwapEffect;
2685     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2686     present_parameters->Windowed = local_parameters.Windowed;
2687     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2688     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2689     present_parameters->Flags = local_parameters.Flags;
2690     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2691     present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
2692
2693     return hr;
2694 }
2695
2696 const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
2697 {
2698     /* IUnknown methods */
2699     device_parent_QueryInterface,
2700     device_parent_AddRef,
2701     device_parent_Release,
2702     /* IWineD3DDeviceParent methods */
2703     device_parent_WineD3DDeviceCreated,
2704     device_parent_CreateSurface,
2705     device_parent_CreateRenderTarget,
2706     device_parent_CreateDepthStencilSurface,
2707     device_parent_CreateVolume,
2708     device_parent_CreateSwapChain,
2709 };