From 4a589cc14128e699392a71debc651cf047283c72 Mon Sep 17 00:00:00 2001 From: psucien Date: Sat, 21 Dec 2024 14:40:26 +0100 Subject: [PATCH] added 64bpp macro detiler --- src/video_core/amdgpu/resource.h | 2 + src/video_core/host_shaders/CMakeLists.txt | 1 + src/video_core/host_shaders/detile_m32x1.comp | 1 - src/video_core/host_shaders/detile_m32x2.comp | 1 - src/video_core/host_shaders/detile_m32x4.comp | 1 - src/video_core/host_shaders/detile_m8x1.comp | 1 - src/video_core/host_shaders/detile_m8x2.comp | 1 - .../host_shaders/detile_macro32x1.comp | 13 +- .../host_shaders/detile_macro32x2.comp | 91 +++++++++ src/video_core/texture_cache/tile_manager.cpp | 193 +++--------------- src/video_core/texture_cache/tile_manager.h | 1 + 11 files changed, 125 insertions(+), 181 deletions(-) create mode 100644 src/video_core/host_shaders/detile_macro32x2.comp diff --git a/src/video_core/amdgpu/resource.h b/src/video_core/amdgpu/resource.h index db51cdbe..d9a8b7ca 100644 --- a/src/video_core/amdgpu/resource.h +++ b/src/video_core/amdgpu/resource.h @@ -141,6 +141,8 @@ constexpr std::string_view NameOf(TilingMode type) { return "Texture_MicroTiled"; case TilingMode::Texture_MacroTiled: return "Texture_MacroTiled"; + case TilingMode::Texture_Volume: + return "Texture_Volume"; default: return "Unknown"; } diff --git a/src/video_core/host_shaders/CMakeLists.txt b/src/video_core/host_shaders/CMakeLists.txt index 79043009..c2a3b53f 100644 --- a/src/video_core/host_shaders/CMakeLists.txt +++ b/src/video_core/host_shaders/CMakeLists.txt @@ -8,6 +8,7 @@ set(SHADER_FILES detile_m32x2.comp detile_m32x4.comp detile_macro32x1.comp + detile_macro32x2.comp fs_tri.vert post_process.frag ) diff --git a/src/video_core/host_shaders/detile_m32x1.comp b/src/video_core/host_shaders/detile_m32x1.comp index 3dfee5eb..802f5f53 100644 --- a/src/video_core/host_shaders/detile_m32x1.comp +++ b/src/video_core/host_shaders/detile_m32x1.comp @@ -16,7 +16,6 @@ layout(push_constant) uniform image_info { uint num_levels; uint pitch; uint height; - uint depth; uint sizes[14]; } info; diff --git a/src/video_core/host_shaders/detile_m32x2.comp b/src/video_core/host_shaders/detile_m32x2.comp index f5085907..90063a18 100644 --- a/src/video_core/host_shaders/detile_m32x2.comp +++ b/src/video_core/host_shaders/detile_m32x2.comp @@ -16,7 +16,6 @@ layout(push_constant) uniform image_info { uint num_levels; uint pitch; uint height; - uint depth; uint sizes[14]; } info; diff --git a/src/video_core/host_shaders/detile_m32x4.comp b/src/video_core/host_shaders/detile_m32x4.comp index f0b924da..e1b98817 100644 --- a/src/video_core/host_shaders/detile_m32x4.comp +++ b/src/video_core/host_shaders/detile_m32x4.comp @@ -16,7 +16,6 @@ layout(push_constant) uniform image_info { uint num_levels; uint pitch; uint height; - uint depth; uint sizes[14]; } info; diff --git a/src/video_core/host_shaders/detile_m8x1.comp b/src/video_core/host_shaders/detile_m8x1.comp index dbed4fa1..39d0aaeb 100644 --- a/src/video_core/host_shaders/detile_m8x1.comp +++ b/src/video_core/host_shaders/detile_m8x1.comp @@ -19,7 +19,6 @@ layout(push_constant) uniform image_info { uint num_levels; uint pitch; uint height; - uint depth; uint sizes[14]; } info; diff --git a/src/video_core/host_shaders/detile_m8x2.comp b/src/video_core/host_shaders/detile_m8x2.comp index 11e35cc7..3f8e5ab3 100644 --- a/src/video_core/host_shaders/detile_m8x2.comp +++ b/src/video_core/host_shaders/detile_m8x2.comp @@ -18,7 +18,6 @@ layout(push_constant) uniform image_info { uint num_levels; uint pitch; uint height; - uint depth; uint sizes[14]; } info; diff --git a/src/video_core/host_shaders/detile_macro32x1.comp b/src/video_core/host_shaders/detile_macro32x1.comp index 190e1a75..086fbcfb 100644 --- a/src/video_core/host_shaders/detile_macro32x1.comp +++ b/src/video_core/host_shaders/detile_macro32x1.comp @@ -16,8 +16,8 @@ layout(push_constant) uniform image_info { uint num_levels; uint pitch; uint height; - uint depth; - uint sizes[14]; + uint c0; + uint c1; } info; // Each LUT is 64 bytes, so should fit into K$ given tiled slices locality @@ -78,14 +78,13 @@ void main() { uint row = bitfieldExtract(y, 0, 3); uint lut = bitfieldExtract(z, 0, 2); uint idx = lut_32bpp[lut][col + row * MICRO_TILE_DIM]; - uint elem_offs = idx * BPP; - uint slice_offs = (z / 4) * MICRO_TILE_DIM * MICRO_TILE_DIM * MICRO_TILE_SZ; + uint slice_offs = (z >> 2u) * info.c1 * MICRO_TILE_SZ; uint tile_row = y / MICRO_TILE_DIM; uint tile_column = x / MICRO_TILE_DIM; - uint tile_offs = ((tile_row * MICRO_TILE_DIM) + tile_column) * MICRO_TILE_SZ; - uint offs = ((slice_offs + tile_offs) * MICRO_TILE_DIM + elem_offs) / MICRO_TILE_DIM; + uint tile_offs = ((tile_row * info.c0) + tile_column) * MICRO_TILE_SZ; + uint offs = slice_offs + tile_offs + (idx * BPP / 8); - uint p0 = in_data[offs / (BPP / 8)]; + uint p0 = in_data[offs >> 2u]; out_data[gl_GlobalInvocationID.x] = p0; } diff --git a/src/video_core/host_shaders/detile_macro32x2.comp b/src/video_core/host_shaders/detile_macro32x2.comp new file mode 100644 index 00000000..296311c7 --- /dev/null +++ b/src/video_core/host_shaders/detile_macro32x2.comp @@ -0,0 +1,91 @@ +// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#version 450 + +layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in; + +layout(std430, binding = 0) buffer input_buf { + uint in_data[]; +}; +layout(std430, binding = 1) buffer output_buf { + uint out_data[]; +}; + +layout(push_constant) uniform image_info { + uint num_levels; + uint pitch; + uint height; + uint c0; + uint c1; +} info; + +const uint lut_64bpp[][64] = { + { + 0x00, 0x01, 0x08, 0x09, 0x40, 0x41, 0x48, 0x49, + 0x02, 0x03, 0x0a, 0x0b, 0x42, 0x43, 0x4a, 0x4b, + 0x10, 0x11, 0x18, 0x19, 0x50, 0x51, 0x58, 0x59, + 0x12, 0x13, 0x1a, 0x1b, 0x52, 0x53, 0x5a, 0x5b, + 0x80, 0x81, 0x88, 0x89, 0xc0, 0xc1, 0xc8, 0xc9, + 0x82, 0x83, 0x8a, 0x8b, 0xc2, 0xc3, 0xca, 0xcb, + 0x90, 0x91, 0x98, 0x99, 0xd0, 0xd1, 0xd8, 0xd9, + 0x92, 0x93, 0x9a, 0x9b, 0xd2, 0xd3, 0xda, 0xdb, + }, + { + 0x04, 0x05, 0x0c, 0x0d, 0x44, 0x45, 0x4c, 0x4d, + 0x06, 0x07, 0x0e, 0x0f, 0x46, 0x47, 0x4e, 0x4f, + 0x14, 0x15, 0x1c, 0x1d, 0x54, 0x55, 0x5c, 0x5d, + 0x16, 0x17, 0x1e, 0x1f, 0x56, 0x57, 0x5e, 0x5f, + 0x84, 0x85, 0x8c, 0x8d, 0xc4, 0xc5, 0xcc, 0xcd, + 0x86, 0x87, 0x8e, 0x8f, 0xc6, 0xc7, 0xce, 0xcf, + 0x94, 0x95, 0x9c, 0x9d, 0xd4, 0xd5, 0xdc, 0xdd, + 0x96, 0x97, 0x9e, 0x9f, 0xd6, 0xd7, 0xde, 0xdf, + }, + { + 0x20, 0x21, 0x28, 0x29, 0x60, 0x61, 0x68, 0x69, + 0x22, 0x23, 0x2a, 0x2b, 0x62, 0x63, 0x6a, 0x6b, + 0x30, 0x31, 0x38, 0x39, 0x70, 0x71, 0x78, 0x79, + 0x32, 0x33, 0x3a, 0x3b, 0x72, 0x73, 0x7a, 0x7b, + 0xa0, 0xa1, 0xa8, 0xa9, 0xe0, 0xe1, 0xe8, 0xe9, + 0xa2, 0xa3, 0xaa, 0xab, 0xe2, 0xe3, 0xea, 0xeb, + 0xb0, 0xb1, 0xb8, 0xb9, 0xf0, 0xf1, 0xf8, 0xf9, + 0xb2, 0xb3, 0xba, 0xbb, 0xf2, 0xf3, 0xfa, 0xfb, + }, + { + 0x24, 0x25, 0x2c, 0x2d, 0x64, 0x65, 0x6c, 0x6d, + 0x26, 0x27, 0x2e, 0x2f, 0x66, 0x67, 0x6e, 0x6f, + 0x34, 0x35, 0x3c, 0x3d, 0x74, 0x75, 0x7c, 0x7d, + 0x36, 0x37, 0x3e, 0x3f, 0x76, 0x77, 0x7e, 0x7f, + 0xa4, 0xa5, 0xac, 0xad, 0xe4, 0xe5, 0xec, 0xed, + 0xa6, 0xa7, 0xae, 0xaf, 0xe6, 0xe7, 0xee, 0xef, + 0xb4, 0xb5, 0xbc, 0xbd, 0xf4, 0xf5, 0xfc, 0xfd, + 0xb6, 0xb7, 0xbe, 0xbf, 0xf6, 0xf7, 0xfe, 0xff, + }, +}; + +#define MICRO_TILE_DIM (8) +#define MICRO_TILE_SZ (2048) +#define TEXELS_PER_ELEMENT (1) +#define BPP (64) + +void main() { + uint x = gl_GlobalInvocationID.x % info.pitch; + uint y = (gl_GlobalInvocationID.x / info.pitch) % info.height; + uint z = gl_GlobalInvocationID.x / (info.pitch * info.height); + + uint col = bitfieldExtract(x, 0, 3); + uint row = bitfieldExtract(y, 0, 3); + uint lut = bitfieldExtract(z, 0, 2); + uint idx = lut_64bpp[lut][col + row * MICRO_TILE_DIM]; + + uint slice_offs = (z >> 2u) * info.c1 * MICRO_TILE_SZ; + uint tile_row = y / MICRO_TILE_DIM; + uint tile_column = x / MICRO_TILE_DIM; + uint tile_offs = ((tile_row * info.c0) + tile_column) * MICRO_TILE_SZ; + uint offs = slice_offs + tile_offs + (idx * BPP / 8); + + uint p0 = in_data[(offs >> 2) + 0]; + uint p1 = in_data[(offs >> 2) + 1]; + out_data[2 * gl_GlobalInvocationID.x + 0] = p0; + out_data[2 * gl_GlobalInvocationID.x + 1] = p1; +} diff --git a/src/video_core/texture_cache/tile_manager.cpp b/src/video_core/texture_cache/tile_manager.cpp index 71a077f9..6a51bae2 100644 --- a/src/video_core/texture_cache/tile_manager.cpp +++ b/src/video_core/texture_cache/tile_manager.cpp @@ -13,6 +13,7 @@ #include "video_core/host_shaders/detile_m8x1_comp.h" #include "video_core/host_shaders/detile_m8x2_comp.h" #include "video_core/host_shaders/detile_macro32x1_comp.h" +#include "video_core/host_shaders/detile_macro32x2_comp.h" #include #include @@ -20,158 +21,7 @@ namespace VideoCore { -class TileManager32 { -public: - u32 m_macro_tile_height = 0; - u32 m_bank_height = 0; - u32 m_num_banks = 0; - u32 m_num_pipes = 0; - u32 m_padded_width = 0; - u32 m_padded_height = 0; - u32 m_pipe_bits = 0; - u32 m_bank_bits = 0; - - void Init(u32 width, u32 height, bool is_neo) { - m_macro_tile_height = (is_neo ? 128 : 64); - m_bank_height = is_neo ? 2 : 1; - m_num_banks = is_neo ? 8 : 16; - m_num_pipes = is_neo ? 16 : 8; - m_padded_width = width; - if (height == 1080) { - m_padded_height = is_neo ? 1152 : 1088; - } - if (height == 720) { - m_padded_height = 768; - } - m_pipe_bits = is_neo ? 4 : 3; - m_bank_bits = is_neo ? 3 : 4; - } - - static u32 getElementIdx(u32 x, u32 y) { - u32 elem = 0; - elem |= ((x >> 0u) & 0x1u) << 0u; - elem |= ((x >> 1u) & 0x1u) << 1u; - elem |= ((y >> 0u) & 0x1u) << 2u; - elem |= ((x >> 2u) & 0x1u) << 3u; - elem |= ((y >> 1u) & 0x1u) << 4u; - elem |= ((y >> 2u) & 0x1u) << 5u; - - return elem; - } - - static u32 getPipeIdx(u32 x, u32 y, bool is_neo) { - u32 pipe = 0; - - if (!is_neo) { - pipe |= (((x >> 3u) ^ (y >> 3u) ^ (x >> 4u)) & 0x1u) << 0u; - pipe |= (((x >> 4u) ^ (y >> 4u)) & 0x1u) << 1u; - pipe |= (((x >> 5u) ^ (y >> 5u)) & 0x1u) << 2u; - } else { - pipe |= (((x >> 3u) ^ (y >> 3u) ^ (x >> 4u)) & 0x1u) << 0u; - pipe |= (((x >> 4u) ^ (y >> 4u)) & 0x1u) << 1u; - pipe |= (((x >> 5u) ^ (y >> 5u)) & 0x1u) << 2u; - pipe |= (((x >> 6u) ^ (y >> 5u)) & 0x1u) << 3u; - } - - return pipe; - } - - static u32 getBankIdx(u32 x, u32 y, u32 bank_width, u32 bank_height, u32 num_banks, - u32 num_pipes) { - const u32 x_shift_offset = std::bit_width(bank_width * num_pipes) - 1; - const u32 y_shift_offset = std::bit_width(bank_height) - 1; - const u32 xs = x >> x_shift_offset; - const u32 ys = y >> y_shift_offset; - u32 bank = 0; - switch (num_banks) { - case 8: - bank |= (((xs >> 3u) ^ (ys >> 5u)) & 0x1u) << 0u; - bank |= (((xs >> 4u) ^ (ys >> 4u) ^ (ys >> 5u)) & 0x1u) << 1u; - bank |= (((xs >> 5u) ^ (ys >> 3u)) & 0x1u) << 2u; - break; - case 16: - bank |= (((xs >> 3u) ^ (ys >> 6u)) & 0x1u) << 0u; - bank |= (((xs >> 4u) ^ (ys >> 5u) ^ (ys >> 6u)) & 0x1u) << 1u; - bank |= (((xs >> 5u) ^ (ys >> 4u)) & 0x1u) << 2u; - bank |= (((xs >> 6u) ^ (ys >> 3u)) & 0x1u) << 3u; - break; - default:; - } - - return bank; - } - - u64 getTiledOffs(u32 x, u32 y, bool is_neo) const { - u64 element_index = getElementIdx(x, y); - - u32 xh = x; - u32 yh = y; - u64 pipe = getPipeIdx(xh, yh, is_neo); - u64 bank = getBankIdx(xh, yh, 1, m_bank_height, m_num_banks, m_num_pipes); - u32 tile_bytes = (8 * 8 * 32 + 7) / 8; - u64 element_offset = (element_index * 32); - u64 tile_split_slice = 0; - - if (tile_bytes > 512) { - tile_split_slice = element_offset / (static_cast(512) * 8); - element_offset %= (static_cast(512) * 8); - tile_bytes = 512; - } - - u64 macro_tile_bytes = - (128 / 8) * (m_macro_tile_height / 8) * tile_bytes / (m_num_pipes * m_num_banks); - u64 macro_tiles_per_row = m_padded_width / 128; - u64 macro_tile_row_index = y / m_macro_tile_height; - u64 macro_tile_column_index = x / 128; - u64 macro_tile_index = - (macro_tile_row_index * macro_tiles_per_row) + macro_tile_column_index; - u64 macro_tile_offset = macro_tile_index * macro_tile_bytes; - u64 macro_tiles_per_slice = macro_tiles_per_row * (m_padded_height / m_macro_tile_height); - u64 slice_bytes = macro_tiles_per_slice * macro_tile_bytes; - u64 slice_offset = tile_split_slice * slice_bytes; - u64 tile_row_index = (y / 8) % m_bank_height; - u64 tile_index = tile_row_index; - u64 tile_offset = tile_index * tile_bytes; - - u64 tile_split_slice_rotation = ((m_num_banks / 2) + 1) * tile_split_slice; - bank ^= tile_split_slice_rotation; - bank &= (m_num_banks - 1); - - u64 total_offset = (slice_offset + macro_tile_offset + tile_offset) * 8 + element_offset; - u64 bit_offset = total_offset & 0x7u; - total_offset /= 8; - - u64 pipe_interleave_offset = total_offset & 0xffu; - u64 offset = total_offset >> 8u; - u64 byte_offset = pipe_interleave_offset | (pipe << (8u)) | (bank << (8u + m_pipe_bits)) | - (offset << (8u + m_pipe_bits + m_bank_bits)); - - return ((byte_offset << 3u) | bit_offset) / 8; - } -}; - -void ConvertTileToLinear(u8* dst, const u8* src, u32 width, u32 height, bool is_neo) { - TileManager32 t; - t.Init(width, height, is_neo); - - for (u32 y = 0; y < height; y++) { - u32 x = 0; - u64 linear_offset = y * width * 4; - - for (; x + 1 < width; x += 2) { - auto tiled_offset = t.getTiledOffs(x, y, is_neo); - - std::memcpy(dst + linear_offset, src + tiled_offset, sizeof(u64)); - linear_offset += 8; - } - if (x < width) { - auto tiled_offset = t.getTiledOffs(x, y, is_neo); - std::memcpy(dst + linear_offset, src + tiled_offset, sizeof(u32)); - } - } -} - -vk::Format DemoteImageFormatForDetiling(vk::Format format) { +static vk::Format DemoteImageFormatForDetiling(vk::Format format) { switch (format) { case vk::Format::eR8Uint: case vk::Format::eR8Unorm: @@ -253,12 +103,9 @@ const DetilerContext* TileManager::GetDetiler(const Image& image) const { case AmdGpu::TilingMode::Texture_Volume: switch (format) { case vk::Format::eR32Uint: - if (image.info.size.width % 8 == 0 && image.info.size.height % 8 == 0) { - return &detilers[DetilerType::Macro32x1]; - } else { - // Need to test non-tile aligned images - return nullptr; - } + return &detilers[DetilerType::Macro32x1]; + case vk::Format::eR32G32Uint: + return &detilers[DetilerType::Macro32x2]; default: return nullptr; } @@ -272,16 +119,16 @@ struct DetilerParams { u32 num_levels; u32 pitch0; u32 height; - u32 depth; u32 sizes[14]; }; TileManager::TileManager(const Vulkan::Instance& instance, Vulkan::Scheduler& scheduler) : instance{instance}, scheduler{scheduler} { static const std::array detiler_shaders{ - HostShaders::DETILE_M8X1_COMP, HostShaders::DETILE_M8X2_COMP, - HostShaders::DETILE_M32X1_COMP, HostShaders::DETILE_M32X2_COMP, - HostShaders::DETILE_M32X4_COMP, HostShaders::DETILE_MACRO32X1_COMP, + HostShaders::DETILE_M8X1_COMP, HostShaders::DETILE_M8X2_COMP, + HostShaders::DETILE_M32X1_COMP, HostShaders::DETILE_M32X2_COMP, + HostShaders::DETILE_M32X4_COMP, HostShaders::DETILE_MACRO32X1_COMP, + HostShaders::DETILE_MACRO32X2_COMP, }; boost::container::static_vector bindings{ @@ -465,16 +312,24 @@ std::pair TileManager::TryDetile(vk::Buffer in_buffer, u32 in_o set_writes); DetilerParams params; + params.num_levels = image.info.resources.levels; params.pitch0 = image.info.pitch >> (image.info.props.is_block ? 2u : 0u); params.height = image.info.size.height; - params.depth = image.info.size.depth; - params.num_levels = image.info.resources.levels; + if (image.info.tiling_mode == AmdGpu::TilingMode::Texture_Volume) { + ASSERT(image.info.resources.levels == 1); + ASSERT(image.info.num_bits >= 32); + const auto tiles_per_row = image.info.pitch / 8u; + const auto tiles_per_slice = tiles_per_row * ((image.info.size.height + 7u) / 8u); + params.sizes[0] = tiles_per_row; + params.sizes[1] = tiles_per_slice; + } else { - ASSERT(image.info.resources.levels <= 14); - std::memset(¶ms.sizes, 0, sizeof(params.sizes)); - for (int m = 0; m < image.info.resources.levels; ++m) { - params.sizes[m] = image.info.mips_layout[m].size * image.info.resources.layers + - (m > 0 ? params.sizes[m - 1] : 0); + ASSERT(image.info.resources.levels <= 14); + std::memset(¶ms.sizes, 0, sizeof(params.sizes)); + for (int m = 0; m < image.info.resources.levels; ++m) { + params.sizes[m] = image.info.mips_layout[m].size * image.info.resources.layers + + (m > 0 ? params.sizes[m - 1] : 0); + } } cmdbuf.pushConstants(*detiler->pl_layout, vk::ShaderStageFlagBits::eCompute, 0u, sizeof(params), diff --git a/src/video_core/texture_cache/tile_manager.h b/src/video_core/texture_cache/tile_manager.h index 7db6d914..fafee0f8 100644 --- a/src/video_core/texture_cache/tile_manager.h +++ b/src/video_core/texture_cache/tile_manager.h @@ -25,6 +25,7 @@ enum DetilerType : u32 { Micro32x4, Macro32x1, + Macro32x2, Max };