1 /*
2 * Copyright (C) 2021 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 * Copyright (c) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stdio.h>
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <stdint.h>
30 #include "util/macros.h"
31 #include "tiling.h"
32
33 /* Z-order with square tiles, at most 64x64:
34 *
35 * [y5][x5][y4][x4][y3][x3][y2][x2][y1][x1][y0][x0]
36 *
37 * Efficient tiling algorithm described in
38 * https://fgiesen.wordpress.com/2011/01/17/texture-tiling-and-swizzling/ but
39 * for posterity, we split into X and Y parts, and are faced with the problem
40 * of incrementing:
41 *
42 * 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0]
43 *
44 * To do so, we fill in the "holes" with 1's by adding the bitwise inverse of
45 * the mask of bits we care about
46 *
47 * 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0]
48 * + 1 0 1 0 1 0 1 0 1 0 1 0
49 * ------------------------------------------
50 * 1 [x5] 1 [x4] 1 [x3] 1 [x2] 1 [x1] 1 [x0]
51 *
52 * Then when we add one, the holes are passed over by forcing carry bits high.
53 * Finally, we need to zero out the holes, by ANDing with the mask of bits we
54 * care about. In total, we get the expression (X + ~mask + 1) & mask, and
55 * applying the two's complement identity, we are left with (X - mask) & mask
56 */
57
58 /* mask of bits used for X coordinate in a tile */
59 #define SPACE_MASK 0x555 // 0b010101010101
60
61 typedef struct {
62 uint16_t lo;
63 uint8_t hi;
64 } __attribute__((packed)) agx_uint24_t;
65
66 typedef struct {
67 uint32_t lo;
68 uint16_t hi;
69 } __attribute__((packed)) agx_uint48_t;
70
71 typedef struct {
72 uint64_t lo;
73 uint32_t hi;
74 } __attribute__((packed)) agx_uint96_t;
75
76 typedef struct {
77 uint64_t lo;
78 uint64_t hi;
79 } __attribute__((packed)) agx_uint128_t;
80
81 static uint32_t
agx_space_bits(unsigned x)82 agx_space_bits(unsigned x)
83 {
84 assert(x < 64);
85 return ((x & 1) << 0) | ((x & 2) << 1) | ((x & 4) << 2) |
86 ((x & 8) << 3) | ((x & 16) << 4) | ((x & 32) << 5);
87 }
88
89 #define TILED_UNALIGNED_TYPE(pixel_t, is_store, tile_shift) { \
90 unsigned tile_size = (1 << tile_shift);\
91 unsigned pixels_per_tile = tile_size * tile_size;\
92 unsigned tiles_per_row = (width + tile_size - 1) >> tile_shift;\
93 unsigned y_offs = agx_space_bits(sy & (tile_size - 1)) << 1;\
94 unsigned x_offs_start = agx_space_bits(sx & (tile_size - 1));\
95 unsigned space_mask = SPACE_MASK & (pixels_per_tile - 1);\
96 \
97 pixel_t *linear = _linear; \
98 pixel_t *tiled = _tiled; \
99 \
100 for (unsigned y = sy; y < smaxy; ++y) {\
101 unsigned tile_y = (y >> tile_shift);\
102 unsigned tile_row = tile_y * tiles_per_row;\
103 unsigned x_offs = x_offs_start;\
104 \
105 pixel_t *linear_row = linear;\
106 \
107 for (unsigned x = sx; x < smaxx; ++x) {\
108 unsigned tile_x = (x >> tile_shift);\
109 unsigned tile_idx = (tile_row + tile_x);\
110 unsigned tile_base = tile_idx * pixels_per_tile;\
111 \
112 pixel_t *ptiled = &tiled[tile_base + y_offs + x_offs];\
113 pixel_t *plinear = (linear_row++);\
114 pixel_t *outp = (pixel_t *) (is_store ? ptiled : plinear); \
115 pixel_t *inp = (pixel_t *) (is_store ? plinear : ptiled); \
116 *outp = *inp;\
117 x_offs = (x_offs - space_mask) & space_mask;\
118 }\
119 \
120 y_offs = (((y_offs >> 1) - space_mask) & space_mask) << 1;\
121 linear += linear_pitch;\
122 }\
123 }
124
125 #define TILED_UNALIGNED_TYPES(bpp, store, tile_shift) { \
126 if (bpp == 8) \
127 TILED_UNALIGNED_TYPE(uint8_t, store, tile_shift) \
128 else if (bpp == 16) \
129 TILED_UNALIGNED_TYPE(uint16_t, store, tile_shift) \
130 else if (bpp == 24) \
131 TILED_UNALIGNED_TYPE(agx_uint24_t, store, tile_shift) \
132 else if (bpp == 32) \
133 TILED_UNALIGNED_TYPE(uint32_t, store, tile_shift) \
134 else if (bpp == 48) \
135 TILED_UNALIGNED_TYPE(agx_uint48_t, store, tile_shift) \
136 else if (bpp == 64) \
137 TILED_UNALIGNED_TYPE(uint64_t, store, tile_shift) \
138 else if (bpp == 96) \
139 TILED_UNALIGNED_TYPE(agx_uint96_t, store, tile_shift) \
140 else if (bpp == 128) \
141 TILED_UNALIGNED_TYPE(agx_uint128_t, store, tile_shift) \
142 else \
143 unreachable("Can't tile this bpp\n"); \
144 }
145
146 void
agx_detile(void * _tiled,void * _linear,unsigned width,unsigned bpp,unsigned linear_pitch,unsigned sx,unsigned sy,unsigned smaxx,unsigned smaxy,unsigned tile_shift)147 agx_detile(void *_tiled, void *_linear,
148 unsigned width, unsigned bpp, unsigned linear_pitch,
149 unsigned sx, unsigned sy, unsigned smaxx, unsigned smaxy, unsigned tile_shift)
150 {
151 TILED_UNALIGNED_TYPES(bpp, false, tile_shift);
152 }
153
154 void
agx_tile(void * _tiled,void * _linear,unsigned width,unsigned bpp,unsigned linear_pitch,unsigned sx,unsigned sy,unsigned smaxx,unsigned smaxy,unsigned tile_shift)155 agx_tile(void *_tiled, void *_linear,
156 unsigned width, unsigned bpp, unsigned linear_pitch,
157 unsigned sx, unsigned sy, unsigned smaxx, unsigned smaxy,
158 unsigned tile_shift)
159 {
160 TILED_UNALIGNED_TYPES(bpp, true, tile_shift);
161 }
162