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"
32 panfrost_invert_swizzle(const unsigned char *in
, unsigned char *out
)
34 /* First, default to all zeroes to prevent uninitialized junk */
36 for (unsigned c
= 0; c
< 4; ++c
)
37 out
[c
] = PIPE_SWIZZLE_0
;
39 /* Now "do" what the swizzle says */
41 for (unsigned c
= 0; c
< 4; ++c
) {
42 unsigned char i
= in
[c
];
45 if (i
< PIPE_SWIZZLE_X
|| i
> PIPE_SWIZZLE_W
)
49 unsigned idx
= i
- PIPE_SWIZZLE_X
;
50 out
[idx
] = PIPE_SWIZZLE_X
+ c
;
54 static struct mali_rt_format
55 panfrost_mfbd_format(struct pipe_surface
*surf
)
57 /* Explode details on the format */
59 const struct util_format_description
*desc
=
60 util_format_description(surf
->format
);
62 /* The swizzle for rendering is inverted from texturing */
64 unsigned char swizzle
[4];
65 panfrost_invert_swizzle(desc
->swizzle
, swizzle
);
67 /* Fill in accordingly, defaulting to 8-bit UNORM */
69 struct mali_rt_format fmt
= {
72 .nr_channels
= MALI_POSITIVE(desc
->nr_channels
),
75 .swizzle
= panfrost_translate_swizzle_4(swizzle
),
79 if (desc
->colorspace
== UTIL_FORMAT_COLORSPACE_SRGB
)
80 fmt
.flags
|= MALI_MFBD_FORMAT_SRGB
;
82 /* Set flags for alternative formats */
85 surf
->format
== PIPE_FORMAT_R16_FLOAT
;
88 surf
->format
== PIPE_FORMAT_R11G11B10_FLOAT
||
89 surf
->format
== PIPE_FORMAT_R32_FLOAT
||
90 surf
->format
== PIPE_FORMAT_R16G16_FLOAT
;
93 surf
->format
== PIPE_FORMAT_R32G32_FLOAT
||
94 surf
->format
== PIPE_FORMAT_R16G16B16A16_FLOAT
;
97 surf
->format
== PIPE_FORMAT_R32G32B32A32_FLOAT
;
99 if (surf
->format
== PIPE_FORMAT_B5G6R5_UNORM
) {
100 fmt
.unk1
= 0x14000000;
101 fmt
.nr_channels
= MALI_POSITIVE(2);
103 } else if (float_32
) {
104 fmt
.unk1
= 0x88000000;
106 fmt
.nr_channels
= MALI_POSITIVE(4);
107 } else if (float_16
) {
108 fmt
.unk1
= 0x84000000;
110 fmt
.nr_channels
= MALI_POSITIVE(2);
111 } else if (float_64
) {
112 fmt
.unk1
= 0x8c000000;
114 fmt
.nr_channels
= MALI_POSITIVE(2);
115 } else if (float_128
) {
116 fmt
.unk1
= 0x90000000;
118 fmt
.nr_channels
= MALI_POSITIVE(4);
127 struct panfrost_job
*job
,
128 struct bifrost_framebuffer
*fb
,
129 struct bifrost_fb_extra
*fbx
,
130 struct bifrost_render_target
*rts
,
133 for (unsigned i
= 0; i
< rt_count
; ++i
) {
134 if (!(job
->clear
& (PIPE_CLEAR_COLOR0
<< i
)))
137 rts
[i
].clear_color_1
= job
->clear_color
[i
][0];
138 rts
[i
].clear_color_2
= job
->clear_color
[i
][1];
139 rts
[i
].clear_color_3
= job
->clear_color
[i
][2];
140 rts
[i
].clear_color_4
= job
->clear_color
[i
][3];
143 if (job
->clear
& PIPE_CLEAR_DEPTH
) {
144 fb
->clear_depth
= job
->clear_depth
;
147 if (job
->clear
& PIPE_CLEAR_STENCIL
) {
148 fb
->clear_stencil
= job
->clear_stencil
;
153 panfrost_mfbd_set_cbuf(
154 struct bifrost_render_target
*rt
,
155 struct pipe_surface
*surf
)
157 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
159 unsigned level
= surf
->u
.tex
.level
;
160 unsigned first_layer
= surf
->u
.tex
.first_layer
;
161 assert(surf
->u
.tex
.last_layer
== first_layer
);
162 int stride
= rsrc
->slices
[level
].stride
;
164 mali_ptr base
= panfrost_get_texture_address(rsrc
, level
, first_layer
);
166 rt
->format
= panfrost_mfbd_format(surf
);
168 /* Now, we set the layout specific pieces */
170 if (rsrc
->layout
== PAN_LINEAR
) {
171 rt
->format
.block
= MALI_MFBD_BLOCK_LINEAR
;
172 rt
->framebuffer
= base
;
173 rt
->framebuffer_stride
= stride
/ 16;
174 } else if (rsrc
->layout
== PAN_TILED
) {
175 rt
->format
.block
= MALI_MFBD_BLOCK_TILED
;
176 rt
->framebuffer
= base
;
177 rt
->framebuffer_stride
= stride
;
178 } else if (rsrc
->layout
== PAN_AFBC
) {
179 rt
->format
.block
= MALI_MFBD_BLOCK_AFBC
;
181 unsigned header_size
= rsrc
->slices
[level
].header_size
;
183 rt
->framebuffer
= base
+ header_size
;
184 rt
->afbc
.metadata
= base
;
186 rt
->afbc
.unk
= 0x30009;
188 /* TODO: Investigate shift */
189 rt
->framebuffer_stride
= stride
<< 1;
191 fprintf(stderr
, "Invalid render layout (cbuf)");
197 panfrost_mfbd_set_zsbuf(
198 struct bifrost_framebuffer
*fb
,
199 struct bifrost_fb_extra
*fbx
,
200 struct pipe_surface
*surf
)
202 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
204 unsigned level
= surf
->u
.tex
.level
;
205 assert(surf
->u
.tex
.first_layer
== 0);
207 unsigned offset
= rsrc
->slices
[level
].offset
;
209 if (rsrc
->layout
== PAN_AFBC
) {
210 mali_ptr base
= rsrc
->bo
->gpu
+ offset
;
211 unsigned header_size
= rsrc
->slices
[level
].header_size
;
213 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
222 fbx
->ds_afbc
.depth_stencil
= base
+ header_size
;
223 fbx
->ds_afbc
.depth_stencil_afbc_metadata
= base
;
224 fbx
->ds_afbc
.depth_stencil_afbc_stride
= 0;
226 fbx
->ds_afbc
.zero1
= 0x10009;
227 fbx
->ds_afbc
.padding
= 0x1000;
228 } else if (rsrc
->layout
== PAN_LINEAR
) {
229 int stride
= rsrc
->slices
[level
].stride
;
230 fb
->mfbd_flags
|= MALI_MFBD_EXTRA
;
232 fbx
->flags
|= MALI_EXTRA_PRESENT
| MALI_EXTRA_ZS
| 0x1;
234 fbx
->ds_linear
.depth
= rsrc
->bo
->gpu
+ offset
;
235 fbx
->ds_linear
.depth_stride
= stride
;
241 /* Helper for sequential uploads used for MFBD */
243 #define UPLOAD(dest, offset, src, max) { \
244 size_t sz = sizeof(*src); \
245 memcpy(dest.cpu + offset, src, sz); \
246 assert((offset + sz) <= max); \
251 panfrost_mfbd_upload(
252 struct panfrost_context
*ctx
,
253 struct bifrost_framebuffer
*fb
,
254 struct bifrost_fb_extra
*fbx
,
255 struct bifrost_render_target
*rts
,
260 /* There may be extra data stuck in the middle */
261 bool has_extra
= fb
->mfbd_flags
& MALI_MFBD_EXTRA
;
263 /* Compute total size for transfer */
266 sizeof(struct bifrost_framebuffer
) +
267 (has_extra
? sizeof(struct bifrost_fb_extra
) : 0) +
268 sizeof(struct bifrost_render_target
) * cbufs
;
270 struct panfrost_transfer m_f_trans
=
271 panfrost_allocate_transient(ctx
, total_sz
);
273 /* Do the transfer */
275 UPLOAD(m_f_trans
, offset
, fb
, total_sz
);
278 UPLOAD(m_f_trans
, offset
, fbx
, total_sz
);
280 for (unsigned c
= 0; c
< cbufs
; ++c
) {
281 UPLOAD(m_f_trans
, offset
, &rts
[c
], total_sz
);
284 /* Return pointer suitable for the fragment section */
285 return m_f_trans
.gpu
| MALI_MFBD
| (has_extra
? 2 : 0);
290 /* Creates an MFBD for the FRAGMENT section of the bound framebuffer */
293 panfrost_mfbd_fragment(struct panfrost_context
*ctx
, bool has_draws
)
295 struct panfrost_job
*job
= panfrost_get_job_for_fbo(ctx
);
297 struct bifrost_framebuffer fb
= panfrost_emit_mfbd(ctx
, has_draws
);
298 struct bifrost_fb_extra fbx
= {};
299 struct bifrost_render_target rts
[4] = {};
303 fb
.mfbd_flags
= 0x100;
305 /* TODO: MRT clear */
306 panfrost_mfbd_clear(job
, &fb
, &fbx
, rts
, fb
.rt_count_2
);
308 for (int cb
= 0; cb
< ctx
->pipe_framebuffer
.nr_cbufs
; ++cb
) {
309 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[cb
];
310 unsigned bpp
= util_format_get_blocksize(surf
->format
);
312 panfrost_mfbd_set_cbuf(&rts
[cb
], surf
);
314 /* What is this? Looks like some extension of the bpp field.
315 * Maybe it establishes how much internal tilebuffer space is
317 fb
.rt_count_2
= MAX2(fb
.rt_count_2
, ALIGN_POT(bpp
, 4) / 4);
320 if (ctx
->pipe_framebuffer
.zsbuf
) {
321 panfrost_mfbd_set_zsbuf(&fb
, &fbx
, ctx
->pipe_framebuffer
.zsbuf
);
324 /* For the special case of a depth-only FBO, we need to attach a dummy render target */
326 if (ctx
->pipe_framebuffer
.nr_cbufs
== 0) {
327 struct mali_rt_format null_rt
= {
332 rts
[0].format
= null_rt
;
333 rts
[0].framebuffer
= 0;
334 rts
[0].framebuffer_stride
= 0;
337 /* When scanning out, the depth buffer is immediately invalidated, so
338 * we don't need to waste bandwidth writing it out. This can improve
339 * performance substantially (Z32_UNORM 1080p @ 60fps is 475 MB/s of
340 * memory bandwidth!).
342 * The exception is ReadPixels, but this is not supported on GLES so we
343 * can safely ignore it. */
345 if (panfrost_is_scanout(ctx
)) {
346 job
->requirements
&= ~PAN_REQ_DEPTH_WRITE
;
349 /* Actualize the requirements */
351 if (job
->requirements
& PAN_REQ_MSAA
) {
352 rts
[0].format
.flags
|= MALI_MFBD_FORMAT_MSAA
;
355 fb
.unk1
|= (1 << 4) | (1 << 1);
359 if (job
->requirements
& PAN_REQ_DEPTH_WRITE
)
360 fb
.mfbd_flags
|= MALI_MFBD_DEPTH_WRITE
;
362 /* Checksumming only works with a single render target */
364 if (ctx
->pipe_framebuffer
.nr_cbufs
== 1) {
365 struct pipe_surface
*surf
= ctx
->pipe_framebuffer
.cbufs
[0];
366 struct panfrost_resource
*rsrc
= pan_resource(surf
->texture
);
367 struct panfrost_bo
*bo
= rsrc
->bo
;
369 if (rsrc
->checksummed
) {
370 unsigned level
= surf
->u
.tex
.level
;
371 struct panfrost_slice
*slice
= &rsrc
->slices
[level
];
373 fb
.mfbd_flags
|= MALI_MFBD_EXTRA
;
374 fbx
.flags
|= MALI_EXTRA_PRESENT
;
375 fbx
.checksum_stride
= slice
->checksum_stride
;
376 fbx
.checksum
= bo
->gpu
+ slice
->checksum_offset
;
380 /* We always upload at least one (dummy) cbuf */
381 unsigned cbufs
= MAX2(ctx
->pipe_framebuffer
.nr_cbufs
, 1);
383 return panfrost_mfbd_upload(ctx
, &fb
, &fbx
, rts
, cbufs
);