wined3d: Remove wined3d_device_get_back_buffer.
[wine/multimedia.git] / dlls / d3d8 / device.c
blob336a07cecfd0e5c5651f3fabeedbc8662f9e3b45
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "wine/debug.h"
33 #include "d3d8_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
37 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
39 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
41 d3d8_null_wined3d_object_destroyed,
44 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
46 BYTE *c = (BYTE *)&format;
48 /* Don't translate FOURCC formats */
49 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
51 switch(format)
53 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
54 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
55 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
56 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
57 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
58 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
59 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
60 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
61 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
62 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
63 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
64 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
65 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
66 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
67 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
68 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
69 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
70 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
71 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
72 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
73 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
74 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
75 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
76 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
77 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
78 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
79 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
80 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
81 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
82 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
83 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
84 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
85 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
86 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
87 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
88 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
89 default:
90 FIXME("Unhandled wined3d format %#x.\n", format);
91 return D3DFMT_UNKNOWN;
95 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
97 BYTE *c = (BYTE *)&format;
99 /* Don't translate FOURCC formats */
100 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
102 switch(format)
104 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
105 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
106 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
107 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
108 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
109 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
110 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
111 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
112 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
113 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
114 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
115 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
116 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
117 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
119 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
120 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
121 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
122 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
123 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
124 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
125 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
126 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
127 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
128 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
129 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
130 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
131 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
132 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
133 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
134 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
135 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
136 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
137 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
138 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
139 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
140 default:
141 FIXME("Unhandled D3DFORMAT %#x\n", format);
142 return WINED3DFMT_UNKNOWN;
146 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
148 switch(primitive_type)
150 case D3DPT_POINTLIST:
151 return primitive_count;
153 case D3DPT_LINELIST:
154 return primitive_count * 2;
156 case D3DPT_LINESTRIP:
157 return primitive_count + 1;
159 case D3DPT_TRIANGLELIST:
160 return primitive_count * 3;
162 case D3DPT_TRIANGLESTRIP:
163 case D3DPT_TRIANGLEFAN:
164 return primitive_count + 2;
166 default:
167 FIXME("Unhandled primitive type %#x\n", primitive_type);
168 return 0;
172 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
173 const struct wined3d_swapchain_desc *swapchain_desc)
175 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
176 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
177 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
178 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
179 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
180 present_parameters->SwapEffect = swapchain_desc->swap_effect;
181 present_parameters->hDeviceWindow = swapchain_desc->device_window;
182 present_parameters->Windowed = swapchain_desc->windowed;
183 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
184 present_parameters->AutoDepthStencilFormat
185 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
186 present_parameters->Flags = swapchain_desc->flags;
187 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
188 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
191 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
192 const D3DPRESENT_PARAMETERS *present_parameters)
194 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY)
196 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
197 return FALSE;
199 if (present_parameters->BackBufferCount > 3
200 || (present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
201 && present_parameters->BackBufferCount > 1))
203 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
204 return FALSE;
207 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
208 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
209 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
210 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
211 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
212 swapchain_desc->multisample_quality = 0; /* d3d9 only */
213 swapchain_desc->swap_effect = present_parameters->SwapEffect;
214 swapchain_desc->device_window = present_parameters->hDeviceWindow;
215 swapchain_desc->windowed = present_parameters->Windowed;
216 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
217 swapchain_desc->auto_depth_stencil_format
218 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
219 swapchain_desc->flags = present_parameters->Flags;
220 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
221 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
222 swapchain_desc->auto_restore_display_mode = TRUE;
224 return TRUE;
227 /* Handle table functions */
228 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
230 struct d3d8_handle_entry *entry;
232 if (t->free_entries)
234 DWORD index = t->free_entries - t->entries;
235 /* Use a free handle */
236 entry = t->free_entries;
237 if (entry->type != D3D8_HANDLE_FREE)
239 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
240 return D3D8_INVALID_HANDLE;
242 t->free_entries = entry->object;
243 entry->object = object;
244 entry->type = type;
246 return index;
249 if (!(t->entry_count < t->table_size))
251 /* Grow the table */
252 UINT new_size = t->table_size + (t->table_size >> 1);
253 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
254 0, t->entries, new_size * sizeof(*t->entries));
255 if (!new_entries)
257 ERR("Failed to grow the handle table.\n");
258 return D3D8_INVALID_HANDLE;
260 t->entries = new_entries;
261 t->table_size = new_size;
264 entry = &t->entries[t->entry_count];
265 entry->object = object;
266 entry->type = type;
268 return t->entry_count++;
271 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
273 struct d3d8_handle_entry *entry;
274 void *object;
276 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
278 WARN("Invalid handle %u passed.\n", handle);
279 return NULL;
282 entry = &t->entries[handle];
283 if (entry->type != type)
285 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
286 return NULL;
289 object = entry->object;
290 entry->object = t->free_entries;
291 entry->type = D3D8_HANDLE_FREE;
292 t->free_entries = entry;
294 return object;
297 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
299 struct d3d8_handle_entry *entry;
301 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
303 WARN("Invalid handle %u passed.\n", handle);
304 return NULL;
307 entry = &t->entries[handle];
308 if (entry->type != type)
310 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
311 return NULL;
314 return entry->object;
317 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
319 TRACE("iface %p, riid %s, out %p.\n",
320 iface, debugstr_guid(riid), out);
322 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
323 || IsEqualGUID(riid, &IID_IUnknown))
325 IDirect3DDevice8_AddRef(iface);
326 *out = iface;
327 return S_OK;
330 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
332 *out = NULL;
333 return E_NOINTERFACE;
336 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
338 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
339 ULONG ref = InterlockedIncrement(&device->ref);
341 TRACE("%p increasing refcount to %u.\n", iface, ref);
343 return ref;
346 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
348 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
349 ULONG ref;
351 if (device->inDestruction)
352 return 0;
354 ref = InterlockedDecrement(&device->ref);
356 TRACE("%p decreasing refcount to %u.\n", iface, ref);
358 if (!ref)
360 IDirect3D8 *parent = device->d3d_parent;
361 unsigned i;
363 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
365 wined3d_mutex_lock();
367 device->inDestruction = TRUE;
369 for (i = 0; i < device->numConvertedDecls; ++i)
371 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
373 HeapFree(GetProcessHeap(), 0, device->decls);
375 if (device->vertex_buffer)
376 wined3d_buffer_decref(device->vertex_buffer);
377 if (device->index_buffer)
378 wined3d_buffer_decref(device->index_buffer);
380 wined3d_device_uninit_3d(device->wined3d_device);
381 wined3d_device_release_focus_window(device->wined3d_device);
382 wined3d_device_decref(device->wined3d_device);
383 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
384 HeapFree(GetProcessHeap(), 0, device);
386 wined3d_mutex_unlock();
388 IDirect3D8_Release(parent);
390 return ref;
393 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
395 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
397 TRACE("iface %p.\n", iface);
399 TRACE("device state: %#x.\n", device->device_state);
401 switch (device->device_state)
403 default:
404 case D3D8_DEVICE_STATE_OK:
405 return D3D_OK;
406 case D3D8_DEVICE_STATE_LOST:
407 return D3DERR_DEVICELOST;
408 case D3D8_DEVICE_STATE_NOT_RESET:
409 return D3DERR_DEVICENOTRESET;
413 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
415 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
416 UINT ret;
418 TRACE("iface %p.\n", iface);
420 wined3d_mutex_lock();
421 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
422 wined3d_mutex_unlock();
424 return ret;
427 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
429 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
431 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
433 if (byte_count)
434 FIXME("Byte count ignored.\n");
436 wined3d_mutex_lock();
437 wined3d_device_evict_managed_resources(device->wined3d_device);
438 wined3d_mutex_unlock();
440 return D3D_OK;
443 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
445 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
447 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
449 if (!d3d8)
450 return D3DERR_INVALIDCALL;
452 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
455 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
457 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
458 WINED3DCAPS *wined3d_caps;
459 HRESULT hr;
461 TRACE("iface %p, caps %p.\n", iface, caps);
463 if (!caps)
464 return D3DERR_INVALIDCALL;
466 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
467 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
469 wined3d_mutex_lock();
470 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
471 wined3d_mutex_unlock();
473 fixup_caps(wined3d_caps);
474 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
475 HeapFree(GetProcessHeap(), 0, wined3d_caps);
477 return hr;
480 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
482 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
483 struct wined3d_display_mode wined3d_mode;
484 HRESULT hr;
486 TRACE("iface %p, mode %p.\n", iface, mode);
488 wined3d_mutex_lock();
489 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
490 wined3d_mutex_unlock();
492 if (SUCCEEDED(hr))
494 mode->Width = wined3d_mode.width;
495 mode->Height = wined3d_mode.height;
496 mode->RefreshRate = wined3d_mode.refresh_rate;
497 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
500 return hr;
503 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
504 D3DDEVICE_CREATION_PARAMETERS *parameters)
506 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
508 TRACE("iface %p, parameters %p.\n", iface, parameters);
510 wined3d_mutex_lock();
511 wined3d_device_get_creation_parameters(device->wined3d_device,
512 (struct wined3d_device_creation_parameters *)parameters);
513 wined3d_mutex_unlock();
515 return D3D_OK;
518 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
519 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
521 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
522 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
523 HRESULT hr;
525 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
526 iface, hotspot_x, hotspot_y, bitmap);
528 if (!bitmap)
530 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
531 return D3DERR_INVALIDCALL;
534 wined3d_mutex_lock();
535 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
536 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
537 wined3d_mutex_unlock();
539 return hr;
542 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
544 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
546 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
548 wined3d_mutex_lock();
549 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
550 wined3d_mutex_unlock();
553 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
555 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
556 BOOL ret;
558 TRACE("iface %p, show %#x.\n", iface, show);
560 wined3d_mutex_lock();
561 ret = wined3d_device_show_cursor(device->wined3d_device, show);
562 wined3d_mutex_unlock();
564 return ret;
567 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
568 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
570 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
571 struct wined3d_swapchain_desc desc;
572 struct d3d8_swapchain *object;
573 UINT i, count;
574 HRESULT hr;
576 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
577 iface, present_parameters, swapchain);
579 if (!present_parameters->Windowed)
581 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
582 return D3DERR_INVALIDCALL;
585 wined3d_mutex_lock();
586 count = wined3d_device_get_swapchain_count(device->wined3d_device);
587 for (i = 0; i < count; ++i)
589 struct wined3d_swapchain *wined3d_swapchain;
591 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
592 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
594 if (!desc.windowed)
596 wined3d_mutex_unlock();
597 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
598 return D3DERR_INVALIDCALL;
601 wined3d_mutex_unlock();
603 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
604 return D3DERR_INVALIDCALL;
605 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
606 *swapchain = &object->IDirect3DSwapChain8_iface;
607 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
609 return hr;
612 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
614 struct wined3d_resource_desc desc;
616 wined3d_resource_get_desc(resource, &desc);
617 if (desc.pool == WINED3D_POOL_DEFAULT)
619 struct d3d8_surface *surface;
621 if (desc.resource_type == WINED3D_RTYPE_TEXTURE)
623 IUnknown *parent = wined3d_resource_get_parent(resource);
624 IDirect3DBaseTexture8 *texture;
626 if (SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
628 IDirect3DBaseTexture8_Release(texture);
629 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
630 return D3DERR_DEVICELOST;
633 return D3D_OK;
636 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
638 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
639 return D3DERR_DEVICELOST;
642 surface = wined3d_resource_get_parent(resource);
643 if (surface->resource.refcount)
645 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
646 return D3DERR_DEVICELOST;
649 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
652 return D3D_OK;
655 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
656 D3DPRESENT_PARAMETERS *present_parameters)
658 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
659 struct wined3d_swapchain_desc swapchain_desc;
660 HRESULT hr;
662 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
664 if (device->device_state == D3D8_DEVICE_STATE_LOST)
666 WARN("App not active, returning D3DERR_DEVICELOST.\n");
667 return D3DERR_DEVICELOST;
669 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
670 return D3DERR_INVALIDCALL;
672 wined3d_mutex_lock();
674 if (device->vertex_buffer)
676 wined3d_buffer_decref(device->vertex_buffer);
677 device->vertex_buffer = NULL;
678 device->vertex_buffer_size = 0;
680 if (device->index_buffer)
682 wined3d_buffer_decref(device->index_buffer);
683 device->index_buffer = NULL;
684 device->index_buffer_size = 0;
687 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
688 NULL, reset_enum_callback, TRUE)))
690 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
691 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
692 device->device_state = D3D8_DEVICE_STATE_OK;
694 else
696 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
698 wined3d_mutex_unlock();
700 return hr;
703 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
704 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
706 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
707 HRESULT hr;
709 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
710 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
712 if (device->device_state != D3D8_DEVICE_STATE_OK)
713 return D3DERR_DEVICELOST;
715 wined3d_mutex_lock();
716 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
717 dst_window_override, dirty_region, 0);
718 wined3d_mutex_unlock();
720 return hr;
723 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
724 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
726 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
727 struct wined3d_swapchain *swapchain;
728 struct wined3d_resource *wined3d_resource;
729 struct wined3d_texture *wined3d_texture;
730 struct d3d8_surface *surface_impl;
732 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
733 iface, backbuffer_idx, backbuffer_type, backbuffer);
735 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
736 wined3d_mutex_lock();
737 if (!(swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0)))
739 wined3d_mutex_unlock();
740 *backbuffer = NULL;
741 return D3DERR_INVALIDCALL;
744 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(swapchain,
745 backbuffer_idx, (enum wined3d_backbuffer_type)backbuffer_type)))
747 wined3d_mutex_unlock();
748 *backbuffer = NULL;
749 return D3DERR_INVALIDCALL;
752 wined3d_resource = wined3d_texture_get_sub_resource(wined3d_texture, 0);
753 surface_impl = wined3d_resource_get_parent(wined3d_resource);
754 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
755 IDirect3DSurface8_AddRef(*backbuffer);
757 wined3d_mutex_unlock();
758 return D3D_OK;
761 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
763 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
764 HRESULT hr;
766 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
768 wined3d_mutex_lock();
769 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
770 wined3d_mutex_unlock();
772 return hr;
775 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
777 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
779 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
781 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
782 wined3d_mutex_lock();
783 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
784 wined3d_mutex_unlock();
787 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
789 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
791 TRACE("iface %p, ramp %p.\n", iface, ramp);
793 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
794 wined3d_mutex_lock();
795 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
796 wined3d_mutex_unlock();
799 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
800 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
801 D3DPOOL pool, IDirect3DTexture8 **texture)
803 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
804 struct d3d8_texture *object;
805 HRESULT hr;
807 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
808 iface, width, height, levels, usage, format, pool, texture);
810 *texture = NULL;
811 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
812 if (!object)
813 return D3DERR_OUTOFVIDEOMEMORY;
815 hr = texture_init(object, device, width, height, levels, usage, format, pool);
816 if (FAILED(hr))
818 WARN("Failed to initialize texture, hr %#x.\n", hr);
819 HeapFree(GetProcessHeap(), 0, object);
820 return hr;
823 TRACE("Created texture %p.\n", object);
824 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
826 return D3D_OK;
829 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
830 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
831 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
833 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
834 struct d3d8_texture *object;
835 HRESULT hr;
837 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
838 iface, width, height, depth, levels, usage, format, pool, texture);
840 *texture = NULL;
841 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
842 if (!object)
843 return D3DERR_OUTOFVIDEOMEMORY;
845 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
846 if (FAILED(hr))
848 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
849 HeapFree(GetProcessHeap(), 0, object);
850 return hr;
853 TRACE("Created volume texture %p.\n", object);
854 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
856 return D3D_OK;
859 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
860 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
862 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
863 struct d3d8_texture *object;
864 HRESULT hr;
866 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
867 iface, edge_length, levels, usage, format, pool, texture);
869 *texture = NULL;
870 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
871 if (!object)
872 return D3DERR_OUTOFVIDEOMEMORY;
874 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
875 if (FAILED(hr))
877 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
878 HeapFree(GetProcessHeap(), 0, object);
879 return hr;
882 TRACE("Created cube texture %p.\n", object);
883 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
885 return hr;
888 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
889 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
891 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
892 struct d3d8_vertexbuffer *object;
893 HRESULT hr;
895 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
896 iface, size, usage, fvf, pool, buffer);
898 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
899 if (!object)
900 return D3DERR_OUTOFVIDEOMEMORY;
902 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
903 if (FAILED(hr))
905 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
906 HeapFree(GetProcessHeap(), 0, object);
907 return hr;
910 TRACE("Created vertex buffer %p.\n", object);
911 *buffer = &object->IDirect3DVertexBuffer8_iface;
913 return D3D_OK;
916 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
917 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
919 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
920 struct d3d8_indexbuffer *object;
921 HRESULT hr;
923 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
924 iface, size, usage, format, pool, buffer);
926 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
927 if (!object)
928 return D3DERR_OUTOFVIDEOMEMORY;
930 hr = indexbuffer_init(object, device, size, usage, format, pool);
931 if (FAILED(hr))
933 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
934 HeapFree(GetProcessHeap(), 0, object);
935 return hr;
938 TRACE("Created index buffer %p.\n", object);
939 *buffer = &object->IDirect3DIndexBuffer8_iface;
941 return D3D_OK;
944 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
945 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
946 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
948 struct wined3d_resource *sub_resource;
949 struct wined3d_resource_desc desc;
950 struct d3d8_surface *surface_impl;
951 struct wined3d_texture *texture;
952 HRESULT hr;
954 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
955 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
956 device, width, height, format, flags, surface,
957 usage, pool, multisample_type, multisample_quality);
959 desc.resource_type = WINED3D_RTYPE_TEXTURE;
960 desc.format = wined3dformat_from_d3dformat(format);
961 desc.multisample_type = multisample_type;
962 desc.multisample_quality = multisample_quality;
963 desc.usage = usage & WINED3DUSAGE_MASK;
964 desc.pool = pool;
965 desc.width = width;
966 desc.height = height;
967 desc.depth = 1;
968 desc.size = 0;
970 wined3d_mutex_lock();
972 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
973 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
975 wined3d_mutex_unlock();
976 WARN("Failed to create texture, hr %#x.\n", hr);
977 return hr;
980 sub_resource = wined3d_texture_get_sub_resource(texture, 0);
981 surface_impl = wined3d_resource_get_parent(sub_resource);
982 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
983 *surface = &surface_impl->IDirect3DSurface8_iface;
984 IDirect3DSurface8_AddRef(*surface);
985 wined3d_texture_decref(texture);
987 wined3d_mutex_unlock();
989 return D3D_OK;
992 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
993 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
994 IDirect3DSurface8 **surface)
996 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
997 DWORD flags = 0;
999 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1000 iface, width, height, format, multisample_type, lockable, surface);
1002 *surface = NULL;
1003 if (lockable)
1004 flags |= WINED3D_SURFACE_MAPPABLE;
1006 return d3d8_device_create_surface(device, width, height, format, flags, surface,
1007 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
1010 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1011 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1012 IDirect3DSurface8 **surface)
1014 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1016 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1017 iface, width, height, format, multisample_type, surface);
1019 *surface = NULL;
1021 /* TODO: Verify that Discard is false */
1022 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
1023 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1026 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1027 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1028 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1030 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1032 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1033 iface, width, height, format, surface);
1035 *surface = NULL;
1037 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
1038 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1041 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1042 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1043 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1045 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1046 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1047 enum wined3d_format_id src_format, dst_format;
1048 struct wined3d_resource_desc wined3d_desc;
1049 struct wined3d_resource *wined3d_resource;
1050 UINT src_w, src_h;
1052 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1053 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1055 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1056 * destination texture is in WINED3D_POOL_DEFAULT. */
1058 wined3d_mutex_lock();
1059 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
1060 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1061 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1063 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1064 wined3d_mutex_unlock();
1065 return D3DERR_INVALIDCALL;
1067 src_format = wined3d_desc.format;
1068 src_w = wined3d_desc.width;
1069 src_h = wined3d_desc.height;
1071 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
1072 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
1073 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1075 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1076 wined3d_mutex_unlock();
1077 return D3DERR_INVALIDCALL;
1079 dst_format = wined3d_desc.format;
1081 /* Check that the source and destination formats match */
1082 if (src_format != dst_format)
1084 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1085 src_surface, dst_surface);
1086 wined3d_mutex_unlock();
1087 return D3DERR_INVALIDCALL;
1090 /* Quick if complete copy ... */
1091 if (!rect_count && !src_rects && !dst_points)
1093 RECT rect = {0, 0, src_w, src_h};
1094 wined3d_surface_blt(dst->wined3d_surface, &rect,
1095 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
1097 else
1099 unsigned int i;
1100 /* Copy rect by rect */
1101 if (src_rects && dst_points)
1103 for (i = 0; i < rect_count; ++i)
1105 UINT w = src_rects[i].right - src_rects[i].left;
1106 UINT h = src_rects[i].bottom - src_rects[i].top;
1107 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1108 dst_points[i].x + w, dst_points[i].y + h};
1110 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1111 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1114 else
1116 for (i = 0; i < rect_count; ++i)
1118 UINT w = src_rects[i].right - src_rects[i].left;
1119 UINT h = src_rects[i].bottom - src_rects[i].top;
1120 RECT dst_rect = {0, 0, w, h};
1122 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1123 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1127 wined3d_mutex_unlock();
1129 return WINED3D_OK;
1132 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1133 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1135 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1136 struct d3d8_texture *src_impl, *dst_impl;
1137 HRESULT hr;
1139 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1141 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1142 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1144 wined3d_mutex_lock();
1145 hr = wined3d_device_update_texture(device->wined3d_device,
1146 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1147 wined3d_mutex_unlock();
1149 return hr;
1152 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1154 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1155 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1156 HRESULT hr;
1158 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1160 if (!dst_surface)
1162 WARN("Invalid destination surface passed.\n");
1163 return D3DERR_INVALIDCALL;
1166 wined3d_mutex_lock();
1167 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1168 wined3d_mutex_unlock();
1170 return hr;
1173 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1174 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1176 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1177 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1178 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1179 struct wined3d_rendertarget_view *original_dsv;
1180 HRESULT hr = D3D_OK;
1182 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1184 wined3d_mutex_lock();
1186 if (ds_impl)
1188 struct wined3d_rendertarget_view *original_rtv;
1189 struct wined3d_resource_desc ds_desc, rt_desc;
1190 struct wined3d_resource *wined3d_resource;
1191 struct d3d8_surface *original_surface;
1193 /* If no render target is passed in check the size against the current RT */
1194 if (!render_target)
1197 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1199 wined3d_mutex_unlock();
1200 return D3DERR_NOTFOUND;
1202 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1203 wined3d_resource = wined3d_surface_get_resource(original_surface->wined3d_surface);
1205 else
1206 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1207 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1209 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1210 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1212 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1214 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1215 wined3d_mutex_unlock();
1216 return D3DERR_INVALIDCALL;
1218 if (ds_desc.multisample_type != rt_desc.multisample_type
1219 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1221 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1222 wined3d_mutex_unlock();
1223 return D3DERR_INVALIDCALL;
1228 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1229 wined3d_device_set_depth_stencil_view(device->wined3d_device,
1230 ds_impl ? d3d8_surface_get_rendertarget_view(ds_impl) : NULL);
1231 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0,
1232 d3d8_surface_get_rendertarget_view(rt_impl), TRUE)))
1233 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1235 wined3d_mutex_unlock();
1237 return hr;
1240 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1242 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1243 struct wined3d_rendertarget_view *wined3d_rtv;
1244 struct d3d8_surface *surface_impl;
1245 HRESULT hr;
1247 TRACE("iface %p, render_target %p.\n", iface, render_target);
1249 if (!render_target)
1250 return D3DERR_INVALIDCALL;
1252 wined3d_mutex_lock();
1253 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1255 /* We want the sub resource parent here, since the view itself may be
1256 * internal to wined3d and may not have a parent. */
1257 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1258 *render_target = &surface_impl->IDirect3DSurface8_iface;
1259 IDirect3DSurface8_AddRef(*render_target);
1260 hr = D3D_OK;
1262 else
1264 ERR("Failed to get wined3d render target.\n");
1265 *render_target = NULL;
1266 hr = D3DERR_NOTFOUND;
1268 wined3d_mutex_unlock();
1270 return hr;
1273 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1275 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1276 struct wined3d_rendertarget_view *wined3d_dsv;
1277 struct d3d8_surface *surface_impl;
1278 HRESULT hr = D3D_OK;
1280 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1282 if (!depth_stencil)
1283 return D3DERR_INVALIDCALL;
1285 wined3d_mutex_lock();
1286 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1288 /* We want the sub resource parent here, since the view itself may be
1289 * internal to wined3d and may not have a parent. */
1290 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1291 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1292 IDirect3DSurface8_AddRef(*depth_stencil);
1294 else
1296 hr = WINED3DERR_NOTFOUND;
1297 *depth_stencil = NULL;
1299 wined3d_mutex_unlock();
1301 return hr;
1304 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1306 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1307 HRESULT hr;
1309 TRACE("iface %p.\n", iface);
1311 wined3d_mutex_lock();
1312 hr = wined3d_device_begin_scene(device->wined3d_device);
1313 wined3d_mutex_unlock();
1315 return hr;
1318 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1320 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1321 HRESULT hr;
1323 TRACE("iface %p.\n", iface);
1325 wined3d_mutex_lock();
1326 hr = wined3d_device_end_scene(device->wined3d_device);
1327 wined3d_mutex_unlock();
1329 return hr;
1332 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1333 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1335 const struct wined3d_color c =
1337 ((color >> 16) & 0xff) / 255.0f,
1338 ((color >> 8) & 0xff) / 255.0f,
1339 (color & 0xff) / 255.0f,
1340 ((color >> 24) & 0xff) / 255.0f,
1342 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1343 HRESULT hr;
1345 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1346 iface, rect_count, rects, flags, color, z, stencil);
1348 wined3d_mutex_lock();
1349 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1350 wined3d_mutex_unlock();
1352 return hr;
1355 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1356 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1358 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1360 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1362 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1363 wined3d_mutex_lock();
1364 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1365 wined3d_mutex_unlock();
1367 return D3D_OK;
1370 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1371 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1373 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1375 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1377 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1378 wined3d_mutex_lock();
1379 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1380 wined3d_mutex_unlock();
1382 return D3D_OK;
1385 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1386 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1388 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1390 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1392 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1393 wined3d_mutex_lock();
1394 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1395 wined3d_mutex_unlock();
1397 return D3D_OK;
1400 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1402 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1404 TRACE("iface %p, viewport %p.\n", iface, viewport);
1406 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1407 wined3d_mutex_lock();
1408 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1409 wined3d_mutex_unlock();
1411 return D3D_OK;
1414 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1416 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1418 TRACE("iface %p, viewport %p.\n", iface, viewport);
1420 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1421 wined3d_mutex_lock();
1422 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1423 wined3d_mutex_unlock();
1425 return D3D_OK;
1428 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1430 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1432 TRACE("iface %p, material %p.\n", iface, material);
1434 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1435 wined3d_mutex_lock();
1436 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1437 wined3d_mutex_unlock();
1439 return D3D_OK;
1442 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1444 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1446 TRACE("iface %p, material %p.\n", iface, material);
1448 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1449 wined3d_mutex_lock();
1450 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1451 wined3d_mutex_unlock();
1453 return D3D_OK;
1456 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1458 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1459 HRESULT hr;
1461 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1463 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1464 wined3d_mutex_lock();
1465 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1466 wined3d_mutex_unlock();
1468 return hr;
1471 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1473 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1474 HRESULT hr;
1476 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1478 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1479 wined3d_mutex_lock();
1480 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1481 wined3d_mutex_unlock();
1483 return hr;
1486 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1488 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1489 HRESULT hr;
1491 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1493 wined3d_mutex_lock();
1494 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1495 wined3d_mutex_unlock();
1497 return hr;
1500 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1502 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1503 HRESULT hr;
1505 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1507 wined3d_mutex_lock();
1508 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1509 wined3d_mutex_unlock();
1511 return hr;
1514 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1516 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1517 HRESULT hr;
1519 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1521 wined3d_mutex_lock();
1522 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1523 wined3d_mutex_unlock();
1525 return hr;
1528 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1530 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1531 HRESULT hr;
1533 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1535 wined3d_mutex_lock();
1536 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1537 wined3d_mutex_unlock();
1539 return hr;
1542 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1543 D3DRENDERSTATETYPE state, DWORD value)
1545 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1547 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1549 wined3d_mutex_lock();
1550 switch (state)
1552 case D3DRS_ZBIAS:
1553 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1554 break;
1556 default:
1557 wined3d_device_set_render_state(device->wined3d_device, state, value);
1559 wined3d_mutex_unlock();
1561 return D3D_OK;
1564 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1565 D3DRENDERSTATETYPE state, DWORD *value)
1567 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1569 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1571 wined3d_mutex_lock();
1572 switch (state)
1574 case D3DRS_ZBIAS:
1575 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1576 break;
1578 default:
1579 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1581 wined3d_mutex_unlock();
1583 return D3D_OK;
1586 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1588 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1589 HRESULT hr;
1591 TRACE("iface %p.\n", iface);
1593 wined3d_mutex_lock();
1594 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1595 wined3d_mutex_unlock();
1597 return hr;
1600 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1602 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1603 struct wined3d_stateblock *stateblock;
1604 HRESULT hr;
1606 TRACE("iface %p, token %p.\n", iface, token);
1608 /* Tell wineD3D to endstateblock before anything else (in case we run out
1609 * of memory later and cause locking problems)
1611 wined3d_mutex_lock();
1612 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1613 if (FAILED(hr))
1615 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1616 wined3d_mutex_unlock();
1617 return hr;
1620 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1621 wined3d_mutex_unlock();
1623 if (*token == D3D8_INVALID_HANDLE)
1625 ERR("Failed to create a handle\n");
1626 wined3d_mutex_lock();
1627 wined3d_stateblock_decref(stateblock);
1628 wined3d_mutex_unlock();
1629 return E_FAIL;
1631 ++*token;
1633 TRACE("Returning %#x (%p).\n", *token, stateblock);
1635 return hr;
1638 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1640 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1641 struct wined3d_stateblock *stateblock;
1643 TRACE("iface %p, token %#x.\n", iface, token);
1645 if (!token)
1646 return D3D_OK;
1648 wined3d_mutex_lock();
1649 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1650 if (!stateblock)
1652 WARN("Invalid handle (%#x) passed.\n", token);
1653 wined3d_mutex_unlock();
1654 return D3DERR_INVALIDCALL;
1656 wined3d_stateblock_apply(stateblock);
1657 wined3d_mutex_unlock();
1659 return D3D_OK;
1662 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1664 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1665 struct wined3d_stateblock *stateblock;
1667 TRACE("iface %p, token %#x.\n", iface, token);
1669 wined3d_mutex_lock();
1670 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1671 if (!stateblock)
1673 WARN("Invalid handle (%#x) passed.\n", token);
1674 wined3d_mutex_unlock();
1675 return D3DERR_INVALIDCALL;
1677 wined3d_stateblock_capture(stateblock);
1678 wined3d_mutex_unlock();
1680 return D3D_OK;
1683 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1685 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1686 struct wined3d_stateblock *stateblock;
1688 TRACE("iface %p, token %#x.\n", iface, token);
1690 wined3d_mutex_lock();
1691 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1693 if (!stateblock)
1695 WARN("Invalid handle (%#x) passed.\n", token);
1696 wined3d_mutex_unlock();
1697 return D3DERR_INVALIDCALL;
1700 if (wined3d_stateblock_decref(stateblock))
1702 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1704 wined3d_mutex_unlock();
1706 return D3D_OK;
1709 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1710 D3DSTATEBLOCKTYPE type, DWORD *handle)
1712 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1713 struct wined3d_stateblock *stateblock;
1714 HRESULT hr;
1716 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1718 if (type != D3DSBT_ALL
1719 && type != D3DSBT_PIXELSTATE
1720 && type != D3DSBT_VERTEXSTATE)
1722 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1723 return D3DERR_INVALIDCALL;
1726 wined3d_mutex_lock();
1727 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1728 if (FAILED(hr))
1730 wined3d_mutex_unlock();
1731 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1732 return hr;
1735 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1736 wined3d_mutex_unlock();
1738 if (*handle == D3D8_INVALID_HANDLE)
1740 ERR("Failed to allocate a handle.\n");
1741 wined3d_mutex_lock();
1742 wined3d_stateblock_decref(stateblock);
1743 wined3d_mutex_unlock();
1744 return E_FAIL;
1746 ++*handle;
1748 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1750 return hr;
1753 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1755 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1756 HRESULT hr;
1758 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1759 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1761 wined3d_mutex_lock();
1762 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1763 wined3d_mutex_unlock();
1765 return hr;
1768 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1770 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1771 HRESULT hr;
1773 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1775 wined3d_mutex_lock();
1776 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1777 wined3d_mutex_unlock();
1779 return hr;
1782 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1784 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1785 struct wined3d_texture *wined3d_texture;
1786 struct d3d8_texture *texture_impl;
1788 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1790 if (!texture)
1791 return D3DERR_INVALIDCALL;
1793 wined3d_mutex_lock();
1794 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1796 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1797 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1798 IDirect3DBaseTexture8_AddRef(*texture);
1800 else
1802 *texture = NULL;
1804 wined3d_mutex_unlock();
1806 return D3D_OK;
1809 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1811 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1812 struct d3d8_texture *texture_impl;
1813 HRESULT hr;
1815 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1817 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1819 wined3d_mutex_lock();
1820 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1821 texture_impl ? texture_impl->wined3d_texture : NULL);
1822 wined3d_mutex_unlock();
1824 return hr;
1827 static const struct tss_lookup
1829 BOOL sampler_state;
1830 enum wined3d_texture_stage_state state;
1832 tss_lookup[] =
1834 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1835 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1836 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1837 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1838 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1839 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1840 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1841 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1842 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1843 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1844 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1845 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1846 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1847 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1848 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1849 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1850 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1851 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1852 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1853 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1854 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1855 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1856 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1857 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1858 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1859 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1860 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1861 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1862 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1865 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1866 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1868 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1869 const struct tss_lookup *l;
1871 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1873 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1875 WARN("Invalid Type %#x passed.\n", Type);
1876 return D3D_OK;
1879 l = &tss_lookup[Type];
1881 wined3d_mutex_lock();
1882 if (l->sampler_state)
1883 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1884 else
1885 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1886 wined3d_mutex_unlock();
1888 return D3D_OK;
1891 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1892 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1894 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1895 const struct tss_lookup *l;
1897 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1899 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1901 WARN("Invalid type %#x passed.\n", type);
1902 return D3D_OK;
1905 l = &tss_lookup[type];
1907 wined3d_mutex_lock();
1908 if (l->sampler_state)
1909 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1910 else
1911 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1912 wined3d_mutex_unlock();
1914 return D3D_OK;
1917 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1919 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1920 HRESULT hr;
1922 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1924 wined3d_mutex_lock();
1925 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1926 wined3d_mutex_unlock();
1928 return hr;
1931 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1932 DWORD info_id, void *info, DWORD info_size)
1934 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1936 return D3D_OK;
1939 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1940 UINT palette_idx, const PALETTEENTRY *entries)
1942 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1944 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1945 * does not have a d3d8/9-style palette API */
1947 return D3D_OK;
1950 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1951 UINT palette_idx, PALETTEENTRY *entries)
1953 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1955 return D3DERR_INVALIDCALL;
1958 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1960 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1962 return D3D_OK;
1965 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1967 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1969 return D3DERR_INVALIDCALL;
1972 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1973 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1975 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1976 HRESULT hr;
1978 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1979 iface, primitive_type, start_vertex, primitive_count);
1981 wined3d_mutex_lock();
1982 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1983 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1984 vertex_count_from_primitive_count(primitive_type, primitive_count));
1985 wined3d_mutex_unlock();
1987 return hr;
1990 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1991 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1992 UINT start_idx, UINT primitive_count)
1994 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1995 HRESULT hr;
1997 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1998 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2000 wined3d_mutex_lock();
2001 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2002 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2003 vertex_count_from_primitive_count(primitive_type, primitive_count));
2004 wined3d_mutex_unlock();
2006 return hr;
2009 /* The caller is responsible for wined3d locking */
2010 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2012 HRESULT hr;
2014 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2016 UINT size = max(device->vertex_buffer_size * 2, min_size);
2017 struct wined3d_buffer *buffer;
2019 TRACE("Growing vertex buffer to %u bytes\n", size);
2021 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2022 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2023 if (FAILED(hr))
2025 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2026 return hr;
2029 if (device->vertex_buffer)
2030 wined3d_buffer_decref(device->vertex_buffer);
2032 device->vertex_buffer = buffer;
2033 device->vertex_buffer_size = size;
2034 device->vertex_buffer_pos = 0;
2036 return D3D_OK;
2039 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2040 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2041 UINT stride)
2043 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2044 HRESULT hr;
2045 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2046 UINT size = vtx_count * stride;
2047 UINT vb_pos, align;
2048 BYTE *buffer_data;
2050 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2051 iface, primitive_type, primitive_count, data, stride);
2053 if (!primitive_count)
2055 WARN("primitive_count is 0, returning D3D_OK\n");
2056 return D3D_OK;
2059 wined3d_mutex_lock();
2060 hr = d3d8_device_prepare_vertex_buffer(device, size);
2061 if (FAILED(hr))
2062 goto done;
2064 vb_pos = device->vertex_buffer_pos;
2065 align = vb_pos % stride;
2066 if (align) align = stride - align;
2067 if (vb_pos + size + align > device->vertex_buffer_size)
2068 vb_pos = 0;
2069 else
2070 vb_pos += align;
2072 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
2073 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2074 if (FAILED(hr))
2075 goto done;
2076 memcpy(buffer_data, data, size);
2077 wined3d_buffer_unmap(device->vertex_buffer);
2078 device->vertex_buffer_pos = vb_pos + size;
2080 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2081 if (FAILED(hr))
2082 goto done;
2084 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2085 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2086 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2088 done:
2089 wined3d_mutex_unlock();
2090 return hr;
2093 /* The caller is responsible for wined3d locking */
2094 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2096 HRESULT hr;
2098 if (device->index_buffer_size < min_size || !device->index_buffer)
2100 UINT size = max(device->index_buffer_size * 2, min_size);
2101 struct wined3d_buffer *buffer;
2103 TRACE("Growing index buffer to %u bytes\n", size);
2105 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2106 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2107 if (FAILED(hr))
2109 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2110 return hr;
2113 if (device->index_buffer)
2114 wined3d_buffer_decref(device->index_buffer);
2116 device->index_buffer = buffer;
2117 device->index_buffer_size = size;
2118 device->index_buffer_pos = 0;
2120 return D3D_OK;
2123 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2124 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2125 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2126 const void *vertex_data, UINT vertex_stride)
2128 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2129 HRESULT hr;
2130 BYTE *buffer_data;
2132 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2133 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2134 UINT idx_size = idx_count * idx_fmt_size;
2135 UINT ib_pos;
2137 UINT vtx_size = vertex_count * vertex_stride;
2138 UINT vb_pos, align;
2140 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2141 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2142 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2143 index_data, index_format, vertex_data, vertex_stride);
2145 if (!primitive_count)
2147 WARN("primitive_count is 0, returning D3D_OK\n");
2148 return D3D_OK;
2151 wined3d_mutex_lock();
2153 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2154 if (FAILED(hr))
2155 goto done;
2157 vb_pos = device->vertex_buffer_pos;
2158 align = vb_pos % vertex_stride;
2159 if (align) align = vertex_stride - align;
2160 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2161 vb_pos = 0;
2162 else
2163 vb_pos += align;
2165 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2166 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2167 if (FAILED(hr))
2168 goto done;
2169 memcpy(buffer_data, vertex_data, vtx_size);
2170 wined3d_buffer_unmap(device->vertex_buffer);
2171 device->vertex_buffer_pos = vb_pos + vtx_size;
2173 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2174 if (FAILED(hr))
2175 goto done;
2177 ib_pos = device->index_buffer_pos;
2178 align = ib_pos % idx_fmt_size;
2179 if (align) align = idx_fmt_size - align;
2180 if (ib_pos + idx_size + align > device->index_buffer_size)
2181 ib_pos = 0;
2182 else
2183 ib_pos += align;
2185 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2186 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2187 if (FAILED(hr))
2188 goto done;
2189 memcpy(buffer_data, index_data, idx_size);
2190 wined3d_buffer_unmap(device->index_buffer);
2191 device->index_buffer_pos = ib_pos + idx_size;
2193 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2194 if (FAILED(hr))
2195 goto done;
2197 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2198 wined3dformat_from_d3dformat(index_format));
2199 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2201 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2202 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2204 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2205 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2206 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2208 done:
2209 wined3d_mutex_unlock();
2210 return hr;
2213 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2214 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2216 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2217 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2218 HRESULT hr;
2220 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2221 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2223 wined3d_mutex_lock();
2224 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2225 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2226 wined3d_mutex_unlock();
2228 return hr;
2231 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2232 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2234 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2235 struct d3d8_vertex_shader *object;
2236 DWORD shader_handle;
2237 DWORD handle;
2238 HRESULT hr;
2240 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2241 iface, declaration, byte_code, shader, usage);
2243 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2244 if (!object)
2246 *shader = 0;
2247 return E_OUTOFMEMORY;
2250 wined3d_mutex_lock();
2251 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2252 wined3d_mutex_unlock();
2253 if (handle == D3D8_INVALID_HANDLE)
2255 ERR("Failed to allocate vertex shader handle.\n");
2256 HeapFree(GetProcessHeap(), 0, object);
2257 *shader = 0;
2258 return E_OUTOFMEMORY;
2261 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2263 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2264 if (FAILED(hr))
2266 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2267 wined3d_mutex_lock();
2268 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2269 wined3d_mutex_unlock();
2270 HeapFree(GetProcessHeap(), 0, object);
2271 *shader = 0;
2272 return hr;
2275 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2276 *shader = shader_handle;
2278 return D3D_OK;
2281 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2283 struct d3d8_vertex_declaration *d3d8_declaration;
2284 struct FvfToDecl *convertedDecls = device->decls;
2285 int p, low, high; /* deliberately signed */
2286 HRESULT hr;
2288 TRACE("Searching for declaration for fvf %08x... ", fvf);
2290 low = 0;
2291 high = device->numConvertedDecls - 1;
2292 while (low <= high)
2294 p = (low + high) >> 1;
2295 TRACE("%d ", p);
2297 if (convertedDecls[p].fvf == fvf)
2299 TRACE("found %p\n", convertedDecls[p].declaration);
2300 return convertedDecls[p].declaration;
2303 if (convertedDecls[p].fvf < fvf)
2304 low = p + 1;
2305 else
2306 high = p - 1;
2308 TRACE("not found. Creating and inserting at position %d.\n", low);
2310 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2311 return NULL;
2313 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2315 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2316 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2317 return NULL;
2320 if (device->declArraySize == device->numConvertedDecls)
2322 UINT grow = device->declArraySize / 2;
2324 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2325 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2326 if (!convertedDecls)
2328 d3d8_vertex_declaration_destroy(d3d8_declaration);
2329 return NULL;
2331 device->decls = convertedDecls;
2332 device->declArraySize += grow;
2335 memmove(convertedDecls + low + 1, convertedDecls + low,
2336 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2337 convertedDecls[low].declaration = d3d8_declaration;
2338 convertedDecls[low].fvf = fvf;
2339 ++device->numConvertedDecls;
2341 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2343 return d3d8_declaration;
2346 static HRESULT WINAPI d3d8_device_SetVertexShader(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 if (VS_HIGHESTFIXEDFXF >= shader)
2355 TRACE("Setting FVF, %#x\n", shader);
2357 wined3d_mutex_lock();
2358 wined3d_device_set_vertex_declaration(device->wined3d_device,
2359 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2360 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2361 wined3d_mutex_unlock();
2363 return D3D_OK;
2366 TRACE("Setting shader\n");
2368 wined3d_mutex_lock();
2369 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2371 WARN("Invalid handle (%#x) passed.\n", shader);
2372 wined3d_mutex_unlock();
2374 return D3DERR_INVALIDCALL;
2377 wined3d_device_set_vertex_declaration(device->wined3d_device,
2378 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2379 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2380 wined3d_mutex_unlock();
2382 return D3D_OK;
2385 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2387 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2388 struct wined3d_vertex_declaration *wined3d_declaration;
2389 struct d3d8_vertex_declaration *d3d8_declaration;
2391 TRACE("iface %p, shader %p.\n", iface, shader);
2393 wined3d_mutex_lock();
2394 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2396 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2397 *shader = d3d8_declaration->shader_handle;
2399 else
2401 *shader = 0;
2403 wined3d_mutex_unlock();
2405 TRACE("Returning %#x.\n", *shader);
2407 return D3D_OK;
2410 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2412 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2413 struct d3d8_vertex_shader *shader_impl;
2415 TRACE("iface %p, shader %#x.\n", iface, shader);
2417 wined3d_mutex_lock();
2418 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2420 WARN("Invalid handle (%#x) passed.\n", shader);
2421 wined3d_mutex_unlock();
2423 return D3DERR_INVALIDCALL;
2426 if (shader_impl->wined3d_shader
2427 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2428 IDirect3DDevice8_SetVertexShader(iface, 0);
2430 wined3d_mutex_unlock();
2432 d3d8_vertex_shader_destroy(shader_impl);
2434 return D3D_OK;
2437 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2438 DWORD start_register, const void *data, DWORD count)
2440 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2441 HRESULT hr;
2443 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2444 iface, start_register, data, count);
2446 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2448 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2449 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2450 return D3DERR_INVALIDCALL;
2453 wined3d_mutex_lock();
2454 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2455 wined3d_mutex_unlock();
2457 return hr;
2460 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2461 DWORD start_register, void *data, DWORD count)
2463 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2464 HRESULT hr;
2466 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2467 iface, start_register, data, count);
2469 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2471 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2472 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2473 return D3DERR_INVALIDCALL;
2476 wined3d_mutex_lock();
2477 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2478 wined3d_mutex_unlock();
2480 return hr;
2483 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2484 DWORD shader, void *data, DWORD *data_size)
2486 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2487 struct d3d8_vertex_declaration *declaration;
2488 struct d3d8_vertex_shader *shader_impl;
2490 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2491 iface, shader, data, data_size);
2493 wined3d_mutex_lock();
2494 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2495 wined3d_mutex_unlock();
2497 if (!shader_impl)
2499 WARN("Invalid handle (%#x) passed.\n", shader);
2500 return D3DERR_INVALIDCALL;
2502 declaration = shader_impl->vertex_declaration;
2504 if (!data)
2506 *data_size = declaration->elements_size;
2507 return D3D_OK;
2510 /* MSDN claims that if *data_size is smaller than the required size
2511 * we should write the required size and return D3DERR_MOREDATA.
2512 * That's not actually true. */
2513 if (*data_size < declaration->elements_size)
2514 return D3DERR_INVALIDCALL;
2516 memcpy(data, declaration->elements, declaration->elements_size);
2518 return D3D_OK;
2521 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2522 DWORD shader, void *data, DWORD *data_size)
2524 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2525 struct d3d8_vertex_shader *shader_impl = NULL;
2526 HRESULT hr;
2528 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2529 iface, shader, data, data_size);
2531 wined3d_mutex_lock();
2532 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2534 WARN("Invalid handle (%#x) passed.\n", shader);
2535 wined3d_mutex_unlock();
2537 return D3DERR_INVALIDCALL;
2540 if (!shader_impl->wined3d_shader)
2542 wined3d_mutex_unlock();
2543 *data_size = 0;
2544 return D3D_OK;
2547 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2548 wined3d_mutex_unlock();
2550 return hr;
2553 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2554 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2556 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2557 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2559 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2561 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2562 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2563 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2564 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2565 * problem)
2567 wined3d_mutex_lock();
2568 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2569 wined3d_device_set_index_buffer(device->wined3d_device,
2570 ib ? ib->wined3d_buffer : NULL,
2571 ib ? ib->format : WINED3DFMT_UNKNOWN);
2572 wined3d_mutex_unlock();
2574 return D3D_OK;
2577 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2578 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2580 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2581 enum wined3d_format_id wined3d_format;
2582 struct wined3d_buffer *wined3d_buffer;
2583 struct d3d8_indexbuffer *buffer_impl;
2585 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2587 if (!buffer)
2588 return D3DERR_INVALIDCALL;
2590 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2591 wined3d_mutex_lock();
2592 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2593 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2595 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2596 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2597 IDirect3DIndexBuffer8_AddRef(*buffer);
2599 else
2601 *buffer = NULL;
2603 wined3d_mutex_unlock();
2605 return D3D_OK;
2608 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2609 const DWORD *byte_code, DWORD *shader)
2611 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2612 struct d3d8_pixel_shader *object;
2613 DWORD shader_handle;
2614 DWORD handle;
2615 HRESULT hr;
2617 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2619 if (!shader)
2620 return D3DERR_INVALIDCALL;
2622 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2623 if (!object)
2624 return E_OUTOFMEMORY;
2626 wined3d_mutex_lock();
2627 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2628 wined3d_mutex_unlock();
2629 if (handle == D3D8_INVALID_HANDLE)
2631 ERR("Failed to allocate pixel shader handle.\n");
2632 HeapFree(GetProcessHeap(), 0, object);
2633 return E_OUTOFMEMORY;
2636 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2638 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2639 if (FAILED(hr))
2641 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2642 wined3d_mutex_lock();
2643 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2644 wined3d_mutex_unlock();
2645 HeapFree(GetProcessHeap(), 0, object);
2646 *shader = 0;
2647 return hr;
2650 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2651 *shader = shader_handle;
2653 return D3D_OK;
2656 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2658 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2659 struct d3d8_pixel_shader *shader_impl;
2661 TRACE("iface %p, shader %#x.\n", iface, shader);
2663 wined3d_mutex_lock();
2665 if (!shader)
2667 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2668 wined3d_mutex_unlock();
2669 return D3D_OK;
2672 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2674 WARN("Invalid handle (%#x) passed.\n", shader);
2675 wined3d_mutex_unlock();
2676 return D3DERR_INVALIDCALL;
2679 TRACE("Setting shader %p.\n", shader_impl);
2680 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2681 wined3d_mutex_unlock();
2683 return D3D_OK;
2686 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2688 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2689 struct wined3d_shader *object;
2691 TRACE("iface %p, shader %p.\n", iface, shader);
2693 if (!shader)
2694 return D3DERR_INVALIDCALL;
2696 wined3d_mutex_lock();
2697 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2699 struct d3d8_pixel_shader *d3d8_shader;
2700 d3d8_shader = wined3d_shader_get_parent(object);
2701 *shader = d3d8_shader->handle;
2703 else
2705 *shader = 0;
2707 wined3d_mutex_unlock();
2709 TRACE("Returning %#x.\n", *shader);
2711 return D3D_OK;
2714 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2717 struct d3d8_pixel_shader *shader_impl;
2719 TRACE("iface %p, shader %#x.\n", iface, shader);
2721 wined3d_mutex_lock();
2723 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2725 WARN("Invalid handle (%#x) passed.\n", shader);
2726 wined3d_mutex_unlock();
2727 return D3DERR_INVALIDCALL;
2730 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2731 IDirect3DDevice8_SetPixelShader(iface, 0);
2733 wined3d_mutex_unlock();
2735 d3d8_pixel_shader_destroy(shader_impl);
2737 return D3D_OK;
2740 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2741 DWORD start_register, const void *data, DWORD count)
2743 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2744 HRESULT hr;
2746 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2747 iface, start_register, data, count);
2749 wined3d_mutex_lock();
2750 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2751 wined3d_mutex_unlock();
2753 return hr;
2756 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2757 DWORD start_register, void *data, DWORD count)
2759 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2760 HRESULT hr;
2762 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2763 iface, start_register, data, count);
2765 wined3d_mutex_lock();
2766 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2767 wined3d_mutex_unlock();
2769 return hr;
2772 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2773 DWORD shader, void *data, DWORD *data_size)
2775 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2776 struct d3d8_pixel_shader *shader_impl = NULL;
2777 HRESULT hr;
2779 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2780 iface, shader, data, data_size);
2782 wined3d_mutex_lock();
2783 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2785 WARN("Invalid handle (%#x) passed.\n", shader);
2786 wined3d_mutex_unlock();
2788 return D3DERR_INVALIDCALL;
2791 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2792 wined3d_mutex_unlock();
2794 return hr;
2797 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2798 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2800 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2801 iface, handle, segment_count, patch_info);
2802 return D3D_OK;
2805 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2806 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2808 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2809 iface, handle, segment_count, patch_info);
2810 return D3D_OK;
2813 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2815 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2816 return D3DERR_INVALIDCALL;
2819 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2820 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2822 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2823 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2824 HRESULT hr;
2826 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2827 iface, stream_idx, buffer, stride);
2829 wined3d_mutex_lock();
2830 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2831 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2832 wined3d_mutex_unlock();
2834 return hr;
2837 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2838 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2840 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2841 struct d3d8_vertexbuffer *buffer_impl;
2842 struct wined3d_buffer *wined3d_buffer = NULL;
2843 HRESULT hr;
2845 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2846 iface, stream_idx, buffer, stride);
2848 if (!buffer)
2849 return D3DERR_INVALIDCALL;
2851 wined3d_mutex_lock();
2852 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2853 if (SUCCEEDED(hr) && wined3d_buffer)
2855 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2856 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2857 IDirect3DVertexBuffer8_AddRef(*buffer);
2859 else
2861 if (FAILED(hr))
2862 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2863 *buffer = NULL;
2865 wined3d_mutex_unlock();
2867 return hr;
2870 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2872 d3d8_device_QueryInterface,
2873 d3d8_device_AddRef,
2874 d3d8_device_Release,
2875 d3d8_device_TestCooperativeLevel,
2876 d3d8_device_GetAvailableTextureMem,
2877 d3d8_device_ResourceManagerDiscardBytes,
2878 d3d8_device_GetDirect3D,
2879 d3d8_device_GetDeviceCaps,
2880 d3d8_device_GetDisplayMode,
2881 d3d8_device_GetCreationParameters,
2882 d3d8_device_SetCursorProperties,
2883 d3d8_device_SetCursorPosition,
2884 d3d8_device_ShowCursor,
2885 d3d8_device_CreateAdditionalSwapChain,
2886 d3d8_device_Reset,
2887 d3d8_device_Present,
2888 d3d8_device_GetBackBuffer,
2889 d3d8_device_GetRasterStatus,
2890 d3d8_device_SetGammaRamp,
2891 d3d8_device_GetGammaRamp,
2892 d3d8_device_CreateTexture,
2893 d3d8_device_CreateVolumeTexture,
2894 d3d8_device_CreateCubeTexture,
2895 d3d8_device_CreateVertexBuffer,
2896 d3d8_device_CreateIndexBuffer,
2897 d3d8_device_CreateRenderTarget,
2898 d3d8_device_CreateDepthStencilSurface,
2899 d3d8_device_CreateImageSurface,
2900 d3d8_device_CopyRects,
2901 d3d8_device_UpdateTexture,
2902 d3d8_device_GetFrontBuffer,
2903 d3d8_device_SetRenderTarget,
2904 d3d8_device_GetRenderTarget,
2905 d3d8_device_GetDepthStencilSurface,
2906 d3d8_device_BeginScene,
2907 d3d8_device_EndScene,
2908 d3d8_device_Clear,
2909 d3d8_device_SetTransform,
2910 d3d8_device_GetTransform,
2911 d3d8_device_MultiplyTransform,
2912 d3d8_device_SetViewport,
2913 d3d8_device_GetViewport,
2914 d3d8_device_SetMaterial,
2915 d3d8_device_GetMaterial,
2916 d3d8_device_SetLight,
2917 d3d8_device_GetLight,
2918 d3d8_device_LightEnable,
2919 d3d8_device_GetLightEnable,
2920 d3d8_device_SetClipPlane,
2921 d3d8_device_GetClipPlane,
2922 d3d8_device_SetRenderState,
2923 d3d8_device_GetRenderState,
2924 d3d8_device_BeginStateBlock,
2925 d3d8_device_EndStateBlock,
2926 d3d8_device_ApplyStateBlock,
2927 d3d8_device_CaptureStateBlock,
2928 d3d8_device_DeleteStateBlock,
2929 d3d8_device_CreateStateBlock,
2930 d3d8_device_SetClipStatus,
2931 d3d8_device_GetClipStatus,
2932 d3d8_device_GetTexture,
2933 d3d8_device_SetTexture,
2934 d3d8_device_GetTextureStageState,
2935 d3d8_device_SetTextureStageState,
2936 d3d8_device_ValidateDevice,
2937 d3d8_device_GetInfo,
2938 d3d8_device_SetPaletteEntries,
2939 d3d8_device_GetPaletteEntries,
2940 d3d8_device_SetCurrentTexturePalette,
2941 d3d8_device_GetCurrentTexturePalette,
2942 d3d8_device_DrawPrimitive,
2943 d3d8_device_DrawIndexedPrimitive,
2944 d3d8_device_DrawPrimitiveUP,
2945 d3d8_device_DrawIndexedPrimitiveUP,
2946 d3d8_device_ProcessVertices,
2947 d3d8_device_CreateVertexShader,
2948 d3d8_device_SetVertexShader,
2949 d3d8_device_GetVertexShader,
2950 d3d8_device_DeleteVertexShader,
2951 d3d8_device_SetVertexShaderConstant,
2952 d3d8_device_GetVertexShaderConstant,
2953 d3d8_device_GetVertexShaderDeclaration,
2954 d3d8_device_GetVertexShaderFunction,
2955 d3d8_device_SetStreamSource,
2956 d3d8_device_GetStreamSource,
2957 d3d8_device_SetIndices,
2958 d3d8_device_GetIndices,
2959 d3d8_device_CreatePixelShader,
2960 d3d8_device_SetPixelShader,
2961 d3d8_device_GetPixelShader,
2962 d3d8_device_DeletePixelShader,
2963 d3d8_device_SetPixelShaderConstant,
2964 d3d8_device_GetPixelShaderConstant,
2965 d3d8_device_GetPixelShaderFunction,
2966 d3d8_device_DrawRectPatch,
2967 d3d8_device_DrawTriPatch,
2968 d3d8_device_DeletePatch,
2971 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2973 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2976 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2977 struct wined3d_device *device)
2979 TRACE("device_parent %p, device %p\n", device_parent, device);
2982 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2984 TRACE("device_parent %p.\n", device_parent);
2987 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
2989 struct d3d8_device *device = device_from_device_parent(device_parent);
2991 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
2993 if (!activate)
2994 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
2995 else
2996 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
2999 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3000 void *container_parent, struct wined3d_surface *surface, void **parent,
3001 const struct wined3d_parent_ops **parent_ops)
3003 struct d3d8_surface *d3d_surface;
3005 TRACE("device_parent %p, container_parent %p, surface %p, parent %p, parent_ops %p.\n",
3006 device_parent, container_parent, surface, parent, parent_ops);
3008 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3009 return E_OUTOFMEMORY;
3011 surface_init(d3d_surface, container_parent, surface, parent_ops);
3012 *parent = d3d_surface;
3013 TRACE("Created surface %p.\n", d3d_surface);
3015 return D3D_OK;
3018 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3019 void *container_parent, struct wined3d_volume *volume, void **parent,
3020 const struct wined3d_parent_ops **parent_ops)
3022 struct d3d8_volume *d3d_volume;
3024 TRACE("device_parent %p, container_parent %p, volume %p, parent %p, parent_ops %p.\n",
3025 device_parent, container_parent, volume, parent, parent_ops);
3027 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3028 return E_OUTOFMEMORY;
3030 volume_init(d3d_volume, container_parent, volume, parent_ops);
3031 *parent = d3d_volume;
3032 TRACE("Created volume %p.\n", d3d_volume);
3034 return D3D_OK;
3037 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
3038 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
3040 struct d3d8_device *device = device_from_device_parent(device_parent);
3041 struct wined3d_resource_desc texture_desc;
3042 struct d3d8_surface *d3d_surface;
3043 struct wined3d_texture *texture;
3044 HRESULT hr;
3046 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
3047 device_parent, container_parent, desc, surface);
3049 texture_desc = *desc;
3050 texture_desc.resource_type = WINED3D_RTYPE_TEXTURE;
3051 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &texture_desc, 1,
3052 WINED3D_SURFACE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface, &d3d8_null_wined3d_parent_ops, &texture)))
3054 WARN("Failed to create texture, hr %#x.\n", hr);
3055 return hr;
3058 *surface = wined3d_surface_from_resource(wined3d_texture_get_sub_resource(texture, 0));
3059 wined3d_surface_incref(*surface);
3060 wined3d_texture_decref(texture);
3062 d3d_surface = wined3d_surface_get_parent(*surface);
3063 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3065 return hr;
3068 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3069 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3071 struct d3d8_device *device = device_from_device_parent(device_parent);
3072 struct d3d8_swapchain *d3d_swapchain;
3073 HRESULT hr;
3075 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3077 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3079 WARN("Failed to create swapchain, hr %#x.\n", hr);
3080 *swapchain = NULL;
3081 return hr;
3084 *swapchain = d3d_swapchain->wined3d_swapchain;
3085 wined3d_swapchain_incref(*swapchain);
3086 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3088 return hr;
3091 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3093 device_parent_wined3d_device_created,
3094 device_parent_mode_changed,
3095 device_parent_activate,
3096 device_parent_surface_created,
3097 device_parent_volume_created,
3098 device_parent_create_swapchain_surface,
3099 device_parent_create_swapchain,
3102 static void setup_fpu(void)
3104 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3105 WORD cw;
3106 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3107 cw = (cw & ~0xf3f) | 0x3f;
3108 __asm__ volatile ("fldcw %0" : : "m" (cw));
3109 #elif defined(__i386__) && defined(_MSC_VER)
3110 WORD cw;
3111 __asm fnstcw cw;
3112 cw = (cw & ~0xf3f) | 0x3f;
3113 __asm fldcw cw;
3114 #else
3115 FIXME("FPU setup not implemented for this platform.\n");
3116 #endif
3119 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3120 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3122 struct wined3d_swapchain_desc swapchain_desc;
3123 HRESULT hr;
3125 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3126 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3127 device->ref = 1;
3128 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3129 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3130 if (!device->handle_table.entries)
3132 ERR("Failed to allocate handle table memory.\n");
3133 return E_OUTOFMEMORY;
3135 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3137 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3139 wined3d_mutex_lock();
3140 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3141 &device->device_parent, &device->wined3d_device);
3142 if (FAILED(hr))
3144 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3145 wined3d_mutex_unlock();
3146 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3147 return hr;
3150 if (!parameters->Windowed)
3152 HWND device_window = parameters->hDeviceWindow;
3154 if (!focus_window)
3155 focus_window = device_window;
3156 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3158 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3159 wined3d_device_decref(device->wined3d_device);
3160 wined3d_mutex_unlock();
3161 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3162 return hr;
3165 if (!device_window)
3166 device_window = focus_window;
3167 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3168 parameters->BackBufferWidth,
3169 parameters->BackBufferHeight);
3172 if (flags & D3DCREATE_MULTITHREADED)
3173 wined3d_device_set_multithreaded(device->wined3d_device);
3175 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3177 wined3d_device_release_focus_window(device->wined3d_device);
3178 wined3d_device_decref(device->wined3d_device);
3179 wined3d_mutex_unlock();
3180 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3181 return D3DERR_INVALIDCALL;
3184 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3185 if (FAILED(hr))
3187 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3188 wined3d_device_release_focus_window(device->wined3d_device);
3189 wined3d_device_decref(device->wined3d_device);
3190 wined3d_mutex_unlock();
3191 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3192 return hr;
3195 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3196 wined3d_mutex_unlock();
3198 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3200 device->declArraySize = 16;
3201 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3202 if (!device->decls)
3204 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3205 hr = E_OUTOFMEMORY;
3206 goto err;
3209 device->d3d_parent = &parent->IDirect3D8_iface;
3210 IDirect3D8_AddRef(device->d3d_parent);
3212 return D3D_OK;
3214 err:
3215 wined3d_mutex_lock();
3216 wined3d_device_uninit_3d(device->wined3d_device);
3217 wined3d_device_release_focus_window(device->wined3d_device);
3218 wined3d_device_decref(device->wined3d_device);
3219 wined3d_mutex_unlock();
3220 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3221 return hr;