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"
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
->texture
->format
);
39 /* Fill in accordingly, defaulting to RGBA8888 (UNORM) */
41 struct mali_rt_format fmt
= {
44 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
47 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
51 /* Set flags for alternative formats */
53 if (surf
->texture
->format
== PIPE_FORMAT_B5G6R5_UNORM
) {
54 fmt
.unk1
= 0x14000000;
55 fmt
.nr_channels
= MALI_POSITIVE(2);
65 struct panfrost_job
*job
,
66 struct bifrost_framebuffer
*fb
,
67 struct bifrost_fb_extra
*fbx
,
68 struct bifrost_render_target
*rt
)
70 if (job
->clear
& PIPE_CLEAR_COLOR
) {
71 rt
->clear_color_1
= job
->clear_color
;
72 rt
->clear_color_2
= job
->clear_color
;
73 rt
->clear_color_3
= job
->clear_color
;
74 rt
->clear_color_4
= job
->clear_color
;
77 if (job
->clear
& PIPE_CLEAR_DEPTH
) {
78 fb
->clear_depth
= job
->clear_depth
;
81 if (job
->clear
& PIPE_CLEAR_STENCIL
) {
82 fb
->clear_stencil
= job
->clear_stencil
;
87 panfrost_mfbd_set_cbuf(
88 struct bifrost_render_target
*rt
,
89 struct pipe_surface
*surf
)
91 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
93 unsigned level
= surf
->u
.tex
.level
;
94 assert(surf
->u
.tex
.first_layer
== 0);
96 int stride
= rsrc
->bo
->slices
[level
].stride
;
97 unsigned offset
= rsrc
->bo
->slices
[level
].offset
;
99 rt
->format
= panfrost_mfbd_format(surf
);
101 /* Now, we set the layout specific pieces */
103 if (rsrc
->bo
->layout
== PAN_LINEAR
) {
104 rt
->format
.block
= MALI_MFBD_BLOCK_LINEAR
;
105 rt
->framebuffer
= rsrc
->bo
->gpu
+ offset
;
106 rt
->framebuffer_stride
= stride
/ 16;
107 } else if (rsrc
->bo
->layout
== PAN_AFBC
) {
109 rt
->afbc
.metadata
= rsrc
->bo
->afbc_slab
.gpu
;
111 rt
->afbc
.unk
= 0x30009;
113 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
115 mali_ptr afbc_main
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
116 rt
->framebuffer
= afbc_main
;
118 /* TODO: Investigate shift */
119 rt
->framebuffer_stride
= stride
<< 1;
121 fprintf(stderr
, "Invalid render layout (cbuf)");
127 panfrost_mfbd_set_zsbuf(
128 struct bifrost_framebuffer
*fb
,
129 struct bifrost_fb_extra
*fbx
,
130 struct pipe_surface
*surf
)
132 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
134 unsigned level
= surf
->u
.tex
.level
;
135 assert(surf
->u
.tex
.first_layer
== 0);
137 unsigned offset
= rsrc
->bo
->slices
[level
].offset
;
139 if (rsrc
->bo
->layout
== PAN_AFBC
) {
141 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
150 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= rsrc
->bo
->afbc_slab
.gpu
;
151 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
153 fbx
->ds_afbc
.depth_stencil
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
155 fbx
->ds_afbc
.zero1
= 0x10009;
156 fbx
->ds_afbc
.padding
= 0x1000;
157 } else if (rsrc
->bo
->layout
== PAN_LINEAR
) {
158 int stride
= rsrc
->bo
->slices
[level
].stride
;
159 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
161 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
| 0x1;
163 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
164 fbx
->ds_linear
.depth_stride
= stride
;
170 /* Helper for sequential uploads used for MFBD */
172 #define UPLOAD(dest, offset, src, max) { \
173 size_t sz = sizeof(*src); \
174 memcpy(dest.cpu + offset, src, sz); \
175 assert((offset + sz) <= max); \
180 panfrost_mfbd_upload(
181 struct panfrost_context
*ctx
,
182 struct bifrost_framebuffer
*fb
,
183 struct bifrost_fb_extra
*fbx
,
184 struct bifrost_render_target
*rts
,
189 /* There may be extra data stuck in the middle */
190 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
192 /* Compute total size for transfer */
195 sizeof(struct bifrost_framebuffer
) +
196 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
197 sizeof(struct bifrost_render_target
) * cbufs
;
199 struct panfrost_transfer m_f_trans
=
200 panfrost_allocate_transient(ctx
, total_sz
);
202 /* Do the transfer */
204 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
207 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
209 for (unsigned c
= 0; c
< cbufs
; ++c
) {
210 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
213 /* Return pointer suitable for the fragment section */
214 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
219 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
222 panfrost_mfbd_fragment(struct panfrost_context
*ctx
, bool has_draws
)
224 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
226 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
, has_draws
);
227 struct bifrost_fb_extra fbx
= {};
228 struct bifrost_render_target rts
[4] = {};
232 fb
.mfbd_flags
= 0x100;
234 /* TODO: MRT clear */
235 panfrost_mfbd_clear(job
, &fb
, &fbx
, &rts
[0]);
237 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
238 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
239 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
242 if (ctx
->pipe_framebuffer
.zsbuf
) {
243 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, ctx
->pipe_framebuffer
.zsbuf
);
246 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
248 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
249 struct mali_rt_format null_rt
= {
254 rts
[0].format
= null_rt
;
255 rts
[0].framebuffer
= 0;
256 rts
[0].framebuffer_stride
= 0;
259 /* When scanning out, the depth buffer is immediately invalidated, so
260 * we don't need to waste bandwidth writing it out. This can improve
261 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
262 * memory bandwidth!).
264 * The exception is ReadPixels, but this is not supported on GLES so we
265 * can safely ignore it. */
267 if (panfrost_is_scanout(ctx
)) {
268 job
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
271 /* Actualize the requirements */
273 if (job
->requirements
& PAN_REQ_MSAA
) {
274 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
277 fb
.unk1
|= (1 << 4) | (1 << 1);
281 if (job
->requirements
& PAN_REQ_DEPTH_WRITE
)
282 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
284 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
285 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[0]->texture
;
287 if (rsrc
->bo
->has_checksum
) {
288 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
289 fbx
.flags
|= MALI_EXTRA_PRESENT
;
290 fbx
.checksum_stride
= rsrc
->bo
->checksum_stride
;
291 fbx
.checksum
= rsrc
->bo
->gpu
+ rsrc
->bo
->slices
[0].stride
* rsrc
->base
.height0
;
295 /* We always upload at least one (dummy) cbuf */
296 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
298 return panfrost_mfbd_upload(ctx
, &fb
, &fbx
, rts
, cbufs
);