1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2014 Broadcom 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21bf215546Sopenharmony_ci * IN THE SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci/** @file vc4_tiling.c 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci * Handles information about the VC4 tiling formats, and loading and storing 27bf215546Sopenharmony_ci * from them. 28bf215546Sopenharmony_ci * 29bf215546Sopenharmony_ci * Texture mipmap levels on VC4 are (with the exception of 32-bit RGBA raster 30bf215546Sopenharmony_ci * textures for scanout) stored as groups of microtiles. If the texture is at 31bf215546Sopenharmony_ci * least 4x4 microtiles (utiles), then those microtiles are arranged in a sort 32bf215546Sopenharmony_ci * of Hilbert-fractal-ish layout (T), otherwise the microtiles are in raster 33bf215546Sopenharmony_ci * order (LT). 34bf215546Sopenharmony_ci * 35bf215546Sopenharmony_ci * Specifically, the T format has: 36bf215546Sopenharmony_ci * 37bf215546Sopenharmony_ci * - 64b utiles of pixels in a raster-order grid according to cpp. It's 4x4 38bf215546Sopenharmony_ci * pixels at 32 bit depth. 39bf215546Sopenharmony_ci * 40bf215546Sopenharmony_ci * - 1k subtiles made of a 4x4 raster-order grid of 64b utiles (so usually 41bf215546Sopenharmony_ci * 16x16 pixels). 42bf215546Sopenharmony_ci * 43bf215546Sopenharmony_ci * - 4k tiles made of a 2x2 grid of 1k subtiles (so usually 32x32 pixels). On 44bf215546Sopenharmony_ci * even 4k tile rows, they're arranged as (BL, TL, TR, BR), and on odd rows 45bf215546Sopenharmony_ci * they're (TR, BR, BL, TL), where bottom left is start of memory. 46bf215546Sopenharmony_ci * 47bf215546Sopenharmony_ci * - an image made of 4k tiles in rows either left-to-right (even rows of 4k 48bf215546Sopenharmony_ci * tiles) or right-to-left (odd rows of 4k tiles). 49bf215546Sopenharmony_ci */ 50bf215546Sopenharmony_ci 51bf215546Sopenharmony_ci#include "vc4_screen.h" 52bf215546Sopenharmony_ci#include "vc4_context.h" 53bf215546Sopenharmony_ci#include "vc4_tiling.h" 54bf215546Sopenharmony_ci 55bf215546Sopenharmony_ci/** 56bf215546Sopenharmony_ci * The texture unit decides what tiling format a particular miplevel is using 57bf215546Sopenharmony_ci * this function, so we lay out our miptrees accordingly. 58bf215546Sopenharmony_ci */ 59bf215546Sopenharmony_cibool 60bf215546Sopenharmony_civc4_size_is_lt(uint32_t width, uint32_t height, int cpp) 61bf215546Sopenharmony_ci{ 62bf215546Sopenharmony_ci return (width <= 4 * vc4_utile_width(cpp) || 63bf215546Sopenharmony_ci height <= 4 * vc4_utile_height(cpp)); 64bf215546Sopenharmony_ci} 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci/** 67bf215546Sopenharmony_ci * Takes a utile x and y (and the number of utiles of width of the image) and 68bf215546Sopenharmony_ci * returns the offset to the utile within a VC4_TILING_FORMAT_TF image. 69bf215546Sopenharmony_ci */ 70bf215546Sopenharmony_cistatic uint32_t 71bf215546Sopenharmony_cit_utile_address(uint32_t utile_x, uint32_t utile_y, 72bf215546Sopenharmony_ci uint32_t utile_stride) 73bf215546Sopenharmony_ci{ 74bf215546Sopenharmony_ci /* T images have to be aligned to 8 utiles (4x4 subtiles, which are 75bf215546Sopenharmony_ci * 2x2 in a 4k tile). 76bf215546Sopenharmony_ci */ 77bf215546Sopenharmony_ci assert(!(utile_stride & 7)); 78bf215546Sopenharmony_ci uint32_t tile_stride = utile_stride >> 3; 79bf215546Sopenharmony_ci /* 4k tile offsets. */ 80bf215546Sopenharmony_ci uint32_t tile_x = utile_x >> 3; 81bf215546Sopenharmony_ci uint32_t tile_y = utile_y >> 3; 82bf215546Sopenharmony_ci bool odd_tile_y = tile_y & 1; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci /* Odd lines of 4k tiles go right-to-left. */ 85bf215546Sopenharmony_ci if (odd_tile_y) 86bf215546Sopenharmony_ci tile_x = tile_stride - tile_x - 1; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci uint32_t tile_offset = 4096 * (tile_y * tile_stride + tile_x); 89bf215546Sopenharmony_ci 90bf215546Sopenharmony_ci uint32_t stile_x = (utile_x >> 2) & 1; 91bf215546Sopenharmony_ci uint32_t stile_y = (utile_y >> 2) & 1; 92bf215546Sopenharmony_ci uint32_t stile_index = (stile_y << 1) + stile_x; 93bf215546Sopenharmony_ci static const uint32_t odd_stile_map[4] = {2, 1, 3, 0}; 94bf215546Sopenharmony_ci static const uint32_t even_stile_map[4] = {0, 3, 1, 2}; 95bf215546Sopenharmony_ci 96bf215546Sopenharmony_ci uint32_t stile_offset = 1024 * (odd_tile_y ? 97bf215546Sopenharmony_ci odd_stile_map[stile_index] : 98bf215546Sopenharmony_ci even_stile_map[stile_index]); 99bf215546Sopenharmony_ci 100bf215546Sopenharmony_ci /* This function no longer handles the utile offset within a subtile. 101bf215546Sopenharmony_ci * Walking subtiles is the job of the LT image handler. 102bf215546Sopenharmony_ci */ 103bf215546Sopenharmony_ci assert(!(utile_x & 3) && !(utile_y & 3)); 104bf215546Sopenharmony_ci 105bf215546Sopenharmony_ci#if 0 106bf215546Sopenharmony_ci fprintf(stderr, "utile %d,%d -> %d + %d + %d (stride %d,%d) = %d\n", 107bf215546Sopenharmony_ci utile_x, utile_y, 108bf215546Sopenharmony_ci tile_offset, stile_offset, utile_offset, 109bf215546Sopenharmony_ci utile_stride, tile_stride, 110bf215546Sopenharmony_ci tile_offset + stile_offset + utile_offset); 111bf215546Sopenharmony_ci#endif 112bf215546Sopenharmony_ci 113bf215546Sopenharmony_ci return tile_offset + stile_offset; 114bf215546Sopenharmony_ci} 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_ci/** 117bf215546Sopenharmony_ci * Loads or stores a T texture image by breaking it down into subtiles 118bf215546Sopenharmony_ci * (1024-byte, 4x4-utile) sub-images that we can use the LT tiling functions 119bf215546Sopenharmony_ci * on. 120bf215546Sopenharmony_ci */ 121bf215546Sopenharmony_cistatic inline void 122bf215546Sopenharmony_civc4_t_image_helper(void *gpu, uint32_t gpu_stride, 123bf215546Sopenharmony_ci void *cpu, uint32_t cpu_stride, 124bf215546Sopenharmony_ci int cpp, const struct pipe_box *box, 125bf215546Sopenharmony_ci bool to_cpu) 126bf215546Sopenharmony_ci{ 127bf215546Sopenharmony_ci uint32_t utile_w = vc4_utile_width(cpp); 128bf215546Sopenharmony_ci uint32_t utile_h = vc4_utile_height(cpp); 129bf215546Sopenharmony_ci uint32_t utile_w_shift = ffs(utile_w) - 1; 130bf215546Sopenharmony_ci uint32_t utile_h_shift = ffs(utile_h) - 1; 131bf215546Sopenharmony_ci uint32_t stile_w = 4 * utile_w; 132bf215546Sopenharmony_ci uint32_t stile_h = 4 * utile_h; 133bf215546Sopenharmony_ci assert(stile_w * stile_h * cpp == 1024); 134bf215546Sopenharmony_ci uint32_t utile_stride = gpu_stride / cpp / utile_w; 135bf215546Sopenharmony_ci uint32_t x1 = box->x; 136bf215546Sopenharmony_ci uint32_t y1 = box->y; 137bf215546Sopenharmony_ci uint32_t x2 = box->x + box->width; 138bf215546Sopenharmony_ci uint32_t y2 = box->y + box->height; 139bf215546Sopenharmony_ci struct pipe_box partial_box; 140bf215546Sopenharmony_ci uint32_t gpu_lt_stride = stile_w * cpp; 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci for (uint32_t y = y1; y < y2; y = align(y + 1, stile_h)) { 143bf215546Sopenharmony_ci partial_box.y = y & (stile_h - 1); 144bf215546Sopenharmony_ci partial_box.height = MIN2(y2 - y, stile_h - partial_box.y); 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci uint32_t cpu_offset = 0; 147bf215546Sopenharmony_ci for (uint32_t x = x1; x < x2; x = align(x + 1, stile_w)) { 148bf215546Sopenharmony_ci partial_box.x = x & (stile_w - 1); 149bf215546Sopenharmony_ci partial_box.width = MIN2(x2 - x, 150bf215546Sopenharmony_ci stile_w - partial_box.x); 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci /* The dst offset we want is the start of this 153bf215546Sopenharmony_ci * subtile 154bf215546Sopenharmony_ci */ 155bf215546Sopenharmony_ci uint32_t gpu_offset = 156bf215546Sopenharmony_ci t_utile_address((x >> utile_w_shift) & ~0x3, 157bf215546Sopenharmony_ci (y >> utile_h_shift) & ~0x3, 158bf215546Sopenharmony_ci utile_stride); 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci if (to_cpu) { 161bf215546Sopenharmony_ci vc4_load_lt_image(cpu + cpu_offset, 162bf215546Sopenharmony_ci cpu_stride, 163bf215546Sopenharmony_ci gpu + gpu_offset, 164bf215546Sopenharmony_ci gpu_lt_stride, 165bf215546Sopenharmony_ci cpp, &partial_box); 166bf215546Sopenharmony_ci } else { 167bf215546Sopenharmony_ci vc4_store_lt_image(gpu + gpu_offset, 168bf215546Sopenharmony_ci gpu_lt_stride, 169bf215546Sopenharmony_ci cpu + cpu_offset, 170bf215546Sopenharmony_ci cpu_stride, 171bf215546Sopenharmony_ci cpp, &partial_box); 172bf215546Sopenharmony_ci } 173bf215546Sopenharmony_ci 174bf215546Sopenharmony_ci cpu_offset += partial_box.width * cpp; 175bf215546Sopenharmony_ci } 176bf215546Sopenharmony_ci cpu += cpu_stride * partial_box.height; 177bf215546Sopenharmony_ci } 178bf215546Sopenharmony_ci} 179bf215546Sopenharmony_ci 180bf215546Sopenharmony_cistatic void 181bf215546Sopenharmony_civc4_store_t_image(void *dst, uint32_t dst_stride, 182bf215546Sopenharmony_ci void *src, uint32_t src_stride, 183bf215546Sopenharmony_ci int cpp, const struct pipe_box *box) 184bf215546Sopenharmony_ci{ 185bf215546Sopenharmony_ci vc4_t_image_helper(dst, dst_stride, 186bf215546Sopenharmony_ci src, src_stride, 187bf215546Sopenharmony_ci cpp, box, false); 188bf215546Sopenharmony_ci} 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_cistatic void 191bf215546Sopenharmony_civc4_load_t_image(void *dst, uint32_t dst_stride, 192bf215546Sopenharmony_ci void *src, uint32_t src_stride, 193bf215546Sopenharmony_ci int cpp, const struct pipe_box *box) 194bf215546Sopenharmony_ci{ 195bf215546Sopenharmony_ci vc4_t_image_helper(src, src_stride, 196bf215546Sopenharmony_ci dst, dst_stride, 197bf215546Sopenharmony_ci cpp, box, true); 198bf215546Sopenharmony_ci} 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci/** 201bf215546Sopenharmony_ci * Loads pixel data from the start (microtile-aligned) box in \p src to the 202bf215546Sopenharmony_ci * start of \p dst according to the given tiling format. 203bf215546Sopenharmony_ci */ 204bf215546Sopenharmony_civoid 205bf215546Sopenharmony_civc4_load_tiled_image(void *dst, uint32_t dst_stride, 206bf215546Sopenharmony_ci void *src, uint32_t src_stride, 207bf215546Sopenharmony_ci uint8_t tiling_format, int cpp, 208bf215546Sopenharmony_ci const struct pipe_box *box) 209bf215546Sopenharmony_ci{ 210bf215546Sopenharmony_ci if (tiling_format == VC4_TILING_FORMAT_LT) { 211bf215546Sopenharmony_ci vc4_load_lt_image(dst, dst_stride, 212bf215546Sopenharmony_ci src, src_stride, 213bf215546Sopenharmony_ci cpp, box); 214bf215546Sopenharmony_ci } else { 215bf215546Sopenharmony_ci assert(tiling_format == VC4_TILING_FORMAT_T); 216bf215546Sopenharmony_ci vc4_load_t_image(dst, dst_stride, 217bf215546Sopenharmony_ci src, src_stride, 218bf215546Sopenharmony_ci cpp, box); 219bf215546Sopenharmony_ci } 220bf215546Sopenharmony_ci} 221bf215546Sopenharmony_ci 222bf215546Sopenharmony_ci/** 223bf215546Sopenharmony_ci * Stores pixel data from the start of \p src into a (microtile-aligned) box in 224bf215546Sopenharmony_ci * \p dst according to the given tiling format. 225bf215546Sopenharmony_ci */ 226bf215546Sopenharmony_civoid 227bf215546Sopenharmony_civc4_store_tiled_image(void *dst, uint32_t dst_stride, 228bf215546Sopenharmony_ci void *src, uint32_t src_stride, 229bf215546Sopenharmony_ci uint8_t tiling_format, int cpp, 230bf215546Sopenharmony_ci const struct pipe_box *box) 231bf215546Sopenharmony_ci{ 232bf215546Sopenharmony_ci if (tiling_format == VC4_TILING_FORMAT_LT) { 233bf215546Sopenharmony_ci vc4_store_lt_image(dst, dst_stride, 234bf215546Sopenharmony_ci src, src_stride, 235bf215546Sopenharmony_ci cpp, box); 236bf215546Sopenharmony_ci } else { 237bf215546Sopenharmony_ci assert(tiling_format == VC4_TILING_FORMAT_T); 238bf215546Sopenharmony_ci vc4_store_t_image(dst, dst_stride, 239bf215546Sopenharmony_ci src, src_stride, 240bf215546Sopenharmony_ci cpp, box); 241bf215546Sopenharmony_ci } 242bf215546Sopenharmony_ci} 243bf215546Sopenharmony_ci 244