mirror of
https://github.com/par274/sharpemu.git
synced 2026-07-30 22:49:53 +08:00
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 ---------
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Copyright (C) 2026 SharpEmu Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
using SharpEmu.Libs.Agc;
|
||||
using SharpEmu.ShaderCompiler;
|
||||
using SharpEmu.ShaderCompiler.Metal;
|
||||
|
||||
@@ -1583,6 +1584,45 @@ internal static partial class MetalVideoPresenter
|
||||
return cached;
|
||||
}
|
||||
|
||||
// Default-on GPU detile packages the tiled source + resolved DetileParams
|
||||
// with empty RgbaPixels so a backend can deswizzle on the GPU. The Metal
|
||||
// GPU compute pass (MetalDetilePass / detile_compute.msl) is the intended
|
||||
// equivalent of VulkanDetilePass, but it is Mac-untested, so the active
|
||||
// Metal path CPU-detiles here via GnmTiling.DetileWithParams — the exact
|
||||
// same DetileParams addressing the kernel runs — restoring the linear-upload
|
||||
// behavior Metal had before GPU detile existed, with no regression.
|
||||
if (texture.RgbaPixels.Length == 0 &&
|
||||
texture.TiledSource is { } tiledSource &&
|
||||
texture.Detile is { } detileParameters)
|
||||
{
|
||||
// The tiled source packs the array slices contiguously (one per layer);
|
||||
// detile each into its layer-major linear region so the reconstructed
|
||||
// pixels match what the CPU array-upload path produced pre-GPU-detile.
|
||||
// A plain 2D texture is just one layer.
|
||||
var layers = Math.Max((int)texture.ArrayLayers, 1);
|
||||
var sliceLinearBytes =
|
||||
detileParameters.ElementsWide * detileParameters.ElementsHigh * detileParameters.BytesPerElement;
|
||||
var sliceTiledBytes = tiledSource.Length / layers;
|
||||
var linear = new byte[sliceLinearBytes * layers];
|
||||
var detiledAll = true;
|
||||
for (var layer = 0; layer < layers; layer++)
|
||||
{
|
||||
if (!GnmTiling.DetileWithParams(
|
||||
detileParameters,
|
||||
tiledSource.AsSpan(layer * sliceTiledBytes, sliceTiledBytes),
|
||||
linear.AsSpan(layer * sliceLinearBytes, sliceLinearBytes)))
|
||||
{
|
||||
detiledAll = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (detiledAll)
|
||||
{
|
||||
texture = texture with { RgbaPixels = linear };
|
||||
}
|
||||
}
|
||||
|
||||
// AGC ships the raw (detiled) source texels; create the texture in the
|
||||
// guest's native format — Mac-family GPUs sample BC blocks directly —
|
||||
// and size expectations with the same block-aware math AGC used.
|
||||
|
||||
Reference in New Issue
Block a user