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