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