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