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