xref: /third_party/mesa3d/src/asahi/lib/tiling.h (revision bf215546)
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#ifndef __AGX_TILING_H
25#define __AGX_TILING_H
26
27#include "util/u_math.h"
28
29void agx_detile(void *tiled, void *linear,
30                unsigned width, unsigned bpp, unsigned linear_pitch,
31                unsigned sx, unsigned sy, unsigned smaxx, unsigned smaxy, unsigned tile_shift);
32
33void agx_tile(void *tiled, void *linear,
34         unsigned width, unsigned bpp, unsigned linear_pitch,
35         unsigned sx, unsigned sy, unsigned smaxx, unsigned smaxy, unsigned tile_shift);
36
37/* Select effective tile size given texture dimensions */
38static inline unsigned
39agx_select_tile_shift(unsigned width, unsigned height, unsigned level, unsigned blocksize)
40{
41   /* Calculate effective width/height due to mipmapping */
42   width = u_minify(width, level);
43   height = u_minify(height, level);
44
45   /* Select the largest square power-of-two tile fitting in the image */
46   unsigned shift = util_logbase2_ceil(MIN3(width, height, 64));
47
48   /* Shrink based on block size */
49   if (blocksize > 4)
50      return MAX2(shift - 1, 0);
51   else
52      return shift;
53}
54
55static inline unsigned
56agx_select_tile_size(unsigned width, unsigned height, unsigned level, unsigned blocksize)
57{
58   return 1 << agx_select_tile_shift(width, height, level, blocksize);
59}
60
61#endif
62