| 1 |
/* |
| 2 |
* Copyright (c) 2011-2013 Luc Verhaegen <[email protected]> |
| 3 |
* Copyright (c) 2018 Alyssa Rosenzweig |
| 4 |
* Copyright (c) 2018 Vasily Khoruzhick <[email protected]> |
| 5 |
* Copyright (c) 2019 Collabora, Ltd. |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "pan_tiling.h" |
| 10 |
#include <math.h> |
| 11 |
#include <stdbool.h> |
| 12 |
#include "util/macros.h" |
| 13 |
#include "util/ralloc.h" |
| 14 |
|
| 15 |
/* |
| 16 |
* This file implements software encode/decode of u-interleaved textures. |
| 17 |
* See docs/drivers/panfrost.rst for details on the format. |
| 18 |
* |
| 19 |
* The tricky bit is ordering along the space-filling curve: |
| 20 |
* |
| 21 |
* | y3 | (x3 ^ y3) | y2 | (y2 ^ x2) | y1 | (y1 ^ x1) | y0 | (y0 ^ x0) | |
| 22 |
* |
| 23 |
* While interleaving bits is trivial in hardware, it is nontrivial in software. |
| 24 |
* The trick is to divide the pattern up: |
| 25 |
* |
| 26 |
* | y3 | y3 | y2 | y2 | y1 | y1 | y0 | y0 | |
| 27 |
* ^ | 0 | x3 | 0 | x2 | 0 | x1 | 0 | x0 | |
| 28 |
* |
| 29 |
* That is, duplicate the bits of the Y and space out the bits of the X. The top |
| 30 |
* line is a function only of Y, so it can be calculated once per row and stored |
| 31 |
* in a register. The bottom line is simply X with the bits spaced out. Spacing |
| 32 |
* out the X is easy enough with a LUT, or by subtracting+ANDing the mask |
| 33 |
* pattern (abusing carry bits). |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* Given the lower 4-bits of the Y coordinate, we would like to |
| 39 |
* duplicate every bit over. So instead of 0b1010, we would like |
| 40 |
* 0b11001100. The idea is that for the bits in the solely Y place, we |
| 41 |
* get a Y place, and the bits in the XOR place *also* get a Y. |
| 42 |
*/ |
| 43 |
/* clang-format off */ |
| 44 |
const uint32_t bit_duplication[16] = { |
| 45 |
0b00000000, |
| 46 |
0b00000011, |
| 47 |
0b00001100, |
| 48 |
0b00001111, |
| 49 |
0b00110000, |
| 50 |
0b00110011, |
| 51 |
0b00111100, |
| 52 |
0b00111111, |
| 53 |
0b11000000, |
| 54 |
0b11000011, |
| 55 |
0b11001100, |
| 56 |
0b11001111, |
| 57 |
0b11110000, |
| 58 |
0b11110011, |
| 59 |
0b11111100, |
| 60 |
0b11111111, |
| 61 |
}; |
| 62 |
/* clang-format on */ |
| 63 |
|
| 64 |
/* |
| 65 |
* Space the bits out of a 4-bit nibble |
| 66 |
*/ |
| 67 |
/* clang-format off */ |
| 68 |
const unsigned space_4[16] = { |
| 69 |
0b0000000, |
| 70 |
0b0000001, |
| 71 |
0b0000100, |
| 72 |
0b0000101, |
| 73 |
0b0010000, |
| 74 |
0b0010001, |
| 75 |
0b0010100, |
| 76 |
0b0010101, |
| 77 |
0b1000000, |
| 78 |
0b1000001, |
| 79 |
0b1000100, |
| 80 |
0b1000101, |
| 81 |
0b1010000, |
| 82 |
0b1010001, |
| 83 |
0b1010100, |
| 84 |
0b1010101 |
| 85 |
}; |
| 86 |
/* clang-format on */ |
| 87 |
|
| 88 |
/* The scheme uses 16x16 tiles */ |
| 89 |
|
| 90 |
#define TILE_WIDTH 16 |
| 91 |
#define TILE_HEIGHT 16 |
| 92 |
#define PIXELS_PER_TILE (TILE_WIDTH * TILE_HEIGHT) |
| 93 |
|
| 94 |
enum pan_interleave_zs |
| 95 |
pan_get_interleave_zs(enum pipe_format format, bool depth, bool stencil) |
| 96 |
{ |
| 97 |
if (format == PIPE_FORMAT_Z24_UNORM_S8_UINT) { |
| 98 |
if (depth && stencil) { |
| 99 |
return PAN_INTERLEAVE_NONE; |
| 100 |
} else if (depth && !stencil) { |
| 101 |
return PAN_INTERLEAVE_DEPTH; |
| 102 |
} else if (!depth && stencil) { |
| 103 |
return PAN_INTERLEAVE_STENCIL; |
| 104 |
} else if (!depth && !stencil) { |
| 105 |
UNREACHABLE("at least one aspect must be specified"); |
| 106 |
} |
| 107 |
} |
| 108 |
return PAN_INTERLEAVE_NONE; |
| 109 |
} |
| 110 |
|
| 111 |
/* Optimized routine to tile an aligned (w & 0xF == 0) texture. Explanation: |
| 112 |
* |
| 113 |
* dest_start precomputes the offset to the beginning of the first horizontal |
| 114 |
* tile we're writing to, knowing that x is 16-aligned. Tiles themselves are |
| 115 |
* stored linearly, so we get the X tile number by shifting and then multiply |
| 116 |
* by the bytes per tile . |
| 117 |
* |
| 118 |
* We iterate across the pixels we're trying to store in source-order. For each |
| 119 |
* row in the destination image, we figure out which row of 16x16 block we're |
| 120 |
* in, by slicing off the lower 4-bits (block_y). |
| 121 |
* |
| 122 |
* dest then precomputes the location of the top-left corner of the block the |
| 123 |
* row starts in. In pixel coordinates (where the origin is the top-left), |
| 124 |
* (block_y, 0) is the top-left corner of the leftmost tile in this row. While |
| 125 |
* pixels are reordered within a block, the blocks themselves are stored |
| 126 |
* linearly, so multiplying block_y by the pixel stride of the destination |
| 127 |
* image equals the byte offset of that top-left corner of the block this row |
| 128 |
* is in. |
| 129 |
* |
| 130 |
* On the other hand, the source is linear so we compute the locations of the |
| 131 |
* start and end of the row in the source by a simple linear addressing. |
| 132 |
* |
| 133 |
* For indexing within the tile, we need to XOR with the [y3 y3 y2 y2 y1 y1 y0 |
| 134 |
* y0] value. Since this is constant across a row, we look it up per-row and |
| 135 |
* store in expanded_y. |
| 136 |
* |
| 137 |
* Finally, we iterate each row in source order. In the outer loop, we iterate |
| 138 |
* each 16 pixel tile. Within each tile, we iterate the 16 pixels (this should |
| 139 |
* be unrolled), calculating the index within the tile and writing. |
| 140 |
*/ |
| 141 |
|
| 142 |
static ALWAYS_INLINE void |
| 143 |
pan_access_tiled_image_aligned( |
| 144 |
void *dst, void *src, |
| 145 |
unsigned dst_pixel_size, unsigned src_pixel_size, |
| 146 |
unsigned shift, |
| 147 |
uint16_t sx, uint16_t sy, |
| 148 |
uint16_t w, uint16_t h, |
| 149 |
uint32_t dst_stride, uint32_t src_stride, |
| 150 |
enum pan_interleave_zs interleave, |
| 151 |
bool is_store) |
| 152 |
{ |
| 153 |
uint8_t *dest_start = dst + ((sx >> 4) * PIXELS_PER_TILE * dst_pixel_size); |
| 154 |
for (int y = sy, src_y = 0; src_y < h; ++y, ++src_y) { |
| 155 |
uint8_t *dest = (uint8_t *)(dest_start + ((y >> 4) * dst_stride)); |
| 156 |
void *source = src + (src_y * src_stride); |
| 157 |
void *source_end = source + w * src_pixel_size; |
| 158 |
unsigned expanded_y = bit_duplication[y & 0xF] << shift; |
| 159 |
for (; source < source_end; dest += (PIXELS_PER_TILE << shift)) { |
| 160 |
for (uint8_t i = 0; i < 16; ++i) { |
| 161 |
unsigned index = expanded_y ^ (space_4[i] << shift); |
| 162 |
pan_access_image_pixel(dest + index, source, dst_pixel_size, |
| 163 |
interleave, is_store); |
| 164 |
source += src_pixel_size; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static ALWAYS_INLINE void |
| 171 |
pan_access_tiled_image_unaligned( |
| 172 |
void *dst, void *src, |
| 173 |
unsigned dst_pixel_size, unsigned src_pixel_size, |
| 174 |
unsigned tile_shift, |
| 175 |
uint16_t sx, uint16_t sy, |
| 176 |
uint16_t w, uint16_t h, |
| 177 |
uint32_t dst_stride, uint32_t src_stride, |
| 178 |
enum pan_interleave_zs interleave, |
| 179 |
bool is_store) |
| 180 |
{ |
| 181 |
const unsigned mask = (1 << tile_shift) - 1; |
| 182 |
for (int y = sy, src_y = 0; src_y < h; ++y, ++src_y) { |
| 183 |
unsigned block_start_s = (y >> tile_shift) * dst_stride; |
| 184 |
unsigned source_start = src_y * src_stride; |
| 185 |
unsigned expanded_y = bit_duplication[y & mask]; |
| 186 |
|
| 187 |
for (int x = sx, src_x = 0; src_x < w; ++x, ++src_x) { |
| 188 |
unsigned block_x_s = (x >> tile_shift) * (1 << (tile_shift * 2)); |
| 189 |
unsigned index = expanded_y ^ space_4[x & mask]; |
| 190 |
uint8_t *source = src + source_start + src_pixel_size * src_x; |
| 191 |
uint8_t *dest = |
| 192 |
dst + block_start_s + dst_pixel_size * (block_x_s + index); |
| 193 |
pan_access_image_pixel(dest, source, dst_pixel_size, interleave, |
| 194 |
is_store); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
#define TILED_UNALIGNED_VARIANT(dst_bpp, src_bpp, interleave, store, shift) \ |
| 200 |
pan_access_tiled_image_unaligned(dst, src, (dst_bpp) / 8, (src_bpp) / 8, \ |
| 201 |
shift, sx, sy, w, h, \ |
| 202 |
dst_stride, src_stride, interleave, store) |
| 203 |
|
| 204 |
/* We have a separate call for each configuration, to ensure that the inlined |
| 205 |
* function is specialized */ |
| 206 |
#define TILED_UNALIGNED_VARIANTS(store, shift) \ |
| 207 |
{ \ |
| 208 |
if (bpp == 8) \ |
| 209 |
TILED_UNALIGNED_VARIANT(8, 8, PAN_INTERLEAVE_NONE, store, shift); \ |
| 210 |
else if (bpp == 16) \ |
| 211 |
TILED_UNALIGNED_VARIANT(16, 16, PAN_INTERLEAVE_NONE, store, shift); \ |
| 212 |
else if (bpp == 24) \ |
| 213 |
TILED_UNALIGNED_VARIANT(24, 24, PAN_INTERLEAVE_NONE, store, shift); \ |
| 214 |
else if (bpp == 32 && interleave == PAN_INTERLEAVE_NONE) \ |
| 215 |
TILED_UNALIGNED_VARIANT(32, 32, PAN_INTERLEAVE_NONE, store, shift); \ |
| 216 |
else if (bpp == 32 && interleave == PAN_INTERLEAVE_DEPTH) \ |
| 217 |
TILED_UNALIGNED_VARIANT(32, 32, PAN_INTERLEAVE_DEPTH, store, shift); \ |
| 218 |
else if (bpp == 32 && interleave == PAN_INTERLEAVE_STENCIL) \ |
| 219 |
TILED_UNALIGNED_VARIANT(32, 8, PAN_INTERLEAVE_STENCIL, store, shift); \ |
| 220 |
else if (bpp == 48) \ |
| 221 |
TILED_UNALIGNED_VARIANT(48, 48, PAN_INTERLEAVE_NONE, store, shift); \ |
| 222 |
else if (bpp == 64) \ |
| 223 |
TILED_UNALIGNED_VARIANT(64, 64, PAN_INTERLEAVE_NONE, store, shift); \ |
| 224 |
else if (bpp == 96) \ |
| 225 |
TILED_UNALIGNED_VARIANT(96, 96, PAN_INTERLEAVE_NONE, store, shift); \ |
| 226 |
else if (bpp == 128) \ |
| 227 |
TILED_UNALIGNED_VARIANT(128, 128, PAN_INTERLEAVE_NONE, store, shift); \ |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
* Perform a generic access to a tiled image with a given format. This works |
| 232 |
* even for block-compressed images on entire blocks at a time. sx/sy/w/h are |
| 233 |
* specified in pixels, not blocks, but our internal routines work in blocks, |
| 234 |
* so we divide here. Alignment is assumed. |
| 235 |
*/ |
| 236 |
static void |
| 237 |
pan_access_tiled_image_generic(void *dst, void *src, unsigned sx, unsigned sy, |
| 238 |
unsigned w, unsigned h, uint32_t dst_stride, |
| 239 |
uint32_t src_stride, |
| 240 |
const struct util_format_description *desc, |
| 241 |
enum pan_interleave_zs interleave, |
| 242 |
bool _is_store) |
| 243 |
{ |
| 244 |
unsigned bpp = desc->block.bits; |
| 245 |
|
| 246 |
/* Convert units */ |
| 247 |
sx /= desc->block.width; |
| 248 |
sy /= desc->block.height; |
| 249 |
w = DIV_ROUND_UP(w, desc->block.width); |
| 250 |
h = DIV_ROUND_UP(h, desc->block.height); |
| 251 |
|
| 252 |
if (desc->block.width > 1) { |
| 253 |
if (_is_store) |
| 254 |
TILED_UNALIGNED_VARIANTS(true, 2) |
| 255 |
else |
| 256 |
TILED_UNALIGNED_VARIANTS(false, 2) |
| 257 |
} else { |
| 258 |
if (_is_store) |
| 259 |
TILED_UNALIGNED_VARIANTS(true, 4) |
| 260 |
else |
| 261 |
TILED_UNALIGNED_VARIANTS(false, 4) |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
#define TILED_ALIGNED_VARIANT(interleave, store, dst_bpp, src_bpp, shift) \ |
| 266 |
pan_access_tiled_image_aligned(dst, src, (dst_bpp) / 8, (src_bpp) / 8, \ |
| 267 |
shift, sx, sy, w, h, \ |
| 268 |
dst_stride, src_stride, interleave, store) |
| 269 |
|
| 270 |
#define TILED_ALIGNED_VARIANTS(store) \ |
| 271 |
{ \ |
| 272 |
if (bpp == 8) \ |
| 273 |
TILED_ALIGNED_VARIANT(PAN_INTERLEAVE_NONE, store, 8, 8, 0); \ |
| 274 |
else if (bpp == 16) \ |
| 275 |
TILED_ALIGNED_VARIANT(PAN_INTERLEAVE_NONE, store, 16, 16, 1); \ |
| 276 |
else if (bpp == 32 && interleave == PAN_INTERLEAVE_NONE) \ |
| 277 |
TILED_ALIGNED_VARIANT(PAN_INTERLEAVE_NONE, store, 32, 32, 2); \ |
| 278 |
else if (bpp == 32 && interleave == PAN_INTERLEAVE_DEPTH) \ |
| 279 |
TILED_ALIGNED_VARIANT(PAN_INTERLEAVE_DEPTH, store, 32, 32, 2); \ |
| 280 |
else if (bpp == 32 && interleave == PAN_INTERLEAVE_STENCIL) \ |
| 281 |
TILED_ALIGNED_VARIANT(PAN_INTERLEAVE_STENCIL, store, 32, 8, 2); \ |
| 282 |
else if (bpp == 64) \ |
| 283 |
TILED_ALIGNED_VARIANT(PAN_INTERLEAVE_NONE, store, 64, 64, 3); \ |
| 284 |
else if (bpp == 128) \ |
| 285 |
TILED_ALIGNED_VARIANT(PAN_INTERLEAVE_NONE, store, 128, 128, 4); \ |
| 286 |
} |
| 287 |
|
| 288 |
/* Optimized variant of pan_access_tiled_image_generic except that requires |
| 289 |
* sx/sy/w/h to be tile-aligned, and bpp to be a power of two */ |
| 290 |
static void |
| 291 |
pan_access_tiled_image_generic_aligned( |
| 292 |
void *dst, void *src, unsigned sx, unsigned sy, unsigned w, unsigned h, |
| 293 |
uint32_t dst_stride, uint32_t src_stride, |
| 294 |
const struct util_format_description *desc, |
| 295 |
enum pan_interleave_zs interleave, bool is_store) |
| 296 |
{ |
| 297 |
unsigned bpp = desc->block.bits; |
| 298 |
|
| 299 |
assert(sx % TILE_WIDTH == 0); |
| 300 |
assert(sy % TILE_HEIGHT == 0); |
| 301 |
assert(w % TILE_WIDTH == 0); |
| 302 |
assert(h % TILE_HEIGHT == 0); |
| 303 |
assert(util_is_power_of_two_nonzero(bpp)); |
| 304 |
|
| 305 |
if (is_store) |
| 306 |
TILED_ALIGNED_VARIANTS(true) |
| 307 |
else |
| 308 |
TILED_ALIGNED_VARIANTS(false) |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
#define OFFSET(src, _x, _y) \ |
| 313 |
(void *)((uint8_t *)src + ((_y)-orig_y) * src_stride + \ |
| 314 |
(((_x)-orig_x) * (bpp / 8))) |
| 315 |
|
| 316 |
static ALWAYS_INLINE void |
| 317 |
pan_access_tiled_image(void *dst, void *src, unsigned x, unsigned y, unsigned w, |
| 318 |
unsigned h, uint32_t dst_stride, uint32_t src_stride, |
| 319 |
enum pipe_format format, |
| 320 |
enum pan_interleave_zs interleave, bool is_store) |
| 321 |
{ |
| 322 |
if (interleave != PAN_INTERLEAVE_NONE) |
| 323 |
assert(format == PIPE_FORMAT_Z24_UNORM_S8_UINT); |
| 324 |
|
| 325 |
const struct util_format_description *desc = util_format_description(format); |
| 326 |
unsigned bpp = desc->block.bits; |
| 327 |
|
| 328 |
/* Our optimized routines cannot handle unaligned blocks (without depending |
| 329 |
* on platform-specific behaviour), and there is no good reason to do so. If |
| 330 |
* these assertions fail, there is either a driver bug or a non-portable unit |
| 331 |
* test. |
| 332 |
*/ |
| 333 |
assert((dst_stride % (bpp / 8)) == 0 && "unaligned destination stride"); |
| 334 |
assert((src_stride % (bpp / 8)) == 0 && "unaligned source stride"); |
| 335 |
|
| 336 |
if (desc->block.width > 1 || |
| 337 |
!util_is_power_of_two_nonzero(desc->block.bits)) { |
| 338 |
pan_access_tiled_image_generic(dst, (void *)src, x, y, w, h, dst_stride, |
| 339 |
src_stride, desc, interleave, is_store); |
| 340 |
|
| 341 |
return; |
| 342 |
} |
| 343 |
|
| 344 |
unsigned first_full_tile_x = DIV_ROUND_UP(x, TILE_WIDTH) * TILE_WIDTH; |
| 345 |
unsigned first_full_tile_y = DIV_ROUND_UP(y, TILE_HEIGHT) * TILE_HEIGHT; |
| 346 |
unsigned last_full_tile_x = ((x + w) / TILE_WIDTH) * TILE_WIDTH; |
| 347 |
unsigned last_full_tile_y = ((y + h) / TILE_HEIGHT) * TILE_HEIGHT; |
| 348 |
|
| 349 |
/* First, tile the top portion */ |
| 350 |
|
| 351 |
unsigned orig_x = x, orig_y = y; |
| 352 |
|
| 353 |
if (first_full_tile_y != y) { |
| 354 |
unsigned dist = MIN2(first_full_tile_y - y, h); |
| 355 |
|
| 356 |
pan_access_tiled_image_generic(dst, OFFSET(src, x, y), x, y, w, dist, |
| 357 |
dst_stride, src_stride, desc, interleave, |
| 358 |
is_store); |
| 359 |
|
| 360 |
if (dist == h) |
| 361 |
return; |
| 362 |
|
| 363 |
y += dist; |
| 364 |
h -= dist; |
| 365 |
} |
| 366 |
|
| 367 |
/* Next, the bottom portion */ |
| 368 |
if (last_full_tile_y != (y + h)) { |
| 369 |
unsigned dist = (y + h) - last_full_tile_y; |
| 370 |
|
| 371 |
pan_access_tiled_image_generic(dst, OFFSET(src, x, last_full_tile_y), x, |
| 372 |
last_full_tile_y, w, dist, dst_stride, |
| 373 |
src_stride, desc, interleave, is_store); |
| 374 |
|
| 375 |
h -= dist; |
| 376 |
} |
| 377 |
|
| 378 |
/* The left portion */ |
| 379 |
if (first_full_tile_x != x) { |
| 380 |
unsigned dist = MIN2(first_full_tile_x - x, w); |
| 381 |
|
| 382 |
pan_access_tiled_image_generic(dst, OFFSET(src, x, y), x, y, dist, h, |
| 383 |
dst_stride, src_stride, desc, interleave, |
| 384 |
is_store); |
| 385 |
|
| 386 |
if (dist == w) |
| 387 |
return; |
| 388 |
|
| 389 |
x += dist; |
| 390 |
w -= dist; |
| 391 |
} |
| 392 |
|
| 393 |
/* Finally, the right portion */ |
| 394 |
if (last_full_tile_x != (x + w)) { |
| 395 |
unsigned dist = (x + w) - last_full_tile_x; |
| 396 |
|
| 397 |
pan_access_tiled_image_generic(dst, OFFSET(src, last_full_tile_x, y), |
| 398 |
last_full_tile_x, y, dist, h, dst_stride, |
| 399 |
src_stride, desc, interleave, is_store); |
| 400 |
|
| 401 |
w -= dist; |
| 402 |
} |
| 403 |
|
| 404 |
pan_access_tiled_image_generic_aligned(dst, OFFSET(src, x, y), x, y, w, |
| 405 |
h, dst_stride, src_stride, desc, |
| 406 |
interleave, is_store); |
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Access a tiled image (load or store). Note: the region of interest (x, y, w, |
| 411 |
* h) is specified in pixels, not blocks. It is expected that these quantities |
| 412 |
* are aligned to the block size. |
| 413 |
*/ |
| 414 |
void |
| 415 |
pan_store_tiled_image(void *dst, const void *src, unsigned x, unsigned y, |
| 416 |
unsigned w, unsigned h, uint32_t dst_stride, |
| 417 |
uint32_t src_stride, enum pipe_format format, |
| 418 |
enum pan_interleave_zs interleave) |
| 419 |
{ |
| 420 |
pan_access_tiled_image(dst, (void *)src, x, y, w, h, dst_stride, src_stride, |
| 421 |
format, interleave, true); |
| 422 |
} |
| 423 |
|
| 424 |
void |
| 425 |
pan_load_tiled_image(void *dst, const void *src, unsigned x, unsigned y, |
| 426 |
unsigned w, unsigned h, uint32_t dst_stride, |
| 427 |
uint32_t src_stride, enum pipe_format format, |
| 428 |
enum pan_interleave_zs interleave) |
| 429 |
{ |
| 430 |
pan_access_tiled_image((void *)src, dst, x, y, w, h, src_stride, dst_stride, |
| 431 |
format, interleave, false); |
| 432 |
} |
| 433 |
|
| 434 |
void |
| 435 |
pan_copy_tiled_image(void *dst, const void *src, unsigned dst_x, unsigned dst_y, |
| 436 |
unsigned src_x, unsigned src_y, unsigned w, unsigned h, |
| 437 |
uint32_t dst_stride, uint32_t src_stride, |
| 438 |
enum pipe_format format) |
| 439 |
{ |
| 440 |
const struct util_format_description *desc = util_format_description(format); |
| 441 |
unsigned block_size_B = desc->block.bits / 8; |
| 442 |
|
| 443 |
/* If both the src and dst region are tile-aligned, we can just memcpy |
| 444 |
* whole tiles without any (de)tiling */ |
| 445 |
if (src_x % TILE_WIDTH == 0 && src_y % TILE_HEIGHT == 0 && |
| 446 |
dst_x % TILE_WIDTH == 0 && dst_y % TILE_HEIGHT == 0 && |
| 447 |
w % TILE_WIDTH == 0 && h % TILE_HEIGHT == 0) { |
| 448 |
|
| 449 |
unsigned tile_size_B = block_size_B * PIXELS_PER_TILE; |
| 450 |
|
| 451 |
unsigned w_t = w / TILE_WIDTH; |
| 452 |
unsigned h_t = h / TILE_HEIGHT; |
| 453 |
unsigned src_x_t = src_x / TILE_WIDTH; |
| 454 |
unsigned src_y_t = src_y / TILE_HEIGHT; |
| 455 |
unsigned dst_x_t = dst_x / TILE_WIDTH; |
| 456 |
unsigned dst_y_t = dst_y / TILE_HEIGHT; |
| 457 |
|
| 458 |
for (unsigned y_t = 0; y_t < h_t; y_t++) { |
| 459 |
void *dst_tile_row = dst + |
| 460 |
(y_t + dst_y_t) * dst_stride + |
| 461 |
dst_x_t * tile_size_B; |
| 462 |
const void *src_tile_row = src + |
| 463 |
(y_t + src_y_t) * src_stride + |
| 464 |
src_x_t * tile_size_B; |
| 465 |
memcpy(dst_tile_row, src_tile_row, tile_size_B * w_t); |
| 466 |
} |
| 467 |
|
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
/* Otherwise, we copy by working across the copy region in 64KiB chunks. |
| 472 |
* For each chunk, we detile part of the src into a linear tempoaray |
| 473 |
* buffer, then tile to the dst */ |
| 474 |
|
| 475 |
/* This could fit on the stack easily on glibc, but it's dicier on musl, |
| 476 |
* which has a 128KiB stack size */ |
| 477 |
const size_t chunk_size_B = 65536; |
| 478 |
void *chunk = ralloc_size(NULL, chunk_size_B); |
| 479 |
|
| 480 |
/* Choose pixel dimensions of the chunk. These should be tile aligned, |
| 481 |
* maximize used space in the buffer, and be close to a square. */ |
| 482 |
unsigned chunk_size_bl = chunk_size_B / block_size_B; |
| 483 |
unsigned chunk_width_bl = (unsigned) sqrtf((float) (chunk_size_bl)); |
| 484 |
chunk_width_bl = (chunk_width_bl / TILE_WIDTH) * TILE_WIDTH; |
| 485 |
unsigned chunk_height_bl = chunk_size_bl / chunk_width_bl; |
| 486 |
chunk_height_bl = (chunk_height_bl / TILE_HEIGHT) * TILE_HEIGHT; |
| 487 |
|
| 488 |
unsigned chunk_width_px = chunk_width_bl * desc->block.width; |
| 489 |
unsigned chunk_height_px = chunk_height_bl * desc->block.height; |
| 490 |
|
| 491 |
unsigned chunk_row_stride_B = chunk_width_bl * block_size_B; |
| 492 |
|
| 493 |
/* Align chunk copy regions to src tiles, to optimize detiling. We can't |
| 494 |
* get tile alignment on both src and dst, but one is better than nothing. */ |
| 495 |
unsigned src_first_tile_x = (src_x / TILE_WIDTH) * TILE_WIDTH; |
| 496 |
unsigned src_first_tile_y = (src_y / TILE_HEIGHT) * TILE_HEIGHT; |
| 497 |
|
| 498 |
for (unsigned x = src_first_tile_x; x < src_x + w; x += chunk_width_px) { |
| 499 |
for (unsigned y = src_first_tile_y; y < src_y + h; y += chunk_height_px) { |
| 500 |
/* x/y are tile-aligned, but because the actual copy region is not, |
| 501 |
* we may need to start at an offset position on the left/top edges */ |
| 502 |
unsigned src_chunk_x = MAX2(src_x, x); |
| 503 |
unsigned src_chunk_y = MAX2(src_y, y); |
| 504 |
unsigned dst_chunk_x = dst_x + (src_chunk_x - src_x); |
| 505 |
unsigned dst_chunk_y = dst_y + (src_chunk_y - src_y); |
| 506 |
|
| 507 |
/* Similarly, right/bottom edges may not need a whole chunk */ |
| 508 |
unsigned src_chunk_right = MIN2(src_chunk_x + chunk_width_px, |
| 509 |
src_x + w); |
| 510 |
unsigned src_chunk_bottom = MIN2(src_chunk_y + chunk_height_px, |
| 511 |
src_y + h); |
| 512 |
unsigned width = src_chunk_right - src_chunk_x; |
| 513 |
unsigned height = src_chunk_bottom - src_chunk_y; |
| 514 |
|
| 515 |
pan_load_tiled_image( |
| 516 |
chunk, src, src_chunk_x, src_chunk_y, width, height, |
| 517 |
chunk_row_stride_B, src_stride, format, PAN_INTERLEAVE_NONE); |
| 518 |
pan_store_tiled_image( |
| 519 |
dst, chunk, dst_chunk_x, dst_chunk_y, width, height, dst_stride, |
| 520 |
chunk_row_stride_B, format, PAN_INTERLEAVE_NONE); |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
ralloc_free(chunk); |
| 525 |
} |