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