wined3d: Handle hull shader control point outputs.
[wine.git] / dlls / d3d8 / device.c
blob8b6bd3f6ef69cf1e709f73f1dd6268131d3968aa
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);
1474 struct wined3d_viewport vp;
1476 TRACE("iface %p, viewport %p.\n", iface, viewport);
1478 vp.x = viewport->X;
1479 vp.y = viewport->Y;
1480 vp.width = viewport->Width;
1481 vp.height = viewport->Height;
1482 vp.min_z = viewport->MinZ;
1483 vp.max_z = viewport->MaxZ;
1485 wined3d_mutex_lock();
1486 wined3d_device_set_viewport(device->wined3d_device, &vp);
1487 wined3d_mutex_unlock();
1489 return D3D_OK;
1492 static HRESULT WINAPI d3d8_device_GetViewport(IDirect3DDevice8 *iface, D3DVIEWPORT8 *viewport)
1494 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1495 struct wined3d_viewport wined3d_viewport;
1497 TRACE("iface %p, viewport %p.\n", iface, viewport);
1499 wined3d_mutex_lock();
1500 wined3d_device_get_viewport(device->wined3d_device, &wined3d_viewport);
1501 wined3d_mutex_unlock();
1503 viewport->X = wined3d_viewport.x;
1504 viewport->Y = wined3d_viewport.y;
1505 viewport->Width = wined3d_viewport.width;
1506 viewport->Height = wined3d_viewport.height;
1507 viewport->MinZ = wined3d_viewport.min_z;
1508 viewport->MaxZ = wined3d_viewport.max_z;
1510 return D3D_OK;
1513 static HRESULT WINAPI d3d8_device_SetMaterial(IDirect3DDevice8 *iface, const 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_set_material(device->wined3d_device, (const struct wined3d_material *)material);
1522 wined3d_mutex_unlock();
1524 return D3D_OK;
1527 static HRESULT WINAPI d3d8_device_GetMaterial(IDirect3DDevice8 *iface, D3DMATERIAL8 *material)
1529 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1531 TRACE("iface %p, material %p.\n", iface, material);
1533 /* Note: D3DMATERIAL8 is compatible with struct wined3d_material. */
1534 wined3d_mutex_lock();
1535 wined3d_device_get_material(device->wined3d_device, (struct wined3d_material *)material);
1536 wined3d_mutex_unlock();
1538 return D3D_OK;
1541 static HRESULT WINAPI d3d8_device_SetLight(IDirect3DDevice8 *iface, DWORD index, const D3DLIGHT8 *light)
1543 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1544 HRESULT hr;
1546 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1548 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1549 wined3d_mutex_lock();
1550 hr = wined3d_device_set_light(device->wined3d_device, index, (const struct wined3d_light *)light);
1551 wined3d_mutex_unlock();
1553 return hr;
1556 static HRESULT WINAPI d3d8_device_GetLight(IDirect3DDevice8 *iface, DWORD index, D3DLIGHT8 *light)
1558 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1559 HRESULT hr;
1561 TRACE("iface %p, index %u, light %p.\n", iface, index, light);
1563 /* Note: D3DLIGHT8 is compatible with struct wined3d_light. */
1564 wined3d_mutex_lock();
1565 hr = wined3d_device_get_light(device->wined3d_device, index, (struct wined3d_light *)light);
1566 wined3d_mutex_unlock();
1568 return hr;
1571 static HRESULT WINAPI d3d8_device_LightEnable(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 %#x.\n", iface, index, enable);
1578 wined3d_mutex_lock();
1579 hr = wined3d_device_set_light_enable(device->wined3d_device, index, enable);
1580 wined3d_mutex_unlock();
1582 return hr;
1585 static HRESULT WINAPI d3d8_device_GetLightEnable(IDirect3DDevice8 *iface, DWORD index, BOOL *enable)
1587 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1588 HRESULT hr;
1590 TRACE("iface %p, index %u, enable %p.\n", iface, index, enable);
1592 wined3d_mutex_lock();
1593 hr = wined3d_device_get_light_enable(device->wined3d_device, index, enable);
1594 wined3d_mutex_unlock();
1596 return hr;
1599 static HRESULT WINAPI d3d8_device_SetClipPlane(IDirect3DDevice8 *iface, DWORD index, const 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_set_clip_plane(device->wined3d_device, index, (const struct wined3d_vec4 *)plane);
1608 wined3d_mutex_unlock();
1610 return hr;
1613 static HRESULT WINAPI d3d8_device_GetClipPlane(IDirect3DDevice8 *iface, DWORD index, float *plane)
1615 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1616 HRESULT hr;
1618 TRACE("iface %p, index %u, plane %p.\n", iface, index, plane);
1620 wined3d_mutex_lock();
1621 hr = wined3d_device_get_clip_plane(device->wined3d_device, index, (struct wined3d_vec4 *)plane);
1622 wined3d_mutex_unlock();
1624 return hr;
1627 static HRESULT WINAPI d3d8_device_SetRenderState(IDirect3DDevice8 *iface,
1628 D3DRENDERSTATETYPE state, DWORD value)
1630 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1632 TRACE("iface %p, state %#x, value %#x.\n", iface, state, value);
1634 wined3d_mutex_lock();
1635 switch (state)
1637 case D3DRS_ZBIAS:
1638 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS, value);
1639 break;
1641 default:
1642 wined3d_device_set_render_state(device->wined3d_device, state, value);
1644 wined3d_mutex_unlock();
1646 return D3D_OK;
1649 static HRESULT WINAPI d3d8_device_GetRenderState(IDirect3DDevice8 *iface,
1650 D3DRENDERSTATETYPE state, DWORD *value)
1652 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1654 TRACE("iface %p, state %#x, value %p.\n", iface, state, value);
1656 wined3d_mutex_lock();
1657 switch (state)
1659 case D3DRS_ZBIAS:
1660 *value = wined3d_device_get_render_state(device->wined3d_device, WINED3D_RS_DEPTHBIAS);
1661 break;
1663 default:
1664 *value = wined3d_device_get_render_state(device->wined3d_device, state);
1666 wined3d_mutex_unlock();
1668 return D3D_OK;
1671 static HRESULT WINAPI d3d8_device_BeginStateBlock(IDirect3DDevice8 *iface)
1673 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1674 HRESULT hr;
1676 TRACE("iface %p.\n", iface);
1678 wined3d_mutex_lock();
1679 hr = wined3d_device_begin_stateblock(device->wined3d_device);
1680 wined3d_mutex_unlock();
1682 return hr;
1685 static HRESULT WINAPI d3d8_device_EndStateBlock(IDirect3DDevice8 *iface, DWORD *token)
1687 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1688 struct wined3d_stateblock *stateblock;
1689 HRESULT hr;
1691 TRACE("iface %p, token %p.\n", iface, token);
1693 /* Tell wineD3D to endstateblock before anything else (in case we run out
1694 * of memory later and cause locking problems)
1696 wined3d_mutex_lock();
1697 hr = wined3d_device_end_stateblock(device->wined3d_device, &stateblock);
1698 if (FAILED(hr))
1700 WARN("Failed to end the state block, %#x.\n", hr);
1701 wined3d_mutex_unlock();
1702 return hr;
1705 *token = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1706 wined3d_mutex_unlock();
1708 if (*token == D3D8_INVALID_HANDLE)
1710 ERR("Failed to create a handle\n");
1711 wined3d_mutex_lock();
1712 wined3d_stateblock_decref(stateblock);
1713 wined3d_mutex_unlock();
1714 return E_FAIL;
1716 ++*token;
1718 TRACE("Returning %#x (%p).\n", *token, stateblock);
1720 return hr;
1723 static HRESULT WINAPI d3d8_device_ApplyStateBlock(IDirect3DDevice8 *iface, DWORD token)
1725 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1726 struct wined3d_stateblock *stateblock;
1728 TRACE("iface %p, token %#x.\n", iface, token);
1730 if (!token)
1731 return D3D_OK;
1733 wined3d_mutex_lock();
1734 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1735 if (!stateblock)
1737 WARN("Invalid handle (%#x) passed.\n", token);
1738 wined3d_mutex_unlock();
1739 return D3DERR_INVALIDCALL;
1741 wined3d_stateblock_apply(stateblock);
1742 wined3d_mutex_unlock();
1744 return D3D_OK;
1747 static HRESULT WINAPI d3d8_device_CaptureStateBlock(IDirect3DDevice8 *iface, DWORD token)
1749 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1750 struct wined3d_stateblock *stateblock;
1752 TRACE("iface %p, token %#x.\n", iface, token);
1754 wined3d_mutex_lock();
1755 stateblock = d3d8_get_object(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1756 if (!stateblock)
1758 WARN("Invalid handle (%#x) passed.\n", token);
1759 wined3d_mutex_unlock();
1760 return D3DERR_INVALIDCALL;
1762 wined3d_stateblock_capture(stateblock);
1763 wined3d_mutex_unlock();
1765 return D3D_OK;
1768 static HRESULT WINAPI d3d8_device_DeleteStateBlock(IDirect3DDevice8 *iface, DWORD token)
1770 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1771 struct wined3d_stateblock *stateblock;
1773 TRACE("iface %p, token %#x.\n", iface, token);
1775 wined3d_mutex_lock();
1776 stateblock = d3d8_free_handle(&device->handle_table, token - 1, D3D8_HANDLE_SB);
1778 if (!stateblock)
1780 WARN("Invalid handle (%#x) passed.\n", token);
1781 wined3d_mutex_unlock();
1782 return D3DERR_INVALIDCALL;
1785 if (wined3d_stateblock_decref(stateblock))
1787 ERR("Stateblock %p has references left, this shouldn't happen.\n", stateblock);
1789 wined3d_mutex_unlock();
1791 return D3D_OK;
1794 static HRESULT WINAPI d3d8_device_CreateStateBlock(IDirect3DDevice8 *iface,
1795 D3DSTATEBLOCKTYPE type, DWORD *handle)
1797 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1798 struct wined3d_stateblock *stateblock;
1799 HRESULT hr;
1801 TRACE("iface %p, type %#x, handle %p.\n", iface, type, handle);
1803 if (type != D3DSBT_ALL
1804 && type != D3DSBT_PIXELSTATE
1805 && type != D3DSBT_VERTEXSTATE)
1807 WARN("Unexpected stateblock type, returning D3DERR_INVALIDCALL\n");
1808 return D3DERR_INVALIDCALL;
1811 wined3d_mutex_lock();
1812 hr = wined3d_stateblock_create(device->wined3d_device, (enum wined3d_stateblock_type)type, &stateblock);
1813 if (FAILED(hr))
1815 wined3d_mutex_unlock();
1816 ERR("Failed to create the state block, hr %#x\n", hr);
1817 return hr;
1820 *handle = d3d8_allocate_handle(&device->handle_table, stateblock, D3D8_HANDLE_SB);
1821 wined3d_mutex_unlock();
1823 if (*handle == D3D8_INVALID_HANDLE)
1825 ERR("Failed to allocate a handle.\n");
1826 wined3d_mutex_lock();
1827 wined3d_stateblock_decref(stateblock);
1828 wined3d_mutex_unlock();
1829 return E_FAIL;
1831 ++*handle;
1833 TRACE("Returning %#x (%p).\n", *handle, stateblock);
1835 return hr;
1838 static HRESULT WINAPI d3d8_device_SetClipStatus(IDirect3DDevice8 *iface, const D3DCLIPSTATUS8 *clip_status)
1840 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1841 HRESULT hr;
1843 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1844 /* FIXME: Verify that D3DCLIPSTATUS8 ~= struct wined3d_clip_status. */
1846 wined3d_mutex_lock();
1847 hr = wined3d_device_set_clip_status(device->wined3d_device, (const struct wined3d_clip_status *)clip_status);
1848 wined3d_mutex_unlock();
1850 return hr;
1853 static HRESULT WINAPI d3d8_device_GetClipStatus(IDirect3DDevice8 *iface, D3DCLIPSTATUS8 *clip_status)
1855 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1856 HRESULT hr;
1858 TRACE("iface %p, clip_status %p.\n", iface, clip_status);
1860 wined3d_mutex_lock();
1861 hr = wined3d_device_get_clip_status(device->wined3d_device, (struct wined3d_clip_status *)clip_status);
1862 wined3d_mutex_unlock();
1864 return hr;
1867 static HRESULT WINAPI d3d8_device_GetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 **texture)
1869 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1870 struct wined3d_texture *wined3d_texture;
1871 struct d3d8_texture *texture_impl;
1873 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1875 if (!texture)
1876 return D3DERR_INVALIDCALL;
1878 wined3d_mutex_lock();
1879 if ((wined3d_texture = wined3d_device_get_texture(device->wined3d_device, stage)))
1881 texture_impl = wined3d_texture_get_parent(wined3d_texture);
1882 *texture = &texture_impl->IDirect3DBaseTexture8_iface;
1883 IDirect3DBaseTexture8_AddRef(*texture);
1885 else
1887 *texture = NULL;
1889 wined3d_mutex_unlock();
1891 return D3D_OK;
1894 static HRESULT WINAPI d3d8_device_SetTexture(IDirect3DDevice8 *iface, DWORD stage, IDirect3DBaseTexture8 *texture)
1896 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1897 struct d3d8_texture *texture_impl;
1898 HRESULT hr;
1900 TRACE("iface %p, stage %u, texture %p.\n", iface, stage, texture);
1902 texture_impl = unsafe_impl_from_IDirect3DBaseTexture8(texture);
1904 wined3d_mutex_lock();
1905 hr = wined3d_device_set_texture(device->wined3d_device, stage,
1906 texture_impl ? texture_impl->wined3d_texture : NULL);
1907 wined3d_mutex_unlock();
1909 return hr;
1912 static const struct tss_lookup
1914 BOOL sampler_state;
1915 union
1917 enum wined3d_texture_stage_state texture_state;
1918 enum wined3d_sampler_state sampler_state;
1919 } u;
1921 tss_lookup[] =
1923 {FALSE, {WINED3D_TSS_INVALID}}, /* 0, unused */
1924 {FALSE, {WINED3D_TSS_COLOR_OP}}, /* 1, D3DTSS_COLOROP */
1925 {FALSE, {WINED3D_TSS_COLOR_ARG1}}, /* 2, D3DTSS_COLORARG1 */
1926 {FALSE, {WINED3D_TSS_COLOR_ARG2}}, /* 3, D3DTSS_COLORARG2 */
1927 {FALSE, {WINED3D_TSS_ALPHA_OP}}, /* 4, D3DTSS_ALPHAOP */
1928 {FALSE, {WINED3D_TSS_ALPHA_ARG1}}, /* 5, D3DTSS_ALPHAARG1 */
1929 {FALSE, {WINED3D_TSS_ALPHA_ARG2}}, /* 6, D3DTSS_ALPHAARG2 */
1930 {FALSE, {WINED3D_TSS_BUMPENV_MAT00}}, /* 7, D3DTSS_BUMPENVMAT00 */
1931 {FALSE, {WINED3D_TSS_BUMPENV_MAT01}}, /* 8, D3DTSS_BUMPENVMAT01 */
1932 {FALSE, {WINED3D_TSS_BUMPENV_MAT10}}, /* 9, D3DTSS_BUMPENVMAT10 */
1933 {FALSE, {WINED3D_TSS_BUMPENV_MAT11}}, /* 10, D3DTSS_BUMPENVMAT11 */
1934 {FALSE, {WINED3D_TSS_TEXCOORD_INDEX}}, /* 11, D3DTSS_TEXCOORDINDEX */
1935 {FALSE, {WINED3D_TSS_INVALID}}, /* 12, unused */
1936 {TRUE, {WINED3D_SAMP_ADDRESS_U}}, /* 13, D3DTSS_ADDRESSU */
1937 {TRUE, {WINED3D_SAMP_ADDRESS_V}}, /* 14, D3DTSS_ADDRESSV */
1938 {TRUE, {WINED3D_SAMP_BORDER_COLOR}}, /* 15, D3DTSS_BORDERCOLOR */
1939 {TRUE, {WINED3D_SAMP_MAG_FILTER}}, /* 16, D3DTSS_MAGFILTER */
1940 {TRUE, {WINED3D_SAMP_MIN_FILTER}}, /* 17, D3DTSS_MINFILTER */
1941 {TRUE, {WINED3D_SAMP_MIP_FILTER}}, /* 18, D3DTSS_MIPFILTER */
1942 {TRUE, {WINED3D_SAMP_MIPMAP_LOD_BIAS}}, /* 19, D3DTSS_MIPMAPLODBIAS */
1943 {TRUE, {WINED3D_SAMP_MAX_MIP_LEVEL}}, /* 20, D3DTSS_MAXMIPLEVEL */
1944 {TRUE, {WINED3D_SAMP_MAX_ANISOTROPY}}, /* 21, D3DTSS_MAXANISOTROPY */
1945 {FALSE, {WINED3D_TSS_BUMPENV_LSCALE}}, /* 22, D3DTSS_BUMPENVLSCALE */
1946 {FALSE, {WINED3D_TSS_BUMPENV_LOFFSET}}, /* 23, D3DTSS_BUMPENVLOFFSET */
1947 {FALSE, {WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS}}, /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1948 {TRUE, {WINED3D_SAMP_ADDRESS_W}}, /* 25, D3DTSS_ADDRESSW */
1949 {FALSE, {WINED3D_TSS_COLOR_ARG0}}, /* 26, D3DTSS_COLORARG0 */
1950 {FALSE, {WINED3D_TSS_ALPHA_ARG0}}, /* 27, D3DTSS_ALPHAARG0 */
1951 {FALSE, {WINED3D_TSS_RESULT_ARG}}, /* 28, D3DTSS_RESULTARG */
1954 static HRESULT WINAPI d3d8_device_GetTextureStageState(IDirect3DDevice8 *iface,
1955 DWORD stage, D3DTEXTURESTAGESTATETYPE Type, DWORD *value)
1957 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1958 const struct tss_lookup *l;
1960 TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, stage, Type, value);
1962 if (Type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1964 WARN("Invalid Type %#x passed.\n", Type);
1965 return D3D_OK;
1968 l = &tss_lookup[Type];
1970 wined3d_mutex_lock();
1971 if (l->sampler_state)
1972 *value = wined3d_device_get_sampler_state(device->wined3d_device, stage, l->u.sampler_state);
1973 else
1974 *value = wined3d_device_get_texture_stage_state(device->wined3d_device, stage, l->u.texture_state);
1975 wined3d_mutex_unlock();
1977 return D3D_OK;
1980 static HRESULT WINAPI d3d8_device_SetTextureStageState(IDirect3DDevice8 *iface,
1981 DWORD stage, D3DTEXTURESTAGESTATETYPE type, DWORD value)
1983 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
1984 const struct tss_lookup *l;
1986 TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, stage, type, value);
1988 if (type >= sizeof(tss_lookup) / sizeof(*tss_lookup))
1990 WARN("Invalid type %#x passed.\n", type);
1991 return D3D_OK;
1994 l = &tss_lookup[type];
1996 wined3d_mutex_lock();
1997 if (l->sampler_state)
1998 wined3d_device_set_sampler_state(device->wined3d_device, stage, l->u.sampler_state, value);
1999 else
2000 wined3d_device_set_texture_stage_state(device->wined3d_device, stage, l->u.texture_state, value);
2001 wined3d_mutex_unlock();
2003 return D3D_OK;
2006 static HRESULT WINAPI d3d8_device_ValidateDevice(IDirect3DDevice8 *iface, DWORD *pass_count)
2008 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2009 HRESULT hr;
2011 TRACE("iface %p, pass_count %p.\n", iface, pass_count);
2013 wined3d_mutex_lock();
2014 hr = wined3d_device_validate_device(device->wined3d_device, pass_count);
2015 wined3d_mutex_unlock();
2017 return hr;
2020 static HRESULT WINAPI d3d8_device_GetInfo(IDirect3DDevice8 *iface,
2021 DWORD info_id, void *info, DWORD info_size)
2023 FIXME("iface %p, info_id %#x, info %p, info_size %u stub!\n", iface, info_id, info, info_size);
2025 return D3D_OK;
2028 static HRESULT WINAPI d3d8_device_SetPaletteEntries(IDirect3DDevice8 *iface,
2029 UINT palette_idx, const PALETTEENTRY *entries)
2031 WARN("iface %p, palette_idx %u, entries %p unimplemented\n", iface, palette_idx, entries);
2033 /* GPUs stopped supporting palettized textures with the Shader Model 1 generation. Wined3d
2034 * does not have a d3d8/9-style palette API */
2036 return D3D_OK;
2039 static HRESULT WINAPI d3d8_device_GetPaletteEntries(IDirect3DDevice8 *iface,
2040 UINT palette_idx, PALETTEENTRY *entries)
2042 FIXME("iface %p, palette_idx %u, entries %p unimplemented.\n", iface, palette_idx, entries);
2044 return D3DERR_INVALIDCALL;
2047 static HRESULT WINAPI d3d8_device_SetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT palette_idx)
2049 WARN("iface %p, palette_idx %u unimplemented.\n", iface, palette_idx);
2051 return D3D_OK;
2054 static HRESULT WINAPI d3d8_device_GetCurrentTexturePalette(IDirect3DDevice8 *iface, UINT *palette_idx)
2056 FIXME("iface %p, palette_idx %p unimplemented.\n", iface, palette_idx);
2058 return D3DERR_INVALIDCALL;
2061 static HRESULT WINAPI d3d8_device_DrawPrimitive(IDirect3DDevice8 *iface,
2062 D3DPRIMITIVETYPE primitive_type, UINT start_vertex, UINT primitive_count)
2064 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2065 HRESULT hr;
2067 TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
2068 iface, primitive_type, start_vertex, primitive_count);
2070 wined3d_mutex_lock();
2071 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2072 hr = wined3d_device_draw_primitive(device->wined3d_device, start_vertex,
2073 vertex_count_from_primitive_count(primitive_type, primitive_count));
2074 wined3d_mutex_unlock();
2076 return hr;
2079 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitive(IDirect3DDevice8 *iface,
2080 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2081 UINT start_idx, UINT primitive_count)
2083 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2084 HRESULT hr;
2086 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, start_idx %u, primitive_count %u.\n",
2087 iface, primitive_type, min_vertex_idx, vertex_count, start_idx, primitive_count);
2089 wined3d_mutex_lock();
2090 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2091 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, start_idx,
2092 vertex_count_from_primitive_count(primitive_type, primitive_count));
2093 wined3d_mutex_unlock();
2095 return hr;
2098 /* The caller is responsible for wined3d locking */
2099 static HRESULT d3d8_device_prepare_vertex_buffer(struct d3d8_device *device, UINT min_size)
2101 HRESULT hr;
2103 if (device->vertex_buffer_size < min_size || !device->vertex_buffer)
2105 UINT size = max(device->vertex_buffer_size * 2, min_size);
2106 struct wined3d_buffer *buffer;
2108 TRACE("Growing vertex buffer to %u bytes\n", size);
2110 hr = wined3d_buffer_create_vb(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2111 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2112 if (FAILED(hr))
2114 ERR("(%p) wined3d_buffer_create_vb failed with hr = %08x\n", device, hr);
2115 return hr;
2118 if (device->vertex_buffer)
2119 wined3d_buffer_decref(device->vertex_buffer);
2121 device->vertex_buffer = buffer;
2122 device->vertex_buffer_size = size;
2123 device->vertex_buffer_pos = 0;
2125 return D3D_OK;
2128 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2129 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2130 UINT stride)
2132 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2133 HRESULT hr;
2134 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2135 struct wined3d_map_desc wined3d_map_desc;
2136 struct wined3d_box wined3d_box = {0};
2137 UINT size = vtx_count * stride;
2138 struct wined3d_resource *vb;
2139 UINT vb_pos, align;
2141 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2142 iface, primitive_type, primitive_count, data, stride);
2144 if (!primitive_count)
2146 WARN("primitive_count is 0, returning D3D_OK\n");
2147 return D3D_OK;
2150 wined3d_mutex_lock();
2151 hr = d3d8_device_prepare_vertex_buffer(device, size);
2152 if (FAILED(hr))
2153 goto done;
2155 vb_pos = device->vertex_buffer_pos;
2156 align = vb_pos % stride;
2157 if (align) align = stride - align;
2158 if (vb_pos + size + align > device->vertex_buffer_size)
2159 vb_pos = 0;
2160 else
2161 vb_pos += align;
2163 wined3d_box.left = vb_pos;
2164 wined3d_box.right = vb_pos + size;
2165 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2166 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2167 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2168 goto done;
2169 memcpy(wined3d_map_desc.data, data, size);
2170 wined3d_resource_unmap(vb, 0);
2171 device->vertex_buffer_pos = vb_pos + size;
2173 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2174 if (FAILED(hr))
2175 goto done;
2177 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2178 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2179 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2181 done:
2182 wined3d_mutex_unlock();
2183 return hr;
2186 /* The caller is responsible for wined3d locking */
2187 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2189 HRESULT hr;
2191 if (device->index_buffer_size < min_size || !device->index_buffer)
2193 UINT size = max(device->index_buffer_size * 2, min_size);
2194 struct wined3d_buffer *buffer;
2196 TRACE("Growing index buffer to %u bytes\n", size);
2198 hr = wined3d_buffer_create_ib(device->wined3d_device, size, WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY,
2199 WINED3D_POOL_DEFAULT, NULL, &d3d8_null_wined3d_parent_ops, &buffer);
2200 if (FAILED(hr))
2202 ERR("(%p) wined3d_buffer_create_ib failed with hr = %08x\n", device, hr);
2203 return hr;
2206 if (device->index_buffer)
2207 wined3d_buffer_decref(device->index_buffer);
2209 device->index_buffer = buffer;
2210 device->index_buffer_size = size;
2211 device->index_buffer_pos = 0;
2213 return D3D_OK;
2216 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2217 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2218 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2219 const void *vertex_data, UINT vertex_stride)
2221 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2222 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2223 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2224 UINT vtx_size = vertex_count * vertex_stride;
2225 UINT idx_size = idx_count * idx_fmt_size;
2226 struct wined3d_map_desc wined3d_map_desc;
2227 struct wined3d_box wined3d_box = {0};
2228 struct wined3d_resource *ib, *vb;
2229 UINT vb_pos, ib_pos, align;
2230 HRESULT hr;
2232 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2233 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2234 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2235 index_data, index_format, vertex_data, vertex_stride);
2237 if (!primitive_count)
2239 WARN("primitive_count is 0, returning D3D_OK\n");
2240 return D3D_OK;
2243 wined3d_mutex_lock();
2245 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2246 if (FAILED(hr))
2247 goto done;
2249 vb_pos = device->vertex_buffer_pos;
2250 align = vb_pos % vertex_stride;
2251 if (align) align = vertex_stride - align;
2252 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2253 vb_pos = 0;
2254 else
2255 vb_pos += align;
2257 wined3d_box.left = vb_pos;
2258 wined3d_box.right = vb_pos + vtx_size;
2259 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2260 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2261 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2262 goto done;
2263 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2264 wined3d_resource_unmap(vb, 0);
2265 device->vertex_buffer_pos = vb_pos + vtx_size;
2267 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2268 if (FAILED(hr))
2269 goto done;
2271 ib_pos = device->index_buffer_pos;
2272 align = ib_pos % idx_fmt_size;
2273 if (align) align = idx_fmt_size - align;
2274 if (ib_pos + idx_size + align > device->index_buffer_size)
2275 ib_pos = 0;
2276 else
2277 ib_pos += align;
2279 wined3d_box.left = ib_pos;
2280 wined3d_box.right = ib_pos + idx_size;
2281 ib = wined3d_buffer_get_resource(device->index_buffer);
2282 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2283 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2284 goto done;
2285 memcpy(wined3d_map_desc.data, index_data, idx_size);
2286 wined3d_resource_unmap(ib, 0);
2287 device->index_buffer_pos = ib_pos + idx_size;
2289 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2290 if (FAILED(hr))
2291 goto done;
2293 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2294 wined3dformat_from_d3dformat(index_format), 0);
2295 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2297 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2298 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2300 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2301 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2302 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2304 done:
2305 wined3d_mutex_unlock();
2306 return hr;
2309 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2310 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2312 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2313 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2314 HRESULT hr;
2316 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2317 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2319 wined3d_mutex_lock();
2320 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2321 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2322 wined3d_mutex_unlock();
2324 return hr;
2327 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2328 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2330 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2331 struct d3d8_vertex_shader *object;
2332 DWORD shader_handle;
2333 DWORD handle;
2334 HRESULT hr;
2336 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2337 iface, declaration, byte_code, shader, usage);
2339 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2340 if (!object)
2342 *shader = 0;
2343 return E_OUTOFMEMORY;
2346 wined3d_mutex_lock();
2347 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2348 wined3d_mutex_unlock();
2349 if (handle == D3D8_INVALID_HANDLE)
2351 ERR("Failed to allocate vertex shader handle.\n");
2352 HeapFree(GetProcessHeap(), 0, object);
2353 *shader = 0;
2354 return E_OUTOFMEMORY;
2357 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2359 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2360 if (FAILED(hr))
2362 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2363 wined3d_mutex_lock();
2364 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2365 wined3d_mutex_unlock();
2366 HeapFree(GetProcessHeap(), 0, object);
2367 *shader = 0;
2368 return hr;
2371 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2372 *shader = shader_handle;
2374 return D3D_OK;
2377 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2379 struct d3d8_vertex_declaration *d3d8_declaration;
2380 struct FvfToDecl *convertedDecls = device->decls;
2381 int p, low, high; /* deliberately signed */
2382 HRESULT hr;
2384 TRACE("Searching for declaration for fvf %08x... ", fvf);
2386 low = 0;
2387 high = device->numConvertedDecls - 1;
2388 while (low <= high)
2390 p = (low + high) >> 1;
2391 TRACE("%d ", p);
2393 if (convertedDecls[p].fvf == fvf)
2395 TRACE("found %p\n", convertedDecls[p].declaration);
2396 return convertedDecls[p].declaration;
2399 if (convertedDecls[p].fvf < fvf)
2400 low = p + 1;
2401 else
2402 high = p - 1;
2404 TRACE("not found. Creating and inserting at position %d.\n", low);
2406 if (!(d3d8_declaration = HeapAlloc(GetProcessHeap(), 0, sizeof(*d3d8_declaration))))
2407 return NULL;
2409 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2411 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2412 HeapFree(GetProcessHeap(), 0, d3d8_declaration);
2413 return NULL;
2416 if (device->declArraySize == device->numConvertedDecls)
2418 UINT grow = device->declArraySize / 2;
2420 convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
2421 sizeof(*convertedDecls) * (device->numConvertedDecls + grow));
2422 if (!convertedDecls)
2424 d3d8_vertex_declaration_destroy(d3d8_declaration);
2425 return NULL;
2427 device->decls = convertedDecls;
2428 device->declArraySize += grow;
2431 memmove(convertedDecls + low + 1, convertedDecls + low,
2432 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2433 convertedDecls[low].declaration = d3d8_declaration;
2434 convertedDecls[low].fvf = fvf;
2435 ++device->numConvertedDecls;
2437 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2439 return d3d8_declaration;
2442 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2444 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2445 struct d3d8_vertex_shader *shader_impl;
2447 TRACE("iface %p, shader %#x.\n", iface, shader);
2449 if (VS_HIGHESTFIXEDFXF >= shader)
2451 TRACE("Setting FVF, %#x\n", shader);
2453 wined3d_mutex_lock();
2454 wined3d_device_set_vertex_declaration(device->wined3d_device,
2455 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2456 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2457 wined3d_mutex_unlock();
2459 return D3D_OK;
2462 TRACE("Setting shader\n");
2464 wined3d_mutex_lock();
2465 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2467 WARN("Invalid handle (%#x) passed.\n", shader);
2468 wined3d_mutex_unlock();
2470 return D3DERR_INVALIDCALL;
2473 wined3d_device_set_vertex_declaration(device->wined3d_device,
2474 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2475 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2476 wined3d_mutex_unlock();
2478 return D3D_OK;
2481 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2483 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2484 struct wined3d_vertex_declaration *wined3d_declaration;
2485 struct d3d8_vertex_declaration *d3d8_declaration;
2487 TRACE("iface %p, shader %p.\n", iface, shader);
2489 wined3d_mutex_lock();
2490 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2492 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2493 *shader = d3d8_declaration->shader_handle;
2495 else
2497 *shader = 0;
2499 wined3d_mutex_unlock();
2501 TRACE("Returning %#x.\n", *shader);
2503 return D3D_OK;
2506 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2508 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2509 struct d3d8_vertex_shader *shader_impl;
2511 TRACE("iface %p, shader %#x.\n", iface, shader);
2513 wined3d_mutex_lock();
2514 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2516 WARN("Invalid handle (%#x) passed.\n", shader);
2517 wined3d_mutex_unlock();
2519 return D3DERR_INVALIDCALL;
2522 if (shader_impl->wined3d_shader
2523 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2524 IDirect3DDevice8_SetVertexShader(iface, 0);
2526 wined3d_mutex_unlock();
2528 d3d8_vertex_shader_destroy(shader_impl);
2530 return D3D_OK;
2533 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2534 DWORD start_register, const void *data, DWORD count)
2536 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2537 HRESULT hr;
2539 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2540 iface, start_register, data, count);
2542 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2544 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2545 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2546 return D3DERR_INVALIDCALL;
2549 wined3d_mutex_lock();
2550 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2551 wined3d_mutex_unlock();
2553 return hr;
2556 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2557 DWORD start_register, void *data, DWORD count)
2559 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2560 HRESULT hr;
2562 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2563 iface, start_register, data, count);
2565 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2567 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2568 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2569 return D3DERR_INVALIDCALL;
2572 wined3d_mutex_lock();
2573 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2574 wined3d_mutex_unlock();
2576 return hr;
2579 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2580 DWORD shader, void *data, DWORD *data_size)
2582 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2583 struct d3d8_vertex_declaration *declaration;
2584 struct d3d8_vertex_shader *shader_impl;
2586 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2587 iface, shader, data, data_size);
2589 wined3d_mutex_lock();
2590 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2591 wined3d_mutex_unlock();
2593 if (!shader_impl)
2595 WARN("Invalid handle (%#x) passed.\n", shader);
2596 return D3DERR_INVALIDCALL;
2598 declaration = shader_impl->vertex_declaration;
2600 if (!data)
2602 *data_size = declaration->elements_size;
2603 return D3D_OK;
2606 /* MSDN claims that if *data_size is smaller than the required size
2607 * we should write the required size and return D3DERR_MOREDATA.
2608 * That's not actually true. */
2609 if (*data_size < declaration->elements_size)
2610 return D3DERR_INVALIDCALL;
2612 memcpy(data, declaration->elements, declaration->elements_size);
2614 return D3D_OK;
2617 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2618 DWORD shader, void *data, DWORD *data_size)
2620 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2621 struct d3d8_vertex_shader *shader_impl = NULL;
2622 HRESULT hr;
2624 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2625 iface, shader, data, data_size);
2627 wined3d_mutex_lock();
2628 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2630 WARN("Invalid handle (%#x) passed.\n", shader);
2631 wined3d_mutex_unlock();
2633 return D3DERR_INVALIDCALL;
2636 if (!shader_impl->wined3d_shader)
2638 wined3d_mutex_unlock();
2639 *data_size = 0;
2640 return D3D_OK;
2643 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2644 wined3d_mutex_unlock();
2646 return hr;
2649 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2650 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2652 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2653 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2655 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2657 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2658 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2659 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2660 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2661 * problem)
2663 wined3d_mutex_lock();
2664 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2665 wined3d_device_set_index_buffer(device->wined3d_device,
2666 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2667 wined3d_mutex_unlock();
2669 return D3D_OK;
2672 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2673 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2675 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2676 enum wined3d_format_id wined3d_format;
2677 struct wined3d_buffer *wined3d_buffer;
2678 struct d3d8_indexbuffer *buffer_impl;
2680 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2682 if (!buffer)
2683 return D3DERR_INVALIDCALL;
2685 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2686 wined3d_mutex_lock();
2687 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2688 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2690 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2691 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2692 IDirect3DIndexBuffer8_AddRef(*buffer);
2694 else
2696 *buffer = NULL;
2698 wined3d_mutex_unlock();
2700 return D3D_OK;
2703 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2704 const DWORD *byte_code, DWORD *shader)
2706 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2707 struct d3d8_pixel_shader *object;
2708 DWORD shader_handle;
2709 DWORD handle;
2710 HRESULT hr;
2712 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2714 if (!shader)
2715 return D3DERR_INVALIDCALL;
2717 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2718 if (!object)
2719 return E_OUTOFMEMORY;
2721 wined3d_mutex_lock();
2722 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2723 wined3d_mutex_unlock();
2724 if (handle == D3D8_INVALID_HANDLE)
2726 ERR("Failed to allocate pixel shader handle.\n");
2727 HeapFree(GetProcessHeap(), 0, object);
2728 return E_OUTOFMEMORY;
2731 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2733 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2734 if (FAILED(hr))
2736 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2737 wined3d_mutex_lock();
2738 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2739 wined3d_mutex_unlock();
2740 HeapFree(GetProcessHeap(), 0, object);
2741 *shader = 0;
2742 return hr;
2745 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2746 *shader = shader_handle;
2748 return D3D_OK;
2751 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2753 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2754 struct d3d8_pixel_shader *shader_impl;
2756 TRACE("iface %p, shader %#x.\n", iface, shader);
2758 wined3d_mutex_lock();
2760 if (!shader)
2762 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2763 wined3d_mutex_unlock();
2764 return D3D_OK;
2767 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2769 WARN("Invalid handle (%#x) passed.\n", shader);
2770 wined3d_mutex_unlock();
2771 return D3DERR_INVALIDCALL;
2774 TRACE("Setting shader %p.\n", shader_impl);
2775 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2776 wined3d_mutex_unlock();
2778 return D3D_OK;
2781 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2783 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2784 struct wined3d_shader *object;
2786 TRACE("iface %p, shader %p.\n", iface, shader);
2788 if (!shader)
2789 return D3DERR_INVALIDCALL;
2791 wined3d_mutex_lock();
2792 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2794 struct d3d8_pixel_shader *d3d8_shader;
2795 d3d8_shader = wined3d_shader_get_parent(object);
2796 *shader = d3d8_shader->handle;
2798 else
2800 *shader = 0;
2802 wined3d_mutex_unlock();
2804 TRACE("Returning %#x.\n", *shader);
2806 return D3D_OK;
2809 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2811 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2812 struct d3d8_pixel_shader *shader_impl;
2814 TRACE("iface %p, shader %#x.\n", iface, shader);
2816 wined3d_mutex_lock();
2818 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2820 WARN("Invalid handle (%#x) passed.\n", shader);
2821 wined3d_mutex_unlock();
2822 return D3DERR_INVALIDCALL;
2825 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2826 IDirect3DDevice8_SetPixelShader(iface, 0);
2828 wined3d_mutex_unlock();
2830 d3d8_pixel_shader_destroy(shader_impl);
2832 return D3D_OK;
2835 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2836 DWORD start_register, const void *data, DWORD count)
2838 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2839 HRESULT hr;
2841 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2842 iface, start_register, data, count);
2844 wined3d_mutex_lock();
2845 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
2846 wined3d_mutex_unlock();
2848 return hr;
2851 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2852 DWORD start_register, void *data, DWORD count)
2854 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2855 HRESULT hr;
2857 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2858 iface, start_register, data, count);
2860 wined3d_mutex_lock();
2861 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
2862 wined3d_mutex_unlock();
2864 return hr;
2867 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2868 DWORD shader, void *data, DWORD *data_size)
2870 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2871 struct d3d8_pixel_shader *shader_impl = NULL;
2872 HRESULT hr;
2874 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2875 iface, shader, data, data_size);
2877 wined3d_mutex_lock();
2878 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2880 WARN("Invalid handle (%#x) passed.\n", shader);
2881 wined3d_mutex_unlock();
2883 return D3DERR_INVALIDCALL;
2886 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2887 wined3d_mutex_unlock();
2889 return hr;
2892 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2893 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2895 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2896 iface, handle, segment_count, patch_info);
2897 return D3D_OK;
2900 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2901 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2903 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2904 iface, handle, segment_count, patch_info);
2905 return D3D_OK;
2908 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2910 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2911 return D3DERR_INVALIDCALL;
2914 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2915 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2917 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2918 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2919 HRESULT hr;
2921 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2922 iface, stream_idx, buffer, stride);
2924 wined3d_mutex_lock();
2925 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2926 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2927 wined3d_mutex_unlock();
2929 return hr;
2932 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2933 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2935 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2936 struct d3d8_vertexbuffer *buffer_impl;
2937 struct wined3d_buffer *wined3d_buffer = NULL;
2938 HRESULT hr;
2940 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2941 iface, stream_idx, buffer, stride);
2943 if (!buffer)
2944 return D3DERR_INVALIDCALL;
2946 wined3d_mutex_lock();
2947 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2948 if (SUCCEEDED(hr) && wined3d_buffer)
2950 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2951 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2952 IDirect3DVertexBuffer8_AddRef(*buffer);
2954 else
2956 if (FAILED(hr))
2957 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2958 *buffer = NULL;
2960 wined3d_mutex_unlock();
2962 return hr;
2965 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2967 d3d8_device_QueryInterface,
2968 d3d8_device_AddRef,
2969 d3d8_device_Release,
2970 d3d8_device_TestCooperativeLevel,
2971 d3d8_device_GetAvailableTextureMem,
2972 d3d8_device_ResourceManagerDiscardBytes,
2973 d3d8_device_GetDirect3D,
2974 d3d8_device_GetDeviceCaps,
2975 d3d8_device_GetDisplayMode,
2976 d3d8_device_GetCreationParameters,
2977 d3d8_device_SetCursorProperties,
2978 d3d8_device_SetCursorPosition,
2979 d3d8_device_ShowCursor,
2980 d3d8_device_CreateAdditionalSwapChain,
2981 d3d8_device_Reset,
2982 d3d8_device_Present,
2983 d3d8_device_GetBackBuffer,
2984 d3d8_device_GetRasterStatus,
2985 d3d8_device_SetGammaRamp,
2986 d3d8_device_GetGammaRamp,
2987 d3d8_device_CreateTexture,
2988 d3d8_device_CreateVolumeTexture,
2989 d3d8_device_CreateCubeTexture,
2990 d3d8_device_CreateVertexBuffer,
2991 d3d8_device_CreateIndexBuffer,
2992 d3d8_device_CreateRenderTarget,
2993 d3d8_device_CreateDepthStencilSurface,
2994 d3d8_device_CreateImageSurface,
2995 d3d8_device_CopyRects,
2996 d3d8_device_UpdateTexture,
2997 d3d8_device_GetFrontBuffer,
2998 d3d8_device_SetRenderTarget,
2999 d3d8_device_GetRenderTarget,
3000 d3d8_device_GetDepthStencilSurface,
3001 d3d8_device_BeginScene,
3002 d3d8_device_EndScene,
3003 d3d8_device_Clear,
3004 d3d8_device_SetTransform,
3005 d3d8_device_GetTransform,
3006 d3d8_device_MultiplyTransform,
3007 d3d8_device_SetViewport,
3008 d3d8_device_GetViewport,
3009 d3d8_device_SetMaterial,
3010 d3d8_device_GetMaterial,
3011 d3d8_device_SetLight,
3012 d3d8_device_GetLight,
3013 d3d8_device_LightEnable,
3014 d3d8_device_GetLightEnable,
3015 d3d8_device_SetClipPlane,
3016 d3d8_device_GetClipPlane,
3017 d3d8_device_SetRenderState,
3018 d3d8_device_GetRenderState,
3019 d3d8_device_BeginStateBlock,
3020 d3d8_device_EndStateBlock,
3021 d3d8_device_ApplyStateBlock,
3022 d3d8_device_CaptureStateBlock,
3023 d3d8_device_DeleteStateBlock,
3024 d3d8_device_CreateStateBlock,
3025 d3d8_device_SetClipStatus,
3026 d3d8_device_GetClipStatus,
3027 d3d8_device_GetTexture,
3028 d3d8_device_SetTexture,
3029 d3d8_device_GetTextureStageState,
3030 d3d8_device_SetTextureStageState,
3031 d3d8_device_ValidateDevice,
3032 d3d8_device_GetInfo,
3033 d3d8_device_SetPaletteEntries,
3034 d3d8_device_GetPaletteEntries,
3035 d3d8_device_SetCurrentTexturePalette,
3036 d3d8_device_GetCurrentTexturePalette,
3037 d3d8_device_DrawPrimitive,
3038 d3d8_device_DrawIndexedPrimitive,
3039 d3d8_device_DrawPrimitiveUP,
3040 d3d8_device_DrawIndexedPrimitiveUP,
3041 d3d8_device_ProcessVertices,
3042 d3d8_device_CreateVertexShader,
3043 d3d8_device_SetVertexShader,
3044 d3d8_device_GetVertexShader,
3045 d3d8_device_DeleteVertexShader,
3046 d3d8_device_SetVertexShaderConstant,
3047 d3d8_device_GetVertexShaderConstant,
3048 d3d8_device_GetVertexShaderDeclaration,
3049 d3d8_device_GetVertexShaderFunction,
3050 d3d8_device_SetStreamSource,
3051 d3d8_device_GetStreamSource,
3052 d3d8_device_SetIndices,
3053 d3d8_device_GetIndices,
3054 d3d8_device_CreatePixelShader,
3055 d3d8_device_SetPixelShader,
3056 d3d8_device_GetPixelShader,
3057 d3d8_device_DeletePixelShader,
3058 d3d8_device_SetPixelShaderConstant,
3059 d3d8_device_GetPixelShaderConstant,
3060 d3d8_device_GetPixelShaderFunction,
3061 d3d8_device_DrawRectPatch,
3062 d3d8_device_DrawTriPatch,
3063 d3d8_device_DeletePatch,
3066 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3068 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3071 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3072 struct wined3d_device *device)
3074 TRACE("device_parent %p, device %p\n", device_parent, device);
3077 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3079 TRACE("device_parent %p.\n", device_parent);
3082 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3084 struct d3d8_device *device = device_from_device_parent(device_parent);
3086 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3088 if (!activate)
3089 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3090 else
3091 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3094 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3095 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3096 void **parent, const struct wined3d_parent_ops **parent_ops)
3098 struct d3d8_surface *d3d_surface;
3100 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3101 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3103 if (!(d3d_surface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_surface))))
3104 return E_OUTOFMEMORY;
3106 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3107 *parent = d3d_surface;
3108 TRACE("Created surface %p.\n", d3d_surface);
3110 return D3D_OK;
3113 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3114 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3115 void **parent, const struct wined3d_parent_ops **parent_ops)
3117 struct d3d8_volume *d3d_volume;
3119 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3120 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3122 if (!(d3d_volume = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*d3d_volume))))
3123 return E_OUTOFMEMORY;
3125 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3126 *parent = d3d_volume;
3127 TRACE("Created volume %p.\n", d3d_volume);
3129 return D3D_OK;
3132 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3133 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3134 struct wined3d_texture **texture)
3136 struct d3d8_device *device = device_from_device_parent(device_parent);
3137 struct d3d8_surface *d3d_surface;
3138 HRESULT hr;
3140 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3141 device_parent, container_parent, desc, texture_flags, texture);
3143 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3144 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface,
3145 &d3d8_null_wined3d_parent_ops, texture)))
3147 WARN("Failed to create texture, hr %#x.\n", hr);
3148 return hr;
3151 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3152 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3154 return hr;
3157 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3158 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3160 struct d3d8_device *device = device_from_device_parent(device_parent);
3161 struct d3d8_swapchain *d3d_swapchain;
3162 HRESULT hr;
3164 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3166 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3168 WARN("Failed to create swapchain, hr %#x.\n", hr);
3169 *swapchain = NULL;
3170 return hr;
3173 *swapchain = d3d_swapchain->wined3d_swapchain;
3174 wined3d_swapchain_incref(*swapchain);
3175 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3177 return hr;
3180 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3182 device_parent_wined3d_device_created,
3183 device_parent_mode_changed,
3184 device_parent_activate,
3185 device_parent_surface_created,
3186 device_parent_volume_created,
3187 device_parent_create_swapchain_texture,
3188 device_parent_create_swapchain,
3191 static void setup_fpu(void)
3193 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3194 WORD cw;
3195 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3196 cw = (cw & ~0xf3f) | 0x3f;
3197 __asm__ volatile ("fldcw %0" : : "m" (cw));
3198 #elif defined(__i386__) && defined(_MSC_VER)
3199 WORD cw;
3200 __asm fnstcw cw;
3201 cw = (cw & ~0xf3f) | 0x3f;
3202 __asm fldcw cw;
3203 #else
3204 FIXME("FPU setup not implemented for this platform.\n");
3205 #endif
3208 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3209 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3211 struct wined3d_swapchain_desc swapchain_desc;
3212 struct wined3d_swapchain *wined3d_swapchain;
3213 HRESULT hr;
3215 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3216 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3217 device->ref = 1;
3218 device->handle_table.entries = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
3219 D3D8_INITIAL_HANDLE_TABLE_SIZE * sizeof(*device->handle_table.entries));
3220 if (!device->handle_table.entries)
3222 ERR("Failed to allocate handle table memory.\n");
3223 return E_OUTOFMEMORY;
3225 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3227 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3229 wined3d_mutex_lock();
3230 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3231 &device->device_parent, &device->wined3d_device);
3232 if (FAILED(hr))
3234 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3235 wined3d_mutex_unlock();
3236 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3237 return hr;
3240 if (!parameters->Windowed)
3242 HWND device_window = parameters->hDeviceWindow;
3244 if (!focus_window)
3245 focus_window = device_window;
3246 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3248 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3249 wined3d_device_decref(device->wined3d_device);
3250 wined3d_mutex_unlock();
3251 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3252 return hr;
3255 if (!device_window)
3256 device_window = focus_window;
3257 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3258 parameters->BackBufferWidth,
3259 parameters->BackBufferHeight);
3262 if (flags & D3DCREATE_MULTITHREADED)
3263 wined3d_device_set_multithreaded(device->wined3d_device);
3265 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3267 wined3d_device_release_focus_window(device->wined3d_device);
3268 wined3d_device_decref(device->wined3d_device);
3269 wined3d_mutex_unlock();
3270 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3271 return D3DERR_INVALIDCALL;
3274 hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc);
3275 if (FAILED(hr))
3277 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3278 wined3d_device_release_focus_window(device->wined3d_device);
3279 wined3d_device_decref(device->wined3d_device);
3280 wined3d_mutex_unlock();
3281 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3282 return hr;
3285 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3286 wined3d_mutex_unlock();
3288 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3290 device->declArraySize = 16;
3291 device->decls = HeapAlloc(GetProcessHeap(), 0, device->declArraySize * sizeof(*device->decls));
3292 if (!device->decls)
3294 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3295 hr = E_OUTOFMEMORY;
3296 goto err;
3299 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3300 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3302 device->d3d_parent = &parent->IDirect3D8_iface;
3303 IDirect3D8_AddRef(device->d3d_parent);
3305 return D3D_OK;
3307 err:
3308 wined3d_mutex_lock();
3309 wined3d_device_uninit_3d(device->wined3d_device);
3310 wined3d_device_release_focus_window(device->wined3d_device);
3311 wined3d_device_decref(device->wined3d_device);
3312 wined3d_mutex_unlock();
3313 HeapFree(GetProcessHeap(), 0, device->handle_table.entries);
3314 return hr;