panfrost: The texture descriptor has a pointer to a trampoline
[mesa.git] / src / panfrost / encoder / pan_texture.c
1 /*
2 * Copyright (C) 2008 VMware, Inc.
3 * Copyright (C) 2014 Broadcom
4 * Copyright (C) 2018-2019 Alyssa Rosenzweig
5 * Copyright (C) 2019-2020 Collabora, Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 */
27
28 #include "util/macros.h"
29 #include "util/u_math.h"
30 #include "pan_texture.h"
31
32 /* Generates a texture descriptor. Ideally, descriptors are immutable after the
33 * texture is created, so we can keep these hanging around in GPU memory in a
34 * dedicated BO and not have to worry. In practice there are some minor gotchas
35 * with this (the driver sometimes will change the format of a texture on the
36 * fly for compression) but it's fast enough to just regenerate the descriptor
37 * in those cases, rather than monkeypatching at drawtime.
38 *
39 * A texture descriptor consists of a 32-byte mali_texture_descriptor structure
40 * followed by a variable number of pointers. Due to this variance and
41 * potentially large size, we actually upload directly rather than returning
42 * the descriptor. Whether the user does a copy themselves or not is irrelevant
43 * to us here.
44 */
45
46 /* Check if we need to set a custom stride by computing the "expected"
47 * stride and comparing it to what the user actually wants. Only applies
48 * to linear textures, since tiled/compressed textures have strict
49 * alignment requirements for their strides as it is */
50
51 static bool
52 panfrost_needs_explicit_stride(
53 struct panfrost_slice *slices,
54 uint16_t width,
55 unsigned first_level, unsigned last_level,
56 unsigned bytes_per_pixel)
57 {
58 for (unsigned l = first_level; l <= last_level; ++l) {
59 unsigned actual = slices[l].stride;
60 unsigned expected = u_minify(width, l) * bytes_per_pixel;
61
62 if (actual != expected)
63 return true;
64 }
65
66 return false;
67 }
68
69 /* A Scalable Texture Compression (ASTC) corresponds to just a few texture type
70 * in the hardware, but in fact can be parametrized to have various widths and
71 * heights for the so-called "stretch factor". It turns out these parameters
72 * are stuffed in the bottom bits of the payload pointers. This functions
73 * computes these magic stuffing constants based on the ASTC format in use. The
74 * constant in a given dimension is 3-bits, and two are stored side-by-side for
75 * each active dimension.
76 */
77
78 static unsigned
79 panfrost_astc_stretch(unsigned dim)
80 {
81 assert(dim >= 4 && dim <= 12);
82 return MIN2(dim, 11) - 4;
83 }
84
85 /* Texture addresses are tagged with information about AFBC (colour AFBC?) xor
86 * ASTC (stretch factor) if in use. */
87
88 static unsigned
89 panfrost_compression_tag(
90 const struct util_format_description *desc,
91 enum mali_format format, enum mali_texture_layout layout)
92 {
93 if (layout == MALI_TEXTURE_AFBC)
94 return util_format_has_depth(desc) ? 0x0 : 0x1;
95 else if (format == MALI_ASTC_HDR_SUPP || format == MALI_ASTC_SRGB_SUPP)
96 return (panfrost_astc_stretch(desc->block.height) << 3) |
97 panfrost_astc_stretch(desc->block.width);
98 else
99 return 0;
100 }
101
102
103 /* Cubemaps have 6 faces as "layers" in between each actual layer. We
104 * need to fix this up. TODO: logic wrong in the asserted out cases ...
105 * can they happen, perhaps from cubemap arrays? */
106
107 static void
108 panfrost_adjust_cube_dimensions(
109 unsigned *first_face, unsigned *last_face,
110 unsigned *first_layer, unsigned *last_layer)
111 {
112 *first_face = *first_layer % 6;
113 *last_face = *last_layer % 6;
114 *first_layer /= 6;
115 *last_layer /= 6;
116
117 assert((*first_layer == *last_layer) || (*first_face == 0 && *last_face == 5));
118 }
119
120 /* Following the texture descriptor is a number of pointers. How many? */
121
122 static unsigned
123 panfrost_texture_num_elements(
124 unsigned first_level, unsigned last_level,
125 unsigned first_layer, unsigned last_layer,
126 bool is_cube, bool manual_stride)
127 {
128 unsigned first_face = 0, last_face = 0;
129
130 if (is_cube) {
131 panfrost_adjust_cube_dimensions(&first_face, &last_face,
132 &first_layer, &last_layer);
133 }
134
135 unsigned levels = 1 + last_level - first_level;
136 unsigned layers = 1 + last_layer - first_layer;
137 unsigned faces = 1 + last_face - first_face;
138 unsigned num_elements = levels * layers * faces;
139
140 if (manual_stride)
141 num_elements *= 2;
142
143 return num_elements;
144 }
145
146 /* Conservative estimate of the size of the texture payload a priori.
147 * Average case, size equal to the actual size. Worst case, off by 2x (if
148 * a manual stride is not needed on a linear texture). Returned value
149 * must be greater than or equal to the actual size, so it's safe to use
150 * as an allocation amount */
151
152 unsigned
153 panfrost_estimate_texture_payload_size(
154 unsigned first_level, unsigned last_level,
155 unsigned first_layer, unsigned last_layer,
156 enum mali_texture_type type, enum mali_texture_layout layout)
157 {
158 /* Assume worst case */
159 unsigned manual_stride = (layout == MALI_TEXTURE_LINEAR);
160
161 unsigned elements = panfrost_texture_num_elements(
162 first_level, last_level,
163 first_layer, last_layer,
164 type == MALI_TEX_CUBE, manual_stride);
165
166 return sizeof(mali_ptr) * elements;
167 }
168
169 static void
170 panfrost_emit_texture_payload(
171 mali_ptr *payload,
172 const struct util_format_description *desc,
173 enum mali_format mali_format,
174 enum mali_texture_type type,
175 enum mali_texture_layout layout,
176 unsigned first_level, unsigned last_level,
177 unsigned first_layer, unsigned last_layer,
178 unsigned cube_stride,
179 bool manual_stride,
180 mali_ptr base,
181 struct panfrost_slice *slices)
182 {
183 base |= panfrost_compression_tag(desc, mali_format, layout);
184
185 /* Inject the addresses in, interleaving array indices, mip levels,
186 * cube faces, and strides in that order */
187
188 unsigned first_face = 0, last_face = 0, face_mult = 1;
189
190 if (type == MALI_TEX_CUBE) {
191 face_mult = 6;
192 panfrost_adjust_cube_dimensions(&first_face, &last_face, &first_layer, &last_layer);
193 }
194
195 unsigned idx = 0;
196
197 for (unsigned w = first_layer; w <= last_layer; ++w) {
198 for (unsigned l = first_level; l <= last_level; ++l) {
199 for (unsigned f = first_face; f <= last_face; ++f) {
200 payload[idx++] = base + panfrost_texture_offset(
201 slices, type == MALI_TEX_3D,
202 cube_stride, l, w * face_mult + f);
203
204 if (manual_stride)
205 payload[idx++] = slices[l].stride;
206 }
207 }
208 }
209 }
210
211 void
212 panfrost_new_texture(
213 void *out,
214 uint16_t width, uint16_t height,
215 uint16_t depth, uint16_t array_size,
216 enum pipe_format format,
217 enum mali_texture_type type,
218 enum mali_texture_layout layout,
219 unsigned first_level, unsigned last_level,
220 unsigned first_layer, unsigned last_layer,
221 unsigned cube_stride,
222 unsigned swizzle,
223 mali_ptr base,
224 struct panfrost_slice *slices)
225 {
226 const struct util_format_description *desc =
227 util_format_description(format);
228
229 unsigned bytes_per_pixel = util_format_get_blocksize(format);
230
231 enum mali_format mali_format = panfrost_find_format(desc);
232
233 bool manual_stride = (layout == MALI_TEXTURE_LINEAR)
234 && panfrost_needs_explicit_stride(slices, width,
235 first_level, last_level, bytes_per_pixel);
236
237 struct mali_texture_descriptor descriptor = {
238 .width = MALI_POSITIVE(u_minify(width, first_level)),
239 .height = MALI_POSITIVE(u_minify(height, first_level)),
240 .depth = MALI_POSITIVE(u_minify(depth, first_level)),
241 .array_size = MALI_POSITIVE(array_size),
242 .format = {
243 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
244 .format = mali_format,
245 .srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB),
246 .type = type,
247 .layout = layout,
248 .manual_stride = manual_stride,
249 .unknown2 = 1,
250 },
251 .levels = last_level - first_level,
252 .swizzle = swizzle
253 };
254
255 memcpy(out, &descriptor, sizeof(descriptor));
256
257 mali_ptr *payload = (mali_ptr *) (out + sizeof(struct mali_texture_descriptor));
258 panfrost_emit_texture_payload(
259 payload,
260 desc,
261 mali_format,
262 type,
263 layout,
264 first_level, last_level,
265 first_layer, last_layer,
266 cube_stride,
267 manual_stride,
268 base,
269 slices);
270 }
271
272 void
273 panfrost_new_texture_bifrost(
274 struct bifrost_texture_descriptor *descriptor,
275 uint16_t width, uint16_t height,
276 uint16_t depth, uint16_t array_size,
277 enum pipe_format format,
278 enum mali_texture_type type,
279 enum mali_texture_layout layout,
280 unsigned first_level, unsigned last_level,
281 unsigned first_layer, unsigned last_layer,
282 unsigned cube_stride,
283 unsigned swizzle,
284 mali_ptr base,
285 struct panfrost_slice *slices,
286 struct panfrost_bo *payload)
287 {
288 const struct util_format_description *desc =
289 util_format_description(format);
290
291 enum mali_format mali_format = panfrost_find_format(desc);
292
293 /* Apparently it's always needed in Bifrost? */
294 bool manual_stride = true;
295
296 panfrost_emit_texture_payload(
297 (mali_ptr *) payload->cpu,
298 desc,
299 mali_format,
300 type,
301 layout,
302 first_level, last_level,
303 first_layer, last_layer,
304 cube_stride,
305 manual_stride,
306 base,
307 slices);
308
309 descriptor->format_unk = 0x2;
310 descriptor->type = type;
311 descriptor->format_unk2 = 0x100;
312 descriptor->format = mali_format;
313 descriptor->srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB);
314 descriptor->format_unk3 = 0x0;
315 descriptor->width = MALI_POSITIVE(u_minify(width, first_level));
316 descriptor->height = MALI_POSITIVE(u_minify(height, first_level));
317 descriptor->swizzle = swizzle;
318 descriptor->layout = layout;
319 descriptor->levels = last_level - first_level;
320 descriptor->unk1 = 0x0;
321 descriptor->levels_unk = 0;
322 descriptor->level_2 = 0;
323 descriptor->payload = payload->gpu;
324 descriptor->array_size = MALI_POSITIVE(array_size);
325 descriptor->unk4 = 0x0;
326 descriptor->depth = MALI_POSITIVE(u_minify(depth, first_level));
327 descriptor->unk5 = 0x0;
328 }
329
330 /* Computes sizes for checksumming, which is 8 bytes per 16x16 tile.
331 * Checksumming is believed to be a CRC variant (CRC64 based on the size?).
332 * This feature is also known as "transaction elimination". */
333
334 #define CHECKSUM_TILE_WIDTH 16
335 #define CHECKSUM_TILE_HEIGHT 16
336 #define CHECKSUM_BYTES_PER_TILE 8
337
338 unsigned
339 panfrost_compute_checksum_size(
340 struct panfrost_slice *slice,
341 unsigned width,
342 unsigned height)
343 {
344 unsigned aligned_width = ALIGN_POT(width, CHECKSUM_TILE_WIDTH);
345 unsigned aligned_height = ALIGN_POT(height, CHECKSUM_TILE_HEIGHT);
346
347 unsigned tile_count_x = aligned_width / CHECKSUM_TILE_WIDTH;
348 unsigned tile_count_y = aligned_height / CHECKSUM_TILE_HEIGHT;
349
350 slice->checksum_stride = tile_count_x * CHECKSUM_BYTES_PER_TILE;
351
352 return slice->checksum_stride * tile_count_y;
353 }
354
355 unsigned
356 panfrost_get_layer_stride(struct panfrost_slice *slices, bool is_3d, unsigned cube_stride, unsigned level)
357 {
358 return is_3d ? slices[level].size0 : cube_stride;
359 }
360
361 /* Computes the offset into a texture at a particular level/face. Add to
362 * the base address of a texture to get the address to that level/face */
363
364 unsigned
365 panfrost_texture_offset(struct panfrost_slice *slices, bool is_3d, unsigned cube_stride, unsigned level, unsigned face)
366 {
367 unsigned layer_stride = panfrost_get_layer_stride(slices, is_3d, cube_stride, level);
368 return slices[level].offset + (face * layer_stride);
369 }