| 1 |
/* |
| 2 |
* Copyright 2024 Alyssa Rosenzweig |
| 3 |
* Copyright 2024 Valve Corporation |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "gallium/include/pipe/p_defines.h" |
| 8 |
#include "poly/cl/libpoly.h" |
| 9 |
#include "poly/nir/poly_nir.h" |
| 10 |
#include "util/format/u_formats.h" |
| 11 |
#include "agx_abi.h" |
| 12 |
#include "agx_linker.h" |
| 13 |
#include "agx_nir.h" |
| 14 |
#include "agx_nir_lower_vbo.h" |
| 15 |
#include "agx_pack.h" |
| 16 |
#include "agx_tilebuffer.h" |
| 17 |
#include "libagx.h" |
| 18 |
#include "nir.h" |
| 19 |
#include "nir_builder.h" |
| 20 |
#include "nir_builder_opcodes.h" |
| 21 |
#include "nir_lower_blend.h" |
| 22 |
#include "shader_enums.h" |
| 23 |
|
| 24 |
/* |
| 25 |
* Insert code into a fragment shader to lower polygon stipple. The stipple is |
| 26 |
* passed in a sideband, rather than requiring a texture binding. This is |
| 27 |
* simpler for drivers to integrate and might be more efficient. |
| 28 |
*/ |
| 29 |
static bool |
| 30 |
agx_nir_lower_poly_stipple(nir_shader *s) |
| 31 |
{ |
| 32 |
assert(s->info.stage == MESA_SHADER_FRAGMENT); |
| 33 |
|
| 34 |
/* Insert at the beginning for performance. */ |
| 35 |
nir_builder b_ = |
| 36 |
nir_builder_at(nir_before_impl(nir_shader_get_entrypoint(s))); |
| 37 |
nir_builder *b = &b_; |
| 38 |
|
| 39 |
/* The stipple coordinate is defined at the window coordinate mod 32. It's |
| 40 |
* reversed along the X-axis to simplify the driver, hence the NOT. |
| 41 |
*/ |
| 42 |
nir_def *raw = nir_u2u32(b, nir_load_pixel_coord(b)); |
| 43 |
nir_def *coord = nir_umod_imm( |
| 44 |
b, |
| 45 |
nir_vec2(b, nir_inot(b, nir_channel(b, raw, 0)), nir_channel(b, raw, 1)), |
| 46 |
32); |
| 47 |
|
| 48 |
/* Extract the column from the packed bitfield */ |
| 49 |
nir_def *pattern = nir_load_polygon_stipple_agx(b, nir_channel(b, coord, 1)); |
| 50 |
nir_def *bit = nir_ubitfield_extract(b, pattern, nir_channel(b, coord, 0), |
| 51 |
nir_imm_int(b, 1)); |
| 52 |
|
| 53 |
/* Discard fragments where the pattern is 0 */ |
| 54 |
nir_demote_if(b, nir_ieq_imm(b, bit, 0)); |
| 55 |
s->info.fs.uses_discard = true; |
| 56 |
|
| 57 |
return nir_progress(true, b->impl, nir_metadata_control_flow); |
| 58 |
} |
| 59 |
|
| 60 |
static int |
| 61 |
map_vs_part_uniform(nir_intrinsic_instr *intr, unsigned nr_attribs) |
| 62 |
{ |
| 63 |
switch (intr->intrinsic) { |
| 64 |
case nir_intrinsic_load_vbo_base_agx: |
| 65 |
return AGX_ABI_VUNI_VBO_BASE(nir_src_as_uint(intr->src[0])); |
| 66 |
|
| 67 |
case nir_intrinsic_load_attrib_clamp_agx: |
| 68 |
return AGX_ABI_VUNI_VBO_CLAMP(nr_attribs, nir_src_as_uint(intr->src[0])); |
| 69 |
|
| 70 |
case nir_intrinsic_load_first_vertex: |
| 71 |
return AGX_ABI_VUNI_FIRST_VERTEX(nr_attribs); |
| 72 |
|
| 73 |
case nir_intrinsic_load_base_instance: |
| 74 |
return AGX_ABI_VUNI_BASE_INSTANCE(nr_attribs); |
| 75 |
|
| 76 |
case nir_intrinsic_load_vertex_param_buffer_poly: |
| 77 |
return AGX_ABI_VUNI_VERTEX_PARAMS(nr_attribs); |
| 78 |
|
| 79 |
default: |
| 80 |
return -1; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
static int |
| 85 |
map_fs_part_uniform(nir_intrinsic_instr *intr) |
| 86 |
{ |
| 87 |
switch (intr->intrinsic) { |
| 88 |
case nir_intrinsic_load_blend_const_color_r_float: |
| 89 |
return AGX_ABI_FUNI_BLEND_R; |
| 90 |
|
| 91 |
case nir_intrinsic_load_blend_const_color_g_float: |
| 92 |
return AGX_ABI_FUNI_BLEND_G; |
| 93 |
|
| 94 |
case nir_intrinsic_load_blend_const_color_b_float: |
| 95 |
return AGX_ABI_FUNI_BLEND_B; |
| 96 |
|
| 97 |
case nir_intrinsic_load_blend_const_color_a_float: |
| 98 |
return AGX_ABI_FUNI_BLEND_A; |
| 99 |
|
| 100 |
default: |
| 101 |
return -1; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
static bool |
| 106 |
lower_non_monolithic_uniforms(nir_builder *b, nir_intrinsic_instr *intr, |
| 107 |
void *data) |
| 108 |
{ |
| 109 |
int unif; |
| 110 |
if (b->shader->info.stage == MESA_SHADER_VERTEX) { |
| 111 |
unsigned *nr_attribs = data; |
| 112 |
unif = map_vs_part_uniform(intr, *nr_attribs); |
| 113 |
} else { |
| 114 |
unif = map_fs_part_uniform(intr); |
| 115 |
} |
| 116 |
|
| 117 |
if (unif >= 0) { |
| 118 |
b->cursor = nir_instr_remove(&intr->instr); |
| 119 |
nir_def *load = nir_load_preamble(b, 1, intr->def.bit_size, .base = unif); |
| 120 |
nir_def_rewrite_uses(&intr->def, load); |
| 121 |
return true; |
| 122 |
} else if (intr->intrinsic == nir_intrinsic_load_texture_handle_agx) { |
| 123 |
b->cursor = nir_instr_remove(&intr->instr); |
| 124 |
nir_def *offs = |
| 125 |
nir_imul_imm(b, nir_u2u32(b, intr->src[0].ssa), AGX_TEXTURE_LENGTH); |
| 126 |
nir_def_rewrite_uses(&intr->def, |
| 127 |
nir_bindless_image_agx(b, offs, .desc_set = 0)); |
| 128 |
return true; |
| 129 |
} else { |
| 130 |
return false; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
bool |
| 135 |
agx_nir_lower_non_monolithic_uniforms(nir_shader *nir, unsigned nr) |
| 136 |
{ |
| 137 |
return nir_shader_intrinsics_pass(nir, lower_non_monolithic_uniforms, |
| 138 |
nir_metadata_control_flow, &nr); |
| 139 |
} |
| 140 |
|
| 141 |
static bool |
| 142 |
lower_adjacency(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 143 |
{ |
| 144 |
const struct agx_vs_prolog_key *key = data; |
| 145 |
b->cursor = nir_before_instr(&intr->instr); |
| 146 |
|
| 147 |
if (intr->intrinsic != nir_intrinsic_load_vertex_id) |
| 148 |
return false; |
| 149 |
|
| 150 |
nir_def *id = nir_load_vertex_id(b); |
| 151 |
|
| 152 |
if (key->adjacency == MESA_PRIM_LINES_ADJACENCY) { |
| 153 |
id = poly_map_to_line_adj(b, id); |
| 154 |
} else if (key->adjacency == MESA_PRIM_TRIANGLE_STRIP_ADJACENCY) { |
| 155 |
id = poly_map_to_tri_strip_adj(b, id); |
| 156 |
} else if (key->adjacency == MESA_PRIM_LINE_STRIP_ADJACENCY) { |
| 157 |
id = poly_map_to_line_strip_adj(b, id); |
| 158 |
} else if (key->adjacency == MESA_PRIM_TRIANGLES_ADJACENCY) { |
| 159 |
/* Sequence (0, 2, 4), (6, 8, 10), ... */ |
| 160 |
id = nir_imul_imm(b, id, 2); |
| 161 |
} else { |
| 162 |
UNREACHABLE("unknown"); |
| 163 |
} |
| 164 |
|
| 165 |
id = poly_nir_load_vertex_id(b, id); |
| 166 |
|
| 167 |
nir_def_replace(&intr->def, id); |
| 168 |
return true; |
| 169 |
} |
| 170 |
|
| 171 |
void |
| 172 |
agx_nir_vs_prolog(nir_builder *b, const void *key_) |
| 173 |
{ |
| 174 |
const struct agx_vs_prolog_key *key = key_; |
| 175 |
b->shader->info.stage = MESA_SHADER_VERTEX; |
| 176 |
b->shader->info.name = "VS prolog"; |
| 177 |
|
| 178 |
/* First, construct a passthrough shader reading each attribute and exporting |
| 179 |
* the value. We also need to export vertex/instance ID in their usual regs. |
| 180 |
*/ |
| 181 |
if (!key->static_vi) { |
| 182 |
unsigned i = 0; |
| 183 |
nir_def *vec = NULL; |
| 184 |
unsigned vec_idx = ~0; |
| 185 |
BITSET_FOREACH_SET(i, key->component_mask, AGX_MAX_ATTRIBS * 4) { |
| 186 |
unsigned a = i / 4; |
| 187 |
unsigned c = i % 4; |
| 188 |
|
| 189 |
if (vec_idx != a) { |
| 190 |
vec = nir_load_input(b, 4, 32, nir_imm_int(b, 0), .base = a); |
| 191 |
vec_idx = a; |
| 192 |
} |
| 193 |
|
| 194 |
nir_export_agx(b, nir_channel(b, vec, c), |
| 195 |
.base = AGX_ABI_VIN_ATTRIB(i)); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
if (!key->hw) { |
| 200 |
nir_export_agx(b, nir_channel(b, nir_load_global_invocation_id(b, 32), 0), |
| 201 |
.base = AGX_ABI_VIN_VERTEX_ID_ZERO_BASE); |
| 202 |
} |
| 203 |
|
| 204 |
nir_export_agx(b, nir_load_vertex_id(b), .base = AGX_ABI_VIN_VERTEX_ID); |
| 205 |
nir_export_agx(b, nir_load_instance_id(b), .base = AGX_ABI_VIN_INSTANCE_ID); |
| 206 |
|
| 207 |
/* Now lower the resulting program using the key */ |
| 208 |
if (!key->static_vi) { |
| 209 |
agx_nir_lower_vbo(b->shader, key->attribs, key->robustness, false); |
| 210 |
|
| 211 |
/* Clean up redundant vertex ID loads */ |
| 212 |
if (!key->hw || key->adjacency) { |
| 213 |
NIR_PASS(_, b->shader, nir_opt_cse); |
| 214 |
NIR_PASS(_, b->shader, nir_opt_dce); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if (!key->hw) { |
| 219 |
b->cursor = nir_before_impl(nir_shader_get_entrypoint(b->shader)); |
| 220 |
poly_nir_lower_sw_vs(b->shader); |
| 221 |
} else if (key->adjacency) { |
| 222 |
nir_shader_intrinsics_pass(b->shader, lower_adjacency, |
| 223 |
nir_metadata_control_flow, (void *)key); |
| 224 |
} |
| 225 |
nir_inline_sysval(b->shader, nir_intrinsic_load_index_size_poly, |
| 226 |
key->sw_index_size_B); |
| 227 |
|
| 228 |
/* Finally, lower uniforms according to our ABI */ |
| 229 |
unsigned nr = DIV_ROUND_UP(BITSET_LAST_BIT(key->component_mask), 4); |
| 230 |
agx_nir_lower_non_monolithic_uniforms(b->shader, nr); |
| 231 |
b->shader->info.io_lowered = true; |
| 232 |
} |
| 233 |
|
| 234 |
static bool |
| 235 |
gather_inputs(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 236 |
{ |
| 237 |
if (intr->intrinsic != nir_intrinsic_load_input) |
| 238 |
return false; |
| 239 |
|
| 240 |
unsigned idx = nir_src_as_uint(intr->src[0]) + nir_intrinsic_base(intr); |
| 241 |
unsigned comp = nir_intrinsic_component(intr); |
| 242 |
|
| 243 |
assert(intr->def.bit_size == 32 && "todo: push conversions up?"); |
| 244 |
unsigned base = 4 * idx + comp; |
| 245 |
|
| 246 |
b->cursor = nir_before_instr(&intr->instr); |
| 247 |
BITSET_WORD *comps_read = data; |
| 248 |
nir_component_mask_t mask = nir_def_components_read(&intr->def); |
| 249 |
|
| 250 |
u_foreach_bit(c, mask) { |
| 251 |
BITSET_SET(comps_read, base + c); |
| 252 |
} |
| 253 |
|
| 254 |
return false; |
| 255 |
} |
| 256 |
|
| 257 |
bool |
| 258 |
agx_nir_gather_vs_inputs(nir_shader *s, BITSET_WORD *attrib_components_read) |
| 259 |
{ |
| 260 |
return nir_shader_intrinsics_pass( |
| 261 |
s, gather_inputs, nir_metadata_control_flow, attrib_components_read); |
| 262 |
} |
| 263 |
|
| 264 |
static bool |
| 265 |
lower_input_to_prolog(nir_builder *b, nir_intrinsic_instr *intr, void *_data) |
| 266 |
{ |
| 267 |
if (intr->intrinsic != nir_intrinsic_load_input) |
| 268 |
return false; |
| 269 |
|
| 270 |
unsigned idx = nir_src_as_uint(intr->src[0]) + nir_intrinsic_base(intr); |
| 271 |
unsigned comp = nir_intrinsic_component(intr); |
| 272 |
|
| 273 |
assert(intr->def.bit_size == 32 && "todo: push conversions up?"); |
| 274 |
unsigned base = 4 * idx + comp; |
| 275 |
|
| 276 |
b->cursor = nir_before_instr(&intr->instr); |
| 277 |
nir_def *val = |
| 278 |
nir_load_exported_agx(b, intr->def.num_components, intr->def.bit_size, |
| 279 |
.base = AGX_ABI_VIN_ATTRIB(base)); |
| 280 |
|
| 281 |
nir_def_replace(&intr->def, val); |
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
bool |
| 286 |
agx_nir_lower_vs_input_to_prolog(nir_shader *s) |
| 287 |
{ |
| 288 |
return nir_shader_intrinsics_pass(s, lower_input_to_prolog, |
| 289 |
nir_metadata_control_flow, NULL); |
| 290 |
} |
| 291 |
|
| 292 |
static bool |
| 293 |
lower_active_samples_to_register(nir_builder *b, nir_intrinsic_instr *intr, |
| 294 |
void *data) |
| 295 |
{ |
| 296 |
if (intr->intrinsic != nir_intrinsic_load_active_samples_agx) |
| 297 |
return false; |
| 298 |
|
| 299 |
b->cursor = nir_before_instr(&intr->instr); |
| 300 |
nir_def *id = |
| 301 |
nir_load_exported_agx(b, 1, 16, .base = AGX_ABI_FIN_SAMPLE_MASK); |
| 302 |
|
| 303 |
nir_def_replace(&intr->def, id); |
| 304 |
return true; |
| 305 |
} |
| 306 |
|
| 307 |
static bool |
| 308 |
lower_tests_zs_intr(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 309 |
{ |
| 310 |
bool *value = data; |
| 311 |
if (intr->intrinsic != nir_intrinsic_load_shader_part_tests_zs_agx) |
| 312 |
return false; |
| 313 |
|
| 314 |
b->cursor = nir_instr_remove(&intr->instr); |
| 315 |
nir_def_rewrite_uses(&intr->def, nir_imm_intN_t(b, *value ? 0xFF : 0, 16)); |
| 316 |
return true; |
| 317 |
} |
| 318 |
|
| 319 |
static bool |
| 320 |
lower_tests_zs(nir_shader *s, bool value) |
| 321 |
{ |
| 322 |
if (!s->info.fs.uses_discard) |
| 323 |
return false; |
| 324 |
|
| 325 |
return nir_shader_intrinsics_pass(s, lower_tests_zs_intr, |
| 326 |
nir_metadata_control_flow, &value); |
| 327 |
} |
| 328 |
|
| 329 |
static inline bool |
| 330 |
blend_uses_2src(struct agx_blend_rt_key rt) |
| 331 |
{ |
| 332 |
assert(rt.advanced_blend == false); |
| 333 |
const struct agx_blend_standard blend = agx_unpack_blend_standard(rt.mode); |
| 334 |
|
| 335 |
enum pipe_blendfactor factors[] = { |
| 336 |
blend.rgb_src_factor, |
| 337 |
blend.rgb_dst_factor, |
| 338 |
blend.alpha_src_factor, |
| 339 |
blend.alpha_dst_factor, |
| 340 |
}; |
| 341 |
|
| 342 |
for (unsigned i = 0; i < ARRAY_SIZE(factors); ++i) { |
| 343 |
switch (factors[i]) { |
| 344 |
case PIPE_BLENDFACTOR_SRC1_COLOR: |
| 345 |
case PIPE_BLENDFACTOR_SRC1_ALPHA: |
| 346 |
case PIPE_BLENDFACTOR_INV_SRC1_COLOR: |
| 347 |
case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: |
| 348 |
return true; |
| 349 |
default: |
| 350 |
break; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
return false; |
| 355 |
} |
| 356 |
|
| 357 |
static void |
| 358 |
copy_colour(nir_builder *b, const struct agx_fs_epilog_key *key, |
| 359 |
unsigned out_rt, unsigned in_loc, bool dual_src) |
| 360 |
{ |
| 361 |
unsigned size = (key->link.size_32 & BITFIELD_BIT(in_loc)) ? 32 : 16; |
| 362 |
|
| 363 |
nir_def *value = |
| 364 |
nir_load_exported_agx(b, 4, size, .base = AGX_ABI_FOUT_COLOUR(in_loc)); |
| 365 |
|
| 366 |
if (key->link.loc0_w_1 && in_loc == 0) { |
| 367 |
value = |
| 368 |
nir_vector_insert_imm(b, value, nir_imm_floatN_t(b, 1.0, size), 3); |
| 369 |
} |
| 370 |
|
| 371 |
nir_store_output(b, value, nir_imm_int(b, 0), |
| 372 |
.io_semantics.location = FRAG_RESULT_DATA0 + out_rt, |
| 373 |
.io_semantics.dual_source_blend_index = dual_src, |
| 374 |
.src_type = nir_type_float | size); |
| 375 |
} |
| 376 |
|
| 377 |
void |
| 378 |
agx_nir_fs_epilog(nir_builder *b, const void *key_) |
| 379 |
{ |
| 380 |
const struct agx_fs_epilog_key *key = key_; |
| 381 |
b->shader->info.stage = MESA_SHADER_FRAGMENT; |
| 382 |
b->shader->info.name = "FS epilog"; |
| 383 |
|
| 384 |
/* First, construct a passthrough shader reading each colour and outputting |
| 385 |
* the value. |
| 386 |
*/ |
| 387 |
for (unsigned rt = 0; rt < ARRAY_SIZE(key->remap); ++rt) { |
| 388 |
int location = key->remap[rt]; |
| 389 |
|
| 390 |
/* Negative remaps indicate the attachment isn't written. */ |
| 391 |
if (location >= 0 && key->link.loc_written & BITFIELD_BIT(location)) { |
| 392 |
copy_colour(b, key, rt, location, false); |
| 393 |
|
| 394 |
/* If this render target uses dual source blending, also copy the dual |
| 395 |
* source colour. While the copy_colour above is needed even for |
| 396 |
* missing attachments to handle alpha-to-coverage, this copy is only |
| 397 |
* for blending so should be suppressed for missing attachments to keep |
| 398 |
* the assert from blowing up on OpenGL. |
| 399 |
*/ |
| 400 |
if (!key->blend.rt[rt].advanced_blend && |
| 401 |
blend_uses_2src(key->blend.rt[rt]) && |
| 402 |
key->rt_formats[rt] != PIPE_FORMAT_NONE) { |
| 403 |
|
| 404 |
assert(location == 0); |
| 405 |
copy_colour(b, key, rt, 1, true); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
/* Grab registers early, this has to happen in the first block. */ |
| 411 |
nir_def *sample_id = NULL, *write_samples = NULL; |
| 412 |
if (key->link.sample_shading) { |
| 413 |
sample_id = |
| 414 |
nir_load_exported_agx(b, 1, 16, .base = AGX_ABI_FOUT_SAMPLE_MASK); |
| 415 |
} |
| 416 |
|
| 417 |
if (key->link.sample_mask_after_force_early) { |
| 418 |
write_samples = |
| 419 |
nir_load_exported_agx(b, 1, 16, .base = AGX_ABI_FOUT_WRITE_SAMPLES); |
| 420 |
} |
| 421 |
|
| 422 |
/* Now lower the resulting program using the key */ |
| 423 |
struct agx_tilebuffer_layout tib = agx_build_tilebuffer_layout( |
| 424 |
key->rt_formats, ARRAY_SIZE(key->rt_formats), key->nr_samples, true); |
| 425 |
|
| 426 |
if (key->force_small_tile) |
| 427 |
tib.tile_size = 16 * 16; |
| 428 |
|
| 429 |
bool force_translucent = false; |
| 430 |
nir_lower_blend_options opts = { |
| 431 |
.scalar_blend_const = true, |
| 432 |
.logicop_enable = key->blend.logicop_enable, |
| 433 |
.logicop_func = key->blend.logicop_func, |
| 434 |
}; |
| 435 |
|
| 436 |
static_assert(ARRAY_SIZE(opts.rt) == 8, "max RTs out of sync"); |
| 437 |
|
| 438 |
for (unsigned i = 0; i < 8; ++i) { |
| 439 |
if (key->blend.rt[i].advanced_blend) { |
| 440 |
const struct agx_blend_advanced blend = |
| 441 |
agx_unpack_blend_advanced(key->blend.rt[i].mode); |
| 442 |
|
| 443 |
opts.rt[i] = (nir_lower_blend_rt){ |
| 444 |
.format = key->rt_formats[i], |
| 445 |
.advanced_blend = true, |
| 446 |
.colormask = key->blend.rt[i].colormask, |
| 447 |
.blend_mode = blend.op, |
| 448 |
.src_premultiplied = blend.src_premultiplied, |
| 449 |
.dst_premultiplied = blend.dst_premultiplied, |
| 450 |
.overlap = blend.overlap, |
| 451 |
}; |
| 452 |
} else { |
| 453 |
const struct agx_blend_standard blend = |
| 454 |
agx_unpack_blend_standard(key->blend.rt[i].mode); |
| 455 |
|
| 456 |
opts.rt[i] = (nir_lower_blend_rt){ |
| 457 |
.format = key->rt_formats[i], |
| 458 |
|
| 459 |
.rgb.src_factor = blend.rgb_src_factor, |
| 460 |
.rgb.dst_factor = blend.rgb_dst_factor, |
| 461 |
.rgb.func = blend.rgb_func, |
| 462 |
|
| 463 |
.alpha.src_factor = blend.alpha_src_factor, |
| 464 |
.alpha.dst_factor = blend.alpha_dst_factor, |
| 465 |
.alpha.func = blend.alpha_func, |
| 466 |
|
| 467 |
.colormask = key->blend.rt[i].colormask, |
| 468 |
}; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/* It's more efficient to use masked stores (with |
| 473 |
* agx_nir_lower_tilebuffer) than to emulate colour masking with |
| 474 |
* nir_lower_blend. |
| 475 |
*/ |
| 476 |
uint8_t colormasks[8] = {0}; |
| 477 |
|
| 478 |
for (unsigned i = 0; i < 8; ++i) { |
| 479 |
if (key->rt_formats[i] == PIPE_FORMAT_NONE) |
| 480 |
continue; |
| 481 |
|
| 482 |
if (agx_tilebuffer_supports_mask(&tib, i)) { |
| 483 |
colormasks[i] = key->blend.rt[i].colormask; |
| 484 |
opts.rt[i].colormask = (uint8_t)BITFIELD_MASK(4); |
| 485 |
} else { |
| 486 |
colormasks[i] = (uint8_t)BITFIELD_MASK(4); |
| 487 |
} |
| 488 |
|
| 489 |
/* If not all bound RTs are fully written to, we need to force |
| 490 |
* translucent pass type. agx_nir_lower_tilebuffer will take |
| 491 |
* care of this for its own colormasks input. |
| 492 |
*/ |
| 493 |
unsigned comps = util_format_get_nr_components(key->rt_formats[i]); |
| 494 |
if ((opts.rt[i].colormask & BITFIELD_MASK(comps)) != |
| 495 |
BITFIELD_MASK(comps)) { |
| 496 |
force_translucent = true; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
/* Alpha-to-coverage must be lowered before alpha-to-one */ |
| 501 |
if (key->blend.alpha_to_coverage) |
| 502 |
NIR_PASS(_, b->shader, nir_lower_alpha_to_coverage, false, |
| 503 |
NULL); |
| 504 |
|
| 505 |
/* Depth/stencil writes must be deferred until after all discards, |
| 506 |
* particularly alpha-to-coverage. |
| 507 |
*/ |
| 508 |
if (key->link.write_z || key->link.write_s) { |
| 509 |
nir_store_zs_agx( |
| 510 |
b, nir_imm_intN_t(b, 0xFF, 16), |
| 511 |
nir_load_exported_agx(b, 1, 32, .base = AGX_ABI_FOUT_Z), |
| 512 |
nir_load_exported_agx(b, 1, 16, .base = AGX_ABI_FOUT_S), |
| 513 |
.base = (key->link.write_z ? 1 : 0) | (key->link.write_s ? 2 : 0)); |
| 514 |
|
| 515 |
if (key->link.write_z) |
| 516 |
b->shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DEPTH); |
| 517 |
|
| 518 |
if (key->link.write_s) |
| 519 |
b->shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_STENCIL); |
| 520 |
} |
| 521 |
|
| 522 |
/* Alpha-to-one must be lowered before blending */ |
| 523 |
if (key->blend.alpha_to_one) |
| 524 |
NIR_PASS(_, b->shader, nir_lower_alpha_to_one); |
| 525 |
|
| 526 |
NIR_PASS(_, b->shader, nir_lower_blend, &opts); |
| 527 |
|
| 528 |
unsigned rt_spill = key->link.rt_spill_base; |
| 529 |
NIR_PASS(_, b->shader, agx_nir_lower_tilebuffer, &tib, colormasks, &rt_spill, |
| 530 |
write_samples, &force_translucent); |
| 531 |
NIR_PASS(_, b->shader, agx_nir_lower_texture); |
| 532 |
NIR_PASS(_, b->shader, agx_nir_lower_multisampled_image_store); |
| 533 |
|
| 534 |
/* If the API shader runs once per sample, then the epilog runs once per |
| 535 |
* sample as well, so we need to lower our code to run for a single sample. |
| 536 |
* |
| 537 |
* If the API shader runs once per pixel, then the epilog runs once per |
| 538 |
* pixel. So we run through the monolithic MSAA lowering, which wraps the |
| 539 |
* epilog in the sample loop if needed. This localizes sample shading |
| 540 |
* to the epilog, when sample shading is not used but blending is. |
| 541 |
*/ |
| 542 |
if (key->link.sample_shading) { |
| 543 |
/* Lower the resulting discards. Done in agx_nir_lower_monolithic_msaa for |
| 544 |
* the pixel shaded path. Must be done before agx_nir_lower_to_per_sample |
| 545 |
* to avoid duplicating tests. |
| 546 |
*/ |
| 547 |
if (key->blend.alpha_to_coverage) { |
| 548 |
NIR_PASS(_, b->shader, agx_nir_lower_sample_mask); |
| 549 |
} |
| 550 |
|
| 551 |
NIR_PASS(_, b->shader, agx_nir_lower_to_per_sample); |
| 552 |
NIR_PASS(_, b->shader, agx_nir_lower_fs_active_samples_to_register); |
| 553 |
|
| 554 |
/* Ensure the sample ID is preserved in register. We do this late since it |
| 555 |
* has to go in the last block, and the above passes might add control |
| 556 |
* flow when lowering. |
| 557 |
*/ |
| 558 |
b->cursor = nir_after_impl(b->impl); |
| 559 |
nir_export_agx(b, sample_id, .base = AGX_ABI_FIN_SAMPLE_MASK); |
| 560 |
} else { |
| 561 |
NIR_PASS(_, b->shader, agx_nir_lower_monolithic_msaa, key->nr_samples); |
| 562 |
} |
| 563 |
|
| 564 |
/* Finally, lower uniforms according to our ABI */ |
| 565 |
nir_shader_intrinsics_pass(b->shader, lower_non_monolithic_uniforms, |
| 566 |
nir_metadata_control_flow, NULL); |
| 567 |
|
| 568 |
/* There is no shader part after the epilog, so we're always responsible for |
| 569 |
* running our own tests, unless the fragment shader forced early tests. |
| 570 |
*/ |
| 571 |
NIR_PASS(_, b->shader, lower_tests_zs, !key->link.already_ran_zs); |
| 572 |
|
| 573 |
b->shader->info.io_lowered = true; |
| 574 |
b->shader->info.fs.uses_fbfetch_output |= force_translucent; |
| 575 |
b->shader->info.fs.uses_sample_shading = key->link.sample_shading; |
| 576 |
} |
| 577 |
|
| 578 |
struct lower_epilog_ctx { |
| 579 |
struct agx_fs_epilog_link_info *info; |
| 580 |
nir_variable *masked_samples; |
| 581 |
}; |
| 582 |
|
| 583 |
static bool |
| 584 |
lower_output_to_epilog(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 585 |
{ |
| 586 |
struct lower_epilog_ctx *ctx = data; |
| 587 |
struct agx_fs_epilog_link_info *info = ctx->info; |
| 588 |
|
| 589 |
if (intr->intrinsic == nir_intrinsic_store_zs_agx) { |
| 590 |
assert(nir_src_as_uint(intr->src[0]) == 0xff && "msaa not yet lowered"); |
| 591 |
b->cursor = nir_instr_remove(&intr->instr); |
| 592 |
|
| 593 |
unsigned base = nir_intrinsic_base(intr); |
| 594 |
info->write_z = !!(base & 1); |
| 595 |
info->write_s = !!(base & 2); |
| 596 |
|
| 597 |
if (info->write_z) |
| 598 |
nir_export_agx(b, intr->src[1].ssa, .base = AGX_ABI_FOUT_Z); |
| 599 |
|
| 600 |
if (info->write_s) |
| 601 |
nir_export_agx(b, intr->src[2].ssa, .base = AGX_ABI_FOUT_S); |
| 602 |
|
| 603 |
return true; |
| 604 |
} |
| 605 |
|
| 606 |
if (intr->intrinsic == nir_intrinsic_demote_samples && |
| 607 |
b->shader->info.fs.early_fragment_tests) { |
| 608 |
|
| 609 |
if (!ctx->masked_samples) { |
| 610 |
b->cursor = nir_before_impl(nir_shader_get_entrypoint(b->shader)); |
| 611 |
|
| 612 |
ctx->masked_samples = |
| 613 |
nir_local_variable_create(b->impl, glsl_uint16_t_type(), NULL); |
| 614 |
|
| 615 |
nir_store_var(b, ctx->masked_samples, nir_imm_intN_t(b, 0xFF, 16), |
| 616 |
nir_component_mask(1)); |
| 617 |
} |
| 618 |
|
| 619 |
b->cursor = nir_before_instr(&intr->instr); |
| 620 |
|
| 621 |
nir_def *mask = nir_load_var(b, ctx->masked_samples); |
| 622 |
nir_def *mask_2 = |
| 623 |
nir_ixor(b, intr->src[0].ssa, nir_imm_intN_t(b, 0xff, 16)); |
| 624 |
|
| 625 |
mask = nir_iand(b, mask, mask_2); |
| 626 |
nir_store_var(b, ctx->masked_samples, mask, nir_component_mask(1)); |
| 627 |
|
| 628 |
nir_instr_remove(&intr->instr); |
| 629 |
return true; |
| 630 |
} |
| 631 |
|
| 632 |
if (intr->intrinsic != nir_intrinsic_store_output) |
| 633 |
return false; |
| 634 |
|
| 635 |
nir_io_semantics sem = nir_intrinsic_io_semantics(intr); |
| 636 |
|
| 637 |
/* Fix up gl_FragColor */ |
| 638 |
if (sem.location == FRAG_RESULT_COLOR) { |
| 639 |
sem.location = FRAG_RESULT_DATA0; |
| 640 |
info->broadcast_rt0 = true; |
| 641 |
} |
| 642 |
|
| 643 |
/* We don't use the epilog for sample mask writes */ |
| 644 |
if (sem.location < FRAG_RESULT_DATA0) |
| 645 |
return false; |
| 646 |
|
| 647 |
/* Determine the ABI location. Dual source blending aliases a second |
| 648 |
* render target, so get that out of the way now. |
| 649 |
*/ |
| 650 |
unsigned loc = sem.location - FRAG_RESULT_DATA0; |
| 651 |
loc += nir_src_as_uint(intr->src[1]); |
| 652 |
|
| 653 |
if (sem.dual_source_blend_index) { |
| 654 |
assert(loc == 0); |
| 655 |
loc = 1; |
| 656 |
} |
| 657 |
|
| 658 |
b->cursor = nir_instr_remove(&intr->instr); |
| 659 |
nir_def *vec = intr->src[0].ssa; |
| 660 |
|
| 661 |
info->loc_written |= BITFIELD_BIT(loc); |
| 662 |
|
| 663 |
if (vec->bit_size == 32) |
| 664 |
info->size_32 |= BITFIELD_BIT(loc); |
| 665 |
else |
| 666 |
assert(vec->bit_size == 16); |
| 667 |
|
| 668 |
uint32_t one_f = (vec->bit_size == 32 ? fui(1.0) : _mesa_float_to_half(1.0)); |
| 669 |
unsigned comp = nir_intrinsic_component(intr); |
| 670 |
|
| 671 |
u_foreach_bit(c, nir_intrinsic_write_mask(intr)) { |
| 672 |
nir_scalar s = nir_scalar_resolved(vec, c); |
| 673 |
if (loc == 0 && c == 3 && nir_scalar_is_const(s) && |
| 674 |
nir_scalar_as_uint(s) == one_f) { |
| 675 |
|
| 676 |
info->loc0_w_1 = true; |
| 677 |
} else { |
| 678 |
unsigned stride = vec->bit_size / 16; |
| 679 |
|
| 680 |
nir_export_agx(b, nir_channel(b, vec, c), |
| 681 |
.base = AGX_ABI_FOUT_COLOUR(loc) + (comp + c) * stride); |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
return true; |
| 686 |
} |
| 687 |
|
| 688 |
bool |
| 689 |
agx_nir_lower_fs_output_to_epilog(nir_shader *s, |
| 690 |
struct agx_fs_epilog_link_info *out) |
| 691 |
{ |
| 692 |
struct lower_epilog_ctx ctx = {.info = out}; |
| 693 |
|
| 694 |
nir_shader_intrinsics_pass(s, lower_output_to_epilog, |
| 695 |
nir_metadata_control_flow, &ctx); |
| 696 |
|
| 697 |
if (ctx.masked_samples) { |
| 698 |
nir_builder b = |
| 699 |
nir_builder_at(nir_after_impl(nir_shader_get_entrypoint(s))); |
| 700 |
|
| 701 |
nir_export_agx(&b, nir_load_var(&b, ctx.masked_samples), |
| 702 |
.base = AGX_ABI_FOUT_WRITE_SAMPLES); |
| 703 |
out->sample_mask_after_force_early = true; |
| 704 |
|
| 705 |
bool progress; |
| 706 |
do { |
| 707 |
progress = false; |
| 708 |
NIR_PASS(progress, s, nir_lower_vars_to_ssa); |
| 709 |
NIR_PASS(progress, s, nir_opt_dce); |
| 710 |
} while (progress); |
| 711 |
} |
| 712 |
|
| 713 |
out->sample_shading = s->info.fs.uses_sample_shading; |
| 714 |
return true; |
| 715 |
} |
| 716 |
|
| 717 |
bool |
| 718 |
agx_nir_lower_fs_active_samples_to_register(nir_shader *s) |
| 719 |
{ |
| 720 |
return nir_shader_intrinsics_pass(s, lower_active_samples_to_register, |
| 721 |
nir_metadata_control_flow, NULL); |
| 722 |
} |
| 723 |
|
| 724 |
static bool |
| 725 |
agx_nir_lower_stats_fs(nir_shader *s) |
| 726 |
{ |
| 727 |
assert(s->info.stage == MESA_SHADER_FRAGMENT); |
| 728 |
nir_builder b_ = |
| 729 |
nir_builder_at(nir_before_impl(nir_shader_get_entrypoint(s))); |
| 730 |
nir_builder *b = &b_; |
| 731 |
|
| 732 |
nir_push_if(b, nir_inot(b, nir_load_helper_invocation(b, 1))); |
| 733 |
nir_def *samples = nir_bit_count(b, nir_load_sample_mask_in(b)); |
| 734 |
unsigned query = PIPE_STAT_QUERY_PS_INVOCATIONS; |
| 735 |
|
| 736 |
nir_def *addr = nir_load_stat_query_address_poly(b, .base = query); |
| 737 |
nir_global_atomic(b, 32, addr, samples, .atomic_op = nir_atomic_op_iadd); |
| 738 |
|
| 739 |
nir_pop_if(b, NULL); |
| 740 |
return nir_progress(true, b->impl, nir_metadata_control_flow); |
| 741 |
} |
| 742 |
|
| 743 |
void |
| 744 |
agx_nir_fs_prolog(nir_builder *b, const void *key_) |
| 745 |
{ |
| 746 |
const struct agx_fs_prolog_key *key = key_; |
| 747 |
b->shader->info.stage = MESA_SHADER_FRAGMENT; |
| 748 |
b->shader->info.name = "FS prolog"; |
| 749 |
|
| 750 |
/* First, insert code for any emulated features */ |
| 751 |
if (key->api_sample_mask != 0xff) { |
| 752 |
/* Kill samples that are NOT covered by the mask */ |
| 753 |
nir_demote_samples(b, nir_imm_intN_t(b, key->api_sample_mask ^ 0xff, 16)); |
| 754 |
b->shader->info.fs.uses_discard = true; |
| 755 |
} |
| 756 |
|
| 757 |
if (key->statistics) { |
| 758 |
NIR_PASS(_, b->shader, agx_nir_lower_stats_fs); |
| 759 |
} |
| 760 |
|
| 761 |
if (key->cull_distance_size) { |
| 762 |
NIR_PASS(_, b->shader, agx_nir_lower_cull_distance_fs, |
| 763 |
key->cull_distance_size); |
| 764 |
} |
| 765 |
|
| 766 |
if (key->polygon_stipple) { |
| 767 |
NIR_PASS(_, b->shader, agx_nir_lower_poly_stipple); |
| 768 |
} |
| 769 |
|
| 770 |
/* Then, lower the prolog */ |
| 771 |
NIR_PASS(_, b->shader, agx_nir_lower_discard_zs_emit); |
| 772 |
NIR_PASS(_, b->shader, agx_nir_lower_sample_mask); |
| 773 |
NIR_PASS(_, b->shader, nir_shader_intrinsics_pass, |
| 774 |
lower_non_monolithic_uniforms, nir_metadata_control_flow, NULL); |
| 775 |
NIR_PASS(_, b->shader, lower_tests_zs, key->run_zs_tests); |
| 776 |
|
| 777 |
b->shader->info.io_lowered = true; |
| 778 |
} |