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