d3d9/tests: Use a separate device for texkill_test().
[wine/multimedia.git] / dlls / d3d8 / device.c
blobd28ada5745275eaebf1ae4c8d6b40ae4e5b5ed46
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
41 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
43 d3d8_null_wined3d_object_destroyed,
46 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
48 BYTE *c = (BYTE *)&format;
50 /* Don't translate FOURCC formats */
51 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
53 switch(format)
55 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
56 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
57 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
58 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
59 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
60 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
61 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
62 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
63 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
64 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
65 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
66 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
67 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
68 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
69 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
70 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
71 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
72 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
73 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
74 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
75 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
76 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
77 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
78 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
79 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
80 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
81 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
82 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
83 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
84 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
85 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
86 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
87 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
88 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
89 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
90 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
91 default:
92 FIXME("Unhandled wined3d format %#x.\n", format);
93 return D3DFMT_UNKNOWN;
97 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
99 BYTE *c = (BYTE *)&format;
101 /* Don't translate FOURCC formats */
102 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
104 switch(format)
106 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
107 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
108 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
109 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
110 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
111 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
112 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
113 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
114 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
115 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
116 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
117 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
118 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
119 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
120 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
131 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
132 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
133 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
134 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
135 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
136 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
137 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
138 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
139 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
140 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
141 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
142 default:
143 FIXME("Unhandled D3DFORMAT %#x\n", format);
144 return WINED3DFMT_UNKNOWN;
148 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
150 switch(primitive_type)
152 case D3DPT_POINTLIST:
153 return primitive_count;
155 case D3DPT_LINELIST:
156 return primitive_count * 2;
158 case D3DPT_LINESTRIP:
159 return primitive_count + 1;
161 case D3DPT_TRIANGLELIST:
162 return primitive_count * 3;
164 case D3DPT_TRIANGLESTRIP:
165 case D3DPT_TRIANGLEFAN:
166 return primitive_count + 2;
168 default:
169 FIXME("Unhandled primitive type %#x\n", primitive_type);
170 return 0;
174 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
175 const struct wined3d_swapchain_desc *swapchain_desc)
177 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
178 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
179 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
180 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
181 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
182 present_parameters->SwapEffect = swapchain_desc->swap_effect;
183 present_parameters->hDeviceWindow = swapchain_desc->device_window;
184 present_parameters->Windowed = swapchain_desc->windowed;
185 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
186 present_parameters->AutoDepthStencilFormat
187 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
188 present_parameters->Flags = swapchain_desc->flags;
189 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
190 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
193 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
194 const D3DPRESENT_PARAMETERS *present_parameters)
196 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
197 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
198 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
199 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
200 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
201 swapchain_desc->multisample_quality = 0; /* d3d9 only */
202 swapchain_desc->swap_effect = present_parameters->SwapEffect;
203 swapchain_desc->device_window = present_parameters->hDeviceWindow;
204 swapchain_desc->windowed = present_parameters->Windowed;
205 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
206 swapchain_desc->auto_depth_stencil_format
207 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
208 swapchain_desc->flags = present_parameters->Flags;
209 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
210 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
211 swapchain_desc->auto_restore_display_mode = TRUE;
214 /* Handle table functions */
215 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
217 struct d3d8_handle_entry *entry;
219 if (t->free_entries)
221 DWORD index = t->free_entries - t->entries;
222 /* Use a free handle */
223 entry = t->free_entries;
224 if (entry->type != D3D8_HANDLE_FREE)
226 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
227 return D3D8_INVALID_HANDLE;
229 t->free_entries = entry->object;
230 entry->object = object;
231 entry->type = type;
233 return index;
236 if (!(t->entry_count < t->table_size))
238 /* Grow the table */
239 UINT new_size = t->table_size + (t->table_size >> 1);
240 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
241 0, t->entries, new_size * sizeof(*t->entries));
242 if (!new_entries)
244 ERR("Failed to grow the handle table.\n");
245 return D3D8_INVALID_HANDLE;
247 t->entries = new_entries;
248 t->table_size = new_size;
251 entry = &t->entries[t->entry_count];
252 entry->object = object;
253 entry->type = type;
255 return t->entry_count++;
258 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
260 struct d3d8_handle_entry *entry;
261 void *object;
263 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
265 WARN("Invalid handle %u passed.\n", handle);
266 return NULL;
269 entry = &t->entries[handle];
270 if (entry->type != type)
272 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
273 return NULL;
276 object = entry->object;
277 entry->object = t->free_entries;
278 entry->type = D3D8_HANDLE_FREE;
279 t->free_entries = entry;
281 return object;
284 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
286 struct d3d8_handle_entry *entry;
288 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
290 WARN("Invalid handle %u passed.\n", handle);
291 return NULL;
294 entry = &t->entries[handle];
295 if (entry->type != type)
297 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
298 return NULL;
301 return entry->object;
304 static inline struct d3d8_device *impl_from_IDirect3DDevice8(IDirect3DDevice8 *iface)
306 return CONTAINING_RECORD(iface, struct d3d8_device, IDirect3DDevice8_iface);
309 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
311 TRACE("iface %p, riid %s, out %p.\n",
312 iface, debugstr_guid(riid), out);
314 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
315 || IsEqualGUID(riid, &IID_IUnknown))
317 IDirect3DDevice8_AddRef(iface);
318 *out = iface;
319 return S_OK;
322 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
324 *out = NULL;
325 return E_NOINTERFACE;
328 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
330 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
331 ULONG ref = InterlockedIncrement(&device->ref);
333 TRACE("%p increasing refcount to %u.\n", iface, ref);
335 return ref;
338 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
340 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
341 ULONG ref;
343 if (device->inDestruction)
344 return 0;
346 ref = InterlockedDecrement(&device->ref);
348 TRACE("%p decreasing refcount to %u.\n", iface, ref);
350 if (!ref)
352 IDirect3D8 *parent = device->d3d_parent;
353 unsigned i;
355 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
357 wined3d_mutex_lock();
359 device->inDestruction = TRUE;
361 for (i = 0; i < device->numConvertedDecls; ++i)
363 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
365 HeapFree(GetProcessHeap(), 0, device->decls);
367 if (device->vertex_buffer)
368 wined3d_buffer_decref(device->vertex_buffer);
369 if (device->index_buffer)
370 wined3d_buffer_decref(device->index_buffer);
372 wined3d_device_uninit_3d(device->wined3d_device);
373 wined3d_device_release_focus_window(device->wined3d_device);
374 wined3d_device_decref(device->wined3d_device);
375 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
376 HeapFree(GetProcessHeap(), 0, device);
378 wined3d_mutex_unlock();
380 IDirect3D8_Release(parent);
382 return ref;
385 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
387 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
389 TRACE("iface %p.\n", iface);
391 if (device->lost)
393 TRACE("Device is lost.\n");
394 return D3DERR_DEVICENOTRESET;
397 return D3D_OK;
400 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
402 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
403 HRESULT hr;
405 TRACE("iface %p.\n", iface);
407 wined3d_mutex_lock();
408 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
409 wined3d_mutex_unlock();
411 return hr;
414 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
418 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
420 if (byte_count)
421 FIXME("Byte count ignored.\n");
423 wined3d_mutex_lock();
424 wined3d_device_evict_managed_resources(device->wined3d_device);
425 wined3d_mutex_unlock();
427 return D3D_OK;
430 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
432 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
434 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
436 if (!d3d8)
437 return D3DERR_INVALIDCALL;
439 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
442 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
444 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
445 WINED3DCAPS *wined3d_caps;
446 HRESULT hr;
448 TRACE("iface %p, caps %p.\n", iface, caps);
450 if (!caps)
451 return D3DERR_INVALIDCALL;
453 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
454 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
456 wined3d_mutex_lock();
457 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
458 wined3d_mutex_unlock();
460 fixup_caps(wined3d_caps);
461 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
462 HeapFree(GetProcessHeap(), 0, wined3d_caps);
464 return hr;
467 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
469 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
470 struct wined3d_display_mode wined3d_mode;
471 HRESULT hr;
473 TRACE("iface %p, mode %p.\n", iface, mode);
475 wined3d_mutex_lock();
476 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
477 wined3d_mutex_unlock();
479 if (SUCCEEDED(hr))
481 mode->Width = wined3d_mode.width;
482 mode->Height = wined3d_mode.height;
483 mode->RefreshRate = wined3d_mode.refresh_rate;
484 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
487 return hr;
490 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
491 D3DDEVICE_CREATION_PARAMETERS *parameters)
493 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
495 TRACE("iface %p, parameters %p.\n", iface, parameters);
497 wined3d_mutex_lock();
498 wined3d_device_get_creation_parameters(device->wined3d_device,
499 (struct wined3d_device_creation_parameters *)parameters);
500 wined3d_mutex_unlock();
502 return D3D_OK;
505 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
506 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
508 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
509 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
510 HRESULT hr;
512 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
513 iface, hotspot_x, hotspot_y, bitmap);
515 if (!bitmap)
517 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
518 return D3DERR_INVALIDCALL;
521 wined3d_mutex_lock();
522 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
523 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
524 wined3d_mutex_unlock();
526 return hr;
529 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
531 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
533 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
535 wined3d_mutex_lock();
536 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
537 wined3d_mutex_unlock();
540 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
542 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
543 BOOL ret;
545 TRACE("iface %p, show %#x.\n", iface, show);
547 wined3d_mutex_lock();
548 ret = wined3d_device_show_cursor(device->wined3d_device, show);
549 wined3d_mutex_unlock();
551 return ret;
554 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
555 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
557 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
558 struct wined3d_swapchain_desc desc;
559 struct d3d8_swapchain *object;
561 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
562 iface, present_parameters, swapchain);
564 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
565 if (SUCCEEDED(d3d8_swapchain_create(device, &desc, &object)))
566 *swapchain = &object->IDirect3DSwapChain8_iface;
567 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
569 return D3D_OK;
572 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
574 struct wined3d_resource_desc desc;
576 wined3d_resource_get_desc(resource, &desc);
577 if (desc.pool == WINED3D_POOL_DEFAULT)
579 struct d3d8_surface *surface;
581 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
583 IUnknown *parent = wined3d_resource_get_parent(resource);
584 IDirect3DBaseTexture8 *texture;
586 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
588 IDirect3DBaseTexture8_Release(texture);
589 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
590 return D3DERR_DEVICELOST;
593 return D3D_OK;
596 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
598 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
599 return D3DERR_DEVICELOST;
602 surface = wined3d_resource_get_parent(resource);
603 if (surface->refcount)
605 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
606 return D3DERR_DEVICELOST;
609 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
612 return D3D_OK;
615 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
616 D3DPRESENT_PARAMETERS *present_parameters)
618 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
619 struct wined3d_swapchain_desc swapchain_desc;
620 HRESULT hr;
622 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
624 wined3d_mutex_lock();
626 if (device->vertex_buffer)
628 wined3d_buffer_decref(device->vertex_buffer);
629 device->vertex_buffer = NULL;
630 device->vertex_buffer_size = 0;
632 if (device->index_buffer)
634 wined3d_buffer_decref(device->index_buffer);
635 device->index_buffer = NULL;
636 device->index_buffer_size = 0;
639 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
640 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
641 NULL, reset_enum_callback, TRUE)))
643 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
644 device->lost = FALSE;
646 else
648 device->lost = TRUE;
650 wined3d_mutex_unlock();
652 return hr;
655 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
656 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
658 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
659 HRESULT hr;
661 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
662 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
664 wined3d_mutex_lock();
665 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
666 dst_window_override, dirty_region, 0);
667 wined3d_mutex_unlock();
669 return hr;
672 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
673 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
675 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
676 struct wined3d_surface *wined3d_surface = NULL;
677 struct d3d8_surface *surface_impl;
678 HRESULT hr;
680 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
681 iface, backbuffer_idx, backbuffer_type, backbuffer);
683 wined3d_mutex_lock();
684 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
685 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
686 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
688 surface_impl = wined3d_surface_get_parent(wined3d_surface);
689 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
690 IDirect3DSurface8_AddRef(*backbuffer);
692 wined3d_mutex_unlock();
694 return hr;
697 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
699 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
700 HRESULT hr;
702 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
704 wined3d_mutex_lock();
705 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
706 wined3d_mutex_unlock();
708 return hr;
711 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
713 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
715 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
717 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
718 wined3d_mutex_lock();
719 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
720 wined3d_mutex_unlock();
723 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
725 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
727 TRACE("iface %p, ramp %p.\n", iface, ramp);
729 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
730 wined3d_mutex_lock();
731 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
732 wined3d_mutex_unlock();
735 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
736 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
737 D3DPOOL pool, IDirect3DTexture8 **texture)
739 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
740 struct d3d8_texture *object;
741 HRESULT hr;
743 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
744 iface, width, height, levels, usage, format, pool, texture);
746 *texture = NULL;
747 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
748 if (!object)
749 return D3DERR_OUTOFVIDEOMEMORY;
751 hr = texture_init(object, device, width, height, levels, usage, format, pool);
752 if (FAILED(hr))
754 WARN("Failed to initialize texture, hr %#x.\n", hr);
755 HeapFree(GetProcessHeap(), 0, object);
756 return hr;
759 TRACE("Created texture %p.\n", object);
760 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
762 return D3D_OK;
765 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
766 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
767 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
770 struct d3d8_texture *object;
771 HRESULT hr;
773 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
774 iface, width, height, depth, levels, usage, format, pool, texture);
776 *texture = NULL;
777 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
778 if (!object)
779 return D3DERR_OUTOFVIDEOMEMORY;
781 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
782 if (FAILED(hr))
784 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
785 HeapFree(GetProcessHeap(), 0, object);
786 return hr;
789 TRACE("Created volume texture %p.\n", object);
790 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
792 return D3D_OK;
795 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
796 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
798 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
799 struct d3d8_texture *object;
800 HRESULT hr;
802 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
803 iface, edge_length, levels, usage, format, pool, texture);
805 *texture = NULL;
806 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
807 if (!object)
808 return D3DERR_OUTOFVIDEOMEMORY;
810 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
811 if (FAILED(hr))
813 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
814 HeapFree(GetProcessHeap(), 0, object);
815 return hr;
818 TRACE("Created cube texture %p.\n", object);
819 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
821 return hr;
824 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
825 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
827 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
828 struct d3d8_vertexbuffer *object;
829 HRESULT hr;
831 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
832 iface, size, usage, fvf, pool, buffer);
834 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
835 if (!object)
836 return D3DERR_OUTOFVIDEOMEMORY;
838 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
839 if (FAILED(hr))
841 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
842 HeapFree(GetProcessHeap(), 0, object);
843 return hr;
846 TRACE("Created vertex buffer %p.\n", object);
847 *buffer = &object->IDirect3DVertexBuffer8_iface;
849 return D3D_OK;
852 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
853 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
855 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
856 struct d3d8_indexbuffer *object;
857 HRESULT hr;
859 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
860 iface, size, usage, format, pool, buffer);
862 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
863 if (!object)
864 return D3DERR_OUTOFVIDEOMEMORY;
866 hr = indexbuffer_init(object, device, size, usage, format, pool);
867 if (FAILED(hr))
869 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
870 HeapFree(GetProcessHeap(), 0, object);
871 return hr;
874 TRACE("Created index buffer %p.\n", object);
875 *buffer = &object->IDirect3DIndexBuffer8_iface;
877 return D3D_OK;
880 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
881 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
882 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
884 struct wined3d_resource *sub_resource;
885 struct wined3d_resource_desc desc;
886 struct d3d8_surface *surface_impl;
887 struct wined3d_texture *texture;
888 HRESULT hr;
890 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
891 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
892 device, width, height, format, flags, surface,
893 usage, pool, multisample_type, multisample_quality);
895 desc.resource_type = WINED3D_RTYPE_TEXTURE;
896 desc.format = wined3dformat_from_d3dformat(format);
897 desc.multisample_type = multisample_type;
898 desc.multisample_quality = multisample_quality;
899 desc.usage = usage & WINED3DUSAGE_MASK;
900 desc.pool = pool;
901 desc.width = width;
902 desc.height = height;
903 desc.depth = 1;
904 desc.size = 0;
906 wined3d_mutex_lock();
908 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
909 1, flags, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
911 wined3d_mutex_unlock();
912 WARN("Failed to create texture, hr %#x.\n", hr);
913 return hr;
916 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
917 surface_impl = wined3d_resource_get_parent(sub_resource);
918 surface_impl->forwardReference = NULL;
919 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
920 *surface = &surface_impl->IDirect3DSurface8_iface;
921 IDirect3DSurface8_AddRef(*surface);
922 wined3d_texture_decref(texture);
924 wined3d_mutex_unlock();
926 return D3D_OK;
929 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
930 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
931 IDirect3DSurface8 **surface)
933 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
934 DWORD flags = 0;
936 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
937 iface, width, height, format, multisample_type, lockable, surface);
939 *surface = NULL;
940 if (lockable)
941 flags |= WINED3D_SURFACE_MAPPABLE;
943 return d3d8_device_create_surface(device, width, height, format, flags, surface,
944 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
947 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
948 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
949 IDirect3DSurface8 **surface)
951 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
953 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
954 iface, width, height, format, multisample_type, surface);
956 *surface = NULL;
958 /* TODO: Verify that Discard is false */
959 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
960 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
963 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
964 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
965 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
967 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
969 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
970 iface, width, height, format, surface);
972 *surface = NULL;
974 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
975 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
978 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
979 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
980 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
982 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
983 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
984 enum wined3d_format_id src_format, dst_format;
985 struct wined3d_resource_desc wined3d_desc;
986 struct wined3d_resource *wined3d_resource;
987 UINT src_w, src_h;
988 HRESULT hr;
990 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
991 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
993 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
994 * destination texture is in WINED3D_POOL_DEFAULT. */
996 wined3d_mutex_lock();
997 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
998 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
999 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1001 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1002 wined3d_mutex_unlock();
1003 return D3DERR_INVALIDCALL;
1005 src_format = wined3d_desc.format;
1006 src_w = wined3d_desc.width;
1007 src_h = wined3d_desc.height;
1009 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1010 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1011 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1013 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1014 wined3d_mutex_unlock();
1015 return D3DERR_INVALIDCALL;
1017 dst_format = wined3d_desc.format;
1019 /* Check that the source and destination formats match */
1020 if (src_format != dst_format && WINED3DFMT_UNKNOWN != dst_format)
1022 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1023 src_surface, dst_surface);
1024 wined3d_mutex_unlock();
1025 return D3DERR_INVALIDCALL;
1027 else if (WINED3DFMT_UNKNOWN == dst_format)
1029 TRACE("Converting destination surface from WINED3DFMT_UNKNOWN to the source format.\n");
1030 if (FAILED(hr = wined3d_surface_update_desc(dst->wined3d_surface, wined3d_desc.width, wined3d_desc.height,
1031 src_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality, NULL, 0)))
1033 WARN("Failed to update surface desc, hr %#x.\n", hr);
1034 wined3d_mutex_unlock();
1035 return hr;
1039 /* Quick if complete copy ... */
1040 if (!rect_count && !src_rects && !dst_points)
1042 RECT rect = {0, 0, src_w, src_h};
1043 wined3d_surface_blt(dst->wined3d_surface, &rect,
1044 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
1046 else
1048 unsigned int i;
1049 /* Copy rect by rect */
1050 if (src_rects && dst_points)
1052 for (i = 0; i < rect_count; ++i)
1054 UINT w = src_rects[i].right - src_rects[i].left;
1055 UINT h = src_rects[i].bottom - src_rects[i].top;
1056 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1057 dst_points[i].x + w, dst_points[i].y + h};
1059 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1060 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1063 else
1065 for (i = 0; i < rect_count; ++i)
1067 UINT w = src_rects[i].right - src_rects[i].left;
1068 UINT h = src_rects[i].bottom - src_rects[i].top;
1069 RECT dst_rect = {0, 0, w, h};
1071 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1072 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1076 wined3d_mutex_unlock();
1078 return WINED3D_OK;
1081 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1082 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1084 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1085 struct d3d8_texture *src_impl, *dst_impl;
1086 HRESULT hr;
1088 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1090 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1091 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1093 wined3d_mutex_lock();
1094 hr = wined3d_device_update_texture(device->wined3d_device,
1095 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1096 wined3d_mutex_unlock();
1098 return hr;
1101 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1103 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1104 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1105 HRESULT hr;
1107 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1109 if (!dst_surface)
1111 WARN("Invalid destination surface passed.\n");
1112 return D3DERR_INVALIDCALL;
1115 wined3d_mutex_lock();
1116 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1117 wined3d_mutex_unlock();
1119 return hr;
1122 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1123 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1125 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1126 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1127 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1128 struct wined3d_surface *original_ds = NULL;
1129 HRESULT hr = D3D_OK;
1131 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1133 wined3d_mutex_lock();
1135 if (ds_impl)
1137 struct wined3d_resource_desc ds_desc, rt_desc;
1138 struct wined3d_resource *wined3d_resource;
1139 struct wined3d_surface *original_rt = NULL;
1141 /* If no render target is passed in check the size against the current RT */
1142 if (!render_target)
1144 if (!(original_rt = wined3d_device_get_render_target(device->wined3d_device, 0)))
1146 wined3d_mutex_unlock();
1147 return D3DERR_NOTFOUND;
1149 wined3d_resource = wined3d_surface_get_resource(original_rt);
1151 else
1152 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1153 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1155 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1156 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1158 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1160 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1161 wined3d_mutex_unlock();
1162 return D3DERR_INVALIDCALL;
1166 original_ds = wined3d_device_get_depth_stencil(device->wined3d_device);
1167 wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1168 if (render_target)
1170 hr = wined3d_device_set_render_target(device->wined3d_device, 0, rt_impl->wined3d_surface, TRUE);
1171 if (FAILED(hr))
1172 wined3d_device_set_depth_stencil(device->wined3d_device, original_ds);
1175 wined3d_mutex_unlock();
1177 return hr;
1180 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1182 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1183 struct wined3d_surface *wined3d_surface;
1184 struct d3d8_surface *surface_impl;
1185 HRESULT hr;
1187 TRACE("iface %p, render_target %p.\n", iface, render_target);
1189 if (!render_target)
1190 return D3DERR_INVALIDCALL;
1192 wined3d_mutex_lock();
1193 if ((wined3d_surface = wined3d_device_get_render_target(device->wined3d_device, 0)))
1195 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1196 *render_target = &surface_impl->IDirect3DSurface8_iface;
1197 IDirect3DSurface8_AddRef(*render_target);
1198 hr = D3D_OK;
1200 else
1202 ERR("Failed to get wined3d render target.\n");
1203 *render_target = NULL;
1204 hr = D3DERR_NOTFOUND;
1206 wined3d_mutex_unlock();
1208 return hr;
1211 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1213 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1214 struct wined3d_surface *wined3d_surface;
1215 struct d3d8_surface *surface_impl;
1216 HRESULT hr = D3D_OK;
1218 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1220 if (!depth_stencil)
1221 return D3DERR_INVALIDCALL;
1223 wined3d_mutex_lock();
1224 if ((wined3d_surface = wined3d_device_get_depth_stencil(device->wined3d_device)))
1226 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1227 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1228 IDirect3DSurface8_AddRef(*depth_stencil);
1230 else
1232 hr = WINED3DERR_NOTFOUND;
1233 *depth_stencil = NULL;
1235 wined3d_mutex_unlock();
1237 return hr;
1240 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1242 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1243 HRESULT hr;
1245 TRACE("iface %p.\n", iface);
1247 wined3d_mutex_lock();
1248 hr = wined3d_device_begin_scene(device->wined3d_device);
1249 wined3d_mutex_unlock();
1251 return hr;
1254 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1256 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1257 HRESULT hr;
1259 TRACE("iface %p.\n", iface);
1261 wined3d_mutex_lock();
1262 hr = wined3d_device_end_scene(device->wined3d_device);
1263 wined3d_mutex_unlock();
1265 return hr;
1268 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1269 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1271 const struct wined3d_color c =
1273 ((color >> 16) & 0xff) / 255.0f,
1274 ((color >> 8) & 0xff) / 255.0f,
1275 (color & 0xff) / 255.0f,
1276 ((color >> 24) & 0xff) / 255.0f,
1278 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1279 HRESULT hr;
1281 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1282 iface, rect_count, rects, flags, color, z, stencil);
1284 wined3d_mutex_lock();
1285 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1286 wined3d_mutex_unlock();
1288 return hr;
1291 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1292 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1294 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1296 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1298 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1299 wined3d_mutex_lock();
1300 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1301 wined3d_mutex_unlock();
1303 return D3D_OK;
1306 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1307 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1309 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1311 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1313 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1314 wined3d_mutex_lock();
1315 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1316 wined3d_mutex_unlock();
1318 return D3D_OK;
1321 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1322 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1324 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1326 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1328 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1329 wined3d_mutex_lock();
1330 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1331 wined3d_mutex_unlock();
1333 return D3D_OK;
1336 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1338 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1340 TRACE("iface %p, viewport %p.\n", iface, viewport);
1342 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1343 wined3d_mutex_lock();
1344 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1345 wined3d_mutex_unlock();
1347 return D3D_OK;
1350 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1352 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1354 TRACE("iface %p, viewport %p.\n", iface, viewport);
1356 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1357 wined3d_mutex_lock();
1358 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1359 wined3d_mutex_unlock();
1361 return D3D_OK;
1364 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1366 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1368 TRACE("iface %p, material %p.\n", iface, material);
1370 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1371 wined3d_mutex_lock();
1372 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1373 wined3d_mutex_unlock();
1375 return D3D_OK;
1378 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1380 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1382 TRACE("iface %p, material %p.\n", iface, material);
1384 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1385 wined3d_mutex_lock();
1386 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1387 wined3d_mutex_unlock();
1389 return D3D_OK;
1392 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1394 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1395 HRESULT hr;
1397 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1399 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1400 wined3d_mutex_lock();
1401 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1402 wined3d_mutex_unlock();
1404 return hr;
1407 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1409 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1410 HRESULT hr;
1412 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1414 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1415 wined3d_mutex_lock();
1416 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1417 wined3d_mutex_unlock();
1419 return hr;
1422 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1424 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1425 HRESULT hr;
1427 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1429 wined3d_mutex_lock();
1430 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1431 wined3d_mutex_unlock();
1433 return hr;
1436 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1438 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1439 HRESULT hr;
1441 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1443 wined3d_mutex_lock();
1444 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1445 wined3d_mutex_unlock();
1447 return hr;
1450 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1452 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1453 HRESULT hr;
1455 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1457 wined3d_mutex_lock();
1458 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1459 wined3d_mutex_unlock();
1461 return hr;
1464 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1466 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1467 HRESULT hr;
1469 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1471 wined3d_mutex_lock();
1472 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1473 wined3d_mutex_unlock();
1475 return hr;
1478 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1479 D3DRENDERSTATETYPE state, DWORD value)
1481 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1483 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1485 wined3d_mutex_lock();
1486 switch (state)
1488 case D3DRS_ZBIAS:
1489 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1490 break;
1492 default:
1493 wined3d_device_set_render_state(device->wined3d_device, state, value);
1495 wined3d_mutex_unlock();
1497 return D3D_OK;
1500 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1501 D3DRENDERSTATETYPE state, DWORD *value)
1503 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1505 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1507 wined3d_mutex_lock();
1508 switch (state)
1510 case D3DRS_ZBIAS:
1511 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1512 break;
1514 default:
1515 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1517 wined3d_mutex_unlock();
1519 return D3D_OK;
1522 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1524 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1525 HRESULT hr;
1527 TRACE("iface %p.\n", iface);
1529 wined3d_mutex_lock();
1530 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1531 wined3d_mutex_unlock();
1533 return hr;
1536 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1538 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1539 struct wined3d_stateblock *stateblock;
1540 HRESULT hr;
1542 TRACE("iface %p, token %p.\n", iface, token);
1544 /* Tell wineD3D to endstateblock before anything else (in case we run out
1545 * of memory later and cause locking problems)
1547 wined3d_mutex_lock();
1548 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1549 if (FAILED(hr))
1551 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1552 wined3d_mutex_unlock();
1553 return hr;
1556 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1557 wined3d_mutex_unlock();
1559 if (*token == D3D8_INVALID_HANDLE)
1561 ERR("Failed to create a handle\n");
1562 wined3d_mutex_lock();
1563 wined3d_stateblock_decref(stateblock);
1564 wined3d_mutex_unlock();
1565 return E_FAIL;
1567 ++*token;
1569 TRACE("Returning %#x (%p).\n", *token, stateblock);
1571 return hr;
1574 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1576 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1577 struct wined3d_stateblock *stateblock;
1579 TRACE("iface %p, token %#x.\n", iface, token);
1581 if (!token)
1582 return D3D_OK;
1584 wined3d_mutex_lock();
1585 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1586 if (!stateblock)
1588 WARN("Invalid handle (%#x) passed.\n", token);
1589 wined3d_mutex_unlock();
1590 return D3DERR_INVALIDCALL;
1592 wined3d_stateblock_apply(stateblock);
1593 wined3d_mutex_unlock();
1595 return D3D_OK;
1598 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1600 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1601 struct wined3d_stateblock *stateblock;
1603 TRACE("iface %p, token %#x.\n", iface, token);
1605 wined3d_mutex_lock();
1606 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1607 if (!stateblock)
1609 WARN("Invalid handle (%#x) passed.\n", token);
1610 wined3d_mutex_unlock();
1611 return D3DERR_INVALIDCALL;
1613 wined3d_stateblock_capture(stateblock);
1614 wined3d_mutex_unlock();
1616 return D3D_OK;
1619 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1621 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1622 struct wined3d_stateblock *stateblock;
1624 TRACE("iface %p, token %#x.\n", iface, token);
1626 wined3d_mutex_lock();
1627 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1629 if (!stateblock)
1631 WARN("Invalid handle (%#x) passed.\n", token);
1632 wined3d_mutex_unlock();
1633 return D3DERR_INVALIDCALL;
1636 if (wined3d_stateblock_decref(stateblock))
1638 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1640 wined3d_mutex_unlock();
1642 return D3D_OK;
1645 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1646 D3DSTATEBLOCKTYPE type, DWORD *handle)
1648 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1649 struct wined3d_stateblock *stateblock;
1650 HRESULT hr;
1652 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1654 if (type != D3DSBT_ALL
1655 && type != D3DSBT_PIXELSTATE
1656 && type != D3DSBT_VERTEXSTATE)
1658 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1659 return D3DERR_INVALIDCALL;
1662 wined3d_mutex_lock();
1663 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1664 if (FAILED(hr))
1666 wined3d_mutex_unlock();
1667 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1668 return hr;
1671 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1672 wined3d_mutex_unlock();
1674 if (*handle == D3D8_INVALID_HANDLE)
1676 ERR("Failed to allocate a handle.\n");
1677 wined3d_mutex_lock();
1678 wined3d_stateblock_decref(stateblock);
1679 wined3d_mutex_unlock();
1680 return E_FAIL;
1682 ++*handle;
1684 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1686 return hr;
1689 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1691 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1692 HRESULT hr;
1694 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1695 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1697 wined3d_mutex_lock();
1698 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1699 wined3d_mutex_unlock();
1701 return hr;
1704 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1706 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1707 HRESULT hr;
1709 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1711 wined3d_mutex_lock();
1712 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1713 wined3d_mutex_unlock();
1715 return hr;
1718 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1720 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1721 struct wined3d_texture *wined3d_texture;
1722 struct d3d8_texture *texture_impl;
1724 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1726 if (!texture)
1727 return D3DERR_INVALIDCALL;
1729 wined3d_mutex_lock();
1730 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1732 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1733 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1734 IDirect3DBaseTexture8_AddRef(*texture);
1736 else
1738 *texture = NULL;
1740 wined3d_mutex_unlock();
1742 return D3D_OK;
1745 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1747 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1748 struct d3d8_texture *texture_impl;
1749 HRESULT hr;
1751 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1753 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1755 wined3d_mutex_lock();
1756 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1757 texture_impl ? texture_impl->wined3d_texture : NULL);
1758 wined3d_mutex_unlock();
1760 return hr;
1763 static const struct tss_lookup
1765 BOOL sampler_state;
1766 enum wined3d_texture_stage_state state;
1768 tss_lookup[] =
1770 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1771 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1772 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1773 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1774 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1775 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1776 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1777 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1778 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1779 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1780 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1781 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1782 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1783 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1784 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1785 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1786 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1787 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1788 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1789 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1790 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1791 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1792 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1793 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1794 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1795 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1796 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1797 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1798 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1801 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1802 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1804 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1805 const struct tss_lookup *l;
1807 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1809 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1811 WARN("Invalid Type %#x passed.\n", Type);
1812 return D3D_OK;
1815 l = &tss_lookup[Type];
1817 wined3d_mutex_lock();
1818 if (l->sampler_state)
1819 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1820 else
1821 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1822 wined3d_mutex_unlock();
1824 return D3D_OK;
1827 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1828 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1830 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1831 const struct tss_lookup *l;
1833 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1835 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1837 WARN("Invalid type %#x passed.\n", type);
1838 return D3D_OK;
1841 l = &tss_lookup[type];
1843 wined3d_mutex_lock();
1844 if (l->sampler_state)
1845 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1846 else
1847 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1848 wined3d_mutex_unlock();
1850 return D3D_OK;
1853 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1855 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1856 HRESULT hr;
1858 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1860 wined3d_mutex_lock();
1861 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1862 wined3d_mutex_unlock();
1864 return hr;
1867 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1868 DWORD info_id, void *info, DWORD info_size)
1870 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1872 return D3D_OK;
1875 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1876 UINT palette_idx, const PALETTEENTRY *entries)
1878 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1880 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1881 * does not have a d3d8/9-style palette API */
1883 return D3D_OK;
1886 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1887 UINT palette_idx, PALETTEENTRY *entries)
1889 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1891 return D3DERR_INVALIDCALL;
1894 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1896 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1898 return D3D_OK;
1901 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1903 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1905 return D3DERR_INVALIDCALL;
1908 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1909 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1911 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1912 HRESULT hr;
1914 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1915 iface, primitive_type, start_vertex, primitive_count);
1917 wined3d_mutex_lock();
1918 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1919 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1920 vertex_count_from_primitive_count(primitive_type, primitive_count));
1921 wined3d_mutex_unlock();
1923 return hr;
1926 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1927 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1928 UINT start_idx, UINT primitive_count)
1930 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1931 HRESULT hr;
1933 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1934 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1936 wined3d_mutex_lock();
1937 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1938 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1939 vertex_count_from_primitive_count(primitive_type, primitive_count));
1940 wined3d_mutex_unlock();
1942 return hr;
1945 /* The caller is responsible for wined3d locking */
1946 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1948 HRESULT hr;
1950 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1952 UINT size = max(device->vertex_buffer_size * 2, min_size);
1953 struct wined3d_buffer *buffer;
1955 TRACE("Growing vertex buffer to %u bytes\n", size);
1957 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1958 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1959 if (FAILED(hr))
1961 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1962 return hr;
1965 if (device->vertex_buffer)
1966 wined3d_buffer_decref(device->vertex_buffer);
1968 device->vertex_buffer = buffer;
1969 device->vertex_buffer_size = size;
1970 device->vertex_buffer_pos = 0;
1972 return D3D_OK;
1975 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1976 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
1977 UINT stride)
1979 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1980 HRESULT hr;
1981 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
1982 UINT size = vtx_count * stride;
1983 UINT vb_pos, align;
1984 BYTE *buffer_data;
1986 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1987 iface, primitive_type, primitive_count, data, stride);
1989 if (!primitive_count)
1991 WARN("primitive_count is 0, returning D3D_OK\n");
1992 return D3D_OK;
1995 wined3d_mutex_lock();
1996 hr = d3d8_device_prepare_vertex_buffer(device, size);
1997 if (FAILED(hr))
1998 goto done;
2000 vb_pos = device->vertex_buffer_pos;
2001 align = vb_pos % stride;
2002 if (align) align = stride - align;
2003 if (vb_pos + size + align > device->vertex_buffer_size)
2004 vb_pos = 0;
2005 else
2006 vb_pos += align;
2008 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2009 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2010 if (FAILED(hr))
2011 goto done;
2012 memcpy(buffer_data, data, size);
2013 wined3d_buffer_unmap(device->vertex_buffer);
2014 device->vertex_buffer_pos = vb_pos + size;
2016 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2017 if (FAILED(hr))
2018 goto done;
2020 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2021 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2022 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2024 done:
2025 wined3d_mutex_unlock();
2026 return hr;
2029 /* The caller is responsible for wined3d locking */
2030 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2032 HRESULT hr;
2034 if (device->index_buffer_size < min_size || !device->index_buffer)
2036 UINT size = max(device->index_buffer_size * 2, min_size);
2037 struct wined3d_buffer *buffer;
2039 TRACE("Growing index buffer to %u bytes\n", size);
2041 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2042 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2043 if (FAILED(hr))
2045 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2046 return hr;
2049 if (device->index_buffer)
2050 wined3d_buffer_decref(device->index_buffer);
2052 device->index_buffer = buffer;
2053 device->index_buffer_size = size;
2054 device->index_buffer_pos = 0;
2056 return D3D_OK;
2059 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2060 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2061 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2062 const void *vertex_data, UINT vertex_stride)
2064 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2065 HRESULT hr;
2066 BYTE *buffer_data;
2068 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2069 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2070 UINT idx_size = idx_count * idx_fmt_size;
2071 UINT ib_pos;
2073 UINT vtx_size = vertex_count * vertex_stride;
2074 UINT vb_pos, align;
2076 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2077 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2078 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2079 index_data, index_format, vertex_data, vertex_stride);
2081 if (!primitive_count)
2083 WARN("primitive_count is 0, returning D3D_OK\n");
2084 return D3D_OK;
2087 wined3d_mutex_lock();
2089 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2090 if (FAILED(hr))
2091 goto done;
2093 vb_pos = device->vertex_buffer_pos;
2094 align = vb_pos % vertex_stride;
2095 if (align) align = vertex_stride - align;
2096 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2097 vb_pos = 0;
2098 else
2099 vb_pos += align;
2101 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2102 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2103 if (FAILED(hr))
2104 goto done;
2105 memcpy(buffer_data, vertex_data, vtx_size);
2106 wined3d_buffer_unmap(device->vertex_buffer);
2107 device->vertex_buffer_pos = vb_pos + vtx_size;
2109 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2110 if (FAILED(hr))
2111 goto done;
2113 ib_pos = device->index_buffer_pos;
2114 align = ib_pos % idx_fmt_size;
2115 if (align) align = idx_fmt_size - align;
2116 if (ib_pos + idx_size + align > device->index_buffer_size)
2117 ib_pos = 0;
2118 else
2119 ib_pos += align;
2121 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2122 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2123 if (FAILED(hr))
2124 goto done;
2125 memcpy(buffer_data, index_data, idx_size);
2126 wined3d_buffer_unmap(device->index_buffer);
2127 device->index_buffer_pos = ib_pos + idx_size;
2129 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2130 if (FAILED(hr))
2131 goto done;
2133 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2134 wined3dformat_from_d3dformat(index_format));
2135 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2137 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2138 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2140 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2141 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2142 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2144 done:
2145 wined3d_mutex_unlock();
2146 return hr;
2149 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2150 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2152 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2153 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2154 HRESULT hr;
2156 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2157 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2159 wined3d_mutex_lock();
2160 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2161 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2162 wined3d_mutex_unlock();
2164 return hr;
2167 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2168 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2170 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2171 struct d3d8_vertex_shader *object;
2172 DWORD shader_handle;
2173 DWORD handle;
2174 HRESULT hr;
2176 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2177 iface, declaration, byte_code, shader, usage);
2179 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2180 if (!object)
2182 *shader = 0;
2183 return E_OUTOFMEMORY;
2186 wined3d_mutex_lock();
2187 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2188 wined3d_mutex_unlock();
2189 if (handle == D3D8_INVALID_HANDLE)
2191 ERR("Failed to allocate vertex shader handle.\n");
2192 HeapFree(GetProcessHeap(), 0, object);
2193 *shader = 0;
2194 return E_OUTOFMEMORY;
2197 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2199 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2200 if (FAILED(hr))
2202 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2203 wined3d_mutex_lock();
2204 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2205 wined3d_mutex_unlock();
2206 HeapFree(GetProcessHeap(), 0, object);
2207 *shader = 0;
2208 return hr;
2211 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2212 *shader = shader_handle;
2214 return D3D_OK;
2217 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2219 struct d3d8_vertex_declaration *d3d8_declaration;
2220 struct FvfToDecl *convertedDecls = device->decls;
2221 int p, low, high; /* deliberately signed */
2222 HRESULT hr;
2224 TRACE("Searching for declaration for fvf %08x... ", fvf);
2226 low = 0;
2227 high = device->numConvertedDecls - 1;
2228 while (low <= high)
2230 p = (low + high) >> 1;
2231 TRACE("%d ", p);
2233 if (convertedDecls[p].fvf == fvf)
2235 TRACE("found %p\n", convertedDecls[p].declaration);
2236 return convertedDecls[p].declaration;
2239 if (convertedDecls[p].fvf < fvf)
2240 low = p + 1;
2241 else
2242 high = p - 1;
2244 TRACE("not found. Creating and inserting at position %d.\n", low);
2246 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2247 return NULL;
2249 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2251 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2252 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2253 return NULL;
2256 if (device->declArraySize == device->numConvertedDecls)
2258 UINT grow = device->declArraySize / 2;
2260 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2261 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2262 if (!convertedDecls)
2264 d3d8_vertex_declaration_destroy(d3d8_declaration);
2265 return NULL;
2267 device->decls = convertedDecls;
2268 device->declArraySize += grow;
2271 memmove(convertedDecls + low + 1, convertedDecls + low,
2272 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2273 convertedDecls[low].declaration = d3d8_declaration;
2274 convertedDecls[low].fvf = fvf;
2275 ++device->numConvertedDecls;
2277 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2279 return d3d8_declaration;
2282 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2284 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2285 struct d3d8_vertex_shader *shader_impl;
2287 TRACE("iface %p, shader %#x.\n", iface, shader);
2289 if (VS_HIGHESTFIXEDFXF >= shader)
2291 TRACE("Setting FVF, %#x\n", shader);
2293 wined3d_mutex_lock();
2294 wined3d_device_set_vertex_declaration(device->wined3d_device,
2295 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2296 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2297 wined3d_mutex_unlock();
2299 return D3D_OK;
2302 TRACE("Setting shader\n");
2304 wined3d_mutex_lock();
2305 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2307 WARN("Invalid handle (%#x) passed.\n", shader);
2308 wined3d_mutex_unlock();
2310 return D3DERR_INVALIDCALL;
2313 wined3d_device_set_vertex_declaration(device->wined3d_device,
2314 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2315 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2316 wined3d_mutex_unlock();
2318 return D3D_OK;
2321 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2323 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2324 struct wined3d_vertex_declaration *wined3d_declaration;
2325 struct d3d8_vertex_declaration *d3d8_declaration;
2327 TRACE("iface %p, shader %p.\n", iface, shader);
2329 wined3d_mutex_lock();
2330 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2332 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2333 *shader = d3d8_declaration->shader_handle;
2335 else
2337 *shader = 0;
2339 wined3d_mutex_unlock();
2341 TRACE("Returning %#x.\n", *shader);
2343 return D3D_OK;
2346 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2348 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2349 struct d3d8_vertex_shader *shader_impl;
2351 TRACE("iface %p, shader %#x.\n", iface, shader);
2353 wined3d_mutex_lock();
2354 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2356 WARN("Invalid handle (%#x) passed.\n", shader);
2357 wined3d_mutex_unlock();
2359 return D3DERR_INVALIDCALL;
2362 if (shader_impl->wined3d_shader
2363 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2364 IDirect3DDevice8_SetVertexShader(iface, 0);
2366 wined3d_mutex_unlock();
2368 d3d8_vertex_shader_destroy(shader_impl);
2370 return D3D_OK;
2373 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2374 DWORD start_register, const void *data, DWORD count)
2376 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2377 HRESULT hr;
2379 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2380 iface, start_register, data, count);
2382 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2384 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2385 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2386 return D3DERR_INVALIDCALL;
2389 wined3d_mutex_lock();
2390 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2391 wined3d_mutex_unlock();
2393 return hr;
2396 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2397 DWORD start_register, void *data, DWORD count)
2399 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2400 HRESULT hr;
2402 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2403 iface, start_register, data, count);
2405 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2407 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2408 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2409 return D3DERR_INVALIDCALL;
2412 wined3d_mutex_lock();
2413 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2414 wined3d_mutex_unlock();
2416 return hr;
2419 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2420 DWORD shader, void *data, DWORD *data_size)
2422 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2423 struct d3d8_vertex_declaration *declaration;
2424 struct d3d8_vertex_shader *shader_impl;
2426 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2427 iface, shader, data, data_size);
2429 wined3d_mutex_lock();
2430 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2431 wined3d_mutex_unlock();
2433 if (!shader_impl)
2435 WARN("Invalid handle (%#x) passed.\n", shader);
2436 return D3DERR_INVALIDCALL;
2438 declaration = shader_impl->vertex_declaration;
2440 if (!data)
2442 *data_size = declaration->elements_size;
2443 return D3D_OK;
2446 /* MSDN claims that if *data_size is smaller than the required size
2447 * we should write the required size and return D3DERR_MOREDATA.
2448 * That's not actually true. */
2449 if (*data_size < declaration->elements_size)
2450 return D3DERR_INVALIDCALL;
2452 memcpy(data, declaration->elements, declaration->elements_size);
2454 return D3D_OK;
2457 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2458 DWORD shader, void *data, DWORD *data_size)
2460 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2461 struct d3d8_vertex_shader *shader_impl = NULL;
2462 HRESULT hr;
2464 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2465 iface, shader, data, data_size);
2467 wined3d_mutex_lock();
2468 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2470 WARN("Invalid handle (%#x) passed.\n", shader);
2471 wined3d_mutex_unlock();
2473 return D3DERR_INVALIDCALL;
2476 if (!shader_impl->wined3d_shader)
2478 wined3d_mutex_unlock();
2479 *data_size = 0;
2480 return D3D_OK;
2483 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2484 wined3d_mutex_unlock();
2486 return hr;
2489 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2490 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2492 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2493 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2495 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2497 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2498 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2499 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2500 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2501 * problem)
2503 wined3d_mutex_lock();
2504 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2505 wined3d_device_set_index_buffer(device->wined3d_device,
2506 ib ? ib->wined3d_buffer : NULL,
2507 ib ? ib->format : WINED3DFMT_UNKNOWN);
2508 wined3d_mutex_unlock();
2510 return D3D_OK;
2513 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2514 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2516 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2517 enum wined3d_format_id wined3d_format;
2518 struct wined3d_buffer *wined3d_buffer;
2519 struct d3d8_indexbuffer *buffer_impl;
2521 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2523 if (!buffer)
2524 return D3DERR_INVALIDCALL;
2526 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2527 wined3d_mutex_lock();
2528 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2529 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2531 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2532 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2533 IDirect3DIndexBuffer8_AddRef(*buffer);
2535 else
2537 *buffer = NULL;
2539 wined3d_mutex_unlock();
2541 return D3D_OK;
2544 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2545 const DWORD *byte_code, DWORD *shader)
2547 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2548 struct d3d8_pixel_shader *object;
2549 DWORD shader_handle;
2550 DWORD handle;
2551 HRESULT hr;
2553 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2555 if (!shader)
2556 return D3DERR_INVALIDCALL;
2558 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2559 if (!object)
2560 return E_OUTOFMEMORY;
2562 wined3d_mutex_lock();
2563 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2564 wined3d_mutex_unlock();
2565 if (handle == D3D8_INVALID_HANDLE)
2567 ERR("Failed to allocate pixel shader handle.\n");
2568 HeapFree(GetProcessHeap(), 0, object);
2569 return E_OUTOFMEMORY;
2572 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2574 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2575 if (FAILED(hr))
2577 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2578 wined3d_mutex_lock();
2579 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2580 wined3d_mutex_unlock();
2581 HeapFree(GetProcessHeap(), 0, object);
2582 *shader = 0;
2583 return hr;
2586 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2587 *shader = shader_handle;
2589 return D3D_OK;
2592 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2594 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2595 struct d3d8_pixel_shader *shader_impl;
2597 TRACE("iface %p, shader %#x.\n", iface, shader);
2599 wined3d_mutex_lock();
2601 if (!shader)
2603 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2604 wined3d_mutex_unlock();
2605 return D3D_OK;
2608 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2610 WARN("Invalid handle (%#x) passed.\n", shader);
2611 wined3d_mutex_unlock();
2612 return D3DERR_INVALIDCALL;
2615 TRACE("Setting shader %p.\n", shader_impl);
2616 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2617 wined3d_mutex_unlock();
2619 return D3D_OK;
2622 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2624 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2625 struct wined3d_shader *object;
2627 TRACE("iface %p, shader %p.\n", iface, shader);
2629 if (!shader)
2630 return D3DERR_INVALIDCALL;
2632 wined3d_mutex_lock();
2633 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2635 struct d3d8_pixel_shader *d3d8_shader;
2636 d3d8_shader = wined3d_shader_get_parent(object);
2637 *shader = d3d8_shader->handle;
2639 else
2641 *shader = 0;
2643 wined3d_mutex_unlock();
2645 TRACE("Returning %#x.\n", *shader);
2647 return D3D_OK;
2650 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2652 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2653 struct d3d8_pixel_shader *shader_impl;
2655 TRACE("iface %p, shader %#x.\n", iface, shader);
2657 wined3d_mutex_lock();
2659 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2661 WARN("Invalid handle (%#x) passed.\n", shader);
2662 wined3d_mutex_unlock();
2663 return D3DERR_INVALIDCALL;
2666 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2667 IDirect3DDevice8_SetPixelShader(iface, 0);
2669 wined3d_mutex_unlock();
2671 d3d8_pixel_shader_destroy(shader_impl);
2673 return D3D_OK;
2676 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2677 DWORD start_register, const void *data, DWORD count)
2679 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2680 HRESULT hr;
2682 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2683 iface, start_register, data, count);
2685 wined3d_mutex_lock();
2686 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2687 wined3d_mutex_unlock();
2689 return hr;
2692 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2693 DWORD start_register, void *data, DWORD count)
2695 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2696 HRESULT hr;
2698 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2699 iface, start_register, data, count);
2701 wined3d_mutex_lock();
2702 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2703 wined3d_mutex_unlock();
2705 return hr;
2708 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2709 DWORD shader, void *data, DWORD *data_size)
2711 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2712 struct d3d8_pixel_shader *shader_impl = NULL;
2713 HRESULT hr;
2715 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2716 iface, shader, data, data_size);
2718 wined3d_mutex_lock();
2719 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2721 WARN("Invalid handle (%#x) passed.\n", shader);
2722 wined3d_mutex_unlock();
2724 return D3DERR_INVALIDCALL;
2727 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2728 wined3d_mutex_unlock();
2730 return hr;
2733 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2734 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2736 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2737 iface, handle, segment_count, patch_info);
2738 return D3D_OK;
2741 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2742 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2744 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2745 iface, handle, segment_count, patch_info);
2746 return D3D_OK;
2749 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2751 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2752 return D3DERR_INVALIDCALL;
2755 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2756 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2758 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2759 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2760 HRESULT hr;
2762 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2763 iface, stream_idx, buffer, stride);
2765 wined3d_mutex_lock();
2766 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2767 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2768 wined3d_mutex_unlock();
2770 return hr;
2773 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2774 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2776 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2777 struct d3d8_vertexbuffer *buffer_impl;
2778 struct wined3d_buffer *wined3d_buffer = NULL;
2779 HRESULT hr;
2781 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2782 iface, stream_idx, buffer, stride);
2784 if (!buffer)
2785 return D3DERR_INVALIDCALL;
2787 wined3d_mutex_lock();
2788 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2789 if (SUCCEEDED(hr) && wined3d_buffer)
2791 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2792 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2793 IDirect3DVertexBuffer8_AddRef(*buffer);
2794 wined3d_buffer_decref(wined3d_buffer);
2796 else
2798 if (FAILED(hr))
2799 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2800 *buffer = NULL;
2802 wined3d_mutex_unlock();
2804 return hr;
2807 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2809 d3d8_device_QueryInterface,
2810 d3d8_device_AddRef,
2811 d3d8_device_Release,
2812 d3d8_device_TestCooperativeLevel,
2813 d3d8_device_GetAvailableTextureMem,
2814 d3d8_device_ResourceManagerDiscardBytes,
2815 d3d8_device_GetDirect3D,
2816 d3d8_device_GetDeviceCaps,
2817 d3d8_device_GetDisplayMode,
2818 d3d8_device_GetCreationParameters,
2819 d3d8_device_SetCursorProperties,
2820 d3d8_device_SetCursorPosition,
2821 d3d8_device_ShowCursor,
2822 d3d8_device_CreateAdditionalSwapChain,
2823 d3d8_device_Reset,
2824 d3d8_device_Present,
2825 d3d8_device_GetBackBuffer,
2826 d3d8_device_GetRasterStatus,
2827 d3d8_device_SetGammaRamp,
2828 d3d8_device_GetGammaRamp,
2829 d3d8_device_CreateTexture,
2830 d3d8_device_CreateVolumeTexture,
2831 d3d8_device_CreateCubeTexture,
2832 d3d8_device_CreateVertexBuffer,
2833 d3d8_device_CreateIndexBuffer,
2834 d3d8_device_CreateRenderTarget,
2835 d3d8_device_CreateDepthStencilSurface,
2836 d3d8_device_CreateImageSurface,
2837 d3d8_device_CopyRects,
2838 d3d8_device_UpdateTexture,
2839 d3d8_device_GetFrontBuffer,
2840 d3d8_device_SetRenderTarget,
2841 d3d8_device_GetRenderTarget,
2842 d3d8_device_GetDepthStencilSurface,
2843 d3d8_device_BeginScene,
2844 d3d8_device_EndScene,
2845 d3d8_device_Clear,
2846 d3d8_device_SetTransform,
2847 d3d8_device_GetTransform,
2848 d3d8_device_MultiplyTransform,
2849 d3d8_device_SetViewport,
2850 d3d8_device_GetViewport,
2851 d3d8_device_SetMaterial,
2852 d3d8_device_GetMaterial,
2853 d3d8_device_SetLight,
2854 d3d8_device_GetLight,
2855 d3d8_device_LightEnable,
2856 d3d8_device_GetLightEnable,
2857 d3d8_device_SetClipPlane,
2858 d3d8_device_GetClipPlane,
2859 d3d8_device_SetRenderState,
2860 d3d8_device_GetRenderState,
2861 d3d8_device_BeginStateBlock,
2862 d3d8_device_EndStateBlock,
2863 d3d8_device_ApplyStateBlock,
2864 d3d8_device_CaptureStateBlock,
2865 d3d8_device_DeleteStateBlock,
2866 d3d8_device_CreateStateBlock,
2867 d3d8_device_SetClipStatus,
2868 d3d8_device_GetClipStatus,
2869 d3d8_device_GetTexture,
2870 d3d8_device_SetTexture,
2871 d3d8_device_GetTextureStageState,
2872 d3d8_device_SetTextureStageState,
2873 d3d8_device_ValidateDevice,
2874 d3d8_device_GetInfo,
2875 d3d8_device_SetPaletteEntries,
2876 d3d8_device_GetPaletteEntries,
2877 d3d8_device_SetCurrentTexturePalette,
2878 d3d8_device_GetCurrentTexturePalette,
2879 d3d8_device_DrawPrimitive,
2880 d3d8_device_DrawIndexedPrimitive,
2881 d3d8_device_DrawPrimitiveUP,
2882 d3d8_device_DrawIndexedPrimitiveUP,
2883 d3d8_device_ProcessVertices,
2884 d3d8_device_CreateVertexShader,
2885 d3d8_device_SetVertexShader,
2886 d3d8_device_GetVertexShader,
2887 d3d8_device_DeleteVertexShader,
2888 d3d8_device_SetVertexShaderConstant,
2889 d3d8_device_GetVertexShaderConstant,
2890 d3d8_device_GetVertexShaderDeclaration,
2891 d3d8_device_GetVertexShaderFunction,
2892 d3d8_device_SetStreamSource,
2893 d3d8_device_GetStreamSource,
2894 d3d8_device_SetIndices,
2895 d3d8_device_GetIndices,
2896 d3d8_device_CreatePixelShader,
2897 d3d8_device_SetPixelShader,
2898 d3d8_device_GetPixelShader,
2899 d3d8_device_DeletePixelShader,
2900 d3d8_device_SetPixelShaderConstant,
2901 d3d8_device_GetPixelShaderConstant,
2902 d3d8_device_GetPixelShaderFunction,
2903 d3d8_device_DrawRectPatch,
2904 d3d8_device_DrawTriPatch,
2905 d3d8_device_DeletePatch,
2908 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2910 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2913 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2914 struct wined3d_device *device)
2916 TRACE("device_parent %p, device %p\n", device_parent, device);
2919 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2921 TRACE("device_parent %p.\n", device_parent);
2924 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
2925 void *container_parent, struct wined3d_surface *surface, void **parent,
2926 const struct wined3d_parent_ops **parent_ops)
2928 struct d3d8_device *device = device_from_device_parent(device_parent);
2929 struct d3d8_surface *d3d_surface;
2931 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
2932 device_parent, container_parent, surface, parent, parent_ops);
2934 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
2936 FIXME("Failed to allocate surface memory.\n");
2937 return D3DERR_OUTOFVIDEOMEMORY;
2940 surface_init(d3d_surface, surface, device, parent_ops);
2941 *parent = d3d_surface;
2942 TRACE("Created surface %p.\n", d3d_surface);
2944 d3d_surface->container = container_parent;
2945 IDirect3DDevice8_Release(d3d_surface->parent_device);
2946 d3d_surface->parent_device = NULL;
2948 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2949 d3d_surface->forwardReference = container_parent;
2951 return D3D_OK;
2954 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
2955 void *container_parent, struct wined3d_volume *volume, void **parent,
2956 const struct wined3d_parent_ops **parent_ops)
2958 struct d3d8_volume *d3d_volume;
2960 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
2961 device_parent, container_parent, volume, parent, parent_ops);
2963 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
2964 return E_OUTOFMEMORY;
2966 volume_init(d3d_volume, volume, parent_ops);
2967 *parent = d3d_volume;
2968 TRACE("Created volume %p.\n", d3d_volume);
2970 d3d_volume->container = container_parent;
2972 IDirect3DVolume8_Release(&d3d_volume->IDirect3DVolume8_iface);
2973 d3d_volume->forwardReference = container_parent;
2975 return D3D_OK;
2978 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
2979 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
2981 struct d3d8_device *device = device_from_device_parent(device_parent);
2982 struct wined3d_resource_desc texture_desc;
2983 struct d3d8_surface *d3d_surface;
2984 struct wined3d_texture *texture;
2985 HRESULT hr;
2987 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
2988 device_parent, container_parent, desc, surface);
2990 texture_desc = *desc;
2991 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
2992 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
2993 WINED3D_SURFACE_MAPPABLE, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
2995 WARN("Failed to create texture, hr %#x.\n", hr);
2996 return hr;
2999 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3000 wined3d_surface_incref(*surface);
3001 wined3d_texture_decref(texture);
3003 d3d_surface = wined3d_surface_get_parent(*surface);
3004 d3d_surface->forwardReference = NULL;
3005 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3007 return hr;
3010 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3011 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3013 struct d3d8_device *device = device_from_device_parent(device_parent);
3014 struct d3d8_swapchain *d3d_swapchain;
3015 HRESULT hr;
3017 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3019 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3021 WARN("Failed to create swapchain, hr %#x.\n", hr);
3022 *swapchain = NULL;
3023 return hr;
3026 *swapchain = d3d_swapchain->wined3d_swapchain;
3027 wined3d_swapchain_incref(*swapchain);
3028 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3030 return hr;
3033 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3035 device_parent_wined3d_device_created,
3036 device_parent_mode_changed,
3037 device_parent_surface_created,
3038 device_parent_volume_created,
3039 device_parent_create_swapchain_surface,
3040 device_parent_create_swapchain,
3043 static void setup_fpu(void)
3045 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3046 WORD cw;
3047 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3048 cw = (cw & ~0xf3f) | 0x3f;
3049 __asm__ volatile ("fldcw %0" : : "m" (cw));
3050 #elif defined(__i386__) && defined(_MSC_VER)
3051 WORD cw;
3052 __asm fnstcw cw;
3053 cw = (cw & ~0xf3f) | 0x3f;
3054 __asm fldcw cw;
3055 #else
3056 FIXME("FPU setup not implemented for this platform.\n");
3057 #endif
3060 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3061 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3063 struct wined3d_swapchain_desc swapchain_desc;
3064 HRESULT hr;
3066 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3067 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3068 device->ref = 1;
3069 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3070 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3071 if (!device->handle_table.entries)
3073 ERR("Failed to allocate handle table memory.\n");
3074 return E_OUTOFMEMORY;
3076 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3078 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3080 wined3d_mutex_lock();
3081 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3082 &device->device_parent, &device->wined3d_device);
3083 if (FAILED(hr))
3085 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3086 wined3d_mutex_unlock();
3087 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3088 return hr;
3091 if (!parameters->Windowed)
3093 HWND device_window = parameters->hDeviceWindow;
3095 if (!focus_window)
3096 focus_window = device_window;
3097 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3099 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3100 wined3d_device_decref(device->wined3d_device);
3101 wined3d_mutex_unlock();
3102 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3103 return hr;
3106 if (!device_window)
3107 device_window = focus_window;
3108 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3109 parameters->BackBufferWidth,
3110 parameters->BackBufferHeight);
3113 if (flags & D3DCREATE_MULTITHREADED)
3114 wined3d_device_set_multithreaded(device->wined3d_device);
3116 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3118 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3119 if (FAILED(hr))
3121 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3122 wined3d_device_release_focus_window(device->wined3d_device);
3123 wined3d_device_decref(device->wined3d_device);
3124 wined3d_mutex_unlock();
3125 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3126 return hr;
3129 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3130 wined3d_mutex_unlock();
3132 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3134 device->declArraySize = 16;
3135 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3136 if (!device->decls)
3138 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3139 hr = E_OUTOFMEMORY;
3140 goto err;
3143 device->d3d_parent = &parent->IDirect3D8_iface;
3144 IDirect3D8_AddRef(device->d3d_parent);
3146 return D3D_OK;
3148 err:
3149 wined3d_mutex_lock();
3150 wined3d_device_uninit_3d(device->wined3d_device);
3151 wined3d_device_release_focus_window(device->wined3d_device);
3152 wined3d_device_decref(device->wined3d_device);
3153 wined3d_mutex_unlock();
3154 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3155 return hr;