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 8-bit UNORM */
41 struct mali_rt_format fmt
= {
44 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
47 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
51 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
)
52 fmt
.flags
|= MALI_MFBD_FORMAT_SRGB
;
54 /* Set flags for alternative formats */
56 if (surf
->texture
->format
== PIPE_FORMAT_B5G6R5_UNORM
) {
57 fmt
.unk1
= 0x14000000;
58 fmt
.nr_channels
= MALI_POSITIVE(2);
68 struct panfrost_job
*job
,
69 struct bifrost_framebuffer
*fb
,
70 struct bifrost_fb_extra
*fbx
,
71 struct bifrost_render_target
*rt
)
73 if (job
->clear
& PIPE_CLEAR_COLOR
) {
74 rt
->clear_color_1
= job
->clear_color
;
75 rt
->clear_color_2
= job
->clear_color
;
76 rt
->clear_color_3
= job
->clear_color
;
77 rt
->clear_color_4
= job
->clear_color
;
80 if (job
->clear
& PIPE_CLEAR_DEPTH
) {
81 fb
->clear_depth
= job
->clear_depth
;
84 if (job
->clear
& PIPE_CLEAR_STENCIL
) {
85 fb
->clear_stencil
= job
->clear_stencil
;
90 panfrost_mfbd_set_cbuf(
91 struct bifrost_render_target
*rt
,
92 struct pipe_surface
*surf
)
94 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
96 unsigned level
= surf
->u
.tex
.level
;
97 assert(surf
->u
.tex
.first_layer
== 0);
99 int stride
= rsrc
->bo
->slices
[level
].stride
;
100 unsigned offset
= rsrc
->bo
->slices
[level
].offset
;
102 rt
->format
= panfrost_mfbd_format(surf
);
104 /* Now, we set the layout specific pieces */
106 if (rsrc
->bo
->layout
== PAN_LINEAR
) {
107 rt
->format
.block
= MALI_MFBD_BLOCK_LINEAR
;
108 rt
->framebuffer
= rsrc
->bo
->gpu
+ offset
;
109 rt
->framebuffer_stride
= stride
/ 16;
110 } else if (rsrc
->bo
->layout
== PAN_TILED
) {
111 rt
->format
.block
= MALI_MFBD_BLOCK_TILED
;
112 rt
->framebuffer
= rsrc
->bo
->gpu
+ offset
;
113 rt
->framebuffer_stride
= stride
;
114 } else if (rsrc
->bo
->layout
== PAN_AFBC
) {
116 rt
->afbc
.metadata
= rsrc
->bo
->afbc_slab
.gpu
;
118 rt
->afbc
.unk
= 0x30009;
120 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
122 mali_ptr afbc_main
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
123 rt
->framebuffer
= afbc_main
;
125 /* TODO: Investigate shift */
126 rt
->framebuffer_stride
= stride
<< 1;
128 fprintf(stderr
, "Invalid render layout (cbuf)");
134 panfrost_mfbd_set_zsbuf(
135 struct bifrost_framebuffer
*fb
,
136 struct bifrost_fb_extra
*fbx
,
137 struct pipe_surface
*surf
)
139 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
141 unsigned level
= surf
->u
.tex
.level
;
142 assert(surf
->u
.tex
.first_layer
== 0);
144 unsigned offset
= rsrc
->bo
->slices
[level
].offset
;
146 if (rsrc
->bo
->layout
== PAN_AFBC
) {
148 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
157 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= rsrc
->bo
->afbc_slab
.gpu
;
158 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
160 fbx
->ds_afbc
.depth_stencil
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
162 fbx
->ds_afbc
.zero1
= 0x10009;
163 fbx
->ds_afbc
.padding
= 0x1000;
164 } else if (rsrc
->bo
->layout
== PAN_LINEAR
) {
165 int stride
= rsrc
->bo
->slices
[level
].stride
;
166 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
168 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
| 0x1;
170 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
171 fbx
->ds_linear
.depth_stride
= stride
;
177 /* Helper for sequential uploads used for MFBD */
179 #define UPLOAD(dest, offset, src, max) { \
180 size_t sz = sizeof(*src); \
181 memcpy(dest.cpu + offset, src, sz); \
182 assert((offset + sz) <= max); \
187 panfrost_mfbd_upload(
188 struct panfrost_context
*ctx
,
189 struct bifrost_framebuffer
*fb
,
190 struct bifrost_fb_extra
*fbx
,
191 struct bifrost_render_target
*rts
,
196 /* There may be extra data stuck in the middle */
197 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
199 /* Compute total size for transfer */
202 sizeof(struct bifrost_framebuffer
) +
203 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
204 sizeof(struct bifrost_render_target
) * cbufs
;
206 struct panfrost_transfer m_f_trans
=
207 panfrost_allocate_transient(ctx
, total_sz
);
209 /* Do the transfer */
211 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
214 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
216 for (unsigned c
= 0; c
< cbufs
; ++c
) {
217 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
220 /* Return pointer suitable for the fragment section */
221 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
226 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
229 panfrost_mfbd_fragment(struct panfrost_context
*ctx
, bool has_draws
)
231 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
233 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
, has_draws
);
234 struct bifrost_fb_extra fbx
= {};
235 struct bifrost_render_target rts
[4] = {};
239 fb
.mfbd_flags
= 0x100;
241 /* TODO: MRT clear */
242 panfrost_mfbd_clear(job
, &fb
, &fbx
, &rts
[0]);
244 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
245 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
246 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
249 if (ctx
->pipe_framebuffer
.zsbuf
) {
250 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, ctx
->pipe_framebuffer
.zsbuf
);
253 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
255 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
256 struct mali_rt_format null_rt
= {
261 rts
[0].format
= null_rt
;
262 rts
[0].framebuffer
= 0;
263 rts
[0].framebuffer_stride
= 0;
266 /* When scanning out, the depth buffer is immediately invalidated, so
267 * we don't need to waste bandwidth writing it out. This can improve
268 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
269 * memory bandwidth!).
271 * The exception is ReadPixels, but this is not supported on GLES so we
272 * can safely ignore it. */
274 if (panfrost_is_scanout(ctx
)) {
275 job
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
278 /* Actualize the requirements */
280 if (job
->requirements
& PAN_REQ_MSAA
) {
281 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
284 fb
.unk1
|= (1 << 4) | (1 << 1);
288 if (job
->requirements
& PAN_REQ_DEPTH_WRITE
)
289 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
291 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
292 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[0]->texture
;
294 if (rsrc
->bo
->has_checksum
) {
295 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
296 fbx
.flags
|= MALI_EXTRA_PRESENT
;
297 fbx
.checksum_stride
= rsrc
->bo
->checksum_stride
;
298 fbx
.checksum
= rsrc
->bo
->gpu
+ rsrc
->bo
->slices
[0].stride
* rsrc
->base
.height0
;
302 /* We always upload at least one (dummy) cbuf */
303 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
305 return panfrost_mfbd_upload(ctx
, &fb
, &fbx
, rts
, cbufs
);