Repository navigation
Conversation
|
The GPU upload path works on an RX 9070 XT (RDNA4): here the table lands in a GPU upload heap. fbecee4, driver 32.0.32029.2010, Windows 11 26200, Ryzen 7 5800X3D.
A temporary test (not committed, release build) on an 8 MiB table, the heaps alternating, 6 measurements each:
So filling in order costs about half as much again, and single slot writes cost the same here. These are CPU write times only. I did not measure shader reads, and I did not run the title, so nothing here says what it does to the frame time. With From the diff: I found no CPU read of the table, only the zeroing and the slot write in Edit: other GPU tests and a large file copy from another session of mine may have been running on this machine at the time, so treat the write times as rough. The heap type and the data check don't depend on that. No game data. AI-assisted (Claude Code); run on my machine. |
|
I tested the two GPU source changes from Shader reads after a CPU update were substantially faster on the GPU upload heap in an isolated D3D12 test. The compute shader reads each dword of an 8 MiB table once and produces 32,768 hashes. These are median GPU timestamps in microseconds over six measurements per cell, alternating heap order. The static warm-up uses the same shader. The update changes one dword per 256 bytes, in permuted order, with fresh values each time.
The CPU update itself took a median of 247–252 µs across these cases. All 2,359,296 result hashes across 72 timed dispatches matched the CPU reference, including a third precondition that wrote a separate 128 MiB GPU buffer. The synthetic access pattern deliberately touches the entire table. Actual shaders can use a much smaller subset. Gameplay on this downstream build gave mixed results. I applied the PR's two source-file hunks without edits to a local build based on
These are 127 complete host Present intervals per run between frames 1920 and 2048, with Jason viewed side-on at the same porch in all eight endpoint images. I chose that window after checking the captures. The originally planned start at frame 1632 was still a loading transition in two runs. Idle animation, streaming and shadows varied. The direction of the gameplay difference reversed across pairs. This supports keeping the option available for targeted A/B tests. A separate instrumented build also found redundant CPU stores while loading. The stride table starts at zero, and the heap already tracks which slots hold buffers. Omitting the zero store only when both the previous and new slot are non-buffers skipped 317,153 of 737,632 eligible store calls in one loading window (43.0%). A later black-screen-to-porch window skipped 466,915 of 959,757 (48.6%). Buffer-to-texture transitions still explicitly cleared the slot. The local slot-reuse test passed. A separate porch timing pair showed 5.49 vs 5.09 presents/s with suppression, so the counts alone do not justify enabling this change. Standalone shader-read probe (C++, Windows SDK 10.0.26100, no game files)Build in a Visual Studio x64 developer prompt using the command in the file header. The program prints CSV, checks both actual heap types and compares each GPU result with the CPU reference. // Synthetic D3D12 shader-read benchmark. Build with a current Windows SDK:
// cl /nologo /O2 /EHsc /std:c++17 descriptor_stride_probe.cpp d3d12.lib dxgi.lib d3dcompiler.lib
#include <windows.h>
#include <d3d12.h>
#include <d3dcompiler.h>
#include <dxgi1_6.h>
#include <wrl/client.h>
#include <array>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <stdexcept>
#include <utility>
#include <vector>
using Microsoft::WRL::ComPtr;
constexpr UINT words = 2 * 1024 * 1024;
constexpr UINT lanes = 32768;
constexpr UINT resultBytes = lanes * sizeof(UINT);
constexpr UINT sweepBytes = 128 * 1024 * 1024;
const char shader[] = R"(
ByteAddressBuffer table : register(t0);
RWByteAddressBuffer outputData : register(u0);
cbuffer Mode : register(b0) { uint scatter; };
[numthreads(64,1,1)]
void readTable(uint3 id : SV_DispatchThreadID) {
uint sum = 2166136261u;
for (uint i = id.x; i < 2097152u; i += 32768u) {
uint slot = scatter ? ((i * 1664525u + 1013904223u) & 2097151u) : i;
sum = (sum * 16777619u) ^ table.Load(slot * 4);
}
outputData.Store(id.x * 4, sum);
}
[numthreads(64,1,1)]
void sweep(uint3 id : SV_DispatchThreadID) {
for (uint i = id.x; i < 33554432u; i += 32768u)
outputData.Store(i * 4, i * 2654435761u);
}
)";
void check(HRESULT hr, const char* step) {
if (FAILED(hr)) {
std::fprintf(stderr, "%s failed (HRESULT 0x%08lx)\n", step, static_cast<unsigned long>(hr));
throw std::runtime_error(step);
}
}
ComPtr<ID3D12Resource> buffer(ID3D12Device* device, D3D12_HEAP_TYPE type, UINT bytes,
D3D12_RESOURCE_STATES state, D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE) {
D3D12_HEAP_PROPERTIES heap{};
heap.Type = type;
heap.CreationNodeMask = heap.VisibleNodeMask = 1;
D3D12_RESOURCE_DESC desc{};
desc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
desc.Width = bytes;
desc.Height = 1;
desc.DepthOrArraySize = 1;
desc.MipLevels = 1;
desc.SampleDesc.Count = 1;
desc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
desc.Flags = flags;
ComPtr<ID3D12Resource> result;
check(device->CreateCommittedResource(&heap, D3D12_HEAP_FLAG_NONE, &desc, state, nullptr,
IID_PPV_ARGS(&result)), "create buffer");
return result;
}
ComPtr<ID3D12PipelineState> pipeline(ID3D12Device* device, ID3D12RootSignature* root, const char* entry) {
ComPtr<ID3DBlob> code, errors;
auto hr = D3DCompile(shader, sizeof(shader) - 1, nullptr, nullptr, nullptr, entry,
"cs_5_1", D3DCOMPILE_OPTIMIZATION_LEVEL3, 0, &code, &errors);
if (FAILED(hr) && errors) std::fprintf(stderr, "%s\n", static_cast<const char*>(errors->GetBufferPointer()));
check(hr, entry);
D3D12_COMPUTE_PIPELINE_STATE_DESC desc{};
desc.pRootSignature = root;
desc.CS = {code->GetBufferPointer(), code->GetBufferSize()};
ComPtr<ID3D12PipelineState> result;
check(device->CreateComputePipelineState(&desc, IID_PPV_ARGS(&result)), "create compute pipeline");
return result;
}
void uavBarrier(ID3D12GraphicsCommandList* list, ID3D12Resource* resource) {
D3D12_RESOURCE_BARRIER barrier{};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
barrier.UAV.pResource = resource;
list->ResourceBarrier(1, &barrier);
}
uint32_t value(uint32_t i) { return ((i ^ (i >> 11)) * 2654435761u) + 97u; }
std::vector<UINT> referenceHashes(bool scatter, UINT salt) {
std::vector<UINT> hashes(lanes, 2166136261u);
for (UINT i = 0; i < words; ++i) {
UINT slot = scatter ? ((i * 1664525u + 1013904223u) & (words - 1)) : i;
UINT updated = value(slot) ^ (slot % 64 == 0 ? salt : 0);
hashes[i % lanes] = (hashes[i % lanes] * 16777619u) ^ updated;
}
return hashes;
}
double updateTable(ID3D12Resource* table, UINT salt) {
D3D12_RANGE noReads{};
void* data = nullptr;
auto start = std::chrono::steady_clock::now();
check(table->Map(0, &noReads, &data), "map table for updates");
for (UINT n = 0; n < lanes; ++n) {
UINT slot = ((n * 1664525u + 1013904223u) & (lanes - 1)) * 64;
static_cast<UINT*>(data)[slot] = value(slot) ^ salt;
}
table->Unmap(0, nullptr);
return std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - start).count();
}
int run() {
ComPtr<IDXGIFactory6> factory;
ComPtr<IDXGIAdapter1> adapter;
ComPtr<ID3D12Device> device;
check(CreateDXGIFactory1(IID_PPV_ARGS(&factory)), "DXGI factory");
check(factory->EnumAdapterByGpuPreference(0, DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
IID_PPV_ARGS(&adapter)), "hardware adapter");
DXGI_ADAPTER_DESC1 adapterInfo{};
check(adapter->GetDesc1(&adapterInfo), "adapter description");
check(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)), "D3D12 device");
D3D12_FEATURE_DATA_D3D12_OPTIONS16 features{};
check(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS16, &features, sizeof(features)), "options 16");
if ((adapterInfo.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) || !features.GPUUploadHeapSupported)
throw std::runtime_error("A hardware adapter with GPU upload heaps is required.");
D3D12_ROOT_PARAMETER params[3]{};
params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;
params[2].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
params[2].Constants.Num32BitValues = 1;
D3D12_ROOT_SIGNATURE_DESC sig{};
sig.NumParameters = 3;
sig.pParameters = params;
ComPtr<ID3DBlob> serialized;
check(D3D12SerializeRootSignature(&sig, D3D_ROOT_SIGNATURE_VERSION_1, &serialized, nullptr), "serialize root");
ComPtr<ID3D12RootSignature> root;
check(device->CreateRootSignature(0, serialized->GetBufferPointer(), serialized->GetBufferSize(),
IID_PPV_ARGS(&root)), "create root");
auto reader = pipeline(device.Get(), root.Get(), "readTable");
auto sweeper = pipeline(device.Get(), root.Get(), "sweep");
const std::array<D3D12_HEAP_TYPE, 2> types{D3D12_HEAP_TYPE_UPLOAD, D3D12_HEAP_TYPE_GPU_UPLOAD};
std::array<ComPtr<ID3D12Resource>, 4> tables;
D3D12_RANGE empty{};
for (UINT h = 0; h < tables.size(); ++h) {
auto type = types[h % types.size()];
tables[h] = buffer(device.Get(), type, words * sizeof(UINT), D3D12_RESOURCE_STATE_GENERIC_READ);
D3D12_HEAP_PROPERTIES actual{};
check(tables[h]->GetHeapProperties(&actual, nullptr), "check actual heap");
if (actual.Type != type) throw std::runtime_error("Unexpected table heap type.");
void* mapping = nullptr;
check(tables[h]->Map(0, &empty, &mapping), "map table for writing");
for (UINT i = 0; i < words; ++i) static_cast<UINT*>(mapping)[i] = value(i);
tables[h]->Unmap(0, nullptr);
}
auto output = buffer(device.Get(), D3D12_HEAP_TYPE_DEFAULT, resultBytes, D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
auto pressure = buffer(device.Get(), D3D12_HEAP_TYPE_DEFAULT, sweepBytes, D3D12_RESOURCE_STATE_UNORDERED_ACCESS,
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS);
auto readback = buffer(device.Get(), D3D12_HEAP_TYPE_READBACK, resultBytes + 16, D3D12_RESOURCE_STATE_COPY_DEST);
const std::array<std::vector<UINT>, 2> expected{referenceHashes(false, 0), referenceHashes(true, 0)};
ComPtr<ID3D12CommandQueue> queue;
ComPtr<ID3D12CommandAllocator> allocator;
ComPtr<ID3D12GraphicsCommandList> list;
ComPtr<ID3D12Fence> fence;
ComPtr<ID3D12QueryHeap> queries;
D3D12_COMMAND_QUEUE_DESC q{};
q.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
check(device->CreateCommandQueue(&q, IID_PPV_ARGS(&queue)), "create queue");
check(device->CreateCommandAllocator(q.Type, IID_PPV_ARGS(&allocator)), "create allocator");
check(device->CreateCommandList(0, q.Type, allocator.Get(), nullptr, IID_PPV_ARGS(&list)), "create list");
check(list->Close(), "close initial list");
check(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence)), "create fence");
D3D12_QUERY_HEAP_DESC queryDesc{D3D12_QUERY_HEAP_TYPE_TIMESTAMP, 2, 0};
check(device->CreateQueryHeap(&queryDesc, IID_PPV_ARGS(&queries)), "create query heap");
UINT64 frequency = 0, completed = 0;
check(queue->GetTimestampFrequency(&frequency), "timestamp frequency");
std::fprintf(stderr, "adapter=%ls frequency=%llu Hz table_bytes=%u lanes=%u\n",
adapterInfo.Description, frequency, words * 4, lanes);
const char* conditions[] = {"warm", "after_128MiB_write", "after_32768_cpu_updates"};
std::puts("repeat,access,precondition,heap,gpu_us,cpu_update_us,wrong_hashes");
for (UINT repeat = 0; repeat <= 6; ++repeat) {
for (UINT scatter = 0; scatter < 2; ++scatter) {
UINT salt = 2654435761u * (1 + repeat * 2 + scatter);
auto updated = referenceHashes(scatter != 0, salt);
for (UINT condition = 0; condition < 3; ++condition) {
for (UINT order = 0; order < 2; ++order) {
UINT h = (repeat + order) % 2;
bool dynamic = condition == 2;
auto* table = tables[h + (dynamic ? 2 : 0)].Get();
const auto& wanted = dynamic ? updated : expected[scatter];
double updateUs = dynamic ? updateTable(table, salt) : 0;
check(allocator->Reset(), "reset allocator");
check(list->Reset(allocator.Get(), nullptr), "reset list");
list->SetComputeRootSignature(root.Get());
list->SetComputeRootShaderResourceView(0, table->GetGPUVirtualAddress());
list->SetComputeRoot32BitConstant(2, scatter, 0);
if (!dynamic) {
auto* before = condition == 1 ? pressure.Get() : output.Get();
list->SetPipelineState(condition == 1 ? sweeper.Get() : reader.Get());
list->SetComputeRootUnorderedAccessView(1, before->GetGPUVirtualAddress());
list->Dispatch(lanes / 64, 1, 1);
// Order the cache-pressure pass even when it uses a different UAV.
uavBarrier(list.Get(), nullptr);
}
list->SetPipelineState(reader.Get());
list->SetComputeRootUnorderedAccessView(1, output->GetGPUVirtualAddress());
list->EndQuery(queries.Get(), D3D12_QUERY_TYPE_TIMESTAMP, 0);
list->Dispatch(lanes / 64, 1, 1);
uavBarrier(list.Get(), output.Get());
list->EndQuery(queries.Get(), D3D12_QUERY_TYPE_TIMESTAMP, 1);
list->ResolveQueryData(queries.Get(), D3D12_QUERY_TYPE_TIMESTAMP, 0, 2, readback.Get(), 0);
D3D12_RESOURCE_BARRIER copy{};
copy.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
copy.Transition = {output.Get(), D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE};
list->ResourceBarrier(1, ©);
list->CopyBufferRegion(readback.Get(), 16, output.Get(), 0, resultBytes);
std::swap(copy.Transition.StateBefore, copy.Transition.StateAfter);
list->ResourceBarrier(1, ©);
check(list->Close(), "close list");
ID3D12CommandList* lists[] = {list.Get()};
queue->ExecuteCommandLists(1, lists);
check(queue->Signal(fence.Get(), ++completed), "signal fence");
check(fence->SetEventOnCompletion(completed, nullptr), "wait for readback");
check(device->GetDeviceRemovedReason(), "GPU completion");
void* data = nullptr;
D3D12_RANGE range{0, resultBytes + 16};
check(readback->Map(0, &range, &data), "map completed readback");
auto* times = static_cast<const UINT64*>(data);
double us = static_cast<double>(times[1] - times[0]) * 1e6 / frequency;
auto* hashes = static_cast<const UINT*>(data) + 4;
UINT wrong = 0;
for (UINT i = 0; i < lanes; ++i) wrong += hashes[i] != wanted[i];
readback->Unmap(0, &empty);
if (repeat) std::printf("%u,%s,%s,%d,%.3f,%.3f,%u\n", repeat, scatter ? "permuted" : "linear",
conditions[condition], types[h], us, updateUs, wrong);
if (wrong) throw std::runtime_error("GPU hashes differ from the CPU reference.");
}
}
}
}
return 0;
}
int main() {
try { return run(); }
catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; }
} |
Experiment
Try placing the descriptor-stride table in CPU-visible VRAM on a supported discrete GPU. The table is 8 MiB per view heap and lives with that heap. Descriptor updates write it from the CPU, and shaders read it through a root SRV. The experiment moves GPU reads of this table to local memory. CPU updates still cross PCIe.
VICTORY_GPU_UPLOAD_STRIDES=1enables the trial for stride tables. It checks the D3D12 architecture and GPU upload capability, leaves 10% local-budget headroom at allocation time, and falls back to the ordinary upload heap when support, room, or allocation is unavailable. The experiment is off by default.Validation and measurement
cargo check --locked -p victoryandgit diff --checkpassed. Validation used synthetic tests.descriptor stride table in CPU-visible video memorylog. Compare the same scene after shader warm-up with and withoutVICTORY_GPU_UPLOAD_STRIDES. Record frame time, GPU time, and local memory use. KeepVICTORY_QUEUE2and the copy-back settings fixed between runs. The PCIe read/write trade-off and frame-time effect remain unmeasured.Microsoft GPU upload heap specification · NVIDIA GPU upload heap guidance
AI-assisted change.