riched20: Initial support for changing font properties.
[wine/multimedia.git] / dlls / d3d8 / device.c
blob10c210344ae592d49b362ac5d6dcd615d12c9c37
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;
1190 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1191 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1192 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1193 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1194 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1195 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1197 wined3d_mutex_unlock();
1199 return hr;
1202 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1204 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1205 struct wined3d_rendertarget_view *wined3d_rtv;
1206 struct d3d8_surface *surface_impl;
1207 HRESULT hr;
1209 TRACE("iface %p, render_target %p.\n", iface, render_target);
1211 if (!render_target)
1212 return D3DERR_INVALIDCALL;
1214 wined3d_mutex_lock();
1215 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1217 /* We want the sub resource parent here, since the view itself may be
1218 * internal to wined3d and may not have a parent. */
1219 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1220 *render_target = &surface_impl->IDirect3DSurface8_iface;
1221 IDirect3DSurface8_AddRef(*render_target);
1222 hr = D3D_OK;
1224 else
1226 ERR("Failed to get wined3d render target.\n");
1227 *render_target = NULL;
1228 hr = D3DERR_NOTFOUND;
1230 wined3d_mutex_unlock();
1232 return hr;
1235 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1237 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1238 struct wined3d_rendertarget_view *wined3d_dsv;
1239 struct d3d8_surface *surface_impl;
1240 HRESULT hr = D3D_OK;
1242 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1244 if (!depth_stencil)
1245 return D3DERR_INVALIDCALL;
1247 wined3d_mutex_lock();
1248 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1250 /* We want the sub resource parent here, since the view itself may be
1251 * internal to wined3d and may not have a parent. */
1252 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1253 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1254 IDirect3DSurface8_AddRef(*depth_stencil);
1256 else
1258 hr = WINED3DERR_NOTFOUND;
1259 *depth_stencil = NULL;
1261 wined3d_mutex_unlock();
1263 return hr;
1266 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1268 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1269 HRESULT hr;
1271 TRACE("iface %p.\n", iface);
1273 wined3d_mutex_lock();
1274 hr = wined3d_device_begin_scene(device->wined3d_device);
1275 wined3d_mutex_unlock();
1277 return hr;
1280 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1282 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1283 HRESULT hr;
1285 TRACE("iface %p.\n", iface);
1287 wined3d_mutex_lock();
1288 hr = wined3d_device_end_scene(device->wined3d_device);
1289 wined3d_mutex_unlock();
1291 return hr;
1294 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1295 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1297 const struct wined3d_color c =
1299 ((color >> 16) & 0xff) / 255.0f,
1300 ((color >> 8) & 0xff) / 255.0f,
1301 (color & 0xff) / 255.0f,
1302 ((color >> 24) & 0xff) / 255.0f,
1304 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1305 HRESULT hr;
1307 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1308 iface, rect_count, rects, flags, color, z, stencil);
1310 wined3d_mutex_lock();
1311 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1312 wined3d_mutex_unlock();
1314 return hr;
1317 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1318 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1320 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1322 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1324 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1325 wined3d_mutex_lock();
1326 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1327 wined3d_mutex_unlock();
1329 return D3D_OK;
1332 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1333 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1335 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1337 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1339 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1340 wined3d_mutex_lock();
1341 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1342 wined3d_mutex_unlock();
1344 return D3D_OK;
1347 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1348 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1350 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1352 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1354 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1355 wined3d_mutex_lock();
1356 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1357 wined3d_mutex_unlock();
1359 return D3D_OK;
1362 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1364 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1366 TRACE("iface %p, viewport %p.\n", iface, viewport);
1368 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1369 wined3d_mutex_lock();
1370 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1371 wined3d_mutex_unlock();
1373 return D3D_OK;
1376 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1378 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1380 TRACE("iface %p, viewport %p.\n", iface, viewport);
1382 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1383 wined3d_mutex_lock();
1384 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1385 wined3d_mutex_unlock();
1387 return D3D_OK;
1390 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1392 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1394 TRACE("iface %p, material %p.\n", iface, material);
1396 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1397 wined3d_mutex_lock();
1398 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1399 wined3d_mutex_unlock();
1401 return D3D_OK;
1404 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1406 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1408 TRACE("iface %p, material %p.\n", iface, material);
1410 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1411 wined3d_mutex_lock();
1412 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1413 wined3d_mutex_unlock();
1415 return D3D_OK;
1418 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1420 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1421 HRESULT hr;
1423 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1425 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1426 wined3d_mutex_lock();
1427 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1428 wined3d_mutex_unlock();
1430 return hr;
1433 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1435 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1436 HRESULT hr;
1438 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1440 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1441 wined3d_mutex_lock();
1442 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1443 wined3d_mutex_unlock();
1445 return hr;
1448 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1450 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1451 HRESULT hr;
1453 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1455 wined3d_mutex_lock();
1456 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1457 wined3d_mutex_unlock();
1459 return hr;
1462 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1464 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1465 HRESULT hr;
1467 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1469 wined3d_mutex_lock();
1470 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1471 wined3d_mutex_unlock();
1473 return hr;
1476 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1478 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1479 HRESULT hr;
1481 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1483 wined3d_mutex_lock();
1484 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1485 wined3d_mutex_unlock();
1487 return hr;
1490 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1492 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1493 HRESULT hr;
1495 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1497 wined3d_mutex_lock();
1498 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1499 wined3d_mutex_unlock();
1501 return hr;
1504 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1505 D3DRENDERSTATETYPE state, DWORD value)
1507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1509 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1511 wined3d_mutex_lock();
1512 switch (state)
1514 case D3DRS_ZBIAS:
1515 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1516 break;
1518 default:
1519 wined3d_device_set_render_state(device->wined3d_device, state, value);
1521 wined3d_mutex_unlock();
1523 return D3D_OK;
1526 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1527 D3DRENDERSTATETYPE state, DWORD *value)
1529 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1531 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1533 wined3d_mutex_lock();
1534 switch (state)
1536 case D3DRS_ZBIAS:
1537 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1538 break;
1540 default:
1541 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1543 wined3d_mutex_unlock();
1545 return D3D_OK;
1548 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1551 HRESULT hr;
1553 TRACE("iface %p.\n", iface);
1555 wined3d_mutex_lock();
1556 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1557 wined3d_mutex_unlock();
1559 return hr;
1562 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1564 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1565 struct wined3d_stateblock *stateblock;
1566 HRESULT hr;
1568 TRACE("iface %p, token %p.\n", iface, token);
1570 /* Tell wineD3D to endstateblock before anything else (in case we run out
1571 * of memory later and cause locking problems)
1573 wined3d_mutex_lock();
1574 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1575 if (FAILED(hr))
1577 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1578 wined3d_mutex_unlock();
1579 return hr;
1582 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1583 wined3d_mutex_unlock();
1585 if (*token == D3D8_INVALID_HANDLE)
1587 ERR("Failed to create a handle\n");
1588 wined3d_mutex_lock();
1589 wined3d_stateblock_decref(stateblock);
1590 wined3d_mutex_unlock();
1591 return E_FAIL;
1593 ++*token;
1595 TRACE("Returning %#x (%p).\n", *token, stateblock);
1597 return hr;
1600 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1602 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1603 struct wined3d_stateblock *stateblock;
1605 TRACE("iface %p, token %#x.\n", iface, token);
1607 if (!token)
1608 return D3D_OK;
1610 wined3d_mutex_lock();
1611 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1612 if (!stateblock)
1614 WARN("Invalid handle (%#x) passed.\n", token);
1615 wined3d_mutex_unlock();
1616 return D3DERR_INVALIDCALL;
1618 wined3d_stateblock_apply(stateblock);
1619 wined3d_mutex_unlock();
1621 return D3D_OK;
1624 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1626 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1627 struct wined3d_stateblock *stateblock;
1629 TRACE("iface %p, token %#x.\n", iface, token);
1631 wined3d_mutex_lock();
1632 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1633 if (!stateblock)
1635 WARN("Invalid handle (%#x) passed.\n", token);
1636 wined3d_mutex_unlock();
1637 return D3DERR_INVALIDCALL;
1639 wined3d_stateblock_capture(stateblock);
1640 wined3d_mutex_unlock();
1642 return D3D_OK;
1645 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1647 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1648 struct wined3d_stateblock *stateblock;
1650 TRACE("iface %p, token %#x.\n", iface, token);
1652 wined3d_mutex_lock();
1653 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1655 if (!stateblock)
1657 WARN("Invalid handle (%#x) passed.\n", token);
1658 wined3d_mutex_unlock();
1659 return D3DERR_INVALIDCALL;
1662 if (wined3d_stateblock_decref(stateblock))
1664 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1666 wined3d_mutex_unlock();
1668 return D3D_OK;
1671 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1672 D3DSTATEBLOCKTYPE type, DWORD *handle)
1674 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1675 struct wined3d_stateblock *stateblock;
1676 HRESULT hr;
1678 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1680 if (type != D3DSBT_ALL
1681 && type != D3DSBT_PIXELSTATE
1682 && type != D3DSBT_VERTEXSTATE)
1684 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1685 return D3DERR_INVALIDCALL;
1688 wined3d_mutex_lock();
1689 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1690 if (FAILED(hr))
1692 wined3d_mutex_unlock();
1693 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1694 return hr;
1697 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1698 wined3d_mutex_unlock();
1700 if (*handle == D3D8_INVALID_HANDLE)
1702 ERR("Failed to allocate a handle.\n");
1703 wined3d_mutex_lock();
1704 wined3d_stateblock_decref(stateblock);
1705 wined3d_mutex_unlock();
1706 return E_FAIL;
1708 ++*handle;
1710 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1712 return hr;
1715 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1718 HRESULT hr;
1720 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1721 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1723 wined3d_mutex_lock();
1724 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1725 wined3d_mutex_unlock();
1727 return hr;
1730 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1732 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1733 HRESULT hr;
1735 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1737 wined3d_mutex_lock();
1738 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1739 wined3d_mutex_unlock();
1741 return hr;
1744 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1746 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1747 struct wined3d_texture *wined3d_texture;
1748 struct d3d8_texture *texture_impl;
1750 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1752 if (!texture)
1753 return D3DERR_INVALIDCALL;
1755 wined3d_mutex_lock();
1756 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1758 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1759 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1760 IDirect3DBaseTexture8_AddRef(*texture);
1762 else
1764 *texture = NULL;
1766 wined3d_mutex_unlock();
1768 return D3D_OK;
1771 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1774 struct d3d8_texture *texture_impl;
1775 HRESULT hr;
1777 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1779 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1781 wined3d_mutex_lock();
1782 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1783 texture_impl ? texture_impl->wined3d_texture : NULL);
1784 wined3d_mutex_unlock();
1786 return hr;
1789 static const struct tss_lookup
1791 BOOL sampler_state;
1792 enum wined3d_texture_stage_state state;
1794 tss_lookup[] =
1796 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1797 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1798 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1799 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1800 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1801 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1802 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1803 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1804 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1805 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1806 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1807 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1808 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1809 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1810 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1811 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1812 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1813 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1814 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1815 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1816 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1817 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1818 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1819 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1820 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1821 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1822 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1823 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1824 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1827 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1828 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1830 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1831 const struct tss_lookup *l;
1833 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1835 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1837 WARN("Invalid Type %#x passed.\n", Type);
1838 return D3D_OK;
1841 l = &tss_lookup[Type];
1843 wined3d_mutex_lock();
1844 if (l->sampler_state)
1845 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1846 else
1847 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1848 wined3d_mutex_unlock();
1850 return D3D_OK;
1853 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1854 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1856 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1857 const struct tss_lookup *l;
1859 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1861 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1863 WARN("Invalid type %#x passed.\n", type);
1864 return D3D_OK;
1867 l = &tss_lookup[type];
1869 wined3d_mutex_lock();
1870 if (l->sampler_state)
1871 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1872 else
1873 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1874 wined3d_mutex_unlock();
1876 return D3D_OK;
1879 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1881 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1882 HRESULT hr;
1884 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1886 wined3d_mutex_lock();
1887 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1888 wined3d_mutex_unlock();
1890 return hr;
1893 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1894 DWORD info_id, void *info, DWORD info_size)
1896 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1898 return D3D_OK;
1901 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1902 UINT palette_idx, const PALETTEENTRY *entries)
1904 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1906 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1907 * does not have a d3d8/9-style palette API */
1909 return D3D_OK;
1912 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1913 UINT palette_idx, PALETTEENTRY *entries)
1915 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1917 return D3DERR_INVALIDCALL;
1920 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1922 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1924 return D3D_OK;
1927 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1929 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1931 return D3DERR_INVALIDCALL;
1934 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1935 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1937 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1938 HRESULT hr;
1940 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1941 iface, primitive_type, start_vertex, primitive_count);
1943 wined3d_mutex_lock();
1944 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1945 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1946 vertex_count_from_primitive_count(primitive_type, primitive_count));
1947 wined3d_mutex_unlock();
1949 return hr;
1952 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1953 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1954 UINT start_idx, UINT primitive_count)
1956 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1957 HRESULT hr;
1959 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1960 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1962 wined3d_mutex_lock();
1963 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1964 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1965 vertex_count_from_primitive_count(primitive_type, primitive_count));
1966 wined3d_mutex_unlock();
1968 return hr;
1971 /* The caller is responsible for wined3d locking */
1972 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1974 HRESULT hr;
1976 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1978 UINT size = max(device->vertex_buffer_size * 2, min_size);
1979 struct wined3d_buffer *buffer;
1981 TRACE("Growing vertex buffer to %u bytes\n", size);
1983 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1984 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1985 if (FAILED(hr))
1987 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1988 return hr;
1991 if (device->vertex_buffer)
1992 wined3d_buffer_decref(device->vertex_buffer);
1994 device->vertex_buffer = buffer;
1995 device->vertex_buffer_size = size;
1996 device->vertex_buffer_pos = 0;
1998 return D3D_OK;
2001 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2002 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2003 UINT stride)
2005 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2006 HRESULT hr;
2007 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2008 UINT size = vtx_count * stride;
2009 UINT vb_pos, align;
2010 BYTE *buffer_data;
2012 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2013 iface, primitive_type, primitive_count, data, stride);
2015 if (!primitive_count)
2017 WARN("primitive_count is 0, returning D3D_OK\n");
2018 return D3D_OK;
2021 wined3d_mutex_lock();
2022 hr = d3d8_device_prepare_vertex_buffer(device, size);
2023 if (FAILED(hr))
2024 goto done;
2026 vb_pos = device->vertex_buffer_pos;
2027 align = vb_pos % stride;
2028 if (align) align = stride - align;
2029 if (vb_pos + size + align > device->vertex_buffer_size)
2030 vb_pos = 0;
2031 else
2032 vb_pos += align;
2034 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2035 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2036 if (FAILED(hr))
2037 goto done;
2038 memcpy(buffer_data, data, size);
2039 wined3d_buffer_unmap(device->vertex_buffer);
2040 device->vertex_buffer_pos = vb_pos + size;
2042 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2043 if (FAILED(hr))
2044 goto done;
2046 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2047 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2048 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2050 done:
2051 wined3d_mutex_unlock();
2052 return hr;
2055 /* The caller is responsible for wined3d locking */
2056 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2058 HRESULT hr;
2060 if (device->index_buffer_size < min_size || !device->index_buffer)
2062 UINT size = max(device->index_buffer_size * 2, min_size);
2063 struct wined3d_buffer *buffer;
2065 TRACE("Growing index buffer to %u bytes\n", size);
2067 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2068 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2069 if (FAILED(hr))
2071 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2072 return hr;
2075 if (device->index_buffer)
2076 wined3d_buffer_decref(device->index_buffer);
2078 device->index_buffer = buffer;
2079 device->index_buffer_size = size;
2080 device->index_buffer_pos = 0;
2082 return D3D_OK;
2085 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2086 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2087 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2088 const void *vertex_data, UINT vertex_stride)
2090 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2091 HRESULT hr;
2092 BYTE *buffer_data;
2094 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2095 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2096 UINT idx_size = idx_count * idx_fmt_size;
2097 UINT ib_pos;
2099 UINT vtx_size = vertex_count * vertex_stride;
2100 UINT vb_pos, align;
2102 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2103 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2104 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2105 index_data, index_format, vertex_data, vertex_stride);
2107 if (!primitive_count)
2109 WARN("primitive_count is 0, returning D3D_OK\n");
2110 return D3D_OK;
2113 wined3d_mutex_lock();
2115 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2116 if (FAILED(hr))
2117 goto done;
2119 vb_pos = device->vertex_buffer_pos;
2120 align = vb_pos % vertex_stride;
2121 if (align) align = vertex_stride - align;
2122 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2123 vb_pos = 0;
2124 else
2125 vb_pos += align;
2127 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2128 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2129 if (FAILED(hr))
2130 goto done;
2131 memcpy(buffer_data, vertex_data, vtx_size);
2132 wined3d_buffer_unmap(device->vertex_buffer);
2133 device->vertex_buffer_pos = vb_pos + vtx_size;
2135 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2136 if (FAILED(hr))
2137 goto done;
2139 ib_pos = device->index_buffer_pos;
2140 align = ib_pos % idx_fmt_size;
2141 if (align) align = idx_fmt_size - align;
2142 if (ib_pos + idx_size + align > device->index_buffer_size)
2143 ib_pos = 0;
2144 else
2145 ib_pos += align;
2147 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2148 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2149 if (FAILED(hr))
2150 goto done;
2151 memcpy(buffer_data, index_data, idx_size);
2152 wined3d_buffer_unmap(device->index_buffer);
2153 device->index_buffer_pos = ib_pos + idx_size;
2155 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2156 if (FAILED(hr))
2157 goto done;
2159 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2160 wined3dformat_from_d3dformat(index_format));
2161 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2163 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2164 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2166 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2167 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2168 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2170 done:
2171 wined3d_mutex_unlock();
2172 return hr;
2175 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2176 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2178 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2179 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2180 HRESULT hr;
2182 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2183 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2185 wined3d_mutex_lock();
2186 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2187 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2188 wined3d_mutex_unlock();
2190 return hr;
2193 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2194 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2196 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2197 struct d3d8_vertex_shader *object;
2198 DWORD shader_handle;
2199 DWORD handle;
2200 HRESULT hr;
2202 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2203 iface, declaration, byte_code, shader, usage);
2205 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2206 if (!object)
2208 *shader = 0;
2209 return E_OUTOFMEMORY;
2212 wined3d_mutex_lock();
2213 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2214 wined3d_mutex_unlock();
2215 if (handle == D3D8_INVALID_HANDLE)
2217 ERR("Failed to allocate vertex shader handle.\n");
2218 HeapFree(GetProcessHeap(), 0, object);
2219 *shader = 0;
2220 return E_OUTOFMEMORY;
2223 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2225 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2226 if (FAILED(hr))
2228 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2229 wined3d_mutex_lock();
2230 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2231 wined3d_mutex_unlock();
2232 HeapFree(GetProcessHeap(), 0, object);
2233 *shader = 0;
2234 return hr;
2237 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2238 *shader = shader_handle;
2240 return D3D_OK;
2243 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2245 struct d3d8_vertex_declaration *d3d8_declaration;
2246 struct FvfToDecl *convertedDecls = device->decls;
2247 int p, low, high; /* deliberately signed */
2248 HRESULT hr;
2250 TRACE("Searching for declaration for fvf %08x... ", fvf);
2252 low = 0;
2253 high = device->numConvertedDecls - 1;
2254 while (low <= high)
2256 p = (low + high) >> 1;
2257 TRACE("%d ", p);
2259 if (convertedDecls[p].fvf == fvf)
2261 TRACE("found %p\n", convertedDecls[p].declaration);
2262 return convertedDecls[p].declaration;
2265 if (convertedDecls[p].fvf < fvf)
2266 low = p + 1;
2267 else
2268 high = p - 1;
2270 TRACE("not found. Creating and inserting at position %d.\n", low);
2272 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2273 return NULL;
2275 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2277 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2278 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2279 return NULL;
2282 if (device->declArraySize == device->numConvertedDecls)
2284 UINT grow = device->declArraySize / 2;
2286 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2287 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2288 if (!convertedDecls)
2290 d3d8_vertex_declaration_destroy(d3d8_declaration);
2291 return NULL;
2293 device->decls = convertedDecls;
2294 device->declArraySize += grow;
2297 memmove(convertedDecls + low + 1, convertedDecls + low,
2298 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2299 convertedDecls[low].declaration = d3d8_declaration;
2300 convertedDecls[low].fvf = fvf;
2301 ++device->numConvertedDecls;
2303 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2305 return d3d8_declaration;
2308 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2310 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2311 struct d3d8_vertex_shader *shader_impl;
2313 TRACE("iface %p, shader %#x.\n", iface, shader);
2315 if (VS_HIGHESTFIXEDFXF >= shader)
2317 TRACE("Setting FVF, %#x\n", shader);
2319 wined3d_mutex_lock();
2320 wined3d_device_set_vertex_declaration(device->wined3d_device,
2321 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2322 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2323 wined3d_mutex_unlock();
2325 return D3D_OK;
2328 TRACE("Setting shader\n");
2330 wined3d_mutex_lock();
2331 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2333 WARN("Invalid handle (%#x) passed.\n", shader);
2334 wined3d_mutex_unlock();
2336 return D3DERR_INVALIDCALL;
2339 wined3d_device_set_vertex_declaration(device->wined3d_device,
2340 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2341 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2342 wined3d_mutex_unlock();
2344 return D3D_OK;
2347 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2350 struct wined3d_vertex_declaration *wined3d_declaration;
2351 struct d3d8_vertex_declaration *d3d8_declaration;
2353 TRACE("iface %p, shader %p.\n", iface, shader);
2355 wined3d_mutex_lock();
2356 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2358 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2359 *shader = d3d8_declaration->shader_handle;
2361 else
2363 *shader = 0;
2365 wined3d_mutex_unlock();
2367 TRACE("Returning %#x.\n", *shader);
2369 return D3D_OK;
2372 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2374 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2375 struct d3d8_vertex_shader *shader_impl;
2377 TRACE("iface %p, shader %#x.\n", iface, shader);
2379 wined3d_mutex_lock();
2380 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2382 WARN("Invalid handle (%#x) passed.\n", shader);
2383 wined3d_mutex_unlock();
2385 return D3DERR_INVALIDCALL;
2388 if (shader_impl->wined3d_shader
2389 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2390 IDirect3DDevice8_SetVertexShader(iface, 0);
2392 wined3d_mutex_unlock();
2394 d3d8_vertex_shader_destroy(shader_impl);
2396 return D3D_OK;
2399 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2400 DWORD start_register, const void *data, DWORD count)
2402 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2403 HRESULT hr;
2405 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2406 iface, start_register, data, count);
2408 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2410 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2411 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2412 return D3DERR_INVALIDCALL;
2415 wined3d_mutex_lock();
2416 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2417 wined3d_mutex_unlock();
2419 return hr;
2422 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2423 DWORD start_register, void *data, DWORD count)
2425 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2426 HRESULT hr;
2428 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2429 iface, start_register, data, count);
2431 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2433 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2434 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2435 return D3DERR_INVALIDCALL;
2438 wined3d_mutex_lock();
2439 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2440 wined3d_mutex_unlock();
2442 return hr;
2445 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2446 DWORD shader, void *data, DWORD *data_size)
2448 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2449 struct d3d8_vertex_declaration *declaration;
2450 struct d3d8_vertex_shader *shader_impl;
2452 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2453 iface, shader, data, data_size);
2455 wined3d_mutex_lock();
2456 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2457 wined3d_mutex_unlock();
2459 if (!shader_impl)
2461 WARN("Invalid handle (%#x) passed.\n", shader);
2462 return D3DERR_INVALIDCALL;
2464 declaration = shader_impl->vertex_declaration;
2466 if (!data)
2468 *data_size = declaration->elements_size;
2469 return D3D_OK;
2472 /* MSDN claims that if *data_size is smaller than the required size
2473 * we should write the required size and return D3DERR_MOREDATA.
2474 * That's not actually true. */
2475 if (*data_size < declaration->elements_size)
2476 return D3DERR_INVALIDCALL;
2478 memcpy(data, declaration->elements, declaration->elements_size);
2480 return D3D_OK;
2483 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2484 DWORD shader, void *data, DWORD *data_size)
2486 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2487 struct d3d8_vertex_shader *shader_impl = NULL;
2488 HRESULT hr;
2490 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2491 iface, shader, data, data_size);
2493 wined3d_mutex_lock();
2494 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2496 WARN("Invalid handle (%#x) passed.\n", shader);
2497 wined3d_mutex_unlock();
2499 return D3DERR_INVALIDCALL;
2502 if (!shader_impl->wined3d_shader)
2504 wined3d_mutex_unlock();
2505 *data_size = 0;
2506 return D3D_OK;
2509 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2510 wined3d_mutex_unlock();
2512 return hr;
2515 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2516 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2518 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2519 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2521 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2523 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2524 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2525 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2526 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2527 * problem)
2529 wined3d_mutex_lock();
2530 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2531 wined3d_device_set_index_buffer(device->wined3d_device,
2532 ib ? ib->wined3d_buffer : NULL,
2533 ib ? ib->format : WINED3DFMT_UNKNOWN);
2534 wined3d_mutex_unlock();
2536 return D3D_OK;
2539 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2540 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2542 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2543 enum wined3d_format_id wined3d_format;
2544 struct wined3d_buffer *wined3d_buffer;
2545 struct d3d8_indexbuffer *buffer_impl;
2547 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2549 if (!buffer)
2550 return D3DERR_INVALIDCALL;
2552 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2553 wined3d_mutex_lock();
2554 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2555 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2557 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2558 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2559 IDirect3DIndexBuffer8_AddRef(*buffer);
2561 else
2563 *buffer = NULL;
2565 wined3d_mutex_unlock();
2567 return D3D_OK;
2570 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2571 const DWORD *byte_code, DWORD *shader)
2573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2574 struct d3d8_pixel_shader *object;
2575 DWORD shader_handle;
2576 DWORD handle;
2577 HRESULT hr;
2579 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2581 if (!shader)
2582 return D3DERR_INVALIDCALL;
2584 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2585 if (!object)
2586 return E_OUTOFMEMORY;
2588 wined3d_mutex_lock();
2589 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2590 wined3d_mutex_unlock();
2591 if (handle == D3D8_INVALID_HANDLE)
2593 ERR("Failed to allocate pixel shader handle.\n");
2594 HeapFree(GetProcessHeap(), 0, object);
2595 return E_OUTOFMEMORY;
2598 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2600 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2601 if (FAILED(hr))
2603 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2604 wined3d_mutex_lock();
2605 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2606 wined3d_mutex_unlock();
2607 HeapFree(GetProcessHeap(), 0, object);
2608 *shader = 0;
2609 return hr;
2612 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2613 *shader = shader_handle;
2615 return D3D_OK;
2618 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2620 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2621 struct d3d8_pixel_shader *shader_impl;
2623 TRACE("iface %p, shader %#x.\n", iface, shader);
2625 wined3d_mutex_lock();
2627 if (!shader)
2629 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2630 wined3d_mutex_unlock();
2631 return D3D_OK;
2634 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2636 WARN("Invalid handle (%#x) passed.\n", shader);
2637 wined3d_mutex_unlock();
2638 return D3DERR_INVALIDCALL;
2641 TRACE("Setting shader %p.\n", shader_impl);
2642 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2643 wined3d_mutex_unlock();
2645 return D3D_OK;
2648 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2650 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2651 struct wined3d_shader *object;
2653 TRACE("iface %p, shader %p.\n", iface, shader);
2655 if (!shader)
2656 return D3DERR_INVALIDCALL;
2658 wined3d_mutex_lock();
2659 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2661 struct d3d8_pixel_shader *d3d8_shader;
2662 d3d8_shader = wined3d_shader_get_parent(object);
2663 *shader = d3d8_shader->handle;
2665 else
2667 *shader = 0;
2669 wined3d_mutex_unlock();
2671 TRACE("Returning %#x.\n", *shader);
2673 return D3D_OK;
2676 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2678 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2679 struct d3d8_pixel_shader *shader_impl;
2681 TRACE("iface %p, shader %#x.\n", iface, shader);
2683 wined3d_mutex_lock();
2685 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2687 WARN("Invalid handle (%#x) passed.\n", shader);
2688 wined3d_mutex_unlock();
2689 return D3DERR_INVALIDCALL;
2692 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2693 IDirect3DDevice8_SetPixelShader(iface, 0);
2695 wined3d_mutex_unlock();
2697 d3d8_pixel_shader_destroy(shader_impl);
2699 return D3D_OK;
2702 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2703 DWORD start_register, const void *data, DWORD count)
2705 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2706 HRESULT hr;
2708 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2709 iface, start_register, data, count);
2711 wined3d_mutex_lock();
2712 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2713 wined3d_mutex_unlock();
2715 return hr;
2718 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2719 DWORD start_register, void *data, DWORD count)
2721 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2722 HRESULT hr;
2724 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2725 iface, start_register, data, count);
2727 wined3d_mutex_lock();
2728 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2729 wined3d_mutex_unlock();
2731 return hr;
2734 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2735 DWORD shader, void *data, DWORD *data_size)
2737 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2738 struct d3d8_pixel_shader *shader_impl = NULL;
2739 HRESULT hr;
2741 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2742 iface, shader, data, data_size);
2744 wined3d_mutex_lock();
2745 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2747 WARN("Invalid handle (%#x) passed.\n", shader);
2748 wined3d_mutex_unlock();
2750 return D3DERR_INVALIDCALL;
2753 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2754 wined3d_mutex_unlock();
2756 return hr;
2759 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2760 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2762 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2763 iface, handle, segment_count, patch_info);
2764 return D3D_OK;
2767 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2768 const float *segment_count, const D3DTRIPATCH_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_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2777 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2778 return D3DERR_INVALIDCALL;
2781 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2782 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2784 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2785 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2786 HRESULT hr;
2788 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2789 iface, stream_idx, buffer, stride);
2791 wined3d_mutex_lock();
2792 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2793 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2794 wined3d_mutex_unlock();
2796 return hr;
2799 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2800 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2802 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2803 struct d3d8_vertexbuffer *buffer_impl;
2804 struct wined3d_buffer *wined3d_buffer = NULL;
2805 HRESULT hr;
2807 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2808 iface, stream_idx, buffer, stride);
2810 if (!buffer)
2811 return D3DERR_INVALIDCALL;
2813 wined3d_mutex_lock();
2814 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2815 if (SUCCEEDED(hr) && wined3d_buffer)
2817 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2818 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2819 IDirect3DVertexBuffer8_AddRef(*buffer);
2821 else
2823 if (FAILED(hr))
2824 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2825 *buffer = NULL;
2827 wined3d_mutex_unlock();
2829 return hr;
2832 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2834 d3d8_device_QueryInterface,
2835 d3d8_device_AddRef,
2836 d3d8_device_Release,
2837 d3d8_device_TestCooperativeLevel,
2838 d3d8_device_GetAvailableTextureMem,
2839 d3d8_device_ResourceManagerDiscardBytes,
2840 d3d8_device_GetDirect3D,
2841 d3d8_device_GetDeviceCaps,
2842 d3d8_device_GetDisplayMode,
2843 d3d8_device_GetCreationParameters,
2844 d3d8_device_SetCursorProperties,
2845 d3d8_device_SetCursorPosition,
2846 d3d8_device_ShowCursor,
2847 d3d8_device_CreateAdditionalSwapChain,
2848 d3d8_device_Reset,
2849 d3d8_device_Present,
2850 d3d8_device_GetBackBuffer,
2851 d3d8_device_GetRasterStatus,
2852 d3d8_device_SetGammaRamp,
2853 d3d8_device_GetGammaRamp,
2854 d3d8_device_CreateTexture,
2855 d3d8_device_CreateVolumeTexture,
2856 d3d8_device_CreateCubeTexture,
2857 d3d8_device_CreateVertexBuffer,
2858 d3d8_device_CreateIndexBuffer,
2859 d3d8_device_CreateRenderTarget,
2860 d3d8_device_CreateDepthStencilSurface,
2861 d3d8_device_CreateImageSurface,
2862 d3d8_device_CopyRects,
2863 d3d8_device_UpdateTexture,
2864 d3d8_device_GetFrontBuffer,
2865 d3d8_device_SetRenderTarget,
2866 d3d8_device_GetRenderTarget,
2867 d3d8_device_GetDepthStencilSurface,
2868 d3d8_device_BeginScene,
2869 d3d8_device_EndScene,
2870 d3d8_device_Clear,
2871 d3d8_device_SetTransform,
2872 d3d8_device_GetTransform,
2873 d3d8_device_MultiplyTransform,
2874 d3d8_device_SetViewport,
2875 d3d8_device_GetViewport,
2876 d3d8_device_SetMaterial,
2877 d3d8_device_GetMaterial,
2878 d3d8_device_SetLight,
2879 d3d8_device_GetLight,
2880 d3d8_device_LightEnable,
2881 d3d8_device_GetLightEnable,
2882 d3d8_device_SetClipPlane,
2883 d3d8_device_GetClipPlane,
2884 d3d8_device_SetRenderState,
2885 d3d8_device_GetRenderState,
2886 d3d8_device_BeginStateBlock,
2887 d3d8_device_EndStateBlock,
2888 d3d8_device_ApplyStateBlock,
2889 d3d8_device_CaptureStateBlock,
2890 d3d8_device_DeleteStateBlock,
2891 d3d8_device_CreateStateBlock,
2892 d3d8_device_SetClipStatus,
2893 d3d8_device_GetClipStatus,
2894 d3d8_device_GetTexture,
2895 d3d8_device_SetTexture,
2896 d3d8_device_GetTextureStageState,
2897 d3d8_device_SetTextureStageState,
2898 d3d8_device_ValidateDevice,
2899 d3d8_device_GetInfo,
2900 d3d8_device_SetPaletteEntries,
2901 d3d8_device_GetPaletteEntries,
2902 d3d8_device_SetCurrentTexturePalette,
2903 d3d8_device_GetCurrentTexturePalette,
2904 d3d8_device_DrawPrimitive,
2905 d3d8_device_DrawIndexedPrimitive,
2906 d3d8_device_DrawPrimitiveUP,
2907 d3d8_device_DrawIndexedPrimitiveUP,
2908 d3d8_device_ProcessVertices,
2909 d3d8_device_CreateVertexShader,
2910 d3d8_device_SetVertexShader,
2911 d3d8_device_GetVertexShader,
2912 d3d8_device_DeleteVertexShader,
2913 d3d8_device_SetVertexShaderConstant,
2914 d3d8_device_GetVertexShaderConstant,
2915 d3d8_device_GetVertexShaderDeclaration,
2916 d3d8_device_GetVertexShaderFunction,
2917 d3d8_device_SetStreamSource,
2918 d3d8_device_GetStreamSource,
2919 d3d8_device_SetIndices,
2920 d3d8_device_GetIndices,
2921 d3d8_device_CreatePixelShader,
2922 d3d8_device_SetPixelShader,
2923 d3d8_device_GetPixelShader,
2924 d3d8_device_DeletePixelShader,
2925 d3d8_device_SetPixelShaderConstant,
2926 d3d8_device_GetPixelShaderConstant,
2927 d3d8_device_GetPixelShaderFunction,
2928 d3d8_device_DrawRectPatch,
2929 d3d8_device_DrawTriPatch,
2930 d3d8_device_DeletePatch,
2933 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2935 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2938 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2939 struct wined3d_device *device)
2941 TRACE("device_parent %p, device %p\n", device_parent, device);
2944 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2946 TRACE("device_parent %p.\n", device_parent);
2949 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2951 struct d3d8_device *device = device_from_device_parent(device_parent);
2953 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2955 if (!activate)
2956 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2957 else
2958 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2961 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2962 void *container_parent, struct wined3d_surface *surface, void **parent,
2963 const struct wined3d_parent_ops **parent_ops)
2965 struct d3d8_surface *d3d_surface;
2967 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
2968 device_parent, container_parent, surface, parent, parent_ops);
2970 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2971 return E_OUTOFMEMORY;
2973 surface_init(d3d_surface, container_parent, surface, parent_ops);
2974 *parent = d3d_surface;
2975 TRACE("Created surface %p.\n", d3d_surface);
2977 return D3D_OK;
2980 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
2981 void *container_parent, struct wined3d_volume *volume, void **parent,
2982 const struct wined3d_parent_ops **parent_ops)
2984 struct d3d8_volume *d3d_volume;
2986 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
2987 device_parent, container_parent, volume, parent, parent_ops);
2989 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
2990 return E_OUTOFMEMORY;
2992 volume_init(d3d_volume, container_parent, volume, parent_ops);
2993 *parent = d3d_volume;
2994 TRACE("Created volume %p.\n", d3d_volume);
2996 return D3D_OK;
2999 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3000 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3002 struct d3d8_device *device = device_from_device_parent(device_parent);
3003 struct wined3d_resource_desc texture_desc;
3004 struct d3d8_surface *d3d_surface;
3005 struct wined3d_texture *texture;
3006 HRESULT hr;
3008 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3009 device_parent, container_parent, desc, surface);
3011 texture_desc = *desc;
3012 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3013 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3014 WINED3D_SURFACE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
3016 WARN("Failed to create texture, hr %#x.\n", hr);
3017 return hr;
3020 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3021 wined3d_surface_incref(*surface);
3022 wined3d_texture_decref(texture);
3024 d3d_surface = wined3d_surface_get_parent(*surface);
3025 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3027 return hr;
3030 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3031 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3033 struct d3d8_device *device = device_from_device_parent(device_parent);
3034 struct d3d8_swapchain *d3d_swapchain;
3035 HRESULT hr;
3037 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3039 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3041 WARN("Failed to create swapchain, hr %#x.\n", hr);
3042 *swapchain = NULL;
3043 return hr;
3046 *swapchain = d3d_swapchain->wined3d_swapchain;
3047 wined3d_swapchain_incref(*swapchain);
3048 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3050 return hr;
3053 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3055 device_parent_wined3d_device_created,
3056 device_parent_mode_changed,
3057 device_parent_activate,
3058 device_parent_surface_created,
3059 device_parent_volume_created,
3060 device_parent_create_swapchain_surface,
3061 device_parent_create_swapchain,
3064 static void setup_fpu(void)
3066 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3067 WORD cw;
3068 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3069 cw = (cw & ~0xf3f) | 0x3f;
3070 __asm__ volatile ("fldcw %0" : : "m" (cw));
3071 #elif defined(__i386__) && defined(_MSC_VER)
3072 WORD cw;
3073 __asm fnstcw cw;
3074 cw = (cw & ~0xf3f) | 0x3f;
3075 __asm fldcw cw;
3076 #else
3077 FIXME("FPU setup not implemented for this platform.\n");
3078 #endif
3081 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3082 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3084 struct wined3d_swapchain_desc swapchain_desc;
3085 HRESULT hr;
3087 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3088 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3089 device->ref = 1;
3090 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3091 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3092 if (!device->handle_table.entries)
3094 ERR("Failed to allocate handle table memory.\n");
3095 return E_OUTOFMEMORY;
3097 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3099 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3101 wined3d_mutex_lock();
3102 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3103 &device->device_parent, &device->wined3d_device);
3104 if (FAILED(hr))
3106 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3107 wined3d_mutex_unlock();
3108 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3109 return hr;
3112 if (!parameters->Windowed)
3114 HWND device_window = parameters->hDeviceWindow;
3116 if (!focus_window)
3117 focus_window = device_window;
3118 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3120 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3121 wined3d_device_decref(device->wined3d_device);
3122 wined3d_mutex_unlock();
3123 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3124 return hr;
3127 if (!device_window)
3128 device_window = focus_window;
3129 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3130 parameters->BackBufferWidth,
3131 parameters->BackBufferHeight);
3134 if (flags & D3DCREATE_MULTITHREADED)
3135 wined3d_device_set_multithreaded(device->wined3d_device);
3137 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3139 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3140 if (FAILED(hr))
3142 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3143 wined3d_device_release_focus_window(device->wined3d_device);
3144 wined3d_device_decref(device->wined3d_device);
3145 wined3d_mutex_unlock();
3146 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3147 return hr;
3150 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3151 wined3d_mutex_unlock();
3153 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3155 device->declArraySize = 16;
3156 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3157 if (!device->decls)
3159 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3160 hr = E_OUTOFMEMORY;
3161 goto err;
3164 device->d3d_parent = &parent->IDirect3D8_iface;
3165 IDirect3D8_AddRef(device->d3d_parent);
3167 return D3D_OK;
3169 err:
3170 wined3d_mutex_lock();
3171 wined3d_device_uninit_3d(device->wined3d_device);
3172 wined3d_device_release_focus_window(device->wined3d_device);
3173 wined3d_device_decref(device->wined3d_device);
3174 wined3d_mutex_unlock();
3175 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3176 return hr;