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