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