| 1 |
/* |
| 2 |
* Copyright 2024 Valve Corporation |
| 3 |
* Copyright 2024 Alyssa Rosenzweig |
| 4 |
* Copyright 2022-2023 Collabora Ltd. and Red Hat Inc. |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
#include "pipe/p_defines.h" |
| 8 |
#include "vulkan/vulkan_core.h" |
| 9 |
#include "agx_abi.h" |
| 10 |
#include "agx_nir_texture.h" |
| 11 |
#include "hk_cmd_buffer.h" |
| 12 |
#include "hk_descriptor_set.h" |
| 13 |
#include "hk_descriptor_set_layout.h" |
| 14 |
#include "hk_shader.h" |
| 15 |
|
| 16 |
#include "nir.h" |
| 17 |
#include "nir_builder.h" |
| 18 |
#include "nir_builder_opcodes.h" |
| 19 |
#include "nir_intrinsics.h" |
| 20 |
#include "nir_intrinsics_indices.h" |
| 21 |
#include "shader_enums.h" |
| 22 |
#include "vk_pipeline.h" |
| 23 |
|
| 24 |
struct lower_descriptors_ctx { |
| 25 |
const struct hk_descriptor_set_layout *set_layouts[HK_MAX_SETS]; |
| 26 |
|
| 27 |
bool clamp_desc_array_bounds; |
| 28 |
nir_address_format ubo_addr_format; |
| 29 |
nir_address_format ssbo_addr_format; |
| 30 |
}; |
| 31 |
|
| 32 |
static const struct hk_descriptor_set_binding_layout * |
| 33 |
get_binding_layout(uint32_t set, uint32_t binding, |
| 34 |
const struct lower_descriptors_ctx *ctx) |
| 35 |
{ |
| 36 |
assert(set < HK_MAX_SETS); |
| 37 |
assert(ctx->set_layouts[set] != NULL); |
| 38 |
|
| 39 |
const struct hk_descriptor_set_layout *set_layout = ctx->set_layouts[set]; |
| 40 |
|
| 41 |
assert(binding < set_layout->binding_count); |
| 42 |
return &set_layout->binding[binding]; |
| 43 |
} |
| 44 |
|
| 45 |
static nir_def * |
| 46 |
load_speculatable(nir_builder *b, unsigned num_components, unsigned bit_size, |
| 47 |
nir_def *addr, unsigned align) |
| 48 |
{ |
| 49 |
return nir_load_global_constant(b, num_components, bit_size, addr, |
| 50 |
.align_mul = align, |
| 51 |
.access = ACCESS_CAN_SPECULATE); |
| 52 |
} |
| 53 |
|
| 54 |
static nir_def * |
| 55 |
load_root(nir_builder *b, unsigned num_components, unsigned bit_size, |
| 56 |
nir_def *offset, unsigned align) |
| 57 |
{ |
| 58 |
nir_def *addr = nir_iadd(b, nir_load_root_agx(b), nir_u2u64(b, offset)); |
| 59 |
|
| 60 |
return load_speculatable(b, num_components, bit_size, addr, align); |
| 61 |
} |
| 62 |
|
| 63 |
static bool |
| 64 |
lower_load_constant(nir_builder *b, nir_intrinsic_instr *load, |
| 65 |
const struct lower_descriptors_ctx *ctx) |
| 66 |
{ |
| 67 |
assert(load->intrinsic == nir_intrinsic_load_constant); |
| 68 |
UNREACHABLE("todo: stick an address in the root descriptor or something"); |
| 69 |
|
| 70 |
uint32_t base = nir_intrinsic_base(load); |
| 71 |
uint32_t range = nir_intrinsic_range(load); |
| 72 |
|
| 73 |
b->cursor = nir_before_instr(&load->instr); |
| 74 |
|
| 75 |
nir_def *offset = nir_iadd_imm(b, load->src[0].ssa, base); |
| 76 |
nir_def *data = nir_load_ubo( |
| 77 |
b, load->def.num_components, load->def.bit_size, nir_imm_int(b, 0), |
| 78 |
offset, .align_mul = nir_intrinsic_align_mul(load), |
| 79 |
.align_offset = nir_intrinsic_align_offset(load), .range_base = base, |
| 80 |
.range = range); |
| 81 |
|
| 82 |
nir_def_rewrite_uses(&load->def, data); |
| 83 |
|
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
static nir_def * |
| 88 |
load_dynamic_buffer_start(nir_builder *b, uint32_t set, |
| 89 |
const struct lower_descriptors_ctx *ctx) |
| 90 |
{ |
| 91 |
int dynamic_buffer_start_imm = 0; |
| 92 |
for (uint32_t s = 0; s < set; s++) { |
| 93 |
if (ctx->set_layouts[s] == NULL) { |
| 94 |
dynamic_buffer_start_imm = -1; |
| 95 |
break; |
| 96 |
} |
| 97 |
|
| 98 |
dynamic_buffer_start_imm += ctx->set_layouts[s]->vk.dynamic_descriptor_count; |
| 99 |
} |
| 100 |
|
| 101 |
if (dynamic_buffer_start_imm >= 0) { |
| 102 |
return nir_imm_int(b, dynamic_buffer_start_imm); |
| 103 |
} else { |
| 104 |
uint32_t root_offset = |
| 105 |
hk_root_descriptor_offset(set_dynamic_buffer_start) + set; |
| 106 |
|
| 107 |
return nir_u2u32(b, load_root(b, 1, 8, nir_imm_int(b, root_offset), 1)); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
static nir_def * |
| 112 |
load_descriptor(nir_builder *b, unsigned num_components, unsigned bit_size, |
| 113 |
uint32_t set, uint32_t binding, nir_def *index, |
| 114 |
unsigned offset_B, const struct lower_descriptors_ctx *ctx) |
| 115 |
{ |
| 116 |
const struct hk_descriptor_set_binding_layout *binding_layout = |
| 117 |
get_binding_layout(set, binding, ctx); |
| 118 |
|
| 119 |
if (ctx->clamp_desc_array_bounds) |
| 120 |
index = |
| 121 |
nir_umin(b, index, nir_imm_int(b, binding_layout->array_size - 1)); |
| 122 |
|
| 123 |
switch (binding_layout->type) { |
| 124 |
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC: |
| 125 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: { |
| 126 |
/* Get the index in the root descriptor table dynamic_buffers array. */ |
| 127 |
nir_def *dynamic_buffer_start = load_dynamic_buffer_start(b, set, ctx); |
| 128 |
|
| 129 |
index = nir_iadd(b, index, |
| 130 |
nir_iadd_imm(b, dynamic_buffer_start, |
| 131 |
binding_layout->dynamic_buffer_index)); |
| 132 |
|
| 133 |
nir_def *root_desc_offset = nir_iadd_imm( |
| 134 |
b, nir_amul_imm(b, index, sizeof(struct hk_buffer_address)), |
| 135 |
hk_root_descriptor_offset(dynamic_buffers)); |
| 136 |
|
| 137 |
assert(num_components == 4 && bit_size == 32); |
| 138 |
nir_def *desc = load_root(b, 4, 32, root_desc_offset, 16); |
| 139 |
|
| 140 |
/* We know a priori that the the .w compnent (offset) is zero */ |
| 141 |
return nir_vector_insert_imm(b, desc, nir_imm_int(b, 0), 3); |
| 142 |
} |
| 143 |
|
| 144 |
case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK: { |
| 145 |
nir_def *base_addr = nir_iadd_imm(b, nir_load_descriptor_set_agx(b, set), |
| 146 |
binding_layout->offset); |
| 147 |
|
| 148 |
assert(binding_layout->stride == 1); |
| 149 |
const uint32_t binding_size = binding_layout->array_size; |
| 150 |
|
| 151 |
/* Convert it to nir_address_format_64bit_bounded_global */ |
| 152 |
assert(num_components == 4 && bit_size == 32); |
| 153 |
return nir_vec4(b, nir_unpack_64_2x32_split_x(b, base_addr), |
| 154 |
nir_unpack_64_2x32_split_y(b, base_addr), |
| 155 |
nir_imm_int(b, binding_size), nir_imm_int(b, 0)); |
| 156 |
} |
| 157 |
|
| 158 |
default: { |
| 159 |
assert(binding_layout->stride > 0); |
| 160 |
nir_def *desc_ubo_offset = |
| 161 |
nir_iadd_imm(b, nir_amul_imm(b, index, binding_layout->stride), |
| 162 |
binding_layout->offset + offset_B); |
| 163 |
|
| 164 |
unsigned desc_align_mul = (1 << (ffs(binding_layout->stride) - 1)); |
| 165 |
desc_align_mul = MIN2(desc_align_mul, 16); |
| 166 |
unsigned desc_align_offset = binding_layout->offset + offset_B; |
| 167 |
desc_align_offset %= desc_align_mul; |
| 168 |
|
| 169 |
nir_def *desc; |
| 170 |
nir_def *set_addr = nir_load_descriptor_set_agx(b, set); |
| 171 |
desc = nir_load_global_constant_offset( |
| 172 |
b, num_components, bit_size, set_addr, desc_ubo_offset, |
| 173 |
.align_mul = desc_align_mul, .align_offset = desc_align_offset, |
| 174 |
.access = ACCESS_CAN_SPECULATE); |
| 175 |
|
| 176 |
if (binding_layout->type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || |
| 177 |
binding_layout->type == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) { |
| 178 |
/* We know a priori that the the .w compnent (offset) is zero */ |
| 179 |
assert(num_components == 4 && bit_size == 32); |
| 180 |
desc = nir_vector_insert_imm(b, desc, nir_imm_int(b, 0), 3); |
| 181 |
} |
| 182 |
return desc; |
| 183 |
} |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
static bool |
| 188 |
is_idx_intrin(nir_intrinsic_instr *intrin) |
| 189 |
{ |
| 190 |
while (intrin->intrinsic == nir_intrinsic_vulkan_resource_reindex) { |
| 191 |
intrin = nir_src_as_intrinsic(intrin->src[0]); |
| 192 |
if (intrin == NULL) |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
return intrin->intrinsic == nir_intrinsic_vulkan_resource_index; |
| 197 |
} |
| 198 |
|
| 199 |
static nir_def * |
| 200 |
load_descriptor_for_idx_intrin(nir_builder *b, nir_intrinsic_instr *intrin, |
| 201 |
const struct lower_descriptors_ctx *ctx) |
| 202 |
{ |
| 203 |
nir_def *index = nir_imm_int(b, 0); |
| 204 |
|
| 205 |
while (intrin->intrinsic == nir_intrinsic_vulkan_resource_reindex) { |
| 206 |
index = nir_iadd(b, index, intrin->src[1].ssa); |
| 207 |
intrin = nir_src_as_intrinsic(intrin->src[0]); |
| 208 |
} |
| 209 |
|
| 210 |
assert(intrin->intrinsic == nir_intrinsic_vulkan_resource_index); |
| 211 |
uint32_t set = nir_intrinsic_desc_set(intrin); |
| 212 |
uint32_t binding = nir_intrinsic_binding(intrin); |
| 213 |
index = nir_iadd(b, index, intrin->src[0].ssa); |
| 214 |
|
| 215 |
return load_descriptor(b, 4, 32, set, binding, index, 0, ctx); |
| 216 |
} |
| 217 |
|
| 218 |
static bool |
| 219 |
try_lower_load_vulkan_descriptor(nir_builder *b, nir_intrinsic_instr *intrin, |
| 220 |
const struct lower_descriptors_ctx *ctx) |
| 221 |
{ |
| 222 |
ASSERTED const nir_descriptor_type desc_type = |
| 223 |
nir_intrinsic_desc_type(intrin); |
| 224 |
b->cursor = nir_before_instr(&intrin->instr); |
| 225 |
|
| 226 |
nir_intrinsic_instr *idx_intrin = nir_src_as_intrinsic(intrin->src[0]); |
| 227 |
if (idx_intrin == NULL || !is_idx_intrin(idx_intrin)) { |
| 228 |
assert(desc_type == nir_descriptor_type_storage_buffer); |
| 229 |
return false; |
| 230 |
} |
| 231 |
|
| 232 |
nir_def *desc = load_descriptor_for_idx_intrin(b, idx_intrin, ctx); |
| 233 |
|
| 234 |
nir_def_rewrite_uses(&intrin->def, desc); |
| 235 |
|
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
static bool |
| 240 |
_lower_sysval_to_root_table(nir_builder *b, nir_intrinsic_instr *intrin, |
| 241 |
uint32_t root_table_offset) |
| 242 |
{ |
| 243 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 244 |
assert((root_table_offset & 3) == 0 && "aligned"); |
| 245 |
|
| 246 |
nir_def *val = load_root(b, intrin->def.num_components, intrin->def.bit_size, |
| 247 |
nir_imm_int(b, root_table_offset), 4); |
| 248 |
|
| 249 |
nir_def_rewrite_uses(&intrin->def, val); |
| 250 |
|
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
#define lower_sysval_to_root_table(b, intrin, member) \ |
| 255 |
_lower_sysval_to_root_table(b, intrin, hk_root_descriptor_offset(member)) |
| 256 |
|
| 257 |
static bool |
| 258 |
lower_load_push_constant(nir_builder *b, nir_intrinsic_instr *load, |
| 259 |
const struct lower_descriptors_ctx *ctx) |
| 260 |
{ |
| 261 |
const uint32_t push_region_offset = hk_root_descriptor_offset(push); |
| 262 |
const uint32_t base = nir_intrinsic_base(load); |
| 263 |
|
| 264 |
b->cursor = nir_before_instr(&load->instr); |
| 265 |
|
| 266 |
nir_def *offset = |
| 267 |
nir_iadd_imm(b, load->src[0].ssa, push_region_offset + base); |
| 268 |
|
| 269 |
nir_def *val = load_root(b, load->def.num_components, load->def.bit_size, |
| 270 |
offset, load->def.bit_size / 8); |
| 271 |
|
| 272 |
nir_def_rewrite_uses(&load->def, val); |
| 273 |
|
| 274 |
return true; |
| 275 |
} |
| 276 |
|
| 277 |
static void |
| 278 |
get_resource_deref_binding(nir_builder *b, nir_deref_instr *deref, |
| 279 |
uint32_t *set, uint32_t *binding, nir_def **index) |
| 280 |
{ |
| 281 |
if (deref->deref_type == nir_deref_type_array) { |
| 282 |
*index = deref->arr.index.ssa; |
| 283 |
deref = nir_deref_instr_parent(deref); |
| 284 |
} else { |
| 285 |
*index = nir_imm_int(b, 0); |
| 286 |
} |
| 287 |
|
| 288 |
assert(deref->deref_type == nir_deref_type_var); |
| 289 |
nir_variable *var = deref->var; |
| 290 |
|
| 291 |
*set = var->data.descriptor_set; |
| 292 |
*binding = var->data.binding; |
| 293 |
} |
| 294 |
|
| 295 |
static nir_def * |
| 296 |
load_resource_deref_desc(nir_builder *b, unsigned num_components, |
| 297 |
unsigned bit_size, nir_deref_instr *deref, |
| 298 |
unsigned offset_B, |
| 299 |
const struct lower_descriptors_ctx *ctx) |
| 300 |
{ |
| 301 |
uint32_t set, binding; |
| 302 |
nir_def *index; |
| 303 |
get_resource_deref_binding(b, deref, &set, &binding, &index); |
| 304 |
return load_descriptor(b, num_components, bit_size, set, binding, index, |
| 305 |
offset_B, ctx); |
| 306 |
} |
| 307 |
|
| 308 |
static nir_def * |
| 309 |
load_image_handle(nir_builder *b, const struct lower_descriptors_ctx *ctx, |
| 310 |
nir_src src, unsigned offset_B) |
| 311 |
|
| 312 |
{ |
| 313 |
uint32_t set, binding; |
| 314 |
nir_def *index; |
| 315 |
get_resource_deref_binding(b, nir_src_as_deref(src), &set, &binding, &index); |
| 316 |
|
| 317 |
const struct hk_descriptor_set_binding_layout *binding_layout = |
| 318 |
get_binding_layout(set, binding, ctx); |
| 319 |
|
| 320 |
if (ctx->clamp_desc_array_bounds) |
| 321 |
index = |
| 322 |
nir_umin(b, index, nir_imm_int(b, binding_layout->array_size - 1)); |
| 323 |
|
| 324 |
assert(binding_layout->stride > 0); |
| 325 |
nir_def *desc_offs_B = |
| 326 |
nir_iadd_imm(b, nir_amul_imm(b, index, binding_layout->stride), |
| 327 |
binding_layout->offset + offset_B); |
| 328 |
|
| 329 |
return nir_bindless_image_agx(b, desc_offs_B, .desc_set = set); |
| 330 |
} |
| 331 |
|
| 332 |
static bool |
| 333 |
lower_image_intrin(nir_builder *b, nir_intrinsic_instr *intr, |
| 334 |
const struct lower_descriptors_ctx *ctx) |
| 335 |
{ |
| 336 |
b->cursor = nir_before_instr(&intr->instr); |
| 337 |
|
| 338 |
/* Reads and queries use the texture descriptor; writes and atomics PBE. */ |
| 339 |
unsigned offs; |
| 340 |
if (intr->intrinsic != nir_intrinsic_image_deref_load && |
| 341 |
intr->intrinsic != nir_intrinsic_image_deref_sparse_load && |
| 342 |
intr->intrinsic != nir_intrinsic_image_deref_size && |
| 343 |
intr->intrinsic != nir_intrinsic_image_deref_samples) { |
| 344 |
|
| 345 |
offs = offsetof(struct hk_storage_image_descriptor, pbe); |
| 346 |
} else { |
| 347 |
offs = offsetof(struct hk_storage_image_descriptor, tex); |
| 348 |
} |
| 349 |
|
| 350 |
nir_rewrite_image_intrinsic(intr, |
| 351 |
load_image_handle(b, ctx, intr->src[0], offs), |
| 352 |
nir_image_intrinsic_type_bindless); |
| 353 |
|
| 354 |
return true; |
| 355 |
} |
| 356 |
|
| 357 |
static VkQueryPipelineStatisticFlagBits |
| 358 |
translate_pipeline_stat_bit(enum pipe_statistics_query_index pipe) |
| 359 |
{ |
| 360 |
switch (pipe) { |
| 361 |
case PIPE_STAT_QUERY_IA_VERTICES: |
| 362 |
return VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_VERTICES_BIT; |
| 363 |
case PIPE_STAT_QUERY_IA_PRIMITIVES: |
| 364 |
return VK_QUERY_PIPELINE_STATISTIC_INPUT_ASSEMBLY_PRIMITIVES_BIT; |
| 365 |
case PIPE_STAT_QUERY_VS_INVOCATIONS: |
| 366 |
return VK_QUERY_PIPELINE_STATISTIC_VERTEX_SHADER_INVOCATIONS_BIT; |
| 367 |
case PIPE_STAT_QUERY_GS_INVOCATIONS: |
| 368 |
return VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_INVOCATIONS_BIT; |
| 369 |
case PIPE_STAT_QUERY_GS_PRIMITIVES: |
| 370 |
return VK_QUERY_PIPELINE_STATISTIC_GEOMETRY_SHADER_PRIMITIVES_BIT; |
| 371 |
case PIPE_STAT_QUERY_C_INVOCATIONS: |
| 372 |
return VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT; |
| 373 |
case PIPE_STAT_QUERY_C_PRIMITIVES: |
| 374 |
return VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT; |
| 375 |
case PIPE_STAT_QUERY_PS_INVOCATIONS: |
| 376 |
return VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT; |
| 377 |
case PIPE_STAT_QUERY_HS_INVOCATIONS: |
| 378 |
return VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_CONTROL_SHADER_PATCHES_BIT; |
| 379 |
case PIPE_STAT_QUERY_DS_INVOCATIONS: |
| 380 |
return VK_QUERY_PIPELINE_STATISTIC_TESSELLATION_EVALUATION_SHADER_INVOCATIONS_BIT; |
| 381 |
case PIPE_STAT_QUERY_CS_INVOCATIONS: |
| 382 |
return VK_QUERY_PIPELINE_STATISTIC_COMPUTE_SHADER_INVOCATIONS_BIT; |
| 383 |
case PIPE_STAT_QUERY_TS_INVOCATIONS: |
| 384 |
return VK_QUERY_PIPELINE_STATISTIC_TASK_SHADER_INVOCATIONS_BIT_EXT; |
| 385 |
case PIPE_STAT_QUERY_MS_INVOCATIONS: |
| 386 |
return VK_QUERY_PIPELINE_STATISTIC_MESH_SHADER_INVOCATIONS_BIT_EXT; |
| 387 |
default: |
| 388 |
UNREACHABLE("invalid statistic"); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
struct lower_uvs_args { |
| 393 |
mesa_shader_stage sw_stage; |
| 394 |
unsigned nr_vbos; |
| 395 |
}; |
| 396 |
|
| 397 |
static bool |
| 398 |
lower_uvs_index(nir_builder *b, nir_intrinsic_instr *intrin, void *data) |
| 399 |
{ |
| 400 |
const struct lower_uvs_args *args = data; |
| 401 |
|
| 402 |
switch (intrin->intrinsic) { |
| 403 |
case nir_intrinsic_load_uvs_index_agx: { |
| 404 |
gl_varying_slot slot = nir_intrinsic_io_semantics(intrin).location; |
| 405 |
unsigned offset = hk_root_descriptor_offset(draw.uvs_index[slot]); |
| 406 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 407 |
|
| 408 |
nir_def *val = load_root(b, 1, 8, nir_imm_int(b, offset), 1); |
| 409 |
nir_def_rewrite_uses(&intrin->def, nir_u2u16(b, val)); |
| 410 |
return true; |
| 411 |
} |
| 412 |
|
| 413 |
case nir_intrinsic_load_shader_part_tests_zs_agx: |
| 414 |
return lower_sysval_to_root_table(b, intrin, draw.no_epilog_discard); |
| 415 |
|
| 416 |
case nir_intrinsic_load_api_sample_mask_agx: |
| 417 |
return lower_sysval_to_root_table(b, intrin, draw.api_sample_mask); |
| 418 |
|
| 419 |
case nir_intrinsic_load_sample_positions_agx: |
| 420 |
return lower_sysval_to_root_table(b, intrin, draw.ppp_multisamplectl); |
| 421 |
|
| 422 |
case nir_intrinsic_load_depth_never_agx: |
| 423 |
return lower_sysval_to_root_table(b, intrin, draw.force_never_in_shader); |
| 424 |
|
| 425 |
case nir_intrinsic_load_geometry_param_buffer_poly: |
| 426 |
return lower_sysval_to_root_table(b, intrin, draw.geometry_params); |
| 427 |
|
| 428 |
case nir_intrinsic_load_vs_outputs_poly: |
| 429 |
return lower_sysval_to_root_table(b, intrin, draw.vertex_outputs); |
| 430 |
|
| 431 |
case nir_intrinsic_load_tess_param_buffer_poly: |
| 432 |
return lower_sysval_to_root_table(b, intrin, draw.tess_params); |
| 433 |
|
| 434 |
case nir_intrinsic_load_vertex_param_buffer_poly: |
| 435 |
if (args->sw_stage == MESA_SHADER_VERTEX) { |
| 436 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 437 |
|
| 438 |
unsigned base = AGX_ABI_VUNI_VERTEX_PARAMS(args->nr_vbos); |
| 439 |
nir_def *val = nir_load_preamble(b, 1, 64, .base = base); |
| 440 |
nir_def_rewrite_uses(&intrin->def, val); |
| 441 |
|
| 442 |
return true; |
| 443 |
} else { |
| 444 |
return lower_sysval_to_root_table(b, intrin, draw.vertex_params); |
| 445 |
} |
| 446 |
|
| 447 |
case nir_intrinsic_load_rasterization_stream: |
| 448 |
return lower_sysval_to_root_table(b, intrin, draw.rasterization_stream); |
| 449 |
|
| 450 |
case nir_intrinsic_load_vbo_stride_agx: |
| 451 |
return lower_sysval_to_root_table( |
| 452 |
b, intrin, draw.attrib_strides[nir_src_as_uint(intrin->src[0])]); |
| 453 |
|
| 454 |
case nir_intrinsic_load_is_first_fan_agx: { |
| 455 |
unsigned offset = hk_root_descriptor_offset(draw.provoking); |
| 456 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 457 |
nir_def *val = load_root(b, 1, 16, nir_imm_int(b, offset), 2); |
| 458 |
nir_def_rewrite_uses(&intrin->def, nir_ieq_imm(b, val, 1)); |
| 459 |
return true; |
| 460 |
} |
| 461 |
|
| 462 |
case nir_intrinsic_load_provoking_last: { |
| 463 |
unsigned offset = hk_root_descriptor_offset(draw.provoking); |
| 464 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 465 |
nir_def *val = load_root(b, 1, 16, nir_imm_int(b, offset), 2); |
| 466 |
nir_def_rewrite_uses(&intrin->def, nir_b2b32(b, nir_ieq_imm(b, val, 2))); |
| 467 |
return true; |
| 468 |
} |
| 469 |
|
| 470 |
case nir_intrinsic_load_base_vertex: |
| 471 |
case nir_intrinsic_load_first_vertex: |
| 472 |
case nir_intrinsic_load_base_instance: |
| 473 |
case nir_intrinsic_load_draw_id: { |
| 474 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 475 |
|
| 476 |
unsigned base = AGX_ABI_VUNI_FIRST_VERTEX(args->nr_vbos); |
| 477 |
unsigned size = 32; |
| 478 |
|
| 479 |
if (intrin->intrinsic == nir_intrinsic_load_base_instance) { |
| 480 |
base = AGX_ABI_VUNI_BASE_INSTANCE(args->nr_vbos); |
| 481 |
} else if (intrin->intrinsic == nir_intrinsic_load_draw_id) { |
| 482 |
base = AGX_ABI_VUNI_DRAW_ID(args->nr_vbos); |
| 483 |
size = 16; |
| 484 |
} |
| 485 |
|
| 486 |
nir_def *val = nir_load_preamble(b, 1, size, .base = base); |
| 487 |
nir_def_rewrite_uses(&intrin->def, |
| 488 |
nir_u2uN(b, val, intrin->def.bit_size)); |
| 489 |
return true; |
| 490 |
} |
| 491 |
|
| 492 |
case nir_intrinsic_load_stat_query_address_poly: { |
| 493 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 494 |
|
| 495 |
unsigned off1 = hk_root_descriptor_offset(draw.pipeline_stats); |
| 496 |
unsigned off2 = hk_root_descriptor_offset(draw.pipeline_stats_flags); |
| 497 |
|
| 498 |
nir_def *base = load_root(b, 1, 64, nir_imm_int(b, off1), 8); |
| 499 |
nir_def *flags = load_root(b, 1, 16, nir_imm_int(b, off2), 2); |
| 500 |
|
| 501 |
unsigned query = nir_intrinsic_base(intrin); |
| 502 |
VkQueryPipelineStatisticFlagBits bit = translate_pipeline_stat_bit(query); |
| 503 |
|
| 504 |
/* Prefix sum to find the compacted offset */ |
| 505 |
nir_def *idx = nir_bit_count(b, nir_iand_imm(b, flags, bit - 1)); |
| 506 |
nir_def *addr = nir_iadd( |
| 507 |
b, base, nir_amul_imm(b, nir_u2u64(b, idx), sizeof(uint64_t))); |
| 508 |
|
| 509 |
/* The above returns garbage if the query isn't actually enabled, handle |
| 510 |
* that case. |
| 511 |
* |
| 512 |
* TODO: Optimize case where we *know* the query is present? |
| 513 |
*/ |
| 514 |
nir_def *present = nir_ine_imm(b, nir_iand_imm(b, flags, bit), 0); |
| 515 |
|
| 516 |
/* Sometimes we insert a GS internally, it should not contribute to GS |
| 517 |
* statistics. This is not strictly needed for Vulkan but vkd3d-proton |
| 518 |
* tests it and we should avoid the surprising behaviour. |
| 519 |
*/ |
| 520 |
if (query == PIPE_STAT_QUERY_GS_INVOCATIONS || |
| 521 |
query == PIPE_STAT_QUERY_GS_PRIMITIVES) { |
| 522 |
|
| 523 |
unsigned api_gs_offset = hk_root_descriptor_offset(draw.api_gs); |
| 524 |
nir_def *api_gs = |
| 525 |
load_root(b, 1, 16, nir_imm_int(b, api_gs_offset), 4); |
| 526 |
|
| 527 |
present = nir_iand(b, present, nir_ine_imm(b, api_gs, 0)); |
| 528 |
} |
| 529 |
|
| 530 |
addr = nir_bcsel(b, present, addr, |
| 531 |
nir_imm_int64(b, AGX_SCRATCH_PAGE_ADDRESS)); |
| 532 |
|
| 533 |
nir_def_rewrite_uses(&intrin->def, addr); |
| 534 |
return true; |
| 535 |
} |
| 536 |
|
| 537 |
default: |
| 538 |
return false; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
bool |
| 543 |
hk_lower_uvs_index(nir_shader *s, mesa_shader_stage sw_stage, unsigned nr_vbos) |
| 544 |
{ |
| 545 |
struct lower_uvs_args args = { |
| 546 |
.sw_stage = sw_stage, |
| 547 |
.nr_vbos = nr_vbos, |
| 548 |
}; |
| 549 |
return nir_shader_intrinsics_pass(s, lower_uvs_index, |
| 550 |
nir_metadata_control_flow, &args); |
| 551 |
} |
| 552 |
|
| 553 |
static bool |
| 554 |
try_lower_intrin(nir_builder *b, nir_intrinsic_instr *intrin, |
| 555 |
const struct lower_descriptors_ctx *ctx) |
| 556 |
{ |
| 557 |
switch (intrin->intrinsic) { |
| 558 |
case nir_intrinsic_load_constant: |
| 559 |
return lower_load_constant(b, intrin, ctx); |
| 560 |
|
| 561 |
case nir_intrinsic_load_vulkan_descriptor: |
| 562 |
return try_lower_load_vulkan_descriptor(b, intrin, ctx); |
| 563 |
|
| 564 |
case nir_intrinsic_load_workgroup_size: |
| 565 |
UNREACHABLE("Should have been lowered by nir_lower_cs_intrinsics()"); |
| 566 |
|
| 567 |
case nir_intrinsic_load_base_workgroup_id: |
| 568 |
return lower_sysval_to_root_table(b, intrin, cs.base_group); |
| 569 |
|
| 570 |
case nir_intrinsic_load_push_constant: |
| 571 |
return lower_load_push_constant(b, intrin, ctx); |
| 572 |
|
| 573 |
case nir_intrinsic_load_view_index: |
| 574 |
return lower_sysval_to_root_table(b, intrin, draw.view_index); |
| 575 |
|
| 576 |
case nir_intrinsic_image_deref_load: |
| 577 |
case nir_intrinsic_image_deref_sparse_load: |
| 578 |
case nir_intrinsic_image_deref_store: |
| 579 |
case nir_intrinsic_image_deref_atomic: |
| 580 |
case nir_intrinsic_image_deref_atomic_swap: |
| 581 |
case nir_intrinsic_image_deref_size: |
| 582 |
case nir_intrinsic_image_deref_samples: |
| 583 |
case nir_intrinsic_image_deref_store_block_agx: |
| 584 |
return lower_image_intrin(b, intrin, ctx); |
| 585 |
|
| 586 |
case nir_intrinsic_load_num_workgroups: { |
| 587 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 588 |
|
| 589 |
unsigned offset = hk_root_descriptor_offset(cs.group_count_addr); |
| 590 |
nir_def *ptr = load_root(b, 1, 64, nir_imm_int(b, offset), 4); |
| 591 |
nir_def *val = load_speculatable(b, 3, 32, ptr, 4); |
| 592 |
|
| 593 |
nir_def_rewrite_uses(&intrin->def, val); |
| 594 |
return true; |
| 595 |
} |
| 596 |
|
| 597 |
default: |
| 598 |
return false; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
static bool |
| 603 |
lower_tex(nir_builder *b, nir_tex_instr *tex, |
| 604 |
const struct lower_descriptors_ctx *ctx) |
| 605 |
{ |
| 606 |
b->cursor = nir_before_instr(&tex->instr); |
| 607 |
|
| 608 |
nir_def *texture = nir_steal_tex_src(tex, nir_tex_src_texture_deref); |
| 609 |
nir_def *sampler = nir_steal_tex_src(tex, nir_tex_src_sampler_deref); |
| 610 |
if (!texture) { |
| 611 |
assert(!sampler); |
| 612 |
return false; |
| 613 |
} |
| 614 |
|
| 615 |
nir_def *plane_ssa = nir_steal_tex_src(tex, nir_tex_src_plane); |
| 616 |
const uint32_t plane = |
| 617 |
plane_ssa ? nir_src_as_uint(nir_src_for_ssa(plane_ssa)) : 0; |
| 618 |
const uint64_t plane_offset_B = |
| 619 |
plane * sizeof(struct hk_sampled_image_descriptor); |
| 620 |
|
| 621 |
/* LOD bias is passed in the descriptor set, rather than embedded into |
| 622 |
* the sampler descriptor. There's no spot in the hardware descriptor, |
| 623 |
* plus this saves on precious sampler heap spots. |
| 624 |
*/ |
| 625 |
if (tex->op == nir_texop_lod_bias) { |
| 626 |
unsigned offs = |
| 627 |
offsetof(struct hk_sampled_image_descriptor, lod_bias_fp16); |
| 628 |
|
| 629 |
nir_def *bias = load_resource_deref_desc( |
| 630 |
b, 1, 16, nir_src_as_deref(nir_src_for_ssa(sampler)), |
| 631 |
plane_offset_B + offs, ctx); |
| 632 |
|
| 633 |
nir_def_replace(&tex->def, bias); |
| 634 |
return true; |
| 635 |
} |
| 636 |
|
| 637 |
if (tex->op == nir_texop_image_min_lod_agx) { |
| 638 |
assert(tex->dest_type == nir_type_float16 || |
| 639 |
tex->dest_type == nir_type_uint16); |
| 640 |
|
| 641 |
unsigned offs = |
| 642 |
tex->dest_type == nir_type_float16 |
| 643 |
? offsetof(struct hk_sampled_image_descriptor, min_lod_fp16) |
| 644 |
: offsetof(struct hk_sampled_image_descriptor, min_lod_uint16); |
| 645 |
|
| 646 |
nir_def *min = load_resource_deref_desc( |
| 647 |
b, 1, 16, nir_src_as_deref(nir_src_for_ssa(texture)), |
| 648 |
plane_offset_B + offs, ctx); |
| 649 |
|
| 650 |
nir_def_replace(&tex->def, min); |
| 651 |
return true; |
| 652 |
} |
| 653 |
|
| 654 |
if (tex->op == nir_texop_has_custom_border_color_agx) { |
| 655 |
unsigned offs = offsetof(struct hk_sampled_image_descriptor, |
| 656 |
clamp_0_sampler_index_or_negative); |
| 657 |
|
| 658 |
nir_def *res = load_resource_deref_desc( |
| 659 |
b, 1, 16, nir_src_as_deref(nir_src_for_ssa(sampler)), |
| 660 |
plane_offset_B + offs, ctx); |
| 661 |
|
| 662 |
nir_def_replace(&tex->def, nir_ige_imm(b, res, 0)); |
| 663 |
return true; |
| 664 |
} |
| 665 |
|
| 666 |
if (tex->op == nir_texop_custom_border_color_agx) { |
| 667 |
unsigned offs = offsetof(struct hk_sampled_image_descriptor, border); |
| 668 |
|
| 669 |
nir_def *border = load_resource_deref_desc( |
| 670 |
b, 4, 32, nir_src_as_deref(nir_src_for_ssa(sampler)), |
| 671 |
plane_offset_B + offs, ctx); |
| 672 |
|
| 673 |
nir_alu_type T = nir_alu_type_get_base_type(tex->dest_type); |
| 674 |
border = nir_convert_to_bit_size(b, border, T, tex->def.bit_size); |
| 675 |
|
| 676 |
nir_def_replace(&tex->def, border); |
| 677 |
return true; |
| 678 |
} |
| 679 |
|
| 680 |
{ |
| 681 |
unsigned offset_B = |
| 682 |
plane_offset_B + offsetof(struct hk_sampled_image_descriptor, tex); |
| 683 |
|
| 684 |
nir_def *handle = |
| 685 |
load_image_handle(b, ctx, nir_src_for_ssa(texture), offset_B); |
| 686 |
|
| 687 |
nir_tex_instr_add_src(tex, nir_tex_src_texture_handle, handle); |
| 688 |
} |
| 689 |
|
| 690 |
if (sampler != NULL) { |
| 691 |
unsigned offs = |
| 692 |
offsetof(struct hk_sampled_image_descriptor, sampler_index); |
| 693 |
bool clamp_to_0 = tex->backend_flags & AGX_TEXTURE_FLAG_CLAMP_TO_0; |
| 694 |
|
| 695 |
if (clamp_to_0) { |
| 696 |
offs = offsetof(struct hk_sampled_image_descriptor, |
| 697 |
clamp_0_sampler_index_or_negative); |
| 698 |
} |
| 699 |
|
| 700 |
nir_def *index = load_resource_deref_desc( |
| 701 |
b, 1, 16, nir_src_as_deref(nir_src_for_ssa(sampler)), |
| 702 |
plane_offset_B + offs, ctx); |
| 703 |
|
| 704 |
if (!clamp_to_0) { |
| 705 |
uint32_t set, binding; |
| 706 |
nir_def *idx; |
| 707 |
get_resource_deref_binding(b, |
| 708 |
nir_src_as_deref(nir_src_for_ssa(sampler)), |
| 709 |
&set, &binding, &idx); |
| 710 |
|
| 711 |
const struct hk_descriptor_set_binding_layout *binding_layout = |
| 712 |
get_binding_layout(set, binding, ctx); |
| 713 |
|
| 714 |
if (ctx->clamp_desc_array_bounds) |
| 715 |
idx = |
| 716 |
nir_umin(b, idx, nir_imm_int(b, binding_layout->array_size - 1)); |
| 717 |
|
| 718 |
assert(binding_layout->stride > 0); |
| 719 |
nir_def *desc_offs_B = nir_iadd_imm( |
| 720 |
b, nir_amul_imm(b, idx, binding_layout->stride), |
| 721 |
binding_layout->offset + plane_offset_B + |
| 722 |
offsetof(struct hk_sampled_image_descriptor, sampler)); |
| 723 |
|
| 724 |
index = |
| 725 |
nir_bindless_sampler_agx(b, desc_offs_B, index, .desc_set = set); |
| 726 |
} |
| 727 |
|
| 728 |
nir_tex_instr_add_src(tex, nir_tex_src_sampler_handle, index); |
| 729 |
} |
| 730 |
|
| 731 |
return true; |
| 732 |
} |
| 733 |
|
| 734 |
static bool |
| 735 |
try_lower_descriptors_instr(nir_builder *b, nir_instr *instr, void *_data) |
| 736 |
{ |
| 737 |
const struct lower_descriptors_ctx *ctx = _data; |
| 738 |
|
| 739 |
switch (instr->type) { |
| 740 |
case nir_instr_type_tex: |
| 741 |
return lower_tex(b, nir_instr_as_tex(instr), ctx); |
| 742 |
case nir_instr_type_intrinsic: |
| 743 |
return try_lower_intrin(b, nir_instr_as_intrinsic(instr), ctx); |
| 744 |
default: |
| 745 |
return false; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
static bool |
| 750 |
lower_ssbo_resource_index(nir_builder *b, nir_intrinsic_instr *intrin, |
| 751 |
const struct lower_descriptors_ctx *ctx) |
| 752 |
{ |
| 753 |
const nir_descriptor_type desc_type = nir_intrinsic_desc_type(intrin); |
| 754 |
if (desc_type != nir_descriptor_type_storage_buffer) |
| 755 |
return false; |
| 756 |
|
| 757 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 758 |
|
| 759 |
uint32_t set = nir_intrinsic_desc_set(intrin); |
| 760 |
uint32_t binding = nir_intrinsic_binding(intrin); |
| 761 |
nir_def *index = intrin->src[0].ssa; |
| 762 |
|
| 763 |
const struct hk_descriptor_set_binding_layout *binding_layout = |
| 764 |
get_binding_layout(set, binding, ctx); |
| 765 |
|
| 766 |
nir_def *binding_addr; |
| 767 |
uint8_t binding_stride; |
| 768 |
switch (binding_layout->type) { |
| 769 |
case VK_DESCRIPTOR_TYPE_MUTABLE_EXT: |
| 770 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER: { |
| 771 |
nir_def *set_addr = nir_load_descriptor_set_agx(b, set); |
| 772 |
binding_addr = nir_iadd_imm(b, set_addr, binding_layout->offset); |
| 773 |
binding_stride = binding_layout->stride; |
| 774 |
break; |
| 775 |
} |
| 776 |
|
| 777 |
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC: { |
| 778 |
const uint32_t root_desc_addr_offset = |
| 779 |
hk_root_descriptor_offset(root_desc_addr); |
| 780 |
|
| 781 |
nir_def *root_desc_addr = |
| 782 |
load_root(b, 1, 64, nir_imm_int(b, root_desc_addr_offset), 8); |
| 783 |
|
| 784 |
nir_def *dynamic_buffer_start = |
| 785 |
nir_iadd_imm(b, load_dynamic_buffer_start(b, set, ctx), |
| 786 |
binding_layout->dynamic_buffer_index); |
| 787 |
|
| 788 |
nir_def *dynamic_binding_offset = |
| 789 |
nir_iadd_imm(b, |
| 790 |
nir_amul_imm(b, dynamic_buffer_start, |
| 791 |
sizeof(struct hk_buffer_address)), |
| 792 |
hk_root_descriptor_offset(dynamic_buffers)); |
| 793 |
|
| 794 |
binding_addr = |
| 795 |
nir_iadd(b, root_desc_addr, nir_u2u64(b, dynamic_binding_offset)); |
| 796 |
binding_stride = sizeof(struct hk_buffer_address); |
| 797 |
break; |
| 798 |
} |
| 799 |
|
| 800 |
default: |
| 801 |
UNREACHABLE("Not an SSBO descriptor"); |
| 802 |
} |
| 803 |
|
| 804 |
/* Tuck the stride in the top 8 bits of the binding address */ |
| 805 |
binding_addr = nir_ior_imm(b, binding_addr, (uint64_t)binding_stride << 56); |
| 806 |
|
| 807 |
const uint32_t binding_size = binding_layout->array_size * binding_stride; |
| 808 |
nir_def *offset_in_binding = nir_amul_imm(b, index, binding_stride); |
| 809 |
|
| 810 |
nir_def *addr = nir_vec4(b, nir_unpack_64_2x32_split_x(b, binding_addr), |
| 811 |
nir_unpack_64_2x32_split_y(b, binding_addr), |
| 812 |
nir_imm_int(b, binding_size), offset_in_binding); |
| 813 |
|
| 814 |
nir_def_rewrite_uses(&intrin->def, addr); |
| 815 |
|
| 816 |
return true; |
| 817 |
} |
| 818 |
|
| 819 |
static bool |
| 820 |
lower_ssbo_resource_reindex(nir_builder *b, nir_intrinsic_instr *intrin, |
| 821 |
const struct lower_descriptors_ctx *ctx) |
| 822 |
{ |
| 823 |
const nir_descriptor_type desc_type = nir_intrinsic_desc_type(intrin); |
| 824 |
if (desc_type != nir_descriptor_type_storage_buffer) |
| 825 |
return false; |
| 826 |
|
| 827 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 828 |
|
| 829 |
nir_def *addr = intrin->src[0].ssa; |
| 830 |
nir_def *index = intrin->src[1].ssa; |
| 831 |
|
| 832 |
nir_def *addr_high32 = nir_channel(b, addr, 1); |
| 833 |
nir_def *stride = nir_ushr_imm(b, addr_high32, 24); |
| 834 |
nir_def *offset = nir_imul(b, index, stride); |
| 835 |
|
| 836 |
addr = nir_build_addr_iadd(b, addr, ctx->ssbo_addr_format, nir_var_mem_ssbo, |
| 837 |
offset); |
| 838 |
nir_def_rewrite_uses(&intrin->def, addr); |
| 839 |
|
| 840 |
return true; |
| 841 |
} |
| 842 |
|
| 843 |
static bool |
| 844 |
lower_load_ssbo_descriptor(nir_builder *b, nir_intrinsic_instr *intrin, |
| 845 |
const struct lower_descriptors_ctx *ctx) |
| 846 |
{ |
| 847 |
const nir_descriptor_type desc_type = nir_intrinsic_desc_type(intrin); |
| 848 |
if (desc_type != nir_descriptor_type_storage_buffer) |
| 849 |
return false; |
| 850 |
|
| 851 |
b->cursor = nir_instr_remove(&intrin->instr); |
| 852 |
|
| 853 |
nir_def *addr = intrin->src[0].ssa; |
| 854 |
|
| 855 |
nir_def *desc; |
| 856 |
switch (ctx->ssbo_addr_format) { |
| 857 |
case nir_address_format_64bit_global_32bit_offset: { |
| 858 |
nir_def *base = nir_pack_64_2x32(b, nir_trim_vector(b, addr, 2)); |
| 859 |
nir_def *offset = nir_channel(b, addr, 3); |
| 860 |
/* Mask off the binding stride */ |
| 861 |
base = nir_iand_imm(b, base, BITFIELD64_MASK(56)); |
| 862 |
desc = nir_load_global_constant_offset(b, 4, 32, base, offset, |
| 863 |
.align_mul = 16, .align_offset = 0, |
| 864 |
.access = ACCESS_CAN_SPECULATE); |
| 865 |
break; |
| 866 |
} |
| 867 |
|
| 868 |
case nir_address_format_64bit_bounded_global: { |
| 869 |
nir_def *base = nir_pack_64_2x32(b, nir_trim_vector(b, addr, 2)); |
| 870 |
nir_def *size = nir_channel(b, addr, 2); |
| 871 |
nir_def *offset = nir_channel(b, addr, 3); |
| 872 |
/* Mask off the binding stride */ |
| 873 |
base = nir_iand_imm(b, base, BITFIELD64_MASK(56)); |
| 874 |
desc = nir_load_global_constant_bounded( |
| 875 |
b, 4, 32, base, offset, size, .align_mul = 16, .align_offset = 0, |
| 876 |
.access = ACCESS_CAN_SPECULATE); |
| 877 |
break; |
| 878 |
} |
| 879 |
|
| 880 |
default: |
| 881 |
UNREACHABLE("Unknown address mode"); |
| 882 |
} |
| 883 |
|
| 884 |
nir_def_rewrite_uses(&intrin->def, desc); |
| 885 |
|
| 886 |
return true; |
| 887 |
} |
| 888 |
|
| 889 |
static bool |
| 890 |
lower_ssbo_descriptor(nir_builder *b, nir_intrinsic_instr *intr, void *_data) |
| 891 |
{ |
| 892 |
const struct lower_descriptors_ctx *ctx = _data; |
| 893 |
|
| 894 |
switch (intr->intrinsic) { |
| 895 |
case nir_intrinsic_vulkan_resource_index: |
| 896 |
return lower_ssbo_resource_index(b, intr, ctx); |
| 897 |
case nir_intrinsic_vulkan_resource_reindex: |
| 898 |
return lower_ssbo_resource_reindex(b, intr, ctx); |
| 899 |
case nir_intrinsic_load_vulkan_descriptor: |
| 900 |
return lower_load_ssbo_descriptor(b, intr, ctx); |
| 901 |
default: |
| 902 |
return false; |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
bool |
| 907 |
hk_nir_lower_descriptors(nir_shader *nir, |
| 908 |
const struct vk_pipeline_robustness_state *rs, |
| 909 |
uint32_t set_layout_count, |
| 910 |
struct vk_descriptor_set_layout *const *set_layouts) |
| 911 |
{ |
| 912 |
struct lower_descriptors_ctx ctx = { |
| 913 |
.clamp_desc_array_bounds = |
| 914 |
rs->storage_buffers != |
| 915 |
VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT || |
| 916 |
|
| 917 |
rs->uniform_buffers != |
| 918 |
VK_PIPELINE_ROBUSTNESS_BUFFER_BEHAVIOR_DISABLED_EXT || |
| 919 |
|
| 920 |
rs->images != VK_PIPELINE_ROBUSTNESS_IMAGE_BEHAVIOR_DISABLED_EXT, |
| 921 |
|
| 922 |
.ssbo_addr_format = hk_buffer_addr_format(rs->storage_buffers), |
| 923 |
.ubo_addr_format = hk_buffer_addr_format(rs->uniform_buffers), |
| 924 |
}; |
| 925 |
|
| 926 |
assert(set_layout_count <= HK_MAX_SETS); |
| 927 |
for (uint32_t s = 0; s < set_layout_count; s++) { |
| 928 |
if (set_layouts[s] != NULL) |
| 929 |
ctx.set_layouts[s] = vk_to_hk_descriptor_set_layout(set_layouts[s]); |
| 930 |
} |
| 931 |
|
| 932 |
/* First lower everything but complex SSBOs, then lower complex SSBOs. |
| 933 |
* |
| 934 |
* TODO: See if we can unify this, not sure if the fast path matters on |
| 935 |
* Apple. This is inherited from NVK. |
| 936 |
*/ |
| 937 |
bool pass_lower_descriptors = nir_shader_instructions_pass( |
| 938 |
nir, try_lower_descriptors_instr, nir_metadata_control_flow, &ctx); |
| 939 |
|
| 940 |
bool pass_lower_ssbo = nir_shader_intrinsics_pass( |
| 941 |
nir, lower_ssbo_descriptor, nir_metadata_control_flow, &ctx); |
| 942 |
|
| 943 |
return pass_lower_descriptors || pass_lower_ssbo; |
| 944 |
} |