| 1 |
/* |
| 2 |
* Copyright 2021 Alyssa Rosenzweig |
| 3 |
* Copyright 2019 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <assert.h> |
| 8 |
#include <stdbool.h> |
| 9 |
#include <stdint.h> |
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include "util/macros.h" |
| 13 |
#include "layout.h" |
| 14 |
|
| 15 |
/* Z-order with rectangular (NxN or 2NxN) tiles, at most 128x128: |
| 16 |
* |
| 17 |
* [y6][x6][y5][x5][y4][x4]y3][x3][y2][x2][y1][x1][y0][x0] |
| 18 |
* |
| 19 |
* Efficient tiling algorithm described in |
| 20 |
* https://fgiesen.wordpress.com/2011/01/17/texture-tiling-and-swizzling/ but |
| 21 |
* for posterity, we split into X and Y parts, and are faced with the problem |
| 22 |
* of incrementing: |
| 23 |
* |
| 24 |
* 0 [x6] 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0] |
| 25 |
* |
| 26 |
* To do so, we fill in the "holes" with 1's by adding the bitwise inverse of |
| 27 |
* the mask of bits we care about |
| 28 |
* |
| 29 |
* 0 [x6] 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0] |
| 30 |
* + 1 0 1 0 1 0 1 0 1 0 1 0 1 0 |
| 31 |
* ------------------------------------------------ |
| 32 |
* 1 [x6] 1 [x5] 1 [x4] 1 [x3] 1 [x2] 1 [x1] 1 [x0] |
| 33 |
* |
| 34 |
* Then when we add one, the holes are passed over by forcing carry bits high. |
| 35 |
* Finally, we need to zero out the holes, by ANDing with the mask of bits we |
| 36 |
* care about. In total, we get the expression (X + ~mask + 1) & mask, and |
| 37 |
* applying the two's complement identity, we are left with (X - mask) & mask |
| 38 |
*/ |
| 39 |
|
| 40 |
typedef struct { |
| 41 |
uint64_t lo; |
| 42 |
uint64_t hi; |
| 43 |
} __attribute__((packed)) ail_uint128_t; |
| 44 |
|
| 45 |
/* |
| 46 |
* Given a power-of-two block width/height, construct the mask of "X" bits. This |
| 47 |
* is found by restricting the full mask of alternating 0s and 1s to only cover |
| 48 |
* the bottom 2 * log2(dim) bits. That's the same as modding by dim^2. |
| 49 |
*/ |
| 50 |
static uint32_t |
| 51 |
ail_space_mask(unsigned x) |
| 52 |
{ |
| 53 |
assert(util_is_power_of_two_nonzero(x)); |
| 54 |
|
| 55 |
return MOD_POT(0x55555555, x * x); |
| 56 |
} |
| 57 |
|
| 58 |
template <typename T, bool is_store> |
| 59 |
void |
| 60 |
memcpy_small(void *_tiled, void *_linear, const struct ail_layout *tiled_layout, |
| 61 |
unsigned level, unsigned linear_pitch_B, unsigned sx_px, |
| 62 |
unsigned sy_px, unsigned swidth_px, unsigned sheight_px) |
| 63 |
{ |
| 64 |
enum pipe_format format = tiled_layout->format; |
| 65 |
unsigned linear_pitch_el = linear_pitch_B / sizeof(T); |
| 66 |
unsigned stride_el = tiled_layout->stride_el[level]; |
| 67 |
unsigned sx_el = util_format_get_nblocksx(format, sx_px); |
| 68 |
unsigned sy_el = util_format_get_nblocksy(format, sy_px); |
| 69 |
unsigned swidth_el = util_format_get_nblocksx(format, swidth_px); |
| 70 |
unsigned sheight_el = util_format_get_nblocksy(format, sheight_px); |
| 71 |
unsigned sx_end_el = sx_el + swidth_el; |
| 72 |
unsigned sy_end_el = sy_el + sheight_el; |
| 73 |
|
| 74 |
struct ail_tile tile_size = tiled_layout->tilesize_el[level]; |
| 75 |
unsigned tile_area_el = tile_size.width_el * tile_size.height_el; |
| 76 |
unsigned tiles_per_row = DIV_ROUND_UP(stride_el, tile_size.width_el); |
| 77 |
unsigned y_offs_el = ail_space_bits(MOD_POT(sy_el, tile_size.height_el)) |
| 78 |
<< 1; |
| 79 |
unsigned x_offs_start_el = |
| 80 |
ail_space_bits(MOD_POT(sx_el, tile_size.width_el)); |
| 81 |
unsigned space_mask_x = ail_space_mask(tile_size.width_el); |
| 82 |
unsigned space_mask_y = ail_space_mask(tile_size.height_el) << 1; |
| 83 |
unsigned log2_tile_width_el = util_logbase2(tile_size.width_el); |
| 84 |
unsigned log2_tile_height_el = util_logbase2(tile_size.height_el); |
| 85 |
|
| 86 |
T *linear = (T *)_linear; |
| 87 |
T *tiled = (T *)_tiled; |
| 88 |
|
| 89 |
for (unsigned y_el = sy_el; y_el < sy_end_el; ++y_el) { |
| 90 |
unsigned y_rowtile = y_el >> log2_tile_height_el; |
| 91 |
unsigned y_tile = y_rowtile * tiles_per_row; |
| 92 |
unsigned x_offs_el = x_offs_start_el; |
| 93 |
|
| 94 |
T *linear_row = linear; |
| 95 |
|
| 96 |
for (unsigned x_el = sx_el; x_el < sx_end_el; ++x_el) { |
| 97 |
unsigned tile_idx = (y_tile + (x_el >> log2_tile_width_el)); |
| 98 |
unsigned tile_offset_el = tile_idx * tile_area_el; |
| 99 |
|
| 100 |
T *ptiled = &tiled[tile_offset_el + y_offs_el + x_offs_el]; |
| 101 |
T *plinear = (linear_row++); |
| 102 |
|
| 103 |
if (is_store) |
| 104 |
*ptiled = *plinear; |
| 105 |
else |
| 106 |
*plinear = *ptiled; |
| 107 |
|
| 108 |
x_offs_el = (x_offs_el - space_mask_x) & space_mask_x; |
| 109 |
} |
| 110 |
|
| 111 |
y_offs_el = (y_offs_el - space_mask_y) & space_mask_y; |
| 112 |
linear += linear_pitch_el; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
#define TILED_UNALIGNED_TYPES(blocksize_B, store) \ |
| 117 |
if (blocksize_B == 1) { \ |
| 118 |
memcpy_small<uint8_t, store>(_tiled, _linear, tiled_layout, level, \ |
| 119 |
linear_pitch_B, sx_px, sy_px, swidth_px, \ |
| 120 |
sheight_px); \ |
| 121 |
} else if (blocksize_B == 2) { \ |
| 122 |
memcpy_small<uint16_t, store>(_tiled, _linear, tiled_layout, level, \ |
| 123 |
linear_pitch_B, sx_px, sy_px, swidth_px, \ |
| 124 |
sheight_px); \ |
| 125 |
} else if (blocksize_B == 4) { \ |
| 126 |
memcpy_small<uint32_t, store>(_tiled, _linear, tiled_layout, level, \ |
| 127 |
linear_pitch_B, sx_px, sy_px, swidth_px, \ |
| 128 |
sheight_px); \ |
| 129 |
} else if (blocksize_B == 8) { \ |
| 130 |
memcpy_small<uint64_t, store>(_tiled, _linear, tiled_layout, level, \ |
| 131 |
linear_pitch_B, sx_px, sy_px, swidth_px, \ |
| 132 |
sheight_px); \ |
| 133 |
} else if (blocksize_B == 16) { \ |
| 134 |
memcpy_small<ail_uint128_t, store>(_tiled, _linear, tiled_layout, level, \ |
| 135 |
linear_pitch_B, sx_px, sy_px, \ |
| 136 |
swidth_px, sheight_px); \ |
| 137 |
} else { \ |
| 138 |
UNREACHABLE("Invalid block size"); \ |
| 139 |
} |
| 140 |
|
| 141 |
void |
| 142 |
ail_detile(void *_tiled, void *_linear, const struct ail_layout *tiled_layout, |
| 143 |
unsigned level, unsigned linear_pitch_B, unsigned sx_px, |
| 144 |
unsigned sy_px, unsigned swidth_px, unsigned sheight_px) |
| 145 |
{ |
| 146 |
unsigned width_px = u_minify(tiled_layout->width_px, level); |
| 147 |
unsigned height_px = u_minify(tiled_layout->height_px, level); |
| 148 |
unsigned blocksize_B = util_format_get_blocksize(tiled_layout->format); |
| 149 |
|
| 150 |
assert(level < tiled_layout->levels && "Mip level out of bounds"); |
| 151 |
assert(ail_is_level_twiddled_uncompressed(tiled_layout, level) && |
| 152 |
"Invalid usage"); |
| 153 |
assert((sx_px + swidth_px) <= width_px && "Invalid usage"); |
| 154 |
assert((sy_px + sheight_px) <= height_px && "Invalid usage"); |
| 155 |
|
| 156 |
TILED_UNALIGNED_TYPES(blocksize_B, false); |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
ail_tile(void *_tiled, void *_linear, const struct ail_layout *tiled_layout, |
| 161 |
unsigned level, unsigned linear_pitch_B, unsigned sx_px, |
| 162 |
unsigned sy_px, unsigned swidth_px, unsigned sheight_px) |
| 163 |
{ |
| 164 |
unsigned width_px = u_minify(tiled_layout->width_px, level); |
| 165 |
unsigned height_px = u_minify(tiled_layout->height_px, level); |
| 166 |
unsigned blocksize_B = ail_get_blocksize_B(tiled_layout); |
| 167 |
|
| 168 |
assert(level < tiled_layout->levels && "Mip level out of bounds"); |
| 169 |
assert(ail_is_level_twiddled_uncompressed(tiled_layout, level) && |
| 170 |
"Invalid usage"); |
| 171 |
assert((sx_px + swidth_px) <= width_px && "Invalid usage"); |
| 172 |
assert((sy_px + sheight_px) <= height_px && "Invalid usage"); |
| 173 |
|
| 174 |
TILED_UNALIGNED_TYPES(blocksize_B, true); |
| 175 |
} |