Wave64 pixel shaders: whole-wave add/count reductions read lanes the fragment subgroup doesn't have (32-lane hosts, partly filled waves) #1944

Open
opened 2026-11-26 10:09:51 +00:00 by github-bot · 2 comments
Owner

@⁠Nawid3333 on GitHub · 2026-10-27 21:34 UTC · original

At 69fdb03d a wave64 pixel shader runs unsplit in the host's fragment subgroup. Only compute and mesh can get two lanes per invocation, for a wave64 program on a 32-lane host (SpirvEmitter.cpp:233, ShaderWorkgroupInput at shader.h:235), and only compute pipelines request a subgroup size (shaders.cpp:423). V_READLANE_B32 is a plain shuffle (spirvEmitterFlow.cpp:836-838). So the usual whole-wave reduction reads lanes that have no invocation:

  • in 32-lane fragment subgroups (NVIDIA, or AMD forced to 32), lanes 32-63;
  • in a 64-lane subgroup, the empty lanes of a partly filled wave.

The reduction is s_orn2_saveexec to enable all 64 lanes, v_cndmask with the identity, DPP row_shr 1/2/4/8 or row_xmask 1/2/4/8, v_permlanex16, then v_readlane 31 + 63 or 0 + 32.

This idiom is being fixed one operation at a time:

  • 302240da: complete wave minima (UMin) for sparse fragment subgroups;
  • 6357740f: wave32 UMin/UMax (WaveReductionMatcher in ReadLaneElimination.cpp:105);
  • #1503: the OR form (black eyes in God of War Ragnarök).

The add/count form is still open. I don't have a game report for it yet; the cases below reproduce it.

Measured on an RX 9070 XT (Windows 11, AMD 26.10.1, main 0685bffb; no shader changes since then). For the 32-lane column, VkPipelineShaderStageRequiredSubgroupSizeCreateInfo{32} is chained to the fragment stage (FRAGMENT is in this GPU's requiredSubgroupSizeStages). Each case adds 1 over the lanes enabled at entry and compares the sum with s_bcnt1_i32_b64 exec; for any fill the difference must be 0:

case 64 lanes 32 lanes
8x8 (one full wave64), row_shr, v_readlane 31 + 63 0 32
8x8, row_xmask, v_readlane 0 + 32 0 32
8x4 (half a wave64), row_shr, 31 + 63 231 32
8x4, row_xmask, 0 + 32 -1.13e9 32
  • With 32 lanes, each subgroup adds its own half twice.
  • The 8x4 values with 64 lanes are stale data from the missing lanes and change between runs. On 10-16, with driver 26.8.1, the same cases gave 63 and values between -1.95e9 and 7.5e8.
  • The cases already on main for the fixed parts (GraphicsSparseWaveMinimum, GraphicsDsAppendSkipsHelperLanes, GraphicsDsConsumeSkipsHelperLanes, GraphicsDsOrderedCountSkipsHelperLanes) pass in both columns.

To run these on AMD Windows: RunGraphicsCase skips every graphics case there, because the harness requires attachmentFeedbackLoopDynamicState, which that driver doesn't expose; RenderFragment doesn't use it. I relaxed that check locally for these runs.

One matcher for the whole idiom (add, count, min/max, and/or/xor; both shapes), lowered to the matching OpGroupNonUniform* reduction, would cover 302240da, 6357740f and #1503 in one place.

Test cases: drop-in for tests/ShaderRecompilerComputeTests.cpp (applies to 69fdb03d). Each pixel expects (sum == count, sum - count, 0, 1) = (1, 0, 0, 1).
diff --git a/tests/ShaderRecompilerComputeTests.cpp b/tests/ShaderRecompilerComputeTests.cpp
index 1f988ca0..64f963c0 100644
--- a/tests/ShaderRecompilerComputeTests.cpp
+++ b/tests/ShaderRecompilerComputeTests.cpp
@@ -1439,6 +1439,7 @@ struct GraphicsCase {
   u32 pixel_perspective_centroid_vgpr = UINT32_MAX;
   u32 pixel_custom_interpolation_mask = 0;
   u32 width = 1;
+  u32 height = 1;
   std::vector<u32> object_ids;
   std::vector<u32> gds_initial;
 };
@@ -20150,7 +20151,7 @@ public:
     ValidateSpirv(test.name, vertex_spirv);

     Image target =
-        CreateImageMips(test.name, test.width, 1, vk::Format::eR32G32B32A32Sfloat,
+        CreateImageMips(test.name, test.width, test.height, vk::Format::eR32G32B32A32Sfloat,
                       vk::ImageUsageFlagBits::eColorAttachment, {}, 4,
                       vk::ImageLayout::eGeneral, vk::ImageType::e2D,
                       test.layers > 1 ? vk::ImageViewType::e2DArray : vk::ImageViewType::e2D,
@@ -20158,7 +20159,7 @@ public:
     Image resolved;
     if (test.samples != vk::SampleCountFlagBits::e1) {
       resolved = CreateImageMips(
-          test.name, test.width, 1, target.format, vk::ImageUsageFlagBits::eColorAttachment,
+          test.name, test.width, test.height, target.format, vk::ImageUsageFlagBits::eColorAttachment,
           {}, 4, vk::ImageLayout::eGeneral, vk::ImageType::e2D,
           test.layers > 1 ? vk::ImageViewType::e2DArray : vk::ImageViewType::e2D,
           test.layers);
@@ -20166,7 +20167,7 @@ public:
     Image primitive_target;
     if (object_id) {
       primitive_target = CreateImageMips(
-          test.name, test.width, 1, vk::Format::eR32Uint,
+          test.name, test.width, test.height, vk::Format::eR32Uint,
           vk::ImageUsageFlagBits::eColorAttachment, {}, 1,
           vk::ImageLayout::eGeneral, vk::ImageType::e2D,
           test.layers > 1 ? vk::ImageViewType::e2DArray : vk::ImageViewType::e2D,
@@ -20285,12 +20286,12 @@ public:
     viewport.x = 0.0f;
     viewport.y = 0.0f;
     viewport.width = static_cast<float>(test.width);
-    viewport.height = 1.0f;
+    viewport.height = static_cast<float>(test.height);
     viewport.minDepth = 0.0f;
     viewport.maxDepth = 1.0f;
     vk::Rect2D scissor{};
     scissor.extent.width = test.width;
-    scissor.extent.height = 1;
+    scissor.extent.height = test.height;
     vk::PipelineViewportStateCreateInfo viewport_state{};
     viewport_state.sType = vk::StructureType::ePipelineViewportStateCreateInfo;
     viewport_state.viewportCount = 1;
@@ -20370,7 +20371,7 @@ public:
     }
     vk::RenderingInfo rendering{};
     rendering.sType = vk::StructureType::eRenderingInfo;
-    rendering.renderArea.extent = {test.width, 1};
+    rendering.renderArea.extent = {test.width, test.height};
     rendering.layerCount = test.layers;
     rendering.colorAttachmentCount = color_blend.attachmentCount;
     rendering.pColorAttachments = colors.data();
@@ -45419,6 +45420,47 @@ GraphicsCase GraphicsFrontFaceBits(bool reversed, bool all_bits) {
   return test;
 }

+GraphicsCase GraphicsWave64WholeWaveCount(u32 height, bool lanes_0_32) {
+  static const char *const names[2][2] = {
+      {"GraphicsWave64WholeWaveCount8x4Lanes31And63", "GraphicsWave64WholeWaveCount8x4Lanes0And32"},
+      {"GraphicsWave64WholeWaveCount8x8Lanes31And63", "GraphicsWave64WholeWaveCount8x8Lanes0And32"}};
+  GraphicsCase test;
+  test.name = names[height == 8u][lanes_0_32];
+  test.width = 8;
+  test.height = height;
+  auto &code = test.fragment_code;
+  code = {EncodeVop1(0x01, 13, InlineU32(1)),
+          EncodeSop1(0x10, 9, 126),
+          EncodeSop1(0x04, 24, 126),
+          EncodeSop1(0x28, 106, 126)};
+  AppendVop3(&code, 0x101, 13, InlineU32(0), Vgpr(13), 24);
+  for (u32 shift : {1u, 2u, 4u, 8u}) {
+    code.push_back(EncodeVop2(0x25, 13, 250, 13));
+    code.push_back(EncodeVop2Dpp(13, (lanes_0_32 ? 0x160u : 0x110u) + shift));
+  }
+  code.insert(code.end(), {0xd778100cu, 0x0305830du,
+                          EncodeVop2(0x25, 13, Vgpr(13), 12),
+                          EncodeSop1(0x04, 126, 106)});
+  AppendVop3(&code, 0x360, 6, Vgpr(13), InlineU32(lanes_0_32 ? 0u : 31u));
+  AppendVop3(&code, 0x360, 7, Vgpr(13), InlineU32(lanes_0_32 ? 32u : 63u));
+  code.insert(code.end(), {EncodeSop2(0x00, 8, 6, 7),
+                          EncodeSopc(0x06, 8, 9),
+                          EncodeSop2(0x0a, 10, 242, 128),
+                          EncodeSop2(0x03, 11, 8, 9),
+                          EncodeVop1(0x01, 0, 10), EncodeVop1(0x05, 1, 11),
+                          EncodeVop1(0x01, 2, InlineU32(0)), EncodeVop1(0x01, 3, 242),
+                          EncodeExp0(0x00, 0xf), EncodeExp1(0, 1, 2, 3)});
+  AppendEnd(&code);
+  for (u32 i = 0; i < 8u * height; i++) {
+    test.expected_pixel.insert(test.expected_pixel.end(),
+                               {0x3f800000u, 0u, 0u, 0x3f800000u});
+  }
+  test.opcodes = {ShaderOpcode::S_BCNT1_I32_B64, ShaderOpcode::V_ADD_NC_U32,
+                  ShaderOpcode::V_PERMLANEX16_B32, ShaderOpcode::V_READLANE_B32,
+                  ShaderOpcode::S_ADD_U32};
+  return test;
+}
+
 void CheckPixelFrontFaceModes() {
   constexpr const char *name = "PixelFrontFaceModes";
   static const std::array<u32, 1> code = {EncodeSopp(0x01)};
@@ -46484,6 +46526,10 @@ std::vector<GraphicsCase> MakeGraphicsCases() {
       GraphicsBranchPathFinalVmExportDiscardsInactiveExec(),
       GraphicsObjectPrimitiveId(false),
       GraphicsObjectPrimitiveId(true),
+      GraphicsWave64WholeWaveCount(8, false),
+      GraphicsWave64WholeWaveCount(8, true),
+      GraphicsWave64WholeWaveCount(4, false),
+      GraphicsWave64WholeWaveCount(4, true),
   };
 }

**@⁠Nawid3333** on GitHub · 2026-10-27 21:34 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1944) At 69fdb03d a wave64 pixel shader runs unsplit in the host's fragment subgroup. Only compute and mesh can get two lanes per invocation, for a wave64 program on a 32-lane host (`SpirvEmitter.cpp:233`, `ShaderWorkgroupInput` at `shader.h:235`), and only compute pipelines request a subgroup size (`shaders.cpp:423`). `V_READLANE_B32` is a plain shuffle (`spirvEmitterFlow.cpp:836-838`). So the usual whole-wave reduction reads lanes that have no invocation: - in 32-lane fragment subgroups (NVIDIA, or AMD forced to 32), lanes 32-63; - in a 64-lane subgroup, the empty lanes of a partly filled wave. The reduction is `s_orn2_saveexec` to enable all 64 lanes, `v_cndmask` with the identity, DPP row_shr 1/2/4/8 or row_xmask 1/2/4/8, `v_permlanex16`, then `v_readlane` 31 + 63 or 0 + 32. This idiom is being fixed one operation at a time: - 302240da: complete wave minima (UMin) for sparse fragment subgroups; - 6357740f: wave32 UMin/UMax (`WaveReductionMatcher` in `ReadLaneElimination.cpp:105`); - #1503: the OR form (black eyes in God of War Ragnarök). The add/count form is still open. I don't have a game report for it yet; the cases below reproduce it. Measured on an RX 9070 XT (Windows 11, AMD 26.10.1, main 0685bffb; no shader changes since then). For the 32-lane column, `VkPipelineShaderStageRequiredSubgroupSizeCreateInfo{32}` is chained to the fragment stage (FRAGMENT is in this GPU's `requiredSubgroupSizeStages`). Each case adds 1 over the lanes enabled at entry and compares the sum with `s_bcnt1_i32_b64 exec`; for any fill the difference must be 0: | case | 64 lanes | 32 lanes | |---|---|---| | 8x8 (one full wave64), row_shr, `v_readlane` 31 + 63 | 0 | 32 | | 8x8, row_xmask, `v_readlane` 0 + 32 | 0 | 32 | | 8x4 (half a wave64), row_shr, 31 + 63 | 231 | 32 | | 8x4, row_xmask, 0 + 32 | -1.13e9 | 32 | - With 32 lanes, each subgroup adds its own half twice. - The 8x4 values with 64 lanes are stale data from the missing lanes and change between runs. On 10-16, with driver 26.8.1, the same cases gave 63 and values between -1.95e9 and 7.5e8. - The cases already on main for the fixed parts (`GraphicsSparseWaveMinimum`, `GraphicsDsAppendSkipsHelperLanes`, `GraphicsDsConsumeSkipsHelperLanes`, `GraphicsDsOrderedCountSkipsHelperLanes`) pass in both columns. To run these on AMD Windows: `RunGraphicsCase` skips every graphics case there, because the harness requires `attachmentFeedbackLoopDynamicState`, which that driver doesn't expose; `RenderFragment` doesn't use it. I relaxed that check locally for these runs. One matcher for the whole idiom (add, count, min/max, and/or/xor; both shapes), lowered to the matching `OpGroupNonUniform*` reduction, would cover 302240da, 6357740f and #1503 in one place. <details><summary>Test cases: drop-in for tests/ShaderRecompilerComputeTests.cpp (applies to 69fdb03d). Each pixel expects (sum == count, sum - count, 0, 1) = (1, 0, 0, 1).</summary> ```diff diff --git a/tests/ShaderRecompilerComputeTests.cpp b/tests/ShaderRecompilerComputeTests.cpp index 1f988ca0..64f963c0 100644 --- a/tests/ShaderRecompilerComputeTests.cpp +++ b/tests/ShaderRecompilerComputeTests.cpp @@ -1439,6 +1439,7 @@ struct GraphicsCase { u32 pixel_perspective_centroid_vgpr = UINT32_MAX; u32 pixel_custom_interpolation_mask = 0; u32 width = 1; + u32 height = 1; std::vector<u32> object_ids; std::vector<u32> gds_initial; }; @@ -20150,7 +20151,7 @@ public: ValidateSpirv(test.name, vertex_spirv); Image target = - CreateImageMips(test.name, test.width, 1, vk::Format::eR32G32B32A32Sfloat, + CreateImageMips(test.name, test.width, test.height, vk::Format::eR32G32B32A32Sfloat, vk::ImageUsageFlagBits::eColorAttachment, {}, 4, vk::ImageLayout::eGeneral, vk::ImageType::e2D, test.layers > 1 ? vk::ImageViewType::e2DArray : vk::ImageViewType::e2D, @@ -20158,7 +20159,7 @@ public: Image resolved; if (test.samples != vk::SampleCountFlagBits::e1) { resolved = CreateImageMips( - test.name, test.width, 1, target.format, vk::ImageUsageFlagBits::eColorAttachment, + test.name, test.width, test.height, target.format, vk::ImageUsageFlagBits::eColorAttachment, {}, 4, vk::ImageLayout::eGeneral, vk::ImageType::e2D, test.layers > 1 ? vk::ImageViewType::e2DArray : vk::ImageViewType::e2D, test.layers); @@ -20166,7 +20167,7 @@ public: Image primitive_target; if (object_id) { primitive_target = CreateImageMips( - test.name, test.width, 1, vk::Format::eR32Uint, + test.name, test.width, test.height, vk::Format::eR32Uint, vk::ImageUsageFlagBits::eColorAttachment, {}, 1, vk::ImageLayout::eGeneral, vk::ImageType::e2D, test.layers > 1 ? vk::ImageViewType::e2DArray : vk::ImageViewType::e2D, @@ -20285,12 +20286,12 @@ public: viewport.x = 0.0f; viewport.y = 0.0f; viewport.width = static_cast<float>(test.width); - viewport.height = 1.0f; + viewport.height = static_cast<float>(test.height); viewport.minDepth = 0.0f; viewport.maxDepth = 1.0f; vk::Rect2D scissor{}; scissor.extent.width = test.width; - scissor.extent.height = 1; + scissor.extent.height = test.height; vk::PipelineViewportStateCreateInfo viewport_state{}; viewport_state.sType = vk::StructureType::ePipelineViewportStateCreateInfo; viewport_state.viewportCount = 1; @@ -20370,7 +20371,7 @@ public: } vk::RenderingInfo rendering{}; rendering.sType = vk::StructureType::eRenderingInfo; - rendering.renderArea.extent = {test.width, 1}; + rendering.renderArea.extent = {test.width, test.height}; rendering.layerCount = test.layers; rendering.colorAttachmentCount = color_blend.attachmentCount; rendering.pColorAttachments = colors.data(); @@ -45419,6 +45420,47 @@ GraphicsCase GraphicsFrontFaceBits(bool reversed, bool all_bits) { return test; } +GraphicsCase GraphicsWave64WholeWaveCount(u32 height, bool lanes_0_32) { + static const char *const names[2][2] = { + {"GraphicsWave64WholeWaveCount8x4Lanes31And63", "GraphicsWave64WholeWaveCount8x4Lanes0And32"}, + {"GraphicsWave64WholeWaveCount8x8Lanes31And63", "GraphicsWave64WholeWaveCount8x8Lanes0And32"}}; + GraphicsCase test; + test.name = names[height == 8u][lanes_0_32]; + test.width = 8; + test.height = height; + auto &code = test.fragment_code; + code = {EncodeVop1(0x01, 13, InlineU32(1)), + EncodeSop1(0x10, 9, 126), + EncodeSop1(0x04, 24, 126), + EncodeSop1(0x28, 106, 126)}; + AppendVop3(&code, 0x101, 13, InlineU32(0), Vgpr(13), 24); + for (u32 shift : {1u, 2u, 4u, 8u}) { + code.push_back(EncodeVop2(0x25, 13, 250, 13)); + code.push_back(EncodeVop2Dpp(13, (lanes_0_32 ? 0x160u : 0x110u) + shift)); + } + code.insert(code.end(), {0xd778100cu, 0x0305830du, + EncodeVop2(0x25, 13, Vgpr(13), 12), + EncodeSop1(0x04, 126, 106)}); + AppendVop3(&code, 0x360, 6, Vgpr(13), InlineU32(lanes_0_32 ? 0u : 31u)); + AppendVop3(&code, 0x360, 7, Vgpr(13), InlineU32(lanes_0_32 ? 32u : 63u)); + code.insert(code.end(), {EncodeSop2(0x00, 8, 6, 7), + EncodeSopc(0x06, 8, 9), + EncodeSop2(0x0a, 10, 242, 128), + EncodeSop2(0x03, 11, 8, 9), + EncodeVop1(0x01, 0, 10), EncodeVop1(0x05, 1, 11), + EncodeVop1(0x01, 2, InlineU32(0)), EncodeVop1(0x01, 3, 242), + EncodeExp0(0x00, 0xf), EncodeExp1(0, 1, 2, 3)}); + AppendEnd(&code); + for (u32 i = 0; i < 8u * height; i++) { + test.expected_pixel.insert(test.expected_pixel.end(), + {0x3f800000u, 0u, 0u, 0x3f800000u}); + } + test.opcodes = {ShaderOpcode::S_BCNT1_I32_B64, ShaderOpcode::V_ADD_NC_U32, + ShaderOpcode::V_PERMLANEX16_B32, ShaderOpcode::V_READLANE_B32, + ShaderOpcode::S_ADD_U32}; + return test; +} + void CheckPixelFrontFaceModes() { constexpr const char *name = "PixelFrontFaceModes"; static const std::array<u32, 1> code = {EncodeSopp(0x01)}; @@ -46484,6 +46526,10 @@ std::vector<GraphicsCase> MakeGraphicsCases() { GraphicsBranchPathFinalVmExportDiscardsInactiveExec(), GraphicsObjectPrimitiveId(false), GraphicsObjectPrimitiveId(true), + GraphicsWave64WholeWaveCount(8, false), + GraphicsWave64WholeWaveCount(8, true), + GraphicsWave64WholeWaveCount(4, false), + GraphicsWave64WholeWaveCount(4, true), }; } ``` </details>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:09:51 +00:00
Author
Owner

@⁠bobrass on GitHub · 2026-10-30 08:00 UTC · original

Thanks for the detailed measurements and repro. I checked current main: WaveReductionMatcher still maps full-wave scans for UMin, UMax, and OR, while the add/count form is not lowered to a subgroup reduction. One integration detail: TestCompleteWaveReduction currently uses IAdd32 for its WrongOperation case, so once add is supported that negative case will need to use a different unsupported operation to keep testing the guard. Your exact wave32 and partly-filled wave64 results give a strong basis for that focused extension.

**@⁠bobrass** on GitHub · 2026-10-30 08:00 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1944#issuecomment-6517913384) Thanks for the detailed measurements and repro. I checked current `main`: `WaveReductionMatcher` still maps full-wave scans for UMin, UMax, and OR, while the add/count form is not lowered to a subgroup reduction. One integration detail: `TestCompleteWaveReduction` currently uses `IAdd32` for its `WrongOperation` case, so once add is supported that negative case will need to use a different unsupported operation to keep testing the guard. Your exact wave32 and partly-filled wave64 results give a strong basis for that focused extension.
Author
Owner

@⁠bobrass on GitHub · 2026-10-30 22:48 UTC · original

Thanks again for the detailed measurements. I opened PR #2008 to lower the proven integer-add scan to a subgroup reduction. It leaves the existing min, max, and OR cases unchanged.

I haven’t run the test suite, built KytyPS5, or run your GPU reproducer. If a build with the change becomes available, the key check is still that the sum equals the active-lane count for both 32-lane and 64-lane fragment subgroups, including the partly filled wave. No game files are needed for that check.

**@⁠bobrass** on GitHub · 2026-10-30 22:48 UTC · [original](https://github.com/KytyPS5/KytyPS5/issues/1944#issuecomment-6535138929) Thanks again for the detailed measurements. I opened [PR #2008](https://git.gtahub.st/github/KytyPS5--KytyPS5/pulls/2008) to lower the proven integer-add scan to a subgroup reduction. It leaves the existing min, max, and OR cases unchanged. I haven’t run the test suite, built KytyPS5, or run your GPU reproducer. If a build with the change becomes available, the key check is still that the sum equals the active-lane count for both 32-lane and 64-lane fragment subgroups, including the partly filled wave. No game files are needed for that check.
This discussion has been locked. Commenting is limited to contributors.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/KytyPS5--KytyPS5#1944
No description provided.