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