mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #954 from CnRJay/feat/ds-wrxchg2-rtn-b64
feat(shader): implement ds_wrxchg2_rtn_b64 and ds_wrxchg2st64_rtn_b64
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
#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 <cstdint>
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
using AgcDriver::Graphics::Require;
|
||||
using ShaderRecompiler::ShaderStage;
|
||||
|
||||
constexpr std::uint32_t Threads = 32;
|
||||
constexpr std::uint32_t Inputs = 8;
|
||||
constexpr std::uint32_t Results = 16;
|
||||
constexpr std::uint32_t LdsDwords = 3072;
|
||||
alignas(256) std::array<std::uint32_t, Threads * Inputs> Input{};
|
||||
alignas(256) std::array<std::uint32_t, Threads * Results> Output{};
|
||||
|
||||
alignas(256) constexpr auto Code = std::to_array<std::uint32_t>({
|
||||
0x34020085, 0xe0381000, 0x80000201, 0xe0381010, 0x80000601,
|
||||
0x345c0085, 0x4a5c5cff, 0x00000480, 0x345e0083, 0x4a5e5eff, 0x00000880,
|
||||
0xbf8c3f70,
|
||||
0xd9380301, 0x0004022e, 0xd9b80301, 0x1208062e, 0xd9dc0301, 0x1600002e,
|
||||
0xd93c0100, 0x0004022f, 0xd9bc0100, 0x1a08062f, 0xd9e00100, 0x1e00002f,
|
||||
0x34020086,
|
||||
0xbf8cc07f,
|
||||
0xe0781000, 0x80011201, 0xe0781010, 0x80011601, 0xe0781020, 0x80011a01, 0xe0781030, 0x80011e01,
|
||||
0xbf810000,
|
||||
});
|
||||
|
||||
alignas(256) constexpr auto CoincidingOffsets = std::to_array<std::uint32_t>({0xd9b80101, 0x1208062e, 0xbf810000});
|
||||
alignas(256) constexpr auto CoincidingOffsetsSt64 = std::to_array<std::uint32_t>({0xd9bc0000, 0x1a08062f, 0xbf810000});
|
||||
alignas(256) constexpr auto DestinationOverflow = std::to_array<std::uint32_t>({0xd9b80301, 0xfd08062e, 0xbf810000});
|
||||
|
||||
void Fill(std::uint32_t tid, std::uint32_t* words) {
|
||||
std::uint64_t seed = (tid + 1u) * 0x9e3779b97f4a7c15ull;
|
||||
for (std::uint32_t i = 0; i < Inputs; ++i) {
|
||||
seed = seed * 0xbf58476d1ce4e5b9ull + 0x94d049bb133111ebull;
|
||||
words[i] = static_cast<std::uint32_t>(seed >> 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;
|
||||
}
|
||||
|
||||
template<typename TUse>
|
||||
void Translate(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, TUse&& use) {
|
||||
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::array<ShaderRecompiler::MemoryRegion, 1> memory{{{reinterpret_cast<std::uintptr_t>(code.data()), std::as_bytes(code)}}};
|
||||
const ShaderRecompiler::ShaderComputeStageInfo compute{{Threads, 1, 1}, LdsDwords, {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;
|
||||
use(ShaderRecompiler::Recompile(request));
|
||||
}
|
||||
|
||||
void Run(AgcDriver::VulkanDevice& device) {
|
||||
for (std::uint32_t tid = 0; tid < Threads; ++tid) Fill(tid, &Input[tid * Inputs]);
|
||||
Output.fill(0xdeadbeefu);
|
||||
Translate(device, Code, [&](const auto& result) {
|
||||
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(Code.data()));
|
||||
device.WaitIdle();
|
||||
});
|
||||
}
|
||||
|
||||
void Reject(AgcDriver::VulkanDevice& device, std::span<const std::uint32_t> code, const std::string& reason) {
|
||||
std::string error;
|
||||
try {
|
||||
Translate(device, code, [](const auto&) {});
|
||||
} catch (const std::exception& exception) {
|
||||
error = exception.what();
|
||||
}
|
||||
Require(error.find(reason) != std::string::npos, "ds wrxchg2 b64: expected a rejection for \"" + reason + "\", got \"" + error + "\"");
|
||||
}
|
||||
|
||||
void CheckRejections(AgcDriver::VulkanDevice& device) {
|
||||
Reject(device, CoincidingOffsets, "DS write exchange of one location through both offsets is not supported");
|
||||
Reject(device, CoincidingOffsetsSt64, "DS write exchange of one location through both offsets is not supported");
|
||||
Reject(device, DestinationOverflow, "DS write exchange destination register range overflow");
|
||||
}
|
||||
|
||||
void Check() {
|
||||
constexpr std::array<const char*, 2> names{"ds_wrxchg2_rtn_b64", "ds_wrxchg2st64_rtn_b64"};
|
||||
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 op = 0; op < 2u; ++op) {
|
||||
for (std::uint32_t i = 0; i < 4u; ++i) {
|
||||
const auto returned = out[op * 8u + i];
|
||||
const auto stored = out[op * 8u + 4u + i];
|
||||
Require(returned == in[i], std::string("ds wrxchg2 b64: lane ") + std::to_string(tid) + " " + names[op] + " returned dword " + std::to_string(i) + " is " + Hex(returned) + ", expected " + Hex(in[i]));
|
||||
Require(stored == in[4u + i], std::string("ds wrxchg2 b64: lane ") + std::to_string(tid) + " " + names[op] + " stored dword " + std::to_string(i) + " is " + Hex(stored) + ", expected " + Hex(in[4u + i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
try {
|
||||
const auto device = OpenVulkanTestDevice();
|
||||
if (!device) return VulkanTestSkipped;
|
||||
Run(*device);
|
||||
Check();
|
||||
CheckRejections(*device);
|
||||
std::puts("ds wrxchg2 b64 tests passed");
|
||||
return 0;
|
||||
} catch (const std::exception& error) {
|
||||
std::cerr << error.what() << '\n';
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -861,6 +861,8 @@ enum class RdnaOpcode : std::uint16_t {
|
||||
DsWrxchgRtnB32,
|
||||
DsWrxchg2RtnB32,
|
||||
DsWrxchg2st64RtnB32,
|
||||
DsWrxchg2RtnB64,
|
||||
DsWrxchg2st64RtnB64,
|
||||
DsMinF32,
|
||||
DsMaxF32,
|
||||
DsSwizzleB32,
|
||||
|
||||
@@ -319,6 +319,8 @@ constexpr MemoryOpcodeInfo dsOpcodes[] = {
|
||||
{0x4du, RdnaOpcode::DsWriteB64, 2, 32, false, false, false},
|
||||
{0x4eu, RdnaOpcode::DsWrite2B64, 4, 32, false, false, false},
|
||||
{0x4fu, RdnaOpcode::DsWrite2st64B64, 4, 32, false, false, false},
|
||||
{0x6eu, RdnaOpcode::DsWrxchg2RtnB64, 4, 32, false, false, false},
|
||||
{0x6fu, RdnaOpcode::DsWrxchg2st64RtnB64, 4, 32, false, false, false},
|
||||
{0x76u, RdnaOpcode::DsReadB64, 2, 32, false, false, false},
|
||||
{0x77u, RdnaOpcode::DsRead2B64, 4, 32, false, false, false},
|
||||
{0x78u, RdnaOpcode::DsRead2st64B64, 4, 32, false, false, false},
|
||||
@@ -535,6 +537,8 @@ std::uint32_t dsSourceCount(RdnaOpcode opcode) {
|
||||
switch (opcode) {
|
||||
case RdnaOpcode::DsWrxchg2RtnB32:
|
||||
case RdnaOpcode::DsWrxchg2st64RtnB32:
|
||||
case RdnaOpcode::DsWrxchg2RtnB64:
|
||||
case RdnaOpcode::DsWrxchg2st64RtnB64:
|
||||
case RdnaOpcode::DsWrite2B32:
|
||||
case RdnaOpcode::DsWrite2st64B32:
|
||||
case RdnaOpcode::DsWrite2B64:
|
||||
@@ -903,11 +907,11 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span<const std::
|
||||
if (gds && (info.opcode == RdnaOpcode::DsSwizzleB32 || info.opcode == RdnaOpcode::DsBpermuteB32 || info.opcode == RdnaOpcode::DsPermuteB32 || info.opcode == RdnaOpcode::DsWriteAddtidB32 || info.opcode == RdnaOpcode::DsReadAddtidB32)) {
|
||||
throw std::runtime_error("DS lane operation is available only for LDS");
|
||||
}
|
||||
if (info.opcode == RdnaOpcode::DsWrxchg2RtnB32 || info.opcode == RdnaOpcode::DsWrxchg2st64RtnB32) {
|
||||
if (info.opcode == RdnaOpcode::DsWrxchg2RtnB32 || info.opcode == RdnaOpcode::DsWrxchg2st64RtnB32 || info.opcode == RdnaOpcode::DsWrxchg2RtnB64 || info.opcode == RdnaOpcode::DsWrxchg2st64RtnB64) {
|
||||
if (offset0 == offset1) {
|
||||
throw std::runtime_error("DS write exchange of one location through both offsets is not supported");
|
||||
}
|
||||
if (vdst == 255u) {
|
||||
if (vdst + info.dataDwords > 256u) {
|
||||
throw std::runtime_error("DS write exchange destination register range overflow");
|
||||
}
|
||||
}
|
||||
@@ -933,10 +937,10 @@ RdnaInstruction DecodeRdnaDs(std::uint32_t programCounter, std::span<const std::
|
||||
} else if (instruction.op == RdnaOpcode::DsWrite2st64B32 || instruction.op == RdnaOpcode::DsRead2st64B32 || instruction.op == RdnaOpcode::DsWrxchg2st64RtnB32) {
|
||||
instruction.memoryOffset = offset0 * 256u;
|
||||
instruction.secondaryOffset = offset1 * 256u;
|
||||
} else if (instruction.op == RdnaOpcode::DsWrite2B64 || instruction.op == RdnaOpcode::DsRead2B64) {
|
||||
} else if (instruction.op == RdnaOpcode::DsWrite2B64 || instruction.op == RdnaOpcode::DsRead2B64 || instruction.op == RdnaOpcode::DsWrxchg2RtnB64) {
|
||||
instruction.memoryOffset = offset0 * 8u;
|
||||
instruction.secondaryOffset = offset1 * 8u;
|
||||
} else if (instruction.op == RdnaOpcode::DsWrite2st64B64 || instruction.op == RdnaOpcode::DsRead2st64B64) {
|
||||
} else if (instruction.op == RdnaOpcode::DsWrite2st64B64 || instruction.op == RdnaOpcode::DsRead2st64B64 || instruction.op == RdnaOpcode::DsWrxchg2st64RtnB64) {
|
||||
instruction.memoryOffset = offset0 * 512u;
|
||||
instruction.secondaryOffset = offset1 * 512u;
|
||||
}
|
||||
|
||||
@@ -167,7 +167,9 @@ void includeInstructionVectorRegisters(const RdnaInstruction& instruction, std::
|
||||
case RdnaOpcode::DsWrite2B32:
|
||||
case RdnaOpcode::DsWrite2st64B32:
|
||||
case RdnaOpcode::DsWrite2B64:
|
||||
case RdnaOpcode::DsWrite2st64B64: {
|
||||
case RdnaOpcode::DsWrite2st64B64:
|
||||
case RdnaOpcode::DsWrxchg2RtnB64:
|
||||
case RdnaOpcode::DsWrxchg2st64RtnB64: {
|
||||
const std::uint32_t width = std::max(instruction.dataDwordCount / 2u, 1u);
|
||||
includeVector(instruction.source1, width);
|
||||
includeVector(instruction.source2, width);
|
||||
|
||||
+2
@@ -475,6 +475,8 @@ bool TranslationContext::emitMemory(const RdnaInstruction& inst) {
|
||||
return dsWrite2(inst);
|
||||
case RdnaOpcode::DsWrxchg2RtnB32:
|
||||
case RdnaOpcode::DsWrxchg2st64RtnB32:
|
||||
case RdnaOpcode::DsWrxchg2RtnB64:
|
||||
case RdnaOpcode::DsWrxchg2st64RtnB64:
|
||||
return dsWrxchg2(inst);
|
||||
case RdnaOpcode::DsWriteB8:
|
||||
case RdnaOpcode::DsWriteB16:
|
||||
|
||||
+13
-8
@@ -44,19 +44,24 @@ bool TranslationContext::dsAtomic(const RdnaInstruction& inst, IrOpcode opcode,
|
||||
|
||||
bool TranslationContext::dsWrxchg2(const RdnaInstruction& inst) {
|
||||
MemoryInfo first = sharedMemoryInfoFromInstruction(inst);
|
||||
first.dataDwords = 1u;
|
||||
first.componentCount = 1u;
|
||||
const std::uint32_t width = first.dataDwords / 2u;
|
||||
if (width == 2u && inst.gds) {
|
||||
throw std::runtime_error("64-bit GDS atomics are not supported");
|
||||
}
|
||||
first.dataDwords = width;
|
||||
first.componentCount = width;
|
||||
MemoryInfo second = first;
|
||||
second.offset = first.secondaryOffset;
|
||||
const IrU32 address = readU32(inst.source0);
|
||||
const IrU32 firstValue = readU32(inst.source1);
|
||||
const IrU32 secondValue = readU32(inst.source2);
|
||||
const auto data = [&](const RdnaOperand& operand) { return width == 2u ? &readU64(operand).Value() : &readU32(operand).Value(); };
|
||||
IrValue* firstValue = data(inst.source1);
|
||||
IrValue* secondValue = data(inst.source2);
|
||||
IrValue& active = ir.GetExec();
|
||||
const IrOpcode opcode = IrOpcode::SharedAtomicSwap32;
|
||||
IrValue& firstOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), &firstValue.Value(), &active}, addMemoryInfo(first, inst.programCounter));
|
||||
IrValue& secondOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), &secondValue.Value(), &active}, addMemoryInfo(second, inst.programCounter));
|
||||
const IrOpcode opcode = width == 2u ? IrOpcode::SharedAtomicSwap64 : IrOpcode::SharedAtomicSwap32;
|
||||
IrValue& firstOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), firstValue, &active}, addMemoryInfo(first, inst.programCounter));
|
||||
IrValue& secondOld = ir.Emit(opcode, IrOpcodeType(opcode), {&address.Value(), secondValue, &active}, addMemoryInfo(second, inst.programCounter));
|
||||
writeOperand(inst.destination, &firstOld);
|
||||
writeOperand(offsetOperand(inst.destination, 1u), &secondOld);
|
||||
writeOperand(offsetOperand(inst.destination, width), &secondOld);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -192,7 +192,7 @@ Throughout the project, every function at every stage either **does exactly what
|
||||
- [Unnormalized S#s](../../core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp) are accepted only when direct and used only by explicit-LOD `image_sample` without offset, comparison, derivatives or `_a`, on a single-level, single-layer 1D or 2D view of mip 0, even when the image is also sampled through a normalized S#.
|
||||
- An [unnormalized S#](../../core/libs/prx/libSceAgcDriver/Graphics/src/GuestSamplerResource.cpp) (libSceAgcDriver) throws with unequal minification and magnification filters, anisotropy, `TRUNC_COORD`, `MC_COORD_TRUNC` or X/Y clamping other than last-texel or border.
|
||||
- [Image atomics](../../core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp) on 32_SINT and 32_FLOAT surfaces act on the raw dword through an R32_UINT storage view; their float results are assumed to match those measured on 32_UINT surfaces. Other formats throw.
|
||||
- [`ds_wrxchg2_rtn_b32` and `ds_wrxchg2st64_rtn_b32`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit atomic swaps per invocation. RDNA2 hardware (measured on gfx1035) performs every lane's first exchange before any lane's second one, so when lanes of the same wave or of different waves overlap addresses, the result can differ from hardware. `offset0 == offset1` throws, although on hardware it is two exchanges in order.
|
||||
- [`ds_wrxchg2_rtn_b32`, `ds_wrxchg2st64_rtn_b32`, `ds_wrxchg2_rtn_b64` and `ds_wrxchg2st64_rtn_b64`](../../core/shader/recompiler/Translation/src/MemoryInstructions/SharedMemoryInstructions.cpp) are translated as two separate 32-bit or 64-bit atomic swaps per invocation. RDNA2 hardware (measured on gfx1035) performs every lane's first exchange before any lane's second one, so when lanes of the same wave or of different waves overlap addresses, the result can differ from hardware. `offset0 == offset1` throws, although on hardware it is two exchanges in order.
|
||||
- [64-bit image atomics](../../core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp) (`image_atomic_*` with a two-dword data mask) need `VK_EXT_shader_image_atomic_int64` and act on 32_32 UINT, SINT and FLOAT surfaces through an R64_UINT storage view. 64-bit `image_atomic_inc`, `image_atomic_dec` and the float atomics throw, as does any other access in the same shader to an image it updates with 64-bit atomics.
|
||||
- The shader recompiler [ignores `s_setreg_b32`](../../core/shader/recompiler/Translation/src/ScalarInstructions.cpp): writes to MODE (float rounding and denormal controls) and the other hardware registers have no effect, and `s_getreg_b32` reads of the MODE round fields report round to nearest even even after `s_setreg_b32` wrote another mode. The initial round mode from the shader's program registers is not read either.
|
||||
- On a host subgroup narrower than 32 lanes, [`threadBit`](../../core/shader/recompiler/Translation/src/TranslationContext/ExecMask.cpp) reads an EXEC written as a value (`s_mov_b32`/`s_mov_b64`) at bit (subgroup invocation id) of EXEC_LO, so lanes at or above the subgroup size read a lower lane's bit.
|
||||
|
||||
Reference in New Issue
Block a user