| 1 |
/* |
| 2 |
* Copyright (C) 2022 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "pan_tiling.h" |
| 7 |
|
| 8 |
#include <gtest/gtest.h> |
| 9 |
|
| 10 |
/* |
| 11 |
* Reference tiling algorithm, written for clarity rather than performance. See |
| 12 |
* docs/drivers/panfrost.rst for details on the format. |
| 13 |
*/ |
| 14 |
|
| 15 |
static unsigned |
| 16 |
u_order(unsigned x, unsigned y) |
| 17 |
{ |
| 18 |
assert(x < 16 && y < 16); |
| 19 |
|
| 20 |
unsigned xy0 = ((x ^ y) & 1) ? 1 : 0; |
| 21 |
unsigned xy1 = ((x ^ y) & 2) ? 1 : 0; |
| 22 |
unsigned xy2 = ((x ^ y) & 4) ? 1 : 0; |
| 23 |
unsigned xy3 = ((x ^ y) & 8) ? 1 : 0; |
| 24 |
|
| 25 |
unsigned y0 = (y & 1) ? 1 : 0; |
| 26 |
unsigned y1 = (y & 2) ? 1 : 0; |
| 27 |
unsigned y2 = (y & 4) ? 1 : 0; |
| 28 |
unsigned y3 = (y & 8) ? 1 : 0; |
| 29 |
|
| 30 |
return (xy0 << 0) | (y0 << 1) | (xy1 << 2) | (y1 << 3) | (xy2 << 4) | |
| 31 |
(y2 << 5) | (xy3 << 6) | (y3 << 7); |
| 32 |
} |
| 33 |
|
| 34 |
/* x/y are in blocks */ |
| 35 |
static unsigned |
| 36 |
tiled_offset(unsigned x, unsigned y, unsigned stride, unsigned tilesize, |
| 37 |
unsigned blocksize) |
| 38 |
{ |
| 39 |
unsigned tile_x = x / tilesize; |
| 40 |
unsigned tile_y = y / tilesize; |
| 41 |
|
| 42 |
unsigned x_in_tile = x % tilesize; |
| 43 |
unsigned y_in_tile = y % tilesize; |
| 44 |
|
| 45 |
unsigned index_in_tile = u_order(x_in_tile, y_in_tile); |
| 46 |
|
| 47 |
unsigned row_offset = tile_y * stride; |
| 48 |
unsigned col_offset = (tile_x * tilesize * tilesize) * blocksize; |
| 49 |
unsigned block_offset = index_in_tile * blocksize; |
| 50 |
|
| 51 |
return row_offset + col_offset + block_offset; |
| 52 |
} |
| 53 |
|
| 54 |
static unsigned |
| 55 |
linear_offset(unsigned x, unsigned y, unsigned stride, unsigned blocksize) |
| 56 |
{ |
| 57 |
return (stride * y) + (x * blocksize); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
ref_access_tiled(void *dst, const void *src, unsigned region_x, |
| 62 |
unsigned region_y, unsigned w, unsigned h, uint32_t dst_stride, |
| 63 |
uint32_t src_stride, enum pipe_format format, |
| 64 |
bool dst_is_tiled) |
| 65 |
{ |
| 66 |
const struct util_format_description *desc = util_format_description(format); |
| 67 |
; |
| 68 |
|
| 69 |
unsigned tilesize = (desc->block.width > 1) ? 4 : 16; |
| 70 |
unsigned blocksize = (desc->block.bits / 8); |
| 71 |
|
| 72 |
unsigned w_block = w / desc->block.width; |
| 73 |
unsigned h_block = h / desc->block.height; |
| 74 |
|
| 75 |
unsigned region_x_block = region_x / desc->block.width; |
| 76 |
unsigned region_y_block = region_y / desc->block.height; |
| 77 |
|
| 78 |
for (unsigned linear_y_block = 0; linear_y_block < h_block; |
| 79 |
++linear_y_block) { |
| 80 |
for (unsigned linear_x_block = 0; linear_x_block < w_block; |
| 81 |
++linear_x_block) { |
| 82 |
|
| 83 |
unsigned tiled_x_block = region_x_block + linear_x_block; |
| 84 |
unsigned tiled_y_block = region_y_block + linear_y_block; |
| 85 |
|
| 86 |
unsigned dst_offset, src_offset; |
| 87 |
|
| 88 |
if (dst_is_tiled) { |
| 89 |
dst_offset = tiled_offset(tiled_x_block, tiled_y_block, dst_stride, |
| 90 |
tilesize, blocksize); |
| 91 |
src_offset = linear_offset(linear_x_block, linear_y_block, |
| 92 |
src_stride, blocksize); |
| 93 |
} else { |
| 94 |
dst_offset = linear_offset(linear_x_block, linear_y_block, |
| 95 |
dst_stride, blocksize); |
| 96 |
src_offset = tiled_offset(tiled_x_block, tiled_y_block, src_stride, |
| 97 |
tilesize, blocksize); |
| 98 |
} |
| 99 |
|
| 100 |
memcpy((uint8_t *)dst + dst_offset, (const uint8_t *)src + src_offset, |
| 101 |
desc->block.bits / 8); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* Helper to build test cases for tiled texture access. This test suite compares |
| 108 |
* the above reference tiling algorithm to the optimized algorithm used in |
| 109 |
* production. |
| 110 |
*/ |
| 111 |
static void |
| 112 |
test(unsigned width, unsigned height, unsigned rx, unsigned ry, unsigned rw, |
| 113 |
unsigned rh, unsigned linear_stride, enum pipe_format format, bool store) |
| 114 |
{ |
| 115 |
unsigned bpp = util_format_get_blocksize(format); |
| 116 |
unsigned tile_height = util_format_is_compressed(format) ? 4 : 16; |
| 117 |
|
| 118 |
unsigned tiled_width = ALIGN_POT(width, 16); |
| 119 |
unsigned tiled_height = ALIGN_POT(height, 16); |
| 120 |
unsigned tiled_stride = tiled_width * tile_height * bpp; |
| 121 |
|
| 122 |
unsigned dst_stride = store ? tiled_stride : linear_stride; |
| 123 |
unsigned src_stride = store ? linear_stride : tiled_stride; |
| 124 |
|
| 125 |
void *tiled = calloc(bpp, tiled_width * tiled_height); |
| 126 |
void *linear = calloc(bpp, rw * linear_stride); |
| 127 |
void *ref = |
| 128 |
calloc(bpp, store ? (tiled_width * tiled_height) : (rw * linear_stride)); |
| 129 |
|
| 130 |
if (store) { |
| 131 |
for (unsigned i = 0; i < bpp * rw * linear_stride; ++i) { |
| 132 |
((uint8_t *)linear)[i] = (i & 0xFF); |
| 133 |
} |
| 134 |
|
| 135 |
pan_store_tiled_image(tiled, linear, rx, ry, rw, rh, dst_stride, |
| 136 |
src_stride, format, PAN_INTERLEAVE_NONE); |
| 137 |
} else { |
| 138 |
for (unsigned i = 0; i < bpp * tiled_width * tiled_height; ++i) { |
| 139 |
((uint8_t *)tiled)[i] = (i & 0xFF); |
| 140 |
} |
| 141 |
|
| 142 |
pan_load_tiled_image(linear, tiled, rx, ry, rw, rh, dst_stride, |
| 143 |
src_stride, format, PAN_INTERLEAVE_NONE); |
| 144 |
} |
| 145 |
|
| 146 |
ref_access_tiled(ref, store ? linear : tiled, rx, ry, rw, rh, dst_stride, |
| 147 |
src_stride, format, store); |
| 148 |
|
| 149 |
if (store) |
| 150 |
EXPECT_EQ(memcmp(ref, tiled, bpp * tiled_width * tiled_height), 0); |
| 151 |
else |
| 152 |
EXPECT_EQ(memcmp(ref, linear, bpp * rw * linear_stride), 0); |
| 153 |
|
| 154 |
free(ref); |
| 155 |
free(tiled); |
| 156 |
free(linear); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
test_ldst(unsigned width, unsigned height, unsigned rx, unsigned ry, |
| 161 |
unsigned rw, unsigned rh, unsigned linear_stride, |
| 162 |
enum pipe_format format) |
| 163 |
{ |
| 164 |
test(width, height, rx, ry, rw, rh, linear_stride, format, true); |
| 165 |
test(width, height, rx, ry, rw, rh, linear_stride, format, false); |
| 166 |
} |
| 167 |
|
| 168 |
TEST(UInterleavedTiling, RegulatFormats) |
| 169 |
{ |
| 170 |
/* 8-bit */ |
| 171 |
test_ldst(23, 17, 0, 0, 23, 17, 23, PIPE_FORMAT_R8_UINT); |
| 172 |
|
| 173 |
/* 16-bit */ |
| 174 |
test_ldst(23, 17, 0, 0, 23, 17, 23 * 2, PIPE_FORMAT_R8G8_UINT); |
| 175 |
|
| 176 |
/* 24-bit */ |
| 177 |
test_ldst(23, 17, 0, 0, 23, 17, 23 * 3, PIPE_FORMAT_R8G8B8_UINT); |
| 178 |
|
| 179 |
/* 32-bit */ |
| 180 |
test_ldst(23, 17, 0, 0, 23, 17, 23 * 4, PIPE_FORMAT_R32_UINT); |
| 181 |
|
| 182 |
/* 48-bit */ |
| 183 |
test_ldst(23, 17, 0, 0, 23, 17, 23 * 6, PIPE_FORMAT_R16G16B16_UINT); |
| 184 |
|
| 185 |
/* 64-bit */ |
| 186 |
test_ldst(23, 17, 0, 0, 23, 17, 23 * 8, PIPE_FORMAT_R32G32_UINT); |
| 187 |
|
| 188 |
/* 96-bit */ |
| 189 |
test_ldst(23, 17, 0, 0, 23, 17, 23 * 12, PIPE_FORMAT_R32G32B32_UINT); |
| 190 |
|
| 191 |
/* 128-bit */ |
| 192 |
test_ldst(23, 17, 0, 0, 23, 17, 23 * 16, PIPE_FORMAT_R32G32B32A32_UINT); |
| 193 |
} |
| 194 |
|
| 195 |
TEST(UInterleavedTiling, UnpackedStrides) |
| 196 |
{ |
| 197 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 1, PIPE_FORMAT_R8_SINT); |
| 198 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 2, PIPE_FORMAT_R8G8_SINT); |
| 199 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 3, PIPE_FORMAT_R8G8B8_SINT); |
| 200 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 4, PIPE_FORMAT_R32_SINT); |
| 201 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 6, PIPE_FORMAT_R16G16B16_SINT); |
| 202 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 8, PIPE_FORMAT_R32G32_SINT); |
| 203 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 12, PIPE_FORMAT_R32G32B32_SINT); |
| 204 |
test_ldst(23, 17, 0, 0, 23, 17, 369 * 16, PIPE_FORMAT_R32G32B32A32_SINT); |
| 205 |
} |
| 206 |
|
| 207 |
TEST(UInterleavedTiling, PartialAccess) |
| 208 |
{ |
| 209 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 1, PIPE_FORMAT_R8_UNORM); |
| 210 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 2, PIPE_FORMAT_R8G8_UNORM); |
| 211 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 3, PIPE_FORMAT_R8G8B8_UNORM); |
| 212 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 4, PIPE_FORMAT_R32_UNORM); |
| 213 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 6, PIPE_FORMAT_R16G16B16_UNORM); |
| 214 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 8, PIPE_FORMAT_R32G32_UNORM); |
| 215 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 12, PIPE_FORMAT_R32G32B32_UNORM); |
| 216 |
test_ldst(23, 17, 3, 1, 13, 7, 369 * 16, PIPE_FORMAT_R32G32B32A32_UNORM); |
| 217 |
} |
| 218 |
|
| 219 |
TEST(UInterleavedTiling, ETC) |
| 220 |
{ |
| 221 |
/* Block alignment assumed */ |
| 222 |
test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_ETC1_RGB8); |
| 223 |
test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_ETC2_RGB8A1); |
| 224 |
test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_ETC2_RG11_SNORM); |
| 225 |
} |
| 226 |
|
| 227 |
TEST(UInterleavedTiling, PartialETC) |
| 228 |
{ |
| 229 |
/* Block alignment assumed */ |
| 230 |
test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_ETC1_RGB8); |
| 231 |
test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_ETC2_RGB8A1); |
| 232 |
test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_ETC2_RG11_SNORM); |
| 233 |
} |
| 234 |
|
| 235 |
TEST(UInterleavedTiling, DXT) |
| 236 |
{ |
| 237 |
/* Block alignment assumed */ |
| 238 |
test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_DXT1_RGB); |
| 239 |
test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_DXT3_RGBA); |
| 240 |
test_ldst(32, 32, 0, 0, 32, 32, 512, PIPE_FORMAT_DXT5_RGBA); |
| 241 |
} |
| 242 |
|
| 243 |
TEST(UInterleavedTiling, PartialDXT) |
| 244 |
{ |
| 245 |
/* Block alignment assumed */ |
| 246 |
test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_DXT1_RGB); |
| 247 |
test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_DXT3_RGBA); |
| 248 |
test_ldst(32, 32, 4, 8, 16, 12, 512, PIPE_FORMAT_DXT5_RGBA); |
| 249 |
} |
| 250 |
|
| 251 |
TEST(UInterleavedTiling, ASTC) |
| 252 |
{ |
| 253 |
/* Block alignment assumed */ |
| 254 |
test_ldst(40, 40, 0, 0, 40, 40, 512, PIPE_FORMAT_ASTC_4x4); |
| 255 |
test_ldst(50, 40, 0, 0, 50, 40, 512, PIPE_FORMAT_ASTC_5x4); |
| 256 |
test_ldst(50, 50, 0, 0, 50, 50, 512, PIPE_FORMAT_ASTC_5x5); |
| 257 |
} |
| 258 |
|
| 259 |
TEST(UInterleavedTiling, PartialASTC) |
| 260 |
{ |
| 261 |
/* Block alignment assumed */ |
| 262 |
test_ldst(40, 40, 4, 4, 16, 8, 512, PIPE_FORMAT_ASTC_4x4); |
| 263 |
test_ldst(50, 40, 5, 4, 10, 8, 512, PIPE_FORMAT_ASTC_5x4); |
| 264 |
test_ldst(50, 50, 5, 5, 10, 10, 512, PIPE_FORMAT_ASTC_5x5); |
| 265 |
} |