mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #333 from adriadam10/feat/sopk-misc
feat(shader): implement s_cmovk_i32, s_version and s_setreg_imm32_b32
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp"
|
||||
#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp"
|
||||
#include "Recompiler.hpp"
|
||||
#include "VulkanTestDevice.hpp"
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
using AgcDriver::Graphics::Require;
|
||||
using ShaderRecompiler::ShaderStage;
|
||||
|
||||
constexpr std::uint32_t Threads = 32;
|
||||
constexpr std::uint32_t Inputs = 4;
|
||||
constexpr std::uint32_t Results = 16;
|
||||
alignas(256) std::array<std::uint32_t, Threads * Inputs> Input{};
|
||||
alignas(256) std::array<std::uint32_t, Threads * Results> Output{};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 31> Code{
|
||||
0x34060086, 0xbe9403ff, 0x11111111, 0xbe9503ff, 0x22222222, 0xbe9703ff, 0x33333333, 0xbf068080,
|
||||
0xb1148001, 0xbf068180, 0xb1157fff, 0xbf068080, 0xb1171234, 0xb0801234, 0xba801801, 0x00000000,
|
||||
0xbe9603ff, 0x44444444, 0x7e140214, 0x7e160215, 0x7e180216, 0x7e1a0217, 0xe0701000, 0x80010a03,
|
||||
0xe0701004, 0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xbf810000,
|
||||
};
|
||||
|
||||
alignas(256) constexpr std::array<std::uint32_t, 3> RoundTowardZeroCode{0xba801801, 0x0000000f, 0xbf810000};
|
||||
|
||||
constexpr std::uint32_t Expected[4] = {0xffff8001u, 0x22222222u, 0x44444444u, 0x00001234u};
|
||||
constexpr const char* Names[4] = {"cmovk_taken", "cmovk_not_taken", "after_setreg_imm32", "cmovk_positive"};
|
||||
|
||||
void Fill(std::uint32_t, std::uint32_t*) {}
|
||||
|
||||
std::array<std::uint32_t, 4> BufferDescriptor(const void* data, std::uint32_t bytes) {
|
||||
const auto address = reinterpret_cast<std::uintptr_t>(data);
|
||||
return {static_cast<std::uint32_t>(address), static_cast<std::uint32_t>((address >> 32u) & 0xffffu), bytes, 0x01016facu};
|
||||
}
|
||||
|
||||
std::string Hex(std::uint32_t value) {
|
||||
char text[16];
|
||||
std::snprintf(text, sizeof(text), "0x%08x", value);
|
||||
return text;
|
||||
}
|
||||
|
||||
void Expect(std::uint32_t tid, std::uint32_t actual, std::uint32_t expected, const char* name) {
|
||||
Require(actual == expected, std::string("scalar sopk misc: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected));
|
||||
}
|
||||
|
||||
ShaderRecompiler::RecompileRequest Request(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, std::span<const std::uint32_t> userData, std::span<const ShaderRecompiler::MemoryRegion> memory) {
|
||||
const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {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;
|
||||
return request;
|
||||
}
|
||||
|
||||
void ExpectRefused(AgcDriver::VulkanDevice& device) {
|
||||
const std::span<const std::uint32_t> code(RoundTowardZeroCode);
|
||||
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}};
|
||||
const std::vector<std::uint32_t> userData(8, 0u);
|
||||
bool refused = false;
|
||||
try {
|
||||
ShaderRecompiler::Recompile(Request(device, code, userData, memory));
|
||||
} catch (const std::exception&) {
|
||||
refused = true;
|
||||
}
|
||||
Require(refused, "scalar sopk misc: s_setreg_imm32_b32 writing round toward zero to MODE was translated");
|
||||
}
|
||||
|
||||
void Run(AgcDriver::VulkanDevice& device) {
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]);
|
||||
Output.fill(0xdeadbeefu);
|
||||
std::vector<std::uint32_t> userData(8, 0u);
|
||||
const auto input = BufferDescriptor(Input.data(), static_cast<std::uint32_t>(Input.size() * 4u));
|
||||
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(Output.size() * 4u));
|
||||
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(Code);
|
||||
const std::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}};
|
||||
const auto result = ShaderRecompiler::Recompile(Request(device, code, userData, memory));
|
||||
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 std::uint32_t* in = &Input[tid * Inputs];
|
||||
const std::uint32_t* out = &Output[tid * Results];
|
||||
for (std::uint32_t index = 0; index < 4; ++index) Expect(tid, out[index], Expected[index], Names[index]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
const auto device = OpenVulkanTestDevice();
|
||||
if (!device) return VulkanTestSkipped;
|
||||
Run(*device);
|
||||
Check();
|
||||
ExpectRefused(*device);
|
||||
std::puts("scalar sopk misc tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -895,6 +895,9 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
SWaitcntDepctr,
|
||||
SSendmsg,
|
||||
SSetregB32,
|
||||
SSetregImm32B32,
|
||||
SCmovkI32,
|
||||
SVersion,
|
||||
SSleep,
|
||||
SWakeup,
|
||||
STrap,
|
||||
|
||||
@@ -19,6 +19,7 @@ bool IsScalarAluOpcode(RdnaOpcode opcode) {
|
||||
case RdnaOpcode::SMovB32:
|
||||
case RdnaOpcode::SMovB64:
|
||||
case RdnaOpcode::SMovkI32:
|
||||
case RdnaOpcode::SCmovkI32:
|
||||
case RdnaOpcode::SAddI32:
|
||||
case RdnaOpcode::SAddU32:
|
||||
case RdnaOpcode::SSubI32:
|
||||
|
||||
@@ -159,6 +159,8 @@ RdnaOpcode decodeSopcOpcode(std::uint32_t opcode) {
|
||||
RdnaOpcode decodeSopkOpcode(std::uint32_t opcode) {
|
||||
switch (opcode) {
|
||||
case 0x00u: return RdnaOpcode::SMovkI32;
|
||||
case 0x01u: return RdnaOpcode::SVersion;
|
||||
case 0x02u: return RdnaOpcode::SCmovkI32;
|
||||
case 0x03u: return RdnaOpcode::SCmpEqI32;
|
||||
case 0x04u: return RdnaOpcode::SCmpLgI32;
|
||||
case 0x05u: return RdnaOpcode::SCmpGtI32;
|
||||
@@ -174,6 +176,7 @@ RdnaOpcode decodeSopkOpcode(std::uint32_t opcode) {
|
||||
case 0x0fu: return RdnaOpcode::SAddI32;
|
||||
case 0x10u: return RdnaOpcode::SMulkI32;
|
||||
case 0x13u: return RdnaOpcode::SSetregB32;
|
||||
case 0x15u: return RdnaOpcode::SSetregImm32B32;
|
||||
case 0x17u: return RdnaOpcode::SWaitcnt;
|
||||
case 0x18u: return RdnaOpcode::SWaitcnt;
|
||||
case 0x19u: return RdnaOpcode::SWaitcnt;
|
||||
@@ -384,7 +387,20 @@ RdnaInstruction DecodeRdnaSopk(std::uint32_t programCounter, std::span<const std
|
||||
instruction.sourceCount = 1;
|
||||
SetRdnaRawWords(instruction, code, wordIndex, 1);
|
||||
|
||||
if (instruction.op == RdnaOpcode::SMovkI32) {
|
||||
if (instruction.op == RdnaOpcode::SVersion) {
|
||||
instruction.destination.kind = RdnaOperandKind::Null;
|
||||
instruction.sourceCount = 0;
|
||||
return instruction;
|
||||
}
|
||||
if (instruction.op == RdnaOpcode::SSetregImm32B32) {
|
||||
instruction.destination.kind = RdnaOperandKind::Null;
|
||||
instruction.source1 = instruction.source0;
|
||||
instruction.source0 = DecodeRdnaScalarSource(0xffu, programCounter);
|
||||
instruction.sourceCount = 2;
|
||||
ReadRdnaLiteralOperands(code, wordIndex, instruction);
|
||||
return instruction;
|
||||
}
|
||||
if (instruction.op == RdnaOpcode::SMovkI32 || instruction.op == RdnaOpcode::SCmovkI32) {
|
||||
instruction.destination = DecodeRdnaScalarDestination(scalarRegister, programCounter);
|
||||
return instruction;
|
||||
}
|
||||
|
||||
@@ -319,6 +319,11 @@ EmbeddedFetchPlan EmbeddedVertexFetchAnalyzer::Analyze(const RdnaProgram& progra
|
||||
dst.constant = inst.source0.value;
|
||||
}
|
||||
break;
|
||||
case RdnaOpcode::SCmovkI32:
|
||||
if (isScalarOperand(inst.destination)) {
|
||||
clearScalarRange(sgprs, inst.destination, 1u);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (isScalarLoad(inst.op)) {
|
||||
if (isScalarOperand(inst.source0) && scalarSlot(inst.source0) < sgprs.size() && sgprs[scalarSlot(inst.source0)].kind == SgprValueKind::AttributeTable) {
|
||||
|
||||
@@ -52,8 +52,25 @@ bool TranslationContext::emitScalar(const RdnaInstruction& inst) {
|
||||
scalarSelect64(inst, inst.destination);
|
||||
return true;
|
||||
case RdnaOpcode::SSetregB32:
|
||||
case RdnaOpcode::SVersion:
|
||||
emitControlNop();
|
||||
return true;
|
||||
case RdnaOpcode::SSetregImm32B32: {
|
||||
const std::uint32_t field = inst.source1.value;
|
||||
const std::uint32_t offset = (field >> 6u) & 0x1fu;
|
||||
const std::uint32_t size = ((field >> 11u) & 0x1fu) + 1u;
|
||||
const std::uint64_t written = static_cast<std::uint64_t>(inst.source0.value) & ((std::uint64_t{1} << size) - 1u);
|
||||
if ((field & 0x3fu) != 1u || offset + size > 4u || written != 0u) {
|
||||
throw std::runtime_error("s_setreg_imm32_b32 at pc " + std::to_string(inst.programCounter) + " writes a hardware register field other than round to nearest even in MODE");
|
||||
}
|
||||
emitControlNop();
|
||||
return true;
|
||||
}
|
||||
case RdnaOpcode::SCmovkI32: {
|
||||
const IrU32 previous = readU32(inst.destination);
|
||||
writeRawU32(inst.destination, IrU32(ir.Select(ir.GetScc(), ir.Constant(inst.source0.value), previous.Value())));
|
||||
return true;
|
||||
}
|
||||
case RdnaOpcode::SWaitcnt:
|
||||
emitWaitcnt();
|
||||
return true;
|
||||
|
||||
@@ -53,6 +53,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
|
||||
### Functional
|
||||
|
||||
- The shader recompiler [ignores `s_setreg_b32`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): writes to MODE (float rounding and denormal controls) and the other hardware registers have no effect.
|
||||
- [Shader recompilation](../../core/shader/recompiler/Recompiler.cpp) currently occurs right before it was transferred to Vulkan with caching, but should be moved to the [relinker](../../core/relinker/main.cpp) stage. For this purpose, [shader/recompiler](../../core/shader/recompiler) was written completely independently from [libs/prx](../../core/libs/prx).
|
||||
- [v_fma_f64](../../core/shader/recompiler/SpirvBackend/src/SpirvAlu/SpirvAluEmitterFloat64.cpp) is fused exactly only for normal inputs: with a subnormal input it is a separate multiply and add, which rounds twice.
|
||||
- The executable file that [relinker](../../core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp) generates opens the console when launched, which is inconvenient for playability.
|
||||
|
||||
Reference in New Issue
Block a user