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