Path: blob/21.2-virgl/src/broadcom/common/v3d_tiling.c
4560 views
/*1* Copyright © 2014-2017 Broadcom2*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, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/** @file v3d_tiling.c24*25* Handles information about the V3D tiling formats, and loading and storing26* from them.27*/2829#include <stdint.h>30#include "v3d_tiling.h"31#include "broadcom/common/v3d_cpu_tiling.h"3233/** Return the width in pixels of a 64-byte microtile. */34uint32_t35v3d_utile_width(int cpp)36{37switch (cpp) {38case 1:39case 2:40return 8;41case 4:42case 8:43return 4;44case 16:45return 2;46default:47unreachable("unknown cpp");48}49}5051/** Return the height in pixels of a 64-byte microtile. */52uint32_t53v3d_utile_height(int cpp)54{55switch (cpp) {56case 1:57return 8;58case 2:59case 4:60return 4;61case 8:62case 16:63return 2;64default:65unreachable("unknown cpp");66}67}6869/**70* Returns the byte address for a given pixel within a utile.71*72* Utiles are 64b blocks of pixels in raster order, with 32bpp being a 4x473* arrangement.74*/75static inline uint32_t76v3d_get_utile_pixel_offset(uint32_t cpp, uint32_t x, uint32_t y)77{78uint32_t utile_w = v3d_utile_width(cpp);7980assert(x < utile_w && y < v3d_utile_height(cpp));8182return x * cpp + y * utile_w * cpp;83}8485/**86* Returns the byte offset for a given pixel in a LINEARTILE layout.87*88* LINEARTILE is a single line of utiles in either the X or Y direction.89*/90static inline uint32_t91v3d_get_lt_pixel_offset(uint32_t cpp, uint32_t image_h, uint32_t x, uint32_t y)92{93uint32_t utile_w = v3d_utile_width(cpp);94uint32_t utile_h = v3d_utile_height(cpp);95uint32_t utile_index_x = x / utile_w;96uint32_t utile_index_y = y / utile_h;9798assert(utile_index_x == 0 || utile_index_y == 0);99100return (64 * (utile_index_x + utile_index_y) +101v3d_get_utile_pixel_offset(cpp,102x & (utile_w - 1),103y & (utile_h - 1)));104}105106/**107* Returns the byte offset for a given pixel in a UBLINEAR layout.108*109* UBLINEAR is the layout where pixels are arranged in UIF blocks (2x2110* utiles), and the UIF blocks are in 1 or 2 columns in raster order.111*/112static inline uint32_t113v3d_get_ublinear_pixel_offset(uint32_t cpp, uint32_t x, uint32_t y,114int ublinear_number)115{116uint32_t utile_w = v3d_utile_width(cpp);117uint32_t utile_h = v3d_utile_height(cpp);118uint32_t ub_w = utile_w * 2;119uint32_t ub_h = utile_h * 2;120uint32_t ub_x = x / ub_w;121uint32_t ub_y = y / ub_h;122123return (256 * (ub_y * ublinear_number +124ub_x) +125((x & utile_w) ? 64 : 0) +126((y & utile_h) ? 128 : 0) +127+ v3d_get_utile_pixel_offset(cpp,128x & (utile_w - 1),129y & (utile_h - 1)));130}131132static inline uint32_t133v3d_get_ublinear_2_column_pixel_offset(uint32_t cpp, uint32_t image_h,134uint32_t x, uint32_t y)135{136return v3d_get_ublinear_pixel_offset(cpp, x, y, 2);137}138139static inline uint32_t140v3d_get_ublinear_1_column_pixel_offset(uint32_t cpp, uint32_t image_h,141uint32_t x, uint32_t y)142{143return v3d_get_ublinear_pixel_offset(cpp, x, y, 1);144}145146/**147* Returns the byte offset for a given pixel in a UIF layout.148*149* UIF is the general V3D tiling layout shared across 3D, media, and scanout.150* It stores pixels in UIF blocks (2x2 utiles), and UIF blocks are stored in151* 4x4 groups, and those 4x4 groups are then stored in raster order.152*/153static inline uint32_t154v3d_get_uif_pixel_offset(uint32_t cpp, uint32_t image_h, uint32_t x, uint32_t y,155bool do_xor)156{157uint32_t utile_w = v3d_utile_width(cpp);158uint32_t utile_h = v3d_utile_height(cpp);159uint32_t mb_width = utile_w * 2;160uint32_t mb_height = utile_h * 2;161uint32_t log2_mb_width = ffs(mb_width) - 1;162uint32_t log2_mb_height = ffs(mb_height) - 1;163164/* Macroblock X, y */165uint32_t mb_x = x >> log2_mb_width;166uint32_t mb_y = y >> log2_mb_height;167/* X, y within the macroblock */168uint32_t mb_pixel_x = x - (mb_x << log2_mb_width);169uint32_t mb_pixel_y = y - (mb_y << log2_mb_height);170171if (do_xor && (mb_x / 4) & 1)172mb_y ^= 0x10;173174uint32_t mb_h = align(image_h, 1 << log2_mb_height) >> log2_mb_height;175uint32_t mb_id = ((mb_x / 4) * ((mb_h - 1) * 4)) + mb_x + mb_y * 4;176177uint32_t mb_base_addr = mb_id * 256;178179bool top = mb_pixel_y < utile_h;180bool left = mb_pixel_x < utile_w;181182/* Docs have this in pixels, we do bytes here. */183uint32_t mb_tile_offset = (!top * 128 + !left * 64);184185uint32_t utile_x = mb_pixel_x & (utile_w - 1);186uint32_t utile_y = mb_pixel_y & (utile_h - 1);187188uint32_t mb_pixel_address = (mb_base_addr +189mb_tile_offset +190v3d_get_utile_pixel_offset(cpp,191utile_x,192utile_y));193194return mb_pixel_address;195}196197static inline uint32_t198v3d_get_uif_xor_pixel_offset(uint32_t cpp, uint32_t image_h,199uint32_t x, uint32_t y)200{201return v3d_get_uif_pixel_offset(cpp, image_h, x, y, true);202}203204static inline uint32_t205v3d_get_uif_no_xor_pixel_offset(uint32_t cpp, uint32_t image_h,206uint32_t x, uint32_t y)207{208return v3d_get_uif_pixel_offset(cpp, image_h, x, y, false);209}210211/* Loads/stores non-utile-aligned boxes by walking over the destination212* rectangle, computing the address on the GPU, and storing/loading a pixel at213* a time.214*/215static inline void216v3d_move_pixels_unaligned(void *gpu, uint32_t gpu_stride,217void *cpu, uint32_t cpu_stride,218int cpp, uint32_t image_h,219const struct pipe_box *box,220uint32_t (*get_pixel_offset)(uint32_t cpp,221uint32_t image_h,222uint32_t x, uint32_t y),223bool is_load)224{225for (uint32_t y = 0; y < box->height; y++) {226void *cpu_row = cpu + y * cpu_stride;227228for (int x = 0; x < box->width; x++) {229uint32_t pixel_offset = get_pixel_offset(cpp, image_h,230box->x + x,231box->y + y);232233if (false) {234fprintf(stderr, "%3d,%3d -> %d\n",235box->x + x, box->y + y,236pixel_offset);237}238239if (is_load) {240memcpy(cpu_row + x * cpp,241gpu + pixel_offset,242cpp);243} else {244memcpy(gpu + pixel_offset,245cpu_row + x * cpp,246cpp);247}248}249}250}251252/* Breaks the image down into utiles and calls either the fast whole-utile253* load/store functions, or the unaligned fallback case.254*/255static inline void256v3d_move_pixels_general_percpp(void *gpu, uint32_t gpu_stride,257void *cpu, uint32_t cpu_stride,258int cpp, uint32_t image_h,259const struct pipe_box *box,260uint32_t (*get_pixel_offset)(uint32_t cpp,261uint32_t image_h,262uint32_t x, uint32_t y),263bool is_load)264{265uint32_t utile_w = v3d_utile_width(cpp);266uint32_t utile_h = v3d_utile_height(cpp);267uint32_t utile_gpu_stride = utile_w * cpp;268uint32_t x1 = box->x;269uint32_t y1 = box->y;270uint32_t x2 = box->x + box->width;271uint32_t y2 = box->y + box->height;272uint32_t align_x1 = align(x1, utile_w);273uint32_t align_y1 = align(y1, utile_h);274uint32_t align_x2 = x2 & ~(utile_w - 1);275uint32_t align_y2 = y2 & ~(utile_h - 1);276277/* Load/store all the whole utiles first. */278for (uint32_t y = align_y1; y < align_y2; y += utile_h) {279void *cpu_row = cpu + (y - box->y) * cpu_stride;280281for (uint32_t x = align_x1; x < align_x2; x += utile_w) {282void *utile_gpu = (gpu +283get_pixel_offset(cpp, image_h, x, y));284void *utile_cpu = cpu_row + (x - box->x) * cpp;285286if (is_load) {287v3d_load_utile(utile_cpu, cpu_stride,288utile_gpu, utile_gpu_stride);289} else {290v3d_store_utile(utile_gpu, utile_gpu_stride,291utile_cpu, cpu_stride);292}293}294}295296/* If there were no aligned utiles in the middle, load/store the whole297* thing unaligned.298*/299if (align_y2 <= align_y1 ||300align_x2 <= align_x1) {301v3d_move_pixels_unaligned(gpu, gpu_stride,302cpu, cpu_stride,303cpp, image_h,304box,305get_pixel_offset, is_load);306return;307}308309/* Load/store the partial utiles. */310struct pipe_box partial_boxes[4] = {311/* Top */312{313.x = x1,314.width = x2 - x1,315.y = y1,316.height = align_y1 - y1,317},318/* Bottom */319{320.x = x1,321.width = x2 - x1,322.y = align_y2,323.height = y2 - align_y2,324},325/* Left */326{327.x = x1,328.width = align_x1 - x1,329.y = align_y1,330.height = align_y2 - align_y1,331},332/* Right */333{334.x = align_x2,335.width = x2 - align_x2,336.y = align_y1,337.height = align_y2 - align_y1,338},339};340for (int i = 0; i < ARRAY_SIZE(partial_boxes); i++) {341void *partial_cpu = (cpu +342(partial_boxes[i].y - y1) * cpu_stride +343(partial_boxes[i].x - x1) * cpp);344345v3d_move_pixels_unaligned(gpu, gpu_stride,346partial_cpu, cpu_stride,347cpp, image_h,348&partial_boxes[i],349get_pixel_offset, is_load);350}351}352353static inline void354v3d_move_pixels_general(void *gpu, uint32_t gpu_stride,355void *cpu, uint32_t cpu_stride,356int cpp, uint32_t image_h,357const struct pipe_box *box,358uint32_t (*get_pixel_offset)(uint32_t cpp,359uint32_t image_h,360uint32_t x, uint32_t y),361bool is_load)362{363switch (cpp) {364case 1:365v3d_move_pixels_general_percpp(gpu, gpu_stride,366cpu, cpu_stride,3671, image_h, box,368get_pixel_offset,369is_load);370break;371case 2:372v3d_move_pixels_general_percpp(gpu, gpu_stride,373cpu, cpu_stride,3742, image_h, box,375get_pixel_offset,376is_load);377break;378case 4:379v3d_move_pixels_general_percpp(gpu, gpu_stride,380cpu, cpu_stride,3814, image_h, box,382get_pixel_offset,383is_load);384break;385case 8:386v3d_move_pixels_general_percpp(gpu, gpu_stride,387cpu, cpu_stride,3888, image_h, box,389get_pixel_offset,390is_load);391break;392case 16:393v3d_move_pixels_general_percpp(gpu, gpu_stride,394cpu, cpu_stride,39516, image_h, box,396get_pixel_offset,397is_load);398break;399}400}401402static inline void403v3d_move_tiled_image(void *gpu, uint32_t gpu_stride,404void *cpu, uint32_t cpu_stride,405enum v3d_tiling_mode tiling_format,406int cpp,407uint32_t image_h,408const struct pipe_box *box,409bool is_load)410{411switch (tiling_format) {412case V3D_TILING_UIF_XOR:413v3d_move_pixels_general(gpu, gpu_stride,414cpu, cpu_stride,415cpp, image_h, box,416v3d_get_uif_xor_pixel_offset,417is_load);418break;419case V3D_TILING_UIF_NO_XOR:420v3d_move_pixels_general(gpu, gpu_stride,421cpu, cpu_stride,422cpp, image_h, box,423v3d_get_uif_no_xor_pixel_offset,424is_load);425break;426case V3D_TILING_UBLINEAR_2_COLUMN:427v3d_move_pixels_general(gpu, gpu_stride,428cpu, cpu_stride,429cpp, image_h, box,430v3d_get_ublinear_2_column_pixel_offset,431is_load);432break;433case V3D_TILING_UBLINEAR_1_COLUMN:434v3d_move_pixels_general(gpu, gpu_stride,435cpu, cpu_stride,436cpp, image_h, box,437v3d_get_ublinear_1_column_pixel_offset,438is_load);439break;440case V3D_TILING_LINEARTILE:441v3d_move_pixels_general(gpu, gpu_stride,442cpu, cpu_stride,443cpp, image_h, box,444v3d_get_lt_pixel_offset,445is_load);446break;447default:448unreachable("Unsupported tiling format");449break;450}451}452453/**454* Loads pixel data from the start (microtile-aligned) box in \p src to the455* start of \p dst according to the given tiling format.456*/457void458v3d_load_tiled_image(void *dst, uint32_t dst_stride,459void *src, uint32_t src_stride,460enum v3d_tiling_mode tiling_format, int cpp,461uint32_t image_h,462const struct pipe_box *box)463{464v3d_move_tiled_image(src, src_stride,465dst, dst_stride,466tiling_format,467cpp,468image_h,469box,470true);471}472473/**474* Stores pixel data from the start of \p src into a (microtile-aligned) box in475* \p dst according to the given tiling format.476*/477void478v3d_store_tiled_image(void *dst, uint32_t dst_stride,479void *src, uint32_t src_stride,480enum v3d_tiling_mode tiling_format, int cpp,481uint32_t image_h,482const struct pipe_box *box)483{484v3d_move_tiled_image(dst, dst_stride,485src, src_stride,486tiling_format,487cpp,488image_h,489box,490false);491}492493494