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