Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions core/libs/prx/libSceAgcDriver/tests/execution/ImagePckN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,12 @@ void FillInput() {
}
}

std::vector<std::uint32_t> StoreCode(std::uint32_t opcode) {
return {0x34060084u, 0xe0381000u, 0x80031e03u, 0xbf8c3f70u, 0xf0000108u | (opcode << 18u), 0x0001211eu, 0xbf810000u};
}

void Run(AgcDriver::VulkanDevice& device, std::uint32_t opcode, std::uint32_t format, std::uint32_t dmask, std::uint32_t swizzle = 0xfacu) {
const auto code = Code(opcode, dmask);
const auto code = opcode >= 0x76u ? StoreCode(opcode) : Code(opcode, dmask);
std::vector<std::uint32_t> userData(16, 0u);
const auto output = BufferDescriptor(Output.data(), static_cast<std::uint32_t>(sizeof(Output)));
const auto descriptor = Descriptor(format, swizzle);
Expand All @@ -122,6 +126,55 @@ void Run(AgcDriver::VulkanDevice& device, std::uint32_t opcode, std::uint32_t fo
const auto result = ShaderRecompiler::Recompile(request);
device.Dispatch(result, 1, 1, 1, {}, reinterpret_cast<std::uintptr_t>(code.data()));
device.WaitIdle();
if (opcode < 0x76u) return;
AgcDriver::Graphics::StorageTexture::FlushPending(reinterpret_cast<std::uintptr_t>(Texels.data()), Bytes, nullptr, "test");
device.WaitIdle();
}

void FillStoreInput() {
constexpr std::array<std::array<std::uint32_t, 3>, Threads> targets{{
{32u, 0u, 0u}, {37u, 0u, 0u}, {42u, 0u, 0u}, {47u, 0u, 0u}, {52u, 0u, 0u}, {58u, 0u, 0u}, {60u, 0u, 0u},
{0u, 1u, 0u}, {5u, 1u, 0u}, {10u, 1u, 0u}, {15u, 1u, 0u}, {21u, 1u, 0u}, {26u, 1u, 0u}, {31u, 1u, 0u}, {57u, 1u, 0u},
{2u, 2u, 0u}, {7u, 2u, 0u}, {13u, 2u, 0u}, {19u, 2u, 0u}, {43u, 2u, 0u}, {59u, 2u, 0u},
{Width, 1u, 0u}, {0xffffffffu, 1u, 0u}, {0x80000000u, 2u, 0u}, {4u, Height, 0u}, {4u, 0xffffffffu, 0u},
{0u, 0u, 1u}, {5u, 0u, 1u}, {13u, 0u, 1u}, {26u, 0u, 1u}, {28u, 0u, 1u}, {9u, 0u, Levels},
}};
for (std::uint32_t thread = 0; thread < Threads; ++thread) {
Input[thread * 4u + 0u] = targets[thread][0];
Input[thread * 4u + 1u] = targets[thread][1];
Input[thread * 4u + 2u] = targets[thread][2];
Input[thread * 4u + 3u] = 0x9e3779b9u * (thread + 1u);
}
}

void CheckStore(AgcDriver::VulkanDevice& device, std::uint32_t opcode, const Format& format) {
const auto elements = opcode == 0x77u || opcode == 0x7au ? 4u : 2u;
const bool mip = opcode == 0x79u || opcode == 0x7au;
const auto texelBytes = format.bytes * format.components;
const auto group = 4u / texelBytes;
const auto name = "image PCK store opcode " + std::to_string(opcode) + " format " + std::to_string(format.id);
FillTexels(format);
FillStoreInput();
auto expected = Texels;
const auto surface = AgcDriver::Graphics::DescribeSurface(AgcDriver::Graphics::DecodeTextureResource(Descriptor(format.id, 0xfacu)));
for (std::uint32_t thread = 0; thread < Threads; ++thread) {
const auto x = Input[thread * 4u + 0u];
const auto y = Input[thread * 4u + 1u];
const auto level = mip ? Input[thread * 4u + 2u] : 0u;
const auto data = Input[thread * 4u + 3u];
const auto width = level < Levels ? std::max(Width >> level, 1u) : 0u;
const auto height = level < Levels ? std::max(Height >> level, 1u) : 0u;
if (level >= Levels || x >= width || width - x < elements || y >= height) continue;
for (std::uint32_t element = 0; (x & (group - 1u)) + element < group && element < elements; ++element) {
const auto bits = data >> (element * texelBytes * 8u);
const auto offset = surface.mips[level].tiledOffset + static_cast<std::uint64_t>(y) * surface.mips[level].pitchBytes + (x + element) * texelBytes;
std::memcpy(expected.data() + offset, &bits, texelBytes);
}
}
Run(device, opcode, format.id, 0x1u);
for (std::size_t index = 0; index < Bytes; ++index) {
Require(Texels[index] == expected[index], name + ": byte " + std::to_string(index) + " is " + std::to_string(Texels[index]) + ", expected " + std::to_string(expected[index]));
}
}

void Check(AgcDriver::VulkanDevice& device, std::uint32_t opcode, const Format& format, std::uint32_t dmask) {
Expand Down Expand Up @@ -185,9 +238,17 @@ int main() {
Refused(*device, 0x71u, R16UInt, 0x1u, 0xfacu, layout);
Refused(*device, 0x71u, Rg8UInt, 0x1u, 0xfacu, layout);
Refused(*device, 0x70u, R16UInt, 0x1u, 0xf2eu, "identity swizzle");
for (const auto opcode : {0x76u, 0x77u, 0x79u, 0x7au}) Refused(*device, opcode, R8UInt, 0x1u, 0xfacu, "stores are not implemented");
for (const auto opcode : {0x76u, 0x79u}) {
CheckStore(*device, opcode, r8);
CheckStore(*device, opcode, r16);
CheckStore(*device, opcode, rg8);
}
for (const auto opcode : {0x77u, 0x7au}) CheckStore(*device, opcode, r8);
Refused(*device, 0x76u, R32UInt, 0x1u, 0xfacu, layout);
Refused(*device, 0x77u, R16UInt, 0x1u, 0xfacu, layout);
Refused(*device, 0x76u, R16UInt, 0x1u, 0xf2eu, layout);
GuestAllocations::Mutation().Remove(Texels.data());
std::cout << "image PCK2/PCK4 execution tests passed\n";
std::cout << "image PCK2/PCK4 load and store execution tests passed\n";
return 0;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
Expand Down
17 changes: 11 additions & 6 deletions core/libs/prx/libSceAgcDriver/tests/execution/ImageTablePacked.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -692,21 +692,26 @@ void UnsupportedStores(AgcDriver::VulkanDevice& device) {
FillTexture(1u, Format{11u, 2u, 1u});
FillInput(false);
const auto initial = Texels;
for (const auto opcode : {0x52u, 0x53u, 0x5au, 0x5bu, 0x76u, 0x77u, 0x79u, 0x7au}) {
for (const auto opcode : {0x52u, 0x53u, 0x5au, 0x5bu}) {
for (const auto table : {false, true}) {
const auto dmask = opcode >= 0x76u ? 1u : (opcode & 1u) != 0u ? 15u : 3u;
const auto code = Code(opcode, dmask, table);
const auto code = Code(opcode, (opcode & 1u) != 0u ? 15u : 3u, table);
std::string refusal;
try {
Run(device, code, 0u, 1u, false);
} catch (const std::exception& error) {
refusal = error.what();
}
Require(refusal.find("stores are not implemented") != std::string::npos, "unsupported BY/PCK store opcode " + std::to_string(opcode) + " was not rejected before GPU submission: " + refusal);
Require(Texels == initial, "unsupported BY/PCK store compilation modified an image");
Require(refusal.find("stores are not implemented") != std::string::npos, "unsupported BY store opcode " + std::to_string(opcode) + " was not rejected before GPU submission: " + refusal);
Require(Texels == initial, "unsupported BY store compilation modified an image");
}
}
std::cout << "BY stores and unmeasured PCK stores rejected without GPU submission\n";
SetEntry(0u, Descriptor(0u, Format{5u, 1u, 1u}));
for (const auto opcode : {0x76u, 0x77u, 0x79u, 0x7au}) {
const auto poisoned = Run(device, Code(opcode, 1u, true), 0u);
Require(poisoned.faults != 0u && poisoned.log.find("image table:") != std::string::npos, "PCK store opcode " + std::to_string(opcode) + " through an image table was not poisoned");
Require(Texels == initial, "PCK store opcode " + std::to_string(opcode) + " through an image table modified an image");
}
std::cout << "BY stores rejected without GPU submission and PCK stores through image tables poisoned\n";
}

struct Rejection {
Expand Down
19 changes: 7 additions & 12 deletions core/libs/tests/ImagePckN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,20 @@ void Check(std::uint32_t index, bool nsa, std::uint32_t mask) {
auto& block = program.CreateBlock();
program.SetEntryBlock(block);
TranslationContext context(program, block, 256);
if (store) {
bool refused = false;
try { context.TranslateInstruction(inst); }
catch (const std::runtime_error& error) { refused = std::string(error.what()).find("stores are not implemented") != std::string::npos; }
Require(refused);
return;
}
context.TranslateInstruction(inst);
std::vector<IrValue*> reads;
std::vector<IrValue*> accesses;
std::uint32_t writes = 0;
std::uint32_t others = 0;
for (auto* value : block.Instructions()) {
if (value->Opcode() == IrOpcode::SetVectorRegister) ++writes;
if (value->Opcode() == IrOpcode::ImageRead) {
if (value->Opcode() == IrOpcode::ImageRead || value->Opcode() == IrOpcode::ImageWrite) {
Require(writes == 0u);
reads.push_back(value);
if (value->Opcode() == (store ? IrOpcode::ImageWrite : IrOpcode::ImageRead)) accesses.push_back(value);
else ++others;
}
}
Require(reads.size() == 1u && writes == dwords);
const auto& memory = program.Resources().memoryInfo[reads[0]->Flags<MemoryFlags>().index];
Require(accesses.size() == 1u && others == 0u && writes == (store ? 0u : dwords));
const auto& memory = program.Resources().memoryInfo[accesses[0]->Flags<MemoryFlags>().index];
Require(memory.imageByElements == elements && memory.imagePacked && memory.dmask == mask && memory.dataDwords == dwords);
Require(memory.imageHasMip == mip);
}
Expand Down
54 changes: 54 additions & 0 deletions core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,61 @@ std::uint32_t QuantizeStoreTexel(SpirvEmitterState& state, std::uint32_t resourc
return result;
}

void EmitPackedByStoreOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access) {
auto& state = ctx.state;
const auto elements = access.mem.imageByElements;
if (access.image.dimension != RdnaImageDimension::Dim2D || access.mem.dataDwords != 1u || access.mem.dmask != 1u || access.mem.dataBits != 32u) {
ctx.Fail(access.inst, "is a MIMG PCK2/PCK4 store outside the measured 2D, one-dword subset");
}
const auto info = PackedTexelFormat(ctx, access);
std::uint32_t texelBits = 0;
for (std::uint32_t component = 0; component < info.componentCount; ++component) texelBits += info.componentBits[component];
if (texelBits == 0u || texelBits % 8u != 0u || texelBits * elements > 32u) ctx.Fail(access.inst, "is a MIMG PCK2/PCK4 store whose elements do not fit one dword");
const auto groupTexels = 32u / texelBits;
state.module.EmitCapability(spv::CapabilityImageQuery);
EmitIfCondition(state, ctx.Arg(access.inst, 3), [&]() {
const auto image = state.module.AllocateId();
state.module.AddFunction(spv::OpLoad, ImageType(state, access.image), image, StorageImageDescriptorPointer(state, access.mem.resource));
const auto x = AddressU32(ctx, access, 0);
const auto y = AddressU32(ctx, access, 1);
const auto lod = access.mem.imageHasMip ? LodU32(ctx, access) : ConstantU32(state, 0u);
const auto shiftLod = Select(state, TypeU32(state), Binary(state, spv::OpUGreaterThan, TypeBool(state), lod, ConstantU32(state, 31u)), ConstantU32(state, 31u), lod);
const auto size = state.module.AllocateId();
state.module.AddFunction(spv::OpImageQuerySize, ImageViewSizeType(state, access.image.dimension), size, image);
const auto levelExtent = [&](std::uint32_t axis) {
const auto base = state.module.AllocateId();
state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), base, size, axis);
const auto shifted = Binary(state, spv::OpShiftRightLogical, TypeU32(state), base, shiftLod);
return Select(state, TypeU32(state), Binary(state, spv::OpUGreaterThan, TypeBool(state), shifted, ConstantU32(state, 1)), shifted, ConstantU32(state, 1));
};
const auto width = levelExtent(0u);
const auto height = levelExtent(1u);
auto inside = Binary(state, spv::OpLogicalAnd, TypeBool(state), Binary(state, spv::OpULessThan, TypeBool(state), x, width), Binary(state, spv::OpUGreaterThanEqual, TypeBool(state), Binary(state, spv::OpISub, TypeU32(state), width, x), ConstantU32(state, elements)));
inside = Binary(state, spv::OpLogicalAnd, TypeBool(state), inside, Binary(state, spv::OpULessThan, TypeBool(state), y, height));
const auto lane = Binary(state, spv::OpBitwiseAnd, TypeU32(state), x, ConstantU32(state, groupTexels - 1u));
const auto word = state.module.AllocateId();
state.module.AddFunction(spv::OpCompositeExtract, TypeU32(state), word, ctx.Arg(access.inst, 2), 0u);
const auto mipLod = access.image.mipMode == ImageMipMode::DynamicStorage ? lod : 0u;
for (std::uint32_t element = 0; element < elements; ++element) {
const auto enabled = Binary(state, spv::OpLogicalAnd, TypeBool(state), inside, Binary(state, spv::OpULessThan, TypeBool(state), Binary(state, spv::OpIAdd, TypeU32(state), lane, ConstantU32(state, element)), ConstantU32(state, groupTexels)));
EmitIfCondition(state, enabled, [&]() {
const auto coord = state.module.AllocateId();
state.module.AddFunction(spv::OpCompositeConstruct, TypeU32Vector(state, 2), coord, Binary(state, spv::OpIAdd, TypeU32(state), x, ConstantU32(state, element)), y);
const auto shifted = element == 0u ? word : Binary(state, spv::OpShiftRightLogical, TypeU32(state), word, ConstantU32(state, element * texelBits));
const auto bits = Binary(state, spv::OpBitwiseAnd, TypeU32(state), shifted, ConstantU32(state, (1u << texelBits) - 1u));
const auto source = state.module.AllocateId();
state.module.AddFunction(spv::OpCompositeConstruct, TypeU32Vector(state, 4), source, bits, ConstantU32(state, 0u), ConstantU32(state, 0u), ConstantU32(state, 0u));
EmitStorageImageWrite(state, access.mem.resource, mipLod, coord, PackedStoreTexel(ctx, access, source), ImageSampleIndex(ctx, access));
});
}
});
}

void EmitWriteOp(SpirvValueEmitContext& ctx, const ImageEmitAccess& access) {
if (access.mem.imagePacked && access.mem.imageByElements != 0u) {
EmitPackedByStoreOp(ctx, access);
return;
}
auto& state = ctx.state;
const bool uintImage = access.image.numericClass == IrTextureNumericClass::Uint;
auto condition = ctx.Arg(access.inst, 3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,22 @@ bool TranslationContext::imageGetLod(const RdnaInstruction& inst) {
bool TranslationContext::imageBy(const RdnaInstruction& inst) {
const auto id = inst.imageOpcodeId;
const bool packed = id >= 0x70u;
if (packed ? id >= 0x76u : (id & 0x10u) != 0u) {
throw std::runtime_error(packed ? "MIMG PCK2/PCK4 stores are not implemented" : "MIMG BY2/BY4 stores are not implemented");
const bool store = packed && id >= 0x76u;
if (!packed && (id & 0x10u) != 0u) {
throw std::runtime_error("MIMG BY2/BY4 stores are not implemented");
}
MemoryInfo memory = imageMemoryInfoFromInstruction(inst);
memory.imageHasMip = packed ? id == 0x73u || id == 0x74u : (id & 8u) != 0u;
memory.imageHasMip = packed ? id == 0x73u || id == 0x74u || id == 0x79u || id == 0x7au : (id & 8u) != 0u;
memory.imagePacked = packed;
memory.imageByElements = packed ? (id == 0x71u || id == 0x74u ? 4u : 2u) : (id & 1u) != 0u ? 4u : 2u;
memory.imageByElements = packed ? (id == 0x71u || id == 0x74u || id == 0x77u || id == 0x7au ? 4u : 2u) : (id & 1u) != 0u ? 4u : 2u;
IrValue* resource = getImageResource(memory);
IrValue* address = makeImageAddress(inst, inst.source0);
IrValue& exec = ir.GetExec();
if (store) {
IrValue* data = constructU32x4(inst.destination, memory.dataDwords);
(void)ir.Emit(IrOpcode::ImageWrite, IrOpcodeType(IrOpcode::ImageWrite), {resource, address, data, &exec}, addMemoryInfo(memory, inst.programCounter));
return true;
}
IrValue& result = ir.Emit(IrOpcode::ImageRead, IrOpcodeType(IrOpcode::ImageRead), {resource, address, &exec}, addMemoryInfo(memory, inst.programCounter));
writeImageComponents(inst.destination, &result, memory, 4u);
return true;
Expand Down
9 changes: 9 additions & 0 deletions docs/dev/debt/changes/feat-shader-pck-stores.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# shader

## change [MIMG PCK2/PCK4 loads]

- [MIMG PCK2/PCK4 loads](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (`image_load[_mip]_pck2/pck4`, opcodes 0x70, 0x71, 0x73 and 0x74) were measured on RDNA2 (gfx1035) with the [hardware oracle](../HW_ORACLE.md) for 2D images with identity swizzle. With data mask 0x1, they write one dword: the raw bits of the N texels (PCK2: 2, PCK4: 4) from `x & ~(N - 1)` on row y, concatenated (element k at bit `k * texel bits`) and zero-extended. The dword is zero when the mip level is past the last level, `x + N - 1` is outside the level's width (unaligned x, unsigned), or y is outside its height. Data only comes back when N texels fit in 32 bits: R8, R16 or RG8 for PCK2, R8 for PCK4. Wider texels (R32, RG16, RGBA8) and masks other than 0x1 write zeros on hardware. Those masks throw at decode, and formats outside UNORM/UINT/SINT R8, R16 and RG8 throw. #1314's earlier measurement saw only zeros because it used masks 0x3 and 0xf.

## add Functional

- [MIMG PCK2/PCK4 stores](../../../core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp) (opcodes 0x76, 0x77, 0x79 and 0x7a) throw for UNORM, SINT and FLOAT formats, masks other than 0x1, texels too wide for N to fit 32 bits and image tables, although RDNA2 hardware writes raw bits or nothing there; tiled surfaces are not measured, and 128-bit formats were not run (0x76 hung a GPU in #1314). Measured on gfx1036 (Radeon 610M) on linear 2D images: under the load's bounds, texel `x + k` gets bits `k * texel bits` of the dword while it stays in the aligned 4-byte group holding x, so x is not aligned down as in the loads.