diff --git a/core/libs/tests/HostThreadLocal.cpp b/core/libs/tests/HostThreadLocal.cpp index 4fedf466a..0a3639bd2 100644 --- a/core/libs/tests/HostThreadLocal.cpp +++ b/core/libs/tests/HostThreadLocal.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #ifdef _WIN32 #include @@ -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, 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(); + } + } diff --git a/core/shader/recompiler/CMakeLists.txt b/core/shader/recompiler/CMakeLists.txt index e9f394071..33751b2a5 100644 --- a/core/shader/recompiler/CMakeLists.txt +++ b/core/shader/recompiler/CMakeLists.txt @@ -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 diff --git a/core/shader/recompiler/CacheKey.hpp b/core/shader/recompiler/CacheKey.hpp index 1e6fc2025..24e4b66b4 100644 --- a/core/shader/recompiler/CacheKey.hpp +++ b/core/shader/recompiler/CacheKey.hpp @@ -2,6 +2,7 @@ #define CORE_SHADER_RECOMPILER_CACHEKEY_HPP #include "Recompiler.hpp" +#include "prx/libc/include/HostThreadLocal.hpp" #include #include #include @@ -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 key; + struct ContextKeyStorage {}; + auto& key = HostThreadLocal, ContextKeyStorage>(); key.clear(); append(key, request.shader.stage); append(key, request.context.waveSize); diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrValue.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrValue.cpp index c9366915a..3a0cabd76 100644 --- a/core/shader/recompiler/IntermediateRepresentation/src/IrValue.cpp +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrValue.cpp @@ -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) { diff --git a/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp b/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp index ee1e7c6d6..7ea4c4a78 100644 --- a/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp +++ b/core/shader/recompiler/Optimization/src/SrtWalker/SrtDescriptorEvaluation.cpp @@ -1,5 +1,6 @@ #include "Optimization/SrtWalker/SrtDescriptorEvaluation.hpp" #include "Optimization/SrtWalker/SrtEvaluator.hpp" +#include "prx/libc/include/HostThreadLocal.hpp" #include #include @@ -10,7 +11,10 @@ namespace ShaderRecompiler::Detail { namespace { -thread_local std::string g_failureReason; +std::string& failureReason() { + struct FailureReasonStorage {}; + return HostThreadLocal(); +} 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 sources, const SrtRuntime& runtime, std::vector& results, std::vector& flat, bool evaluateFlat, std::span cleanFlatSlots, std::vector& 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 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>, SourceKeyHash> sources; - thread_local std::vector key; + struct SourceKeyStorage {}; + auto& key = HostThreadLocal, SourceKeyStorage>(); RecompileCacheKey::Build(request, key); const auto find = [&]() -> std::shared_ptr { const auto found = sources.find(key); diff --git a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp index 2aa9f3c36..3a5f8d709 100644 --- a/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp +++ b/core/shader/recompiler/Translation/include/Translation/TranslationContext.hpp @@ -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); diff --git a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp index 7a3dffd48..310dae951 100644 --- a/core/shader/recompiler/Translation/src/IntegerInstructions.cpp +++ b/core/shader/recompiler/Translation/src/IntegerInstructions.cpp @@ -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)); diff --git a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp index 92fe17ef0..ecd870cdc 100644 --- a/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp +++ b/core/shader/recompiler/Translation/src/ShaderInputInfoBuilder.cpp @@ -1,4 +1,5 @@ #include "Translation/ShaderInputInfoBuilder.hpp" +#include "prx/libc/include/HostThreadLocal.hpp" #include "IntermediateRepresentation/IrMetadata.hpp" #include #include @@ -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(); 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(); 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(); vertexStorage = ShaderVertexInputInfo{}; vertexStorage.logicalStage = _toIrShaderStage(stage); vertexStorage.fetchEmbedded = vertex.fetchEmbedded; diff --git a/core/shader/recompiler/Translation/src/VectorInstructions.cpp b/core/shader/recompiler/Translation/src/VectorInstructions.cpp index 61eba45b1..ff327c922 100644 --- a/core/shader/recompiler/Translation/src/VectorInstructions.cpp +++ b/core/shader/recompiler/Translation/src/VectorInstructions.cpp @@ -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: