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