Merge pull request #447 from adriadam10/feat/vop3-f64-rest

feat(shader): implement v_rcp/rsq/sqrt_f64 and v_trig_preop_f64
This commit is contained in:
Dean
2026-10-05 15:48:23 +13:00
committed by GitHub
10 changed files with 319 additions and 2 deletions
@@ -0,0 +1,190 @@
#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, 27> Code{
0x34020084, 0x34060086, 0xe0381000, 0x80000401, 0xbf8c3f70, 0x7e145f04, 0x7e186304, 0x7e1c6904,
0xd5740010, 0x02020d04, 0xd5740012, 0x02010104, 0xd5740114, 0x2a020d04, 0xd5af8016, 0x02010104,
0xd5b40118, 0x22010104, 0xe0781000, 0x80010a03, 0xe0781010, 0x80010e03, 0xe0781020, 0x80011203,
0xe0781030, 0x80011603, 0xbf810000,
};
constexpr std::uint32_t Rows[32][4] = {
{0x00000001u, 0x7ff00000u, 0xcad921b4u, 0x00000000u},
{0x00000000u, 0x7ff40000u, 0x0000001fu, 0x00000000u},
{0x00000000u, 0x00000000u, 0x00000025u, 0x80000000u},
{0xe0000000u, 0x47efffffu, 0x0000001eu, 0x00000000u},
{0x00000000u, 0x80000000u, 0x0000001bu, 0x00000000u},
{0x00000000u, 0xbff00000u, 0x0000001au, 0x00000000u},
{0x00000000u, 0x7ff00000u, 0x01c32149u, 0x00000000u},
{0x00000000u, 0xfff00000u, 0x00000012u, 0x00000000u},
{0x00000001u, 0xfff80000u, 0x00000012u, 0x00000000u},
{0x00000000u, 0x00000000u, 0x3ceb3ffdu, 0x00000000u},
{0x00000000u, 0x00000000u, 0x8b529b4au, 0x3ff00000u},
{0x00000000u, 0x00000000u, 0x00000008u, 0xbff00000u},
{0x00000000u, 0x00000000u, 0x5eb561a4u, 0x3fe00000u},
{0x00000000u, 0x00000000u, 0x00000026u, 0xbfe00000u},
{0x00000000u, 0x00000000u, 0x795b929eu, 0x3ff80000u},
{0x00000000u, 0x00000000u, 0x00000028u, 0x40040000u},
{0x00000000u, 0x00000000u, 0x94b2b8fdu, 0xc0040000u},
{0x00000000u, 0x00000000u, 0x00000004u, 0x00000000u},
{0x00000000u, 0x00000000u, 0x9b08923du, 0x800fffffu},
{0x00000000u, 0x00000000u, 0x00000000u, 0x00100000u},
{0x00000000u, 0x00000000u, 0xe8a8529fu, 0x7fefffffu},
{0x00000000u, 0x00000000u, 0x0000001eu, 0xffefffffu},
{0x00000000u, 0x00000000u, 0x42650644u, 0x7ff00000u},
{0x00000000u, 0x00000000u, 0x00000023u, 0xfff00000u},
{0x00000000u, 0x00000000u, 0x3bfd1d33u, 0x7ff80000u},
{0x00000000u, 0x00000000u, 0x0000000cu, 0xfff80000u},
{0x00000000u, 0x00000000u, 0xfee29476u, 0x7ff00000u},
{0x00000000u, 0x00000000u, 0x0000001eu, 0x7ff40000u},
{0x00000000u, 0x00000000u, 0x8a7d43b5u, 0x41e00000u},
{0x00000000u, 0x00000000u, 0x00000023u, 0xc1e00000u},
{0x00000000u, 0x00000000u, 0x79f248b0u, 0xc1e00000u},
{0x00000000u, 0x00000000u, 0x00000019u, 0x41dfffffu}
};
constexpr std::uint32_t Expected[32][16] = {
{0x00000001u, 0x7ff80000u, 0x00000001u, 0x7ff80000u, 0x00000001u, 0x7ff80000u, 0x00000000u, 0x00000000u, 0xf5f2f8bdu, 0x0b43dd63u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0xfff80000u},
{0x00000000u, 0x7ffc0000u, 0x00000000u, 0x7ffc0000u, 0x00000000u, 0x7ffc0000u, 0x00000000u, 0x00000000u, 0xf5f2f8bdu, 0x0b43dd63u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xfffc0000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x2126e970u, 0x2f42371du, 0x6dc9c882u, 0x3fe45f30u, 0x2126e970u, 0x2f42371du, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x1af80000u, 0x37f00000u, 0x039e0000u, 0x3bf00000u, 0xed080000u, 0x43efffffu, 0x00000000u, 0x00000000u, 0xfa9a6ee0u, 0x3b43abe8u, 0x00000000u, 0x00000000u, 0x1af80000u, 0x37f00000u, 0x00000000u, 0xfff80000u},
{0x00000000u, 0xfff00000u, 0x00000000u, 0xfff00000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0xbff00000u, 0x00000000u, 0xfff80000u, 0x00000000u, 0xfff80000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xfff80000u},
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0xf5f2f8bdu, 0x0b43dd63u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xfff80000u},
{0x00000000u, 0x80000000u, 0x00000000u, 0xfff80000u, 0x00000000u, 0xfff80000u, 0x00000000u, 0x00000000u, 0xf5f2f8bdu, 0x0b43dd63u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0xfff80000u},
{0x00000001u, 0xfff80000u, 0x00000001u, 0xfff80000u, 0x00000001u, 0xfff80000u, 0x00000000u, 0x00000000u, 0xf5f2f8bdu, 0x0b43dd63u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0xfff80000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x2fbf209cu, 0x1ec8135au, 0x6dc9c882u, 0x3fe45f30u, 0x2fbf209cu, 0x1ec8135au, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x74411af8u, 0x2544d39fu, 0x6dc9c882u, 0x3fe45f30u, 0x74411af8u, 0x2544d39fu, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0xde2b0db8u, 0x328d5ef5u, 0x6dc9c882u, 0x3fe45f30u, 0xde2b0db8u, 0x328d5ef5u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0xa8274600u, 0x2b9924bbu, 0x6dc9c882u, 0x3fe45f30u, 0xa8274600u, 0x2b9924bbu, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x74411af8u, 0x2544d39fu, 0x6dc9c882u, 0x3fe45f30u, 0x74411af8u, 0x2544d39fu, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0xde2b0db8u, 0x328d5ef5u, 0x6dc9c882u, 0x3fe45f30u, 0xde2b0db8u, 0x328d5ef5u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x6dc9c882u, 0x3fe45f30u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0xde2b0db8u, 0x328d5ef5u, 0x6dc9c882u, 0x3fe45f30u, 0xde2b0db8u, 0x328d5ef5u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x041fe516u, 0x35f3c439u, 0x6dc9c882u, 0x3fe45f30u, 0x041fe516u, 0x35f3c439u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0xd0739f78u, 0x00ff17b3u, 0x6dc9c882u, 0x3fe45f30u, 0xd0739f78u, 0x00ff17b3u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x7e2ef7e4u, 0x18127211u, 0x6dc9c882u, 0x3fe45f30u, 0x7e2ef7e4u, 0x18127211u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x041fe516u, 0x35f3c439u, 0x6dc9c882u, 0x3fe45f30u, 0x041fe516u, 0x35f3c439u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0xf2c26dd2u, 0x0adfb3c9u, 0x6dc9c882u, 0x3fe45f30u, 0xf2c26dd2u, 0x0adfb3c9u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u},
{0x00000000u, 0x7ff00000u, 0x00000000u, 0x7ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x6dc9c882u, 0x3fe45f30u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x80000000u}
};
constexpr const char* Names[8] = {"rcp", "rsq", "sqrt", "trig preop", "trig preop inline", "trig preop abs neg omod", "rcp clamp", "sqrt abs neg"};
constexpr bool Approximate[8] = {true, true, true, false, false, false, true, true};
void Fill(std::uint32_t tid, std::uint32_t* words) {
std::copy(std::begin(Rows[tid]), std::end(Rows[tid]), words);
}
double F64(const std::uint32_t* words) {
return std::bit_cast<double>(static_cast<std::uint64_t>(words[1]) << 32u | words[0]);
}
bool Normal(const std::uint32_t* words) {
const auto exponent = (words[1] >> 20u) & 0x7ffu;
return exponent != 0u && exponent != 0x7ffu;
}
void ExpectNear(std::uint32_t tid, const std::uint32_t* actual, const std::uint32_t* expected, const char* name) {
const auto error = std::abs(F64(actual) - F64(expected)) / std::abs(F64(expected));
Require(error <= 0x1p-23, std::string("f64 transcendental: lane ") + std::to_string(tid) + " " + name + " is " + std::to_string(F64(actual)) + ", expected " + std::to_string(F64(expected)));
}
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("f64 transcendental: 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];
for (std::uint32_t i = 0; i < 8; ++i) {
const std::uint32_t* expected = &Expected[tid][2 * i];
if (Approximate[i] && Normal(expected)) {
ExpectNear(tid, out + 2 * i, expected, Names[i]);
continue;
}
Expect(tid, out[2 * i], expected[0], Names[i]);
Expect(tid, out[2 * i + 1], expected[1], Names[i]);
}
}
}
}
int main() {
try {
const auto device = OpenVulkanTestDevice();
if (!device) return VulkanTestSkipped;
Run(*device);
Check();
std::puts("f64 transcendental tests passed");
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
}
@@ -259,6 +259,10 @@ enum class IrOpcode : std::uint16_t {
FPFract64,
FPFrexpMant64,
FPFrexpExp64,
FPRcp64,
FPRsq64,
FPSqrt64,
FPTrigPreop64,
ConvertF32F64,
ConvertF64F32,
ConvertF64S32,
@@ -300,6 +300,10 @@ constexpr std::array<OpcodeMeta, static_cast<std::size_t>(IrOpcode::Count)> Meta
makeMeta("FPFract64", U64, U64),
makeMeta("FPFrexpMant64", U64, U64),
makeMeta("FPFrexpExp64", U32, U64),
makeMeta("FPRcp64", U64, U64),
makeMeta("FPRsq64", U64, U64),
makeMeta("FPSqrt64", U64, U64),
makeMeta("FPTrigPreop64", U64, U64, U32),
makeMeta("ConvertF32F64", F32, U64),
makeMeta("ConvertF64F32", U64, F32),
makeMeta("ConvertF64S32", U64, U32),
@@ -412,6 +412,10 @@ enum class RdnaOpcode : std::uint16_t {
VDivScaleF32,
VDivFmasF32,
VDivFixupF32,
VRcpF64,
VRsqF64,
VSqrtF64,
VTrigPreopF64,
VCvtPknormI16F32,
VCvtPknormU16F32,
VCvtPkU16U32,
@@ -363,6 +363,10 @@ bool IsVectorAluOpcode(RdnaOpcode opcode) {
case RdnaOpcode::VDivScaleF32:
case RdnaOpcode::VDivFmasF32:
case RdnaOpcode::VDivFixupF32:
case RdnaOpcode::VRcpF64:
case RdnaOpcode::VRsqF64:
case RdnaOpcode::VSqrtF64:
case RdnaOpcode::VTrigPreopF64:
case RdnaOpcode::VCvtPknormI16F32:
case RdnaOpcode::VCvtPknormU16F32:
case RdnaOpcode::VCvtPkU16U32:
@@ -141,6 +141,9 @@ constexpr VectorOpcodeInfo vop1Opcodes[] = {
{0x3cu, RdnaOpcode::VFrexpExpI32F64},
{0x3du, RdnaOpcode::VFrexpMantF64},
{0x3eu, RdnaOpcode::VFractF64},
{0x2fu, RdnaOpcode::VRcpF64},
{0x31u, RdnaOpcode::VRsqF64},
{0x34u, RdnaOpcode::VSqrtF64},
{0x25u, RdnaOpcode::VExpF32},
{0x27u, RdnaOpcode::VLogF32},
{0x2bu, RdnaOpcode::VRcpIflagF32},
@@ -222,6 +225,9 @@ constexpr VectorOpcodeInfo vop3EncodedVop1Opcodes[] = {
{0x3cu, RdnaOpcode::VFrexpExpI32F64},
{0x3du, RdnaOpcode::VFrexpMantF64},
{0x3eu, RdnaOpcode::VFractF64},
{0x2fu, RdnaOpcode::VRcpF64},
{0x31u, RdnaOpcode::VRsqF64},
{0x34u, RdnaOpcode::VSqrtF64},
{0x25u, RdnaOpcode::VExpF32},
{0x27u, RdnaOpcode::VLogF32},
{0x2bu, RdnaOpcode::VRcpIflagF32},
@@ -552,6 +558,7 @@ constexpr VectorOpcodeInfo vop3Opcodes[] = {
{0x15fu, RdnaOpcode::VDivFixupF32},
{0x16du, RdnaOpcode::VDivScaleF32},
{0x16fu, RdnaOpcode::VDivFmasF32},
{0x174u, RdnaOpcode::VTrigPreopF64},
{0x363u, RdnaOpcode::VBfmB32},
{0x364u, RdnaOpcode::VBcntU32B32},
{0x365u, RdnaOpcode::VMbcntLoU32B32},
@@ -759,6 +766,9 @@ bool isVop1FloatSourceOpcode(RdnaOpcode opcode) {
case RdnaOpcode::VFrexpExpI32F64:
case RdnaOpcode::VFrexpMantF64:
case RdnaOpcode::VFractF64:
case RdnaOpcode::VRcpF64:
case RdnaOpcode::VRsqF64:
case RdnaOpcode::VSqrtF64:
case RdnaOpcode::VExpF32:
case RdnaOpcode::VLogF32:
case RdnaOpcode::VRsqF32:
@@ -811,6 +821,9 @@ bool isVop1FloatResultOpcode(RdnaOpcode opcode) {
case RdnaOpcode::VFloorF64:
case RdnaOpcode::VFrexpMantF64:
case RdnaOpcode::VFractF64:
case RdnaOpcode::VRcpF64:
case RdnaOpcode::VRsqF64:
case RdnaOpcode::VSqrtF64:
case RdnaOpcode::VFrexpMantF32:
case RdnaOpcode::VExpF32:
case RdnaOpcode::VLogF32:
@@ -1598,6 +1611,7 @@ std::uint32_t nativeVop3SourceCount(RdnaOpcode opcode) {
case RdnaOpcode::VMinF64:
case RdnaOpcode::VMaxF64:
case RdnaOpcode::VLdexpF64:
case RdnaOpcode::VTrigPreopF64:
case RdnaOpcode::VBfmB32:
case RdnaOpcode::VBcntU32B32:
case RdnaOpcode::VCvtPknormI16F32:
@@ -1785,7 +1799,8 @@ bool supportsNativeVop3SourceModifiers(RdnaOpcode opcode) {
case RdnaOpcode::VMulF64:
case RdnaOpcode::VMinF64:
case RdnaOpcode::VMaxF64:
case RdnaOpcode::VLdexpF64: return true;
case RdnaOpcode::VLdexpF64:
case RdnaOpcode::VTrigPreopF64: return true;
default: return false;
}
}
@@ -1813,6 +1828,7 @@ bool supportsNativeVop3ResultModifiers(RdnaOpcode opcode) {
case RdnaOpcode::VMinF64:
case RdnaOpcode::VMaxF64:
case RdnaOpcode::VLdexpF64:
case RdnaOpcode::VTrigPreopF64:
case RdnaOpcode::VMin3F32:
case RdnaOpcode::VMax3F32:
case RdnaOpcode::VMed3F32: return true;
@@ -1862,7 +1878,7 @@ void checkNativeVop3Modifiers(RdnaOpcode opcode, bool permlane, bool carryInOut,
}
return;
}
if (opcode == RdnaOpcode::VLdexpF32 || opcode == RdnaOpcode::VLdexpF64) {
if (opcode == RdnaOpcode::VLdexpF32 || opcode == RdnaOpcode::VLdexpF64 || opcode == RdnaOpcode::VTrigPreopF64) {
if ((abs & ~1u) != 0u || opSel != 0u || (neg & ~1u) != 0u) {
throw std::invalid_argument("VOP3 source modifiers are not implemented");
}
@@ -193,6 +193,10 @@ std::uint32_t EmitFPTrunc64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPFract64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPFrexpMant64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPFrexpExp64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPRcp64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPRsq64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPSqrt64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitFPTrigPreop64(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1);
std::uint32_t EmitConvertF32F64(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitConvertF64F32(SpirvEmitterState& state, std::uint32_t arg0);
std::uint32_t EmitConvertF64S32(SpirvEmitterState& state, std::uint32_t arg0);
@@ -1,8 +1,10 @@
#include "SpirvBackend/SpirvEmitterInstructions.hpp"
#include <spirv/unified1/GLSL.std.450.h>
#include <spirv/unified1/spirv.hpp>
#include <array>
#include <initializer_list>
#include <iterator>
#include <span>
namespace ShaderRecompiler {
namespace {
@@ -352,6 +354,83 @@ std::uint32_t EmitFPFrexpExp64(SpirvEmitterState& state, std::uint32_t arg0) {
return Select(state, TypeU32(state), special, ConstantU32(state, 0u), Unary(state, spv::OpBitcast, TypeU32(state), exponent));
}
std::uint32_t EmitFPRcp64(SpirvEmitterState& state, std::uint32_t arg0) {
const auto cls = Classify(state, arg0);
const auto sign = Binary(state, spv::OpBitwiseAnd, TypeU32(state), cls.bits.high, ConstantU32(state, 0x80000000u));
const auto quotient = Split(state, FromF64(state, Exact(state, spv::OpFDiv, ConstantF64(state, 0x3ff0000000000000ull), ToF64(state, arg0))));
auto result = SelectBits(state, cls.zero, {ConstantU32(state, 0u), Binary(state, spv::OpBitwiseOr, TypeU32(state), sign, ConstantU32(state, 0x7ff00000u))}, quotient);
result = SelectBits(state, cls.infinite, {ConstantU32(state, 0u), sign}, result);
return Join(state, SelectBits(state, cls.nan, Quiet(state, cls.bits), result));
}
std::uint32_t EmitFPRsq64(SpirvEmitterState& state, std::uint32_t arg0) {
const auto cls = Classify(state, arg0);
const auto sign = Binary(state, spv::OpBitwiseAnd, TypeU32(state), cls.bits.high, ConstantU32(state, 0x80000000u));
const auto root = Glsl(state, TypeF64(state), GLSLstd450Sqrt, ToF64(state, arg0));
auto result = Split(state, FromF64(state, Exact(state, spv::OpFDiv, ConstantF64(state, 0x3ff0000000000000ull), root)));
result = SelectBits(state, cls.negative, {ConstantU32(state, 0u), ConstantU32(state, 0xfff80000u)}, result);
result = SelectBits(state, cls.zero, {ConstantU32(state, 0u), Binary(state, spv::OpBitwiseOr, TypeU32(state), sign, ConstantU32(state, 0x7ff00000u))}, result);
result = SelectBits(state, cls.infinite, SelectBits(state, cls.negative, result, {ConstantU32(state, 0u), ConstantU32(state, 0u)}), result);
return Join(state, SelectBits(state, cls.nan, Quiet(state, cls.bits), result));
}
std::uint32_t EmitFPSqrt64(SpirvEmitterState& state, std::uint32_t arg0) {
const auto cls = Classify(state, arg0);
auto result = Split(state, FromF64(state, Glsl(state, TypeF64(state), GLSLstd450Sqrt, ToF64(state, arg0))));
result = SelectBits(state, cls.negative, {ConstantU32(state, 0u), ConstantU32(state, 0xfff80000u)}, result);
const auto passthrough = Binary(state, spv::OpLogicalOr, TypeBool(state), cls.zero, Binary(state, spv::OpLogicalAnd, TypeBool(state), cls.infinite, Unary(state, spv::OpLogicalNot, TypeBool(state), cls.negative)));
result = SelectBits(state, passthrough, cls.bits, result);
return Join(state, SelectBits(state, cls.nan, Quiet(state, cls.bits), result));
}
std::uint32_t EmitFPTrigPreop64(SpirvEmitterState& state, std::uint32_t arg0, std::uint32_t arg1) {
static constexpr std::array<std::uint32_t, 40> TwoOverPi{
0xa2f9836eu, 0x4e441529u, 0xfc2757d1u, 0xf534ddc0u, 0xdb629599u, 0x3c439041u, 0xfe5163abu, 0xdebbc561u,
0xb7246e3au, 0x424dd2e0u, 0x06492eeau, 0x09d1921cu, 0xfe1deb1cu, 0xb129a73eu, 0xe88235f5u, 0x2ebb4484u,
0xe99c7026u, 0xb45f7e41u, 0x3991d639u, 0x835339f4u, 0x9c845f8bu, 0xbdf9283bu, 0x1ff897ffu, 0xde05980fu,
0xef2f118bu, 0x5a0a6d1fu, 0x6d367ecfu, 0x27cb09b7u, 0x4f463f66u, 0x9e5fea2du, 0x7527bac7u, 0xebe5f17bu,
0x3d0739f7u, 0x8a5292eau, 0x6bfb5fb1u, 0x1f8d5d08u, 0x56033046u, 0x00000000u, 0x00000000u, 0x00000000u,
};
std::array<std::uint32_t, TwoOverPi.size()> words{};
for (std::size_t index = 0; index < words.size(); ++index) words[index] = ConstantU32(state, TwoOverPi[index]);
const auto arrayType = state.module.Type(spv::OpTypeArray, TypeU32(state), ConstantU32(state, static_cast<std::uint32_t>(words.size())));
const auto table = state.module.DefineGlobalVariable(TypePointer(state, spv::StorageClassPrivate, arrayType), spv::StorageClassPrivate);
state.module.AddFunction(spv::OpStore, table, state.module.Constant(spv::OpConstantComposite, arrayType, std::span<const std::uint32_t>(words)));
const auto load = [&](std::uint32_t index) {
const auto pointer = state.module.AllocateId();
state.module.AddFunction(spv::OpAccessChain, TypePointer(state, spv::StorageClassPrivate, TypeU32(state)), pointer, table, index);
const auto value = state.module.AllocateId();
state.module.AddFunction(spv::OpLoad, TypeU32(state), value, pointer);
return value;
};
const auto bits = Split(state, arg0);
const auto exponent = Binary(state, spv::OpBitwiseAnd, TypeU32(state), Binary(state, spv::OpShiftRightLogical, TypeU32(state), bits.high, ConstantU32(state, 20u)), ConstantU32(state, 0x7ffu));
const auto extra = Binary(state, spv::OpISub, TypeU32(state), Glsl(state, TypeU32(state), GLSLstd450UMax, exponent, ConstantU32(state, 1077u)), ConstantU32(state, 1077u));
const auto segment = Binary(state, spv::OpIMul, TypeU32(state), Binary(state, spv::OpBitwiseAnd, TypeU32(state), arg1, ConstantU32(state, 31u)), ConstantU32(state, 53u));
const auto shift = Glsl(state, TypeU32(state), GLSLstd450UMin, Binary(state, spv::OpIAdd, TypeU32(state), segment, extra), ConstantU32(state, 1201u));
const auto word = Binary(state, spv::OpShiftRightLogical, TypeU32(state), shift, ConstantU32(state, 5u));
const auto offset = Binary(state, spv::OpBitwiseAnd, TypeU32(state), shift, ConstantU32(state, 31u));
const auto aligned = Binary(state, spv::OpIEqual, TypeBool(state), offset, ConstantU32(state, 0u));
const auto rest = Binary(state, spv::OpISub, TypeU32(state), ConstantU32(state, 32u), offset);
const auto funnel = [&](std::uint32_t upper, std::uint32_t lower) {
const auto merged = Binary(state, spv::OpBitwiseOr, TypeU32(state), Binary(state, spv::OpShiftLeftLogical, TypeU32(state), upper, offset), Binary(state, spv::OpShiftRightLogical, TypeU32(state), lower, rest));
return Select(state, TypeU32(state), aligned, upper, merged);
};
const auto first = load(word);
const auto second = load(Binary(state, spv::OpIAdd, TypeU32(state), word, ConstantU32(state, 1u)));
const auto third = load(Binary(state, spv::OpIAdd, TypeU32(state), word, ConstantU32(state, 2u)));
const auto high = Unary(state, spv::OpConvertUToF, TypeF64(state), funnel(first, second));
const auto low = Unary(state, spv::OpConvertUToF, TypeF64(state), Binary(state, spv::OpShiftRightLogical, TypeU32(state), funnel(second, third), ConstantU32(state, 11u)));
const auto mantissa = Exact(state, spv::OpFAdd, Exact(state, spv::OpFMul, high, ConstantF64(state, 0x4140000000000000ull)), low);
const auto large = Binary(state, spv::OpUGreaterThanEqual, TypeBool(state), exponent, ConstantU32(state, 1968u));
const auto bias = Select(state, TypeU32(state), large, ConstantU32(state, 1023u + 600u - 53u + 128u), ConstantU32(state, 1023u + 600u - 53u));
const auto scaleExponent = Binary(state, spv::OpISub, TypeU32(state), bias, shift);
const auto scale = ToF64(state, Join(state, {ConstantU32(state, 0u), Binary(state, spv::OpShiftLeftLogical, TypeU32(state), scaleExponent, ConstantU32(state, 20u))}));
return FromF64(state, Exact(state, spv::OpFMul, Exact(state, spv::OpFMul, mantissa, scale), ConstantF64(state, 0x1a70000000000000ull)));
}
std::uint32_t EmitConvertF32F64(SpirvEmitterState& state, std::uint32_t arg0) {
const auto converted = Unary(state, spv::OpBitcast, TypeU32(state), Unary(state, spv::OpFConvert, TypeF32(state), ToF64(state, arg0)));
const auto cls = Classify(state, arg0);
@@ -433,6 +433,10 @@ void EmitDirectInstruction(SpirvValueEmitContext& ctx, const IrValue& inst) {
case IrOpcode::FPFract64: return Invoke(EmitFPFract64, ctx, inst);
case IrOpcode::FPFrexpMant64: return Invoke(EmitFPFrexpMant64, ctx, inst);
case IrOpcode::FPFrexpExp64: return Invoke(EmitFPFrexpExp64, ctx, inst);
case IrOpcode::FPRcp64: return Invoke(EmitFPRcp64, ctx, inst);
case IrOpcode::FPRsq64: return Invoke(EmitFPRsq64, ctx, inst);
case IrOpcode::FPSqrt64: return Invoke(EmitFPSqrt64, ctx, inst);
case IrOpcode::FPTrigPreop64: return Invoke(EmitFPTrigPreop64, ctx, inst);
case IrOpcode::ConvertF32F64: return Invoke(EmitConvertF32F64, ctx, inst);
case IrOpcode::ConvertF64F32: return Invoke(EmitConvertF64F32, ctx, inst);
case IrOpcode::ConvertF64S32: return Invoke(EmitConvertF64S32, ctx, inst);
@@ -871,6 +871,14 @@ bool TranslationContext::emitVector(const RdnaInstruction& inst) {
return float64Operation(inst, IrOpcode::FPFloor64);
case RdnaOpcode::VFractF64:
return float64Operation(inst, IrOpcode::FPFract64);
case RdnaOpcode::VRcpF64:
return float64Operation(inst, IrOpcode::FPRcp64);
case RdnaOpcode::VRsqF64:
return float64Operation(inst, IrOpcode::FPRsq64);
case RdnaOpcode::VSqrtF64:
return float64Operation(inst, IrOpcode::FPSqrt64);
case RdnaOpcode::VTrigPreopF64:
return float64Operation(inst, IrOpcode::FPTrigPreop64);
case RdnaOpcode::VFrexpMantF64:
return float64Operation(inst, IrOpcode::FPFrexpMant64);
case RdnaOpcode::VFrexpExpI32F64: