mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #253 from adriadam10/feat/shader-cache-hint-noops
feat(shader): decode cache invalidations and performance hints as no-ops
This commit is contained in:
@@ -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 <array>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
using AgcDriver::Graphics::Require;
|
||||
using ShaderRecompiler::ShaderStage;
|
||||
|
||||
constexpr std::uint32_t Threads = 64;
|
||||
constexpr std::uint32_t Stride = 4;
|
||||
alignas(256) std::array<std::uint32_t, Threads * Stride> Input{};
|
||||
alignas(256) std::array<std::uint32_t, Threads * Stride> Output{};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 23> 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<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t count) {
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(data);
|
||||
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((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<std::uint32_t> userData(8, 0u);
|
||||
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size()));
|
||||
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size()));
|
||||
std::copy(input.begin(), input.end(), userData.begin());
|
||||
std::copy(output.begin(), output.end(), userData.begin() + 4);
|
||||
const std::span<const std::uint32_t> code(CacheControlCode);
|
||||
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(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<std::uintptr_t>(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<std::uintptr_t>(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;
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
@@ -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<const std::uint32_t> 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<const std::uint32_t> code, std::uint32_t wordIndex) {
|
||||
@@ -371,6 +397,9 @@ RdnaInstruction DecodeRdnaSmem(std::uint32_t programCounter, std::span<const std
|
||||
const auto sdst = (word0 >> 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<const st
|
||||
const auto vaddr = word1 & 0xFFu;
|
||||
const auto srsrc = (word1 >> 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{};
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+8
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user