diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/CacheControl.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/CacheControl.cpp new file mode 100644 index 000000000..2c53bea7e --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/CacheControl.cpp @@ -0,0 +1,77 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include + +namespace { + +using AgcDriver::Graphics::Require; +using ShaderRecompiler::ShaderStage; + +constexpr std::uint32_t Threads = 64; +constexpr std::uint32_t Stride = 4; +alignas(256) std::array Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array CacheControlCode{ + 0x34020082, 0xbf930000, 0xbf940001, 0xe0302000, 0x80000401, 0xf4840000, 0x00000000, 0xf4800000, + 0x00000000, 0xf47c0000, 0x00000000, 0xe1c80000, 0x00000000, 0xe1c40000, 0x00000000, 0xbfa20000, + 0xbf8c3f70, 0xbfa80001, 0x4a080881, 0xbf950001, 0xe0702000, 0x80010401, 0xbf810000, +}; + +std::array BufferDescriptor(const void* data, std::uint32_t count) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((address >> 32u) & 0xffffu) | (4u << 16u), count, 0x01016facu}; +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Input[tid * Stride] = tid * 0x01010101u + 7u; + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size())); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size())); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(CacheControlCode); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(code.data()), code, 0, {}}, + {32, 0, userData, compute, std::nullopt, std::nullopt, memory}, + device.Target(), + {0, 0, 0, 128} + }; + request.useCache = false; + const auto result = ShaderRecompiler::Recompile(request); + device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast(code.data())); + device.WaitIdle(); +} + +void Check() { + for (std::uint32_t tid = 0; tid < Threads; ++tid) { + const auto expected = Input[tid * Stride] + 1u; + const auto actual = Output[tid * Stride]; + Require(actual == expected, "cache control: thread " + std::to_string(tid) + " is " + std::to_string(actual) + ", expected " + std::to_string(expected)); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("cache control tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index f825c41ff..4676f95ab 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -610,6 +610,15 @@ enum class RdnaOpcode : std::uint16_t { SInstPrefetch, SClause, SCbranchCdbg, + SIcacheInv, + SIncperflevel, + SDecperflevel, + SWaitIdle, + SGl1Inv, + SDcacheInv, + SDcacheWb, + BufferGl0Inv, + BufferGl1Inv, Exp, VMulHiI32I24, VMulHiU32U24, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index b643c7fbb..068c2947e 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -357,6 +357,32 @@ std::uint32_t toProgramCounter(std::uint32_t wordIndex) { return wordIndex * 4u; } +RdnaOpcode cacheControlOpcode(RdnaInstructionFamily family, std::uint32_t opcode) { + if (family == RdnaInstructionFamily::SMEM) { + switch (opcode) { + case 0x1fu: return RdnaOpcode::SGl1Inv; + case 0x20u: return RdnaOpcode::SDcacheInv; + case 0x21u: return RdnaOpcode::SDcacheWb; + default: return RdnaOpcode::Invalid; + } + } + switch (opcode) { + case 0x71u: return RdnaOpcode::BufferGl0Inv; + case 0x72u: return RdnaOpcode::BufferGl1Inv; + default: return RdnaOpcode::Invalid; + } +} + +RdnaInstruction cacheControlInstruction(RdnaInstructionFamily family, RdnaOpcode op, std::uint32_t opcode, std::uint32_t programCounter, std::span code, std::uint32_t wordIndex) { + RdnaInstruction instruction{}; + instruction.programCounter = programCounter; + instruction.family = family; + instruction.opcodeId = opcode; + instruction.op = op; + setRawWords(instruction, code, wordIndex, 2u); + return instruction; +} + } RdnaInstruction DecodeRdnaSmem(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex) { @@ -371,6 +397,9 @@ RdnaInstruction DecodeRdnaSmem(std::uint32_t programCounter, std::span> 6u) & 0x7Fu; const auto sbase = word0 & 0x3Fu; const auto soffsetCode = (word1 >> 25u) & 0x7Fu; + if (const auto cacheOp = cacheControlOpcode(RdnaInstructionFamily::SMEM, opcode); cacheOp != RdnaOpcode::Invalid) { + return cacheControlInstruction(RdnaInstructionFamily::SMEM, cacheOp, opcode, programCounter, code, wordIndex); + } const auto& info = lookupOpcode(smemOpcodes, opcode, "SMEM opcode is not supported"); RdnaInstruction instruction{}; @@ -413,6 +442,9 @@ RdnaInstruction DecodeRdnaMubuf(std::uint32_t programCounter, std::span> 16u) & 0x1Fu; const auto soffsetCode = (word1 >> 24u) & 0xFFu; + if (const auto cacheOp = cacheControlOpcode(RdnaInstructionFamily::MUBUF, opcode); cacheOp != RdnaOpcode::Invalid) { + return cacheControlInstruction(RdnaInstructionFamily::MUBUF, cacheOp, opcode, programCounter, code, wordIndex); + } const auto& info = lookupOpcode(mubufOpcodes, opcode, "MUBUF opcode is not supported"); RdnaInstruction instruction{}; diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp index df65c3e89..a48ae2d81 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaScalarOpDecoder.cpp @@ -191,6 +191,9 @@ RdnaOpcode decodeSoppOpcode(std::uint32_t opcode) { case 0x0fu: return RdnaOpcode::SSetprio; case 0x10u: return RdnaOpcode::SSendmsg; case 0x12u: return RdnaOpcode::STrap; + case 0x13u: return RdnaOpcode::SIcacheInv; + case 0x14u: return RdnaOpcode::SIncperflevel; + case 0x15u: return RdnaOpcode::SDecperflevel; case 0x16u: return RdnaOpcode::STtracedata; case 0x17u: case 0x18u: @@ -198,7 +201,9 @@ RdnaOpcode decodeSoppOpcode(std::uint32_t opcode) { case 0x1au: return RdnaOpcode::SCbranchCdbg; case 0x20u: return RdnaOpcode::SInstPrefetch; case 0x21u: return RdnaOpcode::SClause; + case 0x22u: return RdnaOpcode::SWaitIdle; case 0x23u: return RdnaOpcode::SWaitcntDepctr; + case 0x28u: return RdnaOpcode::STtracedata; default: throw std::invalid_argument("unsupported SOPP opcode " + std::to_string(opcode)); } } @@ -214,7 +219,8 @@ bool isSoppWaitOpcode(RdnaOpcode opcode) { return opcode == RdnaOpcode::SNop || opcode == RdnaOpcode::SWaitcnt || opcode == RdnaOpcode::SWaitcntDepctr || opcode == RdnaOpcode::SSleep || opcode == RdnaOpcode::SSetprio || opcode == RdnaOpcode::SSendmsg || opcode == RdnaOpcode::STrap || opcode == RdnaOpcode::STtracedata || opcode == RdnaOpcode::SInstPrefetch || - opcode == RdnaOpcode::SClause || opcode == RdnaOpcode::SCbranchCdbg; + opcode == RdnaOpcode::SClause || opcode == RdnaOpcode::SCbranchCdbg || opcode == RdnaOpcode::SIcacheInv || + opcode == RdnaOpcode::SIncperflevel || opcode == RdnaOpcode::SDecperflevel || opcode == RdnaOpcode::SWaitIdle; } std::uint32_t scalarDestinationDwordCount(RdnaOpcode opcode) { diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index be6f77519..f7f7c3af6 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -10,6 +10,14 @@ void TranslateMemoryInstruction(IrBuilder& builder, const RdnaInstruction& instr bool TranslationContext::emitMemory(const RdnaInstruction& inst) { switch (inst.op) { + case RdnaOpcode::SGl1Inv: + case RdnaOpcode::SDcacheInv: + case RdnaOpcode::SDcacheWb: + case RdnaOpcode::BufferGl0Inv: + case RdnaOpcode::BufferGl1Inv: + emitControlNop(); + return true; + case RdnaOpcode::SLoadDword: case RdnaOpcode::SLoadDwordx2: case RdnaOpcode::SLoadDwordx4: diff --git a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp index 69d5eddb6..f4ae67ec0 100644 --- a/core/shader/recompiler/Translation/src/ScalarInstructions.cpp +++ b/core/shader/recompiler/Translation/src/ScalarInstructions.cpp @@ -350,9 +350,13 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) { case RdnaOpcode::SSetprio: case RdnaOpcode::STrap: case RdnaOpcode::SClause: + case RdnaOpcode::SIcacheInv: + case RdnaOpcode::SIncperflevel: + case RdnaOpcode::SDecperflevel: emitControlNop(); return true; case RdnaOpcode::SWaitcntDepctr: + case RdnaOpcode::SWaitIdle: emitWaitcnt(); return true; case RdnaOpcode::SBarrier: