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