freedreno/a6xx: fix 3d+tiled layout
[mesa.git] / src / gallium / drivers / freedreno / a6xx / fd6_resource.c
1 /*
2 * Copyright (C) 2018 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
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 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 */
27
28 #include "fd6_resource.h"
29
30 /* indexed by cpp, including msaa 2x and 4x: */
31 static const struct {
32 unsigned pitchalign;
33 unsigned heightalign;
34 } tile_alignment[] = {
35 [1] = { 128, 32 },
36 [2] = { 64, 32 },
37 [3] = { 64, 32 },
38 [4] = { 64, 16 },
39 [6] = { 64, 16 },
40 [8] = { 64, 16 },
41 [12] = { 64, 16 },
42 [16] = { 64, 16 },
43 [24] = { 64, 16 },
44 [32] = { 64, 16 },
45 [48] = { 64, 16 },
46 [64] = { 64, 16 },
47
48 /* special cases for r16: */
49 [0] = { 128, 16 },
50 };
51
52 /* NOTE: good way to test this is: (for example)
53 * piglit/bin/texelFetch fs sampler3D 100x100x8
54 */
55 static uint32_t
56 setup_slices(struct fd_resource *rsc, uint32_t alignment, enum pipe_format format)
57 {
58 struct pipe_resource *prsc = &rsc->base;
59 struct fd_screen *screen = fd_screen(prsc->screen);
60 enum util_format_layout layout = util_format_description(format)->layout;
61 uint32_t pitchalign = screen->gmem_alignw;
62 uint32_t level, size = 0;
63 uint32_t depth = prsc->depth0;
64 /* linear dimensions: */
65 uint32_t lwidth = prsc->width0;
66 uint32_t lheight = prsc->height0;
67 /* tile_mode dimensions: */
68 uint32_t twidth = util_next_power_of_two(lwidth);
69 uint32_t theight = util_next_power_of_two(lheight);
70 /* in layer_first layout, the level (slice) contains just one
71 * layer (since in fact the layer contains the slices)
72 */
73 uint32_t layers_in_level = rsc->layer_first ? 1 : prsc->array_size;
74 int ta = rsc->cpp;
75
76 /* The z16/r16 formats seem to not play by the normal tiling rules: */
77 if ((rsc->cpp == 2) && (util_format_get_nr_components(format) == 1))
78 ta = 0;
79
80 debug_assert(ta < ARRAY_SIZE(tile_alignment));
81 debug_assert(tile_alignment[ta].pitchalign);
82
83 for (level = 0; level <= prsc->last_level; level++) {
84 struct fd_resource_slice *slice = fd_resource_slice(rsc, level);
85 bool linear_level = fd_resource_level_linear(prsc, level);
86 uint32_t width, height;
87
88 /* tiled levels of 3D textures are rounded up to PoT dimensions: */
89 if ((prsc->target == PIPE_TEXTURE_3D) && rsc->tile_mode && !linear_level) {
90 width = twidth;
91 height = theight;
92 } else {
93 width = lwidth;
94 height = lheight;
95 }
96 uint32_t aligned_height = height;
97 uint32_t blocks;
98
99 if (rsc->tile_mode && !linear_level) {
100 pitchalign = tile_alignment[ta].pitchalign;
101 aligned_height = align(aligned_height,
102 tile_alignment[ta].heightalign);
103 } else {
104 pitchalign = 64;
105 }
106
107 /* The blits used for mem<->gmem work at a granularity of
108 * 32x32, which can cause faults due to over-fetch on the
109 * last level. The simple solution is to over-allocate a
110 * bit the last level to ensure any over-fetch is harmless.
111 * The pitch is already sufficiently aligned, but height
112 * may not be:
113 */
114 if ((level == prsc->last_level) && (prsc->target != PIPE_BUFFER))
115 aligned_height = align(aligned_height, 32);
116
117 if (layout == UTIL_FORMAT_LAYOUT_ASTC)
118 slice->pitch =
119 util_align_npot(width, pitchalign * util_format_get_blockwidth(format));
120 else
121 slice->pitch = align(width, pitchalign);
122
123 slice->offset = size;
124 blocks = util_format_get_nblocks(format, slice->pitch, aligned_height);
125
126 /* 1d array and 2d array textures must all have the same layer size
127 * for each miplevel on a6xx. 3d textures can have different layer
128 * sizes for high levels, but the hw auto-sizer is buggy (or at least
129 * different than what this code does), so as soon as the layer size
130 * range gets into range, we stop reducing it.
131 */
132 if (prsc->target == PIPE_TEXTURE_3D) {
133 if (level <= 1 || (rsc->slices[level - 1].size0 > 0xf000)) {
134 slice->size0 = align(blocks * rsc->cpp, alignment);
135 } else {
136 slice->size0 = rsc->slices[level - 1].size0;
137 }
138 } else {
139 slice->size0 = align(blocks * rsc->cpp, alignment);
140 }
141
142 size += slice->size0 * depth * layers_in_level;
143
144 #if 0
145 debug_printf("%s: %ux%ux%u@%u:\t%2u: stride=%4u, size=%6u,%7u, aligned_height=%3u, blocks=%u\n",
146 util_format_name(prsc->format),
147 width, height, depth, rsc->cpp,
148 level, slice->pitch * rsc->cpp,
149 slice->size0, size, aligned_height, blocks);
150 #endif
151
152 depth = u_minify(depth, 1);
153 lwidth = u_minify(lwidth, 1);
154 lheight = u_minify(lheight, 1);
155 twidth = u_minify(twidth, 1);
156 theight = u_minify(theight, 1);
157 }
158
159 return size;
160 }
161
162 uint32_t
163 fd6_setup_slices(struct fd_resource *rsc)
164 {
165 uint32_t alignment;
166
167 switch (rsc->base.target) {
168 case PIPE_TEXTURE_3D:
169 rsc->layer_first = false;
170 alignment = 4096;
171 break;
172 default:
173 rsc->layer_first = true;
174 alignment = 1;
175 break;
176 }
177
178 return setup_slices(rsc, alignment, rsc->base.format);
179 }