d3d8: Get rid of the implicit swapchain refcounting hacks.
[wine.git] / dlls / d3d8 / device.c
blob54ee3f2ae226fea1af3e76f70b7130d1abb1f72a
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
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.
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.
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
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
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"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
41 BYTE *c = (BYTE *)&format;
43 /* Don't translate FOURCC formats */
44 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
46 switch(format)
48 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
49 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
50 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
51 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
52 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
53 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
54 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
55 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
56 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
57 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
58 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
59 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
60 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
61 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
73 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
74 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
75 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
76 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
77 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
78 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
79 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
80 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
81 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
82 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
83 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
84 default:
85 FIXME("Unhandled wined3d format %#x.\n", format);
86 return D3DFMT_UNKNOWN;
90 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
92 BYTE *c = (BYTE *)&format;
94 /* Don't translate FOURCC formats */
95 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
97 switch(format)
99 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
100 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
101 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
102 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
103 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
104 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
105 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
106 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
107 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
108 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
109 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
110 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
111 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
112 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
113 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
114 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
115 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
116 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
117 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
118 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
119 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
120 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
121 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
122 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
123 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
124 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
125 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
126 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
127 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
128 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
129 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
130 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
131 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
132 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
133 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
134 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
135 default:
136 FIXME("Unhandled D3DFORMAT %#x\n", format);
137 return WINED3DFMT_UNKNOWN;
141 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
143 switch(primitive_type)
145 case D3DPT_POINTLIST:
146 return primitive_count;
148 case D3DPT_LINELIST:
149 return primitive_count * 2;
151 case D3DPT_LINESTRIP:
152 return primitive_count + 1;
154 case D3DPT_TRIANGLELIST:
155 return primitive_count * 3;
157 case D3DPT_TRIANGLESTRIP:
158 case D3DPT_TRIANGLEFAN:
159 return primitive_count + 2;
161 default:
162 FIXME("Unhandled primitive type %#x\n", primitive_type);
163 return 0;
167 /* Handle table functions */
168 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
170 struct d3d8_handle_entry *entry;
172 if (t->free_entries)
174 DWORD index = t->free_entries - t->entries;
175 /* Use a free handle */
176 entry = t->free_entries;
177 if (entry->type != D3D8_HANDLE_FREE)
179 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
180 return D3D8_INVALID_HANDLE;
182 t->free_entries = entry->object;
183 entry->object = object;
184 entry->type = type;
186 return index;
189 if (!(t->entry_count < t->table_size))
191 /* Grow the table */
192 UINT new_size = t->table_size + (t->table_size >> 1);
193 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
194 0, t->entries, new_size * sizeof(*t->entries));
195 if (!new_entries)
197 ERR("Failed to grow the handle table.\n");
198 return D3D8_INVALID_HANDLE;
200 t->entries = new_entries;
201 t->table_size = new_size;
204 entry = &t->entries[t->entry_count];
205 entry->object = object;
206 entry->type = type;
208 return t->entry_count++;
211 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
213 struct d3d8_handle_entry *entry;
214 void *object;
216 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
218 WARN("Invalid handle %u passed.\n", handle);
219 return NULL;
222 entry = &t->entries[handle];
223 if (entry->type != type)
225 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
226 return NULL;
229 object = entry->object;
230 entry->object = t->free_entries;
231 entry->type = D3D8_HANDLE_FREE;
232 t->free_entries = entry;
234 return object;
237 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
239 struct d3d8_handle_entry *entry;
241 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
243 WARN("Invalid handle %u passed.\n", handle);
244 return NULL;
247 entry = &t->entries[handle];
248 if (entry->type != type)
250 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
251 return NULL;
254 return entry->object;
257 static ULONG WINAPI D3D8CB_DestroySwapChain(IWineD3DSwapChain *swapchain)
259 TRACE("swapchain %p.\n", swapchain);
261 return IWineD3DSwapChain_Release(swapchain);
264 static inline IDirect3DDevice8Impl *impl_from_IDirect3DDevice8(IDirect3DDevice8 *iface)
266 return CONTAINING_RECORD(iface, IDirect3DDevice8Impl, IDirect3DDevice8_iface);
269 static HRESULT WINAPI IDirect3DDevice8Impl_QueryInterface(IDirect3DDevice8 *iface, REFIID riid,
270 void **ppobj)
272 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
274 TRACE("iface %p, riid %s, object %p.\n",
275 iface, debugstr_guid(riid), ppobj);
277 if (IsEqualGUID(riid, &IID_IUnknown)
278 || IsEqualGUID(riid, &IID_IDirect3DDevice8)) {
279 IUnknown_AddRef(iface);
280 *ppobj = This;
281 return S_OK;
284 if (IsEqualGUID(riid, &IID_IWineD3DDeviceParent))
286 IWineD3DDeviceParent_AddRef(&This->IWineD3DDeviceParent_iface);
287 *ppobj = &This->IWineD3DDeviceParent_iface;
288 return S_OK;
291 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
292 *ppobj = NULL;
293 return E_NOINTERFACE;
296 static ULONG WINAPI IDirect3DDevice8Impl_AddRef(IDirect3DDevice8 *iface)
298 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
299 ULONG ref = InterlockedIncrement(&This->ref);
301 TRACE("%p increasing refcount to %u.\n", iface, ref);
303 return ref;
306 static ULONG WINAPI IDirect3DDevice8Impl_Release(IDirect3DDevice8 *iface)
308 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
309 ULONG ref;
311 if (This->inDestruction) return 0;
312 ref = InterlockedDecrement(&This->ref);
314 TRACE("%p decreasing refcount to %u.\n", iface, ref);
316 if (ref == 0) {
317 unsigned i;
319 TRACE("Releasing wined3d device %p\n", This->WineD3DDevice);
321 wined3d_mutex_lock();
323 This->inDestruction = TRUE;
325 for(i = 0; i < This->numConvertedDecls; i++) {
326 IDirect3DVertexDeclaration8_Release(This->decls[i].decl);
328 HeapFree(GetProcessHeap(), 0, This->decls);
330 IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D8CB_DestroySwapChain);
331 IWineD3DDevice_ReleaseFocusWindow(This->WineD3DDevice);
332 IWineD3DDevice_Release(This->WineD3DDevice);
333 HeapFree(GetProcessHeap(), 0, This->handle_table.entries);
334 HeapFree(GetProcessHeap(), 0, This);
336 wined3d_mutex_unlock();
338 return ref;
341 /* IDirect3DDevice Interface follow: */
342 static HRESULT WINAPI IDirect3DDevice8Impl_TestCooperativeLevel(IDirect3DDevice8 *iface)
344 TRACE("iface %p.\n", iface);
346 return D3D_OK;
349 static UINT WINAPI IDirect3DDevice8Impl_GetAvailableTextureMem(IDirect3DDevice8 *iface)
351 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
352 HRESULT hr;
354 TRACE("iface %p.\n", iface);
356 wined3d_mutex_lock();
357 hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
358 wined3d_mutex_unlock();
360 return hr;
363 static HRESULT WINAPI IDirect3DDevice8Impl_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface,
364 DWORD Bytes)
366 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
367 HRESULT hr;
369 TRACE("iface %p, byte_count %u.\n", iface, Bytes);
370 FIXME("Byte count ignored.\n");
372 wined3d_mutex_lock();
373 hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
374 wined3d_mutex_unlock();
376 return hr;
379 static HRESULT WINAPI IDirect3DDevice8Impl_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **ppD3D8)
381 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
382 struct wined3d *wined3d;
383 HRESULT hr;
385 TRACE("iface %p, d3d8 %p.\n", iface, ppD3D8);
387 if (NULL == ppD3D8) {
388 return D3DERR_INVALIDCALL;
391 wined3d_mutex_lock();
392 hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &wined3d);
393 if (SUCCEEDED(hr) && wined3d)
395 *ppD3D8 = wined3d_get_parent(wined3d);
396 IDirect3D8_AddRef(*ppD3D8);
397 wined3d_decref(wined3d);
399 else
401 FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
402 *ppD3D8 = NULL;
404 wined3d_mutex_unlock();
406 TRACE("(%p) returning %p\n",This , *ppD3D8);
408 return hr;
411 static HRESULT WINAPI IDirect3DDevice8Impl_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *pCaps)
413 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
414 HRESULT hrc = D3D_OK;
415 WINED3DCAPS *pWineCaps;
417 TRACE("iface %p, caps %p.\n", iface, pCaps);
419 if(NULL == pCaps){
420 return D3DERR_INVALIDCALL;
422 pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
423 if(pWineCaps == NULL){
424 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
427 wined3d_mutex_lock();
428 hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
429 wined3d_mutex_unlock();
431 fixup_caps(pWineCaps);
432 WINECAPSTOD3D8CAPS(pCaps, pWineCaps)
433 HeapFree(GetProcessHeap(), 0, pWineCaps);
435 TRACE("Returning %p %p\n", This, pCaps);
436 return hrc;
439 static HRESULT WINAPI IDirect3DDevice8Impl_GetDisplayMode(IDirect3DDevice8 *iface,
440 D3DDISPLAYMODE *pMode)
442 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
443 HRESULT hr;
445 TRACE("iface %p, mode %p.\n", iface, pMode);
447 wined3d_mutex_lock();
448 hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, 0, (WINED3DDISPLAYMODE *) pMode);
449 wined3d_mutex_unlock();
451 if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
453 return hr;
456 static HRESULT WINAPI IDirect3DDevice8Impl_GetCreationParameters(IDirect3DDevice8 *iface,
457 D3DDEVICE_CREATION_PARAMETERS *pParameters)
459 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
460 HRESULT hr;
462 TRACE("iface %p, parameters %p.\n", iface, pParameters);
464 wined3d_mutex_lock();
465 hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
466 wined3d_mutex_unlock();
468 return hr;
471 static HRESULT WINAPI IDirect3DDevice8Impl_SetCursorProperties(IDirect3DDevice8 *iface,
472 UINT XHotSpot, UINT YHotSpot, IDirect3DSurface8 *pCursorBitmap)
474 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
475 IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl*)pCursorBitmap;
476 HRESULT hr;
478 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
479 iface, XHotSpot, YHotSpot, pCursorBitmap);
481 if (!pCursorBitmap)
483 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
484 return D3DERR_INVALIDCALL;
487 wined3d_mutex_lock();
488 hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice,XHotSpot,YHotSpot,pSurface->wineD3DSurface);
489 wined3d_mutex_unlock();
491 return hr;
494 static void WINAPI IDirect3DDevice8Impl_SetCursorPosition(IDirect3DDevice8 *iface,
495 UINT XScreenSpace, UINT YScreenSpace, DWORD Flags)
497 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
499 TRACE("iface %p, x %u, y %u, flags %#x.\n",
500 iface, XScreenSpace, YScreenSpace, Flags);
502 wined3d_mutex_lock();
503 IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
504 wined3d_mutex_unlock();
507 static BOOL WINAPI IDirect3DDevice8Impl_ShowCursor(IDirect3DDevice8 *iface, BOOL bShow)
509 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
510 BOOL ret;
512 TRACE("iface %p, show %#x.\n", iface, bShow);
514 wined3d_mutex_lock();
515 ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
516 wined3d_mutex_unlock();
518 return ret;
521 static HRESULT WINAPI IDirect3DDevice8Impl_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
522 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
524 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
525 IDirect3DSwapChain8Impl *object;
526 HRESULT hr;
528 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
529 iface, present_parameters, swapchain);
531 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
532 if (!object)
534 ERR("Failed to allocate swapchain memory.\n");
535 return E_OUTOFMEMORY;
538 hr = swapchain_init(object, This, present_parameters);
539 if (FAILED(hr))
541 WARN("Failed to initialize swapchain, hr %#x.\n", hr);
542 HeapFree(GetProcessHeap(), 0, object);
543 return hr;
546 TRACE("Created swapchain %p.\n", object);
547 *swapchain = &object->IDirect3DSwapChain8_iface;
549 return D3D_OK;
552 static HRESULT WINAPI IDirect3DDevice8Impl_Reset(IDirect3DDevice8 *iface,
553 D3DPRESENT_PARAMETERS *pPresentationParameters)
555 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
556 WINED3DPRESENT_PARAMETERS localParameters;
557 HRESULT hr;
559 TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
561 localParameters.BackBufferWidth = pPresentationParameters->BackBufferWidth;
562 localParameters.BackBufferHeight = pPresentationParameters->BackBufferHeight;
563 localParameters.BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
564 localParameters.BackBufferCount = pPresentationParameters->BackBufferCount;
565 localParameters.MultiSampleType = pPresentationParameters->MultiSampleType;
566 localParameters.MultiSampleQuality = 0; /* d3d9 only */
567 localParameters.SwapEffect = pPresentationParameters->SwapEffect;
568 localParameters.hDeviceWindow = pPresentationParameters->hDeviceWindow;
569 localParameters.Windowed = pPresentationParameters->Windowed;
570 localParameters.EnableAutoDepthStencil = pPresentationParameters->EnableAutoDepthStencil;
571 localParameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
572 localParameters.Flags = pPresentationParameters->Flags;
573 localParameters.FullScreen_RefreshRateInHz = pPresentationParameters->FullScreen_RefreshRateInHz;
574 localParameters.PresentationInterval = pPresentationParameters->FullScreen_PresentationInterval;
575 localParameters.AutoRestoreDisplayMode = TRUE;
577 wined3d_mutex_lock();
578 hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
579 if(SUCCEEDED(hr)) {
580 hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, WINED3DRS_POINTSIZE_MIN, 0);
582 wined3d_mutex_unlock();
584 pPresentationParameters->BackBufferWidth = localParameters.BackBufferWidth;
585 pPresentationParameters->BackBufferHeight = localParameters.BackBufferHeight;
586 pPresentationParameters->BackBufferFormat = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
587 pPresentationParameters->BackBufferCount = localParameters.BackBufferCount;
588 pPresentationParameters->MultiSampleType = localParameters.MultiSampleType;
589 pPresentationParameters->SwapEffect = localParameters.SwapEffect;
590 pPresentationParameters->hDeviceWindow = localParameters.hDeviceWindow;
591 pPresentationParameters->Windowed = localParameters.Windowed;
592 pPresentationParameters->EnableAutoDepthStencil = localParameters.EnableAutoDepthStencil;
593 pPresentationParameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
594 pPresentationParameters->Flags = localParameters.Flags;
595 pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
596 pPresentationParameters->FullScreen_PresentationInterval = localParameters.PresentationInterval;
598 return hr;
601 static HRESULT WINAPI IDirect3DDevice8Impl_Present(IDirect3DDevice8 *iface, const RECT *pSourceRect,
602 const RECT *pDestRect, HWND hDestWindowOverride, const RGNDATA *pDirtyRegion)
604 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
605 HRESULT hr;
607 TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
608 iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
610 wined3d_mutex_lock();
611 hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
612 wined3d_mutex_unlock();
614 return hr;
617 static HRESULT WINAPI IDirect3DDevice8Impl_GetBackBuffer(IDirect3DDevice8 *iface,
618 UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface8 **ppBackBuffer)
620 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
621 IWineD3DSurface *retSurface = NULL;
622 HRESULT hr;
624 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
625 iface, BackBuffer, Type, ppBackBuffer);
627 wined3d_mutex_lock();
628 hr = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, 0, BackBuffer, (WINED3DBACKBUFFER_TYPE)Type, &retSurface);
629 if (SUCCEEDED(hr) && retSurface && ppBackBuffer)
631 *ppBackBuffer = IWineD3DSurface_GetParent(retSurface);
632 IDirect3DSurface8_AddRef(*ppBackBuffer);
633 IWineD3DSurface_Release(retSurface);
635 wined3d_mutex_unlock();
637 return hr;
640 static HRESULT WINAPI IDirect3DDevice8Impl_GetRasterStatus(IDirect3DDevice8 *iface,
641 D3DRASTER_STATUS *pRasterStatus)
643 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
644 HRESULT hr;
646 TRACE("iface %p, raster_status %p.\n", iface, pRasterStatus);
648 wined3d_mutex_lock();
649 hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, 0, (WINED3DRASTER_STATUS *) pRasterStatus);
650 wined3d_mutex_unlock();
652 return hr;
655 static void WINAPI IDirect3DDevice8Impl_SetGammaRamp(IDirect3DDevice8 *iface, DWORD Flags,
656 const D3DGAMMARAMP *pRamp)
658 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
660 TRACE("iface %p, flags %#x, ramp %p.\n", iface, Flags, pRamp);
662 /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
663 wined3d_mutex_lock();
664 IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, 0, Flags, (CONST WINED3DGAMMARAMP *) pRamp);
665 wined3d_mutex_unlock();
668 static void WINAPI IDirect3DDevice8Impl_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *pRamp)
670 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
672 TRACE("iface %p, ramp %p.\n", iface, pRamp);
674 /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
675 wined3d_mutex_lock();
676 IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, 0, (WINED3DGAMMARAMP *) pRamp);
677 wined3d_mutex_unlock();
680 static HRESULT WINAPI IDirect3DDevice8Impl_CreateTexture(IDirect3DDevice8 *iface,
681 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
682 D3DPOOL pool, IDirect3DTexture8 **texture)
684 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
685 IDirect3DTexture8Impl *object;
686 HRESULT hr;
688 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
689 iface, width, height, levels, usage, format, pool, texture);
691 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
692 if (!object)
694 ERR("Failed to allocate texture memory.\n");
695 return D3DERR_OUTOFVIDEOMEMORY;
698 hr = texture_init(object, This, width, height, levels, usage, format, pool);
699 if (FAILED(hr))
701 WARN("Failed to initialize texture, hr %#x.\n", hr);
702 HeapFree(GetProcessHeap(), 0, object);
703 return hr;
706 TRACE("Created texture %p.\n", object);
707 *texture = &object->IDirect3DTexture8_iface;
709 return D3D_OK;
712 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVolumeTexture(IDirect3DDevice8 *iface,
713 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
714 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
716 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
717 IDirect3DVolumeTexture8Impl *object;
718 HRESULT hr;
720 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
721 iface, width, height, depth, levels, usage, format, pool, texture);
723 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
724 if (!object)
726 ERR("Failed to allocate volume texture memory.\n");
727 return D3DERR_OUTOFVIDEOMEMORY;
730 hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
731 if (FAILED(hr))
733 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
734 HeapFree(GetProcessHeap(), 0, object);
735 return hr;
738 TRACE("Created volume texture %p.\n", object);
739 *texture = &object->IDirect3DVolumeTexture8_iface;
741 return D3D_OK;
744 static HRESULT WINAPI IDirect3DDevice8Impl_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
745 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
747 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
748 IDirect3DCubeTexture8Impl *object;
749 HRESULT hr;
751 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
752 iface, edge_length, levels, usage, format, pool, texture);
754 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
755 if (!object)
757 ERR("Failed to allocate cube texture memory.\n");
758 return D3DERR_OUTOFVIDEOMEMORY;
761 hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
762 if (FAILED(hr))
764 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
765 HeapFree(GetProcessHeap(), 0, object);
766 return hr;
769 TRACE("Created cube texture %p.\n", object);
770 *texture = &object->IDirect3DCubeTexture8_iface;
772 return hr;
775 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
776 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
778 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
779 IDirect3DVertexBuffer8Impl *object;
780 HRESULT hr;
782 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
783 iface, size, usage, fvf, pool, buffer);
785 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
786 if (!object)
788 ERR("Failed to allocate buffer memory.\n");
789 return D3DERR_OUTOFVIDEOMEMORY;
792 hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
793 if (FAILED(hr))
795 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
796 HeapFree(GetProcessHeap(), 0, object);
797 return hr;
800 TRACE("Created vertex buffer %p.\n", object);
801 *buffer = (IDirect3DVertexBuffer8 *)object;
803 return D3D_OK;
806 static HRESULT WINAPI IDirect3DDevice8Impl_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
807 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
809 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
810 IDirect3DIndexBuffer8Impl *object;
811 HRESULT hr;
813 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
814 iface, size, usage, format, pool, buffer);
816 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
817 if (!object)
819 ERR("Failed to allocate buffer memory.\n");
820 return D3DERR_OUTOFVIDEOMEMORY;
823 hr = indexbuffer_init(object, This, size, usage, format, pool);
824 if (FAILED(hr))
826 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
827 HeapFree(GetProcessHeap(), 0, object);
828 return hr;
831 TRACE("Created index buffer %p.\n", object);
832 *buffer = (IDirect3DIndexBuffer8 *)object;
834 return D3D_OK;
837 static HRESULT IDirect3DDevice8Impl_CreateSurface(IDirect3DDevice8Impl *device, UINT Width,
838 UINT Height, D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level,
839 IDirect3DSurface8 **ppSurface, UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample,
840 DWORD MultisampleQuality)
842 IDirect3DSurface8Impl *object;
843 HRESULT hr;
845 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p,\n"
846 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
847 device, Width, Height, Format, Lockable, Discard, Level, ppSurface,
848 Usage, Pool, MultiSample, MultisampleQuality);
850 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface8Impl));
851 if (!object)
853 FIXME("Failed to allocate surface memory.\n");
854 return D3DERR_OUTOFVIDEOMEMORY;
857 hr = surface_init(object, device, Width, Height, Format, Lockable, Discard, Level, Usage,
858 Pool, MultiSample, MultisampleQuality);
859 if (FAILED(hr))
861 WARN("Failed to initialize surface, hr %#x.\n", hr);
862 HeapFree(GetProcessHeap(), 0, object);
863 return hr;
866 TRACE("Created surface %p.\n", object);
867 *ppSurface = (IDirect3DSurface8 *)object;
869 return D3D_OK;
872 static HRESULT WINAPI IDirect3DDevice8Impl_CreateRenderTarget(IDirect3DDevice8 *iface, UINT Width,
873 UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, BOOL Lockable,
874 IDirect3DSurface8 **ppSurface)
876 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
877 HRESULT hr;
879 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
880 iface, Width, Height, Format, MultiSample, Lockable, ppSurface);
882 hr = IDirect3DDevice8Impl_CreateSurface(This, Width, Height, Format, Lockable,
883 FALSE /* Discard */, 0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,
884 MultiSample, 0);
886 return hr;
889 static HRESULT WINAPI IDirect3DDevice8Impl_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
890 UINT Width, UINT Height, D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
891 IDirect3DSurface8 **ppSurface)
893 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
894 HRESULT hr;
896 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
897 iface, Width, Height, Format, MultiSample, ppSurface);
899 /* TODO: Verify that Discard is false */
900 hr = IDirect3DDevice8Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */, FALSE,
901 0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample, 0);
903 return hr;
906 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
907 static HRESULT WINAPI IDirect3DDevice8Impl_CreateImageSurface(IDirect3DDevice8 *iface, UINT Width,
908 UINT Height, D3DFORMAT Format, IDirect3DSurface8 **ppSurface)
910 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
911 HRESULT hr;
913 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
914 iface, Width, Height, Format, ppSurface);
916 hr = IDirect3DDevice8Impl_CreateSurface(This, Width, Height, Format, TRUE /* Lockable */,
917 FALSE /* Discard */, 0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */,
918 D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
920 return hr;
923 static HRESULT WINAPI IDirect3DDevice8Impl_CopyRects(IDirect3DDevice8 *iface,
924 IDirect3DSurface8 *pSourceSurface, const RECT *pSourceRects, UINT cRects,
925 IDirect3DSurface8 *pDestinationSurface, const POINT *pDestPoints)
927 IDirect3DSurface8Impl *Source = (IDirect3DSurface8Impl *) pSourceSurface;
928 IDirect3DSurface8Impl *Dest = (IDirect3DSurface8Impl *) pDestinationSurface;
929 enum wined3d_format_id srcFormat, destFormat;
930 struct wined3d_resource_desc wined3d_desc;
931 struct wined3d_resource *wined3d_resource;
933 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
934 iface, pSourceSurface, pSourceRects, cRects, pDestinationSurface, pDestPoints);
936 /* Check that the source texture is in WINED3DPOOL_SYSTEMMEM and the
937 * destination texture is in WINED3DPOOL_DEFAULT. */
939 wined3d_mutex_lock();
940 wined3d_resource = IWineD3DSurface_GetResource(Source->wineD3DSurface);
941 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
942 srcFormat = wined3d_desc.format;
944 wined3d_resource = IWineD3DSurface_GetResource(Dest->wineD3DSurface);
945 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
946 destFormat = wined3d_desc.format;
948 /* Check that the source and destination formats match */
949 if (srcFormat != destFormat && WINED3DFMT_UNKNOWN != destFormat)
951 WARN("Source %p format must match the dest %p format, returning D3DERR_INVALIDCALL.\n",
952 pSourceSurface, pDestinationSurface);
953 wined3d_mutex_unlock();
954 return D3DERR_INVALIDCALL;
956 else if (WINED3DFMT_UNKNOWN == destFormat)
958 TRACE("(%p) : Converting destination surface from WINED3DFMT_UNKNOWN to the source format\n", iface);
959 IWineD3DSurface_SetFormat(Dest->wineD3DSurface, srcFormat);
962 /* Quick if complete copy ... */
963 if (cRects == 0 && pSourceRects == NULL && pDestPoints == NULL) {
964 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
965 } else {
966 unsigned int i;
967 /* Copy rect by rect */
968 if (NULL != pSourceRects && NULL != pDestPoints) {
969 for (i = 0; i < cRects; ++i) {
970 IWineD3DSurface_BltFast(Dest->wineD3DSurface, pDestPoints[i].x, pDestPoints[i].y, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
972 } else {
973 for (i = 0; i < cRects; ++i) {
974 IWineD3DSurface_BltFast(Dest->wineD3DSurface, 0, 0, Source->wineD3DSurface, &pSourceRects[i], WINEDDBLTFAST_NOCOLORKEY);
978 wined3d_mutex_unlock();
980 return WINED3D_OK;
983 static HRESULT WINAPI IDirect3DDevice8Impl_UpdateTexture(IDirect3DDevice8 *iface,
984 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
986 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
987 HRESULT hr;
989 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
991 wined3d_mutex_lock();
992 hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,
993 ((IDirect3DBaseTexture8Impl *)src_texture)->wined3d_texture,
994 ((IDirect3DBaseTexture8Impl *)dst_texture)->wined3d_texture);
995 wined3d_mutex_unlock();
997 return hr;
1000 static HRESULT WINAPI IDirect3DDevice8Impl_GetFrontBuffer(IDirect3DDevice8 *iface,
1001 IDirect3DSurface8 *pDestSurface)
1003 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1004 IDirect3DSurface8Impl *destSurface = (IDirect3DSurface8Impl *)pDestSurface;
1005 HRESULT hr;
1007 TRACE("iface %p, dst_surface %p.\n", iface, pDestSurface);
1009 if (pDestSurface == NULL) {
1010 WARN("(%p) : Caller passed NULL as pDestSurface returning D3DERR_INVALIDCALL\n", This);
1011 return D3DERR_INVALIDCALL;
1014 wined3d_mutex_lock();
1015 hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, 0, destSurface->wineD3DSurface);
1016 wined3d_mutex_unlock();
1018 return hr;
1021 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderTarget(IDirect3DDevice8 *iface,
1022 IDirect3DSurface8 *pRenderTarget, IDirect3DSurface8 *pNewZStencil)
1024 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1025 IDirect3DSurface8Impl *pSurface = (IDirect3DSurface8Impl *)pRenderTarget;
1026 IDirect3DSurface8Impl *pZSurface = (IDirect3DSurface8Impl *)pNewZStencil;
1027 IWineD3DSurface *original_ds = NULL;
1028 HRESULT hr;
1030 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, pRenderTarget, pNewZStencil);
1032 wined3d_mutex_lock();
1034 if (pZSurface)
1036 struct wined3d_resource_desc ds_desc, rt_desc;
1037 struct wined3d_resource *wined3d_resource;
1039 wined3d_resource = IWineD3DSurface_GetResource(pZSurface->wineD3DSurface);
1040 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1041 wined3d_resource = IWineD3DSurface_GetResource(pSurface->wineD3DSurface);
1042 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1044 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1046 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1047 wined3d_mutex_unlock();
1048 return D3DERR_INVALIDCALL;
1052 hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &original_ds);
1053 if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
1055 hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, pZSurface ? pZSurface->wineD3DSurface : NULL);
1056 if (SUCCEEDED(hr) && pSurface)
1057 hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, 0, pSurface->wineD3DSurface, TRUE);
1058 if (FAILED(hr)) IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, original_ds);
1060 if (original_ds) IWineD3DSurface_Release(original_ds);
1062 wined3d_mutex_unlock();
1064 return hr;
1067 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderTarget(IDirect3DDevice8 *iface,
1068 IDirect3DSurface8 **ppRenderTarget)
1070 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1071 IWineD3DSurface *pRenderTarget;
1072 HRESULT hr;
1074 TRACE("iface %p, render_target %p.\n", iface, ppRenderTarget);
1076 if (ppRenderTarget == NULL) {
1077 return D3DERR_INVALIDCALL;
1080 wined3d_mutex_lock();
1081 hr = IWineD3DDevice_GetRenderTarget(This->WineD3DDevice, 0, &pRenderTarget);
1082 if (SUCCEEDED(hr) && pRenderTarget)
1084 *ppRenderTarget = IWineD3DSurface_GetParent(pRenderTarget);
1085 IDirect3DSurface8_AddRef(*ppRenderTarget);
1086 IWineD3DSurface_Release(pRenderTarget);
1088 else
1090 FIXME("Call to IWineD3DDevice_GetRenderTarget failed\n");
1091 *ppRenderTarget = NULL;
1093 wined3d_mutex_unlock();
1095 return hr;
1098 static HRESULT WINAPI IDirect3DDevice8Impl_GetDepthStencilSurface(IDirect3DDevice8 *iface,
1099 IDirect3DSurface8 **ppZStencilSurface)
1101 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1102 IWineD3DSurface *pZStencilSurface;
1103 HRESULT hr;
1105 TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1107 if(ppZStencilSurface == NULL){
1108 return D3DERR_INVALIDCALL;
1111 wined3d_mutex_lock();
1112 hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice, &pZStencilSurface);
1113 if (SUCCEEDED(hr))
1115 *ppZStencilSurface = IWineD3DSurface_GetParent(pZStencilSurface);
1116 IDirect3DSurface8_AddRef(*ppZStencilSurface);
1117 IWineD3DSurface_Release(pZStencilSurface);
1119 else
1121 if (hr != WINED3DERR_NOTFOUND)
1122 FIXME("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1123 *ppZStencilSurface = NULL;
1125 wined3d_mutex_unlock();
1127 return hr;
1130 static HRESULT WINAPI IDirect3DDevice8Impl_BeginScene(IDirect3DDevice8 *iface)
1132 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1133 HRESULT hr;
1135 TRACE("iface %p.\n", iface);
1137 wined3d_mutex_lock();
1138 hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1139 wined3d_mutex_unlock();
1141 return hr;
1144 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice8Impl_EndScene(IDirect3DDevice8 *iface)
1146 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1147 HRESULT hr;
1149 TRACE("iface %p.\n", iface);
1151 wined3d_mutex_lock();
1152 hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1153 wined3d_mutex_unlock();
1155 return hr;
1158 static HRESULT WINAPI IDirect3DDevice8Impl_Clear(IDirect3DDevice8 *iface, DWORD Count,
1159 const D3DRECT *pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil)
1161 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1162 HRESULT hr;
1164 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1165 iface, Count, pRects, Flags, Color, Z, Stencil);
1167 wined3d_mutex_lock();
1168 hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (const RECT *)pRects, Flags, Color, Z, Stencil);
1169 wined3d_mutex_unlock();
1171 return hr;
1174 static HRESULT WINAPI IDirect3DDevice8Impl_SetTransform(IDirect3DDevice8 *iface,
1175 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *lpMatrix)
1177 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1178 HRESULT hr;
1180 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1182 /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1183 wined3d_mutex_lock();
1184 hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1185 wined3d_mutex_unlock();
1187 return hr;
1190 static HRESULT WINAPI IDirect3DDevice8Impl_GetTransform(IDirect3DDevice8 *iface,
1191 D3DTRANSFORMSTATETYPE State, D3DMATRIX *pMatrix)
1193 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1194 HRESULT hr;
1196 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1198 /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1199 wined3d_mutex_lock();
1200 hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1201 wined3d_mutex_unlock();
1203 return hr;
1206 static HRESULT WINAPI IDirect3DDevice8Impl_MultiplyTransform(IDirect3DDevice8 *iface,
1207 D3DTRANSFORMSTATETYPE State, const D3DMATRIX *pMatrix)
1209 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1210 HRESULT hr;
1212 TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1214 /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1215 wined3d_mutex_lock();
1216 hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1217 wined3d_mutex_unlock();
1219 return hr;
1222 static HRESULT WINAPI IDirect3DDevice8Impl_SetViewport(IDirect3DDevice8 *iface,
1223 const D3DVIEWPORT8 *pViewport)
1225 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1226 HRESULT hr;
1228 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1230 /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1231 wined3d_mutex_lock();
1232 hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1233 wined3d_mutex_unlock();
1235 return hr;
1238 static HRESULT WINAPI IDirect3DDevice8Impl_GetViewport(IDirect3DDevice8 *iface,
1239 D3DVIEWPORT8 *pViewport)
1241 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1242 HRESULT hr;
1244 TRACE("iface %p, viewport %p.\n", iface, pViewport);
1246 /* Note: D3DVIEWPORT8 is compatible with WINED3DVIEWPORT */
1247 wined3d_mutex_lock();
1248 hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1249 wined3d_mutex_unlock();
1251 return hr;
1254 static HRESULT WINAPI IDirect3DDevice8Impl_SetMaterial(IDirect3DDevice8 *iface,
1255 const D3DMATERIAL8 *pMaterial)
1257 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1258 HRESULT hr;
1260 TRACE("iface %p, material %p.\n", iface, pMaterial);
1262 /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1263 wined3d_mutex_lock();
1264 hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1265 wined3d_mutex_unlock();
1267 return hr;
1270 static HRESULT WINAPI IDirect3DDevice8Impl_GetMaterial(IDirect3DDevice8 *iface,
1271 D3DMATERIAL8 *pMaterial)
1273 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1274 HRESULT hr;
1276 TRACE("iface %p, material %p.\n", iface, pMaterial);
1278 /* Note: D3DMATERIAL8 is compatible with WINED3DMATERIAL */
1279 wined3d_mutex_lock();
1280 hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1281 wined3d_mutex_unlock();
1283 return hr;
1286 static HRESULT WINAPI IDirect3DDevice8Impl_SetLight(IDirect3DDevice8 *iface, DWORD Index,
1287 const D3DLIGHT8 *pLight)
1289 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1290 HRESULT hr;
1292 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1294 /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1295 wined3d_mutex_lock();
1296 hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1297 wined3d_mutex_unlock();
1299 return hr;
1302 static HRESULT WINAPI IDirect3DDevice8Impl_GetLight(IDirect3DDevice8 *iface, DWORD Index,
1303 D3DLIGHT8 *pLight)
1305 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1306 HRESULT hr;
1308 TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1310 /* Note: D3DLIGHT8 is compatible with WINED3DLIGHT */
1311 wined3d_mutex_lock();
1312 hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1313 wined3d_mutex_unlock();
1315 return hr;
1318 static HRESULT WINAPI IDirect3DDevice8Impl_LightEnable(IDirect3DDevice8 *iface, DWORD Index,
1319 BOOL Enable)
1321 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1322 HRESULT hr;
1324 TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1326 wined3d_mutex_lock();
1327 hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1328 wined3d_mutex_unlock();
1330 return hr;
1333 static HRESULT WINAPI IDirect3DDevice8Impl_GetLightEnable(IDirect3DDevice8 *iface, DWORD Index,
1334 BOOL *pEnable)
1336 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1337 HRESULT hr;
1339 TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1341 wined3d_mutex_lock();
1342 hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1343 wined3d_mutex_unlock();
1345 return hr;
1348 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipPlane(IDirect3DDevice8 *iface, DWORD Index,
1349 const float *pPlane)
1351 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1352 HRESULT hr;
1354 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1356 wined3d_mutex_lock();
1357 hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1358 wined3d_mutex_unlock();
1360 return hr;
1363 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipPlane(IDirect3DDevice8 *iface, DWORD Index,
1364 float *pPlane)
1366 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1367 HRESULT hr;
1369 TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1371 wined3d_mutex_lock();
1372 hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1373 wined3d_mutex_unlock();
1375 return hr;
1378 static HRESULT WINAPI IDirect3DDevice8Impl_SetRenderState(IDirect3DDevice8 *iface,
1379 D3DRENDERSTATETYPE State, DWORD Value)
1381 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1382 HRESULT hr;
1384 TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1386 wined3d_mutex_lock();
1387 hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1388 wined3d_mutex_unlock();
1390 return hr;
1393 static HRESULT WINAPI IDirect3DDevice8Impl_GetRenderState(IDirect3DDevice8 *iface,
1394 D3DRENDERSTATETYPE State, DWORD *pValue)
1396 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1397 HRESULT hr;
1399 TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1401 wined3d_mutex_lock();
1402 hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1403 wined3d_mutex_unlock();
1405 return hr;
1408 static HRESULT WINAPI IDirect3DDevice8Impl_BeginStateBlock(IDirect3DDevice8 *iface)
1410 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1411 HRESULT hr;
1413 TRACE("iface %p.\n", iface);
1415 wined3d_mutex_lock();
1416 hr = IWineD3DDevice_BeginStateBlock(This->WineD3DDevice);
1417 wined3d_mutex_unlock();
1419 return hr;
1422 static HRESULT WINAPI IDirect3DDevice8Impl_EndStateBlock(IDirect3DDevice8 *iface, DWORD *pToken)
1424 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1425 struct wined3d_stateblock *stateblock;
1426 HRESULT hr;
1428 TRACE("iface %p, token %p.\n", iface, pToken);
1430 /* Tell wineD3D to endstateblock before anything else (in case we run out
1431 * of memory later and cause locking problems)
1433 wined3d_mutex_lock();
1434 hr = IWineD3DDevice_EndStateBlock(This->WineD3DDevice , &stateblock);
1435 if (hr != D3D_OK) {
1436 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1437 wined3d_mutex_unlock();
1438 return hr;
1441 *pToken = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1442 wined3d_mutex_unlock();
1444 if (*pToken == D3D8_INVALID_HANDLE)
1446 ERR("Failed to create a handle\n");
1447 wined3d_mutex_lock();
1448 wined3d_stateblock_decref(stateblock);
1449 wined3d_mutex_unlock();
1450 return E_FAIL;
1452 ++*pToken;
1454 TRACE("Returning %#x (%p).\n", *pToken, stateblock);
1456 return hr;
1459 static HRESULT WINAPI IDirect3DDevice8Impl_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1461 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1462 struct wined3d_stateblock *stateblock;
1463 HRESULT hr;
1465 TRACE("iface %p, token %#x.\n", iface, Token);
1467 if (!Token) return D3D_OK;
1469 wined3d_mutex_lock();
1470 stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1471 if (!stateblock)
1473 WARN("Invalid handle (%#x) passed.\n", Token);
1474 wined3d_mutex_unlock();
1475 return D3DERR_INVALIDCALL;
1477 hr = wined3d_stateblock_apply(stateblock);
1478 wined3d_mutex_unlock();
1480 return hr;
1483 static HRESULT WINAPI IDirect3DDevice8Impl_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1485 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1486 struct wined3d_stateblock *stateblock;
1487 HRESULT hr;
1489 TRACE("iface %p, token %#x.\n", iface, Token);
1491 wined3d_mutex_lock();
1492 stateblock = d3d8_get_object(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1493 if (!stateblock)
1495 WARN("Invalid handle (%#x) passed.\n", Token);
1496 wined3d_mutex_unlock();
1497 return D3DERR_INVALIDCALL;
1499 hr = wined3d_stateblock_capture(stateblock);
1500 wined3d_mutex_unlock();
1502 return hr;
1505 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD Token)
1507 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1508 struct wined3d_stateblock *stateblock;
1510 TRACE("iface %p, token %#x.\n", iface, Token);
1512 wined3d_mutex_lock();
1513 stateblock = d3d8_free_handle(&This->handle_table, Token - 1, D3D8_HANDLE_SB);
1515 if (!stateblock)
1517 WARN("Invalid handle (%#x) passed.\n", Token);
1518 wined3d_mutex_unlock();
1519 return D3DERR_INVALIDCALL;
1522 if (wined3d_stateblock_decref(stateblock))
1524 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1526 wined3d_mutex_unlock();
1528 return D3D_OK;
1531 static HRESULT WINAPI IDirect3DDevice8Impl_CreateStateBlock(IDirect3DDevice8 *iface,
1532 D3DSTATEBLOCKTYPE Type, DWORD *handle)
1534 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1535 struct wined3d_stateblock *stateblock;
1536 HRESULT hr;
1538 TRACE("iface %p, type %#x, handle %p.\n", iface, Type, handle);
1540 if (Type != D3DSBT_ALL
1541 && Type != D3DSBT_PIXELSTATE
1542 && Type != D3DSBT_VERTEXSTATE)
1544 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1545 return D3DERR_INVALIDCALL;
1548 wined3d_mutex_lock();
1549 hr = IWineD3DDevice_CreateStateBlock(This->WineD3DDevice, (WINED3DSTATEBLOCKTYPE)Type, &stateblock);
1550 if (FAILED(hr))
1552 wined3d_mutex_unlock();
1553 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1554 return hr;
1557 *handle = d3d8_allocate_handle(&This->handle_table, stateblock, D3D8_HANDLE_SB);
1558 wined3d_mutex_unlock();
1560 if (*handle == D3D8_INVALID_HANDLE)
1562 ERR("Failed to allocate a handle.\n");
1563 wined3d_mutex_lock();
1564 wined3d_stateblock_decref(stateblock);
1565 wined3d_mutex_unlock();
1566 return E_FAIL;
1568 ++*handle;
1570 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1572 return hr;
1575 static HRESULT WINAPI IDirect3DDevice8Impl_SetClipStatus(IDirect3DDevice8 *iface,
1576 const D3DCLIPSTATUS8 *pClipStatus)
1578 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1579 HRESULT hr;
1581 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1582 /* FIXME: Verify that D3DCLIPSTATUS8 ~= WINED3DCLIPSTATUS */
1584 wined3d_mutex_lock();
1585 hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1586 wined3d_mutex_unlock();
1588 return hr;
1591 static HRESULT WINAPI IDirect3DDevice8Impl_GetClipStatus(IDirect3DDevice8 *iface,
1592 D3DCLIPSTATUS8 *pClipStatus)
1594 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1595 HRESULT hr;
1597 TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1599 wined3d_mutex_lock();
1600 hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1601 wined3d_mutex_unlock();
1603 return hr;
1606 static HRESULT WINAPI IDirect3DDevice8Impl_GetTexture(IDirect3DDevice8 *iface,
1607 DWORD Stage, IDirect3DBaseTexture8 **ppTexture)
1609 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1610 struct wined3d_texture *wined3d_texture;
1611 HRESULT hr;
1613 TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1615 if(ppTexture == NULL){
1616 return D3DERR_INVALIDCALL;
1619 wined3d_mutex_lock();
1620 hr = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &wined3d_texture);
1621 if (FAILED(hr))
1623 WARN("Failed to get texture for stage %u, hr %#x.\n", Stage, hr);
1624 wined3d_mutex_unlock();
1625 *ppTexture = NULL;
1626 return hr;
1629 if (wined3d_texture)
1631 *ppTexture = wined3d_texture_get_parent(wined3d_texture);
1632 IDirect3DBaseTexture8_AddRef(*ppTexture);
1633 wined3d_texture_decref(wined3d_texture);
1635 else
1637 *ppTexture = NULL;
1639 wined3d_mutex_unlock();
1641 return D3D_OK;
1644 static HRESULT WINAPI IDirect3DDevice8Impl_SetTexture(IDirect3DDevice8 *iface, DWORD Stage,
1645 IDirect3DBaseTexture8 *pTexture)
1647 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1648 HRESULT hr;
1650 TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1652 wined3d_mutex_lock();
1653 hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1654 pTexture ? ((IDirect3DBaseTexture8Impl *)pTexture)->wined3d_texture : NULL);
1655 wined3d_mutex_unlock();
1657 return hr;
1660 static const struct tss_lookup
1662 BOOL sampler_state;
1663 DWORD state;
1665 tss_lookup[] =
1667 {FALSE, WINED3DTSS_FORCE_DWORD}, /* 0, unused */
1668 {FALSE, WINED3DTSS_COLOROP}, /* 1, D3DTSS_COLOROP */
1669 {FALSE, WINED3DTSS_COLORARG1}, /* 2, D3DTSS_COLORARG1 */
1670 {FALSE, WINED3DTSS_COLORARG2}, /* 3, D3DTSS_COLORARG2 */
1671 {FALSE, WINED3DTSS_ALPHAOP}, /* 4, D3DTSS_ALPHAOP */
1672 {FALSE, WINED3DTSS_ALPHAARG1}, /* 5, D3DTSS_ALPHAARG1 */
1673 {FALSE, WINED3DTSS_ALPHAARG2}, /* 6, D3DTSS_ALPHAARG2 */
1674 {FALSE, WINED3DTSS_BUMPENVMAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1675 {FALSE, WINED3DTSS_BUMPENVMAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1676 {FALSE, WINED3DTSS_BUMPENVMAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1677 {FALSE, WINED3DTSS_BUMPENVMAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1678 {FALSE, WINED3DTSS_TEXCOORDINDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1679 {FALSE, WINED3DTSS_FORCE_DWORD}, /* 12, unused */
1680 {TRUE, WINED3DSAMP_ADDRESSU}, /* 13, D3DTSS_ADDRESSU */
1681 {TRUE, WINED3DSAMP_ADDRESSV}, /* 14, D3DTSS_ADDRESSV */
1682 {TRUE, WINED3DSAMP_BORDERCOLOR}, /* 15, D3DTSS_BORDERCOLOR */
1683 {TRUE, WINED3DSAMP_MAGFILTER}, /* 16, D3DTSS_MAGFILTER */
1684 {TRUE, WINED3DSAMP_MINFILTER}, /* 17, D3DTSS_MINFILTER */
1685 {TRUE, WINED3DSAMP_MIPFILTER}, /* 18, D3DTSS_MIPFILTER */
1686 {TRUE, WINED3DSAMP_MIPMAPLODBIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1687 {TRUE, WINED3DSAMP_MAXMIPLEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1688 {TRUE, WINED3DSAMP_MAXANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1689 {FALSE, WINED3DTSS_BUMPENVLSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1690 {FALSE, WINED3DTSS_BUMPENVLOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1691 {FALSE, WINED3DTSS_TEXTURETRANSFORMFLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1692 {TRUE, WINED3DSAMP_ADDRESSW}, /* 25, D3DTSS_ADDRESSW */
1693 {FALSE, WINED3DTSS_COLORARG0}, /* 26, D3DTSS_COLORARG0 */
1694 {FALSE, WINED3DTSS_ALPHAARG0}, /* 27, D3DTSS_ALPHAARG0 */
1695 {FALSE, WINED3DTSS_RESULTARG}, /* 28, D3DTSS_RESULTARG */
1698 static HRESULT WINAPI IDirect3DDevice8Impl_GetTextureStageState(IDirect3DDevice8 *iface,
1699 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *pValue)
1701 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1702 const struct tss_lookup *l;
1703 HRESULT hr;
1705 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1707 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1709 WARN("Invalid Type %#x passed.\n", Type);
1710 return D3D_OK;
1713 l = &tss_lookup[Type];
1715 wined3d_mutex_lock();
1716 if (l->sampler_state) hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Stage, l->state, pValue);
1717 else hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, l->state, pValue);
1718 wined3d_mutex_unlock();
1720 return hr;
1723 static HRESULT WINAPI IDirect3DDevice8Impl_SetTextureStageState(IDirect3DDevice8 *iface,
1724 DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value)
1726 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1727 const struct tss_lookup *l;
1728 HRESULT hr;
1730 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1732 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1734 WARN("Invalid Type %#x passed.\n", Type);
1735 return D3D_OK;
1738 l = &tss_lookup[Type];
1740 wined3d_mutex_lock();
1741 if (l->sampler_state) hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Stage, l->state, Value);
1742 else hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, l->state, Value);
1743 wined3d_mutex_unlock();
1745 return hr;
1748 static HRESULT WINAPI IDirect3DDevice8Impl_ValidateDevice(IDirect3DDevice8 *iface,
1749 DWORD *pNumPasses)
1751 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1752 HRESULT hr;
1754 TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1756 wined3d_mutex_lock();
1757 hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1758 wined3d_mutex_unlock();
1760 return hr;
1763 static HRESULT WINAPI IDirect3DDevice8Impl_GetInfo(IDirect3DDevice8 *iface,
1764 DWORD info_id, void *info, DWORD info_size)
1766 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1768 return D3D_OK;
1771 static HRESULT WINAPI IDirect3DDevice8Impl_SetPaletteEntries(IDirect3DDevice8 *iface,
1772 UINT PaletteNumber, const PALETTEENTRY *pEntries)
1774 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1775 HRESULT hr;
1777 TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1779 wined3d_mutex_lock();
1780 hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1781 wined3d_mutex_unlock();
1783 return hr;
1786 static HRESULT WINAPI IDirect3DDevice8Impl_GetPaletteEntries(IDirect3DDevice8 *iface,
1787 UINT PaletteNumber, PALETTEENTRY *pEntries)
1789 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1790 HRESULT hr;
1792 TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1794 wined3d_mutex_lock();
1795 hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1796 wined3d_mutex_unlock();
1798 return hr;
1801 static HRESULT WINAPI IDirect3DDevice8Impl_SetCurrentTexturePalette(IDirect3DDevice8 *iface,
1802 UINT PaletteNumber)
1804 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1805 HRESULT hr;
1807 TRACE("iface %p, palette_idx %u.\n", iface, PaletteNumber);
1809 wined3d_mutex_lock();
1810 hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1811 wined3d_mutex_unlock();
1813 return hr;
1816 static HRESULT WINAPI IDirect3DDevice8Impl_GetCurrentTexturePalette(IDirect3DDevice8 *iface,
1817 UINT *PaletteNumber)
1819 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1820 HRESULT hr;
1822 TRACE("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1824 wined3d_mutex_lock();
1825 hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1826 wined3d_mutex_unlock();
1828 return hr;
1831 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitive(IDirect3DDevice8 *iface,
1832 D3DPRIMITIVETYPE PrimitiveType, UINT StartVertex, UINT PrimitiveCount)
1834 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1835 HRESULT hr;
1837 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1838 iface, PrimitiveType, StartVertex, PrimitiveCount);
1840 wined3d_mutex_lock();
1841 IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1842 hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1843 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1844 wined3d_mutex_unlock();
1846 return hr;
1849 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1850 D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertices, UINT startIndex,
1851 UINT primCount)
1853 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1854 HRESULT hr;
1856 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1857 iface, PrimitiveType, MinVertexIndex, NumVertices, startIndex, primCount);
1859 wined3d_mutex_lock();
1860 IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1861 hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1862 vertex_count_from_primitive_count(PrimitiveType, primCount));
1863 wined3d_mutex_unlock();
1865 return hr;
1868 static HRESULT WINAPI IDirect3DDevice8Impl_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1869 D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, const void *pVertexStreamZeroData,
1870 UINT VertexStreamZeroStride)
1872 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1873 HRESULT hr;
1875 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1876 iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1878 wined3d_mutex_lock();
1879 IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1880 hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1881 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1882 pVertexStreamZeroData, VertexStreamZeroStride);
1883 wined3d_mutex_unlock();
1885 return hr;
1888 static HRESULT WINAPI IDirect3DDevice8Impl_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
1889 D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex, UINT NumVertexIndices,
1890 UINT PrimitiveCount, const void *pIndexData, D3DFORMAT IndexDataFormat,
1891 const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
1893 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1894 HRESULT hr;
1896 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1897 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1898 iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1899 pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1901 wined3d_mutex_lock();
1902 IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1903 hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1904 vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1905 wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1906 wined3d_mutex_unlock();
1908 return hr;
1911 static HRESULT WINAPI IDirect3DDevice8Impl_ProcessVertices(IDirect3DDevice8 *iface,
1912 UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer8 *pDestBuffer,
1913 DWORD Flags)
1915 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1916 HRESULT hr;
1917 IDirect3DVertexBuffer8Impl *dest = (IDirect3DVertexBuffer8Impl *) pDestBuffer;
1919 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
1920 iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, Flags);
1922 wined3d_mutex_lock();
1923 hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, NULL, Flags, dest->fvf);
1924 wined3d_mutex_unlock();
1926 return hr;
1929 static HRESULT WINAPI IDirect3DDevice8Impl_CreateVertexShader(IDirect3DDevice8 *iface,
1930 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
1932 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
1933 IDirect3DVertexShader8Impl *object;
1934 DWORD shader_handle;
1935 DWORD handle;
1936 HRESULT hr;
1938 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
1939 iface, declaration, byte_code, shader, usage);
1941 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1942 if (!object)
1944 ERR("Failed to allocate vertex shader memory.\n");
1945 *shader = 0;
1946 return E_OUTOFMEMORY;
1949 wined3d_mutex_lock();
1950 handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_VS);
1951 wined3d_mutex_unlock();
1952 if (handle == D3D8_INVALID_HANDLE)
1954 ERR("Failed to allocate vertex shader handle.\n");
1955 HeapFree(GetProcessHeap(), 0, object);
1956 *shader = 0;
1957 return E_OUTOFMEMORY;
1960 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
1962 hr = vertexshader_init(object, This, declaration, byte_code, shader_handle, usage);
1963 if (FAILED(hr))
1965 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
1966 wined3d_mutex_lock();
1967 d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_VS);
1968 wined3d_mutex_unlock();
1969 HeapFree(GetProcessHeap(), 0, object);
1970 *shader = 0;
1971 return hr;
1974 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
1975 *shader = shader_handle;
1977 return D3D_OK;
1980 static IDirect3DVertexDeclaration8Impl *IDirect3DDevice8Impl_FindDecl(IDirect3DDevice8Impl *This, DWORD fvf)
1982 IDirect3DVertexDeclaration8Impl *d3d8_declaration;
1983 HRESULT hr;
1984 int p, low, high; /* deliberately signed */
1985 struct FvfToDecl *convertedDecls = This->decls;
1987 TRACE("Searching for declaration for fvf %08x... ", fvf);
1989 low = 0;
1990 high = This->numConvertedDecls - 1;
1991 while(low <= high) {
1992 p = (low + high) >> 1;
1993 TRACE("%d ", p);
1994 if(convertedDecls[p].fvf == fvf) {
1995 TRACE("found %p\n", convertedDecls[p].decl);
1996 return (IDirect3DVertexDeclaration8Impl *)convertedDecls[p].decl;
1997 } else if(convertedDecls[p].fvf < fvf) {
1998 low = p + 1;
1999 } else {
2000 high = p - 1;
2003 TRACE("not found. Creating and inserting at position %d.\n", low);
2005 d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration));
2006 if (!d3d8_declaration)
2008 ERR("Memory allocation failed.\n");
2009 return NULL;
2012 hr = vertexdeclaration_init_fvf(d3d8_declaration, This, fvf);
2013 if (FAILED(hr))
2015 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2016 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2017 return NULL;
2020 if(This->declArraySize == This->numConvertedDecls) {
2021 int grow = This->declArraySize / 2;
2022 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2023 sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
2024 if(!convertedDecls) {
2025 /* This will destroy it */
2026 IDirect3DVertexDeclaration8_Release((IDirect3DVertexDeclaration8 *)d3d8_declaration);
2027 return NULL;
2029 This->decls = convertedDecls;
2030 This->declArraySize += grow;
2033 memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(convertedDecls[0]) * (This->numConvertedDecls - low));
2034 convertedDecls[low].decl = (IDirect3DVertexDeclaration8 *)d3d8_declaration;
2035 convertedDecls[low].fvf = fvf;
2036 This->numConvertedDecls++;
2038 TRACE("Returning %p. %u decls in array\n", d3d8_declaration, This->numConvertedDecls);
2039 return d3d8_declaration;
2042 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShader(IDirect3DDevice8 *iface, DWORD pShader)
2044 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2045 IDirect3DVertexShader8Impl *shader;
2046 HRESULT hr;
2048 TRACE("iface %p, shader %#x.\n", iface, pShader);
2050 if (VS_HIGHESTFIXEDFXF >= pShader) {
2051 TRACE("Setting FVF, %#x\n", pShader);
2053 wined3d_mutex_lock();
2054 IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
2055 IDirect3DDevice8Impl_FindDecl(This, pShader)->wined3d_vertex_declaration);
2056 IWineD3DDevice_SetVertexShader(This->WineD3DDevice, NULL);
2057 wined3d_mutex_unlock();
2059 return D3D_OK;
2062 TRACE("Setting shader\n");
2064 wined3d_mutex_lock();
2065 shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2066 if (!shader)
2068 WARN("Invalid handle (%#x) passed.\n", pShader);
2069 wined3d_mutex_unlock();
2071 return D3DERR_INVALIDCALL;
2074 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice,
2075 ((IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration)->wined3d_vertex_declaration);
2076 if (SUCCEEDED(hr))
2077 hr = IWineD3DDevice_SetVertexShader(This->WineD3DDevice, shader->wined3d_shader);
2078 wined3d_mutex_unlock();
2080 TRACE("Returning hr %#x\n", hr);
2082 return hr;
2085 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2087 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2088 struct wined3d_vertex_declaration *wined3d_declaration;
2089 IDirect3DVertexDeclaration8 *d3d8_declaration;
2090 HRESULT hr;
2092 TRACE("iface %p, shader %p.\n", iface, ppShader);
2094 wined3d_mutex_lock();
2095 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &wined3d_declaration);
2096 if (FAILED(hr))
2098 wined3d_mutex_unlock();
2099 WARN("(%p) : Call to IWineD3DDevice_GetVertexDeclaration failed %#x (device %p)\n",
2100 This, hr, This->WineD3DDevice);
2101 return hr;
2104 if (!wined3d_declaration)
2106 wined3d_mutex_unlock();
2107 *ppShader = 0;
2108 return D3D_OK;
2111 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2112 wined3d_vertex_declaration_decref(wined3d_declaration);
2113 wined3d_mutex_unlock();
2114 *ppShader = ((IDirect3DVertexDeclaration8Impl *)d3d8_declaration)->shader_handle;
2116 TRACE("(%p) : returning %#x\n", This, *ppShader);
2118 return hr;
2121 static HRESULT WINAPI IDirect3DDevice8Impl_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD pShader)
2123 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2124 IDirect3DVertexShader8Impl *shader;
2125 struct wined3d_shader *cur;
2127 TRACE("iface %p, shader %#x.\n", iface, pShader);
2129 wined3d_mutex_lock();
2130 shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2131 if (!shader)
2133 WARN("Invalid handle (%#x) passed.\n", pShader);
2134 wined3d_mutex_unlock();
2136 return D3DERR_INVALIDCALL;
2139 cur = IWineD3DDevice_GetVertexShader(This->WineD3DDevice);
2140 if (cur)
2142 if (cur == shader->wined3d_shader)
2143 IDirect3DDevice8_SetVertexShader(iface, 0);
2144 wined3d_shader_decref(cur);
2147 wined3d_mutex_unlock();
2149 if (IDirect3DVertexShader8_Release(&shader->IDirect3DVertexShader8_iface))
2151 ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2154 return D3D_OK;
2157 static HRESULT WINAPI IDirect3DDevice8Impl_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2158 DWORD Register, const void *pConstantData, DWORD ConstantCount)
2160 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2161 HRESULT hr;
2163 TRACE("iface %p, register %u, data %p, count %u.\n",
2164 iface, Register, pConstantData, ConstantCount);
2166 if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2167 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2168 Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2169 return D3DERR_INVALIDCALL;
2172 wined3d_mutex_lock();
2173 hr = IWineD3DDevice_SetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2174 wined3d_mutex_unlock();
2176 return hr;
2179 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2180 DWORD Register, void *pConstantData, DWORD ConstantCount)
2182 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2183 HRESULT hr;
2185 TRACE("iface %p, register %u, data %p, count %u.\n",
2186 iface, Register, pConstantData, ConstantCount);
2188 if(Register + ConstantCount > D3D8_MAX_VERTEX_SHADER_CONSTANTF) {
2189 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2190 Register + ConstantCount, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2191 return D3DERR_INVALIDCALL;
2194 wined3d_mutex_lock();
2195 hr = IWineD3DDevice_GetVertexShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2196 wined3d_mutex_unlock();
2198 return hr;
2201 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2202 DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2204 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2205 IDirect3DVertexDeclaration8Impl *declaration;
2206 IDirect3DVertexShader8Impl *shader;
2208 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2209 iface, pVertexShader, pData, pSizeOfData);
2211 wined3d_mutex_lock();
2212 shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2213 wined3d_mutex_unlock();
2215 if (!shader)
2217 WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2218 return D3DERR_INVALIDCALL;
2220 declaration = (IDirect3DVertexDeclaration8Impl *)shader->vertex_declaration;
2222 /* If pData is NULL, we just return the required size of the buffer. */
2223 if (!pData) {
2224 *pSizeOfData = declaration->elements_size;
2225 return D3D_OK;
2228 /* MSDN claims that if *pSizeOfData is smaller than the required size
2229 * we should write the required size and return D3DERR_MOREDATA.
2230 * That's not actually true. */
2231 if (*pSizeOfData < declaration->elements_size) {
2232 return D3DERR_INVALIDCALL;
2235 CopyMemory(pData, declaration->elements, declaration->elements_size);
2237 return D3D_OK;
2240 static HRESULT WINAPI IDirect3DDevice8Impl_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2241 DWORD pVertexShader, void *pData, DWORD *pSizeOfData)
2243 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2244 IDirect3DVertexShader8Impl *shader = NULL;
2245 HRESULT hr;
2247 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2248 iface, pVertexShader, pData, pSizeOfData);
2250 wined3d_mutex_lock();
2251 shader = d3d8_get_object(&This->handle_table, pVertexShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2252 if (!shader)
2254 WARN("Invalid handle (%#x) passed.\n", pVertexShader);
2255 wined3d_mutex_unlock();
2257 return D3DERR_INVALIDCALL;
2260 if (!shader->wined3d_shader)
2262 wined3d_mutex_unlock();
2263 *pSizeOfData = 0;
2264 return D3D_OK;
2267 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, pData, pSizeOfData);
2268 wined3d_mutex_unlock();
2270 return hr;
2273 static HRESULT WINAPI IDirect3DDevice8Impl_SetIndices(IDirect3DDevice8 *iface,
2274 IDirect3DIndexBuffer8 *pIndexData, UINT baseVertexIndex)
2276 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2277 HRESULT hr;
2278 IDirect3DIndexBuffer8Impl *ib = (IDirect3DIndexBuffer8Impl *)pIndexData;
2280 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, pIndexData, baseVertexIndex);
2282 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2283 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2284 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2285 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2286 * problem)
2288 wined3d_mutex_lock();
2289 IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, baseVertexIndex);
2290 hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2291 ib ? ib->wineD3DIndexBuffer : NULL,
2292 ib ? ib->format : WINED3DFMT_UNKNOWN);
2293 wined3d_mutex_unlock();
2295 return hr;
2298 static HRESULT WINAPI IDirect3DDevice8Impl_GetIndices(IDirect3DDevice8 *iface,
2299 IDirect3DIndexBuffer8 **ppIndexData, UINT *pBaseVertexIndex)
2301 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2302 struct wined3d_buffer *retIndexData = NULL;
2303 HRESULT hr;
2305 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, ppIndexData, pBaseVertexIndex);
2307 if(ppIndexData == NULL){
2308 return D3DERR_INVALIDCALL;
2311 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2312 wined3d_mutex_lock();
2313 IWineD3DDevice_GetBaseVertexIndex(This->WineD3DDevice, (INT *) pBaseVertexIndex);
2314 hr = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2315 if (SUCCEEDED(hr) && retIndexData)
2317 *ppIndexData = wined3d_buffer_get_parent(retIndexData);
2318 IDirect3DIndexBuffer8_AddRef(*ppIndexData);
2319 wined3d_buffer_decref(retIndexData);
2320 } else {
2321 if (FAILED(hr)) FIXME("Call to GetIndices failed\n");
2322 *ppIndexData = NULL;
2324 wined3d_mutex_unlock();
2326 return hr;
2329 static HRESULT WINAPI IDirect3DDevice8Impl_CreatePixelShader(IDirect3DDevice8 *iface,
2330 const DWORD *byte_code, DWORD *shader)
2332 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2333 IDirect3DPixelShader8Impl *object;
2334 DWORD shader_handle;
2335 DWORD handle;
2336 HRESULT hr;
2338 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2340 if (!shader)
2342 TRACE("(%p) Invalid call\n", This);
2343 return D3DERR_INVALIDCALL;
2346 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2347 if (!object)
2349 ERR("Failed to allocate pixel shader memmory.\n");
2350 return E_OUTOFMEMORY;
2353 wined3d_mutex_lock();
2354 handle = d3d8_allocate_handle(&This->handle_table, object, D3D8_HANDLE_PS);
2355 wined3d_mutex_unlock();
2356 if (handle == D3D8_INVALID_HANDLE)
2358 ERR("Failed to allocate pixel shader handle.\n");
2359 HeapFree(GetProcessHeap(), 0, object);
2360 return E_OUTOFMEMORY;
2363 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2365 hr = pixelshader_init(object, This, byte_code, shader_handle);
2366 if (FAILED(hr))
2368 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2369 wined3d_mutex_lock();
2370 d3d8_free_handle(&This->handle_table, handle, D3D8_HANDLE_PS);
2371 wined3d_mutex_unlock();
2372 HeapFree(GetProcessHeap(), 0, object);
2373 *shader = 0;
2374 return hr;
2377 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2378 *shader = shader_handle;
2380 return D3D_OK;
2383 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2385 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2386 IDirect3DPixelShader8Impl *shader;
2387 HRESULT hr;
2389 TRACE("iface %p, shader %#x.\n", iface, pShader);
2391 wined3d_mutex_lock();
2393 if (!pShader)
2395 hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, NULL);
2396 wined3d_mutex_unlock();
2397 return hr;
2400 shader = d3d8_get_object(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2401 if (!shader)
2403 WARN("Invalid handle (%#x) passed.\n", pShader);
2404 wined3d_mutex_unlock();
2405 return D3DERR_INVALIDCALL;
2408 TRACE("(%p) : Setting shader %p\n", This, shader);
2409 hr = IWineD3DDevice_SetPixelShader(This->WineD3DDevice, shader->wined3d_shader);
2410 wined3d_mutex_unlock();
2412 return hr;
2415 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShader(IDirect3DDevice8 *iface, DWORD *ppShader)
2417 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2418 struct wined3d_shader *object;
2420 TRACE("iface %p, shader %p.\n", iface, ppShader);
2422 if (NULL == ppShader) {
2423 TRACE("(%p) Invalid call\n", This);
2424 return D3DERR_INVALIDCALL;
2427 wined3d_mutex_lock();
2428 object = IWineD3DDevice_GetPixelShader(This->WineD3DDevice);
2429 if (object)
2431 IDirect3DPixelShader8Impl *d3d8_shader;
2432 d3d8_shader = wined3d_shader_get_parent(object);
2433 wined3d_shader_decref(object);
2434 *ppShader = d3d8_shader->handle;
2436 else
2438 *ppShader = 0;
2440 wined3d_mutex_unlock();
2442 TRACE("(%p) : returning %#x\n", This, *ppShader);
2444 return D3D_OK;
2447 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePixelShader(IDirect3DDevice8 *iface, DWORD pShader)
2449 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2450 IDirect3DPixelShader8Impl *shader;
2451 struct wined3d_shader *cur;
2453 TRACE("iface %p, shader %#x.\n", iface, pShader);
2455 wined3d_mutex_lock();
2457 shader = d3d8_free_handle(&This->handle_table, pShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2458 if (!shader)
2460 WARN("Invalid handle (%#x) passed.\n", pShader);
2461 wined3d_mutex_unlock();
2462 return D3DERR_INVALIDCALL;
2465 cur = IWineD3DDevice_GetPixelShader(This->WineD3DDevice);
2466 if (cur)
2468 if (cur == shader->wined3d_shader)
2469 IDirect3DDevice8_SetPixelShader(iface, 0);
2470 wined3d_shader_decref(cur);
2473 wined3d_mutex_unlock();
2475 if (IDirect3DPixelShader8_Release(&shader->IDirect3DPixelShader8_iface))
2477 ERR("Shader %p has references left, this shouldn't happen.\n", shader);
2480 return D3D_OK;
2483 static HRESULT WINAPI IDirect3DDevice8Impl_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2484 DWORD Register, const void *pConstantData, DWORD ConstantCount)
2486 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2487 HRESULT hr;
2489 TRACE("iface %p, register %u, data %p, count %u.\n",
2490 iface, Register, pConstantData, ConstantCount);
2492 wined3d_mutex_lock();
2493 hr = IWineD3DDevice_SetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2494 wined3d_mutex_unlock();
2496 return hr;
2499 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2500 DWORD Register, void *pConstantData, DWORD ConstantCount)
2502 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2503 HRESULT hr;
2505 TRACE("iface %p, register %u, data %p, count %u.\n",
2506 iface, Register, pConstantData, ConstantCount);
2508 wined3d_mutex_lock();
2509 hr = IWineD3DDevice_GetPixelShaderConstantF(This->WineD3DDevice, Register, pConstantData, ConstantCount);
2510 wined3d_mutex_unlock();
2512 return hr;
2515 static HRESULT WINAPI IDirect3DDevice8Impl_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2516 DWORD pPixelShader, void *pData, DWORD *pSizeOfData)
2518 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2519 IDirect3DPixelShader8Impl *shader = NULL;
2520 HRESULT hr;
2522 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2523 iface, pPixelShader, pData, pSizeOfData);
2525 wined3d_mutex_lock();
2526 shader = d3d8_get_object(&This->handle_table, pPixelShader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS);
2527 if (!shader)
2529 WARN("Invalid handle (%#x) passed.\n", pPixelShader);
2530 wined3d_mutex_unlock();
2532 return D3DERR_INVALIDCALL;
2535 hr = wined3d_shader_get_byte_code(shader->wined3d_shader, pData, pSizeOfData);
2536 wined3d_mutex_unlock();
2538 return hr;
2541 static HRESULT WINAPI IDirect3DDevice8Impl_DrawRectPatch(IDirect3DDevice8 *iface, UINT Handle,
2542 const float *pNumSegs, const D3DRECTPATCH_INFO *pRectPatchInfo)
2544 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2545 HRESULT hr;
2547 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2548 iface, Handle, pNumSegs, pRectPatchInfo);
2550 wined3d_mutex_lock();
2551 hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2552 wined3d_mutex_unlock();
2554 return hr;
2557 static HRESULT WINAPI IDirect3DDevice8Impl_DrawTriPatch(IDirect3DDevice8 *iface, UINT Handle,
2558 const float *pNumSegs, const D3DTRIPATCH_INFO *pTriPatchInfo)
2560 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2561 HRESULT hr;
2563 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2564 iface, Handle, pNumSegs, pTriPatchInfo);
2566 wined3d_mutex_lock();
2567 hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2568 wined3d_mutex_unlock();
2570 return hr;
2573 static HRESULT WINAPI IDirect3DDevice8Impl_DeletePatch(IDirect3DDevice8 *iface, UINT Handle)
2575 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2576 HRESULT hr;
2578 TRACE("iface %p, handle %#x.\n", iface, Handle);
2580 wined3d_mutex_lock();
2581 hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2582 wined3d_mutex_unlock();
2584 return hr;
2587 static HRESULT WINAPI IDirect3DDevice8Impl_SetStreamSource(IDirect3DDevice8 *iface,
2588 UINT StreamNumber, IDirect3DVertexBuffer8 *pStreamData, UINT Stride)
2590 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2591 HRESULT hr;
2593 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2594 iface, StreamNumber, pStreamData, Stride);
2596 wined3d_mutex_lock();
2597 hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
2598 NULL == pStreamData ? NULL : ((IDirect3DVertexBuffer8Impl *)pStreamData)->wineD3DVertexBuffer,
2599 0/* Offset in bytes */, Stride);
2600 wined3d_mutex_unlock();
2602 return hr;
2605 static HRESULT WINAPI IDirect3DDevice8Impl_GetStreamSource(IDirect3DDevice8 *iface,
2606 UINT StreamNumber, IDirect3DVertexBuffer8 **pStream, UINT *pStride)
2608 IDirect3DDevice8Impl *This = impl_from_IDirect3DDevice8(iface);
2609 struct wined3d_buffer *retStream = NULL;
2610 HRESULT hr;
2612 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2613 iface, StreamNumber, pStream, pStride);
2615 if(pStream == NULL){
2616 return D3DERR_INVALIDCALL;
2619 wined3d_mutex_lock();
2620 hr = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber,
2621 &retStream, 0 /* Offset in bytes */, pStride);
2622 if (SUCCEEDED(hr) && retStream)
2624 *pStream = wined3d_buffer_get_parent(retStream);
2625 IDirect3DVertexBuffer8_AddRef(*pStream);
2626 wined3d_buffer_decref(retStream);
2628 else
2630 if (FAILED(hr)) FIXME("Call to GetStreamSource failed, hr %#x.\n", hr);
2631 *pStream = NULL;
2633 wined3d_mutex_unlock();
2635 return hr;
2638 static const IDirect3DDevice8Vtbl Direct3DDevice8_Vtbl =
2640 IDirect3DDevice8Impl_QueryInterface,
2641 IDirect3DDevice8Impl_AddRef,
2642 IDirect3DDevice8Impl_Release,
2643 IDirect3DDevice8Impl_TestCooperativeLevel,
2644 IDirect3DDevice8Impl_GetAvailableTextureMem,
2645 IDirect3DDevice8Impl_ResourceManagerDiscardBytes,
2646 IDirect3DDevice8Impl_GetDirect3D,
2647 IDirect3DDevice8Impl_GetDeviceCaps,
2648 IDirect3DDevice8Impl_GetDisplayMode,
2649 IDirect3DDevice8Impl_GetCreationParameters,
2650 IDirect3DDevice8Impl_SetCursorProperties,
2651 IDirect3DDevice8Impl_SetCursorPosition,
2652 IDirect3DDevice8Impl_ShowCursor,
2653 IDirect3DDevice8Impl_CreateAdditionalSwapChain,
2654 IDirect3DDevice8Impl_Reset,
2655 IDirect3DDevice8Impl_Present,
2656 IDirect3DDevice8Impl_GetBackBuffer,
2657 IDirect3DDevice8Impl_GetRasterStatus,
2658 IDirect3DDevice8Impl_SetGammaRamp,
2659 IDirect3DDevice8Impl_GetGammaRamp,
2660 IDirect3DDevice8Impl_CreateTexture,
2661 IDirect3DDevice8Impl_CreateVolumeTexture,
2662 IDirect3DDevice8Impl_CreateCubeTexture,
2663 IDirect3DDevice8Impl_CreateVertexBuffer,
2664 IDirect3DDevice8Impl_CreateIndexBuffer,
2665 IDirect3DDevice8Impl_CreateRenderTarget,
2666 IDirect3DDevice8Impl_CreateDepthStencilSurface,
2667 IDirect3DDevice8Impl_CreateImageSurface,
2668 IDirect3DDevice8Impl_CopyRects,
2669 IDirect3DDevice8Impl_UpdateTexture,
2670 IDirect3DDevice8Impl_GetFrontBuffer,
2671 IDirect3DDevice8Impl_SetRenderTarget,
2672 IDirect3DDevice8Impl_GetRenderTarget,
2673 IDirect3DDevice8Impl_GetDepthStencilSurface,
2674 IDirect3DDevice8Impl_BeginScene,
2675 IDirect3DDevice8Impl_EndScene,
2676 IDirect3DDevice8Impl_Clear,
2677 IDirect3DDevice8Impl_SetTransform,
2678 IDirect3DDevice8Impl_GetTransform,
2679 IDirect3DDevice8Impl_MultiplyTransform,
2680 IDirect3DDevice8Impl_SetViewport,
2681 IDirect3DDevice8Impl_GetViewport,
2682 IDirect3DDevice8Impl_SetMaterial,
2683 IDirect3DDevice8Impl_GetMaterial,
2684 IDirect3DDevice8Impl_SetLight,
2685 IDirect3DDevice8Impl_GetLight,
2686 IDirect3DDevice8Impl_LightEnable,
2687 IDirect3DDevice8Impl_GetLightEnable,
2688 IDirect3DDevice8Impl_SetClipPlane,
2689 IDirect3DDevice8Impl_GetClipPlane,
2690 IDirect3DDevice8Impl_SetRenderState,
2691 IDirect3DDevice8Impl_GetRenderState,
2692 IDirect3DDevice8Impl_BeginStateBlock,
2693 IDirect3DDevice8Impl_EndStateBlock,
2694 IDirect3DDevice8Impl_ApplyStateBlock,
2695 IDirect3DDevice8Impl_CaptureStateBlock,
2696 IDirect3DDevice8Impl_DeleteStateBlock,
2697 IDirect3DDevice8Impl_CreateStateBlock,
2698 IDirect3DDevice8Impl_SetClipStatus,
2699 IDirect3DDevice8Impl_GetClipStatus,
2700 IDirect3DDevice8Impl_GetTexture,
2701 IDirect3DDevice8Impl_SetTexture,
2702 IDirect3DDevice8Impl_GetTextureStageState,
2703 IDirect3DDevice8Impl_SetTextureStageState,
2704 IDirect3DDevice8Impl_ValidateDevice,
2705 IDirect3DDevice8Impl_GetInfo,
2706 IDirect3DDevice8Impl_SetPaletteEntries,
2707 IDirect3DDevice8Impl_GetPaletteEntries,
2708 IDirect3DDevice8Impl_SetCurrentTexturePalette,
2709 IDirect3DDevice8Impl_GetCurrentTexturePalette,
2710 IDirect3DDevice8Impl_DrawPrimitive,
2711 IDirect3DDevice8Impl_DrawIndexedPrimitive,
2712 IDirect3DDevice8Impl_DrawPrimitiveUP,
2713 IDirect3DDevice8Impl_DrawIndexedPrimitiveUP,
2714 IDirect3DDevice8Impl_ProcessVertices,
2715 IDirect3DDevice8Impl_CreateVertexShader,
2716 IDirect3DDevice8Impl_SetVertexShader,
2717 IDirect3DDevice8Impl_GetVertexShader,
2718 IDirect3DDevice8Impl_DeleteVertexShader,
2719 IDirect3DDevice8Impl_SetVertexShaderConstant,
2720 IDirect3DDevice8Impl_GetVertexShaderConstant,
2721 IDirect3DDevice8Impl_GetVertexShaderDeclaration,
2722 IDirect3DDevice8Impl_GetVertexShaderFunction,
2723 IDirect3DDevice8Impl_SetStreamSource,
2724 IDirect3DDevice8Impl_GetStreamSource,
2725 IDirect3DDevice8Impl_SetIndices,
2726 IDirect3DDevice8Impl_GetIndices,
2727 IDirect3DDevice8Impl_CreatePixelShader,
2728 IDirect3DDevice8Impl_SetPixelShader,
2729 IDirect3DDevice8Impl_GetPixelShader,
2730 IDirect3DDevice8Impl_DeletePixelShader,
2731 IDirect3DDevice8Impl_SetPixelShaderConstant,
2732 IDirect3DDevice8Impl_GetPixelShaderConstant,
2733 IDirect3DDevice8Impl_GetPixelShaderFunction,
2734 IDirect3DDevice8Impl_DrawRectPatch,
2735 IDirect3DDevice8Impl_DrawTriPatch,
2736 IDirect3DDevice8Impl_DeletePatch
2739 static inline IDirect3DDevice8Impl *impl_from_IWineD3DDeviceParent(IWineD3DDeviceParent *iface)
2741 return CONTAINING_RECORD(iface, IDirect3DDevice8Impl, IWineD3DDeviceParent_iface);
2744 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface,
2745 REFIID riid, void **object)
2747 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2748 return IDirect3DDevice8Impl_QueryInterface(&This->IDirect3DDevice8_iface, riid, object);
2751 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2753 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2754 return IDirect3DDevice8Impl_AddRef(&This->IDirect3DDevice8_iface);
2757 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2759 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2760 return IDirect3DDevice8Impl_Release(&This->IDirect3DDevice8_iface);
2763 /* IWineD3DDeviceParent methods */
2765 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2767 TRACE("iface %p, device %p\n", iface, device);
2770 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2771 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2772 WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2774 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2775 IDirect3DSurface8Impl *d3d_surface;
2776 BOOL lockable = TRUE;
2777 HRESULT hr;
2779 TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
2780 "\tpool %#x, level %u, face %u, surface %p\n",
2781 iface, container_parent, width, height, format, usage, pool, level, face, surface);
2784 if (pool == WINED3DPOOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC)) lockable = FALSE;
2786 hr = IDirect3DDevice8Impl_CreateSurface(This, width, height,
2787 d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2788 (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2789 if (FAILED(hr))
2791 ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2792 return hr;
2795 *surface = d3d_surface->wineD3DSurface;
2796 IWineD3DSurface_AddRef(*surface);
2798 d3d_surface->container = container_parent;
2799 IUnknown_Release(d3d_surface->parentDevice);
2800 d3d_surface->parentDevice = NULL;
2802 IDirect3DSurface8_Release((IDirect3DSurface8 *)d3d_surface);
2803 d3d_surface->forwardReference = container_parent;
2805 return hr;
2808 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2809 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
2810 WINED3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality, BOOL lockable,
2811 IWineD3DSurface **surface)
2813 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2814 IDirect3DSurface8Impl *d3d_surface;
2815 HRESULT hr;
2817 TRACE("iface %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2818 "\tmultisample_quality %u, lockable %u, surface %p\n",
2819 iface, container_parent, width, height, format, multisample_type, multisample_quality, lockable, surface);
2821 hr = IDirect3DDevice8_CreateRenderTarget(&This->IDirect3DDevice8_iface, width, height,
2822 d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2823 if (FAILED(hr))
2825 ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2826 return hr;
2829 *surface = d3d_surface->wineD3DSurface;
2830 IWineD3DSurface_AddRef(*surface);
2832 d3d_surface->container = (IUnknown *)&This->IDirect3DDevice8_iface;
2833 /* Implicit surfaces are created with an refcount of 0 */
2834 IUnknown_Release((IUnknown *)d3d_surface);
2836 return hr;
2839 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2840 UINT width, UINT height, enum wined3d_format_id format, WINED3DMULTISAMPLE_TYPE multisample_type,
2841 DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2843 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2844 IDirect3DSurface8Impl *d3d_surface;
2845 HRESULT hr;
2847 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2848 "\tmultisample_quality %u, discard %u, surface %p\n",
2849 iface, width, height, format, multisample_type, multisample_quality, discard, surface);
2851 hr = IDirect3DDevice8_CreateDepthStencilSurface(&This->IDirect3DDevice8_iface, width, height,
2852 d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2853 if (FAILED(hr))
2855 ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2856 return hr;
2859 *surface = d3d_surface->wineD3DSurface;
2860 IWineD3DSurface_AddRef(*surface);
2862 d3d_surface->container = (IUnknown *)&This->IDirect3DDevice8_iface;
2863 /* Implicit surfaces are created with an refcount of 0 */
2864 IUnknown_Release((IUnknown *)d3d_surface);
2866 return hr;
2869 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2870 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2871 WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2873 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2874 IDirect3DVolume8Impl *object;
2875 HRESULT hr;
2877 TRACE("iface %p, container_parent %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2878 iface, container_parent, width, height, depth, format, pool, usage, volume);
2880 /* Allocate the storage for the device */
2881 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2882 if (!object)
2884 FIXME("Allocation of memory failed\n");
2885 *volume = NULL;
2886 return D3DERR_OUTOFVIDEOMEMORY;
2889 hr = volume_init(object, This, width, height, depth, usage, format, pool);
2890 if (FAILED(hr))
2892 WARN("Failed to initialize volume, hr %#x.\n", hr);
2893 HeapFree(GetProcessHeap(), 0, object);
2894 return hr;
2897 *volume = object->wineD3DVolume;
2898 IWineD3DVolume_AddRef(*volume);
2899 IDirect3DVolume8_Release(&object->IDirect3DVolume8_iface);
2901 object->container = container_parent;
2902 object->forwardReference = container_parent;
2904 TRACE("(%p) Created volume %p\n", iface, object);
2906 return hr;
2909 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2910 WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2912 IDirect3DDevice8Impl *This = impl_from_IWineD3DDeviceParent(iface);
2913 D3DPRESENT_PARAMETERS local_parameters;
2914 IDirect3DSwapChain8 *d3d_swapchain;
2915 HRESULT hr;
2917 TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2919 /* Copy the presentation parameters */
2920 local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2921 local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2922 local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2923 local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2924 local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2925 local_parameters.SwapEffect = present_parameters->SwapEffect;
2926 local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2927 local_parameters.Windowed = present_parameters->Windowed;
2928 local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2929 local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2930 local_parameters.Flags = present_parameters->Flags;
2931 local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2932 local_parameters.FullScreen_PresentationInterval = present_parameters->PresentationInterval;
2934 hr = IDirect3DDevice8_CreateAdditionalSwapChain(&This->IDirect3DDevice8_iface,
2935 &local_parameters, &d3d_swapchain);
2936 if (FAILED(hr))
2938 ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2939 *swapchain = NULL;
2940 return hr;
2943 *swapchain = ((IDirect3DSwapChain8Impl *)d3d_swapchain)->wineD3DSwapChain;
2944 IWineD3DSwapChain_AddRef(*swapchain);
2945 IDirect3DSwapChain8_Release(d3d_swapchain);
2947 /* Copy back the presentation parameters */
2948 present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2949 present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2950 present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2951 present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2952 present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2953 present_parameters->SwapEffect = local_parameters.SwapEffect;
2954 present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2955 present_parameters->Windowed = local_parameters.Windowed;
2956 present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2957 present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2958 present_parameters->Flags = local_parameters.Flags;
2959 present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2960 present_parameters->PresentationInterval = local_parameters.FullScreen_PresentationInterval;
2962 return hr;
2965 static const IWineD3DDeviceParentVtbl d3d8_wined3d_device_parent_vtbl =
2967 /* IUnknown methods */
2968 device_parent_QueryInterface,
2969 device_parent_AddRef,
2970 device_parent_Release,
2971 /* IWineD3DDeviceParent methods */
2972 device_parent_WineD3DDeviceCreated,
2973 device_parent_CreateSurface,
2974 device_parent_CreateRenderTarget,
2975 device_parent_CreateDepthStencilSurface,
2976 device_parent_CreateVolume,
2977 device_parent_CreateSwapChain,
2980 static void setup_fpu(void)
2982 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2983 WORD cw;
2984 __asm__ volatile ("fnstcw %0" : "=m" (cw));
2985 cw = (cw & ~0xf3f) | 0x3f;
2986 __asm__ volatile ("fldcw %0" : : "m" (cw));
2987 #else
2988 FIXME("FPU setup not implemented for this platform.\n");
2989 #endif
2992 HRESULT device_init(IDirect3DDevice8Impl *device, struct wined3d *wined3d, UINT adapter,
2993 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
2995 WINED3DPRESENT_PARAMETERS wined3d_parameters;
2996 HRESULT hr;
2998 device->IDirect3DDevice8_iface.lpVtbl = &Direct3DDevice8_Vtbl;
2999 device->IWineD3DDeviceParent_iface.lpVtbl = &d3d8_wined3d_device_parent_vtbl;
3000 device->ref = 1;
3001 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3002 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3003 if (!device->handle_table.entries)
3005 ERR("Failed to allocate handle table memory.\n");
3006 return E_OUTOFMEMORY;
3008 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3010 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3012 wined3d_mutex_lock();
3013 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags,
3014 &device->IWineD3DDeviceParent_iface, &device->WineD3DDevice);
3015 if (FAILED(hr))
3017 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3018 wined3d_mutex_unlock();
3019 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3020 return hr;
3023 if (!parameters->Windowed)
3025 HWND device_window = parameters->hDeviceWindow;
3027 if (!focus_window) focus_window = device_window;
3028 if (FAILED(hr = IWineD3DDevice_AcquireFocusWindow(device->WineD3DDevice, focus_window)))
3030 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3031 IWineD3DDevice_Release(device->WineD3DDevice);
3032 wined3d_mutex_unlock();
3033 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3034 return hr;
3037 if (!device_window) device_window = focus_window;
3038 IWineD3DDevice_SetupFullscreenWindow(device->WineD3DDevice, device_window,
3039 parameters->BackBufferWidth,
3040 parameters->BackBufferHeight);
3043 if (flags & D3DCREATE_MULTITHREADED) IWineD3DDevice_SetMultithreaded(device->WineD3DDevice);
3045 wined3d_parameters.BackBufferWidth = parameters->BackBufferWidth;
3046 wined3d_parameters.BackBufferHeight = parameters->BackBufferHeight;
3047 wined3d_parameters.BackBufferFormat = wined3dformat_from_d3dformat(parameters->BackBufferFormat);
3048 wined3d_parameters.BackBufferCount = parameters->BackBufferCount;
3049 wined3d_parameters.MultiSampleType = parameters->MultiSampleType;
3050 wined3d_parameters.MultiSampleQuality = 0; /* d3d9 only */
3051 wined3d_parameters.SwapEffect = parameters->SwapEffect;
3052 wined3d_parameters.hDeviceWindow = parameters->hDeviceWindow;
3053 wined3d_parameters.Windowed = parameters->Windowed;
3054 wined3d_parameters.EnableAutoDepthStencil = parameters->EnableAutoDepthStencil;
3055 wined3d_parameters.AutoDepthStencilFormat = wined3dformat_from_d3dformat(parameters->AutoDepthStencilFormat);
3056 wined3d_parameters.Flags = parameters->Flags;
3057 wined3d_parameters.FullScreen_RefreshRateInHz = parameters->FullScreen_RefreshRateInHz;
3058 wined3d_parameters.PresentationInterval = parameters->FullScreen_PresentationInterval;
3059 wined3d_parameters.AutoRestoreDisplayMode = TRUE;
3061 hr = IWineD3DDevice_Init3D(device->WineD3DDevice, &wined3d_parameters);
3062 if (FAILED(hr))
3064 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3065 IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3066 IWineD3DDevice_Release(device->WineD3DDevice);
3067 wined3d_mutex_unlock();
3068 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3069 return hr;
3072 hr = IWineD3DDevice_SetRenderState(device->WineD3DDevice, WINED3DRS_POINTSIZE_MIN, 0);
3073 wined3d_mutex_unlock();
3074 if (FAILED(hr))
3076 ERR("Failed to set minimum pointsize, hr %#x.\n", hr);
3077 goto err;
3080 parameters->BackBufferWidth = wined3d_parameters.BackBufferWidth;
3081 parameters->BackBufferHeight = wined3d_parameters.BackBufferHeight;
3082 parameters->BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters.BackBufferFormat);
3083 parameters->BackBufferCount = wined3d_parameters.BackBufferCount;
3084 parameters->MultiSampleType = wined3d_parameters.MultiSampleType;
3085 parameters->SwapEffect = wined3d_parameters.SwapEffect;
3086 parameters->hDeviceWindow = wined3d_parameters.hDeviceWindow;
3087 parameters->Windowed = wined3d_parameters.Windowed;
3088 parameters->EnableAutoDepthStencil = wined3d_parameters.EnableAutoDepthStencil;
3089 parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(wined3d_parameters.AutoDepthStencilFormat);
3090 parameters->Flags = wined3d_parameters.Flags;
3091 parameters->FullScreen_RefreshRateInHz = wined3d_parameters.FullScreen_RefreshRateInHz;
3092 parameters->FullScreen_PresentationInterval = wined3d_parameters.PresentationInterval;
3094 device->declArraySize = 16;
3095 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3096 if (!device->decls)
3098 ERR("Failed to allocate FVF vertex delcaration map memory.\n");
3099 hr = E_OUTOFMEMORY;
3100 goto err;
3103 return D3D_OK;
3105 err:
3106 wined3d_mutex_lock();
3107 IWineD3DDevice_Uninit3D(device->WineD3DDevice, D3D8CB_DestroySwapChain);
3108 IWineD3DDevice_ReleaseFocusWindow(device->WineD3DDevice);
3109 IWineD3DDevice_Release(device->WineD3DDevice);
3110 wined3d_mutex_unlock();
3111 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3112 return hr;