winealsa.drv: Move IAudioClock closer to its related 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 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(IDirect3DDevice8 *iface,
1470         D3DRENDERSTATETYPE State, DWORD Value)
1471 {
1472     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1473     HRESULT hr;
1474     union
1475     {
1476         DWORD d;
1477         float f;
1478     } wined3d_value;
1479
1480     TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1481
1482     wined3d_mutex_lock();
1483     switch (State)
1484     {
1485         case D3DRS_ZBIAS:
1486             wined3d_value.f = Value / -16.0f;
1487             hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, WINED3DRS_DEPTHBIAS, wined3d_value.d);
1488             break;
1489
1490         default:
1491             hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1492     }
1493     wined3d_mutex_unlock();
1494
1495     return hr;
1496 }
1497
1498 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(IDirect3DDevice8 *iface,
1499         D3DRENDERSTATETYPE State, DWORD *pValue)
1500 {
1501     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1502     HRESULT hr;
1503     union
1504     {
1505         DWORD d;
1506         float f;
1507     } wined3d_value;
1508
1509     TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1510
1511     wined3d_mutex_lock();
1512     switch (State)
1513     {
1514         case D3DRS_ZBIAS:
1515             hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, WINED3DRS_DEPTHBIAS, &wined3d_value.d);
1516             if (SUCCEEDED(hr)) *pValue = -wined3d_value.f * 16.0f;
1517             break;
1518
1519         default:
1520         hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1521     }
1522     wined3d_mutex_unlock();
1523
1524     return hr;
1525 }
1526
1527 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(IDirect3DDevice8 *iface)
1528 {
1529     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1530     HRESULT hr;
1531
1532     TRACE("iface %p.\n", iface);
1533
1534     wined3d_mutex_lock();
1535     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1536     wined3d_mutex_unlock();
1537
1538     return hr;
1539 }
1540
1541 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(IDirect3DDevice8 *iface, DWORD *pToken)
1542 {
1543     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1544     struct wined3d_stateblock *stateblock;
1545     HRESULT hr;
1546
1547     TRACE("iface %p, token %p.\n", iface, pToken);
1548
1549     /* Tell wineD3D to endstateblock before anything else (in case we run out
1550      * of memory later and cause locking problems)
1551      */
1552     wined3d_mutex_lock();
1553     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &stateblock);
1554     if (hr != D3D_OK) {
1555         WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1556         wined3d_mutex_unlock();
1557         return hr;
1558     }
1559
1560     *pToken = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1561     wined3d_mutex_unlock();
1562
1563     if (*pToken == D3D8_INVALID_HANDLE)
1564     {
1565         ERR("Failed to create a handle\n");
1566         wined3d_mutex_lock();
1567         wined3d_stateblock_decref(stateblock);
1568         wined3d_mutex_unlock();
1569         return E_FAIL;
1570     }
1571     ++*pToken;
1572
1573     TRACE("Returning %#x (%p).\n", *pToken, stateblock);
1574
1575     return hr;
1576 }
1577
1578 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1579 {
1580     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1581     struct wined3d_stateblock *stateblock;
1582     HRESULT hr;
1583
1584     TRACE("iface %p, token %#x.\n", iface, Token);
1585
1586     if (!Token) return D3D_OK;
1587
1588     wined3d_mutex_lock();
1589     stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1590     if (!stateblock)
1591     {
1592         WARN("Invalid handle (%#x) passed.\n", Token);
1593         wined3d_mutex_unlock();
1594         return D3DERR_INVALIDCALL;
1595     }
1596     hr = wined3d_stateblock_apply(stateblock);
1597     wined3d_mutex_unlock();
1598
1599     return hr;
1600 }
1601
1602 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1603 {
1604     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1605     struct wined3d_stateblock *stateblock;
1606     HRESULT hr;
1607
1608     TRACE("iface %p, token %#x.\n", iface, Token);
1609
1610     wined3d_mutex_lock();
1611     stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1612     if (!stateblock)
1613     {
1614         WARN("Invalid handle (%#x) passed.\n", Token);
1615         wined3d_mutex_unlock();
1616         return D3DERR_INVALIDCALL;
1617     }
1618     hr = wined3d_stateblock_capture(stateblock);
1619     wined3d_mutex_unlock();
1620
1621     return hr;
1622 }
1623
1624 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1625 {
1626     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1627     struct wined3d_stateblock *stateblock;
1628
1629     TRACE("iface %p, token %#x.\n", iface, Token);
1630
1631     wined3d_mutex_lock();
1632     stateblock = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1633
1634     if (!stateblock)
1635     {
1636         WARN("Invalid handle (%#x) passed.\n", Token);
1637         wined3d_mutex_unlock();
1638         return D3DERR_INVALIDCALL;
1639     }
1640
1641     if (wined3d_stateblock_decref(stateblock))
1642     {
1643         ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1644     }
1645     wined3d_mutex_unlock();
1646
1647     return D3D_OK;
1648 }
1649
1650 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1651         D3DSTATEBLOCKTYPE Type, DWORD *handle)
1652 {
1653     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1654     struct wined3d_stateblock *stateblock;
1655     HRESULT hr;
1656
1657     TRACE("iface %p, type %#x, handle %p.\n", iface, Type, handle);
1658
1659     if (Type != D3DSBT_ALL
1660             && Type != D3DSBT_PIXELSTATE
1661             && Type != D3DSBT_VERTEXSTATE)
1662     {
1663         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1664         return D3DERR_INVALIDCALL;
1665     }
1666
1667     wined3d_mutex_lock();
1668     hr = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &stateblock);
1669     if (FAILED(hr))
1670     {
1671         wined3d_mutex_unlock();
1672         ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1673         return hr;
1674     }
1675
1676     *handle = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1677     wined3d_mutex_unlock();
1678
1679     if (*handle == D3D8_INVALID_HANDLE)
1680     {
1681         ERR("Failed to allocate a handle.\n");
1682         wined3d_mutex_lock();
1683         wined3d_stateblock_decref(stateblock);
1684         wined3d_mutex_unlock();
1685         return E_FAIL;
1686     }
1687     ++*handle;
1688
1689     TRACE("Returning %#x (%p).\n", *handle, stateblock);
1690
1691     return hr;
1692 }
1693
1694 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(IDirect3DDevice8 *iface,
1695         const D3DCLIPSTATUS8 *pClipStatus)
1696 {
1697     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1698     HRESULT hr;
1699
1700     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1701 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1702
1703     wined3d_mutex_lock();
1704     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1705     wined3d_mutex_unlock();
1706
1707     return hr;
1708 }
1709
1710 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(IDirect3DDevice8 *iface,
1711         D3DCLIPSTATUS8 *pClipStatus)
1712 {
1713     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1714     HRESULT hr;
1715
1716     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1717
1718     wined3d_mutex_lock();
1719     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1720     wined3d_mutex_unlock();
1721
1722     return hr;
1723 }
1724
1725 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(IDirect3DDevice8 *iface,
1726         DWORD Stage, IDirect3DBaseTexture8 **ppTexture)
1727 {
1728     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1729     struct wined3d_texture *wined3d_texture;
1730     HRESULT hr;
1731
1732     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1733
1734     if(ppTexture == NULL){
1735         return D3DERR_INVALIDCALL;
1736     }
1737
1738     wined3d_mutex_lock();
1739     hr = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &wined3d_texture);
1740     if (FAILED(hr))
1741     {
1742         WARN("Failed to get texture for stage %u, hr %#x.\n", Stage, hr);
1743         wined3d_mutex_unlock();
1744         *ppTexture = NULL;
1745         return hr;
1746     }
1747
1748     if (wined3d_texture)
1749     {
1750         *ppTexture = wined3d_texture_get_parent(wined3d_texture);
1751         IDirect3DBaseTexture8_AddRef(*ppTexture);
1752         wined3d_texture_decref(wined3d_texture);
1753     }
1754     else
1755     {
1756         *ppTexture = NULL;
1757     }
1758     wined3d_mutex_unlock();
1759
1760     return D3D_OK;
1761 }
1762
1763 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(IDirect3DDevice8 *iface, DWORD Stage,
1764         IDirect3DBaseTexture8 *pTexture)
1765 {
1766     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1767     HRESULT hr;
1768
1769     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1770
1771     wined3d_mutex_lock();
1772     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1773             pTexture ? ((IDirect3DBaseTexture8Impl *)pTexture)->wined3d_texture : NULL);
1774     wined3d_mutex_unlock();
1775
1776     return hr;
1777 }
1778
1779 static const struct tss_lookup
1780 {
1781     BOOL sampler_state;
1782     DWORD state;
1783 }
1784 tss_lookup[] =
1785 {
1786     {FALSE, WINED3DTSS_FORCE_DWORD},            /*  0, unused */
1787     {FALSE, WINED3DTSS_COLOROP},                /*  1, D3DTSS_COLOROP */
1788     {FALSE, WINED3DTSS_COLORARG1},              /*  2, D3DTSS_COLORARG1 */
1789     {FALSE, WINED3DTSS_COLORARG2},              /*  3, D3DTSS_COLORARG2 */
1790     {FALSE, WINED3DTSS_ALPHAOP},                /*  4, D3DTSS_ALPHAOP */
1791     {FALSE, WINED3DTSS_ALPHAARG1},              /*  5, D3DTSS_ALPHAARG1 */
1792     {FALSE, WINED3DTSS_ALPHAARG2},              /*  6, D3DTSS_ALPHAARG2 */
1793     {FALSE, WINED3DTSS_BUMPENVMAT00},           /*  7, D3DTSS_BUMPENVMAT00 */
1794     {FALSE, WINED3DTSS_BUMPENVMAT01},           /*  8, D3DTSS_BUMPENVMAT01 */
1795     {FALSE, WINED3DTSS_BUMPENVMAT10},           /*  9, D3DTSS_BUMPENVMAT10 */
1796     {FALSE, WINED3DTSS_BUMPENVMAT11},           /* 10, D3DTSS_BUMPENVMAT11 */
1797     {FALSE, WINED3DTSS_TEXCOORDINDEX},          /* 11, D3DTSS_TEXCOORDINDEX */
1798     {FALSE, WINED3DTSS_FORCE_DWORD},            /* 12, unused */
1799     {TRUE,  WINED3DSAMP_ADDRESSU},              /* 13, D3DTSS_ADDRESSU */
1800     {TRUE,  WINED3DSAMP_ADDRESSV},              /* 14, D3DTSS_ADDRESSV */
1801     {TRUE,  WINED3DSAMP_BORDERCOLOR},           /* 15, D3DTSS_BORDERCOLOR */
1802     {TRUE,  WINED3DSAMP_MAGFILTER},             /* 16, D3DTSS_MAGFILTER */
1803     {TRUE,  WINED3DSAMP_MINFILTER},             /* 17, D3DTSS_MINFILTER */
1804     {TRUE,  WINED3DSAMP_MIPFILTER},             /* 18, D3DTSS_MIPFILTER */
1805     {TRUE,  WINED3DSAMP_MIPMAPLODBIAS},         /* 19, D3DTSS_MIPMAPLODBIAS */
1806     {TRUE,  WINED3DSAMP_MAXMIPLEVEL},           /* 20, D3DTSS_MAXMIPLEVEL */
1807     {TRUE,  WINED3DSAMP_MAXANISOTROPY},         /* 21, D3DTSS_MAXANISOTROPY */
1808     {FALSE, WINED3DTSS_BUMPENVLSCALE},          /* 22, D3DTSS_BUMPENVLSCALE */
1809     {FALSE, WINED3DTSS_BUMPENVLOFFSET},         /* 23, D3DTSS_BUMPENVLOFFSET */
1810     {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS},  /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1811     {TRUE,  WINED3DSAMP_ADDRESSW},              /* 25, D3DTSS_ADDRESSW */
1812     {FALSE, WINED3DTSS_COLORARG0},              /* 26, D3DTSS_COLORARG0 */
1813     {FALSE, WINED3DTSS_ALPHAARG0},              /* 27, D3DTSS_ALPHAARG0 */
1814     {FALSE, WINED3DTSS_RESULTARG},              /* 28, D3DTSS_RESULTARG */
1815 };
1816
1817 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(IDirect3DDevice8 *iface,
1818         DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1819 {
1820     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1821     const struct tss_lookup *l;
1822     HRESULT hr;
1823
1824     TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1825
1826     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1827     {
1828         WARN("Invalid Type %#x passed.\n", Type);
1829         return D3D_OK;
1830     }
1831
1832     l = &tss_lookup[Type];
1833
1834     wined3d_mutex_lock();
1835     if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1836     else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1837     wined3d_mutex_unlock();
1838
1839     return hr;
1840 }
1841
1842 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(IDirect3DDevice8 *iface,
1843         DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1844 {
1845     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1846     const struct tss_lookup *l;
1847     HRESULT hr;
1848
1849     TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1850
1851     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1852     {
1853         WARN("Invalid Type %#x passed.\n", Type);
1854         return D3D_OK;
1855     }
1856
1857     l = &tss_lookup[Type];
1858
1859     wined3d_mutex_lock();
1860     if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1861     else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1862     wined3d_mutex_unlock();
1863
1864     return hr;
1865 }
1866
1867 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(IDirect3DDevice8 *iface,
1868         DWORD *pNumPasses)
1869 {
1870     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1871     HRESULT hr;
1872
1873     TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1874
1875     wined3d_mutex_lock();
1876     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1877     wined3d_mutex_unlock();
1878
1879     return hr;
1880 }
1881
1882 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(IDirect3DDevice8 *iface,
1883         DWORD info_id, void *info, DWORD info_size)
1884 {
1885     FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1886
1887     return D3D_OK;
1888 }
1889
1890 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(IDirect3DDevice8 *iface,
1891         UINT PaletteNumber, const PALETTEENTRY *pEntries)
1892 {
1893     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1894     HRESULT hr;
1895
1896     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1897
1898     wined3d_mutex_lock();
1899     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1900     wined3d_mutex_unlock();
1901
1902     return hr;
1903 }
1904
1905 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(IDirect3DDevice8 *iface,
1906         UINT PaletteNumber, PALETTEENTRY *pEntries)
1907 {
1908     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1909     HRESULT hr;
1910
1911     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1912
1913     wined3d_mutex_lock();
1914     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1915     wined3d_mutex_unlock();
1916
1917     return hr;
1918 }
1919
1920 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(IDirect3DDevice8 *iface,
1921         UINT PaletteNumber)
1922 {
1923     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1924     HRESULT hr;
1925
1926     TRACE("iface %p, palette_idx %u.\n", iface, PaletteNumber);
1927
1928     wined3d_mutex_lock();
1929     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1930     wined3d_mutex_unlock();
1931
1932     return hr;
1933 }
1934
1935 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(IDirect3DDevice8 *iface,
1936         UINT *PaletteNumber)
1937 {
1938     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1939     HRESULT hr;
1940
1941     TRACE("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1942
1943     wined3d_mutex_lock();
1944     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1945     wined3d_mutex_unlock();
1946
1947     return hr;
1948 }
1949
1950 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface,
1951         D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1952 {
1953     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1954     HRESULT hr;
1955
1956     TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1957             iface, PrimitiveType, StartVertex, PrimitiveCount);
1958
1959     wined3d_mutex_lock();
1960     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1961     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1962             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1963     wined3d_mutex_unlock();
1964
1965     return hr;
1966 }
1967
1968 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1969         D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertices, UINT startIndex,
1970         UINT primCount)
1971 {
1972     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1973     HRESULT hr;
1974
1975     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1976             iface, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1977
1978     wined3d_mutex_lock();
1979     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1980     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1981             vertex_count_from_primitive_count(PrimitiveType, primCount));
1982     wined3d_mutex_unlock();
1983
1984     return hr;
1985 }
1986
1987 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1988         D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
1989         UINT VertexStreamZeroStride)
1990 {
1991     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1992     HRESULT hr;
1993
1994     TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1995             iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1996
1997     wined3d_mutex_lock();
1998     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1999     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
2000             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
2001             pVertexStreamZeroData, VertexStreamZeroStride);
2002     wined3d_mutex_unlock();
2003
2004     return hr;
2005 }
2006
2007 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2008         D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
2009         UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
2010         const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
2011 {
2012     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2013     HRESULT hr;
2014
2015     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
2016             "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2017             iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
2018             pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
2019
2020     wined3d_mutex_lock();
2021     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
2022     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
2023             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
2024             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
2025     wined3d_mutex_unlock();
2026
2027     return hr;
2028 }
2029
2030 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(IDirect3DDevice8 *iface,
2031         UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer8 *pDestBuffer,
2032         DWORD Flags)
2033 {
2034     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2035     HRESULT hr;
2036     IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
2037
2038     TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2039             iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, Flags);
2040
2041     wined3d_mutex_lock();
2042     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
2043     wined3d_mutex_unlock();
2044
2045     return hr;
2046 }
2047
2048 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
2049         const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2050 {
2051     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2052     IDirect3DVertexShader8Impl *object;
2053     DWORD shader_handle;
2054     DWORD handle;
2055     HRESULT hr;
2056
2057     TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2058             iface, declaration, byte_code, shader, usage);
2059
2060     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2061     if (!object)
2062     {
2063         ERR("Failed to allocate vertex shader memory.\n");
2064         *shader = 0;
2065         return E_OUTOFMEMORY;
2066     }
2067
2068     wined3d_mutex_lock();
2069     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
2070     wined3d_mutex_unlock();
2071     if (handle == D3D8_INVALID_HANDLE)
2072     {
2073         ERR("Failed to allocate vertex shader handle.\n");
2074         HeapFree(GetProcessHeap(), 0, object);
2075         *shader = 0;
2076         return E_OUTOFMEMORY;
2077     }
2078
2079     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2080
2081     hr = vertexshader_init(object, This, declaration, byte_code, shader_handle, usage);
2082     if (FAILED(hr))
2083     {
2084         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2085         wined3d_mutex_lock();
2086         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
2087         wined3d_mutex_unlock();
2088         HeapFree(GetProcessHeap(), 0, object);
2089         *shader = 0;
2090         return hr;
2091     }
2092
2093     TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2094     *shader = shader_handle;
2095
2096     return D3D_OK;
2097 }
2098
2099 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
2100 {
2101     IDirect3DVertexDeclaration8Impl *d3d8_declaration;
2102     HRESULT hr;
2103     int p, low, high; /* deliberately signed */
2104     struct FvfToDecl *convertedDecls = This->decls;
2105
2106     TRACE("Searching for declaration for fvf %08x... ", fvf);
2107
2108     low = 0;
2109     high = This->numConvertedDecls - 1;
2110     while(low <= high) {
2111         p = (low + high) >> 1;
2112         TRACE("%d ", p);
2113         if(convertedDecls[p].fvf == fvf) {
2114             TRACE("found %p\n", convertedDecls[p].decl);
2115             return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
2116         } else if(convertedDecls[p].fvf < fvf) {
2117             low = p + 1;
2118         } else {
2119             high = p - 1;
2120         }
2121     }
2122     TRACE("not found. Creating and inserting at position %d.\n", low);
2123
2124     d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
2125     if (!d3d8_declaration)
2126     {
2127         ERR("Memory allocation failed.\n");
2128         return NULL;
2129     }
2130
2131     hr = vertexdeclaration_init_fvf(d3d8_declaration, This, fvf);
2132     if (FAILED(hr))
2133     {
2134         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2135         HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2136         return NULL;
2137     }
2138
2139     if(This->declArraySize == This->numConvertedDecls) {
2140         int grow = This->declArraySize / 2;
2141         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2142                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2143         if(!convertedDecls) {
2144             /* This will destroy it */
2145             IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
2146             return NULL;
2147         }
2148         This->decls = convertedDecls;
2149         This->declArraySize += grow;
2150     }
2151
2152     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
2153     convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
2154     convertedDecls[low].fvf = fvf;
2155     This->numConvertedDecls++;
2156
2157     TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
2158     return d3d8_declaration;
2159 }
2160
2161 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(IDirect3DDevice8 *iface, DWORD pShader)
2162 {
2163     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2164     IDirect3DVertexShader8Impl *shader;
2165     HRESULT hr;
2166
2167     TRACE("iface %p, shader %#x.\n", iface, pShader);
2168
2169     if (VS_HIGHESTFIXEDFXF >= pShader) {
2170         TRACE("Setting FVF, %#x\n", pShader);
2171
2172         wined3d_mutex_lock();
2173         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
2174                 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
2175         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
2176         wined3d_mutex_unlock();
2177
2178         return D3D_OK;
2179     }
2180
2181     TRACE("Setting shader\n");
2182
2183     wined3d_mutex_lock();
2184     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2185     if (!shader)
2186     {
2187         WARN("Invalid handle (%#x) passed.\n", pShader);
2188         wined3d_mutex_unlock();
2189
2190         return D3DERR_INVALIDCALL;
2191     }
2192
2193     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
2194             ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
2195     if (SUCCEEDED(hr))
2196         hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wined3d_shader);
2197     wined3d_mutex_unlock();
2198
2199     TRACE("Returning hr %#x\n", hr);
2200
2201     return hr;
2202 }
2203
2204 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2205 {
2206     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2207     struct wined3d_vertex_declaration *wined3d_declaration;
2208     IDirect3DVertexDeclaration8 *d3d8_declaration;
2209     HRESULT hr;
2210
2211     TRACE("iface %p, shader %p.\n", iface, ppShader);
2212
2213     wined3d_mutex_lock();
2214     hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
2215     if (FAILED(hr))
2216     {
2217         wined3d_mutex_unlock();
2218         WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
2219                 This, hr, This->WineD3DDevice);
2220         return hr;
2221     }
2222
2223     if (!wined3d_declaration)
2224     {
2225         wined3d_mutex_unlock();
2226         *ppShader = 0;
2227         return D3D_OK;
2228     }
2229
2230     d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2231     wined3d_vertex_declaration_decref(wined3d_declaration);
2232     wined3d_mutex_unlock();
2233     *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
2234
2235     TRACE("(%p) : returning %#x\n", This, *ppShader);
2236
2237     return hr;
2238 }
2239
2240 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD pShader)
2241 {
2242     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2243     IDirect3DVertexShader8Impl *shader;
2244     struct wined3d_shader *cur;
2245
2246     TRACE("iface %p, shader %#x.\n", iface, pShader);
2247
2248     wined3d_mutex_lock();
2249     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2250     if (!shader)
2251     {
2252         WARN("Invalid handle (%#x) passed.\n", pShader);
2253         wined3d_mutex_unlock();
2254
2255         return D3DERR_INVALIDCALL;
2256     }
2257
2258     cur = IWineD3DDevice_GetVertexShader(This->WineD3DDevice);
2259     if (cur)
2260     {
2261         if (cur == shader->wined3d_shader)
2262             IDirect3DDevice8_SetVertexShader(iface, 0);
2263         wined3d_shader_decref(cur);
2264     }
2265
2266     wined3d_mutex_unlock();
2267
2268     if (IDirect3DVertexShader8_Release(&shader->IDirect3DVertexShader8_iface))
2269     {
2270         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2271     }
2272
2273     return D3D_OK;
2274 }
2275
2276 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2277         DWORD Register, const void *pConstantData, DWORD ConstantCount)
2278 {
2279     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2280     HRESULT hr;
2281
2282     TRACE("iface %p, register %u, data %p, count %u.\n",
2283             iface, Register, pConstantData, ConstantCount);
2284
2285     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2286         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2287              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2288         return D3DERR_INVALIDCALL;
2289     }
2290
2291     wined3d_mutex_lock();
2292     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2293     wined3d_mutex_unlock();
2294
2295     return hr;
2296 }
2297
2298 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2299         DWORD Register, void *pConstantData, DWORD ConstantCount)
2300 {
2301     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2302     HRESULT hr;
2303
2304     TRACE("iface %p, register %u, data %p, count %u.\n",
2305             iface, Register, pConstantData, ConstantCount);
2306
2307     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2308         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2309              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2310         return D3DERR_INVALIDCALL;
2311     }
2312
2313     wined3d_mutex_lock();
2314     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2315     wined3d_mutex_unlock();
2316
2317     return hr;
2318 }
2319
2320 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2321         DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2322 {
2323     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2324     IDirect3DVertexDeclaration8Impl *declaration;
2325     IDirect3DVertexShader8Impl *shader;
2326
2327     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2328             iface, pVertexShader, pData, pSizeOfData);
2329
2330     wined3d_mutex_lock();
2331     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2332     wined3d_mutex_unlock();
2333
2334     if (!shader)
2335     {
2336         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2337         return D3DERR_INVALIDCALL;
2338     }
2339     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2340
2341     /* If pData is NULL, we just return the required size of the buffer. */
2342     if (!pData) {
2343         *pSizeOfData = declaration->elements_size;
2344         return D3D_OK;
2345     }
2346
2347     /* MSDN claims that if *pSizeOfData is smaller than the required size
2348      * we should write the required size and return D3DERR_MOREDATA.
2349      * That's not actually true. */
2350     if (*pSizeOfData < declaration->elements_size) {
2351         return D3DERR_INVALIDCALL;
2352     }
2353
2354     CopyMemory(pData, declaration->elements, declaration->elements_size);
2355
2356     return D3D_OK;
2357 }
2358
2359 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2360         DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2361 {
2362     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2363     IDirect3DVertexShader8Impl *shader = NULL;
2364     HRESULT hr;
2365
2366     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2367             iface, pVertexShader, pData, pSizeOfData);
2368
2369     wined3d_mutex_lock();
2370     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2371     if (!shader)
2372     {
2373         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2374         wined3d_mutex_unlock();
2375
2376         return D3DERR_INVALIDCALL;
2377     }
2378
2379     if (!shader->wined3d_shader)
2380     {
2381         wined3d_mutex_unlock();
2382         *pSizeOfData = 0;
2383         return D3D_OK;
2384     }
2385
2386     hr = wined3d_shader_get_byte_code(shader->wined3d_shader, pData, pSizeOfData);
2387     wined3d_mutex_unlock();
2388
2389     return hr;
2390 }
2391
2392 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(IDirect3DDevice8 *iface,
2393         IDirect3DIndexBuffer8 *pIndexData, UINT baseVertexIndex)
2394 {
2395     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2396     HRESULT hr;
2397     IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2398
2399     TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, pIndexData, baseVertexIndex);
2400
2401     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2402      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2403      * vertex buffers can't be created to address them with an index that requires the 32nd bit
2404      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2405      * problem)
2406      */
2407     wined3d_mutex_lock();
2408     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2409     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2410             ib ? ib->wineD3DIndexBuffer : NULL,
2411             ib ? ib->format : WINED3DFMT_UNKNOWN);
2412     wined3d_mutex_unlock();
2413
2414     return hr;
2415 }
2416
2417 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(IDirect3DDevice8 *iface,
2418         IDirect3DIndexBuffer8 **ppIndexData, UINT *pBaseVertexIndex)
2419 {
2420     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2421     struct wined3d_buffer *retIndexData = NULL;
2422     HRESULT hr;
2423
2424     TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, ppIndexData, pBaseVertexIndex);
2425
2426     if(ppIndexData == NULL){
2427         return D3DERR_INVALIDCALL;
2428     }
2429
2430     /* The case from UINT to INT is safe because d3d8 will never set negative values */
2431     wined3d_mutex_lock();
2432     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2433     hr = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2434     if (SUCCEEDED(hr) && retIndexData)
2435     {
2436         *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2437         IDirect3DIndexBuffer8_AddRef(*ppIndexData);
2438         wined3d_buffer_decref(retIndexData);
2439     } else {
2440         if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2441         *ppIndexData = NULL;
2442     }
2443     wined3d_mutex_unlock();
2444
2445     return hr;
2446 }
2447
2448 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(IDirect3DDevice8 *iface,
2449         const DWORD *byte_code, DWORD *shader)
2450 {
2451     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2452     IDirect3DPixelShader8Impl *object;
2453     DWORD shader_handle;
2454     DWORD handle;
2455     HRESULT hr;
2456
2457     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2458
2459     if (!shader)
2460     {
2461         TRACE("(%p) Invalid call\n", This);
2462         return D3DERR_INVALIDCALL;
2463     }
2464
2465     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2466     if (!object)
2467     {
2468         ERR("Failed to allocate pixel shader memmory.\n");
2469         return E_OUTOFMEMORY;
2470     }
2471
2472     wined3d_mutex_lock();
2473     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2474     wined3d_mutex_unlock();
2475     if (handle == D3D8_INVALID_HANDLE)
2476     {
2477         ERR("Failed to allocate pixel shader handle.\n");
2478         HeapFree(GetProcessHeap(), 0, object);
2479         return E_OUTOFMEMORY;
2480     }
2481
2482     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2483
2484     hr = pixelshader_init(object, This, byte_code, shader_handle);
2485     if (FAILED(hr))
2486     {
2487         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2488         wined3d_mutex_lock();
2489         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_PS);
2490         wined3d_mutex_unlock();
2491         HeapFree(GetProcessHeap(), 0, object);
2492         *shader = 0;
2493         return hr;
2494     }
2495
2496     TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2497     *shader = shader_handle;
2498
2499     return D3D_OK;
2500 }
2501
2502 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2503 {
2504     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2505     IDirect3DPixelShader8Impl *shader;
2506     HRESULT hr;
2507
2508     TRACE("iface %p, shader %#x.\n", iface, pShader);
2509
2510     wined3d_mutex_lock();
2511
2512     if (!pShader)
2513     {
2514         hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2515         wined3d_mutex_unlock();
2516         return hr;
2517     }
2518
2519     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2520     if (!shader)
2521     {
2522         WARN("Invalid handle (%#x) passed.\n", pShader);
2523         wined3d_mutex_unlock();
2524         return D3DERR_INVALIDCALL;
2525     }
2526
2527     TRACE("(%p) : Setting shader %p\n", This, shader);
2528     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wined3d_shader);
2529     wined3d_mutex_unlock();
2530
2531     return hr;
2532 }
2533
2534 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2535 {
2536     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2537     struct wined3d_shader *object;
2538
2539     TRACE("iface %p, shader %p.\n", iface, ppShader);
2540
2541     if (NULL == ppShader) {
2542         TRACE("(%p) Invalid call\n", This);
2543         return D3DERR_INVALIDCALL;
2544     }
2545
2546     wined3d_mutex_lock();
2547     object = IWineD3DDevice_GetPixelShader(This->WineD3DDevice);
2548     if (object)
2549     {
2550         IDirect3DPixelShader8Impl *d3d8_shader;
2551         d3d8_shader = wined3d_shader_get_parent(object);
2552         wined3d_shader_decref(object);
2553         *ppShader = d3d8_shader->handle;
2554     }
2555     else
2556     {
2557         *ppShader = 0;
2558     }
2559     wined3d_mutex_unlock();
2560
2561     TRACE("(%p) : returning %#x\n", This, *ppShader);
2562
2563     return D3D_OK;
2564 }
2565
2566 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2567 {
2568     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2569     IDirect3DPixelShader8Impl *shader;
2570     struct wined3d_shader *cur;
2571
2572     TRACE("iface %p, shader %#x.\n", iface, pShader);
2573
2574     wined3d_mutex_lock();
2575
2576     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2577     if (!shader)
2578     {
2579         WARN("Invalid handle (%#x) passed.\n", pShader);
2580         wined3d_mutex_unlock();
2581         return D3DERR_INVALIDCALL;
2582     }
2583
2584     cur = IWineD3DDevice_GetPixelShader(This->WineD3DDevice);
2585     if (cur)
2586     {
2587         if (cur == shader->wined3d_shader)
2588             IDirect3DDevice8_SetPixelShader(iface, 0);
2589         wined3d_shader_decref(cur);
2590     }
2591
2592     wined3d_mutex_unlock();
2593
2594     if (IDirect3DPixelShader8_Release(&shader->IDirect3DPixelShader8_iface))
2595     {
2596         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2597     }
2598
2599     return D3D_OK;
2600 }
2601
2602 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2603         DWORD Register, const void *pConstantData, DWORD ConstantCount)
2604 {
2605     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2606     HRESULT hr;
2607
2608     TRACE("iface %p, register %u, data %p, count %u.\n",
2609             iface, Register, pConstantData, ConstantCount);
2610
2611     wined3d_mutex_lock();
2612     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2613     wined3d_mutex_unlock();
2614
2615     return hr;
2616 }
2617
2618 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2619         DWORD Register, void *pConstantData, DWORD ConstantCount)
2620 {
2621     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2622     HRESULT hr;
2623
2624     TRACE("iface %p, register %u, data %p, count %u.\n",
2625             iface, Register, pConstantData, ConstantCount);
2626
2627     wined3d_mutex_lock();
2628     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2629     wined3d_mutex_unlock();
2630
2631     return hr;
2632 }
2633
2634 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2635         DWORD pPixelShader, void *pData, DWORD *pSizeOfData)
2636 {
2637     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2638     IDirect3DPixelShader8Impl *shader = NULL;
2639     HRESULT hr;
2640
2641     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2642             iface, pPixelShader, pData, pSizeOfData);
2643
2644     wined3d_mutex_lock();
2645     shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2646     if (!shader)
2647     {
2648         WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2649         wined3d_mutex_unlock();
2650
2651         return D3DERR_INVALIDCALL;
2652     }
2653
2654     hr = wined3d_shader_get_byte_code(shader->wined3d_shader, pData, pSizeOfData);
2655     wined3d_mutex_unlock();
2656
2657     return hr;
2658 }
2659
2660 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(IDirect3DDevice8 *iface, UINT Handle,
2661         const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2662 {
2663     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2664     HRESULT hr;
2665
2666     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2667             iface, Handle, pNumSegs, pRectPatchInfo);
2668
2669     wined3d_mutex_lock();
2670     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2671     wined3d_mutex_unlock();
2672
2673     return hr;
2674 }
2675
2676 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(IDirect3DDevice8 *iface, UINT Handle,
2677         const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2678 {
2679     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2680     HRESULT hr;
2681
2682     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2683             iface, Handle, pNumSegs, pTriPatchInfo);
2684
2685     wined3d_mutex_lock();
2686     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2687     wined3d_mutex_unlock();
2688
2689     return hr;
2690 }
2691
2692 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(IDirect3DDevice8 *iface, UINT Handle)
2693 {
2694     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2695     HRESULT hr;
2696
2697     TRACE("iface %p, handle %#x.\n", iface, Handle);
2698
2699     wined3d_mutex_lock();
2700     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2701     wined3d_mutex_unlock();
2702
2703     return hr;
2704 }
2705
2706 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(IDirect3DDevice8 *iface,
2707         UINT StreamNumber, IDirect3DVertexBuffer8 *pStreamData, UINT Stride)
2708 {
2709     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2710     HRESULT hr;
2711
2712     TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2713             iface, StreamNumber, pStreamData, Stride);
2714
2715     wined3d_mutex_lock();
2716     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2717                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2718                                         0/* Offset in bytes */, Stride);
2719     wined3d_mutex_unlock();
2720
2721     return hr;
2722 }
2723
2724 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(IDirect3DDevice8 *iface,
2725         UINT StreamNumber, IDirect3DVertexBuffer8 **pStream, UINT *pStride)
2726 {
2727     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2728     struct wined3d_buffer *retStream = NULL;
2729     HRESULT hr;
2730
2731     TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2732             iface, StreamNumber, pStream, pStride);
2733
2734     if(pStream == NULL){
2735         return D3DERR_INVALIDCALL;
2736     }
2737
2738     wined3d_mutex_lock();
2739     hr = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber,
2740             &retStream, 0 /* Offset in bytes */, pStride);
2741     if (SUCCEEDED(hr) && retStream)
2742     {
2743         *pStream = wined3d_buffer_get_parent(retStream);
2744         IDirect3DVertexBuffer8_AddRef(*pStream);
2745         wined3d_buffer_decref(retStream);
2746     }
2747     else
2748     {
2749         if (FAILED(hr)) FIXME("Call to GetStreamSource failed, hr %#x.\n", hr);
2750         *pStream = NULL;
2751     }
2752     wined3d_mutex_unlock();
2753
2754     return hr;
2755 }
2756
2757 static const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2758 {
2759     IDirect3DDevice8Impl_QueryInterface,
2760     IDirect3DDevice8Impl_AddRef,
2761     IDirect3DDevice8Impl_Release,
2762     IDirect3DDevice8Impl_TestCooperativeLevel,
2763     IDirect3DDevice8Impl_GetAvailableTextureMem,
2764     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2765     IDirect3DDevice8Impl_GetDirect3D,
2766     IDirect3DDevice8Impl_GetDeviceCaps,
2767     IDirect3DDevice8Impl_GetDisplayMode,
2768     IDirect3DDevice8Impl_GetCreationParameters,
2769     IDirect3DDevice8Impl_SetCursorProperties,
2770     IDirect3DDevice8Impl_SetCursorPosition,
2771     IDirect3DDevice8Impl_ShowCursor,
2772     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2773     IDirect3DDevice8Impl_Reset,
2774     IDirect3DDevice8Impl_Present,
2775     IDirect3DDevice8Impl_GetBackBuffer,
2776     IDirect3DDevice8Impl_GetRasterStatus,
2777     IDirect3DDevice8Impl_SetGammaRamp,
2778     IDirect3DDevice8Impl_GetGammaRamp,
2779     IDirect3DDevice8Impl_CreateTexture,
2780     IDirect3DDevice8Impl_CreateVolumeTexture,
2781     IDirect3DDevice8Impl_CreateCubeTexture,
2782     IDirect3DDevice8Impl_CreateVertexBuffer,
2783     IDirect3DDevice8Impl_CreateIndexBuffer,
2784     IDirect3DDevice8Impl_CreateRenderTarget,
2785     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2786     IDirect3DDevice8Impl_CreateImageSurface,
2787     IDirect3DDevice8Impl_CopyRects,
2788     IDirect3DDevice8Impl_UpdateTexture,
2789     IDirect3DDevice8Impl_GetFrontBuffer,
2790     IDirect3DDevice8Impl_SetRenderTarget,
2791     IDirect3DDevice8Impl_GetRenderTarget,
2792     IDirect3DDevice8Impl_GetDepthStencilSurface,
2793     IDirect3DDevice8Impl_BeginScene,
2794     IDirect3DDevice8Impl_EndScene,
2795     IDirect3DDevice8Impl_Clear,
2796     IDirect3DDevice8Impl_SetTransform,
2797     IDirect3DDevice8Impl_GetTransform,
2798     IDirect3DDevice8Impl_MultiplyTransform,
2799     IDirect3DDevice8Impl_SetViewport,
2800     IDirect3DDevice8Impl_GetViewport,
2801     IDirect3DDevice8Impl_SetMaterial,
2802     IDirect3DDevice8Impl_GetMaterial,
2803     IDirect3DDevice8Impl_SetLight,
2804     IDirect3DDevice8Impl_GetLight,
2805     IDirect3DDevice8Impl_LightEnable,
2806     IDirect3DDevice8Impl_GetLightEnable,
2807     IDirect3DDevice8Impl_SetClipPlane,
2808     IDirect3DDevice8Impl_GetClipPlane,
2809     IDirect3DDevice8Impl_SetRenderState,
2810     IDirect3DDevice8Impl_GetRenderState,
2811     IDirect3DDevice8Impl_BeginStateBlock,
2812     IDirect3DDevice8Impl_EndStateBlock,
2813     IDirect3DDevice8Impl_ApplyStateBlock,
2814     IDirect3DDevice8Impl_CaptureStateBlock,
2815     IDirect3DDevice8Impl_DeleteStateBlock,
2816     IDirect3DDevice8Impl_CreateStateBlock,
2817     IDirect3DDevice8Impl_SetClipStatus,
2818     IDirect3DDevice8Impl_GetClipStatus,
2819     IDirect3DDevice8Impl_GetTexture,
2820     IDirect3DDevice8Impl_SetTexture,
2821     IDirect3DDevice8Impl_GetTextureStageState,
2822     IDirect3DDevice8Impl_SetTextureStageState,
2823     IDirect3DDevice8Impl_ValidateDevice,
2824     IDirect3DDevice8Impl_GetInfo,
2825     IDirect3DDevice8Impl_SetPaletteEntries,
2826     IDirect3DDevice8Impl_GetPaletteEntries,
2827     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2828     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2829     IDirect3DDevice8Impl_DrawPrimitive,
2830     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2831     IDirect3DDevice8Impl_DrawPrimitiveUP,
2832     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2833     IDirect3DDevice8Impl_ProcessVertices,
2834     IDirect3DDevice8Impl_CreateVertexShader,
2835     IDirect3DDevice8Impl_SetVertexShader,
2836     IDirect3DDevice8Impl_GetVertexShader,
2837     IDirect3DDevice8Impl_DeleteVertexShader,
2838     IDirect3DDevice8Impl_SetVertexShaderConstant,
2839     IDirect3DDevice8Impl_GetVertexShaderConstant,
2840     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2841     IDirect3DDevice8Impl_GetVertexShaderFunction,
2842     IDirect3DDevice8Impl_SetStreamSource,
2843     IDirect3DDevice8Impl_GetStreamSource,
2844     IDirect3DDevice8Impl_SetIndices,
2845     IDirect3DDevice8Impl_GetIndices,
2846     IDirect3DDevice8Impl_CreatePixelShader,
2847     IDirect3DDevice8Impl_SetPixelShader,
2848     IDirect3DDevice8Impl_GetPixelShader,
2849     IDirect3DDevice8Impl_DeletePixelShader,
2850     IDirect3DDevice8Impl_SetPixelShaderConstant,
2851     IDirect3DDevice8Impl_GetPixelShaderConstant,
2852     IDirect3DDevice8Impl_GetPixelShaderFunction,
2853     IDirect3DDevice8Impl_DrawRectPatch,
2854     IDirect3DDevice8Impl_DrawTriPatch,
2855     IDirect3DDevice8Impl_DeletePatch
2856 };
2857
2858 static inline IDirect3DDevice8Impl *impl_from_IWineD3DDeviceParent(IWineD3DDeviceParent *iface)
2859 {
2860     return CONTAINING_RECORD(iface, IDirect3DDevice8Impl, IWineD3DDeviceParent_iface);
2861 }
2862
2863 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface,
2864         REFIID riid, void **object)
2865 {
2866     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2867     return IDirect3DDevice8Impl_QueryInterface(&This->IDirect3DDevice8_iface, riid, object);
2868 }
2869
2870 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2871 {
2872     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2873     return IDirect3DDevice8Impl_AddRef(&This->IDirect3DDevice8_iface);
2874 }
2875
2876 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2877 {
2878     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2879     return IDirect3DDevice8Impl_Release(&This->IDirect3DDevice8_iface);
2880 }
2881
2882 /* IWineD3DDeviceParent methods */
2883
2884 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2885 {
2886     TRACE("iface %p, device %p\n", iface, device);
2887 }
2888
2889 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2890         void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2891         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, struct wined3d_surface **surface)
2892 {
2893     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2894     IDirect3DSurface8Impl *d3d_surface;
2895     BOOL lockable = TRUE;
2896     HRESULT hr;
2897
2898     TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
2899             "\tpool %#x, level %u, face %u, surface %p\n",
2900             iface, container_parent, width, height, format, usage, pool, level, face, surface);
2901
2902
2903     if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2904
2905     hr = IDirect3DDevice8Impl_CreateSurface(This, width, height,
2906             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2907             (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2908     if (FAILED(hr))
2909     {
2910         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2911         return hr;
2912     }
2913
2914     *surface = d3d_surface->wined3d_surface;
2915     wined3d_surface_incref(*surface);
2916
2917     d3d_surface->container = container_parent;
2918     IUnknown_Release(d3d_surface->parentDevice);
2919     d3d_surface->parentDevice = NULL;
2920
2921     IDirect3DSurface8_Release((IDirect3DSurface8 *)d3d_surface);
2922     d3d_surface->forwardReference = container_parent;
2923
2924     return hr;
2925 }
2926
2927 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2928         void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
2929         WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
2930         struct wined3d_surface **surface)
2931 {
2932     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2933     IDirect3DSurface8Impl *d3d_surface;
2934     HRESULT hr;
2935
2936     TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2937             "\tmultisample_quality %u, lockable %u, surface %p\n",
2938             iface, container_parent, width, height, format, multisample_type, multisample_quality, lockable, surface);
2939
2940     hr = IDirect3DDevice8_CreateRenderTarget(&This->IDirect3DDevice8_iface, width, height,
2941             d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2942     if (FAILED(hr))
2943     {
2944         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2945         return hr;
2946     }
2947
2948     *surface = d3d_surface->wined3d_surface;
2949     wined3d_surface_incref(*surface);
2950
2951     d3d_surface->container = (IUnknown *)&This->IDirect3DDevice8_iface;
2952     /* Implicit surfaces are created with an refcount of 0 */
2953     IUnknown_Release((IUnknown *)d3d_surface);
2954
2955     return hr;
2956 }
2957
2958 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2959         UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
2960         DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
2961 {
2962     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2963     IDirect3DSurface8Impl *d3d_surface;
2964     HRESULT hr;
2965
2966     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2967             "\tmultisample_quality %u, discard %u, surface %p\n",
2968             iface, width, height, format, multisample_type, multisample_quality, discard, surface);
2969
2970     hr = IDirect3DDevice8_CreateDepthStencilSurface(&This->IDirect3DDevice8_iface, width, height,
2971             d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2972     if (FAILED(hr))
2973     {
2974         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2975         return hr;
2976     }
2977
2978     *surface = d3d_surface->wined3d_surface;
2979     wined3d_surface_incref(*surface);
2980
2981     d3d_surface->container = (IUnknown *)&This->IDirect3DDevice8_iface;
2982     /* Implicit surfaces are created with an refcount of 0 */
2983     IUnknown_Release((IUnknown *)d3d_surface);
2984
2985     return hr;
2986 }
2987
2988 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2989         void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2990         WINED3DPOOL pool, DWORD usage, struct wined3d_volume **volume)
2991 {
2992     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2993     IDirect3DVolume8Impl *object;
2994     HRESULT hr;
2995
2996     TRACE("iface %p, container_parent %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2997             iface, container_parent, width, height, depth, format, pool, usage, volume);
2998
2999     /* Allocate the storage for the device */
3000     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
3001     if (!object)
3002     {
3003         FIXME("Allocation of memory failed\n");
3004         *volume = NULL;
3005         return D3DERR_OUTOFVIDEOMEMORY;
3006     }
3007
3008     hr = volume_init(object, This, width, height, depth, usage, format, pool);
3009     if (FAILED(hr))
3010     {
3011         WARN("Failed to initialize volume, hr %#x.\n", hr);
3012         HeapFree(GetProcessHeap(), 0, object);
3013         return hr;
3014     }
3015
3016     *volume = object->wined3d_volume;
3017     wined3d_volume_incref(*volume);
3018     IDirect3DVolume8_Release(&object->IDirect3DVolume8_iface);
3019
3020     object->container = container_parent;
3021     object->forwardReference = container_parent;
3022
3023     TRACE("(%p) Created volume %p\n", iface, object);
3024
3025     return hr;
3026 }
3027
3028 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
3029         WINED3DPRESENT_PARAMETERS *present_parameters, struct wined3d_swapchain **swapchain)
3030 {
3031     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
3032     D3DPRESENT_PARAMETERS local_parameters;
3033     IDirect3DSwapChain8 *d3d_swapchain;
3034     HRESULT hr;
3035
3036     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
3037
3038     /* Copy the presentation parameters */
3039     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
3040     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
3041     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
3042     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
3043     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
3044     local_parameters.SwapEffect = present_parameters->SwapEffect;
3045     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
3046     local_parameters.Windowed = present_parameters->Windowed;
3047     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
3048     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
3049     local_parameters.Flags = present_parameters->Flags;
3050     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
3051     local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
3052
3053     hr = IDirect3DDevice8_CreateAdditionalSwapChain(&This->IDirect3DDevice8_iface,
3054             &local_parameters, &d3d_swapchain);
3055     if (FAILED(hr))
3056     {
3057         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
3058         *swapchain = NULL;
3059         return hr;
3060     }
3061
3062     *swapchain = ((IDirect3DSwapChain8Impl *)d3d_swapchain)->wined3d_swapchain;
3063     wined3d_swapchain_incref(*swapchain);
3064     IDirect3DSwapChain8_Release(d3d_swapchain);
3065
3066     /* Copy back the presentation parameters */
3067     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
3068     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
3069     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
3070     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
3071     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
3072     present_parameters->SwapEffect = local_parameters.SwapEffect;
3073     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
3074     present_parameters->Windowed = local_parameters.Windowed;
3075     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
3076     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
3077     present_parameters->Flags = local_parameters.Flags;
3078     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
3079     present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
3080
3081     return hr;
3082 }
3083
3084 static const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
3085 {
3086     /* IUnknown methods */
3087     device_parent_QueryInterface,
3088     device_parent_AddRef,
3089     device_parent_Release,
3090     /* IWineD3DDeviceParent methods */
3091     device_parent_WineD3DDeviceCreated,
3092     device_parent_CreateSurface,
3093     device_parent_CreateRenderTarget,
3094     device_parent_CreateDepthStencilSurface,
3095     device_parent_CreateVolume,
3096     device_parent_CreateSwapChain,
3097 };
3098
3099 static void setup_fpu(void)
3100 {
3101 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3102     WORD cw;
3103     __asm__ volatile ("fnstcw %0" : "=m" (cw));
3104     cw = (cw & ~0xf3f) | 0x3f;
3105     __asm__ volatile ("fldcw %0" : : "m" (cw));
3106 #else
3107     FIXME("FPU setup not implemented for this platform.\n");
3108 #endif
3109 }
3110
3111 HRESULT device_init(IDirect3DDevice8Impl *device, struct wined3d *wined3d, UINT adapter,
3112         D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3113 {
3114     WINED3DPRESENT_PARAMETERS wined3d_parameters;
3115     HRESULT hr;
3116
3117     device->IDirect3DDevice8_iface.lpVtbl = &Direct3DDevice8_Vtbl;
3118     device->IWineD3DDeviceParent_iface.lpVtbl = &d3d8_wined3d_device_parent_vtbl;
3119     device->ref = 1;
3120     device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3121             D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3122     if (!device->handle_table.entries)
3123     {
3124         ERR("Failed to allocate handle table memory.\n");
3125         return E_OUTOFMEMORY;
3126     }
3127     device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3128
3129     if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3130
3131     wined3d_mutex_lock();
3132     hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags,
3133             &device->IWineD3DDeviceParent_iface, &device->WineD3DDevice);
3134     if (FAILED(hr))
3135     {
3136         WARN("Failed to create wined3d device, hr %#x.\n", hr);
3137         wined3d_mutex_unlock();
3138         HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3139         return hr;
3140     }
3141
3142     if (!parameters->Windowed)
3143     {
3144         HWND device_window = parameters->hDeviceWindow;
3145
3146         if (!focus_window) focus_window = device_window;
3147         if (FAILED(hr = IWineD3DDevice_AcquireFocusWindow(device->WineD3DDevice, focus_window)))
3148         {
3149             ERR("Failed to acquire focus window, hr %#x.\n", hr);
3150             IWineD3DDevice_Release(device->WineD3DDevice);
3151             wined3d_mutex_unlock();
3152             HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3153             return hr;
3154         }
3155
3156         if (!device_window) device_window = focus_window;
3157         IWineD3DDevice_SetupFullscreenWindow(device->WineD3DDevice, device_window,
3158                 parameters->BackBufferWidth,
3159                 parameters->BackBufferHeight);
3160     }
3161
3162     if (flags & D3DCREATE_MULTITHREADED) IWineD3DDevice_SetMultithreaded(device->WineD3DDevice);
3163
3164     wined3d_parameters.BackBufferWidth = parameters->BackBufferWidth;
3165     wined3d_parameters.BackBufferHeight = parameters->BackBufferHeight;
3166     wined3d_parameters.BackBufferFormat = wined3dformat_from_d3dformat(parameters->BackBufferFormat);
3167     wined3d_parameters.BackBufferCount = parameters->BackBufferCount;
3168     wined3d_parameters.MultiSampleType = parameters->MultiSampleType;
3169     wined3d_parameters.MultiSampleQuality = 0; /* d3d9 only */
3170     wined3d_parameters.SwapEffect = parameters->SwapEffect;
3171     wined3d_parameters.hDeviceWindow = parameters->hDeviceWindow;
3172     wined3d_parameters.Windowed = parameters->Windowed;
3173     wined3d_parameters.EnableAutoDepthStencil = parameters->EnableAutoDepthStencil;
3174     wined3d_parameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(parameters->AutoDepthStencilFormat);
3175     wined3d_parameters.Flags = parameters->Flags;
3176     wined3d_parameters.FullScreen_RefreshRateInHz = parameters->FullScreen_RefreshRateInHz;
3177     wined3d_parameters.PresentationInterval = parameters->FullScreen_PresentationInterval;
3178     wined3d_parameters.AutoRestoreDisplayMode = TRUE;
3179
3180     hr = IWineD3DDevice_Init3D(device->WineD3DDevice, &wined3d_parameters);
3181     if (FAILED(hr))
3182     {
3183         WARN("Failed to initialize 3D, hr %#x.\n", hr);
3184         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3185         IWineD3DDevice_Release(device->WineD3DDevice);
3186         wined3d_mutex_unlock();
3187         HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3188         return hr;
3189     }
3190
3191     hr = IWineD3DDevice_SetRenderState(device->WineD3DDevice, WINED3DRS_POINTSIZE_MIN, 0);
3192     wined3d_mutex_unlock();
3193     if (FAILED(hr))
3194     {
3195         ERR("Failed to set minimum pointsize, hr %#x.\n", hr);
3196         goto err;
3197     }
3198
3199     parameters->BackBufferWidth = wined3d_parameters.BackBufferWidth;
3200     parameters->BackBufferHeight = wined3d_parameters.BackBufferHeight;
3201     parameters->BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters.BackBufferFormat);
3202     parameters->BackBufferCount = wined3d_parameters.BackBufferCount;
3203     parameters->MultiSampleType = wined3d_parameters.MultiSampleType;
3204     parameters->SwapEffect = wined3d_parameters.SwapEffect;
3205     parameters->hDeviceWindow = wined3d_parameters.hDeviceWindow;
3206     parameters->Windowed = wined3d_parameters.Windowed;
3207     parameters->EnableAutoDepthStencil = wined3d_parameters.EnableAutoDepthStencil;
3208     parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(wined3d_parameters.AutoDepthStencilFormat);
3209     parameters->Flags = wined3d_parameters.Flags;
3210     parameters->FullScreen_RefreshRateInHz = wined3d_parameters.FullScreen_RefreshRateInHz;
3211     parameters->FullScreen_PresentationInterval = wined3d_parameters.PresentationInterval;
3212
3213     device->declArraySize = 16;
3214     device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3215     if (!device->decls)
3216     {
3217         ERR("Failed to allocate FVF vertex delcaration map memory.\n");
3218         hr = E_OUTOFMEMORY;
3219         goto err;
3220     }
3221
3222     return D3D_OK;
3223
3224 err:
3225     wined3d_mutex_lock();
3226     IWineD3DDevice_Uninit3D(device->WineD3DDevice);
3227     IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3228     IWineD3DDevice_Release(device->WineD3DDevice);
3229     wined3d_mutex_unlock();
3230     HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3231     return hr;
3232 }