ddraw/tests: Rewrite StructSizeTest().
[wine.git] / dlls / d3d8 / device.c
blob43b8e2be4bbd86cc1df31e4dcc1b656d30514965
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_R16_UINT: return D3DFMT_INDEX16;
87 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
88 default:
89 FIXME("Unhandled wined3d format %#x.\n", format);
90 return D3DFMT_UNKNOWN;
94 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
96 BYTE *c = (BYTE *)&format;
98 /* Don't translate FOURCC formats */
99 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
101 switch(format)
103 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
104 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
105 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
106 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
107 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
108 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
109 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
110 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
111 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
112 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
113 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
114 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
115 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
116 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
117 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
118 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
119 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
120 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
121 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
122 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
123 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
124 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
125 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
126 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
127 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
128 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
129 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
130 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
131 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
132 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
133 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
134 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
135 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
136 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
137 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
138 default:
139 FIXME("Unhandled D3DFORMAT %#x.\n", format);
140 return WINED3DFMT_UNKNOWN;
144 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
146 switch(primitive_type)
148 case D3DPT_POINTLIST:
149 return primitive_count;
151 case D3DPT_LINELIST:
152 return primitive_count * 2;
154 case D3DPT_LINESTRIP:
155 return primitive_count + 1;
157 case D3DPT_TRIANGLELIST:
158 return primitive_count * 3;
160 case D3DPT_TRIANGLESTRIP:
161 case D3DPT_TRIANGLEFAN:
162 return primitive_count + 2;
164 default:
165 FIXME("Unhandled primitive type %#x\n", primitive_type);
166 return 0;
170 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
171 const struct wined3d_swapchain_desc *swapchain_desc)
173 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
174 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
175 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
176 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
177 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
178 present_parameters->SwapEffect = swapchain_desc->swap_effect;
179 present_parameters->hDeviceWindow = swapchain_desc->device_window;
180 present_parameters->Windowed = swapchain_desc->windowed;
181 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
182 present_parameters->AutoDepthStencilFormat
183 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
184 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
185 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
186 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
189 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
190 const D3DPRESENT_PARAMETERS *present_parameters)
192 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
194 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
195 return FALSE;
197 if (present_parameters->BackBufferCount > 3
198 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
199 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
200 && present_parameters->BackBufferCount > 1))
202 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
203 return FALSE;
206 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
207 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
208 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
209 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
210 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
211 swapchain_desc->multisample_quality = 0; /* d3d9 only */
212 swapchain_desc->swap_effect = present_parameters->SwapEffect;
213 swapchain_desc->device_window = present_parameters->hDeviceWindow;
214 swapchain_desc->windowed = present_parameters->Windowed;
215 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
216 swapchain_desc->auto_depth_stencil_format
217 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
218 swapchain_desc->flags
219 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
220 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
221 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
222 swapchain_desc->auto_restore_display_mode = TRUE;
224 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
225 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
227 return TRUE;
230 /* Handle table functions */
231 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
233 struct d3d8_handle_entry *entry;
235 if (t->free_entries)
237 DWORD index = t->free_entries - t->entries;
238 /* Use a free handle */
239 entry = t->free_entries;
240 if (entry->type != D3D8_HANDLE_FREE)
242 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
243 return D3D8_INVALID_HANDLE;
245 t->free_entries = entry->object;
246 entry->object = object;
247 entry->type = type;
249 return index;
252 if (!(t->entry_count < t->table_size))
254 /* Grow the table */
255 UINT new_size = t->table_size + (t->table_size >> 1);
256 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
257 0, t->entries, new_size * sizeof(*t->entries));
258 if (!new_entries)
260 ERR("Failed to grow the handle table.\n");
261 return D3D8_INVALID_HANDLE;
263 t->entries = new_entries;
264 t->table_size = new_size;
267 entry = &t->entries[t->entry_count];
268 entry->object = object;
269 entry->type = type;
271 return t->entry_count++;
274 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
276 struct d3d8_handle_entry *entry;
277 void *object;
279 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
281 WARN("Invalid handle %u passed.\n", handle);
282 return NULL;
285 entry = &t->entries[handle];
286 if (entry->type != type)
288 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
289 return NULL;
292 object = entry->object;
293 entry->object = t->free_entries;
294 entry->type = D3D8_HANDLE_FREE;
295 t->free_entries = entry;
297 return object;
300 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
302 struct d3d8_handle_entry *entry;
304 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
306 WARN("Invalid handle %u passed.\n", handle);
307 return NULL;
310 entry = &t->entries[handle];
311 if (entry->type != type)
313 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
314 return NULL;
317 return entry->object;
320 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
322 TRACE("iface %p, riid %s, out %p.\n",
323 iface, debugstr_guid(riid), out);
325 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
326 || IsEqualGUID(riid, &IID_IUnknown))
328 IDirect3DDevice8_AddRef(iface);
329 *out = iface;
330 return S_OK;
333 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
335 *out = NULL;
336 return E_NOINTERFACE;
339 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
341 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
342 ULONG ref = InterlockedIncrement(&device->ref);
344 TRACE("%p increasing refcount to %u.\n", iface, ref);
346 return ref;
349 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
351 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
352 ULONG ref;
354 if (device->inDestruction)
355 return 0;
357 ref = InterlockedDecrement(&device->ref);
359 TRACE("%p decreasing refcount to %u.\n", iface, ref);
361 if (!ref)
363 IDirect3D8 *parent = device->d3d_parent;
364 unsigned i;
366 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
368 wined3d_mutex_lock();
370 device->inDestruction = TRUE;
372 for (i = 0; i < device->numConvertedDecls; ++i)
374 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
376 HeapFree(GetProcessHeap(), 0, device->decls);
378 if (device->vertex_buffer)
379 wined3d_buffer_decref(device->vertex_buffer);
380 if (device->index_buffer)
381 wined3d_buffer_decref(device->index_buffer);
383 wined3d_device_uninit_3d(device->wined3d_device);
384 wined3d_device_release_focus_window(device->wined3d_device);
385 wined3d_device_decref(device->wined3d_device);
386 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
387 HeapFree(GetProcessHeap(), 0, device);
389 wined3d_mutex_unlock();
391 IDirect3D8_Release(parent);
393 return ref;
396 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
398 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
400 TRACE("iface %p.\n", iface);
402 TRACE("device state: %#x.\n", device->device_state);
404 switch (device->device_state)
406 default:
407 case D3D8_DEVICE_STATE_OK:
408 return D3D_OK;
409 case D3D8_DEVICE_STATE_LOST:
410 return D3DERR_DEVICELOST;
411 case D3D8_DEVICE_STATE_NOT_RESET:
412 return D3DERR_DEVICENOTRESET;
416 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
418 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
419 UINT ret;
421 TRACE("iface %p.\n", iface);
423 wined3d_mutex_lock();
424 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
425 wined3d_mutex_unlock();
427 return ret;
430 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
432 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
434 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
436 if (byte_count)
437 FIXME("Byte count ignored.\n");
439 wined3d_mutex_lock();
440 wined3d_device_evict_managed_resources(device->wined3d_device);
441 wined3d_mutex_unlock();
443 return D3D_OK;
446 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
448 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
450 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
452 if (!d3d8)
453 return D3DERR_INVALIDCALL;
455 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
458 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
460 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
461 WINED3DCAPS *wined3d_caps;
462 HRESULT hr;
464 TRACE("iface %p, caps %p.\n", iface, caps);
466 if (!caps)
467 return D3DERR_INVALIDCALL;
469 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
470 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
472 wined3d_mutex_lock();
473 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
474 wined3d_mutex_unlock();
476 fixup_caps(wined3d_caps);
477 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
478 HeapFree(GetProcessHeap(), 0, wined3d_caps);
480 return hr;
483 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
485 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
486 struct wined3d_display_mode wined3d_mode;
487 HRESULT hr;
489 TRACE("iface %p, mode %p.\n", iface, mode);
491 wined3d_mutex_lock();
492 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
493 wined3d_mutex_unlock();
495 if (SUCCEEDED(hr))
497 mode->Width = wined3d_mode.width;
498 mode->Height = wined3d_mode.height;
499 mode->RefreshRate = wined3d_mode.refresh_rate;
500 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
503 return hr;
506 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
507 D3DDEVICE_CREATION_PARAMETERS *parameters)
509 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
511 TRACE("iface %p, parameters %p.\n", iface, parameters);
513 wined3d_mutex_lock();
514 wined3d_device_get_creation_parameters(device->wined3d_device,
515 (struct wined3d_device_creation_parameters *)parameters);
516 wined3d_mutex_unlock();
518 return D3D_OK;
521 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
522 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
524 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
525 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
526 HRESULT hr;
528 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
529 iface, hotspot_x, hotspot_y, bitmap);
531 if (!bitmap)
533 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
534 return D3DERR_INVALIDCALL;
537 wined3d_mutex_lock();
538 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
539 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
540 wined3d_mutex_unlock();
542 return hr;
545 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
547 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
549 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
551 wined3d_mutex_lock();
552 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
553 wined3d_mutex_unlock();
556 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
559 BOOL ret;
561 TRACE("iface %p, show %#x.\n", iface, show);
563 wined3d_mutex_lock();
564 ret = wined3d_device_show_cursor(device->wined3d_device, show);
565 wined3d_mutex_unlock();
567 return ret;
570 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
571 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
574 struct wined3d_swapchain_desc desc;
575 struct d3d8_swapchain *object;
576 UINT i, count;
577 HRESULT hr;
579 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
580 iface, present_parameters, swapchain);
582 if (!present_parameters->Windowed)
584 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
585 return D3DERR_INVALIDCALL;
588 wined3d_mutex_lock();
589 count = wined3d_device_get_swapchain_count(device->wined3d_device);
590 for (i = 0; i < count; ++i)
592 struct wined3d_swapchain *wined3d_swapchain;
594 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
595 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
597 if (!desc.windowed)
599 wined3d_mutex_unlock();
600 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
601 return D3DERR_INVALIDCALL;
604 wined3d_mutex_unlock();
606 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
607 return D3DERR_INVALIDCALL;
608 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
609 *swapchain = &object->IDirect3DSwapChain8_iface;
610 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
612 return hr;
615 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
617 struct wined3d_resource_desc desc;
618 IDirect3DBaseTexture8 *texture;
619 struct d3d8_surface *surface;
620 IUnknown *parent;
622 wined3d_resource_get_desc(resource, &desc);
623 if (desc.pool != WINED3D_POOL_DEFAULT)
624 return D3D_OK;
626 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
628 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
629 return D3DERR_DEVICELOST;
632 parent = wined3d_resource_get_parent(resource);
633 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
635 IDirect3DBaseTexture8_Release(texture);
636 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
637 return D3DERR_DEVICELOST;
640 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
641 if (!surface->resource.refcount)
642 return D3D_OK;
644 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
645 return D3DERR_DEVICELOST;
648 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
649 D3DPRESENT_PARAMETERS *present_parameters)
651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
652 struct wined3d_swapchain_desc swapchain_desc;
653 HRESULT hr;
655 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
657 if (device->device_state == D3D8_DEVICE_STATE_LOST)
659 WARN("App not active, returning D3DERR_DEVICELOST.\n");
660 return D3DERR_DEVICELOST;
662 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
663 return D3DERR_INVALIDCALL;
665 wined3d_mutex_lock();
667 if (device->vertex_buffer)
669 wined3d_buffer_decref(device->vertex_buffer);
670 device->vertex_buffer = NULL;
671 device->vertex_buffer_size = 0;
673 if (device->index_buffer)
675 wined3d_buffer_decref(device->index_buffer);
676 device->index_buffer = NULL;
677 device->index_buffer_size = 0;
680 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
681 NULL, reset_enum_callback, TRUE)))
683 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
684 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
685 device->device_state = D3D8_DEVICE_STATE_OK;
687 else
689 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
691 wined3d_mutex_unlock();
693 return hr;
696 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
697 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
699 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
701 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
702 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
704 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
705 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
706 * shows a framerate on Windows in applications that only call the device
707 * method, like e.g. the dx8 sdk samples. The conclusion is that native
708 * calls the swapchain's public method from the device. */
709 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
710 src_rect, dst_rect, dst_window_override, dirty_region);
713 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
714 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
717 struct wined3d_swapchain *wined3d_swapchain;
718 struct wined3d_texture *wined3d_texture;
719 struct d3d8_surface *surface_impl;
721 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
722 iface, backbuffer_idx, backbuffer_type, backbuffer);
724 /* backbuffer_type is ignored by native. */
726 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
727 wined3d_mutex_lock();
729 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
730 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
732 wined3d_mutex_unlock();
733 *backbuffer = NULL;
734 return D3DERR_INVALIDCALL;
737 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
738 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
739 IDirect3DSurface8_AddRef(*backbuffer);
741 wined3d_mutex_unlock();
742 return D3D_OK;
745 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
747 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
748 HRESULT hr;
750 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
752 wined3d_mutex_lock();
753 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
754 wined3d_mutex_unlock();
756 return hr;
759 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
761 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
763 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
765 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
766 wined3d_mutex_lock();
767 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
768 wined3d_mutex_unlock();
771 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
775 TRACE("iface %p, ramp %p.\n", iface, ramp);
777 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
778 wined3d_mutex_lock();
779 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
780 wined3d_mutex_unlock();
783 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
784 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
785 D3DPOOL pool, IDirect3DTexture8 **texture)
787 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
788 struct d3d8_texture *object;
789 HRESULT hr;
791 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
792 iface, width, height, levels, usage, format, pool, texture);
794 *texture = NULL;
795 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
796 if (!object)
797 return D3DERR_OUTOFVIDEOMEMORY;
799 hr = texture_init(object, device, width, height, levels, usage, format, pool);
800 if (FAILED(hr))
802 WARN("Failed to initialize texture, hr %#x.\n", hr);
803 HeapFree(GetProcessHeap(), 0, object);
804 return hr;
807 TRACE("Created texture %p.\n", object);
808 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
810 return D3D_OK;
813 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
814 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
815 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
817 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
818 struct d3d8_texture *object;
819 HRESULT hr;
821 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
822 iface, width, height, depth, levels, usage, format, pool, texture);
824 *texture = NULL;
825 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
826 if (!object)
827 return D3DERR_OUTOFVIDEOMEMORY;
829 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
830 if (FAILED(hr))
832 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
833 HeapFree(GetProcessHeap(), 0, object);
834 return hr;
837 TRACE("Created volume texture %p.\n", object);
838 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
840 return D3D_OK;
843 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
844 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
846 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
847 struct d3d8_texture *object;
848 HRESULT hr;
850 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
851 iface, edge_length, levels, usage, format, pool, texture);
853 *texture = NULL;
854 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
855 if (!object)
856 return D3DERR_OUTOFVIDEOMEMORY;
858 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
859 if (FAILED(hr))
861 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
862 HeapFree(GetProcessHeap(), 0, object);
863 return hr;
866 TRACE("Created cube texture %p.\n", object);
867 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
869 return hr;
872 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
873 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
875 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
876 struct d3d8_vertexbuffer *object;
877 HRESULT hr;
879 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
880 iface, size, usage, fvf, pool, buffer);
882 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
883 if (!object)
884 return D3DERR_OUTOFVIDEOMEMORY;
886 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
887 if (FAILED(hr))
889 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
890 HeapFree(GetProcessHeap(), 0, object);
891 return hr;
894 TRACE("Created vertex buffer %p.\n", object);
895 *buffer = &object->IDirect3DVertexBuffer8_iface;
897 return D3D_OK;
900 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
901 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
903 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
904 struct d3d8_indexbuffer *object;
905 HRESULT hr;
907 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
908 iface, size, usage, format, pool, buffer);
910 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
911 if (!object)
912 return D3DERR_OUTOFVIDEOMEMORY;
914 hr = indexbuffer_init(object, device, size, usage, format, pool);
915 if (FAILED(hr))
917 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
918 HeapFree(GetProcessHeap(), 0, object);
919 return hr;
922 TRACE("Created index buffer %p.\n", object);
923 *buffer = &object->IDirect3DIndexBuffer8_iface;
925 return D3D_OK;
928 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
929 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
930 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
932 struct wined3d_resource_desc desc;
933 struct d3d8_surface *surface_impl;
934 struct wined3d_texture *texture;
935 HRESULT hr;
937 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p, "
938 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
939 device, width, height, format, flags, surface,
940 usage, pool, multisample_type, multisample_quality);
942 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
943 desc.format = wined3dformat_from_d3dformat(format);
944 desc.multisample_type = multisample_type;
945 desc.multisample_quality = multisample_quality;
946 desc.usage = usage & WINED3DUSAGE_MASK;
947 desc.pool = pool;
948 desc.width = width;
949 desc.height = height;
950 desc.depth = 1;
951 desc.size = 0;
953 wined3d_mutex_lock();
955 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
956 1, 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
958 wined3d_mutex_unlock();
959 WARN("Failed to create texture, hr %#x.\n", hr);
960 return hr;
963 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
964 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
965 *surface = &surface_impl->IDirect3DSurface8_iface;
966 IDirect3DSurface8_AddRef(*surface);
967 wined3d_texture_decref(texture);
969 wined3d_mutex_unlock();
971 return D3D_OK;
974 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
975 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
976 IDirect3DSurface8 **surface)
978 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
979 DWORD flags = 0;
981 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
982 iface, width, height, format, multisample_type, lockable, surface);
984 *surface = NULL;
985 if (lockable)
986 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
988 return d3d8_device_create_surface(device, width, height, format, flags, surface,
989 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
992 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
993 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
994 IDirect3DSurface8 **surface)
996 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
998 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
999 iface, width, height, format, multisample_type, surface);
1001 *surface = NULL;
1003 /* TODO: Verify that Discard is false */
1004 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1005 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1008 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1009 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1010 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1012 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1014 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1015 iface, width, height, format, surface);
1017 *surface = NULL;
1019 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1020 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1023 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1024 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1025 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1027 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1028 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1029 enum wined3d_format_id src_format, dst_format;
1030 struct wined3d_sub_resource_desc wined3d_desc;
1031 UINT src_w, src_h;
1033 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1034 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1036 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1037 * destination texture is in WINED3D_POOL_DEFAULT. */
1039 wined3d_mutex_lock();
1040 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1041 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1043 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1044 wined3d_mutex_unlock();
1045 return D3DERR_INVALIDCALL;
1047 src_format = wined3d_desc.format;
1048 src_w = wined3d_desc.width;
1049 src_h = wined3d_desc.height;
1051 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1052 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1054 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1055 wined3d_mutex_unlock();
1056 return D3DERR_INVALIDCALL;
1058 dst_format = wined3d_desc.format;
1060 /* Check that the source and destination formats match */
1061 if (src_format != dst_format)
1063 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1064 src_surface, dst_surface);
1065 wined3d_mutex_unlock();
1066 return D3DERR_INVALIDCALL;
1069 /* Quick if complete copy ... */
1070 if (!rect_count && !src_rects && !dst_points)
1072 RECT rect = {0, 0, src_w, src_h};
1073 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1074 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1076 else
1078 unsigned int i;
1079 /* Copy rect by rect */
1080 if (src_rects && dst_points)
1082 for (i = 0; i < rect_count; ++i)
1084 UINT w = src_rects[i].right - src_rects[i].left;
1085 UINT h = src_rects[i].bottom - src_rects[i].top;
1086 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1087 dst_points[i].x + w, dst_points[i].y + h};
1089 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1090 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1093 else
1095 for (i = 0; i < rect_count; ++i)
1097 UINT w = src_rects[i].right - src_rects[i].left;
1098 UINT h = src_rects[i].bottom - src_rects[i].top;
1099 RECT dst_rect = {0, 0, w, h};
1101 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1102 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1106 wined3d_mutex_unlock();
1108 return WINED3D_OK;
1111 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1112 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1114 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1115 struct d3d8_texture *src_impl, *dst_impl;
1116 HRESULT hr;
1118 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1120 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1121 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1123 wined3d_mutex_lock();
1124 hr = wined3d_device_update_texture(device->wined3d_device,
1125 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1126 wined3d_mutex_unlock();
1128 return hr;
1131 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1133 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1134 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1135 HRESULT hr;
1137 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1139 if (!dst_surface)
1141 WARN("Invalid destination surface passed.\n");
1142 return D3DERR_INVALIDCALL;
1145 wined3d_mutex_lock();
1146 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1147 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1148 wined3d_mutex_unlock();
1150 return hr;
1153 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1154 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1156 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1157 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1158 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1159 struct wined3d_rendertarget_view *original_dsv, *rtv;
1160 HRESULT hr = D3D_OK;
1162 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1164 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1166 WARN("Render target surface does not match device.\n");
1167 return D3DERR_INVALIDCALL;
1170 wined3d_mutex_lock();
1172 if (ds_impl)
1174 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1175 struct wined3d_rendertarget_view *original_rtv;
1176 struct d3d8_surface *original_surface;
1178 /* If no render target is passed in check the size against the current RT */
1179 if (!render_target)
1181 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1183 wined3d_mutex_unlock();
1184 return D3DERR_NOTFOUND;
1186 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1187 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1188 original_surface->sub_resource_idx, &rt_desc);
1190 else
1191 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1192 rt_impl->sub_resource_idx, &rt_desc);
1194 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1196 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1198 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1199 wined3d_mutex_unlock();
1200 return D3DERR_INVALIDCALL;
1202 if (ds_desc.multisample_type != rt_desc.multisample_type
1203 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1205 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1206 wined3d_mutex_unlock();
1207 return D3DERR_INVALIDCALL;
1211 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1212 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1213 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1214 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1215 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1216 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1217 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1218 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1220 wined3d_mutex_unlock();
1222 return hr;
1225 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1227 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1228 struct wined3d_rendertarget_view *wined3d_rtv;
1229 struct d3d8_surface *surface_impl;
1230 HRESULT hr;
1232 TRACE("iface %p, render_target %p.\n", iface, render_target);
1234 if (!render_target)
1235 return D3DERR_INVALIDCALL;
1237 wined3d_mutex_lock();
1238 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1240 /* We want the sub resource parent here, since the view itself may be
1241 * internal to wined3d and may not have a parent. */
1242 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1243 *render_target = &surface_impl->IDirect3DSurface8_iface;
1244 IDirect3DSurface8_AddRef(*render_target);
1245 hr = D3D_OK;
1247 else
1249 ERR("Failed to get wined3d render target.\n");
1250 *render_target = NULL;
1251 hr = D3DERR_NOTFOUND;
1253 wined3d_mutex_unlock();
1255 return hr;
1258 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1260 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1261 struct wined3d_rendertarget_view *wined3d_dsv;
1262 struct d3d8_surface *surface_impl;
1263 HRESULT hr = D3D_OK;
1265 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1267 if (!depth_stencil)
1268 return D3DERR_INVALIDCALL;
1270 wined3d_mutex_lock();
1271 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1273 /* We want the sub resource parent here, since the view itself may be
1274 * internal to wined3d and may not have a parent. */
1275 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1276 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1277 IDirect3DSurface8_AddRef(*depth_stencil);
1279 else
1281 hr = D3DERR_NOTFOUND;
1282 *depth_stencil = NULL;
1284 wined3d_mutex_unlock();
1286 return hr;
1289 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1291 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1292 HRESULT hr;
1294 TRACE("iface %p.\n", iface);
1296 wined3d_mutex_lock();
1297 hr = wined3d_device_begin_scene(device->wined3d_device);
1298 wined3d_mutex_unlock();
1300 return hr;
1303 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1305 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1306 HRESULT hr;
1308 TRACE("iface %p.\n", iface);
1310 wined3d_mutex_lock();
1311 hr = wined3d_device_end_scene(device->wined3d_device);
1312 wined3d_mutex_unlock();
1314 return hr;
1317 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1318 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1320 const struct wined3d_color c =
1322 ((color >> 16) & 0xff) / 255.0f,
1323 ((color >> 8) & 0xff) / 255.0f,
1324 (color & 0xff) / 255.0f,
1325 ((color >> 24) & 0xff) / 255.0f,
1327 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1328 HRESULT hr;
1330 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1331 iface, rect_count, rects, flags, color, z, stencil);
1333 if (rect_count && !rects)
1335 WARN("count %u with NULL rects.\n", rect_count);
1336 rect_count = 0;
1339 wined3d_mutex_lock();
1340 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1341 wined3d_mutex_unlock();
1343 return hr;
1346 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1347 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1351 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1353 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1354 wined3d_mutex_lock();
1355 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1356 wined3d_mutex_unlock();
1358 return D3D_OK;
1361 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1362 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1364 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1366 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1368 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1369 wined3d_mutex_lock();
1370 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1371 wined3d_mutex_unlock();
1373 return D3D_OK;
1376 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1377 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1379 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1381 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1383 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1384 wined3d_mutex_lock();
1385 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1386 wined3d_mutex_unlock();
1388 return D3D_OK;
1391 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1393 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1395 TRACE("iface %p, viewport %p.\n", iface, viewport);
1397 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1398 wined3d_mutex_lock();
1399 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1400 wined3d_mutex_unlock();
1402 return D3D_OK;
1405 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1407 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1409 TRACE("iface %p, viewport %p.\n", iface, viewport);
1411 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1412 wined3d_mutex_lock();
1413 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1414 wined3d_mutex_unlock();
1416 return D3D_OK;
1419 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1421 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1423 TRACE("iface %p, material %p.\n", iface, material);
1425 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1426 wined3d_mutex_lock();
1427 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1428 wined3d_mutex_unlock();
1430 return D3D_OK;
1433 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1435 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1437 TRACE("iface %p, material %p.\n", iface, material);
1439 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1440 wined3d_mutex_lock();
1441 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1442 wined3d_mutex_unlock();
1444 return D3D_OK;
1447 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1449 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1450 HRESULT hr;
1452 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1454 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1455 wined3d_mutex_lock();
1456 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1457 wined3d_mutex_unlock();
1459 return hr;
1462 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1464 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1465 HRESULT hr;
1467 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1469 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1470 wined3d_mutex_lock();
1471 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1472 wined3d_mutex_unlock();
1474 return hr;
1477 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1479 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1480 HRESULT hr;
1482 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1484 wined3d_mutex_lock();
1485 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1486 wined3d_mutex_unlock();
1488 return hr;
1491 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1493 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1494 HRESULT hr;
1496 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1498 wined3d_mutex_lock();
1499 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1500 wined3d_mutex_unlock();
1502 return hr;
1505 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1508 HRESULT hr;
1510 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1512 wined3d_mutex_lock();
1513 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1514 wined3d_mutex_unlock();
1516 return hr;
1519 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1521 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1522 HRESULT hr;
1524 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1526 wined3d_mutex_lock();
1527 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1528 wined3d_mutex_unlock();
1530 return hr;
1533 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1534 D3DRENDERSTATETYPE state, DWORD value)
1536 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1538 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1540 wined3d_mutex_lock();
1541 switch (state)
1543 case D3DRS_ZBIAS:
1544 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1545 break;
1547 default:
1548 wined3d_device_set_render_state(device->wined3d_device, state, value);
1550 wined3d_mutex_unlock();
1552 return D3D_OK;
1555 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1556 D3DRENDERSTATETYPE state, DWORD *value)
1558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1560 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1562 wined3d_mutex_lock();
1563 switch (state)
1565 case D3DRS_ZBIAS:
1566 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1567 break;
1569 default:
1570 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1572 wined3d_mutex_unlock();
1574 return D3D_OK;
1577 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1579 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1580 HRESULT hr;
1582 TRACE("iface %p.\n", iface);
1584 wined3d_mutex_lock();
1585 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1586 wined3d_mutex_unlock();
1588 return hr;
1591 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1593 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1594 struct wined3d_stateblock *stateblock;
1595 HRESULT hr;
1597 TRACE("iface %p, token %p.\n", iface, token);
1599 /* Tell wineD3D to endstateblock before anything else (in case we run out
1600 * of memory later and cause locking problems)
1602 wined3d_mutex_lock();
1603 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1604 if (FAILED(hr))
1606 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1607 wined3d_mutex_unlock();
1608 return hr;
1611 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1612 wined3d_mutex_unlock();
1614 if (*token == D3D8_INVALID_HANDLE)
1616 ERR("Failed to create a handle\n");
1617 wined3d_mutex_lock();
1618 wined3d_stateblock_decref(stateblock);
1619 wined3d_mutex_unlock();
1620 return E_FAIL;
1622 ++*token;
1624 TRACE("Returning %#x (%p).\n", *token, stateblock);
1626 return hr;
1629 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1631 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1632 struct wined3d_stateblock *stateblock;
1634 TRACE("iface %p, token %#x.\n", iface, token);
1636 if (!token)
1637 return D3D_OK;
1639 wined3d_mutex_lock();
1640 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1641 if (!stateblock)
1643 WARN("Invalid handle (%#x) passed.\n", token);
1644 wined3d_mutex_unlock();
1645 return D3DERR_INVALIDCALL;
1647 wined3d_stateblock_apply(stateblock);
1648 wined3d_mutex_unlock();
1650 return D3D_OK;
1653 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1656 struct wined3d_stateblock *stateblock;
1658 TRACE("iface %p, token %#x.\n", iface, token);
1660 wined3d_mutex_lock();
1661 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1662 if (!stateblock)
1664 WARN("Invalid handle (%#x) passed.\n", token);
1665 wined3d_mutex_unlock();
1666 return D3DERR_INVALIDCALL;
1668 wined3d_stateblock_capture(stateblock);
1669 wined3d_mutex_unlock();
1671 return D3D_OK;
1674 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1676 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1677 struct wined3d_stateblock *stateblock;
1679 TRACE("iface %p, token %#x.\n", iface, token);
1681 wined3d_mutex_lock();
1682 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1684 if (!stateblock)
1686 WARN("Invalid handle (%#x) passed.\n", token);
1687 wined3d_mutex_unlock();
1688 return D3DERR_INVALIDCALL;
1691 if (wined3d_stateblock_decref(stateblock))
1693 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1695 wined3d_mutex_unlock();
1697 return D3D_OK;
1700 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1701 D3DSTATEBLOCKTYPE type, DWORD *handle)
1703 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1704 struct wined3d_stateblock *stateblock;
1705 HRESULT hr;
1707 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1709 if (type != D3DSBT_ALL
1710 && type != D3DSBT_PIXELSTATE
1711 && type != D3DSBT_VERTEXSTATE)
1713 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1714 return D3DERR_INVALIDCALL;
1717 wined3d_mutex_lock();
1718 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1719 if (FAILED(hr))
1721 wined3d_mutex_unlock();
1722 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1723 return hr;
1726 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1727 wined3d_mutex_unlock();
1729 if (*handle == D3D8_INVALID_HANDLE)
1731 ERR("Failed to allocate a handle.\n");
1732 wined3d_mutex_lock();
1733 wined3d_stateblock_decref(stateblock);
1734 wined3d_mutex_unlock();
1735 return E_FAIL;
1737 ++*handle;
1739 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1741 return hr;
1744 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1746 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1747 HRESULT hr;
1749 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1750 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1752 wined3d_mutex_lock();
1753 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1754 wined3d_mutex_unlock();
1756 return hr;
1759 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1761 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1762 HRESULT hr;
1764 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1766 wined3d_mutex_lock();
1767 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1768 wined3d_mutex_unlock();
1770 return hr;
1773 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1775 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1776 struct wined3d_texture *wined3d_texture;
1777 struct d3d8_texture *texture_impl;
1779 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1781 if (!texture)
1782 return D3DERR_INVALIDCALL;
1784 wined3d_mutex_lock();
1785 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1787 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1788 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1789 IDirect3DBaseTexture8_AddRef(*texture);
1791 else
1793 *texture = NULL;
1795 wined3d_mutex_unlock();
1797 return D3D_OK;
1800 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1802 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1803 struct d3d8_texture *texture_impl;
1804 HRESULT hr;
1806 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1808 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1810 wined3d_mutex_lock();
1811 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1812 texture_impl ? texture_impl->wined3d_texture : NULL);
1813 wined3d_mutex_unlock();
1815 return hr;
1818 static const struct tss_lookup
1820 BOOL sampler_state;
1821 union
1823 enum wined3d_texture_stage_state texture_state;
1824 enum wined3d_sampler_state sampler_state;
1825 } u;
1827 tss_lookup[] =
1829 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
1830 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
1831 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
1832 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
1833 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
1834 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
1835 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
1836 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
1837 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
1838 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
1839 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
1840 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
1841 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
1842 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
1843 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
1844 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
1845 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
1846 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
1847 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
1848 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
1849 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
1850 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
1851 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
1852 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
1853 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1854 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
1855 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
1856 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
1857 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
1860 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1861 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1863 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1864 const struct tss_lookup *l;
1866 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1868 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1870 WARN("Invalid Type %#x passed.\n", Type);
1871 return D3D_OK;
1874 l = &tss_lookup[Type];
1876 wined3d_mutex_lock();
1877 if (l->sampler_state)
1878 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
1879 else
1880 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
1881 wined3d_mutex_unlock();
1883 return D3D_OK;
1886 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1887 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1889 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1890 const struct tss_lookup *l;
1892 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1894 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1896 WARN("Invalid type %#x passed.\n", type);
1897 return D3D_OK;
1900 l = &tss_lookup[type];
1902 wined3d_mutex_lock();
1903 if (l->sampler_state)
1904 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
1905 else
1906 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
1907 wined3d_mutex_unlock();
1909 return D3D_OK;
1912 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1914 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1915 HRESULT hr;
1917 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1919 wined3d_mutex_lock();
1920 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1921 wined3d_mutex_unlock();
1923 return hr;
1926 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1927 DWORD info_id, void *info, DWORD info_size)
1929 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1931 return D3D_OK;
1934 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1935 UINT palette_idx, const PALETTEENTRY *entries)
1937 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1939 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1940 * does not have a d3d8/9-style palette API */
1942 return D3D_OK;
1945 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1946 UINT palette_idx, PALETTEENTRY *entries)
1948 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1950 return D3DERR_INVALIDCALL;
1953 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1955 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1957 return D3D_OK;
1960 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1962 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1964 return D3DERR_INVALIDCALL;
1967 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1968 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1970 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1971 HRESULT hr;
1973 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1974 iface, primitive_type, start_vertex, primitive_count);
1976 wined3d_mutex_lock();
1977 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1978 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1979 vertex_count_from_primitive_count(primitive_type, primitive_count));
1980 wined3d_mutex_unlock();
1982 return hr;
1985 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1986 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1987 UINT start_idx, UINT primitive_count)
1989 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1990 HRESULT hr;
1992 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1993 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1995 wined3d_mutex_lock();
1996 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1997 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1998 vertex_count_from_primitive_count(primitive_type, primitive_count));
1999 wined3d_mutex_unlock();
2001 return hr;
2004 /* The caller is responsible for wined3d locking */
2005 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2007 HRESULT hr;
2009 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2011 UINT size = max(device->vertex_buffer_size * 2, min_size);
2012 struct wined3d_buffer *buffer;
2014 TRACE("Growing vertex buffer to %u bytes\n", size);
2016 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2017 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2018 if (FAILED(hr))
2020 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2021 return hr;
2024 if (device->vertex_buffer)
2025 wined3d_buffer_decref(device->vertex_buffer);
2027 device->vertex_buffer = buffer;
2028 device->vertex_buffer_size = size;
2029 device->vertex_buffer_pos = 0;
2031 return D3D_OK;
2034 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2035 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2036 UINT stride)
2038 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2039 HRESULT hr;
2040 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2041 struct wined3d_map_desc wined3d_map_desc;
2042 struct wined3d_box wined3d_box = {0};
2043 UINT size = vtx_count * stride;
2044 struct wined3d_resource *vb;
2045 UINT vb_pos, align;
2047 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2048 iface, primitive_type, primitive_count, data, stride);
2050 if (!primitive_count)
2052 WARN("primitive_count is 0, returning D3D_OK\n");
2053 return D3D_OK;
2056 wined3d_mutex_lock();
2057 hr = d3d8_device_prepare_vertex_buffer(device, size);
2058 if (FAILED(hr))
2059 goto done;
2061 vb_pos = device->vertex_buffer_pos;
2062 align = vb_pos % stride;
2063 if (align) align = stride - align;
2064 if (vb_pos + size + align > device->vertex_buffer_size)
2065 vb_pos = 0;
2066 else
2067 vb_pos += align;
2069 wined3d_box.left = vb_pos;
2070 wined3d_box.right = vb_pos + size;
2071 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2072 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2073 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2074 goto done;
2075 memcpy(wined3d_map_desc.data, data, size);
2076 wined3d_resource_unmap(vb, 0);
2077 device->vertex_buffer_pos = vb_pos + size;
2079 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2080 if (FAILED(hr))
2081 goto done;
2083 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2084 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2085 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2087 done:
2088 wined3d_mutex_unlock();
2089 return hr;
2092 /* The caller is responsible for wined3d locking */
2093 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2095 HRESULT hr;
2097 if (device->index_buffer_size < min_size || !device->index_buffer)
2099 UINT size = max(device->index_buffer_size * 2, min_size);
2100 struct wined3d_buffer *buffer;
2102 TRACE("Growing index buffer to %u bytes\n", size);
2104 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2105 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2106 if (FAILED(hr))
2108 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2109 return hr;
2112 if (device->index_buffer)
2113 wined3d_buffer_decref(device->index_buffer);
2115 device->index_buffer = buffer;
2116 device->index_buffer_size = size;
2117 device->index_buffer_pos = 0;
2119 return D3D_OK;
2122 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2123 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2124 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2125 const void *vertex_data, UINT vertex_stride)
2127 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2128 HRESULT hr;
2129 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2130 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2131 UINT idx_size = idx_count * idx_fmt_size;
2132 struct wined3d_map_desc wined3d_map_desc;
2133 struct wined3d_box wined3d_box = {0};
2134 struct wined3d_resource *ib, *vb;
2135 UINT ib_pos;
2137 UINT vtx_size = vertex_count * vertex_stride;
2138 UINT vb_pos, align;
2140 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2141 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2142 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2143 index_data, index_format, vertex_data, vertex_stride);
2145 if (!primitive_count)
2147 WARN("primitive_count is 0, returning D3D_OK\n");
2148 return D3D_OK;
2151 wined3d_mutex_lock();
2153 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2154 if (FAILED(hr))
2155 goto done;
2157 vb_pos = device->vertex_buffer_pos;
2158 align = vb_pos % vertex_stride;
2159 if (align) align = vertex_stride - align;
2160 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2161 vb_pos = 0;
2162 else
2163 vb_pos += align;
2165 wined3d_box.left = vb_pos;
2166 wined3d_box.right = vb_pos + vtx_size;
2167 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2168 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2169 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2170 goto done;
2171 memcpy(wined3d_map_desc.data, vertex_data, vtx_size);
2172 wined3d_resource_unmap(vb, 0);
2173 device->vertex_buffer_pos = vb_pos + vtx_size;
2175 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2176 if (FAILED(hr))
2177 goto done;
2179 ib_pos = device->index_buffer_pos;
2180 align = ib_pos % idx_fmt_size;
2181 if (align) align = idx_fmt_size - align;
2182 if (ib_pos + idx_size + align > device->index_buffer_size)
2183 ib_pos = 0;
2184 else
2185 ib_pos += align;
2187 wined3d_box.left = ib_pos;
2188 wined3d_box.right = ib_pos + idx_size;
2189 ib = wined3d_buffer_get_resource(device->index_buffer);
2190 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2191 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2192 goto done;
2193 memcpy(wined3d_map_desc.data, index_data, idx_size);
2194 wined3d_resource_unmap(ib, 0);
2195 device->index_buffer_pos = ib_pos + idx_size;
2197 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2198 if (FAILED(hr))
2199 goto done;
2201 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2202 wined3dformat_from_d3dformat(index_format), 0);
2203 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2205 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2206 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2208 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2209 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2210 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2212 done:
2213 wined3d_mutex_unlock();
2214 return hr;
2217 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2218 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2220 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2221 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2222 HRESULT hr;
2224 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2225 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2227 wined3d_mutex_lock();
2228 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2229 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2230 wined3d_mutex_unlock();
2232 return hr;
2235 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2236 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2238 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2239 struct d3d8_vertex_shader *object;
2240 DWORD shader_handle;
2241 DWORD handle;
2242 HRESULT hr;
2244 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2245 iface, declaration, byte_code, shader, usage);
2247 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2248 if (!object)
2250 *shader = 0;
2251 return E_OUTOFMEMORY;
2254 wined3d_mutex_lock();
2255 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2256 wined3d_mutex_unlock();
2257 if (handle == D3D8_INVALID_HANDLE)
2259 ERR("Failed to allocate vertex shader handle.\n");
2260 HeapFree(GetProcessHeap(), 0, object);
2261 *shader = 0;
2262 return E_OUTOFMEMORY;
2265 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2267 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2268 if (FAILED(hr))
2270 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2271 wined3d_mutex_lock();
2272 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2273 wined3d_mutex_unlock();
2274 HeapFree(GetProcessHeap(), 0, object);
2275 *shader = 0;
2276 return hr;
2279 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2280 *shader = shader_handle;
2282 return D3D_OK;
2285 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2287 struct d3d8_vertex_declaration *d3d8_declaration;
2288 struct FvfToDecl *convertedDecls = device->decls;
2289 int p, low, high; /* deliberately signed */
2290 HRESULT hr;
2292 TRACE("Searching for declaration for fvf %08x... ", fvf);
2294 low = 0;
2295 high = device->numConvertedDecls - 1;
2296 while (low <= high)
2298 p = (low + high) >> 1;
2299 TRACE("%d ", p);
2301 if (convertedDecls[p].fvf == fvf)
2303 TRACE("found %p\n", convertedDecls[p].declaration);
2304 return convertedDecls[p].declaration;
2307 if (convertedDecls[p].fvf < fvf)
2308 low = p + 1;
2309 else
2310 high = p - 1;
2312 TRACE("not found. Creating and inserting at position %d.\n", low);
2314 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2315 return NULL;
2317 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2319 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2320 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2321 return NULL;
2324 if (device->declArraySize == device->numConvertedDecls)
2326 UINT grow = device->declArraySize / 2;
2328 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2329 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2330 if (!convertedDecls)
2332 d3d8_vertex_declaration_destroy(d3d8_declaration);
2333 return NULL;
2335 device->decls = convertedDecls;
2336 device->declArraySize += grow;
2339 memmove(convertedDecls + low + 1, convertedDecls + low,
2340 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2341 convertedDecls[low].declaration = d3d8_declaration;
2342 convertedDecls[low].fvf = fvf;
2343 ++device->numConvertedDecls;
2345 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2347 return d3d8_declaration;
2350 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2352 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2353 struct d3d8_vertex_shader *shader_impl;
2355 TRACE("iface %p, shader %#x.\n", iface, shader);
2357 if (VS_HIGHESTFIXEDFXF >= shader)
2359 TRACE("Setting FVF, %#x\n", shader);
2361 wined3d_mutex_lock();
2362 wined3d_device_set_vertex_declaration(device->wined3d_device,
2363 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2364 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2365 wined3d_mutex_unlock();
2367 return D3D_OK;
2370 TRACE("Setting shader\n");
2372 wined3d_mutex_lock();
2373 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2375 WARN("Invalid handle (%#x) passed.\n", shader);
2376 wined3d_mutex_unlock();
2378 return D3DERR_INVALIDCALL;
2381 wined3d_device_set_vertex_declaration(device->wined3d_device,
2382 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2383 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2384 wined3d_mutex_unlock();
2386 return D3D_OK;
2389 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2391 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2392 struct wined3d_vertex_declaration *wined3d_declaration;
2393 struct d3d8_vertex_declaration *d3d8_declaration;
2395 TRACE("iface %p, shader %p.\n", iface, shader);
2397 wined3d_mutex_lock();
2398 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2400 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2401 *shader = d3d8_declaration->shader_handle;
2403 else
2405 *shader = 0;
2407 wined3d_mutex_unlock();
2409 TRACE("Returning %#x.\n", *shader);
2411 return D3D_OK;
2414 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2417 struct d3d8_vertex_shader *shader_impl;
2419 TRACE("iface %p, shader %#x.\n", iface, shader);
2421 wined3d_mutex_lock();
2422 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2424 WARN("Invalid handle (%#x) passed.\n", shader);
2425 wined3d_mutex_unlock();
2427 return D3DERR_INVALIDCALL;
2430 if (shader_impl->wined3d_shader
2431 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2432 IDirect3DDevice8_SetVertexShader(iface, 0);
2434 wined3d_mutex_unlock();
2436 d3d8_vertex_shader_destroy(shader_impl);
2438 return D3D_OK;
2441 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2442 DWORD start_register, const void *data, DWORD count)
2444 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2445 HRESULT hr;
2447 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2448 iface, start_register, data, count);
2450 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2452 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2453 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2454 return D3DERR_INVALIDCALL;
2457 wined3d_mutex_lock();
2458 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2459 wined3d_mutex_unlock();
2461 return hr;
2464 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2465 DWORD start_register, void *data, DWORD count)
2467 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2468 HRESULT hr;
2470 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2471 iface, start_register, data, count);
2473 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2475 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2476 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2477 return D3DERR_INVALIDCALL;
2480 wined3d_mutex_lock();
2481 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2482 wined3d_mutex_unlock();
2484 return hr;
2487 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2488 DWORD shader, void *data, DWORD *data_size)
2490 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2491 struct d3d8_vertex_declaration *declaration;
2492 struct d3d8_vertex_shader *shader_impl;
2494 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2495 iface, shader, data, data_size);
2497 wined3d_mutex_lock();
2498 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2499 wined3d_mutex_unlock();
2501 if (!shader_impl)
2503 WARN("Invalid handle (%#x) passed.\n", shader);
2504 return D3DERR_INVALIDCALL;
2506 declaration = shader_impl->vertex_declaration;
2508 if (!data)
2510 *data_size = declaration->elements_size;
2511 return D3D_OK;
2514 /* MSDN claims that if *data_size is smaller than the required size
2515 * we should write the required size and return D3DERR_MOREDATA.
2516 * That's not actually true. */
2517 if (*data_size < declaration->elements_size)
2518 return D3DERR_INVALIDCALL;
2520 memcpy(data, declaration->elements, declaration->elements_size);
2522 return D3D_OK;
2525 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2526 DWORD shader, void *data, DWORD *data_size)
2528 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2529 struct d3d8_vertex_shader *shader_impl = NULL;
2530 HRESULT hr;
2532 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2533 iface, shader, data, data_size);
2535 wined3d_mutex_lock();
2536 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2538 WARN("Invalid handle (%#x) passed.\n", shader);
2539 wined3d_mutex_unlock();
2541 return D3DERR_INVALIDCALL;
2544 if (!shader_impl->wined3d_shader)
2546 wined3d_mutex_unlock();
2547 *data_size = 0;
2548 return D3D_OK;
2551 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2552 wined3d_mutex_unlock();
2554 return hr;
2557 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2558 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2560 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2561 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2563 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2565 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2566 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2567 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2568 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2569 * problem)
2571 wined3d_mutex_lock();
2572 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2573 wined3d_device_set_index_buffer(device->wined3d_device,
2574 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2575 wined3d_mutex_unlock();
2577 return D3D_OK;
2580 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2581 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2583 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2584 enum wined3d_format_id wined3d_format;
2585 struct wined3d_buffer *wined3d_buffer;
2586 struct d3d8_indexbuffer *buffer_impl;
2588 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2590 if (!buffer)
2591 return D3DERR_INVALIDCALL;
2593 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2594 wined3d_mutex_lock();
2595 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2596 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2598 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2599 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2600 IDirect3DIndexBuffer8_AddRef(*buffer);
2602 else
2604 *buffer = NULL;
2606 wined3d_mutex_unlock();
2608 return D3D_OK;
2611 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2612 const DWORD *byte_code, DWORD *shader)
2614 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2615 struct d3d8_pixel_shader *object;
2616 DWORD shader_handle;
2617 DWORD handle;
2618 HRESULT hr;
2620 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2622 if (!shader)
2623 return D3DERR_INVALIDCALL;
2625 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2626 if (!object)
2627 return E_OUTOFMEMORY;
2629 wined3d_mutex_lock();
2630 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2631 wined3d_mutex_unlock();
2632 if (handle == D3D8_INVALID_HANDLE)
2634 ERR("Failed to allocate pixel shader handle.\n");
2635 HeapFree(GetProcessHeap(), 0, object);
2636 return E_OUTOFMEMORY;
2639 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2641 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2642 if (FAILED(hr))
2644 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2645 wined3d_mutex_lock();
2646 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2647 wined3d_mutex_unlock();
2648 HeapFree(GetProcessHeap(), 0, object);
2649 *shader = 0;
2650 return hr;
2653 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2654 *shader = shader_handle;
2656 return D3D_OK;
2659 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2661 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2662 struct d3d8_pixel_shader *shader_impl;
2664 TRACE("iface %p, shader %#x.\n", iface, shader);
2666 wined3d_mutex_lock();
2668 if (!shader)
2670 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2671 wined3d_mutex_unlock();
2672 return D3D_OK;
2675 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2677 WARN("Invalid handle (%#x) passed.\n", shader);
2678 wined3d_mutex_unlock();
2679 return D3DERR_INVALIDCALL;
2682 TRACE("Setting shader %p.\n", shader_impl);
2683 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2684 wined3d_mutex_unlock();
2686 return D3D_OK;
2689 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2691 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2692 struct wined3d_shader *object;
2694 TRACE("iface %p, shader %p.\n", iface, shader);
2696 if (!shader)
2697 return D3DERR_INVALIDCALL;
2699 wined3d_mutex_lock();
2700 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2702 struct d3d8_pixel_shader *d3d8_shader;
2703 d3d8_shader = wined3d_shader_get_parent(object);
2704 *shader = d3d8_shader->handle;
2706 else
2708 *shader = 0;
2710 wined3d_mutex_unlock();
2712 TRACE("Returning %#x.\n", *shader);
2714 return D3D_OK;
2717 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2719 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2720 struct d3d8_pixel_shader *shader_impl;
2722 TRACE("iface %p, shader %#x.\n", iface, shader);
2724 wined3d_mutex_lock();
2726 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2728 WARN("Invalid handle (%#x) passed.\n", shader);
2729 wined3d_mutex_unlock();
2730 return D3DERR_INVALIDCALL;
2733 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2734 IDirect3DDevice8_SetPixelShader(iface, 0);
2736 wined3d_mutex_unlock();
2738 d3d8_pixel_shader_destroy(shader_impl);
2740 return D3D_OK;
2743 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2744 DWORD start_register, const void *data, DWORD count)
2746 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2747 HRESULT hr;
2749 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2750 iface, start_register, data, count);
2752 wined3d_mutex_lock();
2753 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
2754 wined3d_mutex_unlock();
2756 return hr;
2759 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2760 DWORD start_register, void *data, DWORD count)
2762 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2763 HRESULT hr;
2765 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2766 iface, start_register, data, count);
2768 wined3d_mutex_lock();
2769 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
2770 wined3d_mutex_unlock();
2772 return hr;
2775 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2776 DWORD shader, void *data, DWORD *data_size)
2778 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2779 struct d3d8_pixel_shader *shader_impl = NULL;
2780 HRESULT hr;
2782 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2783 iface, shader, data, data_size);
2785 wined3d_mutex_lock();
2786 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2788 WARN("Invalid handle (%#x) passed.\n", shader);
2789 wined3d_mutex_unlock();
2791 return D3DERR_INVALIDCALL;
2794 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2795 wined3d_mutex_unlock();
2797 return hr;
2800 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2801 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2803 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2804 iface, handle, segment_count, patch_info);
2805 return D3D_OK;
2808 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2809 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2811 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2812 iface, handle, segment_count, patch_info);
2813 return D3D_OK;
2816 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2818 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2819 return D3DERR_INVALIDCALL;
2822 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2823 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2825 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2826 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2827 HRESULT hr;
2829 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2830 iface, stream_idx, buffer, stride);
2832 wined3d_mutex_lock();
2833 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2834 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2835 wined3d_mutex_unlock();
2837 return hr;
2840 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2841 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2843 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2844 struct d3d8_vertexbuffer *buffer_impl;
2845 struct wined3d_buffer *wined3d_buffer = NULL;
2846 HRESULT hr;
2848 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2849 iface, stream_idx, buffer, stride);
2851 if (!buffer)
2852 return D3DERR_INVALIDCALL;
2854 wined3d_mutex_lock();
2855 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2856 if (SUCCEEDED(hr) && wined3d_buffer)
2858 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2859 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2860 IDirect3DVertexBuffer8_AddRef(*buffer);
2862 else
2864 if (FAILED(hr))
2865 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2866 *buffer = NULL;
2868 wined3d_mutex_unlock();
2870 return hr;
2873 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2875 d3d8_device_QueryInterface,
2876 d3d8_device_AddRef,
2877 d3d8_device_Release,
2878 d3d8_device_TestCooperativeLevel,
2879 d3d8_device_GetAvailableTextureMem,
2880 d3d8_device_ResourceManagerDiscardBytes,
2881 d3d8_device_GetDirect3D,
2882 d3d8_device_GetDeviceCaps,
2883 d3d8_device_GetDisplayMode,
2884 d3d8_device_GetCreationParameters,
2885 d3d8_device_SetCursorProperties,
2886 d3d8_device_SetCursorPosition,
2887 d3d8_device_ShowCursor,
2888 d3d8_device_CreateAdditionalSwapChain,
2889 d3d8_device_Reset,
2890 d3d8_device_Present,
2891 d3d8_device_GetBackBuffer,
2892 d3d8_device_GetRasterStatus,
2893 d3d8_device_SetGammaRamp,
2894 d3d8_device_GetGammaRamp,
2895 d3d8_device_CreateTexture,
2896 d3d8_device_CreateVolumeTexture,
2897 d3d8_device_CreateCubeTexture,
2898 d3d8_device_CreateVertexBuffer,
2899 d3d8_device_CreateIndexBuffer,
2900 d3d8_device_CreateRenderTarget,
2901 d3d8_device_CreateDepthStencilSurface,
2902 d3d8_device_CreateImageSurface,
2903 d3d8_device_CopyRects,
2904 d3d8_device_UpdateTexture,
2905 d3d8_device_GetFrontBuffer,
2906 d3d8_device_SetRenderTarget,
2907 d3d8_device_GetRenderTarget,
2908 d3d8_device_GetDepthStencilSurface,
2909 d3d8_device_BeginScene,
2910 d3d8_device_EndScene,
2911 d3d8_device_Clear,
2912 d3d8_device_SetTransform,
2913 d3d8_device_GetTransform,
2914 d3d8_device_MultiplyTransform,
2915 d3d8_device_SetViewport,
2916 d3d8_device_GetViewport,
2917 d3d8_device_SetMaterial,
2918 d3d8_device_GetMaterial,
2919 d3d8_device_SetLight,
2920 d3d8_device_GetLight,
2921 d3d8_device_LightEnable,
2922 d3d8_device_GetLightEnable,
2923 d3d8_device_SetClipPlane,
2924 d3d8_device_GetClipPlane,
2925 d3d8_device_SetRenderState,
2926 d3d8_device_GetRenderState,
2927 d3d8_device_BeginStateBlock,
2928 d3d8_device_EndStateBlock,
2929 d3d8_device_ApplyStateBlock,
2930 d3d8_device_CaptureStateBlock,
2931 d3d8_device_DeleteStateBlock,
2932 d3d8_device_CreateStateBlock,
2933 d3d8_device_SetClipStatus,
2934 d3d8_device_GetClipStatus,
2935 d3d8_device_GetTexture,
2936 d3d8_device_SetTexture,
2937 d3d8_device_GetTextureStageState,
2938 d3d8_device_SetTextureStageState,
2939 d3d8_device_ValidateDevice,
2940 d3d8_device_GetInfo,
2941 d3d8_device_SetPaletteEntries,
2942 d3d8_device_GetPaletteEntries,
2943 d3d8_device_SetCurrentTexturePalette,
2944 d3d8_device_GetCurrentTexturePalette,
2945 d3d8_device_DrawPrimitive,
2946 d3d8_device_DrawIndexedPrimitive,
2947 d3d8_device_DrawPrimitiveUP,
2948 d3d8_device_DrawIndexedPrimitiveUP,
2949 d3d8_device_ProcessVertices,
2950 d3d8_device_CreateVertexShader,
2951 d3d8_device_SetVertexShader,
2952 d3d8_device_GetVertexShader,
2953 d3d8_device_DeleteVertexShader,
2954 d3d8_device_SetVertexShaderConstant,
2955 d3d8_device_GetVertexShaderConstant,
2956 d3d8_device_GetVertexShaderDeclaration,
2957 d3d8_device_GetVertexShaderFunction,
2958 d3d8_device_SetStreamSource,
2959 d3d8_device_GetStreamSource,
2960 d3d8_device_SetIndices,
2961 d3d8_device_GetIndices,
2962 d3d8_device_CreatePixelShader,
2963 d3d8_device_SetPixelShader,
2964 d3d8_device_GetPixelShader,
2965 d3d8_device_DeletePixelShader,
2966 d3d8_device_SetPixelShaderConstant,
2967 d3d8_device_GetPixelShaderConstant,
2968 d3d8_device_GetPixelShaderFunction,
2969 d3d8_device_DrawRectPatch,
2970 d3d8_device_DrawTriPatch,
2971 d3d8_device_DeletePatch,
2974 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2976 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2979 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2980 struct wined3d_device *device)
2982 TRACE("device_parent %p, device %p\n", device_parent, device);
2985 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2987 TRACE("device_parent %p.\n", device_parent);
2990 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2992 struct d3d8_device *device = device_from_device_parent(device_parent);
2994 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2996 if (!activate)
2997 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2998 else
2999 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3002 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3003 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3004 void **parent, const struct wined3d_parent_ops **parent_ops)
3006 struct d3d8_surface *d3d_surface;
3008 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3009 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3011 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3012 return E_OUTOFMEMORY;
3014 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3015 *parent = d3d_surface;
3016 TRACE("Created surface %p.\n", d3d_surface);
3018 return D3D_OK;
3021 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3022 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3023 void **parent, const struct wined3d_parent_ops **parent_ops)
3025 struct d3d8_volume *d3d_volume;
3027 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3028 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3030 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3031 return E_OUTOFMEMORY;
3033 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3034 *parent = d3d_volume;
3035 TRACE("Created volume %p.\n", d3d_volume);
3037 return D3D_OK;
3040 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3041 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3043 struct d3d8_device *device = device_from_device_parent(device_parent);
3044 struct d3d8_surface *d3d_surface;
3045 HRESULT hr;
3047 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3048 device_parent, container_parent, desc, texture);
3050 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
3051 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3053 WARN("Failed to create texture, hr %#x.\n", hr);
3054 return hr;
3057 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3058 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3060 return hr;
3063 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3064 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3066 struct d3d8_device *device = device_from_device_parent(device_parent);
3067 struct d3d8_swapchain *d3d_swapchain;
3068 HRESULT hr;
3070 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3072 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3074 WARN("Failed to create swapchain, hr %#x.\n", hr);
3075 *swapchain = NULL;
3076 return hr;
3079 *swapchain = d3d_swapchain->wined3d_swapchain;
3080 wined3d_swapchain_incref(*swapchain);
3081 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3083 return hr;
3086 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3088 device_parent_wined3d_device_created,
3089 device_parent_mode_changed,
3090 device_parent_activate,
3091 device_parent_surface_created,
3092 device_parent_volume_created,
3093 device_parent_create_swapchain_texture,
3094 device_parent_create_swapchain,
3097 static void setup_fpu(void)
3099 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3100 WORD cw;
3101 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3102 cw = (cw & ~0xf3f) | 0x3f;
3103 __asm__ volatile ("fldcw %0" : : "m" (cw));
3104 #elif defined(__i386__) && defined(_MSC_VER)
3105 WORD cw;
3106 __asm fnstcw cw;
3107 cw = (cw & ~0xf3f) | 0x3f;
3108 __asm fldcw cw;
3109 #else
3110 FIXME("FPU setup not implemented for this platform.\n");
3111 #endif
3114 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3115 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3117 struct wined3d_swapchain_desc swapchain_desc;
3118 struct wined3d_swapchain *wined3d_swapchain;
3119 HRESULT hr;
3121 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3122 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3123 device->ref = 1;
3124 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3125 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3126 if (!device->handle_table.entries)
3128 ERR("Failed to allocate handle table memory.\n");
3129 return E_OUTOFMEMORY;
3131 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3133 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3135 wined3d_mutex_lock();
3136 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3137 &device->device_parent, &device->wined3d_device);
3138 if (FAILED(hr))
3140 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3141 wined3d_mutex_unlock();
3142 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3143 return hr;
3146 if (!parameters->Windowed)
3148 HWND device_window = parameters->hDeviceWindow;
3150 if (!focus_window)
3151 focus_window = device_window;
3152 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3154 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3155 wined3d_device_decref(device->wined3d_device);
3156 wined3d_mutex_unlock();
3157 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3158 return hr;
3161 if (!device_window)
3162 device_window = focus_window;
3163 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3164 parameters->BackBufferWidth,
3165 parameters->BackBufferHeight);
3168 if (flags & D3DCREATE_MULTITHREADED)
3169 wined3d_device_set_multithreaded(device->wined3d_device);
3171 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3173 wined3d_device_release_focus_window(device->wined3d_device);
3174 wined3d_device_decref(device->wined3d_device);
3175 wined3d_mutex_unlock();
3176 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3177 return D3DERR_INVALIDCALL;
3180 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3181 if (FAILED(hr))
3183 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3184 wined3d_device_release_focus_window(device->wined3d_device);
3185 wined3d_device_decref(device->wined3d_device);
3186 wined3d_mutex_unlock();
3187 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3188 return hr;
3191 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3192 wined3d_mutex_unlock();
3194 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3196 device->declArraySize = 16;
3197 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3198 if (!device->decls)
3200 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3201 hr = E_OUTOFMEMORY;
3202 goto err;
3205 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3206 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3208 device->d3d_parent = &parent->IDirect3D8_iface;
3209 IDirect3D8_AddRef(device->d3d_parent);
3211 return D3D_OK;
3213 err:
3214 wined3d_mutex_lock();
3215 wined3d_device_uninit_3d(device->wined3d_device);
3216 wined3d_device_release_focus_window(device->wined3d_device);
3217 wined3d_device_decref(device->wined3d_device);
3218 wined3d_mutex_unlock();
3219 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3220 return hr;