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/format/u_format.h"
32 static struct mali_rt_format
33 panfrost_mfbd_format(struct pipe_surface
*surf
)
35 /* Explode details on the format */
37 const struct util_format_description
*desc
=
38 util_format_description(surf
->format
);
40 /* The swizzle for rendering is inverted from texturing */
42 unsigned char swizzle
[4];
43 panfrost_invert_swizzle(desc
->swizzle
, swizzle
);
45 /* Fill in accordingly, defaulting to 8-bit UNORM */
47 struct mali_rt_format fmt
= {
50 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
53 .swizzle
= panfrost_translate_swizzle_4(swizzle
),
57 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
)
58 fmt
.flags
|= MALI_MFBD_FORMAT_SRGB
;
60 /* sRGB handled as a dedicated flag */
61 enum pipe_format linearized
= util_format_linear(surf
->format
);
63 /* If RGB, we're good to go */
64 if (util_format_is_unorm8(desc
))
67 /* Set flags for alternative formats */
70 case PIPE_FORMAT_B5G6R5_UNORM
:
71 fmt
.unk1
= 0x14000000;
72 fmt
.nr_channels
= MALI_POSITIVE(2);
76 case PIPE_FORMAT_A4B4G4R4_UNORM
:
77 case PIPE_FORMAT_B4G4R4A4_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);
93 case PIPE_FORMAT_R8_UINT
:
94 case PIPE_FORMAT_R8_SINT
:
95 fmt
.unk1
= 0x80000000;
97 fmt
.nr_channels
= MALI_POSITIVE(1);
101 case PIPE_FORMAT_R11G11B10_FLOAT
:
102 case PIPE_FORMAT_R8G8B8A8_UINT
:
103 case PIPE_FORMAT_R8G8B8A8_SINT
:
104 case PIPE_FORMAT_R16G16_FLOAT
:
105 case PIPE_FORMAT_R16G16_UINT
:
106 case PIPE_FORMAT_R16G16_SINT
:
107 case PIPE_FORMAT_R32_FLOAT
:
108 case PIPE_FORMAT_R32_UINT
:
109 case PIPE_FORMAT_R32_SINT
:
110 case PIPE_FORMAT_R10G10B10A2_UINT
:
111 fmt
.unk1
= 0x88000000;
113 fmt
.nr_channels
= MALI_POSITIVE(4);
117 case PIPE_FORMAT_R8G8_UINT
:
118 case PIPE_FORMAT_R8G8_SINT
:
119 case PIPE_FORMAT_R16_FLOAT
:
120 case PIPE_FORMAT_R16_UINT
:
121 case PIPE_FORMAT_R16_SINT
:
122 case PIPE_FORMAT_B5G5R5A1_UNORM
:
123 fmt
.unk1
= 0x84000000;
125 fmt
.nr_channels
= MALI_POSITIVE(2);
129 case PIPE_FORMAT_R32G32_FLOAT
:
130 case PIPE_FORMAT_R32G32_SINT
:
131 case PIPE_FORMAT_R32G32_UINT
:
132 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
133 case PIPE_FORMAT_R16G16B16A16_SINT
:
134 case PIPE_FORMAT_R16G16B16A16_UINT
:
135 fmt
.unk1
= 0x8c000000;
137 fmt
.nr_channels
= MALI_POSITIVE(2);
140 /* Generic 128-bit */
141 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
142 case PIPE_FORMAT_R32G32B32A32_SINT
:
143 case PIPE_FORMAT_R32G32B32A32_UINT
:
144 fmt
.unk1
= 0x90000000;
146 fmt
.nr_channels
= MALI_POSITIVE(4);
150 unreachable("Invalid format rendering");
159 struct panfrost_batch
*batch
,
160 struct bifrost_framebuffer
*fb
,
161 struct bifrost_fb_extra
*fbx
,
162 struct bifrost_render_target
*rts
,
165 for (unsigned i
= 0; i
< rt_count
; ++i
) {
166 if (!(batch
->clear
& (PIPE_CLEAR_COLOR0
<< i
)))
169 rts
[i
].clear_color_1
= batch
->clear_color
[i
][0];
170 rts
[i
].clear_color_2
= batch
->clear_color
[i
][1];
171 rts
[i
].clear_color_3
= batch
->clear_color
[i
][2];
172 rts
[i
].clear_color_4
= batch
->clear_color
[i
][3];
175 if (batch
->clear
& PIPE_CLEAR_DEPTH
) {
176 fb
->clear_depth
= batch
->clear_depth
;
179 if (batch
->clear
& PIPE_CLEAR_STENCIL
) {
180 fb
->clear_stencil
= batch
->clear_stencil
;
185 panfrost_mfbd_set_cbuf(
186 struct bifrost_render_target
*rt
,
187 struct pipe_surface
*surf
)
189 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
191 unsigned level
= surf
->u
.tex
.level
;
192 unsigned first_layer
= surf
->u
.tex
.first_layer
;
193 assert(surf
->u
.tex
.last_layer
== first_layer
);
194 int stride
= rsrc
->slices
[level
].stride
;
196 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
);
198 rt
->format
= panfrost_mfbd_format(surf
);
200 /* Now, we set the layout specific pieces */
202 if (rsrc
->layout
== PAN_LINEAR
) {
203 rt
->format
.block
= MALI_BLOCK_LINEAR
;
204 rt
->framebuffer
= base
;
205 rt
->framebuffer_stride
= stride
/ 16;
206 } else if (rsrc
->layout
== PAN_TILED
) {
207 rt
->format
.block
= MALI_BLOCK_TILED
;
208 rt
->framebuffer
= base
;
209 rt
->framebuffer_stride
= stride
;
210 } else if (rsrc
->layout
== PAN_AFBC
) {
211 rt
->format
.block
= MALI_BLOCK_AFBC
;
213 unsigned header_size
= rsrc
->slices
[level
].header_size
;
215 rt
->framebuffer
= base
+ header_size
;
216 rt
->afbc
.metadata
= base
;
218 rt
->afbc
.unk
= 0x30009;
220 /* TODO: The blob sets this to something nonzero, but it's not
221 * clear what/how to calculate/if it matters */
222 rt
->framebuffer_stride
= 0;
224 fprintf(stderr
, "Invalid render layout (cbuf)");
230 panfrost_mfbd_set_zsbuf(
231 struct bifrost_framebuffer
*fb
,
232 struct bifrost_fb_extra
*fbx
,
233 struct pipe_surface
*surf
)
235 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
237 unsigned level
= surf
->u
.tex
.level
;
238 unsigned first_layer
= surf
->u
.tex
.first_layer
;
239 assert(surf
->u
.tex
.last_layer
== first_layer
);
241 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
);
243 if (rsrc
->layout
== PAN_AFBC
) {
244 /* The only Z/S format we can compress is Z24S8 or variants
245 * thereof (handled by the state tracker) */
246 assert(panfrost_is_z24s8_variant(surf
->format
));
248 unsigned header_size
= rsrc
->slices
[level
].header_size
;
250 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
252 fbx
->flags_hi
|= MALI_EXTRA_PRESENT
;
253 fbx
->flags_lo
|= MALI_EXTRA_ZS
| 0x1; /* unknown */
254 fbx
->zs_block
= MALI_BLOCK_AFBC
;
256 fbx
->ds_afbc
.depth_stencil
= base
+ header_size
;
257 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= base
;
258 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
260 fbx
->ds_afbc
.zero1
= 0x10009;
261 fbx
->ds_afbc
.padding
= 0x1000;
262 } else if (rsrc
->layout
== PAN_LINEAR
|| rsrc
->layout
== PAN_TILED
) {
263 /* TODO: Z32F(S8) support, which is always linear */
265 int stride
= rsrc
->slices
[level
].stride
;
267 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
268 fbx
->flags_hi
|= MALI_EXTRA_PRESENT
;
269 fbx
->flags_lo
|= MALI_EXTRA_ZS
;
271 fbx
->ds_linear
.depth
= base
;
273 if (rsrc
->layout
== PAN_LINEAR
) {
274 fbx
->zs_block
= MALI_BLOCK_LINEAR
;
275 fbx
->ds_linear
.depth_stride
= stride
/ 16;
277 fbx
->zs_block
= MALI_BLOCK_TILED
;
278 fbx
->ds_linear
.depth_stride
= stride
;
281 if (panfrost_is_z24s8_variant(surf
->format
)) {
282 fbx
->flags_lo
|= 0x1;
283 } else if (surf
->format
== PIPE_FORMAT_Z32_UNORM
) {
284 /* default flags (0 in bottom place) */
285 } else if (surf
->format
== PIPE_FORMAT_Z32_FLOAT
) {
286 fbx
->flags_lo
|= 0xA;
287 fb
->mfbd_flags
^= 0x100;
288 fb
->mfbd_flags
|= 0x200;
289 } else if (surf
->format
== PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
) {
290 fbx
->flags_hi
|= 0x400;
291 fbx
->flags_lo
|= 0xA;
292 fb
->mfbd_flags
^= 0x100;
293 fb
->mfbd_flags
|= 0x201;
295 struct panfrost_resource
*stencil
= rsrc
->separate_stencil
;
296 struct panfrost_slice stencil_slice
= stencil
->slices
[level
];
298 fbx
->ds_linear
.stencil
= panfrost_get_texture_address(stencil
, level
, first_layer
);
299 fbx
->ds_linear
.stencil_stride
= stencil_slice
.stride
;
307 /* Helper for sequential uploads used for MFBD */
309 #define UPLOAD(dest, offset, src, max) { \
310 size_t sz = sizeof(*src); \
311 memcpy(dest.cpu + offset, src, sz); \
312 assert((offset + sz) <= max); \
317 panfrost_mfbd_upload(struct panfrost_batch
*batch
,
318 struct bifrost_framebuffer
*fb
,
319 struct bifrost_fb_extra
*fbx
,
320 struct bifrost_render_target
*rts
,
325 /* There may be extra data stuck in the middle */
326 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
328 /* Compute total size for transfer */
331 sizeof(struct bifrost_framebuffer
) +
332 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
333 sizeof(struct bifrost_render_target
) * 4;
335 struct panfrost_transfer m_f_trans
=
336 panfrost_allocate_transient(batch
, total_sz
);
338 /* Do the transfer */
340 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
343 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
345 for (unsigned c
= 0; c
< 4; ++c
) {
346 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
349 /* Return pointer suitable for the fragment section */
352 (has_extra
? MALI_MFBD_TAG_EXTRA
: 0) |
353 (MALI_POSITIVE(rt_count
) << 2);
355 return m_f_trans
.gpu
| tag
;
360 static struct bifrost_framebuffer
361 panfrost_emit_mfbd(struct panfrost_batch
*batch
, unsigned vertex_count
)
363 struct panfrost_context
*ctx
= batch
->ctx
;
364 struct pipe_context
*gallium
= (struct pipe_context
*) ctx
;
365 struct panfrost_screen
*screen
= pan_screen(gallium
->screen
);
367 unsigned width
= batch
->key
.width
;
368 unsigned height
= batch
->key
.height
;
370 unsigned shift
= panfrost_get_stack_shift(batch
->stack_size
);
372 struct bifrost_framebuffer framebuffer
= {
373 .width1
= MALI_POSITIVE(width
),
374 .height1
= MALI_POSITIVE(height
),
375 .width2
= MALI_POSITIVE(width
),
376 .height2
= MALI_POSITIVE(height
),
380 .rt_count_1
= MALI_POSITIVE(batch
->key
.nr_cbufs
),
384 .tiler
= panfrost_emit_midg_tiler(batch
, vertex_count
),
386 .stack_shift
= shift
,
388 .scratchpad
= panfrost_batch_get_scratchpad(batch
, shift
, screen
->thread_tls_alloc
, screen
->core_count
)->gpu
395 panfrost_attach_mfbd(struct panfrost_batch
*batch
, unsigned vertex_count
)
397 struct bifrost_framebuffer mfbd
=
398 panfrost_emit_mfbd(batch
, vertex_count
);
400 memcpy(batch
->framebuffer
.cpu
, &mfbd
, sizeof(mfbd
));
403 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
406 panfrost_mfbd_fragment(struct panfrost_batch
*batch
, bool has_draws
)
408 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(batch
, has_draws
);
409 struct bifrost_fb_extra fbx
= {0};
410 struct bifrost_render_target rts
[4] = {0};
412 /* We always upload at least one dummy GL_NONE render target */
414 unsigned rt_descriptors
= MAX2(batch
->key
.nr_cbufs
, 1);
416 fb
.rt_count_1
= MALI_POSITIVE(rt_descriptors
);
417 fb
.rt_count_2
= rt_descriptors
;
418 fb
.mfbd_flags
= 0x100;
420 /* TODO: MRT clear */
421 panfrost_mfbd_clear(batch
, &fb
, &fbx
, rts
, fb
.rt_count_2
);
424 /* Upload either the render target or a dummy GL_NONE target */
426 for (int cb
= 0; cb
< rt_descriptors
; ++cb
) {
427 struct pipe_surface
*surf
= batch
->key
.cbufs
[cb
];
430 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
432 /* What is this? Looks like some extension of the bpp
433 * field. Maybe it establishes how much internal
434 * tilebuffer space is reserved? */
436 unsigned bpp
= util_format_get_blocksize(surf
->format
);
437 fb
.rt_count_2
= MAX2(fb
.rt_count_2
, ALIGN_POT(bpp
, 4) / 4);
439 struct mali_rt_format null_rt
= {
444 rts
[cb
].format
= null_rt
;
445 rts
[cb
].framebuffer
= 0;
446 rts
[cb
].framebuffer_stride
= 0;
449 /* TODO: Break out the field */
450 rts
[cb
].format
.unk1
|= (cb
* 0x400);
453 if (batch
->key
.zsbuf
) {
454 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, batch
->key
.zsbuf
);
457 /* When scanning out, the depth buffer is immediately invalidated, so
458 * we don't need to waste bandwidth writing it out. This can improve
459 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
460 * memory bandwidth!).
462 * The exception is ReadPixels, but this is not supported on GLES so we
463 * can safely ignore it. */
465 if (panfrost_batch_is_scanout(batch
))
466 batch
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
468 /* Actualize the requirements */
470 if (batch
->requirements
& PAN_REQ_MSAA
) {
471 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
474 fb
.unk1
|= (1 << 4) | (1 << 1);
478 if (batch
->requirements
& PAN_REQ_DEPTH_WRITE
)
479 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
481 /* Checksumming only works with a single render target */
483 if (batch
->key
.nr_cbufs
== 1) {
484 struct pipe_surface
*surf
= batch
->key
.cbufs
[0];
485 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
486 struct panfrost_bo
*bo
= rsrc
->bo
;
488 if (rsrc
->checksummed
) {
489 unsigned level
= surf
->u
.tex
.level
;
490 struct panfrost_slice
*slice
= &rsrc
->slices
[level
];
492 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
493 fbx
.flags_lo
|= MALI_EXTRA_PRESENT
;
494 fbx
.checksum_stride
= slice
->checksum_stride
;
495 fbx
.checksum
= bo
->gpu
+ slice
->checksum_offset
;
499 return panfrost_mfbd_upload(batch
, &fb
, &fbx
, rts
, rt_descriptors
);