common: SlotVector without generations

This commit is contained in:
nmzik
2026-10-24 03:48:46 +02:00
parent d7856753e0
commit 8dc0d56b7e
7 changed files with 203 additions and 126 deletions
+25 -66
View File
@@ -331,22 +331,6 @@ struct TextureCacheTestAccess {
return count;
}
static void AddPageOwner(TextureCache &cache, uint64_t address, ImageId id) {
std::lock_guard lock(cache.m_lock);
cache
.m_image_page_table[static_cast<size_t>(
address >> TextureCache::ImagePageTable::kPageBits)]
.push_back(id);
}
static bool RemovePageOwner(TextureCache &cache, uint64_t address,
ImageId id) {
std::lock_guard lock(cache.m_lock);
auto *owners = cache.m_image_page_table.Find(static_cast<size_t>(
address >> TextureCache::ImagePageTable::kPageBits));
return owners != nullptr && owners->Erase(id);
}
static void SetQueryEpoch(TextureCache &cache, uint32_t epoch) {
std::lock_guard lock(cache.m_lock);
cache.m_image_query_epoch = epoch;
@@ -4347,11 +4331,8 @@ public:
constexpr uint64_t recycled_offset = index_offset + 4 * index_page;
const auto recycled = cache.FindBuffer(base + recycled_offset + 0x100,
sizeof(uint32_t));
Require(name, "slot generation reuse",
(recycled.index == index_left.index && recycled != index_left) ||
(recycled.index == index_right.index &&
recycled != index_right),
"retired Buffer slot was not reused with a new generation");
Require(name, "slot reuse", recycled == index_left || recycled == index_right,
"retired Buffer slot was not reused");
const auto [resolved_left, resolved_left_offset] = cache.ObtainBuffer(
index_begin + 0x100, sizeof(uint32_t), true, false, index_left);
const auto [resolved_right, resolved_right_offset] =
@@ -5465,6 +5446,28 @@ public:
Require(name, "unmapped descriptor", !textures.IsRegionRegistered(texture(23), 0x1000u),
"a descriptor over unmapped memory was resolved");
// An idle view keeps the element of a freed image. Once a new image takes its slot,
// reactivating the view must resolve the descriptor again.
write(30, descriptor(30, Format::k8_8_8_8UNorm));
Require(name, "idle element", acquire(unfiltered), "the view was declined");
draw();
const auto erased = ImageHeapTestAccess::ImageOf(heap, idle.slots[240]);
textures.UnmapMemory(texture(30), 0x1000u);
Require(name, "replacement", acquire(sampled), "the view was declined");
draw();
write(31, descriptor(31, Format::k8_8_8_8UNorm));
Require(name, "slot reuse", acquire(sampled), "the view was declined");
draw();
Require(name, "reused slot",
erased && ImageHeapTestAccess::ImageOf(heap, filtered.slots[248]) == erased,
"the next image did not take the freed image's slot");
Require(name, "reactivated idle view", acquire(unfiltered), "the view was declined");
draw();
Require(name, "reused slot element",
ImageHeapTestAccess::ImageOf(heap, idle.slots[240]) ==
ImageHeapTestAccess::ImageOf(heap, filtered.slots[240]),
"a reactivated view kept an element whose image slot holds a new image");
heap.UnmapMemory(table, table_size);
Require(name, "unmapped table",
ImageHeapTestAccess::MemoryCount(heap) == 0u &&
@@ -6060,16 +6063,6 @@ public:
large_owner) == 0,
"large image unregister left coarse memberships behind");
const ImageId stale{first.index, first.generation + 1};
TextureCacheTestAccess::AddPageOwner(texture_cache, base, stale);
const auto stale_filtered = TextureCacheTestAccess::FindImages(
texture_cache, base, sizeof(initial), false);
Require(name, "stale page owner filtering",
IsOnlyImage(stale_filtered, first) &&
TextureCacheTestAccess::RemovePageOwner(texture_cache, base,
stale),
"a stale generation escaped the direct page-owner lookup");
TextureCacheTestAccess::SetQueryEpoch(texture_cache, UINT32_MAX);
const auto wrap_results = TextureCacheTestAccess::FindImages(
texture_cache, base, sizeof(initial), false);
@@ -17009,9 +17002,7 @@ public:
scheduler.EndRendering();
RenderExecutorTestAccess::ResetBindings(executor);
Require(name, "draw-scoped target reset",
TextureCacheTestAccess::Owner(texture_cache,
target_subresource_id) == nullptr &&
!texture_cache.GetImage(target_parent_id).binding.is_target &&
!texture_cache.GetImage(target_parent_id).binding.is_target &&
!texture_cache.GetImage(depth_id).binding.is_target,
"RenderExecutor retained target binding state after reset");
@@ -48130,37 +48121,6 @@ void CheckDepthTargetFootprints() {
std::printf("[host] %-32s ok\n", "DepthTargetFootprints");
}
struct SlotLifetimeProbe {
explicit SlotLifetimeProbe(bool *destroyed) : destroyed(destroyed) {
if (destroyed == nullptr || *destroyed) {
EXIT("fence-lifetime probe has invalid construction state\n");
}
}
~SlotLifetimeProbe() { *destroyed = true; }
bool *destroyed = nullptr;
};
void CheckSlotVectorLifetime() {
bool destroyed = false;
Common::SlotVector<SlotLifetimeProbe> slots;
const auto first = slots.insert(&destroyed);
Require("SlotVectorLifetime", "inserted",
slots.is_allocated(first) && slots.try_get(first) != nullptr &&
!destroyed,
"slot insertion did not expose its cache-owned object");
slots.erase(first);
const bool first_destroyed = destroyed;
destroyed = false;
const auto second = slots.insert(&destroyed);
Require("SlotVectorLifetime", "generation",
first_destroyed && !destroyed && first.index == second.index &&
first != second && !slots.is_allocated(first) &&
slots.is_allocated(second),
"reused slot accepted a stale cache resource id");
std::printf("[host] %-32s ok\n", "SlotVectorLifetime");
}
void CheckEmbeddedFetchLaneSpill() {
std::vector<u32> code;
code.push_back(EncodeSMovB32(0, InlineU32(0)));
@@ -51297,7 +51257,6 @@ int main(int argc, char **argv) {
CheckDepthFeedbackAspects();
CheckDynamicRenderingState();
CheckDepthTargetFootprints();
CheckSlotVectorLifetime();
#else
if (argc != 1) {
std::fprintf(stderr, "unknown test selector: %s\n", argv[1]);
+103
View File
@@ -0,0 +1,103 @@
#include "common/slotVector.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <utility>
#include <vector>
namespace {
void Check(bool value, const char *message) {
if (!value) {
std::fprintf(stderr, "SlotVectorTests: failed: %s\n", message);
std::abort();
}
}
// Non-movable like the cached Image/Buffer; counts live objects.
struct Probe {
Probe(int *live, uint32_t value) : live(live), value(value) { ++*live; }
~Probe() { --*live; }
KYTY_CLASS_NO_COPY(Probe);
int *live;
uint32_t value;
};
static_assert(sizeof(Common::SlotId) == sizeof(uint32_t));
void TestInsertEraseReuse() {
int live = 0;
Common::SlotVector<Probe> slots;
Check(!slots.is_allocated(Common::SlotId {}) && slots.try_get(Common::SlotId {}) == nullptr &&
!slots.is_allocated(Common::SlotId {0}),
"an empty vector reported an allocated slot");
const auto a = slots.insert(&live, 0u);
const auto b = slots.insert(&live, 1u);
const auto c = slots.insert(&live, 2u);
Check(a.index == 0 && b.index == 1 && c.index == 2 && live == 3,
"fresh slots were not handed out in order");
Check(slots.is_allocated(b) && slots.try_get(b) == &slots[b] && slots[b].value == 1,
"an inserted slot did not expose its object");
Check(!slots.is_allocated(Common::SlotId {3}), "an unused slot reported an object");
slots.erase(b);
slots.erase(a);
Check(live == 1 && !slots.is_allocated(a) && !slots.is_allocated(b) &&
slots.try_get(b) == nullptr && slots.is_allocated(c),
"erase did not destroy and free exactly its slot");
// Freed indices are reused most recently freed first, with the same plain id.
const auto reused_a = slots.insert(&live, 0u);
const auto reused_b = slots.insert(&live, 1u);
const auto d = slots.insert(&live, 3u);
Check(reused_a == a && reused_b == b && d.index == 3 && live == 4,
"freed slots were not reused in LIFO order");
Check(slots[reused_a].value == 0 && slots[reused_b].value == 1,
"a reused slot did not expose its new object");
}
void TestForEachAndStability() {
int live = 0;
{
Common::SlotVector<Probe> slots;
const auto first = slots.insert(&live, 0u);
Probe *const first_address = &slots[first];
// Cross several chunks; earlier objects must keep their address.
std::vector<Common::SlotId> ids {first};
for (uint32_t i = 1; i < 1000; ++i) {
ids.push_back(slots.insert(&live, i));
}
Check(&slots[first] == first_address && live == 1000,
"growing the vector moved a stored object");
std::vector<uint32_t> expected;
for (uint32_t i = 0; i < 1000; ++i) {
if (i % 3 == 0) {
slots.erase(ids[i]);
} else {
expected.push_back(i);
}
}
std::vector<uint32_t> visited;
std::as_const(slots).ForEach([&](Common::SlotId id, const Probe &probe) {
Check(probe.value == id.index, "ForEach passed an id that does not match its object");
visited.push_back(id.index);
});
Check(visited == expected && live == static_cast<int>(expected.size()),
"ForEach did not visit exactly the live slots in index order");
}
Check(live == 0, "destroying the vector did not destroy every live object exactly once");
}
} // namespace
int main() {
TestInsertEraseReuse();
TestForEachAndStability();
std::puts("SlotVectorTests: all cases passed");
return 0;
}