mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #74 from SamG-Coder/main
fix(shader): support byte permutation and fix compiler lifetime bugs Adds `V_PERM_B32` byte selection and fixes two existing bugs exposed while testing it: IR equality could merge buffer reads with different metadata, and concurrent shader compilation could crash during Windows thread-local destruction. - Include instruction flags when comparing IR values so selections between different buffer offsets survive constant folding. - Decode RDNA2 opcode `0x344` and translate all byte selectors through existing integer IR and the SPIR-V backend, including sign replication, zero/ones fill, and overlapping destinations. - Reuse the existing `HostThreadLocal` helper for six destructible compiler scratch objects. Add async worker/destructor checks to the existing lifetime test. Scratch storage remains reusable.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
#include <cstdlib>
|
||||
#include <array>
|
||||
#include <future>
|
||||
#include <thread>
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
@@ -21,4 +23,14 @@ int main() {
|
||||
if (DestroyedHostThreadLocals() != 128 + (i + 1) * 2) std::abort();
|
||||
}
|
||||
#endif
|
||||
for (unsigned i = 0; i < 16; ++i) {
|
||||
const auto before = DestroyedHostThreadLocals();
|
||||
std::array<std::future<void>, 4> workers;
|
||||
for (auto& worker : workers) {
|
||||
worker = std::async(std::launch::async, [] { TouchHostThreadLocal(); TouchHostThreadLocal(); });
|
||||
}
|
||||
for (auto& worker : workers) worker.get();
|
||||
if (DestroyedHostThreadLocals() != before + 8) std::abort();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -138,6 +138,7 @@ function(add_shader_recompiler target runtimeTarget)
|
||||
)
|
||||
|
||||
target_include_directories(${target} PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/core/libs
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/RdnaDecoder/include
|
||||
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/ControlFlow/include
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define CORE_SHADER_RECOMPILER_CACHEKEY_HPP
|
||||
|
||||
#include "Recompiler.hpp"
|
||||
#include "prx/libc/include/HostThreadLocal.hpp"
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
@@ -37,7 +38,8 @@ public:
|
||||
// key of a source memo whose owner fixes the code (a registered shader at an offset) and the
|
||||
// device itself, and which is bypassed while the probe is active.
|
||||
static std::uint64_t ContextHash(const RecompileRequest& request) {
|
||||
thread_local std::vector<std::uint64_t> key;
|
||||
struct ContextKeyStorage {};
|
||||
auto& key = HostThreadLocal<std::vector<std::uint64_t>, ContextKeyStorage>();
|
||||
key.clear();
|
||||
append(key, request.shader.stage);
|
||||
append(key, request.context.waveSize);
|
||||
|
||||
@@ -261,7 +261,7 @@ bool IrValue::operator==(const IrValue& other) const {
|
||||
if (this == &other) {
|
||||
return true;
|
||||
}
|
||||
if (opcode != other.opcode || type != other.type || hasImmediate != other.hasImmediate) {
|
||||
if (opcode != other.opcode || type != other.type || flags != other.flags || hasImmediate != other.hasImmediate) {
|
||||
return false;
|
||||
}
|
||||
if (hasImmediate && immediateBits != other.immediateBits) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "Optimization/SrtWalker/SrtDescriptorEvaluation.hpp"
|
||||
#include "Optimization/SrtWalker/SrtEvaluator.hpp"
|
||||
#include "prx/libc/include/HostThreadLocal.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
@@ -10,7 +11,10 @@ namespace ShaderRecompiler::Detail {
|
||||
|
||||
namespace {
|
||||
|
||||
thread_local std::string g_failureReason;
|
||||
std::string& failureReason() {
|
||||
struct FailureReasonStorage {};
|
||||
return HostThreadLocal<std::string, FailureReasonStorage>();
|
||||
}
|
||||
|
||||
std::string DescribeValue(const IrValue* value, std::uint32_t depth) {
|
||||
if (value == nullptr) return "null";
|
||||
@@ -27,7 +31,7 @@ std::string DescribeValue(const IrValue* value, std::uint32_t depth) {
|
||||
}
|
||||
|
||||
bool Fail(std::string reason) {
|
||||
g_failureReason = std::move(reason);
|
||||
failureReason() = std::move(reason);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -41,7 +45,7 @@ const DescriptorSource* Source(const IrResourcePlan& program, std::uint32_t sour
|
||||
}
|
||||
|
||||
bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const std::uint32_t> sources, const SrtRuntime& runtime, std::vector<DescriptorValue>& results, std::vector<std::uint32_t>& flat, bool evaluateFlat, std::span<const std::uint8_t> cleanFlatSlots, std::vector<std::uint8_t>& activeSources) {
|
||||
g_failureReason.clear();
|
||||
failureReason().clear();
|
||||
static const bool debug = std::getenv("APS5_SRT_DEBUG") != nullptr;
|
||||
if (debug) {
|
||||
for (std::size_t slot = 0; slot < program.srtReads.size(); ++slot) std::fprintf(stderr, "[srt] slot %zu = %s" "\n", slot, DescribeValue(program.srtReads[slot].value, 6).c_str());
|
||||
@@ -143,7 +147,7 @@ bool EvaluateRuntimeSourcesImpl(const IrResourcePlan& program, std::span<const s
|
||||
}
|
||||
|
||||
const std::string& RuntimeSourceFailureReason() {
|
||||
return g_failureReason;
|
||||
return failureReason();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -284,6 +284,7 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
VAdd3U32,
|
||||
VLshlAddU32,
|
||||
VAddLshlU32,
|
||||
VPermB32,
|
||||
VXadU32,
|
||||
VLshlOrB32,
|
||||
VAndOrB32,
|
||||
|
||||
@@ -257,6 +257,7 @@ bool IsVectorAluOpcode(RdnaOpcode opcode) {
|
||||
case RdnaOpcode::VAdd3U32:
|
||||
case RdnaOpcode::VLshlAddU32:
|
||||
case RdnaOpcode::VAddLshlU32:
|
||||
case RdnaOpcode::VPermB32:
|
||||
case RdnaOpcode::VXadU32:
|
||||
case RdnaOpcode::VLshlOrB32:
|
||||
case RdnaOpcode::VAndOrB32:
|
||||
|
||||
@@ -355,6 +355,7 @@ constexpr VectorOpcodeInfo vop3Opcodes[] = {
|
||||
{0x311u, RdnaOpcode::VPackB32F16},
|
||||
{0x314u, RdnaOpcode::VLshlrevB16},
|
||||
{0x319u, RdnaOpcode::VSubrevI32},
|
||||
{0x344u, RdnaOpcode::VPermB32},
|
||||
{0x345u, RdnaOpcode::VXadU32},
|
||||
{0x346u, RdnaOpcode::VLshlAddU32},
|
||||
{0x347u, RdnaOpcode::VAddLshlU32},
|
||||
|
||||
@@ -223,7 +223,8 @@ std::shared_ptr<SourceEntry> getSource(const RecompileRequest& request) {
|
||||
static std::shared_mutex mutex;
|
||||
// Entries whose code hashes alike share a bucket; the code comparison picks the right one.
|
||||
static std::unordered_map<std::vector<std::uint64_t>, std::vector<std::shared_ptr<SourceEntry>>, SourceKeyHash> sources;
|
||||
thread_local std::vector<std::uint64_t> key;
|
||||
struct SourceKeyStorage {};
|
||||
auto& key = HostThreadLocal<std::vector<std::uint64_t>, SourceKeyStorage>();
|
||||
RecompileCacheKey::Build(request, key);
|
||||
const auto find = [&]() -> std::shared_ptr<SourceEntry> {
|
||||
const auto found = sources.find(key);
|
||||
|
||||
@@ -186,6 +186,7 @@ private:
|
||||
bool vAlignbyteB32(const RdnaInstruction& inst);
|
||||
bool vLshlAddU32(const RdnaInstruction& inst);
|
||||
bool vAddLshlU32(const RdnaInstruction& inst);
|
||||
bool vPermB32(const RdnaInstruction& inst);
|
||||
bool vXadU32(const RdnaInstruction& inst);
|
||||
bool vLshlOrB32(const RdnaInstruction& inst);
|
||||
bool vCndmaskB32(const RdnaInstruction& inst);
|
||||
|
||||
@@ -532,6 +532,28 @@ bool TranslationContext::vAddLshlU32(const RdnaInstruction& inst) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::vPermB32(const RdnaInstruction& inst) {
|
||||
const IrU32 high = readU32(sourceAt(inst, 0u));
|
||||
const IrU32 low = readU32(sourceAt(inst, 1u));
|
||||
const IrU32 selectors = readU32(sourceAt(inst, 2u));
|
||||
IrValue* result = &ir.Constant(0u);
|
||||
for (std::uint32_t byte = 0u; byte < 4u; ++byte) {
|
||||
IrValue& selector = ir.BitwiseAnd(ir.ShiftRightLogical(selectors.Value(), ir.Constant(byte * 8u)), ir.Constant(255u));
|
||||
IrValue& signIndex = ir.IAdd(ir.IMul(ir.BitwiseAnd(selector, ir.Constant(3u)), ir.Constant(2u)), ir.Constant(1u));
|
||||
IrValue& index = ir.Select(ir.ULessThan(selector, ir.Constant(8u)), selector, signIndex);
|
||||
IrValue& word = ir.Select(ir.ULessThan(index, ir.Constant(4u)), low.Value(), high.Value());
|
||||
IrValue& shift = ir.IMul(ir.BitwiseAnd(index, ir.Constant(3u)), ir.Constant(8u));
|
||||
IrValue& value = ir.BitwiseAnd(ir.ShiftRightLogical(word, shift), ir.Constant(255u));
|
||||
IrValue& sign = ir.Select(ir.UGreaterThan(value, ir.Constant(127u)), ir.Constant(255u), ir.Constant(0u));
|
||||
IrValue& selected = ir.Select(ir.ULessThan(selector, ir.Constant(8u)), value, sign);
|
||||
IrValue& fill = ir.Select(ir.IEqual(selector, ir.Constant(12u)), ir.Constant(0u), ir.Constant(255u));
|
||||
IrValue& output = ir.Select(ir.ULessThan(selector, ir.Constant(12u)), selected, fill);
|
||||
result = &ir.BitwiseOr(*result, ir.ShiftLeftLogical(output, ir.Constant(byte * 8u)));
|
||||
}
|
||||
writeOperand(inst.destination, result);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TranslationContext::vXadU32(const RdnaInstruction& inst) {
|
||||
const IrU32 lhs = readU32(sourceAt(inst, 0u));
|
||||
const IrU32 rhs = readU32(sourceAt(inst, 1u));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "Translation/ShaderInputInfoBuilder.hpp"
|
||||
#include "prx/libc/include/HostThreadLocal.hpp"
|
||||
#include "IntermediateRepresentation/IrMetadata.hpp"
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
@@ -7,11 +8,6 @@
|
||||
namespace ShaderRecompiler {
|
||||
|
||||
namespace {
|
||||
|
||||
thread_local ShaderPixelInputInfo pixelStorage;
|
||||
thread_local ShaderComputeInputInfo computeStorage;
|
||||
thread_local ShaderVertexInputInfo vertexStorage;
|
||||
|
||||
IrShaderStage _toIrShaderStage(ShaderStageKind stage) {
|
||||
switch (stage) {
|
||||
case ShaderStageKind::Vertex: return IrShaderStage::Vertex;
|
||||
@@ -74,6 +70,8 @@ ShaderStageInputInfo BuildShaderStageInputInfo(ShaderStageKind stage, const Gues
|
||||
throw std::runtime_error("ShaderInputInfoBuilder: GuestContext.compute is not set");
|
||||
}
|
||||
const auto& compute = *context.compute;
|
||||
struct ComputeStorage {};
|
||||
auto& computeStorage = HostThreadLocal<ShaderComputeInputInfo, ComputeStorage>();
|
||||
computeStorage = ShaderComputeInputInfo{};
|
||||
computeStorage.threadsNum[0] = compute.numThreads[0];
|
||||
computeStorage.threadsNum[1] = compute.numThreads[1];
|
||||
@@ -97,6 +95,8 @@ ShaderStageInputInfo BuildShaderStageInputInfo(ShaderStageKind stage, const Gues
|
||||
throw std::runtime_error("ShaderInputInfoBuilder: GuestContext.pixel is not set");
|
||||
}
|
||||
const auto& pixel = *context.pixel;
|
||||
struct PixelStorage {};
|
||||
auto& pixelStorage = HostThreadLocal<ShaderPixelInputInfo, PixelStorage>();
|
||||
pixelStorage = ShaderPixelInputInfo{};
|
||||
for (std::uint32_t i = 0; i < 32; ++i) {
|
||||
pixelStorage.interpolatorSettings[i] = pixel.interpolatorSettings[i];
|
||||
@@ -136,6 +136,8 @@ ShaderStageInputInfo BuildShaderStageInputInfo(ShaderStageKind stage, const Gues
|
||||
}
|
||||
const auto& vertex = *context.vertex;
|
||||
if (vertex.resourcesNum > vertex.resources.size()) throw std::runtime_error("ShaderInputInfoBuilder: invalid vertex resource count");
|
||||
struct VertexStorage {};
|
||||
auto& vertexStorage = HostThreadLocal<ShaderVertexInputInfo, VertexStorage>();
|
||||
vertexStorage = ShaderVertexInputInfo{};
|
||||
vertexStorage.logicalStage = _toIrShaderStage(stage);
|
||||
vertexStorage.fetchEmbedded = vertex.fetchEmbedded;
|
||||
|
||||
@@ -673,6 +673,8 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) {
|
||||
return vLshlAddU32(inst);
|
||||
case RdnaOpcode::VAddLshlU32:
|
||||
return vAddLshlU32(inst);
|
||||
case RdnaOpcode::VPermB32:
|
||||
return vPermB32(inst);
|
||||
case RdnaOpcode::VXadU32:
|
||||
return vXadU32(inst);
|
||||
case RdnaOpcode::VLshlOrB32:
|
||||
|
||||
Reference in New Issue
Block a user