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_TILED
) {
108 rt
->format
.block
= MALI_MFBD_BLOCK_TILED
;
109 rt
->framebuffer
= rsrc
->bo
->gpu
+ offset
;
110 rt
->framebuffer_stride
= stride
;
111 } else if (rsrc
->bo
->layout
== PAN_AFBC
) {
113 rt
->afbc
.metadata
= rsrc
->bo
->afbc_slab
.gpu
;
115 rt
->afbc
.unk
= 0x30009;
117 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
119 mali_ptr afbc_main
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
120 rt
->framebuffer
= afbc_main
;
122 /* TODO: Investigate shift */
123 rt
->framebuffer_stride
= stride
<< 1;
125 fprintf(stderr
, "Invalid render layout (cbuf)");
131 panfrost_mfbd_set_zsbuf(
132 struct bifrost_framebuffer
*fb
,
133 struct bifrost_fb_extra
*fbx
,
134 struct pipe_surface
*surf
)
136 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
138 unsigned level
= surf
->u
.tex
.level
;
139 assert(surf
->u
.tex
.first_layer
== 0);
141 unsigned offset
= rsrc
->bo
->slices
[level
].offset
;
143 if (rsrc
->bo
->layout
== PAN_AFBC
) {
145 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
154 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= rsrc
->bo
->afbc_slab
.gpu
;
155 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
157 fbx
->ds_afbc
.depth_stencil
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
159 fbx
->ds_afbc
.zero1
= 0x10009;
160 fbx
->ds_afbc
.padding
= 0x1000;
161 } else if (rsrc
->bo
->layout
== PAN_LINEAR
) {
162 int stride
= rsrc
->bo
->slices
[level
].stride
;
163 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
165 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
| 0x1;
167 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
168 fbx
->ds_linear
.depth_stride
= stride
;
174 /* Helper for sequential uploads used for MFBD */
176 #define UPLOAD(dest, offset, src, max) { \
177 size_t sz = sizeof(*src); \
178 memcpy(dest.cpu + offset, src, sz); \
179 assert((offset + sz) <= max); \
184 panfrost_mfbd_upload(
185 struct panfrost_context
*ctx
,
186 struct bifrost_framebuffer
*fb
,
187 struct bifrost_fb_extra
*fbx
,
188 struct bifrost_render_target
*rts
,
193 /* There may be extra data stuck in the middle */
194 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
196 /* Compute total size for transfer */
199 sizeof(struct bifrost_framebuffer
) +
200 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
201 sizeof(struct bifrost_render_target
) * cbufs
;
203 struct panfrost_transfer m_f_trans
=
204 panfrost_allocate_transient(ctx
, total_sz
);
206 /* Do the transfer */
208 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
211 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
213 for (unsigned c
= 0; c
< cbufs
; ++c
) {
214 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
217 /* Return pointer suitable for the fragment section */
218 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
223 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
226 panfrost_mfbd_fragment(struct panfrost_context
*ctx
, bool has_draws
)
228 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
230 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
, has_draws
);
231 struct bifrost_fb_extra fbx
= {};
232 struct bifrost_render_target rts
[4] = {};
236 fb
.mfbd_flags
= 0x100;
238 /* TODO: MRT clear */
239 panfrost_mfbd_clear(job
, &fb
, &fbx
, &rts
[0]);
241 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
242 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
243 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
246 if (ctx
->pipe_framebuffer
.zsbuf
) {
247 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, ctx
->pipe_framebuffer
.zsbuf
);
250 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
252 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
253 struct mali_rt_format null_rt
= {
258 rts
[0].format
= null_rt
;
259 rts
[0].framebuffer
= 0;
260 rts
[0].framebuffer_stride
= 0;
263 /* When scanning out, the depth buffer is immediately invalidated, so
264 * we don't need to waste bandwidth writing it out. This can improve
265 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
266 * memory bandwidth!).
268 * The exception is ReadPixels, but this is not supported on GLES so we
269 * can safely ignore it. */
271 if (panfrost_is_scanout(ctx
)) {
272 job
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
275 /* Actualize the requirements */
277 if (job
->requirements
& PAN_REQ_MSAA
) {
278 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
281 fb
.unk1
|= (1 << 4) | (1 << 1);
285 if (job
->requirements
& PAN_REQ_DEPTH_WRITE
)
286 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
288 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
289 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[0]->texture
;
291 if (rsrc
->bo
->has_checksum
) {
292 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
293 fbx
.flags
|= MALI_EXTRA_PRESENT
;
294 fbx
.checksum_stride
= rsrc
->bo
->checksum_stride
;
295 fbx
.checksum
= rsrc
->bo
->gpu
+ rsrc
->bo
->slices
[0].stride
* rsrc
->base
.height0
;
299 /* We always upload at least one (dummy) cbuf */
300 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
302 return panfrost_mfbd_upload(ctx
, &fb
, &fbx
, rts
, cbufs
);