urlmon: Recognize <body> tag in FindMimeFromData function.
[wine/multimedia.git] / dlls / d3d8 / device.c
blobfd4cd3c56aa2429c798286b06b60375984fd7f68
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 HRESULT hr;
405 TRACE("iface %p.\n", iface);
407 wined3d_mutex_lock();
408 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
409 wined3d_mutex_unlock();
411 return hr;
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->forwardReference = NULL;
922 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
923 *surface = &surface_impl->IDirect3DSurface8_iface;
924 IDirect3DSurface8_AddRef(*surface);
925 wined3d_texture_decref(texture);
927 wined3d_mutex_unlock();
929 return D3D_OK;
932 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
933 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
934 IDirect3DSurface8 **surface)
936 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
937 DWORD flags = 0;
939 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
940 iface, width, height, format, multisample_type, lockable, surface);
942 *surface = NULL;
943 if (lockable)
944 flags |= WINED3D_SURFACE_MAPPABLE;
946 return d3d8_device_create_surface(device, width, height, format, flags, surface,
947 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
950 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
951 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
952 IDirect3DSurface8 **surface)
954 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
956 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
957 iface, width, height, format, multisample_type, surface);
959 *surface = NULL;
961 /* TODO: Verify that Discard is false */
962 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
963 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
966 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
967 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
968 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
970 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
972 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
973 iface, width, height, format, surface);
975 *surface = NULL;
977 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
978 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
981 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
982 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
983 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
985 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
986 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
987 enum wined3d_format_id src_format, dst_format;
988 struct wined3d_resource_desc wined3d_desc;
989 struct wined3d_resource *wined3d_resource;
990 UINT src_w, src_h;
991 HRESULT hr;
993 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
994 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
996 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
997 * destination texture is in WINED3D_POOL_DEFAULT. */
999 wined3d_mutex_lock();
1000 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1001 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1002 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1004 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1005 wined3d_mutex_unlock();
1006 return D3DERR_INVALIDCALL;
1008 src_format = wined3d_desc.format;
1009 src_w = wined3d_desc.width;
1010 src_h = wined3d_desc.height;
1012 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1013 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1014 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1016 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1017 wined3d_mutex_unlock();
1018 return D3DERR_INVALIDCALL;
1020 dst_format = wined3d_desc.format;
1022 /* Check that the source and destination formats match */
1023 if (src_format != dst_format && WINED3DFMT_UNKNOWN != dst_format)
1025 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1026 src_surface, dst_surface);
1027 wined3d_mutex_unlock();
1028 return D3DERR_INVALIDCALL;
1030 else if (WINED3DFMT_UNKNOWN == dst_format)
1032 TRACE("Converting destination surface from WINED3DFMT_UNKNOWN to the source format.\n");
1033 if (FAILED(hr = wined3d_surface_update_desc(dst->wined3d_surface, wined3d_desc.width, wined3d_desc.height,
1034 src_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality, NULL, 0)))
1036 WARN("Failed to update surface desc, hr %#x.\n", hr);
1037 wined3d_mutex_unlock();
1038 return hr;
1042 /* Quick if complete copy ... */
1043 if (!rect_count && !src_rects && !dst_points)
1045 RECT rect = {0, 0, src_w, src_h};
1046 wined3d_surface_blt(dst->wined3d_surface, &rect,
1047 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
1049 else
1051 unsigned int i;
1052 /* Copy rect by rect */
1053 if (src_rects && dst_points)
1055 for (i = 0; i < rect_count; ++i)
1057 UINT w = src_rects[i].right - src_rects[i].left;
1058 UINT h = src_rects[i].bottom - src_rects[i].top;
1059 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1060 dst_points[i].x + w, dst_points[i].y + h};
1062 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1063 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1066 else
1068 for (i = 0; i < rect_count; ++i)
1070 UINT w = src_rects[i].right - src_rects[i].left;
1071 UINT h = src_rects[i].bottom - src_rects[i].top;
1072 RECT dst_rect = {0, 0, w, h};
1074 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1075 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1079 wined3d_mutex_unlock();
1081 return WINED3D_OK;
1084 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1085 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1087 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1088 struct d3d8_texture *src_impl, *dst_impl;
1089 HRESULT hr;
1091 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1093 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1094 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1096 wined3d_mutex_lock();
1097 hr = wined3d_device_update_texture(device->wined3d_device,
1098 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1099 wined3d_mutex_unlock();
1101 return hr;
1104 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1106 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1107 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1108 HRESULT hr;
1110 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1112 if (!dst_surface)
1114 WARN("Invalid destination surface passed.\n");
1115 return D3DERR_INVALIDCALL;
1118 wined3d_mutex_lock();
1119 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1120 wined3d_mutex_unlock();
1122 return hr;
1125 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1126 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1128 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1129 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1130 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1131 struct wined3d_surface *original_ds = NULL;
1132 HRESULT hr = D3D_OK;
1134 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1136 wined3d_mutex_lock();
1138 if (ds_impl)
1140 struct wined3d_resource_desc ds_desc, rt_desc;
1141 struct wined3d_resource *wined3d_resource;
1142 struct wined3d_surface *original_rt = NULL;
1144 /* If no render target is passed in check the size against the current RT */
1145 if (!render_target)
1147 if (!(original_rt = wined3d_device_get_render_target(device->wined3d_device, 0)))
1149 wined3d_mutex_unlock();
1150 return D3DERR_NOTFOUND;
1152 wined3d_resource = wined3d_surface_get_resource(original_rt);
1154 else
1155 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1156 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1158 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1159 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1161 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1163 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1164 wined3d_mutex_unlock();
1165 return D3DERR_INVALIDCALL;
1169 original_ds = wined3d_device_get_depth_stencil(device->wined3d_device);
1170 wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1171 if (render_target)
1173 hr = wined3d_device_set_render_target(device->wined3d_device, 0, rt_impl->wined3d_surface, TRUE);
1174 if (FAILED(hr))
1175 wined3d_device_set_depth_stencil(device->wined3d_device, original_ds);
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_surface *wined3d_surface;
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_surface = wined3d_device_get_render_target(device->wined3d_device, 0)))
1198 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1199 *render_target = &surface_impl->IDirect3DSurface8_iface;
1200 IDirect3DSurface8_AddRef(*render_target);
1201 hr = D3D_OK;
1203 else
1205 ERR("Failed to get wined3d render target.\n");
1206 *render_target = NULL;
1207 hr = D3DERR_NOTFOUND;
1209 wined3d_mutex_unlock();
1211 return hr;
1214 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1216 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1217 struct wined3d_surface *wined3d_surface;
1218 struct d3d8_surface *surface_impl;
1219 HRESULT hr = D3D_OK;
1221 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1223 if (!depth_stencil)
1224 return D3DERR_INVALIDCALL;
1226 wined3d_mutex_lock();
1227 if ((wined3d_surface = wined3d_device_get_depth_stencil(device->wined3d_device)))
1229 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1230 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1231 IDirect3DSurface8_AddRef(*depth_stencil);
1233 else
1235 hr = WINED3DERR_NOTFOUND;
1236 *depth_stencil = NULL;
1238 wined3d_mutex_unlock();
1240 return hr;
1243 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1245 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1246 HRESULT hr;
1248 TRACE("iface %p.\n", iface);
1250 wined3d_mutex_lock();
1251 hr = wined3d_device_begin_scene(device->wined3d_device);
1252 wined3d_mutex_unlock();
1254 return hr;
1257 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1259 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1260 HRESULT hr;
1262 TRACE("iface %p.\n", iface);
1264 wined3d_mutex_lock();
1265 hr = wined3d_device_end_scene(device->wined3d_device);
1266 wined3d_mutex_unlock();
1268 return hr;
1271 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1272 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1274 const struct wined3d_color c =
1276 ((color >> 16) & 0xff) / 255.0f,
1277 ((color >> 8) & 0xff) / 255.0f,
1278 (color & 0xff) / 255.0f,
1279 ((color >> 24) & 0xff) / 255.0f,
1281 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1282 HRESULT hr;
1284 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1285 iface, rect_count, rects, flags, color, z, stencil);
1287 wined3d_mutex_lock();
1288 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1289 wined3d_mutex_unlock();
1291 return hr;
1294 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1295 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1297 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1299 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1301 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1302 wined3d_mutex_lock();
1303 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1304 wined3d_mutex_unlock();
1306 return D3D_OK;
1309 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1310 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1312 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1314 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1316 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1317 wined3d_mutex_lock();
1318 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1319 wined3d_mutex_unlock();
1321 return D3D_OK;
1324 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1325 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1327 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1329 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1331 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1332 wined3d_mutex_lock();
1333 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1334 wined3d_mutex_unlock();
1336 return D3D_OK;
1339 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1341 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1343 TRACE("iface %p, viewport %p.\n", iface, viewport);
1345 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1346 wined3d_mutex_lock();
1347 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1348 wined3d_mutex_unlock();
1350 return D3D_OK;
1353 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1355 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1357 TRACE("iface %p, viewport %p.\n", iface, viewport);
1359 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1360 wined3d_mutex_lock();
1361 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1362 wined3d_mutex_unlock();
1364 return D3D_OK;
1367 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1369 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1371 TRACE("iface %p, material %p.\n", iface, material);
1373 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1374 wined3d_mutex_lock();
1375 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1376 wined3d_mutex_unlock();
1378 return D3D_OK;
1381 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1383 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1385 TRACE("iface %p, material %p.\n", iface, material);
1387 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1388 wined3d_mutex_lock();
1389 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1390 wined3d_mutex_unlock();
1392 return D3D_OK;
1395 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1397 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1398 HRESULT hr;
1400 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1402 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1403 wined3d_mutex_lock();
1404 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1405 wined3d_mutex_unlock();
1407 return hr;
1410 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1412 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1413 HRESULT hr;
1415 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1417 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1418 wined3d_mutex_lock();
1419 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1420 wined3d_mutex_unlock();
1422 return hr;
1425 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1427 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1428 HRESULT hr;
1430 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1432 wined3d_mutex_lock();
1433 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1434 wined3d_mutex_unlock();
1436 return hr;
1439 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1441 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1442 HRESULT hr;
1444 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1446 wined3d_mutex_lock();
1447 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1448 wined3d_mutex_unlock();
1450 return hr;
1453 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1455 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1456 HRESULT hr;
1458 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1460 wined3d_mutex_lock();
1461 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1462 wined3d_mutex_unlock();
1464 return hr;
1467 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1469 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1470 HRESULT hr;
1472 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1474 wined3d_mutex_lock();
1475 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1476 wined3d_mutex_unlock();
1478 return hr;
1481 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1482 D3DRENDERSTATETYPE state, DWORD value)
1484 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1486 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1488 wined3d_mutex_lock();
1489 switch (state)
1491 case D3DRS_ZBIAS:
1492 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1493 break;
1495 default:
1496 wined3d_device_set_render_state(device->wined3d_device, state, value);
1498 wined3d_mutex_unlock();
1500 return D3D_OK;
1503 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1504 D3DRENDERSTATETYPE state, DWORD *value)
1506 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1508 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1510 wined3d_mutex_lock();
1511 switch (state)
1513 case D3DRS_ZBIAS:
1514 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1515 break;
1517 default:
1518 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1520 wined3d_mutex_unlock();
1522 return D3D_OK;
1525 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1527 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1528 HRESULT hr;
1530 TRACE("iface %p.\n", iface);
1532 wined3d_mutex_lock();
1533 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1534 wined3d_mutex_unlock();
1536 return hr;
1539 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1541 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1542 struct wined3d_stateblock *stateblock;
1543 HRESULT hr;
1545 TRACE("iface %p, token %p.\n", iface, token);
1547 /* Tell wineD3D to endstateblock before anything else (in case we run out
1548 * of memory later and cause locking problems)
1550 wined3d_mutex_lock();
1551 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1552 if (FAILED(hr))
1554 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1555 wined3d_mutex_unlock();
1556 return hr;
1559 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1560 wined3d_mutex_unlock();
1562 if (*token == D3D8_INVALID_HANDLE)
1564 ERR("Failed to create a handle\n");
1565 wined3d_mutex_lock();
1566 wined3d_stateblock_decref(stateblock);
1567 wined3d_mutex_unlock();
1568 return E_FAIL;
1570 ++*token;
1572 TRACE("Returning %#x (%p).\n", *token, stateblock);
1574 return hr;
1577 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1579 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1580 struct wined3d_stateblock *stateblock;
1582 TRACE("iface %p, token %#x.\n", iface, token);
1584 if (!token)
1585 return D3D_OK;
1587 wined3d_mutex_lock();
1588 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1589 if (!stateblock)
1591 WARN("Invalid handle (%#x) passed.\n", token);
1592 wined3d_mutex_unlock();
1593 return D3DERR_INVALIDCALL;
1595 wined3d_stateblock_apply(stateblock);
1596 wined3d_mutex_unlock();
1598 return D3D_OK;
1601 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1603 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1604 struct wined3d_stateblock *stateblock;
1606 TRACE("iface %p, token %#x.\n", iface, token);
1608 wined3d_mutex_lock();
1609 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1610 if (!stateblock)
1612 WARN("Invalid handle (%#x) passed.\n", token);
1613 wined3d_mutex_unlock();
1614 return D3DERR_INVALIDCALL;
1616 wined3d_stateblock_capture(stateblock);
1617 wined3d_mutex_unlock();
1619 return D3D_OK;
1622 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1624 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1625 struct wined3d_stateblock *stateblock;
1627 TRACE("iface %p, token %#x.\n", iface, token);
1629 wined3d_mutex_lock();
1630 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1632 if (!stateblock)
1634 WARN("Invalid handle (%#x) passed.\n", token);
1635 wined3d_mutex_unlock();
1636 return D3DERR_INVALIDCALL;
1639 if (wined3d_stateblock_decref(stateblock))
1641 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1643 wined3d_mutex_unlock();
1645 return D3D_OK;
1648 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1649 D3DSTATEBLOCKTYPE type, DWORD *handle)
1651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1652 struct wined3d_stateblock *stateblock;
1653 HRESULT hr;
1655 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1657 if (type != D3DSBT_ALL
1658 && type != D3DSBT_PIXELSTATE
1659 && type != D3DSBT_VERTEXSTATE)
1661 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1662 return D3DERR_INVALIDCALL;
1665 wined3d_mutex_lock();
1666 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1667 if (FAILED(hr))
1669 wined3d_mutex_unlock();
1670 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1671 return hr;
1674 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1675 wined3d_mutex_unlock();
1677 if (*handle == D3D8_INVALID_HANDLE)
1679 ERR("Failed to allocate a handle.\n");
1680 wined3d_mutex_lock();
1681 wined3d_stateblock_decref(stateblock);
1682 wined3d_mutex_unlock();
1683 return E_FAIL;
1685 ++*handle;
1687 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1689 return hr;
1692 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1694 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1695 HRESULT hr;
1697 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1698 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1700 wined3d_mutex_lock();
1701 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1702 wined3d_mutex_unlock();
1704 return hr;
1707 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1709 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1710 HRESULT hr;
1712 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1714 wined3d_mutex_lock();
1715 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1716 wined3d_mutex_unlock();
1718 return hr;
1721 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1723 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1724 struct wined3d_texture *wined3d_texture;
1725 struct d3d8_texture *texture_impl;
1727 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1729 if (!texture)
1730 return D3DERR_INVALIDCALL;
1732 wined3d_mutex_lock();
1733 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1735 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1736 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1737 IDirect3DBaseTexture8_AddRef(*texture);
1739 else
1741 *texture = NULL;
1743 wined3d_mutex_unlock();
1745 return D3D_OK;
1748 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1750 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1751 struct d3d8_texture *texture_impl;
1752 HRESULT hr;
1754 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1756 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1758 wined3d_mutex_lock();
1759 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1760 texture_impl ? texture_impl->wined3d_texture : NULL);
1761 wined3d_mutex_unlock();
1763 return hr;
1766 static const struct tss_lookup
1768 BOOL sampler_state;
1769 enum wined3d_texture_stage_state state;
1771 tss_lookup[] =
1773 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1774 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1775 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1776 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1777 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1778 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1779 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1780 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1781 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1782 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1783 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1784 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1785 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1786 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1787 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1788 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1789 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1790 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1791 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1792 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1793 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1794 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1795 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1796 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1797 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1798 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1799 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1800 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1801 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1804 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1805 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1807 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1808 const struct tss_lookup *l;
1810 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1812 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1814 WARN("Invalid Type %#x passed.\n", Type);
1815 return D3D_OK;
1818 l = &tss_lookup[Type];
1820 wined3d_mutex_lock();
1821 if (l->sampler_state)
1822 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1823 else
1824 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1825 wined3d_mutex_unlock();
1827 return D3D_OK;
1830 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1831 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1833 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1834 const struct tss_lookup *l;
1836 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1838 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1840 WARN("Invalid type %#x passed.\n", type);
1841 return D3D_OK;
1844 l = &tss_lookup[type];
1846 wined3d_mutex_lock();
1847 if (l->sampler_state)
1848 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1849 else
1850 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1851 wined3d_mutex_unlock();
1853 return D3D_OK;
1856 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1858 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1859 HRESULT hr;
1861 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1863 wined3d_mutex_lock();
1864 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1865 wined3d_mutex_unlock();
1867 return hr;
1870 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1871 DWORD info_id, void *info, DWORD info_size)
1873 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1875 return D3D_OK;
1878 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1879 UINT palette_idx, const PALETTEENTRY *entries)
1881 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1883 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1884 * does not have a d3d8/9-style palette API */
1886 return D3D_OK;
1889 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1890 UINT palette_idx, PALETTEENTRY *entries)
1892 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1894 return D3DERR_INVALIDCALL;
1897 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1899 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1901 return D3D_OK;
1904 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1906 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1908 return D3DERR_INVALIDCALL;
1911 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1912 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1914 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1915 HRESULT hr;
1917 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1918 iface, primitive_type, start_vertex, primitive_count);
1920 wined3d_mutex_lock();
1921 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1922 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1923 vertex_count_from_primitive_count(primitive_type, primitive_count));
1924 wined3d_mutex_unlock();
1926 return hr;
1929 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1930 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1931 UINT start_idx, UINT primitive_count)
1933 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1934 HRESULT hr;
1936 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1937 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1939 wined3d_mutex_lock();
1940 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1941 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1942 vertex_count_from_primitive_count(primitive_type, primitive_count));
1943 wined3d_mutex_unlock();
1945 return hr;
1948 /* The caller is responsible for wined3d locking */
1949 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1951 HRESULT hr;
1953 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1955 UINT size = max(device->vertex_buffer_size * 2, min_size);
1956 struct wined3d_buffer *buffer;
1958 TRACE("Growing vertex buffer to %u bytes\n", size);
1960 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1961 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1962 if (FAILED(hr))
1964 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1965 return hr;
1968 if (device->vertex_buffer)
1969 wined3d_buffer_decref(device->vertex_buffer);
1971 device->vertex_buffer = buffer;
1972 device->vertex_buffer_size = size;
1973 device->vertex_buffer_pos = 0;
1975 return D3D_OK;
1978 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1979 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
1980 UINT stride)
1982 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1983 HRESULT hr;
1984 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
1985 UINT size = vtx_count * stride;
1986 UINT vb_pos, align;
1987 BYTE *buffer_data;
1989 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1990 iface, primitive_type, primitive_count, data, stride);
1992 if (!primitive_count)
1994 WARN("primitive_count is 0, returning D3D_OK\n");
1995 return D3D_OK;
1998 wined3d_mutex_lock();
1999 hr = d3d8_device_prepare_vertex_buffer(device, size);
2000 if (FAILED(hr))
2001 goto done;
2003 vb_pos = device->vertex_buffer_pos;
2004 align = vb_pos % stride;
2005 if (align) align = stride - align;
2006 if (vb_pos + size + align > device->vertex_buffer_size)
2007 vb_pos = 0;
2008 else
2009 vb_pos += align;
2011 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2012 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2013 if (FAILED(hr))
2014 goto done;
2015 memcpy(buffer_data, data, size);
2016 wined3d_buffer_unmap(device->vertex_buffer);
2017 device->vertex_buffer_pos = vb_pos + size;
2019 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2020 if (FAILED(hr))
2021 goto done;
2023 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2024 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2025 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2027 done:
2028 wined3d_mutex_unlock();
2029 return hr;
2032 /* The caller is responsible for wined3d locking */
2033 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2035 HRESULT hr;
2037 if (device->index_buffer_size < min_size || !device->index_buffer)
2039 UINT size = max(device->index_buffer_size * 2, min_size);
2040 struct wined3d_buffer *buffer;
2042 TRACE("Growing index buffer to %u bytes\n", size);
2044 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2045 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2046 if (FAILED(hr))
2048 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2049 return hr;
2052 if (device->index_buffer)
2053 wined3d_buffer_decref(device->index_buffer);
2055 device->index_buffer = buffer;
2056 device->index_buffer_size = size;
2057 device->index_buffer_pos = 0;
2059 return D3D_OK;
2062 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2063 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2064 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2065 const void *vertex_data, UINT vertex_stride)
2067 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2068 HRESULT hr;
2069 BYTE *buffer_data;
2071 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2072 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2073 UINT idx_size = idx_count * idx_fmt_size;
2074 UINT ib_pos;
2076 UINT vtx_size = vertex_count * vertex_stride;
2077 UINT vb_pos, align;
2079 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2080 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2081 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2082 index_data, index_format, vertex_data, vertex_stride);
2084 if (!primitive_count)
2086 WARN("primitive_count is 0, returning D3D_OK\n");
2087 return D3D_OK;
2090 wined3d_mutex_lock();
2092 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2093 if (FAILED(hr))
2094 goto done;
2096 vb_pos = device->vertex_buffer_pos;
2097 align = vb_pos % vertex_stride;
2098 if (align) align = vertex_stride - align;
2099 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2100 vb_pos = 0;
2101 else
2102 vb_pos += align;
2104 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2105 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2106 if (FAILED(hr))
2107 goto done;
2108 memcpy(buffer_data, vertex_data, vtx_size);
2109 wined3d_buffer_unmap(device->vertex_buffer);
2110 device->vertex_buffer_pos = vb_pos + vtx_size;
2112 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2113 if (FAILED(hr))
2114 goto done;
2116 ib_pos = device->index_buffer_pos;
2117 align = ib_pos % idx_fmt_size;
2118 if (align) align = idx_fmt_size - align;
2119 if (ib_pos + idx_size + align > device->index_buffer_size)
2120 ib_pos = 0;
2121 else
2122 ib_pos += align;
2124 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2125 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2126 if (FAILED(hr))
2127 goto done;
2128 memcpy(buffer_data, index_data, idx_size);
2129 wined3d_buffer_unmap(device->index_buffer);
2130 device->index_buffer_pos = ib_pos + idx_size;
2132 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2133 if (FAILED(hr))
2134 goto done;
2136 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2137 wined3dformat_from_d3dformat(index_format));
2138 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2140 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2141 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2143 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2144 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2145 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2147 done:
2148 wined3d_mutex_unlock();
2149 return hr;
2152 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2153 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2155 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2156 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2157 HRESULT hr;
2159 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2160 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2162 wined3d_mutex_lock();
2163 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2164 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2165 wined3d_mutex_unlock();
2167 return hr;
2170 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2171 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2173 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2174 struct d3d8_vertex_shader *object;
2175 DWORD shader_handle;
2176 DWORD handle;
2177 HRESULT hr;
2179 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2180 iface, declaration, byte_code, shader, usage);
2182 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2183 if (!object)
2185 *shader = 0;
2186 return E_OUTOFMEMORY;
2189 wined3d_mutex_lock();
2190 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2191 wined3d_mutex_unlock();
2192 if (handle == D3D8_INVALID_HANDLE)
2194 ERR("Failed to allocate vertex shader handle.\n");
2195 HeapFree(GetProcessHeap(), 0, object);
2196 *shader = 0;
2197 return E_OUTOFMEMORY;
2200 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2202 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2203 if (FAILED(hr))
2205 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2206 wined3d_mutex_lock();
2207 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2208 wined3d_mutex_unlock();
2209 HeapFree(GetProcessHeap(), 0, object);
2210 *shader = 0;
2211 return hr;
2214 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2215 *shader = shader_handle;
2217 return D3D_OK;
2220 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2222 struct d3d8_vertex_declaration *d3d8_declaration;
2223 struct FvfToDecl *convertedDecls = device->decls;
2224 int p, low, high; /* deliberately signed */
2225 HRESULT hr;
2227 TRACE("Searching for declaration for fvf %08x... ", fvf);
2229 low = 0;
2230 high = device->numConvertedDecls - 1;
2231 while (low <= high)
2233 p = (low + high) >> 1;
2234 TRACE("%d ", p);
2236 if (convertedDecls[p].fvf == fvf)
2238 TRACE("found %p\n", convertedDecls[p].declaration);
2239 return convertedDecls[p].declaration;
2242 if (convertedDecls[p].fvf < fvf)
2243 low = p + 1;
2244 else
2245 high = p - 1;
2247 TRACE("not found. Creating and inserting at position %d.\n", low);
2249 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2250 return NULL;
2252 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2254 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2255 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2256 return NULL;
2259 if (device->declArraySize == device->numConvertedDecls)
2261 UINT grow = device->declArraySize / 2;
2263 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2264 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2265 if (!convertedDecls)
2267 d3d8_vertex_declaration_destroy(d3d8_declaration);
2268 return NULL;
2270 device->decls = convertedDecls;
2271 device->declArraySize += grow;
2274 memmove(convertedDecls + low + 1, convertedDecls + low,
2275 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2276 convertedDecls[low].declaration = d3d8_declaration;
2277 convertedDecls[low].fvf = fvf;
2278 ++device->numConvertedDecls;
2280 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2282 return d3d8_declaration;
2285 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2287 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2288 struct d3d8_vertex_shader *shader_impl;
2290 TRACE("iface %p, shader %#x.\n", iface, shader);
2292 if (VS_HIGHESTFIXEDFXF >= shader)
2294 TRACE("Setting FVF, %#x\n", shader);
2296 wined3d_mutex_lock();
2297 wined3d_device_set_vertex_declaration(device->wined3d_device,
2298 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2299 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2300 wined3d_mutex_unlock();
2302 return D3D_OK;
2305 TRACE("Setting shader\n");
2307 wined3d_mutex_lock();
2308 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2310 WARN("Invalid handle (%#x) passed.\n", shader);
2311 wined3d_mutex_unlock();
2313 return D3DERR_INVALIDCALL;
2316 wined3d_device_set_vertex_declaration(device->wined3d_device,
2317 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2318 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2319 wined3d_mutex_unlock();
2321 return D3D_OK;
2324 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2326 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2327 struct wined3d_vertex_declaration *wined3d_declaration;
2328 struct d3d8_vertex_declaration *d3d8_declaration;
2330 TRACE("iface %p, shader %p.\n", iface, shader);
2332 wined3d_mutex_lock();
2333 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2335 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2336 *shader = d3d8_declaration->shader_handle;
2338 else
2340 *shader = 0;
2342 wined3d_mutex_unlock();
2344 TRACE("Returning %#x.\n", *shader);
2346 return D3D_OK;
2349 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2351 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2352 struct d3d8_vertex_shader *shader_impl;
2354 TRACE("iface %p, shader %#x.\n", iface, shader);
2356 wined3d_mutex_lock();
2357 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2359 WARN("Invalid handle (%#x) passed.\n", shader);
2360 wined3d_mutex_unlock();
2362 return D3DERR_INVALIDCALL;
2365 if (shader_impl->wined3d_shader
2366 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2367 IDirect3DDevice8_SetVertexShader(iface, 0);
2369 wined3d_mutex_unlock();
2371 d3d8_vertex_shader_destroy(shader_impl);
2373 return D3D_OK;
2376 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2377 DWORD start_register, const void *data, DWORD count)
2379 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2380 HRESULT hr;
2382 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2383 iface, start_register, data, count);
2385 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2387 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2388 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2389 return D3DERR_INVALIDCALL;
2392 wined3d_mutex_lock();
2393 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2394 wined3d_mutex_unlock();
2396 return hr;
2399 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2400 DWORD start_register, void *data, DWORD count)
2402 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2403 HRESULT hr;
2405 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2406 iface, start_register, data, count);
2408 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2410 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2411 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2412 return D3DERR_INVALIDCALL;
2415 wined3d_mutex_lock();
2416 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2417 wined3d_mutex_unlock();
2419 return hr;
2422 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2423 DWORD shader, void *data, DWORD *data_size)
2425 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2426 struct d3d8_vertex_declaration *declaration;
2427 struct d3d8_vertex_shader *shader_impl;
2429 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2430 iface, shader, data, data_size);
2432 wined3d_mutex_lock();
2433 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2434 wined3d_mutex_unlock();
2436 if (!shader_impl)
2438 WARN("Invalid handle (%#x) passed.\n", shader);
2439 return D3DERR_INVALIDCALL;
2441 declaration = shader_impl->vertex_declaration;
2443 if (!data)
2445 *data_size = declaration->elements_size;
2446 return D3D_OK;
2449 /* MSDN claims that if *data_size is smaller than the required size
2450 * we should write the required size and return D3DERR_MOREDATA.
2451 * That's not actually true. */
2452 if (*data_size < declaration->elements_size)
2453 return D3DERR_INVALIDCALL;
2455 memcpy(data, declaration->elements, declaration->elements_size);
2457 return D3D_OK;
2460 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2461 DWORD shader, void *data, DWORD *data_size)
2463 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2464 struct d3d8_vertex_shader *shader_impl = NULL;
2465 HRESULT hr;
2467 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2468 iface, shader, data, data_size);
2470 wined3d_mutex_lock();
2471 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2473 WARN("Invalid handle (%#x) passed.\n", shader);
2474 wined3d_mutex_unlock();
2476 return D3DERR_INVALIDCALL;
2479 if (!shader_impl->wined3d_shader)
2481 wined3d_mutex_unlock();
2482 *data_size = 0;
2483 return D3D_OK;
2486 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2487 wined3d_mutex_unlock();
2489 return hr;
2492 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2493 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2495 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2496 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2498 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2500 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2501 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2502 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2503 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2504 * problem)
2506 wined3d_mutex_lock();
2507 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2508 wined3d_device_set_index_buffer(device->wined3d_device,
2509 ib ? ib->wined3d_buffer : NULL,
2510 ib ? ib->format : WINED3DFMT_UNKNOWN);
2511 wined3d_mutex_unlock();
2513 return D3D_OK;
2516 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2517 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2519 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2520 enum wined3d_format_id wined3d_format;
2521 struct wined3d_buffer *wined3d_buffer;
2522 struct d3d8_indexbuffer *buffer_impl;
2524 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2526 if (!buffer)
2527 return D3DERR_INVALIDCALL;
2529 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2530 wined3d_mutex_lock();
2531 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2532 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2534 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2535 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2536 IDirect3DIndexBuffer8_AddRef(*buffer);
2538 else
2540 *buffer = NULL;
2542 wined3d_mutex_unlock();
2544 return D3D_OK;
2547 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2548 const DWORD *byte_code, DWORD *shader)
2550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2551 struct d3d8_pixel_shader *object;
2552 DWORD shader_handle;
2553 DWORD handle;
2554 HRESULT hr;
2556 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2558 if (!shader)
2559 return D3DERR_INVALIDCALL;
2561 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2562 if (!object)
2563 return E_OUTOFMEMORY;
2565 wined3d_mutex_lock();
2566 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2567 wined3d_mutex_unlock();
2568 if (handle == D3D8_INVALID_HANDLE)
2570 ERR("Failed to allocate pixel shader handle.\n");
2571 HeapFree(GetProcessHeap(), 0, object);
2572 return E_OUTOFMEMORY;
2575 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2577 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2578 if (FAILED(hr))
2580 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2581 wined3d_mutex_lock();
2582 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2583 wined3d_mutex_unlock();
2584 HeapFree(GetProcessHeap(), 0, object);
2585 *shader = 0;
2586 return hr;
2589 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2590 *shader = shader_handle;
2592 return D3D_OK;
2595 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2597 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2598 struct d3d8_pixel_shader *shader_impl;
2600 TRACE("iface %p, shader %#x.\n", iface, shader);
2602 wined3d_mutex_lock();
2604 if (!shader)
2606 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2607 wined3d_mutex_unlock();
2608 return D3D_OK;
2611 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2613 WARN("Invalid handle (%#x) passed.\n", shader);
2614 wined3d_mutex_unlock();
2615 return D3DERR_INVALIDCALL;
2618 TRACE("Setting shader %p.\n", shader_impl);
2619 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2620 wined3d_mutex_unlock();
2622 return D3D_OK;
2625 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2627 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2628 struct wined3d_shader *object;
2630 TRACE("iface %p, shader %p.\n", iface, shader);
2632 if (!shader)
2633 return D3DERR_INVALIDCALL;
2635 wined3d_mutex_lock();
2636 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2638 struct d3d8_pixel_shader *d3d8_shader;
2639 d3d8_shader = wined3d_shader_get_parent(object);
2640 *shader = d3d8_shader->handle;
2642 else
2644 *shader = 0;
2646 wined3d_mutex_unlock();
2648 TRACE("Returning %#x.\n", *shader);
2650 return D3D_OK;
2653 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2656 struct d3d8_pixel_shader *shader_impl;
2658 TRACE("iface %p, shader %#x.\n", iface, shader);
2660 wined3d_mutex_lock();
2662 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2664 WARN("Invalid handle (%#x) passed.\n", shader);
2665 wined3d_mutex_unlock();
2666 return D3DERR_INVALIDCALL;
2669 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2670 IDirect3DDevice8_SetPixelShader(iface, 0);
2672 wined3d_mutex_unlock();
2674 d3d8_pixel_shader_destroy(shader_impl);
2676 return D3D_OK;
2679 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2680 DWORD start_register, const void *data, DWORD count)
2682 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2683 HRESULT hr;
2685 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2686 iface, start_register, data, count);
2688 wined3d_mutex_lock();
2689 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2690 wined3d_mutex_unlock();
2692 return hr;
2695 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2696 DWORD start_register, void *data, DWORD count)
2698 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2699 HRESULT hr;
2701 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2702 iface, start_register, data, count);
2704 wined3d_mutex_lock();
2705 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2706 wined3d_mutex_unlock();
2708 return hr;
2711 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2712 DWORD shader, void *data, DWORD *data_size)
2714 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2715 struct d3d8_pixel_shader *shader_impl = NULL;
2716 HRESULT hr;
2718 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2719 iface, shader, data, data_size);
2721 wined3d_mutex_lock();
2722 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2724 WARN("Invalid handle (%#x) passed.\n", shader);
2725 wined3d_mutex_unlock();
2727 return D3DERR_INVALIDCALL;
2730 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2731 wined3d_mutex_unlock();
2733 return hr;
2736 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2737 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2739 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2740 iface, handle, segment_count, patch_info);
2741 return D3D_OK;
2744 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2745 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2747 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2748 iface, handle, segment_count, patch_info);
2749 return D3D_OK;
2752 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2754 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2755 return D3DERR_INVALIDCALL;
2758 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2759 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2761 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2762 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2763 HRESULT hr;
2765 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2766 iface, stream_idx, buffer, stride);
2768 wined3d_mutex_lock();
2769 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2770 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2771 wined3d_mutex_unlock();
2773 return hr;
2776 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2777 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2779 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2780 struct d3d8_vertexbuffer *buffer_impl;
2781 struct wined3d_buffer *wined3d_buffer = NULL;
2782 HRESULT hr;
2784 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2785 iface, stream_idx, buffer, stride);
2787 if (!buffer)
2788 return D3DERR_INVALIDCALL;
2790 wined3d_mutex_lock();
2791 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2792 if (SUCCEEDED(hr) && wined3d_buffer)
2794 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2795 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2796 IDirect3DVertexBuffer8_AddRef(*buffer);
2797 wined3d_buffer_decref(wined3d_buffer);
2799 else
2801 if (FAILED(hr))
2802 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2803 *buffer = NULL;
2805 wined3d_mutex_unlock();
2807 return hr;
2810 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2812 d3d8_device_QueryInterface,
2813 d3d8_device_AddRef,
2814 d3d8_device_Release,
2815 d3d8_device_TestCooperativeLevel,
2816 d3d8_device_GetAvailableTextureMem,
2817 d3d8_device_ResourceManagerDiscardBytes,
2818 d3d8_device_GetDirect3D,
2819 d3d8_device_GetDeviceCaps,
2820 d3d8_device_GetDisplayMode,
2821 d3d8_device_GetCreationParameters,
2822 d3d8_device_SetCursorProperties,
2823 d3d8_device_SetCursorPosition,
2824 d3d8_device_ShowCursor,
2825 d3d8_device_CreateAdditionalSwapChain,
2826 d3d8_device_Reset,
2827 d3d8_device_Present,
2828 d3d8_device_GetBackBuffer,
2829 d3d8_device_GetRasterStatus,
2830 d3d8_device_SetGammaRamp,
2831 d3d8_device_GetGammaRamp,
2832 d3d8_device_CreateTexture,
2833 d3d8_device_CreateVolumeTexture,
2834 d3d8_device_CreateCubeTexture,
2835 d3d8_device_CreateVertexBuffer,
2836 d3d8_device_CreateIndexBuffer,
2837 d3d8_device_CreateRenderTarget,
2838 d3d8_device_CreateDepthStencilSurface,
2839 d3d8_device_CreateImageSurface,
2840 d3d8_device_CopyRects,
2841 d3d8_device_UpdateTexture,
2842 d3d8_device_GetFrontBuffer,
2843 d3d8_device_SetRenderTarget,
2844 d3d8_device_GetRenderTarget,
2845 d3d8_device_GetDepthStencilSurface,
2846 d3d8_device_BeginScene,
2847 d3d8_device_EndScene,
2848 d3d8_device_Clear,
2849 d3d8_device_SetTransform,
2850 d3d8_device_GetTransform,
2851 d3d8_device_MultiplyTransform,
2852 d3d8_device_SetViewport,
2853 d3d8_device_GetViewport,
2854 d3d8_device_SetMaterial,
2855 d3d8_device_GetMaterial,
2856 d3d8_device_SetLight,
2857 d3d8_device_GetLight,
2858 d3d8_device_LightEnable,
2859 d3d8_device_GetLightEnable,
2860 d3d8_device_SetClipPlane,
2861 d3d8_device_GetClipPlane,
2862 d3d8_device_SetRenderState,
2863 d3d8_device_GetRenderState,
2864 d3d8_device_BeginStateBlock,
2865 d3d8_device_EndStateBlock,
2866 d3d8_device_ApplyStateBlock,
2867 d3d8_device_CaptureStateBlock,
2868 d3d8_device_DeleteStateBlock,
2869 d3d8_device_CreateStateBlock,
2870 d3d8_device_SetClipStatus,
2871 d3d8_device_GetClipStatus,
2872 d3d8_device_GetTexture,
2873 d3d8_device_SetTexture,
2874 d3d8_device_GetTextureStageState,
2875 d3d8_device_SetTextureStageState,
2876 d3d8_device_ValidateDevice,
2877 d3d8_device_GetInfo,
2878 d3d8_device_SetPaletteEntries,
2879 d3d8_device_GetPaletteEntries,
2880 d3d8_device_SetCurrentTexturePalette,
2881 d3d8_device_GetCurrentTexturePalette,
2882 d3d8_device_DrawPrimitive,
2883 d3d8_device_DrawIndexedPrimitive,
2884 d3d8_device_DrawPrimitiveUP,
2885 d3d8_device_DrawIndexedPrimitiveUP,
2886 d3d8_device_ProcessVertices,
2887 d3d8_device_CreateVertexShader,
2888 d3d8_device_SetVertexShader,
2889 d3d8_device_GetVertexShader,
2890 d3d8_device_DeleteVertexShader,
2891 d3d8_device_SetVertexShaderConstant,
2892 d3d8_device_GetVertexShaderConstant,
2893 d3d8_device_GetVertexShaderDeclaration,
2894 d3d8_device_GetVertexShaderFunction,
2895 d3d8_device_SetStreamSource,
2896 d3d8_device_GetStreamSource,
2897 d3d8_device_SetIndices,
2898 d3d8_device_GetIndices,
2899 d3d8_device_CreatePixelShader,
2900 d3d8_device_SetPixelShader,
2901 d3d8_device_GetPixelShader,
2902 d3d8_device_DeletePixelShader,
2903 d3d8_device_SetPixelShaderConstant,
2904 d3d8_device_GetPixelShaderConstant,
2905 d3d8_device_GetPixelShaderFunction,
2906 d3d8_device_DrawRectPatch,
2907 d3d8_device_DrawTriPatch,
2908 d3d8_device_DeletePatch,
2911 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2913 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2916 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2917 struct wined3d_device *device)
2919 TRACE("device_parent %p, device %p\n", device_parent, device);
2922 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2924 TRACE("device_parent %p.\n", device_parent);
2927 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2929 struct d3d8_device *device = device_from_device_parent(device_parent);
2931 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2933 if (!activate)
2934 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2935 else
2936 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2939 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2940 void *container_parent, struct wined3d_surface *surface, void **parent,
2941 const struct wined3d_parent_ops **parent_ops)
2943 struct d3d8_device *device = device_from_device_parent(device_parent);
2944 struct d3d8_surface *d3d_surface;
2946 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
2947 device_parent, container_parent, surface, parent, parent_ops);
2949 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2951 FIXME("Failed to allocate surface memory.\n");
2952 return D3DERR_OUTOFVIDEOMEMORY;
2955 surface_init(d3d_surface, surface, device, parent_ops);
2956 *parent = d3d_surface;
2957 TRACE("Created surface %p.\n", d3d_surface);
2959 d3d_surface->container = container_parent;
2960 IDirect3DDevice8_Release(d3d_surface->parent_device);
2961 d3d_surface->parent_device = NULL;
2963 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2964 d3d_surface->forwardReference = container_parent;
2966 return D3D_OK;
2969 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
2970 void *container_parent, struct wined3d_volume *volume, void **parent,
2971 const struct wined3d_parent_ops **parent_ops)
2973 struct d3d8_volume *d3d_volume;
2975 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
2976 device_parent, container_parent, volume, parent, parent_ops);
2978 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
2979 return E_OUTOFMEMORY;
2981 volume_init(d3d_volume, volume, parent_ops);
2982 *parent = d3d_volume;
2983 TRACE("Created volume %p.\n", d3d_volume);
2985 d3d_volume->container = container_parent;
2987 IDirect3DVolume8_Release(&d3d_volume->IDirect3DVolume8_iface);
2988 d3d_volume->forwardReference = container_parent;
2990 return D3D_OK;
2993 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
2994 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
2996 struct d3d8_device *device = device_from_device_parent(device_parent);
2997 struct wined3d_resource_desc texture_desc;
2998 struct d3d8_surface *d3d_surface;
2999 struct wined3d_texture *texture;
3000 HRESULT hr;
3002 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3003 device_parent, container_parent, desc, surface);
3005 texture_desc = *desc;
3006 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3007 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3008 WINED3D_SURFACE_MAPPABLE, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
3010 WARN("Failed to create texture, hr %#x.\n", hr);
3011 return hr;
3014 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3015 wined3d_surface_incref(*surface);
3016 wined3d_texture_decref(texture);
3018 d3d_surface = wined3d_surface_get_parent(*surface);
3019 d3d_surface->forwardReference = NULL;
3020 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3022 return hr;
3025 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3026 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3028 struct d3d8_device *device = device_from_device_parent(device_parent);
3029 struct d3d8_swapchain *d3d_swapchain;
3030 HRESULT hr;
3032 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3034 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3036 WARN("Failed to create swapchain, hr %#x.\n", hr);
3037 *swapchain = NULL;
3038 return hr;
3041 *swapchain = d3d_swapchain->wined3d_swapchain;
3042 wined3d_swapchain_incref(*swapchain);
3043 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3045 return hr;
3048 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3050 device_parent_wined3d_device_created,
3051 device_parent_mode_changed,
3052 device_parent_activate,
3053 device_parent_surface_created,
3054 device_parent_volume_created,
3055 device_parent_create_swapchain_surface,
3056 device_parent_create_swapchain,
3059 static void setup_fpu(void)
3061 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3062 WORD cw;
3063 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3064 cw = (cw & ~0xf3f) | 0x3f;
3065 __asm__ volatile ("fldcw %0" : : "m" (cw));
3066 #elif defined(__i386__) && defined(_MSC_VER)
3067 WORD cw;
3068 __asm fnstcw cw;
3069 cw = (cw & ~0xf3f) | 0x3f;
3070 __asm fldcw cw;
3071 #else
3072 FIXME("FPU setup not implemented for this platform.\n");
3073 #endif
3076 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3077 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3079 struct wined3d_swapchain_desc swapchain_desc;
3080 HRESULT hr;
3082 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3083 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3084 device->ref = 1;
3085 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3086 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3087 if (!device->handle_table.entries)
3089 ERR("Failed to allocate handle table memory.\n");
3090 return E_OUTOFMEMORY;
3092 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3094 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3096 wined3d_mutex_lock();
3097 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3098 &device->device_parent, &device->wined3d_device);
3099 if (FAILED(hr))
3101 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3102 wined3d_mutex_unlock();
3103 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3104 return hr;
3107 if (!parameters->Windowed)
3109 HWND device_window = parameters->hDeviceWindow;
3111 if (!focus_window)
3112 focus_window = device_window;
3113 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3115 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3116 wined3d_device_decref(device->wined3d_device);
3117 wined3d_mutex_unlock();
3118 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3119 return hr;
3122 if (!device_window)
3123 device_window = focus_window;
3124 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3125 parameters->BackBufferWidth,
3126 parameters->BackBufferHeight);
3129 if (flags & D3DCREATE_MULTITHREADED)
3130 wined3d_device_set_multithreaded(device->wined3d_device);
3132 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3134 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3135 if (FAILED(hr))
3137 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3138 wined3d_device_release_focus_window(device->wined3d_device);
3139 wined3d_device_decref(device->wined3d_device);
3140 wined3d_mutex_unlock();
3141 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3142 return hr;
3145 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3146 wined3d_mutex_unlock();
3148 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3150 device->declArraySize = 16;
3151 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3152 if (!device->decls)
3154 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3155 hr = E_OUTOFMEMORY;
3156 goto err;
3159 device->d3d_parent = &parent->IDirect3D8_iface;
3160 IDirect3D8_AddRef(device->d3d_parent);
3162 return D3D_OK;
3164 err:
3165 wined3d_mutex_lock();
3166 wined3d_device_uninit_3d(device->wined3d_device);
3167 wined3d_device_release_focus_window(device->wined3d_device);
3168 wined3d_device_decref(device->wined3d_device);
3169 wined3d_mutex_unlock();
3170 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3171 return hr;