mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #469 from Pacuka/fix/agc-null-pixel-program
fix(agc): draw without a pixel program when SPI_SHADER_PGM_PS is unset
This commit is contained in:
@@ -39,6 +39,8 @@ using ShaderRegistry = std::map<std::uint64_t, std::shared_ptr<const ShaderSnaps
|
||||
|
||||
bool FailureMemo();
|
||||
|
||||
std::uint64_t NullPixelProgramAddress();
|
||||
|
||||
std::shared_ptr<const ShaderRecompiler::SourceHandle> SourceHandleFor(const ShaderSnapshot& snapshot, std::size_t codeOffset, std::uint64_t deviceSerial, const ShaderRecompiler::RecompileRequest& request, bool bypass, const std::string** poisoned = nullptr);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "prx/libSceAgcDriver/Execution/include/Driver/Driver.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/Driver/Shaders/ShaderRegistry.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/Driver/Diagnostics.hpp"
|
||||
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
|
||||
|
||||
@@ -15,6 +16,8 @@ std::shared_ptr<DrawDecode> Driver::decodeDraw(const QueueState& queue, const Su
|
||||
return (static_cast<std::uint64_t>(readRegister(queue.shader, base)) << 8u) | (static_cast<std::uint64_t>(high) << 40u);
|
||||
};
|
||||
const auto prepare = [&](std::uint64_t address, std::uint8_t type, Stage stage, std::uint32_t rsrc2, std::uint32_t userDataBase) {
|
||||
const bool nullPixel = address == 0 && stage == Stage::Fragment;
|
||||
if (nullPixel) address = NullPixelProgramAddress();
|
||||
auto it = submission.shaders->upper_bound(address);
|
||||
require(it != submission.shaders->begin(), "graphics program does not belong to a registered shader");
|
||||
--it;
|
||||
@@ -22,7 +25,7 @@ std::shared_ptr<DrawDecode> Driver::decodeDraw(const QueueState& queue, const Su
|
||||
require(address - snapshot.codeAddress < snapshot.code.size() * sizeof(std::uint32_t), "graphics program is outside registered shader code");
|
||||
require(snapshot.type == type, "graphics program refers to an incompatible shader binary type");
|
||||
Graphics::NoteRegisterRead(Graphics::RegisterBank::Shader, rsrc2);
|
||||
const auto resources = readRegister(queue.shader, rsrc2);
|
||||
const auto resources = nullPixel && !queue.shader.contains(rsrc2) ? 0u : readRegister(queue.shader, rsrc2);
|
||||
const auto userCount = ((resources >> 1u) & 0x1fu) | (((resources >> 27u) & 1u) << 5u);
|
||||
require(userCount <= 32, "graphics user SGPR count exceeds the register bank");
|
||||
const auto codeOffset = static_cast<std::size_t>((address - snapshot.codeAddress) / sizeof(std::uint32_t));
|
||||
@@ -86,9 +89,14 @@ std::shared_ptr<DrawDecode> Driver::decodeDraw(const QueueState& queue, const Su
|
||||
} else {
|
||||
append(0xc8, 2, Stage::Vertex, 0x8b, 0x8c, Role::Main);
|
||||
}
|
||||
const bool nullPixel = Graphics::PixelProgramUnset(queue);
|
||||
if (nullPixel) {
|
||||
const auto rejection = Graphics::NullPixelProgramRejection(queue);
|
||||
require(rejection.empty(), rejection.c_str());
|
||||
}
|
||||
append(0x008, 1, Stage::Fragment, 0x00b, 0x00c, Role::Fragment);
|
||||
programs.back().firstUserSgpr = 0;
|
||||
product->pixel = Graphics::DecodePixelStageInfo(queue.context, Graphics::ExportMappings(graphics));
|
||||
product->pixel = Graphics::DecodePixelStageInfo(queue.context, Graphics::ExportMappings(graphics), nullPixel);
|
||||
return product;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,6 +77,22 @@ std::shared_ptr<const ShaderRecompiler::SourceHandle> SourceHandleFor(const Shad
|
||||
return handle;
|
||||
}
|
||||
|
||||
alignas(256) static const std::uint32_t NullPixelCode[64] = {0xbf810000u};
|
||||
static const Shader NullPixelShader = [] {
|
||||
Shader shader{};
|
||||
shader.file_header = 0x34333231u;
|
||||
shader.version = 0x18u;
|
||||
shader.code = NullPixelCode;
|
||||
shader.header_size = sizeof(Shader);
|
||||
shader.shader_size = sizeof(NullPixelCode);
|
||||
shader.type = 1;
|
||||
return shader;
|
||||
}();
|
||||
|
||||
std::uint64_t NullPixelProgramAddress() {
|
||||
return reinterpret_cast<std::uintptr_t>(NullPixelCode);
|
||||
}
|
||||
|
||||
void Driver::RegisterShader(const Shader* shader) {
|
||||
CheckFailure();
|
||||
GuestMemory::CheckRange(shader, sizeof(Shader), alignof(Shader));
|
||||
@@ -107,6 +123,13 @@ void Driver::RegisterShader(const Shader* shader) {
|
||||
if (shaders == nullptr) shaders = std::make_shared<ShaderRegistry>();
|
||||
else if (shaders.use_count() != 1) shaders = std::make_shared<ShaderRegistry>(*shaders);
|
||||
shaders->insert_or_assign(address, std::make_shared<const ShaderSnapshot>(std::move(snapshot)));
|
||||
if (shaders->find(NullPixelProgramAddress()) == shaders->end()) {
|
||||
ShaderSnapshot null{NullPixelProgramAddress(), reinterpret_cast<std::uintptr_t>(&NullPixelShader), NullPixelShader.type, {}, {}};
|
||||
null.code.assign(std::begin(NullPixelCode), std::end(NullPixelCode));
|
||||
null.header.resize(sizeof(Shader));
|
||||
std::memcpy(null.header.data(), &NullPixelShader, sizeof(Shader));
|
||||
shaders->insert_or_assign(NullPixelProgramAddress(), std::make_shared<const ShaderSnapshot>(std::move(null)));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
namespace AgcDriver::Graphics {
|
||||
|
||||
ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array<std::uint8_t, 8>& exportMappings);
|
||||
ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array<std::uint8_t, 8>& exportMappings, bool nullProgram = false);
|
||||
ShaderRecompiler::ShaderComputeStageInfo DecodeComputeStageInfo(const Registers& shader);
|
||||
// A guest range DecodeVertexStageInfo read (an attribute word, a vertex V#) with the bytes as read:
|
||||
// the draw cache validates them by value with the stage's capture (design_cpu_final rule RD).
|
||||
|
||||
@@ -111,6 +111,8 @@ std::optional<ColorMetadataPass> DecodeColorMetadataPass(const QueueState& queue
|
||||
// this precheck covers, evaluated without exceptions before the draw is decoded; empty when they
|
||||
// pass (DecodeState still checks everything). A register a rule needs that is absent is no verdict.
|
||||
std::string DrawRejection(const QueueState& queue, bool indexed);
|
||||
bool PixelProgramUnset(const QueueState& queue);
|
||||
std::string NullPixelProgramRejection(const QueueState& queue);
|
||||
|
||||
// The recording facade of the draw decoders (design_cpu_final M8, step 8a): every register read
|
||||
// of DecodeShaderStages, DecodeState, DrawRejection, DecodePixelStageInfo and Driver::draw's
|
||||
|
||||
@@ -25,6 +25,7 @@ constexpr std::uint32_t spiPsInputAddr = 0x1B4;
|
||||
constexpr std::uint32_t spiPsInControl = 0x1B6;
|
||||
constexpr std::uint32_t dbShaderControl = 0x203;
|
||||
constexpr std::uint32_t spiShaderColFormat = 0x1C5;
|
||||
constexpr std::uint32_t defaultPixelInputs = 0x2u;
|
||||
|
||||
std::uint32_t read(const Registers& registers, std::uint32_t offset, RegisterBank bank) {
|
||||
NoteRegisterRead(bank, offset);
|
||||
@@ -37,6 +38,12 @@ std::uint32_t read(const Registers& registers, std::uint32_t offset, RegisterBan
|
||||
return it->second;
|
||||
}
|
||||
|
||||
std::uint32_t readOr(const Registers& registers, std::uint32_t offset, RegisterBank bank, std::uint32_t fallback) {
|
||||
NoteRegisterRead(bank, offset);
|
||||
const auto it = registers.find(offset);
|
||||
return it == registers.end() ? fallback : it->second;
|
||||
}
|
||||
|
||||
template <typename T> T _readHeaderPod(std::span<const std::byte> header, std::uint64_t headerAddress, const void* pointer) {
|
||||
if (pointer == nullptr) throw std::runtime_error("AGC graphics: null AGC header pointer");
|
||||
const auto address = reinterpret_cast<std::uint64_t>(pointer);
|
||||
@@ -84,14 +91,14 @@ ShaderRecompiler::ShaderComputeStageInfo DecodeComputeStageInfo(const Registers&
|
||||
};
|
||||
}
|
||||
|
||||
ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array<std::uint8_t, 8>& exportMappings) {
|
||||
ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context, const std::array<std::uint8_t, 8>& exportMappings, bool nullProgram) {
|
||||
const auto inControl = read(context, spiPsInControl, RegisterBank::Context);
|
||||
const auto inputNum = inControl & 0x3Fu;
|
||||
if (inputNum > 32u) {
|
||||
throw std::runtime_error("AGC graphics: SPI_PS_IN_CONTROL input count exceeds 32");
|
||||
}
|
||||
const auto ena = read(context, spiPsInputEna, RegisterBank::Context);
|
||||
const auto addr = read(context, spiPsInputAddr, RegisterBank::Context);
|
||||
const auto ena = nullProgram ? readOr(context, spiPsInputEna, RegisterBank::Context, defaultPixelInputs) : read(context, spiPsInputEna, RegisterBank::Context);
|
||||
const auto addr = nullProgram ? readOr(context, spiPsInputAddr, RegisterBank::Context, defaultPixelInputs) : read(context, spiPsInputAddr, RegisterBank::Context);
|
||||
const auto activeInputs = ena & addr;
|
||||
using ShaderRecompiler::PixelInput;
|
||||
using ShaderRecompiler::PixelInputBit;
|
||||
@@ -114,7 +121,7 @@ ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& con
|
||||
if (((shaderControl >> 13u) & 0x3u) != 0) {
|
||||
throw std::runtime_error("AGC graphics: DB_SHADER_CONTROL.CONSERVATIVE_Z_EXPORT is unsupported");
|
||||
}
|
||||
const auto colFormat = read(context, spiShaderColFormat, RegisterBank::Context);
|
||||
const auto colFormat = nullProgram ? readOr(context, spiShaderColFormat, RegisterBank::Context, 0u) : read(context, spiShaderColFormat, RegisterBank::Context);
|
||||
std::array<std::uint8_t, 8> targetOutputMode{};
|
||||
for (std::uint32_t i = 0; i < 8u; ++i) {
|
||||
targetOutputMode[i] = static_cast<std::uint8_t>((colFormat >> (4u * i)) & 0xFu);
|
||||
|
||||
@@ -516,7 +516,7 @@ State DecodeState(const QueueState& queue) {
|
||||
// CB_COLOR_CONTROL mode 0 disables color writes, which only matters when a target is written.
|
||||
if (const auto colorControl = read(cx, 0x202); !colorControlSupported(colorControl, result.hasColorTarget)) throw std::runtime_error(colorControlMessage(colorControl));
|
||||
zero(cx, 0x1c4, ~0u, "depth or sample-mask export");
|
||||
const auto exportFormat = read(cx, 0x1c5);
|
||||
const auto exportFormat = result.hasColorTarget ? read(cx, 0x1c5) : 0u;
|
||||
APS5_LOG_OUT_DEBUG("Export format=%u", exportFormat);
|
||||
// SPI_SHADER_POS_FORMAT: POS0 must be a 4-component position; later vectors carry the misc/clip
|
||||
// exports that PA_CL_VS_OUT_CNTL validation above already limits to ignored layer/viewport data.
|
||||
@@ -776,8 +776,7 @@ std::string DrawRejection(const QueueState& queue, bool indexed) {
|
||||
std::uint32_t targetMask = 0, shaderMask = 0;
|
||||
if (value(cx, 0x8e, targetMask) && value(cx, 0x8f, shaderMask) && value(cx, 0x202, word) && !colorControlSupported(word, (targetMask & shaderMask) != 0)) return colorControlMessage(word);
|
||||
if (auto reason = nonzero(cx, 0x1c4, ~0u, "depth or sample-mask export"); !reason.empty()) return reason;
|
||||
// The pixel stage decode (ShaderInputState.cpp) reads these after DecodeState and the program
|
||||
// prepare; a bank without them fails there with this message.
|
||||
if (PixelProgramUnset(queue)) return NullPixelProgramRejection(queue);
|
||||
for (const auto offset : {0x1b3u, 0x1b4u, 0x1c5u}) {
|
||||
if (find(cx, offset) != cx.end()) continue;
|
||||
char text[64];
|
||||
@@ -787,6 +786,20 @@ std::string DrawRejection(const QueueState& queue, bool indexed) {
|
||||
return {};
|
||||
}
|
||||
|
||||
bool PixelProgramUnset(const QueueState& queue) {
|
||||
const auto low = find(queue.shader, 0x008, RegisterBank::Shader);
|
||||
const auto high = find(queue.shader, 0x009, RegisterBank::Shader);
|
||||
return low != queue.shader.end() && high != queue.shader.end() && low->second == 0 && high->second == 0;
|
||||
}
|
||||
|
||||
std::string NullPixelProgramRejection(const QueueState& queue) {
|
||||
const auto targetMask = find(queue.context, 0x8e);
|
||||
const auto shaderMask = find(queue.context, 0x8f);
|
||||
if (targetMask == queue.context.end() || shaderMask == queue.context.end()) return "AGC graphics: a draw without a pixel program needs CB_TARGET_MASK and CB_SHADER_MASK";
|
||||
if ((targetMask->second & shaderMask->second) != 0) return "AGC graphics: a draw without a pixel program writes color";
|
||||
return {};
|
||||
}
|
||||
|
||||
std::vector<RegisterRead>*& RegisterReadLog() {
|
||||
static thread_local std::vector<RegisterRead>* log = nullptr;
|
||||
return log;
|
||||
|
||||
@@ -191,6 +191,25 @@ void stateTests() {
|
||||
log.clear();
|
||||
static_cast<void>(AgcDriver::Graphics::DecodeState(queue));
|
||||
Require(log.empty(), "the register facade recorded without a log");
|
||||
queue = makeState();
|
||||
queue.context.erase(0x1b3);
|
||||
queue.context.erase(0x1b4);
|
||||
queue.context.erase(0x1c5);
|
||||
queue.shader[0x008] = 0x100;
|
||||
queue.shader[0x009] = 0;
|
||||
Require(!AgcDriver::Graphics::PixelProgramUnset(queue), "a pixel program address was read as unset");
|
||||
Require(AgcDriver::Graphics::DrawRejection(queue, true).find("missing register at DWORD 0x1b3") != std::string::npos, "a real pixel program without SPI_PS_INPUT_ENA was accepted");
|
||||
expectFailure([&] { AgcDriver::Graphics::DecodePixelStageInfo(queue.context, std::array<std::uint8_t, 8>{0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u, 0xe4u}); }, "missing register");
|
||||
queue.shader[0x008] = 0;
|
||||
Require(AgcDriver::Graphics::PixelProgramUnset(queue), "a zero pixel program address was not read as unset");
|
||||
Require(AgcDriver::Graphics::DrawRejection(queue, true).find("writes color") != std::string::npos, "a draw without a pixel program that writes color was accepted");
|
||||
queue.context[0x8e] = 0;
|
||||
Require(AgcDriver::Graphics::DrawRejection(queue, true).empty(), "a depth-only draw without a pixel program was rejected");
|
||||
state = AgcDriver::Graphics::DecodeState(queue);
|
||||
Require(!state.hasColorTarget, "a depth-only draw without a pixel program decoded a color target");
|
||||
const auto unset = AgcDriver::Graphics::DecodePixelStageInfo(queue.context, AgcDriver::Graphics::ExportMappings(state), true);
|
||||
Require(unset.inputAddr == ShaderRecompiler::PixelInputBit(ShaderRecompiler::PixelInput::PerspectiveCenter), "unset pixel inputs of the null program did not read as PERSP_CENTER_ENA");
|
||||
for (const auto mode : unset.targetOutputMode) Require(mode == 0, "an unset SPI_SHADER_COL_FORMAT exported a color");
|
||||
}
|
||||
|
||||
void hardwareScreenOffsetTests() {
|
||||
|
||||
Reference in New Issue
Block a user