Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ Useful switches:
open is always traced as `file_open_failed` (path, error, caller) when `--trace` is on.
* `LEONIDA_VK_VALIDATION=1` enables the Khronos validation layer with the Vulkan backend (startup overhead; it is a
diagnostic).
* `LEONIDA_TEXTURE_MIP_DROP=N` (0..3, default 0, #6043) drops the top N mip level(s) of pure sampled textures at
creation time to reduce VRAM consumption on low-VRAM GPUs (e.g. 6 GB or 4 GB cards). Dropping 1 mip halves width
and height, saving ~75% texture memory without altering render targets, depth-stencil surfaces, or UAVs.
* `DISABLE_RTSS_LAYER=1` and `DISABLE_VULKAN_OBS_CAPTURE=1` when MSI Afterburner or OBS is installed; their
implicit layers add swapchain usage that shows up as validation errors ([testing.md](testing.md), "Known
problems").
Expand Down
5 changes: 4 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,10 @@ The runtime allocates image keys from `0x100000000` (4 GiB) in 256-byte steps, a
- Textures and default-heap buffers outside the arena prefer `DEVICE_LOCAL` and need no host access. When VRAM is
full they still get memory, from the next type `memory_type_order` lists, which is system memory. The first one
that does is logged once (`[rt] d3d12: a N-byte allocation did not fit in device-local memory ...`, D3D-299,
`leonida::spilled_to_host`), so a run on a 4 GB GPU shows that it spilled (#2375).
`leonida::spilled_to_host`), so a run on a 4 GB GPU shows that it spilled (#2375). To fit textures within 4 GB
or 6 GB VRAM budgets and avoid host spills, `LEONIDA_TEXTURE_MIP_DROP=N` (0..3, default 0, #6043) drops the top N
mip levels from pure sampled textures at creation time (saving ~75% VRAM for drop 1, ~93.75% for drop 2) while
preserving render targets, depth-stencil surfaces, and UAV attachments at full resolution.

**Capacity.** The arena is sized by buffers, descriptor heaps and shader code, not by textures. The allocator
tracks the high-water mark; start with a configurable capacity (for example 4 GiB for the loading/menu path) and
Expand Down
7 changes: 7 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,13 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_texture1d COMMAND d3d12_texture1d_test)
set_tests_properties(d3d12_texture1d PROPERTIES SKIP_RETURN_CODE 77)

add_executable(d3d12_texture_mip_drop_test test/d3d12_texture_mip_drop_test.cpp)
target_link_libraries(d3d12_texture_mip_drop_test PRIVATE gta6rt_d3d12_com dxguid)
target_compile_definitions(d3d12_texture_mip_drop_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_texture_mip_drop_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME d3d12_texture_mip_drop COMMAND d3d12_texture_mip_drop_test)
set_tests_properties(d3d12_texture_mip_drop PROPERTIES SKIP_RETURN_CODE 77)

# Placed 1D and 3D textures against the host runtime and WARP, and a copy round trip through them.
add_executable(d3d12_placed_dimensions_test test/d3d12_placed_dimensions_test.cpp)
target_link_libraries(d3d12_placed_dimensions_test PRIVATE gta6rt_d3d12_com d3d12 dxgi)
Expand Down
128 changes: 119 additions & 9 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,25 @@ UINT view_slices(const D3D12_RESOURCE_DESC& resource, UINT first_layer, UINT lay
return remaining ? resource.DepthOrArraySize - first_layer : layer_count;
}

namespace {
std::atomic<int> s_texture_mip_drop{-1};
}

// Returns 0, 1, 2, or 3 from LEONIDA_TEXTURE_MIP_DROP (default 0).
// Uses a constant-initialized atomic to avoid function-local static init guards on guest threads (RT-488).
inline UINT configured_texture_mip_drop() {
int drop = s_texture_mip_drop.load(std::memory_order_relaxed);
if (drop >= 0) return static_cast<UINT>(drop);
char text[16]{};
size_t length = 0;
int parsed = 0;
if (getenv_s(&length, text, "LEONIDA_TEXTURE_MIP_DROP") == 0 && length > 1) {
parsed = std::clamp(std::atoi(text), 0, 3);
}
s_texture_mip_drop.store(parsed, std::memory_order_relaxed);
return static_cast<UINT>(parsed);
}

// The VkImage a texture description becomes, with its mip count already resolved (MipLevels != 0). Shared by
// texture creation and GetResourceAllocationInfo, so the sizes reported are the sizes later allocated.
// Keeps the optional VkImageFormatListCreateInfo alongside VkImageCreateInfo so info.pNext stays valid.
Expand All @@ -494,6 +513,7 @@ struct TextureCreateInfo {
VkImageCreateInfo info{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
VkImageFormatListCreateInfo format_list{VK_STRUCTURE_TYPE_IMAGE_FORMAT_LIST_CREATE_INFO};
std::array<VkFormat, 2> view_formats{};
UINT mips_dropped = 0;

TextureCreateInfo(const D3D12_RESOURCE_DESC& desc, const Format& format, bool attachment_usable) {
const bool volume = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
Expand All @@ -503,9 +523,24 @@ struct TextureCreateInfo {
info.flags |= VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT;
info.imageType = volume ? VK_IMAGE_TYPE_3D : one_d ? VK_IMAGE_TYPE_1D : VK_IMAGE_TYPE_2D;
info.format = format.format;
info.extent = {std::uint32_t(desc.Width), one_d ? 1u : desc.Height,
volume ? UINT(desc.DepthOrArraySize) : 1u};
info.mipLevels = desc.MipLevels;

// Low-VRAM texture quality switch (LEONIDA_TEXTURE_MIP_DROP, #6043):
// Only drop mips for pure sampled textures (no RenderTarget, DepthStencil, or UAV):
const bool is_render_target = (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) != 0;
const bool is_depth_stencil = (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL) != 0;
const bool is_uav = (desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS) != 0;
const bool can_drop = !is_render_target && !is_depth_stencil && !is_uav;

if (can_drop && desc.MipLevels > 1) {
mips_dropped = std::min<UINT>(configured_texture_mip_drop(), desc.MipLevels - 1);
}

const uint32_t width = std::max(1u, static_cast<uint32_t>(desc.Width >> mips_dropped));
const uint32_t height = one_d ? 1u : std::max(1u, desc.Height >> mips_dropped);
const uint32_t depth = volume ? std::max(1u, UINT(desc.DepthOrArraySize) >> mips_dropped) : 1u;

info.extent = {width, height, depth};
info.mipLevels = desc.MipLevels - mips_dropped;
info.arrayLayers = volume ? 1 : desc.DepthOrArraySize;
info.samples = VkSampleCountFlagBits(std::max(1u, desc.SampleDesc.Count));
info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
Expand Down Expand Up @@ -1841,6 +1876,8 @@ class Resource final
VkImageAspectFlags aspects() const { return aspects_; }
bool rgb96() const { return rgb96_; } // a 96-bit texture in an RGBA32 image
std::uint64_t image_address() const { return image_address_; } // what a T# names the texture by
UINT mips_dropped() const { return mips_dropped_; }
UINT image_mip_levels() const { return desc_.MipLevels > mips_dropped_ ? desc_.MipLevels - mips_dropped_ : 1; }
VkMemoryPropertyFlags memory_flags() const { return memory_flags_; } // of the memory type behind it
const DeviceState& device_state() const { return device_; }

Expand Down Expand Up @@ -2191,6 +2228,7 @@ class Resource final
const bool one_d = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
if (desc_.MipLevels == 0) desc_.MipLevels = full_mips(desc_.Width, desc_.Height, depth);
TextureCreateInfo create(desc_, format, device_.color_attachment_usable(format.format));
mips_dropped_ = create.mips_dropped;
VkImageCreateInfo& info = create.info;
if (!castable.empty()) {
// D3D12 always allows a view in the resource's own format, whether or not the castable list names
Expand Down Expand Up @@ -2252,14 +2290,15 @@ class Resource final
binding.depth24 = format.d24;
binding.format = format.format;
binding.aspects = format.aspects;
binding.width = std::uint32_t(desc_.Width);
binding.height = one_d ? 1u : desc_.Height;
binding.mip_levels = desc_.MipLevels;
binding.width = info.extent.width;
binding.height = info.extent.height;
binding.mip_levels = info.mipLevels;
binding.array_layers = info.arrayLayers;
binding.depth = depth;
binding.depth = info.extent.depth;
binding.volume = volume;
binding.line = one_d;
binding.samples = samples;
binding.mips_dropped = mips_dropped_;
binding.color_attachment = (info.usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) != 0;
binding.sampled = (info.usage & VK_IMAGE_USAGE_SAMPLED_BIT) != 0;
binding.simultaneous_access = (desc_.Flags & D3D12_RESOURCE_FLAG_ALLOW_SIMULTANEOUS_ACCESS) != 0;
Expand Down Expand Up @@ -2354,9 +2393,22 @@ class Resource final
// WriteToSubresource/ReadFromSubresource.
HRESULT STDMETHODCALLTYPE Map(UINT subresource, const D3D12_RANGE* read, void** data) override {
if (image_) {
if (subresource == leonida::all_subresources) return E_INVALIDARG;
const UINT orig_layers = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? 1 : desc_.DepthOrArraySize;
const UINT total_subresources = desc_.MipLevels * orig_layers;
if (subresource >= total_subresources) return E_INVALIDARG;
if (mips_dropped_ > 0) {
const UINT orig_mips = desc_.MipLevels;
const UINT mip = subresource % orig_mips;
const UINT slice = subresource / orig_mips;
if (mip < mips_dropped_) {
return !data && cpu_visible() ? S_OK : E_INVALIDARG;
}
const UINT new_mips = desc_.MipLevels - mips_dropped_;
subresource = slice * new_mips + (mip - mips_dropped_);
}
VkImageSubresourceRange range{};
return !data && cpu_visible() && subresource != leonida::all_subresources &&
leonida::image_range(binding_, subresource, range)
return !data && cpu_visible() && leonida::image_range(binding_, subresource, range)
? S_OK
: E_INVALIDARG;
}
Expand Down Expand Up @@ -2424,6 +2476,44 @@ class Resource final
HRESULT cpu_copy(bool write, UINT subresource, const D3D12_BOX* box, void* data, UINT row_pitch,
UINT depth_pitch) {
if (!image_ || !data) return E_INVALIDARG;
if (subresource == leonida::all_subresources) return E_INVALIDARG;
const UINT orig_layers = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? 1 : desc_.DepthOrArraySize;
const UINT total_subresources = desc_.MipLevels * orig_layers;
if (subresource >= total_subresources) return E_INVALIDARG;
if (mips_dropped_ > 0) {
const UINT orig_mips = desc_.MipLevels;
const UINT mip = subresource % orig_mips;
const UINT slice = subresource / orig_mips;
if (mip < mips_dropped_) {
if (write) return S_OK;
// Read from a dropped mip: zero the requested box/rows/slices across row_pitch and depth_pitch.
const bool volume = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D;
const bool one_d = desc_.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D;
const std::uint32_t width = std::max(1u, std::uint32_t(desc_.Width) >> mip);
const std::uint32_t height = one_d ? 1u : std::max(1u, desc_.Height >> mip);
const std::uint32_t depth = volume ? std::max(1u, UINT(desc_.DepthOrArraySize) >> mip) : 1u;
const D3D12_BOX b = box ? *box : D3D12_BOX{0, 0, 0, width, height, depth};
if (b.right > width || b.bottom > height || b.back > depth) return E_INVALIDARG;
const auto block = leonida::copy_block_format(binding_, aspects_);
if (!block.bytes) return E_INVALIDARG;
const std::uint32_t k = block.block;
if (b.left % k || b.top % k || (b.right % k && b.right != width) || (b.bottom % k && b.bottom != height))
return E_INVALIDARG;
if (b.left >= b.right || b.top >= b.bottom || b.front >= b.back) return S_OK;
const std::uint32_t rows = (b.bottom - b.top + k - 1) / k;
const std::uint64_t row_bytes = std::uint64_t((b.right - b.left + k - 1) / k) * block.bytes;
const std::uint32_t slices = b.back - b.front;
auto* bytes = static_cast<std::byte*>(data);
for (std::uint32_t z = 0; z < slices; ++z) {
for (std::uint32_t y = 0; y < rows; ++y) {
std::memset(bytes + std::uint64_t(z) * depth_pitch + std::uint64_t(y) * row_pitch, 0, row_bytes);
}
}
return S_OK;
}
const UINT new_mips = desc_.MipLevels - mips_dropped_;
subresource = slice * new_mips + (mip - mips_dropped_);
}
Region r{};
if (!region(subresource, box, r)) return E_INVALIDARG;
if (!r.rows || !r.slices) return S_OK; // an empty box: nothing to copy
Expand Down Expand Up @@ -2632,6 +2722,7 @@ class Resource final
// A placed buffer's heap range, held as a D3D12 placed resource holds its heap: the app may release the heap
// first, and the range must not go to another allocation while this buffer lives.
std::shared_ptr<const leonida::TileHeapAllocation> heap_range_;
UINT mips_dropped_ = 0;
};

// DXGI -> Vulkan for vertex attributes, with each element's size for D3D12_APPEND_ALIGNED_ELEMENT.
Expand Down Expand Up @@ -6580,6 +6671,25 @@ class Device final
return;
}
}
if (r->mips_dropped() > 0) {
const UINT drop = r->mips_dropped();
if (view.first_mip >= drop) {
view.first_mip -= drop;
} else {
const UINT dropped_from_view = drop - view.first_mip;
view.first_mip = 0;
if (view.mip_count != UINT_MAX) {
view.mip_count = (view.mip_count > dropped_from_view) ? (view.mip_count - dropped_from_view) : 1;
}
}
const UINT avail_mips = r->image_mip_levels() > view.first_mip ? (r->image_mip_levels() - view.first_mip) : 1;
if (view.mip_count != UINT_MAX && view.mip_count > 0) {
view.mip_count = std::min(view.mip_count, avail_mips);
}
if (view.min_lod > 0.0f) {
view.min_lod = std::max(0.0f, view.min_lod - float(drop));
}
}
write_image_descriptor(*r, view, destination, false);
}

Expand Down
Loading