/*1* Copyright (C) 2021 Alyssa Rosenzweig <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#include <stdio.h>24#include <assert.h>25#include <stdlib.h>26#include <stdbool.h>27#include <stdint.h>28#include "tiling.h"2930/* Z-order with 64x64 tiles:31*32* [y5][x5][y4][x4][y3][x3][y2][x2][y1][x1][y0][x0]33*34* Efficient tiling algorithm described in35* https://fgiesen.wordpress.com/2011/01/17/texture-tiling-and-swizzling/ but36* for posterity, we split into X and Y parts, and are faced with the problem37* of incrementing:38*39* 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0]40*41* To do so, we fill in the "holes" with 1's by adding the bitwise inverse of42* the mask of bits we care about43*44* 0 [x5] 0 [x4] 0 [x3] 0 [x2] 0 [x1] 0 [x0]45* + 1 0 1 0 1 0 1 0 1 0 1 046* ------------------------------------------47* 1 [x5] 1 [x4] 1 [x3] 1 [x2] 1 [x1] 1 [x0]48*49* Then when we add one, the holes are passed over by forcing carry bits high.50* Finally, we need to zero out the holes, by ANDing with the mask of bits we51* care about. In total, we get the expression (X + ~mask + 1) & mask, and52* applying the two's complement identity, we are left with (X - mask) & mask53*/5455#define TILE_WIDTH 6456#define TILE_HEIGHT 6457#define TILE_SHIFT 658#define TILE_MASK ((1 << TILE_SHIFT) - 1)5960/* mask of bits used for X coordinate in a tile */61#define SPACE_MASK 0x555 // 0b0101010101016263#define MAX2(x, y) (((x) > (y)) ? (x) : (y))64#define MIN2(x, y) (((x) < (y)) ? (x) : (y))6566static uint32_t67agx_space_bits(unsigned x)68{69assert(x < TILE_WIDTH);70return ((x & 1) << 0) | ((x & 2) << 1) | ((x & 4) << 2) |71((x & 8) << 3) | ((x & 16) << 4) | ((x & 32) << 5);72}7374#define TILED_UNALIGNED_TYPE(pixel_t, is_store) { \75unsigned tiles_per_row = (width + TILE_WIDTH - 1) >> TILE_SHIFT;\76unsigned y_offs = agx_space_bits(sy & TILE_MASK) << 1;\77unsigned x_offs_start = agx_space_bits(sx & TILE_MASK);\78\79for (unsigned y = sy; y < smaxy; ++y) {\80unsigned tile_y = (y >> TILE_SHIFT);\81unsigned tile_row = tile_y * tiles_per_row;\82unsigned x_offs = x_offs_start;\83\84pixel_t *linear_row = linear;\85\86for (unsigned x = sx; x < smaxx; ++x) {\87unsigned tile_x = (x >> TILE_SHIFT);\88unsigned tile_idx = (tile_row + tile_x);\89unsigned tile_base = tile_idx * (TILE_WIDTH * TILE_HEIGHT);\90\91pixel_t *ptiled = &tiled[tile_base + y_offs + x_offs];\92pixel_t *plinear = (linear_row++);\93pixel_t *outp = (pixel_t *) (is_store ? ptiled : plinear); \94pixel_t *inp = (pixel_t *) (is_store ? plinear : ptiled); \95*outp = *inp;\96x_offs = (x_offs - SPACE_MASK) & SPACE_MASK;\97}\98\99y_offs = (((y_offs >> 1) - SPACE_MASK) & SPACE_MASK) << 1;\100linear += linear_pitch;\101}\102}103104void105agx_detile(void *_tiled, void *_linear,106unsigned width, unsigned bpp, unsigned linear_pitch,107unsigned sx, unsigned sy, unsigned smaxx, unsigned smaxy)108{109/* TODO: parametrize with macro magic */110assert(bpp == 32);111112uint32_t *linear = _linear;113uint32_t *tiled = _tiled;114TILED_UNALIGNED_TYPE(uint32_t, false);115}116117void118agx_tile(void *_tiled, void *_linear,119unsigned width, unsigned bpp, unsigned linear_pitch,120unsigned sx, unsigned sy, unsigned smaxx, unsigned smaxy)121{122/* TODO: parametrize with macro magic */123assert(bpp == 32);124125uint32_t *linear = _linear;126uint32_t *tiled = _tiled;127TILED_UNALIGNED_TYPE(uint32_t, true);128}129130131