diff --git a/docs/experiments/compute-typed-buffer-inputs.md b/docs/experiments/compute-typed-buffer-inputs.md new file mode 100644 index 000000000..3621005c9 --- /dev/null +++ b/docs/experiments/compute-typed-buffer-inputs.md @@ -0,0 +1,111 @@ +# Published typed buffer inputs (`srv typed`) + +Part of #3224. The direct compiler reads `buffer_load_format_x` to `_xyzw` (MUBUF, element index only) as a +`Buffer` (`typed_buffer_srv`). `format_compute_runtime_manifest` now publishes it as +`srv typed table bytes `, 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-`), 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 typed`) + +`buffer_store_format_x` to `_xyzw` (MUBUF, element index only) is an `RWBuffer` (`typed_buffer_uav`) in the +direct compiler. It is published as `uav typed table bytes `, 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. diff --git a/src/direct_compute_cache.cpp b/src/direct_compute_cache.cpp index 79dcc6b3c..52614fb03 100644 --- a/src/direct_compute_cache.cpp +++ b/src/direct_compute_cache.cpp @@ -957,6 +957,11 @@ std::optional 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; 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)) { @@ -979,6 +984,11 @@ std::optional 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; 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"; diff --git a/src/startup_stubs/d3d12_compute_dispatch.inl b/src/startup_stubs/d3d12_compute_dispatch.inl index ce571a0f8..f080b1c0e 100644 --- a/src/startup_stubs/d3d12_compute_dispatch.inl +++ b/src/startup_stubs/d3d12_compute_dispatch.inl @@ -2,7 +2,9 @@ // the key of its published request; gfx2hlsl.py --pending answers the request with .txt (`stage cs`) and // .cs.cso. Bound from the compute tables recorded by slot 35 and compute root SRVs recorded by slot 43: raw // buffers (`srv buf table ` and `srv buf root ` inputs, `uav table ` -// outputs, or `uav bufhalf table bytes ` for typed half stores) +// outputs, or `uav bufhalf table bytes ` for typed half stores; `srv typed table +// bytes ` inputs and `uav typed table bytes ` outputs use a UINT view of +// 32-bit components) // named by hardware buffer descriptors or 64-bit root SRV addresses, and textures // (`srv tex1d|tex2d|tex2da|tex3d table ` inputs, `uav tex1d/tex2d/tex2da/tex3d float table channels ` 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 @@ -44,6 +46,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 typed`: the widest formatted load, 4 bytes per component }; u64 key = 0; int state = 0; // 0 not loaded, 1 ready, 2 unavailable @@ -101,6 +104,88 @@ 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; + } +} + +// A hardware descriptor read (`srv typed`) or written (`uav typed`) through a UINT view of 32-bit +// components: FORMAT, stride, the selects of the components used and the bounds mode. Sets channels; returns the +// refusal or null. +const char* typed_dword_descriptor(const std::uint32_t* words, unsigned components, bool output, UINT& channels) +{ + static const char* const reasons[2][4] = { + {"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 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"}}; + const auto& reason = reasons[output]; + channels = typed_dword_format_channels(x2w_startup::formatted_buffer_code(words[3])); + if (!channels) return reason[0]; + if (((words[1] >> 16) & 0x3fff) != channels * 4) return reason[1]; + for (unsigned c = 0; c < components; ++c) + if (((words[3] >> (3 * c)) & 7) != 4 + c) return reason[2]; + // 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 reason[3]; + return nullptr; +} + +// Whether the device stores through a typed buffer UAV of this format (R32G32B32 has no typed UAV, for one). +bool typed_buffer_store_supported(DXGI_FORMAT format) +{ + D3D12_FEATURE_DATA_FORMAT_SUPPORT support{format}; + constexpr UINT required = D3D12_FORMAT_SUPPORT1_BUFFER | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW; + return SUCCEEDED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))) && + (support.Support1 & required) == required && (support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE); +} + +// The view a translated buffer_load_format_* reads (`srv typed`, a Buffer): 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(¬e_lock); + const bool known = words[1] < view_records().size(); + if (known) record = view_records()[words[1]]; + ReleaseSRWLockShared(¬e_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. + if (const char* reason = typed_dword_descriptor(words, typed_bytes / 4, false, channels)) return reason; + } 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(range.bytes / element); + return nullptr; +} + std::unordered_map& compute_translations() { static std::unordered_map 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 @@ -202,6 +287,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}); @@ -218,6 +308,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 typed table %d %d bytes %u%n", &i, &a, &b, &x, &used) == 4 && whole() && + i == tr.uavs.size() && x && x <= 16 && x % 4 == 0) { + tr.uavs.push_back({a, b}); + tr.uavs.back().typed_bytes = x; + } 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}); @@ -605,7 +700,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; } @@ -772,6 +867,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); @@ -820,6 +925,14 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& reason = "formatted D16 write has an incompatible typed buffer UAV"; } #endif + if (!reason && slot.typed_bytes) { // the native route's view contract for a typed store + const x2w::ComputeViewBinding binding{x2w::ComputeViewKind::typed_buffer_uav, 0, 0, 0, slot.typed_bytes}; + token.view.Format = desktop_format(token.view.Format); + if (!x2w::native_buffer_store_view(binding, token.view, token.backing.Width)) + reason = "a typed output token is not a typed buffer UAV of the stored width"; + else if (!typed_buffer_store_supported(token.view.Format)) + reason = "an output typed buffer format does not support typed UAV stores"; + } if (reason && token.placement == Placement::copied_buffer && trace_copied_compute_output(key, token.guest_resource, token.guest_base, token.offset, token.bytes)) { log("x2w-compute-output-watch key=%016llx groups=%u,%u,%u indirect=%u owner=0x%llx gpuva=0x%llx", @@ -849,7 +962,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& token.description_bytes, token.offset, token.bytes, reason ? reason : "bound"); } views[i] = {token.resource, token.offset, token.bytes, true}; - if (!reason && slot.half_typed_bytes) { + if (!reason && (slot.half_typed_bytes || slot.typed_bytes)) { views[i].described = true; views[i].desc.uav = token.view; } @@ -863,25 +976,34 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& const u64 address = words[0] | (u64{words[1] & 0xffff} << 32); const UINT stride = (words[1] >> 16) & 0x3fff; const unsigned half_bytes = slot.half_buffer_bytes; - const unsigned alignment = half_bytes ? half_bytes : D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT; + const unsigned typed_bytes = slot.typed_bytes; // `uav typed`: 4 bytes per stored component + const unsigned element = half_bytes ? half_bytes : typed_bytes; + const unsigned alignment = element ? element : D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT; const u64 bytes = u64{stride ? stride : 1} * words[2]; - const bool sized = words[0] != view_token_magic && bytes && (half_bytes || bytes % 4 == 0) && bytes <= (16u << 20); + const bool sized = words[0] != view_token_magic && bytes && (element || bytes % 4 == 0) && bytes <= (16u << 20); const auto placed = sized ? placed_buffer_range(self->own().list_device, address, bytes) : PlacedVertexView{}; const ResourceInfo* info = placed.alias ? resource_info(placed.owner) : nullptr; const u64 offset = placed.alias ? address - reinterpret_cast(placed.owner->own().title_memory) : 0; + UINT channels = 0; + const char* typed_refusal = typed_bytes ? typed_dword_descriptor(words, typed_bytes / 4, true, channels) : nullptr; if (words[0] == view_token_magic) reason = "an output buffer descriptor is a view token"; else if (half_bytes && !half_buffer_descriptor_matches(words, half_bytes)) reason = "a half buffer store requires matching format, stride and structured bounds"; + else if (typed_refusal) reason = typed_refusal; + else if (typed_bytes && channels * 4 != typed_bytes) + reason = "a typed output FORMAT has another width than the stored components"; else if (!bytes) reason = "an output buffer descriptor has zero records"; else if (!sized) reason = half_bytes ? "an output half buffer range is over 16 MiB" - : "an output buffer range is over 16 MiB or not whole dwords"; + : typed_bytes ? "an output typed buffer range is over 16 MiB" + : "an output buffer range is over 16 MiB or not whole dwords"; else if (placed.reject) reason = "an output range crosses the end of a placed buffer"; else if (!placed.owner) reason = "an output is in title memory outside any placed buffer"; else if (!placed.alias) reason = "an output is in a placed buffer that does not alias title memory"; else if (!info || info->initial_state != D3D12_RESOURCE_STATE_COMMON) reason = "an output buffer's resource was not created in the common state"; else if (offset % alignment) reason = half_bytes ? "an output half buffer is not element-aligned in its resource" - : "an output buffer range is not 16-byte aligned in its resource"; + : typed_bytes ? "an output typed buffer is not element-aligned in its resource" + : "an output buffer range is not 16-byte aligned in its resource"; else if (!(placed.alias->GetDesc().Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) reason = "an output buffer has no unordered access"; for (std::size_t input = 0; input < inputs && !reason; ++input) @@ -891,20 +1013,18 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch& "stride=%u bytes=%llu result=%s", key, slot.a, slot.b, entry, words[0], words[1], words[2], words[3], stride, bytes, reason); views[i] = {placed.alias, offset, bytes, true}; - if (!reason && half_bytes) { - const auto format = x2w::native_half_buffer_format(half_bytes); - D3D12_FEATURE_DATA_FORMAT_SUPPORT support{format}; - constexpr UINT required = D3D12_FORMAT_SUPPORT1_BUFFER | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW; - if (FAILED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))) || - (support.Support1 & required) != required || - !(support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE)) { - reason = "an output half buffer format does not support typed UAV stores"; + if (!reason && element) { + const auto format = half_bytes ? x2w::native_half_buffer_format(half_bytes) + : x2w_startup::typed_dword_uint_format(channels); + if (!typed_buffer_store_supported(format)) { + reason = half_bytes ? "an output half buffer format does not support typed UAV stores" + : "an output typed buffer format does not support typed UAV stores"; } else { views[i].described = true; D3D12_UNORDERED_ACCESS_VIEW_DESC desc{}; desc.Format = format; desc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER; - desc.Buffer.FirstElement = offset / half_bytes; + desc.Buffer.FirstElement = offset / element; desc.Buffer.NumElements = words[2]; views[i].desc.uav = desc; } @@ -992,6 +1112,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(view.bytes)); diff --git a/tests/cmake/startup_compute_typed_buffer_srv.cmake b/tests/cmake/startup_compute_typed_buffer_srv.cmake new file mode 100644 index 000000000..aedb10f63 --- /dev/null +++ b/tests/cmake/startup_compute_typed_buffer_srv.cmake @@ -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 + "$" "${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" + "$" "$" "$" + "${DXC_BIN_DIR}" ${adapter}) + set_tests_properties(startup_compute_typed_buffer_srv_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/cmake/startup_compute_typed_buffer_uav.cmake b/tests/cmake/startup_compute_typed_buffer_uav.cmake new file mode 100644 index 000000000..0920a62c8 --- /dev/null +++ b/tests/cmake/startup_compute_typed_buffer_uav.cmake @@ -0,0 +1,18 @@ +# Typed buffer outputs (buffer_store_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_uav_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_typed_buffer_uav_check.py" compile + "$" "${DXC_BIN_DIR}") + set_tests_properties(startup_compute_typed_buffer_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_typed_buffer_uav_${adapter} COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_typed_buffer_uav_check.py" + "$" "$" "$" + "${DXC_BIN_DIR}" ${adapter}) + set_tests_properties(startup_compute_typed_buffer_uav_${adapter} PROPERTIES TIMEOUT 180 SKIP_RETURN_CODE 77) + endforeach() +endif() diff --git a/tests/native_compute_manifest_reasons.cpp b/tests/native_compute_manifest_reasons.cpp index 92d7147f1..536263ce8 100644 --- a/tests/native_compute_manifest_reasons.cpp +++ b/tests/native_compute_manifest_reasons.cpp @@ -67,7 +67,10 @@ int main() { p.bindings[0].kind = K::texture_srv; p.bindings[0].array_count = 4; }}, - {"UAV kind typed_buffer_uav", [](auto &p) { p.bindings[1].kind = K::typed_buffer_uav; }}, + {"UAV kind typed_buffer_uav", [](auto &p) { // a store wider than the four components a line carries + p.bindings[1].kind = K::typed_buffer_uav; + p.bindings[1].required_bytes = 20; + }}, {"UAV kind texture_uav cube", [](auto &p) { p.bindings[1].kind = K::texture_uav; p.bindings[1].image_dimension = 3; diff --git a/tests/startup_compute_typed_buffer_srv_check.py b/tests/startup_compute_typed_buffer_srv_check.py new file mode 100644 index 000000000..2d568e38d --- /dev/null +++ b/tests/startup_compute_typed_buffer_srv_check.py @@ -0,0 +1,168 @@ +"""Publish a synthetic slot 67 program with buffer_load_format_* and check its typed buffer input (#3224). + +The program reads input element `index` with buffer_load_format_ (IDXEN) from table byte 0 and stores the n +dwords raw at output byte 4 * n * index (table byte 32). compile_dxil publishes `srv 0 typed table 1 0 bytes 4n`; +the stand-in binds a UINT view of the title's 32-bit components. Fewer records than lanes checks the zeros past +the view's last element. + +usage: + startup_compute_typed_buffer_srv_check.py compile + startup_compute_typed_buffer_srv_check.py +""" + +from pathlib import Path +import struct +import subprocess +import sys +from tempfile import TemporaryDirectory + +from native_dpp_mirror_check import vop2 +from native_gather_lod_check import render +from native_scalar_lds_check import scalar, sop1 +from startup_compute_dispatch_check import dispatch, lines, prepare_case +from startup_compute_translate_check import state_a, write_request +from startup_gfx2hlsl_compute_check import SENTINEL, TGID_X, TGID_Y, TGID_Z, USER7, WORDS, root_blob + +STATE = state_a((32, 1, 1), USER7 | TGID_X | TGID_Y | TGID_Z) +FLOAT_FORMATS = {1: 22, 2: 64, 3: 74, 4: 77} # gfx10 BUF_FMT_32[_32[_32[_32]]]_FLOAT +INTEGER_FORMATS = {1: (20, 21), 2: (62, 63), 3: (72, 73), 4: (75, 76)} # the _UINT and _SINT codes of each width +STORES = {1: 0x1c, 2: 0x1d, 3: 0x1f, 4: 0x1e} # buffer_store_dword[x2|x3|x4] +RECORDS = 50 +INPUT = [(0x8b02a7f1 + i * 0x413b1) & 0xffffffff for i in range(WORDS)] # the consumer's input words +DXGI_R32G32B32A32_FLOAT, DXGI_R8G8B8A8_UNORM = 2, 28 + + +def program(channels): + offset = (vop2(0x0b, 9, 128 + 12, 8) if channels == 3 else # v9 = 12 * v8 + vop2(0x1a, 9, 128 + {1: 2, 2: 3, 4: 4}[channels], 8)) # v9 = v8 << log2(4n) + words = [ + sop1(3, 20, 6), sop1(3, 21, 128), + 0xf408080a, 0xfa000020, # output descriptor s[32:35], table byte 32 + 0xf408090a, 0xfa000000, # input descriptor s[36:39], table byte 0 + 0xbf8cc07f, # s_waitcnt lgkmcnt(0) + scalar(0x26, 8, 8, 131), # 3 * group.y + scalar(0x26, 9, 9, 134), # 6 * group.z + scalar(0, 11, 7, 8), scalar(0, 11, 11, 9), + scalar(0x1e, 11, 11, 133), # 32 * flat group index + vop2(0x25, 8, 11, 0), # v8 = element index + 0xe0002000 | (channels - 1) << 18, 0x80090108, # buffer_load_format_ v[1:n], v8, s[36:39] idxen + 0xbf8c3f70, # s_waitcnt vmcnt(0) + offset, + 0xe0001000 | STORES[channels] << 18, 0x80080109, # buffer_store_dword v[1:n], v9, s[32:35] offen + 0xbf810000, + ] + return struct.pack('<%dI' % len(words), *words) + + +def output_records(channels): + return WORDS // channels * channels # no store crosses the end of the output view + + +def expected(channels, first=0): + # first: the element the descriptor starts at; its records follow. + return [INPUT[first * channels + k] if k < RECORDS * channels else 0 if k < output_records(channels) else SENTINEL + for k in range(WORDS)] + + +def check_words(got, want, label): + first = next(((i, a, b) for i, (a, b) in enumerate(zip(got, want)) if a != b), None) + if first is not None: + word, actual, expected_word = first + raise AssertionError(f'{label}: word {word} expected {expected_word:08x}, read {actual:08x}') + + +def publish(compile_dxil, sdk, cache, key, channels): + result = subprocess.run([compile_dxil, sdk, cache.parent / 'native', 'gfx1013-direct-cs@0', + '--requests', cache, cache], capture_output=True, text=True, timeout=60) + manifest = cache / f'{key}.txt' + assert result.returncode == 0 and manifest.is_file(), (result.stdout, result.stderr) + text = manifest.read_text().splitlines() + assert f'srv 0 typed table 1 0 bytes {4 * channels}' in text and 'uav 0 table 1 32' in text, text + + +def check_compiler(compile_dxil, sdk): + with TemporaryDirectory() as directory: + for channels in FLOAT_FORMATS: + cache = Path(directory) / str(channels) + cache.mkdir() + key = write_request(cache, program(channels), STATE, root=root_blob(table_range=True)) + publish(compile_dxil, sdk, cache, f'{key:016x}', channels) + print('compile_dxil publishes srv typed lines for 1 to 4 components') + + +def check(compile_dxil, consumer, sdk, work, adapter, channels): + cache, env = prepare_case(work, adapter) + env.pop('X2W_NATIVE_SHADERS', None) + env.pop('X2W_NATIVE_SHADERS_ONLY', None) + (work / 'program').write_bytes(program(channels)) + (work / 'state').write_bytes(STATE) + fields = work / 'fields.bin' + env['X2W_TEST_DISPATCH_FIELDS'] = str(fields) + word3 = 0x01000fac | FLOAT_FORMATS[channels] << 12 + untouched = [SENTINEL] * WORDS + + def run(stride=4 * channels, records=RECORDS, descriptor_word=word3, mode='dispatch-records32', *, probe=False): + fields.write_bytes(struct.pack('<6I', stride, records, descriptor_word, 4, output_records(channels), 0)) + return dispatch(work, consumer, env, mode, probe=probe) + + got, result = run(probe=True) + check_words(got, untouched, 'before translation') + assert lines(result, 'Dispatch(3, 2, 2) refused: no translation'), result.stderr[-3000:] + [request] = cache.glob('req_*.c67') + key = request.stem.removeprefix('req_') + publish(compile_dxil, sdk, cache, key, channels) + + def translated(label, first=0, **options): + got, result = run(**options) + check_words(got, expected(channels, first), label) + assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1 and not lines(result, 'refused:'), result.stderr[-3000:] + assert not [line for line in lines(result, 'x2w-d3d12-debug ') if ' sev=0 ' in line or ' sev=1 ' in line] + return result + + result = translated(f'{channels} channels') + layers = lines(result, 'x2w-d3d12 debug layer: ') + layer = 'enabled' if layers and '0x00000000' in layers[0] else 'unavailable' + print(f'{channels} channels: {RECORDS} typed elements, then zeros, on {adapter} (debug layer {layer})') + for code in INTEGER_FORMATS[channels]: + translated(f'{channels} channels, FORMAT {code}', descriptor_word=0x01000fac | code << 12) + translated(f'{channels} channels, base one element in', first=1, mode=f'dispatch-records32-input-at-{4 * channels}') + print(f'{channels} channels: the UINT and SINT FORMATs read the same words, and a base one element in shifts them') + + refusals = [ + ({'descriptor_word': 0x01038fac}, 'a typed input FORMAT is not 32 bits per channel'), # 8_8_8_8_UNORM + ({'stride': 4 * channels + 4}, 'a typed input stride is not its element size'), + ({'descriptor_word': word3 | 3 << 28}, 'a typed input descriptor is swizzled or not bounds-checked by record'), + ({'descriptor_word': word3 & ~0x01000000}, 'a typed input descriptor is swizzled or not bounds-checked by record'), + ({'stride': 0x4000 | 4 * channels}, 'a typed input descriptor is swizzled or not bounds-checked by record'), # word 1 bit 30 + ] + if channels > 1: + refusals.append(({'descriptor_word': word3 & ~0xfff | 0xfa5}, 'a typed input DST_SEL is not the identity')) # X, Y swapped + narrow = 0x01000fac | FLOAT_FORMATS[channels - 1] << 12 + refusals.append(({'descriptor_word': narrow, 'stride': 4 * (channels - 1)}, + 'a typed input has fewer channels than the program reads')) + refusals.append(({'mode': 'dispatch-records32-input-at-4'}, 'a typed input range does not start at a whole element')) + if channels == 4: + translated('view token', mode='dispatch-srv-token-typed', descriptor_word=DXGI_R32G32B32A32_FLOAT) + refusals.append(({'mode': 'dispatch-srv-token-typed', 'descriptor_word': DXGI_R8G8B8A8_UNORM}, + 'a typed input token is not a typed buffer view with 32-bit channels')) + print('4 channels: a slot 18 typed buffer view token reads the same elements') + for options, reason in refusals: + got, result = run(**options) + check_words(got, untouched, options) + assert len(lines(result, 'Dispatch(3, 2, 2) refused: ' + reason)) == 1, (options, result.stderr[-3000:]) + print(f'{channels} channels: {len(refusals)} invalid inputs refused and the output remained untouched') + + +def main(compile_dxil, consumer, renderer, sdk, adapter): + wave = render(renderer, adapter, '--shader-wave32') + assert wave.returncode == 0, wave.stdout + wave.stderr + with TemporaryDirectory() as directory: + for channels in FLOAT_FORMATS: + check(compile_dxil, consumer, sdk, Path(directory) / str(channels), adapter, channels) + + +if __name__ == '__main__': + if sys.argv[1] == 'compile': + check_compiler(*sys.argv[2:]) + else: + main(*sys.argv[1:]) diff --git a/tests/startup_compute_typed_buffer_uav_check.py b/tests/startup_compute_typed_buffer_uav_check.py new file mode 100644 index 000000000..4d1c97edd --- /dev/null +++ b/tests/startup_compute_typed_buffer_uav_check.py @@ -0,0 +1,187 @@ +"""Publish a synthetic slot 67 program with buffer_store_format_* and check its typed buffer output (#3224). + +Each lane computes n words from its element index and stores them with buffer_store_format_ (IDXEN) to the +output at table byte 32. compile_dxil publishes `uav 0 typed table 1 32 bytes 4n`; the stand-in binds a UINT +typed UAV of the title's 32-bit components. The descriptor has fewer records than lanes, so the stores past its +last element must leave the sentinel. + +usage: + startup_compute_typed_buffer_uav_check.py compile + startup_compute_typed_buffer_uav_check.py +""" + +from pathlib import Path +import struct +import subprocess +import sys +from tempfile import TemporaryDirectory + +from native_dpp_mirror_check import vop2 +from native_gather_lod_check import render +from native_scalar_lds_check import scalar, sop1 +from startup_compute_dispatch_check import dispatch, lines, prepare_case +from startup_compute_translate_check import state_a, write_request +from startup_gfx2hlsl_compute_check import SENTINEL, TGID_X, TGID_Y, TGID_Z, USER7, WORDS, root_blob + +STATE = state_a((32, 1, 1), USER7 | TGID_X | TGID_Y | TGID_Z) +FLOAT_FORMATS = {1: 22, 2: 64, 3: 74, 4: 77} # gfx10 BUF_FMT_32[_32[_32[_32]]]_FLOAT +INTEGER_FORMATS = {1: (20, 21), 2: (62, 63), 3: (72, 73), 4: (75, 76)} # the _UINT and _SINT codes of each width +OUTPUT_OFFSET = 8 # bytes: the consumer's dispatch-records32-output-offset base +KEYS = (0x3f800000, 0x5a5a0000, 0x40000000, 0x00c0ffee) +RECORDS = 50 +TOKEN_FIRST, TOKEN_ELEMENTS = 4, WORDS - 8 # the consumer's dispatch-uav-token-typed view: R32_UINT + + +def value(index, component): + return (index + KEYS[component]) & 0xffffffff if component % 2 == 0 else index ^ KEYS[component] + + +def program(channels): + words = [ + sop1(3, 20, 6), sop1(3, 21, 128), + 0xf408080a, 0xfa000020, # output descriptor s[32:35], table byte 32 + 0xbf8cc07f, # s_waitcnt lgkmcnt(0) + scalar(0x26, 8, 8, 131), # 3 * group.y + scalar(0x26, 9, 9, 134), # 6 * group.z + scalar(0, 11, 7, 8), scalar(0, 11, 11, 9), + scalar(0x1e, 11, 11, 133), # 32 * flat group index + vop2(0x25, 8, 11, 0), # v8 = element index + vop2(0x25, 1, 255, 8), KEYS[0], # v1 = v8 + key 0 + vop2(0x1d, 2, 255, 8), KEYS[1], # v2 = v8 ^ key 1 + vop2(0x25, 3, 255, 8), KEYS[2], # v3 = v8 + key 2 + vop2(0x1d, 4, 255, 8), KEYS[3], # v4 = v8 ^ key 3 + 0xe0002000 | (3 + channels) << 18, 0x80080108, # buffer_store_format_ v[1:n], v8, s[32:35] idxen + 0xbf810000, + ] + return struct.pack('<%dI' % len(words), *words) + + +def expected(channels, first_word=0): + out = [SENTINEL] * WORDS + for index in range(RECORDS): + for component in range(channels): + out[first_word + index * channels + component] = value(index, component) + return out + + +def expected_token(): + out = [SENTINEL] * WORDS + for index in range(TOKEN_ELEMENTS): + out[TOKEN_FIRST + index] = value(index, 0) + return out + + +def check_words(got, want, label): + first = next(((i, a, b) for i, (a, b) in enumerate(zip(got, want)) if a != b), None) + if first is not None: + word, actual, expected_word = first + raise AssertionError(f'{label}: word {word} expected {expected_word:08x}, read {actual:08x}') + + +def publish(compile_dxil, sdk, cache, key, channels): + result = subprocess.run([compile_dxil, sdk, cache.parent / 'native', 'gfx1013-direct-cs@0', + '--requests', cache, cache], capture_output=True, text=True, timeout=60) + manifest = cache / f'{key}.txt' + assert result.returncode == 0 and manifest.is_file(), (result.stdout, result.stderr) + text = manifest.read_text().splitlines() + assert f'uav 0 typed table 1 32 bytes {4 * channels}' in text, text + + +def check_compiler(compile_dxil, sdk): + with TemporaryDirectory() as directory: + for channels in FLOAT_FORMATS: + cache = Path(directory) / str(channels) + cache.mkdir() + key = write_request(cache, program(channels), STATE, root=root_blob(table_range=True)) + publish(compile_dxil, sdk, cache, f'{key:016x}', channels) + print('compile_dxil publishes uav typed lines for 1 to 4 components') + + +def check(compile_dxil, consumer, sdk, work, adapter, channels): + cache, env = prepare_case(work, adapter) + env.pop('X2W_NATIVE_SHADERS', None) + env.pop('X2W_NATIVE_SHADERS_ONLY', None) + (work / 'program').write_bytes(program(channels)) + (work / 'state').write_bytes(STATE) + fields = work / 'fields.bin' + env['X2W_TEST_DISPATCH_FIELDS'] = str(fields) + word3 = 0x01000fac | FLOAT_FORMATS[channels] << 12 + untouched = [SENTINEL] * WORDS + + def run(stride=4 * channels, records=RECORDS, descriptor_word=word3, mode='dispatch-records32', *, probe=False): + fields.write_bytes(struct.pack('<6I', 4, WORDS, 0, stride, records, descriptor_word)) + return dispatch(work, consumer, env, mode, probe=probe) + + got, result = run(probe=True) + check_words(got, untouched, 'before translation') + assert lines(result, 'Dispatch(3, 2, 2) refused: no translation'), result.stderr[-3000:] + [request] = cache.glob('req_*.c67') + key = request.stem.removeprefix('req_') + publish(compile_dxil, sdk, cache, key, channels) + + def translated(label, want, **options): + got, result = run(**options) + check_words(got, want, label) + assert len(lines(result, 'Dispatch(3, 2, 2) translated')) == 1 and not lines(result, 'refused:'), result.stderr[-3000:] + assert not [line for line in lines(result, 'x2w-d3d12-debug ') if ' sev=0 ' in line or ' sev=1 ' in line] + return result + + unsupported = 'an output typed buffer format does not support typed UAV stores' + got, result = run() + if channels == 3 and lines(result, 'Dispatch(3, 2, 2) refused: ' + unsupported): + check_words(got, untouched, '3 channels, unsupported') + print(f'3 channels: the adapter has no typed UAV store for R32G32B32_UINT; refused on {adapter}') + else: + result = translated(f'{channels} channels', expected(channels)) + layers = lines(result, 'x2w-d3d12 debug layer: ') + layer = 'enabled' if layers and '0x00000000' in layers[0] else 'unavailable' + print(f'{channels} channels: {RECORDS} typed elements stored, the rest untouched, on {adapter} (debug layer {layer})') + for code in INTEGER_FORMATS[channels]: + translated(f'{channels} channels, FORMAT {code}', expected(channels), descriptor_word=0x01000fac | code << 12) + if OUTPUT_OFFSET % (4 * channels) == 0: + translated(f'{channels} channels, base {OUTPUT_OFFSET} bytes in', expected(channels, OUTPUT_OFFSET // 4), + mode='dispatch-records32-output-offset') + print(f'{channels} channels: the UINT and SINT FORMATs store the same words' + + (f', and a base {OUTPUT_OFFSET} bytes in shifts them' if OUTPUT_OFFSET % (4 * channels) == 0 else '')) + + other = FLOAT_FORMATS[channels + 1 if channels < 4 else 3] + other_channels = channels + 1 if channels < 4 else 3 + refusals = [ + ({'descriptor_word': 0x01038fac}, 'a typed output FORMAT is not 32 bits per channel'), # 8_8_8_8_UNORM + ({'stride': 4 * channels + 4}, 'a typed output stride is not its element size'), + ({'descriptor_word': word3 | 3 << 28}, 'a typed output descriptor is swizzled or not bounds-checked by record'), + ({'descriptor_word': word3 & ~0x01000000}, 'a typed output descriptor is swizzled or not bounds-checked by record'), + ({'stride': 0x4000 | 4 * channels}, 'a typed output descriptor is swizzled or not bounds-checked by record'), # word 1 bit 30 + ({'descriptor_word': 0x01000fac | other << 12, 'stride': 4 * other_channels}, + 'a typed output FORMAT has another width than the stored components'), + ] + if channels > 1: + refusals.append(({'descriptor_word': word3 & ~0xfff | 0xfa5}, 'a typed output DST_SEL is not the identity')) + if OUTPUT_OFFSET % (4 * channels): + refusals.append(({'mode': 'dispatch-records32-output-offset'}, + 'an output typed buffer is not element-aligned in its resource')) + if channels == 1: + translated('view token', expected_token(), mode='dispatch-uav-token-typed') + refusals.append(({'mode': 'dispatch-uav-token-structured'}, + 'a typed output token is not a typed buffer UAV of the stored width')) + print(f'1 channel: a slot 19 R32_UINT view token stores {TOKEN_ELEMENTS} elements at element {TOKEN_FIRST}') + for options, reason in refusals: + got, result = run(**options) + check_words(got, untouched, options) + assert len(lines(result, 'Dispatch(3, 2, 2) refused: ' + reason)) == 1, (options, result.stderr[-3000:]) + print(f'{channels} channels: {len(refusals)} invalid outputs refused and the output remained untouched') + + +def main(compile_dxil, consumer, renderer, sdk, adapter): + wave = render(renderer, adapter, '--shader-wave32') + assert wave.returncode == 0, wave.stdout + wave.stderr + with TemporaryDirectory() as directory: + for channels in FLOAT_FORMATS: + check(compile_dxil, consumer, sdk, Path(directory) / str(channels), adapter, channels) + + +if __name__ == '__main__': + if sys.argv[1] == 'compile': + check_compiler(*sys.argv[2:]) + else: + main(*sys.argv[1:]) diff --git a/tests/startup_stream_consumer.cpp b/tests/startup_stream_consumer.cpp index 092b6e91e..442f6676e 100644 --- a/tests/startup_stream_consumer.cpp +++ b/tests/startup_stream_consumer.cpp @@ -243,6 +243,9 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin constexpr UINT long_records = 2u << 20, long_frames = 48; const bool shared_uav = mode == "dispatch-read-write-uav"; const bool typed_output_offset = mode == "dispatch-records32-output-offset"; + // dispatch-records32-input-at-: dispatch-records32 with the input descriptor that many bytes into its buffer. + const std::string input_at = "dispatch-records32-input-at-"; + const UINT input_offset = mode.rfind(input_at, 0) == 0 ? static_cast(std::stoul(mode.substr(input_at.size()))) : 0; const bool indirect = mode.rfind("dispatch-indirect-", 0) == 0; const bool heap_waterfall = mode.rfind("dispatch-heap-waterfall", 0) == 0; const bool pixel = mode.rfind("dispatch-indirect-pixel-", 0) == 0; @@ -344,6 +347,10 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin const UINT64 address = reinterpret_cast(buffers[1]) + 8; descriptors[1][0] = static_cast(address); descriptors[1][1] = static_cast(address >> 32) | fields[1][0] << 16; + } else if (input_offset) { + const UINT64 address = reinterpret_cast(buffers[0]) + input_offset; + descriptors[0][0] = static_cast(address); + descriptors[0][1] = static_cast(address >> 32) | fields[0][0] << 16; } const bool heap_short = mode == "dispatch-heap-waterfall-short"; const struct { @@ -397,7 +404,8 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin std::memcpy(reinterpret_cast(cpu + 64), window, sizeof(window)); std::memcpy(reinterpret_cast(cpu + 32), descriptors[1], sizeof(descriptors[1])); } - if (mode == "dispatch-records32" || typed_output_offset || shared_uav) { // output at byte 32, none at byte 16 + if (mode == "dispatch-records32" || typed_output_offset || input_offset || shared_uav || + mode == "dispatch-srv-token-typed") { // output at byte 32, none at byte 16 std::memset(reinterpret_cast(cpu + sizeof(descriptors[0])), 0, sizeof(descriptors[1])); std::memcpy(reinterpret_cast(cpu + 32), descriptors[1], sizeof(descriptors[1])); } @@ -436,6 +444,14 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin if (mode == "dispatch-uav-token-wrong-slot") reinterpret_cast(cpu + 32)[2] = 18; } + if (mode == "dispatch-srv-token-typed") { + // dispatch-srv-token-typed: the input descriptor is the token of a typed buffer view (device slot 18) of the + // input buffer; for this mode the input's word 3 field is the view's DXGI format, its records field the elements. + D3D12_SHADER_RESOURCE_VIEW_DESC view{static_cast(fields[0][2]), D3D12_SRV_DIMENSION_BUFFER, + D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING}; + view.Buffer.NumElements = fields[0][1]; + invoke(device, 18, word(placed[0]), word(&view), cpu); + } const bool heap_record = mode.rfind("dispatch-heap", 0) == 0 && !heap_waterfall; if (heap_record) for (const UINT at : {0u, 32u, 64u})