| 1 |
/* |
| 2 |
* Copyright © 2024 Collabora Ltd. |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "util/u_dynarray.h" |
| 7 |
|
| 8 |
#include "nir_builder.h" |
| 9 |
|
| 10 |
#include "vk_blend.h" |
| 11 |
#include "vk_format.h" |
| 12 |
#include "vk_graphics_state.h" |
| 13 |
#include "vk_log.h" |
| 14 |
|
| 15 |
#include "pan_shader.h" |
| 16 |
|
| 17 |
#include "panvk_blend.h" |
| 18 |
#include "panvk_cmd_buffer.h" |
| 19 |
#include "panvk_device.h" |
| 20 |
#include "panvk_meta.h" |
| 21 |
#include "panvk_shader.h" |
| 22 |
|
| 23 |
struct panvk_blend_shader_key { |
| 24 |
enum panvk_meta_object_key_type type; |
| 25 |
struct pan_blend_shader_key info; |
| 26 |
}; |
| 27 |
|
| 28 |
static bool |
| 29 |
lower_load_blend_const(nir_builder *b, nir_instr *instr, UNUSED void *data) |
| 30 |
{ |
| 31 |
if (instr->type != nir_instr_type_intrinsic) |
| 32 |
return false; |
| 33 |
|
| 34 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 35 |
|
| 36 |
if (intr->intrinsic != nir_intrinsic_load_blend_const_color_rgba) |
| 37 |
return false; |
| 38 |
|
| 39 |
b->cursor = nir_before_instr(instr); |
| 40 |
|
| 41 |
/* Blend constants are always passed through FAU words 0:3. */ |
| 42 |
nir_def *blend_consts = nir_load_push_constant( |
| 43 |
b, intr->def.num_components, intr->def.bit_size, nir_imm_int(b, 0)); |
| 44 |
|
| 45 |
nir_def_rewrite_uses(&intr->def, blend_consts); |
| 46 |
return true; |
| 47 |
} |
| 48 |
|
| 49 |
static VkResult |
| 50 |
get_blend_shader(struct panvk_device *dev, |
| 51 |
const struct pan_blend_state *state, |
| 52 |
nir_alu_type src0_type, nir_alu_type src1_type, |
| 53 |
unsigned rt, uint64_t *shader_addr) |
| 54 |
{ |
| 55 |
struct panvk_physical_device *pdev = |
| 56 |
to_panvk_physical_device(dev->vk.physical); |
| 57 |
struct panvk_blend_shader_key key = { |
| 58 |
.type = PANVK_META_OBJECT_KEY_BLEND_SHADER, |
| 59 |
.info = { |
| 60 |
.format = state->rts[rt].format, |
| 61 |
.src0_type = src0_type, |
| 62 |
.src1_type = src1_type, |
| 63 |
.rt = rt, |
| 64 |
.logicop_enable = state->logicop_enable, |
| 65 |
.logicop_func = state->logicop_func, |
| 66 |
.nr_samples = state->rts[rt].nr_samples, |
| 67 |
.equation = state->rts[rt].equation, |
| 68 |
.alpha_to_one = state->alpha_to_one, |
| 69 |
}, |
| 70 |
}; |
| 71 |
struct panvk_internal_shader *shader; |
| 72 |
|
| 73 |
assert(state->logicop_enable || state->alpha_to_one || |
| 74 |
!pan_blend_is_opaque(state->rts[rt].equation)); |
| 75 |
assert(state->rts[rt].equation.color_mask != 0); |
| 76 |
|
| 77 |
VkShaderEXT shader_handle = (VkShaderEXT)vk_meta_lookup_object( |
| 78 |
&dev->meta, VK_OBJECT_TYPE_SHADER_EXT, &key, sizeof(key)); |
| 79 |
if (shader_handle != VK_NULL_HANDLE) |
| 80 |
goto out; |
| 81 |
|
| 82 |
nir_shader *nir = |
| 83 |
GENX(pan_blend_create_shader)(state, src0_type, src1_type, rt); |
| 84 |
|
| 85 |
NIR_PASS(_, nir, nir_shader_instructions_pass, lower_load_blend_const, |
| 86 |
nir_metadata_control_flow, NULL); |
| 87 |
|
| 88 |
/* Compile the NIR shader */ |
| 89 |
struct pan_compile_inputs inputs = { |
| 90 |
.gpu_id = pdev->kmod.dev->props.gpu_id, |
| 91 |
.gpu_variant = pdev->kmod.dev->props.gpu_variant, |
| 92 |
.is_blend = true, |
| 93 |
}; |
| 94 |
|
| 95 |
pan_preprocess_nir(nir, inputs.gpu_id); |
| 96 |
pan_postprocess_nir(nir, inputs.gpu_id); |
| 97 |
|
| 98 |
VkResult result = |
| 99 |
panvk_per_arch(create_internal_shader)(dev, nir, &inputs, &shader); |
| 100 |
|
| 101 |
ralloc_free(nir); |
| 102 |
|
| 103 |
if (result != VK_SUCCESS) |
| 104 |
return result; |
| 105 |
|
| 106 |
shader_handle = (VkShaderEXT)vk_meta_cache_object( |
| 107 |
&dev->vk, &dev->meta, &key, sizeof(key), VK_OBJECT_TYPE_SHADER_EXT, |
| 108 |
(uint64_t)panvk_internal_shader_to_handle(shader)); |
| 109 |
|
| 110 |
out: |
| 111 |
shader = panvk_internal_shader_from_handle(shader_handle); |
| 112 |
*shader_addr = panvk_priv_mem_dev_addr(shader->code_mem); |
| 113 |
return VK_SUCCESS; |
| 114 |
} |
| 115 |
|
| 116 |
static void |
| 117 |
emit_blend_desc(const struct pan_blend_state *state, uint8_t rt_idx, |
| 118 |
const struct pan_shader_info *fs_info, uint8_t loc, |
| 119 |
uint64_t fs_code, uint64_t blend_shader, uint16_t constant, |
| 120 |
bool dithered, struct mali_blend_packed *bd) |
| 121 |
{ |
| 122 |
const struct pan_blend_rt_state *rt = &state->rts[rt_idx]; |
| 123 |
|
| 124 |
pan_pack(bd, BLEND, cfg) { |
| 125 |
if (loc == MESA_VK_ATTACHMENT_UNUSED || !rt->equation.color_mask) { |
| 126 |
cfg.enable = false; |
| 127 |
cfg.internal.mode = MALI_BLEND_MODE_OFF; |
| 128 |
continue; |
| 129 |
} |
| 130 |
|
| 131 |
cfg.srgb = util_format_is_srgb(rt->format); |
| 132 |
cfg.load_destination = pan_blend_reads_dest(rt->equation); |
| 133 |
cfg.round_to_fb_precision = true; |
| 134 |
cfg.blend_constant = constant; |
| 135 |
|
| 136 |
if (blend_shader) { |
| 137 |
/* Blend and fragment shaders must be in the same 4G region. */ |
| 138 |
assert((blend_shader >> 32) == (fs_code >> 32)); |
| 139 |
/* Blend shader must be 16-byte aligned. */ |
| 140 |
assert((blend_shader & 15) == 0); |
| 141 |
/* Fragment shader return address must be 8-byte aligned. */ |
| 142 |
assert((fs_code & 7) == 0); |
| 143 |
|
| 144 |
cfg.internal.mode = MALI_BLEND_MODE_SHADER; |
| 145 |
cfg.internal.shader.pc = (uint32_t)blend_shader; |
| 146 |
|
| 147 |
#if PAN_ARCH < 9 |
| 148 |
uint32_t ret_offset = fs_info->bifrost.blend[loc].return_offset; |
| 149 |
|
| 150 |
/* If ret_offset is zero, we assume the BLEND is a terminal |
| 151 |
* instruction and set return_value to zero, to let the |
| 152 |
* blend shader jump to address zero, which terminates the |
| 153 |
* thread. |
| 154 |
*/ |
| 155 |
cfg.internal.shader.return_value = |
| 156 |
ret_offset ? fs_code + ret_offset : 0; |
| 157 |
#endif |
| 158 |
} else { |
| 159 |
bool opaque = pan_blend_is_opaque(rt->equation); |
| 160 |
|
| 161 |
cfg.internal.mode = |
| 162 |
opaque ? MALI_BLEND_MODE_OPAQUE : MALI_BLEND_MODE_FIXED_FUNCTION; |
| 163 |
|
| 164 |
pan_blend_to_fixed_function_equation(rt->equation, &cfg.equation); |
| 165 |
|
| 166 |
/* If we want the conversion to work properly, num_comps must be set to |
| 167 |
* 4. |
| 168 |
*/ |
| 169 |
cfg.internal.fixed_function.num_comps = 4; |
| 170 |
cfg.internal.fixed_function.conversion.memory_format = |
| 171 |
GENX(pan_dithered_format_from_pipe_format)(rt->format, dithered); |
| 172 |
|
| 173 |
#if PAN_ARCH >= 7 |
| 174 |
if (cfg.internal.mode == MALI_BLEND_MODE_FIXED_FUNCTION && |
| 175 |
(cfg.internal.fixed_function.conversion.memory_format & 0xff) == |
| 176 |
MALI_RGB_COMPONENT_ORDER_RGB1) { |
| 177 |
/* fixed function does not like RGB1 as the component order */ |
| 178 |
/* force this field to be the RGBA. */ |
| 179 |
cfg.internal.fixed_function.conversion.memory_format &= ~0xff; |
| 180 |
cfg.internal.fixed_function.conversion.memory_format |= |
| 181 |
MALI_RGB_COMPONENT_ORDER_RGBA; |
| 182 |
} |
| 183 |
#endif |
| 184 |
|
| 185 |
cfg.internal.fixed_function.rt = rt_idx; |
| 186 |
|
| 187 |
#if PAN_ARCH < 9 |
| 188 |
nir_alu_type type = fs_info->bifrost.blend[loc].type; |
| 189 |
if (fs_info->fs.untyped_color_outputs) { |
| 190 |
cfg.internal.fixed_function.conversion.register_format = |
| 191 |
GENX(pan_fixup_blend_type)(type, rt->format); |
| 192 |
} else { |
| 193 |
cfg.internal.fixed_function.conversion.register_format = |
| 194 |
pan_blend_type_from_nir(type); |
| 195 |
} |
| 196 |
|
| 197 |
if (!opaque) { |
| 198 |
cfg.internal.fixed_function.alpha_zero_nop = |
| 199 |
pan_blend_alpha_zero_nop(rt->equation); |
| 200 |
cfg.internal.fixed_function.alpha_one_store = |
| 201 |
pan_blend_alpha_one_store(rt->equation); |
| 202 |
} |
| 203 |
#endif |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
static bool |
| 209 |
blend_needs_shader(const struct pan_blend_state *state, unsigned rt_idx, |
| 210 |
unsigned *ff_blend_constant) |
| 211 |
{ |
| 212 |
const struct pan_blend_rt_state *rt = &state->rts[rt_idx]; |
| 213 |
|
| 214 |
/* LogicOp requires a blend shader */ |
| 215 |
if (state->logicop_enable) |
| 216 |
return true; |
| 217 |
|
| 218 |
/* alpha-to-one always requires a blend shader */ |
| 219 |
if (state->alpha_to_one) |
| 220 |
return true; |
| 221 |
|
| 222 |
/* If the output is opaque, we don't need a blend shader, no matter the |
| 223 |
* format. |
| 224 |
*/ |
| 225 |
if (pan_blend_is_opaque(rt->equation)) |
| 226 |
return false; |
| 227 |
|
| 228 |
/* Not all formats can be blended by fixed-function hardware */ |
| 229 |
if (!GENX(pan_format_supports_hw_blend)(rt->format)) |
| 230 |
return true; |
| 231 |
|
| 232 |
bool supports_2src = pan_blend_supports_2src(PAN_ARCH); |
| 233 |
if (!pan_blend_can_fixed_function(PAN_ARCH, rt->equation, supports_2src)) |
| 234 |
return true; |
| 235 |
|
| 236 |
unsigned constant_mask = pan_blend_constant_mask(rt->equation); |
| 237 |
|
| 238 |
/* v6 doesn't support blend constants in FF blend equations. */ |
| 239 |
if (constant_mask && PAN_ARCH == 6) |
| 240 |
return true; |
| 241 |
|
| 242 |
if (!pan_blend_is_homogenous_constant(constant_mask, state->constants)) |
| 243 |
return true; |
| 244 |
|
| 245 |
/* v7+ only uses the constant from RT 0. If we're not RT0, all previous |
| 246 |
* RTs using FF with a blend constant need to have the same constant, |
| 247 |
* otherwise we need a blend shader. |
| 248 |
*/ |
| 249 |
unsigned blend_const = ~0; |
| 250 |
if (constant_mask) { |
| 251 |
const float blend_const_f = |
| 252 |
pan_blend_get_constant(constant_mask, state->constants); |
| 253 |
blend_const = pan_pack_blend_constant(rt->format, blend_const_f); |
| 254 |
|
| 255 |
if (*ff_blend_constant != ~0 && blend_const != *ff_blend_constant) |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
/* Update the fixed function blend constant, if we use it. */ |
| 260 |
if (blend_const != ~0) |
| 261 |
*ff_blend_constant = blend_const; |
| 262 |
|
| 263 |
return false; |
| 264 |
} |
| 265 |
|
| 266 |
VkResult |
| 267 |
panvk_per_arch(blend_emit_descs)(struct panvk_cmd_buffer *cmdbuf, |
| 268 |
struct mali_blend_packed *bds) |
| 269 |
{ |
| 270 |
struct panvk_device *dev = to_panvk_device(cmdbuf->vk.base.device); |
| 271 |
const struct vk_dynamic_graphics_state *dyns = |
| 272 |
&cmdbuf->vk.dynamic_graphics_state; |
| 273 |
const struct vk_color_blend_state *cb = &dyns->cb; |
| 274 |
const struct vk_color_attachment_location_state *cal = &dyns->cal; |
| 275 |
const struct panvk_shader_variant *fs = |
| 276 |
panvk_shader_only_variant(cmdbuf->state.gfx.fs.shader); |
| 277 |
const struct pan_shader_info *fs_info = &fs->info; |
| 278 |
uint64_t fs_code = panvk_shader_variant_get_dev_addr(fs); |
| 279 |
const struct panvk_rendering_state *render = &cmdbuf->state.gfx.render; |
| 280 |
const VkFormat *color_attachment_formats = render->color_attachments.fmts; |
| 281 |
const uint8_t *color_attachment_samples = render->color_attachments.samples; |
| 282 |
struct panvk_blend_info *blend_info = &cmdbuf->state.gfx.cb.info; |
| 283 |
struct pan_blend_state bs = { |
| 284 |
.alpha_to_one = dyns->ms.alpha_to_one_enable, |
| 285 |
.logicop_enable = cb->logic_op_enable, |
| 286 |
.logicop_func = vk_logic_op_to_pipe(cb->logic_op), |
| 287 |
.rt_count = cmdbuf->state.gfx.render.fb.layout.rt_count, |
| 288 |
.constants = |
| 289 |
{ |
| 290 |
cb->blend_constants[0], |
| 291 |
cb->blend_constants[1], |
| 292 |
cb->blend_constants[2], |
| 293 |
cb->blend_constants[3], |
| 294 |
}, |
| 295 |
}; |
| 296 |
uint64_t blend_shaders[8] = {}; |
| 297 |
/* All bits set to one encodes unused fixed-function blend constant. */ |
| 298 |
unsigned ff_blend_constant = ~0; |
| 299 |
|
| 300 |
uint8_t loc_rt[MAX_RTS], rt_loc[MAX_RTS]; |
| 301 |
memset(loc_rt, MESA_VK_ATTACHMENT_UNUSED, sizeof(loc_rt)); |
| 302 |
memset(rt_loc, MESA_VK_ATTACHMENT_UNUSED, sizeof(rt_loc)); |
| 303 |
|
| 304 |
memset(blend_info, 0, sizeof(*blend_info)); |
| 305 |
for (uint8_t i = 0; i < bs.rt_count; i++) { |
| 306 |
struct pan_blend_rt_state *rt = &bs.rts[i]; |
| 307 |
|
| 308 |
uint8_t loc = cal->color_map[i]; |
| 309 |
if (loc == MESA_VK_ATTACHMENT_UNUSED) |
| 310 |
continue; |
| 311 |
|
| 312 |
if (!(fs_info->outputs_written & BITFIELD_BIT(FRAG_RESULT_DATA0 + loc))) |
| 313 |
continue; |
| 314 |
|
| 315 |
/* At this point, we know it's mapped to a shader location. */ |
| 316 |
assert(loc < MAX_RTS && loc_rt[loc] == MESA_VK_ATTACHMENT_UNUSED); |
| 317 |
rt_loc[i] = loc; |
| 318 |
loc_rt[loc] = i; |
| 319 |
|
| 320 |
if (!(cb->color_write_enables & BITFIELD_BIT(i))) |
| 321 |
continue; |
| 322 |
|
| 323 |
if (color_attachment_formats[i] == VK_FORMAT_UNDEFINED) |
| 324 |
continue; |
| 325 |
|
| 326 |
if (!cb->attachments[i].write_mask) |
| 327 |
continue; |
| 328 |
|
| 329 |
rt->format = vk_format_to_pipe_format(color_attachment_formats[i]); |
| 330 |
|
| 331 |
/* Disable blending for LOGICOP_NOOP unless the format is float/srgb */ |
| 332 |
bool is_float = util_format_is_float(rt->format); |
| 333 |
if (bs.logicop_enable && bs.logicop_func == PIPE_LOGICOP_NOOP && |
| 334 |
!(is_float || util_format_is_srgb(rt->format))) |
| 335 |
continue; |
| 336 |
|
| 337 |
rt->nr_samples = color_attachment_samples[i]; |
| 338 |
rt->equation.blend_enable = cb->attachments[i].blend_enable; |
| 339 |
rt->equation.is_float = is_float; |
| 340 |
rt->equation.color_mask = cb->attachments[i].write_mask; |
| 341 |
|
| 342 |
rt->equation.rgb_func = |
| 343 |
vk_blend_op_to_pipe(cb->attachments[i].color_blend_op); |
| 344 |
rt->equation.rgb_src_factor = |
| 345 |
vk_blend_factor_to_pipe(cb->attachments[i].src_color_blend_factor); |
| 346 |
rt->equation.rgb_dst_factor = |
| 347 |
vk_blend_factor_to_pipe(cb->attachments[i].dst_color_blend_factor); |
| 348 |
rt->equation.alpha_func = |
| 349 |
vk_blend_op_to_pipe(cb->attachments[i].alpha_blend_op); |
| 350 |
rt->equation.alpha_src_factor = |
| 351 |
vk_blend_factor_to_pipe(cb->attachments[i].src_alpha_blend_factor); |
| 352 |
rt->equation.alpha_dst_factor = |
| 353 |
vk_blend_factor_to_pipe(cb->attachments[i].dst_alpha_blend_factor); |
| 354 |
|
| 355 |
/* We have the format and the constants so we can optimize the blend |
| 356 |
* equation before we decide if we actually need a blend shader. |
| 357 |
*/ |
| 358 |
pan_blend_optimize_equation(&rt->equation, rt->format, bs.constants); |
| 359 |
|
| 360 |
blend_info->any_dest_read |= pan_blend_reads_dest(rt->equation); |
| 361 |
|
| 362 |
if (blend_needs_shader(&bs, i, &ff_blend_constant)) { |
| 363 |
nir_alu_type src0_type = fs_info->bifrost.blend[loc].type; |
| 364 |
nir_alu_type src1_type = fs_info->bifrost.blend_src1_type; |
| 365 |
|
| 366 |
VkResult result = get_blend_shader(dev, &bs, src0_type, src1_type, |
| 367 |
i, &blend_shaders[i]); |
| 368 |
if (result != VK_SUCCESS) |
| 369 |
return result; |
| 370 |
|
| 371 |
blend_info->shader_loads_blend_const |= |
| 372 |
pan_blend_constant_mask(rt->equation) != 0; |
| 373 |
blend_info->needs_shader = true; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
/* Set the blend constant to zero if it's not used by any of the blend ops. */ |
| 378 |
if (ff_blend_constant == ~0) |
| 379 |
ff_blend_constant = 0; |
| 380 |
|
| 381 |
struct mali_blend_packed packed[MAX_RTS]; |
| 382 |
for (uint8_t rt = 0; rt < bs.rt_count; rt++) { |
| 383 |
bool dithered = render->flags & |
| 384 |
VK_RENDERING_ENABLE_LEGACY_DITHERING_BIT_EXT; |
| 385 |
|
| 386 |
emit_blend_desc(&bs, rt, fs_info, rt_loc[rt], fs_code, |
| 387 |
blend_shaders[rt], ff_blend_constant, dithered, |
| 388 |
&packed[rt]); |
| 389 |
} |
| 390 |
|
| 391 |
/* Copy into the GPU descriptor array */ |
| 392 |
typed_memcpy(bds, packed, bs.rt_count); |
| 393 |
|
| 394 |
/* Re-order blend descriptors for the shader |
| 395 |
* |
| 396 |
* Blending on Bifrost+ is really annoying. In theory, we can order the |
| 397 |
* blend descriptors any way we want and, in theory, they all have an RT |
| 398 |
* index. However, that's not really the way blending works. If every |
| 399 |
* blend descriptor is either disabled or has the RT index that's the same |
| 400 |
* as the blend descriptor index, everything is fine. If not, we're in for |
| 401 |
* a bit of trouble. |
| 402 |
* |
| 403 |
* The RT index is only really consumed by the BLEND instruction to tell it |
| 404 |
* what RT to target. The FF blend hardware, however, assumes that blend |
| 405 |
* RTs map 1:1 to framebuffer RTs. If the BLEND instruction kicks off a |
| 406 |
* blend shader, everything is good because the blend shader has the |
| 407 |
* correct RT index baked into it. But if the BLEND fires off a message to |
| 408 |
* the blend unit, it then looks up the blend descriptor by RT index, |
| 409 |
* ignoring which blend descriptor FAU we read from the shader. Say, for |
| 410 |
* instance bds[3].rt == 5. If we BLEND using blend_descriptor_3, the |
| 411 |
* BLEND instruction will see an RT of 5 and kick off a blend message. The |
| 412 |
* blend unit will then fetch bds[5] and use that to do the actual blend. |
| 413 |
* |
| 414 |
* We could try to do something crazy where we divide blend descriptors |
| 415 |
* into a shader half and a HW half and only re-arrange the shader half so |
| 416 |
* that the hardware's little daisy-chain game works. However, there are a |
| 417 |
* few fields that are used by both, notably the conversion, so this is |
| 418 |
* dead in the water. |
| 419 |
* |
| 420 |
* The solution, then, is to push our own blend descriptors. Or at least |
| 421 |
* the shader half (everything in .internal). This lets us leave the HW |
| 422 |
* blend descriptors with a 1:1 mapping to framebuffer RTs and re-order |
| 423 |
* things from the shader PoV. The BLEND instructions in the shader will |
| 424 |
* use the ones we pushed manually and then, when a blend message gets sent |
| 425 |
* to the hardware, it'll pick up an idential blend descriptor in the RT |
| 426 |
* index location in the bds[] array. |
| 427 |
*/ |
| 428 |
for (uint8_t loc = 0; loc < MAX_RTS; loc++) { |
| 429 |
uint8_t rt = loc_rt[loc]; |
| 430 |
if (rt == MESA_VK_ATTACHMENT_UNUSED) { |
| 431 |
struct mali_blend_packed disabled; |
| 432 |
pan_pack(&disabled, BLEND, cfg) { |
| 433 |
cfg.enable = false; |
| 434 |
cfg.internal.mode = MALI_BLEND_MODE_OFF; |
| 435 |
} |
| 436 |
cmdbuf->state.gfx.fs.blend_descs[loc] = |
| 437 |
disabled.opaque[2] | (uint64_t)disabled.opaque[3] << 32; |
| 438 |
} else { |
| 439 |
cmdbuf->state.gfx.fs.blend_descs[loc] = |
| 440 |
packed[rt].opaque[2] | (uint64_t)packed[rt].opaque[3] << 32; |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
gfx_state_set_dirty(cmdbuf, FS_PUSH_UNIFORMS); |
| 445 |
|
| 446 |
return VK_SUCCESS; |
| 447 |
} |