freedreno: simplified slices setup for a3xx
[mesa.git] / src / gallium / drivers / freedreno / a3xx / fd3_resource.c
1 /*
2 * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
3 * Copyright (C) 2019 Khaled Emara <ekhaled1836@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "fd3_resource.h"
26 #include "fd3_format.h"
27
28 static uint32_t
29 setup_slices(struct fd_resource *rsc, uint32_t alignment, enum pipe_format format)
30 {
31 struct pipe_resource *prsc = &rsc->base;
32 struct fd_screen *screen = fd_screen(prsc->screen);
33 uint32_t pitchalign = screen->gmem_alignw;
34 uint32_t level, size = 0;
35 uint32_t width = prsc->width0;
36 uint32_t height = prsc->height0;
37 uint32_t depth = prsc->depth0;
38
39 for (level = 0; level <= prsc->last_level; level++) {
40 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
41 uint32_t blocks;
42
43 slice->pitch = width = align(width, pitchalign);
44 slice->offset = size;
45 blocks = util_format_get_nblocks(format, width, height);
46 /* 1d array and 2d array textures must all have the same layer size
47 * for each miplevel on a3xx. 3d textures can have different layer
48 * sizes for high levels, but the hw auto-sizer is buggy (or at least
49 * different than what this code does), so as soon as the layer size
50 * range gets into range, we stop reducing it.
51 */
52 if (prsc->target == PIPE_TEXTURE_3D && (
53 level == 1 ||
54 (level > 1 && rsc->slices[level - 1].size0 > 0xf000)))
55 slice->size0 = align(blocks * rsc->cpp, alignment);
56 else if (level == 0 || alignment == 1)
57 slice->size0 = align(blocks * rsc->cpp, alignment);
58 else
59 slice->size0 = rsc->slices[level - 1].size0;
60
61 size += slice->size0 * depth * prsc->array_size;
62
63 width = u_minify(width, 1);
64 height = u_minify(height, 1);
65 depth = u_minify(depth, 1);
66 }
67
68 return size;
69 }
70
71 uint32_t
72 fd3_setup_slices(struct fd_resource *rsc)
73 {
74 uint32_t alignment;
75
76 switch (rsc->base.target) {
77 case PIPE_TEXTURE_3D:
78 case PIPE_TEXTURE_1D_ARRAY:
79 case PIPE_TEXTURE_2D_ARRAY:
80 alignment = 4096;
81 break;
82 default:
83 alignment = 1;
84 break;
85 }
86
87 return setup_slices(rsc, alignment, rsc->base.format);
88 }
89
90 static bool
91 ok_format(enum pipe_format pfmt)
92 {
93 enum a3xx_color_fmt fmt = fd3_pipe2color(pfmt);
94
95 if (fmt == ~0)
96 return false;
97
98 return true;
99 }
100
101 unsigned
102 fd3_tile_mode(const struct pipe_resource *tmpl)
103 {
104 if (ok_format(tmpl->format))
105 return TILE_4X4;
106 return LINEAR;
107 }