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