d3d8: Set WINED3D_RESOURCE_ACCESS_MAP on 2D textures.
[wine.git] / dlls / d3d8 / device.c
blob613cb1e4cbb12bea5941c0a4ce2376ce68c7cf91
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->backbuffer_usage = WINED3DUSAGE_RENDERTARGET;
211 swapchain_desc->multisample_type = present_parameters->MultiSampleType;
212 swapchain_desc->multisample_quality = 0; /* d3d9 only */
213 swapchain_desc->swap_effect = present_parameters->SwapEffect;
214 swapchain_desc->device_window = present_parameters->hDeviceWindow;
215 swapchain_desc->windowed = present_parameters->Windowed;
216 swapchain_desc->enable_auto_depth_stencil = present_parameters->EnableAutoDepthStencil;
217 swapchain_desc->auto_depth_stencil_format
218 = wined3dformat_from_d3dformat(present_parameters->AutoDepthStencilFormat);
219 swapchain_desc->flags
220 = (present_parameters->Flags & D3DPRESENTFLAGS_MASK) | WINED3D_SWAPCHAIN_ALLOW_MODE_SWITCH;
221 swapchain_desc->refresh_rate = present_parameters->FullScreen_RefreshRateInHz;
222 swapchain_desc->swap_interval = present_parameters->FullScreen_PresentationInterval;
223 swapchain_desc->auto_restore_display_mode = TRUE;
225 if (present_parameters->Flags & ~D3DPRESENTFLAGS_MASK)
226 FIXME("Unhandled flags %#x.\n", present_parameters->Flags & ~D3DPRESENTFLAGS_MASK);
228 return TRUE;
231 void d3dcaps_from_wined3dcaps(D3DCAPS8 *caps, const WINED3DCAPS *wined3d_caps)
233 caps->DeviceType = (D3DDEVTYPE)wined3d_caps->DeviceType;
234 caps->AdapterOrdinal = wined3d_caps->AdapterOrdinal;
235 caps->Caps = wined3d_caps->Caps;
236 caps->Caps2 = wined3d_caps->Caps2;
237 caps->Caps3 = wined3d_caps->Caps3;
238 caps->PresentationIntervals = wined3d_caps->PresentationIntervals;
239 caps->CursorCaps = wined3d_caps->CursorCaps;
240 caps->DevCaps = wined3d_caps->DevCaps;
241 caps->PrimitiveMiscCaps = wined3d_caps->PrimitiveMiscCaps;
242 caps->RasterCaps = wined3d_caps->RasterCaps;
243 caps->ZCmpCaps = wined3d_caps->ZCmpCaps;
244 caps->SrcBlendCaps = wined3d_caps->SrcBlendCaps;
245 caps->DestBlendCaps = wined3d_caps->DestBlendCaps;
246 caps->AlphaCmpCaps = wined3d_caps->AlphaCmpCaps;
247 caps->ShadeCaps = wined3d_caps->ShadeCaps;
248 caps->TextureCaps = wined3d_caps->TextureCaps;
249 caps->TextureFilterCaps = wined3d_caps->TextureFilterCaps;
250 caps->CubeTextureFilterCaps = wined3d_caps->CubeTextureFilterCaps;
251 caps->VolumeTextureFilterCaps = wined3d_caps->VolumeTextureFilterCaps;
252 caps->TextureAddressCaps = wined3d_caps->TextureAddressCaps;
253 caps->VolumeTextureAddressCaps = wined3d_caps->VolumeTextureAddressCaps;
254 caps->LineCaps = wined3d_caps->LineCaps;
255 caps->MaxTextureWidth = wined3d_caps->MaxTextureWidth;
256 caps->MaxTextureHeight = wined3d_caps->MaxTextureHeight;
257 caps->MaxVolumeExtent = wined3d_caps->MaxVolumeExtent;
258 caps->MaxTextureRepeat = wined3d_caps->MaxTextureRepeat;
259 caps->MaxTextureAspectRatio = wined3d_caps->MaxTextureAspectRatio;
260 caps->MaxAnisotropy = wined3d_caps->MaxAnisotropy;
261 caps->MaxVertexW = wined3d_caps->MaxVertexW;
262 caps->GuardBandLeft = wined3d_caps->GuardBandLeft;
263 caps->GuardBandTop = wined3d_caps->GuardBandTop;
264 caps->GuardBandRight = wined3d_caps->GuardBandRight;
265 caps->GuardBandBottom = wined3d_caps->GuardBandBottom;
266 caps->ExtentsAdjust = wined3d_caps->ExtentsAdjust;
267 caps->StencilCaps = wined3d_caps->StencilCaps;
268 caps->FVFCaps = wined3d_caps->FVFCaps;
269 caps->TextureOpCaps = wined3d_caps->TextureOpCaps;
270 caps->MaxTextureBlendStages = wined3d_caps->MaxTextureBlendStages;
271 caps->MaxSimultaneousTextures = wined3d_caps->MaxSimultaneousTextures;
272 caps->VertexProcessingCaps = wined3d_caps->VertexProcessingCaps;
273 caps->MaxActiveLights = wined3d_caps->MaxActiveLights;
274 caps->MaxUserClipPlanes = wined3d_caps->MaxUserClipPlanes;
275 caps->MaxVertexBlendMatrices = wined3d_caps->MaxVertexBlendMatrices;
276 caps->MaxVertexBlendMatrixIndex = wined3d_caps->MaxVertexBlendMatrixIndex;
277 caps->MaxPointSize = wined3d_caps->MaxPointSize;
278 caps->MaxPrimitiveCount = wined3d_caps->MaxPrimitiveCount;
279 caps->MaxVertexIndex = wined3d_caps->MaxVertexIndex;
280 caps->MaxStreams = wined3d_caps->MaxStreams;
281 caps->MaxStreamStride = wined3d_caps->MaxStreamStride;
282 caps->VertexShaderVersion = wined3d_caps->VertexShaderVersion;
283 caps->MaxVertexShaderConst = wined3d_caps->MaxVertexShaderConst;
284 caps->PixelShaderVersion = wined3d_caps->PixelShaderVersion;
285 caps->MaxPixelShaderValue = wined3d_caps->PixelShader1xMaxValue;
287 /* D3D8 doesn't support SM 2.0 or higher, so clamp to 1.x */
288 if (caps->PixelShaderVersion)
289 caps->PixelShaderVersion = D3DPS_VERSION(1, 4);
290 else
291 caps->PixelShaderVersion = D3DPS_VERSION(0, 0);
292 if (caps->VertexShaderVersion)
293 caps->VertexShaderVersion = D3DVS_VERSION(1, 1);
294 else
295 caps->VertexShaderVersion = D3DVS_VERSION(0, 0);
296 caps->MaxVertexShaderConst = min(D3D8_MAX_VERTEX_SHADER_CONSTANTF, caps->MaxVertexShaderConst);
298 caps->StencilCaps &= ~WINED3DSTENCILCAPS_TWOSIDED;
301 /* Handle table functions */
302 static DWORD d3d8_allocate_handle(struct d3d8_handle_table *t, void *object, enum d3d8_handle_type type)
304 struct d3d8_handle_entry *entry;
306 if (t->free_entries)
308 DWORD index = t->free_entries - t->entries;
309 /* Use a free handle */
310 entry = t->free_entries;
311 if (entry->type != D3D8_HANDLE_FREE)
313 ERR("Handle %u(%p) is in the free list, but has type %#x.\n", index, entry, entry->type);
314 return D3D8_INVALID_HANDLE;
316 t->free_entries = entry->object;
317 entry->object = object;
318 entry->type = type;
320 return index;
323 if (!(t->entry_count < t->table_size))
325 /* Grow the table */
326 UINT new_size = t->table_size + (t->table_size >> 1);
327 struct d3d8_handle_entry *new_entries;
329 if (!(new_entries = heap_realloc(t->entries, new_size * sizeof(*t->entries))))
331 ERR("Failed to grow the handle table.\n");
332 return D3D8_INVALID_HANDLE;
334 t->entries = new_entries;
335 t->table_size = new_size;
338 entry = &t->entries[t->entry_count];
339 entry->object = object;
340 entry->type = type;
342 return t->entry_count++;
345 static void *d3d8_free_handle(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
347 struct d3d8_handle_entry *entry;
348 void *object;
350 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
352 WARN("Invalid handle %u passed.\n", handle);
353 return NULL;
356 entry = &t->entries[handle];
357 if (entry->type != type)
359 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
360 return NULL;
363 object = entry->object;
364 entry->object = t->free_entries;
365 entry->type = D3D8_HANDLE_FREE;
366 t->free_entries = entry;
368 return object;
371 static void *d3d8_get_object(struct d3d8_handle_table *t, DWORD handle, enum d3d8_handle_type type)
373 struct d3d8_handle_entry *entry;
375 if (handle == D3D8_INVALID_HANDLE || handle >= t->entry_count)
377 WARN("Invalid handle %u passed.\n", handle);
378 return NULL;
381 entry = &t->entries[handle];
382 if (entry->type != type)
384 WARN("Handle %u(%p) is not of type %#x.\n", handle, entry, type);
385 return NULL;
388 return entry->object;
391 static HRESULT WINAPI d3d8_device_QueryInterface(IDirect3DDevice8 *iface, REFIID riid, void **out)
393 TRACE("iface %p, riid %s, out %p.\n",
394 iface, debugstr_guid(riid), out);
396 if (IsEqualGUID(riid, &IID_IDirect3DDevice8)
397 || IsEqualGUID(riid, &IID_IUnknown))
399 IDirect3DDevice8_AddRef(iface);
400 *out = iface;
401 return S_OK;
404 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
406 *out = NULL;
407 return E_NOINTERFACE;
410 static ULONG WINAPI d3d8_device_AddRef(IDirect3DDevice8 *iface)
412 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
413 ULONG ref = InterlockedIncrement(&device->ref);
415 TRACE("%p increasing refcount to %u.\n", iface, ref);
417 return ref;
420 static ULONG WINAPI d3d8_device_Release(IDirect3DDevice8 *iface)
422 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
423 ULONG ref;
425 if (device->inDestruction)
426 return 0;
428 ref = InterlockedDecrement(&device->ref);
430 TRACE("%p decreasing refcount to %u.\n", iface, ref);
432 if (!ref)
434 IDirect3D8 *parent = device->d3d_parent;
435 unsigned i;
437 TRACE("Releasing wined3d device %p.\n", device->wined3d_device);
439 wined3d_mutex_lock();
441 device->inDestruction = TRUE;
443 for (i = 0; i < device->numConvertedDecls; ++i)
445 d3d8_vertex_declaration_destroy(device->decls[i].declaration);
447 heap_free(device->decls);
449 if (device->vertex_buffer)
450 wined3d_buffer_decref(device->vertex_buffer);
451 if (device->index_buffer)
452 wined3d_buffer_decref(device->index_buffer);
454 wined3d_device_uninit_3d(device->wined3d_device);
455 wined3d_device_release_focus_window(device->wined3d_device);
456 wined3d_device_decref(device->wined3d_device);
457 heap_free(device->handle_table.entries);
458 heap_free(device);
460 wined3d_mutex_unlock();
462 IDirect3D8_Release(parent);
464 return ref;
467 static HRESULT WINAPI d3d8_device_TestCooperativeLevel(IDirect3DDevice8 *iface)
469 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
471 TRACE("iface %p.\n", iface);
473 TRACE("device state: %#x.\n", device->device_state);
475 switch (device->device_state)
477 default:
478 case D3D8_DEVICE_STATE_OK:
479 return D3D_OK;
480 case D3D8_DEVICE_STATE_LOST:
481 return D3DERR_DEVICELOST;
482 case D3D8_DEVICE_STATE_NOT_RESET:
483 return D3DERR_DEVICENOTRESET;
487 static UINT WINAPI d3d8_device_GetAvailableTextureMem(IDirect3DDevice8 *iface)
489 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
490 UINT ret;
492 TRACE("iface %p.\n", iface);
494 wined3d_mutex_lock();
495 ret = wined3d_device_get_available_texture_mem(device->wined3d_device);
496 wined3d_mutex_unlock();
498 return ret;
501 static HRESULT WINAPI d3d8_device_ResourceManagerDiscardBytes(IDirect3DDevice8 *iface, DWORD byte_count)
503 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
505 TRACE("iface %p, byte_count %u.\n", iface, byte_count);
507 if (byte_count)
508 FIXME("Byte count ignored.\n");
510 wined3d_mutex_lock();
511 wined3d_device_evict_managed_resources(device->wined3d_device);
512 wined3d_mutex_unlock();
514 return D3D_OK;
517 static HRESULT WINAPI d3d8_device_GetDirect3D(IDirect3DDevice8 *iface, IDirect3D8 **d3d8)
519 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
521 TRACE("iface %p, d3d8 %p.\n", iface, d3d8);
523 if (!d3d8)
524 return D3DERR_INVALIDCALL;
526 return IDirect3D8_QueryInterface(device->d3d_parent, &IID_IDirect3D8, (void **)d3d8);
529 static HRESULT WINAPI d3d8_device_GetDeviceCaps(IDirect3DDevice8 *iface, D3DCAPS8 *caps)
531 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
532 WINED3DCAPS wined3d_caps;
533 HRESULT hr;
535 TRACE("iface %p, caps %p.\n", iface, caps);
537 if (!caps)
538 return D3DERR_INVALIDCALL;
540 wined3d_mutex_lock();
541 hr = wined3d_device_get_device_caps(device->wined3d_device, &wined3d_caps);
542 wined3d_mutex_unlock();
544 d3dcaps_from_wined3dcaps(caps, &wined3d_caps);
546 return hr;
549 static HRESULT WINAPI d3d8_device_GetDisplayMode(IDirect3DDevice8 *iface, D3DDISPLAYMODE *mode)
551 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
552 struct wined3d_display_mode wined3d_mode;
553 HRESULT hr;
555 TRACE("iface %p, mode %p.\n", iface, mode);
557 wined3d_mutex_lock();
558 hr = wined3d_device_get_display_mode(device->wined3d_device, 0, &wined3d_mode, NULL);
559 wined3d_mutex_unlock();
561 if (SUCCEEDED(hr))
563 mode->Width = wined3d_mode.width;
564 mode->Height = wined3d_mode.height;
565 mode->RefreshRate = wined3d_mode.refresh_rate;
566 mode->Format = d3dformat_from_wined3dformat(wined3d_mode.format_id);
569 return hr;
572 static HRESULT WINAPI d3d8_device_GetCreationParameters(IDirect3DDevice8 *iface,
573 D3DDEVICE_CREATION_PARAMETERS *parameters)
575 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
577 TRACE("iface %p, parameters %p.\n", iface, parameters);
579 wined3d_mutex_lock();
580 wined3d_device_get_creation_parameters(device->wined3d_device,
581 (struct wined3d_device_creation_parameters *)parameters);
582 wined3d_mutex_unlock();
584 return D3D_OK;
587 static HRESULT WINAPI d3d8_device_SetCursorProperties(IDirect3DDevice8 *iface,
588 UINT hotspot_x, UINT hotspot_y, IDirect3DSurface8 *bitmap)
590 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
591 struct d3d8_surface *bitmap_impl = unsafe_impl_from_IDirect3DSurface8(bitmap);
592 HRESULT hr;
594 TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
595 iface, hotspot_x, hotspot_y, bitmap);
597 if (!bitmap)
599 WARN("No cursor bitmap, returning D3DERR_INVALIDCALL.\n");
600 return D3DERR_INVALIDCALL;
603 wined3d_mutex_lock();
604 hr = wined3d_device_set_cursor_properties(device->wined3d_device,
605 hotspot_x, hotspot_y, bitmap_impl->wined3d_texture, bitmap_impl->sub_resource_idx);
606 wined3d_mutex_unlock();
608 return hr;
611 static void WINAPI d3d8_device_SetCursorPosition(IDirect3DDevice8 *iface, UINT x, UINT y, DWORD flags)
613 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
615 TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, x, y, flags);
617 wined3d_mutex_lock();
618 wined3d_device_set_cursor_position(device->wined3d_device, x, y, flags);
619 wined3d_mutex_unlock();
622 static BOOL WINAPI d3d8_device_ShowCursor(IDirect3DDevice8 *iface, BOOL show)
624 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
625 BOOL ret;
627 TRACE("iface %p, show %#x.\n", iface, show);
629 wined3d_mutex_lock();
630 ret = wined3d_device_show_cursor(device->wined3d_device, show);
631 wined3d_mutex_unlock();
633 return ret;
636 static HRESULT WINAPI d3d8_device_CreateAdditionalSwapChain(IDirect3DDevice8 *iface,
637 D3DPRESENT_PARAMETERS *present_parameters, IDirect3DSwapChain8 **swapchain)
639 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
640 struct wined3d_swapchain_desc desc;
641 struct d3d8_swapchain *object;
642 UINT i, count;
643 HRESULT hr;
645 TRACE("iface %p, present_parameters %p, swapchain %p.\n",
646 iface, present_parameters, swapchain);
648 if (!present_parameters->Windowed)
650 WARN("Trying to create an additional fullscreen swapchain, returning D3DERR_INVALIDCALL.\n");
651 return D3DERR_INVALIDCALL;
654 wined3d_mutex_lock();
655 count = wined3d_device_get_swapchain_count(device->wined3d_device);
656 for (i = 0; i < count; ++i)
658 struct wined3d_swapchain *wined3d_swapchain;
660 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, i);
661 wined3d_swapchain_get_desc(wined3d_swapchain, &desc);
663 if (!desc.windowed)
665 wined3d_mutex_unlock();
666 WARN("Trying to create an additional swapchain in fullscreen mode, returning D3DERR_INVALIDCALL.\n");
667 return D3DERR_INVALIDCALL;
670 wined3d_mutex_unlock();
672 if (!wined3d_swapchain_desc_from_present_parameters(&desc, present_parameters))
673 return D3DERR_INVALIDCALL;
674 if (SUCCEEDED(hr = d3d8_swapchain_create(device, &desc, &object)))
675 *swapchain = &object->IDirect3DSwapChain8_iface;
676 present_parameters_from_wined3d_swapchain_desc(present_parameters, &desc);
678 return hr;
681 static HRESULT CDECL reset_enum_callback(struct wined3d_resource *resource)
683 struct wined3d_resource_desc desc;
684 IDirect3DBaseTexture8 *texture;
685 struct d3d8_surface *surface;
686 IUnknown *parent;
688 wined3d_resource_get_desc(resource, &desc);
689 if (desc.access & WINED3D_RESOURCE_ACCESS_CPU)
690 return D3D_OK;
692 if (desc.resource_type != WINED3D_RTYPE_TEXTURE_2D)
694 WARN("Resource %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", resource);
695 return D3DERR_DEVICELOST;
698 parent = wined3d_resource_get_parent(resource);
699 if (parent && SUCCEEDED(IUnknown_QueryInterface(parent, &IID_IDirect3DBaseTexture8, (void **)&texture)))
701 IDirect3DBaseTexture8_Release(texture);
702 WARN("Texture %p (resource %p) in pool D3DPOOL_DEFAULT blocks the Reset call.\n", texture, resource);
703 return D3DERR_DEVICELOST;
706 surface = wined3d_texture_get_sub_resource_parent(wined3d_texture_from_resource(resource), 0);
707 if (!surface->resource.refcount)
708 return D3D_OK;
710 WARN("Surface %p in pool D3DPOOL_DEFAULT blocks the Reset call.\n", surface);
711 return D3DERR_DEVICELOST;
714 static HRESULT WINAPI d3d8_device_Reset(IDirect3DDevice8 *iface,
715 D3DPRESENT_PARAMETERS *present_parameters)
717 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
718 struct wined3d_swapchain_desc swapchain_desc;
719 HRESULT hr;
721 TRACE("iface %p, present_parameters %p.\n", iface, present_parameters);
723 if (device->device_state == D3D8_DEVICE_STATE_LOST)
725 WARN("App not active, returning D3DERR_DEVICELOST.\n");
726 return D3DERR_DEVICELOST;
728 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, present_parameters))
729 return D3DERR_INVALIDCALL;
731 wined3d_mutex_lock();
733 if (device->vertex_buffer)
735 wined3d_buffer_decref(device->vertex_buffer);
736 device->vertex_buffer = NULL;
737 device->vertex_buffer_size = 0;
739 if (device->index_buffer)
741 wined3d_buffer_decref(device->index_buffer);
742 device->index_buffer = NULL;
743 device->index_buffer_size = 0;
746 if (SUCCEEDED(hr = wined3d_device_reset(device->wined3d_device, &swapchain_desc,
747 NULL, reset_enum_callback, TRUE)))
749 present_parameters->BackBufferCount = swapchain_desc.backbuffer_count;
750 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
751 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_ZENABLE,
752 !!swapchain_desc.enable_auto_depth_stencil);
753 device->device_state = D3D8_DEVICE_STATE_OK;
755 else
757 device->device_state = D3D8_DEVICE_STATE_NOT_RESET;
759 wined3d_mutex_unlock();
761 return hr;
764 static HRESULT WINAPI d3d8_device_Present(IDirect3DDevice8 *iface, const RECT *src_rect,
765 const RECT *dst_rect, HWND dst_window_override, const RGNDATA *dirty_region)
767 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
769 TRACE("iface %p, src_rect %s, dst_rect %s, dst_window_override %p, dirty_region %p.\n",
770 iface, wine_dbgstr_rect(src_rect), wine_dbgstr_rect(dst_rect), dst_window_override, dirty_region);
772 /* Fraps does not hook IDirect3DDevice8::Present regardless of the hotpatch
773 * attribute. It only hooks IDirect3DSwapChain8::Present. Yet it properly
774 * shows a framerate on Windows in applications that only call the device
775 * method, like e.g. the dx8 sdk samples. The conclusion is that native
776 * calls the swapchain's public method from the device. */
777 return IDirect3DSwapChain8_Present(&device->implicit_swapchain->IDirect3DSwapChain8_iface,
778 src_rect, dst_rect, dst_window_override, dirty_region);
781 static HRESULT WINAPI d3d8_device_GetBackBuffer(IDirect3DDevice8 *iface,
782 UINT backbuffer_idx, D3DBACKBUFFER_TYPE backbuffer_type, IDirect3DSurface8 **backbuffer)
784 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
785 struct wined3d_swapchain *wined3d_swapchain;
786 struct wined3d_texture *wined3d_texture;
787 struct d3d8_surface *surface_impl;
789 TRACE("iface %p, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
790 iface, backbuffer_idx, backbuffer_type, backbuffer);
792 /* backbuffer_type is ignored by native. */
794 /* No need to check for backbuffer == NULL, Windows crashes in that case. */
795 wined3d_mutex_lock();
797 wined3d_swapchain = device->implicit_swapchain->wined3d_swapchain;
798 if (!(wined3d_texture = wined3d_swapchain_get_back_buffer(wined3d_swapchain, backbuffer_idx)))
800 wined3d_mutex_unlock();
801 *backbuffer = NULL;
802 return D3DERR_INVALIDCALL;
805 surface_impl = wined3d_texture_get_sub_resource_parent(wined3d_texture, 0);
806 *backbuffer = &surface_impl->IDirect3DSurface8_iface;
807 IDirect3DSurface8_AddRef(*backbuffer);
809 wined3d_mutex_unlock();
810 return D3D_OK;
813 static HRESULT WINAPI d3d8_device_GetRasterStatus(IDirect3DDevice8 *iface, D3DRASTER_STATUS *raster_status)
815 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
816 HRESULT hr;
818 TRACE("iface %p, raster_status %p.\n", iface, raster_status);
820 wined3d_mutex_lock();
821 hr = wined3d_device_get_raster_status(device->wined3d_device, 0, (struct wined3d_raster_status *)raster_status);
822 wined3d_mutex_unlock();
824 return hr;
827 static void WINAPI d3d8_device_SetGammaRamp(IDirect3DDevice8 *iface, DWORD flags, const D3DGAMMARAMP *ramp)
829 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
831 TRACE("iface %p, flags %#x, ramp %p.\n", iface, flags, ramp);
833 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
834 wined3d_mutex_lock();
835 wined3d_device_set_gamma_ramp(device->wined3d_device, 0, flags, (const struct wined3d_gamma_ramp *)ramp);
836 wined3d_mutex_unlock();
839 static void WINAPI d3d8_device_GetGammaRamp(IDirect3DDevice8 *iface, D3DGAMMARAMP *ramp)
841 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
843 TRACE("iface %p, ramp %p.\n", iface, ramp);
845 /* Note: D3DGAMMARAMP is compatible with struct wined3d_gamma_ramp. */
846 wined3d_mutex_lock();
847 wined3d_device_get_gamma_ramp(device->wined3d_device, 0, (struct wined3d_gamma_ramp *)ramp);
848 wined3d_mutex_unlock();
851 static HRESULT WINAPI d3d8_device_CreateTexture(IDirect3DDevice8 *iface,
852 UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
853 D3DPOOL pool, IDirect3DTexture8 **texture)
855 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
856 struct d3d8_texture *object;
857 HRESULT hr;
859 TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
860 iface, width, height, levels, usage, format, pool, texture);
862 if (!format)
863 return D3DERR_INVALIDCALL;
865 *texture = NULL;
866 if (!(object = heap_alloc_zero(sizeof(*object))))
867 return D3DERR_OUTOFVIDEOMEMORY;
869 hr = texture_init(object, device, width, height, levels, usage, format, pool);
870 if (FAILED(hr))
872 WARN("Failed to initialize texture, hr %#x.\n", hr);
873 heap_free(object);
874 return hr;
877 TRACE("Created texture %p.\n", object);
878 *texture = (IDirect3DTexture8 *)&object->IDirect3DBaseTexture8_iface;
880 return D3D_OK;
883 static HRESULT WINAPI d3d8_device_CreateVolumeTexture(IDirect3DDevice8 *iface,
884 UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
885 D3DPOOL pool, IDirect3DVolumeTexture8 **texture)
887 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
888 struct d3d8_texture *object;
889 HRESULT hr;
891 TRACE("iface %p, width %u, height %u, depth %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
892 iface, width, height, depth, levels, usage, format, pool, texture);
894 if (!format)
895 return D3DERR_INVALIDCALL;
897 *texture = NULL;
898 if (!(object = heap_alloc_zero(sizeof(*object))))
899 return D3DERR_OUTOFVIDEOMEMORY;
901 hr = volumetexture_init(object, device, width, height, depth, levels, usage, format, pool);
902 if (FAILED(hr))
904 WARN("Failed to initialize volume texture, hr %#x.\n", hr);
905 heap_free(object);
906 return hr;
909 TRACE("Created volume texture %p.\n", object);
910 *texture = (IDirect3DVolumeTexture8 *)&object->IDirect3DBaseTexture8_iface;
912 return D3D_OK;
915 static HRESULT WINAPI d3d8_device_CreateCubeTexture(IDirect3DDevice8 *iface, UINT edge_length,
916 UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DCubeTexture8 **texture)
918 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
919 struct d3d8_texture *object;
920 HRESULT hr;
922 TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p.\n",
923 iface, edge_length, levels, usage, format, pool, texture);
925 if (!format)
926 return D3DERR_INVALIDCALL;
928 *texture = NULL;
929 if (!(object = heap_alloc_zero(sizeof(*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 heap_free(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 if (!(object = heap_alloc_zero(sizeof(*object))))
957 return D3DERR_OUTOFVIDEOMEMORY;
959 hr = vertexbuffer_init(object, device, size, usage, fvf, pool);
960 if (FAILED(hr))
962 WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
963 heap_free(object);
964 return hr;
967 TRACE("Created vertex buffer %p.\n", object);
968 *buffer = &object->IDirect3DVertexBuffer8_iface;
970 return D3D_OK;
973 static HRESULT WINAPI d3d8_device_CreateIndexBuffer(IDirect3DDevice8 *iface, UINT size,
974 DWORD usage, D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer8 **buffer)
976 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
977 struct d3d8_indexbuffer *object;
978 HRESULT hr;
980 TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p.\n",
981 iface, size, usage, format, pool, buffer);
983 if (!(object = heap_alloc_zero(sizeof(*object))))
984 return D3DERR_OUTOFVIDEOMEMORY;
986 hr = indexbuffer_init(object, device, size, usage, format, pool);
987 if (FAILED(hr))
989 WARN("Failed to initialize index buffer, hr %#x.\n", hr);
990 heap_free(object);
991 return hr;
994 TRACE("Created index buffer %p.\n", object);
995 *buffer = &object->IDirect3DIndexBuffer8_iface;
997 return D3D_OK;
1000 static HRESULT d3d8_device_create_surface(struct d3d8_device *device, UINT width, UINT height,
1001 D3DFORMAT format, DWORD flags, IDirect3DSurface8 **surface, UINT usage, D3DPOOL pool,
1002 D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality)
1004 struct wined3d_resource_desc desc;
1005 struct d3d8_surface *surface_impl;
1006 struct wined3d_texture *texture;
1007 HRESULT hr;
1009 TRACE("device %p, width %u, height %u, format %#x, flags %#x, surface %p, "
1010 "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
1011 device, width, height, format, flags, surface,
1012 usage, pool, multisample_type, multisample_quality);
1014 desc.resource_type = WINED3D_RTYPE_TEXTURE_2D;
1015 desc.format = wined3dformat_from_d3dformat(format);
1016 desc.multisample_type = multisample_type;
1017 desc.multisample_quality = multisample_quality;
1018 desc.usage = usage & WINED3DUSAGE_MASK;
1019 if (pool == D3DPOOL_SCRATCH)
1020 desc.usage |= WINED3DUSAGE_SCRATCH;
1021 desc.access = wined3daccess_from_d3dpool(pool) | WINED3D_RESOURCE_ACCESS_MAP;
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_desc desc;
2107 struct wined3d_buffer *buffer;
2109 TRACE("Growing vertex buffer to %u bytes\n", size);
2111 desc.byte_width = size;
2112 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY;
2113 desc.bind_flags = WINED3D_BIND_VERTEX_BUFFER;
2114 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
2115 desc.misc_flags = 0;
2116 desc.structure_byte_stride = 0;
2118 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2119 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2121 ERR("Failed to create vertex buffer, hr %#x.\n", hr);
2122 return hr;
2125 if (device->vertex_buffer)
2126 wined3d_buffer_decref(device->vertex_buffer);
2128 device->vertex_buffer = buffer;
2129 device->vertex_buffer_size = size;
2130 device->vertex_buffer_pos = 0;
2132 return D3D_OK;
2135 static HRESULT WINAPI d3d8_device_DrawPrimitiveUP(IDirect3DDevice8 *iface,
2136 D3DPRIMITIVETYPE primitive_type, UINT primitive_count, const void *data,
2137 UINT stride)
2139 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2140 HRESULT hr;
2141 UINT vtx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2142 struct wined3d_map_desc wined3d_map_desc;
2143 struct wined3d_box wined3d_box = {0};
2144 UINT size = vtx_count * stride;
2145 struct wined3d_resource *vb;
2146 UINT vb_pos, align;
2148 TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
2149 iface, primitive_type, primitive_count, data, stride);
2151 if (!primitive_count)
2153 WARN("primitive_count is 0, returning D3D_OK\n");
2154 return D3D_OK;
2157 wined3d_mutex_lock();
2158 hr = d3d8_device_prepare_vertex_buffer(device, size);
2159 if (FAILED(hr))
2160 goto done;
2162 vb_pos = device->vertex_buffer_pos;
2163 align = vb_pos % stride;
2164 if (align) align = stride - align;
2165 if (vb_pos + size + align > device->vertex_buffer_size)
2166 vb_pos = 0;
2167 else
2168 vb_pos += align;
2170 wined3d_box.left = vb_pos;
2171 wined3d_box.right = vb_pos + size;
2172 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2173 if (FAILED(wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2174 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2175 goto done;
2176 memcpy(wined3d_map_desc.data, data, size);
2177 wined3d_resource_unmap(vb, 0);
2178 device->vertex_buffer_pos = vb_pos + size;
2180 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, stride);
2181 if (FAILED(hr))
2182 goto done;
2184 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2185 hr = wined3d_device_draw_primitive(device->wined3d_device, vb_pos / stride, vtx_count);
2186 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2188 done:
2189 wined3d_mutex_unlock();
2190 return hr;
2193 /* The caller is responsible for wined3d locking */
2194 static HRESULT d3d8_device_prepare_index_buffer(struct d3d8_device *device, UINT min_size)
2196 HRESULT hr;
2198 if (device->index_buffer_size < min_size || !device->index_buffer)
2200 UINT size = max(device->index_buffer_size * 2, min_size);
2201 struct wined3d_buffer_desc desc;
2202 struct wined3d_buffer *buffer;
2204 TRACE("Growing index buffer to %u bytes\n", size);
2206 desc.byte_width = size;
2207 desc.usage = WINED3DUSAGE_DYNAMIC | WINED3DUSAGE_WRITEONLY | WINED3DUSAGE_STATICDECL;
2208 desc.bind_flags = WINED3D_BIND_INDEX_BUFFER;
2209 desc.access = WINED3D_RESOURCE_ACCESS_GPU;
2210 desc.misc_flags = 0;
2211 desc.structure_byte_stride = 0;
2213 if (FAILED(hr = wined3d_buffer_create(device->wined3d_device, &desc,
2214 NULL, NULL, &d3d8_null_wined3d_parent_ops, &buffer)))
2216 ERR("Failed to create index buffer, hr %#x.\n", hr);
2217 return hr;
2220 if (device->index_buffer)
2221 wined3d_buffer_decref(device->index_buffer);
2223 device->index_buffer = buffer;
2224 device->index_buffer_size = size;
2225 device->index_buffer_pos = 0;
2227 return D3D_OK;
2230 static HRESULT WINAPI d3d8_device_DrawIndexedPrimitiveUP(IDirect3DDevice8 *iface,
2231 D3DPRIMITIVETYPE primitive_type, UINT min_vertex_idx, UINT vertex_count,
2232 UINT primitive_count, const void *index_data, D3DFORMAT index_format,
2233 const void *vertex_data, UINT vertex_stride)
2235 UINT idx_count = vertex_count_from_primitive_count(primitive_type, primitive_count);
2236 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2237 UINT idx_fmt_size = index_format == D3DFMT_INDEX16 ? 2 : 4;
2238 UINT vtx_size = vertex_count * vertex_stride;
2239 UINT idx_size = idx_count * idx_fmt_size;
2240 struct wined3d_map_desc wined3d_map_desc;
2241 struct wined3d_box wined3d_box = {0};
2242 struct wined3d_resource *ib, *vb;
2243 UINT vb_pos, ib_pos, align;
2244 HRESULT hr;
2246 TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, vertex_count %u, primitive_count %u, "
2247 "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
2248 iface, primitive_type, min_vertex_idx, vertex_count, primitive_count,
2249 index_data, index_format, vertex_data, vertex_stride);
2251 if (!primitive_count)
2253 WARN("primitive_count is 0, returning D3D_OK\n");
2254 return D3D_OK;
2257 wined3d_mutex_lock();
2259 hr = d3d8_device_prepare_vertex_buffer(device, vtx_size);
2260 if (FAILED(hr))
2261 goto done;
2263 vb_pos = device->vertex_buffer_pos;
2264 align = vb_pos % vertex_stride;
2265 if (align) align = vertex_stride - align;
2266 if (vb_pos + vtx_size + align > device->vertex_buffer_size)
2267 vb_pos = 0;
2268 else
2269 vb_pos += align;
2271 wined3d_box.left = vb_pos;
2272 wined3d_box.right = vb_pos + vtx_size;
2273 vb = wined3d_buffer_get_resource(device->vertex_buffer);
2274 if (FAILED(hr = wined3d_resource_map(vb, 0, &wined3d_map_desc, &wined3d_box,
2275 vb_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2276 goto done;
2277 memcpy(wined3d_map_desc.data, (char *)vertex_data + min_vertex_idx * vertex_stride, vtx_size);
2278 wined3d_resource_unmap(vb, 0);
2279 device->vertex_buffer_pos = vb_pos + vtx_size;
2281 hr = d3d8_device_prepare_index_buffer(device, idx_size);
2282 if (FAILED(hr))
2283 goto done;
2285 ib_pos = device->index_buffer_pos;
2286 align = ib_pos % idx_fmt_size;
2287 if (align) align = idx_fmt_size - align;
2288 if (ib_pos + idx_size + align > device->index_buffer_size)
2289 ib_pos = 0;
2290 else
2291 ib_pos += align;
2293 wined3d_box.left = ib_pos;
2294 wined3d_box.right = ib_pos + idx_size;
2295 ib = wined3d_buffer_get_resource(device->index_buffer);
2296 if (FAILED(hr = wined3d_resource_map(ib, 0, &wined3d_map_desc, &wined3d_box,
2297 ib_pos ? WINED3D_MAP_NOOVERWRITE : WINED3D_MAP_DISCARD)))
2298 goto done;
2299 memcpy(wined3d_map_desc.data, index_data, idx_size);
2300 wined3d_resource_unmap(ib, 0);
2301 device->index_buffer_pos = ib_pos + idx_size;
2303 hr = wined3d_device_set_stream_source(device->wined3d_device, 0, device->vertex_buffer, 0, vertex_stride);
2304 if (FAILED(hr))
2305 goto done;
2307 wined3d_device_set_index_buffer(device->wined3d_device, device->index_buffer,
2308 wined3dformat_from_d3dformat(index_format), 0);
2309 wined3d_device_set_base_vertex_index(device->wined3d_device, vb_pos / vertex_stride - min_vertex_idx);
2311 wined3d_device_set_primitive_type(device->wined3d_device, primitive_type, 0);
2312 hr = wined3d_device_draw_indexed_primitive(device->wined3d_device, ib_pos / idx_fmt_size, idx_count);
2314 wined3d_device_set_stream_source(device->wined3d_device, 0, NULL, 0, 0);
2315 wined3d_device_set_index_buffer(device->wined3d_device, NULL, WINED3DFMT_UNKNOWN, 0);
2316 wined3d_device_set_base_vertex_index(device->wined3d_device, 0);
2318 done:
2319 wined3d_mutex_unlock();
2320 return hr;
2323 static HRESULT WINAPI d3d8_device_ProcessVertices(IDirect3DDevice8 *iface, UINT src_start_idx,
2324 UINT dst_idx, UINT vertex_count, IDirect3DVertexBuffer8 *dst_buffer, DWORD flags)
2326 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2327 struct d3d8_vertexbuffer *dst = unsafe_impl_from_IDirect3DVertexBuffer8(dst_buffer);
2328 HRESULT hr;
2330 TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, flags %#x.\n",
2331 iface, src_start_idx, dst_idx, vertex_count, dst_buffer, flags);
2333 wined3d_mutex_lock();
2334 hr = wined3d_device_process_vertices(device->wined3d_device, src_start_idx, dst_idx,
2335 vertex_count, dst->wined3d_buffer, NULL, flags, dst->fvf);
2336 wined3d_mutex_unlock();
2338 return hr;
2341 static HRESULT WINAPI d3d8_device_CreateVertexShader(IDirect3DDevice8 *iface,
2342 const DWORD *declaration, const DWORD *byte_code, DWORD *shader, DWORD usage)
2344 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2345 struct d3d8_vertex_shader *object;
2346 DWORD shader_handle;
2347 DWORD handle;
2348 HRESULT hr;
2350 TRACE("iface %p, declaration %p, byte_code %p, shader %p, usage %#x.\n",
2351 iface, declaration, byte_code, shader, usage);
2353 if (!(object = heap_alloc_zero(sizeof(*object))))
2355 *shader = 0;
2356 return E_OUTOFMEMORY;
2359 wined3d_mutex_lock();
2360 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_VS);
2361 wined3d_mutex_unlock();
2362 if (handle == D3D8_INVALID_HANDLE)
2364 ERR("Failed to allocate vertex shader handle.\n");
2365 heap_free(object);
2366 *shader = 0;
2367 return E_OUTOFMEMORY;
2370 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2372 hr = d3d8_vertex_shader_init(object, device, declaration, byte_code, shader_handle, usage);
2373 if (FAILED(hr))
2375 WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
2376 wined3d_mutex_lock();
2377 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_VS);
2378 wined3d_mutex_unlock();
2379 heap_free(object);
2380 *shader = 0;
2381 return hr;
2384 TRACE("Created vertex shader %p (handle %#x).\n", object, shader_handle);
2385 *shader = shader_handle;
2387 return D3D_OK;
2390 static struct d3d8_vertex_declaration *d3d8_device_get_fvf_declaration(struct d3d8_device *device, DWORD fvf)
2392 struct d3d8_vertex_declaration *d3d8_declaration;
2393 struct FvfToDecl *convertedDecls = device->decls;
2394 int p, low, high; /* deliberately signed */
2395 HRESULT hr;
2397 TRACE("Searching for declaration for fvf %08x... ", fvf);
2399 low = 0;
2400 high = device->numConvertedDecls - 1;
2401 while (low <= high)
2403 p = (low + high) >> 1;
2404 TRACE("%d ", p);
2406 if (convertedDecls[p].fvf == fvf)
2408 TRACE("found %p\n", convertedDecls[p].declaration);
2409 return convertedDecls[p].declaration;
2412 if (convertedDecls[p].fvf < fvf)
2413 low = p + 1;
2414 else
2415 high = p - 1;
2417 TRACE("not found. Creating and inserting at position %d.\n", low);
2419 if (!(d3d8_declaration = heap_alloc(sizeof(*d3d8_declaration))))
2420 return NULL;
2422 if (FAILED(hr = d3d8_vertex_declaration_init_fvf(d3d8_declaration, device, fvf)))
2424 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
2425 heap_free(d3d8_declaration);
2426 return NULL;
2429 if (device->declArraySize == device->numConvertedDecls)
2431 UINT grow = device->declArraySize / 2;
2433 if (!(convertedDecls = heap_realloc(convertedDecls,
2434 sizeof(*convertedDecls) * (device->numConvertedDecls + grow))))
2436 d3d8_vertex_declaration_destroy(d3d8_declaration);
2437 return NULL;
2439 device->decls = convertedDecls;
2440 device->declArraySize += grow;
2443 memmove(convertedDecls + low + 1, convertedDecls + low,
2444 sizeof(*convertedDecls) * (device->numConvertedDecls - low));
2445 convertedDecls[low].declaration = d3d8_declaration;
2446 convertedDecls[low].fvf = fvf;
2447 ++device->numConvertedDecls;
2449 TRACE("Returning %p. %u decls in array.\n", d3d8_declaration, device->numConvertedDecls);
2451 return d3d8_declaration;
2454 static HRESULT WINAPI d3d8_device_SetVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2456 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2457 struct d3d8_vertex_shader *shader_impl;
2459 TRACE("iface %p, shader %#x.\n", iface, shader);
2461 if (VS_HIGHESTFIXEDFXF >= shader)
2463 TRACE("Setting FVF, %#x\n", shader);
2465 wined3d_mutex_lock();
2466 wined3d_device_set_vertex_declaration(device->wined3d_device,
2467 d3d8_device_get_fvf_declaration(device, shader)->wined3d_vertex_declaration);
2468 wined3d_device_set_vertex_shader(device->wined3d_device, NULL);
2469 wined3d_mutex_unlock();
2471 return D3D_OK;
2474 TRACE("Setting shader\n");
2476 wined3d_mutex_lock();
2477 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2479 WARN("Invalid handle (%#x) passed.\n", shader);
2480 wined3d_mutex_unlock();
2482 return D3DERR_INVALIDCALL;
2485 wined3d_device_set_vertex_declaration(device->wined3d_device,
2486 shader_impl->vertex_declaration->wined3d_vertex_declaration);
2487 wined3d_device_set_vertex_shader(device->wined3d_device, shader_impl->wined3d_shader);
2488 wined3d_mutex_unlock();
2490 return D3D_OK;
2493 static HRESULT WINAPI d3d8_device_GetVertexShader(IDirect3DDevice8 *iface, DWORD *shader)
2495 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2496 struct wined3d_vertex_declaration *wined3d_declaration;
2497 struct d3d8_vertex_declaration *d3d8_declaration;
2499 TRACE("iface %p, shader %p.\n", iface, shader);
2501 wined3d_mutex_lock();
2502 if ((wined3d_declaration = wined3d_device_get_vertex_declaration(device->wined3d_device)))
2504 d3d8_declaration = wined3d_vertex_declaration_get_parent(wined3d_declaration);
2505 *shader = d3d8_declaration->shader_handle;
2507 else
2509 *shader = 0;
2511 wined3d_mutex_unlock();
2513 TRACE("Returning %#x.\n", *shader);
2515 return D3D_OK;
2518 static HRESULT WINAPI d3d8_device_DeleteVertexShader(IDirect3DDevice8 *iface, DWORD shader)
2520 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2521 struct d3d8_vertex_shader *shader_impl;
2523 TRACE("iface %p, shader %#x.\n", iface, shader);
2525 wined3d_mutex_lock();
2526 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2528 WARN("Invalid handle (%#x) passed.\n", shader);
2529 wined3d_mutex_unlock();
2531 return D3DERR_INVALIDCALL;
2534 if (shader_impl->wined3d_shader
2535 && wined3d_device_get_vertex_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2536 IDirect3DDevice8_SetVertexShader(iface, 0);
2538 wined3d_mutex_unlock();
2540 d3d8_vertex_shader_destroy(shader_impl);
2542 return D3D_OK;
2545 static HRESULT WINAPI d3d8_device_SetVertexShaderConstant(IDirect3DDevice8 *iface,
2546 DWORD start_register, const void *data, DWORD count)
2548 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2549 HRESULT hr;
2551 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2552 iface, start_register, data, count);
2554 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2556 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2557 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2558 return D3DERR_INVALIDCALL;
2561 wined3d_mutex_lock();
2562 hr = wined3d_device_set_vs_consts_f(device->wined3d_device, start_register, count, data);
2563 wined3d_mutex_unlock();
2565 return hr;
2568 static HRESULT WINAPI d3d8_device_GetVertexShaderConstant(IDirect3DDevice8 *iface,
2569 DWORD start_register, void *data, DWORD count)
2571 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2572 HRESULT hr;
2574 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2575 iface, start_register, data, count);
2577 if (start_register + count > D3D8_MAX_VERTEX_SHADER_CONSTANTF)
2579 WARN("Trying to access %u constants, but d3d8 only supports %u\n",
2580 start_register + count, D3D8_MAX_VERTEX_SHADER_CONSTANTF);
2581 return D3DERR_INVALIDCALL;
2584 wined3d_mutex_lock();
2585 hr = wined3d_device_get_vs_consts_f(device->wined3d_device, start_register, count, data);
2586 wined3d_mutex_unlock();
2588 return hr;
2591 static HRESULT WINAPI d3d8_device_GetVertexShaderDeclaration(IDirect3DDevice8 *iface,
2592 DWORD shader, void *data, DWORD *data_size)
2594 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2595 struct d3d8_vertex_declaration *declaration;
2596 struct d3d8_vertex_shader *shader_impl;
2598 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2599 iface, shader, data, data_size);
2601 wined3d_mutex_lock();
2602 shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS);
2603 wined3d_mutex_unlock();
2605 if (!shader_impl)
2607 WARN("Invalid handle (%#x) passed.\n", shader);
2608 return D3DERR_INVALIDCALL;
2610 declaration = shader_impl->vertex_declaration;
2612 if (!data)
2614 *data_size = declaration->elements_size;
2615 return D3D_OK;
2618 /* MSDN claims that if *data_size is smaller than the required size
2619 * we should write the required size and return D3DERR_MOREDATA.
2620 * That's not actually true. */
2621 if (*data_size < declaration->elements_size)
2622 return D3DERR_INVALIDCALL;
2624 memcpy(data, declaration->elements, declaration->elements_size);
2626 return D3D_OK;
2629 static HRESULT WINAPI d3d8_device_GetVertexShaderFunction(IDirect3DDevice8 *iface,
2630 DWORD shader, void *data, DWORD *data_size)
2632 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2633 struct d3d8_vertex_shader *shader_impl = NULL;
2634 HRESULT hr;
2636 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2637 iface, shader, data, data_size);
2639 wined3d_mutex_lock();
2640 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_VS)))
2642 WARN("Invalid handle (%#x) passed.\n", shader);
2643 wined3d_mutex_unlock();
2645 return D3DERR_INVALIDCALL;
2648 if (!shader_impl->wined3d_shader)
2650 wined3d_mutex_unlock();
2651 *data_size = 0;
2652 return D3D_OK;
2655 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2656 wined3d_mutex_unlock();
2658 return hr;
2661 static HRESULT WINAPI d3d8_device_SetIndices(IDirect3DDevice8 *iface,
2662 IDirect3DIndexBuffer8 *buffer, UINT base_vertex_idx)
2664 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2665 struct d3d8_indexbuffer *ib = unsafe_impl_from_IDirect3DIndexBuffer8(buffer);
2667 TRACE("iface %p, buffer %p, base_vertex_idx %u.\n", iface, buffer, base_vertex_idx);
2669 /* WineD3D takes an INT(due to d3d9), but d3d8 uses UINTs. Do I have to add a check here that
2670 * the UINT doesn't cause an overflow in the INT? It seems rather unlikely because such large
2671 * vertex buffers can't be created to address them with an index that requires the 32nd bit
2672 * (4 Byte minimum vertex size * 2^31-1 -> 8 gb buffer. The index sign would be the least
2673 * problem)
2675 wined3d_mutex_lock();
2676 wined3d_device_set_base_vertex_index(device->wined3d_device, base_vertex_idx);
2677 wined3d_device_set_index_buffer(device->wined3d_device,
2678 ib ? ib->wined3d_buffer : NULL, ib ? ib->format : WINED3DFMT_UNKNOWN, 0);
2679 wined3d_mutex_unlock();
2681 return D3D_OK;
2684 static HRESULT WINAPI d3d8_device_GetIndices(IDirect3DDevice8 *iface,
2685 IDirect3DIndexBuffer8 **buffer, UINT *base_vertex_index)
2687 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2688 enum wined3d_format_id wined3d_format;
2689 struct wined3d_buffer *wined3d_buffer;
2690 struct d3d8_indexbuffer *buffer_impl;
2692 TRACE("iface %p, buffer %p, base_vertex_index %p.\n", iface, buffer, base_vertex_index);
2694 if (!buffer)
2695 return D3DERR_INVALIDCALL;
2697 /* The case from UINT to INT is safe because d3d8 will never set negative values */
2698 wined3d_mutex_lock();
2699 *base_vertex_index = wined3d_device_get_base_vertex_index(device->wined3d_device);
2700 if ((wined3d_buffer = wined3d_device_get_index_buffer(device->wined3d_device, &wined3d_format, NULL)))
2702 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2703 *buffer = &buffer_impl->IDirect3DIndexBuffer8_iface;
2704 IDirect3DIndexBuffer8_AddRef(*buffer);
2706 else
2708 *buffer = NULL;
2710 wined3d_mutex_unlock();
2712 return D3D_OK;
2715 static HRESULT WINAPI d3d8_device_CreatePixelShader(IDirect3DDevice8 *iface,
2716 const DWORD *byte_code, DWORD *shader)
2718 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2719 struct d3d8_pixel_shader *object;
2720 DWORD shader_handle;
2721 DWORD handle;
2722 HRESULT hr;
2724 TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2726 if (!shader)
2727 return D3DERR_INVALIDCALL;
2729 if (!(object = heap_alloc_zero(sizeof(*object))))
2730 return E_OUTOFMEMORY;
2732 wined3d_mutex_lock();
2733 handle = d3d8_allocate_handle(&device->handle_table, object, D3D8_HANDLE_PS);
2734 wined3d_mutex_unlock();
2735 if (handle == D3D8_INVALID_HANDLE)
2737 ERR("Failed to allocate pixel shader handle.\n");
2738 heap_free(object);
2739 return E_OUTOFMEMORY;
2742 shader_handle = handle + VS_HIGHESTFIXEDFXF + 1;
2744 hr = d3d8_pixel_shader_init(object, device, byte_code, shader_handle);
2745 if (FAILED(hr))
2747 WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2748 wined3d_mutex_lock();
2749 d3d8_free_handle(&device->handle_table, handle, D3D8_HANDLE_PS);
2750 wined3d_mutex_unlock();
2751 heap_free(object);
2752 *shader = 0;
2753 return hr;
2756 TRACE("Created pixel shader %p (handle %#x).\n", object, shader_handle);
2757 *shader = shader_handle;
2759 return D3D_OK;
2762 static HRESULT WINAPI d3d8_device_SetPixelShader(IDirect3DDevice8 *iface, DWORD shader)
2764 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2765 struct d3d8_pixel_shader *shader_impl;
2767 TRACE("iface %p, shader %#x.\n", iface, shader);
2769 wined3d_mutex_lock();
2771 if (!shader)
2773 wined3d_device_set_pixel_shader(device->wined3d_device, NULL);
2774 wined3d_mutex_unlock();
2775 return D3D_OK;
2778 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2780 WARN("Invalid handle (%#x) passed.\n", shader);
2781 wined3d_mutex_unlock();
2782 return D3DERR_INVALIDCALL;
2785 TRACE("Setting shader %p.\n", shader_impl);
2786 wined3d_device_set_pixel_shader(device->wined3d_device, shader_impl->wined3d_shader);
2787 wined3d_mutex_unlock();
2789 return D3D_OK;
2792 static HRESULT WINAPI d3d8_device_GetPixelShader(IDirect3DDevice8 *iface, DWORD *shader)
2794 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2795 struct wined3d_shader *object;
2797 TRACE("iface %p, shader %p.\n", iface, shader);
2799 if (!shader)
2800 return D3DERR_INVALIDCALL;
2802 wined3d_mutex_lock();
2803 if ((object = wined3d_device_get_pixel_shader(device->wined3d_device)))
2805 struct d3d8_pixel_shader *d3d8_shader;
2806 d3d8_shader = wined3d_shader_get_parent(object);
2807 *shader = d3d8_shader->handle;
2809 else
2811 *shader = 0;
2813 wined3d_mutex_unlock();
2815 TRACE("Returning %#x.\n", *shader);
2817 return D3D_OK;
2820 static HRESULT WINAPI d3d8_device_DeletePixelShader(IDirect3DDevice8 *iface, DWORD shader)
2822 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2823 struct d3d8_pixel_shader *shader_impl;
2825 TRACE("iface %p, shader %#x.\n", iface, shader);
2827 wined3d_mutex_lock();
2829 if (!(shader_impl = d3d8_free_handle(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2831 WARN("Invalid handle (%#x) passed.\n", shader);
2832 wined3d_mutex_unlock();
2833 return D3DERR_INVALIDCALL;
2836 if (wined3d_device_get_pixel_shader(device->wined3d_device) == shader_impl->wined3d_shader)
2837 IDirect3DDevice8_SetPixelShader(iface, 0);
2839 wined3d_mutex_unlock();
2841 d3d8_pixel_shader_destroy(shader_impl);
2843 return D3D_OK;
2846 static HRESULT WINAPI d3d8_device_SetPixelShaderConstant(IDirect3DDevice8 *iface,
2847 DWORD start_register, const void *data, DWORD count)
2849 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2850 HRESULT hr;
2852 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2853 iface, start_register, data, count);
2855 wined3d_mutex_lock();
2856 hr = wined3d_device_set_ps_consts_f(device->wined3d_device, start_register, count, data);
2857 wined3d_mutex_unlock();
2859 return hr;
2862 static HRESULT WINAPI d3d8_device_GetPixelShaderConstant(IDirect3DDevice8 *iface,
2863 DWORD start_register, void *data, DWORD count)
2865 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2866 HRESULT hr;
2868 TRACE("iface %p, start_register %u, data %p, count %u.\n",
2869 iface, start_register, data, count);
2871 wined3d_mutex_lock();
2872 hr = wined3d_device_get_ps_consts_f(device->wined3d_device, start_register, count, data);
2873 wined3d_mutex_unlock();
2875 return hr;
2878 static HRESULT WINAPI d3d8_device_GetPixelShaderFunction(IDirect3DDevice8 *iface,
2879 DWORD shader, void *data, DWORD *data_size)
2881 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2882 struct d3d8_pixel_shader *shader_impl = NULL;
2883 HRESULT hr;
2885 TRACE("iface %p, shader %#x, data %p, data_size %p.\n",
2886 iface, shader, data, data_size);
2888 wined3d_mutex_lock();
2889 if (!(shader_impl = d3d8_get_object(&device->handle_table, shader - (VS_HIGHESTFIXEDFXF + 1), D3D8_HANDLE_PS)))
2891 WARN("Invalid handle (%#x) passed.\n", shader);
2892 wined3d_mutex_unlock();
2894 return D3DERR_INVALIDCALL;
2897 hr = wined3d_shader_get_byte_code(shader_impl->wined3d_shader, data, data_size);
2898 wined3d_mutex_unlock();
2900 return hr;
2903 static HRESULT WINAPI d3d8_device_DrawRectPatch(IDirect3DDevice8 *iface, UINT handle,
2904 const float *segment_count, const D3DRECTPATCH_INFO *patch_info)
2906 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2907 iface, handle, segment_count, patch_info);
2908 return D3D_OK;
2911 static HRESULT WINAPI d3d8_device_DrawTriPatch(IDirect3DDevice8 *iface, UINT handle,
2912 const float *segment_count, const D3DTRIPATCH_INFO *patch_info)
2914 FIXME("iface %p, handle %#x, segment_count %p, patch_info %p unimplemented.\n",
2915 iface, handle, segment_count, patch_info);
2916 return D3D_OK;
2919 static HRESULT WINAPI d3d8_device_DeletePatch(IDirect3DDevice8 *iface, UINT handle)
2921 FIXME("iface %p, handle %#x unimplemented.\n", iface, handle);
2922 return D3DERR_INVALIDCALL;
2925 static HRESULT WINAPI d3d8_device_SetStreamSource(IDirect3DDevice8 *iface,
2926 UINT stream_idx, IDirect3DVertexBuffer8 *buffer, UINT stride)
2928 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2929 struct d3d8_vertexbuffer *buffer_impl = unsafe_impl_from_IDirect3DVertexBuffer8(buffer);
2930 HRESULT hr;
2932 TRACE("iface %p, stream_idx %u, buffer %p, stride %u.\n",
2933 iface, stream_idx, buffer, stride);
2935 wined3d_mutex_lock();
2936 hr = wined3d_device_set_stream_source(device->wined3d_device, stream_idx,
2937 buffer_impl ? buffer_impl->wined3d_buffer : NULL, 0, stride);
2938 wined3d_mutex_unlock();
2940 return hr;
2943 static HRESULT WINAPI d3d8_device_GetStreamSource(IDirect3DDevice8 *iface,
2944 UINT stream_idx, IDirect3DVertexBuffer8 **buffer, UINT *stride)
2946 struct d3d8_device *device = impl_from_IDirect3DDevice8(iface);
2947 struct d3d8_vertexbuffer *buffer_impl;
2948 struct wined3d_buffer *wined3d_buffer = NULL;
2949 HRESULT hr;
2951 TRACE("iface %p, stream_idx %u, buffer %p, stride %p.\n",
2952 iface, stream_idx, buffer, stride);
2954 if (!buffer)
2955 return D3DERR_INVALIDCALL;
2957 wined3d_mutex_lock();
2958 hr = wined3d_device_get_stream_source(device->wined3d_device, stream_idx, &wined3d_buffer, 0, stride);
2959 if (SUCCEEDED(hr) && wined3d_buffer)
2961 buffer_impl = wined3d_buffer_get_parent(wined3d_buffer);
2962 *buffer = &buffer_impl->IDirect3DVertexBuffer8_iface;
2963 IDirect3DVertexBuffer8_AddRef(*buffer);
2965 else
2967 if (FAILED(hr))
2968 ERR("Failed to get wined3d stream source, hr %#x.\n", hr);
2969 *buffer = NULL;
2971 wined3d_mutex_unlock();
2973 return hr;
2976 static const struct IDirect3DDevice8Vtbl d3d8_device_vtbl =
2978 d3d8_device_QueryInterface,
2979 d3d8_device_AddRef,
2980 d3d8_device_Release,
2981 d3d8_device_TestCooperativeLevel,
2982 d3d8_device_GetAvailableTextureMem,
2983 d3d8_device_ResourceManagerDiscardBytes,
2984 d3d8_device_GetDirect3D,
2985 d3d8_device_GetDeviceCaps,
2986 d3d8_device_GetDisplayMode,
2987 d3d8_device_GetCreationParameters,
2988 d3d8_device_SetCursorProperties,
2989 d3d8_device_SetCursorPosition,
2990 d3d8_device_ShowCursor,
2991 d3d8_device_CreateAdditionalSwapChain,
2992 d3d8_device_Reset,
2993 d3d8_device_Present,
2994 d3d8_device_GetBackBuffer,
2995 d3d8_device_GetRasterStatus,
2996 d3d8_device_SetGammaRamp,
2997 d3d8_device_GetGammaRamp,
2998 d3d8_device_CreateTexture,
2999 d3d8_device_CreateVolumeTexture,
3000 d3d8_device_CreateCubeTexture,
3001 d3d8_device_CreateVertexBuffer,
3002 d3d8_device_CreateIndexBuffer,
3003 d3d8_device_CreateRenderTarget,
3004 d3d8_device_CreateDepthStencilSurface,
3005 d3d8_device_CreateImageSurface,
3006 d3d8_device_CopyRects,
3007 d3d8_device_UpdateTexture,
3008 d3d8_device_GetFrontBuffer,
3009 d3d8_device_SetRenderTarget,
3010 d3d8_device_GetRenderTarget,
3011 d3d8_device_GetDepthStencilSurface,
3012 d3d8_device_BeginScene,
3013 d3d8_device_EndScene,
3014 d3d8_device_Clear,
3015 d3d8_device_SetTransform,
3016 d3d8_device_GetTransform,
3017 d3d8_device_MultiplyTransform,
3018 d3d8_device_SetViewport,
3019 d3d8_device_GetViewport,
3020 d3d8_device_SetMaterial,
3021 d3d8_device_GetMaterial,
3022 d3d8_device_SetLight,
3023 d3d8_device_GetLight,
3024 d3d8_device_LightEnable,
3025 d3d8_device_GetLightEnable,
3026 d3d8_device_SetClipPlane,
3027 d3d8_device_GetClipPlane,
3028 d3d8_device_SetRenderState,
3029 d3d8_device_GetRenderState,
3030 d3d8_device_BeginStateBlock,
3031 d3d8_device_EndStateBlock,
3032 d3d8_device_ApplyStateBlock,
3033 d3d8_device_CaptureStateBlock,
3034 d3d8_device_DeleteStateBlock,
3035 d3d8_device_CreateStateBlock,
3036 d3d8_device_SetClipStatus,
3037 d3d8_device_GetClipStatus,
3038 d3d8_device_GetTexture,
3039 d3d8_device_SetTexture,
3040 d3d8_device_GetTextureStageState,
3041 d3d8_device_SetTextureStageState,
3042 d3d8_device_ValidateDevice,
3043 d3d8_device_GetInfo,
3044 d3d8_device_SetPaletteEntries,
3045 d3d8_device_GetPaletteEntries,
3046 d3d8_device_SetCurrentTexturePalette,
3047 d3d8_device_GetCurrentTexturePalette,
3048 d3d8_device_DrawPrimitive,
3049 d3d8_device_DrawIndexedPrimitive,
3050 d3d8_device_DrawPrimitiveUP,
3051 d3d8_device_DrawIndexedPrimitiveUP,
3052 d3d8_device_ProcessVertices,
3053 d3d8_device_CreateVertexShader,
3054 d3d8_device_SetVertexShader,
3055 d3d8_device_GetVertexShader,
3056 d3d8_device_DeleteVertexShader,
3057 d3d8_device_SetVertexShaderConstant,
3058 d3d8_device_GetVertexShaderConstant,
3059 d3d8_device_GetVertexShaderDeclaration,
3060 d3d8_device_GetVertexShaderFunction,
3061 d3d8_device_SetStreamSource,
3062 d3d8_device_GetStreamSource,
3063 d3d8_device_SetIndices,
3064 d3d8_device_GetIndices,
3065 d3d8_device_CreatePixelShader,
3066 d3d8_device_SetPixelShader,
3067 d3d8_device_GetPixelShader,
3068 d3d8_device_DeletePixelShader,
3069 d3d8_device_SetPixelShaderConstant,
3070 d3d8_device_GetPixelShaderConstant,
3071 d3d8_device_GetPixelShaderFunction,
3072 d3d8_device_DrawRectPatch,
3073 d3d8_device_DrawTriPatch,
3074 d3d8_device_DeletePatch,
3077 static inline struct d3d8_device *device_from_device_parent(struct wined3d_device_parent *device_parent)
3079 return CONTAINING_RECORD(device_parent, struct d3d8_device, device_parent);
3082 static void CDECL device_parent_wined3d_device_created(struct wined3d_device_parent *device_parent,
3083 struct wined3d_device *device)
3085 TRACE("device_parent %p, device %p\n", device_parent, device);
3088 static void CDECL device_parent_mode_changed(struct wined3d_device_parent *device_parent)
3090 TRACE("device_parent %p.\n", device_parent);
3093 static void CDECL device_parent_activate(struct wined3d_device_parent *device_parent, BOOL activate)
3095 struct d3d8_device *device = device_from_device_parent(device_parent);
3097 TRACE("device_parent %p, activate %#x.\n", device_parent, activate);
3099 if (!activate)
3100 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_LOST, D3D8_DEVICE_STATE_OK);
3101 else
3102 InterlockedCompareExchange(&device->device_state, D3D8_DEVICE_STATE_NOT_RESET, D3D8_DEVICE_STATE_LOST);
3105 static HRESULT CDECL device_parent_surface_created(struct wined3d_device_parent *device_parent,
3106 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3107 void **parent, const struct wined3d_parent_ops **parent_ops)
3109 struct d3d8_surface *d3d_surface;
3111 TRACE("device_parent %p, wined3d_texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3112 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3114 if (!(d3d_surface = heap_alloc_zero(sizeof(*d3d_surface))))
3115 return E_OUTOFMEMORY;
3117 surface_init(d3d_surface, wined3d_texture, sub_resource_idx, parent_ops);
3118 *parent = d3d_surface;
3119 TRACE("Created surface %p.\n", d3d_surface);
3121 return D3D_OK;
3124 static HRESULT CDECL device_parent_volume_created(struct wined3d_device_parent *device_parent,
3125 struct wined3d_texture *wined3d_texture, unsigned int sub_resource_idx,
3126 void **parent, const struct wined3d_parent_ops **parent_ops)
3128 struct d3d8_volume *d3d_volume;
3130 TRACE("device_parent %p, texture %p, sub_resource_idx %u, parent %p, parent_ops %p.\n",
3131 device_parent, wined3d_texture, sub_resource_idx, parent, parent_ops);
3133 if (!(d3d_volume = heap_alloc_zero(sizeof(*d3d_volume))))
3134 return E_OUTOFMEMORY;
3136 volume_init(d3d_volume, wined3d_texture, sub_resource_idx, parent_ops);
3137 *parent = d3d_volume;
3138 TRACE("Created volume %p.\n", d3d_volume);
3140 return D3D_OK;
3143 static HRESULT CDECL device_parent_create_swapchain_texture(struct wined3d_device_parent *device_parent,
3144 void *container_parent, const struct wined3d_resource_desc *desc, DWORD texture_flags,
3145 struct wined3d_texture **texture)
3147 struct d3d8_device *device = device_from_device_parent(device_parent);
3148 struct d3d8_surface *d3d_surface;
3149 HRESULT hr;
3151 TRACE("device_parent %p, container_parent %p, desc %p, texture flags %#x, texture %p.\n",
3152 device_parent, container_parent, desc, texture_flags, texture);
3154 if (FAILED(hr = wined3d_texture_create(device->wined3d_device, desc, 1, 1,
3155 texture_flags | WINED3D_TEXTURE_CREATE_MAPPABLE, NULL, &device->IDirect3DDevice8_iface,
3156 &d3d8_null_wined3d_parent_ops, texture)))
3158 WARN("Failed to create texture, hr %#x.\n", hr);
3159 return hr;
3162 d3d_surface = wined3d_texture_get_sub_resource_parent(*texture, 0);
3163 d3d_surface->parent_device = &device->IDirect3DDevice8_iface;
3165 return hr;
3168 static HRESULT CDECL device_parent_create_swapchain(struct wined3d_device_parent *device_parent,
3169 struct wined3d_swapchain_desc *desc, struct wined3d_swapchain **swapchain)
3171 struct d3d8_device *device = device_from_device_parent(device_parent);
3172 struct d3d8_swapchain *d3d_swapchain;
3173 HRESULT hr;
3175 TRACE("device_parent %p, desc %p, swapchain %p.\n", device_parent, desc, swapchain);
3177 if (FAILED(hr = d3d8_swapchain_create(device, desc, &d3d_swapchain)))
3179 WARN("Failed to create swapchain, hr %#x.\n", hr);
3180 *swapchain = NULL;
3181 return hr;
3184 *swapchain = d3d_swapchain->wined3d_swapchain;
3185 wined3d_swapchain_incref(*swapchain);
3186 IDirect3DSwapChain8_Release(&d3d_swapchain->IDirect3DSwapChain8_iface);
3188 return hr;
3191 static const struct wined3d_device_parent_ops d3d8_wined3d_device_parent_ops =
3193 device_parent_wined3d_device_created,
3194 device_parent_mode_changed,
3195 device_parent_activate,
3196 device_parent_surface_created,
3197 device_parent_volume_created,
3198 device_parent_create_swapchain_texture,
3199 device_parent_create_swapchain,
3202 static void setup_fpu(void)
3204 #if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
3205 WORD cw;
3206 __asm__ volatile ("fnstcw %0" : "=m" (cw));
3207 cw = (cw & ~0xf3f) | 0x3f;
3208 __asm__ volatile ("fldcw %0" : : "m" (cw));
3209 #elif defined(__i386__) && defined(_MSC_VER)
3210 WORD cw;
3211 __asm fnstcw cw;
3212 cw = (cw & ~0xf3f) | 0x3f;
3213 __asm fldcw cw;
3214 #else
3215 FIXME("FPU setup not implemented for this platform.\n");
3216 #endif
3219 HRESULT device_init(struct d3d8_device *device, struct d3d8 *parent, struct wined3d *wined3d, UINT adapter,
3220 D3DDEVTYPE device_type, HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
3222 struct wined3d_swapchain_desc swapchain_desc;
3223 struct wined3d_swapchain *wined3d_swapchain;
3224 HRESULT hr;
3226 device->IDirect3DDevice8_iface.lpVtbl = &d3d8_device_vtbl;
3227 device->device_parent.ops = &d3d8_wined3d_device_parent_ops;
3228 device->ref = 1;
3229 if (!(device->handle_table.entries = heap_alloc_zero(D3D8_INITIAL_HANDLE_TABLE_SIZE
3230 * sizeof(*device->handle_table.entries))))
3232 ERR("Failed to allocate handle table memory.\n");
3233 return E_OUTOFMEMORY;
3235 device->handle_table.table_size = D3D8_INITIAL_HANDLE_TABLE_SIZE;
3237 if (!(flags & D3DCREATE_FPU_PRESERVE)) setup_fpu();
3239 wined3d_mutex_lock();
3240 hr = wined3d_device_create(wined3d, adapter, device_type, focus_window, flags, 4,
3241 &device->device_parent, &device->wined3d_device);
3242 if (FAILED(hr))
3244 WARN("Failed to create wined3d device, hr %#x.\n", hr);
3245 wined3d_mutex_unlock();
3246 heap_free(device->handle_table.entries);
3247 return hr;
3250 if (!parameters->Windowed)
3252 HWND device_window = parameters->hDeviceWindow;
3254 if (!focus_window)
3255 focus_window = device_window;
3256 if (FAILED(hr = wined3d_device_acquire_focus_window(device->wined3d_device, focus_window)))
3258 ERR("Failed to acquire focus window, hr %#x.\n", hr);
3259 wined3d_device_decref(device->wined3d_device);
3260 wined3d_mutex_unlock();
3261 heap_free(device->handle_table.entries);
3262 return hr;
3265 if (!device_window)
3266 device_window = focus_window;
3267 wined3d_device_setup_fullscreen_window(device->wined3d_device, device_window,
3268 parameters->BackBufferWidth,
3269 parameters->BackBufferHeight);
3272 if (flags & D3DCREATE_MULTITHREADED)
3273 wined3d_device_set_multithreaded(device->wined3d_device);
3275 if (!wined3d_swapchain_desc_from_present_parameters(&swapchain_desc, parameters))
3277 wined3d_device_release_focus_window(device->wined3d_device);
3278 wined3d_device_decref(device->wined3d_device);
3279 wined3d_mutex_unlock();
3280 heap_free(device->handle_table.entries);
3281 return D3DERR_INVALIDCALL;
3284 if (FAILED(hr = wined3d_device_init_3d(device->wined3d_device, &swapchain_desc)))
3286 WARN("Failed to initialize 3D, hr %#x.\n", hr);
3287 wined3d_device_release_focus_window(device->wined3d_device);
3288 wined3d_device_decref(device->wined3d_device);
3289 wined3d_mutex_unlock();
3290 heap_free(device->handle_table.entries);
3291 return hr;
3294 wined3d_device_set_render_state(device->wined3d_device,
3295 WINED3D_RS_ZENABLE, !!swapchain_desc.enable_auto_depth_stencil);
3296 wined3d_device_set_render_state(device->wined3d_device, WINED3D_RS_POINTSIZE_MIN, 0);
3297 wined3d_mutex_unlock();
3299 present_parameters_from_wined3d_swapchain_desc(parameters, &swapchain_desc);
3301 device->declArraySize = 16;
3302 if (!(device->decls = heap_alloc(device->declArraySize * sizeof(*device->decls))))
3304 ERR("Failed to allocate FVF vertex declaration map memory.\n");
3305 hr = E_OUTOFMEMORY;
3306 goto err;
3309 wined3d_swapchain = wined3d_device_get_swapchain(device->wined3d_device, 0);
3310 device->implicit_swapchain = wined3d_swapchain_get_parent(wined3d_swapchain);
3312 device->d3d_parent = &parent->IDirect3D8_iface;
3313 IDirect3D8_AddRef(device->d3d_parent);
3315 return D3D_OK;
3317 err:
3318 wined3d_mutex_lock();
3319 wined3d_device_uninit_3d(device->wined3d_device);
3320 wined3d_device_release_focus_window(device->wined3d_device);
3321 wined3d_device_decref(device->wined3d_device);
3322 wined3d_mutex_unlock();
3323 heap_free(device->handle_table.entries);
3324 return hr;