mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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<PFN_vkGetPhysicalDeviceFeatures2>("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<PFN_vkGetPhysicalDeviceFeatures2>("vkGetPhysicalDeviceFeatures2")(selected, &features);
|
||||
state->shaderClock = clockFeatures.shaderSubgroupClock == VK_TRUE && clockFeatures.shaderDeviceClock == VK_TRUE;
|
||||
}
|
||||
std::vector<const char*> 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;
|
||||
|
||||
@@ -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 <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, 49> 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<std::uint64_t>(words[0]) | (static_cast<std::uint64_t>(words[1]) << 32u);
|
||||
}
|
||||
|
||||
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("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<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 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;
|
||||
const auto result = ShaderRecompiler::Recompile(request);
|
||||
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];
|
||||
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;
|
||||
}
|
||||
}
|
||||
+2
@@ -75,6 +75,8 @@ enum class IrOpcode : std::uint16_t {
|
||||
ReferenceU32,
|
||||
GetUserData,
|
||||
GetShaderBase,
|
||||
ShaderClock,
|
||||
RealtimeClock,
|
||||
MeshDrawParameter,
|
||||
MeshAllocate,
|
||||
TessellationBase,
|
||||
|
||||
@@ -116,6 +116,8 @@ constexpr std::array<OpcodeMeta, static_cast<std::size_t>(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),
|
||||
|
||||
@@ -602,6 +602,8 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
SGl1Inv,
|
||||
SDcacheInv,
|
||||
SDcacheWb,
|
||||
SMemtime,
|
||||
SMemrealtime,
|
||||
BufferGl0Inv,
|
||||
BufferGl1Inv,
|
||||
Exp,
|
||||
|
||||
@@ -400,6 +400,11 @@ RdnaInstruction DecodeRdnaSmem(std::uint32_t programCounter, std::span<const std
|
||||
if (const auto cacheOp = cacheControlOpcode(RdnaInstructionFamily::SMEM, opcode); cacheOp != RdnaOpcode::Invalid) {
|
||||
return cacheControlInstruction(RdnaInstructionFamily::SMEM, cacheOp, opcode, programCounter, code, wordIndex);
|
||||
}
|
||||
if (opcode == 0x24u || opcode == 0x25u) {
|
||||
auto instruction = cacheControlInstruction(RdnaInstructionFamily::SMEM, opcode == 0x24u ? RdnaOpcode::SMemtime : RdnaOpcode::SMemrealtime, opcode, programCounter, code, wordIndex);
|
||||
instruction.destination = scalarDestination(sdst);
|
||||
return instruction;
|
||||
}
|
||||
const auto& info = lookupOpcode(smemOpcodes, opcode, "SMEM opcode is not supported");
|
||||
|
||||
RdnaInstruction instruction{};
|
||||
|
||||
@@ -80,6 +80,8 @@ std::uint32_t EmitGetAttribute(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitGetInterpolationParameter(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
void EmitSetAttribute(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitGetShaderBase(SpirvValueEmitContext& ctx);
|
||||
std::uint32_t EmitShaderClock(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitRealtimeClock(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
void EmitLoadMemory(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
void EmitStoreMemory(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitAtomic32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
|
||||
@@ -249,6 +249,8 @@ void EmitDirectInstruction(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
case IrOpcode::ReferenceU32: return Invoke(EmitReferenceU32, ctx, inst);
|
||||
case IrOpcode::GetUserData: return Invoke(EmitGetUserData, ctx, inst);
|
||||
case IrOpcode::GetShaderBase: return Invoke(EmitGetShaderBase, ctx, inst);
|
||||
case IrOpcode::ShaderClock: return Invoke(EmitShaderClock, ctx, inst);
|
||||
case IrOpcode::RealtimeClock: return Invoke(EmitRealtimeClock, ctx, inst);
|
||||
case IrOpcode::MeshDrawParameter: return Invoke(EmitMeshDrawParameter, ctx, inst);
|
||||
case IrOpcode::MeshAllocate: return Invoke(EmitMeshAllocate, ctx, inst);
|
||||
case IrOpcode::TessellationBase: return Invoke(EmitTessellationBase, ctx, inst);
|
||||
|
||||
@@ -1095,6 +1095,30 @@ std::uint32_t EmitGetShaderBase(SpirvValueEmitContext& ctx) {
|
||||
return ConstantU64(ctx.state, 0u);
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
std::uint32_t EmitReadClock(SpirvValueEmitContext& ctx, const IrValue& inst, spv::Scope scope) {
|
||||
auto& state = ctx.state;
|
||||
const bool capability = std::find(state.supportedCapabilities.begin(), state.supportedCapabilities.end(), static_cast<std::uint32_t>(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<std::uint32_t>(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");
|
||||
}
|
||||
|
||||
+6
-1
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user