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