From 7ed138d13f5bce5073505e635c55b868a55a7d22 Mon Sep 17 00:00:00 2001 From: Dean Galvin Date: Fri, 2 Oct 2026 12:54:25 +1300 Subject: [PATCH] fix(recompiler): start pixel waves without their helper invocations in EXEC A pixel wave starts with EXEC holding its covered pixels. The other lanes of a partly covered quad run as helpers outside EXEC until the shader asks for whole quads with s_wqm. Pixel shaders started with every invocation in EXEC, Vulkan's helper invocations included. Those take part in subgroup operations, so they counted in ballots (v_mbcnt, s_bcnt1 of EXEC, v_readfirstlane), and a wave's single ds_append or atomic could be performed by one of them: an atomic in a helper invocation does nothing and returns an undefined value. The entry prologue now starts a pixel shader's EXEC as !gl_HelperInvocation, through a new StageInputKind::HelperInvocation builtin input. s_wqm still adds the helpers of every quad with a covered pixel, so derivative code keeps them. The agc_driver_pixel_helper_lanes execution test draws triangles that leave partly covered quads across the target with a pixel shader that stores each covered pixel's v_mbcnt rank in EXEC, the s_bcnt1 count of EXEC and the v_readfirstlane of a pixel id. Every wave's covered pixels must be lanes 0..n-1 of an n-lane EXEC whose first lane is one of them, in wave64 and wave32. It skips on a subgroup narrower than 32, such as lavapipe's. --- .../Graphics/src/ShaderValidation.cpp | 2 +- .../tests/execution/PixelHelperLanes.cpp | 169 ++++++++++++++++++ .../IrMetadata/StageIO.hpp | 1 + .../Optimization/src/ShaderInfoCollector.cpp | 4 + .../src/SpirvMemory/SpirvEmitterHelpers.cpp | 2 + .../SpirvBackend/src/SpirvModuleEmitter.cpp | 7 + .../Translation/src/InstructionTranslator.cpp | 3 + 7 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/PixelHelperLanes.cpp diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp index bf9bf9795..e966e456e 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderValidation.cpp @@ -118,7 +118,7 @@ struct Module { Require(value == spv::BuiltInPosition && signature == "f32x4" && !position, "unsupported or duplicate vertex built-in output"); position = true; } else { - Require(storage == spv::StorageClassInput && ((value == spv::BuiltInFragCoord && signature == "f32x4") || (value == spv::BuiltInFrontFacing && signature == "bool")), "unsupported fragment built-in"); + Require(storage == spv::StorageClassInput && ((value == spv::BuiltInFragCoord && signature == "f32x4") || ((value == spv::BuiltInFrontFacing || value == spv::BuiltInHelperInvocation) && signature == "bool")), "unsupported fragment built-in"); } } }; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/PixelHelperLanes.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/PixelHelperLanes.cpp new file mode 100644 index 000000000..482a0766e --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/PixelHelperLanes.cpp @@ -0,0 +1,169 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Sentinel = 0xdeadbeefu; +constexpr std::uint32_t Width = 192; +constexpr std::uint32_t Height = 128; +alignas(256) std::array Pixels{}; +alignas(256) std::array Lanes{}; + +alignas(256) constexpr std::array VertexCode{ + 0xe0382000, 0x80000005, 0xbf8c3f70, 0xf80008cf, 0x03020100, 0xbf810000, +}; + +alignas(256) constexpr std::array PixelCode{ + 0x7e040f00, 0x7e060f01, 0xd5430006, 0x0409ff03, 0x000000c0, 0xd7650004, 0x0001007e, 0xd7660004, + 0x0002087f, 0xbe88107e, 0x7e120506, 0x8f089708, 0x88080908, 0x34080890, 0x380a0808, 0xe0702000, + 0x80010506, 0x7e0e02f2, 0xf800180f, 0x07070707, 0xbf810000, +}; + +std::array BufferDescriptor(const void* data, std::uint32_t stride, std::uint32_t count) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu) | (stride << 16u), count, 0x01016facu}; +} + +std::vector> Triangles() { + std::vector> vertices; + constexpr std::uint32_t columns = 6; + constexpr std::uint32_t rows = 4; + for (std::uint32_t cell = 0; cell < columns * rows; ++cell) { + const float width = 2.0f / columns; + const float height = 2.0f / rows; + const float x = -1.0f + width * static_cast(cell % columns); + const float y = -1.0f + height * static_cast(cell / columns); + const float skew = 0.07f * static_cast(cell % 5u); + vertices.push_back({x + (0.05f + skew) * width, y + 0.05f * height, 0.5f, 1.0f}); + vertices.push_back({x + 0.93f * width, y + (0.11f + skew) * height, 0.5f, 1.0f}); + vertices.push_back({x + (0.37f - skew * 0.5f) * width, y + 0.91f * height, 0.5f, 1.0f}); + } + return vertices; +} + +void Draw(AgcDriver::VulkanDevice& device, std::uint32_t waveSize) { + static const auto triangles = Triangles(); + Pixels.fill(std::byte{0}); + Lanes.fill(Sentinel); + const auto target = device.Target(); + + std::vector vertexUserData(4, 0u); + const auto vertexBuffer = BufferDescriptor(triangles.data(), 16u, static_cast(triangles.size())); + std::copy(vertexBuffer.begin(), vertexBuffer.end(), vertexUserData.begin()); + const std::array vertexMemory{{{reinterpret_cast(VertexCode.data()), std::as_bytes(std::span(VertexCode))}}}; + ShaderRecompiler::RecompileRequest vertex{ + {ShaderStage::Vertex, reinterpret_cast(VertexCode.data()), VertexCode, 0, {}}, + {waveSize, 0, vertexUserData, std::nullopt, std::nullopt, ShaderRecompiler::ShaderVertexStageInfo{}, vertexMemory}, + target, + {0, 0, 0, 64} + }; + vertex.useCache = false; + const auto vertexResult = ShaderRecompiler::Recompile(vertex); + const auto vertexPush = static_cast(vertexResult.pushConstants.size()); + + ShaderRecompiler::ShaderPixelStageInfo pixel{}; + pixel.wave32 = waveSize == 32u; + pixel.inputAddr = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionX) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PositionY); + pixel.posX = true; + pixel.posY = true; + pixel.targetOutputMode[0] = 9; + pixel.targetExportMapping.fill(0xe4u); + std::vector pixelUserData(8, 0u); + const auto lanes = BufferDescriptor(Lanes.data(), 4u, static_cast(Lanes.size())); + std::copy(lanes.begin(), lanes.end(), pixelUserData.begin() + 4); + const std::array pixelMemory{{{reinterpret_cast(PixelCode.data()), std::as_bytes(std::span(PixelCode))}}}; + ShaderRecompiler::RecompileRequest fragment{ + {ShaderStage::Fragment, reinterpret_cast(PixelCode.data()), PixelCode, 0, {}}, + {waveSize, 0, pixelUserData, std::nullopt, pixel, std::nullopt, pixelMemory}, + target, + {0, 0, vertexPush, 128 - vertexPush} + }; + fragment.useCache = false; + const auto pixelResult = ShaderRecompiler::Recompile(fragment); + const std::array shaders{{ + {ShaderStage::Vertex, &vertexResult, 0}, + {ShaderStage::Fragment, &pixelResult, vertexPush} + }}; + + AgcDriver::Graphics::State state{}; + state.stages = {AgcDriver::Graphics::ShaderPath::Vertex, 0u, waveSize, waveSize, std::nullopt, std::nullopt}; + state.color = {reinterpret_cast(Pixels.data()), {Width, Height}, VK_FORMAT_R8G8B8A8_UNORM, Pixels.size(), 0xe4u}; + state.colors = {state.color}; + state.hasColorTarget = true; + state.renderExtent = {Width, Height}; + state.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; + state.viewport = {0, static_cast(Height), static_cast(Width), -static_cast(Height), 0, 1}; + state.negativeOneToOne = false; + state.scissor = {{0, 0}, {Width, Height}}; + state.cullMode = VK_CULL_MODE_NONE; + state.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; + state.blend.colorWriteMask = 15; + state.blends = {state.blend}; + state.blendConstants = {}; + const AgcDriver::Pm4::DrawParameters draw{0, static_cast(triangles.size()), 0, 1, 0, false}; + device.Draw(state, draw, shaders); + device.WaitIdle(); +} + +void Check(std::uint32_t waveSize) { + const auto what = "pixel helper lanes wave" + std::to_string(waveSize); + std::map>> waves; + std::uint32_t covered = 0; + for (std::uint32_t pixel = 0; pixel < Lanes.size(); ++pixel) { + if (std::to_integer(Pixels[pixel * 4u]) != 255u) { + Require(Lanes[pixel] == Sentinel, what + ": uncovered pixel " + std::to_string(pixel) + " stored " + std::to_string(Lanes[pixel])); + continue; + } + ++covered; + const auto word = Lanes[pixel]; + Require(word != Sentinel, what + ": covered pixel " + std::to_string(pixel) + " stored nothing"); + waves[word & 0xffffu].emplace_back((word >> 16u) & 0x7fu, pixel); + Require((word >> 23u) != 0u, what + ": pixel " + std::to_string(pixel) + " counted no lanes in EXEC"); + } + Require(covered > 1000, what + ": the triangles covered only " + std::to_string(covered) + " pixels"); + for (auto& [first, lanes] : waves) { + std::sort(lanes.begin(), lanes.end()); + for (std::uint32_t lane = 0; lane < lanes.size(); ++lane) { + const auto pixel = lanes[lane].second; + Require(lanes[lane].first == lane, what + ": the wave whose first lane is pixel " + std::to_string(first) + " has " + std::to_string(lanes.size()) + " covered pixels, but pixel " + std::to_string(pixel) + " is lane " + std::to_string(lanes[lane].first) + " of EXEC"); + Require((Lanes[pixel] >> 23u) == lanes.size(), what + ": the wave whose first lane is pixel " + std::to_string(first) + " has " + std::to_string(lanes.size()) + " covered pixels, but EXEC holds " + std::to_string(Lanes[pixel] >> 23u) + " lanes"); + } + Require(lanes.front().second == first, what + ": the first lane of EXEC, pixel " + std::to_string(first) + ", is not a covered pixel of its wave"); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + if (device->Target().subgroupSize < 32u) { + std::printf("skipped, subgroup size %u cannot hold a wave32\n", device->Target().subgroupSize); + return VulkanTestSkipped; + } + for (const auto waveSize : {64u, 32u}) { + for (std::uint32_t pass = 0; pass < 4; ++pass) { + Draw(*device, waveSize); + Check(waveSize); + } + } + std::puts("pixel helper lane tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/StageIO.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/StageIO.hpp index ec74234c2..ca9e8fd91 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/StageIO.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/StageIO.hpp @@ -35,6 +35,7 @@ enum class StageInputKind { GlobalInvocationId, DispatchThreadLimit, Parameter, + HelperInvocation, }; enum class StageOutputKind { diff --git a/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp b/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp index 492fe32da..4fb2a3d89 100644 --- a/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp +++ b/core/shader/recompiler/Optimization/src/ShaderInfoCollector.cpp @@ -128,6 +128,7 @@ void ValidateValueReferences(const IrProgram& program, ShaderStageInputInfo inpu case StageInputKind::InvocationId: case StageInputKind::PrimitiveId: case StageInputKind::FrontFacing: + case StageInputKind::HelperInvocation: case StageInputKind::LocalInvocationIndex: if (component != 0u) { return Fail("typed scalar builtin component is out of range"); @@ -322,6 +323,9 @@ void CollectBuiltinInputs(const IrProgram& program, ShaderInfo& info) { case StageInputKind::FrontFacing: AddInput(info, kind, 0, 1, "gl_FrontFacing"); break; + case StageInputKind::HelperInvocation: + AddInput(info, kind, 0, 1, "gl_HelperInvocation"); + break; case StageInputKind::Layer: AddInput(info, kind, 0, 1, "gl_Layer"); break; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp index 6ba1fbd25..9f6a14fa8 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp @@ -36,6 +36,7 @@ std::uint32_t BuiltInForInput(StageInputKind kind) { case StageInputKind::InstanceIndex: return spv::BuiltInInstanceIndex; case StageInputKind::FragCoord: return spv::BuiltInFragCoord; case StageInputKind::FrontFacing: return spv::BuiltInFrontFacing; + case StageInputKind::HelperInvocation: return spv::BuiltInHelperInvocation; case StageInputKind::Layer: return spv::BuiltInLayer; case StageInputKind::SampleId: return spv::BuiltInSampleId; case StageInputKind::BaryCoordSmooth: return spv::BuiltInBaryCoordKHR; @@ -229,6 +230,7 @@ void DefineInputs(SpirvEmitterState& state) { type = TypeF32Vector(state, 3u); break; case StageInputKind::FrontFacing: + case StageInputKind::HelperInvocation: type = TypeBool(state); break; case StageInputKind::Parameter: diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp index 1be9add80..a2db3e2c8 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvModuleEmitter.cpp @@ -71,6 +71,13 @@ std::uint32_t EmitBuiltinU32(SpirvEmitterState& state, StageInputKind kind, std: state.module.AddFunction(spv::OpSelect, TypeU32(state), bits, value, ConstantU32(state, 0x3f800000u), ConstantU32(state, 0xbf800000u)); return bits; } + if (kind == StageInputKind::HelperInvocation) { + const auto value = state.module.AllocateId(); + const auto bits = state.module.AllocateId(); + state.module.AddFunction(spv::OpLoad, TypeBool(state), value, variable); + state.module.AddFunction(spv::OpSelect, TypeU32(state), bits, value, ConstantU32(state, 1u), ConstantU32(state, 0u)); + return bits; + } if (kind == StageInputKind::VertexIndex || kind == StageInputKind::InstanceIndex || kind == StageInputKind::InvocationId || kind == StageInputKind::PrimitiveId || kind == StageInputKind::Layer || kind == StageInputKind::SampleId) { const auto value = state.module.AllocateId(); const auto bits = state.module.AllocateId(); diff --git a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp index b580ed4e2..a24b01760 100644 --- a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp +++ b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp @@ -194,6 +194,9 @@ void emitEntryPrologue(IrProgram& program, IrBlock& entryBlock, const TranslateO } IrValue* initialExec = &entryIr.ConstantBool(true); + if (options.stage == ShaderStageKind::Pixel) { + initialExec = &entryIr.IEqual(builtin(StageInputKind::HelperInvocation), entryIr.Constant(0u)); + } std::uint32_t totalThreads = 0; const auto* workgroup = shaderWorkgroupInput(options.stage, options.inputInfo); if (workgroup != nullptr) {