Skip to content
71 changes: 71 additions & 0 deletions docs/experiments/compute-typed-buffer-inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Published typed buffer inputs (`srv <i> typed`)

Part of #3224. The direct compiler reads `buffer_load_format_x` to `_xyzw` (MUBUF, element index only) as a
`Buffer<uint4>` (`typed_buffer_srv`). `format_compute_runtime_manifest` now publishes it as
`srv <register> typed table <parameter> <byte> bytes <n>`, where `n` is 4 bytes per component of the widest load
of that binding.

The stand-in (`src/startup_stubs/d3d12_compute_dispatch.inl`) resolves the table entry with the existing input
range rules and binds a UINT buffer view of the title's 32-bit components: `R32_UINT` to `R32G32B32A32_UINT`. A
UINT view returns the same bits as the guest format unit only for 32-bit UINT, SINT and FLOAT components, so only
those are bound:

- **Hardware descriptor:** FORMAT 20-22, 62-64, 72-74 or 75-77 (gfx10 `BUF_FMT_32*`), stride equal to the
element size, identity DST_SEL over the components read, unified FORMAT (RESOURCE_LEVEL 1), structured bounds
(OOB_SELECT 0), and no swizzle, index stride or ADD_TID.
- **Slot 18 view token:** a typed buffer view with 32-bit channels and the default component mapping, read through
the UINT format of the same width (`typed_dword_channels` / `typed_dword_uint_format` in
`src/startup_stubs/native_buffer_stride.h`). The token is read with `token_view`, so content tokens
(`X2W_D3D12_VIEW_CONTENT_TOKENS=1`) are decoded as the other buffer readers decode them.
- **All four words zero:** a null buffer that reads zeros, as for raw inputs.

Each other shape is refused before a dispatch is recorded, with one of these reasons:

- `a typed input FORMAT is not 32 bits per channel`
- `a typed input stride is not its element size`
- `a typed input DST_SEL is not the identity`
- `a typed input descriptor is swizzled or not bounds-checked by record`
- `a typed input has fewer channels than the program reads`
- `a typed input token is not a typed buffer view with 32-bit channels`
- `a typed input range does not start at a whole element`

The range checks of raw inputs also apply. A typed input on an indirect Dispatch is refused. 8- and 16-bit
formats, UNORM, SNORM and 16-bit FLOAT need a conversion that a UINT view does not do.

This binding is on the manifest route only, which a default run does not take. Since #3449 the launchers set
`X2W_NATIVE_SHADERS_ONLY=1`, and `load_compute_translation` then returns before it reads `<key>.txt`. The
in-process route (`translate_compute_natively`) is unchanged and still refuses `typed_buffer_srv` with
`view kind 1 is not bound for a natively translated program`. The `srv typed` line is bound only in runs with
`X2W_NATIVE_SHADERS_ONLY=0` and `compile_dxil` manifests.

## Test

`tests/startup_compute_typed_buffer_srv_check.py` publishes a source-built program for 1 to 4 components with
`compile_dxil --requests`. It then dispatches it through slot 14 with a 32-bit FLOAT descriptor of 50 elements.
The 384 lanes store what they read, so the lanes past element 49 must read zeros. The 4-component case also reads
through a slot 18 `R32G32B32A32_FLOAT` view token (consumer mode `dispatch-srv-token-typed`). Each width also
reads through its UINT and SINT FORMATs, and through a descriptor based one element into the buffer (consumer mode
`dispatch-records32-input-at-<bytes>`), which shifts the words read by one element, and through an all-zero
descriptor (consumer mode `dispatch-records32-input-null`), which reads zeros. Each invalid shape above must be
refused with the output untouched, including a base 4 bytes in for 2 or more components, word 1 bit 30 or 31, and
word 3 INDEX_STRIDE or ADD_TID. Before publication the dispatch reports `no translation`.

`tests/native_compute_manifest_reasons.cpp` checks that a `typed_buffer_srv` wider than 16 bytes or not whole
32-bit components is refused as `SRV kind typed_buffer_srv`. `tests/startup_slot67_indirect_dispatch_check.py`
replaces its manifest line with `srv 0 typed table 1 0 bytes 4` and expects the indirect Dispatch to be refused.

```powershell
ctest --preset dev --no-tests=error -R '^startup_compute_typed_buffer_srv_' -V
```

Measured at `main` `e3f9a64f` plus this change, on Windows 11 build 26300 with the Windows SDK 10.0.26100 DXC,
Debug, with the D3D12 debug layer enabled:

- **NVIDIA GeForce RTX 4090, driver 32.0.16.1088:** passed for 1, 2, 3 and 4 components, the token, the null
descriptor, and 8, 11, 11 and 12 refusal controls. `native_compute_manifest_reasons` and
`startup_slot67_indirect_dispatch_hardware` passed.
- **WARP 10.0.26100.9818:** capability skip (77). WARP reports 4-lane waves and the direct module needs 32.
- **Negative control:** with the runtime's parse of the `typed` line disabled, the published manifest is not
bound and the hardware check reads the sentinel at word 0.

Not measured: how many of the cohort's typed inputs have a 32-bit FORMAT. The census re-measure answers that.
5 changes: 5 additions & 0 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,11 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
if (b.requires_identity_dst_sel)
srv_lines << " identity_dst_sel";
srv_lines << '\n';
} else if (b.kind == ComputeViewKind::typed_buffer_srv && b.array_count == 1 && b.required_bytes &&
b.required_bytes <= 16 && b.required_bytes % 4 == 0) {
// buffer_load_format_*: read as Buffer<uint4>; bytes is the widest load (4 per component).
srv_lines << "srv " << b.shader_register << " typed table " << b.root_parameter << ' ' << b.byte_offset
<< " bytes " << b.required_bytes << '\n';
} else if (b.kind == ComputeViewKind::texture_srv && b.array_count == 1 &&
(b.image_dimension == 0 || b.image_dimension == 1 || b.image_dimension == 2 ||
b.image_dimension == 5)) {
Expand Down
82 changes: 80 additions & 2 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// <key>.cs.cso. Bound from the compute tables recorded by slot 35 and compute root SRVs recorded by slot 43: raw
// buffers (`srv <i> buf table <param> <byte>` and `srv <i> buf root <param>` inputs, `uav <i> table <param> <byte>`
// outputs, `uav <i> bufhalf table <param> <byte> bytes <stride>` for typed half stores, or
// `uav <i> packed_d16_x table <param> <byte>` for a one-component packed D16 store)
// `uav <i> packed_d16_x table <param> <byte>` for a one-component packed D16 store; `srv <i> typed table
// <param> <byte> bytes <n>` inputs are read through a UINT view of 32-bit components)
// 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). An indexed buffer atomic
Expand Down Expand Up @@ -49,6 +50,7 @@ struct ComputeTranslation {
unsigned channels = 4;
D3D12_SRV_DIMENSION srv_dimension = D3D12_SRV_DIMENSION_UNKNOWN;
unsigned half_buffer_bytes = 0;
unsigned typed_bytes = 0; // `srv <i> typed`: the widest formatted load, 4 bytes per component
};
u64 key = 0;
int state = 0; // 0 not loaded, 1 ready, 2 unavailable
Expand Down Expand Up @@ -149,6 +151,62 @@ bool half_buffer_descriptor_matches(const std::uint32_t* words, unsigned bytes)
return (words[1] >> 16) == bytes && (words[3] & (0xfffff000u | selectors)) ==
(0x01000000u | (format << 12) | (0xfacu & selectors));
}
// Channels of a gfx10 unified buffer FORMAT with 32-bit components (UINT, SINT or FLOAT; tools/startup/gfx2hlsl.py
// names the codes): a UINT view of that width reads the bits buffer_load_format returns. 0 for any other FORMAT.
unsigned typed_dword_format_channels(std::uint32_t code)
{
switch (code) {
case 20: case 21: case 22: return 1;
case 62: case 63: case 64: return 2;
case 72: case 73: case 74: return 3;
case 75: case 76: case 77: return 4;
default: return 0;
}
}

// The view a translated buffer_load_format_* reads (`srv <i> typed`, a Buffer<uint4>): the title's descriptor
// resolved as a buffer range, retyped to the UINT format of its 32-bit components. Returns the refusal or null.
const char* typed_dword_input_view(Wrapper* self, const std::uint32_t* words, unsigned typed_bytes,
IndirectBufferView& range, D3D12_SHADER_RESOURCE_VIEW_DESC& view)
{
UINT channels = 0;
if (words[0] == view_token_magic) {
ViewRecord record{};
const bool known = token_view(words, record);
D3D12_SHADER_RESOURCE_VIEW_DESC desc{};
if (known && record.slot == 18 && record.desc_bytes >= ViewRecord::srv_desc_bytes)
std::memcpy(&desc, record.desc, ViewRecord::srv_desc_bytes);
if (desc.Format) desc.Format = desktop_format(desc.Format);
channels = x2w_startup::typed_dword_channels(desc);
if (!channels) return "a typed input token is not a typed buffer view with 32-bit channels";
} else if (words[0] || words[1] || words[2] || words[3]) {
// An all-zero descriptor is a null buffer: indirect_table_buffer gives it zeros, read as the program's width.
channels = typed_dword_format_channels(x2w_startup::formatted_buffer_code(words[3]));
if (!channels) return "a typed input FORMAT is not 32 bits per channel";
if (((words[1] >> 16) & 0x3fff) != channels * 4) return "a typed input stride is not its element size";
for (unsigned c = 0; c < typed_bytes / 4; ++c)
if (((words[3] >> (3 * c)) & 7) != 4 + c) return "a typed input DST_SEL is not the identity";
// Unified FORMAT (RESOURCE_LEVEL 1), structured bounds (OOB_SELECT 0), no swizzle, index stride or ADD_TID.
if ((words[1] >> 30) || (words[3] & 0xfff80000u) != 0x01000000u)
return "a typed input descriptor is swizzled or not bounds-checked by record";
} else {
channels = 4; // the 16 zero bytes indirect_table_buffer gives a null buffer, one element
}
if (channels * 4 < typed_bytes) return "a typed input has fewer channels than the program reads";
const char* reason = nullptr;
range = indirect_table_buffer(self, words, false, &reason, 16u << 20, 4, true);
if (reason) return reason;
const UINT element = channels * 4;
if (range.offset % element || range.bytes < element) return "a typed input range does not start at a whole element";
view = {};
view.Format = x2w_startup::typed_dword_uint_format(channels);
view.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
view.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
view.Buffer.FirstElement = range.offset / element;
view.Buffer.NumElements = static_cast<UINT>(range.bytes / element);
return nullptr;
}

std::unordered_map<u64, ComputeTranslation>& compute_translations() { static std::unordered_map<u64, ComputeTranslation> m; return m; }
LONG compute_dispatches_translated, compute_dispatches_refused, compute_dispatches_untranslated, compute_dispatches_unmodelled;
LONG compute_inputs_gpu_copied, compute_inputs_snapshot, compute_inputs_snapshot_kib; // inputs by path: this list's GPU copy, a title snapshot
Expand Down Expand Up @@ -278,6 +336,11 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.srvs.push_back({a, b});
tr.srvs.back().requires_identity_dst_sel = true;
}
else if (std::sscanf(line.c_str(), "srv %u typed table %d %d bytes %u%n", &i, &a, &b, &x, &used) == 4 && whole() &&
i == tr.srvs.size() && x && x <= 16 && x % 4 == 0) {
tr.srvs.push_back({a, b});
tr.srvs.back().typed_bytes = x;
}
else if (std::sscanf(line.c_str(), "srv %u buf root %d%n", &i, &a, &used) == 2 && whole() && a >= 0 &&
i == tr.srvs.size()) {
tr.srvs.push_back({a, 0});
Expand Down Expand Up @@ -739,7 +802,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
for (std::size_t i = 0; i < count && !reason; ++i) {
const bool output = i >= inputs;
const ComputeTranslation::Slot slot = output ? tr->uavs[i - inputs] : tr->srvs[i];
if (dispatch.signature && (slot.heap || slot.texture || slot.root)) {
if (dispatch.signature && (slot.heap || slot.texture || slot.root || slot.typed_bytes)) {
reason = "translated indirect Dispatch requires fixed-table raw buffers";
break;
}
Expand Down Expand Up @@ -922,6 +985,16 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
views[i].wrapper = wrapper;
continue;
}
if (!output && slot.typed_bytes) {
IndirectBufferView range{};
D3D12_SHADER_RESOURCE_VIEW_DESC typed{};
reason = typed_dword_input_view(self, words, slot.typed_bytes, range, typed);
views[i] = {range.resource, range.offset, range.bytes, !range.upload, false, true};
views[i].desc.srv = typed;
views[i].stride = range.stride;
std::memcpy(views[i].words, words, sizeof(views[i].words));
continue;
}
if (!output) {
// Each condition of a complete GPU-backed buffer range has its own reason.
auto view = indirect_table_buffer(self, words, false, &reason, 16u << 20, D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT, true,
Expand Down Expand Up @@ -1192,6 +1265,11 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
view.described ? &view.desc.srv : nullptr, dest);
continue;
}
if (i < inputs && view.described) { // a typed buffer input
if (view.transition) require_state(self, view.resource, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
device_of(the_device)->CreateShaderResourceView(view.resource, &view.desc.srv, dest);
continue;
}
if (i < inputs || i >= count) {
if (view.transition) require_state(self, view.resource, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
buffer_view(dest, view.resource, view.offset, static_cast<std::size_t>(view.bytes));
Expand Down
18 changes: 18 additions & 0 deletions tests/cmake/startup_compute_typed_buffer_srv.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Typed buffer inputs (buffer_load_format_*) on the published compute route (#3224).
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_typed_buffer_srv_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_typed_buffer_srv_check.py" compile
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(startup_compute_typed_buffer_srv_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_typed_buffer_srv_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_typed_buffer_srv_check.py"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:startup_stream_consumer>" "$<TARGET_FILE:offscreen_check>"
"${DXC_BIN_DIR}" ${adapter})
set_tests_properties(startup_compute_typed_buffer_srv_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77)
endforeach()
endif()
8 changes: 8 additions & 0 deletions tests/native_compute_manifest_reasons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ int main() {
p.bindings[0].kind = K::texture_srv;
p.bindings[0].array_count = 4;
}},
{"SRV kind typed_buffer_srv", [](auto &p) { // wider than four 32-bit components
p.bindings[0].kind = K::typed_buffer_srv;
p.bindings[0].required_bytes = 20;
}},
{"SRV kind typed_buffer_srv", [](auto &p) { // not whole 32-bit components
p.bindings[0].kind = K::typed_buffer_srv;
p.bindings[0].required_bytes = 6;
}},
{"SRV kind cube_comparison_srv cube", [](auto &p) {
p.bindings[0].kind = K::cube_comparison_srv;
p.bindings[0].image_dimension = 3;
Expand Down
Loading
Loading