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 */
41 struct mali_rt_format fmt
= {
44 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
46 .swizzle
= panfrost_translate_swizzle_4(desc
->swizzle
),
54 panfrost_mfbd_enable_msaa(struct panfrost_context
*ctx
)
56 ctx
->fragment_rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
59 ctx
->fragment_mfbd
.unk1
|= (1 << 4) | (1 << 1);
60 ctx
->fragment_mfbd
.rt_count_2
= 4;
64 panfrost_mfbd_clear(struct panfrost_job
*job
)
66 struct panfrost_context
*ctx
= job
->ctx
;
67 struct bifrost_render_target
*buffer_color
= &ctx
->fragment_rts
[0];
68 struct bifrost_framebuffer
*buffer_ds
= &ctx
->fragment_mfbd
;
70 if (job
->clear
& PIPE_CLEAR_COLOR
) {
71 buffer_color
->clear_color_1
= job
->clear_color
;
72 buffer_color
->clear_color_2
= job
->clear_color
;
73 buffer_color
->clear_color_3
= job
->clear_color
;
74 buffer_color
->clear_color_4
= job
->clear_color
;
77 if (job
->clear
& PIPE_CLEAR_DEPTH
) {
78 buffer_ds
->clear_depth
= job
->clear_depth
;
81 if (job
->clear
& PIPE_CLEAR_STENCIL
) {
82 buffer_ds
->clear_stencil
= job
->clear_stencil
;
85 if (job
->clear
& (PIPE_CLEAR_DEPTH
| PIPE_CLEAR_STENCIL
)) {
86 /* Setup combined 24/8 depth/stencil */
87 ctx
->fragment_mfbd
.unk3
|= MALI_MFBD_EXTRA
;
88 ctx
->fragment_extra
.flags
= 0x405;
89 ctx
->fragment_extra
.ds_linear
.depth
= ctx
->depth_stencil_buffer
.gpu
;
90 ctx
->fragment_extra
.ds_linear
.depth_stride
= ctx
->pipe_framebuffer
.width
* 4;
95 panfrost_mfbd_set_cbuf(
96 struct panfrost_context
*ctx
,
97 struct pipe_surface
*surf
,
100 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
103 util_format_get_stride(surf
->format
, surf
->texture
->width0
);
105 ctx
->fragment_rts
[cb
].format
= panfrost_mfbd_format(surf
);
107 /* Now, we set the layout specific pieces */
109 if (rsrc
->bo
->layout
== PAN_LINEAR
) {
110 mali_ptr framebuffer
= rsrc
->bo
->gpu
[0];
112 /* The default is upside down from OpenGL's perspective. */
113 if (panfrost_is_scanout(ctx
)) {
114 framebuffer
+= stride
* (surf
->texture
->height0
- 1);
118 /* MFBD specifies stride in tiles */
119 ctx
->fragment_rts
[cb
].framebuffer
= framebuffer
;
120 ctx
->fragment_rts
[cb
].framebuffer_stride
= stride
/ 16;
121 } else if (rsrc
->bo
->layout
== PAN_AFBC
) {
122 ctx
->fragment_rts
[cb
].afbc
.metadata
= rsrc
->bo
->afbc_slab
.gpu
;
123 ctx
->fragment_rts
[cb
].afbc
.stride
= 0;
124 ctx
->fragment_rts
[cb
].afbc
.unk
= 0x30009;
126 ctx
->fragment_rts
[cb
].format
.flags
|= MALI_MFBD_FORMAT_AFBC
;
128 mali_ptr afbc_main
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
129 ctx
->fragment_rts
[cb
].framebuffer
= afbc_main
;
131 /* TODO: Investigate shift */
132 ctx
->fragment_rts
[cb
].framebuffer_stride
= stride
<< 1;
134 fprintf(stderr
, "Invalid render layout (cbuf %d)", cb
);
140 panfrost_mfbd_set_targets(struct panfrost_context
*ctx
)
142 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
143 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
144 panfrost_mfbd_set_cbuf(ctx
, surf
, cb
);
147 /* Enable depth/stencil AFBC for the framebuffer (not the render target) */
148 if (ctx
->pipe_framebuffer
.zsbuf
) {
149 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) ctx
->pipe_framebuffer
.zsbuf
->texture
;
151 if (rsrc
->bo
->layout
== PAN_AFBC
) {
152 ctx
->fragment_mfbd
.unk3
|= MALI_MFBD_EXTRA
;
154 ctx
->fragment_extra
.flags
=
161 ctx
->fragment_extra
.ds_afbc
.depth_stencil_afbc_metadata
= rsrc
->bo
->afbc_slab
.gpu
;
162 ctx
->fragment_extra
.ds_afbc
.depth_stencil_afbc_stride
= 0;
164 ctx
->fragment_extra
.ds_afbc
.depth_stencil
= rsrc
->bo
->afbc_slab
.gpu
+ rsrc
->bo
->afbc_metadata_size
;
166 ctx
->fragment_extra
.ds_afbc
.zero1
= 0x10009;
167 ctx
->fragment_extra
.ds_afbc
.padding
= 0x1000;
169 ctx
->fragment_mfbd
.unk3
|= MALI_MFBD_DEPTH_WRITE
;
173 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
175 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
176 struct mali_rt_format null_rt
= {
181 ctx
->fragment_rts
[0].format
= null_rt
;
182 ctx
->fragment_rts
[0].framebuffer
= 0;
183 ctx
->fragment_rts
[0].framebuffer_stride
= 0;
187 /* Helper for sequential uploads used for MFBD */
189 #define UPLOAD(dest, offset, src, max) { \
190 size_t sz = sizeof(src); \
191 memcpy(dest.cpu + offset, &src, sz); \
192 assert((offset + sz) <= max); \
197 panfrost_mfbd_upload(struct panfrost_context
*ctx
)
201 /* We always upload at least one (dummy) cbuf */
202 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
204 /* There may be extra data stuck in the middle */
205 bool has_extra
= ctx
->fragment_mfbd
.unk3
& MALI_MFBD_EXTRA
;
207 /* Compute total size for transfer */
210 sizeof(struct bifrost_framebuffer
) +
211 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
212 sizeof(struct bifrost_render_target
) * cbufs
;
214 struct panfrost_transfer m_f_trans
=
215 panfrost_allocate_transient(ctx
, total_sz
);
217 /* Do the transfer */
219 UPLOAD(m_f_trans
, offset
, ctx
->fragment_mfbd
, total_sz
);
222 UPLOAD(m_f_trans
, offset
, ctx
->fragment_extra
, total_sz
);
224 for (unsigned c
= 0; c
< cbufs
; ++c
) {
225 UPLOAD(m_f_trans
, offset
, ctx
->fragment_rts
[c
], total_sz
);
228 /* Return pointer suitable for the fragment seciton */
229 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
234 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
237 panfrost_mfbd_fragment(struct panfrost_context
*ctx
)
239 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
241 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
);
247 struct bifrost_render_target rt
= {};
249 memcpy(&ctx
->fragment_rts
[0], &rt
, sizeof(rt
));
250 memset(&ctx
->fragment_extra
, 0, sizeof(ctx
->fragment_extra
));
251 memcpy(&ctx
->fragment_mfbd
, &fb
, sizeof(fb
));
253 panfrost_mfbd_clear(job
);
254 panfrost_mfbd_set_targets(ctx
);
257 panfrost_mfbd_enable_msaa(ctx
);
259 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
260 struct panfrost_resource
*rsrc
= (struct panfrost_resource
*) ctx
->pipe_framebuffer
.cbufs
[0]->texture
;
262 if (rsrc
->bo
->has_checksum
) {
263 int stride
= util_format_get_stride(rsrc
->base
.format
, rsrc
->base
.width0
);
265 ctx
->fragment_mfbd
.unk3
|= MALI_MFBD_EXTRA
;
266 ctx
->fragment_extra
.flags
|= MALI_EXTRA_PRESENT
;
267 ctx
->fragment_extra
.checksum_stride
= rsrc
->bo
->checksum_stride
;
268 ctx
->fragment_extra
.checksum
= rsrc
->bo
->gpu
[0] + stride
* rsrc
->base
.height0
;
272 return panfrost_mfbd_upload(ctx
);