Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"xmem_placeholder_split_check", "xmem_map_split_first_check", "startup_dstorage_break_window_check",
"xmem_page_runs_device_check", "linear_buffer_backing_check",
"bcpack_formats_check", "startup_stub_consumer", "startup_inline_retire", "startup_resource_retire_title", "startup_table_lock",
"native_compute_ir_dump", "native_pixel_ir_dump", "native_store_replay", "native_raw_buffer_view_check"
"native_compute_ir_dump", "native_pixel_ir_dump", "native_store_replay", "native_raw_buffer_view_check",
"native_heap_token_texture"
]
},
{ "name": "release", "configurePreset": "release" },
Expand Down
2 changes: 2 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -18356,3 +18356,5 @@ ctest --preset dev --no-tests=error -j1 -R '^native_pixel_atomic_(warp|hardware)
```

Repeat with the release preset. The source-built fixture verifies 13 actual draws and four refusal controls per adapter/configuration, including contention, guard words, active out-of-view, odd raster edges and real WQM quad-neighbor reads with and without GLC. WQM+GLC records returned old values through a second exact atomic in a private lane slot and checks a valid serialization of the varying addends. Local RX 7900 XT/WARP Debug/Release integration over main 4b0350e passed 52 draws, 3328 DWORD and 1664 pixel comparisons, and 16 refusal controls. The debug layer was unavailable; actual title rendering is not established.

See [GPU-selected heap textures through view tokens in native compute](experiments/native-heap-token-textures.md) (#3023 stage 3, translator only) for the `heaptok` manifest lines the runtime still refuses.
58 changes: 58 additions & 0 deletions docs/experiments/native-heap-token-textures.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# GPU-selected heap textures through view tokens in native compute (translator, #3023 stage 3, PR 1)

Issue #3023. The native compute translator accepts a texture that the program loads with `s_load_dwordx8` from the directly
indexed resource heap at a byte offset chosen by GPU data (a "heap token texture"). This change is the translator, the
manifest writer, the reader refusal and the fixtures. The runtime still refuses the new manifest lines by name ("heap token
textures are not bound yet"), so no dispatch behaviour changes. The runtime side (binding the token memory, the dimension
table and the host descriptor ranges) is a later change.

## Why a token

The guest computes the descriptor address itself and the title copies descriptors with its own memory copies, so the
translator cannot mirror heap slots. What the title keeps at the descriptor address of a registered view is a 32-byte view
token: word 0 the magic `0x56573258`, word 1 the view id (a content token has bit 31 set and is not an id). The shader reads
those two words at the guest byte address and maps the id to a host descriptor.

## What the translator does

- A program is eligible when every reached `s_load_dwordx8` from the resource heap with an SGPR offset (a token load) is
used only as the T# of `image_load`, `image_load_mip`, `image_sample_l`, `image_sample_lz`, `image_gather4_lz` (2D) or
`image_get_resinfo` of a 1D, 2D or 2D-array view, without D16. A copy, a numeric read, an image field read, a store, a
comparison, cube or 3D use or a partial use makes the program not eligible; it then keeps today's analysis and its
refusal text. An eligible program is analysed a second time with the byte address kept in the destination SGPRs
(`heap_tokens` in `transfer()` and `parse()`).
- The load handler requires an immediate offset and a proven SGPR offset (`constant` or `masked`) that are multiples of 4,
writes `so + imm` to the eight words, and bypasses the heap view contract: the contract exists because the runtime cannot
vouch for the view kind of a GPU-selected slot, and this route checks it on every execution.
- Per image instruction of dimension `d`: `valid = word0 == magic && id < count && dims[min(id, count - 1)] == 1 + d`;
the handle is `ResourceDescriptorHeap[valid ? base(d) + id : null]` (kind 1, 2 or 7, `nonuniform` false: every lane loads
the same address). When `!valid`, the first lane of the wave adds one to a miss counter (`waveIsFirstLane`, `atomicBinOp`), but only when the wave has an
active guest lane (`waveActiveBallot` of EXEC is not zero); a wave that reaches the instruction with EXEC = 0 counts nothing.
- Five more `b0` words follow the stride words: `base1d`, `base2d`, `base2da`, `count`, `null`. Three runtime-provided views
(root parameter `compute_heap_token`): the token memory (`heap_token_srv`, a raw SRV), the dimension table
(`heap_token_dims_srv`, a raw SRV with one uint per view id: 0 none, else 1 + the image dimension) and the miss counter
(`heap_token_miss_uav`, a raw UAV). They take the first SRV and UAV registers after the stride views, because the module
names the registers while it is lowered.
- Manifest lines: `srv N heaptok`, `srv N heaptokdims`, `uav N heaptokmiss`, `udsrc K heaptok base1d|base2d|base2da|count|null`.
`native_compute_output_revision` is 2: a request with such a load was refused before and is published now, so the
same request changed meaning. Every published manifest of revision 1 then reads as "another compiler revision; rebuild
it", also those without a token load, which differ only in the `native` marker line; they are rebuilt once. The native-only
route (no manifest reader) refuses a program with token words with the same text as the manifest reader,
`heap token textures are not bound yet`. The root signature of the pipeline sets `CBV_SRV_UAV_HEAP_DIRECTLY_INDEXED` for them.

## Tests

- `native_heap_token_texture_controls`: compiles source-written requests; checks the published manifest, the module's heap
handle, wave-first-lane and atomic add, and each consumer or address shape that keeps today's refusal text or gets a new one
(`heap token byte offset has no proven 4-byte aligned range`, `unaligned heap token immediate byte offset`).
- `native_heap_token_texture_warp` and `_hardware`: the rig runs 1D, 2D and 2D-array programs on a host heap with one range
per dimension; a valid token gives the texel of its view and no miss; a wrong magic, an id equal to the count, the largest
id, an id of another dimension, a content token, id 0 and an empty token give the null texel and one miss per wave. WARP has
no 32-lane waves and reports 77.
- `native_compute_manifest_reasons` covers the writer; `startup_compute_dispatch_texture` the reader refusal.

## Open

The null slot has one word; a null view of another dimension than the instruction's is not defined by D3D12 for dynamic
indexing, so the runtime change has to provide a null slot that every dimension may read (the fixture uses a null view of the
dimension under test).
40 changes: 35 additions & 5 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Bytes encode(const NativeComputeShader &program) {
for (unsigned n : {c.root_parameter, c.first_sgpr, c.count})
word(out, n);
word(out, program.sampler_heap);
word(out, program.heap_token_words);
word(out, static_cast<unsigned>(program.stride_srvs.size()));
for (unsigned srv : program.stride_srvs)
word(out, srv);
Expand Down Expand Up @@ -137,14 +138,17 @@ NativeComputeShader decode(std::string_view bytes) {
unsigned n = in.count(1000000);
shader.bindings.reserve(n);
for (unsigned j = 0; j < n; ++j) {
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::root_buffer_uav)));
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::heap_token_miss_uav)));
ComputeViewBinding b{kind, in.word(), in.word(), in.word(), in.word(), in.word(), in.word(), in.count(7)};
b.scalar_table = static_cast<ComputeViewBinding::ScalarTable>(in.count(unsigned(ComputeViewBinding::ScalarTable::srv)));
b.requires_identity_dst_sel = in.count(1) != 0;
const bool sampler_heap = kind == ComputeViewKind::heap_sampler;
const bool token = kind == ComputeViewKind::heap_token_srv || kind == ComputeViewKind::heap_token_dims_srv ||
kind == ComputeViewKind::heap_token_miss_uav;
if (!b.array_count || b.array_position >= b.array_count ||
(sampler_heap ? b.root_parameter != compute_sampler_heap
: b.root_parameter >= 16 && b.root_parameter != compute_resource_heap))
: token ? b.root_parameter != compute_heap_token
: b.root_parameter >= 16 && b.root_parameter != compute_resource_heap))
throw std::runtime_error("invalid cached binding");
shader.bindings.push_back(b);
}
Expand All @@ -156,6 +160,7 @@ NativeComputeShader decode(std::string_view bytes) {
shader.constants.push_back(c);
}
shader.sampler_heap = in.count(1) != 0;
shader.heap_token_words = in.count(1) != 0;
n = in.count(32);
for (unsigned j = 0; j < n; ++j)
shader.stride_srvs.push_back(in.count(31));
Expand Down Expand Up @@ -1030,9 +1035,10 @@ static const char *view_kind_name(ComputeViewKind kind) {
"comparison_texture_srv", "half2_texture_uav", "half4_texture_uav", "cube_sample_srv", "cube_comparison_srv",
"indexed_atomic_uav", "d16_sample_srv", "byte_window_srv", "heap_buffer_srv", "heap_sampler",
"uint_texture_uav", "float_texture_uav", "float2_texture_uav", "float3_texture_uav", "packed_d16_x_uav",
"indexed_raw_srv", "heap_typed_srv", "root_buffer_uav"};
"indexed_raw_srv", "heap_typed_srv", "root_buffer_uav", "heap_token_srv", "heap_token_dims_srv",
"heap_token_miss_uav"};
// ponytail: catches a renamed last kind, not one appended after it; extend both together.
static_assert(std::size(names) == static_cast<size_t>(ComputeViewKind::root_buffer_uav) + 1);
static_assert(std::size(names) == static_cast<size_t>(ComputeViewKind::heap_token_miss_uav) + 1);
const auto index = static_cast<size_t>(kind);
return index < std::size(names) ? names[index] : "unknown";
}
Expand Down Expand Up @@ -1068,7 +1074,7 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return refuse("more than 8 root constants in one root parameter");
ud_count += block.count;
}
ud_count += static_cast<unsigned>(program.stride_srvs.size());
ud_count += static_cast<unsigned>(program.stride_srvs.size()) + (program.heap_token_words ? 5u : 0u);
if (ud_count > 32)
return refuse("more than 32 user data words");

Expand Down Expand Up @@ -1098,7 +1104,25 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh

unsigned srv_count = 0, uav_count = 0;
std::ostringstream srv_lines, uav_lines, sampler_lines;
unsigned token_views[3]{};
for (const auto &b : sorted) {
// The runtime-provided views of GPU-selected heap textures: the view token memory, the dimension table and the
// miss counter. They take table registers like any view; the runtime reader does not bind them yet.
const bool token_view = b.kind == ComputeViewKind::heap_token_srv || b.kind == ComputeViewKind::heap_token_dims_srv ||
b.kind == ComputeViewKind::heap_token_miss_uav;
if (token_view != (b.root_parameter == compute_heap_token))
return refuse("heap token view with another root parameter");
if (token_view) {
if (!program.heap_token_words || b.array_count != 1)
return refuse("heap token view without heap token words");
const bool miss = b.kind == ComputeViewKind::heap_token_miss_uav;
if (b.shader_register != (miss ? uav_count++ : srv_count++))
return refuse(miss ? "UAV registers not contiguous" : "SRV registers not contiguous");
++token_views[b.kind == ComputeViewKind::heap_token_srv ? 0 : b.kind == ComputeViewKind::heap_token_dims_srv ? 1 : 2];
(miss ? uav_lines : srv_lines) << (miss ? "uav " : "srv ") << b.shader_register
<< (miss ? " heaptokmiss" : b.kind == ComputeViewKind::heap_token_srv ? " heaptok" : " heaptokdims") << '\n';
continue;
}
if (b.root_parameter == compute_resource_heap)
return refuse("resource heap view");
if (b.root_parameter == compute_sampler_heap)
Expand Down Expand Up @@ -1240,6 +1264,12 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return refuse("stride words from a view that is not one raw vector SRV");
out << "udsrc " << ud_index++ << " stride " << srv << '\n';
}
if (program.heap_token_words) {
if (token_views[0] != 1 || token_views[1] != 1 || token_views[2] != 1)
return refuse("heap token words without the three heap token views");
for (const char *word : {"base1d", "base2d", "base2da", "count", "null"})
out << "udsrc " << ud_index++ << " heaptok " << word << '\n';
}
out << srv_lines.str() << uav_lines.str() << sampler_lines.str();
if (sampler_heap_bytes)
out << "smpidx " << sampler_heap_bytes << '\n';
Expand Down
Loading
Loading