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