From a5fdc8bbf9dccff7af405ea780b08aa783b477fd Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 22 Nov 2026 17:56:49 +0000 Subject: [PATCH] Cache GFX10 tile X offsets and per-row Y contributions Avoid per-element bit interleaving in tile/detile copies; retain element_offset as the independent reference and validate decoded output against it. AI-assisted. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- runtime/src/gfx10_tiling.h | 18 +++++++++++++++++- runtime/test/gfx10_tiling_test.cpp | 9 +++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/runtime/src/gfx10_tiling.h b/runtime/src/gfx10_tiling.h index 2372d716b..d869d1102 100644 --- a/runtime/src/gfx10_tiling.h +++ b/runtime/src/gfx10_tiling.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -122,10 +123,25 @@ private: if (source.size() < (to_tiled ? linear_size : size_) || destination.size() < (to_tiled ? size_ : linear_size) || tiling_detail::overlap(source, destination)) return false; + const auto tile_x_bits = std::countr_zero(tile_width_); + std::size_t x_offsets[256]{}; + for (std::size_t x = 0; x < tile_width_; ++x) { + for (unsigned bit = 0; bit < bits_; ++bit) { + const int source_bit = pattern_[bit]; + if (source_bit > 0) x_offsets[x] |= ((x >> (source_bit - 1)) & 1) << bit; + } + } for (std::size_t y = 0; y < height_; ++y) { + std::size_t y_offset = 0; + for (unsigned bit = 0; bit < bits_; ++bit) { + const int source_bit = pattern_[bit]; + if (source_bit < 0) y_offset |= ((y >> (-source_bit - 1)) & 1) << bit; + } + const std::size_t tile_row = (y / tile_height_) * tiles_x_; for (std::size_t x = 0; x < width_; ++x) { const std::size_t linear = y * pitch + x * element_bytes_; - const std::size_t tiled = address(x, y); + const std::size_t tiled = ((tile_row + (x >> tile_x_bits)) << bits_) + + y_offset + x_offsets[x & (tile_width_ - 1)]; std::memcpy(destination.data() + (to_tiled ? tiled : linear), source.data() + (to_tiled ? linear : tiled), element_bytes_); } diff --git a/runtime/test/gfx10_tiling_test.cpp b/runtime/test/gfx10_tiling_test.cpp index d951f6eac..fd0d14779 100644 --- a/runtime/test/gfx10_tiling_test.cpp +++ b/runtime/test/gfx10_tiling_test.cpp @@ -50,6 +50,15 @@ int check_surface(TileMode mode, std::size_t bpe, std::size_t width, std::size_t for (std::size_t y = 0; y < height; ++y) std::copy_n(linear.begin() + y * pitch, row_bytes, expected.begin() + y * pitch); CHECK(decoded == expected); + std::vector reference(linear_size, std::byte{0xab}); + for (std::size_t y = 0; y < height; ++y) { + for (std::size_t x = 0; x < width; ++x) { + std::size_t offset; + CHECK(surface.element_offset(x, y, offset)); + std::copy_n(tiled.begin() + offset, bpe, reference.begin() + y * pitch + x * bpe); + } + } + CHECK(decoded == reference); std::vector encoded(tiled.size(), std::byte{0xcc}); CHECK(surface.tile(decoded, encoded, pitch)); CHECK(encoded == tiled);