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