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