wined3d: Allow surface flags to be passed to texture creation functions.
[wine/wine-gecko.git] / dlls / d3d8 / device.c
blob5bdf2fd3482b318741b37a702509f2c562df51e4
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "wingdi.h"
33 #include "wine/debug.h"
35 #include "d3d8_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
39 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
41 BYTE *c = (BYTE *)&format;
43 /* Don't translate FOURCC formats */
44 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
46 switch(format)
48 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
49 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
50 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
51 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
52 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
53 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
54 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
55 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
56 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
57 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
58 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
59 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
60 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
61 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
62 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
63 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
64 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
65 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
66 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
67 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
68 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
69 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
70 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
71 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
72 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
73 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
74 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
75 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
76 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
77 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
78 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
79 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
80 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
81 case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
82 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
83 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
84 default:
85 FIXME("Unhandled wined3d format %#x.\n", format);
86 return D3DFMT_UNKNOWN;
90 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
92 BYTE *c = (BYTE *)&format;
94 /* Don't translate FOURCC formats */
95 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
97 switch(format)
99 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
100 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
101 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
102 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
103 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
104 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
105 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
106 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
107 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
108 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
109 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
110 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
111 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
112 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
113 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
114 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
115 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
116 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
117 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
118 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
119 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
120 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
121 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
122 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
123 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
124 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
125 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
126 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
127 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
128 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
129 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
130 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
131 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
132 case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
133 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
134 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
135 default:
136 FIXME("Unhandled D3DFORMAT %#x\n", format);
137 return WINED3DFMT_UNKNOWN;
141 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
143 switch(primitive_type)
145 case D3DPT_POINTLIST:
146 return primitive_count;
148 case D3DPT_LINELIST:
149 return primitive_count * 2;
151 case D3DPT_LINESTRIP:
152 return primitive_count + 1;
154 case D3DPT_TRIANGLELIST:
155 return primitive_count * 3;
157 case D3DPT_TRIANGLESTRIP:
158 case D3DPT_TRIANGLEFAN:
159 return primitive_count + 2;
161 default:
162 FIXME("Unhandled primitive type %#x\n", primitive_type);
163 return 0;
167 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
168 const struct wined3d_swapchain_desc *swapchain_desc)
170 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
171 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
172 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
173 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
174 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
175 present_parameters->SwapEffect = swapchain_desc->swap_effect;
176 present_parameters->hDeviceWindow = swapchain_desc->device_window;
177 present_parameters->Windowed = swapchain_desc->windowed;
178 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
179 present_parameters->AutoDepthStencilFormat
180 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
181 present_parameters->Flags = swapchain_desc->flags;
182 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
183 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
186 static void wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
187 const D3DPRESENT_PARAMETERS *present_parameters)
189 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
190 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
191 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
192 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
193 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
194 swapchain_desc->multisample_quality = 0; /* d3d9 only */
195 swapchain_desc->swap_effect = present_parameters->SwapEffect;
196 swapchain_desc->device_window = present_parameters->hDeviceWindow;
197 swapchain_desc->windowed = present_parameters->Windowed;
198 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
199 swapchain_desc->auto_depth_stencil_format
200 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
201 swapchain_desc->flags = present_parameters->Flags;
202 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
203 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
204 swapchain_desc->auto_restore_display_mode = TRUE;
207 /* Handle table functions */
208 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
210 struct d3d8_handle_entry *entry;
212 if (t->free_entries)
214 DWORD index = t->free_entries - t->entries;
215 /* Use a free handle */
216 entry = t->free_entries;
217 if (entry->type != D3D8_HANDLE_FREE)
219 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
220 return D3D8_INVALID_HANDLE;
222 t->free_entries = entry->object;
223 entry->object = object;
224 entry->type = type;
226 return index;
229 if (!(t->entry_count < t->table_size))
231 /* Grow the table */
232 UINT new_size = t->table_size + (t->table_size >> 1);
233 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
234 0, t->entries, new_size * sizeof(*t->entries));
235 if (!new_entries)
237 ERR("Failed to grow the handle table.\n");
238 return D3D8_INVALID_HANDLE;
240 t->entries = new_entries;
241 t->table_size = new_size;
244 entry = &t->entries[t->entry_count];
245 entry->object = object;
246 entry->type = type;
248 return t->entry_count++;
251 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
253 struct d3d8_handle_entry *entry;
254 void *object;
256 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
258 WARN("Invalid handle %u passed.\n", handle);
259 return NULL;
262 entry = &t->entries[handle];
263 if (entry->type != type)
265 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
266 return NULL;
269 object = entry->object;
270 entry->object = t->free_entries;
271 entry->type = D3D8_HANDLE_FREE;
272 t->free_entries = entry;
274 return object;
277 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
279 struct d3d8_handle_entry *entry;
281 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
283 WARN("Invalid handle %u passed.\n", handle);
284 return NULL;
287 entry = &t->entries[handle];
288 if (entry->type != type)
290 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
291 return NULL;
294 return entry->object;
297 static inline struct d3d8_device *impl_from_IDirect3DDevice8(IDirect3DDevice8 *iface)
299 return CONTAINING_RECORD(iface, struct d3d8_device, IDirect3DDevice8_iface);
302 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
304 TRACE("iface %p, riid %s, out %p.\n",
305 iface, debugstr_guid(riid), out);
307 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
308 || IsEqualGUID(riid, &IID_IUnknown))
310 IDirect3DDevice8_AddRef(iface);
311 *out = iface;
312 return S_OK;
315 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
317 *out = NULL;
318 return E_NOINTERFACE;
321 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
323 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
324 ULONG ref = InterlockedIncrement(&device->ref);
326 TRACE("%p increasing refcount to %u.\n", iface, ref);
328 return ref;
331 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
333 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
334 ULONG ref;
336 if (device->inDestruction)
337 return 0;
339 ref = InterlockedDecrement(&device->ref);
341 TRACE("%p decreasing refcount to %u.\n", iface, ref);
343 if (!ref)
345 IDirect3D8 *parent = device->d3d_parent;
346 unsigned i;
348 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
350 wined3d_mutex_lock();
352 device->inDestruction = TRUE;
354 for (i = 0; i < device->numConvertedDecls; ++i)
356 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
358 HeapFree(GetProcessHeap(), 0, device->decls);
360 if (device->vertex_buffer)
361 wined3d_buffer_decref(device->vertex_buffer);
362 if (device->index_buffer)
363 wined3d_buffer_decref(device->index_buffer);
365 wined3d_device_uninit_3d(device->wined3d_device);
366 wined3d_device_release_focus_window(device->wined3d_device);
367 wined3d_device_decref(device->wined3d_device);
368 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
369 HeapFree(GetProcessHeap(), 0, device);
371 wined3d_mutex_unlock();
373 IDirect3D8_Release(parent);
375 return ref;
378 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
380 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
382 TRACE("iface %p.\n", iface);
384 if (device->lost)
386 TRACE("Device is lost.\n");
387 return D3DERR_DEVICENOTRESET;
390 return D3D_OK;
393 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
395 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
396 HRESULT hr;
398 TRACE("iface %p.\n", iface);
400 wined3d_mutex_lock();
401 hr = wined3d_device_get_available_texture_mem(device->wined3d_device);
402 wined3d_mutex_unlock();
404 return hr;
407 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
409 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
411 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
413 if (byte_count)
414 FIXME("Byte count ignored.\n");
416 wined3d_mutex_lock();
417 wined3d_device_evict_managed_resources(device->wined3d_device);
418 wined3d_mutex_unlock();
420 return D3D_OK;
423 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
425 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
427 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
429 if (!d3d8)
430 return D3DERR_INVALIDCALL;
432 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
435 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
437 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
438 WINED3DCAPS *wined3d_caps;
439 HRESULT hr;
441 TRACE("iface %p, caps %p.\n", iface, caps);
443 if (!caps)
444 return D3DERR_INVALIDCALL;
446 if (!(wined3d_caps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wined3d_caps))))
447 return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
449 wined3d_mutex_lock();
450 hr = wined3d_device_get_device_caps(device->wined3d_device, wined3d_caps);
451 wined3d_mutex_unlock();
453 fixup_caps(wined3d_caps);
454 WINECAPSTOD3D8CAPS(caps, wined3d_caps)
455 HeapFree(GetProcessHeap(), 0, wined3d_caps);
457 return hr;
460 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
462 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
463 struct wined3d_display_mode wined3d_mode;
464 HRESULT hr;
466 TRACE("iface %p, mode %p.\n", iface, mode);
468 wined3d_mutex_lock();
469 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
470 wined3d_mutex_unlock();
472 if (SUCCEEDED(hr))
474 mode->Width = wined3d_mode.width;
475 mode->Height = wined3d_mode.height;
476 mode->RefreshRate = wined3d_mode.refresh_rate;
477 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
480 return hr;
483 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
484 D3DDEVICE_CREATION_PARAMETERS *parameters)
486 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
488 TRACE("iface %p, parameters %p.\n", iface, parameters);
490 wined3d_mutex_lock();
491 wined3d_device_get_creation_parameters(device->wined3d_device,
492 (struct wined3d_device_creation_parameters *)parameters);
493 wined3d_mutex_unlock();
495 return D3D_OK;
498 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
499 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
501 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
502 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
503 HRESULT hr;
505 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
506 iface, hotspot_x, hotspot_y, bitmap);
508 if (!bitmap)
510 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
511 return D3DERR_INVALIDCALL;
514 wined3d_mutex_lock();
515 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
516 hotspot_x, hotspot_y, bitmap_impl->wined3d_surface);
517 wined3d_mutex_unlock();
519 return hr;
522 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
524 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
526 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
528 wined3d_mutex_lock();
529 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
530 wined3d_mutex_unlock();
533 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
535 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
536 BOOL ret;
538 TRACE("iface %p, show %#x.\n", iface, show);
540 wined3d_mutex_lock();
541 ret = wined3d_device_show_cursor(device->wined3d_device, show);
542 wined3d_mutex_unlock();
544 return ret;
547 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
548 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
551 struct wined3d_swapchain_desc desc;
552 struct d3d8_swapchain *object;
554 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
555 iface, present_parameters, swapchain);
557 wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters);
558 if (SUCCEEDED(d3d8_swapchain_create(device, &desc, &object)))
559 *swapchain = &object->IDirect3DSwapChain8_iface;
560 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
562 return D3D_OK;
565 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
567 struct wined3d_resource_desc desc;
569 wined3d_resource_get_desc(resource, &desc);
570 if (desc.pool == WINED3D_POOL_DEFAULT)
572 struct d3d8_surface *surface;
574 if (desc.resource_type != WINED3D_RTYPE_SURFACE)
576 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
577 return D3DERR_DEVICELOST;
580 surface = wined3d_resource_get_parent(resource);
581 if (surface->refcount)
583 WARN("Surface %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface, resource);
584 return D3DERR_DEVICELOST;
587 WARN("Surface %p (resource %p) is an implicit resource with ref 0.\n", surface, resource);
590 return D3D_OK;
593 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
594 D3DPRESENT_PARAMETERS *present_parameters)
596 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
597 struct wined3d_swapchain_desc swapchain_desc;
598 HRESULT hr;
600 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
602 wined3d_mutex_lock();
604 if (device->vertex_buffer)
606 wined3d_buffer_decref(device->vertex_buffer);
607 device->vertex_buffer = NULL;
608 device->vertex_buffer_size = 0;
610 if (device->index_buffer)
612 wined3d_buffer_decref(device->index_buffer);
613 device->index_buffer = NULL;
614 device->index_buffer_size = 0;
617 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters);
618 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
619 NULL, reset_enum_callback, TRUE)))
621 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
622 device->lost = FALSE;
624 else
626 device->lost = TRUE;
628 wined3d_mutex_unlock();
630 return hr;
633 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
634 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
636 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
637 HRESULT hr;
639 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
640 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
642 wined3d_mutex_lock();
643 hr = wined3d_device_present(device->wined3d_device, src_rect, dst_rect,
644 dst_window_override, dirty_region, 0);
645 wined3d_mutex_unlock();
647 return hr;
650 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
651 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
653 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
654 struct wined3d_surface *wined3d_surface = NULL;
655 struct d3d8_surface *surface_impl;
656 HRESULT hr;
658 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
659 iface, backbuffer_idx, backbuffer_type, backbuffer);
661 wined3d_mutex_lock();
662 hr = wined3d_device_get_back_buffer(device->wined3d_device, 0, backbuffer_idx,
663 (enum wined3d_backbuffer_type)backbuffer_type, &wined3d_surface);
664 if (SUCCEEDED(hr) && wined3d_surface && backbuffer)
666 surface_impl = wined3d_surface_get_parent(wined3d_surface);
667 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
668 IDirect3DSurface8_AddRef(*backbuffer);
670 wined3d_mutex_unlock();
672 return hr;
675 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
677 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
678 HRESULT hr;
680 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
682 wined3d_mutex_lock();
683 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
684 wined3d_mutex_unlock();
686 return hr;
689 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
691 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
693 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
695 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
696 wined3d_mutex_lock();
697 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
698 wined3d_mutex_unlock();
701 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
703 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
705 TRACE("iface %p, ramp %p.\n", iface, ramp);
707 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
708 wined3d_mutex_lock();
709 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
710 wined3d_mutex_unlock();
713 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
714 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
715 D3DPOOL pool, IDirect3DTexture8 **texture)
717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
718 struct d3d8_texture *object;
719 HRESULT hr;
721 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
722 iface, width, height, levels, usage, format, pool, texture);
724 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
725 if (!object)
726 return D3DERR_OUTOFVIDEOMEMORY;
728 hr = texture_init(object, device, width, height, levels, usage, format, pool);
729 if (FAILED(hr))
731 WARN("Failed to initialize texture, hr %#x.\n", hr);
732 HeapFree(GetProcessHeap(), 0, object);
733 return hr;
736 TRACE("Created texture %p.\n", object);
737 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
739 return D3D_OK;
742 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
743 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
744 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
746 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
747 struct d3d8_texture *object;
748 HRESULT hr;
750 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
751 iface, width, height, depth, levels, usage, format, pool, texture);
753 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
754 if (!object)
755 return D3DERR_OUTOFVIDEOMEMORY;
757 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
758 if (FAILED(hr))
760 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
761 HeapFree(GetProcessHeap(), 0, object);
762 return hr;
765 TRACE("Created volume texture %p.\n", object);
766 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
768 return D3D_OK;
771 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
772 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
774 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
775 struct d3d8_texture *object;
776 HRESULT hr;
778 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
779 iface, edge_length, levels, usage, format, pool, texture);
781 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
782 if (!object)
783 return D3DERR_OUTOFVIDEOMEMORY;
785 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
786 if (FAILED(hr))
788 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
789 HeapFree(GetProcessHeap(), 0, object);
790 return hr;
793 TRACE("Created cube texture %p.\n", object);
794 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
796 return hr;
799 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
800 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
802 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
803 struct d3d8_vertexbuffer *object;
804 HRESULT hr;
806 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
807 iface, size, usage, fvf, pool, buffer);
809 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
810 if (!object)
811 return D3DERR_OUTOFVIDEOMEMORY;
813 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
814 if (FAILED(hr))
816 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
817 HeapFree(GetProcessHeap(), 0, object);
818 return hr;
821 TRACE("Created vertex buffer %p.\n", object);
822 *buffer = &object->IDirect3DVertexBuffer8_iface;
824 return D3D_OK;
827 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
828 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
830 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
831 struct d3d8_indexbuffer *object;
832 HRESULT hr;
834 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
835 iface, size, usage, format, pool, buffer);
837 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
838 if (!object)
839 return D3DERR_OUTOFVIDEOMEMORY;
841 hr = indexbuffer_init(object, device, size, usage, format, pool);
842 if (FAILED(hr))
844 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
845 HeapFree(GetProcessHeap(), 0, object);
846 return hr;
849 TRACE("Created index buffer %p.\n", object);
850 *buffer = &object->IDirect3DIndexBuffer8_iface;
852 return D3D_OK;
855 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
856 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
857 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
859 struct d3d8_surface *object;
860 HRESULT hr;
862 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p,\n"
863 "\tusage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
864 device, width, height, format, flags, surface,
865 usage, pool, multisample_type, multisample_quality);
867 if (!(object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object))))
869 FIXME("Failed to allocate surface memory.\n");
870 return D3DERR_OUTOFVIDEOMEMORY;
873 if (FAILED(hr = surface_init(object, device, width, height, format,
874 flags, usage, pool, multisample_type, multisample_quality)))
876 WARN("Failed to initialize surface, hr %#x.\n", hr);
877 HeapFree(GetProcessHeap(), 0, object);
878 return hr;
881 TRACE("Created surface %p.\n", object);
882 *surface = &object->IDirect3DSurface8_iface;
884 return D3D_OK;
887 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
888 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
889 IDirect3DSurface8 **surface)
891 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
892 DWORD flags = 0;
894 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
895 iface, width, height, format, multisample_type, lockable, surface);
897 if (lockable)
898 flags |= WINED3D_SURFACE_MAPPABLE;
900 return d3d8_device_create_surface(device, width, height, format, flags, surface,
901 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
904 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
905 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
906 IDirect3DSurface8 **surface)
908 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
910 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
911 iface, width, height, format, multisample_type, surface);
913 /* TODO: Verify that Discard is false */
914 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
915 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
918 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
919 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
920 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
922 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
924 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
925 iface, width, height, format, surface);
927 return d3d8_device_create_surface(device, width, height, format, WINED3D_SURFACE_MAPPABLE,
928 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
931 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
932 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
933 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
935 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
936 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
937 enum wined3d_format_id src_format, dst_format;
938 struct wined3d_resource_desc wined3d_desc;
939 struct wined3d_resource *wined3d_resource;
940 UINT src_w, src_h;
941 HRESULT hr;
943 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
944 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
946 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
947 * destination texture is in WINED3D_POOL_DEFAULT. */
949 wined3d_mutex_lock();
950 wined3d_resource = wined3d_surface_get_resource(src->wined3d_surface);
951 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
952 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
954 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
955 wined3d_mutex_unlock();
956 return D3DERR_INVALIDCALL;
958 src_format = wined3d_desc.format;
959 src_w = wined3d_desc.width;
960 src_h = wined3d_desc.height;
962 wined3d_resource = wined3d_surface_get_resource(dst->wined3d_surface);
963 wined3d_resource_get_desc(wined3d_resource, &wined3d_desc);
964 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
966 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
967 wined3d_mutex_unlock();
968 return D3DERR_INVALIDCALL;
970 dst_format = wined3d_desc.format;
972 /* Check that the source and destination formats match */
973 if (src_format != dst_format && WINED3DFMT_UNKNOWN != dst_format)
975 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
976 src_surface, dst_surface);
977 wined3d_mutex_unlock();
978 return D3DERR_INVALIDCALL;
980 else if (WINED3DFMT_UNKNOWN == dst_format)
982 TRACE("Converting destination surface from WINED3DFMT_UNKNOWN to the source format.\n");
983 if (FAILED(hr = wined3d_surface_update_desc(dst->wined3d_surface, wined3d_desc.width, wined3d_desc.height,
984 src_format, wined3d_desc.multisample_type, wined3d_desc.multisample_quality)))
986 WARN("Failed to update surface desc, hr %#x.\n", hr);
987 wined3d_mutex_unlock();
988 return hr;
992 /* Quick if complete copy ... */
993 if (!rect_count && !src_rects && !dst_points)
995 RECT rect = {0, 0, src_w, src_h};
996 wined3d_surface_blt(dst->wined3d_surface, &rect,
997 src->wined3d_surface, &rect, 0, NULL, WINED3D_TEXF_POINT);
999 else
1001 unsigned int i;
1002 /* Copy rect by rect */
1003 if (src_rects && dst_points)
1005 for (i = 0; i < rect_count; ++i)
1007 UINT w = src_rects[i].right - src_rects[i].left;
1008 UINT h = src_rects[i].bottom - src_rects[i].top;
1009 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1010 dst_points[i].x + w, dst_points[i].y + h};
1012 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1013 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1016 else
1018 for (i = 0; i < rect_count; ++i)
1020 UINT w = src_rects[i].right - src_rects[i].left;
1021 UINT h = src_rects[i].bottom - src_rects[i].top;
1022 RECT dst_rect = {0, 0, w, h};
1024 wined3d_surface_blt(dst->wined3d_surface, &dst_rect,
1025 src->wined3d_surface, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1029 wined3d_mutex_unlock();
1031 return WINED3D_OK;
1034 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1035 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1037 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1038 struct d3d8_texture *src_impl, *dst_impl;
1039 HRESULT hr;
1041 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1043 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1044 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1046 wined3d_mutex_lock();
1047 hr = wined3d_device_update_texture(device->wined3d_device,
1048 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1049 wined3d_mutex_unlock();
1051 return hr;
1054 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1056 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1057 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1058 HRESULT hr;
1060 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1062 if (!dst_surface)
1064 WARN("Invalid destination surface passed.\n");
1065 return D3DERR_INVALIDCALL;
1068 wined3d_mutex_lock();
1069 hr = wined3d_device_get_front_buffer_data(device->wined3d_device, 0, dst_impl->wined3d_surface);
1070 wined3d_mutex_unlock();
1072 return hr;
1075 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1076 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1078 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1079 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1080 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1081 struct wined3d_surface *original_ds = NULL;
1082 HRESULT hr = D3D_OK;
1084 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1086 wined3d_mutex_lock();
1088 if (ds_impl)
1090 struct wined3d_resource_desc ds_desc, rt_desc;
1091 struct wined3d_resource *wined3d_resource;
1092 struct wined3d_surface *original_rt = NULL;
1094 /* If no render target is passed in check the size against the current RT */
1095 if (!render_target)
1097 if (!(original_rt = wined3d_device_get_render_target(device->wined3d_device, 0)))
1099 wined3d_mutex_unlock();
1100 return D3DERR_NOTFOUND;
1102 wined3d_resource = wined3d_surface_get_resource(original_rt);
1104 else
1105 wined3d_resource = wined3d_surface_get_resource(rt_impl->wined3d_surface);
1106 wined3d_resource_get_desc(wined3d_resource, &rt_desc);
1108 wined3d_resource = wined3d_surface_get_resource(ds_impl->wined3d_surface);
1109 wined3d_resource_get_desc(wined3d_resource, &ds_desc);
1111 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1113 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1114 wined3d_mutex_unlock();
1115 return D3DERR_INVALIDCALL;
1119 original_ds = wined3d_device_get_depth_stencil(device->wined3d_device);
1120 wined3d_device_set_depth_stencil(device->wined3d_device, ds_impl ? ds_impl->wined3d_surface : NULL);
1121 if (render_target)
1123 hr = wined3d_device_set_render_target(device->wined3d_device, 0, rt_impl->wined3d_surface, TRUE);
1124 if (FAILED(hr))
1125 wined3d_device_set_depth_stencil(device->wined3d_device, original_ds);
1128 wined3d_mutex_unlock();
1130 return hr;
1133 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1135 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1136 struct wined3d_surface *wined3d_surface;
1137 struct d3d8_surface *surface_impl;
1138 HRESULT hr;
1140 TRACE("iface %p, render_target %p.\n", iface, render_target);
1142 if (!render_target)
1143 return D3DERR_INVALIDCALL;
1145 wined3d_mutex_lock();
1146 if ((wined3d_surface = wined3d_device_get_render_target(device->wined3d_device, 0)))
1148 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1149 *render_target = &surface_impl->IDirect3DSurface8_iface;
1150 IDirect3DSurface8_AddRef(*render_target);
1151 hr = D3D_OK;
1153 else
1155 ERR("Failed to get wined3d render target.\n");
1156 *render_target = NULL;
1157 hr = D3DERR_NOTFOUND;
1159 wined3d_mutex_unlock();
1161 return hr;
1164 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1166 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1167 struct wined3d_surface *wined3d_surface;
1168 struct d3d8_surface *surface_impl;
1169 HRESULT hr = D3D_OK;
1171 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1173 if (!depth_stencil)
1174 return D3DERR_INVALIDCALL;
1176 wined3d_mutex_lock();
1177 if ((wined3d_surface = wined3d_device_get_depth_stencil(device->wined3d_device)))
1179 surface_impl = wined3d_surface_get_parent(wined3d_surface);
1180 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1181 IDirect3DSurface8_AddRef(*depth_stencil);
1183 else
1185 hr = WINED3DERR_NOTFOUND;
1186 *depth_stencil = NULL;
1188 wined3d_mutex_unlock();
1190 return hr;
1193 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1195 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1196 HRESULT hr;
1198 TRACE("iface %p.\n", iface);
1200 wined3d_mutex_lock();
1201 hr = wined3d_device_begin_scene(device->wined3d_device);
1202 wined3d_mutex_unlock();
1204 return hr;
1207 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1209 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1210 HRESULT hr;
1212 TRACE("iface %p.\n", iface);
1214 wined3d_mutex_lock();
1215 hr = wined3d_device_end_scene(device->wined3d_device);
1216 wined3d_mutex_unlock();
1218 return hr;
1221 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1222 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1224 const struct wined3d_color c =
1226 ((color >> 16) & 0xff) / 255.0f,
1227 ((color >> 8) & 0xff) / 255.0f,
1228 (color & 0xff) / 255.0f,
1229 ((color >> 24) & 0xff) / 255.0f,
1231 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1232 HRESULT hr;
1234 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1235 iface, rect_count, rects, flags, color, z, stencil);
1237 wined3d_mutex_lock();
1238 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1239 wined3d_mutex_unlock();
1241 return hr;
1244 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1245 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1247 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1249 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1251 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1252 wined3d_mutex_lock();
1253 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1254 wined3d_mutex_unlock();
1256 return D3D_OK;
1259 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1260 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1262 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1264 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1266 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1267 wined3d_mutex_lock();
1268 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1269 wined3d_mutex_unlock();
1271 return D3D_OK;
1274 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1275 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1277 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1279 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1281 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1282 wined3d_mutex_lock();
1283 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1284 wined3d_mutex_unlock();
1286 return D3D_OK;
1289 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1291 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1293 TRACE("iface %p, viewport %p.\n", iface, viewport);
1295 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1296 wined3d_mutex_lock();
1297 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1298 wined3d_mutex_unlock();
1300 return D3D_OK;
1303 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1305 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1307 TRACE("iface %p, viewport %p.\n", iface, viewport);
1309 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1310 wined3d_mutex_lock();
1311 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1312 wined3d_mutex_unlock();
1314 return D3D_OK;
1317 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1319 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1321 TRACE("iface %p, material %p.\n", iface, material);
1323 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1324 wined3d_mutex_lock();
1325 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1326 wined3d_mutex_unlock();
1328 return D3D_OK;
1331 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1333 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1335 TRACE("iface %p, material %p.\n", iface, material);
1337 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1338 wined3d_mutex_lock();
1339 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1340 wined3d_mutex_unlock();
1342 return D3D_OK;
1345 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1347 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1348 HRESULT hr;
1350 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1352 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1353 wined3d_mutex_lock();
1354 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1355 wined3d_mutex_unlock();
1357 return hr;
1360 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1362 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1363 HRESULT hr;
1365 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1367 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1368 wined3d_mutex_lock();
1369 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1370 wined3d_mutex_unlock();
1372 return hr;
1375 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1377 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1378 HRESULT hr;
1380 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1382 wined3d_mutex_lock();
1383 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1384 wined3d_mutex_unlock();
1386 return hr;
1389 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1391 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1392 HRESULT hr;
1394 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1396 wined3d_mutex_lock();
1397 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1398 wined3d_mutex_unlock();
1400 return hr;
1403 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1405 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1406 HRESULT hr;
1408 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1410 wined3d_mutex_lock();
1411 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1412 wined3d_mutex_unlock();
1414 return hr;
1417 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1419 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1420 HRESULT hr;
1422 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1424 wined3d_mutex_lock();
1425 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1426 wined3d_mutex_unlock();
1428 return hr;
1431 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1432 D3DRENDERSTATETYPE state, DWORD value)
1434 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1436 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1438 wined3d_mutex_lock();
1439 switch (state)
1441 case D3DRS_ZBIAS:
1442 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1443 break;
1445 default:
1446 wined3d_device_set_render_state(device->wined3d_device, state, value);
1448 wined3d_mutex_unlock();
1450 return D3D_OK;
1453 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1454 D3DRENDERSTATETYPE state, DWORD *value)
1456 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1458 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1460 wined3d_mutex_lock();
1461 switch (state)
1463 case D3DRS_ZBIAS:
1464 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1465 break;
1467 default:
1468 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1470 wined3d_mutex_unlock();
1472 return D3D_OK;
1475 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1477 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1478 HRESULT hr;
1480 TRACE("iface %p.\n", iface);
1482 wined3d_mutex_lock();
1483 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1484 wined3d_mutex_unlock();
1486 return hr;
1489 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1491 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1492 struct wined3d_stateblock *stateblock;
1493 HRESULT hr;
1495 TRACE("iface %p, token %p.\n", iface, token);
1497 /* Tell wineD3D to endstateblock before anything else (in case we run out
1498 * of memory later and cause locking problems)
1500 wined3d_mutex_lock();
1501 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1502 if (FAILED(hr))
1504 WARN("IWineD3DDevice_EndStateBlock returned an error\n");
1505 wined3d_mutex_unlock();
1506 return hr;
1509 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1510 wined3d_mutex_unlock();
1512 if (*token == D3D8_INVALID_HANDLE)
1514 ERR("Failed to create a handle\n");
1515 wined3d_mutex_lock();
1516 wined3d_stateblock_decref(stateblock);
1517 wined3d_mutex_unlock();
1518 return E_FAIL;
1520 ++*token;
1522 TRACE("Returning %#x (%p).\n", *token, stateblock);
1524 return hr;
1527 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1529 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1530 struct wined3d_stateblock *stateblock;
1532 TRACE("iface %p, token %#x.\n", iface, token);
1534 if (!token)
1535 return D3D_OK;
1537 wined3d_mutex_lock();
1538 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1539 if (!stateblock)
1541 WARN("Invalid handle (%#x) passed.\n", token);
1542 wined3d_mutex_unlock();
1543 return D3DERR_INVALIDCALL;
1545 wined3d_stateblock_apply(stateblock);
1546 wined3d_mutex_unlock();
1548 return D3D_OK;
1551 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1553 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1554 struct wined3d_stateblock *stateblock;
1556 TRACE("iface %p, token %#x.\n", iface, token);
1558 wined3d_mutex_lock();
1559 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1560 if (!stateblock)
1562 WARN("Invalid handle (%#x) passed.\n", token);
1563 wined3d_mutex_unlock();
1564 return D3DERR_INVALIDCALL;
1566 wined3d_stateblock_capture(stateblock);
1567 wined3d_mutex_unlock();
1569 return D3D_OK;
1572 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1574 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1575 struct wined3d_stateblock *stateblock;
1577 TRACE("iface %p, token %#x.\n", iface, token);
1579 wined3d_mutex_lock();
1580 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1582 if (!stateblock)
1584 WARN("Invalid handle (%#x) passed.\n", token);
1585 wined3d_mutex_unlock();
1586 return D3DERR_INVALIDCALL;
1589 if (wined3d_stateblock_decref(stateblock))
1591 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1593 wined3d_mutex_unlock();
1595 return D3D_OK;
1598 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1599 D3DSTATEBLOCKTYPE type, DWORD *handle)
1601 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1602 struct wined3d_stateblock *stateblock;
1603 HRESULT hr;
1605 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1607 if (type != D3DSBT_ALL
1608 && type != D3DSBT_PIXELSTATE
1609 && type != D3DSBT_VERTEXSTATE)
1611 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1612 return D3DERR_INVALIDCALL;
1615 wined3d_mutex_lock();
1616 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1617 if (FAILED(hr))
1619 wined3d_mutex_unlock();
1620 ERR("IWineD3DDevice_CreateStateBlock failed, hr %#x\n", hr);
1621 return hr;
1624 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1625 wined3d_mutex_unlock();
1627 if (*handle == D3D8_INVALID_HANDLE)
1629 ERR("Failed to allocate a handle.\n");
1630 wined3d_mutex_lock();
1631 wined3d_stateblock_decref(stateblock);
1632 wined3d_mutex_unlock();
1633 return E_FAIL;
1635 ++*handle;
1637 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1639 return hr;
1642 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1644 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1645 HRESULT hr;
1647 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1648 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1650 wined3d_mutex_lock();
1651 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1652 wined3d_mutex_unlock();
1654 return hr;
1657 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1660 HRESULT hr;
1662 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1664 wined3d_mutex_lock();
1665 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1666 wined3d_mutex_unlock();
1668 return hr;
1671 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1673 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1674 struct wined3d_texture *wined3d_texture;
1675 struct d3d8_texture *texture_impl;
1677 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1679 if (!texture)
1680 return D3DERR_INVALIDCALL;
1682 wined3d_mutex_lock();
1683 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1685 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1686 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1687 IDirect3DBaseTexture8_AddRef(*texture);
1689 else
1691 *texture = NULL;
1693 wined3d_mutex_unlock();
1695 return D3D_OK;
1698 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1700 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1701 struct d3d8_texture *texture_impl;
1702 HRESULT hr;
1704 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1706 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1708 wined3d_mutex_lock();
1709 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1710 texture_impl ? texture_impl->wined3d_texture : NULL);
1711 wined3d_mutex_unlock();
1713 return hr;
1716 static const struct tss_lookup
1718 BOOL sampler_state;
1719 enum wined3d_texture_stage_state state;
1721 tss_lookup[] =
1723 {FALSE, WINED3D_TSS_INVALID}, /* 0, unused */
1724 {FALSE, WINED3D_TSS_COLOR_OP}, /* 1, D3DTSS_COLOROP */
1725 {FALSE, WINED3D_TSS_COLOR_ARG1}, /* 2, D3DTSS_COLORARG1 */
1726 {FALSE, WINED3D_TSS_COLOR_ARG2}, /* 3, D3DTSS_COLORARG2 */
1727 {FALSE, WINED3D_TSS_ALPHA_OP}, /* 4, D3DTSS_ALPHAOP */
1728 {FALSE, WINED3D_TSS_ALPHA_ARG1}, /* 5, D3DTSS_ALPHAARG1 */
1729 {FALSE, WINED3D_TSS_ALPHA_ARG2}, /* 6, D3DTSS_ALPHAARG2 */
1730 {FALSE, WINED3D_TSS_BUMPENV_MAT00}, /* 7, D3DTSS_BUMPENVMAT00 */
1731 {FALSE, WINED3D_TSS_BUMPENV_MAT01}, /* 8, D3DTSS_BUMPENVMAT01 */
1732 {FALSE, WINED3D_TSS_BUMPENV_MAT10}, /* 9, D3DTSS_BUMPENVMAT10 */
1733 {FALSE, WINED3D_TSS_BUMPENV_MAT11}, /* 10, D3DTSS_BUMPENVMAT11 */
1734 {FALSE, WINED3D_TSS_TEXCOORD_INDEX}, /* 11, D3DTSS_TEXCOORDINDEX */
1735 {FALSE, WINED3D_TSS_INVALID}, /* 12, unused */
1736 {TRUE, WINED3D_SAMP_ADDRESS_U}, /* 13, D3DTSS_ADDRESSU */
1737 {TRUE, WINED3D_SAMP_ADDRESS_V}, /* 14, D3DTSS_ADDRESSV */
1738 {TRUE, WINED3D_SAMP_BORDER_COLOR}, /* 15, D3DTSS_BORDERCOLOR */
1739 {TRUE, WINED3D_SAMP_MAG_FILTER}, /* 16, D3DTSS_MAGFILTER */
1740 {TRUE, WINED3D_SAMP_MIN_FILTER}, /* 17, D3DTSS_MINFILTER */
1741 {TRUE, WINED3D_SAMP_MIP_FILTER}, /* 18, D3DTSS_MIPFILTER */
1742 {TRUE, WINED3D_SAMP_MIPMAP_LOD_BIAS}, /* 19, D3DTSS_MIPMAPLODBIAS */
1743 {TRUE, WINED3D_SAMP_MAX_MIP_LEVEL}, /* 20, D3DTSS_MAXMIPLEVEL */
1744 {TRUE, WINED3D_SAMP_MAX_ANISOTROPY}, /* 21, D3DTSS_MAXANISOTROPY */
1745 {FALSE, WINED3D_TSS_BUMPENV_LSCALE}, /* 22, D3DTSS_BUMPENVLSCALE */
1746 {FALSE, WINED3D_TSS_BUMPENV_LOFFSET}, /* 23, D3DTSS_BUMPENVLOFFSET */
1747 {FALSE, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1748 {TRUE, WINED3D_SAMP_ADDRESS_W}, /* 25, D3DTSS_ADDRESSW */
1749 {FALSE, WINED3D_TSS_COLOR_ARG0}, /* 26, D3DTSS_COLORARG0 */
1750 {FALSE, WINED3D_TSS_ALPHA_ARG0}, /* 27, D3DTSS_ALPHAARG0 */
1751 {FALSE, WINED3D_TSS_RESULT_ARG}, /* 28, D3DTSS_RESULTARG */
1754 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1755 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1757 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1758 const struct tss_lookup *l;
1760 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1762 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1764 WARN("Invalid Type %#x passed.\n", Type);
1765 return D3D_OK;
1768 l = &tss_lookup[Type];
1770 wined3d_mutex_lock();
1771 if (l->sampler_state)
1772 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->state);
1773 else
1774 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->state);
1775 wined3d_mutex_unlock();
1777 return D3D_OK;
1780 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1781 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1783 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1784 const struct tss_lookup *l;
1786 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1788 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1790 WARN("Invalid type %#x passed.\n", type);
1791 return D3D_OK;
1794 l = &tss_lookup[type];
1796 wined3d_mutex_lock();
1797 if (l->sampler_state)
1798 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->state, value);
1799 else
1800 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->state, value);
1801 wined3d_mutex_unlock();
1803 return D3D_OK;
1806 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1808 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1809 HRESULT hr;
1811 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1813 wined3d_mutex_lock();
1814 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
1815 wined3d_mutex_unlock();
1817 return hr;
1820 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
1821 DWORD info_id, void *info, DWORD info_size)
1823 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
1825 return D3D_OK;
1828 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
1829 UINT palette_idx, const PALETTEENTRY *entries)
1831 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
1833 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
1834 * does not have a d3d8/9-style palette API */
1836 return D3D_OK;
1839 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
1840 UINT palette_idx, PALETTEENTRY *entries)
1842 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
1844 return D3DERR_INVALIDCALL;
1847 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
1849 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
1851 return D3D_OK;
1854 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
1856 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
1858 return D3DERR_INVALIDCALL;
1861 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
1862 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
1864 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1865 HRESULT hr;
1867 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1868 iface, primitive_type, start_vertex, primitive_count);
1870 wined3d_mutex_lock();
1871 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1872 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
1873 vertex_count_from_primitive_count(primitive_type, primitive_count));
1874 wined3d_mutex_unlock();
1876 return hr;
1879 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
1880 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
1881 UINT start_idx, UINT primitive_count)
1883 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1884 HRESULT hr;
1886 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
1887 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
1889 wined3d_mutex_lock();
1890 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1891 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
1892 vertex_count_from_primitive_count(primitive_type, primitive_count));
1893 wined3d_mutex_unlock();
1895 return hr;
1898 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
1900 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
1902 d3d8_null_wined3d_object_destroyed,
1905 /* The caller is responsible for wined3d locking */
1906 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
1908 HRESULT hr;
1910 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
1912 UINT size = max(device->vertex_buffer_size * 2, min_size);
1913 struct wined3d_buffer *buffer;
1915 TRACE("Growing vertex buffer to %u bytes\n", size);
1917 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
1918 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
1919 if (FAILED(hr))
1921 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
1922 return hr;
1925 if (device->vertex_buffer)
1926 wined3d_buffer_decref(device->vertex_buffer);
1928 device->vertex_buffer = buffer;
1929 device->vertex_buffer_size = size;
1930 device->vertex_buffer_pos = 0;
1932 return D3D_OK;
1935 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
1936 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
1937 UINT stride)
1939 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1940 HRESULT hr;
1941 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
1942 UINT size = vtx_count * stride;
1943 UINT vb_pos, align;
1944 BYTE *buffer_data;
1946 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1947 iface, primitive_type, primitive_count, data, stride);
1949 if (!primitive_count)
1951 WARN("primitive_count is 0, returning D3D_OK\n");
1952 return D3D_OK;
1955 wined3d_mutex_lock();
1956 hr = d3d8_device_prepare_vertex_buffer(device, size);
1957 if (FAILED(hr))
1958 goto done;
1960 vb_pos = device->vertex_buffer_pos;
1961 align = vb_pos % stride;
1962 if (align) align = stride - align;
1963 if (vb_pos + size + align > device->vertex_buffer_size)
1964 vb_pos = 0;
1965 else
1966 vb_pos += align;
1968 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, size, &buffer_data,
1969 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
1970 if (FAILED(hr))
1971 goto done;
1972 memcpy(buffer_data, data, size);
1973 wined3d_buffer_unmap(device->vertex_buffer);
1974 device->vertex_buffer_pos = vb_pos + size;
1976 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
1977 if (FAILED(hr))
1978 goto done;
1980 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
1981 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
1982 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
1984 done:
1985 wined3d_mutex_unlock();
1986 return hr;
1989 /* The caller is responsible for wined3d locking */
1990 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
1992 HRESULT hr;
1994 if (device->index_buffer_size < min_size || !device->index_buffer)
1996 UINT size = max(device->index_buffer_size * 2, min_size);
1997 struct wined3d_buffer *buffer;
1999 TRACE("Growing index buffer to %u bytes\n", size);
2001 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2002 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2003 if (FAILED(hr))
2005 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2006 return hr;
2009 if (device->index_buffer)
2010 wined3d_buffer_decref(device->index_buffer);
2012 device->index_buffer = buffer;
2013 device->index_buffer_size = size;
2014 device->index_buffer_pos = 0;
2016 return D3D_OK;
2019 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2020 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2021 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2022 const void *vertex_data, UINT vertex_stride)
2024 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2025 HRESULT hr;
2026 BYTE *buffer_data;
2028 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2029 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2030 UINT idx_size = idx_count * idx_fmt_size;
2031 UINT ib_pos;
2033 UINT vtx_size = vertex_count * vertex_stride;
2034 UINT vb_pos, align;
2036 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u,\n"
2037 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2038 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2039 index_data, index_format, vertex_data, vertex_stride);
2041 if (!primitive_count)
2043 WARN("primitive_count is 0, returning D3D_OK\n");
2044 return D3D_OK;
2047 wined3d_mutex_lock();
2049 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2050 if (FAILED(hr))
2051 goto done;
2053 vb_pos = device->vertex_buffer_pos;
2054 align = vb_pos % vertex_stride;
2055 if (align) align = vertex_stride - align;
2056 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2057 vb_pos = 0;
2058 else
2059 vb_pos += align;
2061 hr = wined3d_buffer_map(device->vertex_buffer, vb_pos, vtx_size, &buffer_data,
2062 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2063 if (FAILED(hr))
2064 goto done;
2065 memcpy(buffer_data, vertex_data, vtx_size);
2066 wined3d_buffer_unmap(device->vertex_buffer);
2067 device->vertex_buffer_pos = vb_pos + vtx_size;
2069 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2070 if (FAILED(hr))
2071 goto done;
2073 ib_pos = device->index_buffer_pos;
2074 align = ib_pos % idx_fmt_size;
2075 if (align) align = idx_fmt_size - align;
2076 if (ib_pos + idx_size + align > device->index_buffer_size)
2077 ib_pos = 0;
2078 else
2079 ib_pos += align;
2081 hr = wined3d_buffer_map(device->index_buffer, ib_pos, idx_size, &buffer_data,
2082 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD);
2083 if (FAILED(hr))
2084 goto done;
2085 memcpy(buffer_data, index_data, idx_size);
2086 wined3d_buffer_unmap(device->index_buffer);
2087 device->index_buffer_pos = ib_pos + idx_size;
2089 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2090 if (FAILED(hr))
2091 goto done;
2093 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2094 wined3dformat_from_d3dformat(index_format));
2095 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride);
2097 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2098 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2100 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2101 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN);
2102 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2104 done:
2105 wined3d_mutex_unlock();
2106 return hr;
2109 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2110 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2112 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2113 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2114 HRESULT hr;
2116 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2117 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2119 wined3d_mutex_lock();
2120 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2121 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2122 wined3d_mutex_unlock();
2124 return hr;
2127 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2128 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2130 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2131 struct d3d8_vertex_shader *object;
2132 DWORD shader_handle;
2133 DWORD handle;
2134 HRESULT hr;
2136 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2137 iface, declaration, byte_code, shader, usage);
2139 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2140 if (!object)
2142 *shader = 0;
2143 return E_OUTOFMEMORY;
2146 wined3d_mutex_lock();
2147 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2148 wined3d_mutex_unlock();
2149 if (handle == D3D8_INVALID_HANDLE)
2151 ERR("Failed to allocate vertex shader handle.\n");
2152 HeapFree(GetProcessHeap(), 0, object);
2153 *shader = 0;
2154 return E_OUTOFMEMORY;
2157 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2159 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2160 if (FAILED(hr))
2162 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2163 wined3d_mutex_lock();
2164 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2165 wined3d_mutex_unlock();
2166 HeapFree(GetProcessHeap(), 0, object);
2167 *shader = 0;
2168 return hr;
2171 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2172 *shader = shader_handle;
2174 return D3D_OK;
2177 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2179 struct d3d8_vertex_declaration *d3d8_declaration;
2180 struct FvfToDecl *convertedDecls = device->decls;
2181 int p, low, high; /* deliberately signed */
2182 HRESULT hr;
2184 TRACE("Searching for declaration for fvf %08x... ", fvf);
2186 low = 0;
2187 high = device->numConvertedDecls - 1;
2188 while (low <= high)
2190 p = (low + high) >> 1;
2191 TRACE("%d ", p);
2193 if (convertedDecls[p].fvf == fvf)
2195 TRACE("found %p\n", convertedDecls[p].declaration);
2196 return convertedDecls[p].declaration;
2199 if (convertedDecls[p].fvf < fvf)
2200 low = p + 1;
2201 else
2202 high = p - 1;
2204 TRACE("not found. Creating and inserting at position %d.\n", low);
2206 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2207 return NULL;
2209 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2211 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2212 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2213 return NULL;
2216 if (device->declArraySize == device->numConvertedDecls)
2218 UINT grow = device->declArraySize / 2;
2220 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2221 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2222 if (!convertedDecls)
2224 d3d8_vertex_declaration_destroy(d3d8_declaration);
2225 return NULL;
2227 device->decls = convertedDecls;
2228 device->declArraySize += grow;
2231 memmove(convertedDecls + low + 1, convertedDecls + low,
2232 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2233 convertedDecls[low].declaration = d3d8_declaration;
2234 convertedDecls[low].fvf = fvf;
2235 ++device->numConvertedDecls;
2237 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2239 return d3d8_declaration;
2242 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2244 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2245 struct d3d8_vertex_shader *shader_impl;
2247 TRACE("iface %p, shader %#x.\n", iface, shader);
2249 if (VS_HIGHESTFIXEDFXF >= shader)
2251 TRACE("Setting FVF, %#x\n", shader);
2253 wined3d_mutex_lock();
2254 wined3d_device_set_vertex_declaration(device->wined3d_device,
2255 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2256 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2257 wined3d_mutex_unlock();
2259 return D3D_OK;
2262 TRACE("Setting shader\n");
2264 wined3d_mutex_lock();
2265 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2267 WARN("Invalid handle (%#x) passed.\n", shader);
2268 wined3d_mutex_unlock();
2270 return D3DERR_INVALIDCALL;
2273 wined3d_device_set_vertex_declaration(device->wined3d_device,
2274 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2275 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2276 wined3d_mutex_unlock();
2278 return D3D_OK;
2281 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2283 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2284 struct wined3d_vertex_declaration *wined3d_declaration;
2285 struct d3d8_vertex_declaration *d3d8_declaration;
2287 TRACE("iface %p, shader %p.\n", iface, shader);
2289 wined3d_mutex_lock();
2290 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2292 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2293 *shader = d3d8_declaration->shader_handle;
2295 else
2297 *shader = 0;
2299 wined3d_mutex_unlock();
2301 TRACE("Returning %#x.\n", *shader);
2303 return D3D_OK;
2306 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2308 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2309 struct d3d8_vertex_shader *shader_impl;
2311 TRACE("iface %p, shader %#x.\n", iface, shader);
2313 wined3d_mutex_lock();
2314 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2316 WARN("Invalid handle (%#x) passed.\n", shader);
2317 wined3d_mutex_unlock();
2319 return D3DERR_INVALIDCALL;
2322 if (shader_impl->wined3d_shader
2323 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2324 IDirect3DDevice8_SetVertexShader(iface, 0);
2326 wined3d_mutex_unlock();
2328 d3d8_vertex_shader_destroy(shader_impl);
2330 return D3D_OK;
2333 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2334 DWORD start_register, const void *data, DWORD count)
2336 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2337 HRESULT hr;
2339 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2340 iface, start_register, data, count);
2342 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2344 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2345 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2346 return D3DERR_INVALIDCALL;
2349 wined3d_mutex_lock();
2350 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, data, count);
2351 wined3d_mutex_unlock();
2353 return hr;
2356 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2357 DWORD start_register, void *data, DWORD count)
2359 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2360 HRESULT hr;
2362 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2363 iface, start_register, data, count);
2365 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2367 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2368 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2369 return D3DERR_INVALIDCALL;
2372 wined3d_mutex_lock();
2373 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, data, count);
2374 wined3d_mutex_unlock();
2376 return hr;
2379 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2380 DWORD shader, void *data, DWORD *data_size)
2382 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2383 struct d3d8_vertex_declaration *declaration;
2384 struct d3d8_vertex_shader *shader_impl;
2386 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2387 iface, shader, data, data_size);
2389 wined3d_mutex_lock();
2390 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2391 wined3d_mutex_unlock();
2393 if (!shader_impl)
2395 WARN("Invalid handle (%#x) passed.\n", shader);
2396 return D3DERR_INVALIDCALL;
2398 declaration = shader_impl->vertex_declaration;
2400 if (!data)
2402 *data_size = declaration->elements_size;
2403 return D3D_OK;
2406 /* MSDN claims that if *data_size is smaller than the required size
2407 * we should write the required size and return D3DERR_MOREDATA.
2408 * That's not actually true. */
2409 if (*data_size < declaration->elements_size)
2410 return D3DERR_INVALIDCALL;
2412 memcpy(data, declaration->elements, declaration->elements_size);
2414 return D3D_OK;
2417 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2418 DWORD shader, void *data, DWORD *data_size)
2420 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2421 struct d3d8_vertex_shader *shader_impl = NULL;
2422 HRESULT hr;
2424 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2425 iface, shader, data, data_size);
2427 wined3d_mutex_lock();
2428 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2430 WARN("Invalid handle (%#x) passed.\n", shader);
2431 wined3d_mutex_unlock();
2433 return D3DERR_INVALIDCALL;
2436 if (!shader_impl->wined3d_shader)
2438 wined3d_mutex_unlock();
2439 *data_size = 0;
2440 return D3D_OK;
2443 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2444 wined3d_mutex_unlock();
2446 return hr;
2449 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2450 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2452 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2453 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2455 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2457 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2458 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2459 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2460 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2461 * problem)
2463 wined3d_mutex_lock();
2464 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2465 wined3d_device_set_index_buffer(device->wined3d_device,
2466 ib ? ib->wined3d_buffer : NULL,
2467 ib ? ib->format : WINED3DFMT_UNKNOWN);
2468 wined3d_mutex_unlock();
2470 return D3D_OK;
2473 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2474 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2476 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2477 enum wined3d_format_id wined3d_format;
2478 struct wined3d_buffer *wined3d_buffer;
2479 struct d3d8_indexbuffer *buffer_impl;
2481 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2483 if (!buffer)
2484 return D3DERR_INVALIDCALL;
2486 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2487 wined3d_mutex_lock();
2488 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2489 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format)))
2491 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2492 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2493 IDirect3DIndexBuffer8_AddRef(*buffer);
2495 else
2497 *buffer = NULL;
2499 wined3d_mutex_unlock();
2501 return D3D_OK;
2504 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2505 const DWORD *byte_code, DWORD *shader)
2507 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2508 struct d3d8_pixel_shader *object;
2509 DWORD shader_handle;
2510 DWORD handle;
2511 HRESULT hr;
2513 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2515 if (!shader)
2516 return D3DERR_INVALIDCALL;
2518 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2519 if (!object)
2520 return E_OUTOFMEMORY;
2522 wined3d_mutex_lock();
2523 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2524 wined3d_mutex_unlock();
2525 if (handle == D3D8_INVALID_HANDLE)
2527 ERR("Failed to allocate pixel shader handle.\n");
2528 HeapFree(GetProcessHeap(), 0, object);
2529 return E_OUTOFMEMORY;
2532 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2534 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2535 if (FAILED(hr))
2537 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2538 wined3d_mutex_lock();
2539 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2540 wined3d_mutex_unlock();
2541 HeapFree(GetProcessHeap(), 0, object);
2542 *shader = 0;
2543 return hr;
2546 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2547 *shader = shader_handle;
2549 return D3D_OK;
2552 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2554 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2555 struct d3d8_pixel_shader *shader_impl;
2557 TRACE("iface %p, shader %#x.\n", iface, shader);
2559 wined3d_mutex_lock();
2561 if (!shader)
2563 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2564 wined3d_mutex_unlock();
2565 return D3D_OK;
2568 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2570 WARN("Invalid handle (%#x) passed.\n", shader);
2571 wined3d_mutex_unlock();
2572 return D3DERR_INVALIDCALL;
2575 TRACE("Setting shader %p.\n", shader_impl);
2576 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2577 wined3d_mutex_unlock();
2579 return D3D_OK;
2582 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2584 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2585 struct wined3d_shader *object;
2587 TRACE("iface %p, shader %p.\n", iface, shader);
2589 if (!shader)
2590 return D3DERR_INVALIDCALL;
2592 wined3d_mutex_lock();
2593 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2595 struct d3d8_pixel_shader *d3d8_shader;
2596 d3d8_shader = wined3d_shader_get_parent(object);
2597 *shader = d3d8_shader->handle;
2599 else
2601 *shader = 0;
2603 wined3d_mutex_unlock();
2605 TRACE("Returning %#x.\n", *shader);
2607 return D3D_OK;
2610 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2612 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2613 struct d3d8_pixel_shader *shader_impl;
2615 TRACE("iface %p, shader %#x.\n", iface, shader);
2617 wined3d_mutex_lock();
2619 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2621 WARN("Invalid handle (%#x) passed.\n", shader);
2622 wined3d_mutex_unlock();
2623 return D3DERR_INVALIDCALL;
2626 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2627 IDirect3DDevice8_SetPixelShader(iface, 0);
2629 wined3d_mutex_unlock();
2631 d3d8_pixel_shader_destroy(shader_impl);
2633 return D3D_OK;
2636 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2637 DWORD start_register, const void *data, DWORD count)
2639 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2640 HRESULT hr;
2642 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2643 iface, start_register, data, count);
2645 wined3d_mutex_lock();
2646 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, data, count);
2647 wined3d_mutex_unlock();
2649 return hr;
2652 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2653 DWORD start_register, void *data, DWORD count)
2655 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2656 HRESULT hr;
2658 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2659 iface, start_register, data, count);
2661 wined3d_mutex_lock();
2662 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, data, count);
2663 wined3d_mutex_unlock();
2665 return hr;
2668 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2669 DWORD shader, void *data, DWORD *data_size)
2671 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2672 struct d3d8_pixel_shader *shader_impl = NULL;
2673 HRESULT hr;
2675 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2676 iface, shader, data, data_size);
2678 wined3d_mutex_lock();
2679 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2681 WARN("Invalid handle (%#x) passed.\n", shader);
2682 wined3d_mutex_unlock();
2684 return D3DERR_INVALIDCALL;
2687 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2688 wined3d_mutex_unlock();
2690 return hr;
2693 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2694 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2696 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2697 iface, handle, segment_count, patch_info);
2698 return D3D_OK;
2701 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2702 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2704 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2705 iface, handle, segment_count, patch_info);
2706 return D3D_OK;
2709 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2711 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2712 return D3DERR_INVALIDCALL;
2715 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2716 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2718 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2719 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2720 HRESULT hr;
2722 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2723 iface, stream_idx, buffer, stride);
2725 wined3d_mutex_lock();
2726 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2727 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2728 wined3d_mutex_unlock();
2730 return hr;
2733 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2734 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2736 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2737 struct d3d8_vertexbuffer *buffer_impl;
2738 struct wined3d_buffer *wined3d_buffer = NULL;
2739 HRESULT hr;
2741 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2742 iface, stream_idx, buffer, stride);
2744 if (!buffer)
2745 return D3DERR_INVALIDCALL;
2747 wined3d_mutex_lock();
2748 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2749 if (SUCCEEDED(hr) && wined3d_buffer)
2751 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2752 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2753 IDirect3DVertexBuffer8_AddRef(*buffer);
2754 wined3d_buffer_decref(wined3d_buffer);
2756 else
2758 if (FAILED(hr))
2759 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2760 *buffer = NULL;
2762 wined3d_mutex_unlock();
2764 return hr;
2767 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2769 d3d8_device_QueryInterface,
2770 d3d8_device_AddRef,
2771 d3d8_device_Release,
2772 d3d8_device_TestCooperativeLevel,
2773 d3d8_device_GetAvailableTextureMem,
2774 d3d8_device_ResourceManagerDiscardBytes,
2775 d3d8_device_GetDirect3D,
2776 d3d8_device_GetDeviceCaps,
2777 d3d8_device_GetDisplayMode,
2778 d3d8_device_GetCreationParameters,
2779 d3d8_device_SetCursorProperties,
2780 d3d8_device_SetCursorPosition,
2781 d3d8_device_ShowCursor,
2782 d3d8_device_CreateAdditionalSwapChain,
2783 d3d8_device_Reset,
2784 d3d8_device_Present,
2785 d3d8_device_GetBackBuffer,
2786 d3d8_device_GetRasterStatus,
2787 d3d8_device_SetGammaRamp,
2788 d3d8_device_GetGammaRamp,
2789 d3d8_device_CreateTexture,
2790 d3d8_device_CreateVolumeTexture,
2791 d3d8_device_CreateCubeTexture,
2792 d3d8_device_CreateVertexBuffer,
2793 d3d8_device_CreateIndexBuffer,
2794 d3d8_device_CreateRenderTarget,
2795 d3d8_device_CreateDepthStencilSurface,
2796 d3d8_device_CreateImageSurface,
2797 d3d8_device_CopyRects,
2798 d3d8_device_UpdateTexture,
2799 d3d8_device_GetFrontBuffer,
2800 d3d8_device_SetRenderTarget,
2801 d3d8_device_GetRenderTarget,
2802 d3d8_device_GetDepthStencilSurface,
2803 d3d8_device_BeginScene,
2804 d3d8_device_EndScene,
2805 d3d8_device_Clear,
2806 d3d8_device_SetTransform,
2807 d3d8_device_GetTransform,
2808 d3d8_device_MultiplyTransform,
2809 d3d8_device_SetViewport,
2810 d3d8_device_GetViewport,
2811 d3d8_device_SetMaterial,
2812 d3d8_device_GetMaterial,
2813 d3d8_device_SetLight,
2814 d3d8_device_GetLight,
2815 d3d8_device_LightEnable,
2816 d3d8_device_GetLightEnable,
2817 d3d8_device_SetClipPlane,
2818 d3d8_device_GetClipPlane,
2819 d3d8_device_SetRenderState,
2820 d3d8_device_GetRenderState,
2821 d3d8_device_BeginStateBlock,
2822 d3d8_device_EndStateBlock,
2823 d3d8_device_ApplyStateBlock,
2824 d3d8_device_CaptureStateBlock,
2825 d3d8_device_DeleteStateBlock,
2826 d3d8_device_CreateStateBlock,
2827 d3d8_device_SetClipStatus,
2828 d3d8_device_GetClipStatus,
2829 d3d8_device_GetTexture,
2830 d3d8_device_SetTexture,
2831 d3d8_device_GetTextureStageState,
2832 d3d8_device_SetTextureStageState,
2833 d3d8_device_ValidateDevice,
2834 d3d8_device_GetInfo,
2835 d3d8_device_SetPaletteEntries,
2836 d3d8_device_GetPaletteEntries,
2837 d3d8_device_SetCurrentTexturePalette,
2838 d3d8_device_GetCurrentTexturePalette,
2839 d3d8_device_DrawPrimitive,
2840 d3d8_device_DrawIndexedPrimitive,
2841 d3d8_device_DrawPrimitiveUP,
2842 d3d8_device_DrawIndexedPrimitiveUP,
2843 d3d8_device_ProcessVertices,
2844 d3d8_device_CreateVertexShader,
2845 d3d8_device_SetVertexShader,
2846 d3d8_device_GetVertexShader,
2847 d3d8_device_DeleteVertexShader,
2848 d3d8_device_SetVertexShaderConstant,
2849 d3d8_device_GetVertexShaderConstant,
2850 d3d8_device_GetVertexShaderDeclaration,
2851 d3d8_device_GetVertexShaderFunction,
2852 d3d8_device_SetStreamSource,
2853 d3d8_device_GetStreamSource,
2854 d3d8_device_SetIndices,
2855 d3d8_device_GetIndices,
2856 d3d8_device_CreatePixelShader,
2857 d3d8_device_SetPixelShader,
2858 d3d8_device_GetPixelShader,
2859 d3d8_device_DeletePixelShader,
2860 d3d8_device_SetPixelShaderConstant,
2861 d3d8_device_GetPixelShaderConstant,
2862 d3d8_device_GetPixelShaderFunction,
2863 d3d8_device_DrawRectPatch,
2864 d3d8_device_DrawTriPatch,
2865 d3d8_device_DeletePatch,
2868 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
2870 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
2873 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
2874 struct wined3d_device *device)
2876 TRACE("device_parent %p, device %p\n", device_parent, device);
2879 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
2881 TRACE("device_parent %p.\n", device_parent);
2884 static HRESULT CDECL device_parent_create_texture_surface(struct wined3d_device_parent *device_parent,
2885 void *container_parent, const struct wined3d_resource_desc *desc, UINT sub_resource_idx,
2886 DWORD flags, struct wined3d_surface **surface)
2888 struct d3d8_device *device = device_from_device_parent(device_parent);
2889 struct d3d8_surface *d3d_surface;
2890 HRESULT hr;
2892 TRACE("device_parent %p, container_parent %p, desc %p, sub_resource_idx %u, flags %#x, surface %p.\n",
2893 device_parent, container_parent, desc, sub_resource_idx, flags, surface);
2895 if (FAILED(hr = d3d8_device_create_surface(device, desc->width, desc->height,
2896 d3dformat_from_wined3dformat(desc->format), flags, (IDirect3DSurface8 **)&d3d_surface,
2897 desc->usage, desc->pool, desc->multisample_type, desc->multisample_quality)))
2899 WARN("Failed to create surface, hr %#x.\n", hr);
2900 return hr;
2903 *surface = d3d_surface->wined3d_surface;
2904 wined3d_surface_incref(*surface);
2906 d3d_surface->container = container_parent;
2907 IDirect3DDevice8_Release(d3d_surface->parent_device);
2908 d3d_surface->parent_device = NULL;
2910 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2911 d3d_surface->forwardReference = container_parent;
2913 return hr;
2916 static HRESULT CDECL device_parent_create_swapchain_surface(struct wined3d_device_parent *device_parent,
2917 void *container_parent, const struct wined3d_resource_desc *desc, struct wined3d_surface **surface)
2919 struct d3d8_device *device = device_from_device_parent(device_parent);
2920 struct d3d8_surface *d3d_surface;
2921 HRESULT hr;
2923 TRACE("device_parent %p, container_parent %p, desc %p, surface %p.\n",
2924 device_parent, container_parent, desc, surface);
2926 if (FAILED(hr = d3d8_device_create_surface(device, desc->width, desc->height,
2927 d3dformat_from_wined3dformat(desc->format), WINED3D_SURFACE_MAPPABLE, (IDirect3DSurface8 **)&d3d_surface,
2928 desc->usage, desc->pool, desc->multisample_type, desc->multisample_quality)))
2930 WARN("Failed to create surface, hr %#x.\n", hr);
2931 return hr;
2934 *surface = d3d_surface->wined3d_surface;
2935 wined3d_surface_incref(*surface);
2937 d3d_surface->container = (IUnknown *)&device->IDirect3DDevice8_iface;
2938 /* Implicit surfaces are created with an refcount of 0 */
2939 IDirect3DSurface8_Release(&d3d_surface->IDirect3DSurface8_iface);
2941 return hr;
2944 static HRESULT CDECL device_parent_create_volume(struct wined3d_device_parent *device_parent,
2945 void *container_parent, UINT width, UINT height, UINT depth, enum wined3d_format_id format,
2946 enum wined3d_pool pool, DWORD usage, struct wined3d_volume **volume)
2948 struct d3d8_device *device = device_from_device_parent(device_parent);
2949 struct d3d8_volume *object;
2950 HRESULT hr;
2952 TRACE("device_parent %p, container_parent %p, width %u, height %u, depth %u, "
2953 "format %#x, pool %#x, usage %#x, volume %p.\n",
2954 device_parent, container_parent, width, height, depth,
2955 format, pool, usage, volume);
2957 /* Allocate the storage for the device */
2958 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2959 if (!object)
2961 FIXME("Allocation of memory failed\n");
2962 *volume = NULL;
2963 return D3DERR_OUTOFVIDEOMEMORY;
2966 hr = volume_init(object, device, width, height, depth, usage, format, pool);
2967 if (FAILED(hr))
2969 WARN("Failed to initialize volume, hr %#x.\n", hr);
2970 HeapFree(GetProcessHeap(), 0, object);
2971 return hr;
2974 *volume = object->wined3d_volume;
2975 wined3d_volume_incref(*volume);
2976 IDirect3DVolume8_Release(&object->IDirect3DVolume8_iface);
2978 object->container = container_parent;
2979 object->forwardReference = container_parent;
2981 TRACE("Created volume %p.\n", object);
2983 return hr;
2986 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
2987 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
2989 struct d3d8_device *device = device_from_device_parent(device_parent);
2990 struct d3d8_swapchain *d3d_swapchain;
2991 HRESULT hr;
2993 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
2995 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
2997 WARN("Failed to create swapchain, hr %#x.\n", hr);
2998 *swapchain = NULL;
2999 return hr;
3002 *swapchain = d3d_swapchain->wined3d_swapchain;
3003 wined3d_swapchain_incref(*swapchain);
3004 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3006 return hr;
3009 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3011 device_parent_wined3d_device_created,
3012 device_parent_mode_changed,
3013 device_parent_create_swapchain_surface,
3014 device_parent_create_texture_surface,
3015 device_parent_create_volume,
3016 device_parent_create_swapchain,
3019 static void setup_fpu(void)
3021 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3022 WORD cw;
3023 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3024 cw = (cw & ~0xf3f) | 0x3f;
3025 __asm__ volatile ("fldcw %0" : : "m" (cw));
3026 #elif defined(__i386__) && defined(_MSC_VER)
3027 WORD cw;
3028 __asm fnstcw cw;
3029 cw = (cw & ~0xf3f) | 0x3f;
3030 __asm fldcw cw;
3031 #else
3032 FIXME("FPU setup not implemented for this platform.\n");
3033 #endif
3036 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3037 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3039 struct wined3d_swapchain_desc swapchain_desc;
3040 HRESULT hr;
3042 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3043 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3044 device->ref = 1;
3045 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3046 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3047 if (!device->handle_table.entries)
3049 ERR("Failed to allocate handle table memory.\n");
3050 return E_OUTOFMEMORY;
3052 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3054 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3056 wined3d_mutex_lock();
3057 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3058 &device->device_parent, &device->wined3d_device);
3059 if (FAILED(hr))
3061 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3062 wined3d_mutex_unlock();
3063 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3064 return hr;
3067 if (!parameters->Windowed)
3069 HWND device_window = parameters->hDeviceWindow;
3071 if (!focus_window)
3072 focus_window = device_window;
3073 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3075 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3076 wined3d_device_decref(device->wined3d_device);
3077 wined3d_mutex_unlock();
3078 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3079 return hr;
3082 if (!device_window)
3083 device_window = focus_window;
3084 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3085 parameters->BackBufferWidth,
3086 parameters->BackBufferHeight);
3089 if (flags & D3DCREATE_MULTITHREADED)
3090 wined3d_device_set_multithreaded(device->wined3d_device);
3092 wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters);
3094 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3095 if (FAILED(hr))
3097 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3098 wined3d_device_release_focus_window(device->wined3d_device);
3099 wined3d_device_decref(device->wined3d_device);
3100 wined3d_mutex_unlock();
3101 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3102 return hr;
3105 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3106 wined3d_mutex_unlock();
3108 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3110 device->declArraySize = 16;
3111 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3112 if (!device->decls)
3114 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3115 hr = E_OUTOFMEMORY;
3116 goto err;
3119 device->d3d_parent = &parent->IDirect3D8_iface;
3120 IDirect3D8_AddRef(device->d3d_parent);
3122 return D3D_OK;
3124 err:
3125 wined3d_mutex_lock();
3126 wined3d_device_uninit_3d(device->wined3d_device);
3127 wined3d_device_release_focus_window(device->wined3d_device);
3128 wined3d_device_decref(device->wined3d_device);
3129 wined3d_mutex_unlock();
3130 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3131 return hr;