feat(agc): centroid pixel inputs and NoPerspective by I/J pair

SPI_PS_INPUT_ENA/ADDR with PERSP_CENTROID (bit 2) or LINEAR_CENTROID
(bit 6) was rejected; PPSA21564 binds 0x326 (perspective center and
centroid, linear center, POS_X/Y).

The PS input VGPR layout now follows the hardware rule: SPI_PS_INPUT_ADDR
places every input it names in SPI order (two VGPRs per I/J pair, three
for the pull model, one otherwise), loaded or not, and ENA & ADDR selects
the loaded ones. ShaderPixelStageInfo carries inputAddr (replacing
perspectiveCenterVgpr) plus perspectiveCentroid and linearCentroid; the
recompiler places each loaded input with PixelInputVgpr. This also fixes
the system inputs, which were always written from v0 (psSystemInputBase
was never set), so POS_X overwrote the center I in any draw with
barycentrics enabled.

Draws are rasterized single-sampled, where the centroid is the pixel
center: with fragment barycentrics the centroid pairs carry the center
barycentrics (BaryCoordKHR for perspective; the linear center and
centroid pairs now get BaryCoordNoPerspKHR).

NoPerspective was decided for every parameter by one flag (LINEAR_CENTER
loaded). v_interp_p2 now records whether it reads the J of a linear or a
perspective pair, and a parameter is NoPerspective when it is read
through a linear pair; parameters read through neither keep the old
rule. A parameter read through both kinds of pair throws. Only pixel
stages consult this.

The request serializer moves to version 5 (older requests read back with
their loaded inputs as the layout); the cache key and the driver's draw
key compare and hash the new fields. The unsupported-input error names
ENA and ADDR.
This commit is contained in:
Dean Galvin
2026-09-29 13:22:25 +13:00
parent a67b644ff6
commit cb66ec2004
14 changed files with 390 additions and 58 deletions
@@ -1446,7 +1446,7 @@ private:
if (std::memcmp(&s.viewport, &t.viewport, sizeof(VkViewport)) != 0 || std::memcmp(&s.scissor, &t.scissor, sizeof(VkRect2D)) != 0) return false;
const auto& p = a.pixel;
const auto& q = b.pixel;
if (p.interpolatorCount != q.interpolatorCount || p.interpolatorSettings != q.interpolatorSettings || p.wave32 != q.wave32 || p.perspectiveCenterVgpr != q.perspectiveCenterVgpr || p.hasPerspectiveCenterVgpr != q.hasPerspectiveCenterVgpr || p.posX != q.posX || p.posY != q.posY || p.posZ != q.posZ || p.posW != q.posW || p.frontFace != q.frontFace || p.ancillary != q.ancillary || p.sampleShading != q.sampleShading || p.noPerspective != q.noPerspective || p.pixelKillEnable != q.pixelKillEnable || p.depthExportEnable != q.depthExportEnable || p.sampleMaskExportEnable != q.sampleMaskExportEnable || p.earlyZ != q.earlyZ || p.executeOnNoop != q.executeOnNoop || p.targetOutputMode != q.targetOutputMode || p.targetExportMapping != q.targetExportMapping) return false;
if (p.interpolatorCount != q.interpolatorCount || p.interpolatorSettings != q.interpolatorSettings || p.wave32 != q.wave32 || p.inputAddr != q.inputAddr || p.hasPerspectiveCenterVgpr != q.hasPerspectiveCenterVgpr || p.perspectiveCentroid != q.perspectiveCentroid || p.posX != q.posX || p.posY != q.posY || p.posZ != q.posZ || p.posW != q.posW || p.frontFace != q.frontFace || p.ancillary != q.ancillary || p.sampleShading != q.sampleShading || p.noPerspective != q.noPerspective || p.linearCentroid != q.linearCentroid || p.pixelKillEnable != q.pixelKillEnable || p.depthExportEnable != q.depthExportEnable || p.sampleMaskExportEnable != q.sampleMaskExportEnable || p.earlyZ != q.earlyZ || p.executeOnNoop != q.executeOnNoop || p.targetOutputMode != q.targetOutputMode || p.targetExportMapping != q.targetExportMapping) return false;
if (a.roles != b.roles || a.programs.size() != b.programs.size()) return false;
for (std::size_t i = 0; i < a.programs.size(); ++i) {
const auto& x = a.programs[i];
@@ -4378,8 +4378,8 @@ private:
require(pixel.interpolatorCount <= pixel.interpolatorSettings.size(), "pixel stage info interpolator count exceeds its table");
mix(pixel.interpolatorCount);
for (std::uint32_t i = 0; i < pixel.interpolatorCount; ++i) mix(pixel.interpolatorSettings[i]);
mix(pixel.perspectiveCenterVgpr);
for (const bool flag : {pixel.wave32, pixel.hasPerspectiveCenterVgpr, pixel.posX, pixel.posY, pixel.posZ, pixel.posW, pixel.frontFace, pixel.ancillary, pixel.sampleShading, pixel.noPerspective, pixel.pixelKillEnable, pixel.depthExportEnable, pixel.sampleMaskExportEnable, pixel.earlyZ, pixel.executeOnNoop}) mix(flag);
mix(pixel.inputAddr);
for (const bool flag : {pixel.wave32, pixel.hasPerspectiveCenterVgpr, pixel.perspectiveCentroid, pixel.posX, pixel.posY, pixel.posZ, pixel.posW, pixel.frontFace, pixel.ancillary, pixel.sampleShading, pixel.noPerspective, pixel.linearCentroid, pixel.pixelKillEnable, pixel.depthExportEnable, pixel.sampleMaskExportEnable, pixel.earlyZ, pixel.executeOnNoop}) mix(flag);
for (const auto value : pixel.targetOutputMode) mix(value);
for (const auto value : pixel.targetExportMapping) mix(value);
std::lock_guard cacheLock(drawCacheMutex);
@@ -93,9 +93,16 @@ ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& con
const auto ena = read(context, spiPsInputEna, RegisterBank::Context);
const auto addr = read(context, spiPsInputAddr, RegisterBank::Context);
const auto activeInputs = ena & addr;
constexpr std::uint32_t knownMask = 0x1u | 0x2u | 0x10u | 0x20u | 0x100u | 0x200u | 0x400u | 0x800u | 0x1000u | 0x2000u;
using ShaderRecompiler::PixelInput;
using ShaderRecompiler::PixelInputBit;
constexpr std::uint32_t knownMask = PixelInputBit(PixelInput::PerspectiveSample) | PixelInputBit(PixelInput::PerspectiveCenter) | PixelInputBit(PixelInput::PerspectiveCentroid) |
PixelInputBit(PixelInput::LinearSample) | PixelInputBit(PixelInput::LinearCenter) | PixelInputBit(PixelInput::LinearCentroid) |
PixelInputBit(PixelInput::PositionX) | PixelInputBit(PixelInput::PositionY) | PixelInputBit(PixelInput::PositionZ) | PixelInputBit(PixelInput::PositionW) |
PixelInputBit(PixelInput::FrontFace) | PixelInputBit(PixelInput::Ancillary);
if ((activeInputs & ~knownMask) != 0) {
throw std::runtime_error("AGC graphics: unsupported SPI_PS_INPUT_ENA/ADDR bit combination");
char message[128];
std::snprintf(message, sizeof(message), "AGC graphics: unsupported SPI_PS_INPUT_ENA/ADDR bit combination (ena 0x%x addr 0x%x)", ena, addr);
throw std::runtime_error(message);
}
std::array<std::uint32_t, 32> interpolatorSettings{};
for (std::uint32_t i = 0; i < inputNum; ++i) {
@@ -112,32 +119,34 @@ ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& con
for (std::uint32_t i = 0; i < 8u; ++i) {
targetOutputMode[i] = static_cast<std::uint8_t>((colFormat >> (4u * i)) & 0xFu);
}
const bool hasPerspectiveCenterVgpr = (activeInputs & 0x2u) != 0;
const bool pixelKillEnable = ((shaderControl >> 6u) & 0x1u) != 0;
const bool depthExportEnable = (shaderControl & 0x1u) != 0;
const bool sampleMaskExportEnable = ((shaderControl >> 8u) & 0x1u) != 0;
const auto zOrder = (shaderControl >> 4u) & 0x3u;
const auto loaded = [&](PixelInput input) { return (activeInputs & PixelInputBit(input)) != 0; };
return ShaderRecompiler::ShaderPixelStageInfo{
inputNum,
interpolatorSettings,
(inControl & 0x8000u) != 0,
hasPerspectiveCenterVgpr ? ((activeInputs & 0x1u) ? 2u : 0u) : 0u,
hasPerspectiveCenterVgpr,
(activeInputs & 0x100u) != 0,
(activeInputs & 0x200u) != 0,
(activeInputs & 0x400u) != 0,
(activeInputs & 0x800u) != 0,
(activeInputs & 0x1000u) != 0,
(activeInputs & 0x2000u) != 0,
(activeInputs & 0x11u) == 0x11u,
(activeInputs & 0x20u) != 0,
pixelKillEnable,
depthExportEnable,
sampleMaskExportEnable,
zOrder == 1u && !pixelKillEnable && !depthExportEnable && !sampleMaskExportEnable,
((shaderControl >> 10u) & 0x1u) != 0,
targetOutputMode,
exportMappings
.interpolatorCount = inputNum,
.interpolatorSettings = interpolatorSettings,
.wave32 = (inControl & 0x8000u) != 0,
.inputAddr = addr,
.hasPerspectiveCenterVgpr = loaded(PixelInput::PerspectiveCenter),
.perspectiveCentroid = loaded(PixelInput::PerspectiveCentroid),
.posX = loaded(PixelInput::PositionX),
.posY = loaded(PixelInput::PositionY),
.posZ = loaded(PixelInput::PositionZ),
.posW = loaded(PixelInput::PositionW),
.frontFace = loaded(PixelInput::FrontFace),
.ancillary = loaded(PixelInput::Ancillary),
.sampleShading = loaded(PixelInput::PerspectiveSample) && loaded(PixelInput::LinearSample),
.noPerspective = loaded(PixelInput::LinearCenter),
.linearCentroid = loaded(PixelInput::LinearCentroid),
.pixelKillEnable = pixelKillEnable,
.depthExportEnable = depthExportEnable,
.sampleMaskExportEnable = sampleMaskExportEnable,
.earlyZ = zOrder == 1u && !pixelKillEnable && !depthExportEnable && !sampleMaskExportEnable,
.executeOnNoop = ((shaderControl >> 10u) & 0x1u) != 0,
.targetOutputMode = targetOutputMode,
.targetExportMapping = exportMappings
};
}
@@ -5,7 +5,9 @@
#include "prx/libSceAgcDriver/Graphics/include/VertexInput.hpp"
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
#include "prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp"
#include "ControlFlow/RequestSerializer.hpp"
#include <spirv/unified1/spirv.hpp>
#include <algorithm>
#include <array>
#include <bit>
#include <cstdlib>
@@ -13,7 +15,9 @@
#include <initializer_list>
#include <iostream>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
@@ -241,6 +245,124 @@ void ShaderStageTests() {
expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "missing register");
}
constexpr std::array<std::uint8_t, 8> IdentityExports{0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u};
std::vector<spv::BuiltIn> pixelBuiltinsRead(std::uint32_t ena, std::uint32_t addr, std::uint32_t source) {
auto queue = makeState();
queue.context[0x1b3] = ena;
queue.context[0x1b4] = addr;
const auto pixel = AgcDriver::Graphics::DecodePixelStageInfo(queue.context, IdentityExports);
const std::array<std::uint32_t, 3> code{0xf800180fu, source * 0x01010101u, 0xbf810000u};
ShaderRecompiler::RecompileRequest request{};
request.shader = {ShaderRecompiler::ShaderStage::Fragment, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
request.context.pixel = pixel;
request.target.vulkanVersion = 0x00401000u;
request.target.spirvVersion = 0x00010300u;
request.target.subgroupSize = 64;
request.target.fragmentShaderBarycentricEnabled = true;
request.layout.pushConstantSizeBytes = 128;
request.useCache = false;
const auto result = ShaderRecompiler::Recompile(request);
const auto& words = result.spirv.Words();
std::map<std::uint32_t, spv::BuiltIn> builtins;
std::vector<spv::BuiltIn> read;
for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) {
const auto op = static_cast<spv::Op>(words[at] & 0xffffu);
if (op == spv::OpDecorate && words[at + 2] == spv::DecorationBuiltIn) builtins[words[at + 1]] = static_cast<spv::BuiltIn>(words[at + 3]);
if (op != spv::OpLoad && op != spv::OpAccessChain && op != spv::OpInBoundsAccessChain) continue;
const auto found = builtins.find(words[at + 3]);
if (found != builtins.end() && std::find(read.begin(), read.end(), found->second) == read.end()) read.push_back(found->second);
}
return read;
}
bool readsBuiltin(const std::vector<spv::BuiltIn>& read, spv::BuiltIn builtin) {
return std::find(read.begin(), read.end(), builtin) != read.end();
}
std::vector<std::uint32_t> pixelNoPerspectiveLocations() {
auto queue = makeState();
queue.context[0x1b3] = 0x22u;
queue.context[0x1b4] = 0x22u;
queue.context[0x1b6] = 2u;
queue.context[0x191] = 0u;
queue.context[0x192] = 1u;
const auto pixel = AgcDriver::Graphics::DecodePixelStageInfo(queue.context, IdentityExports);
const std::array<std::uint32_t, 8> code{0xc8100000u, 0xc8110001u, 0xc8140402u, 0xc8150403u, 0xf800180fu, 0x05040504u, 0xbf810000u, 0xbf810000u};
ShaderRecompiler::RecompileRequest request{};
request.shader = {ShaderRecompiler::ShaderStage::Fragment, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
request.context.pixel = pixel;
request.target.vulkanVersion = 0x00401000u;
request.target.spirvVersion = 0x00010300u;
request.target.subgroupSize = 64;
request.target.fragmentShaderBarycentricEnabled = true;
request.layout.pushConstantSizeBytes = 128;
request.useCache = false;
const auto result = ShaderRecompiler::Recompile(request);
const auto& words = result.spirv.Words();
std::map<std::uint32_t, std::uint32_t> locations;
std::vector<std::uint32_t> noPerspective;
for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) {
if (static_cast<spv::Op>(words[at] & 0xffffu) != spv::OpDecorate) continue;
if (words[at + 2] == spv::DecorationLocation) locations[words[at + 1]] = words[at + 3];
if (words[at + 2] == spv::DecorationNoPerspective) noPerspective.push_back(words[at + 1]);
}
std::vector<std::uint32_t> result2;
for (const auto id : noPerspective) result2.push_back(locations.count(id) != 0 ? locations.at(id) : 0xffffffffu);
return result2;
}
void PixelInputLayoutTests() {
using ShaderRecompiler::PixelInput;
using ShaderRecompiler::PixelInputVgpr;
auto queue = makeState();
const auto decode = [&](std::uint32_t ena, std::uint32_t addr) {
queue.context[0x1b3] = ena;
queue.context[0x1b4] = addr;
return AgcDriver::Graphics::DecodePixelStageInfo(queue.context, IdentityExports);
};
auto pixel = decode(0x326u, 0x326u);
Require(pixel.inputAddr == 0x326u && pixel.hasPerspectiveCenterVgpr && pixel.perspectiveCentroid && pixel.noPerspective && !pixel.linearCentroid && pixel.posX && pixel.posY && !pixel.posZ, "the centroid input flags were not decoded");
Require(PixelInputVgpr(pixel.inputAddr, PixelInput::PerspectiveCenter) == 0u && PixelInputVgpr(pixel.inputAddr, PixelInput::PerspectiveCentroid) == 2u && PixelInputVgpr(pixel.inputAddr, PixelInput::LinearCenter) == 4u && PixelInputVgpr(pixel.inputAddr, PixelInput::PositionX) == 6u && PixelInputVgpr(pixel.inputAddr, PixelInput::PositionY) == 7u, "the centroid layout moved the inputs");
pixel = decode(0x1146u, 0x1146u);
Require(pixel.linearCentroid && !pixel.noPerspective && PixelInputVgpr(pixel.inputAddr, PixelInput::LinearCentroid) == 4u && PixelInputVgpr(pixel.inputAddr, PixelInput::PositionX) == 6u && PixelInputVgpr(pixel.inputAddr, PixelInput::FrontFace) == 7u, "the linear centroid layout moved the inputs");
pixel = decode(0x506u, 0x7afu);
Require(pixel.inputAddr == 0x7afu && !pixel.posY && pixel.posZ && PixelInputVgpr(pixel.inputAddr, PixelInput::PerspectiveCentroid) == 4u && PixelInputVgpr(pixel.inputAddr, PixelInput::PositionX) == 12u && PixelInputVgpr(pixel.inputAddr, PixelInput::PositionZ) == 14u, "ADDR-only inputs did not reserve their VGPRs");
for (const auto bit : {0x8u, 0x80u, 0x4000u, 0x8000u}) {
expectFailure([&] { static_cast<void>(decode(0x2u | bit, 0x2u | bit)); }, "unsupported SPI_PS_INPUT_ENA/ADDR");
}
pixel = decode(0x546u, 0x7c7u);
{
ShaderRecompiler::RecompileRequest request{};
const std::array<std::uint32_t, 1> code{0xbf810000u};
request.shader = {ShaderRecompiler::ShaderStage::Fragment, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
request.context.pixel = pixel;
const ShaderRecompiler::RequestSerializer serializer;
const auto back = serializer.Deserialize(serializer.Serialize(request));
const auto& p = *back.request.context.pixel;
Require(p.inputAddr == 0x7c7u && p.hasPerspectiveCenterVgpr && p.perspectiveCentroid && p.linearCentroid && !p.noPerspective && p.posX && !p.posY && p.posZ, "the pixel input layout did not survive serialization");
}
for (const auto source : {0u, 2u, 3u}) {
const auto read = pixelBuiltinsRead(0x106u, 0x106u, source);
Require(readsBuiltin(read, spv::BuiltInBaryCoordKHR) && !readsBuiltin(read, spv::BuiltInFragCoord), "a centroid-layout I/J VGPR does not hold the barycentrics: v" + std::to_string(source));
}
const auto noPerspective = pixelNoPerspectiveLocations();
Require(noPerspective.size() == 1 && noPerspective[0] == 1u, "only the parameter interpolated through the linear pair must be NoPerspective");
auto read = pixelBuiltinsRead(0x106u, 0x106u, 4u);
Require(readsBuiltin(read, spv::BuiltInFragCoord) && !readsBuiltin(read, spv::BuiltInBaryCoordKHR), "POS_X is not in v4 after the center and centroid pairs");
read = pixelBuiltinsRead(0x326u, 0x326u, 5u);
Require(readsBuiltin(read, spv::BuiltInBaryCoordNoPerspKHR) && !readsBuiltin(read, spv::BuiltInFragCoord), "LINEAR_CENTER's J is not v5");
read = pixelBuiltinsRead(0x326u, 0x326u, 6u);
Require(readsBuiltin(read, spv::BuiltInFragCoord) && !readsBuiltin(read, spv::BuiltInBaryCoordKHR) && !readsBuiltin(read, spv::BuiltInBaryCoordNoPerspKHR), "POS_X is not in v6 after three I/J pairs");
read = pixelBuiltinsRead(0x102u, 0x106u, 4u);
Require(readsBuiltin(read, spv::BuiltInFragCoord), "an ADDR-only centroid pair did not reserve v2/v3");
read = pixelBuiltinsRead(0x102u, 0x106u, 2u);
Require(!readsBuiltin(read, spv::BuiltInFragCoord) && !readsBuiltin(read, spv::BuiltInBaryCoordKHR), "an ADDR-only centroid pair was loaded");
}
void DisabledColorTests() {
auto queue = makeState();
queue.context[0x8e] = 0;
@@ -1319,6 +1441,7 @@ int main() {
DepthStencilTests();
DisabledColorTests();
ShaderStageTests();
PixelInputLayoutTests();
InitialContextTests();
pushConstantTests();
resourceTests();
@@ -9,12 +9,15 @@
#include "SpirvBackend/SpirvOptimizer.hpp"
#endif
#include "CacheKey.hpp"
#include <spirv/unified1/spirv.hpp>
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <future>
#include <memory>
#include <stdexcept>
#include <span>
#include <string>
#include <string_view>
#include <vector>
@@ -446,6 +449,82 @@ void verifyMeshConfiguration() {
require(key != first && RecompileCacheKey::ContextHash(request) != RecompileCacheKey::ContextHash(other), "the cache keys ignore the mesh configuration");
}
ShaderRecompiler::ShaderPixelStageInfo twoParameterPixel() {
ShaderRecompiler::ShaderPixelStageInfo pixel{};
pixel.interpolatorCount = 2u;
pixel.interpolatorSettings[1] = 1u;
pixel.wave32 = true;
pixel.inputAddr = ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveCenter) | ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::LinearCenter);
pixel.hasPerspectiveCenterVgpr = true;
pixel.noPerspective = true;
pixel.targetOutputMode[0] = 9u;
pixel.targetExportMapping[0] = 0xe4u;
return pixel;
}
std::vector<std::uint32_t> noPerspectiveLocations(std::span<const std::uint32_t> code) {
using namespace ShaderRecompiler;
RecompileRequest request{};
request.shader = {ShaderStage::Fragment, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
request.context.pixel = twoParameterPixel();
request.target.vulkanVersion = 0x00401000u;
request.target.spirvVersion = 0x00010300u;
request.target.subgroupSize = 64;
request.layout.pushConstantSizeBytes = 128;
request.useCache = false;
const auto result = Recompile(request);
const auto& words = result.spirv.Words();
std::map<std::uint32_t, std::uint32_t> locations;
std::vector<std::uint32_t> decorated;
for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) {
if (static_cast<spv::Op>(words[at] & 0xffffu) != spv::OpDecorate) continue;
if (words[at + 2] == spv::DecorationLocation) locations[words[at + 1]] = words[at + 3];
if (words[at + 2] == spv::DecorationNoPerspective) decorated.push_back(words[at + 1]);
}
std::vector<std::uint32_t> result2;
for (const auto id : decorated) result2.push_back(locations.count(id) != 0 ? locations.at(id) : 0xffffffffu);
return result2;
}
void verifyPixelInputs() {
using namespace ShaderRecompiler;
require(PixelInputVgpr(0x326u, PixelInput::PerspectiveCentroid) == 2u && PixelInputVgpr(0x326u, PixelInput::LinearCenter) == 4u && PixelInputVgpr(0x326u, PixelInput::PositionX) == 6u, "the SPI_PS_INPUT_ADDR layout moved the inputs");
require(PixelInputVgpr(0x7afu, PixelInput::PerspectiveCentroid) == 4u && PixelInputVgpr(0x7afu, PixelInput::PositionX) == 12u && PixelInputVgpr(0x7afu, PixelInput::PositionZ) == 14u, "ADDR-only inputs did not reserve their VGPRs");
static constexpr std::array<std::uint32_t, 7> byPair{0xc8100000u, 0xc8110001u, 0xc8140402u, 0xc8150403u, 0xf800180fu, 0x05040504u, 0xbf810000u};
const auto linear = noPerspectiveLocations(byPair);
require(linear.size() == 1u && linear[0] == 1u, "only the parameter interpolated through the linear pair must be NoPerspective");
static constexpr std::array<std::uint32_t, 7> bothPairs{0xc8100000u, 0xc8110001u, 0xc8140002u, 0xc8150003u, 0xf800180fu, 0x05040504u, 0xbf810000u};
expectFailure([&] { static_cast<void>(noPerspectiveLocations(bothPairs)); }, "interpolated through both a perspective and a linear I/J pair", "a parameter read through both pairs was given one interpolation");
static constexpr std::array<std::uint32_t, 1> code{0xbf810000u};
RecompileRequest request{};
request.shader = {ShaderStage::Fragment, 0x30000u, code, 0, {}};
request.context.waveSize = 64;
auto pixel = twoParameterPixel();
pixel.inputAddr |= PixelInputBit(PixelInput::PerspectiveCentroid) | PixelInputBit(PixelInput::LinearCentroid);
pixel.perspectiveCentroid = true;
pixel.linearCentroid = true;
request.context.pixel = pixel;
const auto replay = RequestSerializer{}.Deserialize(RequestSerializer{}.Serialize(request));
const auto& back = *replay.request.context.pixel;
require(back.inputAddr == pixel.inputAddr && back.perspectiveCentroid && back.linearCentroid && back.noPerspective, "the pixel input layout did not survive serialization");
std::vector<std::uint64_t> key;
RecompileCacheKey::Build(request, key);
const auto first = key;
for (const auto change : {0, 1, 2}) {
auto other = request;
auto changed = pixel;
if (change == 0) changed.inputAddr |= PixelInputBit(PixelInput::PerspectiveSample);
if (change == 1) changed.perspectiveCentroid = false;
if (change == 2) changed.linearCentroid = false;
other.context.pixel = changed;
RecompileCacheKey::Build(other, key);
require(key != first && RecompileCacheKey::ContextHash(request) != RecompileCacheKey::ContextHash(other), "the cache keys ignore the pixel input layout");
}
}
}
int main() {
@@ -456,6 +535,7 @@ int main() {
verifyBindlessTable();
verifyProgramCounterRelativeData();
verifyMeshConfiguration();
verifyPixelInputs();
#if ANYPS5_ENABLE_SPIRV_TOOLS
const std::vector<std::uint32_t> minimalSpirv{
0x07230203u, 0x00010000u, 0u, 5u, 0u,
+3 -1
View File
@@ -122,8 +122,9 @@ private:
if (value.interpolatorCount > value.interpolatorSettings.size()) throw std::runtime_error("Shader cache: invalid interpolator count");
for (std::uint32_t i = 0; i < value.interpolatorCount; ++i) append(key, value.interpolatorSettings[i]);
append(key, value.wave32);
append(key, value.perspectiveCenterVgpr);
append(key, value.inputAddr);
append(key, value.hasPerspectiveCenterVgpr);
append(key, value.perspectiveCentroid);
append(key, value.posX);
append(key, value.posY);
append(key, value.posZ);
@@ -132,6 +133,7 @@ private:
append(key, value.ancillary);
append(key, value.sampleShading);
append(key, value.noPerspective);
append(key, value.linearCentroid);
append(key, value.pixelKillEnable);
append(key, value.depthExportEnable);
append(key, value.sampleMaskExportEnable);
@@ -279,8 +279,9 @@ void writePixelInfo(Writer& writer, const ShaderPixelStageInfo& info) {
writer.WriteU32(value);
}
writer.WriteBool(info.wave32);
writer.WriteU32(info.perspectiveCenterVgpr);
writer.WriteU32(info.inputAddr);
writer.WriteBool(info.hasPerspectiveCenterVgpr);
writer.WriteBool(info.perspectiveCentroid);
writer.WriteBool(info.posX);
writer.WriteBool(info.posY);
writer.WriteBool(info.posZ);
@@ -289,6 +290,7 @@ void writePixelInfo(Writer& writer, const ShaderPixelStageInfo& info) {
writer.WriteBool(info.ancillary);
writer.WriteBool(info.sampleShading);
writer.WriteBool(info.noPerspective);
writer.WriteBool(info.linearCentroid);
writer.WriteBool(info.pixelKillEnable);
writer.WriteBool(info.depthExportEnable);
writer.WriteBool(info.sampleMaskExportEnable);
@@ -299,15 +301,19 @@ void writePixelInfo(Writer& writer, const ShaderPixelStageInfo& info) {
}
}
ShaderPixelStageInfo readPixelInfo(Reader& reader) {
ShaderPixelStageInfo readPixelInfo(Reader& reader, std::uint32_t version) {
ShaderPixelStageInfo info{};
info.interpolatorCount = reader.ReadU32();
for (std::uint32_t& value : info.interpolatorSettings) {
value = reader.ReadU32();
}
info.wave32 = reader.ReadBool();
info.perspectiveCenterVgpr = reader.ReadU32();
const auto inputAddrOrCenterVgpr = reader.ReadU32();
info.hasPerspectiveCenterVgpr = reader.ReadBool();
if (version >= 5u) {
info.inputAddr = inputAddrOrCenterVgpr;
info.perspectiveCentroid = reader.ReadBool();
}
info.posX = reader.ReadBool();
info.posY = reader.ReadBool();
info.posZ = reader.ReadBool();
@@ -316,6 +322,7 @@ ShaderPixelStageInfo readPixelInfo(Reader& reader) {
info.ancillary = reader.ReadBool();
info.sampleShading = reader.ReadBool();
info.noPerspective = reader.ReadBool();
if (version >= 5u) info.linearCentroid = reader.ReadBool();
info.pixelKillEnable = reader.ReadBool();
info.depthExportEnable = reader.ReadBool();
info.sampleMaskExportEnable = reader.ReadBool();
@@ -324,6 +331,14 @@ ShaderPixelStageInfo readPixelInfo(Reader& reader) {
for (std::uint8_t& value : info.targetOutputMode) {
value = reader.ReadU8();
}
if (version < 5u) {
const auto input = [](PixelInput value, bool present) { return present ? PixelInputBit(value) : 0u; };
info.inputAddr = input(PixelInput::PerspectiveSample, info.hasPerspectiveCenterVgpr && inputAddrOrCenterVgpr == 2u) | input(PixelInput::PerspectiveCenter, info.hasPerspectiveCenterVgpr) |
input(PixelInput::LinearSample, info.sampleShading) | input(PixelInput::LinearCenter, info.noPerspective) |
input(PixelInput::PositionX, info.posX) | input(PixelInput::PositionY, info.posY) | input(PixelInput::PositionZ, info.posZ) | input(PixelInput::PositionW, info.posW) |
input(PixelInput::FrontFace, info.frontFace) | input(PixelInput::Ancillary, info.ancillary);
if (info.sampleShading) info.inputAddr |= PixelInputBit(PixelInput::PerspectiveSample);
}
return info;
}
@@ -484,7 +499,7 @@ void writeGuestContext(Writer& writer, const GuestContext& context) {
}
}
GuestContext readGuestContext(Reader& reader, DeserializedRequest& result) {
GuestContext readGuestContext(Reader& reader, DeserializedRequest& result, std::uint32_t version) {
GuestContext context{};
context.waveSize = reader.ReadU32();
context.userDataBaseRegister = reader.ReadU32();
@@ -495,7 +510,7 @@ GuestContext readGuestContext(Reader& reader, DeserializedRequest& result) {
context.compute = result.compute;
}
if (reader.ReadBool()) {
result.pixel = readPixelInfo(reader);
result.pixel = readPixelInfo(reader, version);
context.pixel = result.pixel;
}
if (reader.ReadBool()) {
@@ -662,7 +677,7 @@ std::string RequestSerializer::Serialize(const RecompileRequest& request) const
std::string buffer;
Writer writer(buffer);
writer.WriteU32(0x41505335u);
writer.WriteU32(4u);
writer.WriteU32(5u);
writeShaderBinary(writer, request.shader);
writeGuestContext(writer, request.context);
writeSpirvTarget(writer, request.target);
@@ -685,10 +700,10 @@ DeserializedRequest RequestSerializer::Deserialize(std::string_view text) const
Reader reader(decoded);
if (reader.ReadU32() != 0x41505335u) throw std::runtime_error("invalid recompile request signature");
const auto version = reader.ReadU32();
if (version < 1u || version > 4u) throw std::runtime_error("unsupported recompile request serialization version");
if (version < 1u || version > 5u) throw std::runtime_error("unsupported recompile request serialization version");
DeserializedRequest result{};
result.request.shader = readShaderBinary(reader, result.shaderCode, result.shaderHeader);
result.request.context = readGuestContext(reader, result);
result.request.context = readGuestContext(reader, result, version);
result.request.target = readSpirvTarget(reader, result);
result.request.layout = readBindingLayout(reader);
if (reader.ReadBool()) {
@@ -17,6 +17,8 @@ struct IrProgramMetadata {
std::string failureReason;
std::vector<BlockInfo> blockInfo;
std::vector<ExportInfo> exportInfo;
std::uint32_t pixelLinearInputs = 0;
std::uint32_t pixelPerspectiveInputs = 0;
std::vector<IrValue*> dynamicReads;
bool shaderInfoComplete = false;
IrBindingLayout bindings;
@@ -6,6 +6,7 @@
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
namespace ShaderRecompiler {
@@ -159,9 +160,13 @@ struct ShaderComputeInputInfo: ShaderWorkgroupInputInfo {
struct ShaderPixelInputInfo {
std::uint32_t interpolatorSettings[32] = {0};
std::uint32_t inputNum = 0;
std::uint32_t psSystemInputBase = 0;
std::uint32_t customInterpolationMask = 0;
std::uint32_t psPerspectiveCenterVgpr = std::numeric_limits<std::uint32_t>::max();
static constexpr std::uint32_t NoPixelInputVgpr = std::numeric_limits<std::uint32_t>::max();
std::array<std::uint32_t, 16> psInputVgpr = [] {
std::array<std::uint32_t, 16> vgprs{};
vgprs.fill(NoPixelInputVgpr);
return vgprs;
}();
std::uint8_t targetOutputMode[8] = {};
std::array<ShaderColorComponentMapping, 8> targetExportMapping = {};
std::uint32_t scratchSizeDwords = 0;
@@ -183,6 +188,14 @@ struct ShaderPixelInputInfo {
bool HasPositionInput() const {
return psPosX || psPosY || psPosZ || psPosW;
}
[[nodiscard]] bool InputIsLinear(std::uint32_t input, std::uint32_t linearInputs, std::uint32_t perspectiveInputs) const {
const auto bit = input < 32u ? 1u << input : 0u;
if ((linearInputs & perspectiveInputs & bit) != 0u) {
throw std::runtime_error("pixel input " + std::to_string(input) + " is interpolated through both a perspective and a linear I/J pair");
}
return (linearInputs & bit) != 0u || ((perspectiveInputs & bit) == 0u && psNoPerspective);
}
};
struct ShaderStageInputInfo {
+51 -1
View File
@@ -50,12 +50,61 @@ struct ShaderComputeStageInfo {
}
};
enum class PixelInput : std::uint32_t {
PerspectiveSample,
PerspectiveCenter,
PerspectiveCentroid,
PerspectivePullModel,
LinearSample,
LinearCenter,
LinearCentroid,
LineStipple,
PositionX,
PositionY,
PositionZ,
PositionW,
FrontFace,
Ancillary,
SampleCoverage,
PositionFixedPoint,
Count
};
constexpr std::uint32_t PixelInputBit(PixelInput input) {
return 1u << static_cast<std::uint32_t>(input);
}
constexpr std::uint32_t PixelInputVgprCount(PixelInput input) {
switch (input) {
case PixelInput::PerspectiveSample:
case PixelInput::PerspectiveCenter:
case PixelInput::PerspectiveCentroid:
case PixelInput::LinearSample:
case PixelInput::LinearCenter:
case PixelInput::LinearCentroid:
return 2u;
case PixelInput::PerspectivePullModel:
return 3u;
default:
return 1u;
}
}
constexpr std::uint32_t PixelInputVgpr(std::uint32_t inputAddr, PixelInput input) {
std::uint32_t vgpr = 0;
for (std::uint32_t i = 0; i < static_cast<std::uint32_t>(input); ++i) {
if ((inputAddr & (1u << i)) != 0u) vgpr += PixelInputVgprCount(static_cast<PixelInput>(i));
}
return vgpr;
}
struct ShaderPixelStageInfo {
std::uint32_t interpolatorCount;
std::array<std::uint32_t, 32> interpolatorSettings;
bool wave32;
std::uint32_t perspectiveCenterVgpr;
std::uint32_t inputAddr;
bool hasPerspectiveCenterVgpr;
bool perspectiveCentroid;
bool posX;
bool posY;
bool posZ;
@@ -64,6 +113,7 @@ struct ShaderPixelStageInfo {
bool ancillary;
bool sampleShading;
bool noPerspective;
bool linearCentroid;
bool pixelKillEnable;
bool depthExportEnable;
bool sampleMaskExportEnable;
@@ -244,7 +244,8 @@ void DefineInputs(SpirvEmitterState& state) {
} else if (flat) {
state.module.AddAnnotation(spv::OpDecorate, input.variableId, spv::DecorationFlat);
}
if (state.program.Resources().stage == IrShaderStage::Pixel && PixelInfo(state).psNoPerspective && !flat && !input.perVertex) {
const auto& metadata = state.program.Metadata();
if (state.program.Resources().stage == IrShaderStage::Pixel && !flat && !input.perVertex && PixelInfo(state).InputIsLinear(input.location, metadata.pixelLinearInputs, metadata.pixelPerspectiveInputs)) {
state.module.AddAnnotation(spv::OpDecorate, input.variableId, spv::DecorationNoPerspective);
}
state.module.AddAnnotation(spv::OpDecorate, input.variableId, spv::DecorationLocation, PixelParameterLocation(state, input.location));
@@ -12,6 +12,7 @@ public:
TranslationContext(IrProgram& program, IrBlock& block, std::uint32_t vectorLimit);
void TranslateInstruction(const RdnaInstruction& instruction);
void SetPixelInput(const ShaderPixelInputInfo* info) { pixelInput = info; }
void TranslateEmbeddedFetch(const RdnaInstruction& instruction, std::uint32_t attribute, std::uint32_t componentCount, const ShaderBufferResource& resource);
void AddBranchCondition(const BasicBlock& source, BlockInfo& info);
@@ -232,6 +233,7 @@ private:
bool emitMemory(const RdnaInstruction& inst);
IrProgram& program;
const ShaderPixelInputInfo* pixelInput = nullptr;
IrBuilder ir;
IrBlock& block;
IrU1 instructionBranchCondition;
@@ -1,5 +1,6 @@
#include "Translation/AttributeInstructions.hpp"
#include "Translation/TranslationContext.hpp"
#include "Recompiler.hpp"
#include <algorithm>
#include <array>
#include <bit>
@@ -105,6 +106,18 @@ void TranslationContext::vInterpP1F32() {
}
void TranslationContext::vInterpP2F32(const RdnaInstruction& inst) {
if (pixelInput != nullptr && inst.source0.kind == RdnaOperandKind::VectorRegister && inst.source1.value < 32u) {
const auto readsPair = [&](PixelInput input) {
const auto base = pixelInput->psInputVgpr[static_cast<std::size_t>(input)];
return base != ShaderPixelInputInfo::NoPixelInputVgpr && inst.source0.reg == base + 1u;
};
const auto bit = 1u << inst.source1.value;
if (readsPair(PixelInput::LinearCenter) || readsPair(PixelInput::LinearCentroid)) {
program.Metadata().pixelLinearInputs |= bit;
} else if (readsPair(PixelInput::PerspectiveCenter) || readsPair(PixelInput::PerspectiveCentroid)) {
program.Metadata().pixelPerspectiveInputs |= bit;
}
}
IrValue& value = ir.Emit(IrOpcode::GetAttribute, IrType::U32, {&ir.Constant(inst.source1.value), &ir.Constant(inst.source2.value)});
writeOperand(inst.destination, &value);
}
@@ -1,4 +1,5 @@
#include "Translation/InstructionTranslator.hpp"
#include "Recompiler.hpp"
#include "Translation/DispatchInstructions.hpp"
#include "Translation/TranslationContext.hpp"
#include <algorithm>
@@ -6,6 +7,7 @@
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <utility>
namespace ShaderRecompiler {
@@ -315,30 +317,35 @@ void emitEntryPrologue(IrProgram& program, IrBlock& entryBlock, const TranslateO
entryIr.SetVectorReg(static_cast<VectorReg>(8), builtin(StageInputKind::PrimitiveId));
} else if (options.stage == ShaderStageKind::Pixel) {
const auto* ps = options.inputInfo.pixel;
if (options.fragmentShaderBarycentricEnabled && ps->psPerspectiveCenterVgpr != std::numeric_limits<std::uint32_t>::max()) {
entryIr.SetVectorReg(static_cast<VectorReg>(ps->psPerspectiveCenterVgpr), builtin(StageInputKind::BaryCoordSmooth, 0u));
entryIr.SetVectorReg(static_cast<VectorReg>(ps->psPerspectiveCenterVgpr + 1u), builtin(StageInputKind::BaryCoordSmooth, 1u));
const auto vgpr = [&](PixelInput input) { return ps->psInputVgpr[static_cast<std::uint32_t>(input)]; };
const auto loaded = [&](PixelInput input) { return vgpr(input) != ShaderPixelInputInfo::NoPixelInputVgpr; };
if (options.fragmentShaderBarycentricEnabled) {
for (const auto [input, kind] : {std::pair{PixelInput::PerspectiveCenter, StageInputKind::BaryCoordSmooth}, std::pair{PixelInput::PerspectiveCentroid, StageInputKind::BaryCoordSmooth},
std::pair{PixelInput::LinearCenter, StageInputKind::BaryCoordNoPerspective}, std::pair{PixelInput::LinearCentroid, StageInputKind::BaryCoordNoPerspective}}) {
if (!loaded(input)) continue;
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(input)), builtin(kind, 0u));
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(input) + 1u), builtin(kind, 1u));
}
}
std::uint32_t reg = ps->psSystemInputBase;
if (ps->psPosX) {
entryIr.SetVectorReg(static_cast<VectorReg>(reg++), builtin(StageInputKind::FragCoord, 0u));
if (loaded(PixelInput::PositionX)) {
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(PixelInput::PositionX)), builtin(StageInputKind::FragCoord, 0u));
}
if (ps->psPosY) {
entryIr.SetVectorReg(static_cast<VectorReg>(reg++), builtin(StageInputKind::FragCoord, 1u));
if (loaded(PixelInput::PositionY)) {
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(PixelInput::PositionY)), builtin(StageInputKind::FragCoord, 1u));
}
if (ps->psPosZ) {
entryIr.SetVectorReg(static_cast<VectorReg>(reg++), builtin(StageInputKind::FragCoord, 2u));
if (loaded(PixelInput::PositionZ)) {
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(PixelInput::PositionZ)), builtin(StageInputKind::FragCoord, 2u));
}
if (ps->psPosW) {
if (loaded(PixelInput::PositionW)) {
IrValue& reciprocalW = entryIr.BitCastF32(builtin(StageInputKind::FragCoord, 3u));
IrValue& w = entryIr.Emit(IrOpcode::FPRecip32, IrOpcodeType(IrOpcode::FPRecip32), {&reciprocalW});
entryIr.SetVectorReg(static_cast<VectorReg>(reg++), entryIr.BitCastU32(w));
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(PixelInput::PositionW)), entryIr.BitCastU32(w));
}
if (ps->psFrontFace) {
entryIr.SetVectorReg(static_cast<VectorReg>(reg++), builtin(StageInputKind::FrontFacing));
if (loaded(PixelInput::FrontFace)) {
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(PixelInput::FrontFace)), builtin(StageInputKind::FrontFacing));
}
if (ps->psAncillary) {
entryIr.SetVectorReg(static_cast<VectorReg>(reg), builtin(StageInputKind::PackedAncillary));
if (loaded(PixelInput::Ancillary)) {
entryIr.SetVectorReg(static_cast<VectorReg>(vgpr(PixelInput::Ancillary)), builtin(StageInputKind::PackedAncillary));
}
} else if (options.stage == ShaderStageKind::Vertex) {
entryIr.SetVectorReg(static_cast<VectorReg>(5), builtin(StageInputKind::VertexIndex));
@@ -432,6 +439,7 @@ IrProgram InstructionTranslator::Translate(const RdnaProgram& decoded, const Con
for (const auto& cfgBlock : cfg.blocks) {
const auto typedIndex = blockIndices.at(cfgBlock.id);
TranslationContext context(program, *blocks[typedIndex], vectorLimit);
context.SetPixelInput(options.inputInfo.pixel);
for (std::uint32_t index = cfgBlock.instructionBegin; index < cfgBlock.instructionEnd; index++) {
const auto& instruction = decoded.instructions[index];
if (isCodeTableLoad(cfg, instruction.programCounter)) {
@@ -103,9 +103,23 @@ ShaderStageInputInfo BuildShaderStageInputInfo(ShaderStageKind stage, const Gues
pixelStorage.interpolatorSettings[i] = pixel.interpolatorSettings[i];
}
pixelStorage.inputNum = pixel.interpolatorCount;
if (pixel.hasPerspectiveCenterVgpr) {
pixelStorage.psPerspectiveCenterVgpr = pixel.perspectiveCenterVgpr;
}
const auto place = [&](PixelInput input, bool loaded) {
if (!loaded) return;
if ((pixel.inputAddr & PixelInputBit(input)) == 0u) {
throw std::runtime_error("ShaderInputInfoBuilder: a loaded pixel input is missing from SPI_PS_INPUT_ADDR");
}
pixelStorage.psInputVgpr[static_cast<std::uint32_t>(input)] = PixelInputVgpr(pixel.inputAddr, input);
};
place(PixelInput::PerspectiveCenter, pixel.hasPerspectiveCenterVgpr);
place(PixelInput::PerspectiveCentroid, pixel.perspectiveCentroid);
place(PixelInput::LinearCenter, pixel.noPerspective);
place(PixelInput::LinearCentroid, pixel.linearCentroid);
place(PixelInput::PositionX, pixel.posX);
place(PixelInput::PositionY, pixel.posY);
place(PixelInput::PositionZ, pixel.posZ);
place(PixelInput::PositionW, pixel.posW);
place(PixelInput::FrontFace, pixel.frontFace);
place(PixelInput::Ancillary, pixel.ancillary);
for (std::uint32_t i = 0; i < 8; ++i) {
pixelStorage.targetOutputMode[i] = pixel.targetOutputMode[i];
pixelStorage.targetExportMapping[i].packed = pixel.targetExportMapping[i];