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