2 * Copyright 2018-2019 Alyssa Rosenzweig
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:
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
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
26 #include "pan_context.h"
27 #include "pan_cmdstream.h"
29 #include "panfrost-quirks.h"
31 static struct mali_rt_format
32 panfrost_mfbd_format(struct pipe_surface
*surf
)
34 /* Explode details on the format */
36 const struct util_format_description
*desc
=
37 util_format_description(surf
->format
);
39 /* The swizzle for rendering is inverted from texturing */
41 unsigned char swizzle
[4];
42 panfrost_invert_swizzle(desc
->swizzle
, swizzle
);
44 /* Fill in accordingly, defaulting to 8-bit UNORM */
46 struct mali_rt_format fmt
= {
49 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
52 .swizzle
= panfrost_translate_swizzle_4(swizzle
),
56 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
)
57 fmt
.flags
|= MALI_MFBD_FORMAT_SRGB
;
59 /* sRGB handled as a dedicated flag */
60 enum pipe_format linearized
= util_format_linear(surf
->format
);
62 /* If RGB, we're good to go */
63 if (util_format_is_unorm8(desc
))
66 /* Set flags for alternative formats */
69 case PIPE_FORMAT_B5G6R5_UNORM
:
70 fmt
.unk1
= 0x14000000;
71 fmt
.nr_channels
= MALI_POSITIVE(2);
75 case PIPE_FORMAT_A4B4G4R4_UNORM
:
76 case PIPE_FORMAT_B4G4R4A4_UNORM
:
77 case PIPE_FORMAT_R4G4B4A4_UNORM
:
78 fmt
.unk1
= 0x10000000;
80 fmt
.nr_channels
= MALI_POSITIVE(1);
83 case PIPE_FORMAT_R10G10B10A2_UNORM
:
84 case PIPE_FORMAT_B10G10R10A2_UNORM
:
85 case PIPE_FORMAT_R10G10B10X2_UNORM
:
86 case PIPE_FORMAT_B10G10R10X2_UNORM
:
87 fmt
.unk1
= 0x08000000;
89 fmt
.nr_channels
= MALI_POSITIVE(1);
92 case PIPE_FORMAT_B5G5R5A1_UNORM
:
93 case PIPE_FORMAT_R5G5B5A1_UNORM
:
94 case PIPE_FORMAT_B5G5R5X1_UNORM
:
95 fmt
.unk1
= 0x18000000;
97 fmt
.nr_channels
= MALI_POSITIVE(2);
101 case PIPE_FORMAT_R8_UINT
:
102 case PIPE_FORMAT_R8_SINT
:
103 fmt
.unk1
= 0x80000000;
105 fmt
.nr_channels
= MALI_POSITIVE(1);
109 case PIPE_FORMAT_R11G11B10_FLOAT
:
110 case PIPE_FORMAT_R8G8B8A8_UINT
:
111 case PIPE_FORMAT_R8G8B8A8_SINT
:
112 case PIPE_FORMAT_R16G16_FLOAT
:
113 case PIPE_FORMAT_R16G16_UINT
:
114 case PIPE_FORMAT_R16G16_SINT
:
115 case PIPE_FORMAT_R32_FLOAT
:
116 case PIPE_FORMAT_R32_UINT
:
117 case PIPE_FORMAT_R32_SINT
:
118 case PIPE_FORMAT_R10G10B10A2_UINT
:
119 fmt
.unk1
= 0x88000000;
121 fmt
.nr_channels
= MALI_POSITIVE(4);
125 case PIPE_FORMAT_R8G8_UINT
:
126 case PIPE_FORMAT_R8G8_SINT
:
127 case PIPE_FORMAT_R16_FLOAT
:
128 case PIPE_FORMAT_R16_UINT
:
129 case PIPE_FORMAT_R16_SINT
:
130 fmt
.unk1
= 0x84000000;
132 fmt
.nr_channels
= MALI_POSITIVE(2);
136 case PIPE_FORMAT_R32G32_FLOAT
:
137 case PIPE_FORMAT_R32G32_SINT
:
138 case PIPE_FORMAT_R32G32_UINT
:
139 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
140 case PIPE_FORMAT_R16G16B16A16_SINT
:
141 case PIPE_FORMAT_R16G16B16A16_UINT
:
142 fmt
.unk1
= 0x8c000000;
144 fmt
.nr_channels
= MALI_POSITIVE(2);
147 /* Generic 128-bit */
148 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
149 case PIPE_FORMAT_R32G32B32A32_SINT
:
150 case PIPE_FORMAT_R32G32B32A32_UINT
:
151 fmt
.unk1
= 0x90000000;
153 fmt
.nr_channels
= MALI_POSITIVE(4);
157 unreachable("Invalid format rendering");
166 struct panfrost_batch
*batch
,
167 struct mali_framebuffer
*fb
,
168 struct mali_framebuffer_extra
*fbx
,
169 struct mali_render_target
*rts
,
172 struct panfrost_context
*ctx
= batch
->ctx
;
173 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
174 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
176 for (unsigned i
= 0; i
< rt_count
; ++i
) {
177 if (!(batch
->clear
& (PIPE_CLEAR_COLOR0
<< i
)))
180 rts
[i
].clear_color_1
= batch
->clear_color
[i
][0];
181 rts
[i
].clear_color_2
= batch
->clear_color
[i
][1];
182 rts
[i
].clear_color_3
= batch
->clear_color
[i
][2];
183 rts
[i
].clear_color_4
= batch
->clear_color
[i
][3];
186 if (batch
->clear
& PIPE_CLEAR_DEPTH
) {
187 fb
->clear_depth
= batch
->clear_depth
;
190 if (batch
->clear
& PIPE_CLEAR_STENCIL
) {
191 fb
->clear_stencil
= batch
->clear_stencil
;
194 if (dev
->quirks
& IS_BIFROST
) {
195 fbx
->clear_color_1
= batch
->clear_color
[0][0];
196 fbx
->clear_color_2
= 0xc0000000 | (fbx
->clear_color_1
& 0xffff); /* WTF? */
201 panfrost_mfbd_set_cbuf(
202 struct mali_render_target
*rt
,
203 struct pipe_surface
*surf
)
205 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
206 struct panfrost_device
*dev
= pan_device(surf
->context
->screen
);
207 bool is_bifrost
= dev
->quirks
& IS_BIFROST
;
209 unsigned level
= surf
->u
.tex
.level
;
210 unsigned first_layer
= surf
->u
.tex
.first_layer
;
211 assert(surf
->u
.tex
.last_layer
== first_layer
);
212 int stride
= rsrc
->slices
[level
].stride
;
214 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
, 0);
216 rt
->format
= panfrost_mfbd_format(surf
);
218 /* Now, we set the layout specific pieces */
220 if (rsrc
->layout
== MALI_TEXTURE_LINEAR
) {
222 rt
->format
.unk4
= 0x1;
224 rt
->format
.block
= MALI_BLOCK_LINEAR
;
227 rt
->framebuffer
= base
;
228 rt
->framebuffer_stride
= stride
/ 16;
229 } else if (rsrc
->layout
== MALI_TEXTURE_TILED
) {
231 rt
->format
.unk3
|= 0x8;
233 rt
->format
.block
= MALI_BLOCK_TILED
;
236 rt
->framebuffer
= base
;
237 rt
->framebuffer_stride
= stride
;
238 } else if (rsrc
->layout
== MALI_TEXTURE_AFBC
) {
239 rt
->format
.block
= MALI_BLOCK_AFBC
;
241 unsigned header_size
= rsrc
->slices
[level
].header_size
;
243 rt
->framebuffer
= base
+ header_size
;
244 rt
->afbc
.metadata
= base
;
246 rt
->afbc
.flags
= MALI_AFBC_FLAGS
;
248 unsigned components
= util_format_get_nr_components(surf
->format
);
250 /* The "lossless colorspace transform" is lossy for R and RG formats */
252 rt
->afbc
.flags
|= MALI_AFBC_YTR
;
254 /* TODO: The blob sets this to something nonzero, but it's not
255 * clear what/how to calculate/if it matters */
256 rt
->framebuffer_stride
= 0;
258 fprintf(stderr
, "Invalid render layout (cbuf)");
264 panfrost_mfbd_set_zsbuf(
265 struct mali_framebuffer
*fb
,
266 struct mali_framebuffer_extra
*fbx
,
267 struct pipe_surface
*surf
)
269 struct panfrost_device
*dev
= pan_device(surf
->context
->screen
);
270 bool is_bifrost
= dev
->quirks
& IS_BIFROST
;
271 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
273 unsigned level
= surf
->u
.tex
.level
;
274 unsigned first_layer
= surf
->u
.tex
.first_layer
;
275 assert(surf
->u
.tex
.last_layer
== first_layer
);
277 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
, 0);
279 if (rsrc
->layout
== MALI_TEXTURE_AFBC
) {
280 /* The only Z/S format we can compress is Z24S8 or variants
281 * thereof (handled by the gallium frontend) */
282 assert(panfrost_is_z24s8_variant(surf
->format
));
284 unsigned header_size
= rsrc
->slices
[level
].header_size
;
286 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
288 fbx
->flags_hi
|= MALI_EXTRA_PRESENT
;
289 fbx
->flags_lo
|= MALI_EXTRA_ZS
| 0x1; /* unknown */
290 fbx
->zs_block
= MALI_BLOCK_AFBC
;
292 fbx
->ds_afbc
.depth_stencil
= base
+ header_size
;
293 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= base
;
294 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
296 fbx
->ds_afbc
.flags
= MALI_AFBC_FLAGS
;
297 fbx
->ds_afbc
.padding
= 0x1000;
298 } else if (rsrc
->layout
== MALI_TEXTURE_LINEAR
|| rsrc
->layout
== MALI_TEXTURE_TILED
) {
299 /* TODO: Z32F(S8) support, which is always linear */
301 int stride
= rsrc
->slices
[level
].stride
;
303 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
304 fbx
->flags_hi
|= MALI_EXTRA_PRESENT
;
305 fbx
->flags_lo
|= MALI_EXTRA_ZS
;
307 fbx
->ds_linear
.depth
= base
;
309 if (rsrc
->layout
== MALI_TEXTURE_LINEAR
) {
310 fbx
->zs_block
= MALI_BLOCK_LINEAR
;
311 fbx
->ds_linear
.depth_stride
= stride
/ 16;
314 fbx
->zs_block
= MALI_BLOCK_UNKNOWN
;
315 fbx
->flags_hi
|= 0x4400;
316 fbx
->flags_lo
|= 0x1;
318 fbx
->zs_block
= MALI_BLOCK_TILED
;
321 fbx
->ds_linear
.depth_stride
= stride
;
324 if (panfrost_is_z24s8_variant(surf
->format
)) {
325 fbx
->flags_lo
|= 0x1;
326 } else if (surf
->format
== PIPE_FORMAT_Z32_FLOAT
) {
327 fbx
->flags_lo
|= 0xA;
328 fb
->mfbd_flags
^= 0x100;
329 fb
->mfbd_flags
|= 0x200;
330 } else if (surf
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
331 fbx
->flags_hi
|= 0x400;
332 fbx
->flags_lo
|= 0xA;
333 fb
->mfbd_flags
^= 0x100;
334 fb
->mfbd_flags
|= 0x201;
336 struct panfrost_resource
*stencil
= rsrc
->separate_stencil
;
337 struct panfrost_slice stencil_slice
= stencil
->slices
[level
];
339 fbx
->ds_linear
.stencil
= panfrost_get_texture_address(stencil
, level
, first_layer
, 0);
340 fbx
->ds_linear
.stencil_stride
= stencil_slice
.stride
;
348 /* Helper for sequential uploads used for MFBD */
350 #define UPLOAD(dest, offset, src, max) { \
351 size_t sz = sizeof(*src); \
352 memcpy(dest.cpu + offset, src, sz); \
353 assert((offset + sz) <= max); \
358 panfrost_mfbd_upload(struct panfrost_batch
*batch
,
359 struct mali_framebuffer
*fb
,
360 struct mali_framebuffer_extra
*fbx
,
361 struct mali_render_target
*rts
,
366 /* There may be extra data stuck in the middle */
367 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
369 /* Compute total size for transfer */
372 sizeof(struct mali_framebuffer
) +
373 (has_extra
? sizeof(struct mali_framebuffer_extra
) : 0) +
374 sizeof(struct mali_render_target
) * 4;
376 struct panfrost_transfer m_f_trans
=
377 panfrost_allocate_transient(batch
, total_sz
);
379 /* Do the transfer */
381 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
384 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
386 for (unsigned c
= 0; c
< 4; ++c
) {
387 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
390 /* Return pointer suitable for the fragment section */
393 (has_extra
? MALI_MFBD_TAG_EXTRA
: 0) |
394 (MALI_POSITIVE(rt_count
) << 2);
396 return m_f_trans
.gpu
| tag
;
401 /* Determines whether a framebuffer uses too much tilebuffer space (requiring
402 * us to scale up the tile at a performance penalty). This is conservative but
403 * afaict you get 128-bits per pixel normally */
406 pan_is_large_tib(struct panfrost_batch
*batch
)
410 for (int cb
= 0; cb
< batch
->key
.nr_cbufs
; ++cb
) {
411 struct pipe_surface
*surf
= batch
->key
.cbufs
[cb
];
413 unsigned bpp
= util_format_get_blocksize(surf
->format
);
414 size
+= ALIGN_POT(bpp
, 4);
420 static struct mali_framebuffer
421 panfrost_emit_mfbd(struct panfrost_batch
*batch
, unsigned vertex_count
)
423 struct panfrost_context
*ctx
= batch
->ctx
;
424 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
425 struct panfrost_device
*dev
= pan_device(gallium
->screen
);
427 unsigned width
= batch
->key
.width
;
428 unsigned height
= batch
->key
.height
;
430 struct mali_framebuffer mfbd
= {
431 .width1
= MALI_POSITIVE(width
),
432 .height1
= MALI_POSITIVE(height
),
433 .width2
= MALI_POSITIVE(width
),
434 .height2
= MALI_POSITIVE(height
),
436 /* Seems to configure tib size */
437 .unk1
= pan_is_large_tib(batch
) ? 0xc80 : 0x1080,
439 .rt_count_1
= MALI_POSITIVE(batch
->key
.nr_cbufs
),
443 if (dev
->quirks
& IS_BIFROST
) {
444 mfbd
.msaa
.sample_locations
= panfrost_emit_sample_locations(batch
);
445 mfbd
.tiler_meta
= panfrost_batch_get_tiler_meta(batch
, vertex_count
);
447 unsigned shift
= panfrost_get_stack_shift(batch
->stack_size
);
448 struct panfrost_bo
*bo
= panfrost_batch_get_scratchpad(batch
,
450 dev
->thread_tls_alloc
,
452 mfbd
.shared_memory
.stack_shift
= shift
;
453 mfbd
.shared_memory
.scratchpad
= bo
->gpu
;
454 mfbd
.shared_memory
.shared_workgroup_count
= ~0;
456 mfbd
.tiler
= panfrost_emit_midg_tiler(batch
, vertex_count
);
463 panfrost_attach_mfbd(struct panfrost_batch
*batch
, unsigned vertex_count
)
465 struct mali_framebuffer mfbd
=
466 panfrost_emit_mfbd(batch
, vertex_count
);
468 memcpy(batch
->framebuffer
.cpu
, &mfbd
, sizeof(mfbd
));
471 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
474 panfrost_mfbd_fragment(struct panfrost_batch
*batch
, bool has_draws
)
476 struct panfrost_device
*dev
= pan_device(batch
->ctx
->base
.screen
);
477 bool is_bifrost
= dev
->quirks
& IS_BIFROST
;
479 struct mali_framebuffer fb
= panfrost_emit_mfbd(batch
, has_draws
);
480 struct mali_framebuffer_extra fbx
= {0};
481 struct mali_render_target rts
[4] = {0};
483 /* We always upload at least one dummy GL_NONE render target */
485 unsigned rt_descriptors
= MAX2(batch
->key
.nr_cbufs
, 1);
487 fb
.rt_count_1
= MALI_POSITIVE(rt_descriptors
);
488 fb
.rt_count_2
= rt_descriptors
;
489 fb
.mfbd_flags
= 0x100;
491 /* TODO: MRT clear */
492 panfrost_mfbd_clear(batch
, &fb
, &fbx
, rts
, fb
.rt_count_2
);
495 /* Upload either the render target or a dummy GL_NONE target */
497 for (int cb
= 0; cb
< rt_descriptors
; ++cb
) {
498 struct pipe_surface
*surf
= batch
->key
.cbufs
[cb
];
501 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
503 /* What is this? Looks like some extension of the bpp
504 * field. Maybe it establishes how much internal
505 * tilebuffer space is reserved? */
507 unsigned bpp
= util_format_get_blocksize(surf
->format
);
508 fb
.rt_count_2
= MAX2(fb
.rt_count_2
, ALIGN_POT(bpp
, 4) / 4);
510 struct mali_rt_format null_rt
= {
520 rts
[cb
].format
= null_rt
;
521 rts
[cb
].framebuffer
= 0;
522 rts
[cb
].framebuffer_stride
= 0;
525 /* TODO: Break out the field */
526 rts
[cb
].format
.unk1
|= (cb
* 0x400);
529 if (batch
->key
.zsbuf
) {
530 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, batch
->key
.zsbuf
);
533 /* When scanning out, the depth buffer is immediately invalidated, so
534 * we don't need to waste bandwidth writing it out. This can improve
535 * performance substantially (Z24X8_UNORM 1080p @ 60fps is 475 MB/s of
536 * memory bandwidth!).
538 * The exception is ReadPixels, but this is not supported on GLES so we
539 * can safely ignore it. */
541 if (panfrost_batch_is_scanout(batch
))
542 batch
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
544 /* Actualize the requirements */
546 if (batch
->requirements
& PAN_REQ_MSAA
) {
547 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
550 fb
.unk1
|= (1 << 4) | (1 << 1);
554 if (batch
->requirements
& PAN_REQ_DEPTH_WRITE
)
555 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
557 /* Checksumming only works with a single render target */
559 if (batch
->key
.nr_cbufs
== 1) {
560 struct pipe_surface
*surf
= batch
->key
.cbufs
[0];
561 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
563 if (rsrc
->checksummed
) {
564 unsigned level
= surf
->u
.tex
.level
;
565 struct panfrost_slice
*slice
= &rsrc
->slices
[level
];
567 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
568 fbx
.flags_hi
|= MALI_EXTRA_PRESENT
;
569 fbx
.checksum_stride
= slice
->checksum_stride
;
570 if (slice
->checksum_bo
)
571 fbx
.checksum
= slice
->checksum_bo
->gpu
;
573 fbx
.checksum
= rsrc
->bo
->gpu
+ slice
->checksum_offset
;
577 return panfrost_mfbd_upload(batch
, &fb
, &fbx
, rts
, rt_descriptors
);