wined3d: Recognize dynamic linking shader instructions.
[wine.git] / dlls / d3d8 / device.c
blobfae561048f855d883e57ca389ce9ac979473eded
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "wine/debug.h"
33 #include "d3d8_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
37 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
39 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
41 d3d8_null_wined3d_object_destroyed,
44 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
46 BYTE *c = (BYTE *)&format;
48 /* Don't translate FOURCC formats */
49 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
51 switch(format)
53 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
54 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
55 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
56 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
57 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
58 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
59 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
60 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
61 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
62 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
63 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
64 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
65 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
66 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
67 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
68 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
69 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
70 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
71 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
72 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
73 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
74 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
75 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
76 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
77 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
78 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
79 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
80 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
81 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
82 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
83 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
84 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
85 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
86 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
87 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
88 default:
89 FIXME("Unhandled wined3d format %#x.\n", format);
90 return D3DFMT_UNKNOWN;
94 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
96 BYTE *c = (BYTE *)&format;
98 /* Don't translate FOURCC formats */
99 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
101 switch(format)
103 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
104 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
105 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
106 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
107 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
108 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
109 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
110 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
111 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
112 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
113 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
114 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
115 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
116 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
117 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
118 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
119 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
120 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
121 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
122 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
123 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
124 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
125 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
126 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
127 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
128 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
129 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
130 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
131 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
132 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
133 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
134 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
135 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
136 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
137 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
138 default:
139 FIXME("Unhandled D3DFORMAT %#x.\n", format);
140 return WINED3DFMT_UNKNOWN;
144 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
146 switch(primitive_type)
148 case D3DPT_POINTLIST:
149 return primitive_count;
151 case D3DPT_LINELIST:
152 return primitive_count * 2;
154 case D3DPT_LINESTRIP:
155 return primitive_count + 1;
157 case D3DPT_TRIANGLELIST:
158 return primitive_count * 3;
160 case D3DPT_TRIANGLESTRIP:
161 case D3DPT_TRIANGLEFAN:
162 return primitive_count + 2;
164 default:
165 FIXME("Unhandled primitive type %#x\n", primitive_type);
166 return 0;
170 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
171 const struct wined3d_swapchain_desc *swapchain_desc)
173 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
174 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
175 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
176 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
177 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
178 present_parameters->SwapEffect = swapchain_desc->swap_effect;
179 present_parameters->hDeviceWindow = swapchain_desc->device_window;
180 present_parameters->Windowed = swapchain_desc->windowed;
181 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
182 present_parameters->AutoDepthStencilFormat
183 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
184 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
185 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
186 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
189 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
190 const D3DPRESENT_PARAMETERS *present_parameters)
192 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
194 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
195 return FALSE;
197 if (present_parameters->BackBufferCount > 3
198 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
199 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
200 && present_parameters->BackBufferCount > 1))
202 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
203 return FALSE;
206 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
207 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
208 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
209 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
210 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
211 swapchain_desc->multisample_quality = 0; /* d3d9 only */
212 swapchain_desc->swap_effect = present_parameters->SwapEffect;
213 swapchain_desc->device_window = present_parameters->hDeviceWindow;
214 swapchain_desc->windowed = present_parameters->Windowed;
215 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
216 swapchain_desc->auto_depth_stencil_format
217 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
218 swapchain_desc->flags
219 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
220 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
221 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
222 swapchain_desc->auto_restore_display_mode = TRUE;
224 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
225 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
227 return TRUE;
230 /* Handle table functions */
231 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
233 struct d3d8_handle_entry *entry;
235 if (t->free_entries)
237 DWORD index = t->free_entries - t->entries;
238 /* Use a free handle */
239 entry = t->free_entries;
240 if (entry->type != D3D8_HANDLE_FREE)
242 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
243 return D3D8_INVALID_HANDLE;
245 t->free_entries = entry->object;
246 entry->object = object;
247 entry->type = type;
249 return index;
252 if (!(t->entry_count < t->table_size))
254 /* Grow the table */
255 UINT new_size = t->table_size + (t->table_size >> 1);
256 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
257 0, t->entries, new_size * sizeof(*t->entries));
258 if (!new_entries)
260 ERR("Failed to grow the handle table.\n");
261 return D3D8_INVALID_HANDLE;
263 t->entries = new_entries;
264 t->table_size = new_size;
267 entry = &t->entries[t->entry_count];
268 entry->object = object;
269 entry->type = type;
271 return t->entry_count++;
274 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
276 struct d3d8_handle_entry *entry;
277 void *object;
279 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
281 WARN("Invalid handle %u passed.\n", handle);
282 return NULL;
285 entry = &t->entries[handle];
286 if (entry->type != type)
288 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
289 return NULL;
292 object = entry->object;
293 entry->object = t->free_entries;
294 entry->type = D3D8_HANDLE_FREE;
295 t->free_entries = entry;
297 return object;
300 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
302 struct d3d8_handle_entry *entry;
304 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
306 WARN("Invalid handle %u passed.\n", handle);
307 return NULL;
310 entry = &t->entries[handle];
311 if (entry->type != type)
313 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
314 return NULL;
317 return entry->object;
320 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
322 TRACE("iface %p, riid %s, out %p.\n",
323 iface, debugstr_guid(riid), out);
325 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
326 || IsEqualGUID(riid, &IID_IUnknown))
328 IDirect3DDevice8_AddRef(iface);
329 *out = iface;
330 return S_OK;
333 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
335 *out = NULL;
336 return E_NOINTERFACE;
339 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
341 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
342 ULONG ref = InterlockedIncrement(&device->ref);
344 TRACE("%p increasing refcount to %u.\n", iface, ref);
346 return ref;
349 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
351 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
352 ULONG ref;
354 if (device->inDestruction)
355 return 0;
357 ref = InterlockedDecrement(&device->ref);
359 TRACE("%p decreasing refcount to %u.\n", iface, ref);
361 if (!ref)
363 IDirect3D8 *parent = device->d3d_parent;
364 unsigned i;
366 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
368 wined3d_mutex_lock();
370 device->inDestruction = TRUE;
372 for (i = 0; i < device->numConvertedDecls; ++i)
374 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
376 HeapFree(GetProcessHeap(), 0, device->decls);
378 if (device->vertex_buffer)
379 wined3d_buffer_decref(device->vertex_buffer);
380 if (device->index_buffer)
381 wined3d_buffer_decref(device->index_buffer);
383 wined3d_device_uninit_3d(device->wined3d_device);
384 wined3d_device_release_focus_window(device->wined3d_device);
385 wined3d_device_decref(device->wined3d_device);
386 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
387 HeapFree(GetProcessHeap(), 0, device);
389 wined3d_mutex_unlock();
391 IDirect3D8_Release(parent);
393 return ref;
396 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
398 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
400 TRACE("iface %p.\n", iface);
402 TRACE("device state: %#x.\n", device->device_state);
404 switch (device->device_state)
406 default:
407 case D3D8_DEVICE_STATE_OK:
408 return D3D_OK;
409 case D3D8_DEVICE_STATE_LOST:
410 return D3DERR_DEVICELOST;
411 case D3D8_DEVICE_STATE_NOT_RESET:
412 return D3DERR_DEVICENOTRESET;
416 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
418 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
419 UINT ret;
421 TRACE("iface %p.\n", iface);
423 wined3d_mutex_lock();
424 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
425 wined3d_mutex_unlock();
427 return ret;
430 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
432 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
434 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
436 if (byte_count)
437 FIXME("Byte count ignored.\n");
439 wined3d_mutex_lock();
440 wined3d_device_evict_managed_resources(device->wined3d_device);
441 wined3d_mutex_unlock();
443 return D3D_OK;
446 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
448 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
450 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
452 if (!d3d8)
453 return D3DERR_INVALIDCALL;
455 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
458 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
460 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
461 WINED3DCAPS *wined3d_caps;
462 HRESULT hr;
464 TRACE("iface %p, caps %p.\n", iface, caps);
466 if (!caps)
467 return D3DERR_INVALIDCALL;
469 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
470 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
472 wined3d_mutex_lock();
473 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
474 wined3d_mutex_unlock();
476 fixup_caps(wined3d_caps);
477 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
478 HeapFree(GetProcessHeap(), 0, wined3d_caps);
480 return hr;
483 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
485 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
486 struct wined3d_display_mode wined3d_mode;
487 HRESULT hr;
489 TRACE("iface %p, mode %p.\n", iface, mode);
491 wined3d_mutex_lock();
492 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
493 wined3d_mutex_unlock();
495 if (SUCCEEDED(hr))
497 mode->Width = wined3d_mode.width;
498 mode->Height = wined3d_mode.height;
499 mode->RefreshRate = wined3d_mode.refresh_rate;
500 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
503 return hr;
506 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
507 D3DDEVICE_CREATION_PARAMETERS *parameters)
509 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
511 TRACE("iface %p, parameters %p.\n", iface, parameters);
513 wined3d_mutex_lock();
514 wined3d_device_get_creation_parameters(device->wined3d_device,
515 (struct wined3d_device_creation_parameters *)parameters);
516 wined3d_mutex_unlock();
518 return D3D_OK;
521 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
522 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
524 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
525 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
526 HRESULT hr;
528 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
529 iface, hotspot_x, hotspot_y, bitmap);
531 if (!bitmap)
533 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
534 return D3DERR_INVALIDCALL;
537 wined3d_mutex_lock();
538 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
539 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
540 wined3d_mutex_unlock();
542 return hr;
545 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
547 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
549 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
551 wined3d_mutex_lock();
552 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
553 wined3d_mutex_unlock();
556 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
559 BOOL ret;
561 TRACE("iface %p, show %#x.\n", iface, show);
563 wined3d_mutex_lock();
564 ret = wined3d_device_show_cursor(device->wined3d_device, show);
565 wined3d_mutex_unlock();
567 return ret;
570 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
571 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
574 struct wined3d_swapchain_desc desc;
575 struct d3d8_swapchain *object;
576 UINT i, count;
577 HRESULT hr;
579 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
580 iface, present_parameters, swapchain);
582 if (!present_parameters->Windowed)
584 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
585 return D3DERR_INVALIDCALL;
588 wined3d_mutex_lock();
589 count = wined3d_device_get_swapchain_count(device->wined3d_device);
590 for (i = 0; i < count; ++i)
592 struct wined3d_swapchain *wined3d_swapchain;
594 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
595 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
597 if (!desc.windowed)
599 wined3d_mutex_unlock();
600 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
601 return D3DERR_INVALIDCALL;
604 wined3d_mutex_unlock();
606 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
607 return D3DERR_INVALIDCALL;
608 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
609 *swapchain = &object->IDirect3DSwapChain8_iface;
610 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
612 return hr;
615 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
617 struct wined3d_resource_desc desc;
618 IDirect3DBaseTexture8 *texture;
619 struct d3d8_surface *surface;
620 IUnknown *parent;
622 wined3d_resource_get_desc(resource, &desc);
623 if (desc.pool != WINED3D_POOL_DEFAULT)
624 return D3D_OK;
626 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
628 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
629 return D3DERR_DEVICELOST;
632 parent = wined3d_resource_get_parent(resource);
633 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
635 IDirect3DBaseTexture8_Release(texture);
636 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
637 return D3DERR_DEVICELOST;
640 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
641 if (!surface->resource.refcount)
642 return D3D_OK;
644 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
645 return D3DERR_DEVICELOST;
648 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
649 D3DPRESENT_PARAMETERS *present_parameters)
651 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
652 struct wined3d_swapchain_desc swapchain_desc;
653 HRESULT hr;
655 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
657 if (device->device_state == D3D8_DEVICE_STATE_LOST)
659 WARN("App not active, returning D3DERR_DEVICELOST.\n");
660 return D3DERR_DEVICELOST;
662 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
663 return D3DERR_INVALIDCALL;
665 wined3d_mutex_lock();
667 if (device->vertex_buffer)
669 wined3d_buffer_decref(device->vertex_buffer);
670 device->vertex_buffer = NULL;
671 device->vertex_buffer_size = 0;
673 if (device->index_buffer)
675 wined3d_buffer_decref(device->index_buffer);
676 device->index_buffer = NULL;
677 device->index_buffer_size = 0;
680 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
681 NULL, reset_enum_callback, TRUE)))
683 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
684 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
685 device->device_state = D3D8_DEVICE_STATE_OK;
687 else
689 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
691 wined3d_mutex_unlock();
693 return hr;
696 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
697 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
699 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
701 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
702 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
704 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
705 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
706 * shows a framerate on Windows in applications that only call the device
707 * method, like e.g. the dx8 sdk samples. The conclusion is that native
708 * calls the swapchain's public method from the device. */
709 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
710 src_rect, dst_rect, dst_window_override, dirty_region);
713 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
714 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
717 struct wined3d_swapchain *wined3d_swapchain;
718 struct wined3d_texture *wined3d_texture;
719 struct d3d8_surface *surface_impl;
721 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
722 iface, backbuffer_idx, backbuffer_type, backbuffer);
724 /* backbuffer_type is ignored by native. */
726 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
727 wined3d_mutex_lock();
729 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
730 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
732 wined3d_mutex_unlock();
733 *backbuffer = NULL;
734 return D3DERR_INVALIDCALL;
737 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
738 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
739 IDirect3DSurface8_AddRef(*backbuffer);
741 wined3d_mutex_unlock();
742 return D3D_OK;
745 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
747 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
748 HRESULT hr;
750 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
752 wined3d_mutex_lock();
753 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
754 wined3d_mutex_unlock();
756 return hr;
759 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
761 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
763 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
765 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
766 wined3d_mutex_lock();
767 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
768 wined3d_mutex_unlock();
771 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
773 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
775 TRACE("iface %p, ramp %p.\n", iface, ramp);
777 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
778 wined3d_mutex_lock();
779 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
780 wined3d_mutex_unlock();
783 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
784 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
785 D3DPOOL pool, IDirect3DTexture8 **texture)
787 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
788 struct d3d8_texture *object;
789 HRESULT hr;
791 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
792 iface, width, height, levels, usage, format, pool, texture);
794 *texture = NULL;
795 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
796 if (!object)
797 return D3DERR_OUTOFVIDEOMEMORY;
799 hr = texture_init(object, device, width, height, levels, usage, format, pool);
800 if (FAILED(hr))
802 WARN("Failed to initialize texture, hr %#x.\n", hr);
803 HeapFree(GetProcessHeap(), 0, object);
804 return hr;
807 TRACE("Created texture %p.\n", object);
808 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
810 return D3D_OK;
813 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
814 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
815 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
817 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
818 struct d3d8_texture *object;
819 HRESULT hr;
821 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
822 iface, width, height, depth, levels, usage, format, pool, texture);
824 *texture = NULL;
825 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
826 if (!object)
827 return D3DERR_OUTOFVIDEOMEMORY;
829 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
830 if (FAILED(hr))
832 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
833 HeapFree(GetProcessHeap(), 0, object);
834 return hr;
837 TRACE("Created volume texture %p.\n", object);
838 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
840 return D3D_OK;
843 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
844 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
846 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
847 struct d3d8_texture *object;
848 HRESULT hr;
850 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
851 iface, edge_length, levels, usage, format, pool, texture);
853 *texture = NULL;
854 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
855 if (!object)
856 return D3DERR_OUTOFVIDEOMEMORY;
858 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
859 if (FAILED(hr))
861 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
862 HeapFree(GetProcessHeap(), 0, object);
863 return hr;
866 TRACE("Created cube texture %p.\n", object);
867 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
869 return hr;
872 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
873 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
875 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
876 struct d3d8_vertexbuffer *object;
877 HRESULT hr;
879 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
880 iface, size, usage, fvf, pool, buffer);
882 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
883 if (!object)
884 return D3DERR_OUTOFVIDEOMEMORY;
886 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
887 if (FAILED(hr))
889 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
890 HeapFree(GetProcessHeap(), 0, object);
891 return hr;
894 TRACE("Created vertex buffer %p.\n", object);
895 *buffer = &object->IDirect3DVertexBuffer8_iface;
897 return D3D_OK;
900 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
901 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
903 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
904 struct d3d8_indexbuffer *object;
905 HRESULT hr;
907 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
908 iface, size, usage, format, pool, buffer);
910 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
911 if (!object)
912 return D3DERR_OUTOFVIDEOMEMORY;
914 hr = indexbuffer_init(object, device, size, usage, format, pool);
915 if (FAILED(hr))
917 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
918 HeapFree(GetProcessHeap(), 0, object);
919 return hr;
922 TRACE("Created index buffer %p.\n", object);
923 *buffer = &object->IDirect3DIndexBuffer8_iface;
925 return D3D_OK;
928 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
929 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
930 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
932 struct wined3d_resource_desc desc;
933 struct d3d8_surface *surface_impl;
934 struct wined3d_texture *texture;
935 HRESULT hr;
937 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p, "
938 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
939 device, width, height, format, flags, surface,
940 usage, pool, multisample_type, multisample_quality);
942 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
943 desc.format = wined3dformat_from_d3dformat(format);
944 desc.multisample_type = multisample_type;
945 desc.multisample_quality = multisample_quality;
946 desc.usage = usage & WINED3DUSAGE_MASK;
947 desc.pool = pool;
948 desc.width = width;
949 desc.height = height;
950 desc.depth = 1;
951 desc.size = 0;
953 wined3d_mutex_lock();
955 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
956 1, 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
958 wined3d_mutex_unlock();
959 WARN("Failed to create texture, hr %#x.\n", hr);
960 return hr;
963 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
964 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
965 *surface = &surface_impl->IDirect3DSurface8_iface;
966 IDirect3DSurface8_AddRef(*surface);
967 wined3d_texture_decref(texture);
969 wined3d_mutex_unlock();
971 return D3D_OK;
974 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
975 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
976 IDirect3DSurface8 **surface)
978 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
979 DWORD flags = 0;
981 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
982 iface, width, height, format, multisample_type, lockable, surface);
984 *surface = NULL;
985 if (lockable)
986 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
988 return d3d8_device_create_surface(device, width, height, format, flags, surface,
989 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
992 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
993 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
994 IDirect3DSurface8 **surface)
996 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
998 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
999 iface, width, height, format, multisample_type, surface);
1001 *surface = NULL;
1003 /* TODO: Verify that Discard is false */
1004 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1005 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1008 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1009 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1010 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1012 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1014 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1015 iface, width, height, format, surface);
1017 *surface = NULL;
1019 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1020 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1023 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1024 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1025 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1027 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1028 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1029 enum wined3d_format_id src_format, dst_format;
1030 struct wined3d_sub_resource_desc wined3d_desc;
1031 UINT src_w, src_h;
1033 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1034 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1036 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1037 * destination texture is in WINED3D_POOL_DEFAULT. */
1039 wined3d_mutex_lock();
1040 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1041 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1043 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1044 wined3d_mutex_unlock();
1045 return D3DERR_INVALIDCALL;
1047 src_format = wined3d_desc.format;
1048 src_w = wined3d_desc.width;
1049 src_h = wined3d_desc.height;
1051 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1052 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1054 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1055 wined3d_mutex_unlock();
1056 return D3DERR_INVALIDCALL;
1058 dst_format = wined3d_desc.format;
1060 /* Check that the source and destination formats match */
1061 if (src_format != dst_format)
1063 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1064 src_surface, dst_surface);
1065 wined3d_mutex_unlock();
1066 return D3DERR_INVALIDCALL;
1069 /* Quick if complete copy ... */
1070 if (!rect_count && !src_rects && !dst_points)
1072 RECT rect = {0, 0, src_w, src_h};
1073 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1074 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1076 else
1078 unsigned int i;
1079 /* Copy rect by rect */
1080 if (src_rects && dst_points)
1082 for (i = 0; i < rect_count; ++i)
1084 UINT w = src_rects[i].right - src_rects[i].left;
1085 UINT h = src_rects[i].bottom - src_rects[i].top;
1086 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1087 dst_points[i].x + w, dst_points[i].y + h};
1089 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1090 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1093 else
1095 for (i = 0; i < rect_count; ++i)
1097 UINT w = src_rects[i].right - src_rects[i].left;
1098 UINT h = src_rects[i].bottom - src_rects[i].top;
1099 RECT dst_rect = {0, 0, w, h};
1101 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1102 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1106 wined3d_mutex_unlock();
1108 return WINED3D_OK;
1111 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1112 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1114 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1115 struct d3d8_texture *src_impl, *dst_impl;
1116 HRESULT hr;
1118 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1120 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1121 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1123 wined3d_mutex_lock();
1124 hr = wined3d_device_update_texture(device->wined3d_device,
1125 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1126 wined3d_mutex_unlock();
1128 return hr;
1131 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1133 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1134 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1135 HRESULT hr;
1137 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1139 if (!dst_surface)
1141 WARN("Invalid destination surface passed.\n");
1142 return D3DERR_INVALIDCALL;
1145 wined3d_mutex_lock();
1146 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1147 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1148 wined3d_mutex_unlock();
1150 return hr;
1153 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1154 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1156 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1157 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1158 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1159 struct wined3d_rendertarget_view *original_dsv;
1160 HRESULT hr = D3D_OK;
1162 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1164 wined3d_mutex_lock();
1166 if (ds_impl)
1168 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1169 struct wined3d_rendertarget_view *original_rtv;
1170 struct d3d8_surface *original_surface;
1172 /* If no render target is passed in check the size against the current RT */
1173 if (!render_target)
1176 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1178 wined3d_mutex_unlock();
1179 return D3DERR_NOTFOUND;
1181 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1182 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1183 original_surface->sub_resource_idx, &rt_desc);
1185 else
1186 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1187 rt_impl->sub_resource_idx, &rt_desc);
1189 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1191 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1193 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1194 wined3d_mutex_unlock();
1195 return D3DERR_INVALIDCALL;
1197 if (ds_desc.multisample_type != rt_desc.multisample_type
1198 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1200 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1201 wined3d_mutex_unlock();
1202 return D3DERR_INVALIDCALL;
1207 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1208 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1209 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1210 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1211 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1212 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1214 wined3d_mutex_unlock();
1216 return hr;
1219 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1221 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1222 struct wined3d_rendertarget_view *wined3d_rtv;
1223 struct d3d8_surface *surface_impl;
1224 HRESULT hr;
1226 TRACE("iface %p, render_target %p.\n", iface, render_target);
1228 if (!render_target)
1229 return D3DERR_INVALIDCALL;
1231 wined3d_mutex_lock();
1232 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1234 /* We want the sub resource parent here, since the view itself may be
1235 * internal to wined3d and may not have a parent. */
1236 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1237 *render_target = &surface_impl->IDirect3DSurface8_iface;
1238 IDirect3DSurface8_AddRef(*render_target);
1239 hr = D3D_OK;
1241 else
1243 ERR("Failed to get wined3d render target.\n");
1244 *render_target = NULL;
1245 hr = D3DERR_NOTFOUND;
1247 wined3d_mutex_unlock();
1249 return hr;
1252 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1254 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1255 struct wined3d_rendertarget_view *wined3d_dsv;
1256 struct d3d8_surface *surface_impl;
1257 HRESULT hr = D3D_OK;
1259 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1261 if (!depth_stencil)
1262 return D3DERR_INVALIDCALL;
1264 wined3d_mutex_lock();
1265 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1267 /* We want the sub resource parent here, since the view itself may be
1268 * internal to wined3d and may not have a parent. */
1269 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1270 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1271 IDirect3DSurface8_AddRef(*depth_stencil);
1273 else
1275 hr = D3DERR_NOTFOUND;
1276 *depth_stencil = NULL;
1278 wined3d_mutex_unlock();
1280 return hr;
1283 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1285 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1286 HRESULT hr;
1288 TRACE("iface %p.\n", iface);
1290 wined3d_mutex_lock();
1291 hr = wined3d_device_begin_scene(device->wined3d_device);
1292 wined3d_mutex_unlock();
1294 return hr;
1297 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1299 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1300 HRESULT hr;
1302 TRACE("iface %p.\n", iface);
1304 wined3d_mutex_lock();
1305 hr = wined3d_device_end_scene(device->wined3d_device);
1306 wined3d_mutex_unlock();
1308 return hr;
1311 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1312 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1314 const struct wined3d_color c =
1316 ((color >> 16) & 0xff) / 255.0f,
1317 ((color >> 8) & 0xff) / 255.0f,
1318 (color & 0xff) / 255.0f,
1319 ((color >> 24) & 0xff) / 255.0f,
1321 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1322 HRESULT hr;
1324 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1325 iface, rect_count, rects, flags, color, z, stencil);
1327 if (rect_count && !rects)
1329 WARN("count %u with NULL rects.\n", rect_count);
1330 rect_count = 0;
1333 wined3d_mutex_lock();
1334 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1335 wined3d_mutex_unlock();
1337 return hr;
1340 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1341 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1343 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1345 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1347 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1348 wined3d_mutex_lock();
1349 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1350 wined3d_mutex_unlock();
1352 return D3D_OK;
1355 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1356 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1358 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1360 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1362 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1363 wined3d_mutex_lock();
1364 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1365 wined3d_mutex_unlock();
1367 return D3D_OK;
1370 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1371 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1373 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1375 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1377 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1378 wined3d_mutex_lock();
1379 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1380 wined3d_mutex_unlock();
1382 return D3D_OK;
1385 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1387 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1389 TRACE("iface %p, viewport %p.\n", iface, viewport);
1391 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1392 wined3d_mutex_lock();
1393 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1394 wined3d_mutex_unlock();
1396 return D3D_OK;
1399 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1401 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1403 TRACE("iface %p, viewport %p.\n", iface, viewport);
1405 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1406 wined3d_mutex_lock();
1407 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1408 wined3d_mutex_unlock();
1410 return D3D_OK;
1413 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1415 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1417 TRACE("iface %p, material %p.\n", iface, material);
1419 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1420 wined3d_mutex_lock();
1421 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1422 wined3d_mutex_unlock();
1424 return D3D_OK;
1427 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1429 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1431 TRACE("iface %p, material %p.\n", iface, material);
1433 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1434 wined3d_mutex_lock();
1435 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1436 wined3d_mutex_unlock();
1438 return D3D_OK;
1441 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1443 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1444 HRESULT hr;
1446 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1448 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1449 wined3d_mutex_lock();
1450 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1451 wined3d_mutex_unlock();
1453 return hr;
1456 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1459 HRESULT hr;
1461 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1463 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1464 wined3d_mutex_lock();
1465 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1466 wined3d_mutex_unlock();
1468 return hr;
1471 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1473 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1474 HRESULT hr;
1476 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1478 wined3d_mutex_lock();
1479 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1480 wined3d_mutex_unlock();
1482 return hr;
1485 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1487 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1488 HRESULT hr;
1490 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1492 wined3d_mutex_lock();
1493 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1494 wined3d_mutex_unlock();
1496 return hr;
1499 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1501 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1502 HRESULT hr;
1504 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1506 wined3d_mutex_lock();
1507 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1508 wined3d_mutex_unlock();
1510 return hr;
1513 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1515 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1516 HRESULT hr;
1518 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1520 wined3d_mutex_lock();
1521 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1522 wined3d_mutex_unlock();
1524 return hr;
1527 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1528 D3DRENDERSTATETYPE state, DWORD value)
1530 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1532 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1534 wined3d_mutex_lock();
1535 switch (state)
1537 case D3DRS_ZBIAS:
1538 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1539 break;
1541 default:
1542 wined3d_device_set_render_state(device->wined3d_device, state, value);
1544 wined3d_mutex_unlock();
1546 return D3D_OK;
1549 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1550 D3DRENDERSTATETYPE state, DWORD *value)
1552 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1554 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1556 wined3d_mutex_lock();
1557 switch (state)
1559 case D3DRS_ZBIAS:
1560 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1561 break;
1563 default:
1564 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1566 wined3d_mutex_unlock();
1568 return D3D_OK;
1571 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1574 HRESULT hr;
1576 TRACE("iface %p.\n", iface);
1578 wined3d_mutex_lock();
1579 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1580 wined3d_mutex_unlock();
1582 return hr;
1585 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1587 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1588 struct wined3d_stateblock *stateblock;
1589 HRESULT hr;
1591 TRACE("iface %p, token %p.\n", iface, token);
1593 /* Tell wineD3D to endstateblock before anything else (in case we run out
1594 * of memory later and cause locking problems)
1596 wined3d_mutex_lock();
1597 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1598 if (FAILED(hr))
1600 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1601 wined3d_mutex_unlock();
1602 return hr;
1605 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1606 wined3d_mutex_unlock();
1608 if (*token == D3D8_INVALID_HANDLE)
1610 ERR("Failed to create a handle\n");
1611 wined3d_mutex_lock();
1612 wined3d_stateblock_decref(stateblock);
1613 wined3d_mutex_unlock();
1614 return E_FAIL;
1616 ++*token;
1618 TRACE("Returning %#x (%p).\n", *token, stateblock);
1620 return hr;
1623 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1625 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1626 struct wined3d_stateblock *stateblock;
1628 TRACE("iface %p, token %#x.\n", iface, token);
1630 if (!token)
1631 return D3D_OK;
1633 wined3d_mutex_lock();
1634 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1635 if (!stateblock)
1637 WARN("Invalid handle (%#x) passed.\n", token);
1638 wined3d_mutex_unlock();
1639 return D3DERR_INVALIDCALL;
1641 wined3d_stateblock_apply(stateblock);
1642 wined3d_mutex_unlock();
1644 return D3D_OK;
1647 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1649 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1650 struct wined3d_stateblock *stateblock;
1652 TRACE("iface %p, token %#x.\n", iface, token);
1654 wined3d_mutex_lock();
1655 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1656 if (!stateblock)
1658 WARN("Invalid handle (%#x) passed.\n", token);
1659 wined3d_mutex_unlock();
1660 return D3DERR_INVALIDCALL;
1662 wined3d_stateblock_capture(stateblock);
1663 wined3d_mutex_unlock();
1665 return D3D_OK;
1668 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1670 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1671 struct wined3d_stateblock *stateblock;
1673 TRACE("iface %p, token %#x.\n", iface, token);
1675 wined3d_mutex_lock();
1676 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1678 if (!stateblock)
1680 WARN("Invalid handle (%#x) passed.\n", token);
1681 wined3d_mutex_unlock();
1682 return D3DERR_INVALIDCALL;
1685 if (wined3d_stateblock_decref(stateblock))
1687 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1689 wined3d_mutex_unlock();
1691 return D3D_OK;
1694 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1695 D3DSTATEBLOCKTYPE type, DWORD *handle)
1697 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1698 struct wined3d_stateblock *stateblock;
1699 HRESULT hr;
1701 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1703 if (type != D3DSBT_ALL
1704 && type != D3DSBT_PIXELSTATE
1705 && type != D3DSBT_VERTEXSTATE)
1707 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1708 return D3DERR_INVALIDCALL;
1711 wined3d_mutex_lock();
1712 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1713 if (FAILED(hr))
1715 wined3d_mutex_unlock();
1716 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1717 return hr;
1720 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1721 wined3d_mutex_unlock();
1723 if (*handle == D3D8_INVALID_HANDLE)
1725 ERR("Failed to allocate a handle.\n");
1726 wined3d_mutex_lock();
1727 wined3d_stateblock_decref(stateblock);
1728 wined3d_mutex_unlock();
1729 return E_FAIL;
1731 ++*handle;
1733 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1735 return hr;
1738 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1740 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1741 HRESULT hr;
1743 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1744 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1746 wined3d_mutex_lock();
1747 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1748 wined3d_mutex_unlock();
1750 return hr;
1753 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1755 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1756 HRESULT hr;
1758 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1760 wined3d_mutex_lock();
1761 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1762 wined3d_mutex_unlock();
1764 return hr;
1767 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1770 struct wined3d_texture *wined3d_texture;
1771 struct d3d8_texture *texture_impl;
1773 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1775 if (!texture)
1776 return D3DERR_INVALIDCALL;
1778 wined3d_mutex_lock();
1779 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1781 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1782 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1783 IDirect3DBaseTexture8_AddRef(*texture);
1785 else
1787 *texture = NULL;
1789 wined3d_mutex_unlock();
1791 return D3D_OK;
1794 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1796 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1797 struct d3d8_texture *texture_impl;
1798 HRESULT hr;
1800 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1802 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1804 wined3d_mutex_lock();
1805 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1806 texture_impl ? texture_impl->wined3d_texture : NULL);
1807 wined3d_mutex_unlock();
1809 return hr;
1812 static const struct tss_lookup
1814 BOOL sampler_state;
1815 union
1817 enum wined3d_texture_stage_state texture_state;
1818 enum wined3d_sampler_state sampler_state;
1819 } u;
1821 tss_lookup[] =
1823 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
1824 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
1825 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
1826 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
1827 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
1828 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
1829 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
1830 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
1831 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
1832 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
1833 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
1834 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
1835 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
1836 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
1837 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
1838 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
1839 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
1840 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
1841 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
1842 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
1843 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
1844 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
1845 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
1846 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
1847 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1848 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
1849 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
1850 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
1851 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
1854 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1855 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1857 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1858 const struct tss_lookup *l;
1860 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1862 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1864 WARN("Invalid Type %#x passed.\n", Type);
1865 return D3D_OK;
1868 l = &tss_lookup[Type];
1870 wined3d_mutex_lock();
1871 if (l->sampler_state)
1872 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
1873 else
1874 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
1875 wined3d_mutex_unlock();
1877 return D3D_OK;
1880 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1881 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1883 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1884 const struct tss_lookup *l;
1886 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1888 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1890 WARN("Invalid type %#x passed.\n", type);
1891 return D3D_OK;
1894 l = &tss_lookup[type];
1896 wined3d_mutex_lock();
1897 if (l->sampler_state)
1898 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
1899 else
1900 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
1901 wined3d_mutex_unlock();
1903 return D3D_OK;
1906 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1908 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1909 HRESULT hr;
1911 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1913 wined3d_mutex_lock();
1914 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1915 wined3d_mutex_unlock();
1917 return hr;
1920 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1921 DWORD info_id, void *info, DWORD info_size)
1923 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1925 return D3D_OK;
1928 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1929 UINT palette_idx, const PALETTEENTRY *entries)
1931 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1933 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1934 * does not have a d3d8/9-style palette API */
1936 return D3D_OK;
1939 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1940 UINT palette_idx, PALETTEENTRY *entries)
1942 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1944 return D3DERR_INVALIDCALL;
1947 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1949 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1951 return D3D_OK;
1954 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1956 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1958 return D3DERR_INVALIDCALL;
1961 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1962 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1964 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1965 HRESULT hr;
1967 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1968 iface, primitive_type, start_vertex, primitive_count);
1970 wined3d_mutex_lock();
1971 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1972 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1973 vertex_count_from_primitive_count(primitive_type, primitive_count));
1974 wined3d_mutex_unlock();
1976 return hr;
1979 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1980 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1981 UINT start_idx, UINT primitive_count)
1983 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1984 HRESULT hr;
1986 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1987 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1989 wined3d_mutex_lock();
1990 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1991 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1992 vertex_count_from_primitive_count(primitive_type, primitive_count));
1993 wined3d_mutex_unlock();
1995 return hr;
1998 /* The caller is responsible for wined3d locking */
1999 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2001 HRESULT hr;
2003 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2005 UINT size = max(device->vertex_buffer_size * 2, min_size);
2006 struct wined3d_buffer *buffer;
2008 TRACE("Growing vertex buffer to %u bytes\n", size);
2010 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2011 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2012 if (FAILED(hr))
2014 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2015 return hr;
2018 if (device->vertex_buffer)
2019 wined3d_buffer_decref(device->vertex_buffer);
2021 device->vertex_buffer = buffer;
2022 device->vertex_buffer_size = size;
2023 device->vertex_buffer_pos = 0;
2025 return D3D_OK;
2028 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2029 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2030 UINT stride)
2032 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2033 HRESULT hr;
2034 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2035 struct wined3d_map_desc wined3d_map_desc;
2036 struct wined3d_box wined3d_box = {0};
2037 UINT size = vtx_count * stride;
2038 struct wined3d_resource *vb;
2039 UINT vb_pos, align;
2041 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2042 iface, primitive_type, primitive_count, data, stride);
2044 if (!primitive_count)
2046 WARN("primitive_count is 0, returning D3D_OK\n");
2047 return D3D_OK;
2050 wined3d_mutex_lock();
2051 hr = d3d8_device_prepare_vertex_buffer(device, size);
2052 if (FAILED(hr))
2053 goto done;
2055 vb_pos = device->vertex_buffer_pos;
2056 align = vb_pos % stride;
2057 if (align) align = stride - align;
2058 if (vb_pos + size + align > device->vertex_buffer_size)
2059 vb_pos = 0;
2060 else
2061 vb_pos += align;
2063 wined3d_box.left = vb_pos;
2064 wined3d_box.right = vb_pos + size;
2065 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2066 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2067 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2068 goto done;
2069 memcpy(wined3d_map_desc.data, data, size);
2070 wined3d_resource_unmap(vb, 0);
2071 device->vertex_buffer_pos = vb_pos + size;
2073 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2074 if (FAILED(hr))
2075 goto done;
2077 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2078 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2079 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2081 done:
2082 wined3d_mutex_unlock();
2083 return hr;
2086 /* The caller is responsible for wined3d locking */
2087 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2089 HRESULT hr;
2091 if (device->index_buffer_size < min_size || !device->index_buffer)
2093 UINT size = max(device->index_buffer_size * 2, min_size);
2094 struct wined3d_buffer *buffer;
2096 TRACE("Growing index buffer to %u bytes\n", size);
2098 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2099 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2100 if (FAILED(hr))
2102 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2103 return hr;
2106 if (device->index_buffer)
2107 wined3d_buffer_decref(device->index_buffer);
2109 device->index_buffer = buffer;
2110 device->index_buffer_size = size;
2111 device->index_buffer_pos = 0;
2113 return D3D_OK;
2116 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2117 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2118 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2119 const void *vertex_data, UINT vertex_stride)
2121 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2122 HRESULT hr;
2123 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2124 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2125 UINT idx_size = idx_count * idx_fmt_size;
2126 struct wined3d_map_desc wined3d_map_desc;
2127 struct wined3d_box wined3d_box = {0};
2128 struct wined3d_resource *ib, *vb;
2129 UINT ib_pos;
2131 UINT vtx_size = vertex_count * vertex_stride;
2132 UINT vb_pos, align;
2134 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2135 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2136 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2137 index_data, index_format, vertex_data, vertex_stride);
2139 if (!primitive_count)
2141 WARN("primitive_count is 0, returning D3D_OK\n");
2142 return D3D_OK;
2145 wined3d_mutex_lock();
2147 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2148 if (FAILED(hr))
2149 goto done;
2151 vb_pos = device->vertex_buffer_pos;
2152 align = vb_pos % vertex_stride;
2153 if (align) align = vertex_stride - align;
2154 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2155 vb_pos = 0;
2156 else
2157 vb_pos += align;
2159 wined3d_box.left = vb_pos;
2160 wined3d_box.right = vb_pos + vtx_size;
2161 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2162 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2163 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2164 goto done;
2165 memcpy(wined3d_map_desc.data, vertex_data, vtx_size);
2166 wined3d_resource_unmap(vb, 0);
2167 device->vertex_buffer_pos = vb_pos + vtx_size;
2169 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2170 if (FAILED(hr))
2171 goto done;
2173 ib_pos = device->index_buffer_pos;
2174 align = ib_pos % idx_fmt_size;
2175 if (align) align = idx_fmt_size - align;
2176 if (ib_pos + idx_size + align > device->index_buffer_size)
2177 ib_pos = 0;
2178 else
2179 ib_pos += align;
2181 wined3d_box.left = ib_pos;
2182 wined3d_box.right = ib_pos + idx_size;
2183 ib = wined3d_buffer_get_resource(device->index_buffer);
2184 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2185 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2186 goto done;
2187 memcpy(wined3d_map_desc.data, index_data, idx_size);
2188 wined3d_resource_unmap(ib, 0);
2189 device->index_buffer_pos = ib_pos + idx_size;
2191 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2192 if (FAILED(hr))
2193 goto done;
2195 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2196 wined3dformat_from_d3dformat(index_format), 0);
2197 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2199 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2200 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2202 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2203 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2204 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2206 done:
2207 wined3d_mutex_unlock();
2208 return hr;
2211 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2212 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2214 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2215 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2216 HRESULT hr;
2218 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2219 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2221 wined3d_mutex_lock();
2222 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2223 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2224 wined3d_mutex_unlock();
2226 return hr;
2229 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2230 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2232 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2233 struct d3d8_vertex_shader *object;
2234 DWORD shader_handle;
2235 DWORD handle;
2236 HRESULT hr;
2238 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2239 iface, declaration, byte_code, shader, usage);
2241 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2242 if (!object)
2244 *shader = 0;
2245 return E_OUTOFMEMORY;
2248 wined3d_mutex_lock();
2249 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2250 wined3d_mutex_unlock();
2251 if (handle == D3D8_INVALID_HANDLE)
2253 ERR("Failed to allocate vertex shader handle.\n");
2254 HeapFree(GetProcessHeap(), 0, object);
2255 *shader = 0;
2256 return E_OUTOFMEMORY;
2259 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2261 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2262 if (FAILED(hr))
2264 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2265 wined3d_mutex_lock();
2266 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2267 wined3d_mutex_unlock();
2268 HeapFree(GetProcessHeap(), 0, object);
2269 *shader = 0;
2270 return hr;
2273 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2274 *shader = shader_handle;
2276 return D3D_OK;
2279 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2281 struct d3d8_vertex_declaration *d3d8_declaration;
2282 struct FvfToDecl *convertedDecls = device->decls;
2283 int p, low, high; /* deliberately signed */
2284 HRESULT hr;
2286 TRACE("Searching for declaration for fvf %08x... ", fvf);
2288 low = 0;
2289 high = device->numConvertedDecls - 1;
2290 while (low <= high)
2292 p = (low + high) >> 1;
2293 TRACE("%d ", p);
2295 if (convertedDecls[p].fvf == fvf)
2297 TRACE("found %p\n", convertedDecls[p].declaration);
2298 return convertedDecls[p].declaration;
2301 if (convertedDecls[p].fvf < fvf)
2302 low = p + 1;
2303 else
2304 high = p - 1;
2306 TRACE("not found. Creating and inserting at position %d.\n", low);
2308 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2309 return NULL;
2311 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2313 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2314 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2315 return NULL;
2318 if (device->declArraySize == device->numConvertedDecls)
2320 UINT grow = device->declArraySize / 2;
2322 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2323 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2324 if (!convertedDecls)
2326 d3d8_vertex_declaration_destroy(d3d8_declaration);
2327 return NULL;
2329 device->decls = convertedDecls;
2330 device->declArraySize += grow;
2333 memmove(convertedDecls + low + 1, convertedDecls + low,
2334 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2335 convertedDecls[low].declaration = d3d8_declaration;
2336 convertedDecls[low].fvf = fvf;
2337 ++device->numConvertedDecls;
2339 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2341 return d3d8_declaration;
2344 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2346 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2347 struct d3d8_vertex_shader *shader_impl;
2349 TRACE("iface %p, shader %#x.\n", iface, shader);
2351 if (VS_HIGHESTFIXEDFXF >= shader)
2353 TRACE("Setting FVF, %#x\n", shader);
2355 wined3d_mutex_lock();
2356 wined3d_device_set_vertex_declaration(device->wined3d_device,
2357 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2358 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2359 wined3d_mutex_unlock();
2361 return D3D_OK;
2364 TRACE("Setting shader\n");
2366 wined3d_mutex_lock();
2367 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2369 WARN("Invalid handle (%#x) passed.\n", shader);
2370 wined3d_mutex_unlock();
2372 return D3DERR_INVALIDCALL;
2375 wined3d_device_set_vertex_declaration(device->wined3d_device,
2376 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2377 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2378 wined3d_mutex_unlock();
2380 return D3D_OK;
2383 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2385 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2386 struct wined3d_vertex_declaration *wined3d_declaration;
2387 struct d3d8_vertex_declaration *d3d8_declaration;
2389 TRACE("iface %p, shader %p.\n", iface, shader);
2391 wined3d_mutex_lock();
2392 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2394 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2395 *shader = d3d8_declaration->shader_handle;
2397 else
2399 *shader = 0;
2401 wined3d_mutex_unlock();
2403 TRACE("Returning %#x.\n", *shader);
2405 return D3D_OK;
2408 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2410 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2411 struct d3d8_vertex_shader *shader_impl;
2413 TRACE("iface %p, shader %#x.\n", iface, shader);
2415 wined3d_mutex_lock();
2416 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2418 WARN("Invalid handle (%#x) passed.\n", shader);
2419 wined3d_mutex_unlock();
2421 return D3DERR_INVALIDCALL;
2424 if (shader_impl->wined3d_shader
2425 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2426 IDirect3DDevice8_SetVertexShader(iface, 0);
2428 wined3d_mutex_unlock();
2430 d3d8_vertex_shader_destroy(shader_impl);
2432 return D3D_OK;
2435 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2436 DWORD start_register, const void *data, DWORD count)
2438 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2439 HRESULT hr;
2441 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2442 iface, start_register, data, count);
2444 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2446 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2447 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2448 return D3DERR_INVALIDCALL;
2451 wined3d_mutex_lock();
2452 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2453 wined3d_mutex_unlock();
2455 return hr;
2458 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2459 DWORD start_register, void *data, DWORD count)
2461 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2462 HRESULT hr;
2464 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2465 iface, start_register, data, count);
2467 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2469 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2470 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2471 return D3DERR_INVALIDCALL;
2474 wined3d_mutex_lock();
2475 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2476 wined3d_mutex_unlock();
2478 return hr;
2481 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2482 DWORD shader, void *data, DWORD *data_size)
2484 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2485 struct d3d8_vertex_declaration *declaration;
2486 struct d3d8_vertex_shader *shader_impl;
2488 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2489 iface, shader, data, data_size);
2491 wined3d_mutex_lock();
2492 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2493 wined3d_mutex_unlock();
2495 if (!shader_impl)
2497 WARN("Invalid handle (%#x) passed.\n", shader);
2498 return D3DERR_INVALIDCALL;
2500 declaration = shader_impl->vertex_declaration;
2502 if (!data)
2504 *data_size = declaration->elements_size;
2505 return D3D_OK;
2508 /* MSDN claims that if *data_size is smaller than the required size
2509 * we should write the required size and return D3DERR_MOREDATA.
2510 * That's not actually true. */
2511 if (*data_size < declaration->elements_size)
2512 return D3DERR_INVALIDCALL;
2514 memcpy(data, declaration->elements, declaration->elements_size);
2516 return D3D_OK;
2519 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2520 DWORD shader, void *data, DWORD *data_size)
2522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2523 struct d3d8_vertex_shader *shader_impl = NULL;
2524 HRESULT hr;
2526 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2527 iface, shader, data, data_size);
2529 wined3d_mutex_lock();
2530 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2532 WARN("Invalid handle (%#x) passed.\n", shader);
2533 wined3d_mutex_unlock();
2535 return D3DERR_INVALIDCALL;
2538 if (!shader_impl->wined3d_shader)
2540 wined3d_mutex_unlock();
2541 *data_size = 0;
2542 return D3D_OK;
2545 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2546 wined3d_mutex_unlock();
2548 return hr;
2551 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2552 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2554 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2555 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2557 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2559 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2560 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2561 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2562 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2563 * problem)
2565 wined3d_mutex_lock();
2566 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2567 wined3d_device_set_index_buffer(device->wined3d_device,
2568 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2569 wined3d_mutex_unlock();
2571 return D3D_OK;
2574 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2575 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2577 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2578 enum wined3d_format_id wined3d_format;
2579 struct wined3d_buffer *wined3d_buffer;
2580 struct d3d8_indexbuffer *buffer_impl;
2582 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2584 if (!buffer)
2585 return D3DERR_INVALIDCALL;
2587 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2588 wined3d_mutex_lock();
2589 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2590 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2592 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2593 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2594 IDirect3DIndexBuffer8_AddRef(*buffer);
2596 else
2598 *buffer = NULL;
2600 wined3d_mutex_unlock();
2602 return D3D_OK;
2605 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2606 const DWORD *byte_code, DWORD *shader)
2608 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2609 struct d3d8_pixel_shader *object;
2610 DWORD shader_handle;
2611 DWORD handle;
2612 HRESULT hr;
2614 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2616 if (!shader)
2617 return D3DERR_INVALIDCALL;
2619 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2620 if (!object)
2621 return E_OUTOFMEMORY;
2623 wined3d_mutex_lock();
2624 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2625 wined3d_mutex_unlock();
2626 if (handle == D3D8_INVALID_HANDLE)
2628 ERR("Failed to allocate pixel shader handle.\n");
2629 HeapFree(GetProcessHeap(), 0, object);
2630 return E_OUTOFMEMORY;
2633 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2635 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2636 if (FAILED(hr))
2638 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2639 wined3d_mutex_lock();
2640 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2641 wined3d_mutex_unlock();
2642 HeapFree(GetProcessHeap(), 0, object);
2643 *shader = 0;
2644 return hr;
2647 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2648 *shader = shader_handle;
2650 return D3D_OK;
2653 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2656 struct d3d8_pixel_shader *shader_impl;
2658 TRACE("iface %p, shader %#x.\n", iface, shader);
2660 wined3d_mutex_lock();
2662 if (!shader)
2664 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2665 wined3d_mutex_unlock();
2666 return D3D_OK;
2669 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2671 WARN("Invalid handle (%#x) passed.\n", shader);
2672 wined3d_mutex_unlock();
2673 return D3DERR_INVALIDCALL;
2676 TRACE("Setting shader %p.\n", shader_impl);
2677 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2678 wined3d_mutex_unlock();
2680 return D3D_OK;
2683 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2685 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2686 struct wined3d_shader *object;
2688 TRACE("iface %p, shader %p.\n", iface, shader);
2690 if (!shader)
2691 return D3DERR_INVALIDCALL;
2693 wined3d_mutex_lock();
2694 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2696 struct d3d8_pixel_shader *d3d8_shader;
2697 d3d8_shader = wined3d_shader_get_parent(object);
2698 *shader = d3d8_shader->handle;
2700 else
2702 *shader = 0;
2704 wined3d_mutex_unlock();
2706 TRACE("Returning %#x.\n", *shader);
2708 return D3D_OK;
2711 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2714 struct d3d8_pixel_shader *shader_impl;
2716 TRACE("iface %p, shader %#x.\n", iface, shader);
2718 wined3d_mutex_lock();
2720 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2722 WARN("Invalid handle (%#x) passed.\n", shader);
2723 wined3d_mutex_unlock();
2724 return D3DERR_INVALIDCALL;
2727 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2728 IDirect3DDevice8_SetPixelShader(iface, 0);
2730 wined3d_mutex_unlock();
2732 d3d8_pixel_shader_destroy(shader_impl);
2734 return D3D_OK;
2737 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2738 DWORD start_register, const void *data, DWORD count)
2740 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2741 HRESULT hr;
2743 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2744 iface, start_register, data, count);
2746 wined3d_mutex_lock();
2747 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
2748 wined3d_mutex_unlock();
2750 return hr;
2753 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2754 DWORD start_register, void *data, DWORD count)
2756 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2757 HRESULT hr;
2759 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2760 iface, start_register, data, count);
2762 wined3d_mutex_lock();
2763 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
2764 wined3d_mutex_unlock();
2766 return hr;
2769 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2770 DWORD shader, void *data, DWORD *data_size)
2772 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2773 struct d3d8_pixel_shader *shader_impl = NULL;
2774 HRESULT hr;
2776 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2777 iface, shader, data, data_size);
2779 wined3d_mutex_lock();
2780 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2782 WARN("Invalid handle (%#x) passed.\n", shader);
2783 wined3d_mutex_unlock();
2785 return D3DERR_INVALIDCALL;
2788 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2789 wined3d_mutex_unlock();
2791 return hr;
2794 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2795 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2797 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2798 iface, handle, segment_count, patch_info);
2799 return D3D_OK;
2802 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2803 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2805 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2806 iface, handle, segment_count, patch_info);
2807 return D3D_OK;
2810 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2812 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2813 return D3DERR_INVALIDCALL;
2816 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2817 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2819 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2820 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2821 HRESULT hr;
2823 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2824 iface, stream_idx, buffer, stride);
2826 wined3d_mutex_lock();
2827 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2828 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2829 wined3d_mutex_unlock();
2831 return hr;
2834 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2835 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2837 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2838 struct d3d8_vertexbuffer *buffer_impl;
2839 struct wined3d_buffer *wined3d_buffer = NULL;
2840 HRESULT hr;
2842 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2843 iface, stream_idx, buffer, stride);
2845 if (!buffer)
2846 return D3DERR_INVALIDCALL;
2848 wined3d_mutex_lock();
2849 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2850 if (SUCCEEDED(hr) && wined3d_buffer)
2852 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2853 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2854 IDirect3DVertexBuffer8_AddRef(*buffer);
2856 else
2858 if (FAILED(hr))
2859 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2860 *buffer = NULL;
2862 wined3d_mutex_unlock();
2864 return hr;
2867 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2869 d3d8_device_QueryInterface,
2870 d3d8_device_AddRef,
2871 d3d8_device_Release,
2872 d3d8_device_TestCooperativeLevel,
2873 d3d8_device_GetAvailableTextureMem,
2874 d3d8_device_ResourceManagerDiscardBytes,
2875 d3d8_device_GetDirect3D,
2876 d3d8_device_GetDeviceCaps,
2877 d3d8_device_GetDisplayMode,
2878 d3d8_device_GetCreationParameters,
2879 d3d8_device_SetCursorProperties,
2880 d3d8_device_SetCursorPosition,
2881 d3d8_device_ShowCursor,
2882 d3d8_device_CreateAdditionalSwapChain,
2883 d3d8_device_Reset,
2884 d3d8_device_Present,
2885 d3d8_device_GetBackBuffer,
2886 d3d8_device_GetRasterStatus,
2887 d3d8_device_SetGammaRamp,
2888 d3d8_device_GetGammaRamp,
2889 d3d8_device_CreateTexture,
2890 d3d8_device_CreateVolumeTexture,
2891 d3d8_device_CreateCubeTexture,
2892 d3d8_device_CreateVertexBuffer,
2893 d3d8_device_CreateIndexBuffer,
2894 d3d8_device_CreateRenderTarget,
2895 d3d8_device_CreateDepthStencilSurface,
2896 d3d8_device_CreateImageSurface,
2897 d3d8_device_CopyRects,
2898 d3d8_device_UpdateTexture,
2899 d3d8_device_GetFrontBuffer,
2900 d3d8_device_SetRenderTarget,
2901 d3d8_device_GetRenderTarget,
2902 d3d8_device_GetDepthStencilSurface,
2903 d3d8_device_BeginScene,
2904 d3d8_device_EndScene,
2905 d3d8_device_Clear,
2906 d3d8_device_SetTransform,
2907 d3d8_device_GetTransform,
2908 d3d8_device_MultiplyTransform,
2909 d3d8_device_SetViewport,
2910 d3d8_device_GetViewport,
2911 d3d8_device_SetMaterial,
2912 d3d8_device_GetMaterial,
2913 d3d8_device_SetLight,
2914 d3d8_device_GetLight,
2915 d3d8_device_LightEnable,
2916 d3d8_device_GetLightEnable,
2917 d3d8_device_SetClipPlane,
2918 d3d8_device_GetClipPlane,
2919 d3d8_device_SetRenderState,
2920 d3d8_device_GetRenderState,
2921 d3d8_device_BeginStateBlock,
2922 d3d8_device_EndStateBlock,
2923 d3d8_device_ApplyStateBlock,
2924 d3d8_device_CaptureStateBlock,
2925 d3d8_device_DeleteStateBlock,
2926 d3d8_device_CreateStateBlock,
2927 d3d8_device_SetClipStatus,
2928 d3d8_device_GetClipStatus,
2929 d3d8_device_GetTexture,
2930 d3d8_device_SetTexture,
2931 d3d8_device_GetTextureStageState,
2932 d3d8_device_SetTextureStageState,
2933 d3d8_device_ValidateDevice,
2934 d3d8_device_GetInfo,
2935 d3d8_device_SetPaletteEntries,
2936 d3d8_device_GetPaletteEntries,
2937 d3d8_device_SetCurrentTexturePalette,
2938 d3d8_device_GetCurrentTexturePalette,
2939 d3d8_device_DrawPrimitive,
2940 d3d8_device_DrawIndexedPrimitive,
2941 d3d8_device_DrawPrimitiveUP,
2942 d3d8_device_DrawIndexedPrimitiveUP,
2943 d3d8_device_ProcessVertices,
2944 d3d8_device_CreateVertexShader,
2945 d3d8_device_SetVertexShader,
2946 d3d8_device_GetVertexShader,
2947 d3d8_device_DeleteVertexShader,
2948 d3d8_device_SetVertexShaderConstant,
2949 d3d8_device_GetVertexShaderConstant,
2950 d3d8_device_GetVertexShaderDeclaration,
2951 d3d8_device_GetVertexShaderFunction,
2952 d3d8_device_SetStreamSource,
2953 d3d8_device_GetStreamSource,
2954 d3d8_device_SetIndices,
2955 d3d8_device_GetIndices,
2956 d3d8_device_CreatePixelShader,
2957 d3d8_device_SetPixelShader,
2958 d3d8_device_GetPixelShader,
2959 d3d8_device_DeletePixelShader,
2960 d3d8_device_SetPixelShaderConstant,
2961 d3d8_device_GetPixelShaderConstant,
2962 d3d8_device_GetPixelShaderFunction,
2963 d3d8_device_DrawRectPatch,
2964 d3d8_device_DrawTriPatch,
2965 d3d8_device_DeletePatch,
2968 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2970 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2973 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2974 struct wined3d_device *device)
2976 TRACE("device_parent %p, device %p\n", device_parent, device);
2979 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2981 TRACE("device_parent %p.\n", device_parent);
2984 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2986 struct d3d8_device *device = device_from_device_parent(device_parent);
2988 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2990 if (!activate)
2991 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2992 else
2993 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2996 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2997 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
2998 void **parent, const struct wined3d_parent_ops **parent_ops)
3000 struct d3d8_surface *d3d_surface;
3002 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3003 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3005 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3006 return E_OUTOFMEMORY;
3008 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3009 *parent = d3d_surface;
3010 TRACE("Created surface %p.\n", d3d_surface);
3012 return D3D_OK;
3015 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3016 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3017 void **parent, const struct wined3d_parent_ops **parent_ops)
3019 struct d3d8_volume *d3d_volume;
3021 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3022 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3024 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3025 return E_OUTOFMEMORY;
3027 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3028 *parent = d3d_volume;
3029 TRACE("Created volume %p.\n", d3d_volume);
3031 return D3D_OK;
3034 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3035 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3037 struct d3d8_device *device = device_from_device_parent(device_parent);
3038 struct d3d8_surface *d3d_surface;
3039 HRESULT hr;
3041 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3042 device_parent, container_parent, desc, texture);
3044 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
3045 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3047 WARN("Failed to create texture, hr %#x.\n", hr);
3048 return hr;
3051 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3052 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3054 return hr;
3057 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3058 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3060 struct d3d8_device *device = device_from_device_parent(device_parent);
3061 struct d3d8_swapchain *d3d_swapchain;
3062 HRESULT hr;
3064 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3066 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3068 WARN("Failed to create swapchain, hr %#x.\n", hr);
3069 *swapchain = NULL;
3070 return hr;
3073 *swapchain = d3d_swapchain->wined3d_swapchain;
3074 wined3d_swapchain_incref(*swapchain);
3075 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3077 return hr;
3080 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3082 device_parent_wined3d_device_created,
3083 device_parent_mode_changed,
3084 device_parent_activate,
3085 device_parent_surface_created,
3086 device_parent_volume_created,
3087 device_parent_create_swapchain_texture,
3088 device_parent_create_swapchain,
3091 static void setup_fpu(void)
3093 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3094 WORD cw;
3095 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3096 cw = (cw & ~0xf3f) | 0x3f;
3097 __asm__ volatile ("fldcw %0" : : "m" (cw));
3098 #elif defined(__i386__) && defined(_MSC_VER)
3099 WORD cw;
3100 __asm fnstcw cw;
3101 cw = (cw & ~0xf3f) | 0x3f;
3102 __asm fldcw cw;
3103 #else
3104 FIXME("FPU setup not implemented for this platform.\n");
3105 #endif
3108 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3109 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3111 struct wined3d_swapchain_desc swapchain_desc;
3112 struct wined3d_swapchain *wined3d_swapchain;
3113 HRESULT hr;
3115 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3116 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3117 device->ref = 1;
3118 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3119 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3120 if (!device->handle_table.entries)
3122 ERR("Failed to allocate handle table memory.\n");
3123 return E_OUTOFMEMORY;
3125 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3127 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3129 wined3d_mutex_lock();
3130 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3131 &device->device_parent, &device->wined3d_device);
3132 if (FAILED(hr))
3134 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3135 wined3d_mutex_unlock();
3136 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3137 return hr;
3140 if (!parameters->Windowed)
3142 HWND device_window = parameters->hDeviceWindow;
3144 if (!focus_window)
3145 focus_window = device_window;
3146 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3148 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3149 wined3d_device_decref(device->wined3d_device);
3150 wined3d_mutex_unlock();
3151 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3152 return hr;
3155 if (!device_window)
3156 device_window = focus_window;
3157 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3158 parameters->BackBufferWidth,
3159 parameters->BackBufferHeight);
3162 if (flags & D3DCREATE_MULTITHREADED)
3163 wined3d_device_set_multithreaded(device->wined3d_device);
3165 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3167 wined3d_device_release_focus_window(device->wined3d_device);
3168 wined3d_device_decref(device->wined3d_device);
3169 wined3d_mutex_unlock();
3170 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3171 return D3DERR_INVALIDCALL;
3174 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3175 if (FAILED(hr))
3177 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3178 wined3d_device_release_focus_window(device->wined3d_device);
3179 wined3d_device_decref(device->wined3d_device);
3180 wined3d_mutex_unlock();
3181 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3182 return hr;
3185 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3186 wined3d_mutex_unlock();
3188 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3190 device->declArraySize = 16;
3191 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3192 if (!device->decls)
3194 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3195 hr = E_OUTOFMEMORY;
3196 goto err;
3199 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3200 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3202 device->d3d_parent = &parent->IDirect3D8_iface;
3203 IDirect3D8_AddRef(device->d3d_parent);
3205 return D3D_OK;
3207 err:
3208 wined3d_mutex_lock();
3209 wined3d_device_uninit_3d(device->wined3d_device);
3210 wined3d_device_release_focus_window(device->wined3d_device);
3211 wined3d_device_decref(device->wined3d_device);
3212 wined3d_mutex_unlock();
3213 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3214 return hr;