mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(shader): implement the float and 64-bit inc/dec buffer atomics
buffer_atomic_fcmpswap, fcmpswap_x2, fmin_x2, fmax_x2, inc_x2 and dec_x2 are compare-and-swap loops on the dword or qword. fcmpswap compares as floats (-0 equals +0, NaN equals nothing) and fmin/fmax are IEEE minNum/maxNum with -0 < +0, a quieted result for a signalling NaN and the other operand for a quiet one, all with integer ops so the driver's float controls do not matter. The existing 32-bit fmin/fmax kept the old value whenever a NaN or two zeros were involved; it now uses the same rule. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
#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, 123> Code{
|
||||
0x34020084, 0x34060086, 0xe0301000, 0x80000401, 0xe0301004, 0x80000501, 0xe0301008, 0x80000601,
|
||||
0xe030100c, 0x80000701, 0xbf8c3f70, 0xe0701000, 0x80010403, 0xbf8c3f70, 0x7e500306, 0x7e520307,
|
||||
0xe0f85000, 0x80012803, 0xbf8c3f70, 0x7e140328, 0xe030d000, 0x80010b03, 0xbf8c3f70, 0xe0701008,
|
||||
0x80010403, 0xbf8c3f70, 0x7e500306, 0xe0fc5008, 0x80012803, 0xbf8c3f70, 0xe030d008, 0x80010c03,
|
||||
0xbf8c3f70, 0xe0701010, 0x80010403, 0xbf8c3f70, 0x7e500306, 0xe1005010, 0x80012803, 0xbf8c3f70,
|
||||
0xe030d010, 0x80010d03, 0xbf8c3f70, 0xe0701018, 0x80010403, 0xe070101c, 0x80010503, 0xbf8c3f70,
|
||||
0x7e500306, 0x7e520307, 0x7e540304, 0x7e560307, 0xe1785018, 0x80012803, 0xbf8c3f70, 0x7e1c0328,
|
||||
0x7e1e0329, 0xe030d018, 0x80011003, 0xe030d01c, 0x80011103, 0xbf8c3f70, 0xe0701020, 0x80010403,
|
||||
0xe0701024, 0x80010503, 0xbf8c3f70, 0x7e500306, 0x7e520307, 0xe17c5020, 0x80012803, 0xbf8c3f70,
|
||||
0x7e240328, 0x7e260329, 0xe030d020, 0x80011403, 0xe030d024, 0x80011503, 0xbf8c3f70, 0xe0701028,
|
||||
0x80010403, 0xe070102c, 0x80010503, 0xbf8c3f70, 0x7e500306, 0x7e520307, 0xe1805028, 0x80012803,
|
||||
0xbf8c3f70, 0xe030d028, 0x80011603, 0xe030d02c, 0x80011703, 0xbf8c3f70, 0xe0701000, 0x80010a03,
|
||||
0xe0701004, 0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xe0701010, 0x80010e03,
|
||||
0xe0701014, 0x80010f03, 0xe0701018, 0x80011003, 0xe070101c, 0x80011103, 0xe0701020, 0x80011203,
|
||||
0xe0701024, 0x80011303, 0xe0701028, 0x80011403, 0xe070102c, 0x80011503, 0xe0701030, 0x80011603,
|
||||
0xe0701034, 0x80011703, 0xbf810000,
|
||||
};
|
||||
|
||||
constexpr const char* Names[14] = {"fcmpswap_ret0", "fcmpswap_mem0", "fmin_mem0", "fmax_mem0", "fcmpswap_x2_ret0", "fcmpswap_x2_ret1", "fcmpswap_x2_mem0", "fcmpswap_x2_mem1", "fmin_x2_ret0", "fmin_x2_ret1", "fmin_x2_mem0", "fmin_x2_mem1", "fmax_x2_mem0", "fmax_x2_mem1"};
|
||||
constexpr std::uint32_t Rows[32][4] = {
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u},
|
||||
{0xbf800000u, 0x00000000u, 0x00000001u, 0x7ff40000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000123u, 0x00000000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u},
|
||||
{0xff800000u, 0x00000000u, 0x3f800000u, 0x80000000u},
|
||||
{0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u},
|
||||
{0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u},
|
||||
{0x00000000u, 0x80000000u, 0x00000123u, 0xfff80000u},
|
||||
{0x7fa00000u, 0x80000000u, 0x00000001u, 0x80000000u},
|
||||
{0x00000000u, 0x80000000u, 0x00000001u, 0x00000000u},
|
||||
{0x00000000u, 0x80000000u, 0x00000001u, 0x80000000u},
|
||||
{0x00000000u, 0x3ff00000u, 0x00000000u, 0x00000000u},
|
||||
{0x7f800000u, 0x3ff00000u, 0x80000001u, 0x7ff40000u},
|
||||
{0x00000000u, 0x3ff00000u, 0x00000123u, 0xfff80000u},
|
||||
{0x00000000u, 0x3ff00000u, 0x00000001u, 0x3ff00000u},
|
||||
{0x00000001u, 0x7ff00000u, 0x31162427u, 0xfee29476u},
|
||||
{0xffc00123u, 0x65aa9c82u, 0x00000000u, 0xffc00123u},
|
||||
{0x28ce6f24u, 0xc21b6092u, 0x00000000u, 0xc21b6092u},
|
||||
{0x00000001u, 0x80000000u, 0x651c5253u, 0xba6676b3u},
|
||||
{0xc1b93750u, 0xbfd543f8u, 0x378c74dcu, 0x420b0ebeu},
|
||||
{0x00000001u, 0x00000000u, 0xffc00123u, 0x00000000u},
|
||||
{0xe593c850u, 0x3fe02d24u, 0x3dad98e4u, 0xe593c850u},
|
||||
{0xf6f22f41u, 0x8ab12c32u, 0xa7cf94d7u, 0x360c4979u},
|
||||
{0xb3db5b74u, 0x3fffd598u, 0x491ba556u, 0x3fffd598u},
|
||||
{0x00000000u, 0xbfedefc5u, 0x00000001u, 0xc00708b3u},
|
||||
{0x60b7d02bu, 0xb7ebb70cu, 0x8d63c8ceu, 0xbff03a78u},
|
||||
{0x1badb4f5u, 0x998a0e31u, 0x00000000u, 0x1badb4f5u},
|
||||
{0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u},
|
||||
{0x7fc00000u, 0xa4d5e415u, 0xffc00123u, 0xf90ee1f2u},
|
||||
{0x00000001u, 0x80000000u, 0x85689935u, 0x80000000u},
|
||||
{0x00000000u, 0x7ff80000u, 0x0fbdababu, 0xc0070997u},
|
||||
};
|
||||
constexpr std::uint32_t Expected[32][14] = {
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u},
|
||||
{0xbf800000u, 0xbf800000u, 0xbf800000u, 0x00000001u, 0xbf800000u, 0x00000000u, 0xbf800000u, 0x00000000u, 0xbf800000u, 0x00000000u, 0x00000001u, 0x7ffc0000u, 0x00000001u, 0x7ffc0000u},
|
||||
{0x00000000u, 0x00000123u, 0x00000000u, 0x00000123u, 0x00000000u, 0x00000000u, 0x00000123u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000123u, 0x00000000u},
|
||||
{0x00000000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u},
|
||||
{0x00000000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u},
|
||||
{0xff800000u, 0xff800000u, 0xff800000u, 0x3f800000u, 0xff800000u, 0x00000000u, 0xff800000u, 0x00000000u, 0xff800000u, 0x00000000u, 0x3f800000u, 0x80000000u, 0xff800000u, 0x00000000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x00000000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000123u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u},
|
||||
{0x7fa00000u, 0x7fa00000u, 0x7fe00000u, 0x7fe00000u, 0x7fa00000u, 0x80000000u, 0x00000001u, 0x80000000u, 0x7fa00000u, 0x80000000u, 0x7fa00000u, 0x80000000u, 0x00000001u, 0x80000000u},
|
||||
{0x00000000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x80000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000001u, 0x00000000u},
|
||||
{0x00000000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0x80000000u, 0x00000001u, 0x80000000u, 0x00000000u, 0x80000000u, 0x00000001u, 0x80000000u, 0x00000000u, 0x80000000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u},
|
||||
{0x7f800000u, 0x7f800000u, 0x80000001u, 0x7f800000u, 0x7f800000u, 0x3ff00000u, 0x7f800000u, 0x3ff00000u, 0x7f800000u, 0x3ff00000u, 0x80000001u, 0x7ffc0000u, 0x80000001u, 0x7ffc0000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000123u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x3ff00000u, 0x00000001u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000001u, 0x3ff00000u},
|
||||
{0x00000001u, 0x00000001u, 0x00000001u, 0x31162427u, 0x00000001u, 0x7ff00000u, 0x00000001u, 0x7ff00000u, 0x00000001u, 0x7ff00000u, 0x00000001u, 0x7ff80000u, 0x00000001u, 0x7ff80000u},
|
||||
{0xffc00123u, 0xffc00123u, 0x00000000u, 0x00000000u, 0xffc00123u, 0x65aa9c82u, 0xffc00123u, 0x65aa9c82u, 0xffc00123u, 0x65aa9c82u, 0x00000000u, 0xffc00123u, 0xffc00123u, 0x65aa9c82u},
|
||||
{0x28ce6f24u, 0x28ce6f24u, 0x00000000u, 0x28ce6f24u, 0x28ce6f24u, 0xc21b6092u, 0x00000000u, 0xc21b6092u, 0x28ce6f24u, 0xc21b6092u, 0x28ce6f24u, 0xc21b6092u, 0x00000000u, 0xc21b6092u},
|
||||
{0x00000001u, 0x00000001u, 0x00000001u, 0x651c5253u, 0x00000001u, 0x80000000u, 0x00000001u, 0x80000000u, 0x00000001u, 0x80000000u, 0x651c5253u, 0xba6676b3u, 0x00000001u, 0x80000000u},
|
||||
{0xc1b93750u, 0xc1b93750u, 0xc1b93750u, 0x378c74dcu, 0xc1b93750u, 0xbfd543f8u, 0xc1b93750u, 0xbfd543f8u, 0xc1b93750u, 0xbfd543f8u, 0xc1b93750u, 0xbfd543f8u, 0x378c74dcu, 0x420b0ebeu},
|
||||
{0x00000001u, 0x00000001u, 0x00000001u, 0x00000001u, 0x00000001u, 0x00000000u, 0xffc00123u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u, 0xffc00123u, 0x00000000u},
|
||||
{0xe593c850u, 0x3dad98e4u, 0xe593c850u, 0x3dad98e4u, 0xe593c850u, 0x3fe02d24u, 0xe593c850u, 0x3fe02d24u, 0xe593c850u, 0x3fe02d24u, 0x3dad98e4u, 0xe593c850u, 0xe593c850u, 0x3fe02d24u},
|
||||
{0xf6f22f41u, 0xf6f22f41u, 0xf6f22f41u, 0xa7cf94d7u, 0xf6f22f41u, 0x8ab12c32u, 0xf6f22f41u, 0x8ab12c32u, 0xf6f22f41u, 0x8ab12c32u, 0xf6f22f41u, 0x8ab12c32u, 0xa7cf94d7u, 0x360c4979u},
|
||||
{0xb3db5b74u, 0xb3db5b74u, 0xb3db5b74u, 0x491ba556u, 0xb3db5b74u, 0x3fffd598u, 0x491ba556u, 0x3fffd598u, 0xb3db5b74u, 0x3fffd598u, 0x491ba556u, 0x3fffd598u, 0xb3db5b74u, 0x3fffd598u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0xbfedefc5u, 0x00000000u, 0xbfedefc5u, 0x00000000u, 0xbfedefc5u, 0x00000001u, 0xc00708b3u, 0x00000000u, 0xbfedefc5u},
|
||||
{0x60b7d02bu, 0x60b7d02bu, 0x8d63c8ceu, 0x60b7d02bu, 0x60b7d02bu, 0xb7ebb70cu, 0x60b7d02bu, 0xb7ebb70cu, 0x60b7d02bu, 0xb7ebb70cu, 0x8d63c8ceu, 0xbff03a78u, 0x60b7d02bu, 0xb7ebb70cu},
|
||||
{0x1badb4f5u, 0x00000000u, 0x00000000u, 0x1badb4f5u, 0x1badb4f5u, 0x998a0e31u, 0x1badb4f5u, 0x998a0e31u, 0x1badb4f5u, 0x998a0e31u, 0x1badb4f5u, 0x998a0e31u, 0x00000000u, 0x1badb4f5u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u, 0x00000000u, 0x3ff00000u},
|
||||
{0x7fc00000u, 0x7fc00000u, 0xffc00123u, 0xffc00123u, 0x7fc00000u, 0xa4d5e415u, 0x7fc00000u, 0xa4d5e415u, 0x7fc00000u, 0xa4d5e415u, 0xffc00123u, 0xf90ee1f2u, 0x7fc00000u, 0xa4d5e415u},
|
||||
{0x00000001u, 0x00000001u, 0x85689935u, 0x00000001u, 0x00000001u, 0x80000000u, 0x85689935u, 0x80000000u, 0x00000001u, 0x80000000u, 0x85689935u, 0x80000000u, 0x00000001u, 0x80000000u},
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x0fbdababu, 0x00000000u, 0x7ff80000u, 0x00000000u, 0x7ff80000u, 0x00000000u, 0x7ff80000u, 0x0fbdababu, 0xc0070997u, 0x0fbdababu, 0xc0070997u},
|
||||
};
|
||||
|
||||
void Fill(std::uint32_t tid, std::uint32_t* words) {
|
||||
std::copy(std::begin(Rows[tid]), std::end(Rows[tid]), words);
|
||||
}
|
||||
|
||||
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("buffer atomics float: 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 index = 0; index < 14; ++index) Expect(tid, out[index], Expected[tid][index], Names[index]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
const auto device = OpenVulkanTestDevice();
|
||||
if (!device) return VulkanTestSkipped;
|
||||
Run(*device);
|
||||
Check();
|
||||
std::puts("buffer atomics float tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
#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, 62> Code{
|
||||
0x34020084, 0x34060086, 0xe0301000, 0x80000401, 0xe0301004, 0x80000501, 0xe0301008, 0x80000601,
|
||||
0xe030100c, 0x80000701, 0xbf8c3f70, 0xe0701000, 0x80010403, 0xe0701004, 0x80010503, 0xbf8c3f70,
|
||||
0x7e500306, 0x7e520307, 0xe1705000, 0x80012803, 0xbf8c3f70, 0x7e140328, 0x7e160329, 0xe030d000,
|
||||
0x80010c03, 0xe030d004, 0x80010d03, 0xbf8c3f70, 0xe0701008, 0x80010403, 0xe070100c, 0x80010503,
|
||||
0xbf8c3f70, 0x7e500306, 0x7e520307, 0xe1745008, 0x80012803, 0xbf8c3f70, 0x7e1c0328, 0x7e1e0329,
|
||||
0xe030d008, 0x80011003, 0xe030d00c, 0x80011103, 0xbf8c3f70, 0xe0701000, 0x80010a03, 0xe0701004,
|
||||
0x80010b03, 0xe0701008, 0x80010c03, 0xe070100c, 0x80010d03, 0xe0701010, 0x80010e03, 0xe0701014,
|
||||
0x80010f03, 0xe0701018, 0x80011003, 0xe070101c, 0x80011103, 0xbf810000,
|
||||
};
|
||||
|
||||
constexpr const char* Names[8] = {"inc_x2_ret0", "inc_x2_ret1", "inc_x2_mem0", "inc_x2_mem1", "dec_x2_ret0", "dec_x2_ret1", "dec_x2_mem0", "dec_x2_mem1"};
|
||||
constexpr std::uint32_t Rows[32][4] = {
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u},
|
||||
{0x00000001u, 0x00000000u, 0x00000001u, 0x00000000u},
|
||||
{0x00000002u, 0x00000000u, 0x00000001u, 0x00000000u},
|
||||
{0xffffffffu, 0x00000000u, 0xffffffffu, 0x00000000u},
|
||||
{0x00000000u, 0x00000001u, 0xffffffffu, 0x00000000u},
|
||||
{0xffffffffu, 0xffffffffu, 0xffffffffu, 0xffffffffu},
|
||||
{0x00000005u, 0x00000000u, 0x00000004u, 0x00000000u},
|
||||
{0x00000005u, 0x00000000u, 0x00000006u, 0x00000000u},
|
||||
{0x0000003cu, 0x00000000u, 0x4da4f9fcu, 0x1a6916c7u},
|
||||
{0x7a97c643u, 0x27ac435au, 0x7a97c643u, 0x27ac435au},
|
||||
{0x00000066u, 0x00000000u, 0x00000065u, 0x00000000u},
|
||||
{0xccea71ffu, 0xfd724452u, 0xccea7200u, 0xfd724452u},
|
||||
{0x00000085u, 0x00000000u, 0x00000084u, 0x00000000u},
|
||||
{0xc79d6793u, 0x2c33be0au, 0xc79d6792u, 0x2c33be0au},
|
||||
{0x00000036u, 0x00000000u, 0x00000036u, 0x00000000u},
|
||||
{0xd4341aadu, 0xa4042bb3u, 0xd4341aacu, 0xa4042bb3u},
|
||||
{0x00000031u, 0x00000000u, 0x00000030u, 0x00000000u},
|
||||
{0xa0817910u, 0xde08caa1u, 0xa081790fu, 0xde08caa1u},
|
||||
{0x00000016u, 0x00000000u, 0x00000015u, 0x00000000u},
|
||||
{0xabf4a07cu, 0x634f806fu, 0xabf4a07du, 0x634f806fu},
|
||||
{0x0000003fu, 0x00000000u, 0x0000003fu, 0x00000000u},
|
||||
{0xf1cfd992u, 0xef412ed6u, 0xf1cfd991u, 0xef412ed6u},
|
||||
{0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u},
|
||||
{0xd9196adau, 0xc3e4a892u, 0x8224b122u, 0x31f3b923u},
|
||||
{0x0000006cu, 0x00000000u, 0x995253fdu, 0x49c7b59bu},
|
||||
{0x738d243au, 0x294c4ea3u, 0x738d2439u, 0x294c4ea3u},
|
||||
{0x000000d0u, 0x00000000u, 0x000000d0u, 0x00000000u},
|
||||
{0x0bdbc23au, 0x7671863cu, 0x0bdbc239u, 0x7671863cu},
|
||||
{0x00000084u, 0x00000000u, 0x88dcf943u, 0xa5e333cbu},
|
||||
{0xb36cc9aau, 0x57c49391u, 0xb36cc9abu, 0x57c49391u},
|
||||
{0x00000011u, 0x00000000u, 0x00000012u, 0x00000000u},
|
||||
{0xa2909cb6u, 0xa1f65507u, 0xa2909cb7u, 0xa1f65507u},
|
||||
};
|
||||
constexpr std::uint32_t Expected[32][8] = {
|
||||
{0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u},
|
||||
{0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u},
|
||||
{0x00000002u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000002u, 0x00000000u, 0x00000001u, 0x00000000u},
|
||||
{0xffffffffu, 0x00000000u, 0x00000000u, 0x00000000u, 0xffffffffu, 0x00000000u, 0xfffffffeu, 0x00000000u},
|
||||
{0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0xffffffffu, 0x00000000u},
|
||||
{0xffffffffu, 0xffffffffu, 0x00000000u, 0x00000000u, 0xffffffffu, 0xffffffffu, 0xfffffffeu, 0xffffffffu},
|
||||
{0x00000005u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000005u, 0x00000000u, 0x00000004u, 0x00000000u},
|
||||
{0x00000005u, 0x00000000u, 0x00000006u, 0x00000000u, 0x00000005u, 0x00000000u, 0x00000004u, 0x00000000u},
|
||||
{0x0000003cu, 0x00000000u, 0x0000003du, 0x00000000u, 0x0000003cu, 0x00000000u, 0x0000003bu, 0x00000000u},
|
||||
{0x7a97c643u, 0x27ac435au, 0x00000000u, 0x00000000u, 0x7a97c643u, 0x27ac435au, 0x7a97c642u, 0x27ac435au},
|
||||
{0x00000066u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000066u, 0x00000000u, 0x00000065u, 0x00000000u},
|
||||
{0xccea71ffu, 0xfd724452u, 0xccea7200u, 0xfd724452u, 0xccea71ffu, 0xfd724452u, 0xccea71feu, 0xfd724452u},
|
||||
{0x00000085u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000085u, 0x00000000u, 0x00000084u, 0x00000000u},
|
||||
{0xc79d6793u, 0x2c33be0au, 0x00000000u, 0x00000000u, 0xc79d6793u, 0x2c33be0au, 0xc79d6792u, 0x2c33be0au},
|
||||
{0x00000036u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000036u, 0x00000000u, 0x00000035u, 0x00000000u},
|
||||
{0xd4341aadu, 0xa4042bb3u, 0x00000000u, 0x00000000u, 0xd4341aadu, 0xa4042bb3u, 0xd4341aacu, 0xa4042bb3u},
|
||||
{0x00000031u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000031u, 0x00000000u, 0x00000030u, 0x00000000u},
|
||||
{0xa0817910u, 0xde08caa1u, 0x00000000u, 0x00000000u, 0xa0817910u, 0xde08caa1u, 0xa081790fu, 0xde08caa1u},
|
||||
{0x00000016u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000016u, 0x00000000u, 0x00000015u, 0x00000000u},
|
||||
{0xabf4a07cu, 0x634f806fu, 0xabf4a07du, 0x634f806fu, 0xabf4a07cu, 0x634f806fu, 0xabf4a07bu, 0x634f806fu},
|
||||
{0x0000003fu, 0x00000000u, 0x00000000u, 0x00000000u, 0x0000003fu, 0x00000000u, 0x0000003eu, 0x00000000u},
|
||||
{0xf1cfd992u, 0xef412ed6u, 0x00000000u, 0x00000000u, 0xf1cfd992u, 0xef412ed6u, 0xf1cfd991u, 0xef412ed6u},
|
||||
{0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u, 0x00000001u, 0x00000000u, 0x00000000u, 0x00000000u},
|
||||
{0xd9196adau, 0xc3e4a892u, 0x00000000u, 0x00000000u, 0xd9196adau, 0xc3e4a892u, 0x8224b122u, 0x31f3b923u},
|
||||
{0x0000006cu, 0x00000000u, 0x0000006du, 0x00000000u, 0x0000006cu, 0x00000000u, 0x0000006bu, 0x00000000u},
|
||||
{0x738d243au, 0x294c4ea3u, 0x00000000u, 0x00000000u, 0x738d243au, 0x294c4ea3u, 0x738d2439u, 0x294c4ea3u},
|
||||
{0x000000d0u, 0x00000000u, 0x00000000u, 0x00000000u, 0x000000d0u, 0x00000000u, 0x000000cfu, 0x00000000u},
|
||||
{0x0bdbc23au, 0x7671863cu, 0x00000000u, 0x00000000u, 0x0bdbc23au, 0x7671863cu, 0x0bdbc239u, 0x7671863cu},
|
||||
{0x00000084u, 0x00000000u, 0x00000085u, 0x00000000u, 0x00000084u, 0x00000000u, 0x00000083u, 0x00000000u},
|
||||
{0xb36cc9aau, 0x57c49391u, 0xb36cc9abu, 0x57c49391u, 0xb36cc9aau, 0x57c49391u, 0xb36cc9a9u, 0x57c49391u},
|
||||
{0x00000011u, 0x00000000u, 0x00000012u, 0x00000000u, 0x00000011u, 0x00000000u, 0x00000010u, 0x00000000u},
|
||||
{0xa2909cb6u, 0xa1f65507u, 0xa2909cb7u, 0xa1f65507u, 0xa2909cb6u, 0xa1f65507u, 0xa2909cb5u, 0xa1f65507u},
|
||||
};
|
||||
|
||||
void Fill(std::uint32_t tid, std::uint32_t* words) {
|
||||
std::copy(std::begin(Rows[tid]), std::end(Rows[tid]), words);
|
||||
}
|
||||
|
||||
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("buffer atomics inc dec64: 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 index = 0; index < 8; ++index) Expect(tid, out[index], Expected[tid][index], Names[index]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
const auto device = OpenVulkanTestDevice();
|
||||
if (!device) return VulkanTestSkipped;
|
||||
Run(*device);
|
||||
Check();
|
||||
std::puts("buffer atomics inc dec64 tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
+6
@@ -326,6 +326,12 @@ enum class IrOpcode : std::uint16_t {
|
||||
BufferAtomicAnd64,
|
||||
BufferAtomicXor64,
|
||||
BufferAtomicCmpSwap64,
|
||||
BufferAtomicFCmpSwap32,
|
||||
BufferAtomicFCmpSwap64,
|
||||
BufferAtomicFMin64,
|
||||
BufferAtomicFMax64,
|
||||
BufferAtomicInc64,
|
||||
BufferAtomicDec64,
|
||||
LoadSharedU8,
|
||||
LoadSharedU16,
|
||||
LoadSharedU32,
|
||||
|
||||
@@ -367,6 +367,12 @@ constexpr std::array<OpcodeMeta, static_cast<std::size_t>(IrOpcode::Count)> Meta
|
||||
makeMeta("BufferAtomicAnd64", U64, BufferResource, U32, U32, U32, U64, U1),
|
||||
makeMeta("BufferAtomicXor64", U64, BufferResource, U32, U32, U32, U64, U1),
|
||||
makeMeta("BufferAtomicCmpSwap64", U64, BufferResource, U32, U32, U32, U64, U64, U1),
|
||||
makeMeta("BufferAtomicFCmpSwap32", U32, BufferResource, U32, U32, U32, U32, U32, U1),
|
||||
makeMeta("BufferAtomicFCmpSwap64", U64, BufferResource, U32, U32, U32, U64, U64, U1),
|
||||
makeMeta("BufferAtomicFMin64", U64, BufferResource, U32, U32, U32, U64, U1),
|
||||
makeMeta("BufferAtomicFMax64", U64, BufferResource, U32, U32, U32, U64, U1),
|
||||
makeMeta("BufferAtomicInc64", U64, BufferResource, U32, U32, U32, U64, U1),
|
||||
makeMeta("BufferAtomicDec64", U64, BufferResource, U32, U32, U32, U64, U1),
|
||||
makeMeta("LoadSharedU8", U8, U32, U1),
|
||||
makeMeta("LoadSharedU16", U16, U32, U1),
|
||||
makeMeta("LoadSharedU32", U32, U32, U1),
|
||||
@@ -504,6 +510,12 @@ BufferAccess BufferAccessOf(IrOpcode opcode) {
|
||||
case IrOpcode::BufferAtomicAnd64:
|
||||
case IrOpcode::BufferAtomicXor64:
|
||||
case IrOpcode::BufferAtomicCmpSwap64:
|
||||
case IrOpcode::BufferAtomicFCmpSwap32:
|
||||
case IrOpcode::BufferAtomicFCmpSwap64:
|
||||
case IrOpcode::BufferAtomicFMin64:
|
||||
case IrOpcode::BufferAtomicFMax64:
|
||||
case IrOpcode::BufferAtomicInc64:
|
||||
case IrOpcode::BufferAtomicDec64:
|
||||
return BufferAccess::Atomic;
|
||||
default:
|
||||
return BufferAccess::None;
|
||||
@@ -523,6 +535,11 @@ std::uint32_t BufferComponentCount(IrOpcode opcode) {
|
||||
case IrOpcode::BufferAtomicAnd64:
|
||||
case IrOpcode::BufferAtomicXor64:
|
||||
case IrOpcode::BufferAtomicCmpSwap64:
|
||||
case IrOpcode::BufferAtomicFCmpSwap64:
|
||||
case IrOpcode::BufferAtomicFMin64:
|
||||
case IrOpcode::BufferAtomicFMax64:
|
||||
case IrOpcode::BufferAtomicInc64:
|
||||
case IrOpcode::BufferAtomicDec64:
|
||||
case IrOpcode::LoadBufferU32x2:
|
||||
case IrOpcode::StoreBufferU32x2:
|
||||
return 2u;
|
||||
|
||||
@@ -732,6 +732,12 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
BufferAtomicUmaxX2,
|
||||
BufferAtomicAndX2,
|
||||
BufferAtomicXorX2,
|
||||
BufferAtomicFcmpswap,
|
||||
BufferAtomicFcmpswapX2,
|
||||
BufferAtomicFminX2,
|
||||
BufferAtomicFmaxX2,
|
||||
BufferAtomicIncX2,
|
||||
BufferAtomicDecX2,
|
||||
BufferAtomicXor,
|
||||
BufferAtomicFmin,
|
||||
BufferAtomicFmax,
|
||||
|
||||
@@ -124,6 +124,12 @@ constexpr MemoryOpcodeInfo mubufOpcodes[] = {
|
||||
{0x58u, RdnaOpcode::BufferAtomicUmaxX2, 2, 32, false, false, false},
|
||||
{0x59u, RdnaOpcode::BufferAtomicAndX2, 2, 32, false, false, false},
|
||||
{0x5bu, RdnaOpcode::BufferAtomicXorX2, 2, 32, false, false, false},
|
||||
{0x3eu, RdnaOpcode::BufferAtomicFcmpswap, 1, 32, false, false, false},
|
||||
{0x5cu, RdnaOpcode::BufferAtomicIncX2, 2, 32, false, false, false},
|
||||
{0x5du, RdnaOpcode::BufferAtomicDecX2, 2, 32, false, false, false},
|
||||
{0x5eu, RdnaOpcode::BufferAtomicFcmpswapX2, 2, 32, false, false, false},
|
||||
{0x5fu, RdnaOpcode::BufferAtomicFminX2, 2, 32, false, false, false},
|
||||
{0x60u, RdnaOpcode::BufferAtomicFmaxX2, 2, 32, false, false, false},
|
||||
{0x80u, RdnaOpcode::BufferLoadFormatD16X, 1, 32, false, false, true},
|
||||
{0x81u, RdnaOpcode::BufferLoadFormatD16Xy, 2, 32, false, false, true},
|
||||
{0x82u, RdnaOpcode::BufferLoadFormatD16Xyz, 3, 32, false, false, true},
|
||||
|
||||
@@ -699,6 +699,12 @@ bool IsBufferMemoryOpcode(RdnaOpcode opcode) {
|
||||
case RdnaOpcode::BufferAtomicUmaxX2:
|
||||
case RdnaOpcode::BufferAtomicAndX2:
|
||||
case RdnaOpcode::BufferAtomicXorX2:
|
||||
case RdnaOpcode::BufferAtomicFcmpswap:
|
||||
case RdnaOpcode::BufferAtomicFcmpswapX2:
|
||||
case RdnaOpcode::BufferAtomicFminX2:
|
||||
case RdnaOpcode::BufferAtomicFmaxX2:
|
||||
case RdnaOpcode::BufferAtomicIncX2:
|
||||
case RdnaOpcode::BufferAtomicDecX2:
|
||||
case RdnaOpcode::BufferAtomicXor:
|
||||
case RdnaOpcode::BufferAtomicFmin:
|
||||
case RdnaOpcode::BufferAtomicFmax:
|
||||
|
||||
@@ -110,7 +110,6 @@ std::uint32_t EmitD16FormatComponent(SpirvEmitterState& state, const SpirvBuffer
|
||||
std::uint32_t EmitFormatStoreComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t data);
|
||||
std::uint32_t EmitD16StoreComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t half);
|
||||
void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state);
|
||||
std::uint32_t EmitFloatAtomicReplacement(SpirvEmitterState& state, std::uint32_t old, std::uint32_t source, bool maxValue);
|
||||
std::uint32_t EmitDsSwizzleTargetLane(SpirvEmitterState& state, std::uint32_t subid, std::uint32_t control);
|
||||
std::uint32_t EmitAndConstant(SpirvEmitterState& state, std::uint32_t value, std::uint32_t mask);
|
||||
std::uint32_t EmitShiftRightConstant(SpirvEmitterState& state, std::uint32_t value, std::uint32_t shift);
|
||||
@@ -214,7 +213,7 @@ std::uint32_t EmitValueIfElse(SpirvEmitterState& state, std::uint32_t condition,
|
||||
}
|
||||
|
||||
template<typename TFunction>
|
||||
std::uint32_t AtomicUpdate(SpirvEmitterState& state, std::uint32_t pointer, ResourceKind kind, TFunction&& function) {
|
||||
std::uint32_t AtomicUpdateTyped(SpirvEmitterState& state, std::uint32_t pointer, ResourceKind kind, std::uint32_t type, TFunction&& function) {
|
||||
const auto scope = kind == ResourceKind::Lds ? spv::ScopeWorkgroup : spv::ScopeDevice;
|
||||
const auto memory = [&]() {
|
||||
switch (kind) {
|
||||
@@ -235,12 +234,12 @@ std::uint32_t AtomicUpdate(SpirvEmitterState& state, std::uint32_t pointer, Reso
|
||||
const auto exchanged = state.module.AllocateId();
|
||||
state.module.AddFunction(spv::OpBranch, preheader);
|
||||
EmitLabel(state, preheader);
|
||||
state.module.AddFunction(spv::OpAtomicLoad, TypeU32(state), initial, pointer, ConstantU32(state, scope), ConstantU32(state, spv::MemorySemanticsMaskNone));
|
||||
state.module.AddFunction(spv::OpAtomicLoad, type, initial, pointer, ConstantU32(state, scope), ConstantU32(state, spv::MemorySemanticsMaskNone));
|
||||
state.module.AddFunction(spv::OpBranch, header);
|
||||
EmitLabel(state, header);
|
||||
state.module.AddFunction(spv::OpPhi, TypeU32(state), observed, initial, preheader, exchanged, cont);
|
||||
state.module.AddFunction(spv::OpPhi, type, observed, initial, preheader, exchanged, cont);
|
||||
const auto next = function(observed);
|
||||
state.module.AddFunction(spv::OpAtomicCompareExchange, TypeU32(state), exchanged, pointer, ConstantU32(state, scope), ConstantU32(state, spv::MemorySemanticsMaskNone), ConstantU32(state, spv::MemorySemanticsMaskNone), next, observed);
|
||||
state.module.AddFunction(spv::OpAtomicCompareExchange, type, exchanged, pointer, ConstantU32(state, scope), ConstantU32(state, spv::MemorySemanticsMaskNone), ConstantU32(state, spv::MemorySemanticsMaskNone), next, observed);
|
||||
const auto success = state.module.AllocateId();
|
||||
state.module.AddFunction(spv::OpIEqual, TypeBool(state), success, exchanged, observed);
|
||||
state.module.AddFunction(spv::OpLoopMerge, merge, cont, spv::LoopControlMaskNone);
|
||||
@@ -252,6 +251,11 @@ std::uint32_t AtomicUpdate(SpirvEmitterState& state, std::uint32_t pointer, Reso
|
||||
return observed;
|
||||
}
|
||||
|
||||
template<typename TFunction>
|
||||
std::uint32_t AtomicUpdate(SpirvEmitterState& state, std::uint32_t pointer, ResourceKind kind, TFunction&& function) {
|
||||
return AtomicUpdateTyped(state, pointer, kind, TypeU32(state), std::forward<TFunction>(function));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -272,6 +272,12 @@ std::uint32_t EmitBufferAtomicUMax64(SpirvValueEmitContext& ctx, const IrValue&
|
||||
std::uint32_t EmitBufferAtomicAnd64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicXor64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicCmpSwap64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicFCmpSwap32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicFCmpSwap64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicFMin64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicFMax64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicInc64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicDec64(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicXor32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicFMin32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
std::uint32_t EmitBufferAtomicFMax32(SpirvValueEmitContext& ctx, const IrValue& inst);
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
namespace ShaderRecompiler {
|
||||
|
||||
void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state);
|
||||
std::uint32_t EmitFloatAtomicReplacement(SpirvEmitterState& state, std::uint32_t old, std::uint32_t source, bool maxValue);
|
||||
std::uint32_t EmitShaderDataDwordLoad(SpirvEmitterState& state, std::uint32_t dwordIndex);
|
||||
|
||||
}
|
||||
|
||||
-1
@@ -12,7 +12,6 @@ bool IsSignedFormatComponent(SpirvFormatComponentType type);
|
||||
std::uint32_t EmitUFloatToF32Bits(SpirvEmitterState& state, std::uint32_t raw, std::uint32_t bits);
|
||||
std::uint32_t NormalizeFormatComponent(SpirvEmitterState& state, const SpirvBufferFormatInfo& info, std::uint32_t component, std::uint32_t raw);
|
||||
void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state);
|
||||
std::uint32_t EmitFloatAtomicReplacement(SpirvEmitterState& state, std::uint32_t old, std::uint32_t source, bool maxValue);
|
||||
std::uint32_t EmitDsSwizzleTargetLane(SpirvEmitterState& state, std::uint32_t subid, std::uint32_t control);
|
||||
|
||||
}
|
||||
|
||||
@@ -497,6 +497,12 @@ void EmitDirectInstruction(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
case IrOpcode::BufferAtomicAnd64: return Invoke(EmitBufferAtomicAnd64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicXor64: return Invoke(EmitBufferAtomicXor64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicCmpSwap64: return Invoke(EmitBufferAtomicCmpSwap64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicFCmpSwap32: return Invoke(EmitBufferAtomicFCmpSwap32, ctx, inst);
|
||||
case IrOpcode::BufferAtomicFCmpSwap64: return Invoke(EmitBufferAtomicFCmpSwap64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicFMin64: return Invoke(EmitBufferAtomicFMin64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicFMax64: return Invoke(EmitBufferAtomicFMax64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicInc64: return Invoke(EmitBufferAtomicInc64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicDec64: return Invoke(EmitBufferAtomicDec64, ctx, inst);
|
||||
case IrOpcode::BufferAtomicXor32: return Invoke(EmitBufferAtomicXor32, ctx, inst);
|
||||
case IrOpcode::BufferAtomicFMin32: return Invoke(EmitBufferAtomicFMin32, ctx, inst);
|
||||
case IrOpcode::BufferAtomicFMax32: return Invoke(EmitBufferAtomicFMax32, ctx, inst);
|
||||
|
||||
@@ -336,28 +336,6 @@ void EmitDeviceAtomicMemoryBarrier(SpirvEmitterState& state) {
|
||||
state.module.AddFunction(spv::OpMemoryBarrier, ConstantU32(state, spv::ScopeDevice), ConstantU32(state, semantics));
|
||||
}
|
||||
|
||||
std::uint32_t EmitFloatAtomicReplacement(SpirvEmitterState& state, std::uint32_t old, std::uint32_t source, bool maxValue) {
|
||||
struct OrderedBits {
|
||||
std::uint32_t nan;
|
||||
std::uint32_t zero;
|
||||
std::uint32_t key;
|
||||
};
|
||||
const auto classify = [&](std::uint32_t bits) {
|
||||
const auto cls = EmitClassifyF32Bits(state, bits);
|
||||
const auto negative = EmitCompareU32Constant(state, spv::OpINotEqual, EmitAndConstant(state, bits, 0x80000000u), 0u);
|
||||
const auto negativeKey = state.module.AllocateId();
|
||||
state.module.AddFunction(spv::OpNot, TypeU32(state), negativeKey, bits);
|
||||
const auto positiveKey = EmitBinaryU32(state, spv::OpBitwiseXor, bits, ConstantU32(state, 0x80000000u));
|
||||
return OrderedBits{cls.nan, cls.zero, EmitSelectValueU32(state, negative, negativeKey, positiveKey)};
|
||||
};
|
||||
const auto sourceClass = classify(source);
|
||||
const auto oldClass = classify(old);
|
||||
const auto unordered = EmitLogicalOrBool(state, EmitLogicalOrBool(state, sourceClass.nan, oldClass.nan), EmitLogicalAndBool(state, sourceClass.zero, oldClass.zero));
|
||||
const auto compare = state.module.AllocateId();
|
||||
state.module.AddFunction(maxValue ? spv::OpUGreaterThan : spv::OpULessThan, TypeBool(state), compare, sourceClass.key, oldClass.key);
|
||||
return EmitSelectValueU32(state, EmitLogicalAndBool(state, EmitLogicalNotBool(state, unordered), compare), source, old);
|
||||
}
|
||||
|
||||
std::uint32_t EmitDsSwizzleTargetLane(SpirvEmitterState& state, std::uint32_t subid, std::uint32_t control) {
|
||||
if ((control & 0xc000u) == 0xc000u) {
|
||||
const std::uint32_t mask = control & 0x1fu;
|
||||
|
||||
@@ -949,12 +949,72 @@ std::uint32_t BufferAtomic64(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
});
|
||||
}
|
||||
|
||||
struct AtomicFloatBits {
|
||||
SpirvEmitterState& state;
|
||||
bool wide;
|
||||
std::uint32_t type() const { return wide ? TypeScalarU64(state) : TypeU32(state); }
|
||||
std::uint32_t constant(std::uint64_t value) const {
|
||||
return wide ? state.module.Constant(spv::OpConstant, TypeScalarU64(state), static_cast<std::uint32_t>(value), static_cast<std::uint32_t>(value >> 32u)) : ConstantU32(state, static_cast<std::uint32_t>(value));
|
||||
}
|
||||
std::uint64_t sign() const { return wide ? 0x8000000000000000ull : 0x80000000ull; }
|
||||
std::uint64_t quiet() const { return wide ? 0x0008000000000000ull : 0x00400000ull; }
|
||||
std::uint64_t infinity() const { return wide ? 0x7ff0000000000000ull : 0x7f800000ull; }
|
||||
std::uint32_t op(spv::Op opcode, std::uint32_t lhs, std::uint32_t rhs) const { return Binary(state, opcode, type(), lhs, rhs); }
|
||||
std::uint32_t test(spv::Op opcode, std::uint32_t lhs, std::uint32_t rhs) const { return Binary(state, opcode, TypeBool(state), lhs, rhs); }
|
||||
std::uint32_t select(std::uint32_t condition, std::uint32_t lhs, std::uint32_t rhs) const { return Select(state, type(), condition, lhs, rhs); }
|
||||
std::uint32_t magnitude(std::uint32_t value) const { return op(spv::OpBitwiseAnd, value, constant(sign() - 1u)); }
|
||||
std::uint32_t nan(std::uint32_t value) const { return test(spv::OpUGreaterThan, magnitude(value), constant(infinity())); }
|
||||
std::uint32_t signalingNan(std::uint32_t value) const {
|
||||
return Binary(state, spv::OpLogicalAnd, TypeBool(state), nan(value), test(spv::OpIEqual, op(spv::OpBitwiseAnd, value, constant(quiet())), constant(0u)));
|
||||
}
|
||||
std::uint32_t orderKey(std::uint32_t value) const {
|
||||
const auto negative = test(spv::OpINotEqual, op(spv::OpBitwiseAnd, value, constant(sign())), constant(0u));
|
||||
return select(negative, Unary(state, spv::OpNot, type(), value), op(spv::OpBitwiseOr, value, constant(sign())));
|
||||
}
|
||||
std::uint32_t equal(std::uint32_t lhs, std::uint32_t rhs) const {
|
||||
const auto same = Binary(state, spv::OpLogicalAnd, TypeBool(state), test(spv::OpIEqual, lhs, rhs), Unary(state, spv::OpLogicalNot, TypeBool(state), nan(lhs)));
|
||||
const auto zeros = test(spv::OpIEqual, magnitude(op(spv::OpBitwiseOr, lhs, rhs)), constant(0u));
|
||||
return Binary(state, spv::OpLogicalOr, TypeBool(state), same, zeros);
|
||||
}
|
||||
std::uint32_t minMax(std::uint32_t old, std::uint32_t source, bool maxValue) const {
|
||||
const auto better = test(maxValue ? spv::OpUGreaterThan : spv::OpULessThan, orderKey(source), orderKey(old));
|
||||
auto result = select(better, source, old);
|
||||
result = select(nan(old), source, result);
|
||||
result = select(nan(source), old, result);
|
||||
result = select(Binary(state, spv::OpLogicalAnd, TypeBool(state), nan(source), nan(old)), source, result);
|
||||
result = select(signalingNan(old), op(spv::OpBitwiseOr, old, constant(quiet())), result);
|
||||
return select(signalingNan(source), op(spv::OpBitwiseOr, source, constant(quiet())), result);
|
||||
}
|
||||
};
|
||||
|
||||
std::uint32_t BufferFloatAtomic(SpirvValueEmitContext& ctx, const IrValue& inst, bool maxValue) {
|
||||
return EmitAtomicUpdate(ctx, inst, BufferMemory(ctx, inst), [maxValue](SpirvEmitterState& state, std::uint32_t old, std::uint32_t value) {
|
||||
return EmitFloatAtomicReplacement(state, old, value, maxValue);
|
||||
return AtomicFloatBits{state, false}.minMax(old, value, maxValue);
|
||||
});
|
||||
}
|
||||
|
||||
template<typename TReplacement>
|
||||
std::uint32_t BufferAtomic64Update(SpirvValueEmitContext& ctx, const IrValue& inst, TReplacement&& replacement) {
|
||||
auto& state = ctx.state;
|
||||
const auto& mem = BufferMemory(ctx, inst);
|
||||
return EmitValueOrDefaultIfCondition(state, ActiveArgument(ctx, inst), TypeU64(state), ConstantU64(state, 0u), [&]() {
|
||||
const auto resource = PrepareStorageBufferResourceAccess(state, mem, state.storageBufferU64Variable, TypeStorageBufferU64Pointer(state));
|
||||
const auto byteAddress = Binary(state, spv::OpIAdd, TypeU32(state), ByteAddress(ctx, inst, mem), resource.byteOffset);
|
||||
const auto index = Binary(state, spv::OpShiftRightLogical, TypeU32(state), byteAddress, ConstantU32(state, 3u));
|
||||
return EmitValueOrDefaultIfCondition(state, EmitMemoryElementInBounds(state, resource, index), TypeU64(state), ConstantU64(state, 0u), [&]() {
|
||||
const auto pointer = EmitStorageBufferElementPointer(state, resource, index, TypeStorageBufferU64ElementPointer(state));
|
||||
const auto old = AtomicUpdateTyped(state, pointer, ResourceKind::Buffer, TypeScalarU64(state), [&](std::uint32_t observed) {
|
||||
return replacement(observed);
|
||||
});
|
||||
return Unary(state, spv::OpBitcast, TypeU64(state), old);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
std::uint32_t ScalarU64Argument(SpirvValueEmitContext& ctx, const IrValue& inst, std::uint32_t fromEnd) {
|
||||
return Unary(ctx.state, spv::OpBitcast, TypeScalarU64(ctx.state), ctx.Arg(inst, inst.ArgumentCount() - fromEnd));
|
||||
}
|
||||
|
||||
template<typename TReplacement>
|
||||
std::uint32_t SharedAtomicUpdate(SpirvValueEmitContext& ctx, const IrValue& inst, TReplacement&& replacement) {
|
||||
auto& state = ctx.state;
|
||||
@@ -1474,4 +1534,52 @@ std::uint32_t EmitBufferAtomicCmpSwap64(SpirvValueEmitContext& ctx, const IrValu
|
||||
return BufferAtomic64(ctx, inst);
|
||||
}
|
||||
|
||||
std::uint32_t EmitBufferAtomicFCmpSwap32(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
const auto desired = ctx.Arg(inst, inst.ArgumentCount() - 3u);
|
||||
const auto comparator = ctx.Arg(inst, inst.ArgumentCount() - 2u);
|
||||
const auto& mem = BufferMemory(ctx, inst);
|
||||
return EmitAtomicAccess(ctx, inst, mem, [&](std::uint32_t pointer) {
|
||||
return AtomicUpdate(ctx.state, pointer, mem.kind, [&](std::uint32_t old) {
|
||||
const AtomicFloatBits bits{ctx.state, false};
|
||||
return bits.select(bits.equal(old, comparator), desired, old);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
std::uint32_t EmitBufferAtomicFCmpSwap64(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
const auto desired = ScalarU64Argument(ctx, inst, 3u);
|
||||
const auto comparator = ScalarU64Argument(ctx, inst, 2u);
|
||||
return BufferAtomic64Update(ctx, inst, [&](std::uint32_t old) {
|
||||
const AtomicFloatBits bits{ctx.state, true};
|
||||
return bits.select(bits.equal(old, comparator), desired, old);
|
||||
});
|
||||
}
|
||||
|
||||
std::uint32_t EmitBufferAtomicFMin64(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
const auto value = ScalarU64Argument(ctx, inst, 2u);
|
||||
return BufferAtomic64Update(ctx, inst, [&](std::uint32_t old) { return AtomicFloatBits{ctx.state, true}.minMax(old, value, false); });
|
||||
}
|
||||
|
||||
std::uint32_t EmitBufferAtomicFMax64(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
const auto value = ScalarU64Argument(ctx, inst, 2u);
|
||||
return BufferAtomic64Update(ctx, inst, [&](std::uint32_t old) { return AtomicFloatBits{ctx.state, true}.minMax(old, value, true); });
|
||||
}
|
||||
|
||||
std::uint32_t EmitBufferAtomicInc64(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
const auto limit = ScalarU64Argument(ctx, inst, 2u);
|
||||
return BufferAtomic64Update(ctx, inst, [&](std::uint32_t old) {
|
||||
const AtomicFloatBits bits{ctx.state, true};
|
||||
return bits.select(bits.test(spv::OpUGreaterThanEqual, old, limit), bits.constant(0u), bits.op(spv::OpIAdd, old, bits.constant(1u)));
|
||||
});
|
||||
}
|
||||
|
||||
std::uint32_t EmitBufferAtomicDec64(SpirvValueEmitContext& ctx, const IrValue& inst) {
|
||||
const auto limit = ScalarU64Argument(ctx, inst, 2u);
|
||||
return BufferAtomic64Update(ctx, inst, [&](std::uint32_t old) {
|
||||
const AtomicFloatBits bits{ctx.state, true};
|
||||
const auto wrap = Binary(ctx.state, spv::OpLogicalOr, TypeBool(ctx.state), bits.test(spv::OpIEqual, old, bits.constant(0u)), bits.test(spv::OpUGreaterThan, old, limit));
|
||||
return bits.select(wrap, limit, bits.op(spv::OpISub, old, bits.constant(1u)));
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -209,11 +209,11 @@ bool TranslationContext::bufferAtomic(const RdnaInstruction& inst, IrOpcode opco
|
||||
const MemoryFlags flags = addMemoryInfo(memory, inst.programCounter);
|
||||
IrValue& exec = ir.GetExec();
|
||||
IrValue* result;
|
||||
if (opcode == IrOpcode::BufferAtomicCmpSwap32) {
|
||||
if (opcode == IrOpcode::BufferAtomicCmpSwap32 || opcode == IrOpcode::BufferAtomicFCmpSwap32) {
|
||||
const IrU32 desired = readU32(inst.destination);
|
||||
const IrU32 comparator = readU32(offsetOperand(inst.destination, 1u));
|
||||
result = &ir.Emit(opcode, IrOpcodeType(opcode), {resource, &address.index.Value(), &address.offset.Value(), &address.soffset.Value(), &desired.Value(), &comparator.Value(), &exec}, flags);
|
||||
} else if (opcode == IrOpcode::BufferAtomicCmpSwap64) {
|
||||
} else if (opcode == IrOpcode::BufferAtomicCmpSwap64 || opcode == IrOpcode::BufferAtomicFCmpSwap64) {
|
||||
const IrU64 desired = readU64(inst.destination);
|
||||
const IrU64 comparator = readU64(offsetOperand(inst.destination, 2u));
|
||||
result = &ir.Emit(opcode, IrOpcodeType(opcode), {resource, &address.index.Value(), &address.offset.Value(), &address.soffset.Value(), &desired.Value(), &comparator.Value(), &exec}, flags);
|
||||
|
||||
+12
@@ -153,6 +153,18 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) {
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicAnd64);
|
||||
case RdnaOpcode::BufferAtomicXorX2:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicXor64);
|
||||
case RdnaOpcode::BufferAtomicFcmpswap:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicFCmpSwap32);
|
||||
case RdnaOpcode::BufferAtomicFcmpswapX2:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicFCmpSwap64);
|
||||
case RdnaOpcode::BufferAtomicFminX2:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicFMin64);
|
||||
case RdnaOpcode::BufferAtomicFmaxX2:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicFMax64);
|
||||
case RdnaOpcode::BufferAtomicIncX2:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicInc64);
|
||||
case RdnaOpcode::BufferAtomicDecX2:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicDec64);
|
||||
case RdnaOpcode::BufferAtomicXor:
|
||||
return bufferAtomic(inst, IrOpcode::BufferAtomicXor32);
|
||||
case RdnaOpcode::BufferAtomicFmin:
|
||||
|
||||
Reference in New Issue
Block a user