diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp index 58df7118cb35..2b859eb916cf 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp @@ -2144,6 +2144,7 @@ void VulkanDevice::QueuePresent() { ShaderRecompiler::SpirvTarget VulkanDevice::Target() const { const auto& limits = state->properties.limits; ShaderRecompiler::SpirvTarget target{VK_API_VERSION_1_1, state->meshShader ? 0x00010400u : 0x00010300u, state->subgroup.subgroupSize, ShaderRecompiler::BdaAbi::Version, state->capabilities, state->spirvExtensions, false, {limits.maxComputeWorkGroupSize[0], limits.maxComputeWorkGroupSize[1], limits.maxComputeWorkGroupSize[2]}, limits.maxComputeWorkGroupInvocations, limits.maxComputeSharedMemorySize, {}, {}}; + target.fragmentShaderBarycentricEnabled = state->fragmentShaderBarycentric; if (state->meshShader) { const auto& mesh = state->meshLimits; target.mesh = ShaderRecompiler::MeshTargetLimits{{mesh.maxMeshWorkGroupSize[0], mesh.maxMeshWorkGroupSize[1], mesh.maxMeshWorkGroupSize[2]}, mesh.maxMeshWorkGroupInvocations, std::min(mesh.maxMeshSharedMemorySize, mesh.maxMeshPayloadAndSharedMemorySize), mesh.maxMeshOutputVertices, mesh.maxMeshOutputPrimitives, mesh.maxMeshOutputComponents, std::min(mesh.maxMeshOutputMemorySize, mesh.maxMeshPayloadAndOutputMemorySize), mesh.meshOutputPerVertexGranularity, mesh.meshOutputPerPrimitiveGranularity}; diff --git a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp index 5984166b3050..7ab3daeffebb 100644 --- a/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/Graphics.cpp @@ -282,7 +282,7 @@ bool readsBuiltin(const std::vector& read, spv::BuiltIn builtin) { return std::find(read.begin(), read.end(), builtin) != read.end(); } -std::vector pixelNoPerspectiveLocations() { +std::vector pixelNoPerspectiveLocations(bool barycentricEnabled) { auto queue = makeState(); queue.context[0x1b3] = 0x22u; queue.context[0x1b4] = 0x22u; @@ -298,18 +298,27 @@ std::vector pixelNoPerspectiveLocations() { request.target.vulkanVersion = 0x00401000u; request.target.spirvVersion = 0x00010300u; request.target.subgroupSize = 64; - request.target.fragmentShaderBarycentricEnabled = true; + request.target.fragmentShaderBarycentricEnabled = barycentricEnabled; request.layout.pushConstantSizeBytes = 128; request.useCache = false; const auto result = ShaderRecompiler::Recompile(request); const auto& words = result.spirv.Words(); std::map locations; std::vector noPerspective; + std::uint32_t perVertex = 0; + bool perspectiveBarycentrics = false; + bool linearBarycentrics = false; for (std::size_t at = 5; at < words.size() && (words[at] >> 16u) != 0; at += words[at] >> 16u) { if (static_cast(words[at] & 0xffffu) != spv::OpDecorate) continue; if (words[at + 2] == spv::DecorationLocation) locations[words[at + 1]] = words[at + 3]; if (words[at + 2] == spv::DecorationNoPerspective) noPerspective.push_back(words[at + 1]); + if (words[at + 2] == spv::DecorationPerVertexKHR) perVertex++; + if (words[at + 2] == spv::DecorationBuiltIn) { + perspectiveBarycentrics |= words[at + 3] == spv::BuiltInBaryCoordKHR; + linearBarycentrics |= words[at + 3] == spv::BuiltInBaryCoordNoPerspKHR; + } } + if (barycentricEnabled) Require(perVertex == 2u && perspectiveBarycentrics && linearBarycentrics, "explicit interpolation must preserve both parameter vertices and both barycentric inputs"); std::vector result2; for (const auto id : noPerspective) result2.push_back(locations.count(id) != 0 ? locations.at(id) : 0xffffffffu); return result2; @@ -350,8 +359,9 @@ void PixelInputLayoutTests() { const auto read = pixelBuiltinsRead(0x106u, 0x106u, source); Require(readsBuiltin(read, spv::BuiltInBaryCoordKHR) && !readsBuiltin(read, spv::BuiltInFragCoord), "a centroid-layout I/J VGPR does not hold the barycentrics: v" + std::to_string(source)); } - const auto noPerspective = pixelNoPerspectiveLocations(); + const auto noPerspective = pixelNoPerspectiveLocations(false); Require(noPerspective.size() == 1 && noPerspective[0] == 1u, "only the parameter interpolated through the linear pair must be NoPerspective"); + Require(pixelNoPerspectiveLocations(true).empty(), "explicit interpolation must not interpolate parameter arrays a second time"); auto read = pixelBuiltinsRead(0x106u, 0x106u, 4u); Require(readsBuiltin(read, spv::BuiltInFragCoord) && !readsBuiltin(read, spv::BuiltInBaryCoordKHR), "POS_X is not in v4 after the center and centroid pairs"); read = pixelBuiltinsRead(0x326u, 0x326u, 5u); @@ -1389,10 +1399,10 @@ void pixelParameterSlotTests() { const std::array shared{0xc8100000u, 0xc8110001u, 0xc8140500u, 0xc8150501u, 0xf800180fu, 0x05040504u, 0xbf810000u}; pixel = recompilePixel({0x3u, 0x3u}, shared); inputs = locatedInputs(pixel.spirv.Words()); - Require(inputs.size() == 1 && inputs[0].location == 3 && !inputs[0].perVertex && !inputs[0].flat, "inputs reading one slot were not declared once at the slot"); + Require(inputs.size() == 1 && inputs[0].location == 3 && inputs[0].perVertex && !inputs[0].flat, "inputs reading one slot were not declared once at the slot"); pixel = recompilePixel({0x404u, 0x0u}, shared); inputs = locatedInputs(pixel.spirv.Words()); - Require(inputs.size() == 2 && inputs[0].location == 0 && !inputs[0].flat && inputs[1].location == 4 && inputs[1].flat, "flat and interpolated inputs of different slots moved"); + Require(inputs.size() == 2 && inputs[0].location == 0 && inputs[0].perVertex && inputs[1].location == 4 && inputs[1].perVertex, "flat and interpolated inputs of different slots moved"); pixel = recompilePixel({0x20u, 0x2320u}, shared); Require(locatedInputs(pixel.spirv.Words()).empty(), "a defaulted input was declared as a parameter"); AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, true); diff --git a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp index 4f9586ac2d92..3f56ab5e860b 100644 --- a/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp +++ b/core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp @@ -573,7 +573,7 @@ std::vector> slotInputs(std::initializer_list shared{0xc8100000u, 0xc8110001u, 0xc8140500u, 0xc8150501u, 0xf800180fu, 0x05040504u, 0xbf810000u}; auto inputs = slotInputs({0x3u, 0x3u}, shared); - require(inputs.size() == 1u && inputs[0].first == 3u && !inputs[0].second, "inputs reading one slot were not declared once at the slot"); + require(inputs.size() == 1u && inputs[0].first == 3u && inputs[0].second, "inputs reading one slot were not declared once at the slot"); inputs = slotInputs({0x404u, 0x0u}, shared); require(inputs.size() == 2u && inputs[0].first == 0u && inputs[1].first == 4u, "inputs of different slots moved"); require(slotInputs({0x20u, 0x2320u}, shared).empty(), "a defaulted input was declared as a parameter"); diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 73e1d092e41d..4b9f4dafed0e 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -12,7 +12,7 @@ class TranslationContext { TranslationContext(IrProgram& program, IrBlock& block, std::uint32_t vectorLimit); void TranslateInstruction(const RdnaInstruction& instruction); - void SetPixelInput(const ShaderPixelInputInfo* info) { pixelInput = info; } + void SetPixelInput(const ShaderPixelInputInfo* info, bool barycentricEnabled) { pixelInput = info; fragmentShaderBarycentricEnabled = barycentricEnabled; } void TranslateEmbeddedFetch(const RdnaInstruction& instruction, std::uint32_t attribute, std::uint32_t componentCount, const ShaderBufferResource& resource); void AddBranchCondition(const BasicBlock& source, BlockInfo& info); @@ -223,7 +223,7 @@ class TranslationContext { void vReadlaneB32(const RdnaInstruction& inst); void vWritelaneB32(const RdnaInstruction& inst); void vPermlane16B32(const RdnaInstruction& inst, bool x16); - void vInterpP1F32(); + void vInterpP1F32(const RdnaInstruction& inst); void vInterpP2F32(const RdnaInstruction& inst); void vInterpMovF32(const RdnaInstruction& inst); void eXP(const RdnaInstruction& inst); @@ -234,6 +234,7 @@ class TranslationContext { IrProgram& program; const ShaderPixelInputInfo* pixelInput = nullptr; + bool fragmentShaderBarycentricEnabled = false; IrBuilder ir; IrBlock& block; IrU1 instructionBranchCondition; diff --git a/core/shader/recompiler/Translation/src/AttributeInstructions.cpp b/core/shader/recompiler/Translation/src/AttributeInstructions.cpp index a39ace36a460..f9d247a0e481 100644 --- a/core/shader/recompiler/Translation/src/AttributeInstructions.cpp +++ b/core/shader/recompiler/Translation/src/AttributeInstructions.cpp @@ -102,10 +102,23 @@ ExportFlags TranslationContext::addExportInfo(const RdnaInstruction& inst) { return ExportFlags{index, inst.programCounter}; } -void TranslationContext::vInterpP1F32() { +void TranslationContext::vInterpP1F32(const RdnaInstruction& inst) { + if (!fragmentShaderBarycentricEnabled) return; + auto& delta = ir.Emit(IrOpcode::GetInterpolationParameter, IrType::U32, {&ir.Constant(inst.source1.value), &ir.Constant(inst.source2.value), &ir.Constant(0u)}); + auto& origin = ir.Emit(IrOpcode::GetInterpolationParameter, IrType::U32, {&ir.Constant(inst.source1.value), &ir.Constant(inst.source2.value), &ir.Constant(2u)}); + auto& product = ir.Emit(IrOpcode::FPMul32, IrType::F32, {&ir.BitCastF32(delta), readOperand(inst.source0, IrType::F32)}); + auto& result = ir.Emit(IrOpcode::FPAdd32, IrType::F32, {&product, &ir.BitCastF32(origin)}); + writeOperand(inst.destination, &result); } void TranslationContext::vInterpP2F32(const RdnaInstruction& inst) { + if (fragmentShaderBarycentricEnabled) { + auto& delta = ir.Emit(IrOpcode::GetInterpolationParameter, IrType::U32, {&ir.Constant(inst.source1.value), &ir.Constant(inst.source2.value), &ir.Constant(1u)}); + auto& product = ir.Emit(IrOpcode::FPMul32, IrType::F32, {&ir.BitCastF32(delta), readOperand(inst.source0, IrType::F32)}); + auto& result = ir.Emit(IrOpcode::FPAdd32, IrType::F32, {&product, readOperand(inst.destination, IrType::F32)}); + writeOperand(inst.destination, &result); + return; + } if (pixelInput != nullptr && inst.source0.kind == RdnaOperandKind::VectorRegister && inst.source1.value < 32u) { const auto readsPair = [&](PixelInput input) { const auto base = pixelInput->psInputVgpr[static_cast(input)]; @@ -146,9 +159,12 @@ void TranslationContext::eXP(const RdnaInstruction& inst) { } bool TranslationContext::emitInterpolation(const RdnaInstruction& inst) { + if ((inst.op == RdnaOpcode::VInterpP1F32 || inst.op == RdnaOpcode::VInterpP2F32) && pixelInput != nullptr && pixelInput->InputIsCustom(inst.source1.value)) { + throw std::runtime_error("pixel input " + std::to_string(inst.source1.value) + " passes its vertices through unchanged but is read with v_interp_p1/p2"); + } switch (inst.op) { case RdnaOpcode::VInterpP1F32: - vInterpP1F32(); + vInterpP1F32(inst); return true; case RdnaOpcode::VInterpP2F32: vInterpP2F32(inst); diff --git a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp index 7f898e9698df..6ee8500eb832 100644 --- a/core/shader/recompiler/Translation/src/InstructionTranslator.cpp +++ b/core/shader/recompiler/Translation/src/InstructionTranslator.cpp @@ -439,7 +439,7 @@ IrProgram InstructionTranslator::Translate(const RdnaProgram& decoded, const Con for (const auto& cfgBlock : cfg.blocks) { const auto typedIndex = blockIndices.at(cfgBlock.id); TranslationContext context(program, *blocks[typedIndex], vectorLimit); - context.SetPixelInput(options.inputInfo.pixel); + context.SetPixelInput(options.inputInfo.pixel, options.fragmentShaderBarycentricEnabled); for (std::uint32_t index = cfgBlock.instructionBegin; index < cfgBlock.instructionEnd; index++) { const auto& instruction = decoded.instructions[index]; if (isCodeTableLoad(cfg, instruction.programCounter)) {