wined3d: Don't enumerate sub-resources in wined3d_device_reset().
[wine.git] / dlls / d3d8 / device.c
blob36a2b12bb1f704069ba3ae3370a7ca138079a088
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 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "wine/debug.h"
33 #include "d3d8_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
37 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
39 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
41 d3d8_null_wined3d_object_destroyed,
44 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
46 BYTE *c = (BYTE *)&format;
48 /* Don't translate FOURCC formats */
49 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
51 switch(format)
53 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
54 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
55 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
56 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
57 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
58 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
59 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
60 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
61 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
62 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
63 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
64 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
65 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
66 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
67 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
68 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
69 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
70 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
71 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
72 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
73 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
74 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
75 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
76 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
77 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
78 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
79 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
80 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
81 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
82 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
83 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
84 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
85 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
86 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
87 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
88 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
89 default:
90 FIXME("Unhandled wined3d format %#x.\n", format);
91 return D3DFMT_UNKNOWN;
95 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
97 BYTE *c = (BYTE *)&format;
99 /* Don't translate FOURCC formats */
100 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
102 switch(format)
104 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
105 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
106 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
107 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
108 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
109 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
110 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
111 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
112 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
113 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
114 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
115 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
116 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
117 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
119 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
120 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
121 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
122 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
123 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
124 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
125 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
126 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
127 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
128 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
129 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
130 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
131 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
132 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
133 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
134 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
135 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
136 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
137 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
138 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
139 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
140 default:
141 FIXME("Unhandled D3DFORMAT %#x\n", format);
142 return WINED3DFMT_UNKNOWN;
146 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
148 switch(primitive_type)
150 case D3DPT_POINTLIST:
151 return primitive_count;
153 case D3DPT_LINELIST:
154 return primitive_count * 2;
156 case D3DPT_LINESTRIP:
157 return primitive_count + 1;
159 case D3DPT_TRIANGLELIST:
160 return primitive_count * 3;
162 case D3DPT_TRIANGLESTRIP:
163 case D3DPT_TRIANGLEFAN:
164 return primitive_count + 2;
166 default:
167 FIXME("Unhandled primitive type %#x\n", primitive_type);
168 return 0;
172 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
173 const struct wined3d_swapchain_desc *swapchain_desc)
175 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
176 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
177 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
178 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
179 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
180 present_parameters->SwapEffect = swapchain_desc->swap_effect;
181 present_parameters->hDeviceWindow = swapchain_desc->device_window;
182 present_parameters->Windowed = swapchain_desc->windowed;
183 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
184 present_parameters->AutoDepthStencilFormat
185 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
186 present_parameters->Flags = swapchain_desc->flags;
187 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
188 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
191 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
192 const D3DPRESENT_PARAMETERS *present_parameters)
194 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
196 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
197 return FALSE;
199 if (present_parameters->BackBufferCount > 3
200 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
201 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
202 && present_parameters->BackBufferCount > 1))
204 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
205 return FALSE;
208 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
209 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
210 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
211 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
212 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
213 swapchain_desc->multisample_quality = 0; /* d3d9 only */
214 swapchain_desc->swap_effect = present_parameters->SwapEffect;
215 swapchain_desc->device_window = present_parameters->hDeviceWindow;
216 swapchain_desc->windowed = present_parameters->Windowed;
217 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
218 swapchain_desc->auto_depth_stencil_format
219 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
220 swapchain_desc->flags = present_parameters->Flags;
221 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
222 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
223 swapchain_desc->auto_restore_display_mode = TRUE;
225 return TRUE;
228 /* Handle table functions */
229 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
231 struct d3d8_handle_entry *entry;
233 if (t->free_entries)
235 DWORD index = t->free_entries - t->entries;
236 /* Use a free handle */
237 entry = t->free_entries;
238 if (entry->type != D3D8_HANDLE_FREE)
240 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
241 return D3D8_INVALID_HANDLE;
243 t->free_entries = entry->object;
244 entry->object = object;
245 entry->type = type;
247 return index;
250 if (!(t->entry_count < t->table_size))
252 /* Grow the table */
253 UINT new_size = t->table_size + (t->table_size >> 1);
254 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
255 0, t->entries, new_size * sizeof(*t->entries));
256 if (!new_entries)
258 ERR("Failed to grow the handle table.\n");
259 return D3D8_INVALID_HANDLE;
261 t->entries = new_entries;
262 t->table_size = new_size;
265 entry = &t->entries[t->entry_count];
266 entry->object = object;
267 entry->type = type;
269 return t->entry_count++;
272 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
274 struct d3d8_handle_entry *entry;
275 void *object;
277 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
279 WARN("Invalid handle %u passed.\n", handle);
280 return NULL;
283 entry = &t->entries[handle];
284 if (entry->type != type)
286 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
287 return NULL;
290 object = entry->object;
291 entry->object = t->free_entries;
292 entry->type = D3D8_HANDLE_FREE;
293 t->free_entries = entry;
295 return object;
298 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
300 struct d3d8_handle_entry *entry;
302 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
304 WARN("Invalid handle %u passed.\n", handle);
305 return NULL;
308 entry = &t->entries[handle];
309 if (entry->type != type)
311 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
312 return NULL;
315 return entry->object;
318 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
320 TRACE("iface %p, riid %s, out %p.\n",
321 iface, debugstr_guid(riid), out);
323 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
324 || IsEqualGUID(riid, &IID_IUnknown))
326 IDirect3DDevice8_AddRef(iface);
327 *out = iface;
328 return S_OK;
331 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
333 *out = NULL;
334 return E_NOINTERFACE;
337 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
339 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
340 ULONG ref = InterlockedIncrement(&device->ref);
342 TRACE("%p increasing refcount to %u.\n", iface, ref);
344 return ref;
347 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
350 ULONG ref;
352 if (device->inDestruction)
353 return 0;
355 ref = InterlockedDecrement(&device->ref);
357 TRACE("%p decreasing refcount to %u.\n", iface, ref);
359 if (!ref)
361 IDirect3D8 *parent = device->d3d_parent;
362 unsigned i;
364 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
366 wined3d_mutex_lock();
368 device->inDestruction = TRUE;
370 for (i = 0; i < device->numConvertedDecls; ++i)
372 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
374 HeapFree(GetProcessHeap(), 0, device->decls);
376 if (device->vertex_buffer)
377 wined3d_buffer_decref(device->vertex_buffer);
378 if (device->index_buffer)
379 wined3d_buffer_decref(device->index_buffer);
381 wined3d_device_uninit_3d(device->wined3d_device);
382 wined3d_device_release_focus_window(device->wined3d_device);
383 wined3d_device_decref(device->wined3d_device);
384 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
385 HeapFree(GetProcessHeap(), 0, device);
387 wined3d_mutex_unlock();
389 IDirect3D8_Release(parent);
391 return ref;
394 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
396 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
398 TRACE("iface %p.\n", iface);
400 TRACE("device state: %#x.\n", device->device_state);
402 switch (device->device_state)
404 default:
405 case D3D8_DEVICE_STATE_OK:
406 return D3D_OK;
407 case D3D8_DEVICE_STATE_LOST:
408 return D3DERR_DEVICELOST;
409 case D3D8_DEVICE_STATE_NOT_RESET:
410 return D3DERR_DEVICENOTRESET;
414 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
417 UINT ret;
419 TRACE("iface %p.\n", iface);
421 wined3d_mutex_lock();
422 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
423 wined3d_mutex_unlock();
425 return ret;
428 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
430 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
432 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
434 if (byte_count)
435 FIXME("Byte count ignored.\n");
437 wined3d_mutex_lock();
438 wined3d_device_evict_managed_resources(device->wined3d_device);
439 wined3d_mutex_unlock();
441 return D3D_OK;
444 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
446 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
448 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
450 if (!d3d8)
451 return D3DERR_INVALIDCALL;
453 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
456 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
459 WINED3DCAPS *wined3d_caps;
460 HRESULT hr;
462 TRACE("iface %p, caps %p.\n", iface, caps);
464 if (!caps)
465 return D3DERR_INVALIDCALL;
467 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
468 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
470 wined3d_mutex_lock();
471 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
472 wined3d_mutex_unlock();
474 fixup_caps(wined3d_caps);
475 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
476 HeapFree(GetProcessHeap(), 0, wined3d_caps);
478 return hr;
481 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
483 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
484 struct wined3d_display_mode wined3d_mode;
485 HRESULT hr;
487 TRACE("iface %p, mode %p.\n", iface, mode);
489 wined3d_mutex_lock();
490 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
491 wined3d_mutex_unlock();
493 if (SUCCEEDED(hr))
495 mode->Width = wined3d_mode.width;
496 mode->Height = wined3d_mode.height;
497 mode->RefreshRate = wined3d_mode.refresh_rate;
498 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
501 return hr;
504 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
505 D3DDEVICE_CREATION_PARAMETERS *parameters)
507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
509 TRACE("iface %p, parameters %p.\n", iface, parameters);
511 wined3d_mutex_lock();
512 wined3d_device_get_creation_parameters(device->wined3d_device,
513 (struct wined3d_device_creation_parameters *)parameters);
514 wined3d_mutex_unlock();
516 return D3D_OK;
519 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
520 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
523 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
524 HRESULT hr;
526 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
527 iface, hotspot_x, hotspot_y, bitmap);
529 if (!bitmap)
531 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
532 return D3DERR_INVALIDCALL;
535 wined3d_mutex_lock();
536 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
537 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
538 wined3d_mutex_unlock();
540 return hr;
543 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
545 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
547 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
549 wined3d_mutex_lock();
550 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
551 wined3d_mutex_unlock();
554 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
556 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
557 BOOL ret;
559 TRACE("iface %p, show %#x.\n", iface, show);
561 wined3d_mutex_lock();
562 ret = wined3d_device_show_cursor(device->wined3d_device, show);
563 wined3d_mutex_unlock();
565 return ret;
568 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
569 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
571 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
572 struct wined3d_swapchain_desc desc;
573 struct d3d8_swapchain *object;
574 UINT i, count;
575 HRESULT hr;
577 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
578 iface, present_parameters, swapchain);
580 if (!present_parameters->Windowed)
582 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
583 return D3DERR_INVALIDCALL;
586 wined3d_mutex_lock();
587 count = wined3d_device_get_swapchain_count(device->wined3d_device);
588 for (i = 0; i < count; ++i)
590 struct wined3d_swapchain *wined3d_swapchain;
592 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
593 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
595 if (!desc.windowed)
597 wined3d_mutex_unlock();
598 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
599 return D3DERR_INVALIDCALL;
602 wined3d_mutex_unlock();
604 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
605 return D3DERR_INVALIDCALL;
606 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
607 *swapchain = &object->IDirect3DSwapChain8_iface;
608 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
610 return hr;
613 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
615 struct wined3d_resource_desc desc;
616 IDirect3DBaseTexture8 *texture;
617 struct d3d8_surface *surface;
618 IUnknown *parent;
620 wined3d_resource_get_desc(resource, &desc);
621 if (desc.pool != WINED3D_POOL_DEFAULT)
622 return D3D_OK;
624 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
626 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
627 return D3DERR_DEVICELOST;
630 parent = wined3d_resource_get_parent(resource);
631 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
633 IDirect3DBaseTexture8_Release(texture);
634 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
635 return D3DERR_DEVICELOST;
638 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
639 if (!surface->resource.refcount)
640 return D3D_OK;
642 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
643 return D3DERR_DEVICELOST;
646 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
647 D3DPRESENT_PARAMETERS *present_parameters)
649 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
650 struct wined3d_swapchain_desc swapchain_desc;
651 HRESULT hr;
653 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
655 if (device->device_state == D3D8_DEVICE_STATE_LOST)
657 WARN("App not active, returning D3DERR_DEVICELOST.\n");
658 return D3DERR_DEVICELOST;
660 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
661 return D3DERR_INVALIDCALL;
663 wined3d_mutex_lock();
665 if (device->vertex_buffer)
667 wined3d_buffer_decref(device->vertex_buffer);
668 device->vertex_buffer = NULL;
669 device->vertex_buffer_size = 0;
671 if (device->index_buffer)
673 wined3d_buffer_decref(device->index_buffer);
674 device->index_buffer = NULL;
675 device->index_buffer_size = 0;
678 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
679 NULL, reset_enum_callback, TRUE)))
681 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
682 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
683 device->device_state = D3D8_DEVICE_STATE_OK;
685 else
687 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
689 wined3d_mutex_unlock();
691 return hr;
694 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
695 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
697 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
699 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
700 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
702 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
703 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
704 * shows a framerate on Windows in applications that only call the device
705 * method, like e.g. the dx8 sdk samples. The conclusion is that native
706 * calls the swapchain's public method from the device. */
707 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
708 src_rect, dst_rect, dst_window_override, dirty_region);
711 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
712 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
714 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
715 struct wined3d_swapchain *wined3d_swapchain;
716 struct wined3d_texture *wined3d_texture;
717 struct d3d8_surface *surface_impl;
719 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
720 iface, backbuffer_idx, backbuffer_type, backbuffer);
722 /* backbuffer_type is ignored by native. */
724 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
725 wined3d_mutex_lock();
727 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
728 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
730 wined3d_mutex_unlock();
731 *backbuffer = NULL;
732 return D3DERR_INVALIDCALL;
735 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
736 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
737 IDirect3DSurface8_AddRef(*backbuffer);
739 wined3d_mutex_unlock();
740 return D3D_OK;
743 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
745 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
746 HRESULT hr;
748 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
750 wined3d_mutex_lock();
751 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
752 wined3d_mutex_unlock();
754 return hr;
757 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
759 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
761 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
763 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
764 wined3d_mutex_lock();
765 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
766 wined3d_mutex_unlock();
769 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
771 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
773 TRACE("iface %p, ramp %p.\n", iface, ramp);
775 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
776 wined3d_mutex_lock();
777 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
778 wined3d_mutex_unlock();
781 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
782 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
783 D3DPOOL pool, IDirect3DTexture8 **texture)
785 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
786 struct d3d8_texture *object;
787 HRESULT hr;
789 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
790 iface, width, height, levels, usage, format, pool, texture);
792 *texture = NULL;
793 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
794 if (!object)
795 return D3DERR_OUTOFVIDEOMEMORY;
797 hr = texture_init(object, device, width, height, levels, usage, format, pool);
798 if (FAILED(hr))
800 WARN("Failed to initialize texture, hr %#x.\n", hr);
801 HeapFree(GetProcessHeap(), 0, object);
802 return hr;
805 TRACE("Created texture %p.\n", object);
806 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
808 return D3D_OK;
811 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
812 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
813 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
815 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
816 struct d3d8_texture *object;
817 HRESULT hr;
819 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
820 iface, width, height, depth, levels, usage, format, pool, texture);
822 *texture = NULL;
823 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
824 if (!object)
825 return D3DERR_OUTOFVIDEOMEMORY;
827 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
828 if (FAILED(hr))
830 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
831 HeapFree(GetProcessHeap(), 0, object);
832 return hr;
835 TRACE("Created volume texture %p.\n", object);
836 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
838 return D3D_OK;
841 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
842 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
844 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
845 struct d3d8_texture *object;
846 HRESULT hr;
848 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
849 iface, edge_length, levels, usage, format, pool, texture);
851 *texture = NULL;
852 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
853 if (!object)
854 return D3DERR_OUTOFVIDEOMEMORY;
856 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
857 if (FAILED(hr))
859 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
860 HeapFree(GetProcessHeap(), 0, object);
861 return hr;
864 TRACE("Created cube texture %p.\n", object);
865 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
867 return hr;
870 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
871 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
873 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
874 struct d3d8_vertexbuffer *object;
875 HRESULT hr;
877 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
878 iface, size, usage, fvf, pool, buffer);
880 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
881 if (!object)
882 return D3DERR_OUTOFVIDEOMEMORY;
884 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
885 if (FAILED(hr))
887 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
888 HeapFree(GetProcessHeap(), 0, object);
889 return hr;
892 TRACE("Created vertex buffer %p.\n", object);
893 *buffer = &object->IDirect3DVertexBuffer8_iface;
895 return D3D_OK;
898 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
899 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
901 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
902 struct d3d8_indexbuffer *object;
903 HRESULT hr;
905 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
906 iface, size, usage, format, pool, buffer);
908 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
909 if (!object)
910 return D3DERR_OUTOFVIDEOMEMORY;
912 hr = indexbuffer_init(object, device, size, usage, format, pool);
913 if (FAILED(hr))
915 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
916 HeapFree(GetProcessHeap(), 0, object);
917 return hr;
920 TRACE("Created index buffer %p.\n", object);
921 *buffer = &object->IDirect3DIndexBuffer8_iface;
923 return D3D_OK;
926 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
927 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
928 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
930 struct wined3d_resource_desc desc;
931 struct d3d8_surface *surface_impl;
932 struct wined3d_texture *texture;
933 HRESULT hr;
935 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p, "
936 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
937 device, width, height, format, flags, surface,
938 usage, pool, multisample_type, multisample_quality);
940 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
941 desc.format = wined3dformat_from_d3dformat(format);
942 desc.multisample_type = multisample_type;
943 desc.multisample_quality = multisample_quality;
944 desc.usage = usage & WINED3DUSAGE_MASK;
945 desc.pool = pool;
946 desc.width = width;
947 desc.height = height;
948 desc.depth = 1;
949 desc.size = 0;
951 wined3d_mutex_lock();
953 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
954 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
956 wined3d_mutex_unlock();
957 WARN("Failed to create texture, hr %#x.\n", hr);
958 return hr;
961 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
962 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
963 *surface = &surface_impl->IDirect3DSurface8_iface;
964 IDirect3DSurface8_AddRef(*surface);
965 wined3d_texture_decref(texture);
967 wined3d_mutex_unlock();
969 return D3D_OK;
972 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
973 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
974 IDirect3DSurface8 **surface)
976 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
977 DWORD flags = 0;
979 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
980 iface, width, height, format, multisample_type, lockable, surface);
982 *surface = NULL;
983 if (lockable)
984 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
986 return d3d8_device_create_surface(device, width, height, format, flags, surface,
987 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
990 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
991 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
992 IDirect3DSurface8 **surface)
994 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
996 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
997 iface, width, height, format, multisample_type, surface);
999 *surface = NULL;
1001 /* TODO: Verify that Discard is false */
1002 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1003 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1006 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1007 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1008 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1010 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1012 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1013 iface, width, height, format, surface);
1015 *surface = NULL;
1017 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1018 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1021 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1022 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1023 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1025 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1026 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1027 enum wined3d_format_id src_format, dst_format;
1028 struct wined3d_sub_resource_desc wined3d_desc;
1029 UINT src_w, src_h;
1031 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1032 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1034 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1035 * destination texture is in WINED3D_POOL_DEFAULT. */
1037 wined3d_mutex_lock();
1038 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1039 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1041 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1042 wined3d_mutex_unlock();
1043 return D3DERR_INVALIDCALL;
1045 src_format = wined3d_desc.format;
1046 src_w = wined3d_desc.width;
1047 src_h = wined3d_desc.height;
1049 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1050 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1052 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1053 wined3d_mutex_unlock();
1054 return D3DERR_INVALIDCALL;
1056 dst_format = wined3d_desc.format;
1058 /* Check that the source and destination formats match */
1059 if (src_format != dst_format)
1061 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1062 src_surface, dst_surface);
1063 wined3d_mutex_unlock();
1064 return D3DERR_INVALIDCALL;
1067 /* Quick if complete copy ... */
1068 if (!rect_count && !src_rects && !dst_points)
1070 RECT rect = {0, 0, src_w, src_h};
1071 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1072 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1074 else
1076 unsigned int i;
1077 /* Copy rect by rect */
1078 if (src_rects && dst_points)
1080 for (i = 0; i < rect_count; ++i)
1082 UINT w = src_rects[i].right - src_rects[i].left;
1083 UINT h = src_rects[i].bottom - src_rects[i].top;
1084 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1085 dst_points[i].x + w, dst_points[i].y + h};
1087 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1088 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1091 else
1093 for (i = 0; i < rect_count; ++i)
1095 UINT w = src_rects[i].right - src_rects[i].left;
1096 UINT h = src_rects[i].bottom - src_rects[i].top;
1097 RECT dst_rect = {0, 0, w, h};
1099 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1100 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1104 wined3d_mutex_unlock();
1106 return WINED3D_OK;
1109 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1110 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1112 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1113 struct d3d8_texture *src_impl, *dst_impl;
1114 HRESULT hr;
1116 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1118 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1119 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1121 wined3d_mutex_lock();
1122 hr = wined3d_device_update_texture(device->wined3d_device,
1123 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1124 wined3d_mutex_unlock();
1126 return hr;
1129 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1131 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1132 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1133 HRESULT hr;
1135 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1137 if (!dst_surface)
1139 WARN("Invalid destination surface passed.\n");
1140 return D3DERR_INVALIDCALL;
1143 wined3d_mutex_lock();
1144 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1145 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1146 wined3d_mutex_unlock();
1148 return hr;
1151 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1152 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1154 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1155 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1156 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1157 struct wined3d_rendertarget_view *original_dsv;
1158 HRESULT hr = D3D_OK;
1160 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1162 wined3d_mutex_lock();
1164 if (ds_impl)
1166 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1167 struct wined3d_rendertarget_view *original_rtv;
1168 struct d3d8_surface *original_surface;
1170 /* If no render target is passed in check the size against the current RT */
1171 if (!render_target)
1174 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1176 wined3d_mutex_unlock();
1177 return D3DERR_NOTFOUND;
1179 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1180 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1181 original_surface->sub_resource_idx, &rt_desc);
1183 else
1184 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1185 rt_impl->sub_resource_idx, &rt_desc);
1187 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1189 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1191 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1192 wined3d_mutex_unlock();
1193 return D3DERR_INVALIDCALL;
1195 if (ds_desc.multisample_type != rt_desc.multisample_type
1196 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1198 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1199 wined3d_mutex_unlock();
1200 return D3DERR_INVALIDCALL;
1205 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1206 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1207 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1208 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1209 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1210 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1212 wined3d_mutex_unlock();
1214 return hr;
1217 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1219 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1220 struct wined3d_rendertarget_view *wined3d_rtv;
1221 struct d3d8_surface *surface_impl;
1222 HRESULT hr;
1224 TRACE("iface %p, render_target %p.\n", iface, render_target);
1226 if (!render_target)
1227 return D3DERR_INVALIDCALL;
1229 wined3d_mutex_lock();
1230 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1232 /* We want the sub resource parent here, since the view itself may be
1233 * internal to wined3d and may not have a parent. */
1234 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1235 *render_target = &surface_impl->IDirect3DSurface8_iface;
1236 IDirect3DSurface8_AddRef(*render_target);
1237 hr = D3D_OK;
1239 else
1241 ERR("Failed to get wined3d render target.\n");
1242 *render_target = NULL;
1243 hr = D3DERR_NOTFOUND;
1245 wined3d_mutex_unlock();
1247 return hr;
1250 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1252 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1253 struct wined3d_rendertarget_view *wined3d_dsv;
1254 struct d3d8_surface *surface_impl;
1255 HRESULT hr = D3D_OK;
1257 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1259 if (!depth_stencil)
1260 return D3DERR_INVALIDCALL;
1262 wined3d_mutex_lock();
1263 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1265 /* We want the sub resource parent here, since the view itself may be
1266 * internal to wined3d and may not have a parent. */
1267 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1268 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1269 IDirect3DSurface8_AddRef(*depth_stencil);
1271 else
1273 hr = D3DERR_NOTFOUND;
1274 *depth_stencil = NULL;
1276 wined3d_mutex_unlock();
1278 return hr;
1281 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1283 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1284 HRESULT hr;
1286 TRACE("iface %p.\n", iface);
1288 wined3d_mutex_lock();
1289 hr = wined3d_device_begin_scene(device->wined3d_device);
1290 wined3d_mutex_unlock();
1292 return hr;
1295 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1297 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1298 HRESULT hr;
1300 TRACE("iface %p.\n", iface);
1302 wined3d_mutex_lock();
1303 hr = wined3d_device_end_scene(device->wined3d_device);
1304 wined3d_mutex_unlock();
1306 return hr;
1309 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1310 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1312 const struct wined3d_color c =
1314 ((color >> 16) & 0xff) / 255.0f,
1315 ((color >> 8) & 0xff) / 255.0f,
1316 (color & 0xff) / 255.0f,
1317 ((color >> 24) & 0xff) / 255.0f,
1319 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1320 HRESULT hr;
1322 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1323 iface, rect_count, rects, flags, color, z, stencil);
1325 if (rect_count && !rects)
1327 WARN("count %u with NULL rects.\n", rect_count);
1328 rect_count = 0;
1331 wined3d_mutex_lock();
1332 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1333 wined3d_mutex_unlock();
1335 return hr;
1338 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1339 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1341 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1343 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1345 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1346 wined3d_mutex_lock();
1347 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1348 wined3d_mutex_unlock();
1350 return D3D_OK;
1353 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1354 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1356 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1358 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1360 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1361 wined3d_mutex_lock();
1362 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1363 wined3d_mutex_unlock();
1365 return D3D_OK;
1368 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1369 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1371 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1373 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1375 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1376 wined3d_mutex_lock();
1377 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1378 wined3d_mutex_unlock();
1380 return D3D_OK;
1383 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1385 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1387 TRACE("iface %p, viewport %p.\n", iface, viewport);
1389 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1390 wined3d_mutex_lock();
1391 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1392 wined3d_mutex_unlock();
1394 return D3D_OK;
1397 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1399 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1401 TRACE("iface %p, viewport %p.\n", iface, viewport);
1403 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1404 wined3d_mutex_lock();
1405 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1406 wined3d_mutex_unlock();
1408 return D3D_OK;
1411 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1413 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1415 TRACE("iface %p, material %p.\n", iface, material);
1417 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1418 wined3d_mutex_lock();
1419 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1420 wined3d_mutex_unlock();
1422 return D3D_OK;
1425 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1427 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1429 TRACE("iface %p, material %p.\n", iface, material);
1431 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1432 wined3d_mutex_lock();
1433 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1434 wined3d_mutex_unlock();
1436 return D3D_OK;
1439 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1441 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1442 HRESULT hr;
1444 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1446 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1447 wined3d_mutex_lock();
1448 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1449 wined3d_mutex_unlock();
1451 return hr;
1454 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1456 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1457 HRESULT hr;
1459 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1461 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1462 wined3d_mutex_lock();
1463 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1464 wined3d_mutex_unlock();
1466 return hr;
1469 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1471 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1472 HRESULT hr;
1474 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1476 wined3d_mutex_lock();
1477 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1478 wined3d_mutex_unlock();
1480 return hr;
1483 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1485 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1486 HRESULT hr;
1488 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1490 wined3d_mutex_lock();
1491 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1492 wined3d_mutex_unlock();
1494 return hr;
1497 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1499 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1500 HRESULT hr;
1502 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1504 wined3d_mutex_lock();
1505 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1506 wined3d_mutex_unlock();
1508 return hr;
1511 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1513 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1514 HRESULT hr;
1516 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1518 wined3d_mutex_lock();
1519 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1520 wined3d_mutex_unlock();
1522 return hr;
1525 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1526 D3DRENDERSTATETYPE state, DWORD value)
1528 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1530 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1532 wined3d_mutex_lock();
1533 switch (state)
1535 case D3DRS_ZBIAS:
1536 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1537 break;
1539 default:
1540 wined3d_device_set_render_state(device->wined3d_device, state, value);
1542 wined3d_mutex_unlock();
1544 return D3D_OK;
1547 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1548 D3DRENDERSTATETYPE state, DWORD *value)
1550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1552 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1554 wined3d_mutex_lock();
1555 switch (state)
1557 case D3DRS_ZBIAS:
1558 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1559 break;
1561 default:
1562 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1564 wined3d_mutex_unlock();
1566 return D3D_OK;
1569 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1571 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1572 HRESULT hr;
1574 TRACE("iface %p.\n", iface);
1576 wined3d_mutex_lock();
1577 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1578 wined3d_mutex_unlock();
1580 return hr;
1583 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1585 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1586 struct wined3d_stateblock *stateblock;
1587 HRESULT hr;
1589 TRACE("iface %p, token %p.\n", iface, token);
1591 /* Tell wineD3D to endstateblock before anything else (in case we run out
1592 * of memory later and cause locking problems)
1594 wined3d_mutex_lock();
1595 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1596 if (FAILED(hr))
1598 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1599 wined3d_mutex_unlock();
1600 return hr;
1603 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1604 wined3d_mutex_unlock();
1606 if (*token == D3D8_INVALID_HANDLE)
1608 ERR("Failed to create a handle\n");
1609 wined3d_mutex_lock();
1610 wined3d_stateblock_decref(stateblock);
1611 wined3d_mutex_unlock();
1612 return E_FAIL;
1614 ++*token;
1616 TRACE("Returning %#x (%p).\n", *token, stateblock);
1618 return hr;
1621 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1623 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1624 struct wined3d_stateblock *stateblock;
1626 TRACE("iface %p, token %#x.\n", iface, token);
1628 if (!token)
1629 return D3D_OK;
1631 wined3d_mutex_lock();
1632 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1633 if (!stateblock)
1635 WARN("Invalid handle (%#x) passed.\n", token);
1636 wined3d_mutex_unlock();
1637 return D3DERR_INVALIDCALL;
1639 wined3d_stateblock_apply(stateblock);
1640 wined3d_mutex_unlock();
1642 return D3D_OK;
1645 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1647 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1648 struct wined3d_stateblock *stateblock;
1650 TRACE("iface %p, token %#x.\n", iface, token);
1652 wined3d_mutex_lock();
1653 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1654 if (!stateblock)
1656 WARN("Invalid handle (%#x) passed.\n", token);
1657 wined3d_mutex_unlock();
1658 return D3DERR_INVALIDCALL;
1660 wined3d_stateblock_capture(stateblock);
1661 wined3d_mutex_unlock();
1663 return D3D_OK;
1666 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1668 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1669 struct wined3d_stateblock *stateblock;
1671 TRACE("iface %p, token %#x.\n", iface, token);
1673 wined3d_mutex_lock();
1674 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1676 if (!stateblock)
1678 WARN("Invalid handle (%#x) passed.\n", token);
1679 wined3d_mutex_unlock();
1680 return D3DERR_INVALIDCALL;
1683 if (wined3d_stateblock_decref(stateblock))
1685 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1687 wined3d_mutex_unlock();
1689 return D3D_OK;
1692 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1693 D3DSTATEBLOCKTYPE type, DWORD *handle)
1695 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1696 struct wined3d_stateblock *stateblock;
1697 HRESULT hr;
1699 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1701 if (type != D3DSBT_ALL
1702 && type != D3DSBT_PIXELSTATE
1703 && type != D3DSBT_VERTEXSTATE)
1705 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1706 return D3DERR_INVALIDCALL;
1709 wined3d_mutex_lock();
1710 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1711 if (FAILED(hr))
1713 wined3d_mutex_unlock();
1714 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1715 return hr;
1718 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1719 wined3d_mutex_unlock();
1721 if (*handle == D3D8_INVALID_HANDLE)
1723 ERR("Failed to allocate a handle.\n");
1724 wined3d_mutex_lock();
1725 wined3d_stateblock_decref(stateblock);
1726 wined3d_mutex_unlock();
1727 return E_FAIL;
1729 ++*handle;
1731 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1733 return hr;
1736 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1738 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1739 HRESULT hr;
1741 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1742 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1744 wined3d_mutex_lock();
1745 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1746 wined3d_mutex_unlock();
1748 return hr;
1751 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1753 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1754 HRESULT hr;
1756 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1758 wined3d_mutex_lock();
1759 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1760 wined3d_mutex_unlock();
1762 return hr;
1765 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1767 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1768 struct wined3d_texture *wined3d_texture;
1769 struct d3d8_texture *texture_impl;
1771 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1773 if (!texture)
1774 return D3DERR_INVALIDCALL;
1776 wined3d_mutex_lock();
1777 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1779 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1780 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1781 IDirect3DBaseTexture8_AddRef(*texture);
1783 else
1785 *texture = NULL;
1787 wined3d_mutex_unlock();
1789 return D3D_OK;
1792 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1794 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1795 struct d3d8_texture *texture_impl;
1796 HRESULT hr;
1798 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1800 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1802 wined3d_mutex_lock();
1803 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1804 texture_impl ? texture_impl->wined3d_texture : NULL);
1805 wined3d_mutex_unlock();
1807 return hr;
1810 static const struct tss_lookup
1812 BOOL sampler_state;
1813 enum wined3d_texture_stage_state state;
1815 tss_lookup[] =
1817 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1818 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1819 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1820 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1821 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1822 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1823 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1824 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1825 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1826 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1827 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1828 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1829 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1830 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1831 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1832 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1833 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1834 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1835 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1836 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1837 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1838 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1839 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1840 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1841 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1842 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1843 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1844 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1845 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1848 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1849 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1851 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1852 const struct tss_lookup *l;
1854 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1856 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1858 WARN("Invalid Type %#x passed.\n", Type);
1859 return D3D_OK;
1862 l = &tss_lookup[Type];
1864 wined3d_mutex_lock();
1865 if (l->sampler_state)
1866 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1867 else
1868 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1869 wined3d_mutex_unlock();
1871 return D3D_OK;
1874 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1875 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1877 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1878 const struct tss_lookup *l;
1880 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1882 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1884 WARN("Invalid type %#x passed.\n", type);
1885 return D3D_OK;
1888 l = &tss_lookup[type];
1890 wined3d_mutex_lock();
1891 if (l->sampler_state)
1892 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1893 else
1894 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1895 wined3d_mutex_unlock();
1897 return D3D_OK;
1900 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1902 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1903 HRESULT hr;
1905 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1907 wined3d_mutex_lock();
1908 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1909 wined3d_mutex_unlock();
1911 return hr;
1914 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1915 DWORD info_id, void *info, DWORD info_size)
1917 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1919 return D3D_OK;
1922 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1923 UINT palette_idx, const PALETTEENTRY *entries)
1925 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1927 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1928 * does not have a d3d8/9-style palette API */
1930 return D3D_OK;
1933 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1934 UINT palette_idx, PALETTEENTRY *entries)
1936 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1938 return D3DERR_INVALIDCALL;
1941 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1943 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1945 return D3D_OK;
1948 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1950 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1952 return D3DERR_INVALIDCALL;
1955 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1956 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1958 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1959 HRESULT hr;
1961 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1962 iface, primitive_type, start_vertex, primitive_count);
1964 wined3d_mutex_lock();
1965 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1966 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1967 vertex_count_from_primitive_count(primitive_type, primitive_count));
1968 wined3d_mutex_unlock();
1970 return hr;
1973 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1974 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1975 UINT start_idx, UINT primitive_count)
1977 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1978 HRESULT hr;
1980 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1981 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1983 wined3d_mutex_lock();
1984 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1985 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1986 vertex_count_from_primitive_count(primitive_type, primitive_count));
1987 wined3d_mutex_unlock();
1989 return hr;
1992 /* The caller is responsible for wined3d locking */
1993 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1995 HRESULT hr;
1997 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1999 UINT size = max(device->vertex_buffer_size * 2, min_size);
2000 struct wined3d_buffer *buffer;
2002 TRACE("Growing vertex buffer to %u bytes\n", size);
2004 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2005 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2006 if (FAILED(hr))
2008 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2009 return hr;
2012 if (device->vertex_buffer)
2013 wined3d_buffer_decref(device->vertex_buffer);
2015 device->vertex_buffer = buffer;
2016 device->vertex_buffer_size = size;
2017 device->vertex_buffer_pos = 0;
2019 return D3D_OK;
2022 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2023 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2024 UINT stride)
2026 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2027 HRESULT hr;
2028 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2029 UINT size = vtx_count * stride;
2030 UINT vb_pos, align;
2031 BYTE *buffer_data;
2033 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2034 iface, primitive_type, primitive_count, data, stride);
2036 if (!primitive_count)
2038 WARN("primitive_count is 0, returning D3D_OK\n");
2039 return D3D_OK;
2042 wined3d_mutex_lock();
2043 hr = d3d8_device_prepare_vertex_buffer(device, size);
2044 if (FAILED(hr))
2045 goto done;
2047 vb_pos = device->vertex_buffer_pos;
2048 align = vb_pos % stride;
2049 if (align) align = stride - align;
2050 if (vb_pos + size + align > device->vertex_buffer_size)
2051 vb_pos = 0;
2052 else
2053 vb_pos += align;
2055 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2056 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2057 if (FAILED(hr))
2058 goto done;
2059 memcpy(buffer_data, data, size);
2060 wined3d_buffer_unmap(device->vertex_buffer);
2061 device->vertex_buffer_pos = vb_pos + size;
2063 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2064 if (FAILED(hr))
2065 goto done;
2067 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2068 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2069 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2071 done:
2072 wined3d_mutex_unlock();
2073 return hr;
2076 /* The caller is responsible for wined3d locking */
2077 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2079 HRESULT hr;
2081 if (device->index_buffer_size < min_size || !device->index_buffer)
2083 UINT size = max(device->index_buffer_size * 2, min_size);
2084 struct wined3d_buffer *buffer;
2086 TRACE("Growing index buffer to %u bytes\n", size);
2088 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2089 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2090 if (FAILED(hr))
2092 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2093 return hr;
2096 if (device->index_buffer)
2097 wined3d_buffer_decref(device->index_buffer);
2099 device->index_buffer = buffer;
2100 device->index_buffer_size = size;
2101 device->index_buffer_pos = 0;
2103 return D3D_OK;
2106 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2107 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2108 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2109 const void *vertex_data, UINT vertex_stride)
2111 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2112 HRESULT hr;
2113 BYTE *buffer_data;
2115 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2116 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2117 UINT idx_size = idx_count * idx_fmt_size;
2118 UINT ib_pos;
2120 UINT vtx_size = vertex_count * vertex_stride;
2121 UINT vb_pos, align;
2123 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2124 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2125 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2126 index_data, index_format, vertex_data, vertex_stride);
2128 if (!primitive_count)
2130 WARN("primitive_count is 0, returning D3D_OK\n");
2131 return D3D_OK;
2134 wined3d_mutex_lock();
2136 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2137 if (FAILED(hr))
2138 goto done;
2140 vb_pos = device->vertex_buffer_pos;
2141 align = vb_pos % vertex_stride;
2142 if (align) align = vertex_stride - align;
2143 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2144 vb_pos = 0;
2145 else
2146 vb_pos += align;
2148 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2149 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2150 if (FAILED(hr))
2151 goto done;
2152 memcpy(buffer_data, vertex_data, vtx_size);
2153 wined3d_buffer_unmap(device->vertex_buffer);
2154 device->vertex_buffer_pos = vb_pos + vtx_size;
2156 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2157 if (FAILED(hr))
2158 goto done;
2160 ib_pos = device->index_buffer_pos;
2161 align = ib_pos % idx_fmt_size;
2162 if (align) align = idx_fmt_size - align;
2163 if (ib_pos + idx_size + align > device->index_buffer_size)
2164 ib_pos = 0;
2165 else
2166 ib_pos += align;
2168 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2169 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2170 if (FAILED(hr))
2171 goto done;
2172 memcpy(buffer_data, index_data, idx_size);
2173 wined3d_buffer_unmap(device->index_buffer);
2174 device->index_buffer_pos = ib_pos + idx_size;
2176 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2177 if (FAILED(hr))
2178 goto done;
2180 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2181 wined3dformat_from_d3dformat(index_format));
2182 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2184 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2185 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2187 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2188 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2189 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2191 done:
2192 wined3d_mutex_unlock();
2193 return hr;
2196 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2197 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2199 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2200 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2201 HRESULT hr;
2203 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2204 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2206 wined3d_mutex_lock();
2207 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2208 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2209 wined3d_mutex_unlock();
2211 return hr;
2214 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2215 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2217 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2218 struct d3d8_vertex_shader *object;
2219 DWORD shader_handle;
2220 DWORD handle;
2221 HRESULT hr;
2223 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2224 iface, declaration, byte_code, shader, usage);
2226 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2227 if (!object)
2229 *shader = 0;
2230 return E_OUTOFMEMORY;
2233 wined3d_mutex_lock();
2234 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2235 wined3d_mutex_unlock();
2236 if (handle == D3D8_INVALID_HANDLE)
2238 ERR("Failed to allocate vertex shader handle.\n");
2239 HeapFree(GetProcessHeap(), 0, object);
2240 *shader = 0;
2241 return E_OUTOFMEMORY;
2244 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2246 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2247 if (FAILED(hr))
2249 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2250 wined3d_mutex_lock();
2251 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2252 wined3d_mutex_unlock();
2253 HeapFree(GetProcessHeap(), 0, object);
2254 *shader = 0;
2255 return hr;
2258 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2259 *shader = shader_handle;
2261 return D3D_OK;
2264 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2266 struct d3d8_vertex_declaration *d3d8_declaration;
2267 struct FvfToDecl *convertedDecls = device->decls;
2268 int p, low, high; /* deliberately signed */
2269 HRESULT hr;
2271 TRACE("Searching for declaration for fvf %08x... ", fvf);
2273 low = 0;
2274 high = device->numConvertedDecls - 1;
2275 while (low <= high)
2277 p = (low + high) >> 1;
2278 TRACE("%d ", p);
2280 if (convertedDecls[p].fvf == fvf)
2282 TRACE("found %p\n", convertedDecls[p].declaration);
2283 return convertedDecls[p].declaration;
2286 if (convertedDecls[p].fvf < fvf)
2287 low = p + 1;
2288 else
2289 high = p - 1;
2291 TRACE("not found. Creating and inserting at position %d.\n", low);
2293 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2294 return NULL;
2296 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2298 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2299 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2300 return NULL;
2303 if (device->declArraySize == device->numConvertedDecls)
2305 UINT grow = device->declArraySize / 2;
2307 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2308 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2309 if (!convertedDecls)
2311 d3d8_vertex_declaration_destroy(d3d8_declaration);
2312 return NULL;
2314 device->decls = convertedDecls;
2315 device->declArraySize += grow;
2318 memmove(convertedDecls + low + 1, convertedDecls + low,
2319 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2320 convertedDecls[low].declaration = d3d8_declaration;
2321 convertedDecls[low].fvf = fvf;
2322 ++device->numConvertedDecls;
2324 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2326 return d3d8_declaration;
2329 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2331 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2332 struct d3d8_vertex_shader *shader_impl;
2334 TRACE("iface %p, shader %#x.\n", iface, shader);
2336 if (VS_HIGHESTFIXEDFXF >= shader)
2338 TRACE("Setting FVF, %#x\n", shader);
2340 wined3d_mutex_lock();
2341 wined3d_device_set_vertex_declaration(device->wined3d_device,
2342 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2343 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2344 wined3d_mutex_unlock();
2346 return D3D_OK;
2349 TRACE("Setting shader\n");
2351 wined3d_mutex_lock();
2352 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2354 WARN("Invalid handle (%#x) passed.\n", shader);
2355 wined3d_mutex_unlock();
2357 return D3DERR_INVALIDCALL;
2360 wined3d_device_set_vertex_declaration(device->wined3d_device,
2361 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2362 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2363 wined3d_mutex_unlock();
2365 return D3D_OK;
2368 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2370 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2371 struct wined3d_vertex_declaration *wined3d_declaration;
2372 struct d3d8_vertex_declaration *d3d8_declaration;
2374 TRACE("iface %p, shader %p.\n", iface, shader);
2376 wined3d_mutex_lock();
2377 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2379 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2380 *shader = d3d8_declaration->shader_handle;
2382 else
2384 *shader = 0;
2386 wined3d_mutex_unlock();
2388 TRACE("Returning %#x.\n", *shader);
2390 return D3D_OK;
2393 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2395 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2396 struct d3d8_vertex_shader *shader_impl;
2398 TRACE("iface %p, shader %#x.\n", iface, shader);
2400 wined3d_mutex_lock();
2401 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2403 WARN("Invalid handle (%#x) passed.\n", shader);
2404 wined3d_mutex_unlock();
2406 return D3DERR_INVALIDCALL;
2409 if (shader_impl->wined3d_shader
2410 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2411 IDirect3DDevice8_SetVertexShader(iface, 0);
2413 wined3d_mutex_unlock();
2415 d3d8_vertex_shader_destroy(shader_impl);
2417 return D3D_OK;
2420 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2421 DWORD start_register, const void *data, DWORD count)
2423 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2424 HRESULT hr;
2426 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2427 iface, start_register, data, count);
2429 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2431 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2432 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2433 return D3DERR_INVALIDCALL;
2436 wined3d_mutex_lock();
2437 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2438 wined3d_mutex_unlock();
2440 return hr;
2443 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2444 DWORD start_register, void *data, DWORD count)
2446 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2447 HRESULT hr;
2449 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2450 iface, start_register, data, count);
2452 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2454 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2455 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2456 return D3DERR_INVALIDCALL;
2459 wined3d_mutex_lock();
2460 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2461 wined3d_mutex_unlock();
2463 return hr;
2466 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2467 DWORD shader, void *data, DWORD *data_size)
2469 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2470 struct d3d8_vertex_declaration *declaration;
2471 struct d3d8_vertex_shader *shader_impl;
2473 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2474 iface, shader, data, data_size);
2476 wined3d_mutex_lock();
2477 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2478 wined3d_mutex_unlock();
2480 if (!shader_impl)
2482 WARN("Invalid handle (%#x) passed.\n", shader);
2483 return D3DERR_INVALIDCALL;
2485 declaration = shader_impl->vertex_declaration;
2487 if (!data)
2489 *data_size = declaration->elements_size;
2490 return D3D_OK;
2493 /* MSDN claims that if *data_size is smaller than the required size
2494 * we should write the required size and return D3DERR_MOREDATA.
2495 * That's not actually true. */
2496 if (*data_size < declaration->elements_size)
2497 return D3DERR_INVALIDCALL;
2499 memcpy(data, declaration->elements, declaration->elements_size);
2501 return D3D_OK;
2504 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2505 DWORD shader, void *data, DWORD *data_size)
2507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2508 struct d3d8_vertex_shader *shader_impl = NULL;
2509 HRESULT hr;
2511 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2512 iface, shader, data, data_size);
2514 wined3d_mutex_lock();
2515 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2517 WARN("Invalid handle (%#x) passed.\n", shader);
2518 wined3d_mutex_unlock();
2520 return D3DERR_INVALIDCALL;
2523 if (!shader_impl->wined3d_shader)
2525 wined3d_mutex_unlock();
2526 *data_size = 0;
2527 return D3D_OK;
2530 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2531 wined3d_mutex_unlock();
2533 return hr;
2536 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2537 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2539 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2540 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2542 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2544 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2545 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2546 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2547 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2548 * problem)
2550 wined3d_mutex_lock();
2551 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2552 wined3d_device_set_index_buffer(device->wined3d_device,
2553 ib ? ib->wined3d_buffer : NULL,
2554 ib ? ib->format : WINED3DFMT_UNKNOWN);
2555 wined3d_mutex_unlock();
2557 return D3D_OK;
2560 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2561 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2563 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2564 enum wined3d_format_id wined3d_format;
2565 struct wined3d_buffer *wined3d_buffer;
2566 struct d3d8_indexbuffer *buffer_impl;
2568 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2570 if (!buffer)
2571 return D3DERR_INVALIDCALL;
2573 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2574 wined3d_mutex_lock();
2575 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2576 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2578 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2579 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2580 IDirect3DIndexBuffer8_AddRef(*buffer);
2582 else
2584 *buffer = NULL;
2586 wined3d_mutex_unlock();
2588 return D3D_OK;
2591 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2592 const DWORD *byte_code, DWORD *shader)
2594 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2595 struct d3d8_pixel_shader *object;
2596 DWORD shader_handle;
2597 DWORD handle;
2598 HRESULT hr;
2600 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2602 if (!shader)
2603 return D3DERR_INVALIDCALL;
2605 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2606 if (!object)
2607 return E_OUTOFMEMORY;
2609 wined3d_mutex_lock();
2610 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2611 wined3d_mutex_unlock();
2612 if (handle == D3D8_INVALID_HANDLE)
2614 ERR("Failed to allocate pixel shader handle.\n");
2615 HeapFree(GetProcessHeap(), 0, object);
2616 return E_OUTOFMEMORY;
2619 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2621 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2622 if (FAILED(hr))
2624 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2625 wined3d_mutex_lock();
2626 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2627 wined3d_mutex_unlock();
2628 HeapFree(GetProcessHeap(), 0, object);
2629 *shader = 0;
2630 return hr;
2633 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2634 *shader = shader_handle;
2636 return D3D_OK;
2639 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2641 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2642 struct d3d8_pixel_shader *shader_impl;
2644 TRACE("iface %p, shader %#x.\n", iface, shader);
2646 wined3d_mutex_lock();
2648 if (!shader)
2650 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2651 wined3d_mutex_unlock();
2652 return D3D_OK;
2655 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2657 WARN("Invalid handle (%#x) passed.\n", shader);
2658 wined3d_mutex_unlock();
2659 return D3DERR_INVALIDCALL;
2662 TRACE("Setting shader %p.\n", shader_impl);
2663 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2664 wined3d_mutex_unlock();
2666 return D3D_OK;
2669 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2671 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2672 struct wined3d_shader *object;
2674 TRACE("iface %p, shader %p.\n", iface, shader);
2676 if (!shader)
2677 return D3DERR_INVALIDCALL;
2679 wined3d_mutex_lock();
2680 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2682 struct d3d8_pixel_shader *d3d8_shader;
2683 d3d8_shader = wined3d_shader_get_parent(object);
2684 *shader = d3d8_shader->handle;
2686 else
2688 *shader = 0;
2690 wined3d_mutex_unlock();
2692 TRACE("Returning %#x.\n", *shader);
2694 return D3D_OK;
2697 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2699 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2700 struct d3d8_pixel_shader *shader_impl;
2702 TRACE("iface %p, shader %#x.\n", iface, shader);
2704 wined3d_mutex_lock();
2706 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2708 WARN("Invalid handle (%#x) passed.\n", shader);
2709 wined3d_mutex_unlock();
2710 return D3DERR_INVALIDCALL;
2713 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2714 IDirect3DDevice8_SetPixelShader(iface, 0);
2716 wined3d_mutex_unlock();
2718 d3d8_pixel_shader_destroy(shader_impl);
2720 return D3D_OK;
2723 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2724 DWORD start_register, const void *data, DWORD count)
2726 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2727 HRESULT hr;
2729 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2730 iface, start_register, data, count);
2732 wined3d_mutex_lock();
2733 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2734 wined3d_mutex_unlock();
2736 return hr;
2739 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2740 DWORD start_register, void *data, DWORD count)
2742 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2743 HRESULT hr;
2745 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2746 iface, start_register, data, count);
2748 wined3d_mutex_lock();
2749 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2750 wined3d_mutex_unlock();
2752 return hr;
2755 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2756 DWORD shader, void *data, DWORD *data_size)
2758 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2759 struct d3d8_pixel_shader *shader_impl = NULL;
2760 HRESULT hr;
2762 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2763 iface, shader, data, data_size);
2765 wined3d_mutex_lock();
2766 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2768 WARN("Invalid handle (%#x) passed.\n", shader);
2769 wined3d_mutex_unlock();
2771 return D3DERR_INVALIDCALL;
2774 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2775 wined3d_mutex_unlock();
2777 return hr;
2780 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2781 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2783 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2784 iface, handle, segment_count, patch_info);
2785 return D3D_OK;
2788 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2789 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2791 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2792 iface, handle, segment_count, patch_info);
2793 return D3D_OK;
2796 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2798 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2799 return D3DERR_INVALIDCALL;
2802 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2803 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2805 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2806 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2807 HRESULT hr;
2809 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2810 iface, stream_idx, buffer, stride);
2812 wined3d_mutex_lock();
2813 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2814 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2815 wined3d_mutex_unlock();
2817 return hr;
2820 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2821 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2823 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2824 struct d3d8_vertexbuffer *buffer_impl;
2825 struct wined3d_buffer *wined3d_buffer = NULL;
2826 HRESULT hr;
2828 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2829 iface, stream_idx, buffer, stride);
2831 if (!buffer)
2832 return D3DERR_INVALIDCALL;
2834 wined3d_mutex_lock();
2835 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2836 if (SUCCEEDED(hr) && wined3d_buffer)
2838 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2839 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2840 IDirect3DVertexBuffer8_AddRef(*buffer);
2842 else
2844 if (FAILED(hr))
2845 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2846 *buffer = NULL;
2848 wined3d_mutex_unlock();
2850 return hr;
2853 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2855 d3d8_device_QueryInterface,
2856 d3d8_device_AddRef,
2857 d3d8_device_Release,
2858 d3d8_device_TestCooperativeLevel,
2859 d3d8_device_GetAvailableTextureMem,
2860 d3d8_device_ResourceManagerDiscardBytes,
2861 d3d8_device_GetDirect3D,
2862 d3d8_device_GetDeviceCaps,
2863 d3d8_device_GetDisplayMode,
2864 d3d8_device_GetCreationParameters,
2865 d3d8_device_SetCursorProperties,
2866 d3d8_device_SetCursorPosition,
2867 d3d8_device_ShowCursor,
2868 d3d8_device_CreateAdditionalSwapChain,
2869 d3d8_device_Reset,
2870 d3d8_device_Present,
2871 d3d8_device_GetBackBuffer,
2872 d3d8_device_GetRasterStatus,
2873 d3d8_device_SetGammaRamp,
2874 d3d8_device_GetGammaRamp,
2875 d3d8_device_CreateTexture,
2876 d3d8_device_CreateVolumeTexture,
2877 d3d8_device_CreateCubeTexture,
2878 d3d8_device_CreateVertexBuffer,
2879 d3d8_device_CreateIndexBuffer,
2880 d3d8_device_CreateRenderTarget,
2881 d3d8_device_CreateDepthStencilSurface,
2882 d3d8_device_CreateImageSurface,
2883 d3d8_device_CopyRects,
2884 d3d8_device_UpdateTexture,
2885 d3d8_device_GetFrontBuffer,
2886 d3d8_device_SetRenderTarget,
2887 d3d8_device_GetRenderTarget,
2888 d3d8_device_GetDepthStencilSurface,
2889 d3d8_device_BeginScene,
2890 d3d8_device_EndScene,
2891 d3d8_device_Clear,
2892 d3d8_device_SetTransform,
2893 d3d8_device_GetTransform,
2894 d3d8_device_MultiplyTransform,
2895 d3d8_device_SetViewport,
2896 d3d8_device_GetViewport,
2897 d3d8_device_SetMaterial,
2898 d3d8_device_GetMaterial,
2899 d3d8_device_SetLight,
2900 d3d8_device_GetLight,
2901 d3d8_device_LightEnable,
2902 d3d8_device_GetLightEnable,
2903 d3d8_device_SetClipPlane,
2904 d3d8_device_GetClipPlane,
2905 d3d8_device_SetRenderState,
2906 d3d8_device_GetRenderState,
2907 d3d8_device_BeginStateBlock,
2908 d3d8_device_EndStateBlock,
2909 d3d8_device_ApplyStateBlock,
2910 d3d8_device_CaptureStateBlock,
2911 d3d8_device_DeleteStateBlock,
2912 d3d8_device_CreateStateBlock,
2913 d3d8_device_SetClipStatus,
2914 d3d8_device_GetClipStatus,
2915 d3d8_device_GetTexture,
2916 d3d8_device_SetTexture,
2917 d3d8_device_GetTextureStageState,
2918 d3d8_device_SetTextureStageState,
2919 d3d8_device_ValidateDevice,
2920 d3d8_device_GetInfo,
2921 d3d8_device_SetPaletteEntries,
2922 d3d8_device_GetPaletteEntries,
2923 d3d8_device_SetCurrentTexturePalette,
2924 d3d8_device_GetCurrentTexturePalette,
2925 d3d8_device_DrawPrimitive,
2926 d3d8_device_DrawIndexedPrimitive,
2927 d3d8_device_DrawPrimitiveUP,
2928 d3d8_device_DrawIndexedPrimitiveUP,
2929 d3d8_device_ProcessVertices,
2930 d3d8_device_CreateVertexShader,
2931 d3d8_device_SetVertexShader,
2932 d3d8_device_GetVertexShader,
2933 d3d8_device_DeleteVertexShader,
2934 d3d8_device_SetVertexShaderConstant,
2935 d3d8_device_GetVertexShaderConstant,
2936 d3d8_device_GetVertexShaderDeclaration,
2937 d3d8_device_GetVertexShaderFunction,
2938 d3d8_device_SetStreamSource,
2939 d3d8_device_GetStreamSource,
2940 d3d8_device_SetIndices,
2941 d3d8_device_GetIndices,
2942 d3d8_device_CreatePixelShader,
2943 d3d8_device_SetPixelShader,
2944 d3d8_device_GetPixelShader,
2945 d3d8_device_DeletePixelShader,
2946 d3d8_device_SetPixelShaderConstant,
2947 d3d8_device_GetPixelShaderConstant,
2948 d3d8_device_GetPixelShaderFunction,
2949 d3d8_device_DrawRectPatch,
2950 d3d8_device_DrawTriPatch,
2951 d3d8_device_DeletePatch,
2954 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2956 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2959 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2960 struct wined3d_device *device)
2962 TRACE("device_parent %p, device %p\n", device_parent, device);
2965 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2967 TRACE("device_parent %p.\n", device_parent);
2970 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2972 struct d3d8_device *device = device_from_device_parent(device_parent);
2974 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2976 if (!activate)
2977 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2978 else
2979 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2982 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2983 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
2984 void **parent, const struct wined3d_parent_ops **parent_ops)
2986 struct d3d8_surface *d3d_surface;
2988 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
2989 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
2991 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2992 return E_OUTOFMEMORY;
2994 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
2995 *parent = d3d_surface;
2996 TRACE("Created surface %p.\n", d3d_surface);
2998 return D3D_OK;
3001 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3002 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3003 void **parent, const struct wined3d_parent_ops **parent_ops)
3005 struct d3d8_volume *d3d_volume;
3007 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3008 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3010 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3011 return E_OUTOFMEMORY;
3013 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3014 *parent = d3d_volume;
3015 TRACE("Created volume %p.\n", d3d_volume);
3017 return D3D_OK;
3020 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3021 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3023 struct d3d8_device *device = device_from_device_parent(device_parent);
3024 struct d3d8_surface *d3d_surface;
3025 HRESULT hr;
3027 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3028 device_parent, container_parent, desc, texture);
3030 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
3031 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3033 WARN("Failed to create texture, hr %#x.\n", hr);
3034 return hr;
3037 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3038 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3040 return hr;
3043 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3044 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3046 struct d3d8_device *device = device_from_device_parent(device_parent);
3047 struct d3d8_swapchain *d3d_swapchain;
3048 HRESULT hr;
3050 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3052 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3054 WARN("Failed to create swapchain, hr %#x.\n", hr);
3055 *swapchain = NULL;
3056 return hr;
3059 *swapchain = d3d_swapchain->wined3d_swapchain;
3060 wined3d_swapchain_incref(*swapchain);
3061 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3063 return hr;
3066 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3068 device_parent_wined3d_device_created,
3069 device_parent_mode_changed,
3070 device_parent_activate,
3071 device_parent_surface_created,
3072 device_parent_volume_created,
3073 device_parent_create_swapchain_texture,
3074 device_parent_create_swapchain,
3077 static void setup_fpu(void)
3079 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3080 WORD cw;
3081 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3082 cw = (cw & ~0xf3f) | 0x3f;
3083 __asm__ volatile ("fldcw %0" : : "m" (cw));
3084 #elif defined(__i386__) && defined(_MSC_VER)
3085 WORD cw;
3086 __asm fnstcw cw;
3087 cw = (cw & ~0xf3f) | 0x3f;
3088 __asm fldcw cw;
3089 #else
3090 FIXME("FPU setup not implemented for this platform.\n");
3091 #endif
3094 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3095 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3097 struct wined3d_swapchain_desc swapchain_desc;
3098 struct wined3d_swapchain *wined3d_swapchain;
3099 HRESULT hr;
3101 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3102 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3103 device->ref = 1;
3104 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3105 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3106 if (!device->handle_table.entries)
3108 ERR("Failed to allocate handle table memory.\n");
3109 return E_OUTOFMEMORY;
3111 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3113 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3115 wined3d_mutex_lock();
3116 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3117 &device->device_parent, &device->wined3d_device);
3118 if (FAILED(hr))
3120 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3121 wined3d_mutex_unlock();
3122 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3123 return hr;
3126 if (!parameters->Windowed)
3128 HWND device_window = parameters->hDeviceWindow;
3130 if (!focus_window)
3131 focus_window = device_window;
3132 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3134 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3135 wined3d_device_decref(device->wined3d_device);
3136 wined3d_mutex_unlock();
3137 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3138 return hr;
3141 if (!device_window)
3142 device_window = focus_window;
3143 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3144 parameters->BackBufferWidth,
3145 parameters->BackBufferHeight);
3148 if (flags & D3DCREATE_MULTITHREADED)
3149 wined3d_device_set_multithreaded(device->wined3d_device);
3151 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3153 wined3d_device_release_focus_window(device->wined3d_device);
3154 wined3d_device_decref(device->wined3d_device);
3155 wined3d_mutex_unlock();
3156 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3157 return D3DERR_INVALIDCALL;
3160 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3161 if (FAILED(hr))
3163 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3164 wined3d_device_release_focus_window(device->wined3d_device);
3165 wined3d_device_decref(device->wined3d_device);
3166 wined3d_mutex_unlock();
3167 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3168 return hr;
3171 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3172 wined3d_mutex_unlock();
3174 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3176 device->declArraySize = 16;
3177 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3178 if (!device->decls)
3180 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3181 hr = E_OUTOFMEMORY;
3182 goto err;
3185 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3186 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3188 device->d3d_parent = &parent->IDirect3D8_iface;
3189 IDirect3D8_AddRef(device->d3d_parent);
3191 return D3D_OK;
3193 err:
3194 wined3d_mutex_lock();
3195 wined3d_device_uninit_3d(device->wined3d_device);
3196 wined3d_device_release_focus_window(device->wined3d_device);
3197 wined3d_device_decref(device->wined3d_device);
3198 wined3d_mutex_unlock();
3199 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3200 return hr;