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"
28 #include "pan_format.h"
30 #include "util/u_format.h"
33 panfrost_invert_swizzle(const unsigned char *in
, unsigned char *out
)
35 /* First, default to all zeroes to prevent uninitialized junk */
37 for (unsigned c
= 0; c
< 4; ++c
)
38 out
[c
] = PIPE_SWIZZLE_0
;
40 /* Now "do" what the swizzle says */
42 for (unsigned c
= 0; c
< 4; ++c
) {
43 unsigned char i
= in
[c
];
46 assert(PIPE_SWIZZLE_X
== 0);
47 if (i
> PIPE_SWIZZLE_W
)
51 unsigned idx
= i
- PIPE_SWIZZLE_X
;
52 out
[idx
] = PIPE_SWIZZLE_X
+ c
;
56 static struct mali_rt_format
57 panfrost_mfbd_format(struct pipe_surface
*surf
)
59 /* Explode details on the format */
61 const struct util_format_description
*desc
=
62 util_format_description(surf
->format
);
64 /* The swizzle for rendering is inverted from texturing */
66 unsigned char swizzle
[4];
67 panfrost_invert_swizzle(desc
->swizzle
, swizzle
);
69 /* Fill in accordingly, defaulting to 8-bit UNORM */
71 struct mali_rt_format fmt
= {
74 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
77 .swizzle
= panfrost_translate_swizzle_4(swizzle
),
81 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
)
82 fmt
.flags
|= MALI_MFBD_FORMAT_SRGB
;
84 /* sRGB handled as a dedicated flag */
85 enum pipe_format linearized
= util_format_linear(surf
->format
);
87 /* If RGB, we're good to go */
88 if (util_format_is_unorm8(desc
))
91 /* Set flags for alternative formats */
94 case PIPE_FORMAT_B5G6R5_UNORM
:
95 fmt
.unk1
= 0x14000000;
96 fmt
.nr_channels
= MALI_POSITIVE(2);
100 case PIPE_FORMAT_A4B4G4R4_UNORM
:
101 case PIPE_FORMAT_B4G4R4A4_UNORM
:
102 fmt
.unk1
= 0x10000000;
104 fmt
.nr_channels
= MALI_POSITIVE(1);
107 case PIPE_FORMAT_R10G10B10A2_UNORM
:
108 case PIPE_FORMAT_B10G10R10A2_UNORM
:
109 case PIPE_FORMAT_R10G10B10X2_UNORM
:
110 case PIPE_FORMAT_B10G10R10X2_UNORM
:
111 fmt
.unk1
= 0x08000000;
113 fmt
.nr_channels
= MALI_POSITIVE(1);
117 case PIPE_FORMAT_R8_UINT
:
118 case PIPE_FORMAT_R8_SINT
:
119 fmt
.unk1
= 0x80000000;
121 fmt
.nr_channels
= MALI_POSITIVE(1);
125 case PIPE_FORMAT_R11G11B10_FLOAT
:
126 case PIPE_FORMAT_R8G8B8A8_UINT
:
127 case PIPE_FORMAT_R8G8B8A8_SINT
:
128 case PIPE_FORMAT_R16G16_FLOAT
:
129 case PIPE_FORMAT_R16G16_UINT
:
130 case PIPE_FORMAT_R16G16_SINT
:
131 case PIPE_FORMAT_R32_FLOAT
:
132 case PIPE_FORMAT_R32_UINT
:
133 case PIPE_FORMAT_R32_SINT
:
134 case PIPE_FORMAT_R10G10B10A2_UINT
:
135 fmt
.unk1
= 0x88000000;
137 fmt
.nr_channels
= MALI_POSITIVE(4);
141 case PIPE_FORMAT_R8G8_UINT
:
142 case PIPE_FORMAT_R8G8_SINT
:
143 case PIPE_FORMAT_R16_FLOAT
:
144 case PIPE_FORMAT_R16_UINT
:
145 case PIPE_FORMAT_R16_SINT
:
146 case PIPE_FORMAT_B5G5R5A1_UNORM
:
147 fmt
.unk1
= 0x84000000;
149 fmt
.nr_channels
= MALI_POSITIVE(2);
153 case PIPE_FORMAT_R32G32_FLOAT
:
154 case PIPE_FORMAT_R32G32_SINT
:
155 case PIPE_FORMAT_R32G32_UINT
:
156 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
157 case PIPE_FORMAT_R16G16B16A16_SINT
:
158 case PIPE_FORMAT_R16G16B16A16_UINT
:
159 fmt
.unk1
= 0x8c000000;
161 fmt
.nr_channels
= MALI_POSITIVE(2);
164 /* Generic 128-bit */
165 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
166 case PIPE_FORMAT_R32G32B32A32_SINT
:
167 case PIPE_FORMAT_R32G32B32A32_UINT
:
168 fmt
.unk1
= 0x90000000;
170 fmt
.nr_channels
= MALI_POSITIVE(4);
174 unreachable("Invalid format rendering");
183 struct panfrost_batch
*batch
,
184 struct bifrost_framebuffer
*fb
,
185 struct bifrost_fb_extra
*fbx
,
186 struct bifrost_render_target
*rts
,
189 for (unsigned i
= 0; i
< rt_count
; ++i
) {
190 if (!(batch
->clear
& (PIPE_CLEAR_COLOR0
<< i
)))
193 rts
[i
].clear_color_1
= batch
->clear_color
[i
][0];
194 rts
[i
].clear_color_2
= batch
->clear_color
[i
][1];
195 rts
[i
].clear_color_3
= batch
->clear_color
[i
][2];
196 rts
[i
].clear_color_4
= batch
->clear_color
[i
][3];
199 if (batch
->clear
& PIPE_CLEAR_DEPTH
) {
200 fb
->clear_depth
= batch
->clear_depth
;
203 if (batch
->clear
& PIPE_CLEAR_STENCIL
) {
204 fb
->clear_stencil
= batch
->clear_stencil
;
209 panfrost_mfbd_set_cbuf(
210 struct bifrost_render_target
*rt
,
211 struct pipe_surface
*surf
)
213 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
215 unsigned level
= surf
->u
.tex
.level
;
216 unsigned first_layer
= surf
->u
.tex
.first_layer
;
217 assert(surf
->u
.tex
.last_layer
== first_layer
);
218 int stride
= rsrc
->slices
[level
].stride
;
220 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
);
222 rt
->format
= panfrost_mfbd_format(surf
);
224 /* Now, we set the layout specific pieces */
226 if (rsrc
->layout
== PAN_LINEAR
) {
227 rt
->format
.block
= MALI_MFBD_BLOCK_LINEAR
;
228 rt
->framebuffer
= base
;
229 rt
->framebuffer_stride
= stride
/ 16;
230 } else if (rsrc
->layout
== PAN_TILED
) {
231 rt
->format
.block
= MALI_MFBD_BLOCK_TILED
;
232 rt
->framebuffer
= base
;
233 rt
->framebuffer_stride
= stride
;
234 } else if (rsrc
->layout
== PAN_AFBC
) {
235 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
237 unsigned header_size
= rsrc
->slices
[level
].header_size
;
239 rt
->framebuffer
= base
+ header_size
;
240 rt
->afbc
.metadata
= base
;
242 rt
->afbc
.unk
= 0x30009;
244 /* TODO: The blob sets this to something nonzero, but it's not
245 * clear what/how to calculate/if it matters */
246 rt
->framebuffer_stride
= 0;
248 fprintf(stderr
, "Invalid render layout (cbuf)");
253 /* Is a format encoded like Z24S8 and therefore compatible for render? */
256 panfrost_is_z24s8_variant(enum pipe_format fmt
)
259 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
260 case PIPE_FORMAT_Z24X8_UNORM
:
268 panfrost_mfbd_set_zsbuf(
269 struct bifrost_framebuffer
*fb
,
270 struct bifrost_fb_extra
*fbx
,
271 struct pipe_surface
*surf
)
273 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
275 unsigned level
= surf
->u
.tex
.level
;
276 assert(surf
->u
.tex
.first_layer
== 0);
278 unsigned offset
= rsrc
->slices
[level
].offset
;
280 if (rsrc
->layout
== PAN_AFBC
) {
281 /* The only Z/S format we can compress is Z24S8 or variants
282 * thereof (handled by the state tracker) */
283 assert(panfrost_is_z24s8_variant(surf
->format
));
285 mali_ptr base
= rsrc
->bo
->gpu
+ offset
;
286 unsigned header_size
= rsrc
->slices
[level
].header_size
;
288 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
297 fbx
->ds_afbc
.depth_stencil
= base
+ header_size
;
298 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= base
;
299 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
301 fbx
->ds_afbc
.zero1
= 0x10009;
302 fbx
->ds_afbc
.padding
= 0x1000;
303 } else if (rsrc
->layout
== PAN_LINEAR
) {
304 /* TODO: Z32F(S8) support, which is always linear */
306 int stride
= rsrc
->slices
[level
].stride
;
308 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
309 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
;
311 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
312 fbx
->ds_linear
.depth_stride
= stride
;
314 if (panfrost_is_z24s8_variant(surf
->format
)) {
316 } else if (surf
->format
== PIPE_FORMAT_Z32_UNORM
) {
317 /* default flags (0 in bottom place) */
318 } else if (surf
->format
== PIPE_FORMAT_Z32_FLOAT
) {
320 fb
->mfbd_flags
^= 0x100;
321 fb
->mfbd_flags
|= 0x200;
322 } else if (surf
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
323 fbx
->flags
|= 0x1000A;
324 fb
->mfbd_flags
^= 0x100;
325 fb
->mfbd_flags
|= 0x201;
327 struct panfrost_resource
*stencil
= rsrc
->separate_stencil
;
328 struct panfrost_slice stencil_slice
= stencil
->slices
[level
];
330 fbx
->ds_linear
.stencil
= stencil
->bo
->gpu
+ stencil_slice
.offset
;
331 fbx
->ds_linear
.stencil_stride
= stencil_slice
.stride
;
339 /* Helper for sequential uploads used for MFBD */
341 #define UPLOAD(dest, offset, src, max) { \
342 size_t sz = sizeof(*src); \
343 memcpy(dest.cpu + offset, src, sz); \
344 assert((offset + sz) <= max); \
349 panfrost_mfbd_upload(struct panfrost_batch
*batch
,
350 struct bifrost_framebuffer
*fb
,
351 struct bifrost_fb_extra
*fbx
,
352 struct bifrost_render_target
*rts
,
357 /* There may be extra data stuck in the middle */
358 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
360 /* Compute total size for transfer */
363 sizeof(struct bifrost_framebuffer
) +
364 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
365 sizeof(struct bifrost_render_target
) * 4;
367 struct panfrost_transfer m_f_trans
=
368 panfrost_allocate_transient(batch
, total_sz
);
370 /* Do the transfer */
372 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
375 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
377 for (unsigned c
= 0; c
< 4; ++c
) {
378 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
381 /* Return pointer suitable for the fragment section */
384 (has_extra
? MALI_MFBD_TAG_EXTRA
: 0) |
385 (MALI_POSITIVE(rt_count
) << 2);
387 return m_f_trans
.gpu
| tag
;
392 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
395 panfrost_mfbd_fragment(struct panfrost_batch
*batch
, bool has_draws
)
397 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(batch
, has_draws
);
398 struct bifrost_fb_extra fbx
= {};
399 struct bifrost_render_target rts
[4] = {};
401 /* We always upload at least one dummy GL_NONE render target */
403 unsigned rt_descriptors
= MAX2(batch
->key
.nr_cbufs
, 1);
405 fb
.rt_count_1
= MALI_POSITIVE(rt_descriptors
);
406 fb
.rt_count_2
= rt_descriptors
;
407 fb
.mfbd_flags
= 0x100;
409 /* TODO: MRT clear */
410 panfrost_mfbd_clear(batch
, &fb
, &fbx
, rts
, fb
.rt_count_2
);
413 /* Upload either the render target or a dummy GL_NONE target */
415 for (int cb
= 0; cb
< rt_descriptors
; ++cb
) {
416 struct pipe_surface
*surf
= batch
->key
.cbufs
[cb
];
419 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
421 /* What is this? Looks like some extension of the bpp
422 * field. Maybe it establishes how much internal
423 * tilebuffer space is reserved? */
425 unsigned bpp
= util_format_get_blocksize(surf
->format
);
426 fb
.rt_count_2
= MAX2(fb
.rt_count_2
, ALIGN_POT(bpp
, 4) / 4);
428 struct mali_rt_format null_rt
= {
433 rts
[cb
].format
= null_rt
;
434 rts
[cb
].framebuffer
= 0;
435 rts
[cb
].framebuffer_stride
= 0;
438 /* TODO: Break out the field */
439 rts
[cb
].format
.unk1
|= (cb
* 0x400);
442 if (batch
->key
.zsbuf
) {
443 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, batch
->key
.zsbuf
);
446 /* When scanning out, the depth buffer is immediately invalidated, so
447 * we don't need to waste bandwidth writing it out. This can improve
448 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
449 * memory bandwidth!).
451 * The exception is ReadPixels, but this is not supported on GLES so we
452 * can safely ignore it. */
454 if (panfrost_batch_is_scanout(batch
))
455 batch
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
457 /* Actualize the requirements */
459 if (batch
->requirements
& PAN_REQ_MSAA
) {
460 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
463 fb
.unk1
|= (1 << 4) | (1 << 1);
467 if (batch
->requirements
& PAN_REQ_DEPTH_WRITE
)
468 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
470 /* Checksumming only works with a single render target */
472 if (batch
->key
.nr_cbufs
== 1) {
473 struct pipe_surface
*surf
= batch
->key
.cbufs
[0];
474 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
475 struct panfrost_bo
*bo
= rsrc
->bo
;
477 if (rsrc
->checksummed
) {
478 unsigned level
= surf
->u
.tex
.level
;
479 struct panfrost_slice
*slice
= &rsrc
->slices
[level
];
481 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
482 fbx
.flags
|= MALI_EXTRA_PRESENT
;
483 fbx
.checksum_stride
= slice
->checksum_stride
;
484 fbx
.checksum
= bo
->gpu
+ slice
->checksum_offset
;
488 return panfrost_mfbd_upload(batch
, &fb
, &fbx
, rts
, rt_descriptors
);