dbghelp: Don't parse the DWARF info from Mach-O files if we were requested to only...
[wine.git] / dlls / d3d8 / device.c
blob26cc9acd968ed6c9b5891389a2e15031041abf59
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 void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
192 const D3DPRESENT_PARAMETERS *present_parameters)
194 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
195 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
196 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
197 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
198 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
199 swapchain_desc->multisample_quality = 0; /* d3d9 only */
200 swapchain_desc->swap_effect = present_parameters->SwapEffect;
201 swapchain_desc->device_window = present_parameters->hDeviceWindow;
202 swapchain_desc->windowed = present_parameters->Windowed;
203 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
204 swapchain_desc->auto_depth_stencil_format
205 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
206 swapchain_desc->flags = present_parameters->Flags;
207 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
208 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
209 swapchain_desc->auto_restore_display_mode = TRUE;
212 /* Handle table functions */
213 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
215 struct d3d8_handle_entry *entry;
217 if (t->free_entries)
219 DWORD index = t->free_entries - t->entries;
220 /* Use a free handle */
221 entry = t->free_entries;
222 if (entry->type != D3D8_HANDLE_FREE)
224 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
225 return D3D8_INVALID_HANDLE;
227 t->free_entries = entry->object;
228 entry->object = object;
229 entry->type = type;
231 return index;
234 if (!(t->entry_count < t->table_size))
236 /* Grow the table */
237 UINT new_size = t->table_size + (t->table_size >> 1);
238 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
239 0, t->entries, new_size * sizeof(*t->entries));
240 if (!new_entries)
242 ERR("Failed to grow the handle table.\n");
243 return D3D8_INVALID_HANDLE;
245 t->entries = new_entries;
246 t->table_size = new_size;
249 entry = &t->entries[t->entry_count];
250 entry->object = object;
251 entry->type = type;
253 return t->entry_count++;
256 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
258 struct d3d8_handle_entry *entry;
259 void *object;
261 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
263 WARN("Invalid handle %u passed.\n", handle);
264 return NULL;
267 entry = &t->entries[handle];
268 if (entry->type != type)
270 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
271 return NULL;
274 object = entry->object;
275 entry->object = t->free_entries;
276 entry->type = D3D8_HANDLE_FREE;
277 t->free_entries = entry;
279 return object;
282 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
284 struct d3d8_handle_entry *entry;
286 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
288 WARN("Invalid handle %u passed.\n", handle);
289 return NULL;
292 entry = &t->entries[handle];
293 if (entry->type != type)
295 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
296 return NULL;
299 return entry->object;
302 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
304 TRACE("iface %p, riid %s, out %p.\n",
305 iface, debugstr_guid(riid), out);
307 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
308 || IsEqualGUID(riid, &IID_IUnknown))
310 IDirect3DDevice8_AddRef(iface);
311 *out = iface;
312 return S_OK;
315 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
317 *out = NULL;
318 return E_NOINTERFACE;
321 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
323 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
324 ULONG ref = InterlockedIncrement(&device->ref);
326 TRACE("%p increasing refcount to %u.\n", iface, ref);
328 return ref;
331 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
333 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
334 ULONG ref;
336 if (device->inDestruction)
337 return 0;
339 ref = InterlockedDecrement(&device->ref);
341 TRACE("%p decreasing refcount to %u.\n", iface, ref);
343 if (!ref)
345 IDirect3D8 *parent = device->d3d_parent;
346 unsigned i;
348 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
350 wined3d_mutex_lock();
352 device->inDestruction = TRUE;
354 for (i = 0; i < device->numConvertedDecls; ++i)
356 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
358 HeapFree(GetProcessHeap(), 0, device->decls);
360 if (device->vertex_buffer)
361 wined3d_buffer_decref(device->vertex_buffer);
362 if (device->index_buffer)
363 wined3d_buffer_decref(device->index_buffer);
365 wined3d_device_uninit_3d(device->wined3d_device);
366 wined3d_device_release_focus_window(device->wined3d_device);
367 wined3d_device_decref(device->wined3d_device);
368 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
369 HeapFree(GetProcessHeap(), 0, device);
371 wined3d_mutex_unlock();
373 IDirect3D8_Release(parent);
375 return ref;
378 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
380 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
382 TRACE("iface %p.\n", iface);
384 TRACE("device state: %#x.\n", device->device_state);
386 switch (device->device_state)
388 default:
389 case D3D8_DEVICE_STATE_OK:
390 return D3D_OK;
391 case D3D8_DEVICE_STATE_LOST:
392 return D3DERR_DEVICELOST;
393 case D3D8_DEVICE_STATE_NOT_RESET:
394 return D3DERR_DEVICENOTRESET;
398 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
400 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
401 UINT ret;
403 TRACE("iface %p.\n", iface);
405 wined3d_mutex_lock();
406 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
407 wined3d_mutex_unlock();
409 return ret;
412 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
414 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
416 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
418 if (byte_count)
419 FIXME("Byte count ignored.\n");
421 wined3d_mutex_lock();
422 wined3d_device_evict_managed_resources(device->wined3d_device);
423 wined3d_mutex_unlock();
425 return D3D_OK;
428 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
430 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
432 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
434 if (!d3d8)
435 return D3DERR_INVALIDCALL;
437 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
440 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
442 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
443 WINED3DCAPS *wined3d_caps;
444 HRESULT hr;
446 TRACE("iface %p, caps %p.\n", iface, caps);
448 if (!caps)
449 return D3DERR_INVALIDCALL;
451 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
452 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
454 wined3d_mutex_lock();
455 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
456 wined3d_mutex_unlock();
458 fixup_caps(wined3d_caps);
459 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
460 HeapFree(GetProcessHeap(), 0, wined3d_caps);
462 return hr;
465 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
467 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
468 struct wined3d_display_mode wined3d_mode;
469 HRESULT hr;
471 TRACE("iface %p, mode %p.\n", iface, mode);
473 wined3d_mutex_lock();
474 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
475 wined3d_mutex_unlock();
477 if (SUCCEEDED(hr))
479 mode->Width = wined3d_mode.width;
480 mode->Height = wined3d_mode.height;
481 mode->RefreshRate = wined3d_mode.refresh_rate;
482 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
485 return hr;
488 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
489 D3DDEVICE_CREATION_PARAMETERS *parameters)
491 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
493 TRACE("iface %p, parameters %p.\n", iface, parameters);
495 wined3d_mutex_lock();
496 wined3d_device_get_creation_parameters(device->wined3d_device,
497 (struct wined3d_device_creation_parameters *)parameters);
498 wined3d_mutex_unlock();
500 return D3D_OK;
503 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
504 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
506 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
507 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
508 HRESULT hr;
510 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
511 iface, hotspot_x, hotspot_y, bitmap);
513 if (!bitmap)
515 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
516 return D3DERR_INVALIDCALL;
519 wined3d_mutex_lock();
520 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
521 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
522 wined3d_mutex_unlock();
524 return hr;
527 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
529 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
531 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
533 wined3d_mutex_lock();
534 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
535 wined3d_mutex_unlock();
538 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
540 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
541 BOOL ret;
543 TRACE("iface %p, show %#x.\n", iface, show);
545 wined3d_mutex_lock();
546 ret = wined3d_device_show_cursor(device->wined3d_device, show);
547 wined3d_mutex_unlock();
549 return ret;
552 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
553 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
555 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
556 struct wined3d_swapchain_desc desc;
557 struct d3d8_swapchain *object;
558 UINT i, count;
559 HRESULT hr;
561 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
562 iface, present_parameters, swapchain);
564 if (!present_parameters->Windowed)
566 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
567 return D3DERR_INVALIDCALL;
570 wined3d_mutex_lock();
571 count = wined3d_device_get_swapchain_count(device->wined3d_device);
572 for (i = 0; i < count; ++i)
574 struct wined3d_swapchain *wined3d_swapchain;
576 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
577 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
579 if (!desc.windowed)
581 wined3d_mutex_unlock();
582 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
583 return D3DERR_INVALIDCALL;
586 wined3d_mutex_unlock();
588 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
589 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
590 *swapchain = &object->IDirect3DSwapChain8_iface;
591 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
593 return hr;
596 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
598 struct wined3d_resource_desc desc;
600 wined3d_resource_get_desc(resource, &desc);
601 if (desc.pool == WINED3D_POOL_DEFAULT)
603 struct d3d8_surface *surface;
605 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
607 IUnknown *parent = wined3d_resource_get_parent(resource);
608 IDirect3DBaseTexture8 *texture;
610 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
612 IDirect3DBaseTexture8_Release(texture);
613 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
614 return D3DERR_DEVICELOST;
617 return D3D_OK;
620 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
622 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
623 return D3DERR_DEVICELOST;
626 surface = wined3d_resource_get_parent(resource);
627 if (surface->resource.refcount)
629 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
630 return D3DERR_DEVICELOST;
633 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
636 return D3D_OK;
639 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
640 D3DPRESENT_PARAMETERS *present_parameters)
642 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
643 struct wined3d_swapchain_desc swapchain_desc;
644 HRESULT hr;
646 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
648 if (device->device_state == D3D8_DEVICE_STATE_LOST)
650 WARN("App not active, returning D3DERR_DEVICELOST.\n");
651 return D3DERR_DEVICELOST;
654 wined3d_mutex_lock();
656 if (device->vertex_buffer)
658 wined3d_buffer_decref(device->vertex_buffer);
659 device->vertex_buffer = NULL;
660 device->vertex_buffer_size = 0;
662 if (device->index_buffer)
664 wined3d_buffer_decref(device->index_buffer);
665 device->index_buffer = NULL;
666 device->index_buffer_size = 0;
669 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
670 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
671 NULL, reset_enum_callback, TRUE)))
673 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
674 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
675 device->device_state = D3D8_DEVICE_STATE_OK;
677 else
679 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
681 wined3d_mutex_unlock();
683 return hr;
686 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
687 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
689 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
690 HRESULT hr;
692 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
693 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
695 if (device->device_state != D3D8_DEVICE_STATE_OK)
696 return D3DERR_DEVICELOST;
698 wined3d_mutex_lock();
699 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
700 dst_window_override, dirty_region, 0);
701 wined3d_mutex_unlock();
703 return hr;
706 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
707 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
709 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
710 struct wined3d_surface *wined3d_surface = NULL;
711 struct d3d8_surface *surface_impl;
712 HRESULT hr;
714 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
715 iface, backbuffer_idx, backbuffer_type, backbuffer);
717 wined3d_mutex_lock();
718 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
719 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
720 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
722 surface_impl = wined3d_surface_get_parent(wined3d_surface);
723 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
724 IDirect3DSurface8_AddRef(*backbuffer);
726 wined3d_mutex_unlock();
728 return hr;
731 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
733 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
734 HRESULT hr;
736 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
738 wined3d_mutex_lock();
739 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
740 wined3d_mutex_unlock();
742 return hr;
745 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
747 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
749 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
751 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
752 wined3d_mutex_lock();
753 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
754 wined3d_mutex_unlock();
757 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
759 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
761 TRACE("iface %p, ramp %p.\n", iface, ramp);
763 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
764 wined3d_mutex_lock();
765 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
766 wined3d_mutex_unlock();
769 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
770 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
771 D3DPOOL pool, IDirect3DTexture8 **texture)
773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
774 struct d3d8_texture *object;
775 HRESULT hr;
777 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
778 iface, width, height, levels, usage, format, pool, texture);
780 *texture = NULL;
781 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
782 if (!object)
783 return D3DERR_OUTOFVIDEOMEMORY;
785 hr = texture_init(object, device, width, height, levels, usage, format, pool);
786 if (FAILED(hr))
788 WARN("Failed to initialize texture, hr %#x.\n", hr);
789 HeapFree(GetProcessHeap(), 0, object);
790 return hr;
793 TRACE("Created texture %p.\n", object);
794 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
796 return D3D_OK;
799 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
800 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
801 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
803 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
804 struct d3d8_texture *object;
805 HRESULT hr;
807 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
808 iface, width, height, depth, levels, usage, format, pool, texture);
810 *texture = NULL;
811 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
812 if (!object)
813 return D3DERR_OUTOFVIDEOMEMORY;
815 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
816 if (FAILED(hr))
818 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
819 HeapFree(GetProcessHeap(), 0, object);
820 return hr;
823 TRACE("Created volume texture %p.\n", object);
824 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
826 return D3D_OK;
829 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
830 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
832 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
833 struct d3d8_texture *object;
834 HRESULT hr;
836 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
837 iface, edge_length, levels, usage, format, pool, texture);
839 *texture = NULL;
840 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
841 if (!object)
842 return D3DERR_OUTOFVIDEOMEMORY;
844 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
845 if (FAILED(hr))
847 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
848 HeapFree(GetProcessHeap(), 0, object);
849 return hr;
852 TRACE("Created cube texture %p.\n", object);
853 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
855 return hr;
858 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
859 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
861 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
862 struct d3d8_vertexbuffer *object;
863 HRESULT hr;
865 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
866 iface, size, usage, fvf, pool, buffer);
868 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
869 if (!object)
870 return D3DERR_OUTOFVIDEOMEMORY;
872 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
873 if (FAILED(hr))
875 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
876 HeapFree(GetProcessHeap(), 0, object);
877 return hr;
880 TRACE("Created vertex buffer %p.\n", object);
881 *buffer = &object->IDirect3DVertexBuffer8_iface;
883 return D3D_OK;
886 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
887 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
889 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
890 struct d3d8_indexbuffer *object;
891 HRESULT hr;
893 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
894 iface, size, usage, format, pool, buffer);
896 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
897 if (!object)
898 return D3DERR_OUTOFVIDEOMEMORY;
900 hr = indexbuffer_init(object, device, size, usage, format, pool);
901 if (FAILED(hr))
903 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
904 HeapFree(GetProcessHeap(), 0, object);
905 return hr;
908 TRACE("Created index buffer %p.\n", object);
909 *buffer = &object->IDirect3DIndexBuffer8_iface;
911 return D3D_OK;
914 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
915 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
916 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
918 struct wined3d_resource *sub_resource;
919 struct wined3d_resource_desc desc;
920 struct d3d8_surface *surface_impl;
921 struct wined3d_texture *texture;
922 HRESULT hr;
924 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
925 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
926 device, width, height, format, flags, surface,
927 usage, pool, multisample_type, multisample_quality);
929 desc.resource_type = WINED3D_RTYPE_TEXTURE;
930 desc.format = wined3dformat_from_d3dformat(format);
931 desc.multisample_type = multisample_type;
932 desc.multisample_quality = multisample_quality;
933 desc.usage = usage & WINED3DUSAGE_MASK;
934 desc.pool = pool;
935 desc.width = width;
936 desc.height = height;
937 desc.depth = 1;
938 desc.size = 0;
940 wined3d_mutex_lock();
942 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
943 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
945 wined3d_mutex_unlock();
946 WARN("Failed to create texture, hr %#x.\n", hr);
947 return hr;
950 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
951 surface_impl = wined3d_resource_get_parent(sub_resource);
952 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
953 *surface = &surface_impl->IDirect3DSurface8_iface;
954 IDirect3DSurface8_AddRef(*surface);
955 wined3d_texture_decref(texture);
957 wined3d_mutex_unlock();
959 return D3D_OK;
962 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
963 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
964 IDirect3DSurface8 **surface)
966 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
967 DWORD flags = 0;
969 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
970 iface, width, height, format, multisample_type, lockable, surface);
972 *surface = NULL;
973 if (lockable)
974 flags |= WINED3D_SURFACE_MAPPABLE;
976 return d3d8_device_create_surface(device, width, height, format, flags, surface,
977 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
980 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
981 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
982 IDirect3DSurface8 **surface)
984 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
986 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
987 iface, width, height, format, multisample_type, surface);
989 *surface = NULL;
991 /* TODO: Verify that Discard is false */
992 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
993 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
996 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
997 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
998 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1000 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1002 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1003 iface, width, height, format, surface);
1005 *surface = NULL;
1007 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
1008 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1011 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1012 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1013 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1015 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1016 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1017 enum wined3d_format_id src_format, dst_format;
1018 struct wined3d_resource_desc wined3d_desc;
1019 struct wined3d_resource *wined3d_resource;
1020 UINT src_w, src_h;
1022 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1023 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1025 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1026 * destination texture is in WINED3D_POOL_DEFAULT. */
1028 wined3d_mutex_lock();
1029 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1030 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1031 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1033 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1034 wined3d_mutex_unlock();
1035 return D3DERR_INVALIDCALL;
1037 src_format = wined3d_desc.format;
1038 src_w = wined3d_desc.width;
1039 src_h = wined3d_desc.height;
1041 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1042 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1043 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1045 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1046 wined3d_mutex_unlock();
1047 return D3DERR_INVALIDCALL;
1049 dst_format = wined3d_desc.format;
1051 /* Check that the source and destination formats match */
1052 if (src_format != dst_format)
1054 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1055 src_surface, dst_surface);
1056 wined3d_mutex_unlock();
1057 return D3DERR_INVALIDCALL;
1060 /* Quick if complete copy ... */
1061 if (!rect_count && !src_rects && !dst_points)
1063 RECT rect = {0, 0, src_w, src_h};
1064 wined3d_surface_blt(dst->wined3d_surface, &rect,
1065 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
1067 else
1069 unsigned int i;
1070 /* Copy rect by rect */
1071 if (src_rects && dst_points)
1073 for (i = 0; i < rect_count; ++i)
1075 UINT w = src_rects[i].right - src_rects[i].left;
1076 UINT h = src_rects[i].bottom - src_rects[i].top;
1077 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1078 dst_points[i].x + w, dst_points[i].y + h};
1080 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1081 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1084 else
1086 for (i = 0; i < rect_count; ++i)
1088 UINT w = src_rects[i].right - src_rects[i].left;
1089 UINT h = src_rects[i].bottom - src_rects[i].top;
1090 RECT dst_rect = {0, 0, w, h};
1092 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1093 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1097 wined3d_mutex_unlock();
1099 return WINED3D_OK;
1102 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1103 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1105 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1106 struct d3d8_texture *src_impl, *dst_impl;
1107 HRESULT hr;
1109 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1111 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1112 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1114 wined3d_mutex_lock();
1115 hr = wined3d_device_update_texture(device->wined3d_device,
1116 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1117 wined3d_mutex_unlock();
1119 return hr;
1122 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1124 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1125 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1126 HRESULT hr;
1128 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1130 if (!dst_surface)
1132 WARN("Invalid destination surface passed.\n");
1133 return D3DERR_INVALIDCALL;
1136 wined3d_mutex_lock();
1137 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1138 wined3d_mutex_unlock();
1140 return hr;
1143 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1144 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1146 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1147 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1148 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1149 struct wined3d_rendertarget_view *original_dsv;
1150 HRESULT hr = D3D_OK;
1152 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1154 wined3d_mutex_lock();
1156 if (ds_impl)
1158 struct wined3d_rendertarget_view *original_rtv;
1159 struct wined3d_resource_desc ds_desc, rt_desc;
1160 struct wined3d_resource *wined3d_resource;
1161 struct d3d8_surface *original_surface;
1163 /* If no render target is passed in check the size against the current RT */
1164 if (!render_target)
1167 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1169 wined3d_mutex_unlock();
1170 return D3DERR_NOTFOUND;
1172 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1173 wined3d_resource = wined3d_surface_get_resource(original_surface->wined3d_surface);
1175 else
1176 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1177 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1179 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1180 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1182 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1184 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1185 wined3d_mutex_unlock();
1186 return D3DERR_INVALIDCALL;
1188 if (ds_desc.multisample_type != rt_desc.multisample_type
1189 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1191 WARN("Multisample settings do not match, returing D3DERR_INVALIDCALL\n");
1192 wined3d_mutex_unlock();
1193 return D3DERR_INVALIDCALL;
1198 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1199 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1200 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1201 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1202 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1203 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1205 wined3d_mutex_unlock();
1207 return hr;
1210 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1212 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1213 struct wined3d_rendertarget_view *wined3d_rtv;
1214 struct d3d8_surface *surface_impl;
1215 HRESULT hr;
1217 TRACE("iface %p, render_target %p.\n", iface, render_target);
1219 if (!render_target)
1220 return D3DERR_INVALIDCALL;
1222 wined3d_mutex_lock();
1223 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1225 /* We want the sub resource parent here, since the view itself may be
1226 * internal to wined3d and may not have a parent. */
1227 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1228 *render_target = &surface_impl->IDirect3DSurface8_iface;
1229 IDirect3DSurface8_AddRef(*render_target);
1230 hr = D3D_OK;
1232 else
1234 ERR("Failed to get wined3d render target.\n");
1235 *render_target = NULL;
1236 hr = D3DERR_NOTFOUND;
1238 wined3d_mutex_unlock();
1240 return hr;
1243 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1245 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1246 struct wined3d_rendertarget_view *wined3d_dsv;
1247 struct d3d8_surface *surface_impl;
1248 HRESULT hr = D3D_OK;
1250 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1252 if (!depth_stencil)
1253 return D3DERR_INVALIDCALL;
1255 wined3d_mutex_lock();
1256 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1258 /* We want the sub resource parent here, since the view itself may be
1259 * internal to wined3d and may not have a parent. */
1260 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1261 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1262 IDirect3DSurface8_AddRef(*depth_stencil);
1264 else
1266 hr = WINED3DERR_NOTFOUND;
1267 *depth_stencil = NULL;
1269 wined3d_mutex_unlock();
1271 return hr;
1274 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1276 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1277 HRESULT hr;
1279 TRACE("iface %p.\n", iface);
1281 wined3d_mutex_lock();
1282 hr = wined3d_device_begin_scene(device->wined3d_device);
1283 wined3d_mutex_unlock();
1285 return hr;
1288 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1290 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1291 HRESULT hr;
1293 TRACE("iface %p.\n", iface);
1295 wined3d_mutex_lock();
1296 hr = wined3d_device_end_scene(device->wined3d_device);
1297 wined3d_mutex_unlock();
1299 return hr;
1302 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1303 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1305 const struct wined3d_color c =
1307 ((color >> 16) & 0xff) / 255.0f,
1308 ((color >> 8) & 0xff) / 255.0f,
1309 (color & 0xff) / 255.0f,
1310 ((color >> 24) & 0xff) / 255.0f,
1312 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1313 HRESULT hr;
1315 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1316 iface, rect_count, rects, flags, color, z, stencil);
1318 wined3d_mutex_lock();
1319 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1320 wined3d_mutex_unlock();
1322 return hr;
1325 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1326 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1328 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1330 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1332 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1333 wined3d_mutex_lock();
1334 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1335 wined3d_mutex_unlock();
1337 return D3D_OK;
1340 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1341 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1343 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1345 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1347 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1348 wined3d_mutex_lock();
1349 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1350 wined3d_mutex_unlock();
1352 return D3D_OK;
1355 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1356 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1358 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1360 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1362 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1363 wined3d_mutex_lock();
1364 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1365 wined3d_mutex_unlock();
1367 return D3D_OK;
1370 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1372 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1374 TRACE("iface %p, viewport %p.\n", iface, viewport);
1376 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1377 wined3d_mutex_lock();
1378 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1379 wined3d_mutex_unlock();
1381 return D3D_OK;
1384 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1386 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1388 TRACE("iface %p, viewport %p.\n", iface, viewport);
1390 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1391 wined3d_mutex_lock();
1392 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1393 wined3d_mutex_unlock();
1395 return D3D_OK;
1398 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1400 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1402 TRACE("iface %p, material %p.\n", iface, material);
1404 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1405 wined3d_mutex_lock();
1406 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1407 wined3d_mutex_unlock();
1409 return D3D_OK;
1412 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1414 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1416 TRACE("iface %p, material %p.\n", iface, material);
1418 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1419 wined3d_mutex_lock();
1420 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1421 wined3d_mutex_unlock();
1423 return D3D_OK;
1426 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1428 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1429 HRESULT hr;
1431 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1433 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1434 wined3d_mutex_lock();
1435 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1436 wined3d_mutex_unlock();
1438 return hr;
1441 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1443 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1444 HRESULT hr;
1446 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1448 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1449 wined3d_mutex_lock();
1450 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1451 wined3d_mutex_unlock();
1453 return hr;
1456 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1459 HRESULT hr;
1461 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1463 wined3d_mutex_lock();
1464 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1465 wined3d_mutex_unlock();
1467 return hr;
1470 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1472 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1473 HRESULT hr;
1475 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1477 wined3d_mutex_lock();
1478 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1479 wined3d_mutex_unlock();
1481 return hr;
1484 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1486 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1487 HRESULT hr;
1489 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1491 wined3d_mutex_lock();
1492 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1493 wined3d_mutex_unlock();
1495 return hr;
1498 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1500 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1501 HRESULT hr;
1503 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1505 wined3d_mutex_lock();
1506 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1507 wined3d_mutex_unlock();
1509 return hr;
1512 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1513 D3DRENDERSTATETYPE state, DWORD value)
1515 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1517 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1519 wined3d_mutex_lock();
1520 switch (state)
1522 case D3DRS_ZBIAS:
1523 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1524 break;
1526 default:
1527 wined3d_device_set_render_state(device->wined3d_device, state, value);
1529 wined3d_mutex_unlock();
1531 return D3D_OK;
1534 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1535 D3DRENDERSTATETYPE state, DWORD *value)
1537 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1539 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1541 wined3d_mutex_lock();
1542 switch (state)
1544 case D3DRS_ZBIAS:
1545 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1546 break;
1548 default:
1549 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1551 wined3d_mutex_unlock();
1553 return D3D_OK;
1556 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1559 HRESULT hr;
1561 TRACE("iface %p.\n", iface);
1563 wined3d_mutex_lock();
1564 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1565 wined3d_mutex_unlock();
1567 return hr;
1570 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1572 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1573 struct wined3d_stateblock *stateblock;
1574 HRESULT hr;
1576 TRACE("iface %p, token %p.\n", iface, token);
1578 /* Tell wineD3D to endstateblock before anything else (in case we run out
1579 * of memory later and cause locking problems)
1581 wined3d_mutex_lock();
1582 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1583 if (FAILED(hr))
1585 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1586 wined3d_mutex_unlock();
1587 return hr;
1590 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1591 wined3d_mutex_unlock();
1593 if (*token == D3D8_INVALID_HANDLE)
1595 ERR("Failed to create a handle\n");
1596 wined3d_mutex_lock();
1597 wined3d_stateblock_decref(stateblock);
1598 wined3d_mutex_unlock();
1599 return E_FAIL;
1601 ++*token;
1603 TRACE("Returning %#x (%p).\n", *token, stateblock);
1605 return hr;
1608 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1610 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1611 struct wined3d_stateblock *stateblock;
1613 TRACE("iface %p, token %#x.\n", iface, token);
1615 if (!token)
1616 return D3D_OK;
1618 wined3d_mutex_lock();
1619 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1620 if (!stateblock)
1622 WARN("Invalid handle (%#x) passed.\n", token);
1623 wined3d_mutex_unlock();
1624 return D3DERR_INVALIDCALL;
1626 wined3d_stateblock_apply(stateblock);
1627 wined3d_mutex_unlock();
1629 return D3D_OK;
1632 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1634 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1635 struct wined3d_stateblock *stateblock;
1637 TRACE("iface %p, token %#x.\n", iface, token);
1639 wined3d_mutex_lock();
1640 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1641 if (!stateblock)
1643 WARN("Invalid handle (%#x) passed.\n", token);
1644 wined3d_mutex_unlock();
1645 return D3DERR_INVALIDCALL;
1647 wined3d_stateblock_capture(stateblock);
1648 wined3d_mutex_unlock();
1650 return D3D_OK;
1653 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1656 struct wined3d_stateblock *stateblock;
1658 TRACE("iface %p, token %#x.\n", iface, token);
1660 wined3d_mutex_lock();
1661 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1663 if (!stateblock)
1665 WARN("Invalid handle (%#x) passed.\n", token);
1666 wined3d_mutex_unlock();
1667 return D3DERR_INVALIDCALL;
1670 if (wined3d_stateblock_decref(stateblock))
1672 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1674 wined3d_mutex_unlock();
1676 return D3D_OK;
1679 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1680 D3DSTATEBLOCKTYPE type, DWORD *handle)
1682 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1683 struct wined3d_stateblock *stateblock;
1684 HRESULT hr;
1686 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1688 if (type != D3DSBT_ALL
1689 && type != D3DSBT_PIXELSTATE
1690 && type != D3DSBT_VERTEXSTATE)
1692 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1693 return D3DERR_INVALIDCALL;
1696 wined3d_mutex_lock();
1697 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1698 if (FAILED(hr))
1700 wined3d_mutex_unlock();
1701 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1702 return hr;
1705 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1706 wined3d_mutex_unlock();
1708 if (*handle == D3D8_INVALID_HANDLE)
1710 ERR("Failed to allocate a handle.\n");
1711 wined3d_mutex_lock();
1712 wined3d_stateblock_decref(stateblock);
1713 wined3d_mutex_unlock();
1714 return E_FAIL;
1716 ++*handle;
1718 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1720 return hr;
1723 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1725 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1726 HRESULT hr;
1728 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1729 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1731 wined3d_mutex_lock();
1732 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1733 wined3d_mutex_unlock();
1735 return hr;
1738 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1740 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1741 HRESULT hr;
1743 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1745 wined3d_mutex_lock();
1746 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1747 wined3d_mutex_unlock();
1749 return hr;
1752 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1754 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1755 struct wined3d_texture *wined3d_texture;
1756 struct d3d8_texture *texture_impl;
1758 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1760 if (!texture)
1761 return D3DERR_INVALIDCALL;
1763 wined3d_mutex_lock();
1764 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1766 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1767 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1768 IDirect3DBaseTexture8_AddRef(*texture);
1770 else
1772 *texture = NULL;
1774 wined3d_mutex_unlock();
1776 return D3D_OK;
1779 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1781 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1782 struct d3d8_texture *texture_impl;
1783 HRESULT hr;
1785 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1787 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1789 wined3d_mutex_lock();
1790 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1791 texture_impl ? texture_impl->wined3d_texture : NULL);
1792 wined3d_mutex_unlock();
1794 return hr;
1797 static const struct tss_lookup
1799 BOOL sampler_state;
1800 enum wined3d_texture_stage_state state;
1802 tss_lookup[] =
1804 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1805 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1806 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1807 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1808 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1809 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1810 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1811 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1812 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1813 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1814 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1815 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1816 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1817 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1818 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1819 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1820 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1821 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1822 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1823 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1824 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1825 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1826 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1827 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1828 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1829 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1830 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1831 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1832 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1835 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1836 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1838 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1839 const struct tss_lookup *l;
1841 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1843 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1845 WARN("Invalid Type %#x passed.\n", Type);
1846 return D3D_OK;
1849 l = &tss_lookup[Type];
1851 wined3d_mutex_lock();
1852 if (l->sampler_state)
1853 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1854 else
1855 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1856 wined3d_mutex_unlock();
1858 return D3D_OK;
1861 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1862 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1864 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1865 const struct tss_lookup *l;
1867 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1869 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1871 WARN("Invalid type %#x passed.\n", type);
1872 return D3D_OK;
1875 l = &tss_lookup[type];
1877 wined3d_mutex_lock();
1878 if (l->sampler_state)
1879 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1880 else
1881 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1882 wined3d_mutex_unlock();
1884 return D3D_OK;
1887 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1889 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1890 HRESULT hr;
1892 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1894 wined3d_mutex_lock();
1895 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1896 wined3d_mutex_unlock();
1898 return hr;
1901 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1902 DWORD info_id, void *info, DWORD info_size)
1904 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1906 return D3D_OK;
1909 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1910 UINT palette_idx, const PALETTEENTRY *entries)
1912 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1914 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1915 * does not have a d3d8/9-style palette API */
1917 return D3D_OK;
1920 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1921 UINT palette_idx, PALETTEENTRY *entries)
1923 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1925 return D3DERR_INVALIDCALL;
1928 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1930 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1932 return D3D_OK;
1935 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1937 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1939 return D3DERR_INVALIDCALL;
1942 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1943 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1945 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1946 HRESULT hr;
1948 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1949 iface, primitive_type, start_vertex, primitive_count);
1951 wined3d_mutex_lock();
1952 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1953 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1954 vertex_count_from_primitive_count(primitive_type, primitive_count));
1955 wined3d_mutex_unlock();
1957 return hr;
1960 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1961 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1962 UINT start_idx, UINT primitive_count)
1964 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1965 HRESULT hr;
1967 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1968 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1970 wined3d_mutex_lock();
1971 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1972 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1973 vertex_count_from_primitive_count(primitive_type, primitive_count));
1974 wined3d_mutex_unlock();
1976 return hr;
1979 /* The caller is responsible for wined3d locking */
1980 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1982 HRESULT hr;
1984 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1986 UINT size = max(device->vertex_buffer_size * 2, min_size);
1987 struct wined3d_buffer *buffer;
1989 TRACE("Growing vertex buffer to %u bytes\n", size);
1991 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1992 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1993 if (FAILED(hr))
1995 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1996 return hr;
1999 if (device->vertex_buffer)
2000 wined3d_buffer_decref(device->vertex_buffer);
2002 device->vertex_buffer = buffer;
2003 device->vertex_buffer_size = size;
2004 device->vertex_buffer_pos = 0;
2006 return D3D_OK;
2009 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2010 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2011 UINT stride)
2013 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2014 HRESULT hr;
2015 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2016 UINT size = vtx_count * stride;
2017 UINT vb_pos, align;
2018 BYTE *buffer_data;
2020 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2021 iface, primitive_type, primitive_count, data, stride);
2023 if (!primitive_count)
2025 WARN("primitive_count is 0, returning D3D_OK\n");
2026 return D3D_OK;
2029 wined3d_mutex_lock();
2030 hr = d3d8_device_prepare_vertex_buffer(device, size);
2031 if (FAILED(hr))
2032 goto done;
2034 vb_pos = device->vertex_buffer_pos;
2035 align = vb_pos % stride;
2036 if (align) align = stride - align;
2037 if (vb_pos + size + align > device->vertex_buffer_size)
2038 vb_pos = 0;
2039 else
2040 vb_pos += align;
2042 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2043 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2044 if (FAILED(hr))
2045 goto done;
2046 memcpy(buffer_data, data, size);
2047 wined3d_buffer_unmap(device->vertex_buffer);
2048 device->vertex_buffer_pos = vb_pos + size;
2050 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2051 if (FAILED(hr))
2052 goto done;
2054 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2055 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2056 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2058 done:
2059 wined3d_mutex_unlock();
2060 return hr;
2063 /* The caller is responsible for wined3d locking */
2064 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2066 HRESULT hr;
2068 if (device->index_buffer_size < min_size || !device->index_buffer)
2070 UINT size = max(device->index_buffer_size * 2, min_size);
2071 struct wined3d_buffer *buffer;
2073 TRACE("Growing index buffer to %u bytes\n", size);
2075 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2076 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2077 if (FAILED(hr))
2079 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2080 return hr;
2083 if (device->index_buffer)
2084 wined3d_buffer_decref(device->index_buffer);
2086 device->index_buffer = buffer;
2087 device->index_buffer_size = size;
2088 device->index_buffer_pos = 0;
2090 return D3D_OK;
2093 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2094 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2095 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2096 const void *vertex_data, UINT vertex_stride)
2098 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2099 HRESULT hr;
2100 BYTE *buffer_data;
2102 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2103 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2104 UINT idx_size = idx_count * idx_fmt_size;
2105 UINT ib_pos;
2107 UINT vtx_size = vertex_count * vertex_stride;
2108 UINT vb_pos, align;
2110 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2111 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2112 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2113 index_data, index_format, vertex_data, vertex_stride);
2115 if (!primitive_count)
2117 WARN("primitive_count is 0, returning D3D_OK\n");
2118 return D3D_OK;
2121 wined3d_mutex_lock();
2123 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2124 if (FAILED(hr))
2125 goto done;
2127 vb_pos = device->vertex_buffer_pos;
2128 align = vb_pos % vertex_stride;
2129 if (align) align = vertex_stride - align;
2130 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2131 vb_pos = 0;
2132 else
2133 vb_pos += align;
2135 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2136 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2137 if (FAILED(hr))
2138 goto done;
2139 memcpy(buffer_data, vertex_data, vtx_size);
2140 wined3d_buffer_unmap(device->vertex_buffer);
2141 device->vertex_buffer_pos = vb_pos + vtx_size;
2143 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2144 if (FAILED(hr))
2145 goto done;
2147 ib_pos = device->index_buffer_pos;
2148 align = ib_pos % idx_fmt_size;
2149 if (align) align = idx_fmt_size - align;
2150 if (ib_pos + idx_size + align > device->index_buffer_size)
2151 ib_pos = 0;
2152 else
2153 ib_pos += align;
2155 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2156 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2157 if (FAILED(hr))
2158 goto done;
2159 memcpy(buffer_data, index_data, idx_size);
2160 wined3d_buffer_unmap(device->index_buffer);
2161 device->index_buffer_pos = ib_pos + idx_size;
2163 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2164 if (FAILED(hr))
2165 goto done;
2167 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2168 wined3dformat_from_d3dformat(index_format));
2169 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2171 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2172 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2174 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2175 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2176 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2178 done:
2179 wined3d_mutex_unlock();
2180 return hr;
2183 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2184 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2186 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2187 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2188 HRESULT hr;
2190 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2191 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2193 wined3d_mutex_lock();
2194 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2195 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2196 wined3d_mutex_unlock();
2198 return hr;
2201 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2202 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2204 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2205 struct d3d8_vertex_shader *object;
2206 DWORD shader_handle;
2207 DWORD handle;
2208 HRESULT hr;
2210 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2211 iface, declaration, byte_code, shader, usage);
2213 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2214 if (!object)
2216 *shader = 0;
2217 return E_OUTOFMEMORY;
2220 wined3d_mutex_lock();
2221 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2222 wined3d_mutex_unlock();
2223 if (handle == D3D8_INVALID_HANDLE)
2225 ERR("Failed to allocate vertex shader handle.\n");
2226 HeapFree(GetProcessHeap(), 0, object);
2227 *shader = 0;
2228 return E_OUTOFMEMORY;
2231 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2233 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2234 if (FAILED(hr))
2236 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2237 wined3d_mutex_lock();
2238 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2239 wined3d_mutex_unlock();
2240 HeapFree(GetProcessHeap(), 0, object);
2241 *shader = 0;
2242 return hr;
2245 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2246 *shader = shader_handle;
2248 return D3D_OK;
2251 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2253 struct d3d8_vertex_declaration *d3d8_declaration;
2254 struct FvfToDecl *convertedDecls = device->decls;
2255 int p, low, high; /* deliberately signed */
2256 HRESULT hr;
2258 TRACE("Searching for declaration for fvf %08x... ", fvf);
2260 low = 0;
2261 high = device->numConvertedDecls - 1;
2262 while (low <= high)
2264 p = (low + high) >> 1;
2265 TRACE("%d ", p);
2267 if (convertedDecls[p].fvf == fvf)
2269 TRACE("found %p\n", convertedDecls[p].declaration);
2270 return convertedDecls[p].declaration;
2273 if (convertedDecls[p].fvf < fvf)
2274 low = p + 1;
2275 else
2276 high = p - 1;
2278 TRACE("not found. Creating and inserting at position %d.\n", low);
2280 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2281 return NULL;
2283 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2285 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2286 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2287 return NULL;
2290 if (device->declArraySize == device->numConvertedDecls)
2292 UINT grow = device->declArraySize / 2;
2294 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2295 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2296 if (!convertedDecls)
2298 d3d8_vertex_declaration_destroy(d3d8_declaration);
2299 return NULL;
2301 device->decls = convertedDecls;
2302 device->declArraySize += grow;
2305 memmove(convertedDecls + low + 1, convertedDecls + low,
2306 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2307 convertedDecls[low].declaration = d3d8_declaration;
2308 convertedDecls[low].fvf = fvf;
2309 ++device->numConvertedDecls;
2311 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2313 return d3d8_declaration;
2316 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2318 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2319 struct d3d8_vertex_shader *shader_impl;
2321 TRACE("iface %p, shader %#x.\n", iface, shader);
2323 if (VS_HIGHESTFIXEDFXF >= shader)
2325 TRACE("Setting FVF, %#x\n", shader);
2327 wined3d_mutex_lock();
2328 wined3d_device_set_vertex_declaration(device->wined3d_device,
2329 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2330 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2331 wined3d_mutex_unlock();
2333 return D3D_OK;
2336 TRACE("Setting shader\n");
2338 wined3d_mutex_lock();
2339 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2341 WARN("Invalid handle (%#x) passed.\n", shader);
2342 wined3d_mutex_unlock();
2344 return D3DERR_INVALIDCALL;
2347 wined3d_device_set_vertex_declaration(device->wined3d_device,
2348 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2349 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2350 wined3d_mutex_unlock();
2352 return D3D_OK;
2355 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2357 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2358 struct wined3d_vertex_declaration *wined3d_declaration;
2359 struct d3d8_vertex_declaration *d3d8_declaration;
2361 TRACE("iface %p, shader %p.\n", iface, shader);
2363 wined3d_mutex_lock();
2364 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2366 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2367 *shader = d3d8_declaration->shader_handle;
2369 else
2371 *shader = 0;
2373 wined3d_mutex_unlock();
2375 TRACE("Returning %#x.\n", *shader);
2377 return D3D_OK;
2380 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2382 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2383 struct d3d8_vertex_shader *shader_impl;
2385 TRACE("iface %p, shader %#x.\n", iface, shader);
2387 wined3d_mutex_lock();
2388 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2390 WARN("Invalid handle (%#x) passed.\n", shader);
2391 wined3d_mutex_unlock();
2393 return D3DERR_INVALIDCALL;
2396 if (shader_impl->wined3d_shader
2397 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2398 IDirect3DDevice8_SetVertexShader(iface, 0);
2400 wined3d_mutex_unlock();
2402 d3d8_vertex_shader_destroy(shader_impl);
2404 return D3D_OK;
2407 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2408 DWORD start_register, const void *data, DWORD count)
2410 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2411 HRESULT hr;
2413 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2414 iface, start_register, data, count);
2416 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2418 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2419 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2420 return D3DERR_INVALIDCALL;
2423 wined3d_mutex_lock();
2424 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2425 wined3d_mutex_unlock();
2427 return hr;
2430 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2431 DWORD start_register, 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_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2448 wined3d_mutex_unlock();
2450 return hr;
2453 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2454 DWORD shader, void *data, DWORD *data_size)
2456 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2457 struct d3d8_vertex_declaration *declaration;
2458 struct d3d8_vertex_shader *shader_impl;
2460 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2461 iface, shader, data, data_size);
2463 wined3d_mutex_lock();
2464 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2465 wined3d_mutex_unlock();
2467 if (!shader_impl)
2469 WARN("Invalid handle (%#x) passed.\n", shader);
2470 return D3DERR_INVALIDCALL;
2472 declaration = shader_impl->vertex_declaration;
2474 if (!data)
2476 *data_size = declaration->elements_size;
2477 return D3D_OK;
2480 /* MSDN claims that if *data_size is smaller than the required size
2481 * we should write the required size and return D3DERR_MOREDATA.
2482 * That's not actually true. */
2483 if (*data_size < declaration->elements_size)
2484 return D3DERR_INVALIDCALL;
2486 memcpy(data, declaration->elements, declaration->elements_size);
2488 return D3D_OK;
2491 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2492 DWORD shader, void *data, DWORD *data_size)
2494 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2495 struct d3d8_vertex_shader *shader_impl = NULL;
2496 HRESULT hr;
2498 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2499 iface, shader, data, data_size);
2501 wined3d_mutex_lock();
2502 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2504 WARN("Invalid handle (%#x) passed.\n", shader);
2505 wined3d_mutex_unlock();
2507 return D3DERR_INVALIDCALL;
2510 if (!shader_impl->wined3d_shader)
2512 wined3d_mutex_unlock();
2513 *data_size = 0;
2514 return D3D_OK;
2517 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2518 wined3d_mutex_unlock();
2520 return hr;
2523 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2524 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2526 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2527 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2529 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2531 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2532 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2533 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2534 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2535 * problem)
2537 wined3d_mutex_lock();
2538 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2539 wined3d_device_set_index_buffer(device->wined3d_device,
2540 ib ? ib->wined3d_buffer : NULL,
2541 ib ? ib->format : WINED3DFMT_UNKNOWN);
2542 wined3d_mutex_unlock();
2544 return D3D_OK;
2547 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2548 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2551 enum wined3d_format_id wined3d_format;
2552 struct wined3d_buffer *wined3d_buffer;
2553 struct d3d8_indexbuffer *buffer_impl;
2555 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2557 if (!buffer)
2558 return D3DERR_INVALIDCALL;
2560 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2561 wined3d_mutex_lock();
2562 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2563 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2565 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2566 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2567 IDirect3DIndexBuffer8_AddRef(*buffer);
2569 else
2571 *buffer = NULL;
2573 wined3d_mutex_unlock();
2575 return D3D_OK;
2578 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2579 const DWORD *byte_code, DWORD *shader)
2581 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2582 struct d3d8_pixel_shader *object;
2583 DWORD shader_handle;
2584 DWORD handle;
2585 HRESULT hr;
2587 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2589 if (!shader)
2590 return D3DERR_INVALIDCALL;
2592 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2593 if (!object)
2594 return E_OUTOFMEMORY;
2596 wined3d_mutex_lock();
2597 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2598 wined3d_mutex_unlock();
2599 if (handle == D3D8_INVALID_HANDLE)
2601 ERR("Failed to allocate pixel shader handle.\n");
2602 HeapFree(GetProcessHeap(), 0, object);
2603 return E_OUTOFMEMORY;
2606 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2608 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2609 if (FAILED(hr))
2611 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2612 wined3d_mutex_lock();
2613 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2614 wined3d_mutex_unlock();
2615 HeapFree(GetProcessHeap(), 0, object);
2616 *shader = 0;
2617 return hr;
2620 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2621 *shader = shader_handle;
2623 return D3D_OK;
2626 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2628 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2629 struct d3d8_pixel_shader *shader_impl;
2631 TRACE("iface %p, shader %#x.\n", iface, shader);
2633 wined3d_mutex_lock();
2635 if (!shader)
2637 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2638 wined3d_mutex_unlock();
2639 return D3D_OK;
2642 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2644 WARN("Invalid handle (%#x) passed.\n", shader);
2645 wined3d_mutex_unlock();
2646 return D3DERR_INVALIDCALL;
2649 TRACE("Setting shader %p.\n", shader_impl);
2650 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2651 wined3d_mutex_unlock();
2653 return D3D_OK;
2656 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2658 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2659 struct wined3d_shader *object;
2661 TRACE("iface %p, shader %p.\n", iface, shader);
2663 if (!shader)
2664 return D3DERR_INVALIDCALL;
2666 wined3d_mutex_lock();
2667 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2669 struct d3d8_pixel_shader *d3d8_shader;
2670 d3d8_shader = wined3d_shader_get_parent(object);
2671 *shader = d3d8_shader->handle;
2673 else
2675 *shader = 0;
2677 wined3d_mutex_unlock();
2679 TRACE("Returning %#x.\n", *shader);
2681 return D3D_OK;
2684 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2686 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2687 struct d3d8_pixel_shader *shader_impl;
2689 TRACE("iface %p, shader %#x.\n", iface, shader);
2691 wined3d_mutex_lock();
2693 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2695 WARN("Invalid handle (%#x) passed.\n", shader);
2696 wined3d_mutex_unlock();
2697 return D3DERR_INVALIDCALL;
2700 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2701 IDirect3DDevice8_SetPixelShader(iface, 0);
2703 wined3d_mutex_unlock();
2705 d3d8_pixel_shader_destroy(shader_impl);
2707 return D3D_OK;
2710 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2711 DWORD start_register, const void *data, DWORD count)
2713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2714 HRESULT hr;
2716 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2717 iface, start_register, data, count);
2719 wined3d_mutex_lock();
2720 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2721 wined3d_mutex_unlock();
2723 return hr;
2726 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2727 DWORD start_register, void *data, DWORD count)
2729 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2730 HRESULT hr;
2732 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2733 iface, start_register, data, count);
2735 wined3d_mutex_lock();
2736 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2737 wined3d_mutex_unlock();
2739 return hr;
2742 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2743 DWORD shader, void *data, DWORD *data_size)
2745 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2746 struct d3d8_pixel_shader *shader_impl = NULL;
2747 HRESULT hr;
2749 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2750 iface, shader, data, data_size);
2752 wined3d_mutex_lock();
2753 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2755 WARN("Invalid handle (%#x) passed.\n", shader);
2756 wined3d_mutex_unlock();
2758 return D3DERR_INVALIDCALL;
2761 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2762 wined3d_mutex_unlock();
2764 return hr;
2767 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2768 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2770 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2771 iface, handle, segment_count, patch_info);
2772 return D3D_OK;
2775 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2776 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2778 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2779 iface, handle, segment_count, patch_info);
2780 return D3D_OK;
2783 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2785 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2786 return D3DERR_INVALIDCALL;
2789 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2790 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2792 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2793 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2794 HRESULT hr;
2796 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2797 iface, stream_idx, buffer, stride);
2799 wined3d_mutex_lock();
2800 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2801 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2802 wined3d_mutex_unlock();
2804 return hr;
2807 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2808 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2810 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2811 struct d3d8_vertexbuffer *buffer_impl;
2812 struct wined3d_buffer *wined3d_buffer = NULL;
2813 HRESULT hr;
2815 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2816 iface, stream_idx, buffer, stride);
2818 if (!buffer)
2819 return D3DERR_INVALIDCALL;
2821 wined3d_mutex_lock();
2822 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2823 if (SUCCEEDED(hr) && wined3d_buffer)
2825 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2826 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2827 IDirect3DVertexBuffer8_AddRef(*buffer);
2829 else
2831 if (FAILED(hr))
2832 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2833 *buffer = NULL;
2835 wined3d_mutex_unlock();
2837 return hr;
2840 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2842 d3d8_device_QueryInterface,
2843 d3d8_device_AddRef,
2844 d3d8_device_Release,
2845 d3d8_device_TestCooperativeLevel,
2846 d3d8_device_GetAvailableTextureMem,
2847 d3d8_device_ResourceManagerDiscardBytes,
2848 d3d8_device_GetDirect3D,
2849 d3d8_device_GetDeviceCaps,
2850 d3d8_device_GetDisplayMode,
2851 d3d8_device_GetCreationParameters,
2852 d3d8_device_SetCursorProperties,
2853 d3d8_device_SetCursorPosition,
2854 d3d8_device_ShowCursor,
2855 d3d8_device_CreateAdditionalSwapChain,
2856 d3d8_device_Reset,
2857 d3d8_device_Present,
2858 d3d8_device_GetBackBuffer,
2859 d3d8_device_GetRasterStatus,
2860 d3d8_device_SetGammaRamp,
2861 d3d8_device_GetGammaRamp,
2862 d3d8_device_CreateTexture,
2863 d3d8_device_CreateVolumeTexture,
2864 d3d8_device_CreateCubeTexture,
2865 d3d8_device_CreateVertexBuffer,
2866 d3d8_device_CreateIndexBuffer,
2867 d3d8_device_CreateRenderTarget,
2868 d3d8_device_CreateDepthStencilSurface,
2869 d3d8_device_CreateImageSurface,
2870 d3d8_device_CopyRects,
2871 d3d8_device_UpdateTexture,
2872 d3d8_device_GetFrontBuffer,
2873 d3d8_device_SetRenderTarget,
2874 d3d8_device_GetRenderTarget,
2875 d3d8_device_GetDepthStencilSurface,
2876 d3d8_device_BeginScene,
2877 d3d8_device_EndScene,
2878 d3d8_device_Clear,
2879 d3d8_device_SetTransform,
2880 d3d8_device_GetTransform,
2881 d3d8_device_MultiplyTransform,
2882 d3d8_device_SetViewport,
2883 d3d8_device_GetViewport,
2884 d3d8_device_SetMaterial,
2885 d3d8_device_GetMaterial,
2886 d3d8_device_SetLight,
2887 d3d8_device_GetLight,
2888 d3d8_device_LightEnable,
2889 d3d8_device_GetLightEnable,
2890 d3d8_device_SetClipPlane,
2891 d3d8_device_GetClipPlane,
2892 d3d8_device_SetRenderState,
2893 d3d8_device_GetRenderState,
2894 d3d8_device_BeginStateBlock,
2895 d3d8_device_EndStateBlock,
2896 d3d8_device_ApplyStateBlock,
2897 d3d8_device_CaptureStateBlock,
2898 d3d8_device_DeleteStateBlock,
2899 d3d8_device_CreateStateBlock,
2900 d3d8_device_SetClipStatus,
2901 d3d8_device_GetClipStatus,
2902 d3d8_device_GetTexture,
2903 d3d8_device_SetTexture,
2904 d3d8_device_GetTextureStageState,
2905 d3d8_device_SetTextureStageState,
2906 d3d8_device_ValidateDevice,
2907 d3d8_device_GetInfo,
2908 d3d8_device_SetPaletteEntries,
2909 d3d8_device_GetPaletteEntries,
2910 d3d8_device_SetCurrentTexturePalette,
2911 d3d8_device_GetCurrentTexturePalette,
2912 d3d8_device_DrawPrimitive,
2913 d3d8_device_DrawIndexedPrimitive,
2914 d3d8_device_DrawPrimitiveUP,
2915 d3d8_device_DrawIndexedPrimitiveUP,
2916 d3d8_device_ProcessVertices,
2917 d3d8_device_CreateVertexShader,
2918 d3d8_device_SetVertexShader,
2919 d3d8_device_GetVertexShader,
2920 d3d8_device_DeleteVertexShader,
2921 d3d8_device_SetVertexShaderConstant,
2922 d3d8_device_GetVertexShaderConstant,
2923 d3d8_device_GetVertexShaderDeclaration,
2924 d3d8_device_GetVertexShaderFunction,
2925 d3d8_device_SetStreamSource,
2926 d3d8_device_GetStreamSource,
2927 d3d8_device_SetIndices,
2928 d3d8_device_GetIndices,
2929 d3d8_device_CreatePixelShader,
2930 d3d8_device_SetPixelShader,
2931 d3d8_device_GetPixelShader,
2932 d3d8_device_DeletePixelShader,
2933 d3d8_device_SetPixelShaderConstant,
2934 d3d8_device_GetPixelShaderConstant,
2935 d3d8_device_GetPixelShaderFunction,
2936 d3d8_device_DrawRectPatch,
2937 d3d8_device_DrawTriPatch,
2938 d3d8_device_DeletePatch,
2941 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2943 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2946 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2947 struct wined3d_device *device)
2949 TRACE("device_parent %p, device %p\n", device_parent, device);
2952 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2954 TRACE("device_parent %p.\n", device_parent);
2957 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2959 struct d3d8_device *device = device_from_device_parent(device_parent);
2961 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2963 if (!activate)
2964 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2965 else
2966 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2969 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2970 void *container_parent, struct wined3d_surface *surface, void **parent,
2971 const struct wined3d_parent_ops **parent_ops)
2973 struct d3d8_surface *d3d_surface;
2975 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
2976 device_parent, container_parent, surface, parent, parent_ops);
2978 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2979 return E_OUTOFMEMORY;
2981 surface_init(d3d_surface, container_parent, surface, parent_ops);
2982 *parent = d3d_surface;
2983 TRACE("Created surface %p.\n", d3d_surface);
2985 return D3D_OK;
2988 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
2989 void *container_parent, struct wined3d_volume *volume, void **parent,
2990 const struct wined3d_parent_ops **parent_ops)
2992 struct d3d8_volume *d3d_volume;
2994 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
2995 device_parent, container_parent, volume, parent, parent_ops);
2997 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
2998 return E_OUTOFMEMORY;
3000 volume_init(d3d_volume, container_parent, volume, parent_ops);
3001 *parent = d3d_volume;
3002 TRACE("Created volume %p.\n", d3d_volume);
3004 return D3D_OK;
3007 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3008 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3010 struct d3d8_device *device = device_from_device_parent(device_parent);
3011 struct wined3d_resource_desc texture_desc;
3012 struct d3d8_surface *d3d_surface;
3013 struct wined3d_texture *texture;
3014 HRESULT hr;
3016 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3017 device_parent, container_parent, desc, surface);
3019 texture_desc = *desc;
3020 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3021 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3022 WINED3D_SURFACE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
3024 WARN("Failed to create texture, hr %#x.\n", hr);
3025 return hr;
3028 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3029 wined3d_surface_incref(*surface);
3030 wined3d_texture_decref(texture);
3032 d3d_surface = wined3d_surface_get_parent(*surface);
3033 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3035 return hr;
3038 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3039 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3041 struct d3d8_device *device = device_from_device_parent(device_parent);
3042 struct d3d8_swapchain *d3d_swapchain;
3043 HRESULT hr;
3045 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3047 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3049 WARN("Failed to create swapchain, hr %#x.\n", hr);
3050 *swapchain = NULL;
3051 return hr;
3054 *swapchain = d3d_swapchain->wined3d_swapchain;
3055 wined3d_swapchain_incref(*swapchain);
3056 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3058 return hr;
3061 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3063 device_parent_wined3d_device_created,
3064 device_parent_mode_changed,
3065 device_parent_activate,
3066 device_parent_surface_created,
3067 device_parent_volume_created,
3068 device_parent_create_swapchain_surface,
3069 device_parent_create_swapchain,
3072 static void setup_fpu(void)
3074 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3075 WORD cw;
3076 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3077 cw = (cw & ~0xf3f) | 0x3f;
3078 __asm__ volatile ("fldcw %0" : : "m" (cw));
3079 #elif defined(__i386__) && defined(_MSC_VER)
3080 WORD cw;
3081 __asm fnstcw cw;
3082 cw = (cw & ~0xf3f) | 0x3f;
3083 __asm fldcw cw;
3084 #else
3085 FIXME("FPU setup not implemented for this platform.\n");
3086 #endif
3089 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3090 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3092 struct wined3d_swapchain_desc swapchain_desc;
3093 HRESULT hr;
3095 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3096 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3097 device->ref = 1;
3098 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3099 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3100 if (!device->handle_table.entries)
3102 ERR("Failed to allocate handle table memory.\n");
3103 return E_OUTOFMEMORY;
3105 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3107 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3109 wined3d_mutex_lock();
3110 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3111 &device->device_parent, &device->wined3d_device);
3112 if (FAILED(hr))
3114 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3115 wined3d_mutex_unlock();
3116 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3117 return hr;
3120 if (!parameters->Windowed)
3122 HWND device_window = parameters->hDeviceWindow;
3124 if (!focus_window)
3125 focus_window = device_window;
3126 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3128 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3129 wined3d_device_decref(device->wined3d_device);
3130 wined3d_mutex_unlock();
3131 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3132 return hr;
3135 if (!device_window)
3136 device_window = focus_window;
3137 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3138 parameters->BackBufferWidth,
3139 parameters->BackBufferHeight);
3142 if (flags & D3DCREATE_MULTITHREADED)
3143 wined3d_device_set_multithreaded(device->wined3d_device);
3145 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3147 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3148 if (FAILED(hr))
3150 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3151 wined3d_device_release_focus_window(device->wined3d_device);
3152 wined3d_device_decref(device->wined3d_device);
3153 wined3d_mutex_unlock();
3154 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3155 return hr;
3158 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3159 wined3d_mutex_unlock();
3161 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3163 device->declArraySize = 16;
3164 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3165 if (!device->decls)
3167 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3168 hr = E_OUTOFMEMORY;
3169 goto err;
3172 device->d3d_parent = &parent->IDirect3D8_iface;
3173 IDirect3D8_AddRef(device->d3d_parent);
3175 return D3D_OK;
3177 err:
3178 wined3d_mutex_lock();
3179 wined3d_device_uninit_3d(device->wined3d_device);
3180 wined3d_device_release_focus_window(device->wined3d_device);
3181 wined3d_device_decref(device->wined3d_device);
3182 wined3d_mutex_unlock();
3183 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3184 return hr;