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