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
) {
115 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
117 mali_ptr base
= rsrc
->bo
->gpu
+ offset
;
118 unsigned header_size
= rsrc
->bo
->slices
[level
].header_size
;
120 rt
->framebuffer
= base
+ header_size
;
121 rt
->afbc
.metadata
= base
;
123 rt
->afbc
.unk
= 0x30009;
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
) {
147 mali_ptr base
= rsrc
->bo
->gpu
+ offset
;
148 unsigned header_size
= rsrc
->bo
->slices
[level
].header_size
;
150 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
159 fbx
->ds_afbc
.depth_stencil
= base
+ header_size
;
160 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= base
;
161 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
163 fbx
->ds_afbc
.zero1
= 0x10009;
164 fbx
->ds_afbc
.padding
= 0x1000;
165 } else if (rsrc
->bo
->layout
== PAN_LINEAR
) {
166 int stride
= rsrc
->bo
->slices
[level
].stride
;
167 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
169 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
| 0x1;
171 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
172 fbx
->ds_linear
.depth_stride
= stride
;
178 /* Helper for sequential uploads used for MFBD */
180 #define UPLOAD(dest, offset, src, max) { \
181 size_t sz = sizeof(*src); \
182 memcpy(dest.cpu + offset, src, sz); \
183 assert((offset + sz) <= max); \
188 panfrost_mfbd_upload(
189 struct panfrost_context
*ctx
,
190 struct bifrost_framebuffer
*fb
,
191 struct bifrost_fb_extra
*fbx
,
192 struct bifrost_render_target
*rts
,
197 /* There may be extra data stuck in the middle */
198 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
200 /* Compute total size for transfer */
203 sizeof(struct bifrost_framebuffer
) +
204 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
205 sizeof(struct bifrost_render_target
) * cbufs
;
207 struct panfrost_transfer m_f_trans
=
208 panfrost_allocate_transient(ctx
, total_sz
);
210 /* Do the transfer */
212 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
215 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
217 for (unsigned c
= 0; c
< cbufs
; ++c
) {
218 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
221 /* Return pointer suitable for the fragment section */
222 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
227 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
230 panfrost_mfbd_fragment(struct panfrost_context
*ctx
, bool has_draws
)
232 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
234 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
, has_draws
);
235 struct bifrost_fb_extra fbx
= {};
236 struct bifrost_render_target rts
[4] = {};
240 fb
.mfbd_flags
= 0x100;
242 /* TODO: MRT clear */
243 panfrost_mfbd_clear(job
, &fb
, &fbx
, &rts
[0]);
245 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
246 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
247 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
250 if (ctx
->pipe_framebuffer
.zsbuf
) {
251 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, ctx
->pipe_framebuffer
.zsbuf
);
254 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
256 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
257 struct mali_rt_format null_rt
= {
262 rts
[0].format
= null_rt
;
263 rts
[0].framebuffer
= 0;
264 rts
[0].framebuffer_stride
= 0;
267 /* When scanning out, the depth buffer is immediately invalidated, so
268 * we don't need to waste bandwidth writing it out. This can improve
269 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
270 * memory bandwidth!).
272 * The exception is ReadPixels, but this is not supported on GLES so we
273 * can safely ignore it. */
275 if (panfrost_is_scanout(ctx
)) {
276 job
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
279 /* Actualize the requirements */
281 if (job
->requirements
& PAN_REQ_MSAA
) {
282 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
285 fb
.unk1
|= (1 << 4) | (1 << 1);
289 if (job
->requirements
& PAN_REQ_DEPTH_WRITE
)
290 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
292 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
293 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[0]->texture
;
295 if (rsrc
->bo
->has_checksum
) {
296 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
297 fbx
.flags
|= MALI_EXTRA_PRESENT
;
298 fbx
.checksum_stride
= rsrc
->bo
->checksum_stride
;
299 fbx
.checksum
= rsrc
->bo
->gpu
+ rsrc
->bo
->slices
[0].stride
* rsrc
->base
.height0
;
303 /* We always upload at least one (dummy) cbuf */
304 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
306 return panfrost_mfbd_upload(ctx
, &fb
, &fbx
, rts
, cbufs
);