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