d3dx9_30: Add stub for D3DXCpuOptimizations.
[wine.git] / dlls / d3d8 / device.c
blobdd0b9ee2e01f1992dab01a58094240d99dd15b07
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 HRESULT hr;
562 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
563 iface, present_parameters, swapchain);
565 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
566 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
567 *swapchain = &object->IDirect3DSwapChain8_iface;
568 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
570 return hr;
573 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
575 struct wined3d_resource_desc desc;
577 wined3d_resource_get_desc(resource, &desc);
578 if (desc.pool == WINED3D_POOL_DEFAULT)
580 struct d3d8_surface *surface;
582 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
584 IUnknown *parent = wined3d_resource_get_parent(resource);
585 IDirect3DBaseTexture8 *texture;
587 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
589 IDirect3DBaseTexture8_Release(texture);
590 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
591 return D3DERR_DEVICELOST;
594 return D3D_OK;
597 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
599 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
600 return D3DERR_DEVICELOST;
603 surface = wined3d_resource_get_parent(resource);
604 if (surface->resource.refcount)
606 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
607 return D3DERR_DEVICELOST;
610 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
613 return D3D_OK;
616 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
617 D3DPRESENT_PARAMETERS *present_parameters)
619 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
620 struct wined3d_swapchain_desc swapchain_desc;
621 HRESULT hr;
623 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
625 wined3d_mutex_lock();
627 if (device->vertex_buffer)
629 wined3d_buffer_decref(device->vertex_buffer);
630 device->vertex_buffer = NULL;
631 device->vertex_buffer_size = 0;
633 if (device->index_buffer)
635 wined3d_buffer_decref(device->index_buffer);
636 device->index_buffer = NULL;
637 device->index_buffer_size = 0;
640 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
641 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
642 NULL, reset_enum_callback, TRUE)))
644 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
645 device->device_state = D3D8_DEVICE_STATE_OK;
647 else
649 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
651 wined3d_mutex_unlock();
653 return hr;
656 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
657 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
660 HRESULT hr;
662 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
663 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
665 if (device->device_state != D3D8_DEVICE_STATE_OK)
666 return D3DERR_DEVICELOST;
668 wined3d_mutex_lock();
669 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
670 dst_window_override, dirty_region, 0);
671 wined3d_mutex_unlock();
673 return hr;
676 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
677 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
679 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
680 struct wined3d_surface *wined3d_surface = NULL;
681 struct d3d8_surface *surface_impl;
682 HRESULT hr;
684 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
685 iface, backbuffer_idx, backbuffer_type, backbuffer);
687 wined3d_mutex_lock();
688 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
689 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
690 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
692 surface_impl = wined3d_surface_get_parent(wined3d_surface);
693 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
694 IDirect3DSurface8_AddRef(*backbuffer);
696 wined3d_mutex_unlock();
698 return hr;
701 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
703 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
704 HRESULT hr;
706 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
708 wined3d_mutex_lock();
709 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
710 wined3d_mutex_unlock();
712 return hr;
715 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
719 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
721 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
722 wined3d_mutex_lock();
723 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
724 wined3d_mutex_unlock();
727 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
729 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
731 TRACE("iface %p, ramp %p.\n", iface, ramp);
733 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
734 wined3d_mutex_lock();
735 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
736 wined3d_mutex_unlock();
739 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
740 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
741 D3DPOOL pool, IDirect3DTexture8 **texture)
743 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
744 struct d3d8_texture *object;
745 HRESULT hr;
747 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
748 iface, width, height, levels, usage, format, pool, texture);
750 *texture = NULL;
751 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
752 if (!object)
753 return D3DERR_OUTOFVIDEOMEMORY;
755 hr = texture_init(object, device, width, height, levels, usage, format, pool);
756 if (FAILED(hr))
758 WARN("Failed to initialize texture, hr %#x.\n", hr);
759 HeapFree(GetProcessHeap(), 0, object);
760 return hr;
763 TRACE("Created texture %p.\n", object);
764 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
766 return D3D_OK;
769 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
770 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
771 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
774 struct d3d8_texture *object;
775 HRESULT hr;
777 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
778 iface, width, height, depth, levels, usage, format, pool, texture);
780 *texture = NULL;
781 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
782 if (!object)
783 return D3DERR_OUTOFVIDEOMEMORY;
785 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
786 if (FAILED(hr))
788 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
789 HeapFree(GetProcessHeap(), 0, object);
790 return hr;
793 TRACE("Created volume texture %p.\n", object);
794 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
796 return D3D_OK;
799 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
800 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
802 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
803 struct d3d8_texture *object;
804 HRESULT hr;
806 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
807 iface, edge_length, levels, usage, format, pool, texture);
809 *texture = NULL;
810 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
811 if (!object)
812 return D3DERR_OUTOFVIDEOMEMORY;
814 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
815 if (FAILED(hr))
817 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
818 HeapFree(GetProcessHeap(), 0, object);
819 return hr;
822 TRACE("Created cube texture %p.\n", object);
823 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
825 return hr;
828 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
829 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
831 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
832 struct d3d8_vertexbuffer *object;
833 HRESULT hr;
835 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
836 iface, size, usage, fvf, pool, buffer);
838 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
839 if (!object)
840 return D3DERR_OUTOFVIDEOMEMORY;
842 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
843 if (FAILED(hr))
845 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
846 HeapFree(GetProcessHeap(), 0, object);
847 return hr;
850 TRACE("Created vertex buffer %p.\n", object);
851 *buffer = &object->IDirect3DVertexBuffer8_iface;
853 return D3D_OK;
856 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
857 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
859 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
860 struct d3d8_indexbuffer *object;
861 HRESULT hr;
863 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
864 iface, size, usage, format, pool, buffer);
866 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
867 if (!object)
868 return D3DERR_OUTOFVIDEOMEMORY;
870 hr = indexbuffer_init(object, device, size, usage, format, pool);
871 if (FAILED(hr))
873 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
874 HeapFree(GetProcessHeap(), 0, object);
875 return hr;
878 TRACE("Created index buffer %p.\n", object);
879 *buffer = &object->IDirect3DIndexBuffer8_iface;
881 return D3D_OK;
884 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
885 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
886 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
888 struct wined3d_resource *sub_resource;
889 struct wined3d_resource_desc desc;
890 struct d3d8_surface *surface_impl;
891 struct wined3d_texture *texture;
892 HRESULT hr;
894 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
895 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
896 device, width, height, format, flags, surface,
897 usage, pool, multisample_type, multisample_quality);
899 desc.resource_type = WINED3D_RTYPE_TEXTURE;
900 desc.format = wined3dformat_from_d3dformat(format);
901 desc.multisample_type = multisample_type;
902 desc.multisample_quality = multisample_quality;
903 desc.usage = usage & WINED3DUSAGE_MASK;
904 desc.pool = pool;
905 desc.width = width;
906 desc.height = height;
907 desc.depth = 1;
908 desc.size = 0;
910 wined3d_mutex_lock();
912 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
913 1, flags, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
915 wined3d_mutex_unlock();
916 WARN("Failed to create texture, hr %#x.\n", hr);
917 return hr;
920 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
921 surface_impl = wined3d_resource_get_parent(sub_resource);
922 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
923 *surface = &surface_impl->IDirect3DSurface8_iface;
924 IDirect3DSurface8_AddRef(*surface);
925 wined3d_texture_decref(texture);
927 wined3d_mutex_unlock();
929 return D3D_OK;
932 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
933 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
934 IDirect3DSurface8 **surface)
936 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
937 DWORD flags = 0;
939 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
940 iface, width, height, format, multisample_type, lockable, surface);
942 *surface = NULL;
943 if (lockable)
944 flags |= WINED3D_SURFACE_MAPPABLE;
946 return d3d8_device_create_surface(device, width, height, format, flags, surface,
947 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
950 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
951 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
952 IDirect3DSurface8 **surface)
954 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
956 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
957 iface, width, height, format, multisample_type, surface);
959 *surface = NULL;
961 /* TODO: Verify that Discard is false */
962 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
963 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
966 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
967 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
968 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
970 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
972 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
973 iface, width, height, format, surface);
975 *surface = NULL;
977 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
978 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
981 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
982 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
983 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
985 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
986 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
987 enum wined3d_format_id src_format, dst_format;
988 struct wined3d_resource_desc wined3d_desc;
989 struct wined3d_resource *wined3d_resource;
990 UINT src_w, src_h;
992 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
993 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
995 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
996 * destination texture is in WINED3D_POOL_DEFAULT. */
998 wined3d_mutex_lock();
999 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1000 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1001 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1003 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1004 wined3d_mutex_unlock();
1005 return D3DERR_INVALIDCALL;
1007 src_format = wined3d_desc.format;
1008 src_w = wined3d_desc.width;
1009 src_h = wined3d_desc.height;
1011 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1012 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1013 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1015 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1016 wined3d_mutex_unlock();
1017 return D3DERR_INVALIDCALL;
1019 dst_format = wined3d_desc.format;
1021 /* Check that the source and destination formats match */
1022 if (src_format != dst_format)
1024 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1025 src_surface, dst_surface);
1026 wined3d_mutex_unlock();
1027 return D3DERR_INVALIDCALL;
1030 /* Quick if complete copy ... */
1031 if (!rect_count && !src_rects && !dst_points)
1033 RECT rect = {0, 0, src_w, src_h};
1034 wined3d_surface_blt(dst->wined3d_surface, &rect,
1035 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
1037 else
1039 unsigned int i;
1040 /* Copy rect by rect */
1041 if (src_rects && dst_points)
1043 for (i = 0; i < rect_count; ++i)
1045 UINT w = src_rects[i].right - src_rects[i].left;
1046 UINT h = src_rects[i].bottom - src_rects[i].top;
1047 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1048 dst_points[i].x + w, dst_points[i].y + h};
1050 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1051 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1054 else
1056 for (i = 0; i < rect_count; ++i)
1058 UINT w = src_rects[i].right - src_rects[i].left;
1059 UINT h = src_rects[i].bottom - src_rects[i].top;
1060 RECT dst_rect = {0, 0, w, h};
1062 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1063 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1067 wined3d_mutex_unlock();
1069 return WINED3D_OK;
1072 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1073 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1075 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1076 struct d3d8_texture *src_impl, *dst_impl;
1077 HRESULT hr;
1079 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1081 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1082 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1084 wined3d_mutex_lock();
1085 hr = wined3d_device_update_texture(device->wined3d_device,
1086 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1087 wined3d_mutex_unlock();
1089 return hr;
1092 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1094 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1095 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1096 HRESULT hr;
1098 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1100 if (!dst_surface)
1102 WARN("Invalid destination surface passed.\n");
1103 return D3DERR_INVALIDCALL;
1106 wined3d_mutex_lock();
1107 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1108 wined3d_mutex_unlock();
1110 return hr;
1113 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1114 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1116 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1117 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1118 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1119 struct wined3d_rendertarget_view *original_dsv;
1120 HRESULT hr = D3D_OK;
1122 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1124 wined3d_mutex_lock();
1126 if (ds_impl)
1128 struct wined3d_rendertarget_view *original_rtv;
1129 struct wined3d_resource_desc ds_desc, rt_desc;
1130 struct wined3d_resource *wined3d_resource;
1131 struct d3d8_surface *original_surface;
1133 /* If no render target is passed in check the size against the current RT */
1134 if (!render_target)
1137 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1139 wined3d_mutex_unlock();
1140 return D3DERR_NOTFOUND;
1142 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1143 wined3d_resource = wined3d_surface_get_resource(original_surface->wined3d_surface);
1145 else
1146 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1147 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1149 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1150 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1152 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1154 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1155 wined3d_mutex_unlock();
1156 return D3DERR_INVALIDCALL;
1160 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1161 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1162 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1163 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1164 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1165 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1167 wined3d_mutex_unlock();
1169 return hr;
1172 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1174 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1175 struct wined3d_rendertarget_view *wined3d_rtv;
1176 struct d3d8_surface *surface_impl;
1177 HRESULT hr;
1179 TRACE("iface %p, render_target %p.\n", iface, render_target);
1181 if (!render_target)
1182 return D3DERR_INVALIDCALL;
1184 wined3d_mutex_lock();
1185 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1187 /* We want the sub resource parent here, since the view itself may be
1188 * internal to wined3d and may not have a parent. */
1189 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1190 *render_target = &surface_impl->IDirect3DSurface8_iface;
1191 IDirect3DSurface8_AddRef(*render_target);
1192 hr = D3D_OK;
1194 else
1196 ERR("Failed to get wined3d render target.\n");
1197 *render_target = NULL;
1198 hr = D3DERR_NOTFOUND;
1200 wined3d_mutex_unlock();
1202 return hr;
1205 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1207 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1208 struct wined3d_rendertarget_view *wined3d_dsv;
1209 struct d3d8_surface *surface_impl;
1210 HRESULT hr = D3D_OK;
1212 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1214 if (!depth_stencil)
1215 return D3DERR_INVALIDCALL;
1217 wined3d_mutex_lock();
1218 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1220 /* We want the sub resource parent here, since the view itself may be
1221 * internal to wined3d and may not have a parent. */
1222 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1223 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1224 IDirect3DSurface8_AddRef(*depth_stencil);
1226 else
1228 hr = WINED3DERR_NOTFOUND;
1229 *depth_stencil = NULL;
1231 wined3d_mutex_unlock();
1233 return hr;
1236 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1238 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1239 HRESULT hr;
1241 TRACE("iface %p.\n", iface);
1243 wined3d_mutex_lock();
1244 hr = wined3d_device_begin_scene(device->wined3d_device);
1245 wined3d_mutex_unlock();
1247 return hr;
1250 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1252 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1253 HRESULT hr;
1255 TRACE("iface %p.\n", iface);
1257 wined3d_mutex_lock();
1258 hr = wined3d_device_end_scene(device->wined3d_device);
1259 wined3d_mutex_unlock();
1261 return hr;
1264 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1265 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1267 const struct wined3d_color c =
1269 ((color >> 16) & 0xff) / 255.0f,
1270 ((color >> 8) & 0xff) / 255.0f,
1271 (color & 0xff) / 255.0f,
1272 ((color >> 24) & 0xff) / 255.0f,
1274 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1275 HRESULT hr;
1277 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1278 iface, rect_count, rects, flags, color, z, stencil);
1280 wined3d_mutex_lock();
1281 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1282 wined3d_mutex_unlock();
1284 return hr;
1287 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1288 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1290 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1292 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1294 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1295 wined3d_mutex_lock();
1296 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1297 wined3d_mutex_unlock();
1299 return D3D_OK;
1302 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1303 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1305 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1307 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1309 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1310 wined3d_mutex_lock();
1311 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1312 wined3d_mutex_unlock();
1314 return D3D_OK;
1317 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1318 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1320 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1322 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1324 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1325 wined3d_mutex_lock();
1326 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1327 wined3d_mutex_unlock();
1329 return D3D_OK;
1332 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1334 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1336 TRACE("iface %p, viewport %p.\n", iface, viewport);
1338 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1339 wined3d_mutex_lock();
1340 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1341 wined3d_mutex_unlock();
1343 return D3D_OK;
1346 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1348 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1350 TRACE("iface %p, viewport %p.\n", iface, viewport);
1352 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1353 wined3d_mutex_lock();
1354 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1355 wined3d_mutex_unlock();
1357 return D3D_OK;
1360 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1362 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1364 TRACE("iface %p, material %p.\n", iface, material);
1366 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1367 wined3d_mutex_lock();
1368 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1369 wined3d_mutex_unlock();
1371 return D3D_OK;
1374 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1376 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1378 TRACE("iface %p, material %p.\n", iface, material);
1380 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1381 wined3d_mutex_lock();
1382 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1383 wined3d_mutex_unlock();
1385 return D3D_OK;
1388 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1390 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1391 HRESULT hr;
1393 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1395 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1396 wined3d_mutex_lock();
1397 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1398 wined3d_mutex_unlock();
1400 return hr;
1403 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1405 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1406 HRESULT hr;
1408 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1410 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1411 wined3d_mutex_lock();
1412 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1413 wined3d_mutex_unlock();
1415 return hr;
1418 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1420 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1421 HRESULT hr;
1423 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1425 wined3d_mutex_lock();
1426 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1427 wined3d_mutex_unlock();
1429 return hr;
1432 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1434 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1435 HRESULT hr;
1437 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1439 wined3d_mutex_lock();
1440 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1441 wined3d_mutex_unlock();
1443 return hr;
1446 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1448 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1449 HRESULT hr;
1451 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1453 wined3d_mutex_lock();
1454 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1455 wined3d_mutex_unlock();
1457 return hr;
1460 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1462 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1463 HRESULT hr;
1465 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1467 wined3d_mutex_lock();
1468 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1469 wined3d_mutex_unlock();
1471 return hr;
1474 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1475 D3DRENDERSTATETYPE state, DWORD value)
1477 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1479 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1481 wined3d_mutex_lock();
1482 switch (state)
1484 case D3DRS_ZBIAS:
1485 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1486 break;
1488 default:
1489 wined3d_device_set_render_state(device->wined3d_device, state, value);
1491 wined3d_mutex_unlock();
1493 return D3D_OK;
1496 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1497 D3DRENDERSTATETYPE state, DWORD *value)
1499 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1501 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1503 wined3d_mutex_lock();
1504 switch (state)
1506 case D3DRS_ZBIAS:
1507 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1508 break;
1510 default:
1511 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1513 wined3d_mutex_unlock();
1515 return D3D_OK;
1518 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1520 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1521 HRESULT hr;
1523 TRACE("iface %p.\n", iface);
1525 wined3d_mutex_lock();
1526 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1527 wined3d_mutex_unlock();
1529 return hr;
1532 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1534 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1535 struct wined3d_stateblock *stateblock;
1536 HRESULT hr;
1538 TRACE("iface %p, token %p.\n", iface, token);
1540 /* Tell wineD3D to endstateblock before anything else (in case we run out
1541 * of memory later and cause locking problems)
1543 wined3d_mutex_lock();
1544 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1545 if (FAILED(hr))
1547 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1548 wined3d_mutex_unlock();
1549 return hr;
1552 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1553 wined3d_mutex_unlock();
1555 if (*token == D3D8_INVALID_HANDLE)
1557 ERR("Failed to create a handle\n");
1558 wined3d_mutex_lock();
1559 wined3d_stateblock_decref(stateblock);
1560 wined3d_mutex_unlock();
1561 return E_FAIL;
1563 ++*token;
1565 TRACE("Returning %#x (%p).\n", *token, stateblock);
1567 return hr;
1570 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1572 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1573 struct wined3d_stateblock *stateblock;
1575 TRACE("iface %p, token %#x.\n", iface, token);
1577 if (!token)
1578 return D3D_OK;
1580 wined3d_mutex_lock();
1581 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1582 if (!stateblock)
1584 WARN("Invalid handle (%#x) passed.\n", token);
1585 wined3d_mutex_unlock();
1586 return D3DERR_INVALIDCALL;
1588 wined3d_stateblock_apply(stateblock);
1589 wined3d_mutex_unlock();
1591 return D3D_OK;
1594 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1596 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1597 struct wined3d_stateblock *stateblock;
1599 TRACE("iface %p, token %#x.\n", iface, token);
1601 wined3d_mutex_lock();
1602 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1603 if (!stateblock)
1605 WARN("Invalid handle (%#x) passed.\n", token);
1606 wined3d_mutex_unlock();
1607 return D3DERR_INVALIDCALL;
1609 wined3d_stateblock_capture(stateblock);
1610 wined3d_mutex_unlock();
1612 return D3D_OK;
1615 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1617 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1618 struct wined3d_stateblock *stateblock;
1620 TRACE("iface %p, token %#x.\n", iface, token);
1622 wined3d_mutex_lock();
1623 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1625 if (!stateblock)
1627 WARN("Invalid handle (%#x) passed.\n", token);
1628 wined3d_mutex_unlock();
1629 return D3DERR_INVALIDCALL;
1632 if (wined3d_stateblock_decref(stateblock))
1634 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1636 wined3d_mutex_unlock();
1638 return D3D_OK;
1641 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1642 D3DSTATEBLOCKTYPE type, DWORD *handle)
1644 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1645 struct wined3d_stateblock *stateblock;
1646 HRESULT hr;
1648 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1650 if (type != D3DSBT_ALL
1651 && type != D3DSBT_PIXELSTATE
1652 && type != D3DSBT_VERTEXSTATE)
1654 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1655 return D3DERR_INVALIDCALL;
1658 wined3d_mutex_lock();
1659 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1660 if (FAILED(hr))
1662 wined3d_mutex_unlock();
1663 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1664 return hr;
1667 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1668 wined3d_mutex_unlock();
1670 if (*handle == D3D8_INVALID_HANDLE)
1672 ERR("Failed to allocate a handle.\n");
1673 wined3d_mutex_lock();
1674 wined3d_stateblock_decref(stateblock);
1675 wined3d_mutex_unlock();
1676 return E_FAIL;
1678 ++*handle;
1680 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1682 return hr;
1685 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1687 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1688 HRESULT hr;
1690 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1691 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1693 wined3d_mutex_lock();
1694 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1695 wined3d_mutex_unlock();
1697 return hr;
1700 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1702 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1703 HRESULT hr;
1705 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1707 wined3d_mutex_lock();
1708 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1709 wined3d_mutex_unlock();
1711 return hr;
1714 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1717 struct wined3d_texture *wined3d_texture;
1718 struct d3d8_texture *texture_impl;
1720 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1722 if (!texture)
1723 return D3DERR_INVALIDCALL;
1725 wined3d_mutex_lock();
1726 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1728 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1729 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1730 IDirect3DBaseTexture8_AddRef(*texture);
1732 else
1734 *texture = NULL;
1736 wined3d_mutex_unlock();
1738 return D3D_OK;
1741 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1743 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1744 struct d3d8_texture *texture_impl;
1745 HRESULT hr;
1747 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1749 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1751 wined3d_mutex_lock();
1752 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1753 texture_impl ? texture_impl->wined3d_texture : NULL);
1754 wined3d_mutex_unlock();
1756 return hr;
1759 static const struct tss_lookup
1761 BOOL sampler_state;
1762 enum wined3d_texture_stage_state state;
1764 tss_lookup[] =
1766 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1767 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1768 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1769 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1770 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1771 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1772 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1773 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1774 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1775 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1776 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1777 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1778 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1779 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1780 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1781 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1782 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1783 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1784 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1785 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1786 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1787 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1788 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1789 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1790 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1791 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1792 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1793 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1794 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1797 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1798 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1800 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1801 const struct tss_lookup *l;
1803 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1805 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1807 WARN("Invalid Type %#x passed.\n", Type);
1808 return D3D_OK;
1811 l = &tss_lookup[Type];
1813 wined3d_mutex_lock();
1814 if (l->sampler_state)
1815 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1816 else
1817 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1818 wined3d_mutex_unlock();
1820 return D3D_OK;
1823 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1824 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1826 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1827 const struct tss_lookup *l;
1829 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1831 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1833 WARN("Invalid type %#x passed.\n", type);
1834 return D3D_OK;
1837 l = &tss_lookup[type];
1839 wined3d_mutex_lock();
1840 if (l->sampler_state)
1841 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1842 else
1843 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1844 wined3d_mutex_unlock();
1846 return D3D_OK;
1849 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1851 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1852 HRESULT hr;
1854 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1856 wined3d_mutex_lock();
1857 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1858 wined3d_mutex_unlock();
1860 return hr;
1863 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1864 DWORD info_id, void *info, DWORD info_size)
1866 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1868 return D3D_OK;
1871 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1872 UINT palette_idx, const PALETTEENTRY *entries)
1874 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1876 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1877 * does not have a d3d8/9-style palette API */
1879 return D3D_OK;
1882 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1883 UINT palette_idx, PALETTEENTRY *entries)
1885 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1887 return D3DERR_INVALIDCALL;
1890 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1892 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1894 return D3D_OK;
1897 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1899 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1901 return D3DERR_INVALIDCALL;
1904 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1905 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1907 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1908 HRESULT hr;
1910 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1911 iface, primitive_type, start_vertex, primitive_count);
1913 wined3d_mutex_lock();
1914 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1915 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1916 vertex_count_from_primitive_count(primitive_type, primitive_count));
1917 wined3d_mutex_unlock();
1919 return hr;
1922 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1923 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1924 UINT start_idx, UINT primitive_count)
1926 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1927 HRESULT hr;
1929 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1930 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1932 wined3d_mutex_lock();
1933 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1934 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1935 vertex_count_from_primitive_count(primitive_type, primitive_count));
1936 wined3d_mutex_unlock();
1938 return hr;
1941 /* The caller is responsible for wined3d locking */
1942 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1944 HRESULT hr;
1946 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1948 UINT size = max(device->vertex_buffer_size * 2, min_size);
1949 struct wined3d_buffer *buffer;
1951 TRACE("Growing vertex buffer to %u bytes\n", size);
1953 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1954 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1955 if (FAILED(hr))
1957 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1958 return hr;
1961 if (device->vertex_buffer)
1962 wined3d_buffer_decref(device->vertex_buffer);
1964 device->vertex_buffer = buffer;
1965 device->vertex_buffer_size = size;
1966 device->vertex_buffer_pos = 0;
1968 return D3D_OK;
1971 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1972 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
1973 UINT stride)
1975 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1976 HRESULT hr;
1977 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
1978 UINT size = vtx_count * stride;
1979 UINT vb_pos, align;
1980 BYTE *buffer_data;
1982 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1983 iface, primitive_type, primitive_count, data, stride);
1985 if (!primitive_count)
1987 WARN("primitive_count is 0, returning D3D_OK\n");
1988 return D3D_OK;
1991 wined3d_mutex_lock();
1992 hr = d3d8_device_prepare_vertex_buffer(device, size);
1993 if (FAILED(hr))
1994 goto done;
1996 vb_pos = device->vertex_buffer_pos;
1997 align = vb_pos % stride;
1998 if (align) align = stride - align;
1999 if (vb_pos + size + align > device->vertex_buffer_size)
2000 vb_pos = 0;
2001 else
2002 vb_pos += align;
2004 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2005 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2006 if (FAILED(hr))
2007 goto done;
2008 memcpy(buffer_data, data, size);
2009 wined3d_buffer_unmap(device->vertex_buffer);
2010 device->vertex_buffer_pos = vb_pos + size;
2012 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2013 if (FAILED(hr))
2014 goto done;
2016 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2017 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2018 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2020 done:
2021 wined3d_mutex_unlock();
2022 return hr;
2025 /* The caller is responsible for wined3d locking */
2026 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2028 HRESULT hr;
2030 if (device->index_buffer_size < min_size || !device->index_buffer)
2032 UINT size = max(device->index_buffer_size * 2, min_size);
2033 struct wined3d_buffer *buffer;
2035 TRACE("Growing index buffer to %u bytes\n", size);
2037 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2038 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2039 if (FAILED(hr))
2041 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2042 return hr;
2045 if (device->index_buffer)
2046 wined3d_buffer_decref(device->index_buffer);
2048 device->index_buffer = buffer;
2049 device->index_buffer_size = size;
2050 device->index_buffer_pos = 0;
2052 return D3D_OK;
2055 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2056 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2057 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2058 const void *vertex_data, UINT vertex_stride)
2060 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2061 HRESULT hr;
2062 BYTE *buffer_data;
2064 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2065 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2066 UINT idx_size = idx_count * idx_fmt_size;
2067 UINT ib_pos;
2069 UINT vtx_size = vertex_count * vertex_stride;
2070 UINT vb_pos, align;
2072 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2073 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2074 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2075 index_data, index_format, vertex_data, vertex_stride);
2077 if (!primitive_count)
2079 WARN("primitive_count is 0, returning D3D_OK\n");
2080 return D3D_OK;
2083 wined3d_mutex_lock();
2085 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2086 if (FAILED(hr))
2087 goto done;
2089 vb_pos = device->vertex_buffer_pos;
2090 align = vb_pos % vertex_stride;
2091 if (align) align = vertex_stride - align;
2092 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2093 vb_pos = 0;
2094 else
2095 vb_pos += align;
2097 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2098 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2099 if (FAILED(hr))
2100 goto done;
2101 memcpy(buffer_data, vertex_data, vtx_size);
2102 wined3d_buffer_unmap(device->vertex_buffer);
2103 device->vertex_buffer_pos = vb_pos + vtx_size;
2105 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2106 if (FAILED(hr))
2107 goto done;
2109 ib_pos = device->index_buffer_pos;
2110 align = ib_pos % idx_fmt_size;
2111 if (align) align = idx_fmt_size - align;
2112 if (ib_pos + idx_size + align > device->index_buffer_size)
2113 ib_pos = 0;
2114 else
2115 ib_pos += align;
2117 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2118 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2119 if (FAILED(hr))
2120 goto done;
2121 memcpy(buffer_data, index_data, idx_size);
2122 wined3d_buffer_unmap(device->index_buffer);
2123 device->index_buffer_pos = ib_pos + idx_size;
2125 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2126 if (FAILED(hr))
2127 goto done;
2129 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2130 wined3dformat_from_d3dformat(index_format));
2131 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2133 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2134 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2136 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2137 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2138 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2140 done:
2141 wined3d_mutex_unlock();
2142 return hr;
2145 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2146 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2148 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2149 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2150 HRESULT hr;
2152 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2153 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2155 wined3d_mutex_lock();
2156 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2157 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2158 wined3d_mutex_unlock();
2160 return hr;
2163 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2164 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2166 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2167 struct d3d8_vertex_shader *object;
2168 DWORD shader_handle;
2169 DWORD handle;
2170 HRESULT hr;
2172 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2173 iface, declaration, byte_code, shader, usage);
2175 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2176 if (!object)
2178 *shader = 0;
2179 return E_OUTOFMEMORY;
2182 wined3d_mutex_lock();
2183 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2184 wined3d_mutex_unlock();
2185 if (handle == D3D8_INVALID_HANDLE)
2187 ERR("Failed to allocate vertex shader handle.\n");
2188 HeapFree(GetProcessHeap(), 0, object);
2189 *shader = 0;
2190 return E_OUTOFMEMORY;
2193 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2195 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2196 if (FAILED(hr))
2198 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2199 wined3d_mutex_lock();
2200 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2201 wined3d_mutex_unlock();
2202 HeapFree(GetProcessHeap(), 0, object);
2203 *shader = 0;
2204 return hr;
2207 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2208 *shader = shader_handle;
2210 return D3D_OK;
2213 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2215 struct d3d8_vertex_declaration *d3d8_declaration;
2216 struct FvfToDecl *convertedDecls = device->decls;
2217 int p, low, high; /* deliberately signed */
2218 HRESULT hr;
2220 TRACE("Searching for declaration for fvf %08x... ", fvf);
2222 low = 0;
2223 high = device->numConvertedDecls - 1;
2224 while (low <= high)
2226 p = (low + high) >> 1;
2227 TRACE("%d ", p);
2229 if (convertedDecls[p].fvf == fvf)
2231 TRACE("found %p\n", convertedDecls[p].declaration);
2232 return convertedDecls[p].declaration;
2235 if (convertedDecls[p].fvf < fvf)
2236 low = p + 1;
2237 else
2238 high = p - 1;
2240 TRACE("not found. Creating and inserting at position %d.\n", low);
2242 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2243 return NULL;
2245 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2247 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2248 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2249 return NULL;
2252 if (device->declArraySize == device->numConvertedDecls)
2254 UINT grow = device->declArraySize / 2;
2256 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2257 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2258 if (!convertedDecls)
2260 d3d8_vertex_declaration_destroy(d3d8_declaration);
2261 return NULL;
2263 device->decls = convertedDecls;
2264 device->declArraySize += grow;
2267 memmove(convertedDecls + low + 1, convertedDecls + low,
2268 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2269 convertedDecls[low].declaration = d3d8_declaration;
2270 convertedDecls[low].fvf = fvf;
2271 ++device->numConvertedDecls;
2273 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2275 return d3d8_declaration;
2278 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2280 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2281 struct d3d8_vertex_shader *shader_impl;
2283 TRACE("iface %p, shader %#x.\n", iface, shader);
2285 if (VS_HIGHESTFIXEDFXF >= shader)
2287 TRACE("Setting FVF, %#x\n", shader);
2289 wined3d_mutex_lock();
2290 wined3d_device_set_vertex_declaration(device->wined3d_device,
2291 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2292 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2293 wined3d_mutex_unlock();
2295 return D3D_OK;
2298 TRACE("Setting shader\n");
2300 wined3d_mutex_lock();
2301 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2303 WARN("Invalid handle (%#x) passed.\n", shader);
2304 wined3d_mutex_unlock();
2306 return D3DERR_INVALIDCALL;
2309 wined3d_device_set_vertex_declaration(device->wined3d_device,
2310 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2311 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2312 wined3d_mutex_unlock();
2314 return D3D_OK;
2317 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2319 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2320 struct wined3d_vertex_declaration *wined3d_declaration;
2321 struct d3d8_vertex_declaration *d3d8_declaration;
2323 TRACE("iface %p, shader %p.\n", iface, shader);
2325 wined3d_mutex_lock();
2326 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2328 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2329 *shader = d3d8_declaration->shader_handle;
2331 else
2333 *shader = 0;
2335 wined3d_mutex_unlock();
2337 TRACE("Returning %#x.\n", *shader);
2339 return D3D_OK;
2342 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2344 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2345 struct d3d8_vertex_shader *shader_impl;
2347 TRACE("iface %p, shader %#x.\n", iface, shader);
2349 wined3d_mutex_lock();
2350 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2352 WARN("Invalid handle (%#x) passed.\n", shader);
2353 wined3d_mutex_unlock();
2355 return D3DERR_INVALIDCALL;
2358 if (shader_impl->wined3d_shader
2359 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2360 IDirect3DDevice8_SetVertexShader(iface, 0);
2362 wined3d_mutex_unlock();
2364 d3d8_vertex_shader_destroy(shader_impl);
2366 return D3D_OK;
2369 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2370 DWORD start_register, const void *data, DWORD count)
2372 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2373 HRESULT hr;
2375 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2376 iface, start_register, data, count);
2378 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2380 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2381 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2382 return D3DERR_INVALIDCALL;
2385 wined3d_mutex_lock();
2386 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2387 wined3d_mutex_unlock();
2389 return hr;
2392 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2393 DWORD start_register, void *data, DWORD count)
2395 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2396 HRESULT hr;
2398 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2399 iface, start_register, data, count);
2401 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2403 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2404 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2405 return D3DERR_INVALIDCALL;
2408 wined3d_mutex_lock();
2409 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2410 wined3d_mutex_unlock();
2412 return hr;
2415 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2416 DWORD shader, void *data, DWORD *data_size)
2418 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2419 struct d3d8_vertex_declaration *declaration;
2420 struct d3d8_vertex_shader *shader_impl;
2422 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2423 iface, shader, data, data_size);
2425 wined3d_mutex_lock();
2426 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2427 wined3d_mutex_unlock();
2429 if (!shader_impl)
2431 WARN("Invalid handle (%#x) passed.\n", shader);
2432 return D3DERR_INVALIDCALL;
2434 declaration = shader_impl->vertex_declaration;
2436 if (!data)
2438 *data_size = declaration->elements_size;
2439 return D3D_OK;
2442 /* MSDN claims that if *data_size is smaller than the required size
2443 * we should write the required size and return D3DERR_MOREDATA.
2444 * That's not actually true. */
2445 if (*data_size < declaration->elements_size)
2446 return D3DERR_INVALIDCALL;
2448 memcpy(data, declaration->elements, declaration->elements_size);
2450 return D3D_OK;
2453 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2454 DWORD shader, void *data, DWORD *data_size)
2456 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2457 struct d3d8_vertex_shader *shader_impl = NULL;
2458 HRESULT hr;
2460 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2461 iface, shader, data, data_size);
2463 wined3d_mutex_lock();
2464 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2466 WARN("Invalid handle (%#x) passed.\n", shader);
2467 wined3d_mutex_unlock();
2469 return D3DERR_INVALIDCALL;
2472 if (!shader_impl->wined3d_shader)
2474 wined3d_mutex_unlock();
2475 *data_size = 0;
2476 return D3D_OK;
2479 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2480 wined3d_mutex_unlock();
2482 return hr;
2485 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2486 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2488 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2489 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2491 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2493 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2494 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2495 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2496 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2497 * problem)
2499 wined3d_mutex_lock();
2500 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2501 wined3d_device_set_index_buffer(device->wined3d_device,
2502 ib ? ib->wined3d_buffer : NULL,
2503 ib ? ib->format : WINED3DFMT_UNKNOWN);
2504 wined3d_mutex_unlock();
2506 return D3D_OK;
2509 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2510 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2512 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2513 enum wined3d_format_id wined3d_format;
2514 struct wined3d_buffer *wined3d_buffer;
2515 struct d3d8_indexbuffer *buffer_impl;
2517 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2519 if (!buffer)
2520 return D3DERR_INVALIDCALL;
2522 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2523 wined3d_mutex_lock();
2524 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2525 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2527 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2528 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2529 IDirect3DIndexBuffer8_AddRef(*buffer);
2531 else
2533 *buffer = NULL;
2535 wined3d_mutex_unlock();
2537 return D3D_OK;
2540 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2541 const DWORD *byte_code, DWORD *shader)
2543 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2544 struct d3d8_pixel_shader *object;
2545 DWORD shader_handle;
2546 DWORD handle;
2547 HRESULT hr;
2549 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2551 if (!shader)
2552 return D3DERR_INVALIDCALL;
2554 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2555 if (!object)
2556 return E_OUTOFMEMORY;
2558 wined3d_mutex_lock();
2559 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2560 wined3d_mutex_unlock();
2561 if (handle == D3D8_INVALID_HANDLE)
2563 ERR("Failed to allocate pixel shader handle.\n");
2564 HeapFree(GetProcessHeap(), 0, object);
2565 return E_OUTOFMEMORY;
2568 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2570 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2571 if (FAILED(hr))
2573 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2574 wined3d_mutex_lock();
2575 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2576 wined3d_mutex_unlock();
2577 HeapFree(GetProcessHeap(), 0, object);
2578 *shader = 0;
2579 return hr;
2582 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2583 *shader = shader_handle;
2585 return D3D_OK;
2588 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2590 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2591 struct d3d8_pixel_shader *shader_impl;
2593 TRACE("iface %p, shader %#x.\n", iface, shader);
2595 wined3d_mutex_lock();
2597 if (!shader)
2599 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2600 wined3d_mutex_unlock();
2601 return D3D_OK;
2604 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2606 WARN("Invalid handle (%#x) passed.\n", shader);
2607 wined3d_mutex_unlock();
2608 return D3DERR_INVALIDCALL;
2611 TRACE("Setting shader %p.\n", shader_impl);
2612 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2613 wined3d_mutex_unlock();
2615 return D3D_OK;
2618 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2620 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2621 struct wined3d_shader *object;
2623 TRACE("iface %p, shader %p.\n", iface, shader);
2625 if (!shader)
2626 return D3DERR_INVALIDCALL;
2628 wined3d_mutex_lock();
2629 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2631 struct d3d8_pixel_shader *d3d8_shader;
2632 d3d8_shader = wined3d_shader_get_parent(object);
2633 *shader = d3d8_shader->handle;
2635 else
2637 *shader = 0;
2639 wined3d_mutex_unlock();
2641 TRACE("Returning %#x.\n", *shader);
2643 return D3D_OK;
2646 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2648 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2649 struct d3d8_pixel_shader *shader_impl;
2651 TRACE("iface %p, shader %#x.\n", iface, shader);
2653 wined3d_mutex_lock();
2655 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2657 WARN("Invalid handle (%#x) passed.\n", shader);
2658 wined3d_mutex_unlock();
2659 return D3DERR_INVALIDCALL;
2662 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2663 IDirect3DDevice8_SetPixelShader(iface, 0);
2665 wined3d_mutex_unlock();
2667 d3d8_pixel_shader_destroy(shader_impl);
2669 return D3D_OK;
2672 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2673 DWORD start_register, const void *data, DWORD count)
2675 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2676 HRESULT hr;
2678 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2679 iface, start_register, data, count);
2681 wined3d_mutex_lock();
2682 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2683 wined3d_mutex_unlock();
2685 return hr;
2688 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2689 DWORD start_register, void *data, DWORD count)
2691 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2692 HRESULT hr;
2694 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2695 iface, start_register, data, count);
2697 wined3d_mutex_lock();
2698 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2699 wined3d_mutex_unlock();
2701 return hr;
2704 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2705 DWORD shader, void *data, DWORD *data_size)
2707 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2708 struct d3d8_pixel_shader *shader_impl = NULL;
2709 HRESULT hr;
2711 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2712 iface, shader, data, data_size);
2714 wined3d_mutex_lock();
2715 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2717 WARN("Invalid handle (%#x) passed.\n", shader);
2718 wined3d_mutex_unlock();
2720 return D3DERR_INVALIDCALL;
2723 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2724 wined3d_mutex_unlock();
2726 return hr;
2729 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2730 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2732 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2733 iface, handle, segment_count, patch_info);
2734 return D3D_OK;
2737 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2738 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2740 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2741 iface, handle, segment_count, patch_info);
2742 return D3D_OK;
2745 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2747 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2748 return D3DERR_INVALIDCALL;
2751 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2752 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2754 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2755 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2756 HRESULT hr;
2758 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2759 iface, stream_idx, buffer, stride);
2761 wined3d_mutex_lock();
2762 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2763 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2764 wined3d_mutex_unlock();
2766 return hr;
2769 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2770 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2772 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2773 struct d3d8_vertexbuffer *buffer_impl;
2774 struct wined3d_buffer *wined3d_buffer = NULL;
2775 HRESULT hr;
2777 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2778 iface, stream_idx, buffer, stride);
2780 if (!buffer)
2781 return D3DERR_INVALIDCALL;
2783 wined3d_mutex_lock();
2784 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2785 if (SUCCEEDED(hr) && wined3d_buffer)
2787 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2788 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2789 IDirect3DVertexBuffer8_AddRef(*buffer);
2791 else
2793 if (FAILED(hr))
2794 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2795 *buffer = NULL;
2797 wined3d_mutex_unlock();
2799 return hr;
2802 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2804 d3d8_device_QueryInterface,
2805 d3d8_device_AddRef,
2806 d3d8_device_Release,
2807 d3d8_device_TestCooperativeLevel,
2808 d3d8_device_GetAvailableTextureMem,
2809 d3d8_device_ResourceManagerDiscardBytes,
2810 d3d8_device_GetDirect3D,
2811 d3d8_device_GetDeviceCaps,
2812 d3d8_device_GetDisplayMode,
2813 d3d8_device_GetCreationParameters,
2814 d3d8_device_SetCursorProperties,
2815 d3d8_device_SetCursorPosition,
2816 d3d8_device_ShowCursor,
2817 d3d8_device_CreateAdditionalSwapChain,
2818 d3d8_device_Reset,
2819 d3d8_device_Present,
2820 d3d8_device_GetBackBuffer,
2821 d3d8_device_GetRasterStatus,
2822 d3d8_device_SetGammaRamp,
2823 d3d8_device_GetGammaRamp,
2824 d3d8_device_CreateTexture,
2825 d3d8_device_CreateVolumeTexture,
2826 d3d8_device_CreateCubeTexture,
2827 d3d8_device_CreateVertexBuffer,
2828 d3d8_device_CreateIndexBuffer,
2829 d3d8_device_CreateRenderTarget,
2830 d3d8_device_CreateDepthStencilSurface,
2831 d3d8_device_CreateImageSurface,
2832 d3d8_device_CopyRects,
2833 d3d8_device_UpdateTexture,
2834 d3d8_device_GetFrontBuffer,
2835 d3d8_device_SetRenderTarget,
2836 d3d8_device_GetRenderTarget,
2837 d3d8_device_GetDepthStencilSurface,
2838 d3d8_device_BeginScene,
2839 d3d8_device_EndScene,
2840 d3d8_device_Clear,
2841 d3d8_device_SetTransform,
2842 d3d8_device_GetTransform,
2843 d3d8_device_MultiplyTransform,
2844 d3d8_device_SetViewport,
2845 d3d8_device_GetViewport,
2846 d3d8_device_SetMaterial,
2847 d3d8_device_GetMaterial,
2848 d3d8_device_SetLight,
2849 d3d8_device_GetLight,
2850 d3d8_device_LightEnable,
2851 d3d8_device_GetLightEnable,
2852 d3d8_device_SetClipPlane,
2853 d3d8_device_GetClipPlane,
2854 d3d8_device_SetRenderState,
2855 d3d8_device_GetRenderState,
2856 d3d8_device_BeginStateBlock,
2857 d3d8_device_EndStateBlock,
2858 d3d8_device_ApplyStateBlock,
2859 d3d8_device_CaptureStateBlock,
2860 d3d8_device_DeleteStateBlock,
2861 d3d8_device_CreateStateBlock,
2862 d3d8_device_SetClipStatus,
2863 d3d8_device_GetClipStatus,
2864 d3d8_device_GetTexture,
2865 d3d8_device_SetTexture,
2866 d3d8_device_GetTextureStageState,
2867 d3d8_device_SetTextureStageState,
2868 d3d8_device_ValidateDevice,
2869 d3d8_device_GetInfo,
2870 d3d8_device_SetPaletteEntries,
2871 d3d8_device_GetPaletteEntries,
2872 d3d8_device_SetCurrentTexturePalette,
2873 d3d8_device_GetCurrentTexturePalette,
2874 d3d8_device_DrawPrimitive,
2875 d3d8_device_DrawIndexedPrimitive,
2876 d3d8_device_DrawPrimitiveUP,
2877 d3d8_device_DrawIndexedPrimitiveUP,
2878 d3d8_device_ProcessVertices,
2879 d3d8_device_CreateVertexShader,
2880 d3d8_device_SetVertexShader,
2881 d3d8_device_GetVertexShader,
2882 d3d8_device_DeleteVertexShader,
2883 d3d8_device_SetVertexShaderConstant,
2884 d3d8_device_GetVertexShaderConstant,
2885 d3d8_device_GetVertexShaderDeclaration,
2886 d3d8_device_GetVertexShaderFunction,
2887 d3d8_device_SetStreamSource,
2888 d3d8_device_GetStreamSource,
2889 d3d8_device_SetIndices,
2890 d3d8_device_GetIndices,
2891 d3d8_device_CreatePixelShader,
2892 d3d8_device_SetPixelShader,
2893 d3d8_device_GetPixelShader,
2894 d3d8_device_DeletePixelShader,
2895 d3d8_device_SetPixelShaderConstant,
2896 d3d8_device_GetPixelShaderConstant,
2897 d3d8_device_GetPixelShaderFunction,
2898 d3d8_device_DrawRectPatch,
2899 d3d8_device_DrawTriPatch,
2900 d3d8_device_DeletePatch,
2903 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2905 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2908 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2909 struct wined3d_device *device)
2911 TRACE("device_parent %p, device %p\n", device_parent, device);
2914 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2916 TRACE("device_parent %p.\n", device_parent);
2919 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2921 struct d3d8_device *device = device_from_device_parent(device_parent);
2923 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2925 if (!activate)
2926 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2927 else
2928 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2931 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2932 void *container_parent, struct wined3d_surface *surface, void **parent,
2933 const struct wined3d_parent_ops **parent_ops)
2935 struct d3d8_surface *d3d_surface;
2937 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
2938 device_parent, container_parent, surface, parent, parent_ops);
2940 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2941 return E_OUTOFMEMORY;
2943 surface_init(d3d_surface, container_parent, surface, parent_ops);
2944 *parent = d3d_surface;
2945 TRACE("Created surface %p.\n", d3d_surface);
2947 return D3D_OK;
2950 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
2951 void *container_parent, struct wined3d_volume *volume, void **parent,
2952 const struct wined3d_parent_ops **parent_ops)
2954 struct d3d8_volume *d3d_volume;
2956 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
2957 device_parent, container_parent, volume, parent, parent_ops);
2959 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
2960 return E_OUTOFMEMORY;
2962 volume_init(d3d_volume, container_parent, volume, parent_ops);
2963 *parent = d3d_volume;
2964 TRACE("Created volume %p.\n", d3d_volume);
2966 return D3D_OK;
2969 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
2970 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
2972 struct d3d8_device *device = device_from_device_parent(device_parent);
2973 struct wined3d_resource_desc texture_desc;
2974 struct d3d8_surface *d3d_surface;
2975 struct wined3d_texture *texture;
2976 HRESULT hr;
2978 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
2979 device_parent, container_parent, desc, surface);
2981 texture_desc = *desc;
2982 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
2983 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
2984 WINED3D_SURFACE_MAPPABLE, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
2986 WARN("Failed to create texture, hr %#x.\n", hr);
2987 return hr;
2990 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
2991 wined3d_surface_incref(*surface);
2992 wined3d_texture_decref(texture);
2994 d3d_surface = wined3d_surface_get_parent(*surface);
2995 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
2997 return hr;
3000 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3001 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3003 struct d3d8_device *device = device_from_device_parent(device_parent);
3004 struct d3d8_swapchain *d3d_swapchain;
3005 HRESULT hr;
3007 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3009 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3011 WARN("Failed to create swapchain, hr %#x.\n", hr);
3012 *swapchain = NULL;
3013 return hr;
3016 *swapchain = d3d_swapchain->wined3d_swapchain;
3017 wined3d_swapchain_incref(*swapchain);
3018 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3020 return hr;
3023 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3025 device_parent_wined3d_device_created,
3026 device_parent_mode_changed,
3027 device_parent_activate,
3028 device_parent_surface_created,
3029 device_parent_volume_created,
3030 device_parent_create_swapchain_surface,
3031 device_parent_create_swapchain,
3034 static void setup_fpu(void)
3036 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3037 WORD cw;
3038 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3039 cw = (cw & ~0xf3f) | 0x3f;
3040 __asm__ volatile ("fldcw %0" : : "m" (cw));
3041 #elif defined(__i386__) && defined(_MSC_VER)
3042 WORD cw;
3043 __asm fnstcw cw;
3044 cw = (cw & ~0xf3f) | 0x3f;
3045 __asm fldcw cw;
3046 #else
3047 FIXME("FPU setup not implemented for this platform.\n");
3048 #endif
3051 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3052 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3054 struct wined3d_swapchain_desc swapchain_desc;
3055 HRESULT hr;
3057 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3058 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3059 device->ref = 1;
3060 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3061 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3062 if (!device->handle_table.entries)
3064 ERR("Failed to allocate handle table memory.\n");
3065 return E_OUTOFMEMORY;
3067 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3069 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3071 wined3d_mutex_lock();
3072 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3073 &device->device_parent, &device->wined3d_device);
3074 if (FAILED(hr))
3076 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3077 wined3d_mutex_unlock();
3078 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3079 return hr;
3082 if (!parameters->Windowed)
3084 HWND device_window = parameters->hDeviceWindow;
3086 if (!focus_window)
3087 focus_window = device_window;
3088 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3090 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3091 wined3d_device_decref(device->wined3d_device);
3092 wined3d_mutex_unlock();
3093 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3094 return hr;
3097 if (!device_window)
3098 device_window = focus_window;
3099 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3100 parameters->BackBufferWidth,
3101 parameters->BackBufferHeight);
3104 if (flags & D3DCREATE_MULTITHREADED)
3105 wined3d_device_set_multithreaded(device->wined3d_device);
3107 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3109 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3110 if (FAILED(hr))
3112 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3113 wined3d_device_release_focus_window(device->wined3d_device);
3114 wined3d_device_decref(device->wined3d_device);
3115 wined3d_mutex_unlock();
3116 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3117 return hr;
3120 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3121 wined3d_mutex_unlock();
3123 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3125 device->declArraySize = 16;
3126 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3127 if (!device->decls)
3129 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3130 hr = E_OUTOFMEMORY;
3131 goto err;
3134 device->d3d_parent = &parent->IDirect3D8_iface;
3135 IDirect3D8_AddRef(device->d3d_parent);
3137 return D3D_OK;
3139 err:
3140 wined3d_mutex_lock();
3141 wined3d_device_uninit_3d(device->wined3d_device);
3142 wined3d_device_release_focus_window(device->wined3d_device);
3143 wined3d_device_decref(device->wined3d_device);
3144 wined3d_mutex_unlock();
3145 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3146 return hr;