webservices: Add support for namespace attributes in the reader.
[wine.git] / dlls / d3d8 / device.c
blob13de8ec7ea5c03e7192c7e589750a63249c5953b
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_VERTEXDATA: return D3DFMT_VERTEXDATA;
87 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
88 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
89 default:
90 FIXME("Unhandled wined3d format %#x.\n", format);
91 return D3DFMT_UNKNOWN;
95 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
97 BYTE *c = (BYTE *)&format;
99 /* Don't translate FOURCC formats */
100 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
102 switch(format)
104 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
105 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
106 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
107 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
108 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
109 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
110 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
111 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
112 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
113 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
114 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
115 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
116 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
117 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
119 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
120 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
121 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
122 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
123 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
124 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
125 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
126 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
127 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
128 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
129 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
130 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
131 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
132 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
133 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
134 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
135 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
136 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
137 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
138 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
139 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
140 default:
141 FIXME("Unhandled D3DFORMAT %#x\n", format);
142 return WINED3DFMT_UNKNOWN;
146 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
148 switch(primitive_type)
150 case D3DPT_POINTLIST:
151 return primitive_count;
153 case D3DPT_LINELIST:
154 return primitive_count * 2;
156 case D3DPT_LINESTRIP:
157 return primitive_count + 1;
159 case D3DPT_TRIANGLELIST:
160 return primitive_count * 3;
162 case D3DPT_TRIANGLESTRIP:
163 case D3DPT_TRIANGLEFAN:
164 return primitive_count + 2;
166 default:
167 FIXME("Unhandled primitive type %#x\n", primitive_type);
168 return 0;
172 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
173 const struct wined3d_swapchain_desc *swapchain_desc)
175 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
176 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
177 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
178 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
179 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
180 present_parameters->SwapEffect = swapchain_desc->swap_effect;
181 present_parameters->hDeviceWindow = swapchain_desc->device_window;
182 present_parameters->Windowed = swapchain_desc->windowed;
183 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
184 present_parameters->AutoDepthStencilFormat
185 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
186 present_parameters->Flags = swapchain_desc->flags;
187 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
188 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
191 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
192 const D3DPRESENT_PARAMETERS *present_parameters)
194 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
196 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
197 return FALSE;
199 if (present_parameters->BackBufferCount > 3
200 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
201 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
202 && present_parameters->BackBufferCount > 1))
204 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
205 return FALSE;
208 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
209 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
210 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
211 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
212 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
213 swapchain_desc->multisample_quality = 0; /* d3d9 only */
214 swapchain_desc->swap_effect = present_parameters->SwapEffect;
215 swapchain_desc->device_window = present_parameters->hDeviceWindow;
216 swapchain_desc->windowed = present_parameters->Windowed;
217 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
218 swapchain_desc->auto_depth_stencil_format
219 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
220 swapchain_desc->flags = present_parameters->Flags;
221 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
222 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
223 swapchain_desc->auto_restore_display_mode = TRUE;
225 return TRUE;
228 /* Handle table functions */
229 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
231 struct d3d8_handle_entry *entry;
233 if (t->free_entries)
235 DWORD index = t->free_entries - t->entries;
236 /* Use a free handle */
237 entry = t->free_entries;
238 if (entry->type != D3D8_HANDLE_FREE)
240 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
241 return D3D8_INVALID_HANDLE;
243 t->free_entries = entry->object;
244 entry->object = object;
245 entry->type = type;
247 return index;
250 if (!(t->entry_count < t->table_size))
252 /* Grow the table */
253 UINT new_size = t->table_size + (t->table_size >> 1);
254 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
255 0, t->entries, new_size * sizeof(*t->entries));
256 if (!new_entries)
258 ERR("Failed to grow the handle table.\n");
259 return D3D8_INVALID_HANDLE;
261 t->entries = new_entries;
262 t->table_size = new_size;
265 entry = &t->entries[t->entry_count];
266 entry->object = object;
267 entry->type = type;
269 return t->entry_count++;
272 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
274 struct d3d8_handle_entry *entry;
275 void *object;
277 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
279 WARN("Invalid handle %u passed.\n", handle);
280 return NULL;
283 entry = &t->entries[handle];
284 if (entry->type != type)
286 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
287 return NULL;
290 object = entry->object;
291 entry->object = t->free_entries;
292 entry->type = D3D8_HANDLE_FREE;
293 t->free_entries = entry;
295 return object;
298 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
300 struct d3d8_handle_entry *entry;
302 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
304 WARN("Invalid handle %u passed.\n", handle);
305 return NULL;
308 entry = &t->entries[handle];
309 if (entry->type != type)
311 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
312 return NULL;
315 return entry->object;
318 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
320 TRACE("iface %p, riid %s, out %p.\n",
321 iface, debugstr_guid(riid), out);
323 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
324 || IsEqualGUID(riid, &IID_IUnknown))
326 IDirect3DDevice8_AddRef(iface);
327 *out = iface;
328 return S_OK;
331 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
333 *out = NULL;
334 return E_NOINTERFACE;
337 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
339 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
340 ULONG ref = InterlockedIncrement(&device->ref);
342 TRACE("%p increasing refcount to %u.\n", iface, ref);
344 return ref;
347 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
350 ULONG ref;
352 if (device->inDestruction)
353 return 0;
355 ref = InterlockedDecrement(&device->ref);
357 TRACE("%p decreasing refcount to %u.\n", iface, ref);
359 if (!ref)
361 IDirect3D8 *parent = device->d3d_parent;
362 unsigned i;
364 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
366 wined3d_mutex_lock();
368 device->inDestruction = TRUE;
370 for (i = 0; i < device->numConvertedDecls; ++i)
372 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
374 HeapFree(GetProcessHeap(), 0, device->decls);
376 if (device->vertex_buffer)
377 wined3d_buffer_decref(device->vertex_buffer);
378 if (device->index_buffer)
379 wined3d_buffer_decref(device->index_buffer);
381 wined3d_device_uninit_3d(device->wined3d_device);
382 wined3d_device_release_focus_window(device->wined3d_device);
383 wined3d_device_decref(device->wined3d_device);
384 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
385 HeapFree(GetProcessHeap(), 0, device);
387 wined3d_mutex_unlock();
389 IDirect3D8_Release(parent);
391 return ref;
394 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
396 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
398 TRACE("iface %p.\n", iface);
400 TRACE("device state: %#x.\n", device->device_state);
402 switch (device->device_state)
404 default:
405 case D3D8_DEVICE_STATE_OK:
406 return D3D_OK;
407 case D3D8_DEVICE_STATE_LOST:
408 return D3DERR_DEVICELOST;
409 case D3D8_DEVICE_STATE_NOT_RESET:
410 return D3DERR_DEVICENOTRESET;
414 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
417 UINT ret;
419 TRACE("iface %p.\n", iface);
421 wined3d_mutex_lock();
422 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
423 wined3d_mutex_unlock();
425 return ret;
428 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
430 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
432 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
434 if (byte_count)
435 FIXME("Byte count ignored.\n");
437 wined3d_mutex_lock();
438 wined3d_device_evict_managed_resources(device->wined3d_device);
439 wined3d_mutex_unlock();
441 return D3D_OK;
444 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
446 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
448 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
450 if (!d3d8)
451 return D3DERR_INVALIDCALL;
453 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
456 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
459 WINED3DCAPS *wined3d_caps;
460 HRESULT hr;
462 TRACE("iface %p, caps %p.\n", iface, caps);
464 if (!caps)
465 return D3DERR_INVALIDCALL;
467 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
468 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
470 wined3d_mutex_lock();
471 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
472 wined3d_mutex_unlock();
474 fixup_caps(wined3d_caps);
475 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
476 HeapFree(GetProcessHeap(), 0, wined3d_caps);
478 return hr;
481 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
483 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
484 struct wined3d_display_mode wined3d_mode;
485 HRESULT hr;
487 TRACE("iface %p, mode %p.\n", iface, mode);
489 wined3d_mutex_lock();
490 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
491 wined3d_mutex_unlock();
493 if (SUCCEEDED(hr))
495 mode->Width = wined3d_mode.width;
496 mode->Height = wined3d_mode.height;
497 mode->RefreshRate = wined3d_mode.refresh_rate;
498 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
501 return hr;
504 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
505 D3DDEVICE_CREATION_PARAMETERS *parameters)
507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
509 TRACE("iface %p, parameters %p.\n", iface, parameters);
511 wined3d_mutex_lock();
512 wined3d_device_get_creation_parameters(device->wined3d_device,
513 (struct wined3d_device_creation_parameters *)parameters);
514 wined3d_mutex_unlock();
516 return D3D_OK;
519 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
520 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
523 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
524 HRESULT hr;
526 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
527 iface, hotspot_x, hotspot_y, bitmap);
529 if (!bitmap)
531 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
532 return D3DERR_INVALIDCALL;
535 wined3d_mutex_lock();
536 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
537 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
538 wined3d_mutex_unlock();
540 return hr;
543 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
545 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
547 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
549 wined3d_mutex_lock();
550 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
551 wined3d_mutex_unlock();
554 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
556 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
557 BOOL ret;
559 TRACE("iface %p, show %#x.\n", iface, show);
561 wined3d_mutex_lock();
562 ret = wined3d_device_show_cursor(device->wined3d_device, show);
563 wined3d_mutex_unlock();
565 return ret;
568 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
569 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
571 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
572 struct wined3d_swapchain_desc desc;
573 struct d3d8_swapchain *object;
574 UINT i, count;
575 HRESULT hr;
577 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
578 iface, present_parameters, swapchain);
580 if (!present_parameters->Windowed)
582 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
583 return D3DERR_INVALIDCALL;
586 wined3d_mutex_lock();
587 count = wined3d_device_get_swapchain_count(device->wined3d_device);
588 for (i = 0; i < count; ++i)
590 struct wined3d_swapchain *wined3d_swapchain;
592 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
593 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
595 if (!desc.windowed)
597 wined3d_mutex_unlock();
598 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
599 return D3DERR_INVALIDCALL;
602 wined3d_mutex_unlock();
604 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
605 return D3DERR_INVALIDCALL;
606 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
607 *swapchain = &object->IDirect3DSwapChain8_iface;
608 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
610 return hr;
613 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
615 struct wined3d_resource_desc desc;
617 wined3d_resource_get_desc(resource, &desc);
618 if (desc.pool == WINED3D_POOL_DEFAULT)
620 struct d3d8_surface *surface;
622 if (desc.resource_type == WINED3D_RTYPE_TEXTURE_2D)
624 IUnknown *parent = wined3d_resource_get_parent(resource);
625 IDirect3DBaseTexture8 *texture;
627 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
629 IDirect3DBaseTexture8_Release(texture);
630 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
631 return D3DERR_DEVICELOST;
634 return D3D_OK;
637 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
639 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
640 return D3DERR_DEVICELOST;
643 surface = wined3d_resource_get_parent(resource);
644 if (surface->resource.refcount)
646 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
647 return D3DERR_DEVICELOST;
650 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
653 return D3D_OK;
656 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
657 D3DPRESENT_PARAMETERS *present_parameters)
659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
660 struct wined3d_swapchain_desc swapchain_desc;
661 HRESULT hr;
663 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
665 if (device->device_state == D3D8_DEVICE_STATE_LOST)
667 WARN("App not active, returning D3DERR_DEVICELOST.\n");
668 return D3DERR_DEVICELOST;
670 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
671 return D3DERR_INVALIDCALL;
673 wined3d_mutex_lock();
675 if (device->vertex_buffer)
677 wined3d_buffer_decref(device->vertex_buffer);
678 device->vertex_buffer = NULL;
679 device->vertex_buffer_size = 0;
681 if (device->index_buffer)
683 wined3d_buffer_decref(device->index_buffer);
684 device->index_buffer = NULL;
685 device->index_buffer_size = 0;
688 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
689 NULL, reset_enum_callback, TRUE)))
691 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
692 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
693 device->device_state = D3D8_DEVICE_STATE_OK;
695 else
697 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
699 wined3d_mutex_unlock();
701 return hr;
704 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
705 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
707 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
709 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
710 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
712 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
713 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
714 * shows a framerate on Windows in applications that only call the device
715 * method, like e.g. the dx8 sdk samples. The conclusion is that native
716 * calls the swapchain's public method from the device. */
717 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
718 src_rect, dst_rect, dst_window_override, dirty_region);
721 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
722 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
724 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
725 struct wined3d_swapchain *wined3d_swapchain;
726 struct wined3d_texture *wined3d_texture;
727 struct d3d8_surface *surface_impl;
729 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
730 iface, backbuffer_idx, backbuffer_type, backbuffer);
732 /* backbuffer_type is ignored by native. */
734 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
735 wined3d_mutex_lock();
737 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
738 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
740 wined3d_mutex_unlock();
741 *backbuffer = NULL;
742 return D3DERR_INVALIDCALL;
745 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
746 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
747 IDirect3DSurface8_AddRef(*backbuffer);
749 wined3d_mutex_unlock();
750 return D3D_OK;
753 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
755 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
756 HRESULT hr;
758 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
760 wined3d_mutex_lock();
761 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
762 wined3d_mutex_unlock();
764 return hr;
767 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
771 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
773 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
774 wined3d_mutex_lock();
775 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
776 wined3d_mutex_unlock();
779 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
781 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
783 TRACE("iface %p, ramp %p.\n", iface, ramp);
785 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
786 wined3d_mutex_lock();
787 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
788 wined3d_mutex_unlock();
791 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
792 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
793 D3DPOOL pool, IDirect3DTexture8 **texture)
795 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
796 struct d3d8_texture *object;
797 HRESULT hr;
799 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
800 iface, width, height, levels, usage, format, pool, texture);
802 *texture = NULL;
803 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
804 if (!object)
805 return D3DERR_OUTOFVIDEOMEMORY;
807 hr = texture_init(object, device, width, height, levels, usage, format, pool);
808 if (FAILED(hr))
810 WARN("Failed to initialize texture, hr %#x.\n", hr);
811 HeapFree(GetProcessHeap(), 0, object);
812 return hr;
815 TRACE("Created texture %p.\n", object);
816 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
818 return D3D_OK;
821 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
822 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
823 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
825 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
826 struct d3d8_texture *object;
827 HRESULT hr;
829 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
830 iface, width, height, depth, levels, usage, format, pool, texture);
832 *texture = NULL;
833 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
834 if (!object)
835 return D3DERR_OUTOFVIDEOMEMORY;
837 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
838 if (FAILED(hr))
840 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
841 HeapFree(GetProcessHeap(), 0, object);
842 return hr;
845 TRACE("Created volume texture %p.\n", object);
846 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
848 return D3D_OK;
851 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
852 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
854 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
855 struct d3d8_texture *object;
856 HRESULT hr;
858 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
859 iface, edge_length, levels, usage, format, pool, texture);
861 *texture = NULL;
862 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
863 if (!object)
864 return D3DERR_OUTOFVIDEOMEMORY;
866 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
867 if (FAILED(hr))
869 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
870 HeapFree(GetProcessHeap(), 0, object);
871 return hr;
874 TRACE("Created cube texture %p.\n", object);
875 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
877 return hr;
880 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
881 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
883 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
884 struct d3d8_vertexbuffer *object;
885 HRESULT hr;
887 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
888 iface, size, usage, fvf, pool, buffer);
890 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
891 if (!object)
892 return D3DERR_OUTOFVIDEOMEMORY;
894 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
895 if (FAILED(hr))
897 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
898 HeapFree(GetProcessHeap(), 0, object);
899 return hr;
902 TRACE("Created vertex buffer %p.\n", object);
903 *buffer = &object->IDirect3DVertexBuffer8_iface;
905 return D3D_OK;
908 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
909 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
911 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
912 struct d3d8_indexbuffer *object;
913 HRESULT hr;
915 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
916 iface, size, usage, format, pool, buffer);
918 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
919 if (!object)
920 return D3DERR_OUTOFVIDEOMEMORY;
922 hr = indexbuffer_init(object, device, size, usage, format, pool);
923 if (FAILED(hr))
925 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
926 HeapFree(GetProcessHeap(), 0, object);
927 return hr;
930 TRACE("Created index buffer %p.\n", object);
931 *buffer = &object->IDirect3DIndexBuffer8_iface;
933 return D3D_OK;
936 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
937 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
938 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
940 struct wined3d_resource_desc desc;
941 struct d3d8_surface *surface_impl;
942 struct wined3d_texture *texture;
943 HRESULT hr;
945 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
946 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
947 device, width, height, format, flags, surface,
948 usage, pool, multisample_type, multisample_quality);
950 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
951 desc.format = wined3dformat_from_d3dformat(format);
952 desc.multisample_type = multisample_type;
953 desc.multisample_quality = multisample_quality;
954 desc.usage = usage & WINED3DUSAGE_MASK;
955 desc.pool = pool;
956 desc.width = width;
957 desc.height = height;
958 desc.depth = 1;
959 desc.size = 0;
961 wined3d_mutex_lock();
963 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
964 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
966 wined3d_mutex_unlock();
967 WARN("Failed to create texture, hr %#x.\n", hr);
968 return hr;
971 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
972 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
973 *surface = &surface_impl->IDirect3DSurface8_iface;
974 IDirect3DSurface8_AddRef(*surface);
975 wined3d_texture_decref(texture);
977 wined3d_mutex_unlock();
979 return D3D_OK;
982 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
983 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
984 IDirect3DSurface8 **surface)
986 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
987 DWORD flags = 0;
989 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
990 iface, width, height, format, multisample_type, lockable, surface);
992 *surface = NULL;
993 if (lockable)
994 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
996 return d3d8_device_create_surface(device, width, height, format, flags, surface,
997 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
1000 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1001 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1002 IDirect3DSurface8 **surface)
1004 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1006 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1007 iface, width, height, format, multisample_type, surface);
1009 *surface = NULL;
1011 /* TODO: Verify that Discard is false */
1012 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1013 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1016 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1017 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1018 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1020 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1022 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1023 iface, width, height, format, surface);
1025 *surface = NULL;
1027 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1028 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1031 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1032 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1033 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1035 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1036 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1037 enum wined3d_format_id src_format, dst_format;
1038 struct wined3d_resource_desc wined3d_desc;
1039 struct wined3d_resource *wined3d_resource;
1040 UINT src_w, src_h;
1042 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1043 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1045 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1046 * destination texture is in WINED3D_POOL_DEFAULT. */
1048 wined3d_mutex_lock();
1049 wined3d_resource = wined3d_texture_get_sub_resource(src->wined3d_texture, src->sub_resource_idx);
1050 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1051 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1053 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1054 wined3d_mutex_unlock();
1055 return D3DERR_INVALIDCALL;
1057 src_format = wined3d_desc.format;
1058 src_w = wined3d_desc.width;
1059 src_h = wined3d_desc.height;
1061 wined3d_resource = wined3d_texture_get_sub_resource(dst->wined3d_texture, dst->sub_resource_idx);
1062 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1063 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1065 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1066 wined3d_mutex_unlock();
1067 return D3DERR_INVALIDCALL;
1069 dst_format = wined3d_desc.format;
1071 /* Check that the source and destination formats match */
1072 if (src_format != dst_format)
1074 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1075 src_surface, dst_surface);
1076 wined3d_mutex_unlock();
1077 return D3DERR_INVALIDCALL;
1080 /* Quick if complete copy ... */
1081 if (!rect_count && !src_rects && !dst_points)
1083 RECT rect = {0, 0, src_w, src_h};
1084 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1085 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1087 else
1089 unsigned int i;
1090 /* Copy rect by rect */
1091 if (src_rects && dst_points)
1093 for (i = 0; i < rect_count; ++i)
1095 UINT w = src_rects[i].right - src_rects[i].left;
1096 UINT h = src_rects[i].bottom - src_rects[i].top;
1097 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1098 dst_points[i].x + w, dst_points[i].y + h};
1100 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1101 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1104 else
1106 for (i = 0; i < rect_count; ++i)
1108 UINT w = src_rects[i].right - src_rects[i].left;
1109 UINT h = src_rects[i].bottom - src_rects[i].top;
1110 RECT dst_rect = {0, 0, w, h};
1112 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1113 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1117 wined3d_mutex_unlock();
1119 return WINED3D_OK;
1122 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1123 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1125 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1126 struct d3d8_texture *src_impl, *dst_impl;
1127 HRESULT hr;
1129 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1131 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1132 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1134 wined3d_mutex_lock();
1135 hr = wined3d_device_update_texture(device->wined3d_device,
1136 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1137 wined3d_mutex_unlock();
1139 return hr;
1142 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1144 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1145 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1146 HRESULT hr;
1148 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1150 if (!dst_surface)
1152 WARN("Invalid destination surface passed.\n");
1153 return D3DERR_INVALIDCALL;
1156 wined3d_mutex_lock();
1157 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1158 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1159 wined3d_mutex_unlock();
1161 return hr;
1164 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1165 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1167 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1168 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1169 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1170 struct wined3d_rendertarget_view *original_dsv;
1171 HRESULT hr = D3D_OK;
1173 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1175 wined3d_mutex_lock();
1177 if (ds_impl)
1179 struct wined3d_rendertarget_view *original_rtv;
1180 struct wined3d_resource_desc ds_desc, rt_desc;
1181 struct wined3d_resource *wined3d_resource;
1182 struct d3d8_surface *original_surface;
1184 /* If no render target is passed in check the size against the current RT */
1185 if (!render_target)
1188 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1190 wined3d_mutex_unlock();
1191 return D3DERR_NOTFOUND;
1193 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1194 wined3d_resource = wined3d_texture_get_sub_resource(original_surface->wined3d_texture, original_surface->sub_resource_idx);
1196 else
1197 wined3d_resource = wined3d_texture_get_sub_resource(rt_impl->wined3d_texture, rt_impl->sub_resource_idx);
1198 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1200 wined3d_resource = wined3d_texture_get_sub_resource(ds_impl->wined3d_texture, ds_impl->sub_resource_idx);
1201 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1203 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1205 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1206 wined3d_mutex_unlock();
1207 return D3DERR_INVALIDCALL;
1209 if (ds_desc.multisample_type != rt_desc.multisample_type
1210 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1212 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1213 wined3d_mutex_unlock();
1214 return D3DERR_INVALIDCALL;
1219 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1220 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1221 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1222 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1223 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1224 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1226 wined3d_mutex_unlock();
1228 return hr;
1231 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1233 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1234 struct wined3d_rendertarget_view *wined3d_rtv;
1235 struct d3d8_surface *surface_impl;
1236 HRESULT hr;
1238 TRACE("iface %p, render_target %p.\n", iface, render_target);
1240 if (!render_target)
1241 return D3DERR_INVALIDCALL;
1243 wined3d_mutex_lock();
1244 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1246 /* We want the sub resource parent here, since the view itself may be
1247 * internal to wined3d and may not have a parent. */
1248 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1249 *render_target = &surface_impl->IDirect3DSurface8_iface;
1250 IDirect3DSurface8_AddRef(*render_target);
1251 hr = D3D_OK;
1253 else
1255 ERR("Failed to get wined3d render target.\n");
1256 *render_target = NULL;
1257 hr = D3DERR_NOTFOUND;
1259 wined3d_mutex_unlock();
1261 return hr;
1264 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1266 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1267 struct wined3d_rendertarget_view *wined3d_dsv;
1268 struct d3d8_surface *surface_impl;
1269 HRESULT hr = D3D_OK;
1271 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1273 if (!depth_stencil)
1274 return D3DERR_INVALIDCALL;
1276 wined3d_mutex_lock();
1277 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1279 /* We want the sub resource parent here, since the view itself may be
1280 * internal to wined3d and may not have a parent. */
1281 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1282 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1283 IDirect3DSurface8_AddRef(*depth_stencil);
1285 else
1287 hr = D3DERR_NOTFOUND;
1288 *depth_stencil = NULL;
1290 wined3d_mutex_unlock();
1292 return hr;
1295 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1297 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1298 HRESULT hr;
1300 TRACE("iface %p.\n", iface);
1302 wined3d_mutex_lock();
1303 hr = wined3d_device_begin_scene(device->wined3d_device);
1304 wined3d_mutex_unlock();
1306 return hr;
1309 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1311 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1312 HRESULT hr;
1314 TRACE("iface %p.\n", iface);
1316 wined3d_mutex_lock();
1317 hr = wined3d_device_end_scene(device->wined3d_device);
1318 wined3d_mutex_unlock();
1320 return hr;
1323 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1324 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1326 const struct wined3d_color c =
1328 ((color >> 16) & 0xff) / 255.0f,
1329 ((color >> 8) & 0xff) / 255.0f,
1330 (color & 0xff) / 255.0f,
1331 ((color >> 24) & 0xff) / 255.0f,
1333 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1334 HRESULT hr;
1336 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1337 iface, rect_count, rects, flags, color, z, stencil);
1339 wined3d_mutex_lock();
1340 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1341 wined3d_mutex_unlock();
1343 return hr;
1346 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1347 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1349 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1351 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1353 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1354 wined3d_mutex_lock();
1355 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1356 wined3d_mutex_unlock();
1358 return D3D_OK;
1361 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1362 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1364 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1366 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1368 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1369 wined3d_mutex_lock();
1370 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1371 wined3d_mutex_unlock();
1373 return D3D_OK;
1376 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1377 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1379 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1381 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1383 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1384 wined3d_mutex_lock();
1385 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1386 wined3d_mutex_unlock();
1388 return D3D_OK;
1391 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1393 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1395 TRACE("iface %p, viewport %p.\n", iface, viewport);
1397 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1398 wined3d_mutex_lock();
1399 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1400 wined3d_mutex_unlock();
1402 return D3D_OK;
1405 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1407 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1409 TRACE("iface %p, viewport %p.\n", iface, viewport);
1411 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1412 wined3d_mutex_lock();
1413 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1414 wined3d_mutex_unlock();
1416 return D3D_OK;
1419 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1421 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1423 TRACE("iface %p, material %p.\n", iface, material);
1425 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1426 wined3d_mutex_lock();
1427 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1428 wined3d_mutex_unlock();
1430 return D3D_OK;
1433 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1435 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1437 TRACE("iface %p, material %p.\n", iface, material);
1439 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1440 wined3d_mutex_lock();
1441 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1442 wined3d_mutex_unlock();
1444 return D3D_OK;
1447 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1449 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1450 HRESULT hr;
1452 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1454 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1455 wined3d_mutex_lock();
1456 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1457 wined3d_mutex_unlock();
1459 return hr;
1462 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1464 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1465 HRESULT hr;
1467 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1469 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1470 wined3d_mutex_lock();
1471 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1472 wined3d_mutex_unlock();
1474 return hr;
1477 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1479 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1480 HRESULT hr;
1482 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1484 wined3d_mutex_lock();
1485 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1486 wined3d_mutex_unlock();
1488 return hr;
1491 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1493 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1494 HRESULT hr;
1496 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1498 wined3d_mutex_lock();
1499 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1500 wined3d_mutex_unlock();
1502 return hr;
1505 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1508 HRESULT hr;
1510 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1512 wined3d_mutex_lock();
1513 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1514 wined3d_mutex_unlock();
1516 return hr;
1519 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1521 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1522 HRESULT hr;
1524 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1526 wined3d_mutex_lock();
1527 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1528 wined3d_mutex_unlock();
1530 return hr;
1533 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1534 D3DRENDERSTATETYPE state, DWORD value)
1536 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1538 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1540 wined3d_mutex_lock();
1541 switch (state)
1543 case D3DRS_ZBIAS:
1544 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1545 break;
1547 default:
1548 wined3d_device_set_render_state(device->wined3d_device, state, value);
1550 wined3d_mutex_unlock();
1552 return D3D_OK;
1555 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1556 D3DRENDERSTATETYPE state, DWORD *value)
1558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1560 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1562 wined3d_mutex_lock();
1563 switch (state)
1565 case D3DRS_ZBIAS:
1566 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1567 break;
1569 default:
1570 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1572 wined3d_mutex_unlock();
1574 return D3D_OK;
1577 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1579 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1580 HRESULT hr;
1582 TRACE("iface %p.\n", iface);
1584 wined3d_mutex_lock();
1585 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1586 wined3d_mutex_unlock();
1588 return hr;
1591 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1593 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1594 struct wined3d_stateblock *stateblock;
1595 HRESULT hr;
1597 TRACE("iface %p, token %p.\n", iface, token);
1599 /* Tell wineD3D to endstateblock before anything else (in case we run out
1600 * of memory later and cause locking problems)
1602 wined3d_mutex_lock();
1603 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1604 if (FAILED(hr))
1606 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1607 wined3d_mutex_unlock();
1608 return hr;
1611 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1612 wined3d_mutex_unlock();
1614 if (*token == D3D8_INVALID_HANDLE)
1616 ERR("Failed to create a handle\n");
1617 wined3d_mutex_lock();
1618 wined3d_stateblock_decref(stateblock);
1619 wined3d_mutex_unlock();
1620 return E_FAIL;
1622 ++*token;
1624 TRACE("Returning %#x (%p).\n", *token, stateblock);
1626 return hr;
1629 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1631 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1632 struct wined3d_stateblock *stateblock;
1634 TRACE("iface %p, token %#x.\n", iface, token);
1636 if (!token)
1637 return D3D_OK;
1639 wined3d_mutex_lock();
1640 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1641 if (!stateblock)
1643 WARN("Invalid handle (%#x) passed.\n", token);
1644 wined3d_mutex_unlock();
1645 return D3DERR_INVALIDCALL;
1647 wined3d_stateblock_apply(stateblock);
1648 wined3d_mutex_unlock();
1650 return D3D_OK;
1653 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1656 struct wined3d_stateblock *stateblock;
1658 TRACE("iface %p, token %#x.\n", iface, token);
1660 wined3d_mutex_lock();
1661 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1662 if (!stateblock)
1664 WARN("Invalid handle (%#x) passed.\n", token);
1665 wined3d_mutex_unlock();
1666 return D3DERR_INVALIDCALL;
1668 wined3d_stateblock_capture(stateblock);
1669 wined3d_mutex_unlock();
1671 return D3D_OK;
1674 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1676 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1677 struct wined3d_stateblock *stateblock;
1679 TRACE("iface %p, token %#x.\n", iface, token);
1681 wined3d_mutex_lock();
1682 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1684 if (!stateblock)
1686 WARN("Invalid handle (%#x) passed.\n", token);
1687 wined3d_mutex_unlock();
1688 return D3DERR_INVALIDCALL;
1691 if (wined3d_stateblock_decref(stateblock))
1693 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1695 wined3d_mutex_unlock();
1697 return D3D_OK;
1700 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1701 D3DSTATEBLOCKTYPE type, DWORD *handle)
1703 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1704 struct wined3d_stateblock *stateblock;
1705 HRESULT hr;
1707 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1709 if (type != D3DSBT_ALL
1710 && type != D3DSBT_PIXELSTATE
1711 && type != D3DSBT_VERTEXSTATE)
1713 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1714 return D3DERR_INVALIDCALL;
1717 wined3d_mutex_lock();
1718 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1719 if (FAILED(hr))
1721 wined3d_mutex_unlock();
1722 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1723 return hr;
1726 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1727 wined3d_mutex_unlock();
1729 if (*handle == D3D8_INVALID_HANDLE)
1731 ERR("Failed to allocate a handle.\n");
1732 wined3d_mutex_lock();
1733 wined3d_stateblock_decref(stateblock);
1734 wined3d_mutex_unlock();
1735 return E_FAIL;
1737 ++*handle;
1739 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1741 return hr;
1744 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1746 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1747 HRESULT hr;
1749 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1750 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1752 wined3d_mutex_lock();
1753 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1754 wined3d_mutex_unlock();
1756 return hr;
1759 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1761 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1762 HRESULT hr;
1764 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1766 wined3d_mutex_lock();
1767 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1768 wined3d_mutex_unlock();
1770 return hr;
1773 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1775 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1776 struct wined3d_texture *wined3d_texture;
1777 struct d3d8_texture *texture_impl;
1779 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1781 if (!texture)
1782 return D3DERR_INVALIDCALL;
1784 wined3d_mutex_lock();
1785 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1787 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1788 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1789 IDirect3DBaseTexture8_AddRef(*texture);
1791 else
1793 *texture = NULL;
1795 wined3d_mutex_unlock();
1797 return D3D_OK;
1800 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1802 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1803 struct d3d8_texture *texture_impl;
1804 HRESULT hr;
1806 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1808 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1810 wined3d_mutex_lock();
1811 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1812 texture_impl ? texture_impl->wined3d_texture : NULL);
1813 wined3d_mutex_unlock();
1815 return hr;
1818 static const struct tss_lookup
1820 BOOL sampler_state;
1821 enum wined3d_texture_stage_state state;
1823 tss_lookup[] =
1825 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1826 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1827 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1828 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1829 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1830 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1831 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1832 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1833 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1834 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1835 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1836 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1837 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1838 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1839 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1840 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1841 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1842 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1843 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1844 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1845 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1846 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1847 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1848 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1849 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1850 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1851 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1852 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1853 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1856 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1857 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1859 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1860 const struct tss_lookup *l;
1862 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1864 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1866 WARN("Invalid Type %#x passed.\n", Type);
1867 return D3D_OK;
1870 l = &tss_lookup[Type];
1872 wined3d_mutex_lock();
1873 if (l->sampler_state)
1874 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1875 else
1876 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1877 wined3d_mutex_unlock();
1879 return D3D_OK;
1882 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1883 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1885 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1886 const struct tss_lookup *l;
1888 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1890 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1892 WARN("Invalid type %#x passed.\n", type);
1893 return D3D_OK;
1896 l = &tss_lookup[type];
1898 wined3d_mutex_lock();
1899 if (l->sampler_state)
1900 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1901 else
1902 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1903 wined3d_mutex_unlock();
1905 return D3D_OK;
1908 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1910 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1911 HRESULT hr;
1913 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1915 wined3d_mutex_lock();
1916 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1917 wined3d_mutex_unlock();
1919 return hr;
1922 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1923 DWORD info_id, void *info, DWORD info_size)
1925 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1927 return D3D_OK;
1930 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1931 UINT palette_idx, const PALETTEENTRY *entries)
1933 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1935 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1936 * does not have a d3d8/9-style palette API */
1938 return D3D_OK;
1941 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1942 UINT palette_idx, PALETTEENTRY *entries)
1944 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1946 return D3DERR_INVALIDCALL;
1949 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1951 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1953 return D3D_OK;
1956 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1958 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1960 return D3DERR_INVALIDCALL;
1963 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1964 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1966 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1967 HRESULT hr;
1969 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1970 iface, primitive_type, start_vertex, primitive_count);
1972 wined3d_mutex_lock();
1973 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1974 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1975 vertex_count_from_primitive_count(primitive_type, primitive_count));
1976 wined3d_mutex_unlock();
1978 return hr;
1981 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1982 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1983 UINT start_idx, UINT primitive_count)
1985 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1986 HRESULT hr;
1988 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1989 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1991 wined3d_mutex_lock();
1992 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1993 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1994 vertex_count_from_primitive_count(primitive_type, primitive_count));
1995 wined3d_mutex_unlock();
1997 return hr;
2000 /* The caller is responsible for wined3d locking */
2001 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2003 HRESULT hr;
2005 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2007 UINT size = max(device->vertex_buffer_size * 2, min_size);
2008 struct wined3d_buffer *buffer;
2010 TRACE("Growing vertex buffer to %u bytes\n", size);
2012 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2013 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2014 if (FAILED(hr))
2016 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2017 return hr;
2020 if (device->vertex_buffer)
2021 wined3d_buffer_decref(device->vertex_buffer);
2023 device->vertex_buffer = buffer;
2024 device->vertex_buffer_size = size;
2025 device->vertex_buffer_pos = 0;
2027 return D3D_OK;
2030 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2031 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2032 UINT stride)
2034 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2035 HRESULT hr;
2036 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2037 UINT size = vtx_count * stride;
2038 UINT vb_pos, align;
2039 BYTE *buffer_data;
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 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2064 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2065 if (FAILED(hr))
2066 goto done;
2067 memcpy(buffer_data, data, size);
2068 wined3d_buffer_unmap(device->vertex_buffer);
2069 device->vertex_buffer_pos = vb_pos + size;
2071 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2072 if (FAILED(hr))
2073 goto done;
2075 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2076 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2077 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2079 done:
2080 wined3d_mutex_unlock();
2081 return hr;
2084 /* The caller is responsible for wined3d locking */
2085 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2087 HRESULT hr;
2089 if (device->index_buffer_size < min_size || !device->index_buffer)
2091 UINT size = max(device->index_buffer_size * 2, min_size);
2092 struct wined3d_buffer *buffer;
2094 TRACE("Growing index buffer to %u bytes\n", size);
2096 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2097 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2098 if (FAILED(hr))
2100 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2101 return hr;
2104 if (device->index_buffer)
2105 wined3d_buffer_decref(device->index_buffer);
2107 device->index_buffer = buffer;
2108 device->index_buffer_size = size;
2109 device->index_buffer_pos = 0;
2111 return D3D_OK;
2114 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2115 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2116 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2117 const void *vertex_data, UINT vertex_stride)
2119 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2120 HRESULT hr;
2121 BYTE *buffer_data;
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 UINT ib_pos;
2128 UINT vtx_size = vertex_count * vertex_stride;
2129 UINT vb_pos, align;
2131 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2132 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2133 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2134 index_data, index_format, vertex_data, vertex_stride);
2136 if (!primitive_count)
2138 WARN("primitive_count is 0, returning D3D_OK\n");
2139 return D3D_OK;
2142 wined3d_mutex_lock();
2144 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2145 if (FAILED(hr))
2146 goto done;
2148 vb_pos = device->vertex_buffer_pos;
2149 align = vb_pos % vertex_stride;
2150 if (align) align = vertex_stride - align;
2151 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2152 vb_pos = 0;
2153 else
2154 vb_pos += align;
2156 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2157 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2158 if (FAILED(hr))
2159 goto done;
2160 memcpy(buffer_data, vertex_data, vtx_size);
2161 wined3d_buffer_unmap(device->vertex_buffer);
2162 device->vertex_buffer_pos = vb_pos + vtx_size;
2164 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2165 if (FAILED(hr))
2166 goto done;
2168 ib_pos = device->index_buffer_pos;
2169 align = ib_pos % idx_fmt_size;
2170 if (align) align = idx_fmt_size - align;
2171 if (ib_pos + idx_size + align > device->index_buffer_size)
2172 ib_pos = 0;
2173 else
2174 ib_pos += align;
2176 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2177 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2178 if (FAILED(hr))
2179 goto done;
2180 memcpy(buffer_data, index_data, idx_size);
2181 wined3d_buffer_unmap(device->index_buffer);
2182 device->index_buffer_pos = ib_pos + idx_size;
2184 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2185 if (FAILED(hr))
2186 goto done;
2188 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2189 wined3dformat_from_d3dformat(index_format));
2190 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2192 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2193 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2195 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2196 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2197 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2199 done:
2200 wined3d_mutex_unlock();
2201 return hr;
2204 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2205 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2207 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2208 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2209 HRESULT hr;
2211 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2212 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2214 wined3d_mutex_lock();
2215 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2216 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2217 wined3d_mutex_unlock();
2219 return hr;
2222 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2223 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2225 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2226 struct d3d8_vertex_shader *object;
2227 DWORD shader_handle;
2228 DWORD handle;
2229 HRESULT hr;
2231 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2232 iface, declaration, byte_code, shader, usage);
2234 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2235 if (!object)
2237 *shader = 0;
2238 return E_OUTOFMEMORY;
2241 wined3d_mutex_lock();
2242 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2243 wined3d_mutex_unlock();
2244 if (handle == D3D8_INVALID_HANDLE)
2246 ERR("Failed to allocate vertex shader handle.\n");
2247 HeapFree(GetProcessHeap(), 0, object);
2248 *shader = 0;
2249 return E_OUTOFMEMORY;
2252 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2254 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2255 if (FAILED(hr))
2257 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2258 wined3d_mutex_lock();
2259 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2260 wined3d_mutex_unlock();
2261 HeapFree(GetProcessHeap(), 0, object);
2262 *shader = 0;
2263 return hr;
2266 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2267 *shader = shader_handle;
2269 return D3D_OK;
2272 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2274 struct d3d8_vertex_declaration *d3d8_declaration;
2275 struct FvfToDecl *convertedDecls = device->decls;
2276 int p, low, high; /* deliberately signed */
2277 HRESULT hr;
2279 TRACE("Searching for declaration for fvf %08x... ", fvf);
2281 low = 0;
2282 high = device->numConvertedDecls - 1;
2283 while (low <= high)
2285 p = (low + high) >> 1;
2286 TRACE("%d ", p);
2288 if (convertedDecls[p].fvf == fvf)
2290 TRACE("found %p\n", convertedDecls[p].declaration);
2291 return convertedDecls[p].declaration;
2294 if (convertedDecls[p].fvf < fvf)
2295 low = p + 1;
2296 else
2297 high = p - 1;
2299 TRACE("not found. Creating and inserting at position %d.\n", low);
2301 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2302 return NULL;
2304 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2306 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2307 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2308 return NULL;
2311 if (device->declArraySize == device->numConvertedDecls)
2313 UINT grow = device->declArraySize / 2;
2315 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2316 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2317 if (!convertedDecls)
2319 d3d8_vertex_declaration_destroy(d3d8_declaration);
2320 return NULL;
2322 device->decls = convertedDecls;
2323 device->declArraySize += grow;
2326 memmove(convertedDecls + low + 1, convertedDecls + low,
2327 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2328 convertedDecls[low].declaration = d3d8_declaration;
2329 convertedDecls[low].fvf = fvf;
2330 ++device->numConvertedDecls;
2332 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2334 return d3d8_declaration;
2337 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2339 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2340 struct d3d8_vertex_shader *shader_impl;
2342 TRACE("iface %p, shader %#x.\n", iface, shader);
2344 if (VS_HIGHESTFIXEDFXF >= shader)
2346 TRACE("Setting FVF, %#x\n", shader);
2348 wined3d_mutex_lock();
2349 wined3d_device_set_vertex_declaration(device->wined3d_device,
2350 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2351 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2352 wined3d_mutex_unlock();
2354 return D3D_OK;
2357 TRACE("Setting shader\n");
2359 wined3d_mutex_lock();
2360 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2362 WARN("Invalid handle (%#x) passed.\n", shader);
2363 wined3d_mutex_unlock();
2365 return D3DERR_INVALIDCALL;
2368 wined3d_device_set_vertex_declaration(device->wined3d_device,
2369 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2370 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2371 wined3d_mutex_unlock();
2373 return D3D_OK;
2376 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2378 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2379 struct wined3d_vertex_declaration *wined3d_declaration;
2380 struct d3d8_vertex_declaration *d3d8_declaration;
2382 TRACE("iface %p, shader %p.\n", iface, shader);
2384 wined3d_mutex_lock();
2385 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2387 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2388 *shader = d3d8_declaration->shader_handle;
2390 else
2392 *shader = 0;
2394 wined3d_mutex_unlock();
2396 TRACE("Returning %#x.\n", *shader);
2398 return D3D_OK;
2401 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2403 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2404 struct d3d8_vertex_shader *shader_impl;
2406 TRACE("iface %p, shader %#x.\n", iface, shader);
2408 wined3d_mutex_lock();
2409 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2411 WARN("Invalid handle (%#x) passed.\n", shader);
2412 wined3d_mutex_unlock();
2414 return D3DERR_INVALIDCALL;
2417 if (shader_impl->wined3d_shader
2418 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2419 IDirect3DDevice8_SetVertexShader(iface, 0);
2421 wined3d_mutex_unlock();
2423 d3d8_vertex_shader_destroy(shader_impl);
2425 return D3D_OK;
2428 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2429 DWORD start_register, const void *data, DWORD count)
2431 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2432 HRESULT hr;
2434 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2435 iface, start_register, data, count);
2437 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2439 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2440 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2441 return D3DERR_INVALIDCALL;
2444 wined3d_mutex_lock();
2445 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2446 wined3d_mutex_unlock();
2448 return hr;
2451 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2452 DWORD start_register, void *data, DWORD count)
2454 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2455 HRESULT hr;
2457 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2458 iface, start_register, data, count);
2460 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2462 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2463 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2464 return D3DERR_INVALIDCALL;
2467 wined3d_mutex_lock();
2468 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2469 wined3d_mutex_unlock();
2471 return hr;
2474 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2475 DWORD shader, void *data, DWORD *data_size)
2477 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2478 struct d3d8_vertex_declaration *declaration;
2479 struct d3d8_vertex_shader *shader_impl;
2481 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2482 iface, shader, data, data_size);
2484 wined3d_mutex_lock();
2485 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2486 wined3d_mutex_unlock();
2488 if (!shader_impl)
2490 WARN("Invalid handle (%#x) passed.\n", shader);
2491 return D3DERR_INVALIDCALL;
2493 declaration = shader_impl->vertex_declaration;
2495 if (!data)
2497 *data_size = declaration->elements_size;
2498 return D3D_OK;
2501 /* MSDN claims that if *data_size is smaller than the required size
2502 * we should write the required size and return D3DERR_MOREDATA.
2503 * That's not actually true. */
2504 if (*data_size < declaration->elements_size)
2505 return D3DERR_INVALIDCALL;
2507 memcpy(data, declaration->elements, declaration->elements_size);
2509 return D3D_OK;
2512 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2513 DWORD shader, void *data, DWORD *data_size)
2515 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2516 struct d3d8_vertex_shader *shader_impl = NULL;
2517 HRESULT hr;
2519 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2520 iface, shader, data, data_size);
2522 wined3d_mutex_lock();
2523 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2525 WARN("Invalid handle (%#x) passed.\n", shader);
2526 wined3d_mutex_unlock();
2528 return D3DERR_INVALIDCALL;
2531 if (!shader_impl->wined3d_shader)
2533 wined3d_mutex_unlock();
2534 *data_size = 0;
2535 return D3D_OK;
2538 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2539 wined3d_mutex_unlock();
2541 return hr;
2544 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2545 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2547 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2548 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2550 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2552 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2553 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2554 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2555 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2556 * problem)
2558 wined3d_mutex_lock();
2559 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2560 wined3d_device_set_index_buffer(device->wined3d_device,
2561 ib ? ib->wined3d_buffer : NULL,
2562 ib ? ib->format : WINED3DFMT_UNKNOWN);
2563 wined3d_mutex_unlock();
2565 return D3D_OK;
2568 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2569 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2571 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2572 enum wined3d_format_id wined3d_format;
2573 struct wined3d_buffer *wined3d_buffer;
2574 struct d3d8_indexbuffer *buffer_impl;
2576 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2578 if (!buffer)
2579 return D3DERR_INVALIDCALL;
2581 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2582 wined3d_mutex_lock();
2583 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2584 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2586 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2587 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2588 IDirect3DIndexBuffer8_AddRef(*buffer);
2590 else
2592 *buffer = NULL;
2594 wined3d_mutex_unlock();
2596 return D3D_OK;
2599 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2600 const DWORD *byte_code, DWORD *shader)
2602 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2603 struct d3d8_pixel_shader *object;
2604 DWORD shader_handle;
2605 DWORD handle;
2606 HRESULT hr;
2608 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2610 if (!shader)
2611 return D3DERR_INVALIDCALL;
2613 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2614 if (!object)
2615 return E_OUTOFMEMORY;
2617 wined3d_mutex_lock();
2618 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2619 wined3d_mutex_unlock();
2620 if (handle == D3D8_INVALID_HANDLE)
2622 ERR("Failed to allocate pixel shader handle.\n");
2623 HeapFree(GetProcessHeap(), 0, object);
2624 return E_OUTOFMEMORY;
2627 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2629 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2630 if (FAILED(hr))
2632 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2633 wined3d_mutex_lock();
2634 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2635 wined3d_mutex_unlock();
2636 HeapFree(GetProcessHeap(), 0, object);
2637 *shader = 0;
2638 return hr;
2641 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2642 *shader = shader_handle;
2644 return D3D_OK;
2647 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2649 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2650 struct d3d8_pixel_shader *shader_impl;
2652 TRACE("iface %p, shader %#x.\n", iface, shader);
2654 wined3d_mutex_lock();
2656 if (!shader)
2658 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2659 wined3d_mutex_unlock();
2660 return D3D_OK;
2663 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2665 WARN("Invalid handle (%#x) passed.\n", shader);
2666 wined3d_mutex_unlock();
2667 return D3DERR_INVALIDCALL;
2670 TRACE("Setting shader %p.\n", shader_impl);
2671 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2672 wined3d_mutex_unlock();
2674 return D3D_OK;
2677 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2679 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2680 struct wined3d_shader *object;
2682 TRACE("iface %p, shader %p.\n", iface, shader);
2684 if (!shader)
2685 return D3DERR_INVALIDCALL;
2687 wined3d_mutex_lock();
2688 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2690 struct d3d8_pixel_shader *d3d8_shader;
2691 d3d8_shader = wined3d_shader_get_parent(object);
2692 *shader = d3d8_shader->handle;
2694 else
2696 *shader = 0;
2698 wined3d_mutex_unlock();
2700 TRACE("Returning %#x.\n", *shader);
2702 return D3D_OK;
2705 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2707 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2708 struct d3d8_pixel_shader *shader_impl;
2710 TRACE("iface %p, shader %#x.\n", iface, shader);
2712 wined3d_mutex_lock();
2714 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2716 WARN("Invalid handle (%#x) passed.\n", shader);
2717 wined3d_mutex_unlock();
2718 return D3DERR_INVALIDCALL;
2721 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2722 IDirect3DDevice8_SetPixelShader(iface, 0);
2724 wined3d_mutex_unlock();
2726 d3d8_pixel_shader_destroy(shader_impl);
2728 return D3D_OK;
2731 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2732 DWORD start_register, const void *data, DWORD count)
2734 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2735 HRESULT hr;
2737 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2738 iface, start_register, data, count);
2740 wined3d_mutex_lock();
2741 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2742 wined3d_mutex_unlock();
2744 return hr;
2747 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2748 DWORD start_register, void *data, DWORD count)
2750 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2751 HRESULT hr;
2753 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2754 iface, start_register, data, count);
2756 wined3d_mutex_lock();
2757 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2758 wined3d_mutex_unlock();
2760 return hr;
2763 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2764 DWORD shader, void *data, DWORD *data_size)
2766 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2767 struct d3d8_pixel_shader *shader_impl = NULL;
2768 HRESULT hr;
2770 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2771 iface, shader, data, data_size);
2773 wined3d_mutex_lock();
2774 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2776 WARN("Invalid handle (%#x) passed.\n", shader);
2777 wined3d_mutex_unlock();
2779 return D3DERR_INVALIDCALL;
2782 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2783 wined3d_mutex_unlock();
2785 return hr;
2788 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2789 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2791 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2792 iface, handle, segment_count, patch_info);
2793 return D3D_OK;
2796 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2797 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2799 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2800 iface, handle, segment_count, patch_info);
2801 return D3D_OK;
2804 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2806 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2807 return D3DERR_INVALIDCALL;
2810 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2811 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2813 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2814 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2815 HRESULT hr;
2817 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2818 iface, stream_idx, buffer, stride);
2820 wined3d_mutex_lock();
2821 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2822 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2823 wined3d_mutex_unlock();
2825 return hr;
2828 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2829 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2831 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2832 struct d3d8_vertexbuffer *buffer_impl;
2833 struct wined3d_buffer *wined3d_buffer = NULL;
2834 HRESULT hr;
2836 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2837 iface, stream_idx, buffer, stride);
2839 if (!buffer)
2840 return D3DERR_INVALIDCALL;
2842 wined3d_mutex_lock();
2843 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2844 if (SUCCEEDED(hr) && wined3d_buffer)
2846 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2847 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2848 IDirect3DVertexBuffer8_AddRef(*buffer);
2850 else
2852 if (FAILED(hr))
2853 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2854 *buffer = NULL;
2856 wined3d_mutex_unlock();
2858 return hr;
2861 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2863 d3d8_device_QueryInterface,
2864 d3d8_device_AddRef,
2865 d3d8_device_Release,
2866 d3d8_device_TestCooperativeLevel,
2867 d3d8_device_GetAvailableTextureMem,
2868 d3d8_device_ResourceManagerDiscardBytes,
2869 d3d8_device_GetDirect3D,
2870 d3d8_device_GetDeviceCaps,
2871 d3d8_device_GetDisplayMode,
2872 d3d8_device_GetCreationParameters,
2873 d3d8_device_SetCursorProperties,
2874 d3d8_device_SetCursorPosition,
2875 d3d8_device_ShowCursor,
2876 d3d8_device_CreateAdditionalSwapChain,
2877 d3d8_device_Reset,
2878 d3d8_device_Present,
2879 d3d8_device_GetBackBuffer,
2880 d3d8_device_GetRasterStatus,
2881 d3d8_device_SetGammaRamp,
2882 d3d8_device_GetGammaRamp,
2883 d3d8_device_CreateTexture,
2884 d3d8_device_CreateVolumeTexture,
2885 d3d8_device_CreateCubeTexture,
2886 d3d8_device_CreateVertexBuffer,
2887 d3d8_device_CreateIndexBuffer,
2888 d3d8_device_CreateRenderTarget,
2889 d3d8_device_CreateDepthStencilSurface,
2890 d3d8_device_CreateImageSurface,
2891 d3d8_device_CopyRects,
2892 d3d8_device_UpdateTexture,
2893 d3d8_device_GetFrontBuffer,
2894 d3d8_device_SetRenderTarget,
2895 d3d8_device_GetRenderTarget,
2896 d3d8_device_GetDepthStencilSurface,
2897 d3d8_device_BeginScene,
2898 d3d8_device_EndScene,
2899 d3d8_device_Clear,
2900 d3d8_device_SetTransform,
2901 d3d8_device_GetTransform,
2902 d3d8_device_MultiplyTransform,
2903 d3d8_device_SetViewport,
2904 d3d8_device_GetViewport,
2905 d3d8_device_SetMaterial,
2906 d3d8_device_GetMaterial,
2907 d3d8_device_SetLight,
2908 d3d8_device_GetLight,
2909 d3d8_device_LightEnable,
2910 d3d8_device_GetLightEnable,
2911 d3d8_device_SetClipPlane,
2912 d3d8_device_GetClipPlane,
2913 d3d8_device_SetRenderState,
2914 d3d8_device_GetRenderState,
2915 d3d8_device_BeginStateBlock,
2916 d3d8_device_EndStateBlock,
2917 d3d8_device_ApplyStateBlock,
2918 d3d8_device_CaptureStateBlock,
2919 d3d8_device_DeleteStateBlock,
2920 d3d8_device_CreateStateBlock,
2921 d3d8_device_SetClipStatus,
2922 d3d8_device_GetClipStatus,
2923 d3d8_device_GetTexture,
2924 d3d8_device_SetTexture,
2925 d3d8_device_GetTextureStageState,
2926 d3d8_device_SetTextureStageState,
2927 d3d8_device_ValidateDevice,
2928 d3d8_device_GetInfo,
2929 d3d8_device_SetPaletteEntries,
2930 d3d8_device_GetPaletteEntries,
2931 d3d8_device_SetCurrentTexturePalette,
2932 d3d8_device_GetCurrentTexturePalette,
2933 d3d8_device_DrawPrimitive,
2934 d3d8_device_DrawIndexedPrimitive,
2935 d3d8_device_DrawPrimitiveUP,
2936 d3d8_device_DrawIndexedPrimitiveUP,
2937 d3d8_device_ProcessVertices,
2938 d3d8_device_CreateVertexShader,
2939 d3d8_device_SetVertexShader,
2940 d3d8_device_GetVertexShader,
2941 d3d8_device_DeleteVertexShader,
2942 d3d8_device_SetVertexShaderConstant,
2943 d3d8_device_GetVertexShaderConstant,
2944 d3d8_device_GetVertexShaderDeclaration,
2945 d3d8_device_GetVertexShaderFunction,
2946 d3d8_device_SetStreamSource,
2947 d3d8_device_GetStreamSource,
2948 d3d8_device_SetIndices,
2949 d3d8_device_GetIndices,
2950 d3d8_device_CreatePixelShader,
2951 d3d8_device_SetPixelShader,
2952 d3d8_device_GetPixelShader,
2953 d3d8_device_DeletePixelShader,
2954 d3d8_device_SetPixelShaderConstant,
2955 d3d8_device_GetPixelShaderConstant,
2956 d3d8_device_GetPixelShaderFunction,
2957 d3d8_device_DrawRectPatch,
2958 d3d8_device_DrawTriPatch,
2959 d3d8_device_DeletePatch,
2962 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2964 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2967 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2968 struct wined3d_device *device)
2970 TRACE("device_parent %p, device %p\n", device_parent, device);
2973 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2975 TRACE("device_parent %p.\n", device_parent);
2978 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2980 struct d3d8_device *device = device_from_device_parent(device_parent);
2982 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2984 if (!activate)
2985 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2986 else
2987 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2990 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2991 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
2992 void **parent, const struct wined3d_parent_ops **parent_ops)
2994 struct d3d8_surface *d3d_surface;
2996 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
2997 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
2999 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3000 return E_OUTOFMEMORY;
3002 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3003 *parent = d3d_surface;
3004 TRACE("Created surface %p.\n", d3d_surface);
3006 return D3D_OK;
3009 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3010 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3011 void **parent, const struct wined3d_parent_ops **parent_ops)
3013 struct d3d8_volume *d3d_volume;
3015 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3016 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3018 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3019 return E_OUTOFMEMORY;
3021 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3022 *parent = d3d_volume;
3023 TRACE("Created volume %p.\n", d3d_volume);
3025 return D3D_OK;
3028 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3029 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_texture **texture)
3031 struct d3d8_device *device = device_from_device_parent(device_parent);
3032 struct d3d8_surface *d3d_surface;
3033 HRESULT hr;
3035 TRACE("device_parent %p, container_parent %p, desc %p, texture %p.\n",
3036 device_parent, container_parent, desc, texture);
3038 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, WINED3D_TEXTURE_CREATE_MAPPABLE,
3039 NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, texture)))
3041 WARN("Failed to create texture, hr %#x.\n", hr);
3042 return hr;
3045 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3046 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3048 return hr;
3051 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3052 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3054 struct d3d8_device *device = device_from_device_parent(device_parent);
3055 struct d3d8_swapchain *d3d_swapchain;
3056 HRESULT hr;
3058 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3060 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3062 WARN("Failed to create swapchain, hr %#x.\n", hr);
3063 *swapchain = NULL;
3064 return hr;
3067 *swapchain = d3d_swapchain->wined3d_swapchain;
3068 wined3d_swapchain_incref(*swapchain);
3069 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3071 return hr;
3074 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3076 device_parent_wined3d_device_created,
3077 device_parent_mode_changed,
3078 device_parent_activate,
3079 device_parent_surface_created,
3080 device_parent_volume_created,
3081 device_parent_create_swapchain_texture,
3082 device_parent_create_swapchain,
3085 static void setup_fpu(void)
3087 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3088 WORD cw;
3089 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3090 cw = (cw & ~0xf3f) | 0x3f;
3091 __asm__ volatile ("fldcw %0" : : "m" (cw));
3092 #elif defined(__i386__) && defined(_MSC_VER)
3093 WORD cw;
3094 __asm fnstcw cw;
3095 cw = (cw & ~0xf3f) | 0x3f;
3096 __asm fldcw cw;
3097 #else
3098 FIXME("FPU setup not implemented for this platform.\n");
3099 #endif
3102 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3103 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3105 struct wined3d_swapchain_desc swapchain_desc;
3106 struct wined3d_swapchain *wined3d_swapchain;
3107 HRESULT hr;
3109 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3110 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3111 device->ref = 1;
3112 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3113 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3114 if (!device->handle_table.entries)
3116 ERR("Failed to allocate handle table memory.\n");
3117 return E_OUTOFMEMORY;
3119 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3121 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3123 wined3d_mutex_lock();
3124 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3125 &device->device_parent, &device->wined3d_device);
3126 if (FAILED(hr))
3128 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3129 wined3d_mutex_unlock();
3130 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3131 return hr;
3134 if (!parameters->Windowed)
3136 HWND device_window = parameters->hDeviceWindow;
3138 if (!focus_window)
3139 focus_window = device_window;
3140 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3142 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3143 wined3d_device_decref(device->wined3d_device);
3144 wined3d_mutex_unlock();
3145 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3146 return hr;
3149 if (!device_window)
3150 device_window = focus_window;
3151 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3152 parameters->BackBufferWidth,
3153 parameters->BackBufferHeight);
3156 if (flags & D3DCREATE_MULTITHREADED)
3157 wined3d_device_set_multithreaded(device->wined3d_device);
3159 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3161 wined3d_device_release_focus_window(device->wined3d_device);
3162 wined3d_device_decref(device->wined3d_device);
3163 wined3d_mutex_unlock();
3164 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3165 return D3DERR_INVALIDCALL;
3168 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3169 if (FAILED(hr))
3171 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3172 wined3d_device_release_focus_window(device->wined3d_device);
3173 wined3d_device_decref(device->wined3d_device);
3174 wined3d_mutex_unlock();
3175 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3176 return hr;
3179 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3180 wined3d_mutex_unlock();
3182 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3184 device->declArraySize = 16;
3185 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3186 if (!device->decls)
3188 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3189 hr = E_OUTOFMEMORY;
3190 goto err;
3193 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3194 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3196 device->d3d_parent = &parent->IDirect3D8_iface;
3197 IDirect3D8_AddRef(device->d3d_parent);
3199 return D3D_OK;
3201 err:
3202 wined3d_mutex_lock();
3203 wined3d_device_uninit_3d(device->wined3d_device);
3204 wined3d_device_release_focus_window(device->wined3d_device);
3205 wined3d_device_decref(device->wined3d_device);
3206 wined3d_mutex_unlock();
3207 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3208 return hr;