pan_bo.h: add time.h include for time_t
[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 compressed formats.
86 * AFBC uses a bit for whether the colorspace transform is enabled (RGB and
87 * RGBA only).
88 * For ASTC, this is a "stretch factor" encoding the block size. */
89
90 static unsigned
91 panfrost_compression_tag(
92 const struct util_format_description *desc,
93 enum mali_format format, enum mali_texture_layout layout)
94 {
95 if (layout == MALI_TEXTURE_AFBC)
96 return desc->nr_channels >= 3;
97 else if (format == MALI_ASTC_HDR_SUPP || format == MALI_ASTC_SRGB_SUPP)
98 return (panfrost_astc_stretch(desc->block.height) << 3) |
99 panfrost_astc_stretch(desc->block.width);
100 else
101 return 0;
102 }
103
104
105 /* Cubemaps have 6 faces as "layers" in between each actual layer. We
106 * need to fix this up. TODO: logic wrong in the asserted out cases ...
107 * can they happen, perhaps from cubemap arrays? */
108
109 static void
110 panfrost_adjust_cube_dimensions(
111 unsigned *first_face, unsigned *last_face,
112 unsigned *first_layer, unsigned *last_layer)
113 {
114 *first_face = *first_layer % 6;
115 *last_face = *last_layer % 6;
116 *first_layer /= 6;
117 *last_layer /= 6;
118
119 assert((*first_layer == *last_layer) || (*first_face == 0 && *last_face == 5));
120 }
121
122 /* Following the texture descriptor is a number of pointers. How many? */
123
124 static unsigned
125 panfrost_texture_num_elements(
126 unsigned first_level, unsigned last_level,
127 unsigned first_layer, unsigned last_layer,
128 bool is_cube, bool manual_stride)
129 {
130 unsigned first_face = 0, last_face = 0;
131
132 if (is_cube) {
133 panfrost_adjust_cube_dimensions(&first_face, &last_face,
134 &first_layer, &last_layer);
135 }
136
137 unsigned levels = 1 + last_level - first_level;
138 unsigned layers = 1 + last_layer - first_layer;
139 unsigned faces = 1 + last_face - first_face;
140 unsigned num_elements = levels * layers * faces;
141
142 if (manual_stride)
143 num_elements *= 2;
144
145 return num_elements;
146 }
147
148 /* Conservative estimate of the size of the texture payload a priori.
149 * Average case, size equal to the actual size. Worst case, off by 2x (if
150 * a manual stride is not needed on a linear texture). Returned value
151 * must be greater than or equal to the actual size, so it's safe to use
152 * as an allocation amount */
153
154 unsigned
155 panfrost_estimate_texture_payload_size(
156 unsigned first_level, unsigned last_level,
157 unsigned first_layer, unsigned last_layer,
158 enum mali_texture_type type, enum mali_texture_layout layout)
159 {
160 /* Assume worst case */
161 unsigned manual_stride = (layout == MALI_TEXTURE_LINEAR);
162
163 unsigned elements = panfrost_texture_num_elements(
164 first_level, last_level,
165 first_layer, last_layer,
166 type == MALI_TEX_CUBE, manual_stride);
167
168 return sizeof(mali_ptr) * elements;
169 }
170
171 /* Bifrost requires a tile stride for tiled textures. This stride is computed
172 * as (16 * bpp * width) assuming there is at least one tile (width >= 16).
173 * Otherwise if width < 16, the blob puts zero. Interactions with AFBC are
174 * currently unknown.
175 */
176
177 static unsigned
178 panfrost_nonlinear_stride(enum mali_texture_layout layout,
179 unsigned bytes_per_pixel,
180 unsigned width)
181 {
182 if (layout == MALI_TEXTURE_TILED) {
183 return (width < 16) ? 0 : (16 * bytes_per_pixel * ALIGN_POT(width, 16));
184 } else {
185 unreachable("TODO: AFBC on Bifrost");
186 }
187 }
188
189 static void
190 panfrost_emit_texture_payload(
191 mali_ptr *payload,
192 const struct util_format_description *desc,
193 enum mali_format mali_format,
194 enum mali_texture_type type,
195 enum mali_texture_layout layout,
196 unsigned width,
197 unsigned first_level, unsigned last_level,
198 unsigned first_layer, unsigned last_layer,
199 unsigned cube_stride,
200 bool manual_stride,
201 mali_ptr base,
202 struct panfrost_slice *slices)
203 {
204 base |= panfrost_compression_tag(desc, mali_format, layout);
205
206 /* Inject the addresses in, interleaving array indices, mip levels,
207 * cube faces, and strides in that order */
208
209 unsigned first_face = 0, last_face = 0, face_mult = 1;
210
211 if (type == MALI_TEX_CUBE) {
212 face_mult = 6;
213 panfrost_adjust_cube_dimensions(&first_face, &last_face, &first_layer, &last_layer);
214 }
215
216 unsigned idx = 0;
217
218 for (unsigned w = first_layer; w <= last_layer; ++w) {
219 for (unsigned l = first_level; l <= last_level; ++l) {
220 for (unsigned f = first_face; f <= last_face; ++f) {
221 payload[idx++] = base + panfrost_texture_offset(
222 slices, type == MALI_TEX_3D,
223 cube_stride, l, w * face_mult + f);
224
225 if (manual_stride) {
226 payload[idx++] = (layout == MALI_TEXTURE_LINEAR) ?
227 slices[l].stride :
228 panfrost_nonlinear_stride(layout,
229 MAX2(desc->block.bits / 8, 1),
230 u_minify(width, l));
231 }
232 }
233 }
234 }
235 }
236
237 void
238 panfrost_new_texture(
239 void *out,
240 uint16_t width, uint16_t height,
241 uint16_t depth, uint16_t array_size,
242 enum pipe_format format,
243 enum mali_texture_type type,
244 enum mali_texture_layout layout,
245 unsigned first_level, unsigned last_level,
246 unsigned first_layer, unsigned last_layer,
247 unsigned cube_stride,
248 unsigned swizzle,
249 mali_ptr base,
250 struct panfrost_slice *slices)
251 {
252 const struct util_format_description *desc =
253 util_format_description(format);
254
255 unsigned bytes_per_pixel = util_format_get_blocksize(format);
256
257 enum mali_format mali_format = panfrost_pipe_format_table[desc->format].hw;
258 assert(mali_format);
259
260 bool manual_stride = (layout == MALI_TEXTURE_LINEAR)
261 && panfrost_needs_explicit_stride(slices, width,
262 first_level, last_level, bytes_per_pixel);
263
264 struct mali_texture_descriptor descriptor = {
265 .width = MALI_POSITIVE(u_minify(width, first_level)),
266 .height = MALI_POSITIVE(u_minify(height, first_level)),
267 .depth = MALI_POSITIVE(u_minify(depth, first_level)),
268 .array_size = MALI_POSITIVE(array_size),
269 .format = {
270 .swizzle = panfrost_translate_swizzle_4(desc->swizzle),
271 .format = mali_format,
272 .srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB),
273 .type = type,
274 .layout = layout,
275 .manual_stride = manual_stride,
276 .unknown2 = 1,
277 },
278 .levels = last_level - first_level,
279 .swizzle = swizzle
280 };
281
282 memcpy(out, &descriptor, sizeof(descriptor));
283
284 mali_ptr *payload = (mali_ptr *) (out + sizeof(struct mali_texture_descriptor));
285 panfrost_emit_texture_payload(
286 payload,
287 desc,
288 mali_format,
289 type,
290 layout,
291 width,
292 first_level, last_level,
293 first_layer, last_layer,
294 cube_stride,
295 manual_stride,
296 base,
297 slices);
298 }
299
300 void
301 panfrost_new_texture_bifrost(
302 struct bifrost_texture_descriptor *descriptor,
303 uint16_t width, uint16_t height,
304 uint16_t depth, uint16_t array_size,
305 enum pipe_format format,
306 enum mali_texture_type type,
307 enum mali_texture_layout layout,
308 unsigned first_level, unsigned last_level,
309 unsigned first_layer, unsigned last_layer,
310 unsigned cube_stride,
311 unsigned swizzle,
312 mali_ptr base,
313 struct panfrost_slice *slices,
314 struct panfrost_bo *payload)
315 {
316 const struct util_format_description *desc =
317 util_format_description(format);
318
319 enum mali_format mali_format = panfrost_pipe_format_table[desc->format].hw;
320 assert(mali_format);
321
322 panfrost_emit_texture_payload(
323 (mali_ptr *) payload->cpu,
324 desc,
325 mali_format,
326 type,
327 layout,
328 width,
329 first_level, last_level,
330 first_layer, last_layer,
331 cube_stride,
332 true, /* Stride explicit on Bifrost */
333 base,
334 slices);
335
336 descriptor->format_unk = 0x2;
337 descriptor->type = type;
338 descriptor->format = mali_format;
339 descriptor->srgb = (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB);
340 descriptor->format_unk3 = 0x0;
341 descriptor->width = MALI_POSITIVE(u_minify(width, first_level));
342 descriptor->height = MALI_POSITIVE(u_minify(height, first_level));
343 descriptor->swizzle = swizzle;
344 descriptor->layout = layout;
345 descriptor->levels = last_level - first_level;
346 descriptor->unk1 = 0x0;
347 descriptor->levels_unk = 0;
348 descriptor->level_2 = 0;
349 descriptor->payload = payload->gpu;
350 descriptor->array_size = MALI_POSITIVE(array_size);
351 descriptor->unk4 = 0x0;
352 descriptor->depth = MALI_POSITIVE(u_minify(depth, first_level));
353 descriptor->unk5 = 0x0;
354 }
355
356 /* Computes sizes for checksumming, which is 8 bytes per 16x16 tile.
357 * Checksumming is believed to be a CRC variant (CRC64 based on the size?).
358 * This feature is also known as "transaction elimination". */
359
360 #define CHECKSUM_TILE_WIDTH 16
361 #define CHECKSUM_TILE_HEIGHT 16
362 #define CHECKSUM_BYTES_PER_TILE 8
363
364 unsigned
365 panfrost_compute_checksum_size(
366 struct panfrost_slice *slice,
367 unsigned width,
368 unsigned height)
369 {
370 unsigned aligned_width = ALIGN_POT(width, CHECKSUM_TILE_WIDTH);
371 unsigned aligned_height = ALIGN_POT(height, CHECKSUM_TILE_HEIGHT);
372
373 unsigned tile_count_x = aligned_width / CHECKSUM_TILE_WIDTH;
374 unsigned tile_count_y = aligned_height / CHECKSUM_TILE_HEIGHT;
375
376 slice->checksum_stride = tile_count_x * CHECKSUM_BYTES_PER_TILE;
377
378 return slice->checksum_stride * tile_count_y;
379 }
380
381 unsigned
382 panfrost_get_layer_stride(struct panfrost_slice *slices, bool is_3d, unsigned cube_stride, unsigned level)
383 {
384 return is_3d ? slices[level].size0 : cube_stride;
385 }
386
387 /* Computes the offset into a texture at a particular level/face. Add to
388 * the base address of a texture to get the address to that level/face */
389
390 unsigned
391 panfrost_texture_offset(struct panfrost_slice *slices, bool is_3d, unsigned cube_stride, unsigned level, unsigned face)
392 {
393 unsigned layer_stride = panfrost_get_layer_stride(slices, is_3d, cube_stride, level);
394 return slices[level].offset + (face * layer_stride);
395 }