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