Skip to content
Merged
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
66 changes: 59 additions & 7 deletions docs/experiments/native-sampler-heap.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Issue #3076, part of #2935 (related #1143). The native compute translator now ac
the sampler heap with `s_load_dwordx4` / `s_load_dwordx8` at a constant 16-byte aligned offset and uses with
`image_sample_l` or `image_sample_lz`. It lowers the sampler to `SamplerDescriptorHeap[index]` in the DXIL module. This is
stage 1: the translator, the binding record, the cache, the root signature and `bind()`. Binding a real, mirrored sampler heap
at dispatch time in the runtime is stage 2 and is not part of this change.
at dispatch time in the runtime is stage 2: the published-manifest route is stage 2a (section "Stage 2a" below); the in-process
route is not done.

## Why

Expand Down Expand Up @@ -44,9 +45,9 @@ requests stop first at such a load (117 `dwordx4`, 13 `dwordx8`). The change rem
does for `SamplerDescriptorHeap[i]` (DXC reference: flags `0x80000010` with raw buffers).
- Binding record: `ComputeViewKind::heap_sampler`, one per program, `{heap_sampler, compute_sampler_heap, byte_offset 0,
register 0, required_bytes 16 * (highest index + 1)}`. The frontend cache accepts the kind with exactly that root value; an
older decoder rejects it and recomputes. `format_compute_runtime_manifest` returns no manifest for it (root parameter above
15), so `compile_dxil --requests` publishes `<key>.cs.cso` without `<key>.txt`; the in-process dispatch route refuses the
kind. `compile_dxil` reports `sampler_heap span=<bytes>`.
older decoder rejects it and recomputes. Stage 1 published no manifest for it (root parameter above 15); stage 2a (below)
publishes `smpidx <bytes>`. The in-process dispatch route (`translate_compute_natively`) still refuses the kind.
`compile_dxil` reports `sampler_heap span=<bytes>`.
- `create_native_compute_pipeline`: `sampler_heap_indexed` adds root flag `0x800` (`SAMPLER_HEAP_DIRECTLY_INDEXED`) and does not
set `heap_indexed` or `0x400`. `NativeComputePipeline::bind` has a last parameter `ID3D12DescriptorHeap *sampler_heap`: with
`sampler_heap_indexed` a null heap throws ("a sampler heap is needed"); with views too, the heaps are set as views first, then
Expand All @@ -61,8 +62,58 @@ requests stop first at such a load (117 `dwordx4`, 13 `dwordx8`). The change rem
The runtime must fill the sampler heap bound for a natively translated compute dispatch so that entry `n` is title byte offset
`16 * n` (index parity), with `dynamic_sampler()` decoding (`src/startup_stubs/translated_sampler_bindings.h`: MIN_LOD and MAX_LOD
applied, which is why only the explicit-LOD forms are translated), and entry 2047 (`translated_sampler_fallback`) holding the
fallback sampler. The translator refuses index 2047, so the fallback is never read. None of this is implemented here; the fixture
builds its heap with `dynamic_sampler()` and the index parity itself.
fallback sampler. The translator refuses index 2047, so the fallback is never read. The stage 1 fixture builds its heap with
`dynamic_sampler()` and the index parity itself; stage 2a below does it in the runtime.

## Stage 2a: the title's sampler heap for a published manifest (#3076)

Scope: the published-manifest route only (`compile_dxil --requests` writes `<key>.txt`, `load_compute_translation` reads it, the
stand-in's `Dispatch` path binds). The in-process route `translate_compute_natively` keeps refusing the heap sampler (stage 2b,
after #3154), and `dynamic_sampler`, `create_sampler_snapshot_heap` and `decode_statics` are untouched (#3199).

- Manifest writer (`format_compute_runtime_manifest`, `src/direct_compute_cache.cpp`): the one `heap_sampler` binding leaves the
SRV/UAV bookkeeping (it has shader register 0 and no table) and is written as `smpidx <bytes>`, `bytes` being the binding's span,
after the `smp`/`smpl` lines and before `native <revision>`. It is published only for exactly one binding with root
`compute_sampler_heap`, register 0, array count 1 and a span of 16 to `16 * 2047` bytes in whole entries (2047 is the fallback
entry that the translator never reads). Any other shape keeps the one reason that every sampler heap had before, `sampler heap`,
and publishes nothing (so `triage_run.py`'s fixed reasons and `native_compute_manifest_reasons` are unchanged). Static samplers
and `smpidx` may coexist.
- Manifest reader (`load_compute_translation`): `smpidx <bytes>` is accepted once; zero, a span that is not a multiple of 16, a span
above `16 * 2047` or a second line is refused as "a sampler heap span that is not 16-byte entries below the fallback" (a line with
trailing text is "a sampler that is not a plain static one", as before). The graphics-style `smpheap` table is still refused
("a sampler from the sampler heap").
- Root signature (`create_translated_root`, new trailing parameter `extra_flags`): a translation with `smpidx` creates its root with
`D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED` (serialized as version 1.0, which takes the flag on the RTX 3080);
graphics callers are unchanged. A root that fails to serialize is refused with "root signature creation failed". A root that
serializes with the flag and is then refused by `CreateRootSignature` is refused with "the device does not take a directly
indexed sampler heap" only when the adapter reports shader model below 6.6 or resource binding tier below 3 (an inference
from the reported capabilities, not proof of the driver's reason), and with "root signature creation failed" otherwise;
a pipeline failure keeps "pipeline creation failed" with the adapter capability added.
- Dispatch (`record_translated_dispatch`), after the views are resolved and before a descriptor is taken: refused with
"no sampler heap is set on the list" when the list has no title sampler heap (neither the title-memory address nor the wrapper);
the heap is the draw path's snapshot of the title heap (`prepare_draw_sampler_heap`, keeping the list's existing fallback
setting so draws and dispatches on one list do not rebuild it), refused with "the sampler heap could not be created" when it cannot be
made, with "the sampler heap has no readable entries" when the snapshot is empty (a wrapper without a readable shadow heap),
and with "the sampler heap is smaller than the program reads" when the snapshot holds fewer entries than the span. The
sampler heap is then set with the view heap before the root signature, and again when another snapshot heap replaces it. A
program without `smpidx` leaves whatever sampler heap is bound, as before. Every refusal counts as a bind refusal.
- Limits kept from the draw path: an unreadable entry or an unresolvable `X2WV` token becomes the border sampler (counted in the
`x2w-draw sampler heap:` log line), and entries are decoded by `dynamic_sampler()`.

Tests: `native_compute_manifest_sampler_heap` (CPU: exact text, the heap binding first, between and last next to a stride source,
static samplers, and the one-reason refusals), `native_sampler_heap_hardware` (the published manifests of SAMPLE, TWO and MIXED are
exact; they were absent before), and `startup_compute_dispatch_sampler_heap_{warp,hardware}`
(`tests/startup_compute_sampler_heap_check.py`, consumer mode `sampler-heap`; `SKIP_RETURN_CODE 77`). The GPU test publishes
the SAMPLE program with `compile_dxil --requests`, runs six reader refusals (they come before the pipeline is created, so WARP
runs them too, then exits 77: no 32-lane waves), runs the dispatch on a 128-entry title heap (entry 67 wrap/point, decoys elsewhere)
and compares all 32 lanes with a CPU model, puts each of four decoys in slot 67 (the output must change and equal the decoy's
model where it is a point sampler), and checks the controls: view heap only ("no sampler heap is set on the list"), 67 entries
("the sampler heap is smaller than the program reads"), exactly 68 entries (runs), a manifest span of 1104 on 68 entries (refused),
and the manifest without `smpidx` ("pipeline creation failed": the root then lacks the flag).

Not done: the in-process route; title heaps on the wrapper-only path (no title memory; `prepare_draw_sampler_heap` serves both,
only the title-memory path is exercised by the GPU test); `ExecuteIndirect` dispatches (the code path is shared, no test);
containers; GPU-selected sampler indices.

## Fixture

Expand Down Expand Up @@ -120,7 +171,8 @@ skips: the four WARP tests that already skipped and the new one).

## Limits

- Stage 1 only: no runtime binding of a mirrored sampler heap for natively translated compute dispatch.
- Stage 1 binds no sampler heap at dispatch time; stage 2a binds the title's heap for the published-manifest route only (the
in-process route is not done).
- GPU-selected sampler indices (SGPR offset), comparison, gather and gradient sampling through the heap, and containers are
refused.
- The fixture samples with point filters and compares exact texels; decoys are checked to change the result (and to run:
Expand Down
22 changes: 20 additions & 2 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,22 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
return refuse("more than 32 user data words");

auto sorted = program.bindings;
// The directly indexed sampler heap (#3076) is no SRV or UAV register and has no table: its one binding leaves the
// view bookkeeping below and becomes the line `smpidx <bytes>`, the span the program reads (16 bytes per title entry,
// whole entries below the fallback entry 2047 that the translator never reads). A binding the runtime cannot bind
// keeps the reason this function always gave for the sampler heap.
unsigned sampler_heap_bytes = 0;
for (auto at = sorted.begin(); at != sorted.end();) {
if (at->kind != ComputeViewKind::heap_sampler) {
++at;
continue;
}
if (sampler_heap_bytes || at->root_parameter != compute_sampler_heap || at->shader_register != 0 ||
at->array_count != 1 || !at->required_bytes || at->required_bytes % 16 || at->required_bytes > 16 * 2047)
return refuse("sampler heap");
sampler_heap_bytes = at->required_bytes;
at = sorted.erase(at);
}
std::sort(sorted.begin(), sorted.end(), [](const auto &a, const auto &b) {
const bool ua = compute_uav(a.kind), ub = compute_uav(b.kind);
if (ua != ub)
Expand Down Expand Up @@ -1069,8 +1085,10 @@ 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';
}
out << srv_lines.str() << uav_lines.str() << sampler_lines.str()
<< "native " << native_compute_output_revision << '\n';
out << srv_lines.str() << uav_lines.str() << sampler_lines.str();
if (sampler_heap_bytes)
out << "smpidx " << sampler_heap_bytes << '\n';
out << "native " << native_compute_output_revision << '\n';
return out.str();
}

Expand Down
54 changes: 48 additions & 6 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct ComputeTranslation {
x2w_startup::ManifestRetry manifest_retry; // absent and stale manifests can become ready later
std::vector<Slot> uds, srvs, uavs;
UINT heap_srvs = 0;
UINT sampler_heap_bytes = 0; // `smpidx`: the program reads the title's sampler heap (directly indexed) up to this byte
x2w_startup::TranslatedSamplers samplers; // static ones only
x2w_startup::ComPtr<ID3D12RootSignature> root;
x2w_startup::ComPtr<ID3D12PipelineState> pipeline;
Expand Down Expand Up @@ -251,6 +252,16 @@ bool adapter_reports_shader_model_below_6_6()
model.HighestShaderModel < D3D_SHADER_MODEL_6_6;
}

// The directly indexed sampler heap needs shader model 6.6 and resource binding tier 3 (tests/native_sampler_heap.cpp).
// A query that fails does not count as lacking: the capability text is only given on a reported shortfall.
bool adapter_lacks_directly_indexed_sampler_heap()
{
D3D12_FEATURE_DATA_D3D12_OPTIONS options{};
return adapter_reports_shader_model_below_6_6() ||
(SUCCEEDED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options))) &&
options.ResourceBindingTier < D3D12_RESOURCE_BINDING_TIER_3);
}

// The root signature and pipeline of a translation whose entry values and views are filled in. Returns why it is
// unavailable, or null when it is ready.
const char* create_compute_pipeline(ComputeTranslation& tr, const void* code, std::size_t bytes)
Expand All @@ -260,15 +271,20 @@ const char* create_compute_pipeline(ComputeTranslation& tr, const void* code, st
std::vector<UINT> atomic_registers;
for (UINT i = 0; i < tr.uavs.size(); ++i)
if (tr.uavs[i].atomic) atomic_registers.push_back(i);
bool serialized = false;
if (FAILED(x2w_startup::create_translated_root(device_of(the_device), static_cast<UINT>(tr.uds.empty() ? 1 : tr.uds.size()),
static_cast<UINT>(tr.srvs.size()), tr.samplers, &tr.root, static_cast<UINT>(tr.uavs.size()), false, tr.heap_srvs,
static_cast<UINT>(atomic_registers.size()), atomic_registers.data())))
return "root signature creation failed";
static_cast<UINT>(atomic_registers.size()), atomic_registers.data(),
tr.sampler_heap_bytes ? D3D12_ROOT_SIGNATURE_FLAG_SAMPLER_HEAP_DIRECTLY_INDEXED : D3D12_ROOT_SIGNATURE_FLAG_NONE,
&serialized)))
return x2w_startup::compute_root_refusal(tr.sampler_heap_bytes != 0, serialized,
tr.sampler_heap_bytes != 0 && serialized &&
adapter_lacks_directly_indexed_sampler_heap());
D3D12_COMPUTE_PIPELINE_STATE_DESC desc{};
desc.pRootSignature = tr.root.Get();
desc.CS = {code, bytes};
if (FAILED(device_of(the_device)->CreateComputePipelineState(&desc, IID_PPV_ARGS(&tr.pipeline))))
return "pipeline creation failed";
return "pipeline creation failed"; // exact text: the caller adds the adapter capability to it
tr.state = 1;
return nullptr;
}
Expand Down Expand Up @@ -425,6 +441,14 @@ const char* load_compute_translation(ComputeTranslation& tr)
return "a resource heap range outside the supported layout";
tr.heap_srvs = x;
}
// The directly indexed sampler heap (#3076): the span in bytes the program reads, whole 16-byte title entries
// below the fallback entry (2047) that the translator never reads. Accepted once.
else if (std::sscanf(line.c_str(), "smpidx %u%n", &x, &used) == 1 && whole()) {
if (tr.sampler_heap_bytes || !x || x % x2w_startup::title_sampler_stride ||
x > x2w_startup::title_sampler_stride * x2w_startup::translated_sampler_fallback)
return "a sampler heap span that is not 16-byte entries below the fallback";
tr.sampler_heap_bytes = x;
}
// A static sampler, decoded as the draw path does: plain (smp), with the descriptor's LOD clamp (smpl) or a
// comparison one (smpc). Not modelled here: the run-time sampler heap table (smpheap).
else if (line.rfind("smpheap", 0) == 0) return "a sampler from the sampler heap";
Expand Down Expand Up @@ -1286,6 +1310,21 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
reason = "an input texture could not be uploaded";
std::memset(words, 0, sizeof(words));
}
// A program that reads the directly indexed sampler heap (#3076) runs with the sampler heap the title set on the
// list, entry n at title byte 16 * n. Each refusal here comes before the view descriptors are taken. The heap is the
// draw path's snapshot (an unreadable entry or an unresolvable token is its border sampler, counted in the
// "x2w-draw sampler heap" line); entry 2047 is never read by the program, so a snapshot with or without the reserved
// fallback serves, and the list's existing one is kept to avoid rebuilding the heap between draws and dispatches.
if (!reason && tr->sampler_heap_bytes) {
if (!pass.title_samplers.address && !pass.sampler_heap_wrapper)
reason = "no sampler heap is set on the list";
else if (!prepare_draw_sampler_heap(self, pass, list, pass.sampler_snapshot ? pass.sampler_snapshot->reserved_fallback : true))
reason = "the sampler heap could not be created";
else if (!pass.sampler_snapshot->count) // a wrapper without a readable shadow heap gives no entries
reason = "the sampler heap has no readable entries";
else if (u64{pass.sampler_snapshot->count} * x2w_startup::title_sampler_stride < tr->sampler_heap_bytes)
reason = "the sampler heap is smaller than the program reads";
}
if (reason) {
if (!tr->first_refusal) {
tr->first_refusal = reason;
Expand Down Expand Up @@ -1340,10 +1379,13 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
desc.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
device_of(the_device)->CreateUnorderedAccessView(view.resource, nullptr, &desc, dest);
}
if (!pass.heap_set) {
x2w_startup::bind_translated_heaps(list, draw_heap, nullptr);
// A directly indexed root needs its sampler heap set before the root signature; another program keeps whatever
// sampler heap is bound.
ID3D12DescriptorHeap* const samplers = tr->sampler_heap_bytes ? pass.sampler_heap.Get() : nullptr;
if (!pass.heap_set || (samplers && pass.bound_sampler_heap != samplers)) {
x2w_startup::bind_translated_heaps(list, draw_heap, samplers);
pass.heap_set = true;
pass.bound_sampler_heap = nullptr;
pass.bound_sampler_heap = samplers;
}
list->SetPipelineState(tr->pipeline.Get());
list->SetComputeRootSignature(tr->root.Get());
Expand Down
Loading
Loading