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