TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / d3d8 / device.c
blob6c942432c6b445efc998b842537c58a2f4840ba9
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "wine/debug.h"
33 #include "d3d8_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
37 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
39 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
41 d3d8_null_wined3d_object_destroyed,
44 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
46 BYTE *c = (BYTE *)&format;
48 /* Don't translate FOURCC formats */
49 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
51 switch(format)
53 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
54 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
55 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
56 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
57 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
58 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
59 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
60 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
61 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
62 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
63 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
64 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
65 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
66 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
67 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
68 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
69 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
70 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
71 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
72 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
73 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
74 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
75 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
76 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
77 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
78 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
79 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
80 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
81 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
82 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
83 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
84 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
85 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
86 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
87 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
88 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
89 default:
90 FIXME("Unhandled wined3d format %#x.\n", format);
91 return D3DFMT_UNKNOWN;
95 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
97 BYTE *c = (BYTE *)&format;
99 /* Don't translate FOURCC formats */
100 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
102 switch(format)
104 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
105 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
106 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
107 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
108 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
109 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
110 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
111 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
112 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
113 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
114 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
115 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
116 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
117 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
119 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
120 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
121 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
122 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
123 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
124 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
125 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
126 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
127 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
128 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
129 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
130 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
131 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
132 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
133 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
134 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
135 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
136 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
137 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
138 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
139 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
140 default:
141 FIXME("Unhandled D3DFORMAT %#x\n", format);
142 return WINED3DFMT_UNKNOWN;
146 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
148 switch(primitive_type)
150 case D3DPT_POINTLIST:
151 return primitive_count;
153 case D3DPT_LINELIST:
154 return primitive_count * 2;
156 case D3DPT_LINESTRIP:
157 return primitive_count + 1;
159 case D3DPT_TRIANGLELIST:
160 return primitive_count * 3;
162 case D3DPT_TRIANGLESTRIP:
163 case D3DPT_TRIANGLEFAN:
164 return primitive_count + 2;
166 default:
167 FIXME("Unhandled primitive type %#x\n", primitive_type);
168 return 0;
172 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
173 const struct wined3d_swapchain_desc *swapchain_desc)
175 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
176 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
177 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
178 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
179 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
180 present_parameters->SwapEffect = swapchain_desc->swap_effect;
181 present_parameters->hDeviceWindow = swapchain_desc->device_window;
182 present_parameters->Windowed = swapchain_desc->windowed;
183 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
184 present_parameters->AutoDepthStencilFormat
185 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
186 present_parameters->Flags = swapchain_desc->flags;
187 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
188 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
191 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
192 const D3DPRESENT_PARAMETERS *present_parameters)
194 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
196 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
197 return FALSE;
199 if (present_parameters->BackBufferCount > 3
200 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
201 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
202 && present_parameters->BackBufferCount > 1))
204 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
205 return FALSE;
208 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
209 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
210 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
211 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
212 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
213 swapchain_desc->multisample_quality = 0; /* d3d9 only */
214 swapchain_desc->swap_effect = present_parameters->SwapEffect;
215 swapchain_desc->device_window = present_parameters->hDeviceWindow;
216 swapchain_desc->windowed = present_parameters->Windowed;
217 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
218 swapchain_desc->auto_depth_stencil_format
219 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
220 swapchain_desc->flags = present_parameters->Flags;
221 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
222 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
223 swapchain_desc->auto_restore_display_mode = TRUE;
225 return TRUE;
228 /* Handle table functions */
229 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
231 struct d3d8_handle_entry *entry;
233 if (t->free_entries)
235 DWORD index = t->free_entries - t->entries;
236 /* Use a free handle */
237 entry = t->free_entries;
238 if (entry->type != D3D8_HANDLE_FREE)
240 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
241 return D3D8_INVALID_HANDLE;
243 t->free_entries = entry->object;
244 entry->object = object;
245 entry->type = type;
247 return index;
250 if (!(t->entry_count < t->table_size))
252 /* Grow the table */
253 UINT new_size = t->table_size + (t->table_size >> 1);
254 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
255 0, t->entries, new_size * sizeof(*t->entries));
256 if (!new_entries)
258 ERR("Failed to grow the handle table.\n");
259 return D3D8_INVALID_HANDLE;
261 t->entries = new_entries;
262 t->table_size = new_size;
265 entry = &t->entries[t->entry_count];
266 entry->object = object;
267 entry->type = type;
269 return t->entry_count++;
272 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
274 struct d3d8_handle_entry *entry;
275 void *object;
277 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
279 WARN("Invalid handle %u passed.\n", handle);
280 return NULL;
283 entry = &t->entries[handle];
284 if (entry->type != type)
286 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
287 return NULL;
290 object = entry->object;
291 entry->object = t->free_entries;
292 entry->type = D3D8_HANDLE_FREE;
293 t->free_entries = entry;
295 return object;
298 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
300 struct d3d8_handle_entry *entry;
302 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
304 WARN("Invalid handle %u passed.\n", handle);
305 return NULL;
308 entry = &t->entries[handle];
309 if (entry->type != type)
311 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
312 return NULL;
315 return entry->object;
318 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
320 TRACE("iface %p, riid %s, out %p.\n",
321 iface, debugstr_guid(riid), out);
323 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
324 || IsEqualGUID(riid, &IID_IUnknown))
326 IDirect3DDevice8_AddRef(iface);
327 *out = iface;
328 return S_OK;
331 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
333 *out = NULL;
334 return E_NOINTERFACE;
337 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
339 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
340 ULONG ref = InterlockedIncrement(&device->ref);
342 TRACE("%p increasing refcount to %u.\n", iface, ref);
344 return ref;
347 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
350 ULONG ref;
352 if (device->inDestruction)
353 return 0;
355 ref = InterlockedDecrement(&device->ref);
357 TRACE("%p decreasing refcount to %u.\n", iface, ref);
359 if (!ref)
361 IDirect3D8 *parent = device->d3d_parent;
362 unsigned i;
364 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
366 wined3d_mutex_lock();
368 device->inDestruction = TRUE;
370 for (i = 0; i < device->numConvertedDecls; ++i)
372 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
374 HeapFree(GetProcessHeap(), 0, device->decls);
376 if (device->vertex_buffer)
377 wined3d_buffer_decref(device->vertex_buffer);
378 if (device->index_buffer)
379 wined3d_buffer_decref(device->index_buffer);
381 wined3d_device_uninit_3d(device->wined3d_device);
382 wined3d_device_release_focus_window(device->wined3d_device);
383 wined3d_device_decref(device->wined3d_device);
384 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
385 HeapFree(GetProcessHeap(), 0, device);
387 wined3d_mutex_unlock();
389 IDirect3D8_Release(parent);
391 return ref;
394 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
396 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
398 TRACE("iface %p.\n", iface);
400 TRACE("device state: %#x.\n", device->device_state);
402 switch (device->device_state)
404 default:
405 case D3D8_DEVICE_STATE_OK:
406 return D3D_OK;
407 case D3D8_DEVICE_STATE_LOST:
408 return D3DERR_DEVICELOST;
409 case D3D8_DEVICE_STATE_NOT_RESET:
410 return D3DERR_DEVICENOTRESET;
414 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
417 UINT ret;
419 TRACE("iface %p.\n", iface);
421 wined3d_mutex_lock();
422 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
423 wined3d_mutex_unlock();
425 return ret;
428 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
430 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
432 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
434 if (byte_count)
435 FIXME("Byte count ignored.\n");
437 wined3d_mutex_lock();
438 wined3d_device_evict_managed_resources(device->wined3d_device);
439 wined3d_mutex_unlock();
441 return D3D_OK;
444 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
446 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
448 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
450 if (!d3d8)
451 return D3DERR_INVALIDCALL;
453 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
456 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
459 WINED3DCAPS *wined3d_caps;
460 HRESULT hr;
462 TRACE("iface %p, caps %p.\n", iface, caps);
464 if (!caps)
465 return D3DERR_INVALIDCALL;
467 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
468 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
470 wined3d_mutex_lock();
471 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
472 wined3d_mutex_unlock();
474 fixup_caps(wined3d_caps);
475 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
476 HeapFree(GetProcessHeap(), 0, wined3d_caps);
478 return hr;
481 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
483 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
484 struct wined3d_display_mode wined3d_mode;
485 HRESULT hr;
487 TRACE("iface %p, mode %p.\n", iface, mode);
489 wined3d_mutex_lock();
490 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
491 wined3d_mutex_unlock();
493 if (SUCCEEDED(hr))
495 mode->Width = wined3d_mode.width;
496 mode->Height = wined3d_mode.height;
497 mode->RefreshRate = wined3d_mode.refresh_rate;
498 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
501 return hr;
504 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
505 D3DDEVICE_CREATION_PARAMETERS *parameters)
507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
509 TRACE("iface %p, parameters %p.\n", iface, parameters);
511 wined3d_mutex_lock();
512 wined3d_device_get_creation_parameters(device->wined3d_device,
513 (struct wined3d_device_creation_parameters *)parameters);
514 wined3d_mutex_unlock();
516 return D3D_OK;
519 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
520 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
523 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
524 HRESULT hr;
526 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
527 iface, hotspot_x, hotspot_y, bitmap);
529 if (!bitmap)
531 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
532 return D3DERR_INVALIDCALL;
535 wined3d_mutex_lock();
536 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
537 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
538 wined3d_mutex_unlock();
540 return hr;
543 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
545 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
547 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
549 wined3d_mutex_lock();
550 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
551 wined3d_mutex_unlock();
554 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
556 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
557 BOOL ret;
559 TRACE("iface %p, show %#x.\n", iface, show);
561 wined3d_mutex_lock();
562 ret = wined3d_device_show_cursor(device->wined3d_device, show);
563 wined3d_mutex_unlock();
565 return ret;
568 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
569 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
571 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
572 struct wined3d_swapchain_desc desc;
573 struct d3d8_swapchain *object;
574 UINT i, count;
575 HRESULT hr;
577 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
578 iface, present_parameters, swapchain);
580 if (!present_parameters->Windowed)
582 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
583 return D3DERR_INVALIDCALL;
586 wined3d_mutex_lock();
587 count = wined3d_device_get_swapchain_count(device->wined3d_device);
588 for (i = 0; i < count; ++i)
590 struct wined3d_swapchain *wined3d_swapchain;
592 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
593 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
595 if (!desc.windowed)
597 wined3d_mutex_unlock();
598 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
599 return D3DERR_INVALIDCALL;
602 wined3d_mutex_unlock();
604 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
605 return D3DERR_INVALIDCALL;
606 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
607 *swapchain = &object->IDirect3DSwapChain8_iface;
608 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
610 return hr;
613 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
615 struct wined3d_resource_desc desc;
617 wined3d_resource_get_desc(resource, &desc);
618 if (desc.pool == WINED3D_POOL_DEFAULT)
620 struct d3d8_surface *surface;
622 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
624 IUnknown *parent = wined3d_resource_get_parent(resource);
625 IDirect3DBaseTexture8 *texture;
627 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
629 IDirect3DBaseTexture8_Release(texture);
630 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
631 return D3DERR_DEVICELOST;
634 return D3D_OK;
637 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
639 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
640 return D3DERR_DEVICELOST;
643 surface = wined3d_resource_get_parent(resource);
644 if (surface->resource.refcount)
646 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
647 return D3DERR_DEVICELOST;
650 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
653 return D3D_OK;
656 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
657 D3DPRESENT_PARAMETERS *present_parameters)
659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
660 struct wined3d_swapchain_desc swapchain_desc;
661 HRESULT hr;
663 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
665 if (device->device_state == D3D8_DEVICE_STATE_LOST)
667 WARN("App not active, returning D3DERR_DEVICELOST.\n");
668 return D3DERR_DEVICELOST;
670 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
671 return D3DERR_INVALIDCALL;
673 wined3d_mutex_lock();
675 if (device->vertex_buffer)
677 wined3d_buffer_decref(device->vertex_buffer);
678 device->vertex_buffer = NULL;
679 device->vertex_buffer_size = 0;
681 if (device->index_buffer)
683 wined3d_buffer_decref(device->index_buffer);
684 device->index_buffer = NULL;
685 device->index_buffer_size = 0;
688 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
689 NULL, reset_enum_callback, TRUE)))
691 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
692 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
693 device->device_state = D3D8_DEVICE_STATE_OK;
695 else
697 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
699 wined3d_mutex_unlock();
701 return hr;
704 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
705 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
707 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
709 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
710 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
712 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
713 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
714 * shows a framerate on Windows in applications that only call the device
715 * method, like e.g. the dx8 sdk samples. The conclusion is that native
716 * calls the swapchain's public method from the device. */
717 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
718 src_rect, dst_rect, dst_window_override, dirty_region);
721 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
722 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
724 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
725 struct wined3d_swapchain *wined3d_swapchain;
726 struct wined3d_resource *wined3d_resource;
727 struct wined3d_texture *wined3d_texture;
728 struct d3d8_surface *surface_impl;
730 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
731 iface, backbuffer_idx, backbuffer_type, backbuffer);
733 /* backbuffer_type is ignored by native. */
735 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
736 wined3d_mutex_lock();
738 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
739 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
741 wined3d_mutex_unlock();
742 *backbuffer = NULL;
743 return D3DERR_INVALIDCALL;
746 wined3d_resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
747 surface_impl = wined3d_resource_get_parent(wined3d_resource);
748 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
749 IDirect3DSurface8_AddRef(*backbuffer);
751 wined3d_mutex_unlock();
752 return D3D_OK;
755 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
757 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
758 HRESULT hr;
760 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
762 wined3d_mutex_lock();
763 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
764 wined3d_mutex_unlock();
766 return hr;
769 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
771 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
773 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
775 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
776 wined3d_mutex_lock();
777 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
778 wined3d_mutex_unlock();
781 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
783 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
785 TRACE("iface %p, ramp %p.\n", iface, ramp);
787 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
788 wined3d_mutex_lock();
789 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
790 wined3d_mutex_unlock();
793 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
794 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
795 D3DPOOL pool, IDirect3DTexture8 **texture)
797 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
798 struct d3d8_texture *object;
799 HRESULT hr;
801 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
802 iface, width, height, levels, usage, format, pool, texture);
804 *texture = NULL;
805 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
806 if (!object)
807 return D3DERR_OUTOFVIDEOMEMORY;
809 hr = texture_init(object, device, width, height, levels, usage, format, pool);
810 if (FAILED(hr))
812 WARN("Failed to initialize texture, hr %#x.\n", hr);
813 HeapFree(GetProcessHeap(), 0, object);
814 return hr;
817 TRACE("Created texture %p.\n", object);
818 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
820 return D3D_OK;
823 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
824 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
825 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
827 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
828 struct d3d8_texture *object;
829 HRESULT hr;
831 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
832 iface, width, height, depth, levels, usage, format, pool, texture);
834 *texture = NULL;
835 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
836 if (!object)
837 return D3DERR_OUTOFVIDEOMEMORY;
839 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
840 if (FAILED(hr))
842 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
843 HeapFree(GetProcessHeap(), 0, object);
844 return hr;
847 TRACE("Created volume texture %p.\n", object);
848 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
850 return D3D_OK;
853 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
854 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
856 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
857 struct d3d8_texture *object;
858 HRESULT hr;
860 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
861 iface, edge_length, levels, usage, format, pool, texture);
863 *texture = NULL;
864 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
865 if (!object)
866 return D3DERR_OUTOFVIDEOMEMORY;
868 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
869 if (FAILED(hr))
871 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
872 HeapFree(GetProcessHeap(), 0, object);
873 return hr;
876 TRACE("Created cube texture %p.\n", object);
877 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
879 return hr;
882 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
883 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
885 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
886 struct d3d8_vertexbuffer *object;
887 HRESULT hr;
889 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
890 iface, size, usage, fvf, pool, buffer);
892 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
893 if (!object)
894 return D3DERR_OUTOFVIDEOMEMORY;
896 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
897 if (FAILED(hr))
899 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
900 HeapFree(GetProcessHeap(), 0, object);
901 return hr;
904 TRACE("Created vertex buffer %p.\n", object);
905 *buffer = &object->IDirect3DVertexBuffer8_iface;
907 return D3D_OK;
910 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
911 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
913 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
914 struct d3d8_indexbuffer *object;
915 HRESULT hr;
917 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
918 iface, size, usage, format, pool, buffer);
920 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
921 if (!object)
922 return D3DERR_OUTOFVIDEOMEMORY;
924 hr = indexbuffer_init(object, device, size, usage, format, pool);
925 if (FAILED(hr))
927 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
928 HeapFree(GetProcessHeap(), 0, object);
929 return hr;
932 TRACE("Created index buffer %p.\n", object);
933 *buffer = &object->IDirect3DIndexBuffer8_iface;
935 return D3D_OK;
938 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
939 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
940 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
942 struct wined3d_resource *sub_resource;
943 struct wined3d_resource_desc desc;
944 struct d3d8_surface *surface_impl;
945 struct wined3d_texture *texture;
946 HRESULT hr;
948 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
949 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
950 device, width, height, format, flags, surface,
951 usage, pool, multisample_type, multisample_quality);
953 desc.resource_type = WINED3D_RTYPE_TEXTURE;
954 desc.format = wined3dformat_from_d3dformat(format);
955 desc.multisample_type = multisample_type;
956 desc.multisample_quality = multisample_quality;
957 desc.usage = usage & WINED3DUSAGE_MASK;
958 desc.pool = pool;
959 desc.width = width;
960 desc.height = height;
961 desc.depth = 1;
962 desc.size = 0;
964 wined3d_mutex_lock();
966 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
967 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
969 wined3d_mutex_unlock();
970 WARN("Failed to create texture, hr %#x.\n", hr);
971 return hr;
974 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
975 surface_impl = wined3d_resource_get_parent(sub_resource);
976 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
977 *surface = &surface_impl->IDirect3DSurface8_iface;
978 IDirect3DSurface8_AddRef(*surface);
979 wined3d_texture_decref(texture);
981 wined3d_mutex_unlock();
983 return D3D_OK;
986 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
987 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
988 IDirect3DSurface8 **surface)
990 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
991 DWORD flags = 0;
993 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
994 iface, width, height, format, multisample_type, lockable, surface);
996 *surface = NULL;
997 if (lockable)
998 flags |= WINED3D_SURFACE_MAPPABLE;
1000 return d3d8_device_create_surface(device, width, height, format, flags, surface,
1001 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
1004 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1005 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1006 IDirect3DSurface8 **surface)
1008 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1010 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1011 iface, width, height, format, multisample_type, surface);
1013 *surface = NULL;
1015 /* TODO: Verify that Discard is false */
1016 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
1017 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1020 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1021 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1022 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1024 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1026 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1027 iface, width, height, format, surface);
1029 *surface = NULL;
1031 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
1032 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1035 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1036 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1037 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1039 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1040 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1041 enum wined3d_format_id src_format, dst_format;
1042 struct wined3d_resource_desc wined3d_desc;
1043 struct wined3d_resource *wined3d_resource;
1044 UINT src_w, src_h;
1046 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1047 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1049 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1050 * destination texture is in WINED3D_POOL_DEFAULT. */
1052 wined3d_mutex_lock();
1053 wined3d_resource = wined3d_texture_get_sub_resource(src->wined3d_texture, src->sub_resource_idx);
1054 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1055 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1057 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1058 wined3d_mutex_unlock();
1059 return D3DERR_INVALIDCALL;
1061 src_format = wined3d_desc.format;
1062 src_w = wined3d_desc.width;
1063 src_h = wined3d_desc.height;
1065 wined3d_resource = wined3d_texture_get_sub_resource(dst->wined3d_texture, dst->sub_resource_idx);
1066 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1067 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1069 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1070 wined3d_mutex_unlock();
1071 return D3DERR_INVALIDCALL;
1073 dst_format = wined3d_desc.format;
1075 /* Check that the source and destination formats match */
1076 if (src_format != dst_format)
1078 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1079 src_surface, dst_surface);
1080 wined3d_mutex_unlock();
1081 return D3DERR_INVALIDCALL;
1084 /* Quick if complete copy ... */
1085 if (!rect_count && !src_rects && !dst_points)
1087 RECT rect = {0, 0, src_w, src_h};
1088 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1089 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1091 else
1093 unsigned int i;
1094 /* Copy rect by rect */
1095 if (src_rects && dst_points)
1097 for (i = 0; i < rect_count; ++i)
1099 UINT w = src_rects[i].right - src_rects[i].left;
1100 UINT h = src_rects[i].bottom - src_rects[i].top;
1101 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1102 dst_points[i].x + w, dst_points[i].y + h};
1104 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1105 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1108 else
1110 for (i = 0; i < rect_count; ++i)
1112 UINT w = src_rects[i].right - src_rects[i].left;
1113 UINT h = src_rects[i].bottom - src_rects[i].top;
1114 RECT dst_rect = {0, 0, w, h};
1116 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1117 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1121 wined3d_mutex_unlock();
1123 return WINED3D_OK;
1126 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1127 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1129 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1130 struct d3d8_texture *src_impl, *dst_impl;
1131 HRESULT hr;
1133 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1135 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1136 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1138 wined3d_mutex_lock();
1139 hr = wined3d_device_update_texture(device->wined3d_device,
1140 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1141 wined3d_mutex_unlock();
1143 return hr;
1146 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1148 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1149 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1150 HRESULT hr;
1152 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1154 if (!dst_surface)
1156 WARN("Invalid destination surface passed.\n");
1157 return D3DERR_INVALIDCALL;
1160 wined3d_mutex_lock();
1161 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1162 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1163 wined3d_mutex_unlock();
1165 return hr;
1168 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1169 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1171 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1172 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1173 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1174 struct wined3d_rendertarget_view *original_dsv;
1175 HRESULT hr = D3D_OK;
1177 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1179 wined3d_mutex_lock();
1181 if (ds_impl)
1183 struct wined3d_rendertarget_view *original_rtv;
1184 struct wined3d_resource_desc ds_desc, rt_desc;
1185 struct wined3d_resource *wined3d_resource;
1186 struct d3d8_surface *original_surface;
1188 /* If no render target is passed in check the size against the current RT */
1189 if (!render_target)
1192 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1194 wined3d_mutex_unlock();
1195 return D3DERR_NOTFOUND;
1197 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1198 wined3d_resource = wined3d_texture_get_sub_resource(original_surface->wined3d_texture, original_surface->sub_resource_idx);
1200 else
1201 wined3d_resource = wined3d_texture_get_sub_resource(rt_impl->wined3d_texture, rt_impl->sub_resource_idx);
1202 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1204 wined3d_resource = wined3d_texture_get_sub_resource(ds_impl->wined3d_texture, ds_impl->sub_resource_idx);
1205 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1207 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1209 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1210 wined3d_mutex_unlock();
1211 return D3DERR_INVALIDCALL;
1213 if (ds_desc.multisample_type != rt_desc.multisample_type
1214 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1216 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1217 wined3d_mutex_unlock();
1218 return D3DERR_INVALIDCALL;
1223 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1224 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1225 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1226 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1227 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1228 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1230 wined3d_mutex_unlock();
1232 return hr;
1235 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1237 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1238 struct wined3d_rendertarget_view *wined3d_rtv;
1239 struct d3d8_surface *surface_impl;
1240 HRESULT hr;
1242 TRACE("iface %p, render_target %p.\n", iface, render_target);
1244 if (!render_target)
1245 return D3DERR_INVALIDCALL;
1247 wined3d_mutex_lock();
1248 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1250 /* We want the sub resource parent here, since the view itself may be
1251 * internal to wined3d and may not have a parent. */
1252 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1253 *render_target = &surface_impl->IDirect3DSurface8_iface;
1254 IDirect3DSurface8_AddRef(*render_target);
1255 hr = D3D_OK;
1257 else
1259 ERR("Failed to get wined3d render target.\n");
1260 *render_target = NULL;
1261 hr = D3DERR_NOTFOUND;
1263 wined3d_mutex_unlock();
1265 return hr;
1268 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1270 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1271 struct wined3d_rendertarget_view *wined3d_dsv;
1272 struct d3d8_surface *surface_impl;
1273 HRESULT hr = D3D_OK;
1275 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1277 if (!depth_stencil)
1278 return D3DERR_INVALIDCALL;
1280 wined3d_mutex_lock();
1281 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1283 /* We want the sub resource parent here, since the view itself may be
1284 * internal to wined3d and may not have a parent. */
1285 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1286 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1287 IDirect3DSurface8_AddRef(*depth_stencil);
1289 else
1291 hr = WINED3DERR_NOTFOUND;
1292 *depth_stencil = NULL;
1294 wined3d_mutex_unlock();
1296 return hr;
1299 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1301 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1302 HRESULT hr;
1304 TRACE("iface %p.\n", iface);
1306 wined3d_mutex_lock();
1307 hr = wined3d_device_begin_scene(device->wined3d_device);
1308 wined3d_mutex_unlock();
1310 return hr;
1313 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1315 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1316 HRESULT hr;
1318 TRACE("iface %p.\n", iface);
1320 wined3d_mutex_lock();
1321 hr = wined3d_device_end_scene(device->wined3d_device);
1322 wined3d_mutex_unlock();
1324 return hr;
1327 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1328 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1330 const struct wined3d_color c =
1332 ((color >> 16) & 0xff) / 255.0f,
1333 ((color >> 8) & 0xff) / 255.0f,
1334 (color & 0xff) / 255.0f,
1335 ((color >> 24) & 0xff) / 255.0f,
1337 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1338 HRESULT hr;
1340 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1341 iface, rect_count, rects, flags, color, z, stencil);
1343 wined3d_mutex_lock();
1344 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1345 wined3d_mutex_unlock();
1347 return hr;
1350 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1351 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1353 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1355 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1357 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1358 wined3d_mutex_lock();
1359 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1360 wined3d_mutex_unlock();
1362 return D3D_OK;
1365 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1366 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1368 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1370 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1372 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1373 wined3d_mutex_lock();
1374 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1375 wined3d_mutex_unlock();
1377 return D3D_OK;
1380 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1381 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1383 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1385 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1387 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1388 wined3d_mutex_lock();
1389 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1390 wined3d_mutex_unlock();
1392 return D3D_OK;
1395 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1397 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1399 TRACE("iface %p, viewport %p.\n", iface, viewport);
1401 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1402 wined3d_mutex_lock();
1403 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1404 wined3d_mutex_unlock();
1406 return D3D_OK;
1409 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1411 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1413 TRACE("iface %p, viewport %p.\n", iface, viewport);
1415 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1416 wined3d_mutex_lock();
1417 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1418 wined3d_mutex_unlock();
1420 return D3D_OK;
1423 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1425 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1427 TRACE("iface %p, material %p.\n", iface, material);
1429 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1430 wined3d_mutex_lock();
1431 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1432 wined3d_mutex_unlock();
1434 return D3D_OK;
1437 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1439 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1441 TRACE("iface %p, material %p.\n", iface, material);
1443 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1444 wined3d_mutex_lock();
1445 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1446 wined3d_mutex_unlock();
1448 return D3D_OK;
1451 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1453 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1454 HRESULT hr;
1456 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1458 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1459 wined3d_mutex_lock();
1460 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1461 wined3d_mutex_unlock();
1463 return hr;
1466 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1468 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1469 HRESULT hr;
1471 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1473 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1474 wined3d_mutex_lock();
1475 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1476 wined3d_mutex_unlock();
1478 return hr;
1481 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1483 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1484 HRESULT hr;
1486 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1488 wined3d_mutex_lock();
1489 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1490 wined3d_mutex_unlock();
1492 return hr;
1495 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1497 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1498 HRESULT hr;
1500 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1502 wined3d_mutex_lock();
1503 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1504 wined3d_mutex_unlock();
1506 return hr;
1509 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1511 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1512 HRESULT hr;
1514 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1516 wined3d_mutex_lock();
1517 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1518 wined3d_mutex_unlock();
1520 return hr;
1523 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1525 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1526 HRESULT hr;
1528 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1530 wined3d_mutex_lock();
1531 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1532 wined3d_mutex_unlock();
1534 return hr;
1537 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1538 D3DRENDERSTATETYPE state, DWORD value)
1540 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1542 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1544 wined3d_mutex_lock();
1545 switch (state)
1547 case D3DRS_ZBIAS:
1548 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1549 break;
1551 default:
1552 wined3d_device_set_render_state(device->wined3d_device, state, value);
1554 wined3d_mutex_unlock();
1556 return D3D_OK;
1559 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1560 D3DRENDERSTATETYPE state, DWORD *value)
1562 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1564 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1566 wined3d_mutex_lock();
1567 switch (state)
1569 case D3DRS_ZBIAS:
1570 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1571 break;
1573 default:
1574 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1576 wined3d_mutex_unlock();
1578 return D3D_OK;
1581 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1583 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1584 HRESULT hr;
1586 TRACE("iface %p.\n", iface);
1588 wined3d_mutex_lock();
1589 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1590 wined3d_mutex_unlock();
1592 return hr;
1595 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1597 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1598 struct wined3d_stateblock *stateblock;
1599 HRESULT hr;
1601 TRACE("iface %p, token %p.\n", iface, token);
1603 /* Tell wineD3D to endstateblock before anything else (in case we run out
1604 * of memory later and cause locking problems)
1606 wined3d_mutex_lock();
1607 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1608 if (FAILED(hr))
1610 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1611 wined3d_mutex_unlock();
1612 return hr;
1615 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1616 wined3d_mutex_unlock();
1618 if (*token == D3D8_INVALID_HANDLE)
1620 ERR("Failed to create a handle\n");
1621 wined3d_mutex_lock();
1622 wined3d_stateblock_decref(stateblock);
1623 wined3d_mutex_unlock();
1624 return E_FAIL;
1626 ++*token;
1628 TRACE("Returning %#x (%p).\n", *token, stateblock);
1630 return hr;
1633 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1635 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1636 struct wined3d_stateblock *stateblock;
1638 TRACE("iface %p, token %#x.\n", iface, token);
1640 if (!token)
1641 return D3D_OK;
1643 wined3d_mutex_lock();
1644 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1645 if (!stateblock)
1647 WARN("Invalid handle (%#x) passed.\n", token);
1648 wined3d_mutex_unlock();
1649 return D3DERR_INVALIDCALL;
1651 wined3d_stateblock_apply(stateblock);
1652 wined3d_mutex_unlock();
1654 return D3D_OK;
1657 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1660 struct wined3d_stateblock *stateblock;
1662 TRACE("iface %p, token %#x.\n", iface, token);
1664 wined3d_mutex_lock();
1665 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1666 if (!stateblock)
1668 WARN("Invalid handle (%#x) passed.\n", token);
1669 wined3d_mutex_unlock();
1670 return D3DERR_INVALIDCALL;
1672 wined3d_stateblock_capture(stateblock);
1673 wined3d_mutex_unlock();
1675 return D3D_OK;
1678 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1680 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1681 struct wined3d_stateblock *stateblock;
1683 TRACE("iface %p, token %#x.\n", iface, token);
1685 wined3d_mutex_lock();
1686 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1688 if (!stateblock)
1690 WARN("Invalid handle (%#x) passed.\n", token);
1691 wined3d_mutex_unlock();
1692 return D3DERR_INVALIDCALL;
1695 if (wined3d_stateblock_decref(stateblock))
1697 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1699 wined3d_mutex_unlock();
1701 return D3D_OK;
1704 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1705 D3DSTATEBLOCKTYPE type, DWORD *handle)
1707 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1708 struct wined3d_stateblock *stateblock;
1709 HRESULT hr;
1711 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1713 if (type != D3DSBT_ALL
1714 && type != D3DSBT_PIXELSTATE
1715 && type != D3DSBT_VERTEXSTATE)
1717 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1718 return D3DERR_INVALIDCALL;
1721 wined3d_mutex_lock();
1722 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1723 if (FAILED(hr))
1725 wined3d_mutex_unlock();
1726 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1727 return hr;
1730 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1731 wined3d_mutex_unlock();
1733 if (*handle == D3D8_INVALID_HANDLE)
1735 ERR("Failed to allocate a handle.\n");
1736 wined3d_mutex_lock();
1737 wined3d_stateblock_decref(stateblock);
1738 wined3d_mutex_unlock();
1739 return E_FAIL;
1741 ++*handle;
1743 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1745 return hr;
1748 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1750 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1751 HRESULT hr;
1753 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1754 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1756 wined3d_mutex_lock();
1757 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1758 wined3d_mutex_unlock();
1760 return hr;
1763 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1765 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1766 HRESULT hr;
1768 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1770 wined3d_mutex_lock();
1771 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1772 wined3d_mutex_unlock();
1774 return hr;
1777 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1779 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1780 struct wined3d_texture *wined3d_texture;
1781 struct d3d8_texture *texture_impl;
1783 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1785 if (!texture)
1786 return D3DERR_INVALIDCALL;
1788 wined3d_mutex_lock();
1789 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1791 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1792 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1793 IDirect3DBaseTexture8_AddRef(*texture);
1795 else
1797 *texture = NULL;
1799 wined3d_mutex_unlock();
1801 return D3D_OK;
1804 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1806 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1807 struct d3d8_texture *texture_impl;
1808 HRESULT hr;
1810 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1812 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1814 wined3d_mutex_lock();
1815 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1816 texture_impl ? texture_impl->wined3d_texture : NULL);
1817 wined3d_mutex_unlock();
1819 return hr;
1822 static const struct tss_lookup
1824 BOOL sampler_state;
1825 enum wined3d_texture_stage_state state;
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->state);
1879 else
1880 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->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->state, value);
1905 else
1906 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->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 UINT size = vtx_count * stride;
2042 UINT vb_pos, align;
2043 BYTE *buffer_data;
2045 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2046 iface, primitive_type, primitive_count, data, stride);
2048 if (!primitive_count)
2050 WARN("primitive_count is 0, returning D3D_OK\n");
2051 return D3D_OK;
2054 wined3d_mutex_lock();
2055 hr = d3d8_device_prepare_vertex_buffer(device, size);
2056 if (FAILED(hr))
2057 goto done;
2059 vb_pos = device->vertex_buffer_pos;
2060 align = vb_pos % stride;
2061 if (align) align = stride - align;
2062 if (vb_pos + size + align > device->vertex_buffer_size)
2063 vb_pos = 0;
2064 else
2065 vb_pos += align;
2067 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2068 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2069 if (FAILED(hr))
2070 goto done;
2071 memcpy(buffer_data, data, size);
2072 wined3d_buffer_unmap(device->vertex_buffer);
2073 device->vertex_buffer_pos = vb_pos + size;
2075 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2076 if (FAILED(hr))
2077 goto done;
2079 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2080 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2081 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2083 done:
2084 wined3d_mutex_unlock();
2085 return hr;
2088 /* The caller is responsible for wined3d locking */
2089 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2091 HRESULT hr;
2093 if (device->index_buffer_size < min_size || !device->index_buffer)
2095 UINT size = max(device->index_buffer_size * 2, min_size);
2096 struct wined3d_buffer *buffer;
2098 TRACE("Growing index buffer to %u bytes\n", size);
2100 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2101 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2102 if (FAILED(hr))
2104 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2105 return hr;
2108 if (device->index_buffer)
2109 wined3d_buffer_decref(device->index_buffer);
2111 device->index_buffer = buffer;
2112 device->index_buffer_size = size;
2113 device->index_buffer_pos = 0;
2115 return D3D_OK;
2118 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2119 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2120 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2121 const void *vertex_data, UINT vertex_stride)
2123 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2124 HRESULT hr;
2125 BYTE *buffer_data;
2127 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2128 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2129 UINT idx_size = idx_count * idx_fmt_size;
2130 UINT ib_pos;
2132 UINT vtx_size = vertex_count * vertex_stride;
2133 UINT vb_pos, align;
2135 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2136 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2137 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2138 index_data, index_format, vertex_data, vertex_stride);
2140 if (!primitive_count)
2142 WARN("primitive_count is 0, returning D3D_OK\n");
2143 return D3D_OK;
2146 wined3d_mutex_lock();
2148 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2149 if (FAILED(hr))
2150 goto done;
2152 vb_pos = device->vertex_buffer_pos;
2153 align = vb_pos % vertex_stride;
2154 if (align) align = vertex_stride - align;
2155 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2156 vb_pos = 0;
2157 else
2158 vb_pos += align;
2160 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2161 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2162 if (FAILED(hr))
2163 goto done;
2164 memcpy(buffer_data, vertex_data, vtx_size);
2165 wined3d_buffer_unmap(device->vertex_buffer);
2166 device->vertex_buffer_pos = vb_pos + vtx_size;
2168 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2169 if (FAILED(hr))
2170 goto done;
2172 ib_pos = device->index_buffer_pos;
2173 align = ib_pos % idx_fmt_size;
2174 if (align) align = idx_fmt_size - align;
2175 if (ib_pos + idx_size + align > device->index_buffer_size)
2176 ib_pos = 0;
2177 else
2178 ib_pos += align;
2180 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2181 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2182 if (FAILED(hr))
2183 goto done;
2184 memcpy(buffer_data, index_data, idx_size);
2185 wined3d_buffer_unmap(device->index_buffer);
2186 device->index_buffer_pos = ib_pos + idx_size;
2188 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2189 if (FAILED(hr))
2190 goto done;
2192 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2193 wined3dformat_from_d3dformat(index_format));
2194 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2196 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2197 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2199 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2200 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2201 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2203 done:
2204 wined3d_mutex_unlock();
2205 return hr;
2208 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2209 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2211 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2212 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2213 HRESULT hr;
2215 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2216 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2218 wined3d_mutex_lock();
2219 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2220 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2221 wined3d_mutex_unlock();
2223 return hr;
2226 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2227 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2229 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2230 struct d3d8_vertex_shader *object;
2231 DWORD shader_handle;
2232 DWORD handle;
2233 HRESULT hr;
2235 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2236 iface, declaration, byte_code, shader, usage);
2238 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2239 if (!object)
2241 *shader = 0;
2242 return E_OUTOFMEMORY;
2245 wined3d_mutex_lock();
2246 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2247 wined3d_mutex_unlock();
2248 if (handle == D3D8_INVALID_HANDLE)
2250 ERR("Failed to allocate vertex shader handle.\n");
2251 HeapFree(GetProcessHeap(), 0, object);
2252 *shader = 0;
2253 return E_OUTOFMEMORY;
2256 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2258 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2259 if (FAILED(hr))
2261 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2262 wined3d_mutex_lock();
2263 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2264 wined3d_mutex_unlock();
2265 HeapFree(GetProcessHeap(), 0, object);
2266 *shader = 0;
2267 return hr;
2270 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2271 *shader = shader_handle;
2273 return D3D_OK;
2276 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2278 struct d3d8_vertex_declaration *d3d8_declaration;
2279 struct FvfToDecl *convertedDecls = device->decls;
2280 int p, low, high; /* deliberately signed */
2281 HRESULT hr;
2283 TRACE("Searching for declaration for fvf %08x... ", fvf);
2285 low = 0;
2286 high = device->numConvertedDecls - 1;
2287 while (low <= high)
2289 p = (low + high) >> 1;
2290 TRACE("%d ", p);
2292 if (convertedDecls[p].fvf == fvf)
2294 TRACE("found %p\n", convertedDecls[p].declaration);
2295 return convertedDecls[p].declaration;
2298 if (convertedDecls[p].fvf < fvf)
2299 low = p + 1;
2300 else
2301 high = p - 1;
2303 TRACE("not found. Creating and inserting at position %d.\n", low);
2305 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2306 return NULL;
2308 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2310 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2311 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2312 return NULL;
2315 if (device->declArraySize == device->numConvertedDecls)
2317 UINT grow = device->declArraySize / 2;
2319 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2320 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2321 if (!convertedDecls)
2323 d3d8_vertex_declaration_destroy(d3d8_declaration);
2324 return NULL;
2326 device->decls = convertedDecls;
2327 device->declArraySize += grow;
2330 memmove(convertedDecls + low + 1, convertedDecls + low,
2331 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2332 convertedDecls[low].declaration = d3d8_declaration;
2333 convertedDecls[low].fvf = fvf;
2334 ++device->numConvertedDecls;
2336 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2338 return d3d8_declaration;
2341 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2343 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2344 struct d3d8_vertex_shader *shader_impl;
2346 TRACE("iface %p, shader %#x.\n", iface, shader);
2348 if (VS_HIGHESTFIXEDFXF >= shader)
2350 TRACE("Setting FVF, %#x\n", shader);
2352 wined3d_mutex_lock();
2353 wined3d_device_set_vertex_declaration(device->wined3d_device,
2354 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2355 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2356 wined3d_mutex_unlock();
2358 return D3D_OK;
2361 TRACE("Setting shader\n");
2363 wined3d_mutex_lock();
2364 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2366 WARN("Invalid handle (%#x) passed.\n", shader);
2367 wined3d_mutex_unlock();
2369 return D3DERR_INVALIDCALL;
2372 wined3d_device_set_vertex_declaration(device->wined3d_device,
2373 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2374 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2375 wined3d_mutex_unlock();
2377 return D3D_OK;
2380 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2382 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2383 struct wined3d_vertex_declaration *wined3d_declaration;
2384 struct d3d8_vertex_declaration *d3d8_declaration;
2386 TRACE("iface %p, shader %p.\n", iface, shader);
2388 wined3d_mutex_lock();
2389 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2391 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2392 *shader = d3d8_declaration->shader_handle;
2394 else
2396 *shader = 0;
2398 wined3d_mutex_unlock();
2400 TRACE("Returning %#x.\n", *shader);
2402 return D3D_OK;
2405 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2407 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2408 struct d3d8_vertex_shader *shader_impl;
2410 TRACE("iface %p, shader %#x.\n", iface, shader);
2412 wined3d_mutex_lock();
2413 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2415 WARN("Invalid handle (%#x) passed.\n", shader);
2416 wined3d_mutex_unlock();
2418 return D3DERR_INVALIDCALL;
2421 if (shader_impl->wined3d_shader
2422 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2423 IDirect3DDevice8_SetVertexShader(iface, 0);
2425 wined3d_mutex_unlock();
2427 d3d8_vertex_shader_destroy(shader_impl);
2429 return D3D_OK;
2432 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2433 DWORD start_register, const void *data, DWORD count)
2435 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2436 HRESULT hr;
2438 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2439 iface, start_register, data, count);
2441 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2443 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2444 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2445 return D3DERR_INVALIDCALL;
2448 wined3d_mutex_lock();
2449 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2450 wined3d_mutex_unlock();
2452 return hr;
2455 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2456 DWORD start_register, void *data, DWORD count)
2458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2459 HRESULT hr;
2461 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2462 iface, start_register, data, count);
2464 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2466 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2467 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2468 return D3DERR_INVALIDCALL;
2471 wined3d_mutex_lock();
2472 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2473 wined3d_mutex_unlock();
2475 return hr;
2478 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2479 DWORD shader, void *data, DWORD *data_size)
2481 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2482 struct d3d8_vertex_declaration *declaration;
2483 struct d3d8_vertex_shader *shader_impl;
2485 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2486 iface, shader, data, data_size);
2488 wined3d_mutex_lock();
2489 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2490 wined3d_mutex_unlock();
2492 if (!shader_impl)
2494 WARN("Invalid handle (%#x) passed.\n", shader);
2495 return D3DERR_INVALIDCALL;
2497 declaration = shader_impl->vertex_declaration;
2499 if (!data)
2501 *data_size = declaration->elements_size;
2502 return D3D_OK;
2505 /* MSDN claims that if *data_size is smaller than the required size
2506 * we should write the required size and return D3DERR_MOREDATA.
2507 * That's not actually true. */
2508 if (*data_size < declaration->elements_size)
2509 return D3DERR_INVALIDCALL;
2511 memcpy(data, declaration->elements, declaration->elements_size);
2513 return D3D_OK;
2516 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2517 DWORD shader, void *data, DWORD *data_size)
2519 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2520 struct d3d8_vertex_shader *shader_impl = NULL;
2521 HRESULT hr;
2523 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2524 iface, shader, data, data_size);
2526 wined3d_mutex_lock();
2527 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2529 WARN("Invalid handle (%#x) passed.\n", shader);
2530 wined3d_mutex_unlock();
2532 return D3DERR_INVALIDCALL;
2535 if (!shader_impl->wined3d_shader)
2537 wined3d_mutex_unlock();
2538 *data_size = 0;
2539 return D3D_OK;
2542 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2543 wined3d_mutex_unlock();
2545 return hr;
2548 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2549 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2551 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2552 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2554 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2556 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2557 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2558 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2559 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2560 * problem)
2562 wined3d_mutex_lock();
2563 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2564 wined3d_device_set_index_buffer(device->wined3d_device,
2565 ib ? ib->wined3d_buffer : NULL,
2566 ib ? ib->format : WINED3DFMT_UNKNOWN);
2567 wined3d_mutex_unlock();
2569 return D3D_OK;
2572 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2573 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2575 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2576 enum wined3d_format_id wined3d_format;
2577 struct wined3d_buffer *wined3d_buffer;
2578 struct d3d8_indexbuffer *buffer_impl;
2580 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2582 if (!buffer)
2583 return D3DERR_INVALIDCALL;
2585 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2586 wined3d_mutex_lock();
2587 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2588 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2590 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2591 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2592 IDirect3DIndexBuffer8_AddRef(*buffer);
2594 else
2596 *buffer = NULL;
2598 wined3d_mutex_unlock();
2600 return D3D_OK;
2603 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2604 const DWORD *byte_code, DWORD *shader)
2606 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2607 struct d3d8_pixel_shader *object;
2608 DWORD shader_handle;
2609 DWORD handle;
2610 HRESULT hr;
2612 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2614 if (!shader)
2615 return D3DERR_INVALIDCALL;
2617 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2618 if (!object)
2619 return E_OUTOFMEMORY;
2621 wined3d_mutex_lock();
2622 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2623 wined3d_mutex_unlock();
2624 if (handle == D3D8_INVALID_HANDLE)
2626 ERR("Failed to allocate pixel shader handle.\n");
2627 HeapFree(GetProcessHeap(), 0, object);
2628 return E_OUTOFMEMORY;
2631 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2633 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2634 if (FAILED(hr))
2636 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2637 wined3d_mutex_lock();
2638 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2639 wined3d_mutex_unlock();
2640 HeapFree(GetProcessHeap(), 0, object);
2641 *shader = 0;
2642 return hr;
2645 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2646 *shader = shader_handle;
2648 return D3D_OK;
2651 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2653 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2654 struct d3d8_pixel_shader *shader_impl;
2656 TRACE("iface %p, shader %#x.\n", iface, shader);
2658 wined3d_mutex_lock();
2660 if (!shader)
2662 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2663 wined3d_mutex_unlock();
2664 return D3D_OK;
2667 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2669 WARN("Invalid handle (%#x) passed.\n", shader);
2670 wined3d_mutex_unlock();
2671 return D3DERR_INVALIDCALL;
2674 TRACE("Setting shader %p.\n", shader_impl);
2675 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2676 wined3d_mutex_unlock();
2678 return D3D_OK;
2681 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2683 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2684 struct wined3d_shader *object;
2686 TRACE("iface %p, shader %p.\n", iface, shader);
2688 if (!shader)
2689 return D3DERR_INVALIDCALL;
2691 wined3d_mutex_lock();
2692 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2694 struct d3d8_pixel_shader *d3d8_shader;
2695 d3d8_shader = wined3d_shader_get_parent(object);
2696 *shader = d3d8_shader->handle;
2698 else
2700 *shader = 0;
2702 wined3d_mutex_unlock();
2704 TRACE("Returning %#x.\n", *shader);
2706 return D3D_OK;
2709 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2711 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2712 struct d3d8_pixel_shader *shader_impl;
2714 TRACE("iface %p, shader %#x.\n", iface, shader);
2716 wined3d_mutex_lock();
2718 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2720 WARN("Invalid handle (%#x) passed.\n", shader);
2721 wined3d_mutex_unlock();
2722 return D3DERR_INVALIDCALL;
2725 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2726 IDirect3DDevice8_SetPixelShader(iface, 0);
2728 wined3d_mutex_unlock();
2730 d3d8_pixel_shader_destroy(shader_impl);
2732 return D3D_OK;
2735 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2736 DWORD start_register, const void *data, DWORD count)
2738 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2739 HRESULT hr;
2741 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2742 iface, start_register, data, count);
2744 wined3d_mutex_lock();
2745 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2746 wined3d_mutex_unlock();
2748 return hr;
2751 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2752 DWORD start_register, void *data, DWORD count)
2754 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2755 HRESULT hr;
2757 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2758 iface, start_register, data, count);
2760 wined3d_mutex_lock();
2761 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2762 wined3d_mutex_unlock();
2764 return hr;
2767 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2768 DWORD shader, void *data, DWORD *data_size)
2770 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2771 struct d3d8_pixel_shader *shader_impl = NULL;
2772 HRESULT hr;
2774 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2775 iface, shader, data, data_size);
2777 wined3d_mutex_lock();
2778 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2780 WARN("Invalid handle (%#x) passed.\n", shader);
2781 wined3d_mutex_unlock();
2783 return D3DERR_INVALIDCALL;
2786 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2787 wined3d_mutex_unlock();
2789 return hr;
2792 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2793 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2795 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2796 iface, handle, segment_count, patch_info);
2797 return D3D_OK;
2800 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2801 const float *segment_count, const D3DTRIPATCH_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_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2810 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2811 return D3DERR_INVALIDCALL;
2814 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2815 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2817 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2818 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2819 HRESULT hr;
2821 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2822 iface, stream_idx, buffer, stride);
2824 wined3d_mutex_lock();
2825 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2826 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2827 wined3d_mutex_unlock();
2829 return hr;
2832 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2833 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2835 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2836 struct d3d8_vertexbuffer *buffer_impl;
2837 struct wined3d_buffer *wined3d_buffer = NULL;
2838 HRESULT hr;
2840 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2841 iface, stream_idx, buffer, stride);
2843 if (!buffer)
2844 return D3DERR_INVALIDCALL;
2846 wined3d_mutex_lock();
2847 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2848 if (SUCCEEDED(hr) && wined3d_buffer)
2850 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2851 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2852 IDirect3DVertexBuffer8_AddRef(*buffer);
2854 else
2856 if (FAILED(hr))
2857 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2858 *buffer = NULL;
2860 wined3d_mutex_unlock();
2862 return hr;
2865 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2867 d3d8_device_QueryInterface,
2868 d3d8_device_AddRef,
2869 d3d8_device_Release,
2870 d3d8_device_TestCooperativeLevel,
2871 d3d8_device_GetAvailableTextureMem,
2872 d3d8_device_ResourceManagerDiscardBytes,
2873 d3d8_device_GetDirect3D,
2874 d3d8_device_GetDeviceCaps,
2875 d3d8_device_GetDisplayMode,
2876 d3d8_device_GetCreationParameters,
2877 d3d8_device_SetCursorProperties,
2878 d3d8_device_SetCursorPosition,
2879 d3d8_device_ShowCursor,
2880 d3d8_device_CreateAdditionalSwapChain,
2881 d3d8_device_Reset,
2882 d3d8_device_Present,
2883 d3d8_device_GetBackBuffer,
2884 d3d8_device_GetRasterStatus,
2885 d3d8_device_SetGammaRamp,
2886 d3d8_device_GetGammaRamp,
2887 d3d8_device_CreateTexture,
2888 d3d8_device_CreateVolumeTexture,
2889 d3d8_device_CreateCubeTexture,
2890 d3d8_device_CreateVertexBuffer,
2891 d3d8_device_CreateIndexBuffer,
2892 d3d8_device_CreateRenderTarget,
2893 d3d8_device_CreateDepthStencilSurface,
2894 d3d8_device_CreateImageSurface,
2895 d3d8_device_CopyRects,
2896 d3d8_device_UpdateTexture,
2897 d3d8_device_GetFrontBuffer,
2898 d3d8_device_SetRenderTarget,
2899 d3d8_device_GetRenderTarget,
2900 d3d8_device_GetDepthStencilSurface,
2901 d3d8_device_BeginScene,
2902 d3d8_device_EndScene,
2903 d3d8_device_Clear,
2904 d3d8_device_SetTransform,
2905 d3d8_device_GetTransform,
2906 d3d8_device_MultiplyTransform,
2907 d3d8_device_SetViewport,
2908 d3d8_device_GetViewport,
2909 d3d8_device_SetMaterial,
2910 d3d8_device_GetMaterial,
2911 d3d8_device_SetLight,
2912 d3d8_device_GetLight,
2913 d3d8_device_LightEnable,
2914 d3d8_device_GetLightEnable,
2915 d3d8_device_SetClipPlane,
2916 d3d8_device_GetClipPlane,
2917 d3d8_device_SetRenderState,
2918 d3d8_device_GetRenderState,
2919 d3d8_device_BeginStateBlock,
2920 d3d8_device_EndStateBlock,
2921 d3d8_device_ApplyStateBlock,
2922 d3d8_device_CaptureStateBlock,
2923 d3d8_device_DeleteStateBlock,
2924 d3d8_device_CreateStateBlock,
2925 d3d8_device_SetClipStatus,
2926 d3d8_device_GetClipStatus,
2927 d3d8_device_GetTexture,
2928 d3d8_device_SetTexture,
2929 d3d8_device_GetTextureStageState,
2930 d3d8_device_SetTextureStageState,
2931 d3d8_device_ValidateDevice,
2932 d3d8_device_GetInfo,
2933 d3d8_device_SetPaletteEntries,
2934 d3d8_device_GetPaletteEntries,
2935 d3d8_device_SetCurrentTexturePalette,
2936 d3d8_device_GetCurrentTexturePalette,
2937 d3d8_device_DrawPrimitive,
2938 d3d8_device_DrawIndexedPrimitive,
2939 d3d8_device_DrawPrimitiveUP,
2940 d3d8_device_DrawIndexedPrimitiveUP,
2941 d3d8_device_ProcessVertices,
2942 d3d8_device_CreateVertexShader,
2943 d3d8_device_SetVertexShader,
2944 d3d8_device_GetVertexShader,
2945 d3d8_device_DeleteVertexShader,
2946 d3d8_device_SetVertexShaderConstant,
2947 d3d8_device_GetVertexShaderConstant,
2948 d3d8_device_GetVertexShaderDeclaration,
2949 d3d8_device_GetVertexShaderFunction,
2950 d3d8_device_SetStreamSource,
2951 d3d8_device_GetStreamSource,
2952 d3d8_device_SetIndices,
2953 d3d8_device_GetIndices,
2954 d3d8_device_CreatePixelShader,
2955 d3d8_device_SetPixelShader,
2956 d3d8_device_GetPixelShader,
2957 d3d8_device_DeletePixelShader,
2958 d3d8_device_SetPixelShaderConstant,
2959 d3d8_device_GetPixelShaderConstant,
2960 d3d8_device_GetPixelShaderFunction,
2961 d3d8_device_DrawRectPatch,
2962 d3d8_device_DrawTriPatch,
2963 d3d8_device_DeletePatch,
2966 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2968 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2971 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2972 struct wined3d_device *device)
2974 TRACE("device_parent %p, device %p\n", device_parent, device);
2977 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2979 TRACE("device_parent %p.\n", device_parent);
2982 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2984 struct d3d8_device *device = device_from_device_parent(device_parent);
2986 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2988 if (!activate)
2989 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2990 else
2991 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2994 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2995 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx, struct wined3d_surface *surface, void **parent,
2996 const struct wined3d_parent_ops **parent_ops)
2998 struct d3d8_surface *d3d_surface;
3000 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, surface %p, parent %p, parent_ops %p.\n",
3001 device_parent, wined3d_texture, sub_resource_idx, surface, parent, parent_ops);
3003 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3004 return E_OUTOFMEMORY;
3006 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3007 *parent = d3d_surface;
3008 TRACE("Created surface %p.\n", d3d_surface);
3010 return D3D_OK;
3013 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3014 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3015 void **parent, const struct wined3d_parent_ops **parent_ops)
3017 struct d3d8_volume *d3d_volume;
3019 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3020 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3022 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3023 return E_OUTOFMEMORY;
3025 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3026 *parent = d3d_volume;
3027 TRACE("Created volume %p.\n", d3d_volume);
3029 return D3D_OK;
3032 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3033 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3035 struct d3d8_device *device = device_from_device_parent(device_parent);
3036 struct d3d8_surface *d3d_surface;
3037 HRESULT hr;
3039 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3040 device_parent, container_parent, desc, texture);
3042 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1,
3043 WINED3D_SURFACE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3045 WARN("Failed to create texture, hr %#x.\n", hr);
3046 return hr;
3049 d3d_surface = wined3d_resource_get_parent(wined3d_texture_get_sub_resource(*texture, 0));
3050 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3052 return hr;
3055 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3056 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3058 struct d3d8_device *device = device_from_device_parent(device_parent);
3059 struct d3d8_swapchain *d3d_swapchain;
3060 HRESULT hr;
3062 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3064 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3066 WARN("Failed to create swapchain, hr %#x.\n", hr);
3067 *swapchain = NULL;
3068 return hr;
3071 *swapchain = d3d_swapchain->wined3d_swapchain;
3072 wined3d_swapchain_incref(*swapchain);
3073 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3075 return hr;
3078 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3080 device_parent_wined3d_device_created,
3081 device_parent_mode_changed,
3082 device_parent_activate,
3083 device_parent_surface_created,
3084 device_parent_volume_created,
3085 device_parent_create_swapchain_texture,
3086 device_parent_create_swapchain,
3089 static void setup_fpu(void)
3091 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3092 WORD cw;
3093 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3094 cw = (cw & ~0xf3f) | 0x3f;
3095 __asm__ volatile ("fldcw %0" : : "m" (cw));
3096 #elif defined(__i386__) && defined(_MSC_VER)
3097 WORD cw;
3098 __asm fnstcw cw;
3099 cw = (cw & ~0xf3f) | 0x3f;
3100 __asm fldcw cw;
3101 #else
3102 FIXME("FPU setup not implemented for this platform.\n");
3103 #endif
3106 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3107 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3109 struct wined3d_swapchain_desc swapchain_desc;
3110 struct wined3d_swapchain *wined3d_swapchain;
3111 HRESULT hr;
3113 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3114 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3115 device->ref = 1;
3116 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3117 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3118 if (!device->handle_table.entries)
3120 ERR("Failed to allocate handle table memory.\n");
3121 return E_OUTOFMEMORY;
3123 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3125 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3127 wined3d_mutex_lock();
3128 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3129 &device->device_parent, &device->wined3d_device);
3130 if (FAILED(hr))
3132 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3133 wined3d_mutex_unlock();
3134 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3135 return hr;
3138 if (!parameters->Windowed)
3140 HWND device_window = parameters->hDeviceWindow;
3142 if (!focus_window)
3143 focus_window = device_window;
3144 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3146 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3147 wined3d_device_decref(device->wined3d_device);
3148 wined3d_mutex_unlock();
3149 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3150 return hr;
3153 if (!device_window)
3154 device_window = focus_window;
3155 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3156 parameters->BackBufferWidth,
3157 parameters->BackBufferHeight);
3160 if (flags & D3DCREATE_MULTITHREADED)
3161 wined3d_device_set_multithreaded(device->wined3d_device);
3163 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3165 wined3d_device_release_focus_window(device->wined3d_device);
3166 wined3d_device_decref(device->wined3d_device);
3167 wined3d_mutex_unlock();
3168 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3169 return D3DERR_INVALIDCALL;
3172 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3173 if (FAILED(hr))
3175 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3176 wined3d_device_release_focus_window(device->wined3d_device);
3177 wined3d_device_decref(device->wined3d_device);
3178 wined3d_mutex_unlock();
3179 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3180 return hr;
3183 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3184 wined3d_mutex_unlock();
3186 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3188 device->declArraySize = 16;
3189 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3190 if (!device->decls)
3192 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3193 hr = E_OUTOFMEMORY;
3194 goto err;
3197 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3198 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3200 device->d3d_parent = &parent->IDirect3D8_iface;
3201 IDirect3D8_AddRef(device->d3d_parent);
3203 return D3D_OK;
3205 err:
3206 wined3d_mutex_lock();
3207 wined3d_device_uninit_3d(device->wined3d_device);
3208 wined3d_device_release_focus_window(device->wined3d_device);
3209 wined3d_device_decref(device->wined3d_device);
3210 wined3d_mutex_unlock();
3211 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3212 return hr;