feat(agcDriver): add draw handling and shader recompile preparation

This commit is contained in:
boykopovar
2026-09-14 12:06:35 +03:00
parent 699df8d5fa
commit b9cc7ffd85
6 changed files with 166 additions and 7 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ Execution reaches `_start`, stack unwinding and exception handling tables are bu
Audio output and video output initialization pass
Reaches the `main` function with render loop (deep call chain, ~64KB stack frame function) - init and shader loading from disk fully complete.
Now: `AGC driver: unsupported PM4 opcode 159 at DWORD 2`.
Now: `ShaderRecompiler::Recompile not implemented`.
## Disclaimer
@@ -9,12 +9,21 @@
namespace AgcDriver::Pm4 {
struct IndexedDraw {
std::uint64_t indexAddress;
std::uint32_t indexCount;
std::uint32_t indexSize;
std::uint32_t instanceCount;
std::uint32_t flags;
};
std::string Name(std::uint32_t header);
std::string_view UnsupportedReason(std::uint32_t header);
void Validate(std::span<const std::uint32_t> packet, std::uint32_t queue);
void Execute(std::span<const std::uint32_t> packet, QueueState& queue);
bool AccessesMemory(std::uint32_t header);
std::array<std::uint32_t, 5> ResolveDispatch(std::span<const std::uint32_t> packet, const QueueState& queue);
IndexedDraw ResolveDraw(std::span<const std::uint32_t> packet, const QueueState& queue);
}
@@ -318,6 +318,59 @@ private:
device->Dispatch(compiled, packet[1], packet[2], packet[3]);
}
void draw(QueueState& queue, std::span<const std::uint32_t> packet, const Submission& submission) {
const auto indexed = Pm4::ResolveDraw(packet, queue);
struct Program {
ShaderRecompiler::ShaderBinary binary;
std::uint32_t userDataBase;
std::vector<std::uint32_t> userData;
std::array<ShaderRecompiler::MemoryRegion, 2> memory;
};
const auto programAddress = [&](std::uint32_t base) {
return (static_cast<std::uint64_t>(readRegister(queue.shader, base)) << 8u) | (static_cast<std::uint64_t>(readRegister(queue.shader, base + 1) & 0xffu) << 40u);
};
const auto prepare = [&](std::uint64_t address, std::uint8_t type, ShaderRecompiler::ShaderStage stage, std::uint32_t rsrc2, std::uint32_t userDataBase) {
auto it = submission.shaders.upper_bound(address);
require(it != submission.shaders.begin(), "graphics program does not belong to a registered shader");
--it;
const auto& snapshot = *it->second;
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");
const auto resources = readRegister(queue.shader, rsrc2);
const auto userCount = ((resources >> 1u) & 0x1fu) | (((resources >> 27u) & 1u) << 5u);
const auto codeOffset = static_cast<std::size_t>((address - snapshot.codeAddress) / sizeof(std::uint32_t));
Program result{
{stage, address, std::span(snapshot.code).subspan(codeOffset), snapshot.headerAddress, snapshot.header},
userDataBase,
{},
{{{snapshot.codeAddress, std::as_bytes(std::span(snapshot.code))}, {snapshot.headerAddress, snapshot.header}}}
};
for (std::uint32_t i = 0; i < userCount; ++i) result.userData.push_back(readRegister(queue.shader, userDataBase + i));
return result;
};
const auto vertex = prepare(programAddress(0x0c8), 2, ShaderRecompiler::ShaderStage::Vertex, 0x08b, 0x08c);
const auto pixelAddress = programAddress(0x008);
std::optional<Program> pixel;
if (pixelAddress != 0) pixel = prepare(pixelAddress, 1, ShaderRecompiler::ShaderStage::Fragment, 0x00b, 0x00c);
const auto shaderRegisters = registerValues(queue.shader);
const auto contextRegisters = registerValues(queue.context);
const auto userConfigRegisters = registerValues(queue.userConfig);
std::lock_guard gpuLock(gpuMutex);
if (device == nullptr) device = std::make_shared<VulkanDevice>();
const auto compile = [&](const Program& program) {
const ShaderRecompiler::RecompileRequest request{
program.binary,
{64, program.userDataBase, program.userData, shaderRegisters, contextRegisters, userConfigRegisters, program.memory},
device->Target(),
{0, 0, 0, 128}
};
return ShaderRecompiler::Recompile(request);
};
if (pixel) static_cast<void>(compile(*pixel));
static_cast<void>(compile(vertex));
throw std::runtime_error("AGC driver: graphics pipeline and guest render-target materialization are not implemented for indexed draw at " + std::to_string(indexed.indexAddress));
}
void execute(const Submission& submission) {
if (submission.suspend) {
std::lock_guard gpuLock(gpuMutex);
@@ -348,6 +401,8 @@ private:
} else if (opcode == 0x16) {
const auto direct = Pm4::ResolveDispatch(packet, queue);
dispatch(queue, direct, submission);
} else if (opcode == 0x35) {
draw(queue, packet, submission);
} else if (opcode != 0x42) {
Pm4::Execute(packet, queue);
}
@@ -98,10 +98,10 @@ std::string_view UnsupportedReason(std::uint32_t header) {
}
switch (opcode) {
case 0x11: case 0x12: case 0x13: case 0x15: case 0x16: case 0x26:
case 0x2a: case 0x2f: case 0x37: case 0x40: case 0x42: case 0x50:
case 0x2a: case 0x2f: case 0x35: case 0x37: case 0x40: case 0x42: case 0x50:
case 0x63: case 0x64: case 0x69: case 0x76: case 0x79: case 0x7a:
case 0x81: case 0x83: case 0x9f: return {};
case 0x24: case 0x25: case 0x27: case 0x2c: case 0x2d: case 0x35: case 0x38: case 0x3a: case 0x8d:
case 0x24: case 0x25: case 0x27: case 0x2c: case 0x2d: case 0x38: case 0x3a: case 0x8d:
return "graphics draw, shader stages and guest render-target materialization are not implemented";
case 0x20: return "GPU query predication is not implemented";
case 0x22: return "conditional command execution and conditional flip reservation are not implemented";
@@ -163,6 +163,12 @@ void Validate(std::span<const std::uint32_t> packet, std::uint32_t queue) {
case 0x13: case 0x2f: graphics(); size(2); break;
case 0x26: graphics(); size(3); break;
case 0x2a: graphics(); size(2); require(packet[1] <= 3, "unsupported index-type modifiers"); break;
case 0x35:
graphics();
size(5);
require(packet[3] <= packet[1], "index count exceeds maximum index size");
require((packet[4] & ~0x20u) == 0, "unsupported indexed draw flags");
break;
case 0x15: size(5); require((packet[4] & ~0x8000u) == 0x41u, "dispatch modifiers are not implemented"); break;
case 0x16:
require(packet.size() == 3 || packet.size() == 4, "invalid indirect dispatch size");
@@ -221,7 +227,7 @@ void Validate(std::span<const std::uint32_t> packet, std::uint32_t queue) {
bool AccessesMemory(std::uint32_t header) {
switch ((header >> 8u) & 0xffu) {
case 0x16: case 0x37: case 0x40: case 0x50: case 0x63: case 0x64: case 0x83: case 0x9f: return true;
case 0x16: case 0x35: case 0x37: case 0x40: case 0x50: case 0x63: case 0x64: case 0x83: case 0x9f: return true;
default: return false;
}
}
@@ -239,6 +245,21 @@ std::array<std::uint32_t, 5> ResolveDispatch(std::span<const std::uint32_t> pack
return result;
}
IndexedDraw ResolveDraw(std::span<const std::uint32_t> packet, const QueueState& queue) {
Validate(packet, 0);
require(((packet[0] >> 8u) & 0xffu) == 0x35, "expected DRAW_INDEX_OFFSET_2 packet");
require(queue.indexType <= 2, "unsupported index type");
const std::uint32_t indexSize = queue.indexType == 0 ? 2 : queue.indexType == 1 ? 4 : 1;
require(queue.indexBase != 0 && queue.indexBase % indexSize == 0, "null or misaligned index base");
const auto offset = static_cast<std::uint64_t>(packet[2]) * indexSize;
require(offset <= std::numeric_limits<std::uint64_t>::max() - queue.indexBase, "index address overflow");
const auto address = queue.indexBase + offset;
const auto bytes = static_cast<std::uint64_t>(packet[3]) * indexSize;
require(bytes <= std::numeric_limits<std::size_t>::max(), "index range size overflow");
GuestMemory::CheckRange(reinterpret_cast<const void*>(address), static_cast<std::size_t>(bytes), indexSize);
return {address, packet[3], indexSize, queue.instanceCount, packet[4]};
}
void Execute(std::span<const std::uint32_t> packet, QueueState& queue) {
const auto opcode = (packet[0] >> 8u) & 0xffu;
switch (opcode) {
@@ -4,6 +4,7 @@
#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp"
#include "prx/libc/include/Shutdown.hpp"
#include <cstdio>
#include <limits>
#include <set>
#include <stdexcept>
#include <vector>
@@ -126,6 +127,36 @@ void testContextAndBases() {
check(state.shader.empty() && state.context.empty() && state.dispatchIndirectBase == 0 && state.indexBase == 0 && !state.savedContext, "dispatch reset retained state");
}
void testIndexedDraw() {
AgcDriver::QueueState state;
alignas(4) std::array<std::uint32_t, 8> indices{};
state.indexBase = reinterpret_cast<std::uintptr_t>(indices.data());
state.instanceCount = 3;
const auto packet = makePacket(0x35, {4, 2, 4, 0x20});
for (std::uint32_t type = 0; type < 3; ++type) {
state.indexType = type;
const auto draw = AgcDriver::Pm4::ResolveDraw(packet, state);
const auto size = type == 0 ? 2u : type == 1 ? 4u : 1u;
check(draw.indexAddress == state.indexBase + 2 * size && draw.indexSize == size && draw.indexCount == 4 && draw.instanceCount == 3 && draw.flags == 0x20, "indexed draw state mismatch");
}
expectFailure([&] { AgcDriver::Pm4::Validate(packet, 0x20); }, "compute queue");
expectFailure([] { AgcDriver::Pm4::Validate(makePacket(0x35, {3, 0, 4, 0}), 0); }, "maximum index size");
expectFailure([] { AgcDriver::Pm4::Validate(makePacket(0x35, {4, 0, 4, 1}), 0); }, "draw flags");
expectFailure([] { AgcDriver::Pm4::Validate(makePacket(0x35, {4, 0, 4}), 0); }, "packet size");
state.indexType = 3;
expectFailure([&] { AgcDriver::Pm4::ResolveDraw(packet, state); }, "index type");
state.indexType = 1;
state.indexBase += 1;
expectFailure([&] { AgcDriver::Pm4::ResolveDraw(packet, state); }, "misaligned index base");
state.indexBase = std::numeric_limits<std::uint64_t>::max() - 3;
expectFailure([&] { AgcDriver::Pm4::ResolveDraw(packet, state); }, "index address overflow");
state.indexType = 2;
state.indexBase = std::numeric_limits<std::uint64_t>::max() - 4;
expectFailure([&] { AgcDriver::Pm4::ResolveDraw(packet, state); }, "address range overflow");
state.indexBase = 0x1000;
expectFailure([&] { AgcDriver::Pm4::ResolveDraw(packet, state); }, "guest");
}
void testMemory() {
AgcDriver::QueueState state;
std::array<std::uint32_t, 4> data{0, 0, 0, 0};
@@ -211,6 +242,7 @@ int main(int argc, char** argv) {
testCatalog();
testRegisters();
testContextAndBases();
testIndexedDraw();
testMemory();
testCopies();
testDriverSubmission();
@@ -1,10 +1,12 @@
#include "prx/libSceAgcDriver/Execution/include/Driver.hpp"
#include "prx/libc/include/Shutdown.hpp"
#include "prx/libSceAgcDriver/Submit/include/Acb.hpp"
#include "prx/libSceAgcDriver/Submit/include/Dcb.hpp"
#include <array>
#include <cstdio>
#include <stdexcept>
#include <string>
#include <vector>
int main(int argc, char** argv) {
try {
@@ -17,13 +19,45 @@ int main(int argc, char** argv) {
shader.code = code.data();
AgcDriverRegisterShader_nid_postfix(&shader);
const auto address = reinterpret_cast<std::uintptr_t>(code.data());
std::array<std::uint32_t, 12> commands{
std::vector<std::uint32_t> commands{
0xc0027600, 0x20c, static_cast<std::uint32_t>(address >> 8u), static_cast<std::uint32_t>(address >> 40u),
0xc0017600, 0x213, 0,
0xc0031500, 1, 1, 1, 0x8041
};
Packet packet{commands.data(), static_cast<std::uint32_t>(commands.size()), 0, {}};
const std::array<std::uint32_t, 3> arguments{1, 1, 1};
alignas(256) const std::array<std::uint32_t, 65> vertexCode{0xbf810000};
Shader vertexShader = shader;
const std::array<std::uint16_t, 4> indices{0, 1, 2, 0};
std::uint32_t destination = 0;
const bool graphics = argc == 2 && (std::string(argv[1]) == "draw" || std::string(argv[1]) == "vertex");
if (graphics) {
shader.type = 1;
AgcDriverRegisterShader_nid_postfix(&shader);
vertexShader.type = 2;
vertexShader.shader_size = sizeof(vertexCode);
vertexShader.code = vertexCode.data();
AgcDriverRegisterShader_nid_postfix(&vertexShader);
const auto vertexAddress = reinterpret_cast<std::uintptr_t>(vertexCode.data()) + 256;
const auto indexAddress = reinterpret_cast<std::uintptr_t>(indices.data());
const auto pixelAddress = std::string(argv[1]) == "vertex" ? 0 : address;
commands = {
0xc0027600, 0x0c8, static_cast<std::uint32_t>(vertexAddress >> 8u), static_cast<std::uint32_t>(vertexAddress >> 40u),
0xc0017600, 0x08b, 0,
0xc0027600, 0x008, static_cast<std::uint32_t>(pixelAddress >> 8u), static_cast<std::uint32_t>(pixelAddress >> 40u),
0xc0017600, 0x00b, 0x08000002,
0xc0217600, 0x00c
};
for (std::uint32_t i = 0; i < 33; ++i) commands.push_back(100 + i);
const auto destinationAddress = reinterpret_cast<std::uintptr_t>(&destination);
const std::array<std::uint32_t, 13> draw{
0xc0012600, static_cast<std::uint32_t>(indexAddress), static_cast<std::uint32_t>(indexAddress >> 32u),
0xc0033500, 3, 1, 3, 0x20,
0xc0033700, 0x100, static_cast<std::uint32_t>(destinationAddress), static_cast<std::uint32_t>(destinationAddress >> 32u), 7
};
commands.insert(commands.end(), draw.begin(), draw.end());
packet = Packet{commands.data(), static_cast<std::uint32_t>(commands.size()), 0, {}};
}
if (argc == 2 && std::string(argv[1]) == "indirect") {
const auto argumentAddress = reinterpret_cast<std::uintptr_t>(arguments.data());
commands[7] = 0xc0021600;
@@ -32,14 +66,22 @@ int main(int argc, char** argv) {
commands[10] = 0x8041;
packet.dw_num = 11;
}
sceAgcDriverSubmitAcb(0x20, &packet);
if (graphics) sceAgcDriverSubmitDcb(&packet);
else sceAgcDriverSubmitAcb(0x20, &packet);
try {
AgcDriverWaitIdle_nid_postfix();
} catch (const std::runtime_error& error) {
if (std::string(error.what()) != "ShaderRecompiler::Recompile not implemented") {
throw;
}
std::puts("Vulkan device initialized; real recompiler exception propagated from compute dispatch");
if (destination != 0) throw std::runtime_error("failed draw executed a later memory write");
std::puts("Vulkan device initialized; real recompiler exception propagated");
try {
AgcDriverWaitIdle_nid_postfix();
throw std::runtime_error("idle lost recompiler failure");
} catch (const std::runtime_error& idle) {
if (std::string(idle.what()) != "ShaderRecompiler::Recompile not implemented") throw;
}
try {
LibcRunShutdown_nid_postfix();
throw std::runtime_error("shutdown lost recompiler failure");