Skip to content
Open
111 changes: 111 additions & 0 deletions docs/experiments/compute-typed-buffer-inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 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`).
- **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, and the in-process
`X2W_NATIVE_SHADERS` route is unchanged. 8- and 16-bit formats, UNORM, SNORM and 16-bit FLOAT need a conversion
that a UINT view does not do.

## 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. Each invalid shape above must
be refused with the output untouched, including a base 4 bytes in for 2 or more components and word 1 bit 30. Before publication the dispatch reports `no translation`.

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

Measured at `d418f70e` 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, and 4, 6, 6
and 7 refusal controls.
- **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.

# Published typed buffer outputs (`uav <i> typed`)

`buffer_store_format_x` to `_xyzw` (MUBUF, element index only) is an `RWBuffer<uint4>` (`typed_buffer_uav`) in the
direct compiler. It is published as `uav <register> typed table <parameter> <byte> bytes <n>`, where `n` is 4 bytes
per stored component. One slot has one store width; the compiler refuses mixed widths.

The stand-in binds a UINT typed UAV of the title's 32-bit components whose channel count equals the stored
components:

- **Hardware descriptor:** the output path of the published D16 stores (#3302) with the typed element width. The
descriptor checks of the inputs above (FORMAT, stride, DST_SEL, bounds), the same channel count as the store,
and every output placement reason of #3229 apply.
- **Slot 19 view token:** `native_buffer_store_view` (`src/native_buffer_store_view.h`), the in-process native
route's typed-store contract: a buffer UAV without counter, structure stride or flags, a 32-bit UINT, SINT or
FLOAT format of the stored width, retyped to UINT and inside its buffer.
- **Typed UAV stores** must be supported for the format (`D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE`). R32G32B32
formats have no typed UAV, so a 3-component store is refused on the adapters measured below. One helper checks
this for these outputs and the half-float ones.

The refusal reasons are `a typed output FORMAT is not 32 bits per channel`,
`a typed output stride is not its element size`, `a typed output DST_SEL is not the identity`,
`a typed output descriptor is swizzled or not bounds-checked by record`,
`a typed output FORMAT has another width than the stored components`,
`an output typed buffer range is over 16 MiB`, `an output typed buffer is not element-aligned in its resource`,
`an output typed buffer format does not support typed UAV stores` and
`a typed output token is not a typed buffer UAV of the stored width`.

## Test

`tests/startup_compute_typed_buffer_uav_check.py` publishes a source-built store of 1 to 4 components. Each of the
384 lanes stores words computed from its element index through a 32-bit FLOAT descriptor of 50 elements, so only
those elements change. The 1-component case also stores through the consumer's slot 19 `R32_UINT` token (312
elements from element 4), and a structured token is refused. Each stored width also writes through its UINT and
SINT FORMATs, and through a descriptor based 8 bytes into the output (`dispatch-records32-output-offset`): 1 and 2
components store shifted by that much, and 3 and 4 components are refused because 8 bytes is not a whole element.
The invalid descriptors above, and word 1 bit 30, are refused with the output untouched, and before publication
the dispatch reports `no translation`.

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

Measured on the same machine as above, stacked on the typed input commits:

- **NVIDIA GeForce RTX 4090, driver 32.0.16.1088:** passed. 1, 2 and 4 components stored 50 elements, the token
stored 312, and 6 refusal controls per width passed. The 3-component store was refused with
`an output typed buffer format does not support typed UAV stores`.
- **WARP:** capability skip (77), as above.
- **Negative control:** with the parse of the `uav typed` line disabled, the hardware check reads the sentinel at
word 0.
- **Regressions:** the published D16 stores (2 and 4 channels) and the output reason check (with #3394's
expectation) also passed on the RTX 4090.
10 changes: 10 additions & 0 deletions src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,11 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
b.kind == ComputeViewKind::byte_window_srv) {
srv_lines << "srv " << b.shader_register << " buf table " << b.root_parameter << ' ' << b.byte_offset
<< '\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 All @@ -979,6 +984,11 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
(b.required_bytes == 2 || b.required_bytes == 4 || b.required_bytes == 8)) {
uav_lines << "uav " << b.shader_register << " bufhalf table " << b.root_parameter << ' '
<< b.byte_offset << " bytes " << b.required_bytes << '\n';
} else if (b.kind == ComputeViewKind::typed_buffer_uav && b.array_count == 1 && b.required_bytes &&
b.required_bytes <= 16 && b.required_bytes % 4 == 0) {
// buffer_store_format_*: an RWBuffer<uint4>; bytes is the stored width (4 per component).
uav_lines << "uav " << b.shader_register << " typed table " << b.root_parameter << ' ' << b.byte_offset
<< " bytes " << b.required_bytes << '\n';
} else if (b.kind == ComputeViewKind::uint_texture_uav && b.image_dimension == 1 && b.array_count == 1) {
uav_lines << "uav " << b.shader_register << " tex2d uint table " << b.root_parameter << ' '
<< b.byte_offset << " channels 1\n";
Expand Down
Loading
Loading