feat(shader): decode embedded vertex fetch metadata for vertex-family stages

This commit is contained in:
boykopovar
2026-09-17 12:00:14 +03:00
parent 50226e4260
commit f7b5df1711
7 changed files with 230 additions and 4 deletions
@@ -72,6 +72,23 @@ enum class PrimitiveType : std::uint32_t {
constexpr std::uint32_t VGT_SHADER_STAGES_GS_BIT = 0x20u;
constexpr std::uint32_t VGT_SHADER_STAGES_NGG_BIT = 0x04u;
enum class AgcDirectResourceType : std::uint32_t {
GdsCounterRange = 0,
ShaderResourceTable = 1,
SubPtrFetchShader = 2,
PtrSoBufferTable = 3,
PtrInternalGlobalTable = 4,
PtrExtendedUserData = 5,
GsFlags = 6,
GdsMemoryRange = 7,
PtrVertexBufferTable = 8,
StereoXOffset = 9,
PtrVertexAttribDescTable = 10,
};
constexpr std::uint32_t AGC_DIRECT_RESOURCE_TYPE_COUNT = 11u;
constexpr std::uint16_t AGC_ILLEGAL_DIRECT_OFFSET = 0xffffu;
}
#endif
@@ -309,7 +309,7 @@ private:
const auto codeOffset = static_cast<std::size_t>((address - snapshot.codeAddress) / sizeof(std::uint32_t));
const ShaderRecompiler::RecompileRequest request{
{ShaderRecompiler::ShaderStage::Compute, address, std::span(snapshot.code).subspan(codeOffset), snapshot.headerAddress, snapshot.header},
{(packet[4] & 0x8000u) != 0 ? 32u : 64u, 0x240, userData, compute, std::nullopt, memory},
{(packet[4] & 0x8000u) != 0 ? 32u : 64u, 0x240, userData, compute, std::nullopt, std::nullopt, memory},
device->Target(),
{0, 0, 0, 128}
};
@@ -419,7 +419,7 @@ private:
const auto waveSize = program.binary.stage == Stage::Fragment ? graphics.stages.fragmentWaveSize : graphics.stages.vertexWaveSize;
const ShaderRecompiler::RecompileRequest request{
program.binary,
{waveSize, program.userDataBase, program.userData, std::nullopt, program.binary.stage == Stage::Fragment ? std::optional(pixel) : std::nullopt, memory},
{waveSize, program.userDataBase, program.userData, std::nullopt, program.binary.stage == Stage::Fragment ? std::optional(pixel) : std::nullopt, program.binary.stage == Stage::Fragment ? std::nullopt : std::optional(Graphics::DecodeVertexStageInfo(program.binary.header, program.binary.headerAddress, program.userData)), memory},
device->Target(),
{descriptorSet, 0, offset, pushStride},
ShaderRecompiler::GraphicsCompileContext{program.firstUserSgpr, linked, graphics.stages.mesh, graphics.stages.tessellation, {indexed.indexAddress, indexed.indexCount, indexed.indexSize, indexed.instanceCount}}
@@ -3,11 +3,14 @@
#include "prx/libSceAgcDriver/Execution/include/QueueState.hpp"
#include "Recompiler.hpp"
#include <cstdint>
#include <span>
namespace AgcDriver::Graphics {
ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context);
ShaderRecompiler::ShaderComputeStageInfo DecodeComputeStageInfo(const Registers& shader);
ShaderRecompiler::ShaderVertexStageInfo DecodeVertexStageInfo(std::span<const std::byte> header, std::uint64_t headerAddress, std::span<const std::uint32_t> userData);
}
@@ -1,4 +1,9 @@
#include "prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp"
#include "SceShaders.hpp"
#include "prx/libSceAgc/Shader/include/ShaderConstants.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include <array>
#include <cstring>
#include <stdexcept>
#include <string>
@@ -25,6 +30,28 @@ std::uint32_t read(const Registers& registers, std::uint32_t offset) {
return 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);
if (address < headerAddress) throw std::runtime_error("AGC graphics: AGC header pointer precedes the shader header");
const auto offset = address - headerAddress;
if (offset + sizeof(T) > header.size()) throw std::runtime_error("AGC graphics: AGC header pointer is outside the registered shader header");
T value;
std::memcpy(&value, header.data() + offset, sizeof(T));
return value;
}
template <typename T> void _readHeaderArray(std::span<const std::byte> header, std::uint64_t headerAddress, const void* pointer, std::uint32_t count, T* destination) {
if (count == 0) return;
if (pointer == nullptr) throw std::runtime_error("AGC graphics: null AGC header array pointer");
const auto address = reinterpret_cast<std::uint64_t>(pointer);
if (address < headerAddress) throw std::runtime_error("AGC graphics: AGC header array pointer precedes the shader header");
const auto offset = address - headerAddress;
const auto bytes = static_cast<std::uint64_t>(count) * sizeof(T);
if (offset + bytes > header.size()) throw std::runtime_error("AGC graphics: AGC header array is outside the registered shader header");
std::memcpy(destination, header.data() + offset, bytes);
}
}
ShaderRecompiler::ShaderComputeStageInfo DecodeComputeStageInfo(const Registers& shader) {
@@ -107,4 +134,81 @@ ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& con
};
}
ShaderRecompiler::ShaderVertexStageInfo DecodeVertexStageInfo(std::span<const std::byte> header, std::uint64_t headerAddress, std::span<const std::uint32_t> userData) {
if (header.size() < sizeof(Shader)) throw std::runtime_error("AGC graphics: shader header is smaller than the fixed AGC header");
Shader shader;
std::memcpy(&shader, header.data(), sizeof(Shader));
ShaderRecompiler::ShaderVertexStageInfo info{};
if (shader.user_data == nullptr) throw std::runtime_error("AGC graphics: missing AGC user-data header");
const auto userDataHeader = _readHeaderPod<ShaderUserData>(header, headerAddress, shader.user_data);
if (userDataHeader.direct_resource_count > ShaderRegs::AGC_DIRECT_RESOURCE_TYPE_COUNT) throw std::runtime_error("AGC graphics: AGC direct-resource count exceeds the known resource domain");
std::array<std::uint16_t, ShaderRegs::AGC_DIRECT_RESOURCE_TYPE_COUNT> directOffsets{};
directOffsets.fill(ShaderRegs::AGC_ILLEGAL_DIRECT_OFFSET);
if (userDataHeader.direct_resource_count != 0) _readHeaderArray(header, headerAddress, userDataHeader.direct_resource_offset, userDataHeader.direct_resource_count, directOffsets.data());
std::int32_t vertexBufferReg = -1;
std::int32_t vertexAttribReg = -1;
for (std::uint32_t type = 0; type < userDataHeader.direct_resource_count; ++type) {
const auto reg = directOffsets[type];
if (reg == ShaderRegs::AGC_ILLEGAL_DIRECT_OFFSET) continue;
if (type == static_cast<std::uint32_t>(ShaderRegs::AgcDirectResourceType::PtrVertexBufferTable)) vertexBufferReg = reg;
if (type == static_cast<std::uint32_t>(ShaderRegs::AgcDirectResourceType::PtrVertexAttribDescTable)) vertexAttribReg = reg;
}
if (vertexAttribReg < 0) return info;
if (vertexBufferReg < 0) throw std::runtime_error("AGC graphics: vertex attribute table requires a vertex buffer table");
if (static_cast<std::uint32_t>(vertexBufferReg) + 1u >= userData.size()) throw std::runtime_error("AGC graphics: vertex buffer table pointer exceeds the user-SGPR domain");
if (static_cast<std::uint32_t>(vertexAttribReg) + 1u >= userData.size()) throw std::runtime_error("AGC graphics: vertex attribute table pointer exceeds the user-SGPR domain");
if (shader.num_input_semantics == 0 || shader.num_input_semantics > ShaderRecompiler::ShaderVertexStageInfo::MaxResources) throw std::runtime_error("AGC graphics: vertex semantic count is outside the supported domain");
if (shader.input_semantics == nullptr) throw std::runtime_error("AGC graphics: missing vertex input semantics");
std::array<ShaderSemantic, ShaderRecompiler::ShaderVertexStageInfo::MaxResources> semantics{};
_readHeaderArray(header, headerAddress, shader.input_semantics, shader.num_input_semantics, semantics.data());
const auto attribTableAddr = static_cast<std::uint64_t>(userData[static_cast<std::uint32_t>(vertexAttribReg)]) | (static_cast<std::uint64_t>(userData[static_cast<std::uint32_t>(vertexAttribReg) + 1u]) << 32u);
const auto bufferTableAddr = static_cast<std::uint64_t>(userData[static_cast<std::uint32_t>(vertexBufferReg)]) | (static_cast<std::uint64_t>(userData[static_cast<std::uint32_t>(vertexBufferReg) + 1u]) << 32u);
if (attribTableAddr == 0) throw std::runtime_error("AGC graphics: null vertex attribute table address");
if (bufferTableAddr == 0) throw std::runtime_error("AGC graphics: null vertex buffer table address");
info.fetchEmbedded = true;
info.fetchAttribReg = static_cast<std::uint32_t>(vertexAttribReg);
info.fetchBufferReg = static_cast<std::uint32_t>(vertexBufferReg);
for (std::uint32_t i = 0; i < shader.num_input_semantics; ++i) {
const auto& semantic = semantics[i];
if (semantic.static_vb_index == 1 || semantic.static_attribute == 1) throw std::runtime_error("AGC graphics: statically bound vertex attributes are not implemented");
std::array<std::byte, 4> attribWordBytes{};
AgcDriver::GuestMemory::Read(attribTableAddr + static_cast<std::uint64_t>(semantic.semantic) * 4u, attribWordBytes, 4);
std::uint32_t attribWord;
std::memcpy(&attribWord, attribWordBytes.data(), 4);
const auto index = attribWord & 0x1fu;
const auto format = (attribWord >> 5u) & 0x1ffu;
const auto offset = (attribWord >> 14u) & 0xfffu;
const auto fetchIndex = (attribWord >> 26u) & 0x1u;
if (index >= ShaderRecompiler::ShaderVertexStageInfo::MaxResources) throw std::runtime_error("AGC graphics: vertex buffer index exceeds the supported domain");
std::array<std::byte, 16> sharpBytes{};
AgcDriver::GuestMemory::Read(bufferTableAddr + static_cast<std::uint64_t>(index) * 16u, sharpBytes, 4);
std::array<std::uint32_t, 4> sharp{};
std::memcpy(sharp.data(), sharpBytes.data(), 16);
if (info.resourcesNum >= ShaderRecompiler::ShaderVertexStageInfo::MaxResources) throw std::runtime_error("AGC graphics: vertex resource count exceeds the supported domain");
auto& resource = info.resources[info.resourcesNum];
auto& destination = info.resourcesDst[info.resourcesNum];
resource.fields = sharp;
destination.registerStart = static_cast<std::int32_t>(semantic.hardware_mapping);
destination.registersNum = static_cast<std::int32_t>(semantic.size_in_elements);
destination.attrId = static_cast<std::int32_t>(semantic.semantic);
destination.fetchIndex = fetchIndex;
if (format != 0u) {
const auto bufferFormat = format >> 2u;
const auto channels = (format & 0x3u) + 1u;
const auto dstSelY = channels > 1u ? 5u : 0u;
const auto dstSelZ = channels > 2u ? 6u : 0u;
const auto dstSelW = channels > 3u ? 7u : 1u;
const auto dstSel = 4u | (dstSelY << 3u) | (dstSelZ << 6u) | (dstSelW << 9u);
resource.fields[3] = (resource.fields[3] & ~((0x7fu << 12u) | 0xfffu)) | (bufferFormat << 12u) | dstSel;
}
if (offset != 0u) {
const auto base = ((static_cast<std::uint64_t>(resource.fields[0]) | (static_cast<std::uint64_t>(resource.fields[1]) << 32u)) & 0xffffffffffffull) + offset;
resource.fields[0] = static_cast<std::uint32_t>(base & 0xffffffffu);
resource.fields[1] = (resource.fields[1] & 0xffff0000u) | static_cast<std::uint32_t>((base >> 32u) & 0xffffu);
}
++info.resourcesNum;
}
return info;
}
}
+1
View File
@@ -80,6 +80,7 @@ function(add_shader_recompiler target runtimeTarget)
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/Optimization/include
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/SpirvBackend/include
)
target_compile_features(${target} PUBLIC cxx_std_20)
set_target_properties(${target} PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_EXTENSIONS OFF)
target_link_libraries(${target} PRIVATE ${runtimeTarget})
+22
View File
@@ -65,12 +65,34 @@ struct ShaderPixelStageInfo {
std::array<std::uint8_t, 8> targetOutputMode;
};
struct ShaderVertexBufferResource {
std::array<std::uint32_t, 4> fields;
};
struct ShaderVertexResourceDestination {
std::int32_t registerStart;
std::int32_t registersNum;
std::int32_t attrId;
std::uint32_t fetchIndex;
};
struct ShaderVertexStageInfo {
static constexpr std::uint32_t MaxResources = 32;
std::array<ShaderVertexBufferResource, MaxResources> resources;
std::array<ShaderVertexResourceDestination, MaxResources> resourcesDst;
std::uint32_t resourcesNum;
std::uint32_t fetchAttribReg;
std::uint32_t fetchBufferReg;
bool fetchEmbedded;
};
struct GuestContext {
std::uint32_t waveSize;
std::uint32_t userDataBaseRegister;
std::span<const std::uint32_t> userData;
std::optional<ShaderComputeStageInfo> compute;
std::optional<ShaderPixelStageInfo> pixel;
std::optional<ShaderVertexStageInfo> vertex;
std::span<const MemoryRegion> memory;
};
@@ -1,4 +1,7 @@
#include "Translation/ShaderInputInfoBuilder.hpp"
#include "IntermediateRepresentation/IrMetadata.hpp"
#include <array>
#include <cstdint>
#include <stdexcept>
namespace ShaderRecompiler {
@@ -7,6 +10,60 @@ namespace {
thread_local ShaderPixelInputInfo pixelStorage;
thread_local ShaderComputeInputInfo computeStorage;
thread_local ShaderVertexInputInfo vertexStorage;
IrShaderStage _toIrShaderStage(ShaderStageKind stage) {
switch (stage) {
case ShaderStageKind::Vertex: return IrShaderStage::Vertex;
case ShaderStageKind::Local: return IrShaderStage::Local;
case ShaderStageKind::TessellationControl: return IrShaderStage::TessellationControl;
case ShaderStageKind::TessellationEvaluation: return IrShaderStage::TessellationEvaluation;
case ShaderStageKind::Mesh: return IrShaderStage::Mesh;
default: throw std::runtime_error("ShaderInputInfoBuilder: unexpected vertex-family stage");
}
}
void _detectVertexBuffers(ShaderVertexInputInfo& info) {
info.buffersNum = 0;
for (int ri = 0; ri < info.resourcesNum; ++ri) {
const auto& r = info.resources[ri];
const std::uint16_t stride = static_cast<std::uint16_t>((r.fields[1] >> 16u) & 0x3fffu);
const std::uint64_t base = (static_cast<std::uint64_t>(r.fields[0]) | (static_cast<std::uint64_t>(r.fields[1]) << 32u)) & 0xffffffffffffull;
const std::uint32_t numRecords = r.fields[2];
bool merged = false;
for (int bi = 0; bi < info.buffersNum; ++bi) {
auto& b = info.buffers[bi];
if (b.stride != stride || b.fetchIndex != static_cast<std::uint32_t>(info.resourcesDst[ri].fetchIndex)) continue;
const auto low = base < b.addr ? base : b.addr;
const auto offset1 = base - low;
const auto offset2 = b.addr - low;
if (offset1 >= stride || offset2 >= stride) continue;
if (b.numRecords != numRecords) throw std::runtime_error("ShaderInputInfoBuilder: merged vertex buffers disagree on record count");
b.addr = low;
if (b.attrNum >= ShaderVertexInputBuffer::MaxAttributes) throw std::runtime_error("ShaderInputInfoBuilder: vertex buffer attribute count exceeds the supported domain");
b.attrIndices[b.attrNum++] = ri;
merged = true;
break;
}
if (merged) continue;
if (info.buffersNum >= ShaderVertexInputInfo::MaxResources) throw std::runtime_error("ShaderInputInfoBuilder: vertex buffer count exceeds the supported domain");
auto& b = info.buffers[info.buffersNum++];
b.addr = base;
b.stride = stride;
b.numRecords = numRecords;
b.fetchIndex = static_cast<std::uint32_t>(info.resourcesDst[ri].fetchIndex);
b.attrNum = 1;
b.attrIndices[0] = ri;
}
for (int bi = 0; bi < info.buffersNum; ++bi) {
auto& b = info.buffers[bi];
for (int ri = 0; ri < b.attrNum; ++ri) {
const auto& r = info.resources[b.attrIndices[ri]];
const std::uint64_t base = (static_cast<std::uint64_t>(r.fields[0]) | (static_cast<std::uint64_t>(r.fields[1]) << 32u)) & 0xffffffffffffull;
b.attrOffsets[ri] = static_cast<std::uint32_t>(base - b.addr);
}
}
}
}
@@ -54,8 +111,30 @@ ShaderStageInputInfo BuildShaderStageInputInfo(ShaderStageKind stage, const Gues
case ShaderStageKind::Local:
case ShaderStageKind::TessellationControl:
case ShaderStageKind::TessellationEvaluation:
case ShaderStageKind::Mesh:
throw std::runtime_error("ShaderInputInfoBuilder: vertex/tessellation/mesh input info is not implemented, embedded fetch metadata source is unavailable");
case ShaderStageKind::Mesh: {
if (!context.vertex.has_value()) {
throw std::runtime_error("ShaderInputInfoBuilder: GuestContext.vertex is not set");
}
const auto& vertex = *context.vertex;
vertexStorage = ShaderVertexInputInfo{};
vertexStorage.logicalStage = _toIrShaderStage(stage);
vertexStorage.fetchEmbedded = vertex.fetchEmbedded;
vertexStorage.fetchExternal = false;
vertexStorage.fetchAttribReg = static_cast<int>(vertex.fetchAttribReg);
vertexStorage.fetchBufferReg = static_cast<int>(vertex.fetchBufferReg);
vertexStorage.resourcesNum = static_cast<int>(vertex.resourcesNum);
for (std::uint32_t i = 0; i < vertex.resourcesNum; ++i) {
vertexStorage.resources[i].fields = vertex.resources[i].fields;
vertexStorage.resourcesDst[i].registerStart = vertex.resourcesDst[i].registerStart;
vertexStorage.resourcesDst[i].registersNum = vertex.resourcesDst[i].registersNum;
vertexStorage.resourcesDst[i].attrId = vertex.resourcesDst[i].attrId;
vertexStorage.resourcesDst[i].fetchIndex = vertex.resourcesDst[i].fetchIndex;
}
_detectVertexBuffers(vertexStorage);
ShaderStageInputInfo result;
result.vertex = &vertexStorage;
return result;
}
case ShaderStageKind::Unknown:
case ShaderStageKind::Fetch:
throw std::runtime_error("ShaderInputInfoBuilder: unexpected stage");