diff --git a/core/libs/prx/libSceAgc/Shader/include/ShaderConstants.hpp b/core/libs/prx/libSceAgc/Shader/include/ShaderConstants.hpp index 811e9cb55..022482de0 100644 --- a/core/libs/prx/libSceAgc/Shader/include/ShaderConstants.hpp +++ b/core/libs/prx/libSceAgc/Shader/include/ShaderConstants.hpp @@ -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 diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp index e87bb4df8..19b62aa79 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/Driver.cpp @@ -309,7 +309,7 @@ private: const auto codeOffset = static_cast((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}} diff --git a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp index f148422e3..3c6fec0f7 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/include/ShaderInputState.hpp @@ -3,11 +3,14 @@ #include "prx/libSceAgcDriver/Execution/include/QueueState.hpp" #include "Recompiler.hpp" +#include +#include namespace AgcDriver::Graphics { ShaderRecompiler::ShaderPixelStageInfo DecodePixelStageInfo(const Registers& context); ShaderRecompiler::ShaderComputeStageInfo DecodeComputeStageInfo(const Registers& shader); +ShaderRecompiler::ShaderVertexStageInfo DecodeVertexStageInfo(std::span header, std::uint64_t headerAddress, std::span userData); } diff --git a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp index 1178dca64..a51eed71c 100644 --- a/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp +++ b/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderInputState.cpp @@ -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 +#include #include #include @@ -25,6 +30,28 @@ std::uint32_t read(const Registers& registers, std::uint32_t offset) { return it->second; } +template T _readHeaderPod(std::span 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(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 void _readHeaderArray(std::span 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(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(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 header, std::uint64_t headerAddress, std::span 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(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 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(ShaderRegs::AgcDirectResourceType::PtrVertexBufferTable)) vertexBufferReg = reg; + if (type == static_cast(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(vertexBufferReg) + 1u >= userData.size()) throw std::runtime_error("AGC graphics: vertex buffer table pointer exceeds the user-SGPR domain"); + if (static_cast(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 semantics{}; + _readHeaderArray(header, headerAddress, shader.input_semantics, shader.num_input_semantics, semantics.data()); + const auto attribTableAddr = static_cast(userData[static_cast(vertexAttribReg)]) | (static_cast(userData[static_cast(vertexAttribReg) + 1u]) << 32u); + const auto bufferTableAddr = static_cast(userData[static_cast(vertexBufferReg)]) | (static_cast(userData[static_cast(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(vertexAttribReg); + info.fetchBufferReg = static_cast(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 attribWordBytes{}; + AgcDriver::GuestMemory::Read(attribTableAddr + static_cast(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 sharpBytes{}; + AgcDriver::GuestMemory::Read(bufferTableAddr + static_cast(index) * 16u, sharpBytes, 4); + std::array 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(semantic.hardware_mapping); + destination.registersNum = static_cast(semantic.size_in_elements); + destination.attrId = static_cast(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(resource.fields[0]) | (static_cast(resource.fields[1]) << 32u)) & 0xffffffffffffull) + offset; + resource.fields[0] = static_cast(base & 0xffffffffu); + resource.fields[1] = (resource.fields[1] & 0xffff0000u) | static_cast((base >> 32u) & 0xffffu); + } + ++info.resourcesNum; + } + return info; +} + } diff --git a/core/shader/recompiler/CMakeLists.txt b/core/shader/recompiler/CMakeLists.txt index 2fa6eeec1..8e7698fa6 100644 --- a/core/shader/recompiler/CMakeLists.txt +++ b/core/shader/recompiler/CMakeLists.txt @@ -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}) diff --git a/core/shader/recompiler/Recompiler.hpp b/core/shader/recompiler/Recompiler.hpp index e3d49b561..b669659e5 100644 --- a/core/shader/recompiler/Recompiler.hpp +++ b/core/shader/recompiler/Recompiler.hpp @@ -65,12 +65,34 @@ struct ShaderPixelStageInfo { std::array targetOutputMode; }; +struct ShaderVertexBufferResource { + std::array 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 resources; + std::array 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 userData; std::optional compute; std::optional pixel; + std::optional vertex; std::span memory; }; diff --git a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp index 61b78af7c..6eec0ad62 100644 --- a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp +++ b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp @@ -1,4 +1,7 @@ #include "Translation/ShaderInputInfoBuilder.hpp" +#include "IntermediateRepresentation/IrMetadata.hpp" +#include +#include #include 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((r.fields[1] >> 16u) & 0x3fffu); + const std::uint64_t base = (static_cast(r.fields[0]) | (static_cast(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(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(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(r.fields[0]) | (static_cast(r.fields[1]) << 32u)) & 0xffffffffffffull; + b.attrOffsets[ri] = static_cast(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(vertex.fetchAttribReg); + vertexStorage.fetchBufferReg = static_cast(vertex.fetchBufferReg); + vertexStorage.resourcesNum = static_cast(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");