Merge pull request #305 from oneandonlydean/fix/recompiler-pixel-helper-lanes

fix(recompiler): start pixel waves without their helper invocations in EXEC
This commit is contained in:
Alexey
2026-10-03 09:47:30 +03:00
committed by GitHub
7 changed files with 187 additions and 1 deletions
@@ -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");
}
}
};
@@ -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 <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
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<std::byte, Width * Height * 4> Pixels{};
alignas(256) std::array<std::uint32_t, Width * Height> Lanes{};
alignas(256) constexpr std::array<std::uint32_t, 6> VertexCode{
0xe0382000, 0x80000005, 0xbf8c3f70, 0xf80008cf, 0x03020100, 0xbf810000,
};
alignas(256) constexpr std::array<std::uint32_t, 21> PixelCode{
0x7e040f00, 0x7e060f01, 0xd5430006, 0x0409ff03, 0x000000c0, 0xd7650004, 0x0001007e, 0xd7660004,
0x0002087f, 0xbe88107e, 0x7e120506, 0x8f089708, 0x88080908, 0x34080890, 0x380a0808, 0xe0702000,
0x80010506, 0x7e0e02f2, 0xf800180f, 0x07070707, 0xbf810000,
};
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t stride, std::uint32_t count) {
const auto address = reinterpret_cast<std::uintptr_t>(data);
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu) | (stride << 16u), count, 0x01016facu};
}
std::vector<std::array<float, 4>> Triangles() {
std::vector<std::array<float, 4>> 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<float>(cell % columns);
const float y = -1.0f + height * static_cast<float>(cell / columns);
const float skew = 0.07f * static_cast<float>(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<std::uint32_t> vertexUserData(4, 0u);
const auto vertexBuffer = BufferDescriptor(triangles.data(), 16u, static_cast<std::uint32_t>(triangles.size()));
std::copy(vertexBuffer.begin(), vertexBuffer.end(), vertexUserData.begin());
const std::array<ShaderRecompiler::MemoryRegion, 1> vertexMemory{{{reinterpret_cast<std::uintptr_t>(VertexCode.data()), std::as_bytes(std::span(VertexCode))}}};
ShaderRecompiler::RecompileRequest vertex{
{ShaderStage::Vertex, reinterpret_cast<std::uintptr_t>(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<std::uint32_t>(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<std::uint32_t> pixelUserData(8, 0u);
const auto lanes = BufferDescriptor(Lanes.data(), 4u, static_cast<std::uint32_t>(Lanes.size()));
std::copy(lanes.begin(), lanes.end(), pixelUserData.begin() + 4);
const std::array<ShaderRecompiler::MemoryRegion, 1> pixelMemory{{{reinterpret_cast<std::uintptr_t>(PixelCode.data()), std::as_bytes(std::span(PixelCode))}}};
ShaderRecompiler::RecompileRequest fragment{
{ShaderStage::Fragment, reinterpret_cast<std::uintptr_t>(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<AgcDriver::Graphics::CompiledShader, 2> 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<std::uintptr_t>(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<float>(Height), static_cast<float>(Width), -static_cast<float>(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<std::uint32_t>(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<std::uint32_t, std::vector<std::pair<std::uint32_t, std::uint32_t>>> waves;
std::uint32_t covered = 0;
for (std::uint32_t pixel = 0; pixel < Lanes.size(); ++pixel) {
if (std::to_integer<std::uint8_t>(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;
}
}
@@ -35,6 +35,7 @@ enum class StageInputKind {
GlobalInvocationId,
DispatchThreadLimit,
Parameter,
HelperInvocation,
};
enum class StageOutputKind {
@@ -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;
@@ -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:
@@ -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();
@@ -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) {