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