feat(shader): add attr and compare instructions translators

This commit is contained in:
boykopovar
2026-09-16 10:32:38 +03:00
parent 4b2fe93080
commit 393a41fa2c
3 changed files with 188 additions and 14 deletions
+8
View File
@@ -16,6 +16,14 @@ A standalone [SPIR-V triangle test](core/libs/prx/libSceAgcDriver/tests/Visual.c
Now: `ShaderRecompiler::Recompile: InstructionTranslator::Translate not implementedgi`.
## Build
The relinker uses only the C++20 standard library and should build with any conforming compiler.
[libc.prx](core/libs/prx/libc) implementations contain compiler-specific code. Linux builds work with GCC; on Windows, MinGW-w64 GCC 15.2.0 (`x86_64-ucrt-posix-seh`) is currently required.
The project targets maximum compiler portability. Support for additional compilers will be addressed after the first successful game launch.
## Disclaimer
This project is intended for interoperability, research, preservation, and compatibility purposes. It does not include, distribute, or require copyrighted software, firmware, cryptographic keys, or proprietary libraries. Users are responsible for ensuring that any binaries used with this project are obtained and used in accordance with applicable laws and their respective license terms.
@@ -1,39 +1,171 @@
#include "Translation/AttributeInstructions.hpp"
#include "Translation/TranslationContext.hpp"
#include <algorithm>
#include <array>
#include <bit>
#include <stdexcept>
namespace ShaderRecompiler {
namespace {
ExportTargetKind exportTargetKindFromTarget(std::uint32_t target, std::uint32_t& index) {
index = 0u;
switch (target) {
case 0x08u: return ExportTargetKind::MrtZ;
case 0x09u: return ExportTargetKind::Null;
case 0x14u: return ExportTargetKind::Primitive;
default: break;
}
if (target <= 0x07u) {
index = target;
return ExportTargetKind::Mrt;
}
if (target >= 0x0cu && target <= 0x0fu) {
index = target - 0x0cu;
return ExportTargetKind::Position;
}
if (target >= 0x20u && target <= 0x3fu) {
index = target - 0x20u;
return ExportTargetKind::Parameter;
}
return ExportTargetKind::Unknown;
}
std::uint32_t getDstSel(std::uint32_t dstSelXYZW, std::uint32_t component) {
return (dstSelXYZW >> (component * 3u)) & 0x7u;
}
bool isIntegerBufferFormat(IrBufferFormat format) {
switch (format) {
case IrBufferFormat::Format8UInt:
case IrBufferFormat::Format8SInt:
case IrBufferFormat::Format16UInt:
case IrBufferFormat::Format16SInt:
case IrBufferFormat::Format8_8UInt:
case IrBufferFormat::Format8_8SInt:
case IrBufferFormat::Format32UInt:
case IrBufferFormat::Format32SInt:
case IrBufferFormat::Format16_16UInt:
case IrBufferFormat::Format16_16SInt:
case IrBufferFormat::Format11_11_10UInt:
case IrBufferFormat::Format11_11_10SInt:
case IrBufferFormat::Format10_11_11UInt:
case IrBufferFormat::Format10_11_11SInt:
case IrBufferFormat::Format2_10_10_10UInt:
case IrBufferFormat::Format2_10_10_10SInt:
case IrBufferFormat::Format10_10_10_2UInt:
case IrBufferFormat::Format10_10_10_2SInt:
case IrBufferFormat::Format8_8_8_8UInt:
case IrBufferFormat::Format8_8_8_8SInt:
case IrBufferFormat::Format32_32UInt:
case IrBufferFormat::Format32_32SInt:
case IrBufferFormat::Format16_16_16_16UInt:
case IrBufferFormat::Format16_16_16_16SInt:
case IrBufferFormat::Format32_32_32UInt:
case IrBufferFormat::Format32_32_32SInt:
case IrBufferFormat::Format32_32_32_32UInt:
case IrBufferFormat::Format32_32_32_32SInt:
return true;
default:
return false;
}
}
std::uint32_t formattedConstantBits(IrBufferFormat format, std::uint32_t selector) {
if (selector == 0u) {
return 0u;
}
if (selector == 1u) {
return isIntegerBufferFormat(format) ? 1u : std::bit_cast<std::uint32_t>(1.0f);
}
throw std::runtime_error("reserved buffer destination selector");
}
}
void TranslateAttributeInstruction(IrBuilder& builder, const RdnaInstruction& instruction, const TranslateOptions& options) {
throw std::runtime_error("TranslateAttributeInstruction not implemented");
}
ExportFlags TranslationContext::addExportInfo(const RdnaInstruction& inst) {
throw std::runtime_error("TranslationContext::addExportInfo not implemented");
ExportInfo info;
info.kind = exportTargetKindFromTarget(inst.exportTarget, info.index);
info.target = inst.exportTarget;
info.en = inst.exportEnableMask;
info.done = inst.exportIsLast;
info.compr = inst.exportIsCompressed;
info.vm = inst.exportValidMask;
const std::uint32_t index = static_cast<std::uint32_t>(program.Metadata().exportInfo.size());
program.Metadata().exportInfo.push_back(info);
return ExportFlags{index, inst.programCounter};
}
void TranslationContext::vInterpP1F32() {
throw std::runtime_error("TranslationContext::vInterpP1F32 not implemented");
}
void TranslationContext::vInterpP2F32(const RdnaInstruction& inst) {
throw std::runtime_error("TranslationContext::vInterpP2F32 not implemented");
IrValue& value = ir.Emit(IrOpcode::GetAttribute, IrType::U32, {&ir.Constant(inst.source1.value), &ir.Constant(inst.source2.value)});
writeOperand(inst.destination, &value);
}
void TranslationContext::vInterpMovF32(const RdnaInstruction& inst) {
throw std::runtime_error("TranslationContext::vInterpMovF32 not implemented");
if (inst.source0.value >= 3u) {
throw std::runtime_error("v_interp_mov_f32 mode is reserved");
}
IrValue& value = ir.Emit(IrOpcode::GetInterpolationParameter, IrType::U32, {&ir.Constant(inst.source1.value), &ir.Constant(inst.source2.value), &ir.Constant(inst.source0.value)});
writeOperand(inst.destination, &value);
}
void TranslationContext::eXP(const RdnaInstruction& inst) {
throw std::runtime_error("TranslationContext::eXP not implemented");
std::uint32_t index = 0u;
if (exportTargetKindFromTarget(inst.exportTarget, index) == ExportTargetKind::Unknown) {
throw std::runtime_error("unsupported EXP target");
}
std::array<IrValue*, 4> components{&ir.Constant(0u), &ir.Constant(0u), &ir.Constant(0u), &ir.Constant(0u)};
const std::uint32_t sourceCount = std::min(inst.sourceCount, 4u);
for (std::uint32_t source = 0u; source < sourceCount; ++source) {
components[source] = &readRawU32(plainOperand(sourceAt(inst, source))).Value();
}
IrValue& data = ir.Emit(IrOpcode::CompositeConstructU32x4, IrType::U32x4, {components[0], components[1], components[2], components[3]});
IrValue& exec = ir.GetExec();
(void)ir.Emit(IrOpcode::SetAttribute, IrType::Void, {&data, &exec}, addExportInfo(inst));
}
bool TranslationContext::emitInterpolation(const RdnaInstruction& inst) {
throw std::runtime_error("TranslationContext::emitInterpolation not implemented");
switch (inst.op) {
case RdnaOpcode::VInterpP1F32:
vInterpP1F32();
return true;
case RdnaOpcode::VInterpP2F32:
vInterpP2F32(inst);
return true;
case RdnaOpcode::VInterpMovF32:
vInterpMovF32(inst);
return true;
default:
return false;
}
}
void TranslationContext::TranslateEmbeddedFetch(const RdnaInstruction& instruction, std::uint32_t attribute, std::uint32_t componentCount, const ShaderBufferResource& resource) {
throw std::runtime_error("TranslationContext::TranslateEmbeddedFetch not implemented");
const auto format = static_cast<IrBufferFormat>((resource.fields[3] >> 12u) & 0x7Fu);
const std::uint32_t dstSel = resource.fields[3] & 0xFFFu;
for (std::uint32_t component = 0u; component < componentCount; ++component) {
const std::uint32_t selector = instruction.formatted && !instruction.typed ? getDstSel(dstSel, component) : component + 4u;
IrValue* value = nullptr;
if (selector <= 1u) {
value = &ir.Constant(formattedConstantBits(format, selector));
} else if (selector >= 4u && selector <= 7u) {
const std::uint32_t memoryComponent = selector - 4u;
value = &ir.Emit(IrOpcode::GetAttribute, IrType::U32, {&ir.Constant(attribute), &ir.Constant(memoryComponent)});
std::uint8_t& required = program.Info().vertexFetchComponents[attribute];
required = static_cast<std::uint8_t>(std::max<std::uint32_t>(required, memoryComponent + 1u));
} else {
throw std::runtime_error("invalid embedded fetch component selector");
}
writeOperand(offsetOperand(instruction.destination, component), value);
}
}
void TranslateAttributeInstruction(TranslationContext& context, const RdnaInstruction& instruction) {
@@ -1,5 +1,6 @@
#include "Translation/CompareInstructions.hpp"
#include "Translation/TranslationContext.hpp"
#include <array>
#include <stdexcept>
namespace ShaderRecompiler {
@@ -9,31 +10,64 @@ void TranslateCompareInstruction(IrBuilder& builder, const RdnaInstruction& inst
}
void TranslationContext::emitCompareResult(const RdnaInstruction& inst, IrU1 value, bool scalar, bool cmpx) {
throw std::runtime_error("TranslationContext::emitCompareResult not implemented");
if (scalar) {
ir.SetScc(value.Value());
return;
}
IrValue& masked = ir.LogicalAnd(ir.GetExec(), value.Value());
if (cmpx) {
const std::array<IrU32, 2> mask = ballotMask(IrU1(masked));
ir.SetExec(masked);
ir.SetExecLo(mask[0].Value());
ir.SetExecHi(mask[1].Value());
return;
}
writeMask(inst.destination, IrU1(masked));
}
void TranslationContext::emitCompareConstant(const RdnaInstruction& inst, bool value, bool scalar, bool cmpx) {
throw std::runtime_error("TranslationContext::emitCompareConstant not implemented");
emitCompareResult(inst, IrU1(ir.ConstantBool(value)), scalar, cmpx);
}
void TranslationContext::emitIntegerCompare(const RdnaInstruction& inst, IrOpcode opcode, IrType type, bool scalar, bool cmpx) {
throw std::runtime_error("TranslationContext::emitIntegerCompare not implemented");
IrValue* lhs = readOperand(sourceAt(inst, 0u), type);
IrValue* rhs = readOperand(sourceAt(inst, 1u), type);
emitCompareResult(inst, IrU1(ir.Emit(opcode, IrType::U1, {lhs, rhs})), scalar, cmpx);
}
void TranslationContext::emitInteger16Compare(const RdnaInstruction& inst, IrOpcode opcode, bool signedValue, bool cmpx) {
throw std::runtime_error("TranslationContext::emitInteger16Compare not implemented");
const IrU32 lhs = readU16AsU32(sourceAt(inst, 0u), signedValue);
const IrU32 rhs = readU16AsU32(sourceAt(inst, 1u), signedValue);
emitCompareResult(inst, IrU1(ir.Emit(opcode, IrType::U1, {&lhs.Value(), &rhs.Value()})), false, cmpx);
}
void TranslationContext::emitFloatCompare(const RdnaInstruction& inst, IrOpcode opcode, bool half, bool cmpx) {
throw std::runtime_error("TranslationContext::emitFloatCompare not implemented");
IrValue* lhs = nullptr;
IrValue* rhs = nullptr;
if (half) {
lhs = &readF16AsF32(sourceAt(inst, 0u)).Value();
rhs = &readF16AsF32(sourceAt(inst, 1u)).Value();
} else {
lhs = readOperand(sourceAt(inst, 0u), IrType::F32);
rhs = readOperand(sourceAt(inst, 1u), IrType::F32);
}
emitCompareResult(inst, IrU1(ir.Emit(opcode, IrType::U1, {lhs, rhs})), false, cmpx);
}
void TranslationContext::emitFloatOrderedCompare(const RdnaInstruction& inst, bool ordered) {
throw std::runtime_error("TranslationContext::emitFloatOrderedCompare not implemented");
IrValue* lhs = readOperand(sourceAt(inst, 0u), IrType::F32);
IrValue* rhs = readOperand(sourceAt(inst, 1u), IrType::F32);
IrValue& lhsNan = ir.Emit(IrOpcode::FPIsNan32, IrType::U1, {lhs});
IrValue& rhsNan = ir.Emit(IrOpcode::FPIsNan32, IrType::U1, {rhs});
IrValue& unordered = ir.LogicalOr(lhsNan, rhsNan);
IrValue& result = ordered ? ir.LogicalNot(unordered) : unordered;
emitCompareResult(inst, IrU1(result), false, false);
}
void TranslationContext::emitFloatClassCompare(const RdnaInstruction& inst, bool cmpx) {
throw std::runtime_error("TranslationContext::emitFloatClassCompare not implemented");
IrValue* value = readOperand(sourceAt(inst, 0u), IrType::F32);
IrValue* mask = readOperand(sourceAt(inst, 1u), IrType::U32);
emitCompareResult(inst, IrU1(ir.Emit(IrOpcode::FPCmpClass32, IrType::U1, {value, mask})), false, cmpx);
}
void TranslateCompareInstruction(TranslationContext& context, const RdnaInstruction& instruction) {