d3d8: Introduce a helper function to create a wined3d_swapchain_desc from D3DPRESENT_...
[wine/multimedia.git] / dlls / d3d8 / device.c
blobeea4a244426f28bb643be23821b6be648893ce90
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 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
168 const D3DPRESENT_PARAMETERS *present_parameters)
170 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
171 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
172 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
173 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
174 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
175 swapchain_desc->multisample_quality = 0; /* d3d9 only */
176 swapchain_desc->swap_effect = present_parameters->SwapEffect;
177 swapchain_desc->device_window = present_parameters->hDeviceWindow;
178 swapchain_desc->windowed = present_parameters->Windowed;
179 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
180 swapchain_desc->auto_depth_stencil_format
181 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
182 swapchain_desc->flags = present_parameters->Flags;
183 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
184 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
185 swapchain_desc->auto_restore_display_mode = TRUE;
188 /* Handle table functions */
189 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
191 struct d3d8_handle_entry *entry;
193 if (t->free_entries)
195 DWORD index = t->free_entries - t->entries;
196 /* Use a free handle */
197 entry = t->free_entries;
198 if (entry->type != D3D8_HANDLE_FREE)
200 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
201 return D3D8_INVALID_HANDLE;
203 t->free_entries = entry->object;
204 entry->object = object;
205 entry->type = type;
207 return index;
210 if (!(t->entry_count < t->table_size))
212 /* Grow the table */
213 UINT new_size = t->table_size + (t->table_size >> 1);
214 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
215 0, t->entries, new_size * sizeof(*t->entries));
216 if (!new_entries)
218 ERR("Failed to grow the handle table.\n");
219 return D3D8_INVALID_HANDLE;
221 t->entries = new_entries;
222 t->table_size = new_size;
225 entry = &t->entries[t->entry_count];
226 entry->object = object;
227 entry->type = type;
229 return t->entry_count++;
232 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
234 struct d3d8_handle_entry *entry;
235 void *object;
237 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
239 WARN("Invalid handle %u passed.\n", handle);
240 return NULL;
243 entry = &t->entries[handle];
244 if (entry->type != type)
246 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
247 return NULL;
250 object = entry->object;
251 entry->object = t->free_entries;
252 entry->type = D3D8_HANDLE_FREE;
253 t->free_entries = entry;
255 return object;
258 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
260 struct d3d8_handle_entry *entry;
262 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
264 WARN("Invalid handle %u passed.\n", handle);
265 return NULL;
268 entry = &t->entries[handle];
269 if (entry->type != type)
271 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
272 return NULL;
275 return entry->object;
278 static inline struct d3d8_device *impl_from_IDirect3DDevice8(IDirect3DDevice8 *iface)
280 return CONTAINING_RECORD(iface, struct d3d8_device, IDirect3DDevice8_iface);
283 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
285 TRACE("iface %p, riid %s, out %p.\n",
286 iface, debugstr_guid(riid), out);
288 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
289 || IsEqualGUID(riid, &IID_IUnknown))
291 IUnknown_AddRef(iface);
292 *out = iface;
293 return S_OK;
296 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
298 *out = NULL;
299 return E_NOINTERFACE;
302 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
304 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
305 ULONG ref = InterlockedIncrement(&device->ref);
307 TRACE("%p increasing refcount to %u.\n", iface, ref);
309 return ref;
312 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
314 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
315 ULONG ref;
317 if (device->inDestruction)
318 return 0;
320 ref = InterlockedDecrement(&device->ref);
322 TRACE("%p decreasing refcount to %u.\n", iface, ref);
324 if (!ref)
326 IDirect3D8 *parent = device->d3d_parent;
327 unsigned i;
329 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
331 wined3d_mutex_lock();
333 device->inDestruction = TRUE;
335 for (i = 0; i < device->numConvertedDecls; ++i)
337 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
339 HeapFree(GetProcessHeap(), 0, device->decls);
341 wined3d_device_uninit_3d(device->wined3d_device);
342 wined3d_device_release_focus_window(device->wined3d_device);
343 wined3d_device_decref(device->wined3d_device);
344 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
345 HeapFree(GetProcessHeap(), 0, device);
347 wined3d_mutex_unlock();
349 IDirect3D8_Release(parent);
351 return ref;
354 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
356 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
358 TRACE("iface %p.\n", iface);
360 if (device->lost)
362 TRACE("Device is lost.\n");
363 return D3DERR_DEVICENOTRESET;
366 return D3D_OK;
369 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
371 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
372 HRESULT hr;
374 TRACE("iface %p.\n", iface);
376 wined3d_mutex_lock();
377 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
378 wined3d_mutex_unlock();
380 return hr;
383 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
385 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
387 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
389 if (byte_count)
390 FIXME("Byte count ignored.\n");
392 wined3d_mutex_lock();
393 wined3d_device_evict_managed_resources(device->wined3d_device);
394 wined3d_mutex_unlock();
396 return D3D_OK;
399 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
401 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
403 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
405 if (!d3d8)
406 return D3DERR_INVALIDCALL;
408 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
411 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
413 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
414 WINED3DCAPS *wined3d_caps;
415 HRESULT hr;
417 TRACE("iface %p, caps %p.\n", iface, caps);
419 if (!caps)
420 return D3DERR_INVALIDCALL;
422 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
423 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
425 wined3d_mutex_lock();
426 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
427 wined3d_mutex_unlock();
429 fixup_caps(wined3d_caps);
430 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
431 HeapFree(GetProcessHeap(), 0, wined3d_caps);
433 return hr;
436 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
438 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
439 struct wined3d_display_mode wined3d_mode;
440 HRESULT hr;
442 TRACE("iface %p, mode %p.\n", iface, mode);
444 wined3d_mutex_lock();
445 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
446 wined3d_mutex_unlock();
448 if (SUCCEEDED(hr))
450 mode->Width = wined3d_mode.width;
451 mode->Height = wined3d_mode.height;
452 mode->RefreshRate = wined3d_mode.refresh_rate;
453 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
456 return hr;
459 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
460 D3DDEVICE_CREATION_PARAMETERS *parameters)
462 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
463 HRESULT hr;
465 TRACE("iface %p, parameters %p.\n", iface, parameters);
467 wined3d_mutex_lock();
468 hr = wined3d_device_get_creation_parameters(device->wined3d_device,
469 (struct wined3d_device_creation_parameters *)parameters);
470 wined3d_mutex_unlock();
472 return hr;
475 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
476 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
478 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
479 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
480 HRESULT hr;
482 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
483 iface, hotspot_x, hotspot_y, bitmap);
485 if (!bitmap)
487 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
488 return D3DERR_INVALIDCALL;
491 wined3d_mutex_lock();
492 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
493 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
494 wined3d_mutex_unlock();
496 return hr;
499 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
501 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
503 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
505 wined3d_mutex_lock();
506 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
507 wined3d_mutex_unlock();
510 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
512 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
513 BOOL ret;
515 TRACE("iface %p, show %#x.\n", iface, show);
517 wined3d_mutex_lock();
518 ret = wined3d_device_show_cursor(device->wined3d_device, show);
519 wined3d_mutex_unlock();
521 return ret;
524 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
525 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
527 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
528 struct wined3d_swapchain_desc desc;
529 struct d3d8_swapchain *object;
530 HRESULT hr;
532 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
533 iface, present_parameters, swapchain);
535 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
536 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
537 *swapchain = &object->IDirect3DSwapChain8_iface;
539 present_parameters->BackBufferWidth = desc.backbuffer_width;
540 present_parameters->BackBufferHeight = desc.backbuffer_height;
541 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(desc.backbuffer_format);
542 present_parameters->BackBufferCount = desc.backbuffer_count;
543 present_parameters->MultiSampleType = desc.multisample_type;
544 present_parameters->SwapEffect = desc.swap_effect;
545 present_parameters->hDeviceWindow = desc.device_window;
546 present_parameters->Windowed = desc.windowed;
547 present_parameters->EnableAutoDepthStencil = desc.enable_auto_depth_stencil;
548 present_parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(desc.auto_depth_stencil_format);
549 present_parameters->Flags = desc.flags;
550 present_parameters->FullScreen_RefreshRateInHz = desc.refresh_rate;
551 present_parameters->FullScreen_PresentationInterval = desc.swap_interval;
553 return D3D_OK;
556 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
558 struct wined3d_resource_desc desc;
560 wined3d_resource_get_desc(resource, &desc);
561 if (desc.pool == WINED3D_POOL_DEFAULT)
563 struct d3d8_surface *surface;
565 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
567 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
568 return D3DERR_DEVICELOST;
571 surface = wined3d_resource_get_parent(resource);
572 if (surface->refcount)
574 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
575 return D3DERR_DEVICELOST;
578 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
581 return D3D_OK;
584 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
585 D3DPRESENT_PARAMETERS *present_parameters)
587 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
588 struct wined3d_swapchain_desc swapchain_desc;
589 HRESULT hr;
591 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
593 wined3d_mutex_lock();
594 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
595 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc, reset_enum_callback)))
597 hr = wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
598 device->lost = FALSE;
600 else
602 device->lost = TRUE;
604 wined3d_mutex_unlock();
606 return hr;
609 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
610 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
612 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
613 HRESULT hr;
615 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
616 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
618 wined3d_mutex_lock();
619 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
620 dst_window_override, dirty_region, 0);
621 wined3d_mutex_unlock();
623 return hr;
626 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
627 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
629 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
630 struct wined3d_surface *wined3d_surface = NULL;
631 struct d3d8_surface *surface_impl;
632 HRESULT hr;
634 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
635 iface, backbuffer_idx, backbuffer_type, backbuffer);
637 wined3d_mutex_lock();
638 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
639 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
640 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
642 surface_impl = wined3d_surface_get_parent(wined3d_surface);
643 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
644 IDirect3DSurface8_AddRef(*backbuffer);
645 wined3d_surface_decref(wined3d_surface);
647 wined3d_mutex_unlock();
649 return hr;
652 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
654 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
655 HRESULT hr;
657 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
659 wined3d_mutex_lock();
660 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
661 wined3d_mutex_unlock();
663 return hr;
666 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
668 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
670 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
672 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
673 wined3d_mutex_lock();
674 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
675 wined3d_mutex_unlock();
678 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
680 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
682 TRACE("iface %p, ramp %p.\n", iface, ramp);
684 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
685 wined3d_mutex_lock();
686 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
687 wined3d_mutex_unlock();
690 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
691 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
692 D3DPOOL pool, IDirect3DTexture8 **texture)
694 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
695 struct d3d8_texture *object;
696 HRESULT hr;
698 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
699 iface, width, height, levels, usage, format, pool, texture);
701 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
702 if (!object)
704 ERR("Failed to allocate texture memory.\n");
705 return D3DERR_OUTOFVIDEOMEMORY;
708 hr = texture_init(object, device, width, height, levels, usage, format, pool);
709 if (FAILED(hr))
711 WARN("Failed to initialize texture, hr %#x.\n", hr);
712 HeapFree(GetProcessHeap(), 0, object);
713 return hr;
716 TRACE("Created texture %p.\n", object);
717 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
719 return D3D_OK;
722 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
723 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
724 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
726 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
727 struct d3d8_texture *object;
728 HRESULT hr;
730 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
731 iface, width, height, depth, levels, usage, format, pool, texture);
733 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
734 if (!object)
736 ERR("Failed to allocate volume texture memory.\n");
737 return D3DERR_OUTOFVIDEOMEMORY;
740 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
741 if (FAILED(hr))
743 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
744 HeapFree(GetProcessHeap(), 0, object);
745 return hr;
748 TRACE("Created volume texture %p.\n", object);
749 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
751 return D3D_OK;
754 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
755 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
757 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
758 struct d3d8_texture *object;
759 HRESULT hr;
761 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
762 iface, edge_length, levels, usage, format, pool, texture);
764 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
765 if (!object)
767 ERR("Failed to allocate cube texture memory.\n");
768 return D3DERR_OUTOFVIDEOMEMORY;
771 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
772 if (FAILED(hr))
774 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
775 HeapFree(GetProcessHeap(), 0, object);
776 return hr;
779 TRACE("Created cube texture %p.\n", object);
780 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
782 return hr;
785 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
786 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
788 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
789 struct d3d8_vertexbuffer *object;
790 HRESULT hr;
792 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
793 iface, size, usage, fvf, pool, buffer);
795 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
796 if (!object)
798 ERR("Failed to allocate buffer memory.\n");
799 return D3DERR_OUTOFVIDEOMEMORY;
802 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
803 if (FAILED(hr))
805 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
806 HeapFree(GetProcessHeap(), 0, object);
807 return hr;
810 TRACE("Created vertex buffer %p.\n", object);
811 *buffer = &object->IDirect3DVertexBuffer8_iface;
813 return D3D_OK;
816 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
817 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
819 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
820 struct d3d8_indexbuffer *object;
821 HRESULT hr;
823 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
824 iface, size, usage, format, pool, buffer);
826 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
827 if (!object)
829 ERR("Failed to allocate buffer memory.\n");
830 return D3DERR_OUTOFVIDEOMEMORY;
833 hr = indexbuffer_init(object, device, size, usage, format, pool);
834 if (FAILED(hr))
836 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
837 HeapFree(GetProcessHeap(), 0, object);
838 return hr;
841 TRACE("Created index buffer %p.\n", object);
842 *buffer = &object->IDirect3DIndexBuffer8_iface;
844 return D3D_OK;
847 static HRESULT d3d8_device_CreateSurface(struct d3d8_device *device, UINT width,
848 UINT height, D3DFORMAT format, BOOL lockable, BOOL discard, UINT level,
849 IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool, D3DMULTISAMPLE_TYPE multisample_type,
850 DWORD multisample_quality)
852 struct d3d8_surface *object;
853 HRESULT hr;
855 TRACE("device %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p,\n"
856 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
857 device, width, height, format, lockable, discard, level, surface,
858 usage, pool, multisample_type, multisample_quality);
860 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
862 FIXME("Failed to allocate surface memory.\n");
863 return D3DERR_OUTOFVIDEOMEMORY;
866 hr = surface_init(object, device, width, height, format, lockable, discard, level, usage,
867 pool, multisample_type, multisample_quality);
868 if (FAILED(hr))
870 WARN("Failed to initialize surface, hr %#x.\n", hr);
871 HeapFree(GetProcessHeap(), 0, object);
872 return hr;
875 TRACE("Created surface %p.\n", object);
876 *surface = &object->IDirect3DSurface8_iface;
878 return D3D_OK;
881 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
882 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
883 IDirect3DSurface8 **surface)
885 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
886 HRESULT hr;
888 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
889 iface, width, height, format, multisample_type, lockable, surface);
891 hr = d3d8_device_CreateSurface(device, width, height, format, lockable,
892 FALSE, 0, surface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT,
893 multisample_type, 0);
895 return hr;
898 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
899 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
900 IDirect3DSurface8 **surface)
902 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
903 HRESULT hr;
905 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
906 iface, width, height, format, multisample_type, surface);
908 /* TODO: Verify that Discard is false */
909 hr = d3d8_device_CreateSurface(device, width, height, format, TRUE, FALSE,
910 0, surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
912 return hr;
915 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
916 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
917 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
919 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
920 HRESULT hr;
922 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
923 iface, width, height, format, surface);
925 hr = d3d8_device_CreateSurface(device, width, height, format, TRUE, FALSE,
926 0, surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
928 return hr;
931 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
932 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
933 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
935 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
936 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
937 enum wined3d_format_id src_format, dst_format;
938 struct wined3d_resource_desc wined3d_desc;
939 struct wined3d_resource *wined3d_resource;
940 UINT src_w, src_h;
941 HRESULT hr;
943 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
944 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
946 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
947 * destination texture is in WINED3D_POOL_DEFAULT. */
949 wined3d_mutex_lock();
950 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
951 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
952 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
954 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
955 wined3d_mutex_unlock();
956 return D3DERR_INVALIDCALL;
958 src_format = wined3d_desc.format;
959 src_w = wined3d_desc.width;
960 src_h = wined3d_desc.height;
962 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
963 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
964 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
966 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
967 wined3d_mutex_unlock();
968 return D3DERR_INVALIDCALL;
970 dst_format = wined3d_desc.format;
972 /* Check that the source and destination formats match */
973 if (src_format != dst_format && WINED3DFMT_UNKNOWN != dst_format)
975 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
976 src_surface, dst_surface);
977 wined3d_mutex_unlock();
978 return D3DERR_INVALIDCALL;
980 else if (WINED3DFMT_UNKNOWN == dst_format)
982 TRACE("Converting destination surface from WINED3DFMT_UNKNOWN to the source format.\n");
983 if (FAILED(hr = wined3d_surface_update_desc(dst->wined3d_surface, wined3d_desc.width, wined3d_desc.height,
984 src_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality)))
986 WARN("Failed to update surface desc, hr %#x.\n", hr);
987 wined3d_mutex_unlock();
988 return hr;
992 /* Quick if complete copy ... */
993 if (!rect_count && !src_rects && !dst_points)
995 RECT rect = {0, 0, src_w, src_h};
996 wined3d_surface_blt(dst->wined3d_surface, &rect,
997 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
999 else
1001 unsigned int i;
1002 /* Copy rect by rect */
1003 if (src_rects && dst_points)
1005 for (i = 0; i < rect_count; ++i)
1007 UINT w = src_rects[i].right - src_rects[i].left;
1008 UINT h = src_rects[i].bottom - src_rects[i].top;
1009 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1010 dst_points[i].x + w, dst_points[i].y + h};
1012 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1013 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1016 else
1018 for (i = 0; i < rect_count; ++i)
1020 UINT w = src_rects[i].right - src_rects[i].left;
1021 UINT h = src_rects[i].bottom - src_rects[i].top;
1022 RECT dst_rect = {0, 0, w, h};
1024 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1025 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1029 wined3d_mutex_unlock();
1031 return WINED3D_OK;
1034 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1035 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1037 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1038 struct d3d8_texture *src_impl, *dst_impl;
1039 HRESULT hr;
1041 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1043 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1044 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1046 wined3d_mutex_lock();
1047 hr = wined3d_device_update_texture(device->wined3d_device,
1048 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1049 wined3d_mutex_unlock();
1051 return hr;
1054 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1056 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1057 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1058 HRESULT hr;
1060 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1062 if (!dst_surface)
1064 WARN("Invalid destination surface passed.\n");
1065 return D3DERR_INVALIDCALL;
1068 wined3d_mutex_lock();
1069 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1070 wined3d_mutex_unlock();
1072 return hr;
1075 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1076 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1078 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1079 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1080 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1081 struct wined3d_surface *original_ds = NULL;
1082 HRESULT hr;
1084 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1086 wined3d_mutex_lock();
1088 if (ds_impl)
1090 struct wined3d_resource_desc ds_desc, rt_desc;
1091 struct wined3d_resource *wined3d_resource;
1092 struct wined3d_surface *original_rt = NULL;
1094 /* If no render target is passed in check the size against the current RT */
1095 if (!render_target)
1097 hr = wined3d_device_get_render_target(device->wined3d_device, 0, &original_rt);
1098 if (FAILED(hr) || !original_rt)
1100 wined3d_mutex_unlock();
1101 return hr;
1103 wined3d_resource = wined3d_surface_get_resource(original_rt);
1104 wined3d_surface_decref(original_rt);
1106 else
1107 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1108 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1110 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1111 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1113 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1115 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1116 wined3d_mutex_unlock();
1117 return D3DERR_INVALIDCALL;
1121 hr = wined3d_device_get_depth_stencil(device->wined3d_device, &original_ds);
1122 if (hr == WINED3D_OK || hr == WINED3DERR_NOTFOUND)
1124 hr = wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1125 if (SUCCEEDED(hr) && render_target)
1127 hr = wined3d_device_set_render_target(device->wined3d_device, 0, rt_impl->wined3d_surface, TRUE);
1128 if (FAILED(hr))
1129 wined3d_device_set_depth_stencil(device->wined3d_device, original_ds);
1132 if (original_ds)
1133 wined3d_surface_decref(original_ds);
1135 wined3d_mutex_unlock();
1137 return hr;
1140 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1142 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1143 struct wined3d_surface *wined3d_surface;
1144 struct d3d8_surface *surface_impl;
1145 HRESULT hr;
1147 TRACE("iface %p, render_target %p.\n", iface, render_target);
1149 if (!render_target)
1150 return D3DERR_INVALIDCALL;
1152 wined3d_mutex_lock();
1153 hr = wined3d_device_get_render_target(device->wined3d_device, 0, &wined3d_surface);
1154 if (SUCCEEDED(hr) && wined3d_surface)
1156 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1157 *render_target = &surface_impl->IDirect3DSurface8_iface;
1158 IDirect3DSurface8_AddRef(*render_target);
1159 wined3d_surface_decref(wined3d_surface);
1161 else
1163 ERR("Failed to get wined3d render target, hr %#x.\n", hr);
1164 *render_target = NULL;
1166 wined3d_mutex_unlock();
1168 return hr;
1171 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1173 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1174 struct wined3d_surface *wined3d_surface;
1175 struct d3d8_surface *surface_impl;
1176 HRESULT hr;
1178 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1180 if (!depth_stencil)
1181 return D3DERR_INVALIDCALL;
1183 wined3d_mutex_lock();
1184 hr = wined3d_device_get_depth_stencil(device->wined3d_device, &wined3d_surface);
1185 if (SUCCEEDED(hr))
1187 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1188 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1189 IDirect3DSurface8_AddRef(*depth_stencil);
1190 wined3d_surface_decref(wined3d_surface);
1192 else
1194 if (hr != WINED3DERR_NOTFOUND)
1195 ERR("Failed to get wined3d depth stencil, hr %#x.\n", hr);
1196 *depth_stencil = NULL;
1198 wined3d_mutex_unlock();
1200 return hr;
1203 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1205 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1206 HRESULT hr;
1208 TRACE("iface %p.\n", iface);
1210 wined3d_mutex_lock();
1211 hr = wined3d_device_begin_scene(device->wined3d_device);
1212 wined3d_mutex_unlock();
1214 return hr;
1217 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1219 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1220 HRESULT hr;
1222 TRACE("iface %p.\n", iface);
1224 wined3d_mutex_lock();
1225 hr = wined3d_device_end_scene(device->wined3d_device);
1226 wined3d_mutex_unlock();
1228 return hr;
1231 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1232 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1234 const struct wined3d_color c =
1236 ((color >> 16) & 0xff) / 255.0f,
1237 ((color >> 8) & 0xff) / 255.0f,
1238 (color & 0xff) / 255.0f,
1239 ((color >> 24) & 0xff) / 255.0f,
1241 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1242 HRESULT hr;
1244 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1245 iface, rect_count, rects, flags, color, z, stencil);
1247 wined3d_mutex_lock();
1248 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1249 wined3d_mutex_unlock();
1251 return hr;
1254 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1255 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1257 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1258 HRESULT hr;
1260 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1262 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1263 wined3d_mutex_lock();
1264 hr = wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1265 wined3d_mutex_unlock();
1267 return hr;
1270 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1271 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1273 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1274 HRESULT hr;
1276 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1278 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1279 wined3d_mutex_lock();
1280 hr = wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1281 wined3d_mutex_unlock();
1283 return hr;
1286 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1287 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1289 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1290 HRESULT hr;
1292 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1294 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1295 wined3d_mutex_lock();
1296 hr = wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1297 wined3d_mutex_unlock();
1299 return hr;
1302 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1304 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1305 HRESULT hr;
1307 TRACE("iface %p, viewport %p.\n", iface, viewport);
1309 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1310 wined3d_mutex_lock();
1311 hr = wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1312 wined3d_mutex_unlock();
1314 return hr;
1317 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1319 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1320 HRESULT hr;
1322 TRACE("iface %p, viewport %p.\n", iface, viewport);
1324 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1325 wined3d_mutex_lock();
1326 hr = wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1327 wined3d_mutex_unlock();
1329 return hr;
1332 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1334 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1335 HRESULT hr;
1337 TRACE("iface %p, material %p.\n", iface, material);
1339 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1340 wined3d_mutex_lock();
1341 hr = wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1342 wined3d_mutex_unlock();
1344 return hr;
1347 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1350 HRESULT hr;
1352 TRACE("iface %p, material %p.\n", iface, material);
1354 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1355 wined3d_mutex_lock();
1356 hr = wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1357 wined3d_mutex_unlock();
1359 return hr;
1362 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1364 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1365 HRESULT hr;
1367 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1369 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1370 wined3d_mutex_lock();
1371 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1372 wined3d_mutex_unlock();
1374 return hr;
1377 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1379 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1380 HRESULT hr;
1382 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1384 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1385 wined3d_mutex_lock();
1386 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1387 wined3d_mutex_unlock();
1389 return hr;
1392 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1394 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1395 HRESULT hr;
1397 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1399 wined3d_mutex_lock();
1400 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1401 wined3d_mutex_unlock();
1403 return hr;
1406 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1408 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1409 HRESULT hr;
1411 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1413 wined3d_mutex_lock();
1414 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1415 wined3d_mutex_unlock();
1417 return hr;
1420 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1422 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1423 HRESULT hr;
1425 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1427 wined3d_mutex_lock();
1428 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1429 wined3d_mutex_unlock();
1431 return hr;
1434 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1436 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1437 HRESULT hr;
1439 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1441 wined3d_mutex_lock();
1442 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1443 wined3d_mutex_unlock();
1445 return hr;
1448 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1449 D3DRENDERSTATETYPE state, DWORD value)
1451 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1452 HRESULT hr;
1454 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1456 wined3d_mutex_lock();
1457 switch (state)
1459 case D3DRS_ZBIAS:
1460 hr = wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1461 break;
1463 default:
1464 hr = wined3d_device_set_render_state(device->wined3d_device, state, value);
1466 wined3d_mutex_unlock();
1468 return hr;
1471 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1472 D3DRENDERSTATETYPE state, DWORD *value)
1474 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1475 HRESULT hr;
1477 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1479 wined3d_mutex_lock();
1480 switch (state)
1482 case D3DRS_ZBIAS:
1483 hr = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1484 break;
1486 default:
1487 hr = wined3d_device_get_render_state(device->wined3d_device, state, value);
1489 wined3d_mutex_unlock();
1491 return hr;
1494 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1496 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1497 HRESULT hr;
1499 TRACE("iface %p.\n", iface);
1501 wined3d_mutex_lock();
1502 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1503 wined3d_mutex_unlock();
1505 return hr;
1508 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1510 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1511 struct wined3d_stateblock *stateblock;
1512 HRESULT hr;
1514 TRACE("iface %p, token %p.\n", iface, token);
1516 /* Tell wineD3D to endstateblock before anything else (in case we run out
1517 * of memory later and cause locking problems)
1519 wined3d_mutex_lock();
1520 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1521 if (FAILED(hr))
1523 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1524 wined3d_mutex_unlock();
1525 return hr;
1528 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1529 wined3d_mutex_unlock();
1531 if (*token == D3D8_INVALID_HANDLE)
1533 ERR("Failed to create a handle\n");
1534 wined3d_mutex_lock();
1535 wined3d_stateblock_decref(stateblock);
1536 wined3d_mutex_unlock();
1537 return E_FAIL;
1539 ++*token;
1541 TRACE("Returning %#x (%p).\n", *token, stateblock);
1543 return hr;
1546 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1548 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1549 struct wined3d_stateblock *stateblock;
1550 HRESULT hr;
1552 TRACE("iface %p, token %#x.\n", iface, token);
1554 if (!token)
1555 return D3D_OK;
1557 wined3d_mutex_lock();
1558 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1559 if (!stateblock)
1561 WARN("Invalid handle (%#x) passed.\n", token);
1562 wined3d_mutex_unlock();
1563 return D3DERR_INVALIDCALL;
1565 hr = wined3d_stateblock_apply(stateblock);
1566 wined3d_mutex_unlock();
1568 return hr;
1571 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1574 struct wined3d_stateblock *stateblock;
1575 HRESULT hr;
1577 TRACE("iface %p, token %#x.\n", iface, token);
1579 wined3d_mutex_lock();
1580 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1581 if (!stateblock)
1583 WARN("Invalid handle (%#x) passed.\n", token);
1584 wined3d_mutex_unlock();
1585 return D3DERR_INVALIDCALL;
1587 hr = wined3d_stateblock_capture(stateblock);
1588 wined3d_mutex_unlock();
1590 return hr;
1593 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1595 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1596 struct wined3d_stateblock *stateblock;
1598 TRACE("iface %p, token %#x.\n", iface, token);
1600 wined3d_mutex_lock();
1601 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1603 if (!stateblock)
1605 WARN("Invalid handle (%#x) passed.\n", token);
1606 wined3d_mutex_unlock();
1607 return D3DERR_INVALIDCALL;
1610 if (wined3d_stateblock_decref(stateblock))
1612 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1614 wined3d_mutex_unlock();
1616 return D3D_OK;
1619 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1620 D3DSTATEBLOCKTYPE type, DWORD *handle)
1622 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1623 struct wined3d_stateblock *stateblock;
1624 HRESULT hr;
1626 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1628 if (type != D3DSBT_ALL
1629 && type != D3DSBT_PIXELSTATE
1630 && type != D3DSBT_VERTEXSTATE)
1632 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1633 return D3DERR_INVALIDCALL;
1636 wined3d_mutex_lock();
1637 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1638 if (FAILED(hr))
1640 wined3d_mutex_unlock();
1641 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1642 return hr;
1645 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1646 wined3d_mutex_unlock();
1648 if (*handle == D3D8_INVALID_HANDLE)
1650 ERR("Failed to allocate a handle.\n");
1651 wined3d_mutex_lock();
1652 wined3d_stateblock_decref(stateblock);
1653 wined3d_mutex_unlock();
1654 return E_FAIL;
1656 ++*handle;
1658 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1660 return hr;
1663 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1665 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1666 HRESULT hr;
1668 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1669 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1671 wined3d_mutex_lock();
1672 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1673 wined3d_mutex_unlock();
1675 return hr;
1678 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1680 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1681 HRESULT hr;
1683 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1685 wined3d_mutex_lock();
1686 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1687 wined3d_mutex_unlock();
1689 return hr;
1692 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1694 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1695 struct wined3d_texture *wined3d_texture;
1696 struct d3d8_texture *texture_impl;
1697 HRESULT hr;
1699 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1701 if (!texture)
1702 return D3DERR_INVALIDCALL;
1704 wined3d_mutex_lock();
1705 hr = wined3d_device_get_texture(device->wined3d_device, stage, &wined3d_texture);
1706 if (FAILED(hr))
1708 WARN("Failed to get texture for stage %u, hr %#x.\n", stage, hr);
1709 wined3d_mutex_unlock();
1710 *texture = NULL;
1711 return hr;
1714 if (wined3d_texture)
1716 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1717 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1718 IDirect3DBaseTexture8_AddRef(*texture);
1719 wined3d_texture_decref(wined3d_texture);
1721 else
1723 *texture = NULL;
1725 wined3d_mutex_unlock();
1727 return D3D_OK;
1730 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1732 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1733 struct d3d8_texture *texture_impl;
1734 HRESULT hr;
1736 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1738 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1740 wined3d_mutex_lock();
1741 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1742 texture_impl ? texture_impl->wined3d_texture : NULL);
1743 wined3d_mutex_unlock();
1745 return hr;
1748 static const struct tss_lookup
1750 BOOL sampler_state;
1751 enum wined3d_texture_stage_state state;
1753 tss_lookup[] =
1755 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1756 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1757 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1758 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1759 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1760 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1761 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1762 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1763 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1764 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1765 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1766 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1767 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1768 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1769 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1770 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1771 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1772 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1773 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1774 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1775 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1776 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1777 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1778 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1779 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1780 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1781 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1782 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1783 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1786 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1787 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1789 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1790 const struct tss_lookup *l;
1791 HRESULT hr;
1793 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1795 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1797 WARN("Invalid Type %#x passed.\n", Type);
1798 return D3D_OK;
1801 l = &tss_lookup[Type];
1803 wined3d_mutex_lock();
1804 if (l->sampler_state)
1805 hr = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state, value);
1806 else
1807 hr = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state, value);
1808 wined3d_mutex_unlock();
1810 return hr;
1813 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1814 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1816 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1817 const struct tss_lookup *l;
1818 HRESULT hr;
1820 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1822 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1824 WARN("Invalid type %#x passed.\n", type);
1825 return D3D_OK;
1828 l = &tss_lookup[type];
1830 wined3d_mutex_lock();
1831 if (l->sampler_state)
1832 hr = wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1833 else
1834 hr = wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1835 wined3d_mutex_unlock();
1837 return hr;
1840 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1842 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1843 HRESULT hr;
1845 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1847 wined3d_mutex_lock();
1848 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1849 wined3d_mutex_unlock();
1851 return hr;
1854 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1855 DWORD info_id, void *info, DWORD info_size)
1857 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1859 return D3D_OK;
1862 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1863 UINT palette_idx, const PALETTEENTRY *entries)
1865 FIXME("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1867 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1868 * does not have a d3d8/9-style palette API */
1870 return D3DERR_INVALIDCALL;
1873 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1874 UINT palette_idx, PALETTEENTRY *entries)
1876 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1878 return D3DERR_INVALIDCALL;
1881 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1883 FIXME("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1885 return D3DERR_INVALIDCALL;
1888 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1890 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1892 return D3DERR_INVALIDCALL;
1895 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1896 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1898 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1899 HRESULT hr;
1901 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1902 iface, primitive_type, start_vertex, primitive_count);
1904 wined3d_mutex_lock();
1905 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1906 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1907 vertex_count_from_primitive_count(primitive_type, primitive_count));
1908 wined3d_mutex_unlock();
1910 return hr;
1913 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1914 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1915 UINT start_idx, UINT primitive_count)
1917 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1918 HRESULT hr;
1920 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1921 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1923 wined3d_mutex_lock();
1924 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1925 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1926 vertex_count_from_primitive_count(primitive_type, primitive_count));
1927 wined3d_mutex_unlock();
1929 return hr;
1932 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1933 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
1934 UINT stride)
1936 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1937 HRESULT hr;
1939 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1940 iface, primitive_type, primitive_count, data, stride);
1942 wined3d_mutex_lock();
1943 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1944 hr = wined3d_device_draw_primitive_up(device->wined3d_device,
1945 vertex_count_from_primitive_count(primitive_type, primitive_count),
1946 data, stride);
1947 wined3d_mutex_unlock();
1949 return hr;
1952 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
1953 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT index_count,
1954 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
1955 const void *vertex_data, UINT vertex_stride)
1957 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1958 HRESULT hr;
1960 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1961 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1962 iface, primitive_type, min_vertex_idx, index_count, primitive_count,
1963 index_data, index_format, vertex_data, vertex_stride);
1965 wined3d_mutex_lock();
1966 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1967 hr = wined3d_device_draw_indexed_primitive_up(device->wined3d_device,
1968 vertex_count_from_primitive_count(primitive_type, primitive_count), index_data,
1969 wined3dformat_from_d3dformat(index_format), vertex_data, vertex_stride);
1970 wined3d_mutex_unlock();
1972 return hr;
1975 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
1976 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
1978 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1979 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
1980 HRESULT hr;
1982 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
1983 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
1985 wined3d_mutex_lock();
1986 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
1987 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
1988 wined3d_mutex_unlock();
1990 return hr;
1993 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
1994 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
1996 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1997 struct d3d8_vertex_shader *object;
1998 DWORD shader_handle;
1999 DWORD handle;
2000 HRESULT hr;
2002 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2003 iface, declaration, byte_code, shader, usage);
2005 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2006 if (!object)
2008 ERR("Failed to allocate vertex shader memory.\n");
2009 *shader = 0;
2010 return E_OUTOFMEMORY;
2013 wined3d_mutex_lock();
2014 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2015 wined3d_mutex_unlock();
2016 if (handle == D3D8_INVALID_HANDLE)
2018 ERR("Failed to allocate vertex shader handle.\n");
2019 HeapFree(GetProcessHeap(), 0, object);
2020 *shader = 0;
2021 return E_OUTOFMEMORY;
2024 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2026 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2027 if (FAILED(hr))
2029 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2030 wined3d_mutex_lock();
2031 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2032 wined3d_mutex_unlock();
2033 HeapFree(GetProcessHeap(), 0, object);
2034 *shader = 0;
2035 return hr;
2038 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2039 *shader = shader_handle;
2041 return D3D_OK;
2044 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2046 struct d3d8_vertex_declaration *d3d8_declaration;
2047 struct FvfToDecl *convertedDecls = device->decls;
2048 int p, low, high; /* deliberately signed */
2049 HRESULT hr;
2051 TRACE("Searching for declaration for fvf %08x... ", fvf);
2053 low = 0;
2054 high = device->numConvertedDecls - 1;
2055 while (low <= high)
2057 p = (low + high) >> 1;
2058 TRACE("%d ", p);
2060 if (convertedDecls[p].fvf == fvf)
2062 TRACE("found %p\n", convertedDecls[p].declaration);
2063 return convertedDecls[p].declaration;
2066 if (convertedDecls[p].fvf < fvf)
2067 low = p + 1;
2068 else
2069 high = p - 1;
2071 TRACE("not found. Creating and inserting at position %d.\n", low);
2073 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2075 ERR("Memory allocation failed.\n");
2076 return NULL;
2079 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2081 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2082 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2083 return NULL;
2086 if (device->declArraySize == device->numConvertedDecls)
2088 UINT grow = device->declArraySize / 2;
2090 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2091 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2092 if (!convertedDecls)
2094 d3d8_vertex_declaration_destroy(d3d8_declaration);
2095 return NULL;
2097 device->decls = convertedDecls;
2098 device->declArraySize += grow;
2101 memmove(convertedDecls + low + 1, convertedDecls + low,
2102 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2103 convertedDecls[low].declaration = d3d8_declaration;
2104 convertedDecls[low].fvf = fvf;
2105 ++device->numConvertedDecls;
2107 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2109 return d3d8_declaration;
2112 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2114 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2115 struct d3d8_vertex_shader *shader_impl;
2116 HRESULT hr;
2118 TRACE("iface %p, shader %#x.\n", iface, shader);
2120 if (VS_HIGHESTFIXEDFXF >= shader)
2122 TRACE("Setting FVF, %#x\n", shader);
2124 wined3d_mutex_lock();
2125 wined3d_device_set_vertex_declaration(device->wined3d_device,
2126 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2127 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2128 wined3d_mutex_unlock();
2130 return D3D_OK;
2133 TRACE("Setting shader\n");
2135 wined3d_mutex_lock();
2136 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2138 WARN("Invalid handle (%#x) passed.\n", shader);
2139 wined3d_mutex_unlock();
2141 return D3DERR_INVALIDCALL;
2144 hr = wined3d_device_set_vertex_declaration(device->wined3d_device,
2145 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2146 if (SUCCEEDED(hr))
2147 hr = wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2148 wined3d_mutex_unlock();
2150 TRACE("Returning hr %#x\n", hr);
2152 return hr;
2155 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2157 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2158 struct wined3d_vertex_declaration *wined3d_declaration;
2159 struct d3d8_vertex_declaration *d3d8_declaration;
2160 HRESULT hr;
2162 TRACE("iface %p, shader %p.\n", iface, shader);
2164 wined3d_mutex_lock();
2165 if (FAILED(hr = wined3d_device_get_vertex_declaration(device->wined3d_device, &wined3d_declaration)))
2167 wined3d_mutex_unlock();
2168 WARN("Failed to get wined3d vertex declaration, hr %#x.\n", hr);
2169 return hr;
2172 if (!wined3d_declaration)
2174 wined3d_mutex_unlock();
2175 *shader = 0;
2176 return D3D_OK;
2179 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2180 wined3d_vertex_declaration_decref(wined3d_declaration);
2181 wined3d_mutex_unlock();
2182 *shader = d3d8_declaration->shader_handle;
2184 TRACE("Returning %#x.\n", *shader);
2186 return hr;
2189 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2191 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2192 struct d3d8_vertex_shader *shader_impl;
2193 struct wined3d_shader *cur;
2195 TRACE("iface %p, shader %#x.\n", iface, shader);
2197 wined3d_mutex_lock();
2198 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2200 WARN("Invalid handle (%#x) passed.\n", shader);
2201 wined3d_mutex_unlock();
2203 return D3DERR_INVALIDCALL;
2206 if ((cur = wined3d_device_get_vertex_shader(device->wined3d_device)))
2208 if (cur == shader_impl->wined3d_shader)
2209 IDirect3DDevice8_SetVertexShader(iface, 0);
2210 wined3d_shader_decref(cur);
2213 wined3d_mutex_unlock();
2215 d3d8_vertex_shader_destroy(shader_impl);
2217 return D3D_OK;
2220 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2221 DWORD start_register, const void *data, DWORD count)
2223 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2224 HRESULT hr;
2226 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2227 iface, start_register, data, count);
2229 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2231 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2232 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2233 return D3DERR_INVALIDCALL;
2236 wined3d_mutex_lock();
2237 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2238 wined3d_mutex_unlock();
2240 return hr;
2243 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2244 DWORD start_register, void *data, DWORD count)
2246 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2247 HRESULT hr;
2249 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2250 iface, start_register, data, count);
2252 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2254 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2255 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2256 return D3DERR_INVALIDCALL;
2259 wined3d_mutex_lock();
2260 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2261 wined3d_mutex_unlock();
2263 return hr;
2266 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2267 DWORD shader, void *data, DWORD *data_size)
2269 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2270 struct d3d8_vertex_declaration *declaration;
2271 struct d3d8_vertex_shader *shader_impl;
2273 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2274 iface, shader, data, data_size);
2276 wined3d_mutex_lock();
2277 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2278 wined3d_mutex_unlock();
2280 if (!shader_impl)
2282 WARN("Invalid handle (%#x) passed.\n", shader);
2283 return D3DERR_INVALIDCALL;
2285 declaration = shader_impl->vertex_declaration;
2287 if (!data)
2289 *data_size = declaration->elements_size;
2290 return D3D_OK;
2293 /* MSDN claims that if *data_size is smaller than the required size
2294 * we should write the required size and return D3DERR_MOREDATA.
2295 * That's not actually true. */
2296 if (*data_size < declaration->elements_size)
2297 return D3DERR_INVALIDCALL;
2299 memcpy(data, declaration->elements, declaration->elements_size);
2301 return D3D_OK;
2304 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2305 DWORD shader, void *data, DWORD *data_size)
2307 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2308 struct d3d8_vertex_shader *shader_impl = NULL;
2309 HRESULT hr;
2311 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2312 iface, shader, data, data_size);
2314 wined3d_mutex_lock();
2315 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2317 WARN("Invalid handle (%#x) passed.\n", shader);
2318 wined3d_mutex_unlock();
2320 return D3DERR_INVALIDCALL;
2323 if (!shader_impl->wined3d_shader)
2325 wined3d_mutex_unlock();
2326 *data_size = 0;
2327 return D3D_OK;
2330 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2331 wined3d_mutex_unlock();
2333 return hr;
2336 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2337 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2339 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2340 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2341 HRESULT hr;
2343 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2345 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2346 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2347 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2348 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2349 * problem)
2351 wined3d_mutex_lock();
2352 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2353 hr = wined3d_device_set_index_buffer(device->wined3d_device,
2354 ib ? ib->wined3d_buffer : NULL,
2355 ib ? ib->format : WINED3DFMT_UNKNOWN);
2356 wined3d_mutex_unlock();
2358 return hr;
2361 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2362 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2364 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2365 struct wined3d_buffer *wined3d_buffer = NULL;
2366 struct d3d8_indexbuffer *buffer_impl;
2367 HRESULT hr;
2369 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2371 if (!buffer)
2372 return D3DERR_INVALIDCALL;
2374 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2375 wined3d_mutex_lock();
2376 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2377 hr = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_buffer);
2378 if (SUCCEEDED(hr) && wined3d_buffer)
2380 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2381 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2382 IDirect3DIndexBuffer8_AddRef(*buffer);
2383 wined3d_buffer_decref(wined3d_buffer);
2385 else
2387 if (FAILED(hr))
2388 ERR("Failed to get wined3d index buffer, hr %#x.\n", hr);
2389 *buffer = NULL;
2391 wined3d_mutex_unlock();
2393 return hr;
2396 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2397 const DWORD *byte_code, DWORD *shader)
2399 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2400 struct d3d8_pixel_shader *object;
2401 DWORD shader_handle;
2402 DWORD handle;
2403 HRESULT hr;
2405 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2407 if (!shader)
2408 return D3DERR_INVALIDCALL;
2410 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2411 if (!object)
2413 ERR("Failed to allocate pixel shader memmory.\n");
2414 return E_OUTOFMEMORY;
2417 wined3d_mutex_lock();
2418 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2419 wined3d_mutex_unlock();
2420 if (handle == D3D8_INVALID_HANDLE)
2422 ERR("Failed to allocate pixel shader handle.\n");
2423 HeapFree(GetProcessHeap(), 0, object);
2424 return E_OUTOFMEMORY;
2427 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2429 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2430 if (FAILED(hr))
2432 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2433 wined3d_mutex_lock();
2434 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2435 wined3d_mutex_unlock();
2436 HeapFree(GetProcessHeap(), 0, object);
2437 *shader = 0;
2438 return hr;
2441 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2442 *shader = shader_handle;
2444 return D3D_OK;
2447 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2449 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2450 struct d3d8_pixel_shader *shader_impl;
2451 HRESULT hr;
2453 TRACE("iface %p, shader %#x.\n", iface, shader);
2455 wined3d_mutex_lock();
2457 if (!shader)
2459 hr = wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2460 wined3d_mutex_unlock();
2461 return hr;
2464 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2466 WARN("Invalid handle (%#x) passed.\n", shader);
2467 wined3d_mutex_unlock();
2468 return D3DERR_INVALIDCALL;
2471 TRACE("Setting shader %p.\n", shader_impl);
2472 hr = wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2473 wined3d_mutex_unlock();
2475 return hr;
2478 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2480 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2481 struct wined3d_shader *object;
2483 TRACE("iface %p, shader %p.\n", iface, shader);
2485 if (!shader)
2486 return D3DERR_INVALIDCALL;
2488 wined3d_mutex_lock();
2489 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2491 struct d3d8_pixel_shader *d3d8_shader;
2492 d3d8_shader = wined3d_shader_get_parent(object);
2493 wined3d_shader_decref(object);
2494 *shader = d3d8_shader->handle;
2496 else
2498 *shader = 0;
2500 wined3d_mutex_unlock();
2502 TRACE("Returning %#x.\n", *shader);
2504 return D3D_OK;
2507 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2509 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2510 struct d3d8_pixel_shader *shader_impl;
2511 struct wined3d_shader *cur;
2513 TRACE("iface %p, shader %#x.\n", iface, shader);
2515 wined3d_mutex_lock();
2517 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2519 WARN("Invalid handle (%#x) passed.\n", shader);
2520 wined3d_mutex_unlock();
2521 return D3DERR_INVALIDCALL;
2524 if ((cur = wined3d_device_get_pixel_shader(device->wined3d_device)))
2526 if (cur == shader_impl->wined3d_shader)
2527 IDirect3DDevice8_SetPixelShader(iface, 0);
2528 wined3d_shader_decref(cur);
2531 wined3d_mutex_unlock();
2533 d3d8_pixel_shader_destroy(shader_impl);
2535 return D3D_OK;
2538 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2539 DWORD start_register, const void *data, DWORD count)
2541 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2542 HRESULT hr;
2544 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2545 iface, start_register, data, count);
2547 wined3d_mutex_lock();
2548 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2549 wined3d_mutex_unlock();
2551 return hr;
2554 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2555 DWORD start_register, void *data, DWORD count)
2557 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2558 HRESULT hr;
2560 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2561 iface, start_register, data, count);
2563 wined3d_mutex_lock();
2564 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2565 wined3d_mutex_unlock();
2567 return hr;
2570 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2571 DWORD shader, void *data, DWORD *data_size)
2573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2574 struct d3d8_pixel_shader *shader_impl = NULL;
2575 HRESULT hr;
2577 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2578 iface, shader, data, data_size);
2580 wined3d_mutex_lock();
2581 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2583 WARN("Invalid handle (%#x) passed.\n", shader);
2584 wined3d_mutex_unlock();
2586 return D3DERR_INVALIDCALL;
2589 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2590 wined3d_mutex_unlock();
2592 return hr;
2595 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2596 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2598 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2599 HRESULT hr;
2601 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2602 iface, handle, segment_count, patch_info);
2604 wined3d_mutex_lock();
2605 hr = wined3d_device_draw_rect_patch(device->wined3d_device, handle,
2606 segment_count, (const struct wined3d_rect_patch_info *)patch_info);
2607 wined3d_mutex_unlock();
2609 return hr;
2612 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2613 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2615 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2616 HRESULT hr;
2618 TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2619 iface, handle, segment_count, patch_info);
2621 wined3d_mutex_lock();
2622 hr = wined3d_device_draw_tri_patch(device->wined3d_device, handle,
2623 segment_count, (const struct wined3d_tri_patch_info *)patch_info);
2624 wined3d_mutex_unlock();
2626 return hr;
2629 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2631 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2632 HRESULT hr;
2634 TRACE("iface %p, handle %#x.\n", iface, handle);
2636 wined3d_mutex_lock();
2637 hr = wined3d_device_delete_patch(device->wined3d_device, handle);
2638 wined3d_mutex_unlock();
2640 return hr;
2643 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2644 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2646 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2647 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2648 HRESULT hr;
2650 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2651 iface, stream_idx, buffer, stride);
2653 wined3d_mutex_lock();
2654 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2655 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2656 wined3d_mutex_unlock();
2658 return hr;
2661 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2662 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2664 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2665 struct d3d8_vertexbuffer *buffer_impl;
2666 struct wined3d_buffer *wined3d_buffer = NULL;
2667 HRESULT hr;
2669 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2670 iface, stream_idx, buffer, stride);
2672 if (!buffer)
2673 return D3DERR_INVALIDCALL;
2675 wined3d_mutex_lock();
2676 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2677 if (SUCCEEDED(hr) && wined3d_buffer)
2679 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2680 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2681 IDirect3DVertexBuffer8_AddRef(*buffer);
2682 wined3d_buffer_decref(wined3d_buffer);
2684 else
2686 if (FAILED(hr))
2687 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2688 *buffer = NULL;
2690 wined3d_mutex_unlock();
2692 return hr;
2695 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2697 d3d8_device_QueryInterface,
2698 d3d8_device_AddRef,
2699 d3d8_device_Release,
2700 d3d8_device_TestCooperativeLevel,
2701 d3d8_device_GetAvailableTextureMem,
2702 d3d8_device_ResourceManagerDiscardBytes,
2703 d3d8_device_GetDirect3D,
2704 d3d8_device_GetDeviceCaps,
2705 d3d8_device_GetDisplayMode,
2706 d3d8_device_GetCreationParameters,
2707 d3d8_device_SetCursorProperties,
2708 d3d8_device_SetCursorPosition,
2709 d3d8_device_ShowCursor,
2710 d3d8_device_CreateAdditionalSwapChain,
2711 d3d8_device_Reset,
2712 d3d8_device_Present,
2713 d3d8_device_GetBackBuffer,
2714 d3d8_device_GetRasterStatus,
2715 d3d8_device_SetGammaRamp,
2716 d3d8_device_GetGammaRamp,
2717 d3d8_device_CreateTexture,
2718 d3d8_device_CreateVolumeTexture,
2719 d3d8_device_CreateCubeTexture,
2720 d3d8_device_CreateVertexBuffer,
2721 d3d8_device_CreateIndexBuffer,
2722 d3d8_device_CreateRenderTarget,
2723 d3d8_device_CreateDepthStencilSurface,
2724 d3d8_device_CreateImageSurface,
2725 d3d8_device_CopyRects,
2726 d3d8_device_UpdateTexture,
2727 d3d8_device_GetFrontBuffer,
2728 d3d8_device_SetRenderTarget,
2729 d3d8_device_GetRenderTarget,
2730 d3d8_device_GetDepthStencilSurface,
2731 d3d8_device_BeginScene,
2732 d3d8_device_EndScene,
2733 d3d8_device_Clear,
2734 d3d8_device_SetTransform,
2735 d3d8_device_GetTransform,
2736 d3d8_device_MultiplyTransform,
2737 d3d8_device_SetViewport,
2738 d3d8_device_GetViewport,
2739 d3d8_device_SetMaterial,
2740 d3d8_device_GetMaterial,
2741 d3d8_device_SetLight,
2742 d3d8_device_GetLight,
2743 d3d8_device_LightEnable,
2744 d3d8_device_GetLightEnable,
2745 d3d8_device_SetClipPlane,
2746 d3d8_device_GetClipPlane,
2747 d3d8_device_SetRenderState,
2748 d3d8_device_GetRenderState,
2749 d3d8_device_BeginStateBlock,
2750 d3d8_device_EndStateBlock,
2751 d3d8_device_ApplyStateBlock,
2752 d3d8_device_CaptureStateBlock,
2753 d3d8_device_DeleteStateBlock,
2754 d3d8_device_CreateStateBlock,
2755 d3d8_device_SetClipStatus,
2756 d3d8_device_GetClipStatus,
2757 d3d8_device_GetTexture,
2758 d3d8_device_SetTexture,
2759 d3d8_device_GetTextureStageState,
2760 d3d8_device_SetTextureStageState,
2761 d3d8_device_ValidateDevice,
2762 d3d8_device_GetInfo,
2763 d3d8_device_SetPaletteEntries,
2764 d3d8_device_GetPaletteEntries,
2765 d3d8_device_SetCurrentTexturePalette,
2766 d3d8_device_GetCurrentTexturePalette,
2767 d3d8_device_DrawPrimitive,
2768 d3d8_device_DrawIndexedPrimitive,
2769 d3d8_device_DrawPrimitiveUP,
2770 d3d8_device_DrawIndexedPrimitiveUP,
2771 d3d8_device_ProcessVertices,
2772 d3d8_device_CreateVertexShader,
2773 d3d8_device_SetVertexShader,
2774 d3d8_device_GetVertexShader,
2775 d3d8_device_DeleteVertexShader,
2776 d3d8_device_SetVertexShaderConstant,
2777 d3d8_device_GetVertexShaderConstant,
2778 d3d8_device_GetVertexShaderDeclaration,
2779 d3d8_device_GetVertexShaderFunction,
2780 d3d8_device_SetStreamSource,
2781 d3d8_device_GetStreamSource,
2782 d3d8_device_SetIndices,
2783 d3d8_device_GetIndices,
2784 d3d8_device_CreatePixelShader,
2785 d3d8_device_SetPixelShader,
2786 d3d8_device_GetPixelShader,
2787 d3d8_device_DeletePixelShader,
2788 d3d8_device_SetPixelShaderConstant,
2789 d3d8_device_GetPixelShaderConstant,
2790 d3d8_device_GetPixelShaderFunction,
2791 d3d8_device_DrawRectPatch,
2792 d3d8_device_DrawTriPatch,
2793 d3d8_device_DeletePatch,
2796 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2798 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2801 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2802 struct wined3d_device *device)
2804 TRACE("device_parent %p, device %p\n", device_parent, device);
2807 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2809 TRACE("device_parent %p.\n", device_parent);
2812 static HRESULT CDECL device_parent_create_surface(struct wined3d_device_parent *device_parent,
2813 void *container_parent, UINT width, UINT height, enum wined3d_format_id format, DWORD usage,
2814 enum wined3d_pool pool, UINT level, enum wined3d_cubemap_face face, struct wined3d_surface **surface)
2816 struct d3d8_device *device = device_from_device_parent(device_parent);
2817 struct d3d8_surface *d3d_surface;
2818 BOOL lockable = TRUE;
2819 HRESULT hr;
2821 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, usage %#x,\n"
2822 "\tpool %#x, level %u, face %u, surface %p.\n",
2823 device_parent, container_parent, width, height, format, usage, pool, level, face, surface);
2826 if (pool == WINED3D_POOL_DEFAULT && !(usage & WINED3DUSAGE_DYNAMIC))
2827 lockable = FALSE;
2829 hr = d3d8_device_CreateSurface(device, width, height,
2830 d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2831 (IDirect3DSurface8 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2832 if (FAILED(hr))
2834 WARN("Failed to create surface, hr %#x.\n", hr);
2835 return hr;
2838 *surface = d3d_surface->wined3d_surface;
2839 wined3d_surface_incref(*surface);
2841 d3d_surface->container = container_parent;
2842 IUnknown_Release(d3d_surface->parent_device);
2843 d3d_surface->parent_device = NULL;
2845 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2846 d3d_surface->forwardReference = container_parent;
2848 return hr;
2851 static HRESULT CDECL device_parent_create_rendertarget(struct wined3d_device_parent *device_parent,
2852 void *container_parent, UINT width, UINT height, enum wined3d_format_id format,
2853 enum wined3d_multisample_type multisample_type, DWORD multisample_quality, BOOL lockable,
2854 struct wined3d_surface **surface)
2856 struct d3d8_device *device = device_from_device_parent(device_parent);
2857 struct d3d8_surface *d3d_surface;
2858 HRESULT hr;
2860 TRACE("device_parent %p, container_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2861 "\tmultisample_quality %u, lockable %u, surface %p.\n",
2862 device_parent, container_parent, width, height, format,
2863 multisample_type, multisample_quality, lockable, surface);
2865 hr = IDirect3DDevice8_CreateRenderTarget(&device->IDirect3DDevice8_iface, width, height,
2866 d3dformat_from_wined3dformat(format), multisample_type, lockable, (IDirect3DSurface8 **)&d3d_surface);
2867 if (FAILED(hr))
2869 WARN("Failed to create rendertarget, hr %#x.\n", hr);
2870 return hr;
2873 *surface = d3d_surface->wined3d_surface;
2874 wined3d_surface_incref(*surface);
2876 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice8_iface;
2877 /* Implicit surfaces are created with an refcount of 0 */
2878 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2880 return hr;
2883 static HRESULT CDECL device_parent_create_depth_stencil(struct wined3d_device_parent *device_parent,
2884 UINT width, UINT height, enum wined3d_format_id format, enum wined3d_multisample_type multisample_type,
2885 DWORD multisample_quality, BOOL discard, struct wined3d_surface **surface)
2887 struct d3d8_device *device = device_from_device_parent(device_parent);
2888 struct d3d8_surface *d3d_surface;
2889 HRESULT hr;
2891 TRACE("device_parent %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2892 "\tmultisample_quality %u, discard %u, surface %p.\n",
2893 device_parent, width, height, format, multisample_type, multisample_quality, discard, surface);
2895 hr = IDirect3DDevice8_CreateDepthStencilSurface(&device->IDirect3DDevice8_iface, width, height,
2896 d3dformat_from_wined3dformat(format), multisample_type, (IDirect3DSurface8 **)&d3d_surface);
2897 if (FAILED(hr))
2899 WARN("Failed to create depth/stencil surface, hr %#x.\n", hr);
2900 return hr;
2903 *surface = d3d_surface->wined3d_surface;
2904 wined3d_surface_incref(*surface);
2906 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice8_iface;
2907 /* Implicit surfaces are created with an refcount of 0 */
2908 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2910 return hr;
2913 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
2914 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2915 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
2917 struct d3d8_device *device = device_from_device_parent(device_parent);
2918 struct d3d8_volume *object;
2919 HRESULT hr;
2921 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
2922 "format %#x, pool %#x, usage %#x, volume %p.\n",
2923 device_parent, container_parent, width, height, depth,
2924 format, pool, usage, volume);
2926 /* Allocate the storage for the device */
2927 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2928 if (!object)
2930 FIXME("Allocation of memory failed\n");
2931 *volume = NULL;
2932 return D3DERR_OUTOFVIDEOMEMORY;
2935 hr = volume_init(object, device, width, height, depth, usage, format, pool);
2936 if (FAILED(hr))
2938 WARN("Failed to initialize volume, hr %#x.\n", hr);
2939 HeapFree(GetProcessHeap(), 0, object);
2940 return hr;
2943 *volume = object->wined3d_volume;
2944 wined3d_volume_incref(*volume);
2945 IDirect3DVolume8_Release(&object->IDirect3DVolume8_iface);
2947 object->container = container_parent;
2948 object->forwardReference = container_parent;
2950 TRACE("Created volume %p.\n", object);
2952 return hr;
2955 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
2956 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
2958 struct d3d8_device *device = device_from_device_parent(device_parent);
2959 struct d3d8_swapchain *d3d_swapchain;
2960 HRESULT hr;
2962 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
2964 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
2966 WARN("Failed to create swapchain, hr %#x.\n", hr);
2967 *swapchain = NULL;
2968 return hr;
2971 *swapchain = d3d_swapchain->wined3d_swapchain;
2972 wined3d_swapchain_incref(*swapchain);
2973 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
2975 return hr;
2978 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
2980 device_parent_wined3d_device_created,
2981 device_parent_mode_changed,
2982 device_parent_create_surface,
2983 device_parent_create_rendertarget,
2984 device_parent_create_depth_stencil,
2985 device_parent_create_volume,
2986 device_parent_create_swapchain,
2989 static void setup_fpu(void)
2991 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
2992 WORD cw;
2993 __asm__ volatile ("fnstcw %0" : "=m" (cw));
2994 cw = (cw & ~0xf3f) | 0x3f;
2995 __asm__ volatile ("fldcw %0" : : "m" (cw));
2996 #elif defined(__i386__) && defined(_MSC_VER)
2997 WORD cw;
2998 __asm fnstcw cw;
2999 cw = (cw & ~0xf3f) | 0x3f;
3000 __asm fldcw cw;
3001 #else
3002 FIXME("FPU setup not implemented for this platform.\n");
3003 #endif
3006 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3007 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3009 struct wined3d_swapchain_desc swapchain_desc;
3010 HRESULT hr;
3012 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3013 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3014 device->ref = 1;
3015 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3016 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3017 if (!device->handle_table.entries)
3019 ERR("Failed to allocate handle table memory.\n");
3020 return E_OUTOFMEMORY;
3022 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3024 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3026 wined3d_mutex_lock();
3027 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3028 &device->device_parent, &device->wined3d_device);
3029 if (FAILED(hr))
3031 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3032 wined3d_mutex_unlock();
3033 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3034 return hr;
3037 if (!parameters->Windowed)
3039 HWND device_window = parameters->hDeviceWindow;
3041 if (!focus_window)
3042 focus_window = device_window;
3043 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3045 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3046 wined3d_device_decref(device->wined3d_device);
3047 wined3d_mutex_unlock();
3048 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3049 return hr;
3052 if (!device_window)
3053 device_window = focus_window;
3054 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3055 parameters->BackBufferWidth,
3056 parameters->BackBufferHeight);
3059 if (flags & D3DCREATE_MULTITHREADED)
3060 wined3d_device_set_multithreaded(device->wined3d_device);
3062 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3064 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3065 if (FAILED(hr))
3067 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3068 wined3d_device_release_focus_window(device->wined3d_device);
3069 wined3d_device_decref(device->wined3d_device);
3070 wined3d_mutex_unlock();
3071 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3072 return hr;
3075 hr = wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3076 wined3d_mutex_unlock();
3077 if (FAILED(hr))
3079 ERR("Failed to set minimum pointsize, hr %#x.\n", hr);
3080 goto err;
3083 parameters->BackBufferWidth = swapchain_desc.backbuffer_width;
3084 parameters->BackBufferHeight = swapchain_desc.backbuffer_height;
3085 parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc.backbuffer_format);
3086 parameters->BackBufferCount = swapchain_desc.backbuffer_count;
3087 parameters->MultiSampleType = swapchain_desc.multisample_type;
3088 parameters->SwapEffect = swapchain_desc.swap_effect;
3089 parameters->hDeviceWindow = swapchain_desc.device_window;
3090 parameters->Windowed = swapchain_desc.windowed;
3091 parameters->EnableAutoDepthStencil = swapchain_desc.enable_auto_depth_stencil;
3092 parameters->AutoDepthStencilFormat = d3dformat_from_wined3dformat(swapchain_desc.auto_depth_stencil_format);
3093 parameters->Flags = swapchain_desc.flags;
3094 parameters->FullScreen_RefreshRateInHz = swapchain_desc.refresh_rate;
3095 parameters->FullScreen_PresentationInterval = swapchain_desc.swap_interval;
3097 device->declArraySize = 16;
3098 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3099 if (!device->decls)
3101 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3102 hr = E_OUTOFMEMORY;
3103 goto err;
3106 device->d3d_parent = &parent->IDirect3D8_iface;
3107 IDirect3D8_AddRef(device->d3d_parent);
3109 return D3D_OK;
3111 err:
3112 wined3d_mutex_lock();
3113 wined3d_device_uninit_3d(device->wined3d_device);
3114 wined3d_device_release_focus_window(device->wined3d_device);
3115 wined3d_device_decref(device->wined3d_device);
3116 wined3d_mutex_unlock();
3117 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3118 return hr;