wined3d: Merge the various resource desc structures.
[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(IDirect3DDevice8 *iface,
927         IDirect3DSurface8 *pSourceSurface, const RECT *pSourceRects, UINT cRects,
928         IDirect3DSurface8 *pDestinationSurface, const POINT *pDestPoints)
929 {
930     IDirect3DSurface8Impl *Source = (IDirect3DSurface8Impl *) pSourceSurface;
931     IDirect3DSurface8Impl *Dest = (IDirect3DSurface8Impl *) pDestinationSurface;
932     enum wined3d_format_id srcFormat, destFormat;
933     struct wined3d_resource_desc wined3d_desc;
934
935     TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
936             iface, pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
937
938     /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the
939      * destination texture is in WINED3DPOOL_DEFAULT. */
940
941     wined3d_mutex_lock();
942     IWineD3DSurface_GetDesc(Source->wineD3DSurface, &wined3d_desc);
943     srcFormat = wined3d_desc.format;
944
945     IWineD3DSurface_GetDesc(Dest->wineD3DSurface, &wined3d_desc);
946     destFormat = wined3d_desc.format;
947
948     /* Check that the source and destination formats match */
949     if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat)
950     {
951         WARN("Source %p format must match the dest %p format, returning D3DERR_INVALIDCALL.\n",
952                 pSourceSurface, pDestinationSurface);
953         wined3d_mutex_unlock();
954         return D3DERR_INVALIDCALL;
955     }
956     else if (WINED3DFMT_UNKNOWN == destFormat)
957     {
958         TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
959         IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
960     }
961
962     /* Quick if complete copy ... */
963     if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
964         IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
965     } else {
966         unsigned int i;
967         /* Copy rect by rect */
968         if (NULL != pSourceRects && NULL != pDestPoints) {
969             for (i = 0; i < cRects; ++i) {
970                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
971             }
972         } else {
973             for (i = 0; i < cRects; ++i) {
974                 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
975             }
976         }
977     }
978     wined3d_mutex_unlock();
979
980     return WINED3D_OK;
981 }
982
983 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(IDirect3DDevice8 *iface,
984         IDirect3DBaseTexture8 *pSourceTexture, IDirect3DBaseTexture8 *pDestinationTexture)
985 {
986     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
987     HRESULT hr;
988
989     TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, pSourceTexture, pDestinationTexture);
990
991     wined3d_mutex_lock();
992     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture8Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture8Impl *)pDestinationTexture)->wineD3DBaseTexture);
993     wined3d_mutex_unlock();
994
995     return hr;
996 }
997
998 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(IDirect3DDevice8 *iface,
999         IDirect3DSurface8 *pDestSurface)
1000 {
1001     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1002     IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
1003     HRESULT hr;
1004
1005     TRACE("iface %p, dst_surface %p.\n", iface, pDestSurface);
1006
1007     if (pDestSurface == NULL) {
1008         WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
1009         return D3DERR_INVALIDCALL;
1010     }
1011
1012     wined3d_mutex_lock();
1013     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
1014     wined3d_mutex_unlock();
1015
1016     return hr;
1017 }
1018
1019 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(IDirect3DDevice8 *iface,
1020         IDirect3DSurface8 *pRenderTarget, IDirect3DSurface8 *pNewZStencil)
1021 {
1022     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1023     IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
1024     IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
1025     IWineD3DSurface *original_ds = NULL;
1026     HRESULT hr;
1027
1028     TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, pRenderTarget, pNewZStencil);
1029
1030     wined3d_mutex_lock();
1031
1032     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &original_ds);
1033     if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
1034     {
1035         hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, pZSurface ? pZSurface->wineD3DSurface : NULL);
1036         if (SUCCEEDED(hr) && pSurface)
1037             hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface->wineD3DSurface, TRUE);
1038         if (FAILED(hr)) IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, original_ds);
1039     }
1040     if (original_ds) IWineD3DSurface_Release(original_ds);
1041
1042     wined3d_mutex_unlock();
1043
1044     return hr;
1045 }
1046
1047 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderTarget(IDirect3DDevice8 *iface,
1048         IDirect3DSurface8 **ppRenderTarget)
1049 {
1050     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1051     IWineD3DSurface *pRenderTarget;
1052     HRESULT hr;
1053
1054     TRACE("iface %p, render_target %p.\n", iface, ppRenderTarget);
1055
1056     if (ppRenderTarget == NULL) {
1057         return D3DERR_INVALIDCALL;
1058     }
1059
1060     wined3d_mutex_lock();
1061     hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
1062     if (SUCCEEDED(hr) && pRenderTarget)
1063     {
1064         *ppRenderTarget = IWineD3DSurface_GetParent(pRenderTarget);
1065         IDirect3DSurface8_AddRef(*ppRenderTarget);
1066         IWineD3DSurface_Release(pRenderTarget);
1067     }
1068     else
1069     {
1070         FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
1071         *ppRenderTarget = NULL;
1072     }
1073     wined3d_mutex_unlock();
1074
1075     return hr;
1076 }
1077
1078 static HRESULT WINAPI IDirect3DDevice8Impl_GetDepthStencilSurface(IDirect3DDevice8 *iface,
1079         IDirect3DSurface8 **ppZStencilSurface)
1080 {
1081     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1082     IWineD3DSurface *pZStencilSurface;
1083     HRESULT hr;
1084
1085     TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1086
1087     if(ppZStencilSurface == NULL){
1088         return D3DERR_INVALIDCALL;
1089     }
1090
1091     wined3d_mutex_lock();
1092     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &pZStencilSurface);
1093     if (SUCCEEDED(hr))
1094     {
1095         *ppZStencilSurface = IWineD3DSurface_GetParent(pZStencilSurface);
1096         IDirect3DSurface8_AddRef(*ppZStencilSurface);
1097         IWineD3DSurface_Release(pZStencilSurface);
1098     }
1099     else
1100     {
1101         if (hr != WINED3DERR_NOTFOUND)
1102                 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1103         *ppZStencilSurface = NULL;
1104     }
1105     wined3d_mutex_unlock();
1106
1107     return hr;
1108 }
1109
1110 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(IDirect3DDevice8 *iface)
1111 {
1112     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1113     HRESULT hr;
1114
1115     TRACE("iface %p.\n", iface);
1116
1117     wined3d_mutex_lock();
1118     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1119     wined3d_mutex_unlock();
1120
1121     return hr;
1122 }
1123
1124 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice8Impl_EndScene(IDirect3DDevice8 *iface)
1125 {
1126     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1127     HRESULT hr;
1128
1129     TRACE("iface %p.\n", iface);
1130
1131     wined3d_mutex_lock();
1132     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1133     wined3d_mutex_unlock();
1134
1135     return hr;
1136 }
1137
1138 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(IDirect3DDevice8 *iface, DWORD Count,
1139         const D3DRECT *pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil)
1140 {
1141     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1142     HRESULT hr;
1143
1144     TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1145             iface, Count, pRects, Flags, Color, Z, Stencil);
1146
1147     wined3d_mutex_lock();
1148     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (const RECT *)pRects, Flags, Color, Z, Stencil);
1149     wined3d_mutex_unlock();
1150
1151     return hr;
1152 }
1153
1154 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(IDirect3DDevice8 *iface,
1155         D3DTRANSFORMSTATETYPE State, const D3DMATRIX *lpMatrix)
1156 {
1157     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1158     HRESULT hr;
1159
1160     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1161
1162     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1163     wined3d_mutex_lock();
1164     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1165     wined3d_mutex_unlock();
1166
1167     return hr;
1168 }
1169
1170 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(IDirect3DDevice8 *iface,
1171         D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
1172 {
1173     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1174     HRESULT hr;
1175
1176     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1177
1178     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1179     wined3d_mutex_lock();
1180     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1181     wined3d_mutex_unlock();
1182
1183     return hr;
1184 }
1185
1186 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(IDirect3DDevice8 *iface,
1187         D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
1188 {
1189     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1190     HRESULT hr;
1191
1192     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1193
1194     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1195     wined3d_mutex_lock();
1196     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1197     wined3d_mutex_unlock();
1198
1199     return hr;
1200 }
1201
1202 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(IDirect3DDevice8 *iface,
1203         const D3DVIEWPORT8 *pViewport)
1204 {
1205     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1206     HRESULT hr;
1207
1208     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1209
1210     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1211     wined3d_mutex_lock();
1212     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1213     wined3d_mutex_unlock();
1214
1215     return hr;
1216 }
1217
1218 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(IDirect3DDevice8 *iface,
1219         D3DVIEWPORT8 *pViewport)
1220 {
1221     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1222     HRESULT hr;
1223
1224     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1225
1226     /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1227     wined3d_mutex_lock();
1228     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1229     wined3d_mutex_unlock();
1230
1231     return hr;
1232 }
1233
1234 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(IDirect3DDevice8 *iface,
1235         const D3DMATERIAL8 *pMaterial)
1236 {
1237     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1238     HRESULT hr;
1239
1240     TRACE("iface %p, material %p.\n", iface, pMaterial);
1241
1242     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1243     wined3d_mutex_lock();
1244     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1245     wined3d_mutex_unlock();
1246
1247     return hr;
1248 }
1249
1250 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(IDirect3DDevice8 *iface,
1251         D3DMATERIAL8 *pMaterial)
1252 {
1253     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1254     HRESULT hr;
1255
1256     TRACE("iface %p, material %p.\n", iface, pMaterial);
1257
1258     /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1259     wined3d_mutex_lock();
1260     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1261     wined3d_mutex_unlock();
1262
1263     return hr;
1264 }
1265
1266 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(IDirect3DDevice8 *iface, DWORD Index,
1267         const D3DLIGHT8 *pLight)
1268 {
1269     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1270     HRESULT hr;
1271
1272     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1273
1274     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1275     wined3d_mutex_lock();
1276     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1277     wined3d_mutex_unlock();
1278
1279     return hr;
1280 }
1281
1282 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(IDirect3DDevice8 *iface, DWORD Index,
1283         D3DLIGHT8 *pLight)
1284 {
1285     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1286     HRESULT hr;
1287
1288     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1289
1290     /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1291     wined3d_mutex_lock();
1292     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1293     wined3d_mutex_unlock();
1294
1295     return hr;
1296 }
1297
1298 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(IDirect3DDevice8 *iface, DWORD Index,
1299         BOOL Enable)
1300 {
1301     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1302     HRESULT hr;
1303
1304     TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1305
1306     wined3d_mutex_lock();
1307     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1308     wined3d_mutex_unlock();
1309
1310     return hr;
1311 }
1312
1313 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(IDirect3DDevice8 *iface, DWORD Index,
1314         BOOL *pEnable)
1315 {
1316     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1317     HRESULT hr;
1318
1319     TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1320
1321     wined3d_mutex_lock();
1322     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1323     wined3d_mutex_unlock();
1324
1325     return hr;
1326 }
1327
1328 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(IDirect3DDevice8 *iface, DWORD Index,
1329         const float *pPlane)
1330 {
1331     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1332     HRESULT hr;
1333
1334     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1335
1336     wined3d_mutex_lock();
1337     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1338     wined3d_mutex_unlock();
1339
1340     return hr;
1341 }
1342
1343 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(IDirect3DDevice8 *iface, DWORD Index,
1344         float *pPlane)
1345 {
1346     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1347     HRESULT hr;
1348
1349     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1350
1351     wined3d_mutex_lock();
1352     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1353     wined3d_mutex_unlock();
1354
1355     return hr;
1356 }
1357
1358 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(IDirect3DDevice8 *iface,
1359         D3DRENDERSTATETYPE State, DWORD Value)
1360 {
1361     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1362     HRESULT hr;
1363
1364     TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1365
1366     wined3d_mutex_lock();
1367     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1368     wined3d_mutex_unlock();
1369
1370     return hr;
1371 }
1372
1373 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(IDirect3DDevice8 *iface,
1374         D3DRENDERSTATETYPE State, DWORD *pValue)
1375 {
1376     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1377     HRESULT hr;
1378
1379     TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1380
1381     wined3d_mutex_lock();
1382     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1383     wined3d_mutex_unlock();
1384
1385     return hr;
1386 }
1387
1388 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(IDirect3DDevice8 *iface)
1389 {
1390     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1391     HRESULT hr;
1392
1393     TRACE("iface %p.\n", iface);
1394
1395     wined3d_mutex_lock();
1396     hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1397     wined3d_mutex_unlock();
1398
1399     return hr;
1400 }
1401
1402 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(IDirect3DDevice8 *iface, DWORD *pToken)
1403 {
1404     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1405     struct wined3d_stateblock *stateblock;
1406     HRESULT hr;
1407
1408     TRACE("iface %p, token %p.\n", iface, pToken);
1409
1410     /* Tell wineD3D to endstateblock before anything else (in case we run out
1411      * of memory later and cause locking problems)
1412      */
1413     wined3d_mutex_lock();
1414     hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &stateblock);
1415     if (hr != D3D_OK) {
1416         WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1417         wined3d_mutex_unlock();
1418         return hr;
1419     }
1420
1421     *pToken = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1422     wined3d_mutex_unlock();
1423
1424     if (*pToken == D3D8_INVALID_HANDLE)
1425     {
1426         ERR("Failed to create a handle\n");
1427         wined3d_mutex_lock();
1428         wined3d_stateblock_decref(stateblock);
1429         wined3d_mutex_unlock();
1430         return E_FAIL;
1431     }
1432     ++*pToken;
1433
1434     TRACE("Returning %#x (%p).\n", *pToken, stateblock);
1435
1436     return hr;
1437 }
1438
1439 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1440 {
1441     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1442     struct wined3d_stateblock *stateblock;
1443     HRESULT hr;
1444
1445     TRACE("iface %p, token %#x.\n", iface, Token);
1446
1447     if (!Token) return D3D_OK;
1448
1449     wined3d_mutex_lock();
1450     stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1451     if (!stateblock)
1452     {
1453         WARN("Invalid handle (%#x) passed.\n", Token);
1454         wined3d_mutex_unlock();
1455         return D3DERR_INVALIDCALL;
1456     }
1457     hr = wined3d_stateblock_apply(stateblock);
1458     wined3d_mutex_unlock();
1459
1460     return hr;
1461 }
1462
1463 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1464 {
1465     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1466     struct wined3d_stateblock *stateblock;
1467     HRESULT hr;
1468
1469     TRACE("iface %p, token %#x.\n", iface, Token);
1470
1471     wined3d_mutex_lock();
1472     stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1473     if (!stateblock)
1474     {
1475         WARN("Invalid handle (%#x) passed.\n", Token);
1476         wined3d_mutex_unlock();
1477         return D3DERR_INVALIDCALL;
1478     }
1479     hr = wined3d_stateblock_capture(stateblock);
1480     wined3d_mutex_unlock();
1481
1482     return hr;
1483 }
1484
1485 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1486 {
1487     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1488     struct wined3d_stateblock *stateblock;
1489
1490     TRACE("iface %p, token %#x.\n", iface, Token);
1491
1492     wined3d_mutex_lock();
1493     stateblock = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1494
1495     if (!stateblock)
1496     {
1497         WARN("Invalid handle (%#x) passed.\n", Token);
1498         wined3d_mutex_unlock();
1499         return D3DERR_INVALIDCALL;
1500     }
1501
1502     if (wined3d_stateblock_decref(stateblock))
1503     {
1504         ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1505     }
1506     wined3d_mutex_unlock();
1507
1508     return D3D_OK;
1509 }
1510
1511 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1512         D3DSTATEBLOCKTYPE Type, DWORD *handle)
1513 {
1514     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1515     struct wined3d_stateblock *stateblock;
1516     HRESULT hr;
1517
1518     TRACE("iface %p, type %#x, handle %p.\n", iface, Type, handle);
1519
1520     if (Type != D3DSBT_ALL
1521             && Type != D3DSBT_PIXELSTATE
1522             && Type != D3DSBT_VERTEXSTATE)
1523     {
1524         WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1525         return D3DERR_INVALIDCALL;
1526     }
1527
1528     wined3d_mutex_lock();
1529     hr = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &stateblock);
1530     if (FAILED(hr))
1531     {
1532         wined3d_mutex_unlock();
1533         ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1534         return hr;
1535     }
1536
1537     *handle = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1538     wined3d_mutex_unlock();
1539
1540     if (*handle == D3D8_INVALID_HANDLE)
1541     {
1542         ERR("Failed to allocate a handle.\n");
1543         wined3d_mutex_lock();
1544         wined3d_stateblock_decref(stateblock);
1545         wined3d_mutex_unlock();
1546         return E_FAIL;
1547     }
1548     ++*handle;
1549
1550     TRACE("Returning %#x (%p).\n", *handle, stateblock);
1551
1552     return hr;
1553 }
1554
1555 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(IDirect3DDevice8 *iface,
1556         const D3DCLIPSTATUS8 *pClipStatus)
1557 {
1558     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1559     HRESULT hr;
1560
1561     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1562 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1563
1564     wined3d_mutex_lock();
1565     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1566     wined3d_mutex_unlock();
1567
1568     return hr;
1569 }
1570
1571 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(IDirect3DDevice8 *iface,
1572         D3DCLIPSTATUS8 *pClipStatus)
1573 {
1574     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1575     HRESULT hr;
1576
1577     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1578
1579     wined3d_mutex_lock();
1580     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1581     wined3d_mutex_unlock();
1582
1583     return hr;
1584 }
1585
1586 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(IDirect3DDevice8 *iface,
1587         DWORD Stage, IDirect3DBaseTexture8 **ppTexture)
1588 {
1589     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1590     IWineD3DBaseTexture *retTexture;
1591     HRESULT hr;
1592
1593     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1594
1595     if(ppTexture == NULL){
1596         return D3DERR_INVALIDCALL;
1597     }
1598
1599     wined3d_mutex_lock();
1600     hr = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1601     if (FAILED(hr))
1602     {
1603         WARN("Failed to get texture for stage %u, hr %#x.\n", Stage, hr);
1604         wined3d_mutex_unlock();
1605         *ppTexture = NULL;
1606         return hr;
1607     }
1608
1609     if (retTexture)
1610     {
1611         *ppTexture = IWineD3DBaseTexture_GetParent(retTexture);
1612         IDirect3DBaseTexture8_AddRef(*ppTexture);
1613         IWineD3DBaseTexture_Release(retTexture);
1614     }
1615     else
1616     {
1617         *ppTexture = NULL;
1618     }
1619     wined3d_mutex_unlock();
1620
1621     return D3D_OK;
1622 }
1623
1624 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(IDirect3DDevice8 *iface, DWORD Stage,
1625         IDirect3DBaseTexture8 *pTexture)
1626 {
1627     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1628     HRESULT hr;
1629
1630     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1631
1632     wined3d_mutex_lock();
1633     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1634                                    pTexture==NULL ? NULL : ((IDirect3DBaseTexture8Impl *)pTexture)->wineD3DBaseTexture);
1635     wined3d_mutex_unlock();
1636
1637     return hr;
1638 }
1639
1640 static const struct tss_lookup
1641 {
1642     BOOL sampler_state;
1643     DWORD state;
1644 }
1645 tss_lookup[] =
1646 {
1647     {FALSE, WINED3DTSS_FORCE_DWORD},            /*  0, unused */
1648     {FALSE, WINED3DTSS_COLOROP},                /*  1, D3DTSS_COLOROP */
1649     {FALSE, WINED3DTSS_COLORARG1},              /*  2, D3DTSS_COLORARG1 */
1650     {FALSE, WINED3DTSS_COLORARG2},              /*  3, D3DTSS_COLORARG2 */
1651     {FALSE, WINED3DTSS_ALPHAOP},                /*  4, D3DTSS_ALPHAOP */
1652     {FALSE, WINED3DTSS_ALPHAARG1},              /*  5, D3DTSS_ALPHAARG1 */
1653     {FALSE, WINED3DTSS_ALPHAARG2},              /*  6, D3DTSS_ALPHAARG2 */
1654     {FALSE, WINED3DTSS_BUMPENVMAT00},           /*  7, D3DTSS_BUMPENVMAT00 */
1655     {FALSE, WINED3DTSS_BUMPENVMAT01},           /*  8, D3DTSS_BUMPENVMAT01 */
1656     {FALSE, WINED3DTSS_BUMPENVMAT10},           /*  9, D3DTSS_BUMPENVMAT10 */
1657     {FALSE, WINED3DTSS_BUMPENVMAT11},           /* 10, D3DTSS_BUMPENVMAT11 */
1658     {FALSE, WINED3DTSS_TEXCOORDINDEX},          /* 11, D3DTSS_TEXCOORDINDEX */
1659     {FALSE, WINED3DTSS_FORCE_DWORD},            /* 12, unused */
1660     {TRUE,  WINED3DSAMP_ADDRESSU},              /* 13, D3DTSS_ADDRESSU */
1661     {TRUE,  WINED3DSAMP_ADDRESSV},              /* 14, D3DTSS_ADDRESSV */
1662     {TRUE,  WINED3DSAMP_BORDERCOLOR},           /* 15, D3DTSS_BORDERCOLOR */
1663     {TRUE,  WINED3DSAMP_MAGFILTER},             /* 16, D3DTSS_MAGFILTER */
1664     {TRUE,  WINED3DSAMP_MINFILTER},             /* 17, D3DTSS_MINFILTER */
1665     {TRUE,  WINED3DSAMP_MIPFILTER},             /* 18, D3DTSS_MIPFILTER */
1666     {TRUE,  WINED3DSAMP_MIPMAPLODBIAS},         /* 19, D3DTSS_MIPMAPLODBIAS */
1667     {TRUE,  WINED3DSAMP_MAXMIPLEVEL},           /* 20, D3DTSS_MAXMIPLEVEL */
1668     {TRUE,  WINED3DSAMP_MAXANISOTROPY},         /* 21, D3DTSS_MAXANISOTROPY */
1669     {FALSE, WINED3DTSS_BUMPENVLSCALE},          /* 22, D3DTSS_BUMPENVLSCALE */
1670     {FALSE, WINED3DTSS_BUMPENVLOFFSET},         /* 23, D3DTSS_BUMPENVLOFFSET */
1671     {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS},  /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1672     {TRUE,  WINED3DSAMP_ADDRESSW},              /* 25, D3DTSS_ADDRESSW */
1673     {FALSE, WINED3DTSS_COLORARG0},              /* 26, D3DTSS_COLORARG0 */
1674     {FALSE, WINED3DTSS_ALPHAARG0},              /* 27, D3DTSS_ALPHAARG0 */
1675     {FALSE, WINED3DTSS_RESULTARG},              /* 28, D3DTSS_RESULTARG */
1676 };
1677
1678 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetTextureStageState(IDirect3DDevice8 *iface,
1679         DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1680 {
1681     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1682     const struct tss_lookup *l;
1683     HRESULT hr;
1684
1685     TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1686
1687     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1688     {
1689         WARN("Invalid Type %#x passed.\n", Type);
1690         return D3D_OK;
1691     }
1692
1693     l = &tss_lookup[Type];
1694
1695     wined3d_mutex_lock();
1696     if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1697     else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1698     wined3d_mutex_unlock();
1699
1700     return hr;
1701 }
1702
1703 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(IDirect3DDevice8 *iface,
1704         DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1705 {
1706     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1707     const struct tss_lookup *l;
1708     HRESULT hr;
1709
1710     TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1711
1712     if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1713     {
1714         WARN("Invalid Type %#x passed.\n", Type);
1715         return D3D_OK;
1716     }
1717
1718     l = &tss_lookup[Type];
1719
1720     wined3d_mutex_lock();
1721     if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1722     else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1723     wined3d_mutex_unlock();
1724
1725     return hr;
1726 }
1727
1728 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(IDirect3DDevice8 *iface,
1729         DWORD *pNumPasses)
1730 {
1731     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1732     HRESULT hr;
1733
1734     TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1735
1736     wined3d_mutex_lock();
1737     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1738     wined3d_mutex_unlock();
1739
1740     return hr;
1741 }
1742
1743 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(IDirect3DDevice8 *iface,
1744         DWORD info_id, void *info, DWORD info_size)
1745 {
1746     FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1747
1748     return D3D_OK;
1749 }
1750
1751 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(IDirect3DDevice8 *iface,
1752         UINT PaletteNumber, const PALETTEENTRY *pEntries)
1753 {
1754     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1755     HRESULT hr;
1756
1757     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1758
1759     wined3d_mutex_lock();
1760     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1761     wined3d_mutex_unlock();
1762
1763     return hr;
1764 }
1765
1766 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(IDirect3DDevice8 *iface,
1767         UINT PaletteNumber, PALETTEENTRY *pEntries)
1768 {
1769     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1770     HRESULT hr;
1771
1772     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1773
1774     wined3d_mutex_lock();
1775     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1776     wined3d_mutex_unlock();
1777
1778     return hr;
1779 }
1780
1781 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(IDirect3DDevice8 *iface,
1782         UINT PaletteNumber)
1783 {
1784     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1785     HRESULT hr;
1786
1787     TRACE("iface %p, palette_idx %u.\n", iface, PaletteNumber);
1788
1789     wined3d_mutex_lock();
1790     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1791     wined3d_mutex_unlock();
1792
1793     return hr;
1794 }
1795
1796 static HRESULT  WINAPI  IDirect3DDevice8Impl_GetCurrentTexturePalette(IDirect3DDevice8 *iface,
1797         UINT *PaletteNumber)
1798 {
1799     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1800     HRESULT hr;
1801
1802     TRACE("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1803
1804     wined3d_mutex_lock();
1805     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1806     wined3d_mutex_unlock();
1807
1808     return hr;
1809 }
1810
1811 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface,
1812         D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1813 {
1814     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1815     HRESULT hr;
1816
1817     TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1818             iface, PrimitiveType, StartVertex, PrimitiveCount);
1819
1820     wined3d_mutex_lock();
1821     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1822     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1823             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1824     wined3d_mutex_unlock();
1825
1826     return hr;
1827 }
1828
1829 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1830         D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertices, UINT startIndex,
1831         UINT primCount)
1832 {
1833     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1834     HRESULT hr;
1835
1836     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1837             iface, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1838
1839     wined3d_mutex_lock();
1840     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1841     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1842             vertex_count_from_primitive_count(PrimitiveType, primCount));
1843     wined3d_mutex_unlock();
1844
1845     return hr;
1846 }
1847
1848 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1849         D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
1850         UINT VertexStreamZeroStride)
1851 {
1852     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1853     HRESULT hr;
1854
1855     TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1856             iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1857
1858     wined3d_mutex_lock();
1859     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1860     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1861             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1862             pVertexStreamZeroData, VertexStreamZeroStride);
1863     wined3d_mutex_unlock();
1864
1865     return hr;
1866 }
1867
1868 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
1869         D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
1870         UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
1871         const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
1872 {
1873     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1874     HRESULT hr;
1875
1876     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1877             "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1878             iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1879             pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1880
1881     wined3d_mutex_lock();
1882     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1883     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1884             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1885             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1886     wined3d_mutex_unlock();
1887
1888     return hr;
1889 }
1890
1891 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(IDirect3DDevice8 *iface,
1892         UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer8 *pDestBuffer,
1893         DWORD Flags)
1894 {
1895     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1896     HRESULT hr;
1897     IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
1898
1899     TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
1900             iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, Flags);
1901
1902     wined3d_mutex_lock();
1903     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1904     wined3d_mutex_unlock();
1905
1906     return hr;
1907 }
1908
1909 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
1910         const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
1911 {
1912     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1913     IDirect3DVertexShader8Impl *object;
1914     DWORD shader_handle;
1915     DWORD handle;
1916     HRESULT hr;
1917
1918     TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
1919             iface, declaration, byte_code, shader, usage);
1920
1921     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1922     if (!object)
1923     {
1924         ERR("Failed to allocate vertex shader memory.\n");
1925         *shader = 0;
1926         return E_OUTOFMEMORY;
1927     }
1928
1929     wined3d_mutex_lock();
1930     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
1931     wined3d_mutex_unlock();
1932     if (handle == D3D8_INVALID_HANDLE)
1933     {
1934         ERR("Failed to allocate vertex shader handle.\n");
1935         HeapFree(GetProcessHeap(), 0, object);
1936         *shader = 0;
1937         return E_OUTOFMEMORY;
1938     }
1939
1940     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
1941
1942     hr = vertexshader_init(object, This, declaration, byte_code, shader_handle, usage);
1943     if (FAILED(hr))
1944     {
1945         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
1946         wined3d_mutex_lock();
1947         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
1948         wined3d_mutex_unlock();
1949         HeapFree(GetProcessHeap(), 0, object);
1950         *shader = 0;
1951         return hr;
1952     }
1953
1954     TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
1955     *shader = shader_handle;
1956
1957     return D3D_OK;
1958 }
1959
1960 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1961 {
1962     IDirect3DVertexDeclaration8Impl *d3d8_declaration;
1963     HRESULT hr;
1964     int p, low, high; /* deliberately signed */
1965     struct FvfToDecl *convertedDecls = This->decls;
1966
1967     TRACE("Searching for declaration for fvf %08x... ", fvf);
1968
1969     low = 0;
1970     high = This->numConvertedDecls - 1;
1971     while(low <= high) {
1972         p = (low + high) >> 1;
1973         TRACE("%d ", p);
1974         if(convertedDecls[p].fvf == fvf) {
1975             TRACE("found %p\n", convertedDecls[p].decl);
1976             return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
1977         } else if(convertedDecls[p].fvf < fvf) {
1978             low = p + 1;
1979         } else {
1980             high = p - 1;
1981         }
1982     }
1983     TRACE("not found. Creating and inserting at position %d.\n", low);
1984
1985     d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
1986     if (!d3d8_declaration)
1987     {
1988         ERR("Memory allocation failed.\n");
1989         return NULL;
1990     }
1991
1992     hr = vertexdeclaration_init_fvf(d3d8_declaration, This, fvf);
1993     if (FAILED(hr))
1994     {
1995         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
1996         HeapFree(GetProcessHeap(), 0, d3d8_declaration);
1997         return NULL;
1998     }
1999
2000     if(This->declArraySize == This->numConvertedDecls) {
2001         int grow = This->declArraySize / 2;
2002         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2003                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2004         if(!convertedDecls) {
2005             /* This will destroy it */
2006             IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
2007             return NULL;
2008         }
2009         This->decls = convertedDecls;
2010         This->declArraySize += grow;
2011     }
2012
2013     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
2014     convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
2015     convertedDecls[low].fvf = fvf;
2016     This->numConvertedDecls++;
2017
2018     TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
2019     return d3d8_declaration;
2020 }
2021
2022 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(IDirect3DDevice8 *iface, DWORD pShader)
2023 {
2024     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2025     IDirect3DVertexShader8Impl *shader;
2026     HRESULT hr;
2027
2028     TRACE("iface %p, shader %#x.\n", iface, pShader);
2029
2030     if (VS_HIGHESTFIXEDFXF >= pShader) {
2031         TRACE("Setting FVF, %#x\n", pShader);
2032
2033         wined3d_mutex_lock();
2034         IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
2035                 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
2036         IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
2037         wined3d_mutex_unlock();
2038
2039         return D3D_OK;
2040     }
2041
2042     TRACE("Setting shader\n");
2043
2044     wined3d_mutex_lock();
2045     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2046     if (!shader)
2047     {
2048         WARN("Invalid handle (%#x) passed.\n", pShader);
2049         wined3d_mutex_unlock();
2050
2051         return D3DERR_INVALIDCALL;
2052     }
2053
2054     hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
2055             ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
2056     if (SUCCEEDED(hr)) hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wineD3DVertexShader);
2057     wined3d_mutex_unlock();
2058
2059     TRACE("Returning hr %#x\n", hr);
2060
2061     return hr;
2062 }
2063
2064 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2065 {
2066     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2067     struct wined3d_vertex_declaration *wined3d_declaration;
2068     IDirect3DVertexDeclaration8 *d3d8_declaration;
2069     HRESULT hr;
2070
2071     TRACE("iface %p, shader %p.\n", iface, ppShader);
2072
2073     wined3d_mutex_lock();
2074     hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
2075     if (FAILED(hr))
2076     {
2077         wined3d_mutex_unlock();
2078         WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
2079                 This, hr, This->WineD3DDevice);
2080         return hr;
2081     }
2082
2083     if (!wined3d_declaration)
2084     {
2085         wined3d_mutex_unlock();
2086         *ppShader = 0;
2087         return D3D_OK;
2088     }
2089
2090     d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2091     wined3d_vertex_declaration_decref(wined3d_declaration);
2092     wined3d_mutex_unlock();
2093     *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
2094
2095     TRACE("(%p) : returning %#x\n", This, *ppShader);
2096
2097     return hr;
2098 }
2099
2100 static HRESULT  WINAPI  IDirect3DDevice8Impl_DeleteVertexShader(IDirect3DDevice8 *iface,
2101         DWORD pShader)
2102 {
2103     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2104     IDirect3DVertexShader8Impl *shader;
2105     IWineD3DVertexShader *cur;
2106
2107     TRACE("iface %p, shader %#x.\n", iface, pShader);
2108
2109     wined3d_mutex_lock();
2110     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2111     if (!shader)
2112     {
2113         WARN("Invalid handle (%#x) passed.\n", pShader);
2114         wined3d_mutex_unlock();
2115
2116         return D3DERR_INVALIDCALL;
2117     }
2118
2119     cur = IWineD3DDevice_GetVertexShader(This->WineD3DDevice);
2120     if (cur)
2121     {
2122         if (cur == shader->wineD3DVertexShader) IDirect3DDevice8_SetVertexShader(iface, 0);
2123         IWineD3DVertexShader_Release(cur);
2124     }
2125
2126     wined3d_mutex_unlock();
2127
2128     if (IUnknown_Release((IUnknown *)shader))
2129     {
2130         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2131     }
2132
2133     return D3D_OK;
2134 }
2135
2136 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2137         DWORD Register, const void *pConstantData, DWORD ConstantCount)
2138 {
2139     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2140     HRESULT hr;
2141
2142     TRACE("iface %p, register %u, data %p, count %u.\n",
2143             iface, Register, pConstantData, ConstantCount);
2144
2145     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2146         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2147              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2148         return D3DERR_INVALIDCALL;
2149     }
2150
2151     wined3d_mutex_lock();
2152     hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2153     wined3d_mutex_unlock();
2154
2155     return hr;
2156 }
2157
2158 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2159         DWORD Register, void *pConstantData, DWORD ConstantCount)
2160 {
2161     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2162     HRESULT hr;
2163
2164     TRACE("iface %p, register %u, data %p, count %u.\n",
2165             iface, Register, pConstantData, ConstantCount);
2166
2167     if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2168         WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2169              Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2170         return D3DERR_INVALIDCALL;
2171     }
2172
2173     wined3d_mutex_lock();
2174     hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2175     wined3d_mutex_unlock();
2176
2177     return hr;
2178 }
2179
2180 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2181         DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2182 {
2183     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2184     IDirect3DVertexDeclaration8Impl *declaration;
2185     IDirect3DVertexShader8Impl *shader;
2186
2187     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2188             iface, pVertexShader, pData, pSizeOfData);
2189
2190     wined3d_mutex_lock();
2191     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2192     wined3d_mutex_unlock();
2193
2194     if (!shader)
2195     {
2196         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2197         return D3DERR_INVALIDCALL;
2198     }
2199     declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2200
2201     /* If pData is NULL, we just return the required size of the buffer. */
2202     if (!pData) {
2203         *pSizeOfData = declaration->elements_size;
2204         return D3D_OK;
2205     }
2206
2207     /* MSDN claims that if *pSizeOfData is smaller than the required size
2208      * we should write the required size and return D3DERR_MOREDATA.
2209      * That's not actually true. */
2210     if (*pSizeOfData < declaration->elements_size) {
2211         return D3DERR_INVALIDCALL;
2212     }
2213
2214     CopyMemory(pData, declaration->elements, declaration->elements_size);
2215
2216     return D3D_OK;
2217 }
2218
2219 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2220         DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2221 {
2222     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2223     IDirect3DVertexShader8Impl *shader = NULL;
2224     HRESULT hr;
2225
2226     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2227             iface, pVertexShader, pData, pSizeOfData);
2228
2229     wined3d_mutex_lock();
2230     shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2231     if (!shader)
2232     {
2233         WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2234         wined3d_mutex_unlock();
2235
2236         return D3DERR_INVALIDCALL;
2237     }
2238
2239     if (!shader->wineD3DVertexShader)
2240     {
2241         wined3d_mutex_unlock();
2242         *pSizeOfData = 0;
2243         return D3D_OK;
2244     }
2245
2246     hr = IWineD3DVertexShader_GetFunction(shader->wineD3DVertexShader, pData, pSizeOfData);
2247     wined3d_mutex_unlock();
2248
2249     return hr;
2250 }
2251
2252 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(IDirect3DDevice8 *iface,
2253         IDirect3DIndexBuffer8 *pIndexData, UINT baseVertexIndex)
2254 {
2255     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2256     HRESULT hr;
2257     IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2258
2259     TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, pIndexData, baseVertexIndex);
2260
2261     /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2262      * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2263      * vertex buffers can't be created to address them with an index that requires the 32nd bit
2264      * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2265      * problem)
2266      */
2267     wined3d_mutex_lock();
2268     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2269     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2270             ib ? ib->wineD3DIndexBuffer : NULL,
2271             ib ? ib->format : WINED3DFMT_UNKNOWN);
2272     wined3d_mutex_unlock();
2273
2274     return hr;
2275 }
2276
2277 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(IDirect3DDevice8 *iface,
2278         IDirect3DIndexBuffer8 **ppIndexData, UINT *pBaseVertexIndex)
2279 {
2280     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2281     struct wined3d_buffer *retIndexData = NULL;
2282     HRESULT hr;
2283
2284     TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, ppIndexData, pBaseVertexIndex);
2285
2286     if(ppIndexData == NULL){
2287         return D3DERR_INVALIDCALL;
2288     }
2289
2290     /* The case from UINT to INT is safe because d3d8 will never set negative values */
2291     wined3d_mutex_lock();
2292     IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2293     hr = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2294     if (SUCCEEDED(hr) && retIndexData)
2295     {
2296         *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2297         IDirect3DIndexBuffer8_AddRef(*ppIndexData);
2298         wined3d_buffer_decref(retIndexData);
2299     } else {
2300         if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2301         *ppIndexData = NULL;
2302     }
2303     wined3d_mutex_unlock();
2304
2305     return hr;
2306 }
2307
2308 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(IDirect3DDevice8 *iface,
2309         const DWORD *byte_code, DWORD *shader)
2310 {
2311     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2312     IDirect3DPixelShader8Impl *object;
2313     DWORD shader_handle;
2314     DWORD handle;
2315     HRESULT hr;
2316
2317     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2318
2319     if (!shader)
2320     {
2321         TRACE("(%p) Invalid call\n", This);
2322         return D3DERR_INVALIDCALL;
2323     }
2324
2325     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2326     if (!object)
2327     {
2328         ERR("Failed to allocate pixel shader memmory.\n");
2329         return E_OUTOFMEMORY;
2330     }
2331
2332     wined3d_mutex_lock();
2333     handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2334     wined3d_mutex_unlock();
2335     if (handle == D3D8_INVALID_HANDLE)
2336     {
2337         ERR("Failed to allocate pixel shader handle.\n");
2338         HeapFree(GetProcessHeap(), 0, object);
2339         return E_OUTOFMEMORY;
2340     }
2341
2342     shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2343
2344     hr = pixelshader_init(object, This, byte_code, shader_handle);
2345     if (FAILED(hr))
2346     {
2347         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2348         wined3d_mutex_lock();
2349         d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_PS);
2350         wined3d_mutex_unlock();
2351         HeapFree(GetProcessHeap(), 0, object);
2352         *shader = 0;
2353         return hr;
2354     }
2355
2356     TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2357     *shader = shader_handle;
2358
2359     return D3D_OK;
2360 }
2361
2362 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2363 {
2364     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2365     IDirect3DPixelShader8Impl *shader;
2366     HRESULT hr;
2367
2368     TRACE("iface %p, shader %#x.\n", iface, pShader);
2369
2370     wined3d_mutex_lock();
2371
2372     if (!pShader)
2373     {
2374         hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2375         wined3d_mutex_unlock();
2376         return hr;
2377     }
2378
2379     shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2380     if (!shader)
2381     {
2382         WARN("Invalid handle (%#x) passed.\n", pShader);
2383         wined3d_mutex_unlock();
2384         return D3DERR_INVALIDCALL;
2385     }
2386
2387     TRACE("(%p) : Setting shader %p\n", This, shader);
2388     hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wineD3DPixelShader);
2389     wined3d_mutex_unlock();
2390
2391     return hr;
2392 }
2393
2394 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2395 {
2396     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2397     IWineD3DPixelShader *object;
2398
2399     TRACE("iface %p, shader %p.\n", iface, ppShader);
2400
2401     if (NULL == ppShader) {
2402         TRACE("(%p) Invalid call\n", This);
2403         return D3DERR_INVALIDCALL;
2404     }
2405
2406     wined3d_mutex_lock();
2407     object = IWineD3DDevice_GetPixelShader(This->WineD3DDevice);
2408     if (object)
2409     {
2410         IDirect3DPixelShader8Impl *d3d8_shader;
2411         d3d8_shader = IWineD3DPixelShader_GetParent(object);
2412         IWineD3DPixelShader_Release(object);
2413         *ppShader = d3d8_shader->handle;
2414     }
2415     else
2416     {
2417         *ppShader = 0;
2418     }
2419     wined3d_mutex_unlock();
2420
2421     TRACE("(%p) : returning %#x\n", This, *ppShader);
2422
2423     return D3D_OK;
2424 }
2425
2426 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2427 {
2428     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2429     IDirect3DPixelShader8Impl *shader;
2430     IWineD3DPixelShader *cur;
2431
2432     TRACE("iface %p, shader %#x.\n", iface, pShader);
2433
2434     wined3d_mutex_lock();
2435
2436     shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2437     if (!shader)
2438     {
2439         WARN("Invalid handle (%#x) passed.\n", pShader);
2440         wined3d_mutex_unlock();
2441         return D3DERR_INVALIDCALL;
2442     }
2443
2444     cur = IWineD3DDevice_GetPixelShader(This->WineD3DDevice);
2445     if (cur)
2446     {
2447         if (cur == shader->wineD3DPixelShader) IDirect3DDevice8_SetPixelShader(iface, 0);
2448         IWineD3DPixelShader_Release(cur);
2449     }
2450
2451     wined3d_mutex_unlock();
2452
2453     if (IUnknown_Release((IUnknown *)shader))
2454     {
2455         ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2456     }
2457
2458     return D3D_OK;
2459 }
2460
2461 static HRESULT  WINAPI  IDirect3DDevice8Impl_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2462         DWORD Register, const void *pConstantData, DWORD ConstantCount)
2463 {
2464     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2465     HRESULT hr;
2466
2467     TRACE("iface %p, register %u, data %p, count %u.\n",
2468             iface, Register, pConstantData, ConstantCount);
2469
2470     wined3d_mutex_lock();
2471     hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2472     wined3d_mutex_unlock();
2473
2474     return hr;
2475 }
2476
2477 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2478         DWORD Register, void *pConstantData, DWORD ConstantCount)
2479 {
2480     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2481     HRESULT hr;
2482
2483     TRACE("iface %p, register %u, data %p, count %u.\n",
2484             iface, Register, pConstantData, ConstantCount);
2485
2486     wined3d_mutex_lock();
2487     hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2488     wined3d_mutex_unlock();
2489
2490     return hr;
2491 }
2492
2493 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2494         DWORD pPixelShader, void *pData, DWORD *pSizeOfData)
2495 {
2496     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2497     IDirect3DPixelShader8Impl *shader = NULL;
2498     HRESULT hr;
2499
2500     TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2501             iface, pPixelShader, pData, pSizeOfData);
2502
2503     wined3d_mutex_lock();
2504     shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2505     if (!shader)
2506     {
2507         WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2508         wined3d_mutex_unlock();
2509
2510         return D3DERR_INVALIDCALL;
2511     }
2512
2513     hr = IWineD3DPixelShader_GetFunction(shader->wineD3DPixelShader, pData, pSizeOfData);
2514     wined3d_mutex_unlock();
2515
2516     return hr;
2517 }
2518
2519 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(IDirect3DDevice8 *iface, UINT Handle,
2520         const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2521 {
2522     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2523     HRESULT hr;
2524
2525     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2526             iface, Handle, pNumSegs, pRectPatchInfo);
2527
2528     wined3d_mutex_lock();
2529     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2530     wined3d_mutex_unlock();
2531
2532     return hr;
2533 }
2534
2535 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(IDirect3DDevice8 *iface, UINT Handle,
2536         const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2537 {
2538     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2539     HRESULT hr;
2540
2541     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2542             iface, Handle, pNumSegs, pTriPatchInfo);
2543
2544     wined3d_mutex_lock();
2545     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2546     wined3d_mutex_unlock();
2547
2548     return hr;
2549 }
2550
2551 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(IDirect3DDevice8 *iface, UINT Handle)
2552 {
2553     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2554     HRESULT hr;
2555
2556     TRACE("iface %p, handle %#x.\n", iface, Handle);
2557
2558     wined3d_mutex_lock();
2559     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2560     wined3d_mutex_unlock();
2561
2562     return hr;
2563 }
2564
2565 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(IDirect3DDevice8 *iface,
2566         UINT StreamNumber, IDirect3DVertexBuffer8 *pStreamData, UINT Stride)
2567 {
2568     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2569     HRESULT hr;
2570
2571     TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2572             iface, StreamNumber, pStreamData, Stride);
2573
2574     wined3d_mutex_lock();
2575     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2576                                         NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2577                                         0/* Offset in bytes */, Stride);
2578     wined3d_mutex_unlock();
2579
2580     return hr;
2581 }
2582
2583 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(IDirect3DDevice8 *iface,
2584         UINT StreamNumber, IDirect3DVertexBuffer8 **pStream, UINT *pStride)
2585 {
2586     IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2587     struct wined3d_buffer *retStream = NULL;
2588     HRESULT hr;
2589
2590     TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2591             iface, StreamNumber, pStream, pStride);
2592
2593     if(pStream == NULL){
2594         return D3DERR_INVALIDCALL;
2595     }
2596
2597     wined3d_mutex_lock();
2598     hr = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber,
2599             &retStream, 0 /* Offset in bytes */, pStride);
2600     if (SUCCEEDED(hr) && retStream)
2601     {
2602         *pStream = wined3d_buffer_get_parent(retStream);
2603         IDirect3DVertexBuffer8_AddRef(*pStream);
2604         wined3d_buffer_decref(retStream);
2605     }
2606     else
2607     {
2608         if (FAILED(hr)) FIXME("Call to GetStreamSource failed, hr %#x.\n", hr);
2609         *pStream = NULL;
2610     }
2611     wined3d_mutex_unlock();
2612
2613     return hr;
2614 }
2615
2616 static const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2617 {
2618     IDirect3DDevice8Impl_QueryInterface,
2619     IDirect3DDevice8Impl_AddRef,
2620     IDirect3DDevice8Impl_Release,
2621     IDirect3DDevice8Impl_TestCooperativeLevel,
2622     IDirect3DDevice8Impl_GetAvailableTextureMem,
2623     IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2624     IDirect3DDevice8Impl_GetDirect3D,
2625     IDirect3DDevice8Impl_GetDeviceCaps,
2626     IDirect3DDevice8Impl_GetDisplayMode,
2627     IDirect3DDevice8Impl_GetCreationParameters,
2628     IDirect3DDevice8Impl_SetCursorProperties,
2629     IDirect3DDevice8Impl_SetCursorPosition,
2630     IDirect3DDevice8Impl_ShowCursor,
2631     IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2632     IDirect3DDevice8Impl_Reset,
2633     IDirect3DDevice8Impl_Present,
2634     IDirect3DDevice8Impl_GetBackBuffer,
2635     IDirect3DDevice8Impl_GetRasterStatus,
2636     IDirect3DDevice8Impl_SetGammaRamp,
2637     IDirect3DDevice8Impl_GetGammaRamp,
2638     IDirect3DDevice8Impl_CreateTexture,
2639     IDirect3DDevice8Impl_CreateVolumeTexture,
2640     IDirect3DDevice8Impl_CreateCubeTexture,
2641     IDirect3DDevice8Impl_CreateVertexBuffer,
2642     IDirect3DDevice8Impl_CreateIndexBuffer,
2643     IDirect3DDevice8Impl_CreateRenderTarget,
2644     IDirect3DDevice8Impl_CreateDepthStencilSurface,
2645     IDirect3DDevice8Impl_CreateImageSurface,
2646     IDirect3DDevice8Impl_CopyRects,
2647     IDirect3DDevice8Impl_UpdateTexture,
2648     IDirect3DDevice8Impl_GetFrontBuffer,
2649     IDirect3DDevice8Impl_SetRenderTarget,
2650     IDirect3DDevice8Impl_GetRenderTarget,
2651     IDirect3DDevice8Impl_GetDepthStencilSurface,
2652     IDirect3DDevice8Impl_BeginScene,
2653     IDirect3DDevice8Impl_EndScene,
2654     IDirect3DDevice8Impl_Clear,
2655     IDirect3DDevice8Impl_SetTransform,
2656     IDirect3DDevice8Impl_GetTransform,
2657     IDirect3DDevice8Impl_MultiplyTransform,
2658     IDirect3DDevice8Impl_SetViewport,
2659     IDirect3DDevice8Impl_GetViewport,
2660     IDirect3DDevice8Impl_SetMaterial,
2661     IDirect3DDevice8Impl_GetMaterial,
2662     IDirect3DDevice8Impl_SetLight,
2663     IDirect3DDevice8Impl_GetLight,
2664     IDirect3DDevice8Impl_LightEnable,
2665     IDirect3DDevice8Impl_GetLightEnable,
2666     IDirect3DDevice8Impl_SetClipPlane,
2667     IDirect3DDevice8Impl_GetClipPlane,
2668     IDirect3DDevice8Impl_SetRenderState,
2669     IDirect3DDevice8Impl_GetRenderState,
2670     IDirect3DDevice8Impl_BeginStateBlock,
2671     IDirect3DDevice8Impl_EndStateBlock,
2672     IDirect3DDevice8Impl_ApplyStateBlock,
2673     IDirect3DDevice8Impl_CaptureStateBlock,
2674     IDirect3DDevice8Impl_DeleteStateBlock,
2675     IDirect3DDevice8Impl_CreateStateBlock,
2676     IDirect3DDevice8Impl_SetClipStatus,
2677     IDirect3DDevice8Impl_GetClipStatus,
2678     IDirect3DDevice8Impl_GetTexture,
2679     IDirect3DDevice8Impl_SetTexture,
2680     IDirect3DDevice8Impl_GetTextureStageState,
2681     IDirect3DDevice8Impl_SetTextureStageState,
2682     IDirect3DDevice8Impl_ValidateDevice,
2683     IDirect3DDevice8Impl_GetInfo,
2684     IDirect3DDevice8Impl_SetPaletteEntries,
2685     IDirect3DDevice8Impl_GetPaletteEntries,
2686     IDirect3DDevice8Impl_SetCurrentTexturePalette,
2687     IDirect3DDevice8Impl_GetCurrentTexturePalette,
2688     IDirect3DDevice8Impl_DrawPrimitive,
2689     IDirect3DDevice8Impl_DrawIndexedPrimitive,
2690     IDirect3DDevice8Impl_DrawPrimitiveUP,
2691     IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2692     IDirect3DDevice8Impl_ProcessVertices,
2693     IDirect3DDevice8Impl_CreateVertexShader,
2694     IDirect3DDevice8Impl_SetVertexShader,
2695     IDirect3DDevice8Impl_GetVertexShader,
2696     IDirect3DDevice8Impl_DeleteVertexShader,
2697     IDirect3DDevice8Impl_SetVertexShaderConstant,
2698     IDirect3DDevice8Impl_GetVertexShaderConstant,
2699     IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2700     IDirect3DDevice8Impl_GetVertexShaderFunction,
2701     IDirect3DDevice8Impl_SetStreamSource,
2702     IDirect3DDevice8Impl_GetStreamSource,
2703     IDirect3DDevice8Impl_SetIndices,
2704     IDirect3DDevice8Impl_GetIndices,
2705     IDirect3DDevice8Impl_CreatePixelShader,
2706     IDirect3DDevice8Impl_SetPixelShader,
2707     IDirect3DDevice8Impl_GetPixelShader,
2708     IDirect3DDevice8Impl_DeletePixelShader,
2709     IDirect3DDevice8Impl_SetPixelShaderConstant,
2710     IDirect3DDevice8Impl_GetPixelShaderConstant,
2711     IDirect3DDevice8Impl_GetPixelShaderFunction,
2712     IDirect3DDevice8Impl_DrawRectPatch,
2713     IDirect3DDevice8Impl_DrawTriPatch,
2714     IDirect3DDevice8Impl_DeletePatch
2715 };
2716
2717 static inline IDirect3DDevice8Impl *impl_from_IWineD3DDeviceParent(IWineD3DDeviceParent *iface)
2718 {
2719     return CONTAINING_RECORD(iface, IDirect3DDevice8Impl, IWineD3DDeviceParent_iface);
2720 }
2721
2722 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface,
2723         REFIID riid, void **object)
2724 {
2725     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2726     return IDirect3DDevice8Impl_QueryInterface(&This->IDirect3DDevice8_iface, riid, object);
2727 }
2728
2729 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2730 {
2731     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2732     return IDirect3DDevice8Impl_AddRef(&This->IDirect3DDevice8_iface);
2733 }
2734
2735 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2736 {
2737     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2738     return IDirect3DDevice8Impl_Release(&This->IDirect3DDevice8_iface);
2739 }
2740
2741 /* IWineD3DDeviceParent methods */
2742
2743 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2744 {
2745     TRACE("iface %p, device %p\n", iface, device);
2746 }
2747
2748 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2749         void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2750         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2751 {
2752     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2753     IDirect3DSurface8Impl *d3d_surface;
2754     BOOL lockable = TRUE;
2755     HRESULT hr;
2756
2757     TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
2758             "\tpool %#x, level %u, face %u, surface %p\n",
2759             iface, container_parent, width, height, format, usage, pool, level, face, surface);
2760
2761
2762     if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2763
2764     hr = IDirect3DDevice8Impl_CreateSurface(This, width, height,
2765             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2766             (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2767     if (FAILED(hr))
2768     {
2769         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2770         return hr;
2771     }
2772
2773     *surface = d3d_surface->wineD3DSurface;
2774     IWineD3DSurface_AddRef(*surface);
2775
2776     d3d_surface->container = container_parent;
2777     IUnknown_Release(d3d_surface->parentDevice);
2778     d3d_surface->parentDevice = NULL;
2779
2780     IDirect3DSurface8_Release((IDirect3DSurface8 *)d3d_surface);
2781     d3d_surface->forwardReference = container_parent;
2782
2783     return hr;
2784 }
2785
2786 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2787         void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
2788         WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
2789         IWineD3DSurface **surface)
2790 {
2791     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2792     IDirect3DSurface8Impl *d3d_surface;
2793     HRESULT hr;
2794
2795     TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2796             "\tmultisample_quality %u, lockable %u, surface %p\n",
2797             iface, container_parent, width, height, format, multisample_type, multisample_quality, lockable, surface);
2798
2799     hr = IDirect3DDevice8_CreateRenderTarget(&This->IDirect3DDevice8_iface, width, height,
2800             d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2801     if (FAILED(hr))
2802     {
2803         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2804         return hr;
2805     }
2806
2807     *surface = d3d_surface->wineD3DSurface;
2808     IWineD3DSurface_AddRef(*surface);
2809
2810     d3d_surface->container = (IUnknown *)&This->IDirect3DDevice8_iface;
2811     /* Implicit surfaces are created with an refcount of 0 */
2812     IUnknown_Release((IUnknown *)d3d_surface);
2813
2814     return hr;
2815 }
2816
2817 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2818         UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
2819         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2820 {
2821     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2822     IDirect3DSurface8Impl *d3d_surface;
2823     HRESULT hr;
2824
2825     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2826             "\tmultisample_quality %u, discard %u, surface %p\n",
2827             iface, width, height, format, multisample_type, multisample_quality, discard, surface);
2828
2829     hr = IDirect3DDevice8_CreateDepthStencilSurface(&This->IDirect3DDevice8_iface, width, height,
2830             d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2831     if (FAILED(hr))
2832     {
2833         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2834         return hr;
2835     }
2836
2837     *surface = d3d_surface->wineD3DSurface;
2838     IWineD3DSurface_AddRef(*surface);
2839
2840     d3d_surface->container = (IUnknown *)&This->IDirect3DDevice8_iface;
2841     /* Implicit surfaces are created with an refcount of 0 */
2842     IUnknown_Release((IUnknown *)d3d_surface);
2843
2844     return hr;
2845 }
2846
2847 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2848         void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2849         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2850 {
2851     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2852     IDirect3DVolume8Impl *object;
2853     HRESULT hr;
2854
2855     TRACE("iface %p, container_parent %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2856             iface, container_parent, width, height, depth, format, pool, usage, volume);
2857
2858     /* Allocate the storage for the device */
2859     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2860     if (!object)
2861     {
2862         FIXME("Allocation of memory failed\n");
2863         *volume = NULL;
2864         return D3DERR_OUTOFVIDEOMEMORY;
2865     }
2866
2867     hr = volume_init(object, This, width, height, depth, usage, format, pool);
2868     if (FAILED(hr))
2869     {
2870         WARN("Failed to initialize volume, hr %#x.\n", hr);
2871         HeapFree(GetProcessHeap(), 0, object);
2872         return hr;
2873     }
2874
2875     *volume = object->wineD3DVolume;
2876     IWineD3DVolume_AddRef(*volume);
2877     IDirect3DVolume8_Release(&object->IDirect3DVolume8_iface);
2878
2879     object->container = container_parent;
2880     object->forwardReference = container_parent;
2881
2882     TRACE("(%p) Created volume %p\n", iface, object);
2883
2884     return hr;
2885 }
2886
2887 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2888         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2889 {
2890     IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2891     IDirect3DSwapChain8Impl *d3d_swapchain;
2892     D3DPRESENT_PARAMETERS local_parameters;
2893     HRESULT hr;
2894
2895     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2896
2897     /* Copy the presentation parameters */
2898     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2899     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2900     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2901     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2902     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2903     local_parameters.SwapEffect = present_parameters->SwapEffect;
2904     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2905     local_parameters.Windowed = present_parameters->Windowed;
2906     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2907     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2908     local_parameters.Flags = present_parameters->Flags;
2909     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2910     local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
2911
2912     hr = IDirect3DDevice8_CreateAdditionalSwapChain(&This->IDirect3DDevice8_iface,
2913             &local_parameters, (IDirect3DSwapChain8 **)&d3d_swapchain);
2914     if (FAILED(hr))
2915     {
2916         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2917         *swapchain = NULL;
2918         return hr;
2919     }
2920
2921     *swapchain = d3d_swapchain->wineD3DSwapChain;
2922     IUnknown_Release(d3d_swapchain->parentDevice);
2923     d3d_swapchain->parentDevice = NULL;
2924
2925     /* Copy back the presentation parameters */
2926     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2927     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2928     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2929     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2930     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2931     present_parameters->SwapEffect = local_parameters.SwapEffect;
2932     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2933     present_parameters->Windowed = local_parameters.Windowed;
2934     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2935     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2936     present_parameters->Flags = local_parameters.Flags;
2937     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2938     present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
2939
2940     return hr;
2941 }
2942
2943 static const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
2944 {
2945     /* IUnknown methods */
2946     device_parent_QueryInterface,
2947     device_parent_AddRef,
2948     device_parent_Release,
2949     /* IWineD3DDeviceParent methods */
2950     device_parent_WineD3DDeviceCreated,
2951     device_parent_CreateSurface,
2952     device_parent_CreateRenderTarget,
2953     device_parent_CreateDepthStencilSurface,
2954     device_parent_CreateVolume,
2955     device_parent_CreateSwapChain,
2956 };
2957
2958 static void setup_fpu(void)
2959 {
2960 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2961     WORD cw;
2962     __asm__ volatile ("fnstcw %0" : "=m" (cw));
2963     cw = (cw & ~0xf3f) | 0x3f;
2964     __asm__ volatile ("fldcw %0" : : "m" (cw));
2965 #else
2966     FIXME("FPU setup not implemented for this platform.\n");
2967 #endif
2968 }
2969
2970 HRESULT device_init(IDirect3DDevice8Impl *device, struct wined3d *wined3d, UINT adapter,
2971         D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
2972 {
2973     WINED3DPRESENT_PARAMETERS wined3d_parameters;
2974     HRESULT hr;
2975
2976     device->IDirect3DDevice8_iface.lpVtbl = &Direct3DDevice8_Vtbl;
2977     device->IWineD3DDeviceParent_iface.lpVtbl = &d3d8_wined3d_device_parent_vtbl;
2978     device->ref = 1;
2979     device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
2980             D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
2981     if (!device->handle_table.entries)
2982     {
2983         ERR("Failed to allocate handle table memory.\n");
2984         return E_OUTOFMEMORY;
2985     }
2986     device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
2987
2988     if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
2989
2990     wined3d_mutex_lock();
2991     hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags,
2992             &device->IWineD3DDeviceParent_iface, &device->WineD3DDevice);
2993     if (FAILED(hr))
2994     {
2995         WARN("Failed to create wined3d device, hr %#x.\n", hr);
2996         wined3d_mutex_unlock();
2997         HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
2998         return hr;
2999     }
3000
3001     if (!parameters->Windowed)
3002     {
3003         HWND device_window = parameters->hDeviceWindow;
3004
3005         if (!focus_window) focus_window = device_window;
3006         if (FAILED(hr = IWineD3DDevice_AcquireFocusWindow(device->WineD3DDevice, focus_window)))
3007         {
3008             ERR("Failed to acquire focus window, hr %#x.\n", hr);
3009             IWineD3DDevice_Release(device->WineD3DDevice);
3010             wined3d_mutex_unlock();
3011             HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3012             return hr;
3013         }
3014
3015         if (!device_window) device_window = focus_window;
3016         IWineD3DDevice_SetupFullscreenWindow(device->WineD3DDevice, device_window,
3017                 parameters->BackBufferWidth,
3018                 parameters->BackBufferHeight);
3019     }
3020
3021     if (flags & D3DCREATE_MULTITHREADED) IWineD3DDevice_SetMultithreaded(device->WineD3DDevice);
3022
3023     wined3d_parameters.BackBufferWidth = parameters->BackBufferWidth;
3024     wined3d_parameters.BackBufferHeight = parameters->BackBufferHeight;
3025     wined3d_parameters.BackBufferFormat = wined3dformat_from_d3dformat(parameters->BackBufferFormat);
3026     wined3d_parameters.BackBufferCount = parameters->BackBufferCount;
3027     wined3d_parameters.MultiSampleType = parameters->MultiSampleType;
3028     wined3d_parameters.MultiSampleQuality = 0; /* d3d9 only */
3029     wined3d_parameters.SwapEffect = parameters->SwapEffect;
3030     wined3d_parameters.hDeviceWindow = parameters->hDeviceWindow;
3031     wined3d_parameters.Windowed = parameters->Windowed;
3032     wined3d_parameters.EnableAutoDepthStencil = parameters->EnableAutoDepthStencil;
3033     wined3d_parameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(parameters->AutoDepthStencilFormat);
3034     wined3d_parameters.Flags = parameters->Flags;
3035     wined3d_parameters.FullScreen_RefreshRateInHz = parameters->FullScreen_RefreshRateInHz;
3036     wined3d_parameters.PresentationInterval = parameters->FullScreen_PresentationInterval;
3037     wined3d_parameters.AutoRestoreDisplayMode = TRUE;
3038
3039     hr = IWineD3DDevice_Init3D(device->WineD3DDevice, &wined3d_parameters);
3040     if (FAILED(hr))
3041     {
3042         WARN("Failed to initialize 3D, hr %#x.\n", hr);
3043         IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3044         IWineD3DDevice_Release(device->WineD3DDevice);
3045         wined3d_mutex_unlock();
3046         HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3047         return hr;
3048     }
3049
3050     hr = IWineD3DDevice_SetRenderState(device->WineD3DDevice, WINED3DRS_POINTSIZE_MIN, 0);
3051     wined3d_mutex_unlock();
3052     if (FAILED(hr))
3053     {
3054         ERR("Failed to set minimum pointsize, hr %#x.\n", hr);
3055         goto err;
3056     }
3057
3058     parameters->BackBufferWidth = wined3d_parameters.BackBufferWidth;
3059     parameters->BackBufferHeight = wined3d_parameters.BackBufferHeight;
3060     parameters->BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters.BackBufferFormat);
3061     parameters->BackBufferCount = wined3d_parameters.BackBufferCount;
3062     parameters->MultiSampleType = wined3d_parameters.MultiSampleType;
3063     parameters->SwapEffect = wined3d_parameters.SwapEffect;
3064     parameters->hDeviceWindow = wined3d_parameters.hDeviceWindow;
3065     parameters->Windowed = wined3d_parameters.Windowed;
3066     parameters->EnableAutoDepthStencil = wined3d_parameters.EnableAutoDepthStencil;
3067     parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(wined3d_parameters.AutoDepthStencilFormat);
3068     parameters->Flags = wined3d_parameters.Flags;
3069     parameters->FullScreen_RefreshRateInHz = wined3d_parameters.FullScreen_RefreshRateInHz;
3070     parameters->FullScreen_PresentationInterval = wined3d_parameters.PresentationInterval;
3071
3072     device->declArraySize = 16;
3073     device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3074     if (!device->decls)
3075     {
3076         ERR("Failed to allocate FVF vertex delcaration map memory.\n");
3077         hr = E_OUTOFMEMORY;
3078         goto err;
3079     }
3080
3081     return D3D_OK;
3082
3083 err:
3084     wined3d_mutex_lock();
3085     IWineD3DDevice_Uninit3D(device->WineD3DDevice, D3D8CB_DestroySwapChain);
3086     IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3087     IWineD3DDevice_Release(device->WineD3DDevice);
3088     wined3d_mutex_unlock();
3089     HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3090     return hr;
3091 }