Merge pull request #776 from adriadam10/feat/agc-gs-oversubscription

feat(agc): implement sceAgcGetGsOversubscription
This commit is contained in:
Dean
2026-10-06 12:47:24 +13:00
committed by GitHub
6 changed files with 281 additions and 5 deletions
+17
View File
@@ -45,6 +45,7 @@ add_library(libSceAgc SHARED EXCLUDE_FROM_ALL
${CMAKE_CURRENT_SOURCE_DIR}/Patch/src/Branch.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Predication/src/Predication.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Shader/src/CreateShader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Shader/src/GsOversubscription.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Shader/src/InterpolantMapping.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Shader/src/LinkShaders.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Shader/src/PrimState.cpp
@@ -201,6 +202,22 @@ if(BUILD_TESTING)
endif()
endif()
add_test_executable(agc_gs_oversubscription_tests EXCLUDE_FROM_ALL
tests/GsOversubscription.cpp
Shader/src/GsOversubscription.cpp
)
target_include_directories(agc_gs_oversubscription_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(agc_gs_oversubscription_tests PRIVATE libc)
configure_windows_unwind(agc_gs_oversubscription_tests)
if(BUILD_TESTING)
set_target_properties(agc_gs_oversubscription_tests PROPERTIES EXCLUDE_FROM_ALL FALSE)
add_test(NAME agc_gs_oversubscription_tests COMMAND agc_gs_oversubscription_tests)
if(WIN32)
set_tests_properties(agc_gs_oversubscription_tests PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
endif()
endif()
add_test_executable(agc_dma_data_tests EXCLUDE_FROM_ALL
tests/DmaData.cpp
Command/src/Packet.cpp
@@ -20,6 +20,14 @@ constexpr std::uint32_t VGT_PRIMITIVE_TYPE = 0x242u;
constexpr std::uint32_t GE_CNTL = 0x25Bu;
constexpr std::uint32_t GE_USER_VGPR_EN = 0x262u;
constexpr std::uint32_t GE_PC_ALLOC = 0x260u;
constexpr std::uint32_t SPI_SHADER_PGM_RSRC4_GS = 0x081u;
constexpr std::uint32_t SPI_VS_OUT_CONFIG = 0x1B1u;
constexpr std::uint32_t GE_MAX_OUTPUT_PER_SUBGROUP = 0x1FFu;
constexpr std::uint32_t PA_CL_VS_OUT_CNTL = 0x207u;
constexpr std::uint32_t VGT_GS_ONCHIP_CNTL = 0x291u;
constexpr std::uint32_t GE_NGG_SUBGRP_CNTL = 0x2D3u;
constexpr std::uint32_t SHADER_FILE_HEADER_MAGIC = 0x34333231u;
constexpr std::uint32_t SHADER_VERSION = 0x00000018u;
@@ -0,0 +1,111 @@
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <string>
#include <prx/libc/include/General.hpp>
#include "SceShaders.hpp"
#include "prx/libSceAgc/Shader/include/ShaderConstants.hpp"
namespace {
constexpr std::uint32_t GE_PC_ALLOC_FULL = 0x7FFu;
constexpr std::uint32_t LATE_ALLOC_GS_FULL = 0x007F0000u;
constexpr std::uint32_t VGT_SHADER_STAGES_GS_W32_BIT = 0x00400000u;
struct GsOccupancyLimits {
std::uint32_t vertex;
std::uint32_t exportCount;
};
std::uint32_t findContextRegister(const Shader* gs, std::uint32_t offset) {
if (gs->cx_registers != nullptr) {
for (std::uint32_t i = 0; i < gs->num_cx_registers; ++i) {
if (gs->cx_registers[i].offset == offset) {
return gs->cx_registers[i].value;
}
}
}
throw std::runtime_error("sceAgcGetGsOversubscription: gs has no context register " + std::to_string(offset));
}
GsOccupancyLimits getGsOccupancyLimits(const Shader* gs, std::uint32_t vertexCapacity, std::uint32_t exportCapacity) {
const std::uint32_t onchip = findContextRegister(gs, ShaderRegs::VGT_GS_ONCHIP_CNTL);
const std::uint32_t subgroup = findContextRegister(gs, ShaderRegs::GE_NGG_SUBGRP_CNTL);
const std::uint32_t vsOut = findContextRegister(gs, ShaderRegs::SPI_VS_OUT_CONFIG);
const std::uint32_t clOut = findContextRegister(gs, ShaderRegs::PA_CL_VS_OUT_CNTL);
const std::uint32_t maxOutput = findContextRegister(gs, ShaderRegs::GE_MAX_OUTPUT_PER_SUBGROUP);
const std::uint32_t outputWords = ((maxOutput & 0x3FFu) + 31u) >> 5u;
if (outputWords == 0) {
throw std::runtime_error("sceAgcGetGsOversubscription: GE_MAX_OUTPUT_PER_SUBGROUP is zero");
}
const std::uint32_t subgroupWork = ((((onchip >> 11u) & 0x7FFu) * (subgroup & 0x1FFu)) + 31u) >> 5u;
std::uint32_t waves = std::max(subgroupWork, outputWords);
if ((gs->specials->vgt_shader_stages_en.value & VGT_SHADER_STAGES_GS_W32_BIT) == 0) {
waves >>= 1u;
}
waves = std::max(waves, 1u);
const std::uint32_t exports = 1u + ((clOut >> 21u) & 1u) + ((clOut >> 22u) & 1u) + ((clOut >> 23u) & 1u);
const std::uint32_t exportLimit = (exportCapacity / exports) * 4u;
const std::uint32_t vertexLimit = (vsOut & 0x80u) != 0 ? 2048u : vertexCapacity / (((vsOut >> 2u) & 0xFu) + 1u);
return {waves * (vertexLimit / outputWords), waves * (exportLimit / outputWords)};
}
}
extern "C" {
int APS5_VABI sceAgcGetGsOversubscription(ShaderRegister* regs, const Shader* gs, std::uint32_t budget, float factor) {
if (regs == nullptr) {
throw std::runtime_error(std::string(__func__) + ": regs is null");
}
ShaderRegister pcAlloc{ShaderRegs::GE_PC_ALLOC, 0u};
ShaderRegister rsrc4{ShaderRegs::SPI_SHADER_PGM_RSRC4_GS, 0u};
if (budget == std::numeric_limits<std::uint32_t>::max()) {
pcAlloc.value = GE_PC_ALLOC_FULL;
rsrc4.value = LATE_ALLOC_GS_FULL;
} else if (budget != 0) {
if (gs == nullptr || gs->specials == nullptr) {
throw std::runtime_error(std::string(__func__) + ": gs or its special registers are null");
}
const GsOccupancyLimits base = getGsOccupancyLimits(gs, 1024u, 128u);
const GsOccupancyLimits expanded = getGsOccupancyLimits(gs, 2048u, 382u);
const std::uint32_t baseMin = std::min(base.vertex, base.exportCount);
const std::uint32_t limitShift = (gs->specials->vgt_shader_stages_en.value & VGT_SHADER_STAGES_GS_W32_BIT) != 0 ? 5u : 6u;
const std::uint32_t expandedLimit = std::min({expanded.vertex, expanded.exportCount, budget >> limitShift, 1024u});
const std::uint32_t headroom = expandedLimit > baseMin ? expandedLimit - baseMin : 0u;
const float scaled = std::fma(factor, static_cast<float>(headroom), static_cast<float>(baseMin));
if (!(scaled >= 0.0f && scaled < 4294967296.0f)) {
throw std::runtime_error(std::string(__func__) + ": factor gives an unrepresentable target");
}
const auto target = static_cast<std::uint32_t>(scaled);
if (target > baseMin) {
if (target < base.exportCount) {
const std::uint32_t range = std::max(expanded.vertex - base.vertex, 1u);
std::uint32_t value = static_cast<std::uint32_t>(std::min<std::uint64_t>((static_cast<std::uint64_t>(target - base.vertex) << 10u) / range, 1024u));
value = std::max(value, 1u);
pcAlloc.value = ((value << 1u) - 1u) & GE_PC_ALLOC_FULL;
rsrc4.value = LATE_ALLOC_GS_FULL;
} else {
const std::uint32_t range = std::max(expanded.exportCount - base.exportCount, 1u);
const auto value = static_cast<std::uint32_t>(std::min<std::uint64_t>((static_cast<std::uint64_t>(target - base.exportCount) * 127u) / range, 127u));
pcAlloc.value = GE_PC_ALLOC_FULL;
rsrc4.value = value << 16u;
}
}
}
regs[0] = pcAlloc;
regs[1] = rsrc4;
return 0;
}
}
@@ -60,11 +60,6 @@ int APS5_VABI sceAgcGetDefaultCxStateFlat() {
return 0;
}
int APS5_VABI sceAgcGetGsOversubscription() {
NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceAgcGetSemaphoreLabel() {
NotImplemented_nid_no_patch(__func__);
return 0;
@@ -0,0 +1,144 @@
#include "SceShaders.hpp"
#include "prx/libc/include/Shutdown.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libSceAgc/Shader/include/ShaderConstants.hpp"
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <limits>
#include <stdexcept>
extern "C" int APS5_VABI sceAgcGetGsOversubscription(ShaderRegister* regs, const Shader* gs, std::uint32_t budget, float factor);
namespace {
using Registers = std::array<ShaderRegister, 2>;
struct GsSetup {
std::array<ShaderRegister, 5> cx;
ShaderSpecialRegs specials;
Shader shader;
};
void check(bool condition, const char* message) {
if (!condition) throw std::runtime_error(message);
}
template <typename TAction>
void expectFailure(TAction action) {
try {
action();
} catch (const std::exception& error) {
check(error.what()[0] != '\0', "empty exception message");
return;
}
throw std::runtime_error("expected an exception");
}
Registers filled() {
Registers regs{};
std::memset(regs.data(), 0xcc, sizeof(regs));
return regs;
}
void makeGs(GsSetup& setup, std::uint32_t onchip, std::uint32_t subgroup, std::uint32_t vsOut, std::uint32_t clOut, std::uint32_t maxOutput, bool wave32) {
setup.cx = {{
{ShaderRegs::VGT_GS_ONCHIP_CNTL, onchip},
{ShaderRegs::GE_NGG_SUBGRP_CNTL, subgroup},
{ShaderRegs::SPI_VS_OUT_CONFIG, vsOut},
{ShaderRegs::PA_CL_VS_OUT_CNTL, clOut},
{ShaderRegs::GE_MAX_OUTPUT_PER_SUBGROUP, maxOutput},
}};
setup.specials = {};
setup.specials.vgt_shader_stages_en = {ShaderRegs::VGT_SHADER_STAGES_EN, wave32 ? 0x00400000u : 0u};
setup.shader = {};
setup.shader.cx_registers = setup.cx.data();
setup.shader.num_cx_registers = static_cast<std::uint8_t>(setup.cx.size());
setup.shader.specials = &setup.specials;
}
void expectRegs(const Shader* gs, std::uint32_t budget, float factor, std::uint32_t pcAlloc, std::uint32_t rsrc4, const char* message) {
auto regs = filled();
check(sceAgcGetGsOversubscription(regs.data(), gs, budget, factor) == 0, message);
check(regs[0].offset == ShaderRegs::GE_PC_ALLOC && regs[1].offset == ShaderRegs::SPI_SHADER_PGM_RSRC4_GS, message);
check(regs[0].value == pcAlloc && regs[1].value == rsrc4, message);
}
void testBudgetLimits() {
expectRegs(nullptr, 0, 0.5f, 0, 0, "zero budget does not disable oversubscription");
expectRegs(nullptr, std::numeric_limits<std::uint32_t>::max(), 0.5f, 0x7ffu, 0x7f0000u, "unlimited budget does not allow full oversubscription");
}
void testVertexBound() {
GsSetup setup;
makeGs(setup, 2u << 11u, 4, 3u << 2u, 0, 64, false);
expectRegs(&setup.shader, 1u << 20u, 0.5f, 0x3ffu, 0x7f0000u, "vertex-bound oversubscription changed");
expectRegs(&setup.shader, 64, 0.5f, 0, 0, "budget below the base occupancy changed the registers");
expectRegs(&setup.shader, 1u << 20u, 0.0f, 0, 0, "zero factor changed the registers");
expectRegs(&setup.shader, 1u << 20u, -1.0f, 0, 0, "factor reaching a zero target changed the registers");
}
void testExportBound() {
GsSetup setup;
makeGs(setup, 8u << 11u, 8, 0, 7u << 21u, 64, true);
expectRegs(&setup.shader, 1u << 20u, 0.75f, 0x7ffu, 0x5f0000u, "export-bound oversubscription changed");
expectRegs(&setup.shader, 1u << 20u, 4.0f, 0x7ffu, 0x7f0000u, "factor above one is not clamped");
makeGs(setup, 1u << 11u, 1, 0x80u, 1u << 21u, 32, false);
expectRegs(&setup.shader, 1u << 20u, 0.25f, 0x7ffu, 0x1f0000u, "oversubscription without parameter cache exports changed");
}
void testRegisterFields() {
GsSetup setup;
makeGs(setup, 5u << 11u, 2, 2u << 2u, 0, 128, false);
expectRegs(&setup.shader, 16384, 0.75f, 0x301u, 0x7f0000u, "wave64 subgroup waves are not halved");
makeGs(setup, 13u << 11u, 4, 0, 3u << 22u, 32, false);
expectRegs(&setup.shader, 16384, 0.5f, 0x7ffu, 0x100000u, "position export count changed");
makeGs(setup, 10u << 11u, 1, 3u << 2u, 7u << 21u, 32, true);
expectRegs(&setup.shader, 8192, 0.25f, 0x7ffu, 0x100000u, "wave32 budget shift changed");
makeGs(setup, 6u << 11u, 8, 0x80u, 0, 96, false);
expectRegs(&setup.shader, 32768, 0.5f, 0x7ffu, 0x3f0000u, "vertex limit without parameter cache exports changed");
makeGs(setup, 12u << 11u, 7, 0, 1u << 22u, 32, true);
expectRegs(&setup.shader, 32768, 0.25f, 0x7ffu, 0x50000u, "primitive amplification factor is ignored");
}
void testRejections() {
GsSetup setup;
makeGs(setup, 2u << 11u, 4, 3u << 2u, 0, 64, false);
auto regs = filled();
const auto saved = regs;
expectFailure([&] { sceAgcGetGsOversubscription(nullptr, &setup.shader, 1u << 20u, 0.5f); });
expectFailure([&] { sceAgcGetGsOversubscription(regs.data(), nullptr, 1u << 20u, 0.5f); });
expectFailure([&] { sceAgcGetGsOversubscription(regs.data(), &setup.shader, 1u << 20u, -2.0f); });
expectFailure([&] { sceAgcGetGsOversubscription(regs.data(), &setup.shader, 1u << 20u, std::nanf("")); });
setup.shader.specials = nullptr;
expectFailure([&] { sceAgcGetGsOversubscription(regs.data(), &setup.shader, 1u << 20u, 0.5f); });
setup.shader.specials = &setup.specials;
setup.cx[4].value = 0;
expectFailure([&] { sceAgcGetGsOversubscription(regs.data(), &setup.shader, 1u << 20u, 0.5f); });
setup.shader.num_cx_registers = 4;
expectFailure([&] { sceAgcGetGsOversubscription(regs.data(), &setup.shader, 1u << 20u, 0.5f); });
check(std::memcmp(regs.data(), saved.data(), sizeof(regs)) == 0, "rejected call wrote registers");
}
}
int main() {
try {
testBudgetLimits();
testVertexBound();
testExportBound();
testRegisterFields();
testRejections();
LibcRunShutdown_nid_postfix();
std::puts("AGC GS oversubscription tests passed");
return 0;
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
try { LibcRunShutdown_nid_postfix(); }
catch (const std::exception& shutdown) { std::fprintf(stderr, "shutdown: %s\n", shutdown.what()); }
return 1;
}
}
+1
View File
@@ -45,6 +45,7 @@ Throughout the project, every function at every stage either **does exactly what
- [sceSaveDataTransferringMountPs4, sceSaveDataDirNameSearchPs4](../../core/libs/prx/libSceSaveData.native/Export.cpp) (libSceSaveData.native) - arguments assumed to be those of `sceSaveDataTransferringMount` and `sceSaveDataDirNameSearch`: KytyPS5 binds both NIDs to the PS5 functions, and [prosper](https://github.com/mattias800/prosper) captured the same result layout on a title for the search. There is no PS4 save area, so the mount returns `NOT_FOUND` and the search reports zero hits
- [sceAgcSetSubmitMode](../../core/libs/prx/libSceAgc/Misc/src/Suspend.cpp) (libSceAgc) - mode values unknown; only 0 is accepted
- [SET_PREDICATION](../../core/libs/prx/libSceAgcDriver/Execution/src/Pm4.cpp) (AGC driver) - boolean predication follows the AMD PM4 encoding [Mesa](https://gitlab.freedesktop.org/mesa/mesa/-/blob/main/src/amd/common/sid.h) uses: operation 3 reads 64 bits and 4 reads 32 bits, and bit 8 runs predicated packets when the value is non-zero (clear: when it is zero); the bit 12 hint is ignored. The address must have its low 4 bits clear, as `sceAgcDcbSetPredication` writes it. Query predication (Z-pass, primitive count), a predicated COND_EXEC, predicated command buffer chains and flips in predicated command buffers throw
- [sceAgcGetGsOversubscription](../../core/libs/prx/libSceAgc/Shader/src/GsOversubscription.cpp) (libSceAgc) - signature and occupancy formula (base and expanded vertex/export capacities, budget shift, `GE_PC_ALLOC` / `SPI_SHADER_PGM_RSRC4_GS` encoding) follow KytyPS5 only, not confirmed on hardware or a title; a negative or NaN factor that gives a negative target, a missing GS context register or a zero `GE_MAX_OUTPUT_PER_SUBGROUP` throws
- [sceAgcGetIsTrinityMode](../../core/libs/prx/libSceAgc/Misc/src/Platform.cpp) (libSceAgc) - signature from the only PPSA26344 call (a pointer to a one-byte flag, result ignored); a null pointer throws because its error code is unknown
- [zARR5aCmkoY](../../core/libs/prx/libSceAgc/DcbFlow/src/Control.cpp) (libSceAgc) - unknown name, signature
- [qj7QZpgr9Uw](../../core/libs/prx/libSceAgc/DcbState/src/ContextState.cpp) (libSceAgc) - unknown name