diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp index f38b2a916..315cb6bf8 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp @@ -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); diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp index a45d6ee4b..788458ce7 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp @@ -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 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((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 }; } diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index e6fb5904c..1c4c6b45b 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -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 +#include #include #include #include @@ -13,7 +15,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -241,6 +245,124 @@ void ShaderStageTests() { expectFailure([&] { AgcDriver::Graphics::DecodeState(queue); }, "missing register"); } +constexpr std::array IdentityExports{0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u}; + +std::vector 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 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 builtins; + std::vector read; + for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) { + const auto op = static_cast(words[at] & 0xffffu); + if (op == spv::OpDecorate && words[at + 2] == spv::DecorationBuiltIn) builtins[words[at + 1]] = static_cast(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& read, spv::BuiltIn builtin) { + return std::find(read.begin(), read.end(), builtin) != read.end(); +} + +std::vector 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 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 locations; + std::vector noPerspective; + for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) { + if (static_cast(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 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(decode(0x2u | bit, 0x2u | bit)); }, "unsupported SPI_PS_INPUT_ENA/ADDR"); + } + pixel = decode(0x546u, 0x7c7u); + { + ShaderRecompiler::RecompileRequest request{}; + const std::array 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(); diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp index 8ef194d1c..d330c63be 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp @@ -9,12 +9,15 @@ #include "SpirvBackend/SpirvOptimizer.hpp" #endif #include "CacheKey.hpp" +#include #include #include #include +#include #include #include #include +#include #include #include #include @@ -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 noPerspectiveLocations(std::span 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 locations; + std::vector decorated; + for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) { + if (static_cast(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 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 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 bothPairs{0xc8100000u, 0xc8110001u, 0xc8140002u, 0xc8150003u, 0xf800180fu, 0x05040504u, 0xbf810000u}; + expectFailure([&] { static_cast(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 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 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 minimalSpirv{ 0x07230203u, 0x00010000u, 0u, 5u, 0u, diff --git a/core/shader/recompiler/CacheKey.hpp b/core/shader/recompiler/CacheKey.hpp index c7515d453..aacc33f90 100644 --- a/core/shader/recompiler/CacheKey.hpp +++ b/core/shader/recompiler/CacheKey.hpp @@ -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); diff --git a/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp b/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp index 2a4ee6295..f483746f1 100644 --- a/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp +++ b/core/shader/recompiler/ControlFlow/src/RequestSerializer.cpp @@ -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()) { diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ProgramMetadata.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ProgramMetadata.hpp index ce67ea273..99cf118a7 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ProgramMetadata.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrMetadata/ProgramMetadata.hpp @@ -17,6 +17,8 @@ struct IrProgramMetadata { std::string failureReason; std::vector blockInfo; std::vector exportInfo; + std::uint32_t pixelLinearInputs = 0; + std::uint32_t pixelPerspectiveInputs = 0; std::vector dynamicReads; bool shaderInfoComplete = false; IrBindingLayout bindings; diff --git a/core/shader/recompiler/Optimization/include/Optimization/ShaderStageInputInfo.hpp b/core/shader/recompiler/Optimization/include/Optimization/ShaderStageInputInfo.hpp index e8915fadd..e975e6318 100644 --- a/core/shader/recompiler/Optimization/include/Optimization/ShaderStageInputInfo.hpp +++ b/core/shader/recompiler/Optimization/include/Optimization/ShaderStageInputInfo.hpp @@ -6,6 +6,7 @@ #include #include #include +#include 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::max(); + static constexpr std::uint32_t NoPixelInputVgpr = std::numeric_limits::max(); + std::array psInputVgpr = [] { + std::array vgprs{}; + vgprs.fill(NoPixelInputVgpr); + return vgprs; + }(); std::uint8_t targetOutputMode[8] = {}; std::array 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 { diff --git a/core/shader/recompiler/Recompiler.hpp b/core/shader/recompiler/Recompiler.hpp index 59c3e0ca9..6fcb04b64 100644 --- a/core/shader/recompiler/Recompiler.hpp +++ b/core/shader/recompiler/Recompiler.hpp @@ -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(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(input); ++i) { + if ((inputAddr & (1u << i)) != 0u) vgpr += PixelInputVgprCount(static_cast(i)); + } + return vgpr; +} + struct ShaderPixelStageInfo { std::uint32_t interpolatorCount; std::array 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; diff --git a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp index 377a084ee..c2db6eea9 100644 --- a/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp +++ b/core/shader/recompiler/SpirvBackend/src/SpirvMemory/SpirvEmitterHelpers.cpp @@ -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)); diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index db47cd652..73e1d092e 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -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; diff --git a/core/shader/recompiler/Translation/src/AttributeInstructions.cpp b/core/shader/recompiler/Translation/src/AttributeInstructions.cpp index 7156c134a..a39ace36a 100644 --- a/core/shader/recompiler/Translation/src/AttributeInstructions.cpp +++ b/core/shader/recompiler/Translation/src/AttributeInstructions.cpp @@ -1,5 +1,6 @@ #include "Translation/AttributeInstructions.hpp" #include "Translation/TranslationContext.hpp" +#include "Recompiler.hpp" #include #include #include @@ -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(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); } diff --git a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp index 3f88ce180..7f898e969 100644 --- a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp +++ b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp @@ -1,4 +1,5 @@ #include "Translation/InstructionTranslator.hpp" +#include "Recompiler.hpp" #include "Translation/DispatchInstructions.hpp" #include "Translation/TranslationContext.hpp" #include @@ -6,6 +7,7 @@ #include #include #include +#include namespace ShaderRecompiler { @@ -315,30 +317,35 @@ void emitEntryPrologue(IrProgram& program, IrBlock& entryBlock, const TranslateO entryIr.SetVectorReg(static_cast(8), builtin(StageInputKind::PrimitiveId)); } else if (options.stage == ShaderStageKind::Pixel) { const auto* ps = options.inputInfo.pixel; - if (options.fragmentShaderBarycentricEnabled && ps->psPerspectiveCenterVgpr != std::numeric_limits::max()) { - entryIr.SetVectorReg(static_cast(ps->psPerspectiveCenterVgpr), builtin(StageInputKind::BaryCoordSmooth, 0u)); - entryIr.SetVectorReg(static_cast(ps->psPerspectiveCenterVgpr + 1u), builtin(StageInputKind::BaryCoordSmooth, 1u)); + const auto vgpr = [&](PixelInput input) { return ps->psInputVgpr[static_cast(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(vgpr(input)), builtin(kind, 0u)); + entryIr.SetVectorReg(static_cast(vgpr(input) + 1u), builtin(kind, 1u)); + } } - std::uint32_t reg = ps->psSystemInputBase; - if (ps->psPosX) { - entryIr.SetVectorReg(static_cast(reg++), builtin(StageInputKind::FragCoord, 0u)); + if (loaded(PixelInput::PositionX)) { + entryIr.SetVectorReg(static_cast(vgpr(PixelInput::PositionX)), builtin(StageInputKind::FragCoord, 0u)); } - if (ps->psPosY) { - entryIr.SetVectorReg(static_cast(reg++), builtin(StageInputKind::FragCoord, 1u)); + if (loaded(PixelInput::PositionY)) { + entryIr.SetVectorReg(static_cast(vgpr(PixelInput::PositionY)), builtin(StageInputKind::FragCoord, 1u)); } - if (ps->psPosZ) { - entryIr.SetVectorReg(static_cast(reg++), builtin(StageInputKind::FragCoord, 2u)); + if (loaded(PixelInput::PositionZ)) { + entryIr.SetVectorReg(static_cast(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(reg++), entryIr.BitCastU32(w)); + entryIr.SetVectorReg(static_cast(vgpr(PixelInput::PositionW)), entryIr.BitCastU32(w)); } - if (ps->psFrontFace) { - entryIr.SetVectorReg(static_cast(reg++), builtin(StageInputKind::FrontFacing)); + if (loaded(PixelInput::FrontFace)) { + entryIr.SetVectorReg(static_cast(vgpr(PixelInput::FrontFace)), builtin(StageInputKind::FrontFacing)); } - if (ps->psAncillary) { - entryIr.SetVectorReg(static_cast(reg), builtin(StageInputKind::PackedAncillary)); + if (loaded(PixelInput::Ancillary)) { + entryIr.SetVectorReg(static_cast(vgpr(PixelInput::Ancillary)), builtin(StageInputKind::PackedAncillary)); } } else if (options.stage == ShaderStageKind::Vertex) { entryIr.SetVectorReg(static_cast(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)) { diff --git a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp index 2525f99cd..32887b3bc 100644 --- a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp +++ b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp @@ -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(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];