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