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