ddraw/tests: Add another invalid arguments test for surface QI.
[wine.git] / dlls / d3d8 / device.c
blob506961a215bd55422c3b82b40f33298b0c718d5d
1 /*
2 * IDirect3DDevice8 implementation
4 * Copyright 2002-2004 Jason Edmeades
5 * Copyright 2004 Christian Costa
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <math.h>
25 #include <stdarg.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "wingdi.h"
31 #include "wine/debug.h"
33 #include "d3d8_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
37 static void STDMETHODCALLTYPE d3d8_null_wined3d_object_destroyed(void *parent) {}
39 static const struct wined3d_parent_ops d3d8_null_wined3d_parent_ops =
41 d3d8_null_wined3d_object_destroyed,
44 D3DFORMAT d3dformat_from_wined3dformat(enum wined3d_format_id format)
46 BYTE *c = (BYTE *)&format;
48 /* Don't translate FOURCC formats */
49 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
51 switch(format)
53 case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
54 case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
55 case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
56 case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
57 case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
58 case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
59 case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
60 case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
61 case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
62 case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
63 case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
64 case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
65 case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
66 case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
67 case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
68 case WINED3DFMT_P8_UINT: return D3DFMT_P8;
69 case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
70 case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
71 case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
72 case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
73 case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
74 case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
75 case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
76 case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
77 case WINED3DFMT_R10G11B11_SNORM: return D3DFMT_W11V11U10;
78 case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
79 case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
80 case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
81 case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
82 case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
83 case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
84 case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
85 case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
86 case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
87 case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
88 default:
89 FIXME("Unhandled wined3d format %#x.\n", format);
90 return D3DFMT_UNKNOWN;
94 enum wined3d_format_id wined3dformat_from_d3dformat(D3DFORMAT format)
96 BYTE *c = (BYTE *)&format;
98 /* Don't translate FOURCC formats */
99 if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
101 switch(format)
103 case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
104 case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
105 case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
106 case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
107 case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
108 case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
109 case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
110 case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
111 case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
112 case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
113 case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
114 case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
115 case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
116 case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
117 case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
118 case D3DFMT_P8: return WINED3DFMT_P8_UINT;
119 case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
120 case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
121 case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
122 case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
123 case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
124 case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
125 case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
126 case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
127 case D3DFMT_W11V11U10: return WINED3DFMT_R10G11B11_SNORM;
128 case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
129 case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
130 case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
131 case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
132 case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
133 case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
134 case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
135 case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
136 case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
137 case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
138 default:
139 FIXME("Unhandled D3DFORMAT %#x.\n", format);
140 return WINED3DFMT_UNKNOWN;
144 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
146 switch (primitive_type)
148 case D3DPT_POINTLIST:
149 return primitive_count;
151 case D3DPT_LINELIST:
152 return primitive_count * 2;
154 case D3DPT_LINESTRIP:
155 return primitive_count + 1;
157 case D3DPT_TRIANGLELIST:
158 return primitive_count * 3;
160 case D3DPT_TRIANGLESTRIP:
161 case D3DPT_TRIANGLEFAN:
162 return primitive_count + 2;
164 default:
165 FIXME("Unhandled primitive type %#x.\n", primitive_type);
166 return 0;
170 static void present_parameters_from_wined3d_swapchain_desc(D3DPRESENT_PARAMETERS *present_parameters,
171 const struct wined3d_swapchain_desc *swapchain_desc)
173 present_parameters->BackBufferWidth = swapchain_desc->backbuffer_width;
174 present_parameters->BackBufferHeight = swapchain_desc->backbuffer_height;
175 present_parameters->BackBufferFormat = d3dformat_from_wined3dformat(swapchain_desc->backbuffer_format);
176 present_parameters->BackBufferCount = swapchain_desc->backbuffer_count;
177 present_parameters->MultiSampleType = swapchain_desc->multisample_type;
178 present_parameters->SwapEffect = swapchain_desc->swap_effect;
179 present_parameters->hDeviceWindow = swapchain_desc->device_window;
180 present_parameters->Windowed = swapchain_desc->windowed;
181 present_parameters->EnableAutoDepthStencil = swapchain_desc->enable_auto_depth_stencil;
182 present_parameters->AutoDepthStencilFormat
183 = d3dformat_from_wined3dformat(swapchain_desc->auto_depth_stencil_format);
184 present_parameters->Flags = swapchain_desc->flags & D3DPRESENTFLAGS_MASK;
185 present_parameters->FullScreen_RefreshRateInHz = swapchain_desc->refresh_rate;
186 present_parameters->FullScreen_PresentationInterval = swapchain_desc->swap_interval;
189 static BOOL wined3d_swapchain_desc_from_present_parameters(struct wined3d_swapchain_desc *swapchain_desc,
190 const D3DPRESENT_PARAMETERS *present_parameters)
192 if (!present_parameters->SwapEffect || present_parameters->SwapEffect > D3DSWAPEFFECT_COPY_VSYNC)
194 WARN("Invalid swap effect %u passed.\n", present_parameters->SwapEffect);
195 return FALSE;
197 if (present_parameters->BackBufferCount > 3
198 || ((present_parameters->SwapEffect == D3DSWAPEFFECT_COPY
199 || present_parameters->SwapEffect == D3DSWAPEFFECT_COPY_VSYNC)
200 && present_parameters->BackBufferCount > 1))
202 WARN("Invalid backbuffer count %u.\n", present_parameters->BackBufferCount);
203 return FALSE;
206 swapchain_desc->backbuffer_width = present_parameters->BackBufferWidth;
207 swapchain_desc->backbuffer_height = present_parameters->BackBufferHeight;
208 swapchain_desc->backbuffer_format = wined3dformat_from_d3dformat(present_parameters->BackBufferFormat);
209 swapchain_desc->backbuffer_count = max(1, present_parameters->BackBufferCount);
210 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
211 swapchain_desc->multisample_quality = 0; /* d3d9 only */
212 swapchain_desc->swap_effect = present_parameters->SwapEffect;
213 swapchain_desc->device_window = present_parameters->hDeviceWindow;
214 swapchain_desc->windowed = present_parameters->Windowed;
215 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
216 swapchain_desc->auto_depth_stencil_format
217 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
218 swapchain_desc->flags
219 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
220 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
221 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
222 swapchain_desc->auto_restore_display_mode = TRUE;
224 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
225 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
227 return TRUE;
230 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const WINED3DCAPS *wined3d_caps)
232 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
233 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
234 caps->Caps = wined3d_caps->Caps;
235 caps->Caps2 = wined3d_caps->Caps2;
236 caps->Caps3 = wined3d_caps->Caps3;
237 caps->PresentationIntervals = wined3d_caps->PresentationIntervals;
238 caps->CursorCaps = wined3d_caps->CursorCaps;
239 caps->DevCaps = wined3d_caps->DevCaps;
240 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
241 caps->RasterCaps = wined3d_caps->RasterCaps;
242 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
243 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
244 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
245 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
246 caps->ShadeCaps = wined3d_caps->ShadeCaps;
247 caps->TextureCaps = wined3d_caps->TextureCaps;
248 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
249 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
250 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
251 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
252 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
253 caps->LineCaps = wined3d_caps->LineCaps;
254 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
255 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
256 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
257 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
258 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
259 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
260 caps->MaxVertexW = wined3d_caps->MaxVertexW;
261 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
262 caps->GuardBandTop = wined3d_caps->GuardBandTop;
263 caps->GuardBandRight = wined3d_caps->GuardBandRight;
264 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
265 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
266 caps->StencilCaps = wined3d_caps->StencilCaps;
267 caps->FVFCaps = wined3d_caps->FVFCaps;
268 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
269 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
270 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
271 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
272 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
273 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
274 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
275 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
276 caps->MaxPointSize = wined3d_caps->MaxPointSize;
277 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
278 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
279 caps->MaxStreams = wined3d_caps->MaxStreams;
280 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
281 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
282 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
283 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
284 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
286 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
287 if (caps->PixelShaderVersion)
288 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
289 else
290 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
291 if (caps->VertexShaderVersion)
292 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
293 else
294 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
295 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
297 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
300 /* Handle table functions */
301 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
303 struct d3d8_handle_entry *entry;
305 if (t->free_entries)
307 DWORD index = t->free_entries - t->entries;
308 /* Use a free handle */
309 entry = t->free_entries;
310 if (entry->type != D3D8_HANDLE_FREE)
312 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
313 return D3D8_INVALID_HANDLE;
315 t->free_entries = entry->object;
316 entry->object = object;
317 entry->type = type;
319 return index;
322 if (!(t->entry_count < t->table_size))
324 /* Grow the table */
325 UINT new_size = t->table_size + (t->table_size >> 1);
326 struct d3d8_handle_entry *new_entries = HeapReAlloc(GetProcessHeap(),
327 0, t->entries, new_size * sizeof(*t->entries));
328 if (!new_entries)
330 ERR("Failed to grow the handle table.\n");
331 return D3D8_INVALID_HANDLE;
333 t->entries = new_entries;
334 t->table_size = new_size;
337 entry = &t->entries[t->entry_count];
338 entry->object = object;
339 entry->type = type;
341 return t->entry_count++;
344 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
346 struct d3d8_handle_entry *entry;
347 void *object;
349 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
351 WARN("Invalid handle %u passed.\n", handle);
352 return NULL;
355 entry = &t->entries[handle];
356 if (entry->type != type)
358 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
359 return NULL;
362 object = entry->object;
363 entry->object = t->free_entries;
364 entry->type = D3D8_HANDLE_FREE;
365 t->free_entries = entry;
367 return object;
370 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
372 struct d3d8_handle_entry *entry;
374 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
376 WARN("Invalid handle %u passed.\n", handle);
377 return NULL;
380 entry = &t->entries[handle];
381 if (entry->type != type)
383 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
384 return NULL;
387 return entry->object;
390 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
392 TRACE("iface %p, riid %s, out %p.\n",
393 iface, debugstr_guid(riid), out);
395 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
396 || IsEqualGUID(riid, &IID_IUnknown))
398 IDirect3DDevice8_AddRef(iface);
399 *out = iface;
400 return S_OK;
403 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
405 *out = NULL;
406 return E_NOINTERFACE;
409 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
411 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
412 ULONG ref = InterlockedIncrement(&device->ref);
414 TRACE("%p increasing refcount to %u.\n", iface, ref);
416 return ref;
419 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
421 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
422 ULONG ref;
424 if (device->inDestruction)
425 return 0;
427 ref = InterlockedDecrement(&device->ref);
429 TRACE("%p decreasing refcount to %u.\n", iface, ref);
431 if (!ref)
433 IDirect3D8 *parent = device->d3d_parent;
434 unsigned i;
436 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
438 wined3d_mutex_lock();
440 device->inDestruction = TRUE;
442 for (i = 0; i < device->numConvertedDecls; ++i)
444 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
446 HeapFree(GetProcessHeap(), 0, device->decls);
448 if (device->vertex_buffer)
449 wined3d_buffer_decref(device->vertex_buffer);
450 if (device->index_buffer)
451 wined3d_buffer_decref(device->index_buffer);
453 wined3d_device_uninit_3d(device->wined3d_device);
454 wined3d_device_release_focus_window(device->wined3d_device);
455 wined3d_device_decref(device->wined3d_device);
456 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
457 HeapFree(GetProcessHeap(), 0, device);
459 wined3d_mutex_unlock();
461 IDirect3D8_Release(parent);
463 return ref;
466 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
468 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
470 TRACE("iface %p.\n", iface);
472 TRACE("device state: %#x.\n", device->device_state);
474 switch (device->device_state)
476 default:
477 case D3D8_DEVICE_STATE_OK:
478 return D3D_OK;
479 case D3D8_DEVICE_STATE_LOST:
480 return D3DERR_DEVICELOST;
481 case D3D8_DEVICE_STATE_NOT_RESET:
482 return D3DERR_DEVICENOTRESET;
486 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
488 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
489 UINT ret;
491 TRACE("iface %p.\n", iface);
493 wined3d_mutex_lock();
494 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
495 wined3d_mutex_unlock();
497 return ret;
500 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
502 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
504 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
506 if (byte_count)
507 FIXME("Byte count ignored.\n");
509 wined3d_mutex_lock();
510 wined3d_device_evict_managed_resources(device->wined3d_device);
511 wined3d_mutex_unlock();
513 return D3D_OK;
516 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
518 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
520 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
522 if (!d3d8)
523 return D3DERR_INVALIDCALL;
525 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
528 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
530 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
531 WINED3DCAPS wined3d_caps;
532 HRESULT hr;
534 TRACE("iface %p, caps %p.\n", iface, caps);
536 if (!caps)
537 return D3DERR_INVALIDCALL;
539 wined3d_mutex_lock();
540 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
541 wined3d_mutex_unlock();
543 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
545 return hr;
548 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
550 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
551 struct wined3d_display_mode wined3d_mode;
552 HRESULT hr;
554 TRACE("iface %p, mode %p.\n", iface, mode);
556 wined3d_mutex_lock();
557 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
558 wined3d_mutex_unlock();
560 if (SUCCEEDED(hr))
562 mode->Width = wined3d_mode.width;
563 mode->Height = wined3d_mode.height;
564 mode->RefreshRate = wined3d_mode.refresh_rate;
565 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
568 return hr;
571 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
572 D3DDEVICE_CREATION_PARAMETERS *parameters)
574 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
576 TRACE("iface %p, parameters %p.\n", iface, parameters);
578 wined3d_mutex_lock();
579 wined3d_device_get_creation_parameters(device->wined3d_device,
580 (struct wined3d_device_creation_parameters *)parameters);
581 wined3d_mutex_unlock();
583 return D3D_OK;
586 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
587 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
589 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
590 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
591 HRESULT hr;
593 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
594 iface, hotspot_x, hotspot_y, bitmap);
596 if (!bitmap)
598 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
599 return D3DERR_INVALIDCALL;
602 wined3d_mutex_lock();
603 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
604 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
605 wined3d_mutex_unlock();
607 return hr;
610 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
612 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
614 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
616 wined3d_mutex_lock();
617 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
618 wined3d_mutex_unlock();
621 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
623 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
624 BOOL ret;
626 TRACE("iface %p, show %#x.\n", iface, show);
628 wined3d_mutex_lock();
629 ret = wined3d_device_show_cursor(device->wined3d_device, show);
630 wined3d_mutex_unlock();
632 return ret;
635 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
636 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
638 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
639 struct wined3d_swapchain_desc desc;
640 struct d3d8_swapchain *object;
641 UINT i, count;
642 HRESULT hr;
644 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
645 iface, present_parameters, swapchain);
647 if (!present_parameters->Windowed)
649 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
650 return D3DERR_INVALIDCALL;
653 wined3d_mutex_lock();
654 count = wined3d_device_get_swapchain_count(device->wined3d_device);
655 for (i = 0; i < count; ++i)
657 struct wined3d_swapchain *wined3d_swapchain;
659 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
660 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
662 if (!desc.windowed)
664 wined3d_mutex_unlock();
665 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
666 return D3DERR_INVALIDCALL;
669 wined3d_mutex_unlock();
671 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
672 return D3DERR_INVALIDCALL;
673 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
674 *swapchain = &object->IDirect3DSwapChain8_iface;
675 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
677 return hr;
680 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
682 struct wined3d_resource_desc desc;
683 IDirect3DBaseTexture8 *texture;
684 struct d3d8_surface *surface;
685 IUnknown *parent;
687 wined3d_resource_get_desc(resource, &desc);
688 if (desc.pool != WINED3D_POOL_DEFAULT)
689 return D3D_OK;
691 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
693 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
694 return D3DERR_DEVICELOST;
697 parent = wined3d_resource_get_parent(resource);
698 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
700 IDirect3DBaseTexture8_Release(texture);
701 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
702 return D3DERR_DEVICELOST;
705 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
706 if (!surface->resource.refcount)
707 return D3D_OK;
709 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
710 return D3DERR_DEVICELOST;
713 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
714 D3DPRESENT_PARAMETERS *present_parameters)
716 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
717 struct wined3d_swapchain_desc swapchain_desc;
718 HRESULT hr;
720 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
722 if (device->device_state == D3D8_DEVICE_STATE_LOST)
724 WARN("App not active, returning D3DERR_DEVICELOST.\n");
725 return D3DERR_DEVICELOST;
727 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
728 return D3DERR_INVALIDCALL;
730 wined3d_mutex_lock();
732 if (device->vertex_buffer)
734 wined3d_buffer_decref(device->vertex_buffer);
735 device->vertex_buffer = NULL;
736 device->vertex_buffer_size = 0;
738 if (device->index_buffer)
740 wined3d_buffer_decref(device->index_buffer);
741 device->index_buffer = NULL;
742 device->index_buffer_size = 0;
745 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
746 NULL, reset_enum_callback, TRUE)))
748 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
749 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
750 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_ZENABLE,
751 !!swapchain_desc.enable_auto_depth_stencil);
752 device->device_state = D3D8_DEVICE_STATE_OK;
754 else
756 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
758 wined3d_mutex_unlock();
760 return hr;
763 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
764 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
766 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
768 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
769 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
771 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
772 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
773 * shows a framerate on Windows in applications that only call the device
774 * method, like e.g. the dx8 sdk samples. The conclusion is that native
775 * calls the swapchain's public method from the device. */
776 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
777 src_rect, dst_rect, dst_window_override, dirty_region);
780 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
781 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
783 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
784 struct wined3d_swapchain *wined3d_swapchain;
785 struct wined3d_texture *wined3d_texture;
786 struct d3d8_surface *surface_impl;
788 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
789 iface, backbuffer_idx, backbuffer_type, backbuffer);
791 /* backbuffer_type is ignored by native. */
793 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
794 wined3d_mutex_lock();
796 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
797 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
799 wined3d_mutex_unlock();
800 *backbuffer = NULL;
801 return D3DERR_INVALIDCALL;
804 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
805 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
806 IDirect3DSurface8_AddRef(*backbuffer);
808 wined3d_mutex_unlock();
809 return D3D_OK;
812 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
814 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
815 HRESULT hr;
817 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
819 wined3d_mutex_lock();
820 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
821 wined3d_mutex_unlock();
823 return hr;
826 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
828 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
830 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
832 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
833 wined3d_mutex_lock();
834 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
835 wined3d_mutex_unlock();
838 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
840 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
842 TRACE("iface %p, ramp %p.\n", iface, ramp);
844 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
845 wined3d_mutex_lock();
846 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
847 wined3d_mutex_unlock();
850 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
851 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
852 D3DPOOL pool, IDirect3DTexture8 **texture)
854 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
855 struct d3d8_texture *object;
856 HRESULT hr;
858 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
859 iface, width, height, levels, usage, format, pool, texture);
861 if (!format)
862 return D3DERR_INVALIDCALL;
864 *texture = NULL;
865 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
866 if (!object)
867 return D3DERR_OUTOFVIDEOMEMORY;
869 hr = texture_init(object, device, width, height, levels, usage, format, pool);
870 if (FAILED(hr))
872 WARN("Failed to initialize texture, hr %#x.\n", hr);
873 HeapFree(GetProcessHeap(), 0, object);
874 return hr;
877 TRACE("Created texture %p.\n", object);
878 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
880 return D3D_OK;
883 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
884 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
885 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
887 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
888 struct d3d8_texture *object;
889 HRESULT hr;
891 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
892 iface, width, height, depth, levels, usage, format, pool, texture);
894 if (!format)
895 return D3DERR_INVALIDCALL;
897 *texture = NULL;
898 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
899 if (!object)
900 return D3DERR_OUTOFVIDEOMEMORY;
902 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
903 if (FAILED(hr))
905 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
906 HeapFree(GetProcessHeap(), 0, object);
907 return hr;
910 TRACE("Created volume texture %p.\n", object);
911 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
913 return D3D_OK;
916 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
917 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
919 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
920 struct d3d8_texture *object;
921 HRESULT hr;
923 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
924 iface, edge_length, levels, usage, format, pool, texture);
926 if (!format)
927 return D3DERR_INVALIDCALL;
929 *texture = NULL;
930 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
931 if (!object)
932 return D3DERR_OUTOFVIDEOMEMORY;
934 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
935 if (FAILED(hr))
937 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
938 HeapFree(GetProcessHeap(), 0, object);
939 return hr;
942 TRACE("Created cube texture %p.\n", object);
943 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
945 return hr;
948 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
949 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
951 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
952 struct d3d8_vertexbuffer *object;
953 HRESULT hr;
955 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
956 iface, size, usage, fvf, pool, buffer);
958 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
959 if (!object)
960 return D3DERR_OUTOFVIDEOMEMORY;
962 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
963 if (FAILED(hr))
965 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
966 HeapFree(GetProcessHeap(), 0, object);
967 return hr;
970 TRACE("Created vertex buffer %p.\n", object);
971 *buffer = &object->IDirect3DVertexBuffer8_iface;
973 return D3D_OK;
976 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
977 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
979 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
980 struct d3d8_indexbuffer *object;
981 HRESULT hr;
983 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
984 iface, size, usage, format, pool, buffer);
986 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
987 if (!object)
988 return D3DERR_OUTOFVIDEOMEMORY;
990 hr = indexbuffer_init(object, device, size, usage, format, pool);
991 if (FAILED(hr))
993 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
994 HeapFree(GetProcessHeap(), 0, object);
995 return hr;
998 TRACE("Created index buffer %p.\n", object);
999 *buffer = &object->IDirect3DIndexBuffer8_iface;
1001 return D3D_OK;
1004 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
1005 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
1006 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
1008 struct wined3d_resource_desc desc;
1009 struct d3d8_surface *surface_impl;
1010 struct wined3d_texture *texture;
1011 HRESULT hr;
1013 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p, "
1014 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1015 device, width, height, format, flags, surface,
1016 usage, pool, multisample_type, multisample_quality);
1018 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1019 desc.format = wined3dformat_from_d3dformat(format);
1020 desc.multisample_type = multisample_type;
1021 desc.multisample_quality = multisample_quality;
1022 desc.usage = usage & WINED3DUSAGE_MASK;
1023 desc.pool = pool;
1024 desc.width = width;
1025 desc.height = height;
1026 desc.depth = 1;
1027 desc.size = 0;
1029 wined3d_mutex_lock();
1031 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1032 1, 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1034 wined3d_mutex_unlock();
1035 WARN("Failed to create texture, hr %#x.\n", hr);
1036 return hr;
1039 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1040 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1041 *surface = &surface_impl->IDirect3DSurface8_iface;
1042 IDirect3DSurface8_AddRef(*surface);
1043 wined3d_texture_decref(texture);
1045 wined3d_mutex_unlock();
1047 return D3D_OK;
1050 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1051 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1052 IDirect3DSurface8 **surface)
1054 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1055 DWORD flags = 0;
1057 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1058 iface, width, height, format, multisample_type, lockable, surface);
1060 if (!format)
1061 return D3DERR_INVALIDCALL;
1063 *surface = NULL;
1064 if (lockable)
1065 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1067 return d3d8_device_create_surface(device, width, height, format, flags, surface,
1068 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
1071 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1072 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1073 IDirect3DSurface8 **surface)
1075 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1077 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1078 iface, width, height, format, multisample_type, surface);
1080 if (!format)
1081 return D3DERR_INVALIDCALL;
1083 *surface = NULL;
1085 /* TODO: Verify that Discard is false */
1086 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1087 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1090 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1091 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1092 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1094 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1096 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1097 iface, width, height, format, surface);
1099 *surface = NULL;
1101 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1102 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1105 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1106 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1107 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1109 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1110 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1111 enum wined3d_format_id src_format, dst_format;
1112 struct wined3d_sub_resource_desc wined3d_desc;
1113 UINT src_w, src_h;
1115 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1116 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1118 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1119 * destination texture is in WINED3D_POOL_DEFAULT. */
1121 wined3d_mutex_lock();
1122 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1123 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1125 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1126 wined3d_mutex_unlock();
1127 return D3DERR_INVALIDCALL;
1129 src_format = wined3d_desc.format;
1130 src_w = wined3d_desc.width;
1131 src_h = wined3d_desc.height;
1133 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1134 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1136 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1137 wined3d_mutex_unlock();
1138 return D3DERR_INVALIDCALL;
1140 dst_format = wined3d_desc.format;
1142 /* Check that the source and destination formats match */
1143 if (src_format != dst_format)
1145 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1146 src_surface, dst_surface);
1147 wined3d_mutex_unlock();
1148 return D3DERR_INVALIDCALL;
1151 /* Quick if complete copy ... */
1152 if (!rect_count && !src_rects && !dst_points)
1154 RECT rect = {0, 0, src_w, src_h};
1155 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1156 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1158 else
1160 unsigned int i;
1161 /* Copy rect by rect */
1162 if (src_rects && dst_points)
1164 for (i = 0; i < rect_count; ++i)
1166 UINT w = src_rects[i].right - src_rects[i].left;
1167 UINT h = src_rects[i].bottom - src_rects[i].top;
1168 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1169 dst_points[i].x + w, dst_points[i].y + h};
1171 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1172 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1175 else
1177 for (i = 0; i < rect_count; ++i)
1179 UINT w = src_rects[i].right - src_rects[i].left;
1180 UINT h = src_rects[i].bottom - src_rects[i].top;
1181 RECT dst_rect = {0, 0, w, h};
1183 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1184 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1188 wined3d_mutex_unlock();
1190 return WINED3D_OK;
1193 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1194 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1196 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1197 struct d3d8_texture *src_impl, *dst_impl;
1198 HRESULT hr;
1200 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1202 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1203 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1205 wined3d_mutex_lock();
1206 hr = wined3d_device_update_texture(device->wined3d_device,
1207 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1208 wined3d_mutex_unlock();
1210 return hr;
1213 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1215 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1216 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1217 HRESULT hr;
1219 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1221 if (!dst_surface)
1223 WARN("Invalid destination surface passed.\n");
1224 return D3DERR_INVALIDCALL;
1227 wined3d_mutex_lock();
1228 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1229 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1230 wined3d_mutex_unlock();
1232 return hr;
1235 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1236 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1238 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1239 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1240 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1241 struct wined3d_rendertarget_view *original_dsv, *rtv;
1242 HRESULT hr = D3D_OK;
1244 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1246 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1248 WARN("Render target surface does not match device.\n");
1249 return D3DERR_INVALIDCALL;
1252 wined3d_mutex_lock();
1254 if (ds_impl)
1256 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1257 struct wined3d_rendertarget_view *original_rtv;
1258 struct d3d8_surface *original_surface;
1260 /* If no render target is passed in check the size against the current RT */
1261 if (!render_target)
1263 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1265 wined3d_mutex_unlock();
1266 return D3DERR_NOTFOUND;
1268 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1269 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1270 original_surface->sub_resource_idx, &rt_desc);
1272 else
1273 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1274 rt_impl->sub_resource_idx, &rt_desc);
1276 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1278 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1280 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1281 wined3d_mutex_unlock();
1282 return D3DERR_INVALIDCALL;
1284 if (ds_desc.multisample_type != rt_desc.multisample_type
1285 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1287 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1288 wined3d_mutex_unlock();
1289 return D3DERR_INVALIDCALL;
1293 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1294 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1295 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1296 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1297 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1298 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1299 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1300 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1302 wined3d_mutex_unlock();
1304 return hr;
1307 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1309 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1310 struct wined3d_rendertarget_view *wined3d_rtv;
1311 struct d3d8_surface *surface_impl;
1312 HRESULT hr;
1314 TRACE("iface %p, render_target %p.\n", iface, render_target);
1316 if (!render_target)
1317 return D3DERR_INVALIDCALL;
1319 wined3d_mutex_lock();
1320 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1322 /* We want the sub resource parent here, since the view itself may be
1323 * internal to wined3d and may not have a parent. */
1324 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1325 *render_target = &surface_impl->IDirect3DSurface8_iface;
1326 IDirect3DSurface8_AddRef(*render_target);
1327 hr = D3D_OK;
1329 else
1331 ERR("Failed to get wined3d render target.\n");
1332 *render_target = NULL;
1333 hr = D3DERR_NOTFOUND;
1335 wined3d_mutex_unlock();
1337 return hr;
1340 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1342 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1343 struct wined3d_rendertarget_view *wined3d_dsv;
1344 struct d3d8_surface *surface_impl;
1345 HRESULT hr = D3D_OK;
1347 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1349 if (!depth_stencil)
1350 return D3DERR_INVALIDCALL;
1352 wined3d_mutex_lock();
1353 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1355 /* We want the sub resource parent here, since the view itself may be
1356 * internal to wined3d and may not have a parent. */
1357 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1358 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1359 IDirect3DSurface8_AddRef(*depth_stencil);
1361 else
1363 hr = D3DERR_NOTFOUND;
1364 *depth_stencil = NULL;
1366 wined3d_mutex_unlock();
1368 return hr;
1371 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1373 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1374 HRESULT hr;
1376 TRACE("iface %p.\n", iface);
1378 wined3d_mutex_lock();
1379 hr = wined3d_device_begin_scene(device->wined3d_device);
1380 wined3d_mutex_unlock();
1382 return hr;
1385 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1387 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1388 HRESULT hr;
1390 TRACE("iface %p.\n", iface);
1392 wined3d_mutex_lock();
1393 hr = wined3d_device_end_scene(device->wined3d_device);
1394 wined3d_mutex_unlock();
1396 return hr;
1399 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1400 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1402 const struct wined3d_color c =
1404 ((color >> 16) & 0xff) / 255.0f,
1405 ((color >> 8) & 0xff) / 255.0f,
1406 (color & 0xff) / 255.0f,
1407 ((color >> 24) & 0xff) / 255.0f,
1409 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1410 HRESULT hr;
1412 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1413 iface, rect_count, rects, flags, color, z, stencil);
1415 if (rect_count && !rects)
1417 WARN("count %u with NULL rects.\n", rect_count);
1418 rect_count = 0;
1421 wined3d_mutex_lock();
1422 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1423 wined3d_mutex_unlock();
1425 return hr;
1428 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1429 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1431 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1433 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1435 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1436 wined3d_mutex_lock();
1437 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1438 wined3d_mutex_unlock();
1440 return D3D_OK;
1443 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1444 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1446 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1448 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1450 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1451 wined3d_mutex_lock();
1452 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1453 wined3d_mutex_unlock();
1455 return D3D_OK;
1458 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1459 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1461 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1463 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1465 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1466 wined3d_mutex_lock();
1467 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1468 wined3d_mutex_unlock();
1470 return D3D_OK;
1473 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1475 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1476 struct wined3d_viewport vp;
1478 TRACE("iface %p, viewport %p.\n", iface, viewport);
1480 vp.x = viewport->X;
1481 vp.y = viewport->Y;
1482 vp.width = viewport->Width;
1483 vp.height = viewport->Height;
1484 vp.min_z = viewport->MinZ;
1485 vp.max_z = viewport->MaxZ;
1487 wined3d_mutex_lock();
1488 wined3d_device_set_viewport(device->wined3d_device, &vp);
1489 wined3d_mutex_unlock();
1491 return D3D_OK;
1494 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1496 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1497 struct wined3d_viewport wined3d_viewport;
1499 TRACE("iface %p, viewport %p.\n", iface, viewport);
1501 wined3d_mutex_lock();
1502 wined3d_device_get_viewport(device->wined3d_device, &wined3d_viewport);
1503 wined3d_mutex_unlock();
1505 viewport->X = wined3d_viewport.x;
1506 viewport->Y = wined3d_viewport.y;
1507 viewport->Width = wined3d_viewport.width;
1508 viewport->Height = wined3d_viewport.height;
1509 viewport->MinZ = wined3d_viewport.min_z;
1510 viewport->MaxZ = wined3d_viewport.max_z;
1512 return D3D_OK;
1515 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1517 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1519 TRACE("iface %p, material %p.\n", iface, material);
1521 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1522 wined3d_mutex_lock();
1523 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1524 wined3d_mutex_unlock();
1526 return D3D_OK;
1529 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1531 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1533 TRACE("iface %p, material %p.\n", iface, material);
1535 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1536 wined3d_mutex_lock();
1537 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1538 wined3d_mutex_unlock();
1540 return D3D_OK;
1543 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1545 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1546 HRESULT hr;
1548 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1550 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1551 wined3d_mutex_lock();
1552 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1553 wined3d_mutex_unlock();
1555 return hr;
1558 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1560 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1561 HRESULT hr;
1563 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1565 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1566 wined3d_mutex_lock();
1567 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1568 wined3d_mutex_unlock();
1570 return hr;
1573 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1575 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1576 HRESULT hr;
1578 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1580 wined3d_mutex_lock();
1581 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1582 wined3d_mutex_unlock();
1584 return hr;
1587 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1589 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1590 HRESULT hr;
1592 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1594 wined3d_mutex_lock();
1595 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1596 wined3d_mutex_unlock();
1598 return hr;
1601 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1603 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1604 HRESULT hr;
1606 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1608 wined3d_mutex_lock();
1609 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1610 wined3d_mutex_unlock();
1612 return hr;
1615 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1617 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1618 HRESULT hr;
1620 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1622 wined3d_mutex_lock();
1623 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1624 wined3d_mutex_unlock();
1626 return hr;
1629 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1630 D3DRENDERSTATETYPE state, DWORD value)
1632 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1634 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1636 wined3d_mutex_lock();
1637 switch (state)
1639 case D3DRS_ZBIAS:
1640 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1641 break;
1643 default:
1644 wined3d_device_set_render_state(device->wined3d_device, state, value);
1646 wined3d_mutex_unlock();
1648 return D3D_OK;
1651 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1652 D3DRENDERSTATETYPE state, DWORD *value)
1654 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1656 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1658 wined3d_mutex_lock();
1659 switch (state)
1661 case D3DRS_ZBIAS:
1662 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1663 break;
1665 default:
1666 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1668 wined3d_mutex_unlock();
1670 return D3D_OK;
1673 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1675 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1676 HRESULT hr;
1678 TRACE("iface %p.\n", iface);
1680 wined3d_mutex_lock();
1681 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1682 wined3d_mutex_unlock();
1684 return hr;
1687 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1689 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1690 struct wined3d_stateblock *stateblock;
1691 HRESULT hr;
1693 TRACE("iface %p, token %p.\n", iface, token);
1695 /* Tell wineD3D to endstateblock before anything else (in case we run out
1696 * of memory later and cause locking problems)
1698 wined3d_mutex_lock();
1699 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1700 if (FAILED(hr))
1702 WARN("Failed to end the state block, %#x.\n", hr);
1703 wined3d_mutex_unlock();
1704 return hr;
1707 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1708 wined3d_mutex_unlock();
1710 if (*token == D3D8_INVALID_HANDLE)
1712 ERR("Failed to create a handle\n");
1713 wined3d_mutex_lock();
1714 wined3d_stateblock_decref(stateblock);
1715 wined3d_mutex_unlock();
1716 return E_FAIL;
1718 ++*token;
1720 TRACE("Returning %#x (%p).\n", *token, stateblock);
1722 return hr;
1725 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1727 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1728 struct wined3d_stateblock *stateblock;
1730 TRACE("iface %p, token %#x.\n", iface, token);
1732 if (!token)
1733 return D3D_OK;
1735 wined3d_mutex_lock();
1736 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1737 if (!stateblock)
1739 WARN("Invalid handle (%#x) passed.\n", token);
1740 wined3d_mutex_unlock();
1741 return D3DERR_INVALIDCALL;
1743 wined3d_stateblock_apply(stateblock);
1744 wined3d_mutex_unlock();
1746 return D3D_OK;
1749 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1751 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1752 struct wined3d_stateblock *stateblock;
1754 TRACE("iface %p, token %#x.\n", iface, token);
1756 wined3d_mutex_lock();
1757 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1758 if (!stateblock)
1760 WARN("Invalid handle (%#x) passed.\n", token);
1761 wined3d_mutex_unlock();
1762 return D3DERR_INVALIDCALL;
1764 wined3d_stateblock_capture(stateblock);
1765 wined3d_mutex_unlock();
1767 return D3D_OK;
1770 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1772 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1773 struct wined3d_stateblock *stateblock;
1775 TRACE("iface %p, token %#x.\n", iface, token);
1777 wined3d_mutex_lock();
1778 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1780 if (!stateblock)
1782 WARN("Invalid handle (%#x) passed.\n", token);
1783 wined3d_mutex_unlock();
1784 return D3DERR_INVALIDCALL;
1787 if (wined3d_stateblock_decref(stateblock))
1789 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1791 wined3d_mutex_unlock();
1793 return D3D_OK;
1796 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1797 D3DSTATEBLOCKTYPE type, DWORD *handle)
1799 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1800 struct wined3d_stateblock *stateblock;
1801 HRESULT hr;
1803 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1805 if (type != D3DSBT_ALL
1806 && type != D3DSBT_PIXELSTATE
1807 && type != D3DSBT_VERTEXSTATE)
1809 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1810 return D3DERR_INVALIDCALL;
1813 wined3d_mutex_lock();
1814 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1815 if (FAILED(hr))
1817 wined3d_mutex_unlock();
1818 ERR("Failed to create the state block, hr %#x\n", hr);
1819 return hr;
1822 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1823 wined3d_mutex_unlock();
1825 if (*handle == D3D8_INVALID_HANDLE)
1827 ERR("Failed to allocate a handle.\n");
1828 wined3d_mutex_lock();
1829 wined3d_stateblock_decref(stateblock);
1830 wined3d_mutex_unlock();
1831 return E_FAIL;
1833 ++*handle;
1835 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1837 return hr;
1840 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1842 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1843 HRESULT hr;
1845 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1846 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1848 wined3d_mutex_lock();
1849 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1850 wined3d_mutex_unlock();
1852 return hr;
1855 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1857 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1858 HRESULT hr;
1860 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1862 wined3d_mutex_lock();
1863 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1864 wined3d_mutex_unlock();
1866 return hr;
1869 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1871 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1872 struct wined3d_texture *wined3d_texture;
1873 struct d3d8_texture *texture_impl;
1875 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1877 if (!texture)
1878 return D3DERR_INVALIDCALL;
1880 wined3d_mutex_lock();
1881 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1883 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1884 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1885 IDirect3DBaseTexture8_AddRef(*texture);
1887 else
1889 *texture = NULL;
1891 wined3d_mutex_unlock();
1893 return D3D_OK;
1896 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1898 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1899 struct d3d8_texture *texture_impl;
1900 HRESULT hr;
1902 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1904 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1906 wined3d_mutex_lock();
1907 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1908 texture_impl ? texture_impl->wined3d_texture : NULL);
1909 wined3d_mutex_unlock();
1911 return hr;
1914 static const struct tss_lookup
1916 BOOL sampler_state;
1917 union
1919 enum wined3d_texture_stage_state texture_state;
1920 enum wined3d_sampler_state sampler_state;
1921 } u;
1923 tss_lookup[] =
1925 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
1926 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
1927 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
1928 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
1929 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
1930 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
1931 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
1932 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
1933 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
1934 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
1935 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
1936 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
1937 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
1938 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
1939 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
1940 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
1941 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
1942 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
1943 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
1944 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
1945 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
1946 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
1947 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
1948 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
1949 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1950 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
1951 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
1952 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
1953 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
1956 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1957 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1959 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1960 const struct tss_lookup *l;
1962 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1964 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1966 WARN("Invalid Type %#x passed.\n", Type);
1967 return D3D_OK;
1970 l = &tss_lookup[Type];
1972 wined3d_mutex_lock();
1973 if (l->sampler_state)
1974 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
1975 else
1976 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
1977 wined3d_mutex_unlock();
1979 return D3D_OK;
1982 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1983 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1985 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1986 const struct tss_lookup *l;
1988 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1990 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1992 WARN("Invalid type %#x passed.\n", type);
1993 return D3D_OK;
1996 l = &tss_lookup[type];
1998 wined3d_mutex_lock();
1999 if (l->sampler_state)
2000 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
2001 else
2002 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
2003 wined3d_mutex_unlock();
2005 return D3D_OK;
2008 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2010 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2011 HRESULT hr;
2013 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2015 wined3d_mutex_lock();
2016 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2017 wined3d_mutex_unlock();
2019 return hr;
2022 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2023 DWORD info_id, void *info, DWORD info_size)
2025 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
2027 return D3D_OK;
2030 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2031 UINT palette_idx, const PALETTEENTRY *entries)
2033 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2035 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2036 * does not have a d3d8/9-style palette API */
2038 return D3D_OK;
2041 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2042 UINT palette_idx, PALETTEENTRY *entries)
2044 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2046 return D3DERR_INVALIDCALL;
2049 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2051 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2053 return D3D_OK;
2056 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2058 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2060 return D3DERR_INVALIDCALL;
2063 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2064 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2066 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2067 HRESULT hr;
2069 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2070 iface, primitive_type, start_vertex, primitive_count);
2072 wined3d_mutex_lock();
2073 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2074 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2075 vertex_count_from_primitive_count(primitive_type, primitive_count));
2076 wined3d_mutex_unlock();
2078 return hr;
2081 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2082 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2083 UINT start_idx, UINT primitive_count)
2085 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2086 HRESULT hr;
2088 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2089 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2091 wined3d_mutex_lock();
2092 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2093 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2094 vertex_count_from_primitive_count(primitive_type, primitive_count));
2095 wined3d_mutex_unlock();
2097 return hr;
2100 /* The caller is responsible for wined3d locking */
2101 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2103 HRESULT hr;
2105 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2107 UINT size = max(device->vertex_buffer_size * 2, min_size);
2108 struct wined3d_buffer *buffer;
2110 TRACE("Growing vertex buffer to %u bytes\n", size);
2112 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2113 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2114 if (FAILED(hr))
2116 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2117 return hr;
2120 if (device->vertex_buffer)
2121 wined3d_buffer_decref(device->vertex_buffer);
2123 device->vertex_buffer = buffer;
2124 device->vertex_buffer_size = size;
2125 device->vertex_buffer_pos = 0;
2127 return D3D_OK;
2130 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2131 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2132 UINT stride)
2134 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2135 HRESULT hr;
2136 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2137 struct wined3d_map_desc wined3d_map_desc;
2138 struct wined3d_box wined3d_box = {0};
2139 UINT size = vtx_count * stride;
2140 struct wined3d_resource *vb;
2141 UINT vb_pos, align;
2143 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2144 iface, primitive_type, primitive_count, data, stride);
2146 if (!primitive_count)
2148 WARN("primitive_count is 0, returning D3D_OK\n");
2149 return D3D_OK;
2152 wined3d_mutex_lock();
2153 hr = d3d8_device_prepare_vertex_buffer(device, size);
2154 if (FAILED(hr))
2155 goto done;
2157 vb_pos = device->vertex_buffer_pos;
2158 align = vb_pos % stride;
2159 if (align) align = stride - align;
2160 if (vb_pos + size + align > device->vertex_buffer_size)
2161 vb_pos = 0;
2162 else
2163 vb_pos += align;
2165 wined3d_box.left = vb_pos;
2166 wined3d_box.right = vb_pos + size;
2167 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2168 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2169 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2170 goto done;
2171 memcpy(wined3d_map_desc.data, data, size);
2172 wined3d_resource_unmap(vb, 0);
2173 device->vertex_buffer_pos = vb_pos + size;
2175 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2176 if (FAILED(hr))
2177 goto done;
2179 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2180 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2181 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2183 done:
2184 wined3d_mutex_unlock();
2185 return hr;
2188 /* The caller is responsible for wined3d locking */
2189 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2191 HRESULT hr;
2193 if (device->index_buffer_size < min_size || !device->index_buffer)
2195 UINT size = max(device->index_buffer_size * 2, min_size);
2196 struct wined3d_buffer *buffer;
2198 TRACE("Growing index buffer to %u bytes\n", size);
2200 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2201 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2202 if (FAILED(hr))
2204 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2205 return hr;
2208 if (device->index_buffer)
2209 wined3d_buffer_decref(device->index_buffer);
2211 device->index_buffer = buffer;
2212 device->index_buffer_size = size;
2213 device->index_buffer_pos = 0;
2215 return D3D_OK;
2218 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2219 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2220 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2221 const void *vertex_data, UINT vertex_stride)
2223 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2224 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2225 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2226 UINT vtx_size = vertex_count * vertex_stride;
2227 UINT idx_size = idx_count * idx_fmt_size;
2228 struct wined3d_map_desc wined3d_map_desc;
2229 struct wined3d_box wined3d_box = {0};
2230 struct wined3d_resource *ib, *vb;
2231 UINT vb_pos, ib_pos, align;
2232 HRESULT hr;
2234 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2235 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2236 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2237 index_data, index_format, vertex_data, vertex_stride);
2239 if (!primitive_count)
2241 WARN("primitive_count is 0, returning D3D_OK\n");
2242 return D3D_OK;
2245 wined3d_mutex_lock();
2247 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2248 if (FAILED(hr))
2249 goto done;
2251 vb_pos = device->vertex_buffer_pos;
2252 align = vb_pos % vertex_stride;
2253 if (align) align = vertex_stride - align;
2254 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2255 vb_pos = 0;
2256 else
2257 vb_pos += align;
2259 wined3d_box.left = vb_pos;
2260 wined3d_box.right = vb_pos + vtx_size;
2261 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2262 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2263 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2264 goto done;
2265 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2266 wined3d_resource_unmap(vb, 0);
2267 device->vertex_buffer_pos = vb_pos + vtx_size;
2269 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2270 if (FAILED(hr))
2271 goto done;
2273 ib_pos = device->index_buffer_pos;
2274 align = ib_pos % idx_fmt_size;
2275 if (align) align = idx_fmt_size - align;
2276 if (ib_pos + idx_size + align > device->index_buffer_size)
2277 ib_pos = 0;
2278 else
2279 ib_pos += align;
2281 wined3d_box.left = ib_pos;
2282 wined3d_box.right = ib_pos + idx_size;
2283 ib = wined3d_buffer_get_resource(device->index_buffer);
2284 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2285 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2286 goto done;
2287 memcpy(wined3d_map_desc.data, index_data, idx_size);
2288 wined3d_resource_unmap(ib, 0);
2289 device->index_buffer_pos = ib_pos + idx_size;
2291 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2292 if (FAILED(hr))
2293 goto done;
2295 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2296 wined3dformat_from_d3dformat(index_format), 0);
2297 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2299 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2300 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2302 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2303 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2304 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2306 done:
2307 wined3d_mutex_unlock();
2308 return hr;
2311 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2312 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2314 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2315 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2316 HRESULT hr;
2318 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2319 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2321 wined3d_mutex_lock();
2322 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2323 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2324 wined3d_mutex_unlock();
2326 return hr;
2329 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2330 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2332 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2333 struct d3d8_vertex_shader *object;
2334 DWORD shader_handle;
2335 DWORD handle;
2336 HRESULT hr;
2338 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2339 iface, declaration, byte_code, shader, usage);
2341 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2342 if (!object)
2344 *shader = 0;
2345 return E_OUTOFMEMORY;
2348 wined3d_mutex_lock();
2349 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2350 wined3d_mutex_unlock();
2351 if (handle == D3D8_INVALID_HANDLE)
2353 ERR("Failed to allocate vertex shader handle.\n");
2354 HeapFree(GetProcessHeap(), 0, object);
2355 *shader = 0;
2356 return E_OUTOFMEMORY;
2359 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2361 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2362 if (FAILED(hr))
2364 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2365 wined3d_mutex_lock();
2366 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2367 wined3d_mutex_unlock();
2368 HeapFree(GetProcessHeap(), 0, object);
2369 *shader = 0;
2370 return hr;
2373 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2374 *shader = shader_handle;
2376 return D3D_OK;
2379 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2381 struct d3d8_vertex_declaration *d3d8_declaration;
2382 struct FvfToDecl *convertedDecls = device->decls;
2383 int p, low, high; /* deliberately signed */
2384 HRESULT hr;
2386 TRACE("Searching for declaration for fvf %08x... ", fvf);
2388 low = 0;
2389 high = device->numConvertedDecls - 1;
2390 while (low <= high)
2392 p = (low + high) >> 1;
2393 TRACE("%d ", p);
2395 if (convertedDecls[p].fvf == fvf)
2397 TRACE("found %p\n", convertedDecls[p].declaration);
2398 return convertedDecls[p].declaration;
2401 if (convertedDecls[p].fvf < fvf)
2402 low = p + 1;
2403 else
2404 high = p - 1;
2406 TRACE("not found. Creating and inserting at position %d.\n", low);
2408 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2409 return NULL;
2411 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2413 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2414 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2415 return NULL;
2418 if (device->declArraySize == device->numConvertedDecls)
2420 UINT grow = device->declArraySize / 2;
2422 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2423 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2424 if (!convertedDecls)
2426 d3d8_vertex_declaration_destroy(d3d8_declaration);
2427 return NULL;
2429 device->decls = convertedDecls;
2430 device->declArraySize += grow;
2433 memmove(convertedDecls + low + 1, convertedDecls + low,
2434 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2435 convertedDecls[low].declaration = d3d8_declaration;
2436 convertedDecls[low].fvf = fvf;
2437 ++device->numConvertedDecls;
2439 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2441 return d3d8_declaration;
2444 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2446 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2447 struct d3d8_vertex_shader *shader_impl;
2449 TRACE("iface %p, shader %#x.\n", iface, shader);
2451 if (VS_HIGHESTFIXEDFXF >= shader)
2453 TRACE("Setting FVF, %#x\n", shader);
2455 wined3d_mutex_lock();
2456 wined3d_device_set_vertex_declaration(device->wined3d_device,
2457 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2458 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2459 wined3d_mutex_unlock();
2461 return D3D_OK;
2464 TRACE("Setting shader\n");
2466 wined3d_mutex_lock();
2467 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2469 WARN("Invalid handle (%#x) passed.\n", shader);
2470 wined3d_mutex_unlock();
2472 return D3DERR_INVALIDCALL;
2475 wined3d_device_set_vertex_declaration(device->wined3d_device,
2476 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2477 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2478 wined3d_mutex_unlock();
2480 return D3D_OK;
2483 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2485 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2486 struct wined3d_vertex_declaration *wined3d_declaration;
2487 struct d3d8_vertex_declaration *d3d8_declaration;
2489 TRACE("iface %p, shader %p.\n", iface, shader);
2491 wined3d_mutex_lock();
2492 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2494 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2495 *shader = d3d8_declaration->shader_handle;
2497 else
2499 *shader = 0;
2501 wined3d_mutex_unlock();
2503 TRACE("Returning %#x.\n", *shader);
2505 return D3D_OK;
2508 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2510 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2511 struct d3d8_vertex_shader *shader_impl;
2513 TRACE("iface %p, shader %#x.\n", iface, shader);
2515 wined3d_mutex_lock();
2516 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2518 WARN("Invalid handle (%#x) passed.\n", shader);
2519 wined3d_mutex_unlock();
2521 return D3DERR_INVALIDCALL;
2524 if (shader_impl->wined3d_shader
2525 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2526 IDirect3DDevice8_SetVertexShader(iface, 0);
2528 wined3d_mutex_unlock();
2530 d3d8_vertex_shader_destroy(shader_impl);
2532 return D3D_OK;
2535 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2536 DWORD start_register, const void *data, DWORD count)
2538 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2539 HRESULT hr;
2541 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2542 iface, start_register, data, count);
2544 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2546 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2547 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2548 return D3DERR_INVALIDCALL;
2551 wined3d_mutex_lock();
2552 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2553 wined3d_mutex_unlock();
2555 return hr;
2558 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2559 DWORD start_register, void *data, DWORD count)
2561 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2562 HRESULT hr;
2564 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2565 iface, start_register, data, count);
2567 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2569 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2570 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2571 return D3DERR_INVALIDCALL;
2574 wined3d_mutex_lock();
2575 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2576 wined3d_mutex_unlock();
2578 return hr;
2581 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2582 DWORD shader, void *data, DWORD *data_size)
2584 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2585 struct d3d8_vertex_declaration *declaration;
2586 struct d3d8_vertex_shader *shader_impl;
2588 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2589 iface, shader, data, data_size);
2591 wined3d_mutex_lock();
2592 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2593 wined3d_mutex_unlock();
2595 if (!shader_impl)
2597 WARN("Invalid handle (%#x) passed.\n", shader);
2598 return D3DERR_INVALIDCALL;
2600 declaration = shader_impl->vertex_declaration;
2602 if (!data)
2604 *data_size = declaration->elements_size;
2605 return D3D_OK;
2608 /* MSDN claims that if *data_size is smaller than the required size
2609 * we should write the required size and return D3DERR_MOREDATA.
2610 * That's not actually true. */
2611 if (*data_size < declaration->elements_size)
2612 return D3DERR_INVALIDCALL;
2614 memcpy(data, declaration->elements, declaration->elements_size);
2616 return D3D_OK;
2619 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2620 DWORD shader, void *data, DWORD *data_size)
2622 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2623 struct d3d8_vertex_shader *shader_impl = NULL;
2624 HRESULT hr;
2626 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2627 iface, shader, data, data_size);
2629 wined3d_mutex_lock();
2630 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2632 WARN("Invalid handle (%#x) passed.\n", shader);
2633 wined3d_mutex_unlock();
2635 return D3DERR_INVALIDCALL;
2638 if (!shader_impl->wined3d_shader)
2640 wined3d_mutex_unlock();
2641 *data_size = 0;
2642 return D3D_OK;
2645 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2646 wined3d_mutex_unlock();
2648 return hr;
2651 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2652 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2654 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2655 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2657 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2659 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2660 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2661 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2662 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2663 * problem)
2665 wined3d_mutex_lock();
2666 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2667 wined3d_device_set_index_buffer(device->wined3d_device,
2668 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2669 wined3d_mutex_unlock();
2671 return D3D_OK;
2674 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2675 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2677 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2678 enum wined3d_format_id wined3d_format;
2679 struct wined3d_buffer *wined3d_buffer;
2680 struct d3d8_indexbuffer *buffer_impl;
2682 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2684 if (!buffer)
2685 return D3DERR_INVALIDCALL;
2687 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2688 wined3d_mutex_lock();
2689 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2690 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2692 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2693 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2694 IDirect3DIndexBuffer8_AddRef(*buffer);
2696 else
2698 *buffer = NULL;
2700 wined3d_mutex_unlock();
2702 return D3D_OK;
2705 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2706 const DWORD *byte_code, DWORD *shader)
2708 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2709 struct d3d8_pixel_shader *object;
2710 DWORD shader_handle;
2711 DWORD handle;
2712 HRESULT hr;
2714 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2716 if (!shader)
2717 return D3DERR_INVALIDCALL;
2719 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2720 if (!object)
2721 return E_OUTOFMEMORY;
2723 wined3d_mutex_lock();
2724 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2725 wined3d_mutex_unlock();
2726 if (handle == D3D8_INVALID_HANDLE)
2728 ERR("Failed to allocate pixel shader handle.\n");
2729 HeapFree(GetProcessHeap(), 0, object);
2730 return E_OUTOFMEMORY;
2733 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2735 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2736 if (FAILED(hr))
2738 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2739 wined3d_mutex_lock();
2740 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2741 wined3d_mutex_unlock();
2742 HeapFree(GetProcessHeap(), 0, object);
2743 *shader = 0;
2744 return hr;
2747 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2748 *shader = shader_handle;
2750 return D3D_OK;
2753 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2755 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2756 struct d3d8_pixel_shader *shader_impl;
2758 TRACE("iface %p, shader %#x.\n", iface, shader);
2760 wined3d_mutex_lock();
2762 if (!shader)
2764 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2765 wined3d_mutex_unlock();
2766 return D3D_OK;
2769 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2771 WARN("Invalid handle (%#x) passed.\n", shader);
2772 wined3d_mutex_unlock();
2773 return D3DERR_INVALIDCALL;
2776 TRACE("Setting shader %p.\n", shader_impl);
2777 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2778 wined3d_mutex_unlock();
2780 return D3D_OK;
2783 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2785 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2786 struct wined3d_shader *object;
2788 TRACE("iface %p, shader %p.\n", iface, shader);
2790 if (!shader)
2791 return D3DERR_INVALIDCALL;
2793 wined3d_mutex_lock();
2794 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2796 struct d3d8_pixel_shader *d3d8_shader;
2797 d3d8_shader = wined3d_shader_get_parent(object);
2798 *shader = d3d8_shader->handle;
2800 else
2802 *shader = 0;
2804 wined3d_mutex_unlock();
2806 TRACE("Returning %#x.\n", *shader);
2808 return D3D_OK;
2811 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2813 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2814 struct d3d8_pixel_shader *shader_impl;
2816 TRACE("iface %p, shader %#x.\n", iface, shader);
2818 wined3d_mutex_lock();
2820 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2822 WARN("Invalid handle (%#x) passed.\n", shader);
2823 wined3d_mutex_unlock();
2824 return D3DERR_INVALIDCALL;
2827 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2828 IDirect3DDevice8_SetPixelShader(iface, 0);
2830 wined3d_mutex_unlock();
2832 d3d8_pixel_shader_destroy(shader_impl);
2834 return D3D_OK;
2837 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2838 DWORD start_register, const void *data, DWORD count)
2840 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2841 HRESULT hr;
2843 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2844 iface, start_register, data, count);
2846 wined3d_mutex_lock();
2847 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
2848 wined3d_mutex_unlock();
2850 return hr;
2853 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2854 DWORD start_register, void *data, DWORD count)
2856 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2857 HRESULT hr;
2859 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2860 iface, start_register, data, count);
2862 wined3d_mutex_lock();
2863 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
2864 wined3d_mutex_unlock();
2866 return hr;
2869 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2870 DWORD shader, void *data, DWORD *data_size)
2872 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2873 struct d3d8_pixel_shader *shader_impl = NULL;
2874 HRESULT hr;
2876 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2877 iface, shader, data, data_size);
2879 wined3d_mutex_lock();
2880 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2882 WARN("Invalid handle (%#x) passed.\n", shader);
2883 wined3d_mutex_unlock();
2885 return D3DERR_INVALIDCALL;
2888 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2889 wined3d_mutex_unlock();
2891 return hr;
2894 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2895 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2897 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2898 iface, handle, segment_count, patch_info);
2899 return D3D_OK;
2902 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2903 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2905 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2906 iface, handle, segment_count, patch_info);
2907 return D3D_OK;
2910 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2912 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2913 return D3DERR_INVALIDCALL;
2916 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2917 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2919 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2920 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2921 HRESULT hr;
2923 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2924 iface, stream_idx, buffer, stride);
2926 wined3d_mutex_lock();
2927 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2928 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2929 wined3d_mutex_unlock();
2931 return hr;
2934 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2935 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2937 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2938 struct d3d8_vertexbuffer *buffer_impl;
2939 struct wined3d_buffer *wined3d_buffer = NULL;
2940 HRESULT hr;
2942 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2943 iface, stream_idx, buffer, stride);
2945 if (!buffer)
2946 return D3DERR_INVALIDCALL;
2948 wined3d_mutex_lock();
2949 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2950 if (SUCCEEDED(hr) && wined3d_buffer)
2952 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2953 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2954 IDirect3DVertexBuffer8_AddRef(*buffer);
2956 else
2958 if (FAILED(hr))
2959 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2960 *buffer = NULL;
2962 wined3d_mutex_unlock();
2964 return hr;
2967 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2969 d3d8_device_QueryInterface,
2970 d3d8_device_AddRef,
2971 d3d8_device_Release,
2972 d3d8_device_TestCooperativeLevel,
2973 d3d8_device_GetAvailableTextureMem,
2974 d3d8_device_ResourceManagerDiscardBytes,
2975 d3d8_device_GetDirect3D,
2976 d3d8_device_GetDeviceCaps,
2977 d3d8_device_GetDisplayMode,
2978 d3d8_device_GetCreationParameters,
2979 d3d8_device_SetCursorProperties,
2980 d3d8_device_SetCursorPosition,
2981 d3d8_device_ShowCursor,
2982 d3d8_device_CreateAdditionalSwapChain,
2983 d3d8_device_Reset,
2984 d3d8_device_Present,
2985 d3d8_device_GetBackBuffer,
2986 d3d8_device_GetRasterStatus,
2987 d3d8_device_SetGammaRamp,
2988 d3d8_device_GetGammaRamp,
2989 d3d8_device_CreateTexture,
2990 d3d8_device_CreateVolumeTexture,
2991 d3d8_device_CreateCubeTexture,
2992 d3d8_device_CreateVertexBuffer,
2993 d3d8_device_CreateIndexBuffer,
2994 d3d8_device_CreateRenderTarget,
2995 d3d8_device_CreateDepthStencilSurface,
2996 d3d8_device_CreateImageSurface,
2997 d3d8_device_CopyRects,
2998 d3d8_device_UpdateTexture,
2999 d3d8_device_GetFrontBuffer,
3000 d3d8_device_SetRenderTarget,
3001 d3d8_device_GetRenderTarget,
3002 d3d8_device_GetDepthStencilSurface,
3003 d3d8_device_BeginScene,
3004 d3d8_device_EndScene,
3005 d3d8_device_Clear,
3006 d3d8_device_SetTransform,
3007 d3d8_device_GetTransform,
3008 d3d8_device_MultiplyTransform,
3009 d3d8_device_SetViewport,
3010 d3d8_device_GetViewport,
3011 d3d8_device_SetMaterial,
3012 d3d8_device_GetMaterial,
3013 d3d8_device_SetLight,
3014 d3d8_device_GetLight,
3015 d3d8_device_LightEnable,
3016 d3d8_device_GetLightEnable,
3017 d3d8_device_SetClipPlane,
3018 d3d8_device_GetClipPlane,
3019 d3d8_device_SetRenderState,
3020 d3d8_device_GetRenderState,
3021 d3d8_device_BeginStateBlock,
3022 d3d8_device_EndStateBlock,
3023 d3d8_device_ApplyStateBlock,
3024 d3d8_device_CaptureStateBlock,
3025 d3d8_device_DeleteStateBlock,
3026 d3d8_device_CreateStateBlock,
3027 d3d8_device_SetClipStatus,
3028 d3d8_device_GetClipStatus,
3029 d3d8_device_GetTexture,
3030 d3d8_device_SetTexture,
3031 d3d8_device_GetTextureStageState,
3032 d3d8_device_SetTextureStageState,
3033 d3d8_device_ValidateDevice,
3034 d3d8_device_GetInfo,
3035 d3d8_device_SetPaletteEntries,
3036 d3d8_device_GetPaletteEntries,
3037 d3d8_device_SetCurrentTexturePalette,
3038 d3d8_device_GetCurrentTexturePalette,
3039 d3d8_device_DrawPrimitive,
3040 d3d8_device_DrawIndexedPrimitive,
3041 d3d8_device_DrawPrimitiveUP,
3042 d3d8_device_DrawIndexedPrimitiveUP,
3043 d3d8_device_ProcessVertices,
3044 d3d8_device_CreateVertexShader,
3045 d3d8_device_SetVertexShader,
3046 d3d8_device_GetVertexShader,
3047 d3d8_device_DeleteVertexShader,
3048 d3d8_device_SetVertexShaderConstant,
3049 d3d8_device_GetVertexShaderConstant,
3050 d3d8_device_GetVertexShaderDeclaration,
3051 d3d8_device_GetVertexShaderFunction,
3052 d3d8_device_SetStreamSource,
3053 d3d8_device_GetStreamSource,
3054 d3d8_device_SetIndices,
3055 d3d8_device_GetIndices,
3056 d3d8_device_CreatePixelShader,
3057 d3d8_device_SetPixelShader,
3058 d3d8_device_GetPixelShader,
3059 d3d8_device_DeletePixelShader,
3060 d3d8_device_SetPixelShaderConstant,
3061 d3d8_device_GetPixelShaderConstant,
3062 d3d8_device_GetPixelShaderFunction,
3063 d3d8_device_DrawRectPatch,
3064 d3d8_device_DrawTriPatch,
3065 d3d8_device_DeletePatch,
3068 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3070 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3073 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3074 struct wined3d_device *device)
3076 TRACE("device_parent %p, device %p\n", device_parent, device);
3079 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3081 TRACE("device_parent %p.\n", device_parent);
3084 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3086 struct d3d8_device *device = device_from_device_parent(device_parent);
3088 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3090 if (!activate)
3091 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3092 else
3093 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3096 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3097 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3098 void **parent, const struct wined3d_parent_ops **parent_ops)
3100 struct d3d8_surface *d3d_surface;
3102 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3103 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3105 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3106 return E_OUTOFMEMORY;
3108 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3109 *parent = d3d_surface;
3110 TRACE("Created surface %p.\n", d3d_surface);
3112 return D3D_OK;
3115 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3116 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3117 void **parent, const struct wined3d_parent_ops **parent_ops)
3119 struct d3d8_volume *d3d_volume;
3121 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3122 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3124 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3125 return E_OUTOFMEMORY;
3127 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3128 *parent = d3d_volume;
3129 TRACE("Created volume %p.\n", d3d_volume);
3131 return D3D_OK;
3134 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3135 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3136 struct wined3d_texture **texture)
3138 struct d3d8_device *device = device_from_device_parent(device_parent);
3139 struct d3d8_surface *d3d_surface;
3140 HRESULT hr;
3142 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3143 device_parent, container_parent, desc, texture_flags, texture);
3145 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3146 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface,
3147 &d3d8_null_wined3d_parent_ops, texture)))
3149 WARN("Failed to create texture, hr %#x.\n", hr);
3150 return hr;
3153 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3154 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3156 return hr;
3159 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3160 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3162 struct d3d8_device *device = device_from_device_parent(device_parent);
3163 struct d3d8_swapchain *d3d_swapchain;
3164 HRESULT hr;
3166 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3168 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3170 WARN("Failed to create swapchain, hr %#x.\n", hr);
3171 *swapchain = NULL;
3172 return hr;
3175 *swapchain = d3d_swapchain->wined3d_swapchain;
3176 wined3d_swapchain_incref(*swapchain);
3177 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3179 return hr;
3182 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3184 device_parent_wined3d_device_created,
3185 device_parent_mode_changed,
3186 device_parent_activate,
3187 device_parent_surface_created,
3188 device_parent_volume_created,
3189 device_parent_create_swapchain_texture,
3190 device_parent_create_swapchain,
3193 static void setup_fpu(void)
3195 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3196 WORD cw;
3197 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3198 cw = (cw & ~0xf3f) | 0x3f;
3199 __asm__ volatile ("fldcw %0" : : "m" (cw));
3200 #elif defined(__i386__) && defined(_MSC_VER)
3201 WORD cw;
3202 __asm fnstcw cw;
3203 cw = (cw & ~0xf3f) | 0x3f;
3204 __asm fldcw cw;
3205 #else
3206 FIXME("FPU setup not implemented for this platform.\n");
3207 #endif
3210 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3211 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3213 struct wined3d_swapchain_desc swapchain_desc;
3214 struct wined3d_swapchain *wined3d_swapchain;
3215 HRESULT hr;
3217 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3218 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3219 device->ref = 1;
3220 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3221 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3222 if (!device->handle_table.entries)
3224 ERR("Failed to allocate handle table memory.\n");
3225 return E_OUTOFMEMORY;
3227 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3229 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3231 wined3d_mutex_lock();
3232 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3233 &device->device_parent, &device->wined3d_device);
3234 if (FAILED(hr))
3236 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3237 wined3d_mutex_unlock();
3238 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3239 return hr;
3242 if (!parameters->Windowed)
3244 HWND device_window = parameters->hDeviceWindow;
3246 if (!focus_window)
3247 focus_window = device_window;
3248 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3250 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3251 wined3d_device_decref(device->wined3d_device);
3252 wined3d_mutex_unlock();
3253 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3254 return hr;
3257 if (!device_window)
3258 device_window = focus_window;
3259 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3260 parameters->BackBufferWidth,
3261 parameters->BackBufferHeight);
3264 if (flags & D3DCREATE_MULTITHREADED)
3265 wined3d_device_set_multithreaded(device->wined3d_device);
3267 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3269 wined3d_device_release_focus_window(device->wined3d_device);
3270 wined3d_device_decref(device->wined3d_device);
3271 wined3d_mutex_unlock();
3272 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3273 return D3DERR_INVALIDCALL;
3276 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc)))
3278 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3279 wined3d_device_release_focus_window(device->wined3d_device);
3280 wined3d_device_decref(device->wined3d_device);
3281 wined3d_mutex_unlock();
3282 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3283 return hr;
3286 wined3d_device_set_render_state(device->wined3d_device,
3287 WINED3D_RS_ZENABLE, !!swapchain_desc.enable_auto_depth_stencil);
3288 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3289 wined3d_mutex_unlock();
3291 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3293 device->declArraySize = 16;
3294 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3295 if (!device->decls)
3297 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3298 hr = E_OUTOFMEMORY;
3299 goto err;
3302 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3303 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3305 device->d3d_parent = &parent->IDirect3D8_iface;
3306 IDirect3D8_AddRef(device->d3d_parent);
3308 return D3D_OK;
3310 err:
3311 wined3d_mutex_lock();
3312 wined3d_device_uninit_3d(device->wined3d_device);
3313 wined3d_device_release_focus_window(device->wined3d_device);
3314 wined3d_device_decref(device->wined3d_device);
3315 wined3d_mutex_unlock();
3316 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3317 return hr;