Files
sharpemu/tests/SharpEmu.Libs.Tests/Agc/DetileComputeSpirvTests.cs
T
shadowbeat070 a158960c20 feat(gpu): GPU compute detile for guest tiled textures (Vulkan + Metal) (#592)
* feat(gpu): GPU compute detile for guest tiled textures (Vulkan + Metal)

Move RDNA2 exact-XOR deswizzle (swizzle modes 5/9/24/27, 4bpp) off the CPU
onto a GPU compute pass. GnmTiling.GetDetileParams resolves the shared
addressing into DetileParams; the CPU fallback and both GPU kernels consume
the same params so they never disagree.

Vulkan (verified bit-exact on NVIDIA): SpirvFixedShaders.CreateDetileCompute
hand-emits the SPIR-V kernel; VulkanDetilePass.RecordDetile records the
dispatch into the async batch command buffer (never a blocking submit on the
render thread) with transients retired via fence; VulkanDetileSelfTest
(SHARPEMU_DETILE_SELFTEST=1) checks both entry points against the CPU detile.

Metal (Mac-untested): detile_compute.msl (detile_cs) + MetalDetilePass mirror
the Vulkan pass. The active Metal path CPU-detiles via the new
GnmTiling.DetileWithParams when a texture arrives packaged (empty RgbaPixels +
TiledSource/Detile), keeping Metal correct under default-on with no regression;
wiring MetalDetilePass live is the remaining on-device step.

Flags: GPU detile is default-on (SHARPEMU_GPU_DETILE=0 disables);
[GPU-DETILE] diagnostics gated behind SHARPEMU_LOG_GPU_DETILE=1.

Tests: 17 detile unit tests pass, incl. DetileWithParams and GetDetileParams
each matching TryDetile bit-for-bit across all supported modes/bpp, plus a
SPIR-V structural-validity test.

* feat(gpu): GPU compute detile for guest tiled textures (Vulkan + Metal)

Move RDNA2 exact-XOR deswizzle (swizzle modes 5/9/24/27, 4bpp) off the CPU
onto a GPU compute pass. GnmTiling.GetDetileParams resolves the shared
addressing into DetileParams that the CPU fallback and both GPU kernels
consume, so they never disagree; everything else keeps the CPU path.

Vulkan (verified bit-exact on NVIDIA): SpirvFixedShaders.CreateDetileCompute
hand-emits the kernel; VulkanDetilePass.RecordDetile records into the async
batch command buffer (never a blocking submit on the render thread) with
transients retired via fence, falling back to CPU detile on failure.
VulkanDetileSelfTest (SHARPEMU_DETILE_SELFTEST=1) checks both entry points.

Metal (Mac-untested): detile_compute.msl + MetalDetilePass mirror the Vulkan
pass; the active Metal path CPU-detiles via GnmTiling.DetileWithParams so it
stays correct under default-on. Wiring MetalDetilePass live is a follow-up.

Flags: default-on (SHARPEMU_GPU_DETILE=0 disables); diagnostics behind
SHARPEMU_LOG_GPU_DETILE=1. Adds 17 passing detile unit tests.

* Fix: added support layered texture support for the GPU-Detiling.

* Fix: Added support for BlockTable (1 / 4 / 8 (Morton/Z-order))

* feat: added support for 8 and 16 bpp (bytes per element)

* Fixed a build failure specific to this branch

---------
2026-07-24 20:13:02 +03:00

103 lines
3.9 KiB
C#

// Copyright (C) 2026 SharpEmu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
using System.Buffers.Binary;
using SharpEmu.ShaderCompiler.Vulkan;
using Xunit;
namespace SharpEmu.Libs.Tests.Agc;
// Structural validation of the GPU detile compute kernel. This cannot run the
// shader without a Vulkan device, but it pins the SPIR-V is well-formed: the
// header is correct, every instruction's word count sums to exactly the module
// length (the classic hand-emit bug), and the compute-specific pieces are
// present (a GLCompute entry point, a LocalSize execution mode, and a runtime
// array for the storage buffers). Full pixel correctness is verified on a GPU.
public sealed class DetileComputeSpirvTests
{
private const uint SpirvMagic = 0x07230203;
private const uint SpirvVersion15 = 0x00010500;
private const ushort OpEntryPoint = 15;
private const ushort OpExecutionMode = 16;
private const ushort OpTypeRuntimeArray = 29;
private const ushort OpFunction = 54;
private const ushort OpFunctionEnd = 56;
private const uint ExecutionModelGLCompute = 5;
private const uint ExecutionModeLocalSize = 17;
[Fact]
public void CreateDetileCompute_EmitsWellFormedComputeModule()
{
var spirv = SpirvFixedShaders.CreateDetileCompute();
Assert.True(spirv.Length % sizeof(uint) == 0, "SPIR-V must be a whole number of words.");
var words = new uint[spirv.Length / sizeof(uint)];
for (var i = 0; i < words.Length; i++)
{
words[i] = BinaryPrimitives.ReadUInt32LittleEndian(spirv.AsSpan(i * sizeof(uint)));
}
Assert.True(words.Length > 5, "Module must have a header plus instructions.");
Assert.Equal(SpirvMagic, words[0]);
Assert.Equal(SpirvVersion15, words[1]);
var bound = words[3];
Assert.True(bound > 1, "Id bound must be set.");
var sawComputeEntry = false;
var sawLocalSize = false;
var sawRuntimeArray = false;
var functionCount = 0;
var functionEndCount = 0;
var offset = 5;
while (offset < words.Length)
{
var word = words[offset];
var wordCount = (int)(word >> 16);
var opcode = (ushort)(word & 0xFFFF);
Assert.True(wordCount >= 1, $"Instruction at {offset} has a zero word count.");
Assert.True(
offset + wordCount <= words.Length,
$"Instruction at {offset} (op {opcode}, wc {wordCount}) overruns the module.");
switch (opcode)
{
case OpEntryPoint when words[offset + 1] == ExecutionModelGLCompute:
sawComputeEntry = true;
break;
case OpExecutionMode
when wordCount >= 6 &&
words[offset + 2] == ExecutionModeLocalSize &&
words[offset + 3] == 8 &&
words[offset + 4] == 8 &&
words[offset + 5] == 1:
sawLocalSize = true;
break;
case OpTypeRuntimeArray:
sawRuntimeArray = true;
break;
case OpFunction:
functionCount++;
break;
case OpFunctionEnd:
functionEndCount++;
break;
}
offset += wordCount;
}
// Word counts must tile the module exactly — a wrong length lands here.
Assert.Equal(words.Length, offset);
Assert.True(sawComputeEntry, "Missing a GLCompute OpEntryPoint.");
Assert.True(sawLocalSize, "Missing an 8x8x1 LocalSize execution mode.");
Assert.True(sawRuntimeArray, "Missing a runtime array (storage buffers).");
Assert.Equal(1, functionCount);
Assert.Equal(1, functionEndCount);
}
}