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 /* Set flags for alternative formats */
84 if (surf
->format
== PIPE_FORMAT_B5G6R5_UNORM
) {
85 fmt
.unk1
= 0x14000000;
86 fmt
.nr_channels
= MALI_POSITIVE(2);
88 } else if (surf
->format
== PIPE_FORMAT_R11G11B10_FLOAT
) {
89 fmt
.unk1
= 0x88000000;
91 fmt
.nr_channels
= MALI_POSITIVE(4);
100 struct panfrost_job
*job
,
101 struct bifrost_framebuffer
*fb
,
102 struct bifrost_fb_extra
*fbx
,
103 struct bifrost_render_target
*rt
)
105 if (job
->clear
& PIPE_CLEAR_COLOR
) {
106 rt
->clear_color_1
= job
->clear_color
;
107 rt
->clear_color_2
= job
->clear_color
;
108 rt
->clear_color_3
= job
->clear_color
;
109 rt
->clear_color_4
= job
->clear_color
;
112 if (job
->clear
& PIPE_CLEAR_DEPTH
) {
113 fb
->clear_depth
= job
->clear_depth
;
116 if (job
->clear
& PIPE_CLEAR_STENCIL
) {
117 fb
->clear_stencil
= job
->clear_stencil
;
122 panfrost_mfbd_set_cbuf(
123 struct bifrost_render_target
*rt
,
124 struct pipe_surface
*surf
)
126 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
128 unsigned level
= surf
->u
.tex
.level
;
129 unsigned first_layer
= surf
->u
.tex
.first_layer
;
130 assert(surf
->u
.tex
.last_layer
== first_layer
);
131 int stride
= rsrc
->slices
[level
].stride
;
133 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
);
135 rt
->format
= panfrost_mfbd_format(surf
);
137 /* Now, we set the layout specific pieces */
139 if (rsrc
->layout
== PAN_LINEAR
) {
140 rt
->format
.block
= MALI_MFBD_BLOCK_LINEAR
;
141 rt
->framebuffer
= base
;
142 rt
->framebuffer_stride
= stride
/ 16;
143 } else if (rsrc
->layout
== PAN_TILED
) {
144 rt
->format
.block
= MALI_MFBD_BLOCK_TILED
;
145 rt
->framebuffer
= base
;
146 rt
->framebuffer_stride
= stride
;
147 } else if (rsrc
->layout
== PAN_AFBC
) {
148 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
150 unsigned header_size
= rsrc
->slices
[level
].header_size
;
152 rt
->framebuffer
= base
+ header_size
;
153 rt
->afbc
.metadata
= base
;
155 rt
->afbc
.unk
= 0x30009;
157 /* TODO: Investigate shift */
158 rt
->framebuffer_stride
= stride
<< 1;
160 fprintf(stderr
, "Invalid render layout (cbuf)");
166 panfrost_mfbd_set_zsbuf(
167 struct bifrost_framebuffer
*fb
,
168 struct bifrost_fb_extra
*fbx
,
169 struct pipe_surface
*surf
)
171 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
173 unsigned level
= surf
->u
.tex
.level
;
174 assert(surf
->u
.tex
.first_layer
== 0);
176 unsigned offset
= rsrc
->slices
[level
].offset
;
178 if (rsrc
->layout
== PAN_AFBC
) {
179 mali_ptr base
= rsrc
->bo
->gpu
+ offset
;
180 unsigned header_size
= rsrc
->slices
[level
].header_size
;
182 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
191 fbx
->ds_afbc
.depth_stencil
= base
+ header_size
;
192 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= base
;
193 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
195 fbx
->ds_afbc
.zero1
= 0x10009;
196 fbx
->ds_afbc
.padding
= 0x1000;
197 } else if (rsrc
->layout
== PAN_LINEAR
) {
198 int stride
= rsrc
->slices
[level
].stride
;
199 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
201 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
| 0x1;
203 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
204 fbx
->ds_linear
.depth_stride
= stride
;
210 /* Helper for sequential uploads used for MFBD */
212 #define UPLOAD(dest, offset, src, max) { \
213 size_t sz = sizeof(*src); \
214 memcpy(dest.cpu + offset, src, sz); \
215 assert((offset + sz) <= max); \
220 panfrost_mfbd_upload(
221 struct panfrost_context
*ctx
,
222 struct bifrost_framebuffer
*fb
,
223 struct bifrost_fb_extra
*fbx
,
224 struct bifrost_render_target
*rts
,
229 /* There may be extra data stuck in the middle */
230 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
232 /* Compute total size for transfer */
235 sizeof(struct bifrost_framebuffer
) +
236 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
237 sizeof(struct bifrost_render_target
) * cbufs
;
239 struct panfrost_transfer m_f_trans
=
240 panfrost_allocate_transient(ctx
, total_sz
);
242 /* Do the transfer */
244 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
247 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
249 for (unsigned c
= 0; c
< cbufs
; ++c
) {
250 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
253 /* Return pointer suitable for the fragment section */
254 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
259 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
262 panfrost_mfbd_fragment(struct panfrost_context
*ctx
, bool has_draws
)
264 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
266 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
, has_draws
);
267 struct bifrost_fb_extra fbx
= {};
268 struct bifrost_render_target rts
[4] = {};
272 fb
.mfbd_flags
= 0x100;
274 /* TODO: MRT clear */
275 panfrost_mfbd_clear(job
, &fb
, &fbx
, &rts
[0]);
277 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
278 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
279 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
282 if (ctx
->pipe_framebuffer
.zsbuf
) {
283 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, ctx
->pipe_framebuffer
.zsbuf
);
286 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
288 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
289 struct mali_rt_format null_rt
= {
294 rts
[0].format
= null_rt
;
295 rts
[0].framebuffer
= 0;
296 rts
[0].framebuffer_stride
= 0;
299 /* When scanning out, the depth buffer is immediately invalidated, so
300 * we don't need to waste bandwidth writing it out. This can improve
301 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
302 * memory bandwidth!).
304 * The exception is ReadPixels, but this is not supported on GLES so we
305 * can safely ignore it. */
307 if (panfrost_is_scanout(ctx
)) {
308 job
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
311 /* Actualize the requirements */
313 if (job
->requirements
& PAN_REQ_MSAA
) {
314 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
317 fb
.unk1
|= (1 << 4) | (1 << 1);
321 if (job
->requirements
& PAN_REQ_DEPTH_WRITE
)
322 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
324 /* Checksumming only works with a single render target */
326 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
327 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
328 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
329 struct panfrost_bo
*bo
= rsrc
->bo
;
331 if (rsrc
->checksummed
) {
332 unsigned level
= surf
->u
.tex
.level
;
333 struct panfrost_slice
*slice
= &rsrc
->slices
[level
];
335 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
336 fbx
.flags
|= MALI_EXTRA_PRESENT
;
337 fbx
.checksum_stride
= slice
->checksum_stride
;
338 fbx
.checksum
= bo
->gpu
+ slice
->checksum_offset
;
342 /* We always upload at least one (dummy) cbuf */
343 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
345 return panfrost_mfbd_upload(ctx
, &fb
, &fbx
, rts
, cbufs
);