Repository navigation
Conversation
kacper-serewis
left a comment
There was a problem hiding this comment.
Independent Windows RTX 5070 (616.56) buffer-copy readback on parent 98d977a and head 8e1fae6. ExecuteCommandLists submitted 9, 2, then 11 copies interleaved with resource barriers. Each submission changed the source pattern, offsets and destination sentinel. All 1,536 destination bytes per revision matched after the corresponding fences, including 1,272 sentinel bytes. The CPU replay self-test also passed on both. Each buffer copy had one region. GPU evidence. AI-assisted.
8e1fae6 to
2ce4ff2
Compare
|
Rebased onto Two merges on
Tested at 19353e1 (the head of the chain, which carries this commit):
AI-assisted (Claude Code). |
Copies, resolves and atomic copies keep their source, regions, depth24/rgb96 conversion and staging in detail::PreparedCopy, in PreparedList::copies, indexed by Prepared::copy, the way ItsNotRexed#2123 moved the draws' data. An ExecuteIndirect's count buffer uses the same slot. Each PreparedCopy is kept between executions and reset() keeps its vectors' storage. detail::Prepared: 872 -> 392 bytes (MSVC 14.44, measured with sizeof). replay_cpu_bench --self-test checks that a buffer copy prepared where a texture upload was before keeps none of its regions. AI-assisted (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2ce4ff2 to
0c1f606
Compare
|
Rebased onto Tested 2446c4a (#2969 on top of this) on RTX 5070, driver 621.01, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363,
AI-assisted (Claude Code). |
|
Tested 0c1f606 on AMD Radeon RX 6800 (RDNA2), AMD 26.8.1 (Windows 32.0.21045.5002), Windows 11 Pro 25H2 (build 26200): pass. The runs so far were NVIDIA; this is the AMD side.
Built with AI-assisted (Claude Code): it built both commits and ran the tests. |
|
Tested 0c1f606 on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran AI-assisted. |
|
Independent GPU readback check of the prepared-copy pool reuse (BUILD-851, claim): the head AMD Radeon RX 9060 XT, driver 26.10.44, Windows 11, MSVC 14.44 (Visual Studio 2022), Ninja, RelWithDebInfo. Synthetic data, no game files. How. One program (below, not for the repository), built unchanged in a worktree of each commit, drives the layer through The submissions are sized to make a pool entry serve a different copy than before:
In all, 49,320 recorded commands: 8,265 buffer copies, 5,530 buffer to texture, 4,508 texture to texture, 4,544 texture to buffer, 1,503 + 1,540 Results (each run twice, same result both times):
So this PR changes nothing that the readback can see, in either mode. The 734,195 bytes are not from this PR. They are the same on the parent, and on current The check can fail. With Not covered: resolves, AI-assisted (Claude Code). The program (copy_pool_check_851.cpp)Environment: // BUILD-851: an independent GPU readback check of #2928 (a copy's prepared data lives in a pool that is kept between
// executions). Not part of the repository: built from scratch worktrees of #2928's head and of its parent.
// It drives the D3D12 layer with command lists whose number and mix of copies grows, shrinks and jumps, so a pool
// entry is reused by a copy of another kind with fewer or more regions, and after every ExecuteCommandLists it reads
// every buffer and texture back and compares them with a CPU model of the same copies.
// Synthetic data only. Prints the mismatch count and a digest of everything read back, so two builds can be compared.
#include "../shims/d3d12_com.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <random>
#include <string>
#include <vector>
namespace {
constexpr UINT64 buf_bytes = 16384;
constexpr UINT64 upload_bytes = 65536;
constexpr UINT tex_size = 64, tex_mips = 2, tex_layers = 2, tex_subs = tex_mips * tex_layers;
constexpr DXGI_FORMAT tex_format = DXGI_FORMAT_R8G8B8A8_UNORM;
constexpr UINT default_buffers = 4, textures = 2;
struct Buf {
ID3D12Resource* r = nullptr;
std::vector<std::uint8_t> m;
D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_COPY_DEST;
bool fixed = false; // an upload or readback buffer: its state never changes
bool written = false; // written since it entered COPY_DEST
};
struct Tex {
ID3D12Resource* r = nullptr;
std::vector<std::uint8_t> sub[tex_subs];
D3D12_RESOURCE_STATES state = D3D12_RESOURCE_STATE_COPY_DEST;
bool written = false;
};
UINT sub_width(UINT sub) { return tex_size >> (sub % tex_mips); }
UINT pitch_of(UINT width) { return (width * 4 + 255) & ~255u; }
struct Check {
ID3D12Device* dev = nullptr;
ID3D12CommandQueue* queue = nullptr;
ID3D12Fence* fence = nullptr;
ID3D12CommandAllocator* alloc[4]{};
ID3D12GraphicsCommandList* list[4]{};
HANDLE event = nullptr;
UINT64 fence_value = 0;
Buf upload, readback, bufs[default_buffers];
Tex tex[textures];
std::mt19937 rng{851};
std::uint64_t digest = 1469598103934665603ull, mismatched_bytes = 0, compared_bytes = 0;
std::uint64_t submissions = 0, commands = 0, barriers = 0;
std::uint64_t where_bad[default_buffers + textures * tex_subs]{}; // mismatched bytes per buffer and subresource
std::uint64_t kinds[6]{}; // buffer, upload, texture-texture, download, CopyResource buffer, CopyResource texture
bool failed = false;
bool sync = std::getenv("CHECK851_SYNC") != nullptr;
std::uint64_t forced = 0; // barriers forced between two writes (CHECK851_SYNC)
UINT rnd(UINT n) { return n ? static_cast<UINT>(rng() % n) : 0; }
bool ok(HRESULT hr, const char* what) {
if (SUCCEEDED(hr)) return true;
std::fprintf(stderr, "FAIL: %s (hr=0x%08X)\n", what, static_cast<unsigned>(hr));
failed = true;
return false;
}
bool buffer(Buf& b, D3D12_HEAP_TYPE heap, UINT64 bytes, D3D12_RESOURCE_STATES state) {
D3D12_HEAP_PROPERTIES hp{heap};
D3D12_RESOURCE_DESC d{};
d.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
d.Width = bytes;
d.Height = 1;
d.DepthOrArraySize = 1;
d.MipLevels = 1;
d.SampleDesc = {1, 0};
d.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
b.m.assign(bytes, 0);
b.state = state;
b.fixed = heap != D3D12_HEAP_TYPE_DEFAULT;
return ok(dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, state, nullptr, IID_PPV_ARGS(&b.r)),
"CreateCommittedResource(buffer)");
}
bool create() {
if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&dev))) || !dev) {
std::printf("SKIP: no Vulkan device\n");
return false;
}
D3D12_COMMAND_QUEUE_DESC qd{};
if (!ok(dev->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue)), "CreateCommandQueue")) return false;
if (!ok(dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)), "CreateFence")) return false;
event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
for (int i = 0; i < 4; ++i) {
if (!ok(dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc[i])), "allocator"))
return false;
if (!ok(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc[i], nullptr, IID_PPV_ARGS(&list[i])),
"CreateCommandList"))
return false;
list[i]->Close();
}
if (!buffer(upload, D3D12_HEAP_TYPE_UPLOAD, upload_bytes, D3D12_RESOURCE_STATE_GENERIC_READ)) return false;
const UINT64 readback_size = default_buffers * buf_bytes + textures * tex_subs * UINT64(pitch_of(tex_size)) * tex_size;
if (!buffer(readback, D3D12_HEAP_TYPE_READBACK, readback_size, D3D12_RESOURCE_STATE_COPY_DEST)) return false;
for (Buf& b : bufs)
if (!buffer(b, D3D12_HEAP_TYPE_DEFAULT, buf_bytes, D3D12_RESOURCE_STATE_COPY_DEST)) return false;
std::uint8_t* p = nullptr;
if (!ok(upload.r->Map(0, nullptr, reinterpret_cast<void**>(&p)), "Map(upload)")) return false;
for (UINT64 i = 0; i < upload_bytes; ++i)
upload.m[i] = p[i] = static_cast<std::uint8_t>((i * 2654435761ull >> 11) ^ (i >> 3) ^ 0x5a);
upload.r->Unmap(0, nullptr);
for (Tex& t : tex) {
D3D12_HEAP_PROPERTIES hp{D3D12_HEAP_TYPE_DEFAULT};
D3D12_RESOURCE_DESC d{};
d.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
d.Width = tex_size;
d.Height = tex_size;
d.DepthOrArraySize = tex_layers;
d.MipLevels = tex_mips;
d.Format = tex_format;
d.SampleDesc = {1, 0};
if (!ok(dev->CreateCommittedResource(&hp, D3D12_HEAP_FLAG_NONE, &d, D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
IID_PPV_ARGS(&t.r)), "CreateCommittedResource(texture)"))
return false;
for (UINT s = 0; s < tex_subs; ++s) t.sub[s].assign(sub_width(s) * sub_width(s) * 4, 0);
}
return true;
}
std::vector<std::string> log; // the copies of the current submission, for the first-mismatch report
const char* name(const Buf& b) {
static const char* const names[] = {"buffer0", "buffer1", "buffer2", "buffer3"};
return &b == &upload ? "upload" : names[&b - bufs];
}
template<class... A>
void note(const char* format, A... a) {
char line[160];
std::snprintf(line, sizeof(line), format, a...);
log.push_back(line);
}
// ---- recording: every call records the D3D12 command and applies it to the model ----
void transition(ID3D12GraphicsCommandList* c, ID3D12Resource* r, D3D12_RESOURCE_STATES& now, D3D12_RESOURCE_STATES to) {
if (now == to) return;
D3D12_RESOURCE_BARRIER b{};
b.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
b.Transition = {r, D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, now, to};
c->ResourceBarrier(1, &b);
now = to;
++barriers;
++commands;
}
void want(ID3D12GraphicsCommandList* c, Buf& b, D3D12_RESOURCE_STATES to) {
if (!b.fixed) transition(c, b.r, b.state, to);
}
void want(ID3D12GraphicsCommandList* c, Tex& t, D3D12_RESOURCE_STATES to) { transition(c, t.r, t.state, to); }
// D3D12 orders two writes to one resource without a barrier between them. With CHECK851_SYNC set, a resource that
// was already written in its current state goes to COPY_SOURCE and back first, so the layer emits a barrier there.
template<class R>
void before_write(ID3D12GraphicsCommandList* c, R& r) {
const D3D12_RESOURCE_STATES was = r.state;
want(c, r, D3D12_RESOURCE_STATE_COPY_DEST);
if (was != D3D12_RESOURCE_STATE_COPY_DEST) r.written = false;
if (sync && r.written) {
want(c, r, D3D12_RESOURCE_STATE_COPY_SOURCE);
want(c, r, D3D12_RESOURCE_STATE_COPY_DEST);
++forced;
}
r.written = true;
}
void copy_buffer(ID3D12GraphicsCommandList* c, Buf& dst, UINT64 doff, Buf& src, UINT64 soff, UINT64 n) {
want(c, src, D3D12_RESOURCE_STATE_COPY_SOURCE);
before_write(c, dst);
c->CopyBufferRegion(dst.r, doff, src.r, soff, n);
note("buffer %s+%llu <- %s+%llu, %llu bytes", name(dst), doff, name(src), soff, n);
std::memcpy(dst.m.data() + doff, src.m.data() + soff, n);
++kinds[0], ++commands;
}
void upload_texture(ID3D12GraphicsCommandList* c, Tex& dst, UINT sub, UINT x, UINT y, Buf& src, UINT64 off, UINT w,
UINT h) {
want(c, src, D3D12_RESOURCE_STATE_COPY_SOURCE);
before_write(c, dst);
const UINT pitch = pitch_of(w);
D3D12_TEXTURE_COPY_LOCATION s{src.r, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}};
s.PlacedFootprint = {off, {tex_format, w, h, 1, pitch}};
D3D12_TEXTURE_COPY_LOCATION d{dst.r, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}};
d.SubresourceIndex = sub;
c->CopyTextureRegion(&d, x, y, 0, &s, nullptr);
note("upload tex%d.%u (%u,%u) <- %s+%llu, %ux%u pitch %u", int(&dst - tex), sub, x, y, name(src), off, w, h, pitch);
const UINT width = sub_width(sub);
for (UINT row = 0; row < h; ++row)
std::memcpy(dst.sub[sub].data() + ((y + row) * width + x) * 4, src.m.data() + off + UINT64(row) * pitch, w * 4);
++kinds[1], ++commands;
}
void copy_texture(ID3D12GraphicsCommandList* c, Tex& dst, UINT dsub, UINT dx, UINT dy, Tex& src, UINT ssub, UINT sx,
UINT sy, UINT w, UINT h) {
want(c, src, D3D12_RESOURCE_STATE_COPY_SOURCE);
before_write(c, dst);
D3D12_TEXTURE_COPY_LOCATION s{src.r, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}};
s.SubresourceIndex = ssub;
D3D12_TEXTURE_COPY_LOCATION d{dst.r, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}};
d.SubresourceIndex = dsub;
const D3D12_BOX box{sx, sy, 0, sx + w, sy + h, 1};
c->CopyTextureRegion(&d, dx, dy, 0, &s, &box);
note("texture tex%d.%u (%u,%u) <- tex%d.%u (%u,%u), %ux%u", int(&dst - tex), dsub, dx, dy, int(&src - tex), ssub, sx,
sy, w, h);
const UINT dw = sub_width(dsub), sw = sub_width(ssub);
for (UINT row = 0; row < h; ++row)
std::memcpy(dst.sub[dsub].data() + ((dy + row) * dw + dx) * 4, src.sub[ssub].data() + ((sy + row) * sw + sx) * 4,
w * 4);
++kinds[2], ++commands;
}
void download_texture(ID3D12GraphicsCommandList* c, Buf& dst, UINT64 off, Tex& src, UINT sub, UINT x, UINT y, UINT w,
UINT h) {
want(c, src, D3D12_RESOURCE_STATE_COPY_SOURCE);
before_write(c, dst);
const UINT pitch = pitch_of(w);
D3D12_TEXTURE_COPY_LOCATION d{dst.r, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}};
d.PlacedFootprint = {off, {tex_format, w, h, 1, pitch}};
D3D12_TEXTURE_COPY_LOCATION s{src.r, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}};
s.SubresourceIndex = sub;
const D3D12_BOX box{x, y, 0, x + w, y + h, 1};
c->CopyTextureRegion(&d, 0, 0, 0, &s, &box);
note("download %s+%llu <- tex%d.%u (%u,%u), %ux%u pitch %u", name(dst), off, int(&src - tex), sub, x, y, w, h, pitch);
const UINT width = sub_width(sub);
for (UINT row = 0; row < h; ++row)
std::memcpy(dst.m.data() + off + UINT64(row) * pitch, src.sub[sub].data() + ((y + row) * width + x) * 4, w * 4);
++kinds[3], ++commands;
}
void copy_resource(ID3D12GraphicsCommandList* c, Buf& dst, Buf& src) {
want(c, src, D3D12_RESOURCE_STATE_COPY_SOURCE);
before_write(c, dst);
c->CopyResource(dst.r, src.r);
note("CopyResource %s <- %s", name(dst), name(src));
dst.m = src.m;
++kinds[4], ++commands;
}
void copy_resource(ID3D12GraphicsCommandList* c, Tex& dst, Tex& src) {
want(c, src, D3D12_RESOURCE_STATE_COPY_SOURCE);
before_write(c, dst);
c->CopyResource(dst.r, src.r);
note("CopyResource tex%d <- tex%d", int(&dst - tex), int(&src - tex));
for (UINT s = 0; s < tex_subs; ++s) dst.sub[s] = src.sub[s];
++kinds[5], ++commands;
}
// One random copy. `kind` < 0 picks the kind at random.
void random_copy(ID3D12GraphicsCommandList* c, int kind = -1) {
if (kind < 0) {
const UINT roll = rnd(100);
kind = roll < 34 ? 0 : roll < 56 ? 1 : roll < 74 ? 2 : roll < 92 ? 3 : roll < 96 ? 4 : 5;
}
const UINT a = rnd(default_buffers), b = (a + 1 + rnd(default_buffers - 1)) % default_buffers;
const UINT ta = rnd(textures), tb = 1 - ta;
if (kind == 0) {
const UINT64 n = 1 + rnd(4096);
if (rnd(3) == 0) copy_buffer(c, bufs[a], rnd(UINT(buf_bytes - n + 1)), upload, rnd(UINT(upload_bytes - n + 1)), n);
else copy_buffer(c, bufs[a], rnd(UINT(buf_bytes - n + 1)), bufs[b], rnd(UINT(buf_bytes - n + 1)), n);
} else if (kind == 1) {
const UINT sub = rnd(tex_subs), width = sub_width(sub);
const UINT w = 1 + rnd(width), h = 1 + rnd(width), x = rnd(width - w + 1), y = rnd(width - h + 1);
const UINT64 need = UINT64(pitch_of(w)) * (h - 1) + w * 4;
Buf& src = rnd(2) ? upload : bufs[a];
const UINT64 room = src.m.size() - need;
upload_texture(c, tex[ta], sub, x, y, src, UINT64(rnd(UINT(room / 512 + 1))) * 512, w, h);
} else if (kind == 2) {
const UINT ssub = rnd(tex_subs), dsub = rnd(tex_subs);
const UINT limit = sub_width(ssub) < sub_width(dsub) ? sub_width(ssub) : sub_width(dsub);
const UINT w = 1 + rnd(limit), h = 1 + rnd(limit);
copy_texture(c, tex[ta], dsub, rnd(sub_width(dsub) - w + 1), rnd(sub_width(dsub) - h + 1), tex[tb], ssub,
rnd(sub_width(ssub) - w + 1), rnd(sub_width(ssub) - h + 1), w, h);
} else if (kind == 3) {
const UINT sub = rnd(tex_subs), width = sub_width(sub);
const UINT w = 1 + rnd(width), h = 1 + rnd(width), x = rnd(width - w + 1), y = rnd(width - h + 1);
const UINT64 need = UINT64(pitch_of(w)) * (h - 1) + w * 4;
download_texture(c, bufs[a], UINT64(rnd(UINT((buf_bytes - need) / 512 + 1))) * 512, tex[ta], sub, x, y, w, h);
} else if (kind == 4) {
copy_resource(c, bufs[a], bufs[b]);
} else {
copy_resource(c, tex[ta], tex[tb]);
}
}
bool execute(UINT count, UINT first = 0) {
ID3D12CommandList* lists[4];
for (UINT i = 0; i < count; ++i) {
if (!ok(list[first + i]->Close(), "Close")) return false;
lists[i] = list[first + i];
}
queue->ExecuteCommandLists(count, lists);
if (!ok(queue->Signal(fence, ++fence_value), "Signal")) return false;
if (fence->GetCompletedValue() < fence_value) {
fence->SetEventOnCompletion(fence_value, event);
if (WaitForSingleObject(event, 60000) != WAIT_OBJECT_0) return ok(E_FAIL, "fence wait timed out");
}
return true;
}
bool begin(UINT i) {
return ok(alloc[i]->Reset(), "allocator Reset") && ok(list[i]->Reset(alloc[i], nullptr), "list Reset");
}
// Reads every buffer and every texture subresource back (itself a list of copies) and compares with the model.
bool verify(const char* where) {
ID3D12GraphicsCommandList* c = list[3];
if (!begin(3)) return false;
for (UINT k = 0; k < default_buffers; ++k) {
want(c, bufs[k], D3D12_RESOURCE_STATE_COPY_SOURCE);
c->CopyBufferRegion(readback.r, k * buf_bytes, bufs[k].r, 0, buf_bytes);
}
const UINT64 tex_base = default_buffers * buf_bytes, slot = UINT64(pitch_of(tex_size)) * tex_size;
for (UINT t = 0; t < textures; ++t) {
want(c, tex[t], D3D12_RESOURCE_STATE_COPY_SOURCE);
for (UINT s = 0; s < tex_subs; ++s) {
D3D12_TEXTURE_COPY_LOCATION d{readback.r, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT, {}};
d.PlacedFootprint = {tex_base + (t * tex_subs + s) * slot,
{tex_format, sub_width(s), sub_width(s), 1, pitch_of(sub_width(s))}};
D3D12_TEXTURE_COPY_LOCATION src{tex[t].r, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX, {}};
src.SubresourceIndex = s;
c->CopyTextureRegion(&d, 0, 0, 0, &src, nullptr);
}
}
if (!execute(1, 3)) return false;
const std::uint8_t* p = nullptr;
if (!ok(readback.r->Map(0, nullptr, reinterpret_cast<void**>(const_cast<std::uint8_t**>(&p))), "Map(readback)"))
return false;
std::uint64_t bad = 0;
const auto compare = [&](const std::uint8_t* got, const std::uint8_t* want_bytes, std::size_t n) {
for (std::size_t i = 0; i < n; ++i) {
digest = (digest ^ got[i]) * 1099511628211ull;
bad += got[i] != want_bytes[i];
}
compared_bytes += n;
};
for (UINT k = 0; k < default_buffers; ++k) {
const std::uint64_t before = bad;
compare(p + k * buf_bytes, bufs[k].m.data(), buf_bytes);
where_bad[k] += bad - before;
}
for (UINT t = 0; t < textures; ++t)
for (UINT s = 0; s < tex_subs; ++s) {
const UINT width = sub_width(s);
const std::uint64_t before = bad;
for (UINT row = 0; row < width; ++row)
compare(p + tex_base + (t * tex_subs + s) * slot + UINT64(row) * pitch_of(width),
tex[t].sub[s].data() + row * width * 4, width * 4);
where_bad[default_buffers + t * tex_subs + s] += bad - before;
}
if (bad && std::getenv("CHECK851_VERBOSE"))
std::printf(" submission %llu (%s): %llu bytes differ\n", static_cast<unsigned long long>(submissions), where,
static_cast<unsigned long long>(bad));
if (bad && std::getenv("CHECK851_RESYNC")) { // diagnosis only: take what the GPU holds as the model
for (UINT k = 0; k < default_buffers; ++k) std::memcpy(bufs[k].m.data(), p + k * buf_bytes, buf_bytes);
for (UINT t = 0; t < textures; ++t)
for (UINT s = 0; s < tex_subs; ++s)
for (UINT row = 0; row < sub_width(s); ++row)
std::memcpy(tex[t].sub[s].data() + row * sub_width(s) * 4,
p + tex_base + (t * tex_subs + s) * slot + UINT64(row) * pitch_of(sub_width(s)),
sub_width(s) * 4);
}
readback.r->Unmap(0, nullptr);
if (bad && mismatched_bytes == 0 && std::getenv("CHECK851_LOG")) {
for (UINT k = 0; k < default_buffers; ++k) {
UINT64 first = buf_bytes, last = 0, n = 0;
for (UINT64 i = 0; i < buf_bytes; ++i)
if (p[k * buf_bytes + i] != bufs[k].m[i]) {
if (first == buf_bytes) first = i;
last = i;
++n;
}
if (n) std::printf(" buffer%u differs in %llu bytes, from %llu to %llu\n", k, static_cast<unsigned long long>(n),
static_cast<unsigned long long>(first), static_cast<unsigned long long>(last));
}
for (UINT t = 0; t < textures; ++t)
for (UINT s = 0; s < tex_subs; ++s) {
const UINT width = sub_width(s);
UINT x0 = width, y0 = width, x1 = 0, y1 = 0, n = 0;
for (UINT row = 0; row < width; ++row)
for (UINT col = 0; col < width; ++col)
if (std::memcmp(p + tex_base + (t * tex_subs + s) * slot + UINT64(row) * pitch_of(width) + col * 4,
tex[t].sub[s].data() + (row * width + col) * 4, 4)) {
x0 = col < x0 ? col : x0, y0 = row < y0 ? row : y0;
x1 = col > x1 ? col : x1, y1 = row > y1 ? row : y1;
++n;
}
if (n) std::printf(" tex%u.%u differs in %u texels, inside (%u,%u)-(%u,%u)\n", t, s, n, x0, y0, x1, y1);
}
for (std::size_t i = 0; i < log.size(); ++i) std::printf(" %3zu %s\n", i, log[i].c_str());
}
if (bad && mismatched_bytes == 0) std::printf("first mismatch: %s, submission %llu, %llu bytes differ\n", where,
static_cast<unsigned long long>(submissions),
static_cast<unsigned long long>(bad));
mismatched_bytes += bad;
return true;
}
// One ExecuteCommandLists of `lists` lists holding `copies` copies between them, then the readback.
bool submit(UINT copies, UINT lists, const char* where, int kind = -1) {
// Lists run in the order given, so each list is recorded whole before the next: the model then applies the
// copies in the order the queue executes them.
UINT left = copies;
log.clear();
for (UINT i = 0; i < lists; ++i) {
if (!begin(i)) return false;
if (i) note("-- next list");
const UINT share = i + 1 == lists ? left : rnd(left + 1);
for (UINT n = 0; n < share; ++n) random_copy(list[i], kind);
left -= share;
}
if (!execute(lists)) return false;
++submissions;
return verify(where);
}
// CHECK851_PAIRS: two copies that write the same bytes or texels, in one list, with nothing between them.
// D3D12 runs them in order, so the second one's data must be what is read back.
bool pairs() {
struct Case { const char* name; int first, second; };
const Case cases[] = {
{"texture-to-buffer, then buffer copy over it", 3, 0}, {"buffer copy, then texture-to-buffer over it", 0, 3},
{"buffer copy, then buffer copy over it", 0, 0}, {"texture-to-buffer twice", 3, 3},
{"buffer-to-texture, then texture copy over it", 1, 2}, {"texture copy, then buffer-to-texture over it", 2, 1},
{"buffer-to-texture twice", 1, 1}, {"texture copy twice", 2, 2},
};
const auto write = [&](ID3D12GraphicsCommandList* c, int kind, UINT round) {
const UINT v = round * 7; // different source data each round
if (kind == 0) copy_buffer(c, bufs[0], 4096, upload, 1024 + v * 16, 8192);
else if (kind == 3) download_texture(c, bufs[0], 4096, tex[1], 0, v % 8, v % 5, 48, 30);
else if (kind == 1) upload_texture(c, tex[0], 0, 4, 4, upload, 2048 + UINT64(v % 16) * 512, 56, 56);
else copy_texture(c, tex[0], 0, 4, 4, tex[1], 0, v % 8, v % 8, 56, 56);
};
for (const Case& k : cases) {
UINT wrong = 0;
const UINT rounds = 50;
for (UINT round = 0; round < rounds; ++round) {
// A separate submission gives bufs[0], tex[0] and tex[1] new contents first.
log.clear();
if (!begin(0)) return false;
copy_buffer(list[0], bufs[0], 0, upload, 8192 + round * 64, buf_bytes);
upload_texture(list[0], tex[0], 0, 0, 0, upload, 16384 + UINT64(round % 32) * 512, tex_size, tex_size);
upload_texture(list[0], tex[1], 0, 0, 0, upload, 32768 + UINT64(round % 32) * 512, tex_size, tex_size);
if (!execute(1)) return false;
++submissions;
const std::uint64_t before = mismatched_bytes;
if (!verify("pair setup")) return false;
if (!begin(0)) return false;
write(list[0], k.first, round);
write(list[0], k.second, round + 3);
if (!execute(1)) return false;
++submissions;
if (!verify(k.name)) return false;
wrong += mismatched_bytes != before;
// Bring the model back to what the GPU holds, so one wrong round does not count again.
if (mismatched_bytes != before && !begin(0)) return false;
if (mismatched_bytes != before) {
copy_buffer(list[0], bufs[0], 0, upload, 0, buf_bytes);
upload_texture(list[0], tex[0], 0, 0, 0, upload, 0, tex_size, tex_size);
if (!execute(1)) return false;
}
}
std::printf("%-46s wrong in %2u of %u rounds\n", k.name, wrong, rounds);
}
return true;
}
bool run() {
// Defined contents first: every buffer and every texture subresource is written whole.
if (!begin(0)) return false;
for (UINT k = 0; k < default_buffers; ++k) copy_buffer(list[0], bufs[k], 0, upload, k * 4096, buf_bytes);
for (UINT t = 0; t < textures; ++t)
for (UINT s = 0; s < tex_subs; ++s)
upload_texture(list[0], tex[t], s, 0, 0, upload, (t * tex_subs + s) * 2048ull, sub_width(s), sub_width(s));
if (!execute(1)) return false;
++submissions;
if (!verify("initial contents")) return false;
if (std::getenv("CHECK851_PAIRS")) return pairs();
const UINT steps[] = {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144};
const UINT count = sizeof(steps) / sizeof(steps[0]);
struct Phase { const char* name; std::uint64_t submissions, mismatches; };
std::vector<Phase> phases;
std::uint64_t s0 = submissions, m0 = mismatched_bytes;
const auto phase = [&](const char* name) {
phases.push_back({name, submissions - s0, mismatched_bytes - m0});
s0 = submissions, m0 = mismatched_bytes;
};
for (UINT round = 0; round < 3; ++round)
for (UINT i = 0; i < count; ++i)
if (!submit(steps[i], 1 + round, "expanding")) return false;
phase("expanding (1 to 144 copies)");
for (UINT round = 0; round < 3; ++round)
for (UINT i = count; i-- > 0;)
if (!submit(steps[i], 1 + round, "shrinking")) return false;
phase("shrinking (144 to 1 copies)");
for (UINT round = 0; round < 4; ++round)
for (UINT i = 0; i < count; ++i)
if (!submit(i % 2 ? steps[i / 2] : steps[count - 1 - i / 2], 1 + round % 3, "alternating")) return false;
phase("alternating large and small");
static const char* const kind_names[] = {"buffer", "buffer-to-texture", "texture-to-texture", "texture-to-buffer",
"CopyResource buffer", "CopyResource texture"};
for (int kind = 0; kind < 6; ++kind) // a pool of one kind reused by every other kind, both orders
for (int next = 0; next < 6; ++next) {
const std::uint64_t before = mismatched_bytes;
if (!submit(40, 1, kind_names[kind], kind)) return false;
if (!submit(7, 1, kind_names[next], next)) return false;
if (!submit(60, 2, kind_names[next], next)) return false;
if (mismatched_bytes != before && std::getenv("CHECK851_VERBOSE"))
std::printf(" pair %s then %s: %llu bytes differ\n", kind_names[kind], kind_names[next],
static_cast<unsigned long long>(mismatched_bytes - before));
}
phase("one kind after another");
for (UINT i = 0; i < 6; ++i)
if (!submit(0, 1 + i % 3, "empty lists") || !submit(100, 1 + i % 3, "after empty lists")) return false;
phase("empty lists between large ones");
for (UINT i = 0; i < 300; ++i)
if (!submit(rnd(120), 1 + rnd(3), "random")) return false;
phase("random sizes (0 to 119 copies)");
for (const Phase& p : phases)
std::printf("%-34s %4llu submissions, %llu mismatched bytes\n", p.name,
static_cast<unsigned long long>(p.submissions), static_cast<unsigned long long>(p.mismatches));
return true;
}
};
} // namespace
int main() {
Check c;
if (!c.create()) return c.failed ? 1 : 77;
const bool ran = c.run();
std::printf("commands recorded: %llu (%llu transitions; copies: %llu buffer, %llu buffer-to-texture, %llu texture-to-"
"texture, %llu texture-to-buffer, %llu CopyResource buffer, %llu CopyResource texture)\n",
static_cast<unsigned long long>(c.commands), static_cast<unsigned long long>(c.barriers),
static_cast<unsigned long long>(c.kinds[0]), static_cast<unsigned long long>(c.kinds[1]),
static_cast<unsigned long long>(c.kinds[2]), static_cast<unsigned long long>(c.kinds[3]),
static_cast<unsigned long long>(c.kinds[4]), static_cast<unsigned long long>(c.kinds[5]));
if (c.mismatched_bytes) {
std::printf("mismatched bytes by resource:");
for (UINT k = 0; k < default_buffers; ++k) std::printf(" buffer%u %llu", k, static_cast<unsigned long long>(c.where_bad[k]));
for (UINT t = 0; t < textures; ++t)
for (UINT s = 0; s < tex_subs; ++s)
std::printf(" tex%u.%u %llu", t, s, static_cast<unsigned long long>(c.where_bad[default_buffers + t * tex_subs + s]));
std::printf("\n");
}
if (c.sync) std::printf("CHECK851_SYNC: %llu state round trips forced between two writes to one resource\n", static_cast<unsigned long long>(c.forced));
std::printf("%s: %llu submissions, %llu bytes read back, %llu mismatched, digest %016llx\n",
ran && !c.mismatched_bytes ? "OK" : "FAILED", static_cast<unsigned long long>(c.submissions),
static_cast<unsigned long long>(c.compared_bytes), static_cast<unsigned long long>(c.mismatched_bytes),
static_cast<unsigned long long>(c.digest));
return ran && !c.mismatched_bytes ? 0 : 1;
}
|
#2123 is merged (41cdb55), so this sits directly on
main. Claim: #2716 (comment)#2123 moved a draw's prepared data out of
detail::Prepared. This does the same for copies.detail::PreparedCopy. That coversCopyBufferRegion,CopyTextureRegion,CopyResource,ResolveSubresource(Region)andAtomicCopyBuffer*. The new structs live inPreparedList::copies, indexed byPrepared::copy(no_copyfor every other command).ExecuteIndirectcount buffer uses the same slot, insource, as it did inPrepared.reset()keeps each copy's vectors' storage.sizeof). Every command's entry is refilled on everyExecuteCommandLists, and most commands are neither draws nor copies.(Prepared& p, PreparedCopy& c):prepare_texture_copy,prepare_resource_copy,prepare_resolveandkeep_around_resolve.p.binding/p.rangestay the destination's.d3d12_state_map_testandpredicated_copy_testread the fields there now.Numbers
replay_cpu_bench --reps 25, i7-14700K, frame row (37,888 commands: 8 lists x 128 passes, draws included). Median of 5 alternating runs of the base (98d977a, #2123's head) and this branch:Recording is unchanged (~75 ns), and allocations are unchanged (prepare 0.04, emit 0 per command).
Tests
Tested 8e1fae6 on an RTX 5070, driver 621.01, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363, with
LEONIDA_VK_VALIDATION=1:ctest -E loader: 117/117 passed.d3d12_vulkan_device_test: exit 0, 0 validation messages.replay_cpu_bench --self-test: ok. It has a new check that a buffer copy prepared where a texture upload was before keeps none of the upload's regions.reset()inadd_copy, that check fails (FAIL copy storage, exit 1).AI-assisted (Claude Code).
🤖 Generated with Claude Code