makefiles: Expand the names of static libraries in linker commands.
[wine.git] / dlls / d3d8 / device.c
blob1cad4bedd264c488746857d855d3a960e76d2416
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_2D)
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_texture *wined3d_texture;
727 struct d3d8_surface *surface_impl;
729 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
730 iface, backbuffer_idx, backbuffer_type, backbuffer);
732 /* backbuffer_type is ignored by native. */
734 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
735 wined3d_mutex_lock();
737 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
738 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
740 wined3d_mutex_unlock();
741 *backbuffer = NULL;
742 return D3DERR_INVALIDCALL;
745 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
746 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
747 IDirect3DSurface8_AddRef(*backbuffer);
749 wined3d_mutex_unlock();
750 return D3D_OK;
753 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
755 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
756 HRESULT hr;
758 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
760 wined3d_mutex_lock();
761 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
762 wined3d_mutex_unlock();
764 return hr;
767 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
771 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
773 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
774 wined3d_mutex_lock();
775 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
776 wined3d_mutex_unlock();
779 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
781 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
783 TRACE("iface %p, ramp %p.\n", iface, ramp);
785 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
786 wined3d_mutex_lock();
787 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
788 wined3d_mutex_unlock();
791 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
792 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
793 D3DPOOL pool, IDirect3DTexture8 **texture)
795 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
796 struct d3d8_texture *object;
797 HRESULT hr;
799 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
800 iface, width, height, levels, usage, format, pool, texture);
802 *texture = NULL;
803 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
804 if (!object)
805 return D3DERR_OUTOFVIDEOMEMORY;
807 hr = texture_init(object, device, width, height, levels, usage, format, pool);
808 if (FAILED(hr))
810 WARN("Failed to initialize texture, hr %#x.\n", hr);
811 HeapFree(GetProcessHeap(), 0, object);
812 return hr;
815 TRACE("Created texture %p.\n", object);
816 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
818 return D3D_OK;
821 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
822 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
823 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
825 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
826 struct d3d8_texture *object;
827 HRESULT hr;
829 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
830 iface, width, height, depth, levels, usage, format, pool, texture);
832 *texture = NULL;
833 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
834 if (!object)
835 return D3DERR_OUTOFVIDEOMEMORY;
837 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
838 if (FAILED(hr))
840 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
841 HeapFree(GetProcessHeap(), 0, object);
842 return hr;
845 TRACE("Created volume texture %p.\n", object);
846 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
848 return D3D_OK;
851 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
852 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
854 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
855 struct d3d8_texture *object;
856 HRESULT hr;
858 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
859 iface, edge_length, levels, usage, format, pool, texture);
861 *texture = NULL;
862 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
863 if (!object)
864 return D3DERR_OUTOFVIDEOMEMORY;
866 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
867 if (FAILED(hr))
869 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
870 HeapFree(GetProcessHeap(), 0, object);
871 return hr;
874 TRACE("Created cube texture %p.\n", object);
875 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
877 return hr;
880 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
881 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
883 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
884 struct d3d8_vertexbuffer *object;
885 HRESULT hr;
887 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
888 iface, size, usage, fvf, pool, buffer);
890 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
891 if (!object)
892 return D3DERR_OUTOFVIDEOMEMORY;
894 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
895 if (FAILED(hr))
897 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
898 HeapFree(GetProcessHeap(), 0, object);
899 return hr;
902 TRACE("Created vertex buffer %p.\n", object);
903 *buffer = &object->IDirect3DVertexBuffer8_iface;
905 return D3D_OK;
908 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
909 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
911 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
912 struct d3d8_indexbuffer *object;
913 HRESULT hr;
915 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
916 iface, size, usage, format, pool, buffer);
918 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
919 if (!object)
920 return D3DERR_OUTOFVIDEOMEMORY;
922 hr = indexbuffer_init(object, device, size, usage, format, pool);
923 if (FAILED(hr))
925 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
926 HeapFree(GetProcessHeap(), 0, object);
927 return hr;
930 TRACE("Created index buffer %p.\n", object);
931 *buffer = &object->IDirect3DIndexBuffer8_iface;
933 return D3D_OK;
936 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
937 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
938 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
940 struct wined3d_resource_desc desc;
941 struct d3d8_surface *surface_impl;
942 struct wined3d_texture *texture;
943 HRESULT hr;
945 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
946 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
947 device, width, height, format, flags, surface,
948 usage, pool, multisample_type, multisample_quality);
950 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
951 desc.format = wined3dformat_from_d3dformat(format);
952 desc.multisample_type = multisample_type;
953 desc.multisample_quality = multisample_quality;
954 desc.usage = usage & WINED3DUSAGE_MASK;
955 desc.pool = pool;
956 desc.width = width;
957 desc.height = height;
958 desc.depth = 1;
959 desc.size = 0;
961 wined3d_mutex_lock();
963 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
964 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
966 wined3d_mutex_unlock();
967 WARN("Failed to create texture, hr %#x.\n", hr);
968 return hr;
971 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
972 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
973 *surface = &surface_impl->IDirect3DSurface8_iface;
974 IDirect3DSurface8_AddRef(*surface);
975 wined3d_texture_decref(texture);
977 wined3d_mutex_unlock();
979 return D3D_OK;
982 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
983 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
984 IDirect3DSurface8 **surface)
986 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
987 DWORD flags = 0;
989 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
990 iface, width, height, format, multisample_type, lockable, surface);
992 *surface = NULL;
993 if (lockable)
994 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
996 return d3d8_device_create_surface(device, width, height, format, flags, surface,
997 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
1000 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1001 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1002 IDirect3DSurface8 **surface)
1004 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1006 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1007 iface, width, height, format, multisample_type, surface);
1009 *surface = NULL;
1011 /* TODO: Verify that Discard is false */
1012 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1013 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1016 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1017 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1018 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1020 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1022 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1023 iface, width, height, format, surface);
1025 *surface = NULL;
1027 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1028 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1031 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1032 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1033 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1035 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1036 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1037 enum wined3d_format_id src_format, dst_format;
1038 struct wined3d_sub_resource_desc wined3d_desc;
1039 UINT src_w, src_h;
1041 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1042 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1044 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1045 * destination texture is in WINED3D_POOL_DEFAULT. */
1047 wined3d_mutex_lock();
1048 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1049 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1051 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1052 wined3d_mutex_unlock();
1053 return D3DERR_INVALIDCALL;
1055 src_format = wined3d_desc.format;
1056 src_w = wined3d_desc.width;
1057 src_h = wined3d_desc.height;
1059 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1060 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1062 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1063 wined3d_mutex_unlock();
1064 return D3DERR_INVALIDCALL;
1066 dst_format = wined3d_desc.format;
1068 /* Check that the source and destination formats match */
1069 if (src_format != dst_format)
1071 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1072 src_surface, dst_surface);
1073 wined3d_mutex_unlock();
1074 return D3DERR_INVALIDCALL;
1077 /* Quick if complete copy ... */
1078 if (!rect_count && !src_rects && !dst_points)
1080 RECT rect = {0, 0, src_w, src_h};
1081 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1082 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1084 else
1086 unsigned int i;
1087 /* Copy rect by rect */
1088 if (src_rects && dst_points)
1090 for (i = 0; i < rect_count; ++i)
1092 UINT w = src_rects[i].right - src_rects[i].left;
1093 UINT h = src_rects[i].bottom - src_rects[i].top;
1094 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1095 dst_points[i].x + w, dst_points[i].y + h};
1097 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1098 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1101 else
1103 for (i = 0; i < rect_count; ++i)
1105 UINT w = src_rects[i].right - src_rects[i].left;
1106 UINT h = src_rects[i].bottom - src_rects[i].top;
1107 RECT dst_rect = {0, 0, w, h};
1109 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1110 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1114 wined3d_mutex_unlock();
1116 return WINED3D_OK;
1119 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1120 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1122 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1123 struct d3d8_texture *src_impl, *dst_impl;
1124 HRESULT hr;
1126 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1128 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1129 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1131 wined3d_mutex_lock();
1132 hr = wined3d_device_update_texture(device->wined3d_device,
1133 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1134 wined3d_mutex_unlock();
1136 return hr;
1139 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1141 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1142 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1143 HRESULT hr;
1145 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1147 if (!dst_surface)
1149 WARN("Invalid destination surface passed.\n");
1150 return D3DERR_INVALIDCALL;
1153 wined3d_mutex_lock();
1154 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1155 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1156 wined3d_mutex_unlock();
1158 return hr;
1161 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1162 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1164 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1165 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1166 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1167 struct wined3d_rendertarget_view *original_dsv;
1168 HRESULT hr = D3D_OK;
1170 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1172 wined3d_mutex_lock();
1174 if (ds_impl)
1176 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1177 struct wined3d_rendertarget_view *original_rtv;
1178 struct d3d8_surface *original_surface;
1180 /* If no render target is passed in check the size against the current RT */
1181 if (!render_target)
1184 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1186 wined3d_mutex_unlock();
1187 return D3DERR_NOTFOUND;
1189 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1190 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1191 original_surface->sub_resource_idx, &rt_desc);
1193 else
1194 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1195 rt_impl->sub_resource_idx, &rt_desc);
1197 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1199 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1201 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1202 wined3d_mutex_unlock();
1203 return D3DERR_INVALIDCALL;
1205 if (ds_desc.multisample_type != rt_desc.multisample_type
1206 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1208 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1209 wined3d_mutex_unlock();
1210 return D3DERR_INVALIDCALL;
1215 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1216 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1217 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1218 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1219 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1220 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1222 wined3d_mutex_unlock();
1224 return hr;
1227 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1229 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1230 struct wined3d_rendertarget_view *wined3d_rtv;
1231 struct d3d8_surface *surface_impl;
1232 HRESULT hr;
1234 TRACE("iface %p, render_target %p.\n", iface, render_target);
1236 if (!render_target)
1237 return D3DERR_INVALIDCALL;
1239 wined3d_mutex_lock();
1240 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1242 /* We want the sub resource parent here, since the view itself may be
1243 * internal to wined3d and may not have a parent. */
1244 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1245 *render_target = &surface_impl->IDirect3DSurface8_iface;
1246 IDirect3DSurface8_AddRef(*render_target);
1247 hr = D3D_OK;
1249 else
1251 ERR("Failed to get wined3d render target.\n");
1252 *render_target = NULL;
1253 hr = D3DERR_NOTFOUND;
1255 wined3d_mutex_unlock();
1257 return hr;
1260 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1262 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1263 struct wined3d_rendertarget_view *wined3d_dsv;
1264 struct d3d8_surface *surface_impl;
1265 HRESULT hr = D3D_OK;
1267 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1269 if (!depth_stencil)
1270 return D3DERR_INVALIDCALL;
1272 wined3d_mutex_lock();
1273 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1275 /* We want the sub resource parent here, since the view itself may be
1276 * internal to wined3d and may not have a parent. */
1277 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1278 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1279 IDirect3DSurface8_AddRef(*depth_stencil);
1281 else
1283 hr = D3DERR_NOTFOUND;
1284 *depth_stencil = NULL;
1286 wined3d_mutex_unlock();
1288 return hr;
1291 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1293 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1294 HRESULT hr;
1296 TRACE("iface %p.\n", iface);
1298 wined3d_mutex_lock();
1299 hr = wined3d_device_begin_scene(device->wined3d_device);
1300 wined3d_mutex_unlock();
1302 return hr;
1305 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1307 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1308 HRESULT hr;
1310 TRACE("iface %p.\n", iface);
1312 wined3d_mutex_lock();
1313 hr = wined3d_device_end_scene(device->wined3d_device);
1314 wined3d_mutex_unlock();
1316 return hr;
1319 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1320 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1322 const struct wined3d_color c =
1324 ((color >> 16) & 0xff) / 255.0f,
1325 ((color >> 8) & 0xff) / 255.0f,
1326 (color & 0xff) / 255.0f,
1327 ((color >> 24) & 0xff) / 255.0f,
1329 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1330 HRESULT hr;
1332 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1333 iface, rect_count, rects, flags, color, z, stencil);
1335 if (rect_count && !rects)
1337 WARN("count %u with NULL rects.\n", rect_count);
1338 rect_count = 0;
1341 wined3d_mutex_lock();
1342 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1343 wined3d_mutex_unlock();
1345 return hr;
1348 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1349 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1351 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1353 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1355 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1356 wined3d_mutex_lock();
1357 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1358 wined3d_mutex_unlock();
1360 return D3D_OK;
1363 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1364 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1366 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1368 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1370 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1371 wined3d_mutex_lock();
1372 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1373 wined3d_mutex_unlock();
1375 return D3D_OK;
1378 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1379 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1381 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1383 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1385 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1386 wined3d_mutex_lock();
1387 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1388 wined3d_mutex_unlock();
1390 return D3D_OK;
1393 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1395 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1397 TRACE("iface %p, viewport %p.\n", iface, viewport);
1399 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1400 wined3d_mutex_lock();
1401 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1402 wined3d_mutex_unlock();
1404 return D3D_OK;
1407 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1409 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1411 TRACE("iface %p, viewport %p.\n", iface, viewport);
1413 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1414 wined3d_mutex_lock();
1415 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1416 wined3d_mutex_unlock();
1418 return D3D_OK;
1421 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1423 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1425 TRACE("iface %p, material %p.\n", iface, material);
1427 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1428 wined3d_mutex_lock();
1429 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1430 wined3d_mutex_unlock();
1432 return D3D_OK;
1435 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1437 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1439 TRACE("iface %p, material %p.\n", iface, material);
1441 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1442 wined3d_mutex_lock();
1443 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1444 wined3d_mutex_unlock();
1446 return D3D_OK;
1449 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1451 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1452 HRESULT hr;
1454 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1456 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1457 wined3d_mutex_lock();
1458 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1459 wined3d_mutex_unlock();
1461 return hr;
1464 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1466 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1467 HRESULT hr;
1469 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1471 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1472 wined3d_mutex_lock();
1473 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1474 wined3d_mutex_unlock();
1476 return hr;
1479 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1481 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1482 HRESULT hr;
1484 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1486 wined3d_mutex_lock();
1487 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1488 wined3d_mutex_unlock();
1490 return hr;
1493 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1495 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1496 HRESULT hr;
1498 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1500 wined3d_mutex_lock();
1501 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1502 wined3d_mutex_unlock();
1504 return hr;
1507 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1509 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1510 HRESULT hr;
1512 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1514 wined3d_mutex_lock();
1515 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1516 wined3d_mutex_unlock();
1518 return hr;
1521 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1523 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1524 HRESULT hr;
1526 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1528 wined3d_mutex_lock();
1529 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1530 wined3d_mutex_unlock();
1532 return hr;
1535 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1536 D3DRENDERSTATETYPE state, DWORD value)
1538 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1540 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1542 wined3d_mutex_lock();
1543 switch (state)
1545 case D3DRS_ZBIAS:
1546 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1547 break;
1549 default:
1550 wined3d_device_set_render_state(device->wined3d_device, state, value);
1552 wined3d_mutex_unlock();
1554 return D3D_OK;
1557 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1558 D3DRENDERSTATETYPE state, DWORD *value)
1560 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1562 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1564 wined3d_mutex_lock();
1565 switch (state)
1567 case D3DRS_ZBIAS:
1568 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1569 break;
1571 default:
1572 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1574 wined3d_mutex_unlock();
1576 return D3D_OK;
1579 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1581 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1582 HRESULT hr;
1584 TRACE("iface %p.\n", iface);
1586 wined3d_mutex_lock();
1587 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1588 wined3d_mutex_unlock();
1590 return hr;
1593 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1595 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1596 struct wined3d_stateblock *stateblock;
1597 HRESULT hr;
1599 TRACE("iface %p, token %p.\n", iface, token);
1601 /* Tell wineD3D to endstateblock before anything else (in case we run out
1602 * of memory later and cause locking problems)
1604 wined3d_mutex_lock();
1605 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1606 if (FAILED(hr))
1608 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1609 wined3d_mutex_unlock();
1610 return hr;
1613 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1614 wined3d_mutex_unlock();
1616 if (*token == D3D8_INVALID_HANDLE)
1618 ERR("Failed to create a handle\n");
1619 wined3d_mutex_lock();
1620 wined3d_stateblock_decref(stateblock);
1621 wined3d_mutex_unlock();
1622 return E_FAIL;
1624 ++*token;
1626 TRACE("Returning %#x (%p).\n", *token, stateblock);
1628 return hr;
1631 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1633 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1634 struct wined3d_stateblock *stateblock;
1636 TRACE("iface %p, token %#x.\n", iface, token);
1638 if (!token)
1639 return D3D_OK;
1641 wined3d_mutex_lock();
1642 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1643 if (!stateblock)
1645 WARN("Invalid handle (%#x) passed.\n", token);
1646 wined3d_mutex_unlock();
1647 return D3DERR_INVALIDCALL;
1649 wined3d_stateblock_apply(stateblock);
1650 wined3d_mutex_unlock();
1652 return D3D_OK;
1655 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1657 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1658 struct wined3d_stateblock *stateblock;
1660 TRACE("iface %p, token %#x.\n", iface, token);
1662 wined3d_mutex_lock();
1663 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1664 if (!stateblock)
1666 WARN("Invalid handle (%#x) passed.\n", token);
1667 wined3d_mutex_unlock();
1668 return D3DERR_INVALIDCALL;
1670 wined3d_stateblock_capture(stateblock);
1671 wined3d_mutex_unlock();
1673 return D3D_OK;
1676 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1678 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1679 struct wined3d_stateblock *stateblock;
1681 TRACE("iface %p, token %#x.\n", iface, token);
1683 wined3d_mutex_lock();
1684 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1686 if (!stateblock)
1688 WARN("Invalid handle (%#x) passed.\n", token);
1689 wined3d_mutex_unlock();
1690 return D3DERR_INVALIDCALL;
1693 if (wined3d_stateblock_decref(stateblock))
1695 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1697 wined3d_mutex_unlock();
1699 return D3D_OK;
1702 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1703 D3DSTATEBLOCKTYPE type, DWORD *handle)
1705 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1706 struct wined3d_stateblock *stateblock;
1707 HRESULT hr;
1709 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1711 if (type != D3DSBT_ALL
1712 && type != D3DSBT_PIXELSTATE
1713 && type != D3DSBT_VERTEXSTATE)
1715 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1716 return D3DERR_INVALIDCALL;
1719 wined3d_mutex_lock();
1720 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1721 if (FAILED(hr))
1723 wined3d_mutex_unlock();
1724 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1725 return hr;
1728 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1729 wined3d_mutex_unlock();
1731 if (*handle == D3D8_INVALID_HANDLE)
1733 ERR("Failed to allocate a handle.\n");
1734 wined3d_mutex_lock();
1735 wined3d_stateblock_decref(stateblock);
1736 wined3d_mutex_unlock();
1737 return E_FAIL;
1739 ++*handle;
1741 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1743 return hr;
1746 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1748 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1749 HRESULT hr;
1751 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1752 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1754 wined3d_mutex_lock();
1755 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1756 wined3d_mutex_unlock();
1758 return hr;
1761 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1763 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1764 HRESULT hr;
1766 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1768 wined3d_mutex_lock();
1769 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1770 wined3d_mutex_unlock();
1772 return hr;
1775 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1777 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1778 struct wined3d_texture *wined3d_texture;
1779 struct d3d8_texture *texture_impl;
1781 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1783 if (!texture)
1784 return D3DERR_INVALIDCALL;
1786 wined3d_mutex_lock();
1787 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1789 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1790 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1791 IDirect3DBaseTexture8_AddRef(*texture);
1793 else
1795 *texture = NULL;
1797 wined3d_mutex_unlock();
1799 return D3D_OK;
1802 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1804 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1805 struct d3d8_texture *texture_impl;
1806 HRESULT hr;
1808 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1810 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1812 wined3d_mutex_lock();
1813 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1814 texture_impl ? texture_impl->wined3d_texture : NULL);
1815 wined3d_mutex_unlock();
1817 return hr;
1820 static const struct tss_lookup
1822 BOOL sampler_state;
1823 enum wined3d_texture_stage_state state;
1825 tss_lookup[] =
1827 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1828 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1829 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1830 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1831 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1832 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1833 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1834 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1835 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1836 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1837 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1838 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1839 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1840 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1841 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1842 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1843 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1844 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1845 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1846 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1847 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1848 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1849 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1850 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1851 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1852 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1853 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1854 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1855 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1858 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1859 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1861 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1862 const struct tss_lookup *l;
1864 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1866 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1868 WARN("Invalid Type %#x passed.\n", Type);
1869 return D3D_OK;
1872 l = &tss_lookup[Type];
1874 wined3d_mutex_lock();
1875 if (l->sampler_state)
1876 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1877 else
1878 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1879 wined3d_mutex_unlock();
1881 return D3D_OK;
1884 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1885 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1887 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1888 const struct tss_lookup *l;
1890 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1892 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1894 WARN("Invalid type %#x passed.\n", type);
1895 return D3D_OK;
1898 l = &tss_lookup[type];
1900 wined3d_mutex_lock();
1901 if (l->sampler_state)
1902 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1903 else
1904 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1905 wined3d_mutex_unlock();
1907 return D3D_OK;
1910 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1912 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1913 HRESULT hr;
1915 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1917 wined3d_mutex_lock();
1918 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1919 wined3d_mutex_unlock();
1921 return hr;
1924 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1925 DWORD info_id, void *info, DWORD info_size)
1927 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1929 return D3D_OK;
1932 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1933 UINT palette_idx, const PALETTEENTRY *entries)
1935 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1937 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1938 * does not have a d3d8/9-style palette API */
1940 return D3D_OK;
1943 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1944 UINT palette_idx, PALETTEENTRY *entries)
1946 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1948 return D3DERR_INVALIDCALL;
1951 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1953 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1955 return D3D_OK;
1958 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1960 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1962 return D3DERR_INVALIDCALL;
1965 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1966 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1968 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1969 HRESULT hr;
1971 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1972 iface, primitive_type, start_vertex, primitive_count);
1974 wined3d_mutex_lock();
1975 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1976 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1977 vertex_count_from_primitive_count(primitive_type, primitive_count));
1978 wined3d_mutex_unlock();
1980 return hr;
1983 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1984 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1985 UINT start_idx, UINT primitive_count)
1987 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1988 HRESULT hr;
1990 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1991 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1993 wined3d_mutex_lock();
1994 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1995 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1996 vertex_count_from_primitive_count(primitive_type, primitive_count));
1997 wined3d_mutex_unlock();
1999 return hr;
2002 /* The caller is responsible for wined3d locking */
2003 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2005 HRESULT hr;
2007 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2009 UINT size = max(device->vertex_buffer_size * 2, min_size);
2010 struct wined3d_buffer *buffer;
2012 TRACE("Growing vertex buffer to %u bytes\n", size);
2014 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2015 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2016 if (FAILED(hr))
2018 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2019 return hr;
2022 if (device->vertex_buffer)
2023 wined3d_buffer_decref(device->vertex_buffer);
2025 device->vertex_buffer = buffer;
2026 device->vertex_buffer_size = size;
2027 device->vertex_buffer_pos = 0;
2029 return D3D_OK;
2032 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2033 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2034 UINT stride)
2036 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2037 HRESULT hr;
2038 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2039 UINT size = vtx_count * stride;
2040 UINT vb_pos, align;
2041 BYTE *buffer_data;
2043 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2044 iface, primitive_type, primitive_count, data, stride);
2046 if (!primitive_count)
2048 WARN("primitive_count is 0, returning D3D_OK\n");
2049 return D3D_OK;
2052 wined3d_mutex_lock();
2053 hr = d3d8_device_prepare_vertex_buffer(device, size);
2054 if (FAILED(hr))
2055 goto done;
2057 vb_pos = device->vertex_buffer_pos;
2058 align = vb_pos % stride;
2059 if (align) align = stride - align;
2060 if (vb_pos + size + align > device->vertex_buffer_size)
2061 vb_pos = 0;
2062 else
2063 vb_pos += align;
2065 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2066 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2067 if (FAILED(hr))
2068 goto done;
2069 memcpy(buffer_data, data, size);
2070 wined3d_buffer_unmap(device->vertex_buffer);
2071 device->vertex_buffer_pos = vb_pos + size;
2073 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2074 if (FAILED(hr))
2075 goto done;
2077 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2078 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2079 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2081 done:
2082 wined3d_mutex_unlock();
2083 return hr;
2086 /* The caller is responsible for wined3d locking */
2087 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2089 HRESULT hr;
2091 if (device->index_buffer_size < min_size || !device->index_buffer)
2093 UINT size = max(device->index_buffer_size * 2, min_size);
2094 struct wined3d_buffer *buffer;
2096 TRACE("Growing index buffer to %u bytes\n", size);
2098 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2099 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2100 if (FAILED(hr))
2102 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2103 return hr;
2106 if (device->index_buffer)
2107 wined3d_buffer_decref(device->index_buffer);
2109 device->index_buffer = buffer;
2110 device->index_buffer_size = size;
2111 device->index_buffer_pos = 0;
2113 return D3D_OK;
2116 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2117 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2118 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2119 const void *vertex_data, UINT vertex_stride)
2121 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2122 HRESULT hr;
2123 BYTE *buffer_data;
2125 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2126 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2127 UINT idx_size = idx_count * idx_fmt_size;
2128 UINT ib_pos;
2130 UINT vtx_size = vertex_count * vertex_stride;
2131 UINT vb_pos, align;
2133 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2134 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2135 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2136 index_data, index_format, vertex_data, vertex_stride);
2138 if (!primitive_count)
2140 WARN("primitive_count is 0, returning D3D_OK\n");
2141 return D3D_OK;
2144 wined3d_mutex_lock();
2146 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2147 if (FAILED(hr))
2148 goto done;
2150 vb_pos = device->vertex_buffer_pos;
2151 align = vb_pos % vertex_stride;
2152 if (align) align = vertex_stride - align;
2153 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2154 vb_pos = 0;
2155 else
2156 vb_pos += align;
2158 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2159 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2160 if (FAILED(hr))
2161 goto done;
2162 memcpy(buffer_data, vertex_data, vtx_size);
2163 wined3d_buffer_unmap(device->vertex_buffer);
2164 device->vertex_buffer_pos = vb_pos + vtx_size;
2166 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2167 if (FAILED(hr))
2168 goto done;
2170 ib_pos = device->index_buffer_pos;
2171 align = ib_pos % idx_fmt_size;
2172 if (align) align = idx_fmt_size - align;
2173 if (ib_pos + idx_size + align > device->index_buffer_size)
2174 ib_pos = 0;
2175 else
2176 ib_pos += align;
2178 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2179 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2180 if (FAILED(hr))
2181 goto done;
2182 memcpy(buffer_data, index_data, idx_size);
2183 wined3d_buffer_unmap(device->index_buffer);
2184 device->index_buffer_pos = ib_pos + idx_size;
2186 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2187 if (FAILED(hr))
2188 goto done;
2190 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2191 wined3dformat_from_d3dformat(index_format));
2192 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2194 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2195 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2197 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2198 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2199 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2201 done:
2202 wined3d_mutex_unlock();
2203 return hr;
2206 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2207 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2209 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2210 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2211 HRESULT hr;
2213 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2214 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2216 wined3d_mutex_lock();
2217 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2218 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2219 wined3d_mutex_unlock();
2221 return hr;
2224 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2225 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2227 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2228 struct d3d8_vertex_shader *object;
2229 DWORD shader_handle;
2230 DWORD handle;
2231 HRESULT hr;
2233 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2234 iface, declaration, byte_code, shader, usage);
2236 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2237 if (!object)
2239 *shader = 0;
2240 return E_OUTOFMEMORY;
2243 wined3d_mutex_lock();
2244 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2245 wined3d_mutex_unlock();
2246 if (handle == D3D8_INVALID_HANDLE)
2248 ERR("Failed to allocate vertex shader handle.\n");
2249 HeapFree(GetProcessHeap(), 0, object);
2250 *shader = 0;
2251 return E_OUTOFMEMORY;
2254 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2256 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2257 if (FAILED(hr))
2259 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2260 wined3d_mutex_lock();
2261 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2262 wined3d_mutex_unlock();
2263 HeapFree(GetProcessHeap(), 0, object);
2264 *shader = 0;
2265 return hr;
2268 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2269 *shader = shader_handle;
2271 return D3D_OK;
2274 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2276 struct d3d8_vertex_declaration *d3d8_declaration;
2277 struct FvfToDecl *convertedDecls = device->decls;
2278 int p, low, high; /* deliberately signed */
2279 HRESULT hr;
2281 TRACE("Searching for declaration for fvf %08x... ", fvf);
2283 low = 0;
2284 high = device->numConvertedDecls - 1;
2285 while (low <= high)
2287 p = (low + high) >> 1;
2288 TRACE("%d ", p);
2290 if (convertedDecls[p].fvf == fvf)
2292 TRACE("found %p\n", convertedDecls[p].declaration);
2293 return convertedDecls[p].declaration;
2296 if (convertedDecls[p].fvf < fvf)
2297 low = p + 1;
2298 else
2299 high = p - 1;
2301 TRACE("not found. Creating and inserting at position %d.\n", low);
2303 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2304 return NULL;
2306 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2308 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2309 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2310 return NULL;
2313 if (device->declArraySize == device->numConvertedDecls)
2315 UINT grow = device->declArraySize / 2;
2317 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2318 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2319 if (!convertedDecls)
2321 d3d8_vertex_declaration_destroy(d3d8_declaration);
2322 return NULL;
2324 device->decls = convertedDecls;
2325 device->declArraySize += grow;
2328 memmove(convertedDecls + low + 1, convertedDecls + low,
2329 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2330 convertedDecls[low].declaration = d3d8_declaration;
2331 convertedDecls[low].fvf = fvf;
2332 ++device->numConvertedDecls;
2334 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2336 return d3d8_declaration;
2339 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2341 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2342 struct d3d8_vertex_shader *shader_impl;
2344 TRACE("iface %p, shader %#x.\n", iface, shader);
2346 if (VS_HIGHESTFIXEDFXF >= shader)
2348 TRACE("Setting FVF, %#x\n", shader);
2350 wined3d_mutex_lock();
2351 wined3d_device_set_vertex_declaration(device->wined3d_device,
2352 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2353 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2354 wined3d_mutex_unlock();
2356 return D3D_OK;
2359 TRACE("Setting shader\n");
2361 wined3d_mutex_lock();
2362 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2364 WARN("Invalid handle (%#x) passed.\n", shader);
2365 wined3d_mutex_unlock();
2367 return D3DERR_INVALIDCALL;
2370 wined3d_device_set_vertex_declaration(device->wined3d_device,
2371 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2372 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2373 wined3d_mutex_unlock();
2375 return D3D_OK;
2378 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2380 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2381 struct wined3d_vertex_declaration *wined3d_declaration;
2382 struct d3d8_vertex_declaration *d3d8_declaration;
2384 TRACE("iface %p, shader %p.\n", iface, shader);
2386 wined3d_mutex_lock();
2387 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2389 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2390 *shader = d3d8_declaration->shader_handle;
2392 else
2394 *shader = 0;
2396 wined3d_mutex_unlock();
2398 TRACE("Returning %#x.\n", *shader);
2400 return D3D_OK;
2403 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2405 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2406 struct d3d8_vertex_shader *shader_impl;
2408 TRACE("iface %p, shader %#x.\n", iface, shader);
2410 wined3d_mutex_lock();
2411 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2413 WARN("Invalid handle (%#x) passed.\n", shader);
2414 wined3d_mutex_unlock();
2416 return D3DERR_INVALIDCALL;
2419 if (shader_impl->wined3d_shader
2420 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2421 IDirect3DDevice8_SetVertexShader(iface, 0);
2423 wined3d_mutex_unlock();
2425 d3d8_vertex_shader_destroy(shader_impl);
2427 return D3D_OK;
2430 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2431 DWORD start_register, const void *data, DWORD count)
2433 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2434 HRESULT hr;
2436 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2437 iface, start_register, data, count);
2439 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2441 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2442 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2443 return D3DERR_INVALIDCALL;
2446 wined3d_mutex_lock();
2447 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2448 wined3d_mutex_unlock();
2450 return hr;
2453 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2454 DWORD start_register, void *data, DWORD count)
2456 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2457 HRESULT hr;
2459 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2460 iface, start_register, data, count);
2462 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2464 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2465 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2466 return D3DERR_INVALIDCALL;
2469 wined3d_mutex_lock();
2470 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2471 wined3d_mutex_unlock();
2473 return hr;
2476 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2477 DWORD shader, void *data, DWORD *data_size)
2479 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2480 struct d3d8_vertex_declaration *declaration;
2481 struct d3d8_vertex_shader *shader_impl;
2483 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2484 iface, shader, data, data_size);
2486 wined3d_mutex_lock();
2487 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2488 wined3d_mutex_unlock();
2490 if (!shader_impl)
2492 WARN("Invalid handle (%#x) passed.\n", shader);
2493 return D3DERR_INVALIDCALL;
2495 declaration = shader_impl->vertex_declaration;
2497 if (!data)
2499 *data_size = declaration->elements_size;
2500 return D3D_OK;
2503 /* MSDN claims that if *data_size is smaller than the required size
2504 * we should write the required size and return D3DERR_MOREDATA.
2505 * That's not actually true. */
2506 if (*data_size < declaration->elements_size)
2507 return D3DERR_INVALIDCALL;
2509 memcpy(data, declaration->elements, declaration->elements_size);
2511 return D3D_OK;
2514 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2515 DWORD shader, void *data, DWORD *data_size)
2517 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2518 struct d3d8_vertex_shader *shader_impl = NULL;
2519 HRESULT hr;
2521 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2522 iface, shader, data, data_size);
2524 wined3d_mutex_lock();
2525 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2527 WARN("Invalid handle (%#x) passed.\n", shader);
2528 wined3d_mutex_unlock();
2530 return D3DERR_INVALIDCALL;
2533 if (!shader_impl->wined3d_shader)
2535 wined3d_mutex_unlock();
2536 *data_size = 0;
2537 return D3D_OK;
2540 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2541 wined3d_mutex_unlock();
2543 return hr;
2546 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2547 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2549 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2550 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2552 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2554 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2555 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2556 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2557 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2558 * problem)
2560 wined3d_mutex_lock();
2561 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2562 wined3d_device_set_index_buffer(device->wined3d_device,
2563 ib ? ib->wined3d_buffer : NULL,
2564 ib ? ib->format : WINED3DFMT_UNKNOWN);
2565 wined3d_mutex_unlock();
2567 return D3D_OK;
2570 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2571 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2574 enum wined3d_format_id wined3d_format;
2575 struct wined3d_buffer *wined3d_buffer;
2576 struct d3d8_indexbuffer *buffer_impl;
2578 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2580 if (!buffer)
2581 return D3DERR_INVALIDCALL;
2583 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2584 wined3d_mutex_lock();
2585 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2586 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2588 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2589 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2590 IDirect3DIndexBuffer8_AddRef(*buffer);
2592 else
2594 *buffer = NULL;
2596 wined3d_mutex_unlock();
2598 return D3D_OK;
2601 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2602 const DWORD *byte_code, DWORD *shader)
2604 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2605 struct d3d8_pixel_shader *object;
2606 DWORD shader_handle;
2607 DWORD handle;
2608 HRESULT hr;
2610 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2612 if (!shader)
2613 return D3DERR_INVALIDCALL;
2615 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2616 if (!object)
2617 return E_OUTOFMEMORY;
2619 wined3d_mutex_lock();
2620 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2621 wined3d_mutex_unlock();
2622 if (handle == D3D8_INVALID_HANDLE)
2624 ERR("Failed to allocate pixel shader handle.\n");
2625 HeapFree(GetProcessHeap(), 0, object);
2626 return E_OUTOFMEMORY;
2629 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2631 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2632 if (FAILED(hr))
2634 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2635 wined3d_mutex_lock();
2636 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2637 wined3d_mutex_unlock();
2638 HeapFree(GetProcessHeap(), 0, object);
2639 *shader = 0;
2640 return hr;
2643 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2644 *shader = shader_handle;
2646 return D3D_OK;
2649 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2652 struct d3d8_pixel_shader *shader_impl;
2654 TRACE("iface %p, shader %#x.\n", iface, shader);
2656 wined3d_mutex_lock();
2658 if (!shader)
2660 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2661 wined3d_mutex_unlock();
2662 return D3D_OK;
2665 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2667 WARN("Invalid handle (%#x) passed.\n", shader);
2668 wined3d_mutex_unlock();
2669 return D3DERR_INVALIDCALL;
2672 TRACE("Setting shader %p.\n", shader_impl);
2673 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2674 wined3d_mutex_unlock();
2676 return D3D_OK;
2679 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2681 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2682 struct wined3d_shader *object;
2684 TRACE("iface %p, shader %p.\n", iface, shader);
2686 if (!shader)
2687 return D3DERR_INVALIDCALL;
2689 wined3d_mutex_lock();
2690 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2692 struct d3d8_pixel_shader *d3d8_shader;
2693 d3d8_shader = wined3d_shader_get_parent(object);
2694 *shader = d3d8_shader->handle;
2696 else
2698 *shader = 0;
2700 wined3d_mutex_unlock();
2702 TRACE("Returning %#x.\n", *shader);
2704 return D3D_OK;
2707 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2709 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2710 struct d3d8_pixel_shader *shader_impl;
2712 TRACE("iface %p, shader %#x.\n", iface, shader);
2714 wined3d_mutex_lock();
2716 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2718 WARN("Invalid handle (%#x) passed.\n", shader);
2719 wined3d_mutex_unlock();
2720 return D3DERR_INVALIDCALL;
2723 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2724 IDirect3DDevice8_SetPixelShader(iface, 0);
2726 wined3d_mutex_unlock();
2728 d3d8_pixel_shader_destroy(shader_impl);
2730 return D3D_OK;
2733 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2734 DWORD start_register, const void *data, DWORD count)
2736 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2737 HRESULT hr;
2739 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2740 iface, start_register, data, count);
2742 wined3d_mutex_lock();
2743 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2744 wined3d_mutex_unlock();
2746 return hr;
2749 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2750 DWORD start_register, void *data, DWORD count)
2752 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2753 HRESULT hr;
2755 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2756 iface, start_register, data, count);
2758 wined3d_mutex_lock();
2759 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2760 wined3d_mutex_unlock();
2762 return hr;
2765 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2766 DWORD shader, void *data, DWORD *data_size)
2768 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2769 struct d3d8_pixel_shader *shader_impl = NULL;
2770 HRESULT hr;
2772 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2773 iface, shader, data, data_size);
2775 wined3d_mutex_lock();
2776 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2778 WARN("Invalid handle (%#x) passed.\n", shader);
2779 wined3d_mutex_unlock();
2781 return D3DERR_INVALIDCALL;
2784 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2785 wined3d_mutex_unlock();
2787 return hr;
2790 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2791 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2793 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2794 iface, handle, segment_count, patch_info);
2795 return D3D_OK;
2798 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2799 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2801 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2802 iface, handle, segment_count, patch_info);
2803 return D3D_OK;
2806 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2808 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2809 return D3DERR_INVALIDCALL;
2812 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2813 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2815 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2816 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2817 HRESULT hr;
2819 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2820 iface, stream_idx, buffer, stride);
2822 wined3d_mutex_lock();
2823 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2824 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2825 wined3d_mutex_unlock();
2827 return hr;
2830 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2831 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2833 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2834 struct d3d8_vertexbuffer *buffer_impl;
2835 struct wined3d_buffer *wined3d_buffer = NULL;
2836 HRESULT hr;
2838 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2839 iface, stream_idx, buffer, stride);
2841 if (!buffer)
2842 return D3DERR_INVALIDCALL;
2844 wined3d_mutex_lock();
2845 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2846 if (SUCCEEDED(hr) && wined3d_buffer)
2848 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2849 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2850 IDirect3DVertexBuffer8_AddRef(*buffer);
2852 else
2854 if (FAILED(hr))
2855 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2856 *buffer = NULL;
2858 wined3d_mutex_unlock();
2860 return hr;
2863 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2865 d3d8_device_QueryInterface,
2866 d3d8_device_AddRef,
2867 d3d8_device_Release,
2868 d3d8_device_TestCooperativeLevel,
2869 d3d8_device_GetAvailableTextureMem,
2870 d3d8_device_ResourceManagerDiscardBytes,
2871 d3d8_device_GetDirect3D,
2872 d3d8_device_GetDeviceCaps,
2873 d3d8_device_GetDisplayMode,
2874 d3d8_device_GetCreationParameters,
2875 d3d8_device_SetCursorProperties,
2876 d3d8_device_SetCursorPosition,
2877 d3d8_device_ShowCursor,
2878 d3d8_device_CreateAdditionalSwapChain,
2879 d3d8_device_Reset,
2880 d3d8_device_Present,
2881 d3d8_device_GetBackBuffer,
2882 d3d8_device_GetRasterStatus,
2883 d3d8_device_SetGammaRamp,
2884 d3d8_device_GetGammaRamp,
2885 d3d8_device_CreateTexture,
2886 d3d8_device_CreateVolumeTexture,
2887 d3d8_device_CreateCubeTexture,
2888 d3d8_device_CreateVertexBuffer,
2889 d3d8_device_CreateIndexBuffer,
2890 d3d8_device_CreateRenderTarget,
2891 d3d8_device_CreateDepthStencilSurface,
2892 d3d8_device_CreateImageSurface,
2893 d3d8_device_CopyRects,
2894 d3d8_device_UpdateTexture,
2895 d3d8_device_GetFrontBuffer,
2896 d3d8_device_SetRenderTarget,
2897 d3d8_device_GetRenderTarget,
2898 d3d8_device_GetDepthStencilSurface,
2899 d3d8_device_BeginScene,
2900 d3d8_device_EndScene,
2901 d3d8_device_Clear,
2902 d3d8_device_SetTransform,
2903 d3d8_device_GetTransform,
2904 d3d8_device_MultiplyTransform,
2905 d3d8_device_SetViewport,
2906 d3d8_device_GetViewport,
2907 d3d8_device_SetMaterial,
2908 d3d8_device_GetMaterial,
2909 d3d8_device_SetLight,
2910 d3d8_device_GetLight,
2911 d3d8_device_LightEnable,
2912 d3d8_device_GetLightEnable,
2913 d3d8_device_SetClipPlane,
2914 d3d8_device_GetClipPlane,
2915 d3d8_device_SetRenderState,
2916 d3d8_device_GetRenderState,
2917 d3d8_device_BeginStateBlock,
2918 d3d8_device_EndStateBlock,
2919 d3d8_device_ApplyStateBlock,
2920 d3d8_device_CaptureStateBlock,
2921 d3d8_device_DeleteStateBlock,
2922 d3d8_device_CreateStateBlock,
2923 d3d8_device_SetClipStatus,
2924 d3d8_device_GetClipStatus,
2925 d3d8_device_GetTexture,
2926 d3d8_device_SetTexture,
2927 d3d8_device_GetTextureStageState,
2928 d3d8_device_SetTextureStageState,
2929 d3d8_device_ValidateDevice,
2930 d3d8_device_GetInfo,
2931 d3d8_device_SetPaletteEntries,
2932 d3d8_device_GetPaletteEntries,
2933 d3d8_device_SetCurrentTexturePalette,
2934 d3d8_device_GetCurrentTexturePalette,
2935 d3d8_device_DrawPrimitive,
2936 d3d8_device_DrawIndexedPrimitive,
2937 d3d8_device_DrawPrimitiveUP,
2938 d3d8_device_DrawIndexedPrimitiveUP,
2939 d3d8_device_ProcessVertices,
2940 d3d8_device_CreateVertexShader,
2941 d3d8_device_SetVertexShader,
2942 d3d8_device_GetVertexShader,
2943 d3d8_device_DeleteVertexShader,
2944 d3d8_device_SetVertexShaderConstant,
2945 d3d8_device_GetVertexShaderConstant,
2946 d3d8_device_GetVertexShaderDeclaration,
2947 d3d8_device_GetVertexShaderFunction,
2948 d3d8_device_SetStreamSource,
2949 d3d8_device_GetStreamSource,
2950 d3d8_device_SetIndices,
2951 d3d8_device_GetIndices,
2952 d3d8_device_CreatePixelShader,
2953 d3d8_device_SetPixelShader,
2954 d3d8_device_GetPixelShader,
2955 d3d8_device_DeletePixelShader,
2956 d3d8_device_SetPixelShaderConstant,
2957 d3d8_device_GetPixelShaderConstant,
2958 d3d8_device_GetPixelShaderFunction,
2959 d3d8_device_DrawRectPatch,
2960 d3d8_device_DrawTriPatch,
2961 d3d8_device_DeletePatch,
2964 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2966 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2969 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2970 struct wined3d_device *device)
2972 TRACE("device_parent %p, device %p\n", device_parent, device);
2975 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2977 TRACE("device_parent %p.\n", device_parent);
2980 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2982 struct d3d8_device *device = device_from_device_parent(device_parent);
2984 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2986 if (!activate)
2987 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2988 else
2989 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2992 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2993 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
2994 void **parent, const struct wined3d_parent_ops **parent_ops)
2996 struct d3d8_surface *d3d_surface;
2998 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
2999 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3001 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3002 return E_OUTOFMEMORY;
3004 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3005 *parent = d3d_surface;
3006 TRACE("Created surface %p.\n", d3d_surface);
3008 return D3D_OK;
3011 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3012 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3013 void **parent, const struct wined3d_parent_ops **parent_ops)
3015 struct d3d8_volume *d3d_volume;
3017 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3018 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3020 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3021 return E_OUTOFMEMORY;
3023 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3024 *parent = d3d_volume;
3025 TRACE("Created volume %p.\n", d3d_volume);
3027 return D3D_OK;
3030 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3031 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3033 struct d3d8_device *device = device_from_device_parent(device_parent);
3034 struct d3d8_surface *d3d_surface;
3035 HRESULT hr;
3037 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3038 device_parent, container_parent, desc, texture);
3040 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
3041 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3043 WARN("Failed to create texture, hr %#x.\n", hr);
3044 return hr;
3047 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3048 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3050 return hr;
3053 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3054 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3056 struct d3d8_device *device = device_from_device_parent(device_parent);
3057 struct d3d8_swapchain *d3d_swapchain;
3058 HRESULT hr;
3060 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3062 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3064 WARN("Failed to create swapchain, hr %#x.\n", hr);
3065 *swapchain = NULL;
3066 return hr;
3069 *swapchain = d3d_swapchain->wined3d_swapchain;
3070 wined3d_swapchain_incref(*swapchain);
3071 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3073 return hr;
3076 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3078 device_parent_wined3d_device_created,
3079 device_parent_mode_changed,
3080 device_parent_activate,
3081 device_parent_surface_created,
3082 device_parent_volume_created,
3083 device_parent_create_swapchain_texture,
3084 device_parent_create_swapchain,
3087 static void setup_fpu(void)
3089 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3090 WORD cw;
3091 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3092 cw = (cw & ~0xf3f) | 0x3f;
3093 __asm__ volatile ("fldcw %0" : : "m" (cw));
3094 #elif defined(__i386__) && defined(_MSC_VER)
3095 WORD cw;
3096 __asm fnstcw cw;
3097 cw = (cw & ~0xf3f) | 0x3f;
3098 __asm fldcw cw;
3099 #else
3100 FIXME("FPU setup not implemented for this platform.\n");
3101 #endif
3104 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3105 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3107 struct wined3d_swapchain_desc swapchain_desc;
3108 struct wined3d_swapchain *wined3d_swapchain;
3109 HRESULT hr;
3111 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3112 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3113 device->ref = 1;
3114 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3115 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3116 if (!device->handle_table.entries)
3118 ERR("Failed to allocate handle table memory.\n");
3119 return E_OUTOFMEMORY;
3121 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3123 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3125 wined3d_mutex_lock();
3126 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3127 &device->device_parent, &device->wined3d_device);
3128 if (FAILED(hr))
3130 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3131 wined3d_mutex_unlock();
3132 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3133 return hr;
3136 if (!parameters->Windowed)
3138 HWND device_window = parameters->hDeviceWindow;
3140 if (!focus_window)
3141 focus_window = device_window;
3142 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3144 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3145 wined3d_device_decref(device->wined3d_device);
3146 wined3d_mutex_unlock();
3147 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3148 return hr;
3151 if (!device_window)
3152 device_window = focus_window;
3153 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3154 parameters->BackBufferWidth,
3155 parameters->BackBufferHeight);
3158 if (flags & D3DCREATE_MULTITHREADED)
3159 wined3d_device_set_multithreaded(device->wined3d_device);
3161 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3163 wined3d_device_release_focus_window(device->wined3d_device);
3164 wined3d_device_decref(device->wined3d_device);
3165 wined3d_mutex_unlock();
3166 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3167 return D3DERR_INVALIDCALL;
3170 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3171 if (FAILED(hr))
3173 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3174 wined3d_device_release_focus_window(device->wined3d_device);
3175 wined3d_device_decref(device->wined3d_device);
3176 wined3d_mutex_unlock();
3177 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3178 return hr;
3181 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3182 wined3d_mutex_unlock();
3184 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3186 device->declArraySize = 16;
3187 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3188 if (!device->decls)
3190 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3191 hr = E_OUTOFMEMORY;
3192 goto err;
3195 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3196 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3198 device->d3d_parent = &parent->IDirect3D8_iface;
3199 IDirect3D8_AddRef(device->d3d_parent);
3201 return D3D_OK;
3203 err:
3204 wined3d_mutex_lock();
3205 wined3d_device_uninit_3d(device->wined3d_device);
3206 wined3d_device_release_focus_window(device->wined3d_device);
3207 wined3d_device_decref(device->wined3d_device);
3208 wined3d_mutex_unlock();
3209 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3210 return hr;