d3d12_title_image_census: guard null entries/resources and use stable_sort

This commit is contained in:
Narek Shaghoyan
2026-11-26 13:56:21 +00:00
parent 73516a2902
commit d11cbcc1f5
2 changed files with 76 additions and 2 deletions

View File

@ -24,6 +24,7 @@ void log_title_image_uses(const Registry& textures, std::size_t retired_count, s
using Shape = std::tuple<std::uint64_t, unsigned, unsigned, unsigned, unsigned>;
std::map<Shape, AllocationClass> 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<std::pair<Shape, AllocationClass>> 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<std::size_t>(8, ranked.size()); ++i) {

View File

@ -0,0 +1,72 @@
#include "../shims/d3d12_title_image_census.h"
#include <cstdint>
#include <cstdio>
#include <map>
#include <memory>
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<FakeResource> resource;
};
} // namespace
int main() {
std::map<std::uint64_t, std::unique_ptr<FakeEntry>> textures;
// Valid placed entry.
auto placed = std::make_unique<FakeEntry>();
placed->cpu = 0x1000;
placed->backing_bytes = 2ull << 20;
placed->resource = std::make_unique<FakeResource>();
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<FakeEntry>();
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<FakeEntry>();
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;
}