oleacc: Added CAccPropServices stub implementation.
[wine/multimedia.git] / dlls / d3d8 / device.c
blob429b30c8140717ae456296dbc9a9754cf19ab916
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 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
41 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
43 d3d8_null_wined3d_object_destroyed,
46 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
48 BYTE *c = (BYTE *)&format;
50 /* Don't translate FOURCC formats */
51 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
53 switch(format)
55 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
56 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
57 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
58 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
59 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
60 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
61 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
62 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
63 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
64 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
65 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
66 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
67 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
68 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
69 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
70 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
71 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
72 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
73 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
74 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
75 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
76 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
77 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
78 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
79 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
80 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
81 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
82 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
83 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
84 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
85 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
86 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
87 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
88 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
89 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
90 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
91 default:
92 FIXME("Unhandled wined3d format %#x.\n", format);
93 return D3DFMT_UNKNOWN;
97 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
99 BYTE *c = (BYTE *)&format;
101 /* Don't translate FOURCC formats */
102 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
104 switch(format)
106 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
107 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
108 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
109 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
110 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
111 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
112 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
113 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
114 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
115 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
116 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
117 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
118 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
119 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
120 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
131 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
132 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
133 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
134 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
135 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
136 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
137 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
138 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
139 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
140 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
141 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
142 default:
143 FIXME("Unhandled D3DFORMAT %#x\n", format);
144 return WINED3DFMT_UNKNOWN;
148 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
150 switch(primitive_type)
152 case D3DPT_POINTLIST:
153 return primitive_count;
155 case D3DPT_LINELIST:
156 return primitive_count * 2;
158 case D3DPT_LINESTRIP:
159 return primitive_count + 1;
161 case D3DPT_TRIANGLELIST:
162 return primitive_count * 3;
164 case D3DPT_TRIANGLESTRIP:
165 case D3DPT_TRIANGLEFAN:
166 return primitive_count + 2;
168 default:
169 FIXME("Unhandled primitive type %#x\n", primitive_type);
170 return 0;
174 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
175 const struct wined3d_swapchain_desc *swapchain_desc)
177 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
178 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
179 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
180 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
181 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
182 present_parameters->SwapEffect = swapchain_desc->swap_effect;
183 present_parameters->hDeviceWindow = swapchain_desc->device_window;
184 present_parameters->Windowed = swapchain_desc->windowed;
185 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
186 present_parameters->AutoDepthStencilFormat
187 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
188 present_parameters->Flags = swapchain_desc->flags;
189 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
190 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
193 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
194 const D3DPRESENT_PARAMETERS *present_parameters)
196 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
197 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
198 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
199 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
200 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
201 swapchain_desc->multisample_quality = 0; /* d3d9 only */
202 swapchain_desc->swap_effect = present_parameters->SwapEffect;
203 swapchain_desc->device_window = present_parameters->hDeviceWindow;
204 swapchain_desc->windowed = present_parameters->Windowed;
205 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
206 swapchain_desc->auto_depth_stencil_format
207 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
208 swapchain_desc->flags = present_parameters->Flags;
209 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
210 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
211 swapchain_desc->auto_restore_display_mode = TRUE;
214 /* Handle table functions */
215 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
217 struct d3d8_handle_entry *entry;
219 if (t->free_entries)
221 DWORD index = t->free_entries - t->entries;
222 /* Use a free handle */
223 entry = t->free_entries;
224 if (entry->type != D3D8_HANDLE_FREE)
226 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
227 return D3D8_INVALID_HANDLE;
229 t->free_entries = entry->object;
230 entry->object = object;
231 entry->type = type;
233 return index;
236 if (!(t->entry_count < t->table_size))
238 /* Grow the table */
239 UINT new_size = t->table_size + (t->table_size >> 1);
240 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
241 0, t->entries, new_size * sizeof(*t->entries));
242 if (!new_entries)
244 ERR("Failed to grow the handle table.\n");
245 return D3D8_INVALID_HANDLE;
247 t->entries = new_entries;
248 t->table_size = new_size;
251 entry = &t->entries[t->entry_count];
252 entry->object = object;
253 entry->type = type;
255 return t->entry_count++;
258 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
260 struct d3d8_handle_entry *entry;
261 void *object;
263 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
265 WARN("Invalid handle %u passed.\n", handle);
266 return NULL;
269 entry = &t->entries[handle];
270 if (entry->type != type)
272 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
273 return NULL;
276 object = entry->object;
277 entry->object = t->free_entries;
278 entry->type = D3D8_HANDLE_FREE;
279 t->free_entries = entry;
281 return object;
284 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
286 struct d3d8_handle_entry *entry;
288 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
290 WARN("Invalid handle %u passed.\n", handle);
291 return NULL;
294 entry = &t->entries[handle];
295 if (entry->type != type)
297 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
298 return NULL;
301 return entry->object;
304 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
306 TRACE("iface %p, riid %s, out %p.\n",
307 iface, debugstr_guid(riid), out);
309 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
310 || IsEqualGUID(riid, &IID_IUnknown))
312 IDirect3DDevice8_AddRef(iface);
313 *out = iface;
314 return S_OK;
317 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
319 *out = NULL;
320 return E_NOINTERFACE;
323 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
325 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
326 ULONG ref = InterlockedIncrement(&device->ref);
328 TRACE("%p increasing refcount to %u.\n", iface, ref);
330 return ref;
333 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
335 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
336 ULONG ref;
338 if (device->inDestruction)
339 return 0;
341 ref = InterlockedDecrement(&device->ref);
343 TRACE("%p decreasing refcount to %u.\n", iface, ref);
345 if (!ref)
347 IDirect3D8 *parent = device->d3d_parent;
348 unsigned i;
350 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
352 wined3d_mutex_lock();
354 device->inDestruction = TRUE;
356 for (i = 0; i < device->numConvertedDecls; ++i)
358 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
360 HeapFree(GetProcessHeap(), 0, device->decls);
362 if (device->vertex_buffer)
363 wined3d_buffer_decref(device->vertex_buffer);
364 if (device->index_buffer)
365 wined3d_buffer_decref(device->index_buffer);
367 wined3d_device_uninit_3d(device->wined3d_device);
368 wined3d_device_release_focus_window(device->wined3d_device);
369 wined3d_device_decref(device->wined3d_device);
370 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
371 HeapFree(GetProcessHeap(), 0, device);
373 wined3d_mutex_unlock();
375 IDirect3D8_Release(parent);
377 return ref;
380 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
382 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
384 TRACE("iface %p.\n", iface);
386 TRACE("device state: %#x.\n", device->device_state);
388 switch (device->device_state)
390 default:
391 case D3D8_DEVICE_STATE_OK:
392 return D3D_OK;
393 case D3D8_DEVICE_STATE_LOST:
394 return D3DERR_DEVICELOST;
395 case D3D8_DEVICE_STATE_NOT_RESET:
396 return D3DERR_DEVICENOTRESET;
400 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
402 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
403 UINT ret;
405 TRACE("iface %p.\n", iface);
407 wined3d_mutex_lock();
408 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
409 wined3d_mutex_unlock();
411 return ret;
414 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
418 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
420 if (byte_count)
421 FIXME("Byte count ignored.\n");
423 wined3d_mutex_lock();
424 wined3d_device_evict_managed_resources(device->wined3d_device);
425 wined3d_mutex_unlock();
427 return D3D_OK;
430 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
432 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
434 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
436 if (!d3d8)
437 return D3DERR_INVALIDCALL;
439 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
442 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
444 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
445 WINED3DCAPS *wined3d_caps;
446 HRESULT hr;
448 TRACE("iface %p, caps %p.\n", iface, caps);
450 if (!caps)
451 return D3DERR_INVALIDCALL;
453 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
454 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
456 wined3d_mutex_lock();
457 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
458 wined3d_mutex_unlock();
460 fixup_caps(wined3d_caps);
461 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
462 HeapFree(GetProcessHeap(), 0, wined3d_caps);
464 return hr;
467 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
469 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
470 struct wined3d_display_mode wined3d_mode;
471 HRESULT hr;
473 TRACE("iface %p, mode %p.\n", iface, mode);
475 wined3d_mutex_lock();
476 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
477 wined3d_mutex_unlock();
479 if (SUCCEEDED(hr))
481 mode->Width = wined3d_mode.width;
482 mode->Height = wined3d_mode.height;
483 mode->RefreshRate = wined3d_mode.refresh_rate;
484 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
487 return hr;
490 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
491 D3DDEVICE_CREATION_PARAMETERS *parameters)
493 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
495 TRACE("iface %p, parameters %p.\n", iface, parameters);
497 wined3d_mutex_lock();
498 wined3d_device_get_creation_parameters(device->wined3d_device,
499 (struct wined3d_device_creation_parameters *)parameters);
500 wined3d_mutex_unlock();
502 return D3D_OK;
505 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
506 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
508 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
509 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
510 HRESULT hr;
512 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
513 iface, hotspot_x, hotspot_y, bitmap);
515 if (!bitmap)
517 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
518 return D3DERR_INVALIDCALL;
521 wined3d_mutex_lock();
522 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
523 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
524 wined3d_mutex_unlock();
526 return hr;
529 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
531 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
533 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
535 wined3d_mutex_lock();
536 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
537 wined3d_mutex_unlock();
540 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
542 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
543 BOOL ret;
545 TRACE("iface %p, show %#x.\n", iface, show);
547 wined3d_mutex_lock();
548 ret = wined3d_device_show_cursor(device->wined3d_device, show);
549 wined3d_mutex_unlock();
551 return ret;
554 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
555 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
557 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
558 struct wined3d_swapchain_desc desc;
559 struct d3d8_swapchain *object;
561 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
562 iface, present_parameters, swapchain);
564 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
565 if (SUCCEEDED(d3d8_swapchain_create(device, &desc, &object)))
566 *swapchain = &object->IDirect3DSwapChain8_iface;
567 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
569 return D3D_OK;
572 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
574 struct wined3d_resource_desc desc;
576 wined3d_resource_get_desc(resource, &desc);
577 if (desc.pool == WINED3D_POOL_DEFAULT)
579 struct d3d8_surface *surface;
581 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
583 IUnknown *parent = wined3d_resource_get_parent(resource);
584 IDirect3DBaseTexture8 *texture;
586 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
588 IDirect3DBaseTexture8_Release(texture);
589 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
590 return D3DERR_DEVICELOST;
593 return D3D_OK;
596 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
598 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
599 return D3DERR_DEVICELOST;
602 surface = wined3d_resource_get_parent(resource);
603 if (surface->resource.refcount)
605 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
606 return D3DERR_DEVICELOST;
609 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
612 return D3D_OK;
615 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
616 D3DPRESENT_PARAMETERS *present_parameters)
618 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
619 struct wined3d_swapchain_desc swapchain_desc;
620 HRESULT hr;
622 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
624 wined3d_mutex_lock();
626 if (device->vertex_buffer)
628 wined3d_buffer_decref(device->vertex_buffer);
629 device->vertex_buffer = NULL;
630 device->vertex_buffer_size = 0;
632 if (device->index_buffer)
634 wined3d_buffer_decref(device->index_buffer);
635 device->index_buffer = NULL;
636 device->index_buffer_size = 0;
639 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
640 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
641 NULL, reset_enum_callback, TRUE)))
643 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
644 device->device_state = D3D8_DEVICE_STATE_OK;
646 else
648 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
650 wined3d_mutex_unlock();
652 return hr;
655 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
656 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
658 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
659 HRESULT hr;
661 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
662 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
664 if (device->device_state != D3D8_DEVICE_STATE_OK)
665 return D3DERR_DEVICELOST;
667 wined3d_mutex_lock();
668 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
669 dst_window_override, dirty_region, 0);
670 wined3d_mutex_unlock();
672 return hr;
675 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
676 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
678 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
679 struct wined3d_surface *wined3d_surface = NULL;
680 struct d3d8_surface *surface_impl;
681 HRESULT hr;
683 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
684 iface, backbuffer_idx, backbuffer_type, backbuffer);
686 wined3d_mutex_lock();
687 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
688 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
689 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
691 surface_impl = wined3d_surface_get_parent(wined3d_surface);
692 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
693 IDirect3DSurface8_AddRef(*backbuffer);
695 wined3d_mutex_unlock();
697 return hr;
700 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
702 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
703 HRESULT hr;
705 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
707 wined3d_mutex_lock();
708 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
709 wined3d_mutex_unlock();
711 return hr;
714 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
718 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
720 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
721 wined3d_mutex_lock();
722 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
723 wined3d_mutex_unlock();
726 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
728 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
730 TRACE("iface %p, ramp %p.\n", iface, ramp);
732 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
733 wined3d_mutex_lock();
734 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
735 wined3d_mutex_unlock();
738 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
739 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
740 D3DPOOL pool, IDirect3DTexture8 **texture)
742 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
743 struct d3d8_texture *object;
744 HRESULT hr;
746 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
747 iface, width, height, levels, usage, format, pool, texture);
749 *texture = NULL;
750 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
751 if (!object)
752 return D3DERR_OUTOFVIDEOMEMORY;
754 hr = texture_init(object, device, width, height, levels, usage, format, pool);
755 if (FAILED(hr))
757 WARN("Failed to initialize texture, hr %#x.\n", hr);
758 HeapFree(GetProcessHeap(), 0, object);
759 return hr;
762 TRACE("Created texture %p.\n", object);
763 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
765 return D3D_OK;
768 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
769 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
770 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
772 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
773 struct d3d8_texture *object;
774 HRESULT hr;
776 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
777 iface, width, height, depth, levels, usage, format, pool, texture);
779 *texture = NULL;
780 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
781 if (!object)
782 return D3DERR_OUTOFVIDEOMEMORY;
784 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
785 if (FAILED(hr))
787 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
788 HeapFree(GetProcessHeap(), 0, object);
789 return hr;
792 TRACE("Created volume texture %p.\n", object);
793 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
795 return D3D_OK;
798 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
799 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
801 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
802 struct d3d8_texture *object;
803 HRESULT hr;
805 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
806 iface, edge_length, levels, usage, format, pool, texture);
808 *texture = NULL;
809 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
810 if (!object)
811 return D3DERR_OUTOFVIDEOMEMORY;
813 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
814 if (FAILED(hr))
816 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
817 HeapFree(GetProcessHeap(), 0, object);
818 return hr;
821 TRACE("Created cube texture %p.\n", object);
822 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
824 return hr;
827 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
828 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
830 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
831 struct d3d8_vertexbuffer *object;
832 HRESULT hr;
834 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
835 iface, size, usage, fvf, pool, buffer);
837 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
838 if (!object)
839 return D3DERR_OUTOFVIDEOMEMORY;
841 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
842 if (FAILED(hr))
844 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
845 HeapFree(GetProcessHeap(), 0, object);
846 return hr;
849 TRACE("Created vertex buffer %p.\n", object);
850 *buffer = &object->IDirect3DVertexBuffer8_iface;
852 return D3D_OK;
855 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
856 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
858 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
859 struct d3d8_indexbuffer *object;
860 HRESULT hr;
862 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
863 iface, size, usage, format, pool, buffer);
865 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
866 if (!object)
867 return D3DERR_OUTOFVIDEOMEMORY;
869 hr = indexbuffer_init(object, device, size, usage, format, pool);
870 if (FAILED(hr))
872 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
873 HeapFree(GetProcessHeap(), 0, object);
874 return hr;
877 TRACE("Created index buffer %p.\n", object);
878 *buffer = &object->IDirect3DIndexBuffer8_iface;
880 return D3D_OK;
883 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
884 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
885 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
887 struct wined3d_resource *sub_resource;
888 struct wined3d_resource_desc desc;
889 struct d3d8_surface *surface_impl;
890 struct wined3d_texture *texture;
891 HRESULT hr;
893 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
894 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
895 device, width, height, format, flags, surface,
896 usage, pool, multisample_type, multisample_quality);
898 desc.resource_type = WINED3D_RTYPE_TEXTURE;
899 desc.format = wined3dformat_from_d3dformat(format);
900 desc.multisample_type = multisample_type;
901 desc.multisample_quality = multisample_quality;
902 desc.usage = usage & WINED3DUSAGE_MASK;
903 desc.pool = pool;
904 desc.width = width;
905 desc.height = height;
906 desc.depth = 1;
907 desc.size = 0;
909 wined3d_mutex_lock();
911 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
912 1, flags, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
914 wined3d_mutex_unlock();
915 WARN("Failed to create texture, hr %#x.\n", hr);
916 return hr;
919 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
920 surface_impl = wined3d_resource_get_parent(sub_resource);
921 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
922 *surface = &surface_impl->IDirect3DSurface8_iface;
923 IDirect3DSurface8_AddRef(*surface);
924 wined3d_texture_decref(texture);
926 wined3d_mutex_unlock();
928 return D3D_OK;
931 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
932 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
933 IDirect3DSurface8 **surface)
935 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
936 DWORD flags = 0;
938 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
939 iface, width, height, format, multisample_type, lockable, surface);
941 *surface = NULL;
942 if (lockable)
943 flags |= WINED3D_SURFACE_MAPPABLE;
945 return d3d8_device_create_surface(device, width, height, format, flags, surface,
946 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
949 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
950 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
951 IDirect3DSurface8 **surface)
953 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
955 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
956 iface, width, height, format, multisample_type, surface);
958 *surface = NULL;
960 /* TODO: Verify that Discard is false */
961 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
962 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
965 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
966 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
967 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
969 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
971 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
972 iface, width, height, format, surface);
974 *surface = NULL;
976 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
977 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
980 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
981 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
982 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
984 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
985 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
986 enum wined3d_format_id src_format, dst_format;
987 struct wined3d_resource_desc wined3d_desc;
988 struct wined3d_resource *wined3d_resource;
989 UINT src_w, src_h;
990 HRESULT hr;
992 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
993 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
995 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
996 * destination texture is in WINED3D_POOL_DEFAULT. */
998 wined3d_mutex_lock();
999 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1000 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1001 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1003 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1004 wined3d_mutex_unlock();
1005 return D3DERR_INVALIDCALL;
1007 src_format = wined3d_desc.format;
1008 src_w = wined3d_desc.width;
1009 src_h = wined3d_desc.height;
1011 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1012 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1013 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1015 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1016 wined3d_mutex_unlock();
1017 return D3DERR_INVALIDCALL;
1019 dst_format = wined3d_desc.format;
1021 /* Check that the source and destination formats match */
1022 if (src_format != dst_format && WINED3DFMT_UNKNOWN != dst_format)
1024 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1025 src_surface, dst_surface);
1026 wined3d_mutex_unlock();
1027 return D3DERR_INVALIDCALL;
1029 else if (WINED3DFMT_UNKNOWN == dst_format)
1031 TRACE("Converting destination surface from WINED3DFMT_UNKNOWN to the source format.\n");
1032 if (FAILED(hr = wined3d_surface_update_desc(dst->wined3d_surface, wined3d_desc.width, wined3d_desc.height,
1033 src_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality, NULL, 0)))
1035 WARN("Failed to update surface desc, hr %#x.\n", hr);
1036 wined3d_mutex_unlock();
1037 return hr;
1041 /* Quick if complete copy ... */
1042 if (!rect_count && !src_rects && !dst_points)
1044 RECT rect = {0, 0, src_w, src_h};
1045 wined3d_surface_blt(dst->wined3d_surface, &rect,
1046 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
1048 else
1050 unsigned int i;
1051 /* Copy rect by rect */
1052 if (src_rects && dst_points)
1054 for (i = 0; i < rect_count; ++i)
1056 UINT w = src_rects[i].right - src_rects[i].left;
1057 UINT h = src_rects[i].bottom - src_rects[i].top;
1058 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1059 dst_points[i].x + w, dst_points[i].y + h};
1061 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1062 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1065 else
1067 for (i = 0; i < rect_count; ++i)
1069 UINT w = src_rects[i].right - src_rects[i].left;
1070 UINT h = src_rects[i].bottom - src_rects[i].top;
1071 RECT dst_rect = {0, 0, w, h};
1073 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1074 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1078 wined3d_mutex_unlock();
1080 return WINED3D_OK;
1083 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1084 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1086 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1087 struct d3d8_texture *src_impl, *dst_impl;
1088 HRESULT hr;
1090 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1092 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1093 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1095 wined3d_mutex_lock();
1096 hr = wined3d_device_update_texture(device->wined3d_device,
1097 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1098 wined3d_mutex_unlock();
1100 return hr;
1103 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1105 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1106 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1107 HRESULT hr;
1109 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1111 if (!dst_surface)
1113 WARN("Invalid destination surface passed.\n");
1114 return D3DERR_INVALIDCALL;
1117 wined3d_mutex_lock();
1118 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1119 wined3d_mutex_unlock();
1121 return hr;
1124 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1125 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1127 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1128 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1129 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1130 struct wined3d_rendertarget_view *original_dsv;
1131 HRESULT hr = D3D_OK;
1133 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1135 wined3d_mutex_lock();
1137 if (ds_impl)
1139 struct wined3d_rendertarget_view *original_rtv;
1140 struct wined3d_resource_desc ds_desc, rt_desc;
1141 struct wined3d_resource *wined3d_resource;
1142 struct d3d8_surface *original_surface;
1144 /* If no render target is passed in check the size against the current RT */
1145 if (!render_target)
1148 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1150 wined3d_mutex_unlock();
1151 return D3DERR_NOTFOUND;
1153 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1154 wined3d_resource = wined3d_surface_get_resource(original_surface->wined3d_surface);
1156 else
1157 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1158 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1160 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1161 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1163 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1165 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1166 wined3d_mutex_unlock();
1167 return D3DERR_INVALIDCALL;
1171 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1172 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1173 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1174 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1175 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1176 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1178 wined3d_mutex_unlock();
1180 return hr;
1183 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1185 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1186 struct wined3d_rendertarget_view *wined3d_rtv;
1187 struct d3d8_surface *surface_impl;
1188 HRESULT hr;
1190 TRACE("iface %p, render_target %p.\n", iface, render_target);
1192 if (!render_target)
1193 return D3DERR_INVALIDCALL;
1195 wined3d_mutex_lock();
1196 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1198 /* We want the sub resource parent here, since the view itself may be
1199 * internal to wined3d and may not have a parent. */
1200 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1201 *render_target = &surface_impl->IDirect3DSurface8_iface;
1202 IDirect3DSurface8_AddRef(*render_target);
1203 hr = D3D_OK;
1205 else
1207 ERR("Failed to get wined3d render target.\n");
1208 *render_target = NULL;
1209 hr = D3DERR_NOTFOUND;
1211 wined3d_mutex_unlock();
1213 return hr;
1216 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1218 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1219 struct wined3d_rendertarget_view *wined3d_dsv;
1220 struct d3d8_surface *surface_impl;
1221 HRESULT hr = D3D_OK;
1223 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1225 if (!depth_stencil)
1226 return D3DERR_INVALIDCALL;
1228 wined3d_mutex_lock();
1229 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1231 /* We want the sub resource parent here, since the view itself may be
1232 * internal to wined3d and may not have a parent. */
1233 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1234 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1235 IDirect3DSurface8_AddRef(*depth_stencil);
1237 else
1239 hr = WINED3DERR_NOTFOUND;
1240 *depth_stencil = NULL;
1242 wined3d_mutex_unlock();
1244 return hr;
1247 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1249 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1250 HRESULT hr;
1252 TRACE("iface %p.\n", iface);
1254 wined3d_mutex_lock();
1255 hr = wined3d_device_begin_scene(device->wined3d_device);
1256 wined3d_mutex_unlock();
1258 return hr;
1261 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1263 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1264 HRESULT hr;
1266 TRACE("iface %p.\n", iface);
1268 wined3d_mutex_lock();
1269 hr = wined3d_device_end_scene(device->wined3d_device);
1270 wined3d_mutex_unlock();
1272 return hr;
1275 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1276 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1278 const struct wined3d_color c =
1280 ((color >> 16) & 0xff) / 255.0f,
1281 ((color >> 8) & 0xff) / 255.0f,
1282 (color & 0xff) / 255.0f,
1283 ((color >> 24) & 0xff) / 255.0f,
1285 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1286 HRESULT hr;
1288 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1289 iface, rect_count, rects, flags, color, z, stencil);
1291 wined3d_mutex_lock();
1292 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1293 wined3d_mutex_unlock();
1295 return hr;
1298 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1299 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1301 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1303 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1305 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1306 wined3d_mutex_lock();
1307 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1308 wined3d_mutex_unlock();
1310 return D3D_OK;
1313 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1314 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1316 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1318 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1320 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1321 wined3d_mutex_lock();
1322 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1323 wined3d_mutex_unlock();
1325 return D3D_OK;
1328 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1329 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1331 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1333 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1335 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1336 wined3d_mutex_lock();
1337 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1338 wined3d_mutex_unlock();
1340 return D3D_OK;
1343 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1345 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1347 TRACE("iface %p, viewport %p.\n", iface, viewport);
1349 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1350 wined3d_mutex_lock();
1351 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1352 wined3d_mutex_unlock();
1354 return D3D_OK;
1357 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1359 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1361 TRACE("iface %p, viewport %p.\n", iface, viewport);
1363 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1364 wined3d_mutex_lock();
1365 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1366 wined3d_mutex_unlock();
1368 return D3D_OK;
1371 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1373 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1375 TRACE("iface %p, material %p.\n", iface, material);
1377 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1378 wined3d_mutex_lock();
1379 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1380 wined3d_mutex_unlock();
1382 return D3D_OK;
1385 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1387 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1389 TRACE("iface %p, material %p.\n", iface, material);
1391 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1392 wined3d_mutex_lock();
1393 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1394 wined3d_mutex_unlock();
1396 return D3D_OK;
1399 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1401 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1402 HRESULT hr;
1404 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1406 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1407 wined3d_mutex_lock();
1408 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1409 wined3d_mutex_unlock();
1411 return hr;
1414 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1417 HRESULT hr;
1419 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1421 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1422 wined3d_mutex_lock();
1423 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1424 wined3d_mutex_unlock();
1426 return hr;
1429 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1431 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1432 HRESULT hr;
1434 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1436 wined3d_mutex_lock();
1437 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1438 wined3d_mutex_unlock();
1440 return hr;
1443 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1445 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1446 HRESULT hr;
1448 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1450 wined3d_mutex_lock();
1451 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1452 wined3d_mutex_unlock();
1454 return hr;
1457 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1459 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1460 HRESULT hr;
1462 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1464 wined3d_mutex_lock();
1465 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1466 wined3d_mutex_unlock();
1468 return hr;
1471 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1473 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1474 HRESULT hr;
1476 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1478 wined3d_mutex_lock();
1479 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1480 wined3d_mutex_unlock();
1482 return hr;
1485 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1486 D3DRENDERSTATETYPE state, DWORD value)
1488 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1490 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1492 wined3d_mutex_lock();
1493 switch (state)
1495 case D3DRS_ZBIAS:
1496 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1497 break;
1499 default:
1500 wined3d_device_set_render_state(device->wined3d_device, state, value);
1502 wined3d_mutex_unlock();
1504 return D3D_OK;
1507 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1508 D3DRENDERSTATETYPE state, DWORD *value)
1510 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1512 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1514 wined3d_mutex_lock();
1515 switch (state)
1517 case D3DRS_ZBIAS:
1518 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1519 break;
1521 default:
1522 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1524 wined3d_mutex_unlock();
1526 return D3D_OK;
1529 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1531 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1532 HRESULT hr;
1534 TRACE("iface %p.\n", iface);
1536 wined3d_mutex_lock();
1537 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1538 wined3d_mutex_unlock();
1540 return hr;
1543 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1545 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1546 struct wined3d_stateblock *stateblock;
1547 HRESULT hr;
1549 TRACE("iface %p, token %p.\n", iface, token);
1551 /* Tell wineD3D to endstateblock before anything else (in case we run out
1552 * of memory later and cause locking problems)
1554 wined3d_mutex_lock();
1555 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1556 if (FAILED(hr))
1558 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1559 wined3d_mutex_unlock();
1560 return hr;
1563 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1564 wined3d_mutex_unlock();
1566 if (*token == D3D8_INVALID_HANDLE)
1568 ERR("Failed to create a handle\n");
1569 wined3d_mutex_lock();
1570 wined3d_stateblock_decref(stateblock);
1571 wined3d_mutex_unlock();
1572 return E_FAIL;
1574 ++*token;
1576 TRACE("Returning %#x (%p).\n", *token, stateblock);
1578 return hr;
1581 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1583 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1584 struct wined3d_stateblock *stateblock;
1586 TRACE("iface %p, token %#x.\n", iface, token);
1588 if (!token)
1589 return D3D_OK;
1591 wined3d_mutex_lock();
1592 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1593 if (!stateblock)
1595 WARN("Invalid handle (%#x) passed.\n", token);
1596 wined3d_mutex_unlock();
1597 return D3DERR_INVALIDCALL;
1599 wined3d_stateblock_apply(stateblock);
1600 wined3d_mutex_unlock();
1602 return D3D_OK;
1605 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1607 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1608 struct wined3d_stateblock *stateblock;
1610 TRACE("iface %p, token %#x.\n", iface, token);
1612 wined3d_mutex_lock();
1613 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1614 if (!stateblock)
1616 WARN("Invalid handle (%#x) passed.\n", token);
1617 wined3d_mutex_unlock();
1618 return D3DERR_INVALIDCALL;
1620 wined3d_stateblock_capture(stateblock);
1621 wined3d_mutex_unlock();
1623 return D3D_OK;
1626 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1628 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1629 struct wined3d_stateblock *stateblock;
1631 TRACE("iface %p, token %#x.\n", iface, token);
1633 wined3d_mutex_lock();
1634 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1636 if (!stateblock)
1638 WARN("Invalid handle (%#x) passed.\n", token);
1639 wined3d_mutex_unlock();
1640 return D3DERR_INVALIDCALL;
1643 if (wined3d_stateblock_decref(stateblock))
1645 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1647 wined3d_mutex_unlock();
1649 return D3D_OK;
1652 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1653 D3DSTATEBLOCKTYPE type, DWORD *handle)
1655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1656 struct wined3d_stateblock *stateblock;
1657 HRESULT hr;
1659 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1661 if (type != D3DSBT_ALL
1662 && type != D3DSBT_PIXELSTATE
1663 && type != D3DSBT_VERTEXSTATE)
1665 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1666 return D3DERR_INVALIDCALL;
1669 wined3d_mutex_lock();
1670 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1671 if (FAILED(hr))
1673 wined3d_mutex_unlock();
1674 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1675 return hr;
1678 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1679 wined3d_mutex_unlock();
1681 if (*handle == D3D8_INVALID_HANDLE)
1683 ERR("Failed to allocate a handle.\n");
1684 wined3d_mutex_lock();
1685 wined3d_stateblock_decref(stateblock);
1686 wined3d_mutex_unlock();
1687 return E_FAIL;
1689 ++*handle;
1691 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1693 return hr;
1696 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1698 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1699 HRESULT hr;
1701 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1702 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1704 wined3d_mutex_lock();
1705 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1706 wined3d_mutex_unlock();
1708 return hr;
1711 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1714 HRESULT hr;
1716 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1718 wined3d_mutex_lock();
1719 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1720 wined3d_mutex_unlock();
1722 return hr;
1725 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1727 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1728 struct wined3d_texture *wined3d_texture;
1729 struct d3d8_texture *texture_impl;
1731 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1733 if (!texture)
1734 return D3DERR_INVALIDCALL;
1736 wined3d_mutex_lock();
1737 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1739 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1740 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1741 IDirect3DBaseTexture8_AddRef(*texture);
1743 else
1745 *texture = NULL;
1747 wined3d_mutex_unlock();
1749 return D3D_OK;
1752 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1754 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1755 struct d3d8_texture *texture_impl;
1756 HRESULT hr;
1758 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1760 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1762 wined3d_mutex_lock();
1763 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1764 texture_impl ? texture_impl->wined3d_texture : NULL);
1765 wined3d_mutex_unlock();
1767 return hr;
1770 static const struct tss_lookup
1772 BOOL sampler_state;
1773 enum wined3d_texture_stage_state state;
1775 tss_lookup[] =
1777 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1778 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1779 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1780 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1781 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1782 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1783 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1784 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1785 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1786 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1787 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1788 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1789 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1790 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1791 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1792 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1793 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1794 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1795 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1796 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1797 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1798 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1799 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1800 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1801 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1802 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1803 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1804 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1805 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1808 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1809 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1811 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1812 const struct tss_lookup *l;
1814 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1816 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1818 WARN("Invalid Type %#x passed.\n", Type);
1819 return D3D_OK;
1822 l = &tss_lookup[Type];
1824 wined3d_mutex_lock();
1825 if (l->sampler_state)
1826 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1827 else
1828 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1829 wined3d_mutex_unlock();
1831 return D3D_OK;
1834 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1835 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1837 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1838 const struct tss_lookup *l;
1840 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1842 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1844 WARN("Invalid type %#x passed.\n", type);
1845 return D3D_OK;
1848 l = &tss_lookup[type];
1850 wined3d_mutex_lock();
1851 if (l->sampler_state)
1852 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1853 else
1854 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1855 wined3d_mutex_unlock();
1857 return D3D_OK;
1860 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1862 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1863 HRESULT hr;
1865 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1867 wined3d_mutex_lock();
1868 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1869 wined3d_mutex_unlock();
1871 return hr;
1874 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1875 DWORD info_id, void *info, DWORD info_size)
1877 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1879 return D3D_OK;
1882 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1883 UINT palette_idx, const PALETTEENTRY *entries)
1885 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1887 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1888 * does not have a d3d8/9-style palette API */
1890 return D3D_OK;
1893 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1894 UINT palette_idx, PALETTEENTRY *entries)
1896 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1898 return D3DERR_INVALIDCALL;
1901 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1903 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1905 return D3D_OK;
1908 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1910 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1912 return D3DERR_INVALIDCALL;
1915 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1916 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1918 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1919 HRESULT hr;
1921 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1922 iface, primitive_type, start_vertex, primitive_count);
1924 wined3d_mutex_lock();
1925 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1926 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1927 vertex_count_from_primitive_count(primitive_type, primitive_count));
1928 wined3d_mutex_unlock();
1930 return hr;
1933 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1934 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1935 UINT start_idx, UINT primitive_count)
1937 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1938 HRESULT hr;
1940 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1941 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1943 wined3d_mutex_lock();
1944 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1945 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1946 vertex_count_from_primitive_count(primitive_type, primitive_count));
1947 wined3d_mutex_unlock();
1949 return hr;
1952 /* The caller is responsible for wined3d locking */
1953 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1955 HRESULT hr;
1957 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1959 UINT size = max(device->vertex_buffer_size * 2, min_size);
1960 struct wined3d_buffer *buffer;
1962 TRACE("Growing vertex buffer to %u bytes\n", size);
1964 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1965 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1966 if (FAILED(hr))
1968 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1969 return hr;
1972 if (device->vertex_buffer)
1973 wined3d_buffer_decref(device->vertex_buffer);
1975 device->vertex_buffer = buffer;
1976 device->vertex_buffer_size = size;
1977 device->vertex_buffer_pos = 0;
1979 return D3D_OK;
1982 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1983 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
1984 UINT stride)
1986 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1987 HRESULT hr;
1988 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
1989 UINT size = vtx_count * stride;
1990 UINT vb_pos, align;
1991 BYTE *buffer_data;
1993 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1994 iface, primitive_type, primitive_count, data, stride);
1996 if (!primitive_count)
1998 WARN("primitive_count is 0, returning D3D_OK\n");
1999 return D3D_OK;
2002 wined3d_mutex_lock();
2003 hr = d3d8_device_prepare_vertex_buffer(device, size);
2004 if (FAILED(hr))
2005 goto done;
2007 vb_pos = device->vertex_buffer_pos;
2008 align = vb_pos % stride;
2009 if (align) align = stride - align;
2010 if (vb_pos + size + align > device->vertex_buffer_size)
2011 vb_pos = 0;
2012 else
2013 vb_pos += align;
2015 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2016 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2017 if (FAILED(hr))
2018 goto done;
2019 memcpy(buffer_data, data, size);
2020 wined3d_buffer_unmap(device->vertex_buffer);
2021 device->vertex_buffer_pos = vb_pos + size;
2023 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2024 if (FAILED(hr))
2025 goto done;
2027 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2028 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2029 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2031 done:
2032 wined3d_mutex_unlock();
2033 return hr;
2036 /* The caller is responsible for wined3d locking */
2037 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2039 HRESULT hr;
2041 if (device->index_buffer_size < min_size || !device->index_buffer)
2043 UINT size = max(device->index_buffer_size * 2, min_size);
2044 struct wined3d_buffer *buffer;
2046 TRACE("Growing index buffer to %u bytes\n", size);
2048 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2049 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2050 if (FAILED(hr))
2052 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2053 return hr;
2056 if (device->index_buffer)
2057 wined3d_buffer_decref(device->index_buffer);
2059 device->index_buffer = buffer;
2060 device->index_buffer_size = size;
2061 device->index_buffer_pos = 0;
2063 return D3D_OK;
2066 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2067 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2068 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2069 const void *vertex_data, UINT vertex_stride)
2071 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2072 HRESULT hr;
2073 BYTE *buffer_data;
2075 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2076 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2077 UINT idx_size = idx_count * idx_fmt_size;
2078 UINT ib_pos;
2080 UINT vtx_size = vertex_count * vertex_stride;
2081 UINT vb_pos, align;
2083 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2084 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2085 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2086 index_data, index_format, vertex_data, vertex_stride);
2088 if (!primitive_count)
2090 WARN("primitive_count is 0, returning D3D_OK\n");
2091 return D3D_OK;
2094 wined3d_mutex_lock();
2096 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2097 if (FAILED(hr))
2098 goto done;
2100 vb_pos = device->vertex_buffer_pos;
2101 align = vb_pos % vertex_stride;
2102 if (align) align = vertex_stride - align;
2103 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2104 vb_pos = 0;
2105 else
2106 vb_pos += align;
2108 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2109 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2110 if (FAILED(hr))
2111 goto done;
2112 memcpy(buffer_data, vertex_data, vtx_size);
2113 wined3d_buffer_unmap(device->vertex_buffer);
2114 device->vertex_buffer_pos = vb_pos + vtx_size;
2116 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2117 if (FAILED(hr))
2118 goto done;
2120 ib_pos = device->index_buffer_pos;
2121 align = ib_pos % idx_fmt_size;
2122 if (align) align = idx_fmt_size - align;
2123 if (ib_pos + idx_size + align > device->index_buffer_size)
2124 ib_pos = 0;
2125 else
2126 ib_pos += align;
2128 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2129 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2130 if (FAILED(hr))
2131 goto done;
2132 memcpy(buffer_data, index_data, idx_size);
2133 wined3d_buffer_unmap(device->index_buffer);
2134 device->index_buffer_pos = ib_pos + idx_size;
2136 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2137 if (FAILED(hr))
2138 goto done;
2140 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2141 wined3dformat_from_d3dformat(index_format));
2142 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2144 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2145 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2147 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2148 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2149 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2151 done:
2152 wined3d_mutex_unlock();
2153 return hr;
2156 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2157 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2159 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2160 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2161 HRESULT hr;
2163 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2164 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2166 wined3d_mutex_lock();
2167 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2168 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2169 wined3d_mutex_unlock();
2171 return hr;
2174 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2175 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2177 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2178 struct d3d8_vertex_shader *object;
2179 DWORD shader_handle;
2180 DWORD handle;
2181 HRESULT hr;
2183 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2184 iface, declaration, byte_code, shader, usage);
2186 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2187 if (!object)
2189 *shader = 0;
2190 return E_OUTOFMEMORY;
2193 wined3d_mutex_lock();
2194 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2195 wined3d_mutex_unlock();
2196 if (handle == D3D8_INVALID_HANDLE)
2198 ERR("Failed to allocate vertex shader handle.\n");
2199 HeapFree(GetProcessHeap(), 0, object);
2200 *shader = 0;
2201 return E_OUTOFMEMORY;
2204 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2206 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2207 if (FAILED(hr))
2209 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2210 wined3d_mutex_lock();
2211 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2212 wined3d_mutex_unlock();
2213 HeapFree(GetProcessHeap(), 0, object);
2214 *shader = 0;
2215 return hr;
2218 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2219 *shader = shader_handle;
2221 return D3D_OK;
2224 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2226 struct d3d8_vertex_declaration *d3d8_declaration;
2227 struct FvfToDecl *convertedDecls = device->decls;
2228 int p, low, high; /* deliberately signed */
2229 HRESULT hr;
2231 TRACE("Searching for declaration for fvf %08x... ", fvf);
2233 low = 0;
2234 high = device->numConvertedDecls - 1;
2235 while (low <= high)
2237 p = (low + high) >> 1;
2238 TRACE("%d ", p);
2240 if (convertedDecls[p].fvf == fvf)
2242 TRACE("found %p\n", convertedDecls[p].declaration);
2243 return convertedDecls[p].declaration;
2246 if (convertedDecls[p].fvf < fvf)
2247 low = p + 1;
2248 else
2249 high = p - 1;
2251 TRACE("not found. Creating and inserting at position %d.\n", low);
2253 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2254 return NULL;
2256 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2258 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2259 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2260 return NULL;
2263 if (device->declArraySize == device->numConvertedDecls)
2265 UINT grow = device->declArraySize / 2;
2267 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2268 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2269 if (!convertedDecls)
2271 d3d8_vertex_declaration_destroy(d3d8_declaration);
2272 return NULL;
2274 device->decls = convertedDecls;
2275 device->declArraySize += grow;
2278 memmove(convertedDecls + low + 1, convertedDecls + low,
2279 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2280 convertedDecls[low].declaration = d3d8_declaration;
2281 convertedDecls[low].fvf = fvf;
2282 ++device->numConvertedDecls;
2284 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2286 return d3d8_declaration;
2289 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2291 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2292 struct d3d8_vertex_shader *shader_impl;
2294 TRACE("iface %p, shader %#x.\n", iface, shader);
2296 if (VS_HIGHESTFIXEDFXF >= shader)
2298 TRACE("Setting FVF, %#x\n", shader);
2300 wined3d_mutex_lock();
2301 wined3d_device_set_vertex_declaration(device->wined3d_device,
2302 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2303 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2304 wined3d_mutex_unlock();
2306 return D3D_OK;
2309 TRACE("Setting shader\n");
2311 wined3d_mutex_lock();
2312 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2314 WARN("Invalid handle (%#x) passed.\n", shader);
2315 wined3d_mutex_unlock();
2317 return D3DERR_INVALIDCALL;
2320 wined3d_device_set_vertex_declaration(device->wined3d_device,
2321 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2322 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2323 wined3d_mutex_unlock();
2325 return D3D_OK;
2328 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2330 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2331 struct wined3d_vertex_declaration *wined3d_declaration;
2332 struct d3d8_vertex_declaration *d3d8_declaration;
2334 TRACE("iface %p, shader %p.\n", iface, shader);
2336 wined3d_mutex_lock();
2337 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2339 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2340 *shader = d3d8_declaration->shader_handle;
2342 else
2344 *shader = 0;
2346 wined3d_mutex_unlock();
2348 TRACE("Returning %#x.\n", *shader);
2350 return D3D_OK;
2353 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2355 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2356 struct d3d8_vertex_shader *shader_impl;
2358 TRACE("iface %p, shader %#x.\n", iface, shader);
2360 wined3d_mutex_lock();
2361 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2363 WARN("Invalid handle (%#x) passed.\n", shader);
2364 wined3d_mutex_unlock();
2366 return D3DERR_INVALIDCALL;
2369 if (shader_impl->wined3d_shader
2370 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2371 IDirect3DDevice8_SetVertexShader(iface, 0);
2373 wined3d_mutex_unlock();
2375 d3d8_vertex_shader_destroy(shader_impl);
2377 return D3D_OK;
2380 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2381 DWORD start_register, const void *data, DWORD count)
2383 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2384 HRESULT hr;
2386 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2387 iface, start_register, data, count);
2389 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2391 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2392 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2393 return D3DERR_INVALIDCALL;
2396 wined3d_mutex_lock();
2397 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2398 wined3d_mutex_unlock();
2400 return hr;
2403 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2404 DWORD start_register, void *data, DWORD count)
2406 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2407 HRESULT hr;
2409 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2410 iface, start_register, data, count);
2412 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2414 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2415 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2416 return D3DERR_INVALIDCALL;
2419 wined3d_mutex_lock();
2420 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2421 wined3d_mutex_unlock();
2423 return hr;
2426 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2427 DWORD shader, void *data, DWORD *data_size)
2429 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2430 struct d3d8_vertex_declaration *declaration;
2431 struct d3d8_vertex_shader *shader_impl;
2433 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2434 iface, shader, data, data_size);
2436 wined3d_mutex_lock();
2437 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2438 wined3d_mutex_unlock();
2440 if (!shader_impl)
2442 WARN("Invalid handle (%#x) passed.\n", shader);
2443 return D3DERR_INVALIDCALL;
2445 declaration = shader_impl->vertex_declaration;
2447 if (!data)
2449 *data_size = declaration->elements_size;
2450 return D3D_OK;
2453 /* MSDN claims that if *data_size is smaller than the required size
2454 * we should write the required size and return D3DERR_MOREDATA.
2455 * That's not actually true. */
2456 if (*data_size < declaration->elements_size)
2457 return D3DERR_INVALIDCALL;
2459 memcpy(data, declaration->elements, declaration->elements_size);
2461 return D3D_OK;
2464 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2465 DWORD shader, void *data, DWORD *data_size)
2467 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2468 struct d3d8_vertex_shader *shader_impl = NULL;
2469 HRESULT hr;
2471 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2472 iface, shader, data, data_size);
2474 wined3d_mutex_lock();
2475 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2477 WARN("Invalid handle (%#x) passed.\n", shader);
2478 wined3d_mutex_unlock();
2480 return D3DERR_INVALIDCALL;
2483 if (!shader_impl->wined3d_shader)
2485 wined3d_mutex_unlock();
2486 *data_size = 0;
2487 return D3D_OK;
2490 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2491 wined3d_mutex_unlock();
2493 return hr;
2496 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2497 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2499 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2500 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2502 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2504 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2505 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2506 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2507 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2508 * problem)
2510 wined3d_mutex_lock();
2511 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2512 wined3d_device_set_index_buffer(device->wined3d_device,
2513 ib ? ib->wined3d_buffer : NULL,
2514 ib ? ib->format : WINED3DFMT_UNKNOWN);
2515 wined3d_mutex_unlock();
2517 return D3D_OK;
2520 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2521 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2523 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2524 enum wined3d_format_id wined3d_format;
2525 struct wined3d_buffer *wined3d_buffer;
2526 struct d3d8_indexbuffer *buffer_impl;
2528 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2530 if (!buffer)
2531 return D3DERR_INVALIDCALL;
2533 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2534 wined3d_mutex_lock();
2535 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2536 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2538 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2539 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2540 IDirect3DIndexBuffer8_AddRef(*buffer);
2542 else
2544 *buffer = NULL;
2546 wined3d_mutex_unlock();
2548 return D3D_OK;
2551 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2552 const DWORD *byte_code, DWORD *shader)
2554 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2555 struct d3d8_pixel_shader *object;
2556 DWORD shader_handle;
2557 DWORD handle;
2558 HRESULT hr;
2560 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2562 if (!shader)
2563 return D3DERR_INVALIDCALL;
2565 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2566 if (!object)
2567 return E_OUTOFMEMORY;
2569 wined3d_mutex_lock();
2570 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2571 wined3d_mutex_unlock();
2572 if (handle == D3D8_INVALID_HANDLE)
2574 ERR("Failed to allocate pixel shader handle.\n");
2575 HeapFree(GetProcessHeap(), 0, object);
2576 return E_OUTOFMEMORY;
2579 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2581 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2582 if (FAILED(hr))
2584 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2585 wined3d_mutex_lock();
2586 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2587 wined3d_mutex_unlock();
2588 HeapFree(GetProcessHeap(), 0, object);
2589 *shader = 0;
2590 return hr;
2593 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2594 *shader = shader_handle;
2596 return D3D_OK;
2599 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2601 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2602 struct d3d8_pixel_shader *shader_impl;
2604 TRACE("iface %p, shader %#x.\n", iface, shader);
2606 wined3d_mutex_lock();
2608 if (!shader)
2610 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2611 wined3d_mutex_unlock();
2612 return D3D_OK;
2615 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2617 WARN("Invalid handle (%#x) passed.\n", shader);
2618 wined3d_mutex_unlock();
2619 return D3DERR_INVALIDCALL;
2622 TRACE("Setting shader %p.\n", shader_impl);
2623 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2624 wined3d_mutex_unlock();
2626 return D3D_OK;
2629 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2631 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2632 struct wined3d_shader *object;
2634 TRACE("iface %p, shader %p.\n", iface, shader);
2636 if (!shader)
2637 return D3DERR_INVALIDCALL;
2639 wined3d_mutex_lock();
2640 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2642 struct d3d8_pixel_shader *d3d8_shader;
2643 d3d8_shader = wined3d_shader_get_parent(object);
2644 *shader = d3d8_shader->handle;
2646 else
2648 *shader = 0;
2650 wined3d_mutex_unlock();
2652 TRACE("Returning %#x.\n", *shader);
2654 return D3D_OK;
2657 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2660 struct d3d8_pixel_shader *shader_impl;
2662 TRACE("iface %p, shader %#x.\n", iface, shader);
2664 wined3d_mutex_lock();
2666 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2668 WARN("Invalid handle (%#x) passed.\n", shader);
2669 wined3d_mutex_unlock();
2670 return D3DERR_INVALIDCALL;
2673 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2674 IDirect3DDevice8_SetPixelShader(iface, 0);
2676 wined3d_mutex_unlock();
2678 d3d8_pixel_shader_destroy(shader_impl);
2680 return D3D_OK;
2683 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2684 DWORD start_register, const void *data, DWORD count)
2686 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2687 HRESULT hr;
2689 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2690 iface, start_register, data, count);
2692 wined3d_mutex_lock();
2693 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2694 wined3d_mutex_unlock();
2696 return hr;
2699 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2700 DWORD start_register, void *data, DWORD count)
2702 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2703 HRESULT hr;
2705 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2706 iface, start_register, data, count);
2708 wined3d_mutex_lock();
2709 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2710 wined3d_mutex_unlock();
2712 return hr;
2715 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2716 DWORD shader, void *data, DWORD *data_size)
2718 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2719 struct d3d8_pixel_shader *shader_impl = NULL;
2720 HRESULT hr;
2722 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2723 iface, shader, data, data_size);
2725 wined3d_mutex_lock();
2726 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2728 WARN("Invalid handle (%#x) passed.\n", shader);
2729 wined3d_mutex_unlock();
2731 return D3DERR_INVALIDCALL;
2734 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2735 wined3d_mutex_unlock();
2737 return hr;
2740 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2741 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2743 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2744 iface, handle, segment_count, patch_info);
2745 return D3D_OK;
2748 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2749 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2751 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2752 iface, handle, segment_count, patch_info);
2753 return D3D_OK;
2756 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2758 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2759 return D3DERR_INVALIDCALL;
2762 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2763 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2765 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2766 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2767 HRESULT hr;
2769 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2770 iface, stream_idx, buffer, stride);
2772 wined3d_mutex_lock();
2773 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2774 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2775 wined3d_mutex_unlock();
2777 return hr;
2780 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2781 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2783 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2784 struct d3d8_vertexbuffer *buffer_impl;
2785 struct wined3d_buffer *wined3d_buffer = NULL;
2786 HRESULT hr;
2788 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2789 iface, stream_idx, buffer, stride);
2791 if (!buffer)
2792 return D3DERR_INVALIDCALL;
2794 wined3d_mutex_lock();
2795 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2796 if (SUCCEEDED(hr) && wined3d_buffer)
2798 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2799 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2800 IDirect3DVertexBuffer8_AddRef(*buffer);
2801 wined3d_buffer_decref(wined3d_buffer);
2803 else
2805 if (FAILED(hr))
2806 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2807 *buffer = NULL;
2809 wined3d_mutex_unlock();
2811 return hr;
2814 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2816 d3d8_device_QueryInterface,
2817 d3d8_device_AddRef,
2818 d3d8_device_Release,
2819 d3d8_device_TestCooperativeLevel,
2820 d3d8_device_GetAvailableTextureMem,
2821 d3d8_device_ResourceManagerDiscardBytes,
2822 d3d8_device_GetDirect3D,
2823 d3d8_device_GetDeviceCaps,
2824 d3d8_device_GetDisplayMode,
2825 d3d8_device_GetCreationParameters,
2826 d3d8_device_SetCursorProperties,
2827 d3d8_device_SetCursorPosition,
2828 d3d8_device_ShowCursor,
2829 d3d8_device_CreateAdditionalSwapChain,
2830 d3d8_device_Reset,
2831 d3d8_device_Present,
2832 d3d8_device_GetBackBuffer,
2833 d3d8_device_GetRasterStatus,
2834 d3d8_device_SetGammaRamp,
2835 d3d8_device_GetGammaRamp,
2836 d3d8_device_CreateTexture,
2837 d3d8_device_CreateVolumeTexture,
2838 d3d8_device_CreateCubeTexture,
2839 d3d8_device_CreateVertexBuffer,
2840 d3d8_device_CreateIndexBuffer,
2841 d3d8_device_CreateRenderTarget,
2842 d3d8_device_CreateDepthStencilSurface,
2843 d3d8_device_CreateImageSurface,
2844 d3d8_device_CopyRects,
2845 d3d8_device_UpdateTexture,
2846 d3d8_device_GetFrontBuffer,
2847 d3d8_device_SetRenderTarget,
2848 d3d8_device_GetRenderTarget,
2849 d3d8_device_GetDepthStencilSurface,
2850 d3d8_device_BeginScene,
2851 d3d8_device_EndScene,
2852 d3d8_device_Clear,
2853 d3d8_device_SetTransform,
2854 d3d8_device_GetTransform,
2855 d3d8_device_MultiplyTransform,
2856 d3d8_device_SetViewport,
2857 d3d8_device_GetViewport,
2858 d3d8_device_SetMaterial,
2859 d3d8_device_GetMaterial,
2860 d3d8_device_SetLight,
2861 d3d8_device_GetLight,
2862 d3d8_device_LightEnable,
2863 d3d8_device_GetLightEnable,
2864 d3d8_device_SetClipPlane,
2865 d3d8_device_GetClipPlane,
2866 d3d8_device_SetRenderState,
2867 d3d8_device_GetRenderState,
2868 d3d8_device_BeginStateBlock,
2869 d3d8_device_EndStateBlock,
2870 d3d8_device_ApplyStateBlock,
2871 d3d8_device_CaptureStateBlock,
2872 d3d8_device_DeleteStateBlock,
2873 d3d8_device_CreateStateBlock,
2874 d3d8_device_SetClipStatus,
2875 d3d8_device_GetClipStatus,
2876 d3d8_device_GetTexture,
2877 d3d8_device_SetTexture,
2878 d3d8_device_GetTextureStageState,
2879 d3d8_device_SetTextureStageState,
2880 d3d8_device_ValidateDevice,
2881 d3d8_device_GetInfo,
2882 d3d8_device_SetPaletteEntries,
2883 d3d8_device_GetPaletteEntries,
2884 d3d8_device_SetCurrentTexturePalette,
2885 d3d8_device_GetCurrentTexturePalette,
2886 d3d8_device_DrawPrimitive,
2887 d3d8_device_DrawIndexedPrimitive,
2888 d3d8_device_DrawPrimitiveUP,
2889 d3d8_device_DrawIndexedPrimitiveUP,
2890 d3d8_device_ProcessVertices,
2891 d3d8_device_CreateVertexShader,
2892 d3d8_device_SetVertexShader,
2893 d3d8_device_GetVertexShader,
2894 d3d8_device_DeleteVertexShader,
2895 d3d8_device_SetVertexShaderConstant,
2896 d3d8_device_GetVertexShaderConstant,
2897 d3d8_device_GetVertexShaderDeclaration,
2898 d3d8_device_GetVertexShaderFunction,
2899 d3d8_device_SetStreamSource,
2900 d3d8_device_GetStreamSource,
2901 d3d8_device_SetIndices,
2902 d3d8_device_GetIndices,
2903 d3d8_device_CreatePixelShader,
2904 d3d8_device_SetPixelShader,
2905 d3d8_device_GetPixelShader,
2906 d3d8_device_DeletePixelShader,
2907 d3d8_device_SetPixelShaderConstant,
2908 d3d8_device_GetPixelShaderConstant,
2909 d3d8_device_GetPixelShaderFunction,
2910 d3d8_device_DrawRectPatch,
2911 d3d8_device_DrawTriPatch,
2912 d3d8_device_DeletePatch,
2915 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2917 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2920 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2921 struct wined3d_device *device)
2923 TRACE("device_parent %p, device %p\n", device_parent, device);
2926 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2928 TRACE("device_parent %p.\n", device_parent);
2931 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2933 struct d3d8_device *device = device_from_device_parent(device_parent);
2935 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2937 if (!activate)
2938 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2939 else
2940 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2943 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2944 void *container_parent, struct wined3d_surface *surface, void **parent,
2945 const struct wined3d_parent_ops **parent_ops)
2947 struct d3d8_surface *d3d_surface;
2949 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
2950 device_parent, container_parent, surface, parent, parent_ops);
2952 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2953 return E_OUTOFMEMORY;
2955 surface_init(d3d_surface, container_parent, surface, parent_ops);
2956 *parent = d3d_surface;
2957 TRACE("Created surface %p.\n", d3d_surface);
2959 return D3D_OK;
2962 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
2963 void *container_parent, struct wined3d_volume *volume, void **parent,
2964 const struct wined3d_parent_ops **parent_ops)
2966 struct d3d8_volume *d3d_volume;
2968 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
2969 device_parent, container_parent, volume, parent, parent_ops);
2971 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
2972 return E_OUTOFMEMORY;
2974 volume_init(d3d_volume, container_parent, volume, parent_ops);
2975 *parent = d3d_volume;
2976 TRACE("Created volume %p.\n", d3d_volume);
2978 return D3D_OK;
2981 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
2982 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
2984 struct d3d8_device *device = device_from_device_parent(device_parent);
2985 struct wined3d_resource_desc texture_desc;
2986 struct d3d8_surface *d3d_surface;
2987 struct wined3d_texture *texture;
2988 HRESULT hr;
2990 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
2991 device_parent, container_parent, desc, surface);
2993 texture_desc = *desc;
2994 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
2995 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
2996 WINED3D_SURFACE_MAPPABLE, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
2998 WARN("Failed to create texture, hr %#x.\n", hr);
2999 return hr;
3002 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3003 wined3d_surface_incref(*surface);
3004 wined3d_texture_decref(texture);
3006 d3d_surface = wined3d_surface_get_parent(*surface);
3007 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3009 return hr;
3012 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3013 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3015 struct d3d8_device *device = device_from_device_parent(device_parent);
3016 struct d3d8_swapchain *d3d_swapchain;
3017 HRESULT hr;
3019 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3021 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3023 WARN("Failed to create swapchain, hr %#x.\n", hr);
3024 *swapchain = NULL;
3025 return hr;
3028 *swapchain = d3d_swapchain->wined3d_swapchain;
3029 wined3d_swapchain_incref(*swapchain);
3030 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3032 return hr;
3035 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3037 device_parent_wined3d_device_created,
3038 device_parent_mode_changed,
3039 device_parent_activate,
3040 device_parent_surface_created,
3041 device_parent_volume_created,
3042 device_parent_create_swapchain_surface,
3043 device_parent_create_swapchain,
3046 static void setup_fpu(void)
3048 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3049 WORD cw;
3050 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3051 cw = (cw & ~0xf3f) | 0x3f;
3052 __asm__ volatile ("fldcw %0" : : "m" (cw));
3053 #elif defined(__i386__) && defined(_MSC_VER)
3054 WORD cw;
3055 __asm fnstcw cw;
3056 cw = (cw & ~0xf3f) | 0x3f;
3057 __asm fldcw cw;
3058 #else
3059 FIXME("FPU setup not implemented for this platform.\n");
3060 #endif
3063 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3064 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3066 struct wined3d_swapchain_desc swapchain_desc;
3067 HRESULT hr;
3069 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3070 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3071 device->ref = 1;
3072 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3073 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3074 if (!device->handle_table.entries)
3076 ERR("Failed to allocate handle table memory.\n");
3077 return E_OUTOFMEMORY;
3079 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3081 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3083 wined3d_mutex_lock();
3084 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3085 &device->device_parent, &device->wined3d_device);
3086 if (FAILED(hr))
3088 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3089 wined3d_mutex_unlock();
3090 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3091 return hr;
3094 if (!parameters->Windowed)
3096 HWND device_window = parameters->hDeviceWindow;
3098 if (!focus_window)
3099 focus_window = device_window;
3100 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3102 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3103 wined3d_device_decref(device->wined3d_device);
3104 wined3d_mutex_unlock();
3105 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3106 return hr;
3109 if (!device_window)
3110 device_window = focus_window;
3111 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3112 parameters->BackBufferWidth,
3113 parameters->BackBufferHeight);
3116 if (flags & D3DCREATE_MULTITHREADED)
3117 wined3d_device_set_multithreaded(device->wined3d_device);
3119 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3121 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3122 if (FAILED(hr))
3124 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3125 wined3d_device_release_focus_window(device->wined3d_device);
3126 wined3d_device_decref(device->wined3d_device);
3127 wined3d_mutex_unlock();
3128 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3129 return hr;
3132 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3133 wined3d_mutex_unlock();
3135 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3137 device->declArraySize = 16;
3138 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3139 if (!device->decls)
3141 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3142 hr = E_OUTOFMEMORY;
3143 goto err;
3146 device->d3d_parent = &parent->IDirect3D8_iface;
3147 IDirect3D8_AddRef(device->d3d_parent);
3149 return D3D_OK;
3151 err:
3152 wined3d_mutex_lock();
3153 wined3d_device_uninit_3d(device->wined3d_device);
3154 wined3d_device_release_focus_window(device->wined3d_device);
3155 wined3d_device_decref(device->wined3d_device);
3156 wined3d_mutex_unlock();
3157 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3158 return hr;