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