From 7d90b1dceccb87e7b7845eb3e9b0ed2df6077131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A0=20Franch?= Date: Fri, 2 Oct 2026 07:58:41 +0200 Subject: [PATCH] feat(shader): implement s_memtime and s_memrealtime Both SMEM opcodes read a 64-bit counter into an SGPR pair. They now lower to OpReadClockKHR: s_memtime with subgroup scope (the shader clock) and s_memrealtime with device scope (the constant real-time clock). The device enables VK_KHR_shader_clock when it has both clocks and the backend refuses the opcodes with a clear reason otherwise. Co-Authored-By: Claude --- .../Execution/src/VulkanDevice.cpp | 16 +++ .../tests/execution/ShaderClock.cpp | 111 ++++++++++++++++++ .../IntermediateRepresentation/IrOpcode.hpp | 2 + .../src/IrOpcode.cpp | 2 + .../include/RdnaDecoder/RdnaOpcode.hpp | 2 + .../RdnaDecoder/src/RdnaMemoryOpDecoder.cpp | 5 + .../SpirvBackend/SpirvEmitterInstructions.hpp | 2 + .../SpirvBackend/src/SpirvFlowEmitter.cpp | 2 + .../SpirvBackend/src/SpirvModuleEmitter.cpp | 24 ++++ .../MemoryInstructionDispatch.cpp | 7 +- 10 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 core/libs/prx/libSceAgcDriver/tests/execution/ShaderClock.cpp diff --git a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp index 2b859eb91..9770ec493 100644 --- a/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp +++ b/core/libs/prx/libSceAgcDriver/Execution/src/VulkanDevice.cpp @@ -183,6 +183,7 @@ struct VulkanDevice::State { bool tessellationShader = false; bool meshShader = false; bool fragmentShaderBarycentric = false; + bool shaderClock = false; // VK_EXT_descriptor_indexing with non-uniform image array indexing (bindless image tables in // graphics stages, and compute workgroups wider than a wave). bool descriptorIndexing = false; @@ -692,6 +693,12 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u state->InstanceFunction("vkGetPhysicalDeviceFeatures2")(selected, &features); state->fragmentShaderBarycentric = barycentricFeatures.fragmentShaderBarycentric == VK_TRUE; } + VkPhysicalDeviceShaderClockFeaturesKHR clockFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_CLOCK_FEATURES_KHR}; + if (hasExtension(VK_KHR_SHADER_CLOCK_EXTENSION_NAME)) { + VkPhysicalDeviceFeatures2 features{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2, &clockFeatures}; + state->InstanceFunction("vkGetPhysicalDeviceFeatures2")(selected, &features); + state->shaderClock = clockFeatures.shaderSubgroupClock == VK_TRUE && clockFeatures.shaderDeviceClock == VK_TRUE; + } std::vector deviceExtensions; if (window != nullptr) deviceExtensions.assign(presentationExtensions.begin(), presentationExtensions.end()); if (state->fragmentShaderBarycentric) { @@ -699,6 +706,11 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u state->capabilities.push_back(spv::CapabilityFragmentBarycentricKHR); state->spirvExtensions.push_back("SPV_KHR_fragment_shader_barycentric"); } + if (state->shaderClock) { + deviceExtensions.push_back(VK_KHR_SHADER_CLOCK_EXTENSION_NAME); + state->capabilities.push_back(spv::CapabilityShaderClockKHR); + state->spirvExtensions.push_back("SPV_KHR_shader_clock"); + } deviceExtensions.push_back(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME); state->capabilities.push_back(spv::CapabilitySignedZeroInfNanPreserve); state->spirvExtensions.push_back("SPV_KHR_float_controls"); @@ -857,6 +869,10 @@ VulkanDevice::VulkanDevice(const PresentationWindow* window) : state(std::make_u barycentricFeatures.pNext = byteFeatures.pNext; byteFeatures.pNext = &barycentricFeatures; } + if (state->shaderClock) { + clockFeatures.pNext = byteFeatures.pNext; + byteFeatures.pNext = &clockFeatures; + } VkPhysicalDeviceImageRobustnessFeaturesEXT imageRobustnessFeatures{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_ROBUSTNESS_FEATURES_EXT, nullptr, VK_TRUE}; if (imageRobustness) { imageRobustnessFeatures.pNext = byteFeatures.pNext; diff --git a/core/libs/prx/libSceAgcDriver/tests/execution/ShaderClock.cpp b/core/libs/prx/libSceAgcDriver/tests/execution/ShaderClock.cpp new file mode 100644 index 000000000..73e876521 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/execution/ShaderClock.cpp @@ -0,0 +1,111 @@ +#include "prx/libSceAgcDriver/Execution/include/VulkanDevice.hpp" +#include "prx/libSceAgcDriver/Graphics/include/Draw.hpp" +#include "Recompiler.hpp" +#include "VulkanTestDevice.hpp" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +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 Input{}; +alignas(256) std::array Output{}; + +alignas(256) constexpr std::array Code{ + 0x34020084, 0x34060086, 0xe0301000, 0x80000401, 0xf4900200, 0x00000000, 0xf4940280, 0x00000000, + 0xbf8c0070, 0x7e0a0280, 0xbe9003ff, 0x000007d0, 0xd5430005, 0x04110705, 0x80908110, 0xbf078010, + 0xbf85fffb, 0xf4900300, 0x00000000, 0xf4940380, 0x00000000, 0xbf8cc07f, 0x7e140208, 0x7e160209, + 0x7e18020a, 0x7e1a020b, 0x7e1c020c, 0x7e1e020d, 0x7e20020e, 0x7e22020f, 0xe0701000, 0x80010a03, + 0xe0701004, 0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xe0701010, 0x80010e03, + 0xe0701014, 0x80010f03, 0xe0701018, 0x80011003, 0xe070101c, 0x80011103, 0xe0701020, 0x80010503, + 0xbf810000, +}; + +void Fill(std::uint32_t tid, std::uint32_t* words) { + words[0] = tid + 1u; +} + +std::uint64_t Pair(const std::uint32_t* words) { + return static_cast(words[0]) | (static_cast(words[1]) << 32u); +} + +std::array BufferDescriptor(const void* data, std::uint32_t bytes) { + const auto address = reinterpret_cast(data); + return {static_cast(address), static_cast((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("shader clock: lane ") + std::to_string(tid) + " " + name + " is " + Hex(actual) + ", expected " + Hex(expected)); +} + +void Run(AgcDriver::VulkanDevice& device) { + for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]); + Output.fill(0xdeadbeefu); + std::vector userData(8, 0u); + const auto input = BufferDescriptor(Input.data(), static_cast(Input.size() * 4u)); + const auto output = BufferDescriptor(Output.data(), static_cast(Output.size() * 4u)); + std::copy(input.begin(), input.end(), userData.begin()); + std::copy(output.begin(), output.end(), userData.begin() + 4); + const std::span code(Code); + const std::array memory{{{reinterpret_cast(code.data()), std::as_bytes(code)}}}; + const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, 0u, {false, false, false}, false, 1}; + ShaderRecompiler::RecompileRequest request{ + {ShaderStage::Compute, reinterpret_cast(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(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]; + const std::uint32_t* first = &Output[(tid & ~7u) * Results]; + for (std::uint32_t index = 0; index < 8; ++index) Expect(tid, out[index], first[index], "clock read uniform over the wave"); + Require(Pair(out + 4) >= Pair(out), "memtime: lane " + std::to_string(tid) + " went back from " + std::to_string(Pair(out)) + " to " + std::to_string(Pair(out + 4))); + Require(Pair(out + 6) >= Pair(out + 2), "memrealtime: lane " + std::to_string(tid) + " went back from " + std::to_string(Pair(out + 2)) + " to " + std::to_string(Pair(out + 6))); + Require(Pair(out + 4) != 0u && Pair(out + 6) != 0u, "clocks read zero"); + std::uint32_t sum = 0u; + for (std::uint32_t step = 0; step < 2000u; ++step) sum = (sum & 0xffffffu) * 3u + in[0]; + Expect(tid, out[8], sum, "loop result"); + } +} + +} + +int main() { + try { + const auto device = OpenVulkanTestDevice(); + if (!device) return VulkanTestSkipped; + Run(*device); + Check(); + std::puts("shader clock tests passed"); + return 0; + } catch (const std::exception& error) { + std::cerr << error.what() << '\n'; + return 1; + } +} diff --git a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp index 9aa0fa561..fc4309a9e 100644 --- a/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp +++ b/core/shader/recompiler/IntermediateRepresentation/include/IntermediateRepresentation/IrOpcode.hpp @@ -75,6 +75,8 @@ enum class IrOpcode : std::uint16_t { ReferenceU32, GetUserData, GetShaderBase, + ShaderClock, + RealtimeClock, MeshDrawParameter, MeshAllocate, TessellationBase, diff --git a/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp b/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp index 2c44ed9cb..09c0b84f7 100644 --- a/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp +++ b/core/shader/recompiler/IntermediateRepresentation/src/IrOpcode.cpp @@ -116,6 +116,8 @@ constexpr std::array(IrOpcode::Count)> Meta makeMeta("ReferenceU32", Void, U32), makeMeta("GetUserData", U32, ScalarReg), makeMeta("GetShaderBase", U64), + makeMeta("ShaderClock", U64), + makeMeta("RealtimeClock", U64), makeMeta("MeshDrawParameter", U32, U32), makeMeta("MeshAllocate", Void, U32), makeMeta("TessellationBase", U32, U32), diff --git a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp index 2b7acb5ce..703f14949 100644 --- a/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp +++ b/core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaOpcode.hpp @@ -602,6 +602,8 @@ enum class RdnaOpcode : std::uint16_t { SGl1Inv, SDcacheInv, SDcacheWb, + SMemtime, + SMemrealtime, BufferGl0Inv, BufferGl1Inv, Exp, diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp index 068c2947e..e2bc5d198 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaMemoryOpDecoder.cpp @@ -400,6 +400,11 @@ RdnaInstruction DecodeRdnaSmem(std::uint32_t programCounter, std::span(spv::CapabilityShaderClockKHR)) != state.supportedCapabilities.end(); + const bool extension = std::find(state.supportedExtensions.begin(), state.supportedExtensions.end(), "SPV_KHR_shader_clock") != state.supportedExtensions.end(); + if (!capability || !extension) ctx.Fail(inst, "reads the shader clock, which needs the device's VK_KHR_shader_clock"); + state.module.EmitCapability(spv::CapabilityShaderClockKHR); + state.module.EmitExtension("SPV_KHR_shader_clock"); + const auto result = state.module.AllocateId(); + state.module.AddFunction(spv::OpReadClockKHR, TypeU64(state), result, ConstantU32(state, static_cast(scope))); + return result; +} + +} + +std::uint32_t EmitShaderClock(SpirvValueEmitContext& ctx, const IrValue& inst) { + return EmitReadClock(ctx, inst, spv::ScopeSubgroup); +} + +std::uint32_t EmitRealtimeClock(SpirvValueEmitContext& ctx, const IrValue& inst) { + return EmitReadClock(ctx, inst, spv::ScopeDevice); +} + void EmitTessellationBase(SpirvValueEmitContext& ctx, const IrValue& inst) { ctx.Fail(inst, "must be lowered before SPIR-V emission"); } diff --git a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp index f7f7c3af6..29a87cee2 100644 --- a/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp +++ b/core/shader/recompiler/Translation/src/MemoryInstructions/MemoryInstructionDispatch.cpp @@ -17,7 +17,12 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) { case RdnaOpcode::BufferGl1Inv: emitControlNop(); return true; - + case RdnaOpcode::SMemtime: + writeU32Pair(inst.destination, extractU64(IrU64(ir.Emit(IrOpcode::ShaderClock, IrType::U64, {})))); + return true; + case RdnaOpcode::SMemrealtime: + writeU32Pair(inst.destination, extractU64(IrU64(ir.Emit(IrOpcode::RealtimeClock, IrType::U64, {})))); + return true; case RdnaOpcode::SLoadDword: case RdnaOpcode::SLoadDwordx2: case RdnaOpcode::SLoadDwordx4: