| 1 |
/* |
| 2 |
* Copyright 2022 Alyssa Rosenzweig |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "agx_bg_eot.h" |
| 7 |
#include "util/simple_mtx.h" |
| 8 |
#include "util/u_debug.h" |
| 9 |
#include "agx_compile.h" |
| 10 |
#include "agx_device.h" |
| 11 |
#include "agx_nir.h" |
| 12 |
#include "agx_nir_texture.h" |
| 13 |
#include "agx_tilebuffer.h" |
| 14 |
#include "agx_usc.h" |
| 15 |
#include "libagx_shaders.h" |
| 16 |
#include "nir.h" |
| 17 |
#include "nir_builder.h" |
| 18 |
#include "pool.h" |
| 19 |
|
| 20 |
static bool |
| 21 |
lower_tex_handle_to_u0(nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 22 |
{ |
| 23 |
if (intr->intrinsic != nir_intrinsic_load_texture_handle_agx) |
| 24 |
return false; |
| 25 |
|
| 26 |
b->cursor = nir_instr_remove(&intr->instr); |
| 27 |
nir_def_rewrite_uses( |
| 28 |
&intr->def, nir_bindless_image_agx( |
| 29 |
b, nir_imul_imm(b, intr->src[0].ssa, 24), .desc_set = 0)); |
| 30 |
|
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
static struct agx_bg_eot_shader * |
| 35 |
agx_compile_bg_eot_shader(struct agx_bg_eot_cache *cache, nir_shader *shader, |
| 36 |
struct agx_shader_key *key, |
| 37 |
struct agx_tilebuffer_layout *tib) |
| 38 |
{ |
| 39 |
agx_nir_lower_texture(shader); |
| 40 |
agx_preprocess_nir(shader); |
| 41 |
if (tib) { |
| 42 |
unsigned bindless_base = 0; |
| 43 |
NIR_PASS(_, shader, agx_nir_lower_tilebuffer, tib, NULL, &bindless_base, |
| 44 |
NULL, NULL); |
| 45 |
NIR_PASS(_, shader, agx_nir_lower_monolithic_msaa, tib->nr_samples); |
| 46 |
NIR_PASS(_, shader, agx_nir_lower_multisampled_image_store); |
| 47 |
NIR_PASS(_, shader, agx_nir_lower_texture); |
| 48 |
NIR_PASS(_, shader, nir_shader_intrinsics_pass, lower_tex_handle_to_u0, |
| 49 |
nir_metadata_control_flow, NULL); |
| 50 |
} |
| 51 |
|
| 52 |
struct agx_bg_eot_shader *res = rzalloc(cache->ht, struct agx_bg_eot_shader); |
| 53 |
struct agx_shader_part bin; |
| 54 |
agx_compile_shader_nir(shader, key, &bin); |
| 55 |
|
| 56 |
res->info = bin.info; |
| 57 |
res->ptr = agx_pool_upload_aligned_with_bo( |
| 58 |
&cache->pool, bin.binary, bin.info.binary_size, 128, &res->bo); |
| 59 |
free(bin.binary); |
| 60 |
ralloc_free(shader); |
| 61 |
|
| 62 |
return res; |
| 63 |
} |
| 64 |
|
| 65 |
static nir_def * |
| 66 |
build_background_op(nir_builder *b, enum agx_bg_eot_op op, unsigned rt, |
| 67 |
unsigned nr, bool msaa, bool layered) |
| 68 |
{ |
| 69 |
if (op == AGX_BG_LOAD) { |
| 70 |
nir_def *coord = nir_load_pixel_coord(b); |
| 71 |
|
| 72 |
/* MSAA/array lowerings assume 32-bit coordinates, could be lifted but |
| 73 |
* that's going to need more work. |
| 74 |
*/ |
| 75 |
if (layered || msaa) |
| 76 |
coord = nir_u2u32(b, coord); |
| 77 |
|
| 78 |
if (layered) { |
| 79 |
coord = nir_vec3(b, nir_channel(b, coord, 0), nir_channel(b, coord, 1), |
| 80 |
nir_load_layer_id(b)); |
| 81 |
} |
| 82 |
|
| 83 |
b->shader->info.fs.uses_sample_shading |= msaa; |
| 84 |
|
| 85 |
nir_def *tex = nir_txf( |
| 86 |
b, coord, .ms_index = msaa ? nir_load_sample_id(b) : NULL, |
| 87 |
.texture_index = rt * 2, |
| 88 |
.dim = msaa ? GLSL_SAMPLER_DIM_MS : GLSL_SAMPLER_DIM_2D, |
| 89 |
.is_array = layered, |
| 90 |
|
| 91 |
/* The type doesn't matter as long as it matches the store */ |
| 92 |
.dest_type = nir_type_uint32, |
| 93 |
|
| 94 |
/* Layer is necessarily already in-bounds so we do not want the |
| 95 |
* compiler to clamp it, which would require reading the descriptor |
| 96 |
*/ |
| 97 |
.backend_flags = AGX_TEXTURE_FLAG_NO_CLAMP); |
| 98 |
|
| 99 |
return nir_trim_vector(b, tex, nr); |
| 100 |
} else { |
| 101 |
assert(op == AGX_BG_CLEAR); |
| 102 |
|
| 103 |
return nir_load_preamble(b, nr, 32, 4 + (rt * 8)); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
static struct agx_bg_eot_shader * |
| 108 |
agx_build_background_shader(struct agx_bg_eot_cache *cache, |
| 109 |
struct agx_bg_eot_key *key) |
| 110 |
{ |
| 111 |
nir_builder b = nir_builder_init_simple_shader( |
| 112 |
MESA_SHADER_FRAGMENT, &agx_nir_options, "agx_background"); |
| 113 |
b.shader->info.fs.untyped_color_outputs = true; |
| 114 |
|
| 115 |
struct agx_shader_key compiler_key = { |
| 116 |
.fs.ignore_tib_dependencies = true, |
| 117 |
.reserved_preamble = key->reserved_preamble, |
| 118 |
}; |
| 119 |
|
| 120 |
for (unsigned rt = 0; rt < ARRAY_SIZE(key->op); ++rt) { |
| 121 |
if (key->op[rt] == AGX_BG_EOT_NONE) |
| 122 |
continue; |
| 123 |
|
| 124 |
unsigned nr = util_format_get_nr_components(key->tib.logical_format[rt]); |
| 125 |
bool msaa = key->tib.nr_samples > 1; |
| 126 |
bool layered = key->tib.layered; |
| 127 |
assert(nr > 0); |
| 128 |
|
| 129 |
nir_store_output( |
| 130 |
&b, build_background_op(&b, key->op[rt], rt, nr, msaa, layered), |
| 131 |
nir_imm_int(&b, 0), .write_mask = BITFIELD_MASK(nr), |
| 132 |
.src_type = nir_type_uint32, |
| 133 |
.io_semantics.location = FRAG_RESULT_DATA0 + rt, |
| 134 |
.io_semantics.num_slots = 1); |
| 135 |
|
| 136 |
b.shader->info.outputs_written |= BITFIELD64_BIT(FRAG_RESULT_DATA0 + rt); |
| 137 |
} |
| 138 |
|
| 139 |
return agx_compile_bg_eot_shader(cache, b.shader, &compiler_key, &key->tib); |
| 140 |
} |
| 141 |
|
| 142 |
static struct agx_bg_eot_shader * |
| 143 |
agx_build_end_of_tile_shader(struct agx_bg_eot_cache *cache, |
| 144 |
struct agx_bg_eot_key *key) |
| 145 |
{ |
| 146 |
nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, |
| 147 |
&agx_nir_options, "agx_eot"); |
| 148 |
|
| 149 |
enum glsl_sampler_dim dim = |
| 150 |
(key->tib.nr_samples > 1) ? GLSL_SAMPLER_DIM_MS : GLSL_SAMPLER_DIM_2D; |
| 151 |
|
| 152 |
for (unsigned rt = 0; rt < ARRAY_SIZE(key->op); ++rt) { |
| 153 |
if (key->op[rt] == AGX_BG_EOT_NONE) |
| 154 |
continue; |
| 155 |
|
| 156 |
/* The end-of-tile shader is unsuitable to handle spilled render targets. |
| 157 |
* Skip them. If blits are needed with spilled render targets, other parts |
| 158 |
* of the driver need to implement them. |
| 159 |
*/ |
| 160 |
if (key->tib.spilled[rt]) |
| 161 |
continue; |
| 162 |
|
| 163 |
assert(key->op[rt] == AGX_EOT_STORE); |
| 164 |
unsigned offset_B = agx_tilebuffer_offset_B(&key->tib, rt); |
| 165 |
|
| 166 |
nir_def *layer = nir_undef(&b, 1, 16); |
| 167 |
if (key->tib.layered) |
| 168 |
layer = nir_u2u16(&b, nir_load_layer_id(&b)); |
| 169 |
|
| 170 |
nir_image_store_block_agx( |
| 171 |
&b, nir_imm_intN_t(&b, rt, 16), nir_imm_int(&b, offset_B), layer, |
| 172 |
.format = agx_tilebuffer_physical_format(&key->tib, rt), |
| 173 |
.image_dim = dim, .image_array = key->tib.layered); |
| 174 |
} |
| 175 |
|
| 176 |
struct agx_shader_key compiler_key = { |
| 177 |
.reserved_preamble = key->reserved_preamble, |
| 178 |
}; |
| 179 |
|
| 180 |
return agx_compile_bg_eot_shader(cache, b.shader, &compiler_key, NULL); |
| 181 |
} |
| 182 |
|
| 183 |
struct agx_bg_eot_shader * |
| 184 |
agx_get_bg_eot_shader(struct agx_bg_eot_cache *cache, |
| 185 |
struct agx_bg_eot_key *key) |
| 186 |
{ |
| 187 |
struct hash_entry *ent = _mesa_hash_table_search(cache->ht, key); |
| 188 |
if (ent) |
| 189 |
return ent->data; |
| 190 |
|
| 191 |
struct agx_bg_eot_shader *ret = NULL; |
| 192 |
|
| 193 |
for (unsigned rt = 0; rt < ARRAY_SIZE(key->op); ++rt) { |
| 194 |
if (key->op[rt] == AGX_EOT_STORE) { |
| 195 |
ret = agx_build_end_of_tile_shader(cache, key); |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
if (!ret) |
| 201 |
ret = agx_build_background_shader(cache, key); |
| 202 |
|
| 203 |
ret->key = *key; |
| 204 |
_mesa_hash_table_insert(cache->ht, &ret->key, ret); |
| 205 |
return ret; |
| 206 |
} |
| 207 |
|
| 208 |
DERIVE_HASH_TABLE(agx_bg_eot_key); |
| 209 |
|
| 210 |
void |
| 211 |
agx_bg_eot_init(struct agx_bg_eot_cache *cache, struct agx_device *dev) |
| 212 |
{ |
| 213 |
agx_pool_init(&cache->pool, dev, "Internal programs", |
| 214 |
AGX_BO_EXEC | AGX_BO_LOW_VA, true); |
| 215 |
simple_mtx_init(&cache->lock, mtx_plain); |
| 216 |
cache->ht = agx_bg_eot_key_table_create(NULL); |
| 217 |
cache->dev = dev; |
| 218 |
} |
| 219 |
|
| 220 |
void |
| 221 |
agx_bg_eot_cleanup(struct agx_bg_eot_cache *cache) |
| 222 |
{ |
| 223 |
agx_pool_cleanup(&cache->pool); |
| 224 |
_mesa_hash_table_destroy(cache->ht, NULL); |
| 225 |
simple_mtx_destroy(&cache->lock); |
| 226 |
cache->ht = NULL; |
| 227 |
cache->dev = NULL; |
| 228 |
} |
| 229 |
|
| 230 |
static struct agx_precompiled_shader * |
| 231 |
agx_get_precompiled_locked(struct agx_bg_eot_cache *cache, unsigned program) |
| 232 |
{ |
| 233 |
simple_mtx_assert_locked(&cache->lock); |
| 234 |
|
| 235 |
/* It is possible that, while waiting for the lock, another thread uploaded |
| 236 |
* the shader. Check for that so we don't double-upload. |
| 237 |
*/ |
| 238 |
if (cache->precomp[program]) |
| 239 |
return cache->precomp[program]; |
| 240 |
|
| 241 |
/* Otherwise, we need to upload. */ |
| 242 |
struct agx_precompiled_shader *p = |
| 243 |
ralloc(cache->ht, struct agx_precompiled_shader); |
| 244 |
|
| 245 |
const uint32_t *bin = cache->dev->libagx_programs[program]; |
| 246 |
const struct agx_precompiled_kernel_info *info = (void *)bin; |
| 247 |
const void *binary = (const uint8_t *)bin + sizeof(*info); |
| 248 |
|
| 249 |
assert(info->main_offset == 0 || program != LIBAGX_HELPER); |
| 250 |
|
| 251 |
p->b.workgroup = |
| 252 |
agx_workgroup(info->workgroup_size[0], info->workgroup_size[1], |
| 253 |
info->workgroup_size[2]); |
| 254 |
|
| 255 |
p->ptr = agx_pool_upload_aligned_with_bo(&cache->pool, binary, |
| 256 |
info->binary_size, 128, &p->bo); |
| 257 |
|
| 258 |
/* Bake launch */ |
| 259 |
agx_pack(&p->b.launch, CDM_LAUNCH_WORD_0, cfg) { |
| 260 |
cfg.texture_state_register_count = 8 /* same encoding as 0 */; |
| 261 |
cfg.sampler_state_register_count = 1; |
| 262 |
cfg.uniform_register_count = info->push_count; |
| 263 |
cfg.preshader_register_count = info->nr_preamble_gprs; |
| 264 |
} |
| 265 |
|
| 266 |
/* Bake USC */ |
| 267 |
struct agx_usc_builder b = |
| 268 |
agx_usc_builder(p->b.usc.data, sizeof(p->b.usc.data)); |
| 269 |
|
| 270 |
agx_usc_immediates(&b, &info->rodata, p->ptr); |
| 271 |
|
| 272 |
if (info->uses_txf) |
| 273 |
agx_usc_push_packed(&b, SAMPLER, cache->dev->txf_sampler); |
| 274 |
|
| 275 |
agx_usc_shared(&b, info->local_size, info->imageblock_stride, 0); |
| 276 |
|
| 277 |
agx_usc_pack(&b, SHADER, cfg) { |
| 278 |
cfg.code = agx_usc_addr(cache->dev, p->ptr + info->main_offset); |
| 279 |
cfg.unk_2 = 3; |
| 280 |
} |
| 281 |
|
| 282 |
agx_usc_pack(&b, REGISTERS, cfg) { |
| 283 |
cfg.register_count = info->nr_gprs; |
| 284 |
cfg.spill_size = 0; |
| 285 |
} |
| 286 |
|
| 287 |
if (info->nr_preamble_gprs) { |
| 288 |
agx_usc_pack(&b, PRESHADER, cfg) { |
| 289 |
cfg.code = agx_usc_addr(cache->dev, p->ptr + info->preamble_offset); |
| 290 |
} |
| 291 |
} else { |
| 292 |
agx_usc_pack(&b, NO_PRESHADER, cfg) |
| 293 |
; |
| 294 |
} |
| 295 |
|
| 296 |
p->b.usc.size = b.head - p->b.usc.data; |
| 297 |
|
| 298 |
/* We must only write to the cache once we are done compiling, since other |
| 299 |
* threads may be reading the cache concurrently. Do this last. |
| 300 |
*/ |
| 301 |
p_atomic_set(&cache->precomp[program], p); |
| 302 |
return p; |
| 303 |
} |
| 304 |
|
| 305 |
struct agx_precompiled_shader * |
| 306 |
agx_get_precompiled(struct agx_bg_eot_cache *cache, unsigned program) |
| 307 |
{ |
| 308 |
/* Shaders are immutable once written, so if we atomically read a non-NULL |
| 309 |
* shader, then we have a valid cached shader and are done. |
| 310 |
*/ |
| 311 |
struct agx_precompiled_shader *ret = p_atomic_read(cache->precomp + program); |
| 312 |
|
| 313 |
if (ret != NULL) |
| 314 |
return ret; |
| 315 |
|
| 316 |
/* Otherwise, take the lock and upload. */ |
| 317 |
simple_mtx_lock(&cache->lock); |
| 318 |
ret = agx_get_precompiled_locked(cache, program); |
| 319 |
simple_mtx_unlock(&cache->lock); |
| 320 |
|
| 321 |
return ret; |
| 322 |
} |
| 323 |
|
| 324 |
uint64_t |
| 325 |
agx_helper_program(struct agx_bg_eot_cache *cache) |
| 326 |
{ |
| 327 |
struct agx_precompiled_shader *pc = |
| 328 |
agx_get_precompiled(cache, LIBAGX_HELPER); |
| 329 |
|
| 330 |
return pc->ptr | 1; |
| 331 |
} |