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