diff --git a/src/FiveStar.Loader/Shims/D3D12X/Binding/BufferFormatCodes.cs b/src/FiveStar.Loader/Shims/D3D12X/Binding/BufferFormatCodes.cs
index 7ad65427..338359c5 100644
--- a/src/FiveStar.Loader/Shims/D3D12X/Binding/BufferFormatCodes.cs
+++ b/src/FiveStar.Loader/Shims/D3D12X/Binding/BufferFormatCodes.cs
@@ -6,7 +6,7 @@ namespace FiveStar.Loader.Shims.D3D12X.Binding;
///
/// The hardware dword 3 a typed buffer view's V# carries (RDNA 2 ISA, buffer resource): DST_SEL_X..W in bits 0-11, the
/// gfx10 unified buffer format in bits 12-18 and RESOURCE_LEVEL (bit 24, 1 on gfx10). Translated programs decode it as
-/// they decode a V# the game builds itself (rdna_prelude buf_code, fmt_swizzle), so a B8G8R8A8 view reads R in x.
+/// they decode a V# the game builds itself (rdna_prelude buf_code, FmtLoad), so a B8G8R8A8 view reads R in x.
///
internal static class BufferFormatCodes
{
diff --git a/src/FiveStar.Shaders/Translation/Emitters/BufferEmitter.cs b/src/FiveStar.Shaders/Translation/Emitters/BufferEmitter.cs
index 78ebafbf..588c3f49 100644
--- a/src/FiveStar.Shaders/Translation/Emitters/BufferEmitter.cs
+++ b/src/FiveStar.Shaders/Translation/Emitters/BufferEmitter.cs
@@ -229,7 +229,7 @@ public void TranslateFormatted(string op, IReadOnlyList a, InstructionM
}
var name = $"_fmt{_state.NextTemporary()}";
- _state.Emit($" uint4 {name} = select({inRange}, fmt_swizzle(FmtLoad({view}.x, {byteAddress}, {format}), {swizzle}, {format}), uint4(0u, 0u, 0u, 0u));");
+ _state.Emit($" uint4 {name} = select({inRange}, FmtLoad({view}.x, {byteAddress}, {format}, {swizzle}), uint4(0u, 0u, 0u, 0u));");
if (half != HalfKind.None)
{
diff --git a/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli b/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli
index da2c487e..82f7549c 100644
--- a/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli
+++ b/src/FiveStar.Shaders/Translation/Runtime/rdna_prelude.hlsli
@@ -463,7 +463,16 @@ uint fmt_convert(uint v, uint bits, uint num) {
uint fmt_component(uint4 w, uint field, uint num, uint missing) {
return select((field >> 8) != 0u, fmt_convert(fmt_field(w, field), field >> 8, num), missing);
}
-uint4 FmtLoad(uint heap_index, uint addr, uint code) {
+// DST_SEL_X..W (V# dword3 bits 0-11, d3): 0 and 1 the constants, 4-7 the loaded x..w (BGRA views read z, y, x, w);
+// all four 0 keeps the loaded order. Each result component converts only the field its selector names, so a load
+// whose program reads x converts one field rather than four (the unread components then fold away): converting every
+// field and choosing after made the 3,281 loads of a 122k-dword compute program 41% of its DXIL instructions.
+uint fmt_selected(uint4 w, uint4 fields, uint num, uint one, uint s, uint k) {
+ uint sel = select(s == 0u, 4u + k, (s >> (3u * k)) & 7u), c = sel & 3u;
+ uint field = select(c == 0u, fields.x, select(c == 1u, fields.y, select(c == 2u, fields.z, fields.w)));
+ return select(sel >= 4u, fmt_component(w, field, num, select(c == 3u, one, 0u)), select(sel == 1u, one, 0u));
+}
+uint4 FmtLoad(uint heap_index, uint addr, uint code, uint d3) {
ByteAddressBuffer b = ResourceDescriptorHeap[NonUniformResourceIndex(view_slot(heap_index))];
uint base = addr & ~3u, sh = (addr & 3u) * 8u;
uint4 w = b.Load4(base); uint w4 = b.Load(base + 16);
@@ -471,9 +480,9 @@ uint4 FmtLoad(uint heap_index, uint addr, uint code) {
w = select(sh != 0u, shifted, w);
uint layout = code >> 3, num = code & 7u;
uint4 fields = select(layout < 16u, FMT_FIELDS[layout & 15u], uint4(0, 0, 0, 0));
- uint one = select(num - 4u <= 1u, 1u, asuint(1.0));
- return uint4(fmt_component(w, fields.x, num, 0u), fmt_component(w, fields.y, num, 0u),
- fmt_component(w, fields.z, num, 0u), fmt_component(w, fields.w, num, one));
+ uint one = select(num - 4u <= 1u, 1u, asuint(1.0)), s = d3 & 0xfffu;
+ return uint4(fmt_selected(w, fields, num, one, s, 0u), fmt_selected(w, fields, num, one, s, 1u),
+ fmt_selected(w, fields, num, one, s, 2u), fmt_selected(w, fields, num, one, s, 3u));
}
// gfx10 unified buffer formats (RDNA 2 ISA, buffer data formats: 1 8_UNORM .. 77 32_32_32_32_FLOAT) as codes; 0 invalid.
static const uint FMT_UNIFIED[80] = {
@@ -497,18 +506,6 @@ uint fmt_unhalf(uint h, uint code) {
uint num = code & 7u;
return select(num == 4u, h, select(num == 5u, (h ^ 0x8000u) - 0x8000u, asuint(f16tof32(h))));
}
-uint fmt_select(uint4 v, uint sel, uint one) {
- uint c = select((sel & 3u) == 0u, v.x, select((sel & 3u) == 1u, v.y, select((sel & 3u) == 2u, v.z, v.w)));
- return select(sel >= 4u, c, select(sel == 1u, one, 0u));
-}
-// DST_SEL_X..W (V# dword3 bits 0-11): 0 and 1 the constants, 4-7 the loaded x..w (BGRA views read z, y, x, w). Loads
-// only; all four 0 keeps the loaded order.
-uint4 fmt_swizzle(uint4 v, uint d3, uint code) {
- uint s = d3 & 0xfffu, one = select((code & 7u) - 4u <= 1u, 1u, asuint(1.0));
- uint4 r = uint4(fmt_select(v, s & 7u, one), fmt_select(v, (s >> 3) & 7u, one),
- fmt_select(v, (s >> 6) & 7u, one), fmt_select(v, (s >> 9) & 7u, one));
- return s == 0u ? v : r;
-}
// ---- cube maps (AMD conventions). face: 0 +X, 1 -X, 2 +Y, 3 -Y, 4 +Z, 5 -Z
float cubeid(float x, float y, float z) {
@@ -676,7 +673,7 @@ void probe_view(uint site, uint heap_index) {
}
uint probe_BufLoad(uint site, uint heap_index, uint off) { probe_view(site, heap_index); return BufLoad(heap_index, off); }
uint probe_BufLoadRW(uint site, uint heap_index, uint off) { probe_view(site, heap_index); return BufLoadRW(heap_index, off); }
-uint4 probe_FmtLoad(uint site, uint heap_index, uint addr, uint code) { probe_view(site, heap_index); return FmtLoad(heap_index, addr, code); }
+uint4 probe_FmtLoad(uint site, uint heap_index, uint addr, uint code, uint d3) { probe_view(site, heap_index); return FmtLoad(heap_index, addr, code, d3); }
uint probe_PtrLoad(uint site, uint lo, uint hi, uint off) {
if (hi != 0u) { uint2 a = ptr_add(lo, hi, 0u, asint(off)); probe_view(site, gpu_page(a.x, a.y).x); }
else { // a 32-bit pointer: the window (descriptor records); the first such address thread 0 reads is kept
diff --git a/tests/FiveStar.Shaders.Tests/Assembled/cs_buffer_rule_format_select.851b0d8790d13645.bin b/tests/FiveStar.Shaders.Tests/Assembled/cs_buffer_rule_format_select.851b0d8790d13645.bin
new file mode 100644
index 00000000..e707690f
Binary files /dev/null and b/tests/FiveStar.Shaders.Tests/Assembled/cs_buffer_rule_format_select.851b0d8790d13645.bin differ
diff --git a/tests/FiveStar.Shaders.Tests/Cases/ComputeBufferRuleCases.cs b/tests/FiveStar.Shaders.Tests/Cases/ComputeBufferRuleCases.cs
index 00388d9f..11d21e20 100644
--- a/tests/FiveStar.Shaders.Tests/Cases/ComputeBufferRuleCases.cs
+++ b/tests/FiveStar.Shaders.Tests/Cases/ComputeBufferRuleCases.cs
@@ -27,6 +27,11 @@ public static class ComputeBufferRuleCases
private const uint Format16Float = 13;
private const uint Format16x4Float = 71;
private const uint Format32x4Uint = 75;
+ private const uint Format8x4Uint = 60;
+
+ /// DST_SEL z, y, x, 1 (a BGRA view with its alpha forced to one), and w, 0, 0, 0.
+ private const uint BgraOneSelect = 6 | (5 << 3) | (4 << 6) | (1 << 9);
+ private const uint WOnlySelect = 7;
private static readonly uint[] Input = [.. Enumerable.Range(0, 1024).Select(index => 0xA0000000 + (0x11 * (uint)index))];
@@ -35,13 +40,13 @@ public static class ComputeBufferRuleCases
public static IEnumerable All()
{
- return [Structured(), Unchecked(), FormatElements(), HalfData(), AtomicPastEnd()];
+ return [Structured(), Unchecked(), FormatElements(), FormatSelects(), HalfData(), AtomicPastEnd()];
}
/// A V# of heap slot as the hardware reads it.
- private static uint[] Descriptor(int heap, uint stride, uint records, uint select, uint format = 0)
+ private static uint[] Descriptor(int heap, uint stride, uint records, uint select, uint format = 0, uint destination = DwordSelect)
{
- return [(uint)heap, stride << StrideShift, records, DwordSelect | (format << FormatShift) | (select << SelectShift)];
+ return [(uint)heap, stride << StrideShift, records, destination | (format << FormatShift) | (select << SelectShift)];
}
// OOB_SELECT 0, stride 8, 4 records: a dword at offset 8 is past the stride, a dwordx2 at 4 keeps its first dword
@@ -120,6 +125,29 @@ s_waitcnt vmcnt(0)
};
}
+ // DST_SEL picks the fields of an 8_8_8_8_UINT element: z, y, x and the constant 1 for a BGRA-ordered view, and the
+ // fourth field in x with zero after it, so the selected field and not the result slot decides each value.
+ private static ComputeCase FormatSelects()
+ {
+ const string Program = """
+ buffer_load_format_xyzw v[20:23], v41, s[4:7], 0 idxen
+ buffer_load_format_xy v[24:25], v41, s[8:11], 0 idxen
+ s_waitcnt vmcnt(0)
+ """;
+
+ return new ComputeCase("cs_buffer_rule_format_select", Program, 6, thread =>
+ {
+ uint Field(int index) => (Input[thread.Id] >> (8 * index)) & 0xFF;
+
+ return [.. new[] { Field(2), Field(1), Field(0), 1u, Field(3), 0u }.Select(Expected.Exact)];
+ })
+ {
+ SecondDescriptor = Descriptor(DataHeap, 4, Threads, 0, Format8x4Uint, BgraOneSelect),
+ User = Descriptor(DataHeap, 4, Threads, 0, Format8x4Uint, WOnlySelect),
+ Buffers = [new BufferResource(DataHeap, Input, Writable: false)],
+ };
+ }
+
// The d16 forms: buffer_load_format_d16_x (16_FLOAT, stride 2) writes the low half and keeps the high one,
// d16_xyzw (16_16_16_16_FLOAT, stride 8) packs four halves into two VGPRs, and buffer_store_format_d16_xyzw stores
// them back unchanged through a copy of that V# naming the written buffer.