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
70 changes: 70 additions & 0 deletions docs/experiments/compute-indexed-atomic-uav.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Indexed atomic UAVs in the direct compute runtime path

The direct gfx1013 compute translator lowers an indexed `buffer_atomic_*`
(`idxen`) to an `atomicBinOp`/`atomicCompareExchange` on a raw buffer UAV,
reading the view's `{stride, count}` from the constant buffer at
`b(uav register + 1)` and testing the lane index against the count before
multiplying by the stride. That binding, `ComputeViewKind::indexed_atomic_uav`,
previously had no runtime manifest, so `compile_dxil` reported
`compiled, no runtime manifest: UAV kind indexed_atomic_uav` and the dispatch
path never bound it.

## Contract

`format_compute_runtime_manifest` publishes the view only as a single
4-byte view:

```text
uav <register> atomic table <parameter> <byte>
```

Any other shape (a descriptor array, or a `required_bytes` other than 4)
stays refused by name as `UAV kind indexed_atomic_uav`.

The startup dispatch path (`src/startup_stubs/d3d12_compute_dispatch.inl`)
parses that line and builds a root signature with one extra 32-bit-constants
parameter per indexed atomic UAV, after the view table and in UAV order,
with `ShaderRegister = uav register + 1` and two values — the same layout
`NativeComputePipeline` builds. The in-process native translation classifies
the binding as an output the same way, so a program translated in-process
binds identically.

Binding is token-only: the output must be a slot-19 token view. At dispatch
time the shared `native_buffer_store_view` check derives `{stride, count}`
from that view — structured views need a stride that is a multiple of 4 and
at most 2048 bytes, raw views need `R32_TYPELESS` with no stride, typed views
need `R32_UINT` or `R32_SINT` — and rewrites it to the raw `R32_TYPELESS`
view the atomic operates on. A view that fails that check is refused as
`an indexed atomic output view has an unsupported stride or range`. An
indexed atomic output bound from a hardware descriptor instead of a token is
refused as `an indexed atomic output needs a slot-19 token view`. Multiple
indexed atomic UAVs each get their own root-constants parameter; the
manifest's existing caps (8 UAVs, 32 user-data words) keep the root
signature inside the root-cost budget.

## Verification

`startup_compute_indexed_atomic_uav_controls` publishes the synthetic
program through `compile_dxil` and checks the manifest line, with no
`.cs.hlsl` sidecar. `startup_compute_indexed_atomic_uav_warp` and
`_hardware` dispatch it through the stand-in's command list: every lane
adds 1 to the indexed counter at byte index `4 * lane` over
`Dispatch(3, 2, 2)`, so each addressed dword gains 12. Raw, structured
stride-8 and typed `R32_UINT` token views are checked, each with its own
addressed dwords, plus four token shapes and a hardware descriptor that must
each refuse with their own reason and leave the output untouched, and a
native in-process translation of the same program. Every negative-control
view is creatable in D3D12 on its buffer: the 4096-stride view is created on
the 16 MiB + 64 KiB committed buffer, since a view D3D12 rejects at creation
removes the device in slot 19 before the dispatch can refuse it. The
published path needs 32-lane waves too: the published DXIL declares
WaveSize(32), so `CreateComputePipelineState` fails on 4-lane WARP, and
the check probes `--shader-wave32` first and exits 77 without them. The
pure-Python controls (program encoding, table loads, `idxen` atomic,
per-shape expectations, token views, and that every negative view fits its
buffer) run without Windows.

```powershell
cmake --build --preset dev --target compile_dxil startup_stream_consumer offscreen_check
ctest --preset dev --no-tests=error -R '^startup_compute_indexed_atomic_uav_(controls|warp|hardware)$' --output-on-failure
```
7 changes: 7 additions & 0 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,13 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
const char *dimension = b.image_dimension == 2 ? "tex3d" : b.image_dimension == 5 ? "tex2da" : "tex2d";
uav_lines << "uav " << b.shader_register << ' ' << dimension << " float table " << b.root_parameter
<< ' ' << b.byte_offset << " channels " << channels << '\n';
} else if (b.kind == ComputeViewKind::indexed_atomic_uav && b.array_count == 1 &&
b.required_bytes == 4) {
// One indexed atomic UAV: the runtime binds it from a slot-19 token view and fills the
// {stride, count} root constants the translated program reads at b(register + 1). Any other
// shape stays refused by name below.
uav_lines << "uav " << b.shader_register << " atomic table " << b.root_parameter << ' '
<< b.byte_offset << '\n';
} else if ((b.kind == ComputeViewKind::texture_uav || b.kind == ComputeViewKind::half4_texture_uav) &&
b.array_count == 1) {
const char *tex;
Expand Down
56 changes: 52 additions & 4 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
// `uav <i> packed_d16_x table <param> <byte>` for a one-component packed D16 store)
// named by hardware buffer descriptors or 64-bit root SRV addresses, and textures
// (`srv <i> tex1d|tex2d|tex2da|tex3d|texcubearr table <param> <byte>` inputs, `uav <i> tex1d/tex2d/tex2da/tex3d float table <param> <byte> channels <n>` outputs) named
// by the token of a view the title created (device slots 18 and 19). `smp`, `smpl` and `smpc` lines become static samplers. Entry
// by the token of a view the title created (device slots 18 and 19). An indexed buffer atomic
// (`uav <i> atomic table <param> <byte>`) is bound only from a slot-19 token view: the view's {stride, count}
// are bound as root constants at b(u + 1), matching the native compute pipeline. `smp`, `smpl` and `smpc` lines become static samplers. Entry
// values come from the compute root constants of slots 37 and 39, or from the descriptor a buffer input or output is
// bound from: its stride and word 3 (`stride`, `descword3`), and word 3, the upper half of word 1 and the record
// count of an output (`uav_descword3`, `uav_stride`, `uav_numrecords`). Any other manifest line makes the translation
Expand Down Expand Up @@ -43,6 +45,7 @@ struct ComputeTranslation {
bool packed_d16_x = false;
unsigned scalar_window = 0;
bool atomic_uint_texture = false;
bool atomic = false; // an indexed atomic UAV: {stride, count} root constants at b(u + 1)
unsigned channels = 4;
D3D12_SRV_DIMENSION srv_dimension = D3D12_SRV_DIMENSION_UNKNOWN;
unsigned half_buffer_bytes = 0;
Expand Down Expand Up @@ -171,8 +174,13 @@ bool adapter_reports_shader_model_below_6_6()
const char* create_compute_pipeline(ComputeTranslation& tr, const void* code, std::size_t bytes)
{
// Parameter 0 keeps one constant for a program without entry values: a constants parameter cannot be empty.
// One 32-bit-constants parameter per indexed atomic UAV follows the view table, in UAV order.
std::vector<UINT> atomic_registers;
for (UINT i = 0; i < tr.uavs.size(); ++i)
if (tr.uavs[i].atomic) atomic_registers.push_back(i);
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>(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";
D3D12_COMPUTE_PIPELINE_STATE_DESC desc{};
desc.pRootSignature = tr.root.Get();
Expand Down Expand Up @@ -286,6 +294,11 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.srvs.push_back({c, d, false, true, a, b, x, y});
else if (std::sscanf(line.c_str(), "uav %u table %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.uavs.size())
tr.uavs.push_back({a, b});
else if (std::sscanf(line.c_str(), "uav %u atomic table %d %d%n", &i, &a, &b, &used) == 3 && whole() &&
i == tr.uavs.size()) {
tr.uavs.push_back({a, b});
tr.uavs.back().atomic = true;
}
else if (std::sscanf(line.c_str(), "uav %u bufhalf table %d %d bytes %u%n", &i, &a, &b, &x, &used) == 4 &&
whole() && i == tr.uavs.size() && x2w::native_half_buffer_format(x) != DXGI_FORMAT_UNKNOWN) {
tr.uavs.push_back({a, b});
Expand Down Expand Up @@ -427,7 +440,8 @@ bool translate_compute_natively(ComputeTranslation& tr)
// A comparison sample reads its 2D texture through the same kind of view as a plain one.
const bool texture_input = (view.kind == Kind::texture_srv || view.kind == Kind::comparison_texture_srv) &&
view.image_dimension == 1;
const bool output = texture_output || view.kind == Kind::raw_buffer_uav || half_typed;
const bool atomic = view.kind == Kind::indexed_atomic_uav;
const bool output = texture_output || view.kind == Kind::raw_buffer_uav || half_typed || atomic;
auto dimension = D3D12_UAV_DIMENSION_UNKNOWN;
if (texture_output) {
switch (view.image_dimension) {
Expand All @@ -439,7 +453,7 @@ bool translate_compute_natively(ComputeTranslation& tr)
}
auto& slots = output ? tr.uavs : tr.srvs;
const bool supported = view.kind == Kind::raw_buffer_uav || half_typed || view.kind == Kind::raw_vector_srv ||
x2w::accepts_cbv_window(view.kind) || texture_input ||
x2w::accepts_cbv_window(view.kind) || texture_input || atomic ||
dimension != D3D12_UAV_DIMENSION_UNKNOWN;
if (!supported || view.array_count != 1 ||
view.root_parameter >= 16 || view.shader_register != slots.size())
Expand All @@ -454,6 +468,7 @@ bool translate_compute_natively(ComputeTranslation& tr)
slot.channels = channels;
slot.half_typed_bytes = half_typed ? view.required_bytes : 0;
slot.packed_d16_x = packed_x;
slot.atomic = atomic;
// An MTBUF byte window needs identity channel order, as on the published route (identity_dst_sel).
slot.requires_identity_dst_sel = view.requires_identity_dst_sel;
if (x2w::accepts_cbv_window(view.kind))
Expand Down Expand Up @@ -679,6 +694,9 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
Wrapper* wrapper;
std::uint32_t words[8]; // a buffer's descriptor words as it was bound from (a content token is 8), and its stride
UINT stride;
// An indexed atomic UAV: the {stride, count} root constants bound at parameter 2 + its UAV index.
UINT atomic_stride = 0, atomic_count = 0;
bool atomic = false;
};
static_assert(sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC) >= ViewRecord::srv_desc_bytes);
static_assert(sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC) >= ViewRecord::uav_desc_bytes);
Expand Down Expand Up @@ -975,6 +993,17 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
if (views[input].resource == token.resource) reason = "an output buffer is also an input";
if (!reason && !retain_with_recording(self, x2w_startup::ComPtr<ID3D12Resource>(token.resource)))
reason = "an output token's backing could not be retained with the recording";
// An indexed atomic UAV reads its {stride, count} from root constants at b(u + 1). The shared
// store-view check derives them from the token's view and rewrites it to the raw R32 view the
// atomic operates on; any other shape is refused by that check's name.
x2w::IndexedAtomicView atomic_shape{};
D3D12_UNORDERED_ACCESS_VIEW_DESC atomic_view{};
if (!reason && slot.atomic) {
const x2w::ComputeViewBinding binding{x2w::ComputeViewKind::indexed_atomic_uav, 0, 0, 0, 4};
atomic_view = token.view;
if (!x2w::native_buffer_store_view(binding, atomic_view, token.backing.Width, &atomic_shape))
reason = "an indexed atomic output view has an unsupported stride or range";
}
if (!tr->logged_token_output || (!reason && !tr->logged_bound_token_output)) {
tr->logged_token_output = true;
if (!reason) tr->logged_bound_token_output = true;
Expand All @@ -992,8 +1021,19 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
views[i].described = true;
views[i].desc.uav = token.view;
}
if (!reason && slot.atomic) {
views[i].described = true;
views[i].desc.uav = atomic_view;
views[i].atomic = true;
views[i].atomic_stride = atomic_shape.stride;
views[i].atomic_count = atomic_shape.count;
}
continue;
}
if (slot.atomic) {
reason = "an indexed atomic output needs a slot-19 token view";
break;
}
if (slot.half_typed_bytes) {
reason = "formatted D16 write needs the original typed buffer view";
break;
Expand Down Expand Up @@ -1180,6 +1220,14 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
list->SetComputeRootSignature(tr->root.Get());
if (!tr->uds.empty()) list->SetComputeRoot32BitConstants(0, static_cast<UINT>(tr->uds.size()), values, 0);
if (n) list->SetComputeRootDescriptorTable(1, D3D12_GPU_DESCRIPTOR_HANDLE{draw_heap->GetGPUDescriptorHandleForHeapStart().ptr + u64{first} * draw_heap_increment});
// Indexed atomic UAVs: one {stride, count} pair per atomic output, at parameter 2 + its UAV index,
// matching the root signature create_compute_pipeline built.
UINT atomic_parameter = 2;
for (std::size_t i = inputs; i < count; ++i) {
if (!views[i].atomic) continue;
const UINT pair[2] = {views[i].atomic_stride, views[i].atomic_count};
list->SetComputeRoot32BitConstants(atomic_parameter++, 2, pair, 0);
}
if (dispatch.signature) {
require_state(self, dispatch.argument, D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
list->ExecuteIndirect(dispatch.signature, 1, dispatch.argument, dispatch.offset, nullptr, 0);
Expand Down
15 changes: 13 additions & 2 deletions src/startup_stubs/translated_sampler_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,15 @@ inline std::vector<D3D12_STATIC_SAMPLER_DESC> decode_statics(const TranslatedSam
inline UINT sampler_root_parameter(UINT views) { return views ? 2u : 1u; }

// Parameter 1, when there are views, is one table: SRVs t0.. first, then UAVs u0.., then bounded resheap SRVs (space 2).
// `atomic` names the UAV registers (one per indexed atomic UAV, in UAV order) that each get a 32-bit-constants
// parameter after the view table: the translated program reads the view's {stride, count} from b(register + 1),
// as NativeComputePipeline binds it. The sampler-heap table, when present, follows those parameters.
inline HRESULT create_translated_root(ID3D12Device* device, UINT constants, UINT srvs,
const TranslatedSamplers& samplers, ID3D12RootSignature** out, UINT uavs = 0,
bool input_assembler = false, UINT heap_srvs = 0)
bool input_assembler = false, UINT heap_srvs = 0, UINT atomic = 0,
const UINT *atomic_registers = nullptr)
{
D3D12_ROOT_PARAMETER parameters[3]{};
D3D12_ROOT_PARAMETER parameters[11]{};
parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
parameters[0].Constants.Num32BitValues = constants;
// A compute program's outputs (u0..) follow its resources in the same table; bounded heap SRVs follow in space 2.
Expand All @@ -139,6 +143,13 @@ inline HRESULT create_translated_root(ID3D12Device* device, UINT constants, UINT
const D3D12_DESCRIPTOR_RANGE sampler_range{
D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, translated_sampler_count, 0, translated_sampler_space, 0};
UINT count = sampler_root_parameter(srvs + uavs + heap_srvs);
for (UINT i = 0; i < atomic; ++i) {
auto &param = parameters[count++];
param.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
param.Constants.ShaderRegister = atomic_registers[i] + 1;
param.Constants.RegisterSpace = 0;
param.Constants.Num32BitValues = 2;
}
if (samplers.heap) {
parameters[count].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
parameters[count].DescriptorTable = {1, &sampler_range};
Expand Down
17 changes: 17 additions & 0 deletions tests/cmake/startup_compute_indexed_atomic_uav.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if(TARGET compile_dxil AND EXISTS "${INTAKE_PYTHON}"
AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME startup_compute_indexed_atomic_uav_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_indexed_atomic_uav_check.py" compile
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(startup_compute_indexed_atomic_uav_controls PROPERTIES TIMEOUT 60)
endif()
if(TARGET compile_dxil AND TARGET startup_stream_consumer AND TARGET offscreen_check AND EXISTS "${INTAKE_PYTHON}"
AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_compute_indexed_atomic_uav_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_indexed_atomic_uav_check.py"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:startup_stream_consumer>" "$<TARGET_FILE:offscreen_check>"
"${DXC_BIN_DIR}" ${adapter})
set_tests_properties(startup_compute_indexed_atomic_uav_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77)
endforeach()
endif()
26 changes: 25 additions & 1 deletion tests/native_compute_manifest_reasons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ int main() {
p.bindings[0].image_dimension = 1;
}},
{"UAV kind typed_buffer_uav", [](auto &p) { p.bindings[1].kind = K::typed_buffer_uav; }},
{"UAV kind indexed_atomic_uav array", [](auto &p) {
p.bindings[1].kind = K::indexed_atomic_uav;
p.bindings[1].array_count = 2;
}},
{"UAV kind indexed_atomic_uav", [](auto &p) {
p.bindings[1].kind = K::indexed_atomic_uav;
p.bindings[1].required_bytes = 8;
}},
{"UAV kind packed_d16_x_uav", [](auto &p) {
p.bindings[1].kind = K::packed_d16_x_uav;
p.bindings[1].required_bytes = 4;
Expand Down Expand Up @@ -144,6 +152,22 @@ int main() {
std::printf(" | got: %s\n", manifest ? manifest->c_str() : reason.c_str());
failures += !ok;
}
// An indexed atomic UAV is published only as a single 4-byte view.
{
auto program = base();
program.bindings[1].kind = K::indexed_atomic_uav;
program.bindings[1].required_bytes = 4;
reason = "unset";
const auto manifest = format_compute_runtime_manifest(program, &reason);
std::string wanted = expected;
wanted.replace(wanted.find("uav 0 table 1 32\n"), std::strlen("uav 0 table 1 32\n"),
"uav 0 atomic table 1 32\n");
const bool ok = manifest && *manifest == wanted && reason == "unset";
std::printf("%s publishes uav 0 atomic table 1 32\n", ok ? "ok " : "FAIL");
if (!ok)
std::printf(" | got: %s\n", manifest ? manifest->c_str() : reason.c_str());
failures += !ok;
}
{
auto program = base();
program.bindings[1].kind = K::packed_d16_x_uav;
Expand All @@ -159,6 +183,6 @@ int main() {
std::printf(" | got: %s\n", manifest ? manifest->c_str() : reason.c_str());
failures += !ok;
}
std::printf("%d of %zu manifest reasons wrong\n", failures, std::size(cases) + std::size(published) + 2);
std::printf("%d of %zu manifest reasons wrong\n", failures, std::size(cases) + std::size(published) + 3);
return failures ? 1 : 0;
}
Loading
Loading