| 1 |
/* |
| 2 |
* Copyright 2023 Valve Corporation |
| 3 |
* Copyright 2021 Alyssa Rosenzweig |
| 4 |
* Copyright 2020 Collabora Ltd. |
| 5 |
* Copyright 2016 Broadcom |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "compiler/nir/nir.h" |
| 10 |
#include "compiler/nir/nir_builder.h" |
| 11 |
#include "agx_nir.h" |
| 12 |
#include "agx_nir_texture.h" |
| 13 |
#include "glsl_types.h" |
| 14 |
#include "libagx.h" |
| 15 |
#include "nir_builder_opcodes.h" |
| 16 |
#include "nir_builtin_builder.h" |
| 17 |
#include "nir_intrinsics.h" |
| 18 |
#include "nir_intrinsics_indices.h" |
| 19 |
#include "shader_enums.h" |
| 20 |
|
| 21 |
/* Residency flags are inverted from NIR */ |
| 22 |
#define AGX_RESIDENT (0) |
| 23 |
|
| 24 |
static bool |
| 25 |
fence_image(struct nir_builder *b, nir_intrinsic_instr *intr, void *data) |
| 26 |
{ |
| 27 |
b->cursor = nir_after_instr(&intr->instr); |
| 28 |
|
| 29 |
/* If the image is write-only, there is no fencing needed */ |
| 30 |
if (nir_intrinsic_has_access(intr) && |
| 31 |
(nir_intrinsic_access(intr) & ACCESS_NON_READABLE)) { |
| 32 |
return false; |
| 33 |
} |
| 34 |
|
| 35 |
switch (intr->intrinsic) { |
| 36 |
case nir_intrinsic_image_store: |
| 37 |
case nir_intrinsic_bindless_image_store: |
| 38 |
nir_fence_pbe_to_tex_agx(b); |
| 39 |
return true; |
| 40 |
|
| 41 |
case nir_intrinsic_image_atomic: |
| 42 |
case nir_intrinsic_bindless_image_atomic: |
| 43 |
case nir_intrinsic_image_atomic_swap: |
| 44 |
case nir_intrinsic_bindless_image_atomic_swap: |
| 45 |
nir_fence_mem_to_tex_agx(b); |
| 46 |
return true; |
| 47 |
|
| 48 |
default: |
| 49 |
return false; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static nir_def * |
| 54 |
texture_descriptor_ptr(nir_builder *b, nir_tex_instr *tex) |
| 55 |
{ |
| 56 |
int handle_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_handle); |
| 57 |
assert(handle_idx >= 0 && "must be bindless"); |
| 58 |
return nir_load_from_texture_handle_agx(b, tex->src[handle_idx].src.ssa); |
| 59 |
} |
| 60 |
|
| 61 |
static bool |
| 62 |
has_nonzero_lod(nir_tex_instr *tex) |
| 63 |
{ |
| 64 |
int idx = nir_tex_instr_src_index(tex, nir_tex_src_lod); |
| 65 |
if (idx < 0) |
| 66 |
return false; |
| 67 |
|
| 68 |
nir_src src = tex->src[idx].src; |
| 69 |
return !(nir_src_is_const(src) && nir_src_as_uint(src) == 0); |
| 70 |
} |
| 71 |
|
| 72 |
static bool |
| 73 |
lower_tex_crawl(nir_builder *b, nir_instr *instr, UNUSED void *data) |
| 74 |
{ |
| 75 |
if (instr->type != nir_instr_type_tex) |
| 76 |
return false; |
| 77 |
|
| 78 |
nir_tex_instr *tex = nir_instr_as_tex(instr); |
| 79 |
b->cursor = nir_before_instr(instr); |
| 80 |
|
| 81 |
if (tex->op != nir_texop_txs && tex->op != nir_texop_texture_samples && |
| 82 |
tex->op != nir_texop_query_levels) |
| 83 |
return false; |
| 84 |
|
| 85 |
nir_def *ptr = texture_descriptor_ptr(b, tex); |
| 86 |
unsigned nr_comps = tex->def.num_components; |
| 87 |
assert(nr_comps <= 3); |
| 88 |
|
| 89 |
int lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_lod); |
| 90 |
nir_def *lod = lod_idx >= 0 ? nir_u2u16(b, tex->src[lod_idx].src.ssa) |
| 91 |
: nir_imm_intN_t(b, 0, 16); |
| 92 |
|
| 93 |
nir_def *res; |
| 94 |
if (tex->op == nir_texop_txs) { |
| 95 |
res = |
| 96 |
libagx_txs(b, ptr, lod, nir_imm_int(b, nr_comps), |
| 97 |
nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_BUF), |
| 98 |
nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_1D), |
| 99 |
nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_2D), |
| 100 |
nir_imm_bool(b, tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE), |
| 101 |
nir_imm_bool(b, tex->is_array)); |
| 102 |
} else if (tex->op == nir_texop_query_levels) { |
| 103 |
res = libagx_texture_levels(b, ptr); |
| 104 |
} else { |
| 105 |
res = libagx_texture_samples(b, ptr); |
| 106 |
} |
| 107 |
|
| 108 |
nir_def_rewrite_uses(&tex->def, nir_trim_vector(b, res, nr_comps)); |
| 109 |
nir_instr_remove(instr); |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* Given a 1D buffer texture coordinate, calculate the 2D coordinate vector that |
| 115 |
* will be used to access the linear 2D texture bound to the buffer. |
| 116 |
*/ |
| 117 |
static nir_def * |
| 118 |
coords_for_buffer_texture(nir_builder *b, nir_def *coord) |
| 119 |
{ |
| 120 |
return nir_vec2(b, nir_umod_imm(b, coord, AGX_TEXTURE_BUFFER_WIDTH), |
| 121 |
nir_udiv_imm(b, coord, AGX_TEXTURE_BUFFER_WIDTH)); |
| 122 |
} |
| 123 |
|
| 124 |
/* |
| 125 |
* Buffer textures are lowered to 2D (1024xN) textures in the driver to access |
| 126 |
* more storage. When lowering, we need to fix up the coordinate accordingly. |
| 127 |
* |
| 128 |
* Furthermore, RGB32 formats are emulated by lowering to global memory access, |
| 129 |
* so to read a buffer texture we generate code that looks like: |
| 130 |
* |
| 131 |
* if (descriptor->format == RGB32) |
| 132 |
* return ((uint32_t *) descriptor->address)[x]; |
| 133 |
* else |
| 134 |
* return txf(texture_as_2d, vec2(x % 1024, x / 1024)); |
| 135 |
*/ |
| 136 |
static bool |
| 137 |
lower_buffer_texture(nir_builder *b, nir_tex_instr *tex) |
| 138 |
{ |
| 139 |
nir_def *coord = nir_steal_tex_src(tex, nir_tex_src_coord); |
| 140 |
nir_def *size = nir_get_texture_size(b, tex); |
| 141 |
nir_def *oob = nir_uge(b, coord, size); |
| 142 |
|
| 143 |
/* Apply the buffer offset after calculating oob but before remapping */ |
| 144 |
nir_def *desc = texture_descriptor_ptr(b, tex); |
| 145 |
coord = libagx_buffer_texture_offset(b, desc, coord); |
| 146 |
|
| 147 |
/* Map out-of-bounds indices to out-of-bounds coordinates for robustness2 |
| 148 |
* semantics from the hardware. |
| 149 |
*/ |
| 150 |
coord = nir_bcsel(b, oob, nir_imm_int(b, -1), coord); |
| 151 |
|
| 152 |
bool is_float = nir_alu_type_get_base_type(tex->dest_type) == nir_type_float; |
| 153 |
|
| 154 |
/* Lower RGB32 reads if the format requires. If we are out-of-bounds, we use |
| 155 |
* the hardware path so we get a zero texel. |
| 156 |
*/ |
| 157 |
nir_if *nif = nir_push_if( |
| 158 |
b, nir_iand(b, libagx_texture_is_rgb32(b, desc), nir_inot(b, oob))); |
| 159 |
|
| 160 |
nir_def *rgb32 = nir_trim_vector( |
| 161 |
b, libagx_texture_load_rgb32(b, desc, coord, nir_imm_bool(b, is_float)), |
| 162 |
nir_tex_instr_result_size(tex)); |
| 163 |
|
| 164 |
/* Raw loads do not return residency information, but residency queries are |
| 165 |
* supported on buffer textures. Fortunately, we do not need to support |
| 166 |
* sparse RGB32 buffers, so we simply claim all RGB32 loads were resident. |
| 167 |
* Nothing should hit this in practice, but if we don't do *something* here |
| 168 |
* we'll get vector size mismatches which blow up in vkd3d-proton. |
| 169 |
*/ |
| 170 |
if (tex->is_sparse) { |
| 171 |
rgb32 = nir_pad_vector_imm_int(b, rgb32, AGX_RESIDENT, |
| 172 |
rgb32->num_components + 1); |
| 173 |
} |
| 174 |
|
| 175 |
nir_push_else(b, nif); |
| 176 |
|
| 177 |
/* Otherwise, lower the texture instruction to read from 2D */ |
| 178 |
assert(coord->num_components == 1 && "buffer textures are 1D"); |
| 179 |
tex->sampler_dim = GLSL_SAMPLER_DIM_2D; |
| 180 |
|
| 181 |
nir_def *coord2d = coords_for_buffer_texture(b, coord); |
| 182 |
nir_instr_remove(&tex->instr); |
| 183 |
nir_builder_instr_insert(b, &tex->instr); |
| 184 |
nir_tex_instr_add_src(tex, nir_tex_src_backend1, coord2d); |
| 185 |
nir_steal_tex_src(tex, nir_tex_src_sampler_handle); |
| 186 |
nir_steal_tex_src(tex, nir_tex_src_sampler_offset); |
| 187 |
nir_block *else_block = nir_cursor_current_block(b->cursor); |
| 188 |
nir_pop_if(b, nif); |
| 189 |
|
| 190 |
/* Put it together with a phi */ |
| 191 |
nir_def *phi = nir_if_phi(b, rgb32, &tex->def); |
| 192 |
nir_def_rewrite_uses(&tex->def, phi); |
| 193 |
nir_phi_instr *phi_instr = nir_def_as_phi(phi); |
| 194 |
nir_phi_src *else_src = nir_phi_get_src_from_block(phi_instr, else_block); |
| 195 |
nir_src_rewrite(&else_src->src, &tex->def); |
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
/* |
| 200 |
* NIR indexes into array textures with unclamped floats (integer for txf). AGX |
| 201 |
* requires the index to be a clamped integer. Lower tex_src_coord into |
| 202 |
* tex_src_backend1 for array textures by type-converting and clamping. |
| 203 |
*/ |
| 204 |
static bool |
| 205 |
lower_regular_texture(nir_builder *b, nir_instr *instr, UNUSED void *data) |
| 206 |
{ |
| 207 |
if (instr->type != nir_instr_type_tex) |
| 208 |
return false; |
| 209 |
|
| 210 |
nir_tex_instr *tex = nir_instr_as_tex(instr); |
| 211 |
b->cursor = nir_before_instr(instr); |
| 212 |
|
| 213 |
if (nir_tex_instr_is_query(tex) && tex->op != nir_texop_lod) |
| 214 |
return false; |
| 215 |
|
| 216 |
if (tex->sampler_dim == GLSL_SAMPLER_DIM_BUF) |
| 217 |
return lower_buffer_texture(b, tex); |
| 218 |
|
| 219 |
/* Don't lower twice */ |
| 220 |
if (nir_tex_instr_src_index(tex, nir_tex_src_backend1) >= 0) |
| 221 |
return false; |
| 222 |
|
| 223 |
/* Get the coordinates */ |
| 224 |
nir_def *coord = nir_steal_tex_src(tex, nir_tex_src_coord); |
| 225 |
nir_def *ms_idx = nir_steal_tex_src(tex, nir_tex_src_ms_index); |
| 226 |
|
| 227 |
/* Apply txf workaround, see libagx_lower_txf_robustness */ |
| 228 |
bool is_txf = ((tex->op == nir_texop_txf) || (tex->op == nir_texop_txf_ms)); |
| 229 |
|
| 230 |
if (is_txf && |
| 231 |
(has_nonzero_lod(tex) || tex->is_array || |
| 232 |
nir_tex_instr_src_index(tex, nir_tex_src_min_lod) >= 0) && |
| 233 |
!(tex->backend_flags & AGX_TEXTURE_FLAG_NO_CLAMP)) { |
| 234 |
|
| 235 |
int lod_idx = nir_tex_instr_src_index(tex, nir_tex_src_lod); |
| 236 |
nir_def *lod = |
| 237 |
lod_idx >= 0 ? tex->src[lod_idx].src.ssa : nir_undef(b, 1, 16); |
| 238 |
|
| 239 |
nir_def *min_lod = nir_steal_tex_src(tex, nir_tex_src_min_lod); |
| 240 |
|
| 241 |
unsigned lidx = coord->num_components - 1; |
| 242 |
nir_def *layer = nir_channel(b, coord, lidx); |
| 243 |
|
| 244 |
nir_def *replaced = libagx_lower_txf_robustness( |
| 245 |
b, texture_descriptor_ptr(b, tex), |
| 246 |
nir_imm_bool(b, has_nonzero_lod(tex)), lod, |
| 247 |
nir_imm_bool(b, min_lod != NULL), min_lod ?: nir_undef(b, 1, 16), |
| 248 |
nir_imm_bool(b, tex->is_array), layer, nir_channel(b, coord, 0)); |
| 249 |
|
| 250 |
coord = nir_vector_insert_imm(b, coord, replaced, 0); |
| 251 |
} |
| 252 |
|
| 253 |
/* The layer is always the last component of the NIR coordinate, split it off |
| 254 |
* because we'll need to swizzle. |
| 255 |
*/ |
| 256 |
nir_def *layer = NULL; |
| 257 |
|
| 258 |
if (tex->is_array && tex->op != nir_texop_lod) { |
| 259 |
unsigned lidx = coord->num_components - 1; |
| 260 |
nir_def *unclamped_layer = nir_channel(b, coord, lidx); |
| 261 |
coord = nir_trim_vector(b, coord, lidx); |
| 262 |
|
| 263 |
/* Round layer to nearest even */ |
| 264 |
if (!is_txf) { |
| 265 |
unclamped_layer = nir_fround_even(b, unclamped_layer); |
| 266 |
|
| 267 |
/* Explicitly round negative to avoid undefined behaviour when constant |
| 268 |
* folding. This is load bearing on x86 builds. |
| 269 |
*/ |
| 270 |
unclamped_layer = |
| 271 |
nir_f2u32(b, nir_fmax(b, unclamped_layer, nir_imm_float(b, 0.0f))); |
| 272 |
} |
| 273 |
|
| 274 |
/* For a cube array, the layer is zero-indexed component 3 of the |
| 275 |
* coordinate but the number of layers is component 2 of the txs result. |
| 276 |
*/ |
| 277 |
if (tex->sampler_dim == GLSL_SAMPLER_DIM_CUBE) { |
| 278 |
assert(lidx == 3 && "4 components"); |
| 279 |
lidx = 2; |
| 280 |
} |
| 281 |
|
| 282 |
/* Clamp to max layer = (# of layers - 1) for out-of-bounds handling. |
| 283 |
* Layer must be 16-bits for the hardware, drop top bits after clamping. |
| 284 |
* |
| 285 |
* For txf, we drop out-of-bounds components rather than clamp, see the |
| 286 |
* above txf robustness workaround. |
| 287 |
*/ |
| 288 |
if (!(tex->backend_flags & AGX_TEXTURE_FLAG_NO_CLAMP) && !is_txf) { |
| 289 |
nir_def *txs = nir_get_texture_size(b, tex); |
| 290 |
nir_def *nr_layers = nir_channel(b, txs, lidx); |
| 291 |
nir_def *max_layer = nir_iadd_imm(b, nr_layers, -1); |
| 292 |
layer = nir_umin(b, unclamped_layer, max_layer); |
| 293 |
} else { |
| 294 |
layer = unclamped_layer; |
| 295 |
} |
| 296 |
|
| 297 |
layer = nir_u2u16(b, layer); |
| 298 |
} |
| 299 |
|
| 300 |
/* Combine layer and multisample index into 32-bit so we don't need a vec5 or |
| 301 |
* vec6 16-bit coordinate tuple, which would be inconvenient in NIR for |
| 302 |
* little benefit (a minor optimization, I guess). |
| 303 |
*/ |
| 304 |
nir_def *sample_array = (ms_idx && layer) |
| 305 |
? nir_pack_32_2x16_split(b, ms_idx, layer) |
| 306 |
: ms_idx ? nir_u2u32(b, ms_idx) |
| 307 |
: layer ? nir_u2u32(b, layer) |
| 308 |
: NULL; |
| 309 |
|
| 310 |
/* Combine into the final 32-bit tuple */ |
| 311 |
if (sample_array != NULL) { |
| 312 |
unsigned end = coord->num_components; |
| 313 |
coord = nir_pad_vector(b, coord, end + 1); |
| 314 |
coord = nir_vector_insert_imm(b, coord, sample_array, end); |
| 315 |
} |
| 316 |
|
| 317 |
nir_tex_instr_add_src(tex, nir_tex_src_backend1, coord); |
| 318 |
|
| 319 |
/* Furthermore, if there is an offset vector, it must be packed */ |
| 320 |
nir_def *offset = nir_steal_tex_src(tex, nir_tex_src_offset); |
| 321 |
|
| 322 |
if (offset != NULL) { |
| 323 |
nir_def *packed = NULL; |
| 324 |
|
| 325 |
for (unsigned c = 0; c < offset->num_components; ++c) { |
| 326 |
nir_def *nibble = nir_iand_imm(b, nir_channel(b, offset, c), 0xF); |
| 327 |
nir_def *shifted = nir_ishl_imm(b, nibble, 4 * c); |
| 328 |
|
| 329 |
/* We pack with iadd instead of ior to let us fuse in the shift with an |
| 330 |
* iadd-lsl instruction. |
| 331 |
*/ |
| 332 |
if (packed != NULL) |
| 333 |
packed = nir_iadd(b, packed, shifted); |
| 334 |
else |
| 335 |
packed = shifted; |
| 336 |
} |
| 337 |
|
| 338 |
nir_tex_instr_add_src(tex, nir_tex_src_backend2, packed); |
| 339 |
} |
| 340 |
|
| 341 |
if (nir_tex_instr_src_index(tex, nir_tex_src_bias) >= 0 && |
| 342 |
nir_tex_instr_src_index(tex, nir_tex_src_min_lod) >= 0) { |
| 343 |
|
| 344 |
nir_def *bias = nir_steal_tex_src(tex, nir_tex_src_bias); |
| 345 |
nir_def *min_lod = nir_steal_tex_src(tex, nir_tex_src_min_lod); |
| 346 |
nir_def *packed = nir_pack_32_2x16_split(b, bias, min_lod); |
| 347 |
nir_tex_instr_add_src(tex, nir_tex_src_lod_bias_min_agx, packed); |
| 348 |
} |
| 349 |
|
| 350 |
/* We reserve bound sampler #0, so we offset bound samplers by 1 and |
| 351 |
* otherwise map bound samplers as-is. |
| 352 |
*/ |
| 353 |
nir_def *sampler = nir_steal_tex_src(tex, nir_tex_src_sampler_offset); |
| 354 |
if (!sampler) |
| 355 |
sampler = nir_imm_intN_t(b, tex->sampler_index, 16); |
| 356 |
|
| 357 |
if (!is_txf && |
| 358 |
nir_tex_instr_src_index(tex, nir_tex_src_sampler_handle) < 0) { |
| 359 |
|
| 360 |
nir_tex_instr_add_src(tex, nir_tex_src_sampler_handle, |
| 361 |
nir_iadd_imm(b, nir_u2u16(b, sampler), 1)); |
| 362 |
} |
| 363 |
|
| 364 |
return true; |
| 365 |
} |
| 366 |
|
| 367 |
static bool |
| 368 |
legalize_image_lod(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) |
| 369 |
{ |
| 370 |
nir_src *src; |
| 371 |
|
| 372 |
#define CASE(op, idx) \ |
| 373 |
case nir_intrinsic_##op: \ |
| 374 |
case nir_intrinsic_bindless_##op: \ |
| 375 |
src = &intr->src[idx]; \ |
| 376 |
break; |
| 377 |
|
| 378 |
switch (intr->intrinsic) { |
| 379 |
CASE(image_load, 3) |
| 380 |
CASE(image_sparse_load, 3) |
| 381 |
CASE(image_store, 4) |
| 382 |
CASE(image_size, 1) |
| 383 |
default: |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
#undef CASE |
| 388 |
|
| 389 |
if (src->ssa->bit_size == 16) |
| 390 |
return false; |
| 391 |
|
| 392 |
b->cursor = nir_before_instr(&intr->instr); |
| 393 |
nir_src_rewrite(src, nir_i2i16(b, src->ssa)); |
| 394 |
return true; |
| 395 |
} |
| 396 |
|
| 397 |
static nir_def * |
| 398 |
txs_for_image(nir_builder *b, nir_intrinsic_instr *intr, |
| 399 |
unsigned num_components, unsigned bit_size, bool query_samples) |
| 400 |
{ |
| 401 |
enum glsl_sampler_dim dim = nir_intrinsic_image_dim(intr); |
| 402 |
nir_def *lod = query_samples ? NULL : intr->src[1].ssa; |
| 403 |
nir_texop op = query_samples ? nir_texop_texture_samples : nir_texop_txs; |
| 404 |
|
| 405 |
nir_def *res = |
| 406 |
nir_build_tex(b, op, .texture_handle = intr->src[0].ssa, .lod = lod, |
| 407 |
.dim = dim, .is_array = nir_intrinsic_image_array(intr), |
| 408 |
.can_speculate = nir_instr_can_speculate(&intr->instr)); |
| 409 |
|
| 410 |
/* Cube images are implemented as 2D arrays, so we need to divide here. */ |
| 411 |
if (dim == GLSL_SAMPLER_DIM_CUBE && res->num_components > 2 && |
| 412 |
!query_samples) { |
| 413 |
nir_def *divided = nir_udiv_imm(b, nir_channel(b, res, 2), 6); |
| 414 |
res = nir_vector_insert_imm(b, res, divided, 2); |
| 415 |
} |
| 416 |
|
| 417 |
return res; |
| 418 |
} |
| 419 |
|
| 420 |
static nir_def * |
| 421 |
image_texel_address(nir_builder *b, nir_intrinsic_instr *intr, |
| 422 |
bool return_index) |
| 423 |
{ |
| 424 |
/* First, calculate the address of the PBE descriptor */ |
| 425 |
nir_def *desc_address = |
| 426 |
nir_load_from_texture_handle_agx(b, intr->src[0].ssa); |
| 427 |
|
| 428 |
nir_def *coord = intr->src[1].ssa; |
| 429 |
|
| 430 |
/* For atomics, we always infer the format. We only go down this path with |
| 431 |
* formatless intrinsics when lowering multisampled image stores, but that |
| 432 |
* uses the return_index path that ignores the block size. |
| 433 |
*/ |
| 434 |
enum pipe_format format = nir_intrinsic_format(intr); |
| 435 |
assert(return_index || format != PIPE_FORMAT_NONE); |
| 436 |
|
| 437 |
nir_def *blocksize_B = nir_imm_int(b, util_format_get_blocksize(format)); |
| 438 |
|
| 439 |
enum glsl_sampler_dim dim = nir_intrinsic_image_dim(intr); |
| 440 |
bool layered = nir_intrinsic_image_array(intr) || |
| 441 |
(dim == GLSL_SAMPLER_DIM_CUBE) || |
| 442 |
(dim == GLSL_SAMPLER_DIM_3D); |
| 443 |
|
| 444 |
if (dim == GLSL_SAMPLER_DIM_BUF && return_index) { |
| 445 |
return nir_channel(b, coord, 0); |
| 446 |
} else if (dim == GLSL_SAMPLER_DIM_BUF) { |
| 447 |
return libagx_buffer_texel_address(b, desc_address, coord, blocksize_B); |
| 448 |
} else { |
| 449 |
return libagx_image_texel_address( |
| 450 |
b, desc_address, coord, nir_u2u32(b, intr->src[2].ssa), blocksize_B, |
| 451 |
nir_imm_bool(b, dim == GLSL_SAMPLER_DIM_1D), |
| 452 |
nir_imm_bool(b, dim == GLSL_SAMPLER_DIM_MS), nir_imm_bool(b, layered), |
| 453 |
nir_imm_bool(b, return_index)); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
static void |
| 458 |
lower_buffer_image(nir_builder *b, nir_intrinsic_instr *intr) |
| 459 |
{ |
| 460 |
nir_def *coord_vector = intr->src[1].ssa; |
| 461 |
nir_def *coord = nir_channel(b, coord_vector, 0); |
| 462 |
|
| 463 |
/* If we're not bindless, assume we don't need an offset (GL driver) */ |
| 464 |
if (intr->intrinsic == nir_intrinsic_bindless_image_load || |
| 465 |
intr->intrinsic == nir_intrinsic_bindless_image_sparse_load) { |
| 466 |
|
| 467 |
nir_def *desc = nir_load_from_texture_handle_agx(b, intr->src[0].ssa); |
| 468 |
coord = libagx_buffer_texture_offset(b, desc, coord); |
| 469 |
} else if (intr->intrinsic == nir_intrinsic_bindless_image_store) { |
| 470 |
nir_def *desc = nir_load_from_texture_handle_agx(b, intr->src[0].ssa); |
| 471 |
coord = libagx_buffer_image_offset(b, desc, coord); |
| 472 |
} |
| 473 |
|
| 474 |
/* Lower the buffer load/store to a 2D image load/store, matching the 2D |
| 475 |
* texture/PBE descriptor the driver supplies for buffer images. |
| 476 |
*/ |
| 477 |
nir_def *coord2d = coords_for_buffer_texture(b, coord); |
| 478 |
nir_src_rewrite(&intr->src[1], nir_pad_vector(b, coord2d, 4)); |
| 479 |
nir_intrinsic_set_image_dim(intr, GLSL_SAMPLER_DIM_2D); |
| 480 |
} |
| 481 |
|
| 482 |
static void |
| 483 |
lower_1d_image(nir_builder *b, nir_intrinsic_instr *intr) |
| 484 |
{ |
| 485 |
nir_def *coord = intr->src[1].ssa; |
| 486 |
bool is_array = nir_intrinsic_image_array(intr); |
| 487 |
nir_def *zero = nir_imm_intN_t(b, 0, coord->bit_size); |
| 488 |
|
| 489 |
if (is_array) { |
| 490 |
assert(coord->num_components >= 2); |
| 491 |
coord = |
| 492 |
nir_vec3(b, nir_channel(b, coord, 0), zero, nir_channel(b, coord, 1)); |
| 493 |
} else { |
| 494 |
assert(coord->num_components >= 1); |
| 495 |
coord = nir_vec2(b, coord, zero); |
| 496 |
} |
| 497 |
|
| 498 |
nir_src_rewrite(&intr->src[1], nir_pad_vector(b, coord, 4)); |
| 499 |
nir_intrinsic_set_image_dim(intr, GLSL_SAMPLER_DIM_2D); |
| 500 |
} |
| 501 |
|
| 502 |
/* |
| 503 |
* Just like for txf, we need special handling around layers (and LODs, but we |
| 504 |
* don't support mipmapped images yet) for robust image_loads. See |
| 505 |
* libagx_lower_txf_robustness for more info. |
| 506 |
*/ |
| 507 |
static bool |
| 508 |
lower_image_load_robustness(nir_builder *b, nir_intrinsic_instr *intr) |
| 509 |
{ |
| 510 |
if (nir_intrinsic_access(intr) & ACCESS_IN_BOUNDS) |
| 511 |
return false; |
| 512 |
|
| 513 |
/* We only need to worry about array-like loads */ |
| 514 |
enum glsl_sampler_dim dim = nir_intrinsic_image_dim(intr); |
| 515 |
if (!nir_intrinsic_image_array(intr) && dim != GLSL_SAMPLER_DIM_CUBE) |
| 516 |
return false; |
| 517 |
|
| 518 |
/* Determine the coordinate component of the layer. Cubes and cube arrays |
| 519 |
* keep their array in their last non-array coordinate component, other |
| 520 |
* arrays are immediately after. |
| 521 |
*/ |
| 522 |
unsigned lidx = glsl_get_sampler_dim_coordinate_components(dim); |
| 523 |
if (dim == GLSL_SAMPLER_DIM_CUBE) |
| 524 |
lidx--; |
| 525 |
|
| 526 |
nir_def *coord = intr->src[1].ssa; |
| 527 |
nir_def *lod = nir_undef(b, 1, 16); |
| 528 |
nir_def *layer = nir_channel(b, coord, lidx); |
| 529 |
|
| 530 |
/* image_load is effectively the same as txf, reuse the txf lower */ |
| 531 |
nir_def *replaced = libagx_lower_txf_robustness( |
| 532 |
b, nir_load_from_texture_handle_agx(b, intr->src[0].ssa), |
| 533 |
nir_imm_bool(b, false /* lower LOD */), lod, |
| 534 |
nir_imm_bool(b, false /* lower min LOD */), lod, |
| 535 |
nir_imm_bool(b, true /* lower layer */), layer, nir_channel(b, coord, 0)); |
| 536 |
|
| 537 |
nir_src_rewrite(&intr->src[1], nir_vector_insert_imm(b, coord, replaced, 0)); |
| 538 |
return true; |
| 539 |
} |
| 540 |
|
| 541 |
static bool |
| 542 |
lower_images(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) |
| 543 |
{ |
| 544 |
b->cursor = nir_before_instr(&intr->instr); |
| 545 |
|
| 546 |
switch (intr->intrinsic) { |
| 547 |
case nir_intrinsic_image_load: |
| 548 |
case nir_intrinsic_image_store: |
| 549 |
case nir_intrinsic_bindless_image_load: |
| 550 |
case nir_intrinsic_bindless_image_sparse_load: |
| 551 |
case nir_intrinsic_bindless_image_store: { |
| 552 |
/* Legalize MSAA index */ |
| 553 |
nir_src_rewrite(&intr->src[2], nir_u2u16(b, intr->src[2].ssa)); |
| 554 |
|
| 555 |
if (intr->intrinsic == nir_intrinsic_image_load || |
| 556 |
intr->intrinsic == nir_intrinsic_bindless_image_load || |
| 557 |
intr->intrinsic == nir_intrinsic_bindless_image_sparse_load) { |
| 558 |
lower_image_load_robustness(b, intr); |
| 559 |
} |
| 560 |
|
| 561 |
switch (nir_intrinsic_image_dim(intr)) { |
| 562 |
case GLSL_SAMPLER_DIM_1D: |
| 563 |
lower_1d_image(b, intr); |
| 564 |
return true; |
| 565 |
|
| 566 |
case GLSL_SAMPLER_DIM_BUF: |
| 567 |
lower_buffer_image(b, intr); |
| 568 |
return true; |
| 569 |
|
| 570 |
default: |
| 571 |
return true; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
case nir_intrinsic_bindless_image_size: |
| 576 |
case nir_intrinsic_bindless_image_samples: |
| 577 |
nir_def_rewrite_uses( |
| 578 |
&intr->def, |
| 579 |
txs_for_image( |
| 580 |
b, intr, intr->def.num_components, intr->def.bit_size, |
| 581 |
intr->intrinsic == nir_intrinsic_bindless_image_samples)); |
| 582 |
return true; |
| 583 |
|
| 584 |
case nir_intrinsic_bindless_image_texel_address: |
| 585 |
nir_def_rewrite_uses(&intr->def, image_texel_address(b, intr, false)); |
| 586 |
return true; |
| 587 |
|
| 588 |
case nir_intrinsic_is_sparse_texels_resident: |
| 589 |
/* Residency information is in bit 0, so we need to mask. Unclear what's |
| 590 |
* in the upper bits. For now, let's match the blob. |
| 591 |
*/ |
| 592 |
nir_def_replace( |
| 593 |
&intr->def, |
| 594 |
nir_ieq_imm(b, nir_iand_imm(b, intr->src[0].ssa, 1), AGX_RESIDENT)); |
| 595 |
return true; |
| 596 |
|
| 597 |
case nir_intrinsic_sparse_residency_code_and: |
| 598 |
/* ior because residency codes are inverted from NIR */ |
| 599 |
nir_def_replace(&intr->def, |
| 600 |
nir_ior(b, intr->src[0].ssa, intr->src[1].ssa)); |
| 601 |
return true; |
| 602 |
|
| 603 |
case nir_intrinsic_image_size: |
| 604 |
case nir_intrinsic_image_texel_address: |
| 605 |
UNREACHABLE("should've been lowered"); |
| 606 |
|
| 607 |
default: |
| 608 |
return false; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
/* |
| 613 |
* Map out-of-bounds storage texel buffer accesses and multisampled image stores |
| 614 |
* to -1 indices, which will become an out-of-bounds hardware access. This gives |
| 615 |
* cheap robustness2. |
| 616 |
*/ |
| 617 |
static bool |
| 618 |
lower_robustness(nir_builder *b, nir_intrinsic_instr *intr, UNUSED void *data) |
| 619 |
{ |
| 620 |
b->cursor = nir_before_instr(&intr->instr); |
| 621 |
|
| 622 |
switch (intr->intrinsic) { |
| 623 |
case nir_intrinsic_image_deref_load: |
| 624 |
case nir_intrinsic_image_deref_sparse_load: |
| 625 |
case nir_intrinsic_image_deref_store: |
| 626 |
break; |
| 627 |
default: |
| 628 |
return false; |
| 629 |
} |
| 630 |
|
| 631 |
enum glsl_sampler_dim dim = nir_intrinsic_image_dim(intr); |
| 632 |
bool array = nir_intrinsic_image_array(intr); |
| 633 |
unsigned size_components = nir_image_intrinsic_coord_components(intr); |
| 634 |
|
| 635 |
nir_def *deref = intr->src[0].ssa; |
| 636 |
nir_def *coord = intr->src[1].ssa; |
| 637 |
|
| 638 |
if (dim != GLSL_SAMPLER_DIM_BUF && |
| 639 |
!(dim == GLSL_SAMPLER_DIM_MS && |
| 640 |
intr->intrinsic == nir_intrinsic_image_deref_store)) |
| 641 |
return false; |
| 642 |
|
| 643 |
/* Bounds check the coordinate */ |
| 644 |
nir_def *size = |
| 645 |
nir_image_deref_size(b, size_components, 32, deref, nir_imm_int(b, 0), |
| 646 |
.image_dim = dim, .image_array = array); |
| 647 |
nir_def *oob = nir_bany(b, nir_uge(b, coord, size)); |
| 648 |
|
| 649 |
/* Bounds check the sample */ |
| 650 |
if (dim == GLSL_SAMPLER_DIM_MS) { |
| 651 |
nir_def *samples = nir_image_deref_samples(b, 32, deref, .image_dim = dim, |
| 652 |
.image_array = array); |
| 653 |
|
| 654 |
oob = nir_ior(b, oob, nir_uge(b, intr->src[2].ssa, samples)); |
| 655 |
} |
| 656 |
|
| 657 |
/* Replace the last coordinate component with a large coordinate for |
| 658 |
* out-of-bounds. We pick 0xFFF0 as it fits in 16-bit, and it is not signed |
| 659 |
* as 32-bit so we won't get in-bounds coordinates for arrays due to two's |
| 660 |
* complement wraparound. Additionally it still meets this requirement after |
| 661 |
* adding 0xF, the maximum tail offset. |
| 662 |
* |
| 663 |
* This ensures the resulting hardware coordinate is definitely |
| 664 |
* out-of-bounds, giving hardware-level robustness2 behaviour. |
| 665 |
*/ |
| 666 |
unsigned c = size_components - 1; |
| 667 |
nir_def *r = |
| 668 |
nir_bcsel(b, oob, nir_imm_int(b, 0xFFF0), nir_channel(b, coord, c)); |
| 669 |
|
| 670 |
nir_src_rewrite(&intr->src[1], nir_vector_insert_imm(b, coord, r, c)); |
| 671 |
return true; |
| 672 |
} |
| 673 |
|
| 674 |
/* |
| 675 |
* Early texture lowering passes, called by the driver before lowering |
| 676 |
* descriptor bindings. That means these passes operate on texture derefs. The |
| 677 |
* purpose is to make descriptor crawls explicit in the NIR, so that the driver |
| 678 |
* can accurately lower descriptors after this pass but before calling |
| 679 |
* the full agx_nir_lower_texture. |
| 680 |
*/ |
| 681 |
bool |
| 682 |
agx_nir_lower_texture_early(nir_shader *s, bool support_lod_bias) |
| 683 |
{ |
| 684 |
bool progress = false; |
| 685 |
|
| 686 |
NIR_PASS(progress, s, nir_shader_intrinsics_pass, lower_robustness, |
| 687 |
nir_metadata_control_flow, NULL); |
| 688 |
|
| 689 |
nir_lower_tex_options lower_tex_options = { |
| 690 |
.lower_txp = ~0, |
| 691 |
.lower_invalid_implicit_lod = true, |
| 692 |
.lower_tg4_offsets = true, |
| 693 |
.lower_index_to_offset = true, |
| 694 |
.lower_sampler_lod_bias = support_lod_bias, |
| 695 |
|
| 696 |
/* Unclear if/how mipmapped 1D textures work in the hardware. */ |
| 697 |
.lower_1d = true, |
| 698 |
|
| 699 |
/* XXX: Metal seems to handle just like 3D txd, so why doesn't it work? |
| 700 |
* TODO: Stop using this lowering |
| 701 |
*/ |
| 702 |
.lower_txd_cube_map = true, |
| 703 |
}; |
| 704 |
|
| 705 |
NIR_PASS(progress, s, nir_lower_tex, &lower_tex_options); |
| 706 |
return progress; |
| 707 |
} |
| 708 |
|
| 709 |
bool |
| 710 |
agx_nir_lower_texture(nir_shader *s) |
| 711 |
{ |
| 712 |
bool progress = false; |
| 713 |
|
| 714 |
nir_tex_src_type_constraints tex_constraints = { |
| 715 |
[nir_tex_src_lod] = {true, 16}, |
| 716 |
[nir_tex_src_bias] = {true, 16}, |
| 717 |
[nir_tex_src_ms_index] = {true, 16}, |
| 718 |
[nir_tex_src_min_lod] = {true, 16}, |
| 719 |
[nir_tex_src_texture_offset] = {true, 16}, |
| 720 |
[nir_tex_src_sampler_offset] = {true, 16}, |
| 721 |
}; |
| 722 |
|
| 723 |
/* Insert fences before lowering image atomics, since image atomics need |
| 724 |
* different fencing than other image operations. |
| 725 |
*/ |
| 726 |
NIR_PASS(progress, s, nir_shader_intrinsics_pass, fence_image, |
| 727 |
nir_metadata_control_flow, NULL); |
| 728 |
|
| 729 |
NIR_PASS(progress, s, nir_lower_image_atomics_to_global, NULL, NULL); |
| 730 |
|
| 731 |
NIR_PASS(progress, s, nir_shader_intrinsics_pass, legalize_image_lod, |
| 732 |
nir_metadata_control_flow, NULL); |
| 733 |
NIR_PASS(progress, s, nir_shader_intrinsics_pass, lower_images, |
| 734 |
nir_metadata_control_flow, NULL); |
| 735 |
NIR_PASS(progress, s, nir_legalize_16bit_sampler_srcs, tex_constraints); |
| 736 |
|
| 737 |
/* Fold constants after nir_legalize_16bit_sampler_srcs so we can detect 0 in |
| 738 |
* lower_regular_texture. This is required for correctness. |
| 739 |
*/ |
| 740 |
NIR_PASS(progress, s, nir_opt_constant_folding); |
| 741 |
|
| 742 |
/* Lower texture sources after legalizing types (as the lowering depends on |
| 743 |
* 16-bit multisample indices) but before lowering queries (as the lowering |
| 744 |
* generates txs for array textures). |
| 745 |
*/ |
| 746 |
NIR_PASS(progress, s, nir_shader_instructions_pass, lower_regular_texture, |
| 747 |
nir_metadata_none, NULL); |
| 748 |
NIR_PASS(progress, s, nir_shader_instructions_pass, lower_tex_crawl, |
| 749 |
nir_metadata_control_flow, NULL); |
| 750 |
|
| 751 |
return progress; |
| 752 |
} |
| 753 |
|
| 754 |
static bool |
| 755 |
lower_multisampled_store(nir_builder *b, nir_intrinsic_instr *intr, |
| 756 |
UNUSED void *data) |
| 757 |
{ |
| 758 |
b->cursor = nir_before_instr(&intr->instr); |
| 759 |
|
| 760 |
if (intr->intrinsic != nir_intrinsic_bindless_image_store) |
| 761 |
return false; |
| 762 |
|
| 763 |
if (nir_intrinsic_image_dim(intr) != GLSL_SAMPLER_DIM_MS) |
| 764 |
return false; |
| 765 |
|
| 766 |
nir_def *index_px = nir_u2u32(b, image_texel_address(b, intr, true)); |
| 767 |
nir_def *coord2d = coords_for_buffer_texture(b, index_px); |
| 768 |
|
| 769 |
nir_src_rewrite(&intr->src[1], nir_pad_vector(b, coord2d, 4)); |
| 770 |
nir_src_rewrite(&intr->src[2], nir_imm_int(b, 0)); |
| 771 |
nir_intrinsic_set_image_dim(intr, GLSL_SAMPLER_DIM_2D); |
| 772 |
nir_intrinsic_set_image_array(intr, false); |
| 773 |
return true; |
| 774 |
} |
| 775 |
|
| 776 |
bool |
| 777 |
agx_nir_lower_multisampled_image_store(nir_shader *s) |
| 778 |
{ |
| 779 |
return nir_shader_intrinsics_pass(s, lower_multisampled_store, |
| 780 |
nir_metadata_control_flow, NULL); |
| 781 |
} |
| 782 |
|
| 783 |
/* |
| 784 |
* Given a non-bindless instruction, return whether agx_nir_lower_texture will |
| 785 |
* lower it to something involving a descriptor crawl. This requires the driver |
| 786 |
* to lower the instruction to bindless before calling agx_nir_lower_texture. |
| 787 |
* The implementation just enumerates the cases handled in this file. |
| 788 |
*/ |
| 789 |
bool |
| 790 |
agx_nir_needs_texture_crawl(nir_instr *instr) |
| 791 |
{ |
| 792 |
if (instr->type == nir_instr_type_intrinsic) { |
| 793 |
nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); |
| 794 |
|
| 795 |
switch (intr->intrinsic) { |
| 796 |
/* Queries, atomics always become a crawl */ |
| 797 |
case nir_intrinsic_image_size: |
| 798 |
case nir_intrinsic_image_deref_size: |
| 799 |
case nir_intrinsic_image_samples: |
| 800 |
case nir_intrinsic_image_deref_samples: |
| 801 |
case nir_intrinsic_image_atomic: |
| 802 |
case nir_intrinsic_image_deref_atomic: |
| 803 |
case nir_intrinsic_image_atomic_swap: |
| 804 |
case nir_intrinsic_image_deref_atomic_swap: |
| 805 |
return true; |
| 806 |
|
| 807 |
/* Multisampled stores need a crawl, others do not */ |
| 808 |
case nir_intrinsic_image_store: |
| 809 |
case nir_intrinsic_image_deref_store: |
| 810 |
return nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_MS; |
| 811 |
|
| 812 |
/* Array loads need a crawl, other load do not */ |
| 813 |
case nir_intrinsic_image_load: |
| 814 |
return nir_intrinsic_image_array(intr) || |
| 815 |
nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_CUBE; |
| 816 |
|
| 817 |
default: |
| 818 |
return false; |
| 819 |
} |
| 820 |
} else if (instr->type == nir_instr_type_tex) { |
| 821 |
nir_tex_instr *tex = nir_instr_as_tex(instr); |
| 822 |
|
| 823 |
/* Array textures get clamped to their size via txs */ |
| 824 |
if (tex->is_array && !(tex->backend_flags & AGX_TEXTURE_FLAG_NO_CLAMP)) |
| 825 |
return true; |
| 826 |
|
| 827 |
switch (tex->op) { |
| 828 |
/* Queries always become a crawl */ |
| 829 |
case nir_texop_txs: |
| 830 |
case nir_texop_texture_samples: |
| 831 |
case nir_texop_query_levels: |
| 832 |
return true; |
| 833 |
|
| 834 |
/* Buffer textures need their format read and txf needs its LOD/layer |
| 835 |
* clamped. Buffer textures are only read through txf. |
| 836 |
*/ |
| 837 |
case nir_texop_txf: |
| 838 |
case nir_texop_txf_ms: |
| 839 |
return has_nonzero_lod(tex) || tex->is_array || |
| 840 |
tex->sampler_dim == GLSL_SAMPLER_DIM_BUF; |
| 841 |
|
| 842 |
default: |
| 843 |
return false; |
| 844 |
} |
| 845 |
} |
| 846 |
|
| 847 |
return false; |
| 848 |
} |