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