From d11cbcc1f552f2f4fb2a4c23f8964d33346d698b Mon Sep 17 00:00:00 2001 From: Narek Shaghoyan Date: Thu, 26 Nov 2026 13:56:21 +0000 Subject: [PATCH] d3d12_title_image_census: guard null entries/resources and use stable_sort --- runtime/shims/d3d12_title_image_census.h | 6 +- .../test/d3d12_title_image_census_test.cpp | 72 +++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 runtime/test/d3d12_title_image_census_test.cpp diff --git a/runtime/shims/d3d12_title_image_census.h b/runtime/shims/d3d12_title_image_census.h index 4d374fce8..5dc496c52 100644 --- a/runtime/shims/d3d12_title_image_census.h +++ b/runtime/shims/d3d12_title_image_census.h @@ -24,6 +24,7 @@ void log_title_image_uses(const Registry& textures, std::size_t retired_count, s using Shape = std::tuple; std::map classes; for (const auto& [address, t] : textures) { + if (!t) continue; if (t->owned) { unbound += t->unbound_bytes != 0; unbound_bytes += t->unbound_bytes; @@ -32,6 +33,7 @@ void log_title_image_uses(const Registry& textures, std::size_t retired_count, s unbound_placed += t->unbound_bytes != 0; unbound_placed_bytes += t->unbound_bytes; placed_addresses.insert(t->cpu); + if (!t->resource) continue; const auto& d = t->resource->desc(); auto& c = classes[{d.Width, d.Height, unsigned(d.Format), unsigned(d.Flags), d.SampleDesc.Count}]; ++c.count; @@ -52,7 +54,7 @@ void log_title_image_uses(const Registry& textures, std::size_t retired_count, s else if (!t->fills) cpu_unfilled += t->backing_bytes; } for (const auto& [address, t] : textures) { - if (!t->owned || !placed_addresses.contains(t->cpu)) continue; + if (!t || !t->owned || !placed_addresses.contains(t->cpu)) continue; ++duplicate_count; duplicates += t->backing_bytes; } @@ -63,7 +65,7 @@ void log_title_image_uses(const Registry& textures, std::size_t retired_count, s "shader descriptors; %.1f MiB with no replay, shader descriptor, map or fill " "(%.1f MiB 4K attachments)\n", replayed * mib, shader_only * mib, cold * mib, cold_4k * mib); std::vector> ranked(classes.begin(), classes.end()); - std::sort(ranked.begin(), ranked.end(), [](const auto& a, const auto& b) { + std::stable_sort(ranked.begin(), ranked.end(), [](const auto& a, const auto& b) { return a.second.bytes + a.second.unbound > b.second.bytes + b.second.unbound; }); for (std::size_t i = 0; i < std::min(8, ranked.size()); ++i) { diff --git a/runtime/test/d3d12_title_image_census_test.cpp b/runtime/test/d3d12_title_image_census_test.cpp new file mode 100644 index 000000000..5eaa301b9 --- /dev/null +++ b/runtime/test/d3d12_title_image_census_test.cpp @@ -0,0 +1,72 @@ +#include "../shims/d3d12_title_image_census.h" + +#include +#include +#include +#include + +namespace { + +struct FakeSampleDesc { + unsigned Count = 1; +}; + +struct FakeDesc { + std::uint64_t Width = 1920; + unsigned Height = 1080; + unsigned Format = 28; + unsigned Flags = 0; + FakeSampleDesc SampleDesc{}; +}; + +struct FakeResource { + FakeDesc d{}; + bool replayed = false; + bool views = false; + const FakeDesc& desc() const { return d; } + bool replay_seen() const { return replayed; } + bool shader_views() const { return views; } +}; + +struct FakeEntry { + bool owned = false; + std::uint64_t unbound_bytes = 0; + std::uint64_t backing_bytes = 0; + std::uint64_t cpu = 0; + bool mapped = false; + unsigned fills = 0; + bool attachment_4k = false; + bool gpu_written = false; + std::unique_ptr resource; +}; + +} // namespace + +int main() { + std::map> textures; + + // Valid placed entry. + auto placed = std::make_unique(); + placed->cpu = 0x1000; + placed->backing_bytes = 2ull << 20; + placed->resource = std::make_unique(); + textures.emplace(1, std::move(placed)); + + // Regression: null entry pointer and placed entry with null resource must not crash. + textures.emplace(2, nullptr); + auto no_res = std::make_unique(); + no_res->cpu = 0x2000; + no_res->backing_bytes = 1ull << 20; + textures.emplace(3, std::move(no_res)); + + // Owned entry aliasing placed CPU address. + auto owned = std::make_unique(); + owned->owned = true; + owned->cpu = 0x1000; + owned->backing_bytes = 2ull << 20; + textures.emplace(4, std::move(owned)); + + rt::d3d12::log_title_image_uses(textures, 1, 1ull << 20, 0); + std::puts("d3d12_title_image_census_test: ok"); + return 0; +}