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
25 #include "pan_context.h"
27 #include "pan_format.h"
29 #include "util/u_format.h"
32 panfrost_invert_swizzle(const unsigned char *in
, unsigned char *out
)
34 /* First, default to all zeroes to prevent uninitialized junk */
36 for (unsigned c
= 0; c
< 4; ++c
)
37 out
[c
] = PIPE_SWIZZLE_0
;
39 /* Now "do" what the swizzle says */
41 for (unsigned c
= 0; c
< 4; ++c
) {
42 unsigned char i
= in
[c
];
45 if (i
< PIPE_SWIZZLE_X
|| i
> PIPE_SWIZZLE_W
)
49 unsigned idx
= i
- PIPE_SWIZZLE_X
;
50 out
[idx
] = PIPE_SWIZZLE_X
+ c
;
54 static struct mali_rt_format
55 panfrost_mfbd_format(struct pipe_surface
*surf
)
57 /* Explode details on the format */
59 const struct util_format_description
*desc
=
60 util_format_description(surf
->format
);
62 /* The swizzle for rendering is inverted from texturing */
64 unsigned char swizzle
[4];
65 panfrost_invert_swizzle(desc
->swizzle
, swizzle
);
67 /* Fill in accordingly, defaulting to 8-bit UNORM */
69 struct mali_rt_format fmt
= {
72 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
75 .swizzle
= panfrost_translate_swizzle_4(swizzle
),
79 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
)
80 fmt
.flags
|= MALI_MFBD_FORMAT_SRGB
;
82 /* sRGB handled as a dedicated flag */
83 enum pipe_format linearized
= util_format_linear(surf
->format
);
85 /* If RGB, we're good to go */
86 if (util_format_is_unorm8(desc
))
89 /* Set flags for alternative formats */
92 case PIPE_FORMAT_B5G6R5_UNORM
:
93 fmt
.unk1
= 0x14000000;
94 fmt
.nr_channels
= MALI_POSITIVE(2);
98 case PIPE_FORMAT_A4B4G4R4_UNORM
:
99 case PIPE_FORMAT_B4G4R4A4_UNORM
:
100 fmt
.unk1
= 0x10000000;
102 fmt
.nr_channels
= MALI_POSITIVE(1);
105 case PIPE_FORMAT_R10G10B10A2_UNORM
:
106 case PIPE_FORMAT_B10G10R10A2_UNORM
:
107 case PIPE_FORMAT_R10G10B10X2_UNORM
:
108 case PIPE_FORMAT_B10G10R10X2_UNORM
:
109 fmt
.unk1
= 0x08000000;
111 fmt
.nr_channels
= MALI_POSITIVE(1);
115 case PIPE_FORMAT_R8_UINT
:
116 case PIPE_FORMAT_R8_SINT
:
117 fmt
.unk1
= 0x80000000;
119 fmt
.nr_channels
= MALI_POSITIVE(1);
123 case PIPE_FORMAT_R11G11B10_FLOAT
:
124 case PIPE_FORMAT_R8G8B8A8_UINT
:
125 case PIPE_FORMAT_R8G8B8A8_SINT
:
126 case PIPE_FORMAT_R16G16_FLOAT
:
127 case PIPE_FORMAT_R16G16_UINT
:
128 case PIPE_FORMAT_R16G16_SINT
:
129 case PIPE_FORMAT_R32_FLOAT
:
130 case PIPE_FORMAT_R32_UINT
:
131 case PIPE_FORMAT_R32_SINT
:
132 case PIPE_FORMAT_R10G10B10A2_UINT
:
133 fmt
.unk1
= 0x88000000;
135 fmt
.nr_channels
= MALI_POSITIVE(4);
139 case PIPE_FORMAT_R8G8_UINT
:
140 case PIPE_FORMAT_R8G8_SINT
:
141 case PIPE_FORMAT_R16_FLOAT
:
142 case PIPE_FORMAT_R16_UINT
:
143 case PIPE_FORMAT_R16_SINT
:
144 case PIPE_FORMAT_B5G5R5A1_UNORM
:
145 fmt
.unk1
= 0x84000000;
147 fmt
.nr_channels
= MALI_POSITIVE(2);
151 case PIPE_FORMAT_R32G32_FLOAT
:
152 case PIPE_FORMAT_R32G32_SINT
:
153 case PIPE_FORMAT_R32G32_UINT
:
154 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
155 case PIPE_FORMAT_R16G16B16A16_SINT
:
156 case PIPE_FORMAT_R16G16B16A16_UINT
:
157 fmt
.unk1
= 0x8c000000;
159 fmt
.nr_channels
= MALI_POSITIVE(2);
162 /* Generic 128-bit */
163 case PIPE_FORMAT_R32G32B32A32_FLOAT
:
164 case PIPE_FORMAT_R32G32B32A32_SINT
:
165 case PIPE_FORMAT_R32G32B32A32_UINT
:
166 fmt
.unk1
= 0x90000000;
168 fmt
.nr_channels
= MALI_POSITIVE(4);
172 unreachable("Invalid format rendering");
181 struct panfrost_job
*job
,
182 struct bifrost_framebuffer
*fb
,
183 struct bifrost_fb_extra
*fbx
,
184 struct bifrost_render_target
*rts
,
187 for (unsigned i
= 0; i
< rt_count
; ++i
) {
188 if (!(job
->clear
& (PIPE_CLEAR_COLOR0
<< i
)))
191 rts
[i
].clear_color_1
= job
->clear_color
[i
][0];
192 rts
[i
].clear_color_2
= job
->clear_color
[i
][1];
193 rts
[i
].clear_color_3
= job
->clear_color
[i
][2];
194 rts
[i
].clear_color_4
= job
->clear_color
[i
][3];
197 if (job
->clear
& PIPE_CLEAR_DEPTH
) {
198 fb
->clear_depth
= job
->clear_depth
;
201 if (job
->clear
& PIPE_CLEAR_STENCIL
) {
202 fb
->clear_stencil
= job
->clear_stencil
;
207 panfrost_mfbd_set_cbuf(
208 struct bifrost_render_target
*rt
,
209 struct pipe_surface
*surf
)
211 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
213 unsigned level
= surf
->u
.tex
.level
;
214 unsigned first_layer
= surf
->u
.tex
.first_layer
;
215 assert(surf
->u
.tex
.last_layer
== first_layer
);
216 int stride
= rsrc
->slices
[level
].stride
;
218 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
);
220 rt
->format
= panfrost_mfbd_format(surf
);
222 /* Now, we set the layout specific pieces */
224 if (rsrc
->layout
== PAN_LINEAR
) {
225 rt
->format
.block
= MALI_MFBD_BLOCK_LINEAR
;
226 rt
->framebuffer
= base
;
227 rt
->framebuffer_stride
= stride
/ 16;
228 } else if (rsrc
->layout
== PAN_TILED
) {
229 rt
->format
.block
= MALI_MFBD_BLOCK_TILED
;
230 rt
->framebuffer
= base
;
231 rt
->framebuffer_stride
= stride
;
232 } else if (rsrc
->layout
== PAN_AFBC
) {
233 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
235 unsigned header_size
= rsrc
->slices
[level
].header_size
;
237 rt
->framebuffer
= base
+ header_size
;
238 rt
->afbc
.metadata
= base
;
240 rt
->afbc
.unk
= 0x30009;
242 /* TODO: Investigate shift */
243 rt
->framebuffer_stride
= stride
<< 1;
245 fprintf(stderr
, "Invalid render layout (cbuf)");
251 panfrost_mfbd_set_zsbuf(
252 struct bifrost_framebuffer
*fb
,
253 struct bifrost_fb_extra
*fbx
,
254 struct pipe_surface
*surf
)
256 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
258 unsigned level
= surf
->u
.tex
.level
;
259 assert(surf
->u
.tex
.first_layer
== 0);
261 unsigned offset
= rsrc
->slices
[level
].offset
;
263 if (rsrc
->layout
== PAN_AFBC
) {
264 mali_ptr base
= rsrc
->bo
->gpu
+ offset
;
265 unsigned header_size
= rsrc
->slices
[level
].header_size
;
267 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
276 fbx
->ds_afbc
.depth_stencil
= base
+ header_size
;
277 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= base
;
278 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
280 fbx
->ds_afbc
.zero1
= 0x10009;
281 fbx
->ds_afbc
.padding
= 0x1000;
282 } else if (rsrc
->layout
== PAN_LINEAR
) {
283 int stride
= rsrc
->slices
[level
].stride
;
284 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
286 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
| 0x1;
288 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
289 fbx
->ds_linear
.depth_stride
= stride
;
295 /* Helper for sequential uploads used for MFBD */
297 #define UPLOAD(dest, offset, src, max) { \
298 size_t sz = sizeof(*src); \
299 memcpy(dest.cpu + offset, src, sz); \
300 assert((offset + sz) <= max); \
305 panfrost_mfbd_upload(
306 struct panfrost_context
*ctx
,
307 struct bifrost_framebuffer
*fb
,
308 struct bifrost_fb_extra
*fbx
,
309 struct bifrost_render_target
*rts
,
314 /* There may be extra data stuck in the middle */
315 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
317 /* Compute total size for transfer */
320 sizeof(struct bifrost_framebuffer
) +
321 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
322 sizeof(struct bifrost_render_target
) * cbufs
;
324 struct panfrost_transfer m_f_trans
=
325 panfrost_allocate_transient(ctx
, total_sz
);
327 /* Do the transfer */
329 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
332 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
334 for (unsigned c
= 0; c
< cbufs
; ++c
) {
335 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
338 /* Return pointer suitable for the fragment section */
339 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
344 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
347 panfrost_mfbd_fragment(struct panfrost_context
*ctx
, bool has_draws
)
349 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
351 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
, has_draws
);
352 struct bifrost_fb_extra fbx
= {};
353 struct bifrost_render_target rts
[4] = {};
357 fb
.mfbd_flags
= 0x100;
359 /* TODO: MRT clear */
360 panfrost_mfbd_clear(job
, &fb
, &fbx
, rts
, fb
.rt_count_2
);
362 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
363 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
364 unsigned bpp
= util_format_get_blocksize(surf
->format
);
366 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
368 /* What is this? Looks like some extension of the bpp field.
369 * Maybe it establishes how much internal tilebuffer space is
371 fb
.rt_count_2
= MAX2(fb
.rt_count_2
, ALIGN_POT(bpp
, 4) / 4);
374 if (ctx
->pipe_framebuffer
.zsbuf
) {
375 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, ctx
->pipe_framebuffer
.zsbuf
);
378 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
380 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
381 struct mali_rt_format null_rt
= {
386 rts
[0].format
= null_rt
;
387 rts
[0].framebuffer
= 0;
388 rts
[0].framebuffer_stride
= 0;
391 /* When scanning out, the depth buffer is immediately invalidated, so
392 * we don't need to waste bandwidth writing it out. This can improve
393 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
394 * memory bandwidth!).
396 * The exception is ReadPixels, but this is not supported on GLES so we
397 * can safely ignore it. */
399 if (panfrost_is_scanout(ctx
)) {
400 job
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
403 /* Actualize the requirements */
405 if (job
->requirements
& PAN_REQ_MSAA
) {
406 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
409 fb
.unk1
|= (1 << 4) | (1 << 1);
413 if (job
->requirements
& PAN_REQ_DEPTH_WRITE
)
414 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
416 /* Checksumming only works with a single render target */
418 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
419 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
420 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
421 struct panfrost_bo
*bo
= rsrc
->bo
;
423 if (rsrc
->checksummed
) {
424 unsigned level
= surf
->u
.tex
.level
;
425 struct panfrost_slice
*slice
= &rsrc
->slices
[level
];
427 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
428 fbx
.flags
|= MALI_EXTRA_PRESENT
;
429 fbx
.checksum_stride
= slice
->checksum_stride
;
430 fbx
.checksum
= bo
->gpu
+ slice
->checksum_offset
;
434 /* We always upload at least one (dummy) cbuf */
435 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
437 return panfrost_mfbd_upload(ctx
, &fb
, &fbx
, rts
, cbufs
);