d3d12: a title texture over a render target's memory is not filled from it; textures are read at first use and by their written tiles (RT-TITLE-TEXTURE-WRITE-TRACKED)

Blocks of older frames in run 58: a texture without GPU-write flags placed over the memory of a GPU-written
one (a render target, depth or UAV texture) was filled from the title's pages there. Those pages never hold
what our GPU renders into the target's image, only what the CPU last put there, and every write mark over
them (a write-back of a GPU copy, a neighbour's write in the same tile) re-uploaded those old bytes. The
registry now keeps the CPU-view range of every GPU-written texture (DeviceState::gpu_owned), and a texture
over such a range is not watched or filled by the refresh (index_texture); one already watched stops when the
target is registered. Test: d3d12_existing_heap's "texture over a render target's memory" (fails without
this: the written 64 KiB piece shows in the texture).

Upload at first use: a title texture no descriptor names is not read, uploaded or watched. Its first
descriptor of any kind (a D3D12 view, a slot-70 view, a T# the title wrote into a console heap: all reach
Resource::note_shader_view) queues it, and the refresh before that submission reads it whole and uploads it.
A texture whose descriptors are all gone later stays on the write-driven path (nothing counts them down).

Reads by written tiles: each texture keeps a hash of its part in every 64 KiB tile (TilePieces). When some
of its tiles were written, those parts are hashed in place first; equal, and the texture is unchanged
without a whole read. The watch reports writes per 64 KiB tile (the geometry cells' fault reopens a whole
tile), so a neighbour in a written tile costs its share of that tile, not a read of all of it.

The 10 s line: textures written or new, read whole, found unchanged by their parts, uploaded, named for the
first time, not named by any descriptor (not read), ms a pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Jan Trencansky
2026-11-26 18:41:15 -05:00
parent 1f2eac9c48
commit f7c3df1724
4 changed files with 327 additions and 10 deletions

View File

@ -3062,6 +3062,29 @@ struct DeviceState {
TitleTextureCells texture_cells;
leonida::TitleTextureWatch<TitleTextureCells> texture_watch{texture_cells};
void index_texture(const TitleTexture& t);
// The CPU-view ranges of the registered GPU-written textures (render targets, depth, unordered access), by their
// registry address. A texture over such memory is not filled from it (index_texture): the title's pages there
// hold what the CPU last put there, never what our GPU renders into the image, and a fill uploaded those old
// bytes, in 64 KiB pieces as write-backs and neighbours' writes marked their tiles written. Under
// title_texture_mutex.
std::map<std::uint64_t, std::pair<std::uintptr_t, std::uintptr_t>> gpu_owned;
std::uint64_t gpu_owned_refusals = 0;
bool over_gpu_owned(std::uintptr_t begin, std::uintptr_t end) const {
for (const auto& [key, range] : gpu_owned)
if (range.first < end && begin < range.second) return true;
return false;
}
void add_gpu_owned(std::uint64_t key, std::uintptr_t cpu, std::uint64_t span);
// Textures named by their first descriptor since the last refresh (Resource::note_shader_view; any thread, under
// any lock: this one is a leaf).
std::mutex titles_named_mutex;
std::vector<std::uint64_t> titles_named;
std::atomic<bool> titles_named_pending{false};
void title_texture_named(std::uint64_t address) {
std::lock_guard lock(titles_named_mutex);
titles_named.push_back(address);
titles_named_pending.store(true, std::memory_order_release);
}
void title_texture_mapped(const void* resource, std::uint64_t address);
void title_texture_unmapped(const void* resource, std::uint64_t address); // the title wrote texels
void title_texture_gone(const void* resource, std::uint64_t address);
@ -8630,7 +8653,12 @@ public:
}
// Map and the destructor notify the registry for a slot-133 placement.
void mark_title_texture() { title_texture_ = true; }
void note_shader_view() const { shader_views_.fetch_add(1, std::memory_order_relaxed); }
// Every descriptor of the texture (a D3D12 view, a slot-70 view, a T# the title wrote into a console heap) comes
// here. The first one of a title texture tells the registry, which fills it from memory only from then on.
void note_shader_view() const {
if (!shader_views_.fetch_add(1, std::memory_order_relaxed) && title_texture_ && title_address_)
device_.title_texture_named(reinterpret_cast<std::uint64_t>(title_address_));
}
std::uint32_t shader_views() const { return shader_views_.load(std::memory_order_relaxed); }
// The element a CPU copy moves (a 4x4 block for block-compressed formats), as WriteToSubresource takes it.
leonida::BlockFormat copy_element() const { return leonida::copy_block_format(binding_, VK_IMAGE_ASPECT_COLOR_BIT); }
@ -13777,6 +13805,7 @@ struct TitleTexture {
leonida::TextureReadability memory; // protected by title_texture_mutex
std::vector<DXGI_FORMAT> formats; // owned: the image's format and castable view formats
std::uint64_t hash = 0; // hash_guarded (every byte) of the bytes the image was last filled from
leonida::TilePieces pieces; // and of its part in each 64 KiB tile (the refresh's reads)
bool hashed = false;
std::uint64_t fills = 0;
};
@ -13810,9 +13839,30 @@ bool title_xg_layout(const Resource& r, rt::xg_xs::Texture& out, std::array<std:
// Whether the refresh fills the texture from memory, and so watches its pages: not a texture the GPU writes, not one
// without a layout, and not a slot-133 texture whose layout is only guessed until the title maps it.
// The watched textures over the new range stop being filled from memory.
void DeviceState::add_gpu_owned(std::uint64_t key, std::uintptr_t cpu, std::uint64_t span) {
gpu_owned[key] = {cpu, cpu + span};
std::vector<std::uint64_t> over;
texture_watch.for_each_over(cpu, cpu + span, [&](std::uint64_t k) { over.push_back(k); });
for (const std::uint64_t k : over) {
const auto it = title_textures.find(k);
if (it != title_textures.end() && over_gpu_owned(it->second->cpu, it->second->cpu + it->second->layout.layout.size))
texture_watch.untrack(k);
}
}
// Not one nothing names yet either: a texture no descriptor names is not read, uploaded or watched (the title may
// fill it over many passes before its first view); its first descriptor (title_texture_named) brings it in, and it is
// read whole and uploaded before the submission that made that descriptor. An image made for slot-70 views is named
// by the view it is made for.
void DeviceState::index_texture(const TitleTexture& t) {
const bool filled = !t.gpu_written && t.layout_known && t.resource &&
const bool filled = !t.gpu_written && t.layout_known && t.resource && (t.owned || t.resource->shader_views()) &&
!((t.layout_guessed || t.layout_from_xg) && !t.mapped && !t.stale);
if (filled && over_gpu_owned(t.cpu, t.cpu + t.layout.layout.size)) {
++gpu_owned_refusals;
texture_watch.untrack(t.address);
return;
}
if (filled)
texture_watch.track(t.address, t.cpu, t.layout.layout.size);
else
@ -13880,6 +13930,7 @@ void DeviceState::title_texture_gone(const void* resource, std::uint64_t address
if (it != title_textures.end() && it->second->resource == resource) {
it->second->resource = nullptr;
texture_watch.untrack(address);
gpu_owned.erase(address);
}
titles_gone.push_back({resource, address});
titles_gone_pending.store(true, std::memory_order_release);
@ -13894,6 +13945,7 @@ void DeviceState::title_texture_gone(const void* resource, std::uint64_t address
const auto it = title_textures.find(address);
if (it != title_textures.end() && it->second->resource == resource) {
texture_watch.untrack(address);
gpu_owned.erase(address);
it->second->resource = nullptr;
title_textures.erase(it);
}
@ -15927,9 +15979,28 @@ public:
std::memcpy(entry->desc.data(), &d, sizeof(d));
}
}
// The memory a GPU-written texture covers: what the console's GPU renders there, our GPU renders into the image
// instead. Its console footprint (XG's default layout), or its image's size if larger or unknown.
std::uint64_t gpu_span = 0;
if (entry->gpu_written) {
rt::xg_xs::ResourceDesc d{};
d.dimension = std::uint32_t(desc.Dimension);
d.width = desc.Width;
d.height = desc.Height;
d.depth_or_array = desc.DepthOrArraySize;
d.mips = desc.MipLevels;
d.format = std::uint32_t(desc.Format);
d.samples = 1;
rt::xg_xs::Texture layout{};
const char* why = nullptr;
gpu_span = std::max<std::uint64_t>(title_layout(d, layout, why) ? layout.layout.size : 0,
texture->image_backing_bytes());
}
texture->mark_title_texture();
{
std::lock_guard lock(state_.title_texture_mutex);
state_.gpu_owned.erase(address);
if (gpu_span) state_.add_gpu_owned(address, entry->cpu, gpu_span);
if (entry->gpu_written && !(address & 0xff) && texture->image_address() != address) {
std::lock_guard descriptors(state_.descriptor_mutex);
state_.gpu_placements.add(address, texture);
@ -16000,6 +16071,21 @@ public:
watch.note_changed(from, to);
}
}
if (state_.titles_named_pending.load(std::memory_order_acquire)) {
std::vector<std::uint64_t> named;
{
std::lock_guard lock(state_.titles_named_mutex);
named.swap(state_.titles_named);
state_.titles_named_pending.store(false, std::memory_order_relaxed);
}
for (const std::uint64_t address : named) {
const auto it = state_.title_textures.find(address);
if (it != state_.title_textures.end() && it->second->resource && !watch.tracked(address)) {
state_.index_texture(*it->second);
++stats.first_named;
}
}
}
title_named_.clear();
watch.collect([&](std::uint64_t key) { title_named_.push_back(key); });
// Not filled: with no backing yet or after a failed upload it is looked at again next pass. Memory not
@ -16030,9 +16116,13 @@ public:
not_filled(t);
continue;
}
if (job.whole) {
++stats.read_whole;
t.pieces = std::move(job.pieces);
}
if (job.unchanged) {
rt::d3d12::TitleImageUpload::unreserve(*job.slot);
++stats.unchanged;
++(job.whole ? stats.unchanged : stats.unchanged_parts);
t.stale = false;
continue;
}
@ -16078,7 +16168,7 @@ public:
if (t.gpu_written || !t.layout_known) continue;
// Armed before it is read: a write from now on is in the next pass. A kernel write still in progress
// over it: read once it is done (its tile stays unarmed, so the next pass names it again).
if (watch.examine(key) == leonida::TitleTextureWatch<TitleTextureCells>::Look::busy) {
if (watch.examine(key, &title_written_) == leonida::TitleTextureWatch<TitleTextureCells>::Look::busy) {
++stats.busy;
continue;
}
@ -16105,6 +16195,13 @@ public:
job.slot = slot;
job.span = t.layout.layout.size;
job.previous = t.hashed && t.fills ? std::optional<std::uint64_t>(t.hash) : std::nullopt;
// Only some of its tiles written: those parts are compared first, in place (read_title_job).
const std::size_t tiles = std::size_t(((t.cpu + job.span + 0xffff) & ~std::uint64_t(0xffff)) -
(t.cpu & ~std::uint64_t(0xffff))) >> 16;
job.pieces = t.pieces;
job.compare = job.previous && t.pieces.valid_for(t.cpu, job.span) && !title_written_.empty() &&
title_written_.size() < tiles;
if (job.compare) job.written = title_written_;
job.write_combined = title_memory_write_combined(t.cpu);
job.trace = leonida::dstorage::trace_enabled() && leonida::title_gpu_views::other_view(t.cpu);
batch_bytes += job.span;
@ -16129,16 +16226,27 @@ public:
stats.last_log = now;
const auto& w = watch.stats();
const auto& l = title_watch_logged_;
// Registered textures that would be filled from memory but no descriptor names yet: not read, not
// watched (index_texture), whatever the title writes into them.
std::size_t unnamed = 0;
for (const auto& [address, entry] : state_.title_textures)
unnamed += entry->resource && !entry->gpu_written && entry->layout_known && !entry->owned &&
!entry->resource->shader_views();
std::fprintf(stderr,
"[rt] d3d12: title texture refresh: %llu passes, %llu textures looked at (%llu named over "
"tiles not armed, %llu left for a kernel write in progress), %llu uploaded (%.1f MiB), %llu "
"unchanged; %llu written tiles, %llu arms (%llu busy, %llu refused), %llu queue overflows; %zu "
"textures over %zu tiles watched, %zu tiles not armed; %.3f ms a pass, %.3f at most\n",
"[rt] d3d12: title texture refresh: %llu passes; %llu textures written or new, %llu read whole, "
"%llu found unchanged by their written parts alone, %llu left for a kernel write in progress, "
"%llu named over tiles not armed; %llu uploaded (%.1f MiB), %llu read whole and unchanged; %llu "
"named for the first time, %zu not named by any descriptor (not read); %llu written tiles, "
"%llu arms (%llu busy, %llu refused), %llu queue overflows; %zu textures over %zu tiles "
"watched, %zu tiles not armed; %.3f ms a pass, %.3f at most\n",
static_cast<unsigned long long>(stats.passes), static_cast<unsigned long long>(stats.checked),
static_cast<unsigned long long>(w.unarmed_named - l.unarmed_named),
static_cast<unsigned long long>(stats.read_whole),
static_cast<unsigned long long>(stats.unchanged_parts),
static_cast<unsigned long long>(stats.busy),
static_cast<unsigned long long>(w.unarmed_named - l.unarmed_named),
static_cast<unsigned long long>(stats.changed), double(stats.filled_bytes) / 1048576.0,
static_cast<unsigned long long>(stats.unchanged),
static_cast<unsigned long long>(stats.first_named), unnamed,
static_cast<unsigned long long>(w.written_tiles - l.written_tiles),
static_cast<unsigned long long>(w.arms - l.arms),
static_cast<unsigned long long>(w.arm_busy - l.arm_busy),
@ -16149,6 +16257,7 @@ public:
title_watch_logged_ = w;
if (leonida::fsdraw::enabled()) log_title_census("refresh");
stats.passes = stats.checked = stats.busy = stats.changed = stats.unchanged = stats.filled_bytes = 0;
stats.read_whole = stats.unchanged_parts = stats.first_named = 0;
stats.refresh_ns = stats.max_refresh_ns = stats.max_pass_bytes = 0;
}
inside.reset();
@ -16166,6 +16275,7 @@ private:
std::uint64_t views = 0, null_views = 0, of_slot133 = 0, of_cache = 0, made = 0, retired = 0, registered = 0,
refusals = 0, written_ranges = 0, unmaps = 0, unreadable = 0, pending_views = 0;
std::uint64_t fills = 0, fill_bytes = 0, fill_failures = 0, unchanged = 0, busy = 0;
std::uint64_t read_whole = 0, unchanged_parts = 0, first_named = 0;
std::uint64_t staged_fills = 0, staged_ns = 0;
// refresh_title_textures, since the last log line
std::uint64_t passes = 0, checked = 0, hashed = 0, changed = 0, deferred = 0, filled_bytes = 0,
@ -16183,6 +16293,7 @@ private:
TitleTextureStats title_stats_; // under title_texture_mutex
bool title_pass_ = false; // in refresh_title_textures, likewise
std::vector<std::uint64_t> title_named_; // refresh scratch, under title_texture_mutex
std::vector<std::uintptr_t> title_written_; // likewise
// One texture of a refresh batch: what the serial part found (the slot reserved for it, the last fill's hash) and
// what a TitleReadPool thread read (read_title_job).
struct TitleJob {
@ -16191,6 +16302,9 @@ private:
rt::d3d12::TitleImageUpload::Slot* slot = nullptr;
std::uint64_t span = 0, hash = 0;
std::optional<std::uint64_t> previous;
leonida::TilePieces pieces; // the texture's, then (read whole) the new ones
std::vector<std::uintptr_t> written; // its tiles written since its last read (compare)
bool compare = false, whole = false;
bool write_combined = false, trace = false, copied = false, unchanged = false, nonzero = false;
std::vector<std::uint8_t> tiled;
std::vector<std::uint64_t> columns;
@ -16771,11 +16885,27 @@ private:
// last fill's, the detile into the reserved upload slot. Touches only the job's own buffers and slot.
void read_title_job(TitleJob& job) {
const TitleTexture& t = *job.entry;
job.whole = false;
// Its parts in the tiles written since its last read, hashed where they are: equal, and it is unchanged.
if (job.compare && job.pieces.unchanged(job.written, [](std::uintptr_t from, std::uint64_t bytes) {
std::uint64_t h = 0;
return hash_guarded(reinterpret_cast<const std::uint8_t*>(from), std::size_t(bytes), h, true)
? std::optional<std::uint64_t>(h) : std::nullopt;
})) {
job.copied = job.unchanged = true;
return;
}
job.whole = true;
job.tiled.resize(std::size_t(job.span));
job.copied = copy_guarded(job.tiled.data(), reinterpret_cast<const std::uint8_t*>(t.cpu), std::size_t(job.span),
job.write_combined);
if (!job.copied) return;
hash_guarded(job.tiled.data(), job.tiled.size(), job.hash, true);
job.pieces.compute(t.cpu, job.span, job.tiled.data(), [](const std::uint8_t* p, std::uint64_t bytes) {
std::uint64_t h = 0;
hash_guarded(p, std::size_t(bytes), h, true);
return h;
});
job.unchanged = job.previous && *job.previous == job.hash;
if (job.unchanged) return;
job.nonzero = false;

View File

@ -40,12 +40,59 @@
#include <cstddef>
#include <cstdint>
#include <map>
#include <optional>
#include <set>
#include <utility>
#include <vector>
namespace leonida {
// A texture's bytes in each 64 KiB tile it covers, hashed as of its last read. When some of its tiles were written
// (TitleTextureWatch::examine's `written`), only its bytes in those tiles are hashed again: equal, and the texture is
// unchanged without being read whole. A write to a neighbour in the same tile costs that texture its share of the
// tile, not a read of all of it. The faults that report writes are per tile (the geometry watch's cells reopen a
// whole tile, which the geometry copies sharing them depend on), so this is the finest the watch can tell apart.
struct TilePieces {
static constexpr std::uintptr_t kTile = 0x10000;
std::uintptr_t cpu = 0;
std::uint64_t span = 0;
std::vector<std::uint64_t> hashes; // per tile from cpu's tile on
// The texture's bytes [cpu, cpu + span) inside `tile`, as an offset from cpu and a length.
static std::pair<std::uint64_t, std::uint64_t> part(std::uintptr_t cpu, std::uint64_t span, std::uintptr_t tile) {
const std::uintptr_t from = (std::max)(cpu, tile), to = (std::min)(std::uintptr_t(cpu + span), tile + kTile);
return from < to ? std::pair<std::uint64_t, std::uint64_t>{from - cpu, to - from}
: std::pair<std::uint64_t, std::uint64_t>{0, 0};
}
bool valid_for(std::uintptr_t at, std::uint64_t bytes) const {
return cpu == at && span == bytes && !hashes.empty();
}
// From a whole copy of the texture's bytes: hash(pointer, length) of each tile's part.
template<class Hash>
void compute(std::uintptr_t at, std::uint64_t bytes, const std::uint8_t* copy, Hash&& hash) {
cpu = at;
span = bytes;
hashes.clear();
for (std::uintptr_t tile = at & ~(kTile - 1); tile < at + bytes; tile += kTile) {
const auto [offset, length] = part(at, bytes, tile);
hashes.push_back(hash(copy + offset, length));
}
}
// Whether the texture's parts in `written` hash as before: hash(title address, length) reads them where they are.
// False when a part differs, cannot be read (hash returns nullopt) or is not known.
template<class Hash>
bool unchanged(const std::vector<std::uintptr_t>& written, Hash&& hash) const {
for (const std::uintptr_t tile : written) {
const std::size_t i = (tile - (cpu & ~(kTile - 1))) / kTile;
if (tile < (cpu & ~(kTile - 1)) || i >= hashes.size()) return false;
const auto [offset, length] = part(cpu, span, tile);
const auto h = hash(cpu + offset, length);
if (!h || *h != hashes[i]) return false;
}
return true;
}
};
template<class Watch, class Key = std::uint64_t>
class TitleTextureWatch {
public:
@ -157,6 +204,16 @@ public:
for (const Key k : it->second.textures)
if (dirty_.insert(k).second) ++stats_.unarmed_named;
}
// The tiles written as of now, for examine()'s `written`: the first texture examined over a tile arms it again,
// and the others over it must still see it written.
pass_written_.clear();
for (const Key k : dirty_) {
const auto it = textures_.find(k);
if (it == textures_.end()) continue;
for (std::uintptr_t a = it->second.first; a < it->second.last; a += kTile)
if (const auto t = tiles_.find(a); t != tiles_.end() && (t->second.stale || unarmed_.count(a)))
pass_written_.insert(a);
}
named_.assign(dirty_.begin(), dirty_.end());
dirty_.clear();
stats_.named += named_.size();
@ -169,13 +226,16 @@ public:
// Before the texture's bytes are read for an upload: its tiles that changed (or were never armed) are armed
// again, so a write from now on is seen by the next collect(). Tiles not armed are tried at the next collect().
Look examine(Key key) {
// `written` (optional) gets those tiles: the only ones whose bytes can differ from the texture's last read.
Look examine(Key key, std::vector<std::uintptr_t>* written = nullptr) {
if (written) written->clear();
const auto it = textures_.find(key);
if (it == textures_.end()) return Look::unwatched;
Look look = Look::armed;
for (std::uintptr_t a = it->second.first; a < it->second.last; a += kTile) {
const auto t = tiles_.find(a);
if (t == tiles_.end()) continue;
if (written && (pass_written_.count(a) || t->second.stale || unarmed_.count(a))) written->push_back(a);
const int armed = arm(a, t->second);
if (!armed) look = Look::busy;
else if (armed < 0 && look == Look::armed) look = Look::unwatched;
@ -278,6 +338,7 @@ private:
std::map<Key, Texture> textures_;
std::set<std::uintptr_t> unarmed_;
std::set<Key> dirty_, over_;
std::set<std::uintptr_t> pass_written_; // the tiles written as the last collect() found them
std::vector<Key> named_;
std::vector<std::uintptr_t> written_, aliases_;
Stats stats_;

View File

@ -1656,6 +1656,72 @@ int check_placed_view_map_changes(ID3D12Device* device) {
return 0;
}
// A shader view of `texture`, as a draw would name it: a title texture is filled from its memory only once a
// descriptor names it (the refresh's upload at first use). `heap` holds the descriptor; the caller keeps it.
int name_texture(ID3D12Device* device, ID3D12Resource* texture, Ref<ID3D12DescriptorHeap>& heap) {
const char* who = "naming a title texture";
D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
EXPECT_HR(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&heap.p)), S_OK);
device->CreateShaderResourceView(texture, nullptr, heap->GetCPUDescriptorHandleForHeapStart());
return 0;
}
// A texture over the memory of a GPU-written texture (a render target placed at the same title pages) is not filled
// from those pages: they hold what the CPU last wrote there, not what the GPU rendered, and an upload of them (in
// 64 KiB pieces, as write-backs and neighbours' writes marked tiles written) showed blocks of older frames.
int check_texture_over_render_target(ID3D12Device* device) {
const char* who = "texture over a render target's memory";
constexpr SIZE_T bytes = 4 * 65536;
void* pages = VirtualAlloc(nullptr, bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
EXPECT(pages);
std::memset(pages, 0x11, bytes);
GpuReader gpu;
EXPECT(!gpu.init(device, 256));
D3D12_RESOURCE_DESC target{};
target.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
target.Width = 256;
target.Height = 256;
target.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
target.SampleDesc.Count = target.DepthOrArraySize = target.MipLevels = 1;
target.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
Ref<ID3D12Resource> rendered;
EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, target, 6, IID_PPV_ARGS(&rendered.p)), S_OK);
// A plain texture over the target's second 64 KiB, laid out linearly (the title's XG computer for its shape).
constexpr UINT width = 256, height = 256;
D3D12_RESOURCE_DESC desc{};
desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
desc.Width = width;
desc.Height = height;
desc.Format = DXGI_FORMAT_R8_UNORM;
desc.SampleDesc.Count = desc.DepthOrArraySize = desc.MipLevels = 1;
leonida::title_xg_layouts::note({UINT(desc.Dimension), UINT(desc.Format), height, 1, 1, width}, 1);
auto* over = static_cast<std::uint8_t*>(pages) + 65536;
Ref<ID3D12Resource> texture;
EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, over, desc, 6, IID_PPV_ARGS(&texture.p)), S_OK);
void* mapped = nullptr;
EXPECT_HR(texture->Map(0, nullptr, &mapped), S_OK);
Ref<ID3D12DescriptorHeap> named;
EXPECT(!name_texture(device, texture.p, named));
ID3D12CommandList* lists[]{gpu.list.p};
std::array<std::uint8_t, width * height> pixels{};
for (unsigned pass = 1; pass <= 2; ++pass) {
std::memset(over, 0x40 + pass, 65536); // a CPU write (a write-back of a GPU copy, say) of one 64 KiB piece
gpu.queue->ExecuteCommandLists(1, lists);
EXPECT_HR(gpu.queue->Signal(gpu.fence.p, pass), S_OK);
EXPECT_HR(gpu.fence->SetEventOnCompletion(pass, gpu.done), S_OK);
EXPECT(WaitForSingleObject(gpu.done, 30000) == WAIT_OBJECT_0);
EXPECT_HR(texture->ReadFromSubresource(pixels.data(), width, UINT(pixels.size()), 0, nullptr), S_OK);
EXPECT(std::none_of(pixels.begin(), pixels.end(), [&](auto value) { return value == 0x40 + pass; }));
}
texture->Unmap(0, nullptr);
texture.p->Release();
texture.p = nullptr;
rendered.p->Release();
rendered.p = nullptr;
VirtualFree(pages, 0, MEM_RELEASE);
return 0;
}
// A persistent Map can become readable without notifying xmem or calling Map a second time.
int check_mapped_plane(ID3D12Device* device) {
const char* who = "persistent slot-133 plane";
@ -1675,6 +1741,8 @@ int check_mapped_plane(ID3D12Device* device) {
EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, desc, 6, IID_PPV_ARGS(&texture.p)), S_OK);
void* mapped = nullptr;
EXPECT_HR(texture->Map(0, nullptr, &mapped), S_OK);
Ref<ID3D12DescriptorHeap> named; // filled from memory once a descriptor names it
EXPECT(!name_texture(device, texture.p, named));
EXPECT(mapped == pages);
ID3D12CommandList* lists[]{gpu.list.p};
std::array<std::uint8_t, size> pixels{};
@ -1721,6 +1789,8 @@ int check_mapped_plane_first_use(ID3D12Device* device) {
EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, desc, 6, IID_PPV_ARGS(&texture.p)), S_OK);
void* mapped = nullptr;
EXPECT_HR(texture->Map(0, nullptr, &mapped), S_OK);
Ref<ID3D12DescriptorHeap> named; // filled from memory once a descriptor names it
EXPECT(!name_texture(device, texture.p, named));
EXPECT(mapped == pages);
EXPECT_HR(gpu.allocator->Reset(), S_OK);
EXPECT_HR(gpu.list->Reset(gpu.allocator.p, nullptr), S_OK);
@ -2295,6 +2365,8 @@ int check_mapped_bc_array(ID3D12Device* device) {
EXPECT_HR(rt::d3d12::create_buffer_at_host_address(device, pages, desc, 6, IID_PPV_ARGS(&texture.p)), S_OK);
void* mapped = nullptr;
EXPECT_HR(texture->Map(0, nullptr, &mapped), S_OK);
Ref<ID3D12DescriptorHeap> named; // filled from memory once a descriptor names it
EXPECT(!name_texture(device, texture.p, named));
EXPECT(mapped == pages);
std::vector<std::uint64_t> columns;
std::vector<std::vector<std::uint8_t>> patterns(x.mips * x.depth_or_array);
@ -2387,6 +2459,7 @@ int main(int argc, char** argv) {
failures += check_placed_view_map_changes(ours.p);
failures += check_shared_bc_placements(ours.p);
failures += check_mapped_plane(ours.p);
failures += check_texture_over_render_target(ours.p);
failures += check_mapped_bc_array(ours.p);
}
{

View File

@ -194,6 +194,59 @@ int main() {
pass();
EXPECT(looks.size() == 1 && looks[0] == Watch::Look::unwatched);
}
{ // two textures in one tile, one written: both are named (the fault is per tile), exactly one is read whole
FakeWatch f;
Watch w(f);
static std::uint8_t memory[3 * kTile];
const auto at = [&](std::uintptr_t a) { return memory + (a - base); };
const std::uintptr_t a = base + 0x1000, b = base + 0x9000; // 16 KiB each, in tile 0
const std::uintptr_t c = base + kTile - 0x2000; // 72 KiB, tiles 0 and 1
const std::uint64_t sa = 0x4000, sb = 0x4000, sc = 0x12000;
for (std::size_t i = 0; i < sizeof memory; ++i) memory[i] = std::uint8_t(i * 7 + 1);
const auto hash = [](const std::uint8_t* p, std::uint64_t n) {
std::uint64_t h = 1469598103934665603ull;
for (std::uint64_t i = 0; i < n; ++i) h = (h ^ p[i]) * 1099511628211ull;
return h;
};
std::map<std::uint64_t, leonida::TilePieces> pieces;
const std::map<std::uint64_t, std::pair<std::uintptr_t, std::uint64_t>> range{
{1, {a, sa}}, {2, {b, sb}}, {3, {c, sc}}};
for (const auto& [k, r] : range) w.track(k, r.first, r.second);
unsigned whole = 0;
std::uint64_t compared = 0;
std::vector<std::uintptr_t> written;
const auto pass = [&] {
std::vector<std::uint64_t> read;
w.collect([&](std::uint64_t k) {
w.examine(k, &written);
const auto [cpu, span] = range.at(k);
auto& p = pieces[k];
if (p.valid_for(cpu, span) &&
p.unchanged(written, [&](std::uintptr_t from, std::uint64_t n) -> std::optional<std::uint64_t> {
compared += n;
return hash(at(from), n);
}))
return;
++whole;
read.push_back(k);
p.compute(cpu, span, at(cpu), hash);
});
return read;
};
EXPECT((pass() == V{1, 2, 3})); // new: read whole
whole = 0;
compared = 0;
memory[b - base + 5] ^= 0xff; // texture 2 written; the fault names tile 0 (textures 1, 2 and 3)
f.write(b + 5);
EXPECT((pass() == V{2}));
EXPECT(whole == 1);
EXPECT(compared == sa + sb + 0x2000); // 1 and 3 hashed only their parts of tile 0
whole = 0;
memory[c - base + 0x3000] ^= 0xff; // texture 3 written in tile 1: only 3 is named, and read
f.write(c + 0x3000);
EXPECT((pass() == V{3}));
EXPECT(whole == 1);
}
{ // a lost queue names every texture once; untrack releases the tiles
FakeWatch f;
Watch w(f);