Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ class Driver {
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];
Expand Down Expand Up @@ -4378,8 +4378,8 @@ class Driver {
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);
Expand Down
55 changes: 32 additions & 23 deletions core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
};
}

Expand Down
123 changes: 123 additions & 0 deletions core/libs/prx/libSceAgcDriver/tests/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
#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>
#include <cstring>
#include <initializer_list>
#include <iostream>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1319,6 +1441,7 @@ int main() {
DepthStencilTests();
DisabledColorTests();
ShaderStageTests();
PixelInputLayoutTests();
InitialContextTests();
pushConstantTests();
resourceTests();
Expand Down
Loading
Loading