| 1 |
/* |
| 2 |
* Copyright (C) 2026 Collabora, Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
#include "pan_fb.h" |
| 6 |
|
| 7 |
#include "pan_afbc.h" |
| 8 |
#include "pan_afrc.h" |
| 9 |
#include "pan_desc.h" |
| 10 |
#include "pan_encoder.h" |
| 11 |
#include "pan_format.h" |
| 12 |
#include "pan_image.h" |
| 13 |
#include "pan_props.h" |
| 14 |
#include "pan_util.h" |
| 15 |
|
| 16 |
static unsigned |
| 17 |
pan_bytes_per_pixel_tib(enum pipe_format format) |
| 18 |
{ |
| 19 |
const struct pan_blendable_format *bf = |
| 20 |
GENX(pan_blendable_format_from_pipe_format)(format); |
| 21 |
return pan_format_tib_size(format, bf->internal); |
| 22 |
} |
| 23 |
|
| 24 |
void |
| 25 |
GENX(pan_select_fb_tile_size)(struct pan_fb_layout *fb) |
| 26 |
{ |
| 27 |
uint32_t rt_B_per_sa = 0; |
| 28 |
for (unsigned rt = 0; rt < fb->rt_count; rt++) { |
| 29 |
enum pipe_format format = fb->rt_formats[rt]; |
| 30 |
if (format == PIPE_FORMAT_NONE) |
| 31 |
continue; |
| 32 |
|
| 33 |
const struct pan_blendable_format *bf = |
| 34 |
GENX(pan_blendable_format_from_pipe_format)(format); |
| 35 |
rt_B_per_sa += pan_format_tib_size(format, bf->internal); |
| 36 |
} |
| 37 |
|
| 38 |
/* The PLS area overlaps with the color targets */ |
| 39 |
rt_B_per_sa = MAX2(rt_B_per_sa, fb->pls_size_B); |
| 40 |
|
| 41 |
const uint32_t rt_B_per_px = rt_B_per_sa * fb->sample_count; |
| 42 |
|
| 43 |
/* We always have depth and it's is always stored in a 32-bit float. |
| 44 |
* Stencil requires depth to be allocated, but doesn't have it's own |
| 45 |
* budget; it's tied to the depth buffer. |
| 46 |
*/ |
| 47 |
const uint32_t z_B_per_px = sizeof(float) * fb->sample_count; |
| 48 |
|
| 49 |
fb->tile_size_px = |
| 50 |
MIN2(fb->tile_rt_budget_B >> util_logbase2_ceil(rt_B_per_px), |
| 51 |
fb->tile_z_budget_B >> util_logbase2_ceil(z_B_per_px)); |
| 52 |
|
| 53 |
/* Check if we're using too much tile-memory; if we are, try disabling |
| 54 |
* pipelining. This works because we're starting with an optimistic half |
| 55 |
* of the tile-budget, so we actually have another half that can be used. |
| 56 |
* |
| 57 |
* On v6 GPUs, doing this is not allowed; they *have* to pipeline. |
| 58 |
*/ |
| 59 |
if (PAN_ARCH != 6 && fb->tile_size_px < 4 * 4) |
| 60 |
fb->tile_size_px *= 2; |
| 61 |
|
| 62 |
/* Clamp tile size to hardware limits */ |
| 63 |
fb->tile_size_px = |
| 64 |
MIN2(fb->tile_size_px, pan_max_effective_tile_size(PAN_ARCH)); |
| 65 |
assert(util_is_power_of_two_nonzero(fb->tile_size_px)); |
| 66 |
assert(fb->tile_size_px >= 4 * 4); |
| 67 |
|
| 68 |
/* Colour buffer allocations must be 1K aligned. */ |
| 69 |
fb->tile_rt_alloc_B = ALIGN_POT(rt_B_per_px * fb->tile_size_px, 1024); |
| 70 |
|
| 71 |
#if PAN_ARCH == 6 |
| 72 |
assert(fb->tile_rt_alloc_B <= fb->tile_rt_budget_B && "tile too big"); |
| 73 |
#else |
| 74 |
assert(fb->tile_rt_alloc_B <= fb->tile_rt_budget_B * 2 && "tile too big"); |
| 75 |
#endif |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns true if there's enough space in the tile buffer for at least two |
| 80 |
* Z/S tiles. |
| 81 |
*/ |
| 82 |
static inline bool |
| 83 |
pan_fb_can_pipeline_zs(const struct pan_fb_layout *fb) |
| 84 |
{ |
| 85 |
const uint32_t z_B_per_px = sizeof(float) * fb->sample_count; |
| 86 |
const uint32_t z_B_per_tile = z_B_per_px * fb->tile_size_px; |
| 87 |
|
| 88 |
/* The budget is already half the available Z space */ |
| 89 |
return z_B_per_tile < fb->tile_z_budget_B; |
| 90 |
} |
| 91 |
|
| 92 |
static void |
| 93 |
align_fb_tiling_area_for_image_plane(struct pan_fb_layout *fb, |
| 94 |
struct pan_image_plane_ref pref) |
| 95 |
{ |
| 96 |
if (!pref.image) |
| 97 |
return; |
| 98 |
|
| 99 |
struct pan_image_block_size block_size; |
| 100 |
if (drm_is_afbc(pref.image->props.modifier)) { |
| 101 |
/* For AFBC render targets, the hardware always writes full superblocks. |
| 102 |
* In order to ensure we don't write garbage, we need to expand the |
| 103 |
* render area accordingly and load the border pixels. |
| 104 |
*/ |
| 105 |
block_size = pan_afbc_renderblock_size(pref.image->props.modifier); |
| 106 |
assert(block_size.width >= 16 && block_size.height >= 16); |
| 107 |
} else if (drm_is_afrc(pref.image->props.modifier)) { |
| 108 |
/* For AFRC render targets, the hardware always writes full clumps. In |
| 109 |
* order to ensure we don't write garbage, we need to expand the render |
| 110 |
* area accordingly and load the border pixels. |
| 111 |
*/ |
| 112 |
bool scan = pan_afrc_is_scan(pref.image->props.modifier); |
| 113 |
block_size = pan_afrc_clump_size(pref.image->props.format, scan); |
| 114 |
} else { |
| 115 |
/* No alignment requirements */ |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
fb->tiling_area_px = pan_fb_bbox_align(fb->tiling_area_px, |
| 120 |
block_size.width, |
| 121 |
block_size.height); |
| 122 |
} |
| 123 |
|
| 124 |
void |
| 125 |
GENX(pan_align_fb_tiling_area)(struct pan_fb_layout *fb, |
| 126 |
const struct pan_fb_store *store) |
| 127 |
{ |
| 128 |
if (store == NULL) |
| 129 |
return; |
| 130 |
|
| 131 |
for (unsigned rt = 0; rt < fb->rt_count; rt++) { |
| 132 |
if (store->rts[rt].store) { |
| 133 |
align_fb_tiling_area_for_image_plane(fb, |
| 134 |
pan_image_view_get_color_plane(store->rts[rt].iview)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
if (store->zs.store) { |
| 139 |
align_fb_tiling_area_for_image_plane(fb, |
| 140 |
pan_image_view_get_zs_plane(store->zs.iview)); |
| 141 |
} |
| 142 |
|
| 143 |
if (store->s.store) { |
| 144 |
align_fb_tiling_area_for_image_plane(fb, |
| 145 |
pan_image_view_get_s_plane(store->s.iview)); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
fold_sample_0_image_load(struct pan_fb_resolve_op_msaa *rm, |
| 151 |
const struct pan_image_view *iview, |
| 152 |
uint8_t fb_sample_count) |
| 153 |
{ |
| 154 |
if (rm->resolve != PAN_FB_RESOLVE_IMAGE) |
| 155 |
return; |
| 156 |
|
| 157 |
if (rm->msaa != PAN_FB_MSAA_COPY_ALL) |
| 158 |
return; |
| 159 |
|
| 160 |
assert(pan_image_view_get_nr_samples(iview) == fb_sample_count); |
| 161 |
|
| 162 |
/* If we're loading all samples of the image but only storing SAMPLE_0, |
| 163 |
* then we don't actually need all samples in the image. Drop it to a |
| 164 |
* SAMPLE_0 load to save bandwidth. |
| 165 |
*/ |
| 166 |
rm->msaa = PAN_FB_MSAA_COPY_SAMPLE_0; |
| 167 |
} |
| 168 |
|
| 169 |
static bool |
| 170 |
resolve_msaa_can_fold(struct pan_fb_resolve_op_msaa rm, |
| 171 |
enum pan_fb_resolve_op self_op, |
| 172 |
enum pipe_format format) |
| 173 |
{ |
| 174 |
if (rm.resolve == PAN_FB_RESOLVE_IMAGE) { |
| 175 |
/* We already folded away COPY_ALL and all the other MSAA modes produce |
| 176 |
* a single per-pixel result across all the samples, i.e. COPY_IDENTICAL. |
| 177 |
* We can fold that with any resolve mode. |
| 178 |
*/ |
| 179 |
assert(rm.msaa != PAN_FB_MSAA_COPY_ALL); |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
if (rm.resolve != self_op) |
| 184 |
return false; |
| 185 |
|
| 186 |
/* PAN_FB_MSAA_COPY_IDENTICAL is always foldable */ |
| 187 |
if (rm.msaa == PAN_FB_MSAA_COPY_IDENTICAL) |
| 188 |
return true; |
| 189 |
|
| 190 |
if (self_op == PAN_FB_RESOLVE_Z || self_op == PAN_FB_RESOLVE_S) { |
| 191 |
/* Z/S only supports SAMPLE_0 */ |
| 192 |
return rm.msaa == PAN_FB_MSAA_COPY_SAMPLE_0; |
| 193 |
} else { |
| 194 |
if (rm.msaa == PAN_FB_MSAA_COPY_AVERAGE && |
| 195 |
!GENX(pan_format_supports_msaa_average)(format)) |
| 196 |
return false; |
| 197 |
|
| 198 |
/* The color hardware supports SAMPLE_0 and AVERAGE */ |
| 199 |
return rm.msaa == PAN_FB_MSAA_COPY_SAMPLE_0 || |
| 200 |
rm.msaa == PAN_FB_MSAA_COPY_AVERAGE; |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
static void |
| 205 |
fold_resolve_into_store_target(enum pipe_format format, |
| 206 |
uint8_t fb_sample_count, |
| 207 |
enum pan_fb_resolve_op self_op, |
| 208 |
struct pan_fb_resolve_target *resolve, |
| 209 |
struct pan_fb_store_target *store) |
| 210 |
{ |
| 211 |
if (format == PIPE_FORMAT_NONE) |
| 212 |
return; |
| 213 |
|
| 214 |
/* We need SAMPLE_0 if we're going to fold in the resolve */ |
| 215 |
if (store->msaa != PAN_FB_MSAA_COPY_SAMPLE_0) |
| 216 |
return; |
| 217 |
|
| 218 |
fold_sample_0_image_load(&resolve->in_bounds, resolve->iview, |
| 219 |
fb_sample_count); |
| 220 |
fold_sample_0_image_load(&resolve->in_bounds, resolve->iview, |
| 221 |
fb_sample_count); |
| 222 |
|
| 223 |
if (!resolve_msaa_can_fold(resolve->in_bounds, self_op, format) || |
| 224 |
!resolve_msaa_can_fold(resolve->border, self_op, format)) |
| 225 |
return; |
| 226 |
|
| 227 |
enum pan_fb_msaa_copy_op msaa; |
| 228 |
if (resolve->in_bounds.resolve == PAN_FB_RESOLVE_IMAGE) { |
| 229 |
/* If they're both IMAGE, there's nothing to fold */ |
| 230 |
if (resolve->border.resolve == PAN_FB_RESOLVE_IMAGE) |
| 231 |
return; |
| 232 |
msaa = resolve->border.msaa; |
| 233 |
} else if (resolve->border.resolve == PAN_FB_RESOLVE_IMAGE) { |
| 234 |
msaa = resolve->in_bounds.msaa; |
| 235 |
} else { |
| 236 |
/* If neither is an image resolve, the MSAA copy op has to match or one |
| 237 |
* of them has to be PAN_FB_MSAA_COPY_IDENTICAL. |
| 238 |
*/ |
| 239 |
if (resolve->in_bounds.msaa != PAN_FB_MSAA_COPY_IDENTICAL) { |
| 240 |
if (resolve->border.msaa != PAN_FB_MSAA_COPY_IDENTICAL && |
| 241 |
resolve->border.msaa != resolve->in_bounds.msaa) |
| 242 |
return; |
| 243 |
msaa = resolve->in_bounds.msaa; |
| 244 |
} else { |
| 245 |
msaa = resolve->border.msaa; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
/* PAN_FB_MSAA_COPY_IDENTICAL isn't real. Use SAMPLE_0 */ |
| 250 |
if (msaa == PAN_FB_MSAA_COPY_IDENTICAL) |
| 251 |
msaa = PAN_FB_MSAA_COPY_SAMPLE_0; |
| 252 |
|
| 253 |
/* At this point, we know we can fold the resolve */ |
| 254 |
store->msaa = msaa; |
| 255 |
|
| 256 |
if (resolve->in_bounds.resolve != PAN_FB_RESOLVE_IMAGE) { |
| 257 |
resolve->in_bounds.resolve = PAN_FB_RESOLVE_NONE; |
| 258 |
resolve->in_bounds.msaa = PAN_FB_MSAA_COPY_ALL; |
| 259 |
} |
| 260 |
|
| 261 |
if (resolve->border.resolve != PAN_FB_RESOLVE_IMAGE) { |
| 262 |
resolve->border.resolve = PAN_FB_RESOLVE_NONE; |
| 263 |
resolve->border.msaa = PAN_FB_MSAA_COPY_ALL; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void |
| 268 |
GENX(pan_fb_fold_resolve_into_store)(const struct pan_fb_layout *fb, |
| 269 |
struct pan_fb_resolve *resolve, |
| 270 |
struct pan_fb_store *store) |
| 271 |
{ |
| 272 |
if (fb->sample_count == 1) |
| 273 |
return; |
| 274 |
|
| 275 |
fold_resolve_into_store_target(fb->z_format, fb->sample_count, |
| 276 |
PAN_FB_RESOLVE_Z, &resolve->z, &store->zs); |
| 277 |
fold_resolve_into_store_target(fb->s_format, fb->sample_count, |
| 278 |
PAN_FB_RESOLVE_S, &resolve->s, &store->s); |
| 279 |
for (unsigned rt = 0; rt < fb->rt_count; rt++) { |
| 280 |
fold_resolve_into_store_target(fb->rt_formats[rt], fb->sample_count, |
| 281 |
PAN_FB_RESOLVE_RT(rt), |
| 282 |
&resolve->rts[rt], &store->rts[rt]); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
GENX(pan_fill_fb_info)(const struct pan_fb_desc_info *info, |
| 288 |
struct pan_fb_info *fbinfo) |
| 289 |
{ |
| 290 |
struct pan_bbox bbox = { 0, 0, 0, 0 }; |
| 291 |
if (info->fb->width_px > 0 && info->fb->width_px > 0) { |
| 292 |
const struct pan_fb_bbox fb_area_px = |
| 293 |
pan_fb_bbox_from_xywh(0, 0, info->fb->width_px, info->fb->height_px); |
| 294 |
|
| 295 |
assert(pan_fb_bbox_is_valid(info->fb->tiling_area_px)); |
| 296 |
const struct pan_fb_bbox bbox_px = |
| 297 |
pan_fb_bbox_clamp(info->fb->tiling_area_px, fb_area_px); |
| 298 |
|
| 299 |
bbox = (struct pan_bbox) { |
| 300 |
.minx = bbox_px.min_x, |
| 301 |
.miny = bbox_px.min_y, |
| 302 |
.maxx = bbox_px.max_x, |
| 303 |
.maxy = bbox_px.max_y, |
| 304 |
}; |
| 305 |
} |
| 306 |
|
| 307 |
*fbinfo = (struct pan_fb_info) { |
| 308 |
.width = info->fb->width_px, |
| 309 |
.height = info->fb->height_px, |
| 310 |
.draw_extent = bbox, |
| 311 |
.frame_bounding_box = bbox, |
| 312 |
.nr_samples = info->fb->sample_count, |
| 313 |
.rt_count = info->fb->rt_count, |
| 314 |
.pls_enabled = info->fb->pls_size_B > 0, |
| 315 |
.bifrost.pre_post = { |
| 316 |
.dcds.gpu = info->frame_shaders.dcd_pointer, |
| 317 |
.modes = { |
| 318 |
info->frame_shaders.modes[0], |
| 319 |
info->frame_shaders.modes[1], |
| 320 |
info->frame_shaders.modes[2], |
| 321 |
}, |
| 322 |
}, |
| 323 |
|
| 324 |
.tile_buf_budget = info->fb->tile_rt_budget_B, |
| 325 |
.z_tile_buf_budget = info->fb->tile_z_budget_B, |
| 326 |
.tile_size = info->fb->tile_size_px, |
| 327 |
.cbuf_allocation = info->fb->tile_rt_alloc_B, |
| 328 |
|
| 329 |
.sample_positions = info->sample_pos_array_pointer, |
| 330 |
.sprite_coord_origin = info->sprite_coord_origin_max_y, |
| 331 |
.first_provoking_vertex = info->provoking_vertex_first, |
| 332 |
.allow_hsr_prepass = info->provoking_vertex_first, |
| 333 |
}; |
| 334 |
|
| 335 |
/* There are cases where we only want to fill out a partial fb_info */ |
| 336 |
if (info->load == NULL && info->store == NULL) |
| 337 |
return; |
| 338 |
|
| 339 |
for (unsigned rt = 0; rt < info->fb->rt_count; rt++) { |
| 340 |
fbinfo->rts[rt] = (struct pan_fb_color_attachment) { |
| 341 |
.view = info->store->rts[rt].iview, |
| 342 |
.clear = info->load->rts[rt].border_load == PAN_FB_LOAD_CLEAR || |
| 343 |
info->load->rts[rt].in_bounds_load == PAN_FB_LOAD_CLEAR, |
| 344 |
.preload = info->load->rts[rt].in_bounds_load == PAN_FB_LOAD_IMAGE, |
| 345 |
.discard = !info->store->rts[rt].store, |
| 346 |
}; |
| 347 |
|
| 348 |
if (fbinfo->rts[rt].clear) { |
| 349 |
pan_pack_color(GENX(pan_blendable_formats), |
| 350 |
fbinfo->rts[rt].clear_value, |
| 351 |
&info->load->rts[rt].clear.color, |
| 352 |
info->fb->rt_formats[rt], |
| 353 |
false /* dithered */); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
if (info->fb->z_format != PIPE_FORMAT_NONE) { |
| 358 |
fbinfo->zs.view.zs = info->store->zs.iview; |
| 359 |
fbinfo->zs.clear.z = info->load->z.border_load == PAN_FB_LOAD_CLEAR || |
| 360 |
info->load->z.in_bounds_load == PAN_FB_LOAD_CLEAR; |
| 361 |
fbinfo->zs.preload.z = info->load->z.in_bounds_load == PAN_FB_LOAD_IMAGE; |
| 362 |
fbinfo->zs.discard.z = !info->store->zs.store; |
| 363 |
if (fbinfo->zs.clear.z) |
| 364 |
fbinfo->zs.clear_value.depth = info->load->z.clear.depth; |
| 365 |
} |
| 366 |
|
| 367 |
if (info->fb->s_format != PIPE_FORMAT_NONE) { |
| 368 |
fbinfo->zs.view.s = info->store->s.iview; |
| 369 |
fbinfo->zs.clear.s = info->load->s.border_load == PAN_FB_LOAD_CLEAR || |
| 370 |
info->load->s.in_bounds_load == PAN_FB_LOAD_CLEAR; |
| 371 |
fbinfo->zs.preload.s = info->load->s.in_bounds_load == PAN_FB_LOAD_IMAGE; |
| 372 |
fbinfo->zs.discard.s = !info->store->s.store; |
| 373 |
if (fbinfo->zs.clear.s) |
| 374 |
fbinfo->zs.clear_value.stencil = info->load->s.clear.stencil; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
#if PAN_ARCH >= 5 |
| 379 |
static bool |
| 380 |
target_has_clear(const struct pan_fb_load_target *target) |
| 381 |
{ |
| 382 |
return target->in_bounds_load == PAN_FB_LOAD_CLEAR || |
| 383 |
target->border_load == PAN_FB_LOAD_CLEAR; |
| 384 |
} |
| 385 |
|
| 386 |
static enum mali_msaa |
| 387 |
translate_msaa_copy_op(const struct pan_fb_layout *fb, |
| 388 |
const struct pan_image_view *iview, |
| 389 |
enum pan_fb_msaa_copy_op msaa) |
| 390 |
{ |
| 391 |
switch (msaa) { |
| 392 |
case PAN_FB_MSAA_COPY_ALL: |
| 393 |
assert(pan_image_view_get_nr_samples(iview) == fb->sample_count); |
| 394 |
if (fb->sample_count > 1) |
| 395 |
return MALI_MSAA_LAYERED; |
| 396 |
else |
| 397 |
return MALI_MSAA_SINGLE; |
| 398 |
|
| 399 |
case PAN_FB_MSAA_COPY_SINGLE: |
| 400 |
case PAN_FB_MSAA_COPY_SAMPLE_0: |
| 401 |
return MALI_MSAA_SINGLE; |
| 402 |
|
| 403 |
case PAN_FB_MSAA_COPY_AVERAGE: |
| 404 |
/* It must be a blendable format or the hardware can't average */ |
| 405 |
assert(GENX(pan_blendable_format_from_pipe_format)(iview->format)->internal); |
| 406 |
if (fb->sample_count > 1) |
| 407 |
return MALI_MSAA_AVERAGE; |
| 408 |
else |
| 409 |
return MALI_MSAA_SINGLE; |
| 410 |
|
| 411 |
default: |
| 412 |
UNREACHABLE("Unsupported MSAA copy op"); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
struct pan_fb_clean_tile { |
| 417 |
uint8_t rts; |
| 418 |
bool zs, s; |
| 419 |
}; |
| 420 |
|
| 421 |
static bool |
| 422 |
pan_fb_load_target_always(const struct pan_fb_load_target *target) |
| 423 |
{ |
| 424 |
if (target->in_bounds_load == PAN_FB_LOAD_NONE && |
| 425 |
target->border_load == PAN_FB_LOAD_NONE) |
| 426 |
return false; |
| 427 |
|
| 428 |
return target->always; |
| 429 |
} |
| 430 |
|
| 431 |
static bool |
| 432 |
pan_fb_store_target_always(const struct pan_fb_store_target *target) |
| 433 |
{ |
| 434 |
return target->store && target->always; |
| 435 |
} |
| 436 |
|
| 437 |
static struct pan_fb_clean_tile |
| 438 |
pan_fb_get_clean_tile(const struct pan_fb_desc_info *info) |
| 439 |
{ |
| 440 |
const struct pan_fb_layout *fb = info->fb; |
| 441 |
const struct pan_fb_load *load = info->load; |
| 442 |
const struct pan_fb_store *store = info->store; |
| 443 |
|
| 444 |
struct pan_fb_clean_tile ct = { }; |
| 445 |
|
| 446 |
for (unsigned rt = 0; rt < fb->rt_count; rt++) { |
| 447 |
if (fb->rt_formats[rt] == PIPE_FORMAT_NONE) |
| 448 |
continue; |
| 449 |
|
| 450 |
if ((load && pan_fb_load_target_always(&load->rts[rt])) || |
| 451 |
(store && pan_fb_store_target_always(&store->rts[rt]))) |
| 452 |
ct.rts |= BITFIELD_BIT(rt); |
| 453 |
|
| 454 |
if (store && store->rts[rt].store) { |
| 455 |
const struct pan_image *img = |
| 456 |
pan_image_view_get_color_plane(store->rts[rt].iview).image; |
| 457 |
if (GENX(pan_force_clean_write_on)(img, fb->tile_size_px)) |
| 458 |
ct.rts |= BITFIELD_BIT(rt); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
const bool z_always_load = load && pan_fb_load_target_always(&load->z); |
| 463 |
const bool s_always_load = load && pan_fb_load_target_always(&load->s); |
| 464 |
const bool zs_always_store = store && pan_fb_store_target_always(&store->zs); |
| 465 |
const bool s_always_store = store && pan_fb_store_target_always(&store->s); |
| 466 |
|
| 467 |
if (fb->z_format != PIPE_FORMAT_NONE) { |
| 468 |
ct.zs = z_always_load || zs_always_store; |
| 469 |
|
| 470 |
if (store && store->zs.store) { |
| 471 |
const struct pan_image *img = |
| 472 |
pan_image_view_get_zs_plane(store->zs.iview).image; |
| 473 |
assert(util_format_get_depth_bits(img->props.format) == |
| 474 |
util_format_get_depth_bits(fb->z_format)); |
| 475 |
assert(util_format_get_depth_bits(store->zs.iview->format) == |
| 476 |
util_format_get_depth_bits(fb->z_format)); |
| 477 |
const struct util_format_description *zs_fmt_desc = |
| 478 |
util_format_description(img->props.format); |
| 479 |
|
| 480 |
/* If ZS writes stencil, we have to also include stencil loads */ |
| 481 |
if (util_format_has_stencil(zs_fmt_desc) && s_always_load) |
| 482 |
ct.zs = true; |
| 483 |
|
| 484 |
if (GENX(pan_force_clean_write_on)(img, fb->tile_size_px)) |
| 485 |
ct.zs = true; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
if (fb->s_format != PIPE_FORMAT_NONE) { |
| 490 |
ct.s = s_always_load || s_always_store; |
| 491 |
|
| 492 |
if (store && store->s.store) { |
| 493 |
const struct pan_image *img = |
| 494 |
pan_image_view_get_s_plane(store->s.iview).image; |
| 495 |
if (GENX(pan_force_clean_write_on)(img, fb->tile_size_px)) |
| 496 |
ct.s = true; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
return ct; |
| 501 |
} |
| 502 |
|
| 503 |
static void |
| 504 |
emit_zs_crc_desc(const struct pan_fb_desc_info *info, |
| 505 |
const struct pan_fb_clean_tile ct, |
| 506 |
struct mali_zs_crc_extension_packed *zs_crc) |
| 507 |
{ |
| 508 |
const struct pan_fb_layout *fb = info->fb; |
| 509 |
const struct pan_fb_store *store = info->store; |
| 510 |
|
| 511 |
pan_pack(zs_crc, ZS_CRC_EXTENSION, cfg) { |
| 512 |
if (store && store->zs.store) { |
| 513 |
cfg.zs.msaa = translate_msaa_copy_op(fb, store->zs.iview, |
| 514 |
store->zs.msaa); |
| 515 |
|
| 516 |
#if PAN_ARCH >= 6 |
| 517 |
cfg.zs.clean_tile_write_enable = ct.zs; |
| 518 |
#else |
| 519 |
cfg.zs.clean_pixel_write_enable = ct.zs; |
| 520 |
#endif |
| 521 |
|
| 522 |
} |
| 523 |
|
| 524 |
if (store && store->s.store) { |
| 525 |
cfg.s.msaa = translate_msaa_copy_op(fb, store->s.iview, |
| 526 |
store->s.msaa); |
| 527 |
|
| 528 |
#if PAN_ARCH >= 6 |
| 529 |
cfg.s.clean_tile_write_enable = ct.s; |
| 530 |
#else |
| 531 |
cfg.s.clean_pixel_write_enable = ct.s; |
| 532 |
#endif |
| 533 |
} |
| 534 |
|
| 535 |
/* TODO CRC */ |
| 536 |
} |
| 537 |
|
| 538 |
if (store && store->zs.store) { |
| 539 |
const struct pan_image_view *iview = store->zs.iview; |
| 540 |
const struct pan_mod_handler *mod_handler = |
| 541 |
pan_image_view_get_zs_plane(iview).image->mod_handler; |
| 542 |
|
| 543 |
assert(info->layer < pan_image_view_get_layer_count(iview)); |
| 544 |
const struct pan_attachment_info att = { |
| 545 |
.iview = iview, |
| 546 |
.layer_or_z_slice = iview->first_layer + info->layer, |
| 547 |
.fb_tile_size_px = fb->tile_size_px, |
| 548 |
}; |
| 549 |
|
| 550 |
struct mali_zs_crc_extension_packed zs_part; |
| 551 |
mod_handler->emit_zs_attachment(&att, &zs_part); |
| 552 |
pan_merge(zs_crc, &zs_part, ZS_CRC_EXTENSION); |
| 553 |
} |
| 554 |
|
| 555 |
if (store && store->s.store) { |
| 556 |
const struct pan_image_view *iview = store->s.iview; |
| 557 |
const struct pan_mod_handler *mod_handler = |
| 558 |
pan_image_view_get_s_plane(iview).image->mod_handler; |
| 559 |
|
| 560 |
assert(info->layer < pan_image_view_get_layer_count(iview)); |
| 561 |
const struct pan_attachment_info att = { |
| 562 |
.iview = iview, |
| 563 |
.layer_or_z_slice = iview->first_layer + info->layer, |
| 564 |
.fb_tile_size_px = fb->tile_size_px, |
| 565 |
}; |
| 566 |
|
| 567 |
struct mali_zs_crc_extension_packed s_part; |
| 568 |
mod_handler->emit_s_attachment(&att, &s_part); |
| 569 |
pan_merge(zs_crc, &s_part, ZS_CRC_EXTENSION); |
| 570 |
} |
| 571 |
|
| 572 |
/* TODO: CRC */ |
| 573 |
} |
| 574 |
|
| 575 |
static void |
| 576 |
emit_rgb_rt_desc(const struct pan_fb_desc_info *info, |
| 577 |
const struct pan_fb_clean_tile ct, |
| 578 |
unsigned rt, uint32_t tile_rt_offset_B, |
| 579 |
struct mali_rgb_render_target_packed *rgb_rt) |
| 580 |
{ |
| 581 |
const struct pan_fb_layout *fb = info->fb; |
| 582 |
const struct pan_fb_load *load = info->load; |
| 583 |
const struct pan_fb_store *store = info->store; |
| 584 |
|
| 585 |
if (fb->rt_formats[rt] == PIPE_FORMAT_NONE) { |
| 586 |
pan_pack(rgb_rt, RGB_RENDER_TARGET, cfg) { |
| 587 |
/* Place non-existent targets at the end. Hardware bounds |
| 588 |
* checking should just discard writes and zero reads. |
| 589 |
*/ |
| 590 |
cfg.internal_buffer_offset = fb->tile_rt_alloc_B; |
| 591 |
cfg.internal_format = MALI_COLOR_BUFFER_INTERNAL_FORMAT_R8G8B8A8; |
| 592 |
cfg.write_enable = false; |
| 593 |
#if PAN_ARCH >= 7 |
| 594 |
cfg.writeback_block_format = MALI_BLOCK_FORMAT_TILED_U_INTERLEAVED; |
| 595 |
#endif |
| 596 |
} |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
pan_pack(rgb_rt, RGB_RENDER_TARGET, cfg) { |
| 601 |
cfg.internal_buffer_offset = tile_rt_offset_B; |
| 602 |
cfg.dithering_enable = true; |
| 603 |
|
| 604 |
if (store && store->rts[rt].store) { |
| 605 |
cfg.writeback_msaa = |
| 606 |
translate_msaa_copy_op(fb, store->rts[rt].iview, |
| 607 |
store->rts[rt].msaa); |
| 608 |
} |
| 609 |
|
| 610 |
#if PAN_ARCH >= 6 |
| 611 |
cfg.clean_tile_write_enable = !!(ct.rts & BITFIELD_BIT(rt)); |
| 612 |
#else |
| 613 |
cfg.clean_pixel_write_enable = !!(ct.rts & BITFIELD_BIT(rt)); |
| 614 |
#endif |
| 615 |
|
| 616 |
if (load && target_has_clear(&load->rts[rt])) { |
| 617 |
uint32_t packed[4] = {}; |
| 618 |
pan_pack_color(GENX(pan_blendable_formats), packed, |
| 619 |
&load->rts[rt].clear.color, fb->rt_formats[rt], |
| 620 |
false /* dithered */); |
| 621 |
|
| 622 |
cfg.clear = (struct MALI_RT_CLEAR) { |
| 623 |
.color_0 = packed[0], |
| 624 |
.color_1 = packed[1], |
| 625 |
.color_2 = packed[2], |
| 626 |
.color_3 = packed[3], |
| 627 |
}; |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
struct mali_render_target_packed desc; |
| 632 |
if (store && store->rts[rt].store) { |
| 633 |
const struct pan_image_view *iview = store->rts[rt].iview; |
| 634 |
const struct pan_mod_handler *mod_handler = |
| 635 |
pan_image_view_get_color_plane(iview).image->mod_handler; |
| 636 |
|
| 637 |
assert(info->layer < pan_image_view_get_layer_count(iview)); |
| 638 |
const struct pan_attachment_info att = { |
| 639 |
.iview = iview, |
| 640 |
.layer_or_z_slice = iview->first_layer + info->layer, |
| 641 |
.fb_tile_size_px = fb->tile_size_px, |
| 642 |
}; |
| 643 |
|
| 644 |
mod_handler->emit_color_attachment(&att, &desc); |
| 645 |
} else { |
| 646 |
GENX(pan_emit_default_color_attachment)(fb->rt_formats[rt], &desc); |
| 647 |
} |
| 648 |
pan_merge(rgb_rt, &desc, RGB_RENDER_TARGET); |
| 649 |
} |
| 650 |
|
| 651 |
#if PAN_ARCH >= 6 |
| 652 |
/* All GPUs starting from Bifrost are affected by issue TSIX-2033: |
| 653 |
* |
| 654 |
* Forcing clean_tile_writes breaks INTERSECT readbacks |
| 655 |
* |
| 656 |
* To workaround, use the pre-frame shader mode ALWAYS instead of INTERSECT if |
| 657 |
* clean_tile_write_enable is set on either one of the color, depth or stencil |
| 658 |
* buffers. Since INTERSECT is a hint that the hardware may ignore, this |
| 659 |
* cannot affect correctness, only performance. */ |
| 660 |
|
| 661 |
static enum mali_pre_post_frame_shader_mode |
| 662 |
pan_fix_frame_shader_mode(enum mali_pre_post_frame_shader_mode mode, |
| 663 |
bool force_clean_tile) |
| 664 |
{ |
| 665 |
if (force_clean_tile && mode == MALI_PRE_POST_FRAME_SHADER_MODE_INTERSECT) |
| 666 |
return MALI_PRE_POST_FRAME_SHADER_MODE_ALWAYS; |
| 667 |
else |
| 668 |
return mode; |
| 669 |
} |
| 670 |
#endif |
| 671 |
|
| 672 |
uint32_t |
| 673 |
GENX(pan_emit_fb_desc)(const struct pan_fb_desc_info *info, void *out) |
| 674 |
{ |
| 675 |
const struct pan_fb_layout *fb = info->fb; |
| 676 |
const struct pan_fb_load *load = info->load; |
| 677 |
const struct pan_fb_store *store = info->store; |
| 678 |
const struct pan_fb_clean_tile ct = pan_fb_get_clean_tile(info); |
| 679 |
|
| 680 |
const bool has_zs_crc_ext = pan_fb_has_zs(fb); |
| 681 |
|
| 682 |
struct mali_framebuffer_packed fbd = {}; |
| 683 |
|
| 684 |
#if PAN_ARCH <= 5 |
| 685 |
GENX(pan_emit_tls)(info->tls, |
| 686 |
pan_section_ptr(&fbd, FRAMEBUFFER, LOCAL_STORAGE)); |
| 687 |
#endif |
| 688 |
|
| 689 |
pan_section_pack(&fbd, FRAMEBUFFER, PARAMETERS, cfg) { |
| 690 |
#if PAN_ARCH >= 6 |
| 691 |
cfg.pre_frame_0 = pan_fix_frame_shader_mode(info->frame_shaders.modes[0], |
| 692 |
ct.rts || ct.zs || ct.s); |
| 693 |
cfg.pre_frame_1 = pan_fix_frame_shader_mode(info->frame_shaders.modes[1], |
| 694 |
ct.rts || ct.zs || ct.s); |
| 695 |
cfg.post_frame = info->frame_shaders.modes[2]; |
| 696 |
cfg.frame_shader_dcds = info->frame_shaders.dcd_pointer; |
| 697 |
|
| 698 |
cfg.sample_locations = info->sample_pos_array_pointer; |
| 699 |
#endif |
| 700 |
|
| 701 |
#if PAN_ARCH >= 13 |
| 702 |
/* Enabling prepass without pipelineing is generally not good for |
| 703 |
* performance, so disable HSR in that case. |
| 704 |
*/ |
| 705 |
cfg.hsr_prepass_enable = info->allow_hsr_prepass && |
| 706 |
pan_fb_can_pipeline_zs(fb); |
| 707 |
cfg.hsr_prepass_interleaving_enable = pan_fb_can_pipeline_zs(fb); |
| 708 |
cfg.hsr_prepass_filter_enable = true; |
| 709 |
cfg.hsr_hierarchical_optimizations_enable = true; |
| 710 |
#endif |
| 711 |
|
| 712 |
#if PAN_ARCH >= 9 |
| 713 |
/* internal_layer_index is used to select the right primitive list in |
| 714 |
* the tiler context, and frame_arg is the value that's passed to the |
| 715 |
* fragment shader through r62-r63, which we use to pass gl_Layer. Since |
| 716 |
* the layer_idx only takes 8-bits, we might use the extra 56-bits we |
| 717 |
* have in frame_argument to pass other information to the fragment |
| 718 |
* shader at some point. |
| 719 |
*/ |
| 720 |
assert(info->layer >= info->tiler_ctx->valhall.layer_offset); |
| 721 |
cfg.internal_layer_index = |
| 722 |
info->layer - info->tiler_ctx->valhall.layer_offset; |
| 723 |
cfg.frame_argument = info->layer; |
| 724 |
#endif |
| 725 |
|
| 726 |
cfg.width = fb->width_px; |
| 727 |
cfg.height = fb->height_px; |
| 728 |
|
| 729 |
const struct pan_fb_bbox fb_area_px = |
| 730 |
pan_fb_bbox_from_xywh(0, 0, info->fb->width_px, info->fb->height_px); |
| 731 |
assert(pan_fb_bbox_is_valid(info->fb->tiling_area_px)); |
| 732 |
const struct pan_fb_bbox bbox_px = |
| 733 |
pan_fb_bbox_clamp(info->fb->tiling_area_px, fb_area_px); |
| 734 |
cfg.bound_min_x = bbox_px.min_x; |
| 735 |
cfg.bound_min_y = bbox_px.min_y; |
| 736 |
cfg.bound_max_x = bbox_px.max_x; |
| 737 |
cfg.bound_max_y = bbox_px.max_y; |
| 738 |
|
| 739 |
cfg.sample_count = fb->sample_count; |
| 740 |
cfg.sample_pattern = pan_sample_pattern(fb->sample_count); |
| 741 |
|
| 742 |
/* Ensure we cover the samples on the edge for 16x MSAA */ |
| 743 |
cfg.tie_break_rule = fb->sample_count == 16 ? |
| 744 |
MALI_TIE_BREAK_RULE_MINUS_180_OUT_0_IN : |
| 745 |
MALI_TIE_BREAK_RULE_MINUS_180_IN_0_OUT; |
| 746 |
|
| 747 |
cfg.effective_tile_size = fb->tile_size_px; |
| 748 |
|
| 749 |
#if PAN_ARCH >= 9 |
| 750 |
cfg.point_sprite_coord_origin_max_y = info->sprite_coord_origin_max_y; |
| 751 |
cfg.first_provoking_vertex = info->provoking_vertex_first; |
| 752 |
#endif |
| 753 |
|
| 754 |
assert(fb->rt_count > 0); |
| 755 |
cfg.render_target_count = fb->rt_count; |
| 756 |
cfg.color_buffer_allocation = fb->tile_rt_alloc_B; |
| 757 |
|
| 758 |
if (fb->s_format != PIPE_FORMAT_NONE) { |
| 759 |
cfg.s_clear = load && target_has_clear(&load->s) ? |
| 760 |
load->s.clear.stencil : 0; |
| 761 |
cfg.s_write_enable = store && store->s.store; |
| 762 |
} |
| 763 |
|
| 764 |
if (fb->z_format != PIPE_FORMAT_NONE) { |
| 765 |
cfg.z_internal_format = pan_get_z_internal_format(fb->z_format); |
| 766 |
cfg.z_clear = load && target_has_clear(&load->z) ? |
| 767 |
load->z.clear.depth : 0; |
| 768 |
cfg.z_write_enable = store && store->zs.store; |
| 769 |
} else { |
| 770 |
/* Default to 24 bit depth if there's no surface. */ |
| 771 |
cfg.z_internal_format = MALI_Z_INTERNAL_FORMAT_D24; |
| 772 |
|
| 773 |
/* If we want a stencil store and we don't have depth, that should |
| 774 |
* happen as a stencil store, not combined depth/stencil. |
| 775 |
*/ |
| 776 |
assert(!store || !store->zs.store); |
| 777 |
} |
| 778 |
|
| 779 |
cfg.has_zs_crc_extension = has_zs_crc_ext; |
| 780 |
|
| 781 |
#if PAN_ARCH >= 6 |
| 782 |
cfg.tiler = PAN_ARCH >= 9 ? info->tiler_ctx->valhall.desc |
| 783 |
: info->tiler_ctx->bifrost.desc; |
| 784 |
#endif |
| 785 |
} |
| 786 |
|
| 787 |
#if PAN_ARCH <= 5 |
| 788 |
/* TODO: Midgard tiler */ |
| 789 |
assert(info->tiler_ctx == NULL); |
| 790 |
|
| 791 |
/* All weights set to 0, nothing to do here */ |
| 792 |
pan_section_pack(&fbd, FRAMEBUFFER, TILER_WEIGHTS, w); |
| 793 |
#endif |
| 794 |
|
| 795 |
memcpy(out, &fbd, sizeof(fbd)); |
| 796 |
out += sizeof(fbd); |
| 797 |
|
| 798 |
if (has_zs_crc_ext) { |
| 799 |
struct mali_zs_crc_extension_packed zs_crc; |
| 800 |
emit_zs_crc_desc(info, ct, &zs_crc); |
| 801 |
memcpy(out, &zs_crc, sizeof(zs_crc)); |
| 802 |
out += sizeof(zs_crc); |
| 803 |
} |
| 804 |
|
| 805 |
uint32_t tile_rt_offset_B = 0; |
| 806 |
for (unsigned rt = 0; rt < fb->rt_count; rt++) { |
| 807 |
struct mali_rgb_render_target_packed rgb_rt; |
| 808 |
emit_rgb_rt_desc(info, ct, rt, tile_rt_offset_B, &rgb_rt); |
| 809 |
memcpy(out, &rgb_rt, sizeof(rgb_rt)); |
| 810 |
out += sizeof(rgb_rt); |
| 811 |
|
| 812 |
if (fb->rt_formats[rt] != PIPE_FORMAT_NONE) { |
| 813 |
tile_rt_offset_B += pan_bytes_per_pixel_tib(fb->rt_formats[rt]) * |
| 814 |
fb->tile_size_px * fb->sample_count; |
| 815 |
} |
| 816 |
} |
| 817 |
assert(tile_rt_offset_B <= fb->tile_rt_alloc_B); |
| 818 |
|
| 819 |
struct mali_framebuffer_pointer_packed tag; |
| 820 |
pan_pack(&tag, FRAMEBUFFER_POINTER, cfg) { |
| 821 |
cfg.zs_crc_extension_present = has_zs_crc_ext; |
| 822 |
cfg.render_target_count = fb->rt_count; |
| 823 |
} |
| 824 |
return tag.opaque[0]; |
| 825 |
} |
| 826 |
#endif |