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