wbemdisp: Implement ISWbemPropertySet::Item.
[wine.git] / dlls / d3d8 / device.c
blobe255392c2617226baceae380ce4f3aea276e502c
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 device->device_state = D3D8_DEVICE_STATE_OK;
752 else
754 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
756 wined3d_mutex_unlock();
758 return hr;
761 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
762 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
764 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
766 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
767 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
769 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
770 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
771 * shows a framerate on Windows in applications that only call the device
772 * method, like e.g. the dx8 sdk samples. The conclusion is that native
773 * calls the swapchain's public method from the device. */
774 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
775 src_rect, dst_rect, dst_window_override, dirty_region);
778 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
779 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
781 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
782 struct wined3d_swapchain *wined3d_swapchain;
783 struct wined3d_texture *wined3d_texture;
784 struct d3d8_surface *surface_impl;
786 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
787 iface, backbuffer_idx, backbuffer_type, backbuffer);
789 /* backbuffer_type is ignored by native. */
791 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
792 wined3d_mutex_lock();
794 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
795 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
797 wined3d_mutex_unlock();
798 *backbuffer = NULL;
799 return D3DERR_INVALIDCALL;
802 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
803 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
804 IDirect3DSurface8_AddRef(*backbuffer);
806 wined3d_mutex_unlock();
807 return D3D_OK;
810 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
812 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
813 HRESULT hr;
815 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
817 wined3d_mutex_lock();
818 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
819 wined3d_mutex_unlock();
821 return hr;
824 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
826 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
828 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
830 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
831 wined3d_mutex_lock();
832 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
833 wined3d_mutex_unlock();
836 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
838 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
840 TRACE("iface %p, ramp %p.\n", iface, ramp);
842 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
843 wined3d_mutex_lock();
844 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
845 wined3d_mutex_unlock();
848 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
849 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
850 D3DPOOL pool, IDirect3DTexture8 **texture)
852 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
853 struct d3d8_texture *object;
854 HRESULT hr;
856 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
857 iface, width, height, levels, usage, format, pool, texture);
859 if (!format)
860 return D3DERR_INVALIDCALL;
862 *texture = NULL;
863 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
864 if (!object)
865 return D3DERR_OUTOFVIDEOMEMORY;
867 hr = texture_init(object, device, width, height, levels, usage, format, pool);
868 if (FAILED(hr))
870 WARN("Failed to initialize texture, hr %#x.\n", hr);
871 HeapFree(GetProcessHeap(), 0, object);
872 return hr;
875 TRACE("Created texture %p.\n", object);
876 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
878 return D3D_OK;
881 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
882 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
883 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
885 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
886 struct d3d8_texture *object;
887 HRESULT hr;
889 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
890 iface, width, height, depth, levels, usage, format, pool, texture);
892 if (!format)
893 return D3DERR_INVALIDCALL;
895 *texture = NULL;
896 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
897 if (!object)
898 return D3DERR_OUTOFVIDEOMEMORY;
900 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
901 if (FAILED(hr))
903 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
904 HeapFree(GetProcessHeap(), 0, object);
905 return hr;
908 TRACE("Created volume texture %p.\n", object);
909 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
911 return D3D_OK;
914 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
915 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
917 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
918 struct d3d8_texture *object;
919 HRESULT hr;
921 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
922 iface, edge_length, levels, usage, format, pool, texture);
924 if (!format)
925 return D3DERR_INVALIDCALL;
927 *texture = NULL;
928 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
929 if (!object)
930 return D3DERR_OUTOFVIDEOMEMORY;
932 hr = cubetexture_init(object, device, edge_length, levels, usage, format, pool);
933 if (FAILED(hr))
935 WARN("Failed to initialize cube texture, hr %#x.\n", hr);
936 HeapFree(GetProcessHeap(), 0, object);
937 return hr;
940 TRACE("Created cube texture %p.\n", object);
941 *texture = (IDirect3DCubeTexture8 *)&object->IDirect3DBaseTexture8_iface;
943 return hr;
946 static HRESULT WINAPI d3d8_device_CreateVertexBuffer(IDirect3DDevice8 *iface, UINT size,
947 DWORD usage, DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer8 **buffer)
949 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
950 struct d3d8_vertexbuffer *object;
951 HRESULT hr;
953 TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p.\n",
954 iface, size, usage, fvf, pool, buffer);
956 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
957 if (!object)
958 return D3DERR_OUTOFVIDEOMEMORY;
960 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
961 if (FAILED(hr))
963 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
964 HeapFree(GetProcessHeap(), 0, object);
965 return hr;
968 TRACE("Created vertex buffer %p.\n", object);
969 *buffer = &object->IDirect3DVertexBuffer8_iface;
971 return D3D_OK;
974 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
975 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
977 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
978 struct d3d8_indexbuffer *object;
979 HRESULT hr;
981 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
982 iface, size, usage, format, pool, buffer);
984 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
985 if (!object)
986 return D3DERR_OUTOFVIDEOMEMORY;
988 hr = indexbuffer_init(object, device, size, usage, format, pool);
989 if (FAILED(hr))
991 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
992 HeapFree(GetProcessHeap(), 0, object);
993 return hr;
996 TRACE("Created index buffer %p.\n", object);
997 *buffer = &object->IDirect3DIndexBuffer8_iface;
999 return D3D_OK;
1002 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
1003 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
1004 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
1006 struct wined3d_resource_desc desc;
1007 struct d3d8_surface *surface_impl;
1008 struct wined3d_texture *texture;
1009 HRESULT hr;
1011 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p, "
1012 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1013 device, width, height, format, flags, surface,
1014 usage, pool, multisample_type, multisample_quality);
1016 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1017 desc.format = wined3dformat_from_d3dformat(format);
1018 desc.multisample_type = multisample_type;
1019 desc.multisample_quality = multisample_quality;
1020 desc.usage = usage & WINED3DUSAGE_MASK;
1021 desc.pool = pool;
1022 desc.width = width;
1023 desc.height = height;
1024 desc.depth = 1;
1025 desc.size = 0;
1027 wined3d_mutex_lock();
1029 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, &desc,
1030 1, 1, flags, NULL, NULL, &d3d8_null_wined3d_parent_ops, &texture)))
1032 wined3d_mutex_unlock();
1033 WARN("Failed to create texture, hr %#x.\n", hr);
1034 return hr;
1037 surface_impl = wined3d_texture_get_sub_resource_parent(texture, 0);
1038 surface_impl->parent_device = &device->IDirect3DDevice8_iface;
1039 *surface = &surface_impl->IDirect3DSurface8_iface;
1040 IDirect3DSurface8_AddRef(*surface);
1041 wined3d_texture_decref(texture);
1043 wined3d_mutex_unlock();
1045 return D3D_OK;
1048 static HRESULT WINAPI d3d8_device_CreateRenderTarget(IDirect3DDevice8 *iface, UINT width,
1049 UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, BOOL lockable,
1050 IDirect3DSurface8 **surface)
1052 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1053 DWORD flags = 0;
1055 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, lockable %#x, surface %p.\n",
1056 iface, width, height, format, multisample_type, lockable, surface);
1058 if (!format)
1059 return D3DERR_INVALIDCALL;
1061 *surface = NULL;
1062 if (lockable)
1063 flags |= WINED3D_TEXTURE_CREATE_MAPPABLE;
1065 return d3d8_device_create_surface(device, width, height, format, flags, surface,
1066 D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, multisample_type, 0);
1069 static HRESULT WINAPI d3d8_device_CreateDepthStencilSurface(IDirect3DDevice8 *iface,
1070 UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type,
1071 IDirect3DSurface8 **surface)
1073 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1075 TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, surface %p.\n",
1076 iface, width, height, format, multisample_type, surface);
1078 if (!format)
1079 return D3DERR_INVALIDCALL;
1081 *surface = NULL;
1083 /* TODO: Verify that Discard is false */
1084 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1085 surface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, multisample_type, 0);
1088 /* IDirect3DDevice8Impl::CreateImageSurface returns surface with pool type SYSTEMMEM */
1089 static HRESULT WINAPI d3d8_device_CreateImageSurface(IDirect3DDevice8 *iface, UINT width,
1090 UINT height, D3DFORMAT format, IDirect3DSurface8 **surface)
1092 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1094 TRACE("iface %p, width %u, height %u, format %#x, surface %p.\n",
1095 iface, width, height, format, surface);
1097 *surface = NULL;
1099 return d3d8_device_create_surface(device, width, height, format, WINED3D_TEXTURE_CREATE_MAPPABLE,
1100 surface, 0, D3DPOOL_SYSTEMMEM, D3DMULTISAMPLE_NONE, 0);
1103 static HRESULT WINAPI d3d8_device_CopyRects(IDirect3DDevice8 *iface,
1104 IDirect3DSurface8 *src_surface, const RECT *src_rects, UINT rect_count,
1105 IDirect3DSurface8 *dst_surface, const POINT *dst_points)
1107 struct d3d8_surface *src = unsafe_impl_from_IDirect3DSurface8(src_surface);
1108 struct d3d8_surface *dst = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1109 enum wined3d_format_id src_format, dst_format;
1110 struct wined3d_sub_resource_desc wined3d_desc;
1111 UINT src_w, src_h;
1113 TRACE("iface %p, src_surface %p, src_rects %p, rect_count %u, dst_surface %p, dst_points %p.\n",
1114 iface, src_surface, src_rects, rect_count, dst_surface, dst_points);
1116 /* Check that the source texture is in WINED3D_POOL_SYSTEM_MEM and the
1117 * destination texture is in WINED3D_POOL_DEFAULT. */
1119 wined3d_mutex_lock();
1120 wined3d_texture_get_sub_resource_desc(src->wined3d_texture, src->sub_resource_idx, &wined3d_desc);
1121 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1123 WARN("Source %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", src_surface);
1124 wined3d_mutex_unlock();
1125 return D3DERR_INVALIDCALL;
1127 src_format = wined3d_desc.format;
1128 src_w = wined3d_desc.width;
1129 src_h = wined3d_desc.height;
1131 wined3d_texture_get_sub_resource_desc(dst->wined3d_texture, dst->sub_resource_idx, &wined3d_desc);
1132 if (wined3d_desc.usage & WINED3DUSAGE_DEPTHSTENCIL)
1134 WARN("Destination %p is a depth stencil surface, returning D3DERR_INVALIDCALL.\n", dst_surface);
1135 wined3d_mutex_unlock();
1136 return D3DERR_INVALIDCALL;
1138 dst_format = wined3d_desc.format;
1140 /* Check that the source and destination formats match */
1141 if (src_format != dst_format)
1143 WARN("Source %p format must match the destination %p format, returning D3DERR_INVALIDCALL.\n",
1144 src_surface, dst_surface);
1145 wined3d_mutex_unlock();
1146 return D3DERR_INVALIDCALL;
1149 /* Quick if complete copy ... */
1150 if (!rect_count && !src_rects && !dst_points)
1152 RECT rect = {0, 0, src_w, src_h};
1153 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &rect,
1154 src->wined3d_texture, src->sub_resource_idx, &rect, 0, NULL, WINED3D_TEXF_POINT);
1156 else
1158 unsigned int i;
1159 /* Copy rect by rect */
1160 if (src_rects && dst_points)
1162 for (i = 0; i < rect_count; ++i)
1164 UINT w = src_rects[i].right - src_rects[i].left;
1165 UINT h = src_rects[i].bottom - src_rects[i].top;
1166 RECT dst_rect = {dst_points[i].x, dst_points[i].y,
1167 dst_points[i].x + w, dst_points[i].y + h};
1169 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1170 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1173 else
1175 for (i = 0; i < rect_count; ++i)
1177 UINT w = src_rects[i].right - src_rects[i].left;
1178 UINT h = src_rects[i].bottom - src_rects[i].top;
1179 RECT dst_rect = {0, 0, w, h};
1181 wined3d_texture_blt(dst->wined3d_texture, dst->sub_resource_idx, &dst_rect,
1182 src->wined3d_texture, src->sub_resource_idx, &src_rects[i], 0, NULL, WINED3D_TEXF_POINT);
1186 wined3d_mutex_unlock();
1188 return WINED3D_OK;
1191 static HRESULT WINAPI d3d8_device_UpdateTexture(IDirect3DDevice8 *iface,
1192 IDirect3DBaseTexture8 *src_texture, IDirect3DBaseTexture8 *dst_texture)
1194 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1195 struct d3d8_texture *src_impl, *dst_impl;
1196 HRESULT hr;
1198 TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, src_texture, dst_texture);
1200 src_impl = unsafe_impl_from_IDirect3DBaseTexture8(src_texture);
1201 dst_impl = unsafe_impl_from_IDirect3DBaseTexture8(dst_texture);
1203 wined3d_mutex_lock();
1204 hr = wined3d_device_update_texture(device->wined3d_device,
1205 src_impl->wined3d_texture, dst_impl->wined3d_texture);
1206 wined3d_mutex_unlock();
1208 return hr;
1211 static HRESULT WINAPI d3d8_device_GetFrontBuffer(IDirect3DDevice8 *iface, IDirect3DSurface8 *dst_surface)
1213 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1214 struct d3d8_surface *dst_impl = unsafe_impl_from_IDirect3DSurface8(dst_surface);
1215 HRESULT hr;
1217 TRACE("iface %p, dst_surface %p.\n", iface, dst_surface);
1219 if (!dst_surface)
1221 WARN("Invalid destination surface passed.\n");
1222 return D3DERR_INVALIDCALL;
1225 wined3d_mutex_lock();
1226 hr = wined3d_swapchain_get_front_buffer_data(device->implicit_swapchain->wined3d_swapchain,
1227 dst_impl->wined3d_texture, dst_impl->sub_resource_idx);
1228 wined3d_mutex_unlock();
1230 return hr;
1233 static HRESULT WINAPI d3d8_device_SetRenderTarget(IDirect3DDevice8 *iface,
1234 IDirect3DSurface8 *render_target, IDirect3DSurface8 *depth_stencil)
1236 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1237 struct d3d8_surface *rt_impl = unsafe_impl_from_IDirect3DSurface8(render_target);
1238 struct d3d8_surface *ds_impl = unsafe_impl_from_IDirect3DSurface8(depth_stencil);
1239 struct wined3d_rendertarget_view *original_dsv, *rtv;
1240 HRESULT hr = D3D_OK;
1242 TRACE("iface %p, render_target %p, depth_stencil %p.\n", iface, render_target, depth_stencil);
1244 if (rt_impl && d3d8_surface_get_device(rt_impl) != device)
1246 WARN("Render target surface does not match device.\n");
1247 return D3DERR_INVALIDCALL;
1250 wined3d_mutex_lock();
1252 if (ds_impl)
1254 struct wined3d_sub_resource_desc ds_desc, rt_desc;
1255 struct wined3d_rendertarget_view *original_rtv;
1256 struct d3d8_surface *original_surface;
1258 /* If no render target is passed in check the size against the current RT */
1259 if (!render_target)
1261 if (!(original_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1263 wined3d_mutex_unlock();
1264 return D3DERR_NOTFOUND;
1266 original_surface = wined3d_rendertarget_view_get_sub_resource_parent(original_rtv);
1267 wined3d_texture_get_sub_resource_desc(original_surface->wined3d_texture,
1268 original_surface->sub_resource_idx, &rt_desc);
1270 else
1271 wined3d_texture_get_sub_resource_desc(rt_impl->wined3d_texture,
1272 rt_impl->sub_resource_idx, &rt_desc);
1274 wined3d_texture_get_sub_resource_desc(ds_impl->wined3d_texture, ds_impl->sub_resource_idx, &ds_desc);
1276 if (ds_desc.width < rt_desc.width || ds_desc.height < rt_desc.height)
1278 WARN("Depth stencil is smaller than the render target, returning D3DERR_INVALIDCALL\n");
1279 wined3d_mutex_unlock();
1280 return D3DERR_INVALIDCALL;
1282 if (ds_desc.multisample_type != rt_desc.multisample_type
1283 || ds_desc.multisample_quality != rt_desc.multisample_quality)
1285 WARN("Multisample settings do not match, returning D3DERR_INVALIDCALL\n");
1286 wined3d_mutex_unlock();
1287 return D3DERR_INVALIDCALL;
1291 original_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device);
1292 rtv = ds_impl ? d3d8_surface_acquire_rendertarget_view(ds_impl) : NULL;
1293 wined3d_device_set_depth_stencil_view(device->wined3d_device, rtv);
1294 d3d8_surface_release_rendertarget_view(ds_impl, rtv);
1295 rtv = render_target ? d3d8_surface_acquire_rendertarget_view(rt_impl) : NULL;
1296 if (render_target && FAILED(hr = wined3d_device_set_rendertarget_view(device->wined3d_device, 0, rtv, TRUE)))
1297 wined3d_device_set_depth_stencil_view(device->wined3d_device, original_dsv);
1298 d3d8_surface_release_rendertarget_view(rt_impl, rtv);
1300 wined3d_mutex_unlock();
1302 return hr;
1305 static HRESULT WINAPI d3d8_device_GetRenderTarget(IDirect3DDevice8 *iface, IDirect3DSurface8 **render_target)
1307 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1308 struct wined3d_rendertarget_view *wined3d_rtv;
1309 struct d3d8_surface *surface_impl;
1310 HRESULT hr;
1312 TRACE("iface %p, render_target %p.\n", iface, render_target);
1314 if (!render_target)
1315 return D3DERR_INVALIDCALL;
1317 wined3d_mutex_lock();
1318 if ((wined3d_rtv = wined3d_device_get_rendertarget_view(device->wined3d_device, 0)))
1320 /* We want the sub resource parent here, since the view itself may be
1321 * internal to wined3d and may not have a parent. */
1322 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_rtv);
1323 *render_target = &surface_impl->IDirect3DSurface8_iface;
1324 IDirect3DSurface8_AddRef(*render_target);
1325 hr = D3D_OK;
1327 else
1329 ERR("Failed to get wined3d render target.\n");
1330 *render_target = NULL;
1331 hr = D3DERR_NOTFOUND;
1333 wined3d_mutex_unlock();
1335 return hr;
1338 static HRESULT WINAPI d3d8_device_GetDepthStencilSurface(IDirect3DDevice8 *iface, IDirect3DSurface8 **depth_stencil)
1340 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1341 struct wined3d_rendertarget_view *wined3d_dsv;
1342 struct d3d8_surface *surface_impl;
1343 HRESULT hr = D3D_OK;
1345 TRACE("iface %p, depth_stencil %p.\n", iface, depth_stencil);
1347 if (!depth_stencil)
1348 return D3DERR_INVALIDCALL;
1350 wined3d_mutex_lock();
1351 if ((wined3d_dsv = wined3d_device_get_depth_stencil_view(device->wined3d_device)))
1353 /* We want the sub resource parent here, since the view itself may be
1354 * internal to wined3d and may not have a parent. */
1355 surface_impl = wined3d_rendertarget_view_get_sub_resource_parent(wined3d_dsv);
1356 *depth_stencil = &surface_impl->IDirect3DSurface8_iface;
1357 IDirect3DSurface8_AddRef(*depth_stencil);
1359 else
1361 hr = D3DERR_NOTFOUND;
1362 *depth_stencil = NULL;
1364 wined3d_mutex_unlock();
1366 return hr;
1369 static HRESULT WINAPI d3d8_device_BeginScene(IDirect3DDevice8 *iface)
1371 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1372 HRESULT hr;
1374 TRACE("iface %p.\n", iface);
1376 wined3d_mutex_lock();
1377 hr = wined3d_device_begin_scene(device->wined3d_device);
1378 wined3d_mutex_unlock();
1380 return hr;
1383 static HRESULT WINAPI DECLSPEC_HOTPATCH d3d8_device_EndScene(IDirect3DDevice8 *iface)
1385 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1386 HRESULT hr;
1388 TRACE("iface %p.\n", iface);
1390 wined3d_mutex_lock();
1391 hr = wined3d_device_end_scene(device->wined3d_device);
1392 wined3d_mutex_unlock();
1394 return hr;
1397 static HRESULT WINAPI d3d8_device_Clear(IDirect3DDevice8 *iface, DWORD rect_count,
1398 const D3DRECT *rects, DWORD flags, D3DCOLOR color, float z, DWORD stencil)
1400 const struct wined3d_color c =
1402 ((color >> 16) & 0xff) / 255.0f,
1403 ((color >> 8) & 0xff) / 255.0f,
1404 (color & 0xff) / 255.0f,
1405 ((color >> 24) & 0xff) / 255.0f,
1407 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1408 HRESULT hr;
1410 TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1411 iface, rect_count, rects, flags, color, z, stencil);
1413 if (rect_count && !rects)
1415 WARN("count %u with NULL rects.\n", rect_count);
1416 rect_count = 0;
1419 wined3d_mutex_lock();
1420 hr = wined3d_device_clear(device->wined3d_device, rect_count, (const RECT *)rects, flags, &c, z, stencil);
1421 wined3d_mutex_unlock();
1423 return hr;
1426 static HRESULT WINAPI d3d8_device_SetTransform(IDirect3DDevice8 *iface,
1427 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1429 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1431 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1433 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1434 wined3d_mutex_lock();
1435 wined3d_device_set_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1436 wined3d_mutex_unlock();
1438 return D3D_OK;
1441 static HRESULT WINAPI d3d8_device_GetTransform(IDirect3DDevice8 *iface,
1442 D3DTRANSFORMSTATETYPE state, D3DMATRIX *matrix)
1444 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1446 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1448 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1449 wined3d_mutex_lock();
1450 wined3d_device_get_transform(device->wined3d_device, state, (struct wined3d_matrix *)matrix);
1451 wined3d_mutex_unlock();
1453 return D3D_OK;
1456 static HRESULT WINAPI d3d8_device_MultiplyTransform(IDirect3DDevice8 *iface,
1457 D3DTRANSFORMSTATETYPE state, const D3DMATRIX *matrix)
1459 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1461 TRACE("iface %p, state %#x, matrix %p.\n", iface, state, matrix);
1463 /* Note: D3DMATRIX is compatible with struct wined3d_matrix. */
1464 wined3d_mutex_lock();
1465 wined3d_device_multiply_transform(device->wined3d_device, state, (const struct wined3d_matrix *)matrix);
1466 wined3d_mutex_unlock();
1468 return D3D_OK;
1471 static HRESULT WINAPI d3d8_device_SetViewport(IDirect3DDevice8 *iface, const D3DVIEWPORT8 *viewport)
1473 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1475 TRACE("iface %p, viewport %p.\n", iface, viewport);
1477 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1478 wined3d_mutex_lock();
1479 wined3d_device_set_viewport(device->wined3d_device, (const struct wined3d_viewport *)viewport);
1480 wined3d_mutex_unlock();
1482 return D3D_OK;
1485 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1487 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1489 TRACE("iface %p, viewport %p.\n", iface, viewport);
1491 /* Note: D3DVIEWPORT8 is compatible with struct wined3d_viewport. */
1492 wined3d_mutex_lock();
1493 wined3d_device_get_viewport(device->wined3d_device, (struct wined3d_viewport *)viewport);
1494 wined3d_mutex_unlock();
1496 return D3D_OK;
1499 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const D3DMATERIAL8 *material)
1501 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1503 TRACE("iface %p, material %p.\n", iface, material);
1505 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1506 wined3d_mutex_lock();
1507 wined3d_device_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1508 wined3d_mutex_unlock();
1510 return D3D_OK;
1513 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1515 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1517 TRACE("iface %p, material %p.\n", iface, material);
1519 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1520 wined3d_mutex_lock();
1521 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1522 wined3d_mutex_unlock();
1524 return D3D_OK;
1527 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1529 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1530 HRESULT hr;
1532 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1534 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1535 wined3d_mutex_lock();
1536 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1537 wined3d_mutex_unlock();
1539 return hr;
1542 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1544 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1545 HRESULT hr;
1547 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1549 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1550 wined3d_mutex_lock();
1551 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1552 wined3d_mutex_unlock();
1554 return hr;
1557 static HRESULT WINAPI d3d8_device_LightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL enable)
1559 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1560 HRESULT hr;
1562 TRACE("iface %p, index %u, enable %#x.\n", iface, index, enable);
1564 wined3d_mutex_lock();
1565 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1566 wined3d_mutex_unlock();
1568 return hr;
1571 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1573 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1574 HRESULT hr;
1576 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1578 wined3d_mutex_lock();
1579 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1580 wined3d_mutex_unlock();
1582 return hr;
1585 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const float *plane)
1587 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1588 HRESULT hr;
1590 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1592 wined3d_mutex_lock();
1593 hr = wined3d_device_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1594 wined3d_mutex_unlock();
1596 return hr;
1599 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1601 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1602 HRESULT hr;
1604 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1606 wined3d_mutex_lock();
1607 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1608 wined3d_mutex_unlock();
1610 return hr;
1613 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1614 D3DRENDERSTATETYPE state, DWORD value)
1616 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1618 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1620 wined3d_mutex_lock();
1621 switch (state)
1623 case D3DRS_ZBIAS:
1624 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1625 break;
1627 default:
1628 wined3d_device_set_render_state(device->wined3d_device, state, value);
1630 wined3d_mutex_unlock();
1632 return D3D_OK;
1635 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1636 D3DRENDERSTATETYPE state, DWORD *value)
1638 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1640 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1642 wined3d_mutex_lock();
1643 switch (state)
1645 case D3DRS_ZBIAS:
1646 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1647 break;
1649 default:
1650 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1652 wined3d_mutex_unlock();
1654 return D3D_OK;
1657 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1659 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1660 HRESULT hr;
1662 TRACE("iface %p.\n", iface);
1664 wined3d_mutex_lock();
1665 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1666 wined3d_mutex_unlock();
1668 return hr;
1671 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1673 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1674 struct wined3d_stateblock *stateblock;
1675 HRESULT hr;
1677 TRACE("iface %p, token %p.\n", iface, token);
1679 /* Tell wineD3D to endstateblock before anything else (in case we run out
1680 * of memory later and cause locking problems)
1682 wined3d_mutex_lock();
1683 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1684 if (FAILED(hr))
1686 WARN("Failed to end the state block, %#x.\n", hr);
1687 wined3d_mutex_unlock();
1688 return hr;
1691 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1692 wined3d_mutex_unlock();
1694 if (*token == D3D8_INVALID_HANDLE)
1696 ERR("Failed to create a handle\n");
1697 wined3d_mutex_lock();
1698 wined3d_stateblock_decref(stateblock);
1699 wined3d_mutex_unlock();
1700 return E_FAIL;
1702 ++*token;
1704 TRACE("Returning %#x (%p).\n", *token, stateblock);
1706 return hr;
1709 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1711 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1712 struct wined3d_stateblock *stateblock;
1714 TRACE("iface %p, token %#x.\n", iface, token);
1716 if (!token)
1717 return D3D_OK;
1719 wined3d_mutex_lock();
1720 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1721 if (!stateblock)
1723 WARN("Invalid handle (%#x) passed.\n", token);
1724 wined3d_mutex_unlock();
1725 return D3DERR_INVALIDCALL;
1727 wined3d_stateblock_apply(stateblock);
1728 wined3d_mutex_unlock();
1730 return D3D_OK;
1733 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1735 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1736 struct wined3d_stateblock *stateblock;
1738 TRACE("iface %p, token %#x.\n", iface, token);
1740 wined3d_mutex_lock();
1741 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1742 if (!stateblock)
1744 WARN("Invalid handle (%#x) passed.\n", token);
1745 wined3d_mutex_unlock();
1746 return D3DERR_INVALIDCALL;
1748 wined3d_stateblock_capture(stateblock);
1749 wined3d_mutex_unlock();
1751 return D3D_OK;
1754 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1756 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1757 struct wined3d_stateblock *stateblock;
1759 TRACE("iface %p, token %#x.\n", iface, token);
1761 wined3d_mutex_lock();
1762 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1764 if (!stateblock)
1766 WARN("Invalid handle (%#x) passed.\n", token);
1767 wined3d_mutex_unlock();
1768 return D3DERR_INVALIDCALL;
1771 if (wined3d_stateblock_decref(stateblock))
1773 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1775 wined3d_mutex_unlock();
1777 return D3D_OK;
1780 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1781 D3DSTATEBLOCKTYPE type, DWORD *handle)
1783 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1784 struct wined3d_stateblock *stateblock;
1785 HRESULT hr;
1787 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1789 if (type != D3DSBT_ALL
1790 && type != D3DSBT_PIXELSTATE
1791 && type != D3DSBT_VERTEXSTATE)
1793 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1794 return D3DERR_INVALIDCALL;
1797 wined3d_mutex_lock();
1798 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1799 if (FAILED(hr))
1801 wined3d_mutex_unlock();
1802 ERR("Failed to create the state block, hr %#x\n", hr);
1803 return hr;
1806 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1807 wined3d_mutex_unlock();
1809 if (*handle == D3D8_INVALID_HANDLE)
1811 ERR("Failed to allocate a handle.\n");
1812 wined3d_mutex_lock();
1813 wined3d_stateblock_decref(stateblock);
1814 wined3d_mutex_unlock();
1815 return E_FAIL;
1817 ++*handle;
1819 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1821 return hr;
1824 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1826 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1827 HRESULT hr;
1829 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1830 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1832 wined3d_mutex_lock();
1833 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1834 wined3d_mutex_unlock();
1836 return hr;
1839 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1841 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1842 HRESULT hr;
1844 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1846 wined3d_mutex_lock();
1847 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1848 wined3d_mutex_unlock();
1850 return hr;
1853 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1855 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1856 struct wined3d_texture *wined3d_texture;
1857 struct d3d8_texture *texture_impl;
1859 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1861 if (!texture)
1862 return D3DERR_INVALIDCALL;
1864 wined3d_mutex_lock();
1865 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1867 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1868 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1869 IDirect3DBaseTexture8_AddRef(*texture);
1871 else
1873 *texture = NULL;
1875 wined3d_mutex_unlock();
1877 return D3D_OK;
1880 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1882 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1883 struct d3d8_texture *texture_impl;
1884 HRESULT hr;
1886 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1888 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1890 wined3d_mutex_lock();
1891 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1892 texture_impl ? texture_impl->wined3d_texture : NULL);
1893 wined3d_mutex_unlock();
1895 return hr;
1898 static const struct tss_lookup
1900 BOOL sampler_state;
1901 union
1903 enum wined3d_texture_stage_state texture_state;
1904 enum wined3d_sampler_state sampler_state;
1905 } u;
1907 tss_lookup[] =
1909 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
1910 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
1911 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
1912 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
1913 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
1914 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
1915 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
1916 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
1917 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
1918 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
1919 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
1920 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
1921 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
1922 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
1923 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
1924 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
1925 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
1926 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
1927 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
1928 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
1929 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
1930 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
1931 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
1932 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
1933 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1934 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
1935 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
1936 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
1937 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
1940 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1941 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1943 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1944 const struct tss_lookup *l;
1946 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1948 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1950 WARN("Invalid Type %#x passed.\n", Type);
1951 return D3D_OK;
1954 l = &tss_lookup[Type];
1956 wined3d_mutex_lock();
1957 if (l->sampler_state)
1958 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
1959 else
1960 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
1961 wined3d_mutex_unlock();
1963 return D3D_OK;
1966 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1967 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1969 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1970 const struct tss_lookup *l;
1972 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1974 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1976 WARN("Invalid type %#x passed.\n", type);
1977 return D3D_OK;
1980 l = &tss_lookup[type];
1982 wined3d_mutex_lock();
1983 if (l->sampler_state)
1984 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
1985 else
1986 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
1987 wined3d_mutex_unlock();
1989 return D3D_OK;
1992 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
1994 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1995 HRESULT hr;
1997 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
1999 wined3d_mutex_lock();
2000 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2001 wined3d_mutex_unlock();
2003 return hr;
2006 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2007 DWORD info_id, void *info, DWORD info_size)
2009 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
2011 return D3D_OK;
2014 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2015 UINT palette_idx, const PALETTEENTRY *entries)
2017 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2019 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2020 * does not have a d3d8/9-style palette API */
2022 return D3D_OK;
2025 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2026 UINT palette_idx, PALETTEENTRY *entries)
2028 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2030 return D3DERR_INVALIDCALL;
2033 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2035 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2037 return D3D_OK;
2040 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2042 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2044 return D3DERR_INVALIDCALL;
2047 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2048 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2050 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2051 HRESULT hr;
2053 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2054 iface, primitive_type, start_vertex, primitive_count);
2056 wined3d_mutex_lock();
2057 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2058 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2059 vertex_count_from_primitive_count(primitive_type, primitive_count));
2060 wined3d_mutex_unlock();
2062 return hr;
2065 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2066 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2067 UINT start_idx, UINT primitive_count)
2069 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2070 HRESULT hr;
2072 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2073 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2075 wined3d_mutex_lock();
2076 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2077 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2078 vertex_count_from_primitive_count(primitive_type, primitive_count));
2079 wined3d_mutex_unlock();
2081 return hr;
2084 /* The caller is responsible for wined3d locking */
2085 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2087 HRESULT hr;
2089 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2091 UINT size = max(device->vertex_buffer_size * 2, min_size);
2092 struct wined3d_buffer *buffer;
2094 TRACE("Growing vertex buffer to %u bytes\n", size);
2096 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2097 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2098 if (FAILED(hr))
2100 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2101 return hr;
2104 if (device->vertex_buffer)
2105 wined3d_buffer_decref(device->vertex_buffer);
2107 device->vertex_buffer = buffer;
2108 device->vertex_buffer_size = size;
2109 device->vertex_buffer_pos = 0;
2111 return D3D_OK;
2114 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2115 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2116 UINT stride)
2118 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2119 HRESULT hr;
2120 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2121 struct wined3d_map_desc wined3d_map_desc;
2122 struct wined3d_box wined3d_box = {0};
2123 UINT size = vtx_count * stride;
2124 struct wined3d_resource *vb;
2125 UINT vb_pos, align;
2127 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2128 iface, primitive_type, primitive_count, data, stride);
2130 if (!primitive_count)
2132 WARN("primitive_count is 0, returning D3D_OK\n");
2133 return D3D_OK;
2136 wined3d_mutex_lock();
2137 hr = d3d8_device_prepare_vertex_buffer(device, size);
2138 if (FAILED(hr))
2139 goto done;
2141 vb_pos = device->vertex_buffer_pos;
2142 align = vb_pos % stride;
2143 if (align) align = stride - align;
2144 if (vb_pos + size + align > device->vertex_buffer_size)
2145 vb_pos = 0;
2146 else
2147 vb_pos += align;
2149 wined3d_box.left = vb_pos;
2150 wined3d_box.right = vb_pos + size;
2151 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2152 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2153 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2154 goto done;
2155 memcpy(wined3d_map_desc.data, data, size);
2156 wined3d_resource_unmap(vb, 0);
2157 device->vertex_buffer_pos = vb_pos + size;
2159 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2160 if (FAILED(hr))
2161 goto done;
2163 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2164 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2165 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2167 done:
2168 wined3d_mutex_unlock();
2169 return hr;
2172 /* The caller is responsible for wined3d locking */
2173 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2175 HRESULT hr;
2177 if (device->index_buffer_size < min_size || !device->index_buffer)
2179 UINT size = max(device->index_buffer_size * 2, min_size);
2180 struct wined3d_buffer *buffer;
2182 TRACE("Growing index buffer to %u bytes\n", size);
2184 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2185 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2186 if (FAILED(hr))
2188 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2189 return hr;
2192 if (device->index_buffer)
2193 wined3d_buffer_decref(device->index_buffer);
2195 device->index_buffer = buffer;
2196 device->index_buffer_size = size;
2197 device->index_buffer_pos = 0;
2199 return D3D_OK;
2202 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2203 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2204 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2205 const void *vertex_data, UINT vertex_stride)
2207 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2208 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2209 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2210 UINT vtx_size = vertex_count * vertex_stride;
2211 UINT idx_size = idx_count * idx_fmt_size;
2212 struct wined3d_map_desc wined3d_map_desc;
2213 struct wined3d_box wined3d_box = {0};
2214 struct wined3d_resource *ib, *vb;
2215 UINT vb_pos, ib_pos, align;
2216 HRESULT hr;
2218 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2219 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2220 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2221 index_data, index_format, vertex_data, vertex_stride);
2223 if (!primitive_count)
2225 WARN("primitive_count is 0, returning D3D_OK\n");
2226 return D3D_OK;
2229 wined3d_mutex_lock();
2231 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2232 if (FAILED(hr))
2233 goto done;
2235 vb_pos = device->vertex_buffer_pos;
2236 align = vb_pos % vertex_stride;
2237 if (align) align = vertex_stride - align;
2238 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2239 vb_pos = 0;
2240 else
2241 vb_pos += align;
2243 wined3d_box.left = vb_pos;
2244 wined3d_box.right = vb_pos + vtx_size;
2245 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2246 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2247 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2248 goto done;
2249 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2250 wined3d_resource_unmap(vb, 0);
2251 device->vertex_buffer_pos = vb_pos + vtx_size;
2253 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2254 if (FAILED(hr))
2255 goto done;
2257 ib_pos = device->index_buffer_pos;
2258 align = ib_pos % idx_fmt_size;
2259 if (align) align = idx_fmt_size - align;
2260 if (ib_pos + idx_size + align > device->index_buffer_size)
2261 ib_pos = 0;
2262 else
2263 ib_pos += align;
2265 wined3d_box.left = ib_pos;
2266 wined3d_box.right = ib_pos + idx_size;
2267 ib = wined3d_buffer_get_resource(device->index_buffer);
2268 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2269 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2270 goto done;
2271 memcpy(wined3d_map_desc.data, index_data, idx_size);
2272 wined3d_resource_unmap(ib, 0);
2273 device->index_buffer_pos = ib_pos + idx_size;
2275 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2276 if (FAILED(hr))
2277 goto done;
2279 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2280 wined3dformat_from_d3dformat(index_format), 0);
2281 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2283 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type);
2284 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2286 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2287 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2288 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2290 done:
2291 wined3d_mutex_unlock();
2292 return hr;
2295 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2296 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2298 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2299 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2300 HRESULT hr;
2302 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2303 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2305 wined3d_mutex_lock();
2306 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2307 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2308 wined3d_mutex_unlock();
2310 return hr;
2313 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2314 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2316 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2317 struct d3d8_vertex_shader *object;
2318 DWORD shader_handle;
2319 DWORD handle;
2320 HRESULT hr;
2322 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2323 iface, declaration, byte_code, shader, usage);
2325 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2326 if (!object)
2328 *shader = 0;
2329 return E_OUTOFMEMORY;
2332 wined3d_mutex_lock();
2333 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2334 wined3d_mutex_unlock();
2335 if (handle == D3D8_INVALID_HANDLE)
2337 ERR("Failed to allocate vertex shader handle.\n");
2338 HeapFree(GetProcessHeap(), 0, object);
2339 *shader = 0;
2340 return E_OUTOFMEMORY;
2343 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2345 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2346 if (FAILED(hr))
2348 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2349 wined3d_mutex_lock();
2350 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2351 wined3d_mutex_unlock();
2352 HeapFree(GetProcessHeap(), 0, object);
2353 *shader = 0;
2354 return hr;
2357 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2358 *shader = shader_handle;
2360 return D3D_OK;
2363 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2365 struct d3d8_vertex_declaration *d3d8_declaration;
2366 struct FvfToDecl *convertedDecls = device->decls;
2367 int p, low, high; /* deliberately signed */
2368 HRESULT hr;
2370 TRACE("Searching for declaration for fvf %08x... ", fvf);
2372 low = 0;
2373 high = device->numConvertedDecls - 1;
2374 while (low <= high)
2376 p = (low + high) >> 1;
2377 TRACE("%d ", p);
2379 if (convertedDecls[p].fvf == fvf)
2381 TRACE("found %p\n", convertedDecls[p].declaration);
2382 return convertedDecls[p].declaration;
2385 if (convertedDecls[p].fvf < fvf)
2386 low = p + 1;
2387 else
2388 high = p - 1;
2390 TRACE("not found. Creating and inserting at position %d.\n", low);
2392 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2393 return NULL;
2395 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2397 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2398 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2399 return NULL;
2402 if (device->declArraySize == device->numConvertedDecls)
2404 UINT grow = device->declArraySize / 2;
2406 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2407 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2408 if (!convertedDecls)
2410 d3d8_vertex_declaration_destroy(d3d8_declaration);
2411 return NULL;
2413 device->decls = convertedDecls;
2414 device->declArraySize += grow;
2417 memmove(convertedDecls + low + 1, convertedDecls + low,
2418 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2419 convertedDecls[low].declaration = d3d8_declaration;
2420 convertedDecls[low].fvf = fvf;
2421 ++device->numConvertedDecls;
2423 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2425 return d3d8_declaration;
2428 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2430 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2431 struct d3d8_vertex_shader *shader_impl;
2433 TRACE("iface %p, shader %#x.\n", iface, shader);
2435 if (VS_HIGHESTFIXEDFXF >= shader)
2437 TRACE("Setting FVF, %#x\n", shader);
2439 wined3d_mutex_lock();
2440 wined3d_device_set_vertex_declaration(device->wined3d_device,
2441 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2442 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2443 wined3d_mutex_unlock();
2445 return D3D_OK;
2448 TRACE("Setting shader\n");
2450 wined3d_mutex_lock();
2451 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2453 WARN("Invalid handle (%#x) passed.\n", shader);
2454 wined3d_mutex_unlock();
2456 return D3DERR_INVALIDCALL;
2459 wined3d_device_set_vertex_declaration(device->wined3d_device,
2460 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2461 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2462 wined3d_mutex_unlock();
2464 return D3D_OK;
2467 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2469 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2470 struct wined3d_vertex_declaration *wined3d_declaration;
2471 struct d3d8_vertex_declaration *d3d8_declaration;
2473 TRACE("iface %p, shader %p.\n", iface, shader);
2475 wined3d_mutex_lock();
2476 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2478 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2479 *shader = d3d8_declaration->shader_handle;
2481 else
2483 *shader = 0;
2485 wined3d_mutex_unlock();
2487 TRACE("Returning %#x.\n", *shader);
2489 return D3D_OK;
2492 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2494 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2495 struct d3d8_vertex_shader *shader_impl;
2497 TRACE("iface %p, shader %#x.\n", iface, shader);
2499 wined3d_mutex_lock();
2500 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2502 WARN("Invalid handle (%#x) passed.\n", shader);
2503 wined3d_mutex_unlock();
2505 return D3DERR_INVALIDCALL;
2508 if (shader_impl->wined3d_shader
2509 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2510 IDirect3DDevice8_SetVertexShader(iface, 0);
2512 wined3d_mutex_unlock();
2514 d3d8_vertex_shader_destroy(shader_impl);
2516 return D3D_OK;
2519 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2520 DWORD start_register, const void *data, DWORD count)
2522 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2523 HRESULT hr;
2525 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2526 iface, start_register, data, count);
2528 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2530 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2531 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2532 return D3DERR_INVALIDCALL;
2535 wined3d_mutex_lock();
2536 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2537 wined3d_mutex_unlock();
2539 return hr;
2542 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2543 DWORD start_register, void *data, DWORD count)
2545 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2546 HRESULT hr;
2548 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2549 iface, start_register, data, count);
2551 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2553 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2554 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2555 return D3DERR_INVALIDCALL;
2558 wined3d_mutex_lock();
2559 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2560 wined3d_mutex_unlock();
2562 return hr;
2565 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2566 DWORD shader, void *data, DWORD *data_size)
2568 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2569 struct d3d8_vertex_declaration *declaration;
2570 struct d3d8_vertex_shader *shader_impl;
2572 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2573 iface, shader, data, data_size);
2575 wined3d_mutex_lock();
2576 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2577 wined3d_mutex_unlock();
2579 if (!shader_impl)
2581 WARN("Invalid handle (%#x) passed.\n", shader);
2582 return D3DERR_INVALIDCALL;
2584 declaration = shader_impl->vertex_declaration;
2586 if (!data)
2588 *data_size = declaration->elements_size;
2589 return D3D_OK;
2592 /* MSDN claims that if *data_size is smaller than the required size
2593 * we should write the required size and return D3DERR_MOREDATA.
2594 * That's not actually true. */
2595 if (*data_size < declaration->elements_size)
2596 return D3DERR_INVALIDCALL;
2598 memcpy(data, declaration->elements, declaration->elements_size);
2600 return D3D_OK;
2603 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2604 DWORD shader, void *data, DWORD *data_size)
2606 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2607 struct d3d8_vertex_shader *shader_impl = NULL;
2608 HRESULT hr;
2610 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2611 iface, shader, data, data_size);
2613 wined3d_mutex_lock();
2614 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2616 WARN("Invalid handle (%#x) passed.\n", shader);
2617 wined3d_mutex_unlock();
2619 return D3DERR_INVALIDCALL;
2622 if (!shader_impl->wined3d_shader)
2624 wined3d_mutex_unlock();
2625 *data_size = 0;
2626 return D3D_OK;
2629 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2630 wined3d_mutex_unlock();
2632 return hr;
2635 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2636 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2638 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2639 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2641 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2643 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2644 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2645 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2646 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2647 * problem)
2649 wined3d_mutex_lock();
2650 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2651 wined3d_device_set_index_buffer(device->wined3d_device,
2652 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2653 wined3d_mutex_unlock();
2655 return D3D_OK;
2658 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2659 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2661 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2662 enum wined3d_format_id wined3d_format;
2663 struct wined3d_buffer *wined3d_buffer;
2664 struct d3d8_indexbuffer *buffer_impl;
2666 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2668 if (!buffer)
2669 return D3DERR_INVALIDCALL;
2671 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2672 wined3d_mutex_lock();
2673 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2674 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2676 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2677 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2678 IDirect3DIndexBuffer8_AddRef(*buffer);
2680 else
2682 *buffer = NULL;
2684 wined3d_mutex_unlock();
2686 return D3D_OK;
2689 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2690 const DWORD *byte_code, DWORD *shader)
2692 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2693 struct d3d8_pixel_shader *object;
2694 DWORD shader_handle;
2695 DWORD handle;
2696 HRESULT hr;
2698 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2700 if (!shader)
2701 return D3DERR_INVALIDCALL;
2703 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2704 if (!object)
2705 return E_OUTOFMEMORY;
2707 wined3d_mutex_lock();
2708 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2709 wined3d_mutex_unlock();
2710 if (handle == D3D8_INVALID_HANDLE)
2712 ERR("Failed to allocate pixel shader handle.\n");
2713 HeapFree(GetProcessHeap(), 0, object);
2714 return E_OUTOFMEMORY;
2717 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2719 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2720 if (FAILED(hr))
2722 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2723 wined3d_mutex_lock();
2724 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2725 wined3d_mutex_unlock();
2726 HeapFree(GetProcessHeap(), 0, object);
2727 *shader = 0;
2728 return hr;
2731 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2732 *shader = shader_handle;
2734 return D3D_OK;
2737 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2739 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2740 struct d3d8_pixel_shader *shader_impl;
2742 TRACE("iface %p, shader %#x.\n", iface, shader);
2744 wined3d_mutex_lock();
2746 if (!shader)
2748 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2749 wined3d_mutex_unlock();
2750 return D3D_OK;
2753 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2755 WARN("Invalid handle (%#x) passed.\n", shader);
2756 wined3d_mutex_unlock();
2757 return D3DERR_INVALIDCALL;
2760 TRACE("Setting shader %p.\n", shader_impl);
2761 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2762 wined3d_mutex_unlock();
2764 return D3D_OK;
2767 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2769 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2770 struct wined3d_shader *object;
2772 TRACE("iface %p, shader %p.\n", iface, shader);
2774 if (!shader)
2775 return D3DERR_INVALIDCALL;
2777 wined3d_mutex_lock();
2778 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2780 struct d3d8_pixel_shader *d3d8_shader;
2781 d3d8_shader = wined3d_shader_get_parent(object);
2782 *shader = d3d8_shader->handle;
2784 else
2786 *shader = 0;
2788 wined3d_mutex_unlock();
2790 TRACE("Returning %#x.\n", *shader);
2792 return D3D_OK;
2795 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2797 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2798 struct d3d8_pixel_shader *shader_impl;
2800 TRACE("iface %p, shader %#x.\n", iface, shader);
2802 wined3d_mutex_lock();
2804 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2806 WARN("Invalid handle (%#x) passed.\n", shader);
2807 wined3d_mutex_unlock();
2808 return D3DERR_INVALIDCALL;
2811 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2812 IDirect3DDevice8_SetPixelShader(iface, 0);
2814 wined3d_mutex_unlock();
2816 d3d8_pixel_shader_destroy(shader_impl);
2818 return D3D_OK;
2821 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2822 DWORD start_register, const void *data, DWORD count)
2824 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2825 HRESULT hr;
2827 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2828 iface, start_register, data, count);
2830 wined3d_mutex_lock();
2831 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
2832 wined3d_mutex_unlock();
2834 return hr;
2837 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2838 DWORD start_register, 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_get_ps_consts_f(device->wined3d_device, start_register, count, data);
2848 wined3d_mutex_unlock();
2850 return hr;
2853 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2854 DWORD shader, void *data, DWORD *data_size)
2856 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2857 struct d3d8_pixel_shader *shader_impl = NULL;
2858 HRESULT hr;
2860 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2861 iface, shader, data, data_size);
2863 wined3d_mutex_lock();
2864 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2866 WARN("Invalid handle (%#x) passed.\n", shader);
2867 wined3d_mutex_unlock();
2869 return D3DERR_INVALIDCALL;
2872 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2873 wined3d_mutex_unlock();
2875 return hr;
2878 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2879 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2881 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2882 iface, handle, segment_count, patch_info);
2883 return D3D_OK;
2886 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2887 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2889 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2890 iface, handle, segment_count, patch_info);
2891 return D3D_OK;
2894 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2896 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2897 return D3DERR_INVALIDCALL;
2900 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2901 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2903 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2904 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2905 HRESULT hr;
2907 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2908 iface, stream_idx, buffer, stride);
2910 wined3d_mutex_lock();
2911 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2912 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2913 wined3d_mutex_unlock();
2915 return hr;
2918 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2919 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2921 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2922 struct d3d8_vertexbuffer *buffer_impl;
2923 struct wined3d_buffer *wined3d_buffer = NULL;
2924 HRESULT hr;
2926 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2927 iface, stream_idx, buffer, stride);
2929 if (!buffer)
2930 return D3DERR_INVALIDCALL;
2932 wined3d_mutex_lock();
2933 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2934 if (SUCCEEDED(hr) && wined3d_buffer)
2936 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2937 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2938 IDirect3DVertexBuffer8_AddRef(*buffer);
2940 else
2942 if (FAILED(hr))
2943 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2944 *buffer = NULL;
2946 wined3d_mutex_unlock();
2948 return hr;
2951 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2953 d3d8_device_QueryInterface,
2954 d3d8_device_AddRef,
2955 d3d8_device_Release,
2956 d3d8_device_TestCooperativeLevel,
2957 d3d8_device_GetAvailableTextureMem,
2958 d3d8_device_ResourceManagerDiscardBytes,
2959 d3d8_device_GetDirect3D,
2960 d3d8_device_GetDeviceCaps,
2961 d3d8_device_GetDisplayMode,
2962 d3d8_device_GetCreationParameters,
2963 d3d8_device_SetCursorProperties,
2964 d3d8_device_SetCursorPosition,
2965 d3d8_device_ShowCursor,
2966 d3d8_device_CreateAdditionalSwapChain,
2967 d3d8_device_Reset,
2968 d3d8_device_Present,
2969 d3d8_device_GetBackBuffer,
2970 d3d8_device_GetRasterStatus,
2971 d3d8_device_SetGammaRamp,
2972 d3d8_device_GetGammaRamp,
2973 d3d8_device_CreateTexture,
2974 d3d8_device_CreateVolumeTexture,
2975 d3d8_device_CreateCubeTexture,
2976 d3d8_device_CreateVertexBuffer,
2977 d3d8_device_CreateIndexBuffer,
2978 d3d8_device_CreateRenderTarget,
2979 d3d8_device_CreateDepthStencilSurface,
2980 d3d8_device_CreateImageSurface,
2981 d3d8_device_CopyRects,
2982 d3d8_device_UpdateTexture,
2983 d3d8_device_GetFrontBuffer,
2984 d3d8_device_SetRenderTarget,
2985 d3d8_device_GetRenderTarget,
2986 d3d8_device_GetDepthStencilSurface,
2987 d3d8_device_BeginScene,
2988 d3d8_device_EndScene,
2989 d3d8_device_Clear,
2990 d3d8_device_SetTransform,
2991 d3d8_device_GetTransform,
2992 d3d8_device_MultiplyTransform,
2993 d3d8_device_SetViewport,
2994 d3d8_device_GetViewport,
2995 d3d8_device_SetMaterial,
2996 d3d8_device_GetMaterial,
2997 d3d8_device_SetLight,
2998 d3d8_device_GetLight,
2999 d3d8_device_LightEnable,
3000 d3d8_device_GetLightEnable,
3001 d3d8_device_SetClipPlane,
3002 d3d8_device_GetClipPlane,
3003 d3d8_device_SetRenderState,
3004 d3d8_device_GetRenderState,
3005 d3d8_device_BeginStateBlock,
3006 d3d8_device_EndStateBlock,
3007 d3d8_device_ApplyStateBlock,
3008 d3d8_device_CaptureStateBlock,
3009 d3d8_device_DeleteStateBlock,
3010 d3d8_device_CreateStateBlock,
3011 d3d8_device_SetClipStatus,
3012 d3d8_device_GetClipStatus,
3013 d3d8_device_GetTexture,
3014 d3d8_device_SetTexture,
3015 d3d8_device_GetTextureStageState,
3016 d3d8_device_SetTextureStageState,
3017 d3d8_device_ValidateDevice,
3018 d3d8_device_GetInfo,
3019 d3d8_device_SetPaletteEntries,
3020 d3d8_device_GetPaletteEntries,
3021 d3d8_device_SetCurrentTexturePalette,
3022 d3d8_device_GetCurrentTexturePalette,
3023 d3d8_device_DrawPrimitive,
3024 d3d8_device_DrawIndexedPrimitive,
3025 d3d8_device_DrawPrimitiveUP,
3026 d3d8_device_DrawIndexedPrimitiveUP,
3027 d3d8_device_ProcessVertices,
3028 d3d8_device_CreateVertexShader,
3029 d3d8_device_SetVertexShader,
3030 d3d8_device_GetVertexShader,
3031 d3d8_device_DeleteVertexShader,
3032 d3d8_device_SetVertexShaderConstant,
3033 d3d8_device_GetVertexShaderConstant,
3034 d3d8_device_GetVertexShaderDeclaration,
3035 d3d8_device_GetVertexShaderFunction,
3036 d3d8_device_SetStreamSource,
3037 d3d8_device_GetStreamSource,
3038 d3d8_device_SetIndices,
3039 d3d8_device_GetIndices,
3040 d3d8_device_CreatePixelShader,
3041 d3d8_device_SetPixelShader,
3042 d3d8_device_GetPixelShader,
3043 d3d8_device_DeletePixelShader,
3044 d3d8_device_SetPixelShaderConstant,
3045 d3d8_device_GetPixelShaderConstant,
3046 d3d8_device_GetPixelShaderFunction,
3047 d3d8_device_DrawRectPatch,
3048 d3d8_device_DrawTriPatch,
3049 d3d8_device_DeletePatch,
3052 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3054 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3057 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3058 struct wined3d_device *device)
3060 TRACE("device_parent %p, device %p\n", device_parent, device);
3063 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3065 TRACE("device_parent %p.\n", device_parent);
3068 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3070 struct d3d8_device *device = device_from_device_parent(device_parent);
3072 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3074 if (!activate)
3075 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3076 else
3077 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3080 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3081 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3082 void **parent, const struct wined3d_parent_ops **parent_ops)
3084 struct d3d8_surface *d3d_surface;
3086 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3087 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3089 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3090 return E_OUTOFMEMORY;
3092 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3093 *parent = d3d_surface;
3094 TRACE("Created surface %p.\n", d3d_surface);
3096 return D3D_OK;
3099 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3100 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3101 void **parent, const struct wined3d_parent_ops **parent_ops)
3103 struct d3d8_volume *d3d_volume;
3105 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3106 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3108 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3109 return E_OUTOFMEMORY;
3111 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3112 *parent = d3d_volume;
3113 TRACE("Created volume %p.\n", d3d_volume);
3115 return D3D_OK;
3118 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3119 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3120 struct wined3d_texture **texture)
3122 struct d3d8_device *device = device_from_device_parent(device_parent);
3123 struct d3d8_surface *d3d_surface;
3124 HRESULT hr;
3126 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3127 device_parent, container_parent, desc, texture_flags, texture);
3129 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3130 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface,
3131 &d3d8_null_wined3d_parent_ops, texture)))
3133 WARN("Failed to create texture, hr %#x.\n", hr);
3134 return hr;
3137 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3138 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3140 return hr;
3143 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3144 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3146 struct d3d8_device *device = device_from_device_parent(device_parent);
3147 struct d3d8_swapchain *d3d_swapchain;
3148 HRESULT hr;
3150 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3152 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3154 WARN("Failed to create swapchain, hr %#x.\n", hr);
3155 *swapchain = NULL;
3156 return hr;
3159 *swapchain = d3d_swapchain->wined3d_swapchain;
3160 wined3d_swapchain_incref(*swapchain);
3161 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3163 return hr;
3166 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3168 device_parent_wined3d_device_created,
3169 device_parent_mode_changed,
3170 device_parent_activate,
3171 device_parent_surface_created,
3172 device_parent_volume_created,
3173 device_parent_create_swapchain_texture,
3174 device_parent_create_swapchain,
3177 static void setup_fpu(void)
3179 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3180 WORD cw;
3181 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3182 cw = (cw & ~0xf3f) | 0x3f;
3183 __asm__ volatile ("fldcw %0" : : "m" (cw));
3184 #elif defined(__i386__) && defined(_MSC_VER)
3185 WORD cw;
3186 __asm fnstcw cw;
3187 cw = (cw & ~0xf3f) | 0x3f;
3188 __asm fldcw cw;
3189 #else
3190 FIXME("FPU setup not implemented for this platform.\n");
3191 #endif
3194 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3195 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3197 struct wined3d_swapchain_desc swapchain_desc;
3198 struct wined3d_swapchain *wined3d_swapchain;
3199 HRESULT hr;
3201 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3202 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3203 device->ref = 1;
3204 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3205 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3206 if (!device->handle_table.entries)
3208 ERR("Failed to allocate handle table memory.\n");
3209 return E_OUTOFMEMORY;
3211 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3213 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3215 wined3d_mutex_lock();
3216 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3217 &device->device_parent, &device->wined3d_device);
3218 if (FAILED(hr))
3220 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3221 wined3d_mutex_unlock();
3222 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3223 return hr;
3226 if (!parameters->Windowed)
3228 HWND device_window = parameters->hDeviceWindow;
3230 if (!focus_window)
3231 focus_window = device_window;
3232 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3234 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3235 wined3d_device_decref(device->wined3d_device);
3236 wined3d_mutex_unlock();
3237 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3238 return hr;
3241 if (!device_window)
3242 device_window = focus_window;
3243 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3244 parameters->BackBufferWidth,
3245 parameters->BackBufferHeight);
3248 if (flags & D3DCREATE_MULTITHREADED)
3249 wined3d_device_set_multithreaded(device->wined3d_device);
3251 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3253 wined3d_device_release_focus_window(device->wined3d_device);
3254 wined3d_device_decref(device->wined3d_device);
3255 wined3d_mutex_unlock();
3256 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3257 return D3DERR_INVALIDCALL;
3260 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3261 if (FAILED(hr))
3263 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3264 wined3d_device_release_focus_window(device->wined3d_device);
3265 wined3d_device_decref(device->wined3d_device);
3266 wined3d_mutex_unlock();
3267 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3268 return hr;
3271 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3272 wined3d_mutex_unlock();
3274 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3276 device->declArraySize = 16;
3277 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3278 if (!device->decls)
3280 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3281 hr = E_OUTOFMEMORY;
3282 goto err;
3285 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3286 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3288 device->d3d_parent = &parent->IDirect3D8_iface;
3289 IDirect3D8_AddRef(device->d3d_parent);
3291 return D3D_OK;
3293 err:
3294 wined3d_mutex_lock();
3295 wined3d_device_uninit_3d(device->wined3d_device);
3296 wined3d_device_release_focus_window(device->wined3d_device);
3297 wined3d_device_decref(device->wined3d_device);
3298 wined3d_mutex_unlock();
3299 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3300 return hr;