62d86a678e74dbaebf4de364c9ad91b52a934247
[mesa.git] / src / gallium / drivers / panfrost / pan_mfbd.c
1 /*
2 * Copyright 2018-2019 Alyssa Rosenzweig
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 */
24
25 #include "pan_bo.h"
26 #include "pan_context.h"
27 #include "pan_cmdstream.h"
28 #include "pan_util.h"
29 #include "panfrost-quirks.h"
30
31 static struct mali_rt_format
32 panfrost_mfbd_format(struct pipe_surface *surf)
33 {
34 /* Explode details on the format */
35
36 const struct util_format_description *desc =
37 util_format_description(surf->format);
38
39 /* The swizzle for rendering is inverted from texturing */
40
41 unsigned char swizzle[4];
42 panfrost_invert_swizzle(desc->swizzle, swizzle);
43
44 /* Fill in accordingly, defaulting to 8-bit UNORM */
45
46 struct mali_rt_format fmt = {
47 .unk1 = 0x4000000,
48 .unk2 = 0x1,
49 .nr_channels = MALI_POSITIVE(desc->nr_channels),
50 .unk3 = 0x4,
51 .flags = 0x8,
52 .swizzle = panfrost_translate_swizzle_4(swizzle),
53 .no_preload = true
54 };
55
56 if (desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
57 fmt.flags |= MALI_MFBD_FORMAT_SRGB;
58
59 /* sRGB handled as a dedicated flag */
60 enum pipe_format linearized = util_format_linear(surf->format);
61
62 /* If RGB, we're good to go */
63 if (util_format_is_unorm8(desc))
64 return fmt;
65
66 /* Set flags for alternative formats */
67
68 switch (linearized) {
69 case PIPE_FORMAT_B5G6R5_UNORM:
70 fmt.unk1 = 0x14000000;
71 fmt.nr_channels = MALI_POSITIVE(2);
72 fmt.unk3 |= 0x1;
73 break;
74
75 case PIPE_FORMAT_A4B4G4R4_UNORM:
76 case PIPE_FORMAT_B4G4R4A4_UNORM:
77 fmt.unk1 = 0x10000000;
78 fmt.unk3 = 0x5;
79 fmt.nr_channels = MALI_POSITIVE(1);
80 break;
81
82 case PIPE_FORMAT_R10G10B10A2_UNORM:
83 case PIPE_FORMAT_B10G10R10A2_UNORM:
84 case PIPE_FORMAT_R10G10B10X2_UNORM:
85 case PIPE_FORMAT_B10G10R10X2_UNORM:
86 fmt.unk1 = 0x08000000;
87 fmt.unk3 = 0x6;
88 fmt.nr_channels = MALI_POSITIVE(1);
89 break;
90
91 case PIPE_FORMAT_B5G5R5A1_UNORM:
92 case PIPE_FORMAT_B5G5R5X1_UNORM:
93 fmt.unk1 = 0x18000000;
94 fmt.unk3 = 0x7;
95 fmt.nr_channels = MALI_POSITIVE(2);
96 break;
97
98 /* Generic 8-bit */
99 case PIPE_FORMAT_R8_UINT:
100 case PIPE_FORMAT_R8_SINT:
101 fmt.unk1 = 0x80000000;
102 fmt.unk3 = 0x0;
103 fmt.nr_channels = MALI_POSITIVE(1);
104 break;
105
106 /* Generic 32-bit */
107 case PIPE_FORMAT_R11G11B10_FLOAT:
108 case PIPE_FORMAT_R8G8B8A8_UINT:
109 case PIPE_FORMAT_R8G8B8A8_SINT:
110 case PIPE_FORMAT_R16G16_FLOAT:
111 case PIPE_FORMAT_R16G16_UINT:
112 case PIPE_FORMAT_R16G16_SINT:
113 case PIPE_FORMAT_R32_FLOAT:
114 case PIPE_FORMAT_R32_UINT:
115 case PIPE_FORMAT_R32_SINT:
116 case PIPE_FORMAT_R10G10B10A2_UINT:
117 fmt.unk1 = 0x88000000;
118 fmt.unk3 = 0x0;
119 fmt.nr_channels = MALI_POSITIVE(4);
120 break;
121
122 /* Generic 16-bit */
123 case PIPE_FORMAT_R8G8_UINT:
124 case PIPE_FORMAT_R8G8_SINT:
125 case PIPE_FORMAT_R16_FLOAT:
126 case PIPE_FORMAT_R16_UINT:
127 case PIPE_FORMAT_R16_SINT:
128 fmt.unk1 = 0x84000000;
129 fmt.unk3 = 0x0;
130 fmt.nr_channels = MALI_POSITIVE(2);
131 break;
132
133 /* Generic 64-bit */
134 case PIPE_FORMAT_R32G32_FLOAT:
135 case PIPE_FORMAT_R32G32_SINT:
136 case PIPE_FORMAT_R32G32_UINT:
137 case PIPE_FORMAT_R16G16B16A16_FLOAT:
138 case PIPE_FORMAT_R16G16B16A16_SINT:
139 case PIPE_FORMAT_R16G16B16A16_UINT:
140 fmt.unk1 = 0x8c000000;
141 fmt.unk3 = 0x1;
142 fmt.nr_channels = MALI_POSITIVE(2);
143 break;
144
145 /* Generic 128-bit */
146 case PIPE_FORMAT_R32G32B32A32_FLOAT:
147 case PIPE_FORMAT_R32G32B32A32_SINT:
148 case PIPE_FORMAT_R32G32B32A32_UINT:
149 fmt.unk1 = 0x90000000;
150 fmt.unk3 = 0x1;
151 fmt.nr_channels = MALI_POSITIVE(4);
152 break;
153
154 default:
155 unreachable("Invalid format rendering");
156 }
157
158 return fmt;
159 }
160
161
162 static void
163 panfrost_mfbd_clear(
164 struct panfrost_batch *batch,
165 struct mali_framebuffer *fb,
166 struct mali_framebuffer_extra *fbx,
167 struct mali_render_target *rts,
168 unsigned rt_count)
169 {
170 struct panfrost_context *ctx = batch->ctx;
171 struct pipe_context *gallium = (struct pipe_context *) ctx;
172 struct panfrost_device *dev = pan_device(gallium->screen);
173
174 for (unsigned i = 0; i < rt_count; ++i) {
175 if (!(batch->clear & (PIPE_CLEAR_COLOR0 << i)))
176 continue;
177
178 rts[i].clear_color_1 = batch->clear_color[i][0];
179 rts[i].clear_color_2 = batch->clear_color[i][1];
180 rts[i].clear_color_3 = batch->clear_color[i][2];
181 rts[i].clear_color_4 = batch->clear_color[i][3];
182 }
183
184 if (batch->clear & PIPE_CLEAR_DEPTH) {
185 fb->clear_depth = batch->clear_depth;
186 }
187
188 if (batch->clear & PIPE_CLEAR_STENCIL) {
189 fb->clear_stencil = batch->clear_stencil;
190 }
191
192 if (dev->quirks & IS_BIFROST) {
193 fbx->clear_color_1 = batch->clear_color[0][0];
194 fbx->clear_color_2 = 0xc0000000 | (fbx->clear_color_1 & 0xffff); /* WTF? */
195 }
196 }
197
198 static void
199 panfrost_mfbd_set_cbuf(
200 struct mali_render_target *rt,
201 struct pipe_surface *surf)
202 {
203 struct panfrost_resource *rsrc = pan_resource(surf->texture);
204 struct panfrost_device *dev = pan_device(surf->context->screen);
205 bool is_bifrost = dev->quirks & IS_BIFROST;
206
207 unsigned level = surf->u.tex.level;
208 unsigned first_layer = surf->u.tex.first_layer;
209 assert(surf->u.tex.last_layer == first_layer);
210 int stride = rsrc->slices[level].stride;
211
212 mali_ptr base = panfrost_get_texture_address(rsrc, level, first_layer);
213
214 rt->format = panfrost_mfbd_format(surf);
215
216 /* Now, we set the layout specific pieces */
217
218 if (rsrc->layout == MALI_TEXTURE_LINEAR) {
219 if (is_bifrost) {
220 rt->format.unk4 = 0x1;
221 } else {
222 rt->format.block = MALI_BLOCK_LINEAR;
223 }
224
225 rt->framebuffer = base;
226 rt->framebuffer_stride = stride / 16;
227 } else if (rsrc->layout == MALI_TEXTURE_TILED) {
228 if (is_bifrost) {
229 rt->format.unk3 |= 0x8;
230 } else {
231 rt->format.block = MALI_BLOCK_TILED;
232 }
233
234 rt->framebuffer = base;
235 rt->framebuffer_stride = stride;
236 } else if (rsrc->layout == MALI_TEXTURE_AFBC) {
237 rt->format.block = MALI_BLOCK_AFBC;
238
239 unsigned header_size = rsrc->slices[level].header_size;
240
241 rt->framebuffer = base + header_size;
242 rt->afbc.metadata = base;
243 rt->afbc.stride = 0;
244 rt->afbc.flags = MALI_AFBC_FLAGS;
245
246 unsigned components = util_format_get_nr_components(surf->format);
247
248 /* The "lossless colorspace transform" is lossy for R and RG formats */
249 if (components >= 3)
250 rt->afbc.flags |= MALI_AFBC_YTR;
251
252 /* TODO: The blob sets this to something nonzero, but it's not
253 * clear what/how to calculate/if it matters */
254 rt->framebuffer_stride = 0;
255 } else {
256 fprintf(stderr, "Invalid render layout (cbuf)");
257 assert(0);
258 }
259 }
260
261 static void
262 panfrost_mfbd_set_zsbuf(
263 struct mali_framebuffer *fb,
264 struct mali_framebuffer_extra *fbx,
265 struct pipe_surface *surf)
266 {
267 struct panfrost_device *dev = pan_device(surf->context->screen);
268 bool is_bifrost = dev->quirks & IS_BIFROST;
269 struct panfrost_resource *rsrc = pan_resource(surf->texture);
270
271 unsigned level = surf->u.tex.level;
272 unsigned first_layer = surf->u.tex.first_layer;
273 assert(surf->u.tex.last_layer == first_layer);
274
275 mali_ptr base = panfrost_get_texture_address(rsrc, level, first_layer);
276
277 if (rsrc->layout == MALI_TEXTURE_AFBC) {
278 /* The only Z/S format we can compress is Z24S8 or variants
279 * thereof (handled by the gallium frontend) */
280 assert(panfrost_is_z24s8_variant(surf->format));
281
282 unsigned header_size = rsrc->slices[level].header_size;
283
284 fb->mfbd_flags |= MALI_MFBD_EXTRA;
285
286 fbx->flags_hi |= MALI_EXTRA_PRESENT;
287 fbx->flags_lo |= MALI_EXTRA_ZS | 0x1; /* unknown */
288 fbx->zs_block = MALI_BLOCK_AFBC;
289
290 fbx->ds_afbc.depth_stencil = base + header_size;
291 fbx->ds_afbc.depth_stencil_afbc_metadata = base;
292 fbx->ds_afbc.depth_stencil_afbc_stride = 0;
293
294 fbx->ds_afbc.flags = MALI_AFBC_FLAGS;
295 fbx->ds_afbc.padding = 0x1000;
296 } else if (rsrc->layout == MALI_TEXTURE_LINEAR || rsrc->layout == MALI_TEXTURE_TILED) {
297 /* TODO: Z32F(S8) support, which is always linear */
298
299 int stride = rsrc->slices[level].stride;
300
301 fb->mfbd_flags |= MALI_MFBD_EXTRA;
302 fbx->flags_hi |= MALI_EXTRA_PRESENT;
303 fbx->flags_lo |= MALI_EXTRA_ZS;
304
305 fbx->ds_linear.depth = base;
306
307 if (rsrc->layout == MALI_TEXTURE_LINEAR) {
308 fbx->zs_block = MALI_BLOCK_LINEAR;
309 fbx->ds_linear.depth_stride = stride / 16;
310 } else {
311 if (is_bifrost) {
312 fbx->zs_block = MALI_BLOCK_UNKNOWN;
313 fbx->flags_hi |= 0x4400;
314 fbx->flags_lo |= 0x1;
315 } else {
316 fbx->zs_block = MALI_BLOCK_TILED;
317 }
318
319 fbx->ds_linear.depth_stride = stride;
320 }
321
322 if (panfrost_is_z24s8_variant(surf->format)) {
323 fbx->flags_lo |= 0x1;
324 } else if (surf->format == PIPE_FORMAT_Z32_FLOAT) {
325 fbx->flags_lo |= 0xA;
326 fb->mfbd_flags ^= 0x100;
327 fb->mfbd_flags |= 0x200;
328 } else if (surf->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT) {
329 fbx->flags_hi |= 0x400;
330 fbx->flags_lo |= 0xA;
331 fb->mfbd_flags ^= 0x100;
332 fb->mfbd_flags |= 0x201;
333
334 struct panfrost_resource *stencil = rsrc->separate_stencil;
335 struct panfrost_slice stencil_slice = stencil->slices[level];
336
337 fbx->ds_linear.stencil = panfrost_get_texture_address(stencil, level, first_layer);
338 fbx->ds_linear.stencil_stride = stencil_slice.stride;
339 }
340
341 } else {
342 assert(0);
343 }
344 }
345
346 /* Helper for sequential uploads used for MFBD */
347
348 #define UPLOAD(dest, offset, src, max) { \
349 size_t sz = sizeof(*src); \
350 memcpy(dest.cpu + offset, src, sz); \
351 assert((offset + sz) <= max); \
352 offset += sz; \
353 }
354
355 static mali_ptr
356 panfrost_mfbd_upload(struct panfrost_batch *batch,
357 struct mali_framebuffer *fb,
358 struct mali_framebuffer_extra *fbx,
359 struct mali_render_target *rts,
360 unsigned rt_count)
361 {
362 off_t offset = 0;
363
364 /* There may be extra data stuck in the middle */
365 bool has_extra = fb->mfbd_flags & MALI_MFBD_EXTRA;
366
367 /* Compute total size for transfer */
368
369 size_t total_sz =
370 sizeof(struct mali_framebuffer) +
371 (has_extra ? sizeof(struct mali_framebuffer_extra) : 0) +
372 sizeof(struct mali_render_target) * 4;
373
374 struct panfrost_transfer m_f_trans =
375 panfrost_allocate_transient(batch, total_sz);
376
377 /* Do the transfer */
378
379 UPLOAD(m_f_trans, offset, fb, total_sz);
380
381 if (has_extra)
382 UPLOAD(m_f_trans, offset, fbx, total_sz);
383
384 for (unsigned c = 0; c < 4; ++c) {
385 UPLOAD(m_f_trans, offset, &rts[c], total_sz);
386 }
387
388 /* Return pointer suitable for the fragment section */
389 unsigned tag =
390 MALI_MFBD |
391 (has_extra ? MALI_MFBD_TAG_EXTRA : 0) |
392 (MALI_POSITIVE(rt_count) << 2);
393
394 return m_f_trans.gpu | tag;
395 }
396
397 #undef UPLOAD
398
399 /* Determines whether a framebuffer uses too much tilebuffer space (requiring
400 * us to scale up the tile at a performance penalty). This is conservative but
401 * afaict you get 128-bits per pixel normally */
402
403 static bool
404 pan_is_large_tib(struct panfrost_batch *batch)
405 {
406 unsigned size = 0;
407
408 for (int cb = 0; cb < batch->key.nr_cbufs; ++cb) {
409 struct pipe_surface *surf = batch->key.cbufs[cb];
410 assert(surf);
411 unsigned bpp = util_format_get_blocksize(surf->format);
412 size += ALIGN_POT(bpp, 4);
413 }
414
415 return (size > 16);
416 }
417
418 static struct mali_framebuffer
419 panfrost_emit_mfbd(struct panfrost_batch *batch, unsigned vertex_count)
420 {
421 struct panfrost_context *ctx = batch->ctx;
422 struct pipe_context *gallium = (struct pipe_context *) ctx;
423 struct panfrost_device *dev = pan_device(gallium->screen);
424
425 unsigned width = batch->key.width;
426 unsigned height = batch->key.height;
427
428 struct mali_framebuffer mfbd = {
429 .width1 = MALI_POSITIVE(width),
430 .height1 = MALI_POSITIVE(height),
431 .width2 = MALI_POSITIVE(width),
432 .height2 = MALI_POSITIVE(height),
433
434 /* Seems to configure tib size */
435 .unk1 = pan_is_large_tib(batch) ? 0xc80 : 0x1080,
436
437 .rt_count_1 = MALI_POSITIVE(batch->key.nr_cbufs),
438 .rt_count_2 = 4,
439 };
440
441 if (dev->quirks & IS_BIFROST) {
442 mfbd.msaa.sample_locations = panfrost_emit_sample_locations(batch);
443 mfbd.tiler_meta = panfrost_batch_get_tiler_meta(batch, vertex_count);
444 } else {
445 unsigned shift = panfrost_get_stack_shift(batch->stack_size);
446 struct panfrost_bo *bo = panfrost_batch_get_scratchpad(batch,
447 shift,
448 dev->thread_tls_alloc,
449 dev->core_count);
450 mfbd.shared_memory.stack_shift = shift;
451 mfbd.shared_memory.scratchpad = bo->gpu;
452 mfbd.shared_memory.shared_workgroup_count = ~0;
453
454 mfbd.tiler = panfrost_emit_midg_tiler(batch, vertex_count);
455 }
456
457 return mfbd;
458 }
459
460 void
461 panfrost_attach_mfbd(struct panfrost_batch *batch, unsigned vertex_count)
462 {
463 struct mali_framebuffer mfbd =
464 panfrost_emit_mfbd(batch, vertex_count);
465
466 memcpy(batch->framebuffer.cpu, &mfbd, sizeof(mfbd));
467 }
468
469 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
470
471 mali_ptr
472 panfrost_mfbd_fragment(struct panfrost_batch *batch, bool has_draws)
473 {
474 struct panfrost_device *dev = pan_device(batch->ctx->base.screen);
475 bool is_bifrost = dev->quirks & IS_BIFROST;
476
477 struct mali_framebuffer fb = panfrost_emit_mfbd(batch, has_draws);
478 struct mali_framebuffer_extra fbx = {0};
479 struct mali_render_target rts[4] = {0};
480
481 /* We always upload at least one dummy GL_NONE render target */
482
483 unsigned rt_descriptors = MAX2(batch->key.nr_cbufs, 1);
484
485 fb.rt_count_1 = MALI_POSITIVE(rt_descriptors);
486 fb.rt_count_2 = rt_descriptors;
487 fb.mfbd_flags = 0x100;
488
489 /* TODO: MRT clear */
490 panfrost_mfbd_clear(batch, &fb, &fbx, rts, fb.rt_count_2);
491
492
493 /* Upload either the render target or a dummy GL_NONE target */
494
495 for (int cb = 0; cb < rt_descriptors; ++cb) {
496 struct pipe_surface *surf = batch->key.cbufs[cb];
497
498 if (surf) {
499 panfrost_mfbd_set_cbuf(&rts[cb], surf);
500
501 /* What is this? Looks like some extension of the bpp
502 * field. Maybe it establishes how much internal
503 * tilebuffer space is reserved? */
504
505 unsigned bpp = util_format_get_blocksize(surf->format);
506 fb.rt_count_2 = MAX2(fb.rt_count_2, ALIGN_POT(bpp, 4) / 4);
507 } else {
508 struct mali_rt_format null_rt = {
509 .unk1 = 0x4000000,
510 .no_preload = true
511 };
512
513 if (is_bifrost) {
514 null_rt.flags = 0x8;
515 null_rt.unk3 = 0x8;
516 }
517
518 rts[cb].format = null_rt;
519 rts[cb].framebuffer = 0;
520 rts[cb].framebuffer_stride = 0;
521 }
522
523 /* TODO: Break out the field */
524 rts[cb].format.unk1 |= (cb * 0x400);
525 }
526
527 if (batch->key.zsbuf) {
528 panfrost_mfbd_set_zsbuf(&fb, &fbx, batch->key.zsbuf);
529 }
530
531 /* When scanning out, the depth buffer is immediately invalidated, so
532 * we don't need to waste bandwidth writing it out. This can improve
533 * performance substantially (Z24X8_UNORM 1080p @ 60fps is 475 MB/s of
534 * memory bandwidth!).
535 *
536 * The exception is ReadPixels, but this is not supported on GLES so we
537 * can safely ignore it. */
538
539 if (panfrost_batch_is_scanout(batch))
540 batch->requirements &= ~PAN_REQ_DEPTH_WRITE;
541
542 /* Actualize the requirements */
543
544 if (batch->requirements & PAN_REQ_MSAA) {
545 rts[0].format.flags |= MALI_MFBD_FORMAT_MSAA;
546
547 /* XXX */
548 fb.unk1 |= (1 << 4) | (1 << 1);
549 fb.rt_count_2 = 4;
550 }
551
552 if (batch->requirements & PAN_REQ_DEPTH_WRITE)
553 fb.mfbd_flags |= MALI_MFBD_DEPTH_WRITE;
554
555 /* Checksumming only works with a single render target */
556
557 if (batch->key.nr_cbufs == 1) {
558 struct pipe_surface *surf = batch->key.cbufs[0];
559 struct panfrost_resource *rsrc = pan_resource(surf->texture);
560
561 if (rsrc->checksummed) {
562 unsigned level = surf->u.tex.level;
563 struct panfrost_slice *slice = &rsrc->slices[level];
564
565 fb.mfbd_flags |= MALI_MFBD_EXTRA;
566 fbx.flags_hi |= MALI_EXTRA_PRESENT;
567 fbx.checksum_stride = slice->checksum_stride;
568 if (slice->checksum_bo)
569 fbx.checksum = slice->checksum_bo->gpu;
570 else
571 fbx.checksum = rsrc->bo->gpu + slice->checksum_offset;
572 }
573 }
574
575 return panfrost_mfbd_upload(batch, &fb, &fbx, rts, rt_descriptors);
576 }