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
56 changes: 56 additions & 0 deletions docs/experiments/compute-typed-buffer-inputs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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 invalid shape
above must be refused with the output untouched. 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.
5 changes: 5 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 Down
85 changes: 83 additions & 2 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// the key of its published request; gfx2hlsl.py --pending answers the request with <key>.txt (`stage cs`) and
// <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, or `uav <i> bufhalf table <param> <byte> bytes <stride>` for typed half stores)
// outputs, or `uav <i> bufhalf table <param> <byte> bytes <stride>` for typed half stores; `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 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
Expand Down Expand Up @@ -44,6 +45,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 @@ -101,6 +103,65 @@ 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{};
AcquireSRWLockShared(&note_lock);
const bool known = words[1] < view_records().size();
if (known) record = view_records()[words[1]];
ReleaseSRWLockShared(&note_lock);
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 @@ -202,6 +263,11 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.uds.push_back({a, 0, false, false, -1, 0, 0, 0, source()});
else if (std::sscanf(line.c_str(), "srv %u buf table %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.srvs.size())
tr.srvs.push_back({a, b});
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 @@ -605,7 +671,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 @@ -772,6 +838,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 @@ -992,6 +1068,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()
Loading
Loading