wrc: Declare the data buffer as unsigned char.
[wine.git] / dlls / d3d8 / device.c
blob659cae603decb4a5bc387b0d316b55d6e97b9285
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
41 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
43 d3d8_null_wined3d_object_destroyed,
46 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
48 BYTE *c = (BYTE *)&format;
50 /* Don't translate FOURCC formats */
51 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
53 switch(format)
55 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
56 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
57 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
58 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
59 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
60 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
61 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
62 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
63 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
64 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
65 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
66 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
67 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
68 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
69 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
70 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
71 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
72 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
73 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
74 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
75 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
76 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
77 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
78 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
79 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
80 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
81 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
82 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
83 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
84 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
85 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
86 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
87 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
88 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
89 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
90 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
91 default:
92 FIXME("Unhandled wined3d format %#x.\n", format);
93 return D3DFMT_UNKNOWN;
97 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
99 BYTE *c = (BYTE *)&format;
101 /* Don't translate FOURCC formats */
102 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
104 switch(format)
106 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
107 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
108 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
109 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
110 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
111 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
112 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
113 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
114 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
115 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
116 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
117 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
118 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
119 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
120 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
131 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
132 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
133 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
134 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
135 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
136 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
137 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
138 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
139 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
140 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
141 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
142 default:
143 FIXME("Unhandled D3DFORMAT %#x\n", format);
144 return WINED3DFMT_UNKNOWN;
148 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
150 switch(primitive_type)
152 case D3DPT_POINTLIST:
153 return primitive_count;
155 case D3DPT_LINELIST:
156 return primitive_count * 2;
158 case D3DPT_LINESTRIP:
159 return primitive_count + 1;
161 case D3DPT_TRIANGLELIST:
162 return primitive_count * 3;
164 case D3DPT_TRIANGLESTRIP:
165 case D3DPT_TRIANGLEFAN:
166 return primitive_count + 2;
168 default:
169 FIXME("Unhandled primitive type %#x\n", primitive_type);
170 return 0;
174 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
175 const struct wined3d_swapchain_desc *swapchain_desc)
177 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
178 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
179 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
180 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
181 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
182 present_parameters->SwapEffect = swapchain_desc->swap_effect;
183 present_parameters->hDeviceWindow = swapchain_desc->device_window;
184 present_parameters->Windowed = swapchain_desc->windowed;
185 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
186 present_parameters->AutoDepthStencilFormat
187 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
188 present_parameters->Flags = swapchain_desc->flags;
189 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
190 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
193 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
194 const D3DPRESENT_PARAMETERS *present_parameters)
196 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
197 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
198 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
199 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
200 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
201 swapchain_desc->multisample_quality = 0; /* d3d9 only */
202 swapchain_desc->swap_effect = present_parameters->SwapEffect;
203 swapchain_desc->device_window = present_parameters->hDeviceWindow;
204 swapchain_desc->windowed = present_parameters->Windowed;
205 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
206 swapchain_desc->auto_depth_stencil_format
207 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
208 swapchain_desc->flags = present_parameters->Flags;
209 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
210 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
211 swapchain_desc->auto_restore_display_mode = TRUE;
214 /* Handle table functions */
215 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
217 struct d3d8_handle_entry *entry;
219 if (t->free_entries)
221 DWORD index = t->free_entries - t->entries;
222 /* Use a free handle */
223 entry = t->free_entries;
224 if (entry->type != D3D8_HANDLE_FREE)
226 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
227 return D3D8_INVALID_HANDLE;
229 t->free_entries = entry->object;
230 entry->object = object;
231 entry->type = type;
233 return index;
236 if (!(t->entry_count < t->table_size))
238 /* Grow the table */
239 UINT new_size = t->table_size + (t->table_size >> 1);
240 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
241 0, t->entries, new_size * sizeof(*t->entries));
242 if (!new_entries)
244 ERR("Failed to grow the handle table.\n");
245 return D3D8_INVALID_HANDLE;
247 t->entries = new_entries;
248 t->table_size = new_size;
251 entry = &t->entries[t->entry_count];
252 entry->object = object;
253 entry->type = type;
255 return t->entry_count++;
258 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
260 struct d3d8_handle_entry *entry;
261 void *object;
263 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
265 WARN("Invalid handle %u passed.\n", handle);
266 return NULL;
269 entry = &t->entries[handle];
270 if (entry->type != type)
272 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
273 return NULL;
276 object = entry->object;
277 entry->object = t->free_entries;
278 entry->type = D3D8_HANDLE_FREE;
279 t->free_entries = entry;
281 return object;
284 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
286 struct d3d8_handle_entry *entry;
288 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
290 WARN("Invalid handle %u passed.\n", handle);
291 return NULL;
294 entry = &t->entries[handle];
295 if (entry->type != type)
297 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
298 return NULL;
301 return entry->object;
304 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
306 TRACE("iface %p, riid %s, out %p.\n",
307 iface, debugstr_guid(riid), out);
309 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
310 || IsEqualGUID(riid, &IID_IUnknown))
312 IDirect3DDevice8_AddRef(iface);
313 *out = iface;
314 return S_OK;
317 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
319 *out = NULL;
320 return E_NOINTERFACE;
323 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
325 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
326 ULONG ref = InterlockedIncrement(&device->ref);
328 TRACE("%p increasing refcount to %u.\n", iface, ref);
330 return ref;
333 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
335 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
336 ULONG ref;
338 if (device->inDestruction)
339 return 0;
341 ref = InterlockedDecrement(&device->ref);
343 TRACE("%p decreasing refcount to %u.\n", iface, ref);
345 if (!ref)
347 IDirect3D8 *parent = device->d3d_parent;
348 unsigned i;
350 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
352 wined3d_mutex_lock();
354 device->inDestruction = TRUE;
356 for (i = 0; i < device->numConvertedDecls; ++i)
358 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
360 HeapFree(GetProcessHeap(), 0, device->decls);
362 if (device->vertex_buffer)
363 wined3d_buffer_decref(device->vertex_buffer);
364 if (device->index_buffer)
365 wined3d_buffer_decref(device->index_buffer);
367 wined3d_device_uninit_3d(device->wined3d_device);
368 wined3d_device_release_focus_window(device->wined3d_device);
369 wined3d_device_decref(device->wined3d_device);
370 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
371 HeapFree(GetProcessHeap(), 0, device);
373 wined3d_mutex_unlock();
375 IDirect3D8_Release(parent);
377 return ref;
380 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
382 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
384 TRACE("iface %p.\n", iface);
386 TRACE("device state: %#x.\n", device->device_state);
388 switch (device->device_state)
390 default:
391 case D3D8_DEVICE_STATE_OK:
392 return D3D_OK;
393 case D3D8_DEVICE_STATE_LOST:
394 return D3DERR_DEVICELOST;
395 case D3D8_DEVICE_STATE_NOT_RESET:
396 return D3DERR_DEVICENOTRESET;
400 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
402 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
403 UINT ret;
405 TRACE("iface %p.\n", iface);
407 wined3d_mutex_lock();
408 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
409 wined3d_mutex_unlock();
411 return ret;
414 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
418 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
420 if (byte_count)
421 FIXME("Byte count ignored.\n");
423 wined3d_mutex_lock();
424 wined3d_device_evict_managed_resources(device->wined3d_device);
425 wined3d_mutex_unlock();
427 return D3D_OK;
430 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
432 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
434 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
436 if (!d3d8)
437 return D3DERR_INVALIDCALL;
439 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
442 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
444 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
445 WINED3DCAPS *wined3d_caps;
446 HRESULT hr;
448 TRACE("iface %p, caps %p.\n", iface, caps);
450 if (!caps)
451 return D3DERR_INVALIDCALL;
453 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
454 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
456 wined3d_mutex_lock();
457 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
458 wined3d_mutex_unlock();
460 fixup_caps(wined3d_caps);
461 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
462 HeapFree(GetProcessHeap(), 0, wined3d_caps);
464 return hr;
467 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
469 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
470 struct wined3d_display_mode wined3d_mode;
471 HRESULT hr;
473 TRACE("iface %p, mode %p.\n", iface, mode);
475 wined3d_mutex_lock();
476 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
477 wined3d_mutex_unlock();
479 if (SUCCEEDED(hr))
481 mode->Width = wined3d_mode.width;
482 mode->Height = wined3d_mode.height;
483 mode->RefreshRate = wined3d_mode.refresh_rate;
484 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
487 return hr;
490 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
491 D3DDEVICE_CREATION_PARAMETERS *parameters)
493 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
495 TRACE("iface %p, parameters %p.\n", iface, parameters);
497 wined3d_mutex_lock();
498 wined3d_device_get_creation_parameters(device->wined3d_device,
499 (struct wined3d_device_creation_parameters *)parameters);
500 wined3d_mutex_unlock();
502 return D3D_OK;
505 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
506 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
508 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
509 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
510 HRESULT hr;
512 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
513 iface, hotspot_x, hotspot_y, bitmap);
515 if (!bitmap)
517 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
518 return D3DERR_INVALIDCALL;
521 wined3d_mutex_lock();
522 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
523 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
524 wined3d_mutex_unlock();
526 return hr;
529 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
531 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
533 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
535 wined3d_mutex_lock();
536 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
537 wined3d_mutex_unlock();
540 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
542 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
543 BOOL ret;
545 TRACE("iface %p, show %#x.\n", iface, show);
547 wined3d_mutex_lock();
548 ret = wined3d_device_show_cursor(device->wined3d_device, show);
549 wined3d_mutex_unlock();
551 return ret;
554 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
555 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
557 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
558 struct wined3d_swapchain_desc desc;
559 struct d3d8_swapchain *object;
560 UINT i, count;
561 HRESULT hr;
563 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
564 iface, present_parameters, swapchain);
566 if (!present_parameters->Windowed)
568 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
569 return D3DERR_INVALIDCALL;
572 wined3d_mutex_lock();
573 count = wined3d_device_get_swapchain_count(device->wined3d_device);
574 for (i = 0; i < count; ++i)
576 struct wined3d_swapchain *wined3d_swapchain;
578 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
579 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
581 if (!desc.windowed)
583 wined3d_mutex_unlock();
584 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
585 return D3DERR_INVALIDCALL;
588 wined3d_mutex_unlock();
590 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
591 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
592 *swapchain = &object->IDirect3DSwapChain8_iface;
593 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
595 return hr;
598 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
600 struct wined3d_resource_desc desc;
602 wined3d_resource_get_desc(resource, &desc);
603 if (desc.pool == WINED3D_POOL_DEFAULT)
605 struct d3d8_surface *surface;
607 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
609 IUnknown *parent = wined3d_resource_get_parent(resource);
610 IDirect3DBaseTexture8 *texture;
612 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
614 IDirect3DBaseTexture8_Release(texture);
615 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
616 return D3DERR_DEVICELOST;
619 return D3D_OK;
622 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
624 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
625 return D3DERR_DEVICELOST;
628 surface = wined3d_resource_get_parent(resource);
629 if (surface->resource.refcount)
631 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
632 return D3DERR_DEVICELOST;
635 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
638 return D3D_OK;
641 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
642 D3DPRESENT_PARAMETERS *present_parameters)
644 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
645 struct wined3d_swapchain_desc swapchain_desc;
646 HRESULT hr;
648 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
650 if (device->device_state == D3D8_DEVICE_STATE_LOST)
652 WARN("App not active, returning D3DERR_DEVICELOST.\n");
653 return D3DERR_DEVICELOST;
656 wined3d_mutex_lock();
658 if (device->vertex_buffer)
660 wined3d_buffer_decref(device->vertex_buffer);
661 device->vertex_buffer = NULL;
662 device->vertex_buffer_size = 0;
664 if (device->index_buffer)
666 wined3d_buffer_decref(device->index_buffer);
667 device->index_buffer = NULL;
668 device->index_buffer_size = 0;
671 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
672 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
673 NULL, reset_enum_callback, TRUE)))
675 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
676 device->device_state = D3D8_DEVICE_STATE_OK;
678 else
680 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
682 wined3d_mutex_unlock();
684 return hr;
687 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
688 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
690 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
691 HRESULT hr;
693 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
694 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
696 if (device->device_state != D3D8_DEVICE_STATE_OK)
697 return D3DERR_DEVICELOST;
699 wined3d_mutex_lock();
700 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
701 dst_window_override, dirty_region, 0);
702 wined3d_mutex_unlock();
704 return hr;
707 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
708 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
710 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
711 struct wined3d_surface *wined3d_surface = NULL;
712 struct d3d8_surface *surface_impl;
713 HRESULT hr;
715 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
716 iface, backbuffer_idx, backbuffer_type, backbuffer);
718 wined3d_mutex_lock();
719 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
720 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
721 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
723 surface_impl = wined3d_surface_get_parent(wined3d_surface);
724 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
725 IDirect3DSurface8_AddRef(*backbuffer);
727 wined3d_mutex_unlock();
729 return hr;
732 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
734 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
735 HRESULT hr;
737 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
739 wined3d_mutex_lock();
740 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
741 wined3d_mutex_unlock();
743 return hr;
746 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
748 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
750 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
752 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
753 wined3d_mutex_lock();
754 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
755 wined3d_mutex_unlock();
758 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
760 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
762 TRACE("iface %p, ramp %p.\n", iface, ramp);
764 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
765 wined3d_mutex_lock();
766 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
767 wined3d_mutex_unlock();
770 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
771 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
772 D3DPOOL pool, IDirect3DTexture8 **texture)
774 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
775 struct d3d8_texture *object;
776 HRESULT hr;
778 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
779 iface, width, height, levels, usage, format, pool, texture);
781 *texture = NULL;
782 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
783 if (!object)
784 return D3DERR_OUTOFVIDEOMEMORY;
786 hr = texture_init(object, device, width, height, levels, usage, format, pool);
787 if (FAILED(hr))
789 WARN("Failed to initialize texture, hr %#x.\n", hr);
790 HeapFree(GetProcessHeap(), 0, object);
791 return hr;
794 TRACE("Created texture %p.\n", object);
795 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
797 return D3D_OK;
800 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
801 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
802 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
804 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
805 struct d3d8_texture *object;
806 HRESULT hr;
808 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
809 iface, width, height, depth, levels, usage, format, pool, texture);
811 *texture = NULL;
812 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
813 if (!object)
814 return D3DERR_OUTOFVIDEOMEMORY;
816 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
817 if (FAILED(hr))
819 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
820 HeapFree(GetProcessHeap(), 0, object);
821 return hr;
824 TRACE("Created volume texture %p.\n", object);
825 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
827 return D3D_OK;
830 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
831 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
833 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
834 struct d3d8_texture *object;
835 HRESULT hr;
837 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
838 iface, edge_length, levels, usage, format, pool, texture);
840 *texture = NULL;
841 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
842 if (!object)
843 return D3DERR_OUTOFVIDEOMEMORY;
845 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
846 if (FAILED(hr))
848 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
849 HeapFree(GetProcessHeap(), 0, object);
850 return hr;
853 TRACE("Created cube texture %p.\n", object);
854 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
856 return hr;
859 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
860 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
862 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
863 struct d3d8_vertexbuffer *object;
864 HRESULT hr;
866 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
867 iface, size, usage, fvf, pool, buffer);
869 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
870 if (!object)
871 return D3DERR_OUTOFVIDEOMEMORY;
873 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
874 if (FAILED(hr))
876 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
877 HeapFree(GetProcessHeap(), 0, object);
878 return hr;
881 TRACE("Created vertex buffer %p.\n", object);
882 *buffer = &object->IDirect3DVertexBuffer8_iface;
884 return D3D_OK;
887 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
888 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
890 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
891 struct d3d8_indexbuffer *object;
892 HRESULT hr;
894 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
895 iface, size, usage, format, pool, buffer);
897 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
898 if (!object)
899 return D3DERR_OUTOFVIDEOMEMORY;
901 hr = indexbuffer_init(object, device, size, usage, format, pool);
902 if (FAILED(hr))
904 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
905 HeapFree(GetProcessHeap(), 0, object);
906 return hr;
909 TRACE("Created index buffer %p.\n", object);
910 *buffer = &object->IDirect3DIndexBuffer8_iface;
912 return D3D_OK;
915 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
916 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
917 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
919 struct wined3d_resource *sub_resource;
920 struct wined3d_resource_desc desc;
921 struct d3d8_surface *surface_impl;
922 struct wined3d_texture *texture;
923 HRESULT hr;
925 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
926 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
927 device, width, height, format, flags, surface,
928 usage, pool, multisample_type, multisample_quality);
930 desc.resource_type = WINED3D_RTYPE_TEXTURE;
931 desc.format = wined3dformat_from_d3dformat(format);
932 desc.multisample_type = multisample_type;
933 desc.multisample_quality = multisample_quality;
934 desc.usage = usage & WINED3DUSAGE_MASK;
935 desc.pool = pool;
936 desc.width = width;
937 desc.height = height;
938 desc.depth = 1;
939 desc.size = 0;
941 wined3d_mutex_lock();
943 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
944 1, flags, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
946 wined3d_mutex_unlock();
947 WARN("Failed to create texture, hr %#x.\n", hr);
948 return hr;
951 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
952 surface_impl = wined3d_resource_get_parent(sub_resource);
953 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
954 *surface = &surface_impl->IDirect3DSurface8_iface;
955 IDirect3DSurface8_AddRef(*surface);
956 wined3d_texture_decref(texture);
958 wined3d_mutex_unlock();
960 return D3D_OK;
963 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
964 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
965 IDirect3DSurface8 **surface)
967 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
968 DWORD flags = 0;
970 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
971 iface, width, height, format, multisample_type, lockable, surface);
973 *surface = NULL;
974 if (lockable)
975 flags |= WINED3D_SURFACE_MAPPABLE;
977 return d3d8_device_create_surface(device, width, height, format, flags, surface,
978 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
981 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
982 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
983 IDirect3DSurface8 **surface)
985 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
987 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
988 iface, width, height, format, multisample_type, surface);
990 *surface = NULL;
992 /* TODO: Verify that Discard is false */
993 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
994 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
997 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
998 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
999 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1001 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1003 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1004 iface, width, height, format, surface);
1006 *surface = NULL;
1008 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
1009 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1012 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1013 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1014 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1016 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1017 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1018 enum wined3d_format_id src_format, dst_format;
1019 struct wined3d_resource_desc wined3d_desc;
1020 struct wined3d_resource *wined3d_resource;
1021 UINT src_w, src_h;
1023 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1024 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1026 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1027 * destination texture is in WINED3D_POOL_DEFAULT. */
1029 wined3d_mutex_lock();
1030 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1031 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1032 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1034 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1035 wined3d_mutex_unlock();
1036 return D3DERR_INVALIDCALL;
1038 src_format = wined3d_desc.format;
1039 src_w = wined3d_desc.width;
1040 src_h = wined3d_desc.height;
1042 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1043 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1044 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1046 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1047 wined3d_mutex_unlock();
1048 return D3DERR_INVALIDCALL;
1050 dst_format = wined3d_desc.format;
1052 /* Check that the source and destination formats match */
1053 if (src_format != dst_format)
1055 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1056 src_surface, dst_surface);
1057 wined3d_mutex_unlock();
1058 return D3DERR_INVALIDCALL;
1061 /* Quick if complete copy ... */
1062 if (!rect_count && !src_rects && !dst_points)
1064 RECT rect = {0, 0, src_w, src_h};
1065 wined3d_surface_blt(dst->wined3d_surface, &rect,
1066 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
1068 else
1070 unsigned int i;
1071 /* Copy rect by rect */
1072 if (src_rects && dst_points)
1074 for (i = 0; i < rect_count; ++i)
1076 UINT w = src_rects[i].right - src_rects[i].left;
1077 UINT h = src_rects[i].bottom - src_rects[i].top;
1078 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1079 dst_points[i].x + w, dst_points[i].y + h};
1081 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1082 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1085 else
1087 for (i = 0; i < rect_count; ++i)
1089 UINT w = src_rects[i].right - src_rects[i].left;
1090 UINT h = src_rects[i].bottom - src_rects[i].top;
1091 RECT dst_rect = {0, 0, w, h};
1093 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1094 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1098 wined3d_mutex_unlock();
1100 return WINED3D_OK;
1103 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1104 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1106 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1107 struct d3d8_texture *src_impl, *dst_impl;
1108 HRESULT hr;
1110 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1112 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1113 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1115 wined3d_mutex_lock();
1116 hr = wined3d_device_update_texture(device->wined3d_device,
1117 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1118 wined3d_mutex_unlock();
1120 return hr;
1123 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1125 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1126 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1127 HRESULT hr;
1129 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1131 if (!dst_surface)
1133 WARN("Invalid destination surface passed.\n");
1134 return D3DERR_INVALIDCALL;
1137 wined3d_mutex_lock();
1138 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1139 wined3d_mutex_unlock();
1141 return hr;
1144 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1145 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1147 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1148 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1149 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1150 struct wined3d_rendertarget_view *original_dsv;
1151 HRESULT hr = D3D_OK;
1153 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1155 wined3d_mutex_lock();
1157 if (ds_impl)
1159 struct wined3d_rendertarget_view *original_rtv;
1160 struct wined3d_resource_desc ds_desc, rt_desc;
1161 struct wined3d_resource *wined3d_resource;
1162 struct d3d8_surface *original_surface;
1164 /* If no render target is passed in check the size against the current RT */
1165 if (!render_target)
1168 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1170 wined3d_mutex_unlock();
1171 return D3DERR_NOTFOUND;
1173 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1174 wined3d_resource = wined3d_surface_get_resource(original_surface->wined3d_surface);
1176 else
1177 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1178 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1180 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1181 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1183 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1185 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1186 wined3d_mutex_unlock();
1187 return D3DERR_INVALIDCALL;
1191 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1192 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1193 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1194 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1195 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1196 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1198 wined3d_mutex_unlock();
1200 return hr;
1203 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1205 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1206 struct wined3d_rendertarget_view *wined3d_rtv;
1207 struct d3d8_surface *surface_impl;
1208 HRESULT hr;
1210 TRACE("iface %p, render_target %p.\n", iface, render_target);
1212 if (!render_target)
1213 return D3DERR_INVALIDCALL;
1215 wined3d_mutex_lock();
1216 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1218 /* We want the sub resource parent here, since the view itself may be
1219 * internal to wined3d and may not have a parent. */
1220 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1221 *render_target = &surface_impl->IDirect3DSurface8_iface;
1222 IDirect3DSurface8_AddRef(*render_target);
1223 hr = D3D_OK;
1225 else
1227 ERR("Failed to get wined3d render target.\n");
1228 *render_target = NULL;
1229 hr = D3DERR_NOTFOUND;
1231 wined3d_mutex_unlock();
1233 return hr;
1236 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1238 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1239 struct wined3d_rendertarget_view *wined3d_dsv;
1240 struct d3d8_surface *surface_impl;
1241 HRESULT hr = D3D_OK;
1243 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1245 if (!depth_stencil)
1246 return D3DERR_INVALIDCALL;
1248 wined3d_mutex_lock();
1249 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1251 /* We want the sub resource parent here, since the view itself may be
1252 * internal to wined3d and may not have a parent. */
1253 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1254 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1255 IDirect3DSurface8_AddRef(*depth_stencil);
1257 else
1259 hr = WINED3DERR_NOTFOUND;
1260 *depth_stencil = NULL;
1262 wined3d_mutex_unlock();
1264 return hr;
1267 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1269 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1270 HRESULT hr;
1272 TRACE("iface %p.\n", iface);
1274 wined3d_mutex_lock();
1275 hr = wined3d_device_begin_scene(device->wined3d_device);
1276 wined3d_mutex_unlock();
1278 return hr;
1281 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1283 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1284 HRESULT hr;
1286 TRACE("iface %p.\n", iface);
1288 wined3d_mutex_lock();
1289 hr = wined3d_device_end_scene(device->wined3d_device);
1290 wined3d_mutex_unlock();
1292 return hr;
1295 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1296 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1298 const struct wined3d_color c =
1300 ((color >> 16) & 0xff) / 255.0f,
1301 ((color >> 8) & 0xff) / 255.0f,
1302 (color & 0xff) / 255.0f,
1303 ((color >> 24) & 0xff) / 255.0f,
1305 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1306 HRESULT hr;
1308 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1309 iface, rect_count, rects, flags, color, z, stencil);
1311 wined3d_mutex_lock();
1312 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1313 wined3d_mutex_unlock();
1315 return hr;
1318 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1319 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1321 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1323 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1325 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1326 wined3d_mutex_lock();
1327 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1328 wined3d_mutex_unlock();
1330 return D3D_OK;
1333 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1334 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1336 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1338 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1340 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1341 wined3d_mutex_lock();
1342 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1343 wined3d_mutex_unlock();
1345 return D3D_OK;
1348 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1349 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1351 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1353 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1355 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1356 wined3d_mutex_lock();
1357 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1358 wined3d_mutex_unlock();
1360 return D3D_OK;
1363 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1365 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1367 TRACE("iface %p, viewport %p.\n", iface, viewport);
1369 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1370 wined3d_mutex_lock();
1371 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1372 wined3d_mutex_unlock();
1374 return D3D_OK;
1377 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1379 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1381 TRACE("iface %p, viewport %p.\n", iface, viewport);
1383 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1384 wined3d_mutex_lock();
1385 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1386 wined3d_mutex_unlock();
1388 return D3D_OK;
1391 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1393 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1395 TRACE("iface %p, material %p.\n", iface, material);
1397 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1398 wined3d_mutex_lock();
1399 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1400 wined3d_mutex_unlock();
1402 return D3D_OK;
1405 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1407 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1409 TRACE("iface %p, material %p.\n", iface, material);
1411 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1412 wined3d_mutex_lock();
1413 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1414 wined3d_mutex_unlock();
1416 return D3D_OK;
1419 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1421 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1422 HRESULT hr;
1424 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1426 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1427 wined3d_mutex_lock();
1428 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1429 wined3d_mutex_unlock();
1431 return hr;
1434 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1436 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1437 HRESULT hr;
1439 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1441 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1442 wined3d_mutex_lock();
1443 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1444 wined3d_mutex_unlock();
1446 return hr;
1449 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1451 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1452 HRESULT hr;
1454 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1456 wined3d_mutex_lock();
1457 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1458 wined3d_mutex_unlock();
1460 return hr;
1463 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1465 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1466 HRESULT hr;
1468 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1470 wined3d_mutex_lock();
1471 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1472 wined3d_mutex_unlock();
1474 return hr;
1477 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1479 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1480 HRESULT hr;
1482 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1484 wined3d_mutex_lock();
1485 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1486 wined3d_mutex_unlock();
1488 return hr;
1491 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1493 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1494 HRESULT hr;
1496 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1498 wined3d_mutex_lock();
1499 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1500 wined3d_mutex_unlock();
1502 return hr;
1505 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1506 D3DRENDERSTATETYPE state, DWORD value)
1508 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1510 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1512 wined3d_mutex_lock();
1513 switch (state)
1515 case D3DRS_ZBIAS:
1516 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1517 break;
1519 default:
1520 wined3d_device_set_render_state(device->wined3d_device, state, value);
1522 wined3d_mutex_unlock();
1524 return D3D_OK;
1527 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1528 D3DRENDERSTATETYPE state, DWORD *value)
1530 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1532 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1534 wined3d_mutex_lock();
1535 switch (state)
1537 case D3DRS_ZBIAS:
1538 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1539 break;
1541 default:
1542 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1544 wined3d_mutex_unlock();
1546 return D3D_OK;
1549 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1551 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1552 HRESULT hr;
1554 TRACE("iface %p.\n", iface);
1556 wined3d_mutex_lock();
1557 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1558 wined3d_mutex_unlock();
1560 return hr;
1563 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1565 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1566 struct wined3d_stateblock *stateblock;
1567 HRESULT hr;
1569 TRACE("iface %p, token %p.\n", iface, token);
1571 /* Tell wineD3D to endstateblock before anything else (in case we run out
1572 * of memory later and cause locking problems)
1574 wined3d_mutex_lock();
1575 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1576 if (FAILED(hr))
1578 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1579 wined3d_mutex_unlock();
1580 return hr;
1583 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1584 wined3d_mutex_unlock();
1586 if (*token == D3D8_INVALID_HANDLE)
1588 ERR("Failed to create a handle\n");
1589 wined3d_mutex_lock();
1590 wined3d_stateblock_decref(stateblock);
1591 wined3d_mutex_unlock();
1592 return E_FAIL;
1594 ++*token;
1596 TRACE("Returning %#x (%p).\n", *token, stateblock);
1598 return hr;
1601 static HRESULT WINAPI d3d8_device_ApplyStateBlock(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 if (!token)
1609 return D3D_OK;
1611 wined3d_mutex_lock();
1612 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1613 if (!stateblock)
1615 WARN("Invalid handle (%#x) passed.\n", token);
1616 wined3d_mutex_unlock();
1617 return D3DERR_INVALIDCALL;
1619 wined3d_stateblock_apply(stateblock);
1620 wined3d_mutex_unlock();
1622 return D3D_OK;
1625 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1627 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1628 struct wined3d_stateblock *stateblock;
1630 TRACE("iface %p, token %#x.\n", iface, token);
1632 wined3d_mutex_lock();
1633 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1634 if (!stateblock)
1636 WARN("Invalid handle (%#x) passed.\n", token);
1637 wined3d_mutex_unlock();
1638 return D3DERR_INVALIDCALL;
1640 wined3d_stateblock_capture(stateblock);
1641 wined3d_mutex_unlock();
1643 return D3D_OK;
1646 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1648 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1649 struct wined3d_stateblock *stateblock;
1651 TRACE("iface %p, token %#x.\n", iface, token);
1653 wined3d_mutex_lock();
1654 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1656 if (!stateblock)
1658 WARN("Invalid handle (%#x) passed.\n", token);
1659 wined3d_mutex_unlock();
1660 return D3DERR_INVALIDCALL;
1663 if (wined3d_stateblock_decref(stateblock))
1665 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1667 wined3d_mutex_unlock();
1669 return D3D_OK;
1672 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1673 D3DSTATEBLOCKTYPE type, DWORD *handle)
1675 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1676 struct wined3d_stateblock *stateblock;
1677 HRESULT hr;
1679 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1681 if (type != D3DSBT_ALL
1682 && type != D3DSBT_PIXELSTATE
1683 && type != D3DSBT_VERTEXSTATE)
1685 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1686 return D3DERR_INVALIDCALL;
1689 wined3d_mutex_lock();
1690 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1691 if (FAILED(hr))
1693 wined3d_mutex_unlock();
1694 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1695 return hr;
1698 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1699 wined3d_mutex_unlock();
1701 if (*handle == D3D8_INVALID_HANDLE)
1703 ERR("Failed to allocate a handle.\n");
1704 wined3d_mutex_lock();
1705 wined3d_stateblock_decref(stateblock);
1706 wined3d_mutex_unlock();
1707 return E_FAIL;
1709 ++*handle;
1711 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1713 return hr;
1716 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1718 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1719 HRESULT hr;
1721 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1722 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1724 wined3d_mutex_lock();
1725 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1726 wined3d_mutex_unlock();
1728 return hr;
1731 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1733 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1734 HRESULT hr;
1736 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1738 wined3d_mutex_lock();
1739 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1740 wined3d_mutex_unlock();
1742 return hr;
1745 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1747 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1748 struct wined3d_texture *wined3d_texture;
1749 struct d3d8_texture *texture_impl;
1751 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1753 if (!texture)
1754 return D3DERR_INVALIDCALL;
1756 wined3d_mutex_lock();
1757 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1759 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1760 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1761 IDirect3DBaseTexture8_AddRef(*texture);
1763 else
1765 *texture = NULL;
1767 wined3d_mutex_unlock();
1769 return D3D_OK;
1772 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1774 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1775 struct d3d8_texture *texture_impl;
1776 HRESULT hr;
1778 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1780 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1782 wined3d_mutex_lock();
1783 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1784 texture_impl ? texture_impl->wined3d_texture : NULL);
1785 wined3d_mutex_unlock();
1787 return hr;
1790 static const struct tss_lookup
1792 BOOL sampler_state;
1793 enum wined3d_texture_stage_state state;
1795 tss_lookup[] =
1797 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1798 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1799 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1800 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1801 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1802 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1803 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1804 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1805 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1806 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1807 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1808 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1809 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1810 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1811 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1812 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1813 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1814 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1815 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1816 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1817 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1818 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1819 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1820 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1821 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1822 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1823 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1824 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1825 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1828 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1829 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1831 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1832 const struct tss_lookup *l;
1834 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1836 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1838 WARN("Invalid Type %#x passed.\n", Type);
1839 return D3D_OK;
1842 l = &tss_lookup[Type];
1844 wined3d_mutex_lock();
1845 if (l->sampler_state)
1846 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1847 else
1848 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1849 wined3d_mutex_unlock();
1851 return D3D_OK;
1854 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1855 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1857 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1858 const struct tss_lookup *l;
1860 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1862 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1864 WARN("Invalid type %#x passed.\n", type);
1865 return D3D_OK;
1868 l = &tss_lookup[type];
1870 wined3d_mutex_lock();
1871 if (l->sampler_state)
1872 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1873 else
1874 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1875 wined3d_mutex_unlock();
1877 return D3D_OK;
1880 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1882 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1883 HRESULT hr;
1885 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1887 wined3d_mutex_lock();
1888 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1889 wined3d_mutex_unlock();
1891 return hr;
1894 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1895 DWORD info_id, void *info, DWORD info_size)
1897 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1899 return D3D_OK;
1902 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1903 UINT palette_idx, const PALETTEENTRY *entries)
1905 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1907 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1908 * does not have a d3d8/9-style palette API */
1910 return D3D_OK;
1913 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1914 UINT palette_idx, PALETTEENTRY *entries)
1916 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1918 return D3DERR_INVALIDCALL;
1921 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1923 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1925 return D3D_OK;
1928 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1930 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1932 return D3DERR_INVALIDCALL;
1935 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1936 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1938 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1939 HRESULT hr;
1941 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1942 iface, primitive_type, start_vertex, primitive_count);
1944 wined3d_mutex_lock();
1945 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1946 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1947 vertex_count_from_primitive_count(primitive_type, primitive_count));
1948 wined3d_mutex_unlock();
1950 return hr;
1953 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1954 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1955 UINT start_idx, UINT primitive_count)
1957 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1958 HRESULT hr;
1960 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1961 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1963 wined3d_mutex_lock();
1964 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1965 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1966 vertex_count_from_primitive_count(primitive_type, primitive_count));
1967 wined3d_mutex_unlock();
1969 return hr;
1972 /* The caller is responsible for wined3d locking */
1973 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1975 HRESULT hr;
1977 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1979 UINT size = max(device->vertex_buffer_size * 2, min_size);
1980 struct wined3d_buffer *buffer;
1982 TRACE("Growing vertex buffer to %u bytes\n", size);
1984 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1985 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1986 if (FAILED(hr))
1988 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1989 return hr;
1992 if (device->vertex_buffer)
1993 wined3d_buffer_decref(device->vertex_buffer);
1995 device->vertex_buffer = buffer;
1996 device->vertex_buffer_size = size;
1997 device->vertex_buffer_pos = 0;
1999 return D3D_OK;
2002 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2003 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2004 UINT stride)
2006 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2007 HRESULT hr;
2008 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2009 UINT size = vtx_count * stride;
2010 UINT vb_pos, align;
2011 BYTE *buffer_data;
2013 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2014 iface, primitive_type, primitive_count, data, stride);
2016 if (!primitive_count)
2018 WARN("primitive_count is 0, returning D3D_OK\n");
2019 return D3D_OK;
2022 wined3d_mutex_lock();
2023 hr = d3d8_device_prepare_vertex_buffer(device, size);
2024 if (FAILED(hr))
2025 goto done;
2027 vb_pos = device->vertex_buffer_pos;
2028 align = vb_pos % stride;
2029 if (align) align = stride - align;
2030 if (vb_pos + size + align > device->vertex_buffer_size)
2031 vb_pos = 0;
2032 else
2033 vb_pos += align;
2035 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2036 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2037 if (FAILED(hr))
2038 goto done;
2039 memcpy(buffer_data, data, size);
2040 wined3d_buffer_unmap(device->vertex_buffer);
2041 device->vertex_buffer_pos = vb_pos + size;
2043 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2044 if (FAILED(hr))
2045 goto done;
2047 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2048 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2049 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2051 done:
2052 wined3d_mutex_unlock();
2053 return hr;
2056 /* The caller is responsible for wined3d locking */
2057 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2059 HRESULT hr;
2061 if (device->index_buffer_size < min_size || !device->index_buffer)
2063 UINT size = max(device->index_buffer_size * 2, min_size);
2064 struct wined3d_buffer *buffer;
2066 TRACE("Growing index buffer to %u bytes\n", size);
2068 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2069 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2070 if (FAILED(hr))
2072 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2073 return hr;
2076 if (device->index_buffer)
2077 wined3d_buffer_decref(device->index_buffer);
2079 device->index_buffer = buffer;
2080 device->index_buffer_size = size;
2081 device->index_buffer_pos = 0;
2083 return D3D_OK;
2086 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2087 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2088 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2089 const void *vertex_data, UINT vertex_stride)
2091 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2092 HRESULT hr;
2093 BYTE *buffer_data;
2095 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2096 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2097 UINT idx_size = idx_count * idx_fmt_size;
2098 UINT ib_pos;
2100 UINT vtx_size = vertex_count * vertex_stride;
2101 UINT vb_pos, align;
2103 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2104 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2105 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2106 index_data, index_format, vertex_data, vertex_stride);
2108 if (!primitive_count)
2110 WARN("primitive_count is 0, returning D3D_OK\n");
2111 return D3D_OK;
2114 wined3d_mutex_lock();
2116 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2117 if (FAILED(hr))
2118 goto done;
2120 vb_pos = device->vertex_buffer_pos;
2121 align = vb_pos % vertex_stride;
2122 if (align) align = vertex_stride - align;
2123 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2124 vb_pos = 0;
2125 else
2126 vb_pos += align;
2128 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2129 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2130 if (FAILED(hr))
2131 goto done;
2132 memcpy(buffer_data, vertex_data, vtx_size);
2133 wined3d_buffer_unmap(device->vertex_buffer);
2134 device->vertex_buffer_pos = vb_pos + vtx_size;
2136 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2137 if (FAILED(hr))
2138 goto done;
2140 ib_pos = device->index_buffer_pos;
2141 align = ib_pos % idx_fmt_size;
2142 if (align) align = idx_fmt_size - align;
2143 if (ib_pos + idx_size + align > device->index_buffer_size)
2144 ib_pos = 0;
2145 else
2146 ib_pos += align;
2148 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2149 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2150 if (FAILED(hr))
2151 goto done;
2152 memcpy(buffer_data, index_data, idx_size);
2153 wined3d_buffer_unmap(device->index_buffer);
2154 device->index_buffer_pos = ib_pos + idx_size;
2156 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2157 if (FAILED(hr))
2158 goto done;
2160 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2161 wined3dformat_from_d3dformat(index_format));
2162 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2164 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2165 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2167 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2168 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2169 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2171 done:
2172 wined3d_mutex_unlock();
2173 return hr;
2176 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2177 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2179 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2180 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2181 HRESULT hr;
2183 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2184 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2186 wined3d_mutex_lock();
2187 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2188 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2189 wined3d_mutex_unlock();
2191 return hr;
2194 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2195 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2197 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2198 struct d3d8_vertex_shader *object;
2199 DWORD shader_handle;
2200 DWORD handle;
2201 HRESULT hr;
2203 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2204 iface, declaration, byte_code, shader, usage);
2206 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2207 if (!object)
2209 *shader = 0;
2210 return E_OUTOFMEMORY;
2213 wined3d_mutex_lock();
2214 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2215 wined3d_mutex_unlock();
2216 if (handle == D3D8_INVALID_HANDLE)
2218 ERR("Failed to allocate vertex shader handle.\n");
2219 HeapFree(GetProcessHeap(), 0, object);
2220 *shader = 0;
2221 return E_OUTOFMEMORY;
2224 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2226 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2227 if (FAILED(hr))
2229 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2230 wined3d_mutex_lock();
2231 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2232 wined3d_mutex_unlock();
2233 HeapFree(GetProcessHeap(), 0, object);
2234 *shader = 0;
2235 return hr;
2238 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2239 *shader = shader_handle;
2241 return D3D_OK;
2244 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2246 struct d3d8_vertex_declaration *d3d8_declaration;
2247 struct FvfToDecl *convertedDecls = device->decls;
2248 int p, low, high; /* deliberately signed */
2249 HRESULT hr;
2251 TRACE("Searching for declaration for fvf %08x... ", fvf);
2253 low = 0;
2254 high = device->numConvertedDecls - 1;
2255 while (low <= high)
2257 p = (low + high) >> 1;
2258 TRACE("%d ", p);
2260 if (convertedDecls[p].fvf == fvf)
2262 TRACE("found %p\n", convertedDecls[p].declaration);
2263 return convertedDecls[p].declaration;
2266 if (convertedDecls[p].fvf < fvf)
2267 low = p + 1;
2268 else
2269 high = p - 1;
2271 TRACE("not found. Creating and inserting at position %d.\n", low);
2273 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2274 return NULL;
2276 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2278 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2279 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2280 return NULL;
2283 if (device->declArraySize == device->numConvertedDecls)
2285 UINT grow = device->declArraySize / 2;
2287 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2288 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2289 if (!convertedDecls)
2291 d3d8_vertex_declaration_destroy(d3d8_declaration);
2292 return NULL;
2294 device->decls = convertedDecls;
2295 device->declArraySize += grow;
2298 memmove(convertedDecls + low + 1, convertedDecls + low,
2299 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2300 convertedDecls[low].declaration = d3d8_declaration;
2301 convertedDecls[low].fvf = fvf;
2302 ++device->numConvertedDecls;
2304 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2306 return d3d8_declaration;
2309 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2311 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2312 struct d3d8_vertex_shader *shader_impl;
2314 TRACE("iface %p, shader %#x.\n", iface, shader);
2316 if (VS_HIGHESTFIXEDFXF >= shader)
2318 TRACE("Setting FVF, %#x\n", shader);
2320 wined3d_mutex_lock();
2321 wined3d_device_set_vertex_declaration(device->wined3d_device,
2322 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2323 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2324 wined3d_mutex_unlock();
2326 return D3D_OK;
2329 TRACE("Setting shader\n");
2331 wined3d_mutex_lock();
2332 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2334 WARN("Invalid handle (%#x) passed.\n", shader);
2335 wined3d_mutex_unlock();
2337 return D3DERR_INVALIDCALL;
2340 wined3d_device_set_vertex_declaration(device->wined3d_device,
2341 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2342 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2343 wined3d_mutex_unlock();
2345 return D3D_OK;
2348 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2350 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2351 struct wined3d_vertex_declaration *wined3d_declaration;
2352 struct d3d8_vertex_declaration *d3d8_declaration;
2354 TRACE("iface %p, shader %p.\n", iface, shader);
2356 wined3d_mutex_lock();
2357 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2359 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2360 *shader = d3d8_declaration->shader_handle;
2362 else
2364 *shader = 0;
2366 wined3d_mutex_unlock();
2368 TRACE("Returning %#x.\n", *shader);
2370 return D3D_OK;
2373 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2375 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2376 struct d3d8_vertex_shader *shader_impl;
2378 TRACE("iface %p, shader %#x.\n", iface, shader);
2380 wined3d_mutex_lock();
2381 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2383 WARN("Invalid handle (%#x) passed.\n", shader);
2384 wined3d_mutex_unlock();
2386 return D3DERR_INVALIDCALL;
2389 if (shader_impl->wined3d_shader
2390 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2391 IDirect3DDevice8_SetVertexShader(iface, 0);
2393 wined3d_mutex_unlock();
2395 d3d8_vertex_shader_destroy(shader_impl);
2397 return D3D_OK;
2400 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2401 DWORD start_register, const void *data, DWORD count)
2403 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2404 HRESULT hr;
2406 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2407 iface, start_register, data, count);
2409 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2411 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2412 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2413 return D3DERR_INVALIDCALL;
2416 wined3d_mutex_lock();
2417 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2418 wined3d_mutex_unlock();
2420 return hr;
2423 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2424 DWORD start_register, void *data, DWORD count)
2426 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2427 HRESULT hr;
2429 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2430 iface, start_register, data, count);
2432 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2434 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2435 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2436 return D3DERR_INVALIDCALL;
2439 wined3d_mutex_lock();
2440 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2441 wined3d_mutex_unlock();
2443 return hr;
2446 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2447 DWORD shader, void *data, DWORD *data_size)
2449 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2450 struct d3d8_vertex_declaration *declaration;
2451 struct d3d8_vertex_shader *shader_impl;
2453 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2454 iface, shader, data, data_size);
2456 wined3d_mutex_lock();
2457 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2458 wined3d_mutex_unlock();
2460 if (!shader_impl)
2462 WARN("Invalid handle (%#x) passed.\n", shader);
2463 return D3DERR_INVALIDCALL;
2465 declaration = shader_impl->vertex_declaration;
2467 if (!data)
2469 *data_size = declaration->elements_size;
2470 return D3D_OK;
2473 /* MSDN claims that if *data_size is smaller than the required size
2474 * we should write the required size and return D3DERR_MOREDATA.
2475 * That's not actually true. */
2476 if (*data_size < declaration->elements_size)
2477 return D3DERR_INVALIDCALL;
2479 memcpy(data, declaration->elements, declaration->elements_size);
2481 return D3D_OK;
2484 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2485 DWORD shader, void *data, DWORD *data_size)
2487 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2488 struct d3d8_vertex_shader *shader_impl = NULL;
2489 HRESULT hr;
2491 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2492 iface, shader, data, data_size);
2494 wined3d_mutex_lock();
2495 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2497 WARN("Invalid handle (%#x) passed.\n", shader);
2498 wined3d_mutex_unlock();
2500 return D3DERR_INVALIDCALL;
2503 if (!shader_impl->wined3d_shader)
2505 wined3d_mutex_unlock();
2506 *data_size = 0;
2507 return D3D_OK;
2510 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2511 wined3d_mutex_unlock();
2513 return hr;
2516 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2517 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2519 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2520 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2522 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2524 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2525 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2526 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2527 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2528 * problem)
2530 wined3d_mutex_lock();
2531 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2532 wined3d_device_set_index_buffer(device->wined3d_device,
2533 ib ? ib->wined3d_buffer : NULL,
2534 ib ? ib->format : WINED3DFMT_UNKNOWN);
2535 wined3d_mutex_unlock();
2537 return D3D_OK;
2540 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2541 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2543 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2544 enum wined3d_format_id wined3d_format;
2545 struct wined3d_buffer *wined3d_buffer;
2546 struct d3d8_indexbuffer *buffer_impl;
2548 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2550 if (!buffer)
2551 return D3DERR_INVALIDCALL;
2553 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2554 wined3d_mutex_lock();
2555 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2556 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2558 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2559 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2560 IDirect3DIndexBuffer8_AddRef(*buffer);
2562 else
2564 *buffer = NULL;
2566 wined3d_mutex_unlock();
2568 return D3D_OK;
2571 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2572 const DWORD *byte_code, DWORD *shader)
2574 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2575 struct d3d8_pixel_shader *object;
2576 DWORD shader_handle;
2577 DWORD handle;
2578 HRESULT hr;
2580 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2582 if (!shader)
2583 return D3DERR_INVALIDCALL;
2585 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2586 if (!object)
2587 return E_OUTOFMEMORY;
2589 wined3d_mutex_lock();
2590 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2591 wined3d_mutex_unlock();
2592 if (handle == D3D8_INVALID_HANDLE)
2594 ERR("Failed to allocate pixel shader handle.\n");
2595 HeapFree(GetProcessHeap(), 0, object);
2596 return E_OUTOFMEMORY;
2599 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2601 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2602 if (FAILED(hr))
2604 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2605 wined3d_mutex_lock();
2606 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2607 wined3d_mutex_unlock();
2608 HeapFree(GetProcessHeap(), 0, object);
2609 *shader = 0;
2610 return hr;
2613 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2614 *shader = shader_handle;
2616 return D3D_OK;
2619 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2621 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2622 struct d3d8_pixel_shader *shader_impl;
2624 TRACE("iface %p, shader %#x.\n", iface, shader);
2626 wined3d_mutex_lock();
2628 if (!shader)
2630 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2631 wined3d_mutex_unlock();
2632 return D3D_OK;
2635 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2637 WARN("Invalid handle (%#x) passed.\n", shader);
2638 wined3d_mutex_unlock();
2639 return D3DERR_INVALIDCALL;
2642 TRACE("Setting shader %p.\n", shader_impl);
2643 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2644 wined3d_mutex_unlock();
2646 return D3D_OK;
2649 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2652 struct wined3d_shader *object;
2654 TRACE("iface %p, shader %p.\n", iface, shader);
2656 if (!shader)
2657 return D3DERR_INVALIDCALL;
2659 wined3d_mutex_lock();
2660 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2662 struct d3d8_pixel_shader *d3d8_shader;
2663 d3d8_shader = wined3d_shader_get_parent(object);
2664 *shader = d3d8_shader->handle;
2666 else
2668 *shader = 0;
2670 wined3d_mutex_unlock();
2672 TRACE("Returning %#x.\n", *shader);
2674 return D3D_OK;
2677 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2679 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2680 struct d3d8_pixel_shader *shader_impl;
2682 TRACE("iface %p, shader %#x.\n", iface, shader);
2684 wined3d_mutex_lock();
2686 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2688 WARN("Invalid handle (%#x) passed.\n", shader);
2689 wined3d_mutex_unlock();
2690 return D3DERR_INVALIDCALL;
2693 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2694 IDirect3DDevice8_SetPixelShader(iface, 0);
2696 wined3d_mutex_unlock();
2698 d3d8_pixel_shader_destroy(shader_impl);
2700 return D3D_OK;
2703 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2704 DWORD start_register, const void *data, DWORD count)
2706 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2707 HRESULT hr;
2709 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2710 iface, start_register, data, count);
2712 wined3d_mutex_lock();
2713 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2714 wined3d_mutex_unlock();
2716 return hr;
2719 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2720 DWORD start_register, void *data, DWORD count)
2722 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2723 HRESULT hr;
2725 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2726 iface, start_register, data, count);
2728 wined3d_mutex_lock();
2729 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2730 wined3d_mutex_unlock();
2732 return hr;
2735 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2736 DWORD shader, void *data, DWORD *data_size)
2738 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2739 struct d3d8_pixel_shader *shader_impl = NULL;
2740 HRESULT hr;
2742 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2743 iface, shader, data, data_size);
2745 wined3d_mutex_lock();
2746 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2748 WARN("Invalid handle (%#x) passed.\n", shader);
2749 wined3d_mutex_unlock();
2751 return D3DERR_INVALIDCALL;
2754 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2755 wined3d_mutex_unlock();
2757 return hr;
2760 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2761 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2763 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2764 iface, handle, segment_count, patch_info);
2765 return D3D_OK;
2768 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2769 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2771 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2772 iface, handle, segment_count, patch_info);
2773 return D3D_OK;
2776 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2778 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2779 return D3DERR_INVALIDCALL;
2782 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2783 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2785 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2786 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2787 HRESULT hr;
2789 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2790 iface, stream_idx, buffer, stride);
2792 wined3d_mutex_lock();
2793 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2794 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2795 wined3d_mutex_unlock();
2797 return hr;
2800 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2801 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2803 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2804 struct d3d8_vertexbuffer *buffer_impl;
2805 struct wined3d_buffer *wined3d_buffer = NULL;
2806 HRESULT hr;
2808 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2809 iface, stream_idx, buffer, stride);
2811 if (!buffer)
2812 return D3DERR_INVALIDCALL;
2814 wined3d_mutex_lock();
2815 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2816 if (SUCCEEDED(hr) && wined3d_buffer)
2818 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2819 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2820 IDirect3DVertexBuffer8_AddRef(*buffer);
2822 else
2824 if (FAILED(hr))
2825 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2826 *buffer = NULL;
2828 wined3d_mutex_unlock();
2830 return hr;
2833 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2835 d3d8_device_QueryInterface,
2836 d3d8_device_AddRef,
2837 d3d8_device_Release,
2838 d3d8_device_TestCooperativeLevel,
2839 d3d8_device_GetAvailableTextureMem,
2840 d3d8_device_ResourceManagerDiscardBytes,
2841 d3d8_device_GetDirect3D,
2842 d3d8_device_GetDeviceCaps,
2843 d3d8_device_GetDisplayMode,
2844 d3d8_device_GetCreationParameters,
2845 d3d8_device_SetCursorProperties,
2846 d3d8_device_SetCursorPosition,
2847 d3d8_device_ShowCursor,
2848 d3d8_device_CreateAdditionalSwapChain,
2849 d3d8_device_Reset,
2850 d3d8_device_Present,
2851 d3d8_device_GetBackBuffer,
2852 d3d8_device_GetRasterStatus,
2853 d3d8_device_SetGammaRamp,
2854 d3d8_device_GetGammaRamp,
2855 d3d8_device_CreateTexture,
2856 d3d8_device_CreateVolumeTexture,
2857 d3d8_device_CreateCubeTexture,
2858 d3d8_device_CreateVertexBuffer,
2859 d3d8_device_CreateIndexBuffer,
2860 d3d8_device_CreateRenderTarget,
2861 d3d8_device_CreateDepthStencilSurface,
2862 d3d8_device_CreateImageSurface,
2863 d3d8_device_CopyRects,
2864 d3d8_device_UpdateTexture,
2865 d3d8_device_GetFrontBuffer,
2866 d3d8_device_SetRenderTarget,
2867 d3d8_device_GetRenderTarget,
2868 d3d8_device_GetDepthStencilSurface,
2869 d3d8_device_BeginScene,
2870 d3d8_device_EndScene,
2871 d3d8_device_Clear,
2872 d3d8_device_SetTransform,
2873 d3d8_device_GetTransform,
2874 d3d8_device_MultiplyTransform,
2875 d3d8_device_SetViewport,
2876 d3d8_device_GetViewport,
2877 d3d8_device_SetMaterial,
2878 d3d8_device_GetMaterial,
2879 d3d8_device_SetLight,
2880 d3d8_device_GetLight,
2881 d3d8_device_LightEnable,
2882 d3d8_device_GetLightEnable,
2883 d3d8_device_SetClipPlane,
2884 d3d8_device_GetClipPlane,
2885 d3d8_device_SetRenderState,
2886 d3d8_device_GetRenderState,
2887 d3d8_device_BeginStateBlock,
2888 d3d8_device_EndStateBlock,
2889 d3d8_device_ApplyStateBlock,
2890 d3d8_device_CaptureStateBlock,
2891 d3d8_device_DeleteStateBlock,
2892 d3d8_device_CreateStateBlock,
2893 d3d8_device_SetClipStatus,
2894 d3d8_device_GetClipStatus,
2895 d3d8_device_GetTexture,
2896 d3d8_device_SetTexture,
2897 d3d8_device_GetTextureStageState,
2898 d3d8_device_SetTextureStageState,
2899 d3d8_device_ValidateDevice,
2900 d3d8_device_GetInfo,
2901 d3d8_device_SetPaletteEntries,
2902 d3d8_device_GetPaletteEntries,
2903 d3d8_device_SetCurrentTexturePalette,
2904 d3d8_device_GetCurrentTexturePalette,
2905 d3d8_device_DrawPrimitive,
2906 d3d8_device_DrawIndexedPrimitive,
2907 d3d8_device_DrawPrimitiveUP,
2908 d3d8_device_DrawIndexedPrimitiveUP,
2909 d3d8_device_ProcessVertices,
2910 d3d8_device_CreateVertexShader,
2911 d3d8_device_SetVertexShader,
2912 d3d8_device_GetVertexShader,
2913 d3d8_device_DeleteVertexShader,
2914 d3d8_device_SetVertexShaderConstant,
2915 d3d8_device_GetVertexShaderConstant,
2916 d3d8_device_GetVertexShaderDeclaration,
2917 d3d8_device_GetVertexShaderFunction,
2918 d3d8_device_SetStreamSource,
2919 d3d8_device_GetStreamSource,
2920 d3d8_device_SetIndices,
2921 d3d8_device_GetIndices,
2922 d3d8_device_CreatePixelShader,
2923 d3d8_device_SetPixelShader,
2924 d3d8_device_GetPixelShader,
2925 d3d8_device_DeletePixelShader,
2926 d3d8_device_SetPixelShaderConstant,
2927 d3d8_device_GetPixelShaderConstant,
2928 d3d8_device_GetPixelShaderFunction,
2929 d3d8_device_DrawRectPatch,
2930 d3d8_device_DrawTriPatch,
2931 d3d8_device_DeletePatch,
2934 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2936 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2939 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2940 struct wined3d_device *device)
2942 TRACE("device_parent %p, device %p\n", device_parent, device);
2945 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2947 TRACE("device_parent %p.\n", device_parent);
2950 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2952 struct d3d8_device *device = device_from_device_parent(device_parent);
2954 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2956 if (!activate)
2957 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2958 else
2959 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2962 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2963 void *container_parent, struct wined3d_surface *surface, void **parent,
2964 const struct wined3d_parent_ops **parent_ops)
2966 struct d3d8_surface *d3d_surface;
2968 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
2969 device_parent, container_parent, surface, parent, parent_ops);
2971 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2972 return E_OUTOFMEMORY;
2974 surface_init(d3d_surface, container_parent, surface, parent_ops);
2975 *parent = d3d_surface;
2976 TRACE("Created surface %p.\n", d3d_surface);
2978 return D3D_OK;
2981 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
2982 void *container_parent, struct wined3d_volume *volume, void **parent,
2983 const struct wined3d_parent_ops **parent_ops)
2985 struct d3d8_volume *d3d_volume;
2987 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
2988 device_parent, container_parent, volume, parent, parent_ops);
2990 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
2991 return E_OUTOFMEMORY;
2993 volume_init(d3d_volume, container_parent, volume, parent_ops);
2994 *parent = d3d_volume;
2995 TRACE("Created volume %p.\n", d3d_volume);
2997 return D3D_OK;
3000 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3001 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3003 struct d3d8_device *device = device_from_device_parent(device_parent);
3004 struct wined3d_resource_desc texture_desc;
3005 struct d3d8_surface *d3d_surface;
3006 struct wined3d_texture *texture;
3007 HRESULT hr;
3009 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3010 device_parent, container_parent, desc, surface);
3012 texture_desc = *desc;
3013 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3014 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3015 WINED3D_SURFACE_MAPPABLE, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
3017 WARN("Failed to create texture, hr %#x.\n", hr);
3018 return hr;
3021 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3022 wined3d_surface_incref(*surface);
3023 wined3d_texture_decref(texture);
3025 d3d_surface = wined3d_surface_get_parent(*surface);
3026 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3028 return hr;
3031 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3032 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3034 struct d3d8_device *device = device_from_device_parent(device_parent);
3035 struct d3d8_swapchain *d3d_swapchain;
3036 HRESULT hr;
3038 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3040 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3042 WARN("Failed to create swapchain, hr %#x.\n", hr);
3043 *swapchain = NULL;
3044 return hr;
3047 *swapchain = d3d_swapchain->wined3d_swapchain;
3048 wined3d_swapchain_incref(*swapchain);
3049 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3051 return hr;
3054 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3056 device_parent_wined3d_device_created,
3057 device_parent_mode_changed,
3058 device_parent_activate,
3059 device_parent_surface_created,
3060 device_parent_volume_created,
3061 device_parent_create_swapchain_surface,
3062 device_parent_create_swapchain,
3065 static void setup_fpu(void)
3067 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3068 WORD cw;
3069 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3070 cw = (cw & ~0xf3f) | 0x3f;
3071 __asm__ volatile ("fldcw %0" : : "m" (cw));
3072 #elif defined(__i386__) && defined(_MSC_VER)
3073 WORD cw;
3074 __asm fnstcw cw;
3075 cw = (cw & ~0xf3f) | 0x3f;
3076 __asm fldcw cw;
3077 #else
3078 FIXME("FPU setup not implemented for this platform.\n");
3079 #endif
3082 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3083 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3085 struct wined3d_swapchain_desc swapchain_desc;
3086 HRESULT hr;
3088 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3089 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3090 device->ref = 1;
3091 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3092 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3093 if (!device->handle_table.entries)
3095 ERR("Failed to allocate handle table memory.\n");
3096 return E_OUTOFMEMORY;
3098 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3100 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3102 wined3d_mutex_lock();
3103 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3104 &device->device_parent, &device->wined3d_device);
3105 if (FAILED(hr))
3107 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3108 wined3d_mutex_unlock();
3109 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3110 return hr;
3113 if (!parameters->Windowed)
3115 HWND device_window = parameters->hDeviceWindow;
3117 if (!focus_window)
3118 focus_window = device_window;
3119 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3121 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3122 wined3d_device_decref(device->wined3d_device);
3123 wined3d_mutex_unlock();
3124 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3125 return hr;
3128 if (!device_window)
3129 device_window = focus_window;
3130 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3131 parameters->BackBufferWidth,
3132 parameters->BackBufferHeight);
3135 if (flags & D3DCREATE_MULTITHREADED)
3136 wined3d_device_set_multithreaded(device->wined3d_device);
3138 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3140 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3141 if (FAILED(hr))
3143 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3144 wined3d_device_release_focus_window(device->wined3d_device);
3145 wined3d_device_decref(device->wined3d_device);
3146 wined3d_mutex_unlock();
3147 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3148 return hr;
3151 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3152 wined3d_mutex_unlock();
3154 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3156 device->declArraySize = 16;
3157 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3158 if (!device->decls)
3160 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3161 hr = E_OUTOFMEMORY;
3162 goto err;
3165 device->d3d_parent = &parent->IDirect3D8_iface;
3166 IDirect3D8_AddRef(device->d3d_parent);
3168 return D3D_OK;
3170 err:
3171 wined3d_mutex_lock();
3172 wined3d_device_uninit_3d(device->wined3d_device);
3173 wined3d_device_release_focus_window(device->wined3d_device);
3174 wined3d_device_decref(device->wined3d_device);
3175 wined3d_mutex_unlock();
3176 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3177 return hr;