Path: blob/21.2-virgl/src/intel/vulkan/anv_nir_apply_pipeline_layout.c
4547 views
/*1* Copyright © 2015 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223#include "anv_nir.h"24#include "program/prog_parameter.h"25#include "nir/nir_builder.h"26#include "compiler/brw_nir.h"27#include "util/mesa-sha1.h"28#include "util/set.h"2930/* Sampler tables don't actually have a maximum size but we pick one just so31* that we don't end up emitting too much state on-the-fly.32*/33#define MAX_SAMPLER_TABLE_SIZE 12834#define BINDLESS_OFFSET 2553536struct apply_pipeline_layout_state {37const struct anv_physical_device *pdevice;3839const struct anv_pipeline_layout *layout;40bool add_bounds_checks;41nir_address_format desc_addr_format;42nir_address_format ssbo_addr_format;43nir_address_format ubo_addr_format;4445/* Place to flag lowered instructions so we don't lower them twice */46struct set *lowered_instrs;4748bool uses_constants;49bool has_dynamic_buffers;50uint8_t constants_offset;51struct {52bool desc_buffer_used;53uint8_t desc_offset;5455uint8_t *use_count;56uint8_t *surface_offsets;57uint8_t *sampler_offsets;58} set[MAX_SETS];59};6061static nir_address_format62addr_format_for_desc_type(VkDescriptorType desc_type,63struct apply_pipeline_layout_state *state)64{65switch (desc_type) {66case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:67case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:68return state->ssbo_addr_format;6970case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:71case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:72return state->ubo_addr_format;7374case VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT:75return state->desc_addr_format;7677default:78unreachable("Unsupported descriptor type");79}80}8182static void83add_binding(struct apply_pipeline_layout_state *state,84uint32_t set, uint32_t binding)85{86const struct anv_descriptor_set_binding_layout *bind_layout =87&state->layout->set[set].layout->binding[binding];8889if (state->set[set].use_count[binding] < UINT8_MAX)90state->set[set].use_count[binding]++;9192/* Only flag the descriptor buffer as used if there's actually data for93* this binding. This lets us be lazy and call this function constantly94* without worrying about unnecessarily enabling the buffer.95*/96if (anv_descriptor_size(bind_layout))97state->set[set].desc_buffer_used = true;98}99100static void101add_deref_src_binding(struct apply_pipeline_layout_state *state, nir_src src)102{103nir_deref_instr *deref = nir_src_as_deref(src);104nir_variable *var = nir_deref_instr_get_variable(deref);105add_binding(state, var->data.descriptor_set, var->data.binding);106}107108static void109add_tex_src_binding(struct apply_pipeline_layout_state *state,110nir_tex_instr *tex, nir_tex_src_type deref_src_type)111{112int deref_src_idx = nir_tex_instr_src_index(tex, deref_src_type);113if (deref_src_idx < 0)114return;115116add_deref_src_binding(state, tex->src[deref_src_idx].src);117}118119static bool120get_used_bindings(UNUSED nir_builder *_b, nir_instr *instr, void *_state)121{122struct apply_pipeline_layout_state *state = _state;123124switch (instr->type) {125case nir_instr_type_intrinsic: {126nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);127switch (intrin->intrinsic) {128case nir_intrinsic_vulkan_resource_index:129add_binding(state, nir_intrinsic_desc_set(intrin),130nir_intrinsic_binding(intrin));131break;132133case nir_intrinsic_image_deref_load:134case nir_intrinsic_image_deref_store:135case nir_intrinsic_image_deref_atomic_add:136case nir_intrinsic_image_deref_atomic_imin:137case nir_intrinsic_image_deref_atomic_umin:138case nir_intrinsic_image_deref_atomic_imax:139case nir_intrinsic_image_deref_atomic_umax:140case nir_intrinsic_image_deref_atomic_and:141case nir_intrinsic_image_deref_atomic_or:142case nir_intrinsic_image_deref_atomic_xor:143case nir_intrinsic_image_deref_atomic_exchange:144case nir_intrinsic_image_deref_atomic_comp_swap:145case nir_intrinsic_image_deref_size:146case nir_intrinsic_image_deref_samples:147case nir_intrinsic_image_deref_load_param_intel:148case nir_intrinsic_image_deref_load_raw_intel:149case nir_intrinsic_image_deref_store_raw_intel:150add_deref_src_binding(state, intrin->src[0]);151break;152153case nir_intrinsic_load_constant:154state->uses_constants = true;155break;156157default:158break;159}160break;161}162case nir_instr_type_tex: {163nir_tex_instr *tex = nir_instr_as_tex(instr);164add_tex_src_binding(state, tex, nir_tex_src_texture_deref);165add_tex_src_binding(state, tex, nir_tex_src_sampler_deref);166break;167}168default:169break;170}171172return false;173}174175static nir_intrinsic_instr *176find_descriptor_for_index_src(nir_src src,177struct apply_pipeline_layout_state *state)178{179nir_intrinsic_instr *intrin = nir_src_as_intrinsic(src);180181while (intrin && intrin->intrinsic == nir_intrinsic_vulkan_resource_reindex)182intrin = nir_src_as_intrinsic(intrin->src[0]);183184if (!intrin || intrin->intrinsic != nir_intrinsic_vulkan_resource_index)185return NULL;186187return intrin;188}189190static bool191descriptor_has_bti(nir_intrinsic_instr *intrin,192struct apply_pipeline_layout_state *state)193{194assert(intrin->intrinsic == nir_intrinsic_vulkan_resource_index);195196uint32_t set = nir_intrinsic_desc_set(intrin);197uint32_t binding = nir_intrinsic_binding(intrin);198const struct anv_descriptor_set_binding_layout *bind_layout =199&state->layout->set[set].layout->binding[binding];200201uint32_t surface_index;202if (bind_layout->data & ANV_DESCRIPTOR_INLINE_UNIFORM)203surface_index = state->set[set].desc_offset;204else205surface_index = state->set[set].surface_offsets[binding];206207/* Only lower to a BTI message if we have a valid binding table index. */208return surface_index < MAX_BINDING_TABLE_SIZE;209}210211static nir_address_format212descriptor_address_format(nir_intrinsic_instr *intrin,213struct apply_pipeline_layout_state *state)214{215assert(intrin->intrinsic == nir_intrinsic_vulkan_resource_index);216217uint32_t set = nir_intrinsic_desc_set(intrin);218uint32_t binding = nir_intrinsic_binding(intrin);219const struct anv_descriptor_set_binding_layout *bind_layout =220&state->layout->set[set].layout->binding[binding];221222return addr_format_for_desc_type(bind_layout->type, state);223}224225static nir_intrinsic_instr *226nir_deref_find_descriptor(nir_deref_instr *deref,227struct apply_pipeline_layout_state *state)228{229while (1) {230/* Nothing we will use this on has a variable */231assert(deref->deref_type != nir_deref_type_var);232233nir_deref_instr *parent = nir_src_as_deref(deref->parent);234if (!parent)235break;236237deref = parent;238}239assert(deref->deref_type == nir_deref_type_cast);240241nir_intrinsic_instr *intrin = nir_src_as_intrinsic(deref->parent);242if (!intrin || intrin->intrinsic != nir_intrinsic_load_vulkan_descriptor)243return false;244245return find_descriptor_for_index_src(intrin->src[0], state);246}247248static nir_ssa_def *249build_load_descriptor_mem(nir_builder *b,250nir_ssa_def *desc_addr, unsigned desc_offset,251unsigned num_components, unsigned bit_size,252struct apply_pipeline_layout_state *state)253254{255switch (state->desc_addr_format) {256case nir_address_format_64bit_global_32bit_offset: {257nir_ssa_def *base_addr =258nir_pack_64_2x32(b, nir_channels(b, desc_addr, 0x3));259nir_ssa_def *offset32 =260nir_iadd_imm(b, nir_channel(b, desc_addr, 3), desc_offset);261262return nir_load_global_constant_offset(b, num_components, bit_size,263base_addr, offset32,264.align_mul = 8,265.align_offset = desc_offset % 8);266}267268case nir_address_format_32bit_index_offset: {269nir_ssa_def *surface_index = nir_channel(b, desc_addr, 0);270nir_ssa_def *offset32 =271nir_iadd_imm(b, nir_channel(b, desc_addr, 1), desc_offset);272273return nir_load_ubo(b, num_components, bit_size,274surface_index, offset32,275.align_mul = 8,276.align_offset = desc_offset % 8,277.range_base = 0,278.range = ~0);279}280281default:282unreachable("Unsupported address format");283}284}285286/** Build a Vulkan resource index287*288* A "resource index" is the term used by our SPIR-V parser and the relevant289* NIR intrinsics for a reference into a descriptor set. It acts much like a290* deref in NIR except that it accesses opaque descriptors instead of memory.291*292* Coming out of SPIR-V, both the resource indices (in the form of293* vulkan_resource_[re]index intrinsics) and the memory derefs (in the form294* of nir_deref_instr) use the same vector component/bit size. The meaning295* of those values for memory derefs (nir_deref_instr) is given by the296* nir_address_format associated with the descriptor type. For resource297* indices, it's an entirely internal to ANV encoding which describes, in some298* sense, the address of the descriptor. Thanks to the NIR/SPIR-V rules, it299* must be packed into the same size SSA values as a memory address. For this300* reason, the actual encoding may depend both on the address format for301* memory derefs and the descriptor address format.302*303* The load_vulkan_descriptor intrinsic exists to provide a transition point304* between these two forms of derefs: descriptor and memory.305*/306static nir_ssa_def *307build_res_index(nir_builder *b, uint32_t set, uint32_t binding,308nir_ssa_def *array_index, nir_address_format addr_format,309struct apply_pipeline_layout_state *state)310{311const struct anv_descriptor_set_binding_layout *bind_layout =312&state->layout->set[set].layout->binding[binding];313314uint32_t array_size = bind_layout->array_size;315316switch (addr_format) {317case nir_address_format_64bit_global_32bit_offset:318case nir_address_format_64bit_bounded_global: {319uint32_t set_idx;320switch (state->desc_addr_format) {321case nir_address_format_64bit_global_32bit_offset:322set_idx = set;323break;324325case nir_address_format_32bit_index_offset:326assert(state->set[set].desc_offset < MAX_BINDING_TABLE_SIZE);327set_idx = state->set[set].desc_offset;328break;329330default:331unreachable("Unsupported address format");332}333334assert(bind_layout->dynamic_offset_index < MAX_DYNAMIC_BUFFERS);335uint32_t dynamic_offset_index = 0xff; /* No dynamic offset */336if (bind_layout->dynamic_offset_index >= 0) {337dynamic_offset_index =338state->layout->set[set].dynamic_offset_start +339bind_layout->dynamic_offset_index;340}341342const uint32_t packed = (set_idx << 16) | dynamic_offset_index;343344return nir_vec4(b, nir_imm_int(b, packed),345nir_imm_int(b, bind_layout->descriptor_offset),346nir_imm_int(b, array_size - 1),347array_index);348}349350case nir_address_format_32bit_index_offset: {351assert(state->desc_addr_format == nir_address_format_32bit_index_offset);352if (bind_layout->type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {353uint32_t surface_index = state->set[set].desc_offset;354return nir_imm_ivec2(b, surface_index,355bind_layout->descriptor_offset);356} else {357uint32_t surface_index = state->set[set].surface_offsets[binding];358assert(array_size > 0 && array_size <= UINT16_MAX);359assert(surface_index <= UINT16_MAX);360uint32_t packed = ((array_size - 1) << 16) | surface_index;361return nir_vec2(b, array_index, nir_imm_int(b, packed));362}363}364365default:366unreachable("Unsupported address format");367}368}369370struct res_index_defs {371nir_ssa_def *set_idx;372nir_ssa_def *dyn_offset_base;373nir_ssa_def *desc_offset_base;374nir_ssa_def *array_index;375};376377static struct res_index_defs378unpack_res_index(nir_builder *b, nir_ssa_def *index)379{380struct res_index_defs defs;381382nir_ssa_def *packed = nir_channel(b, index, 0);383defs.set_idx = nir_extract_u16(b, packed, nir_imm_int(b, 1));384defs.dyn_offset_base = nir_extract_u16(b, packed, nir_imm_int(b, 0));385386defs.desc_offset_base = nir_channel(b, index, 1);387defs.array_index = nir_umin(b, nir_channel(b, index, 2),388nir_channel(b, index, 3));389390return defs;391}392393/** Adjust a Vulkan resource index394*395* This is the equivalent of nir_deref_type_ptr_as_array for resource indices.396* For array descriptors, it allows us to adjust the array index. Thanks to397* variable pointers, we cannot always fold this re-index operation into the398* vulkan_resource_index intrinsic and we have to do it based on nothing but399* the address format.400*/401static nir_ssa_def *402build_res_reindex(nir_builder *b, nir_ssa_def *orig, nir_ssa_def *delta,403nir_address_format addr_format)404{405switch (addr_format) {406case nir_address_format_64bit_global_32bit_offset:407case nir_address_format_64bit_bounded_global:408return nir_vec4(b, nir_channel(b, orig, 0),409nir_channel(b, orig, 1),410nir_channel(b, orig, 2),411nir_iadd(b, nir_channel(b, orig, 3), delta));412413case nir_address_format_32bit_index_offset:414return nir_vec2(b, nir_iadd(b, nir_channel(b, orig, 0), delta),415nir_channel(b, orig, 1));416417default:418unreachable("Unhandled address format");419}420}421422/** Get the address for a descriptor given its resource index423*424* Because of the re-indexing operations, we can't bounds check descriptor425* array access until we have the final index. That means we end up doing the426* bounds check here, if needed. See unpack_res_index() for more details.427*428* This function takes both a bind_layout and a desc_type which are used to429* determine the descriptor stride for array descriptors. The bind_layout is430* optional for buffer descriptor types.431*/432static nir_ssa_def *433build_desc_addr(nir_builder *b,434const struct anv_descriptor_set_binding_layout *bind_layout,435const VkDescriptorType desc_type,436nir_ssa_def *index, nir_address_format addr_format,437struct apply_pipeline_layout_state *state)438{439switch (addr_format) {440case nir_address_format_64bit_global_32bit_offset:441case nir_address_format_64bit_bounded_global: {442struct res_index_defs res = unpack_res_index(b, index);443444nir_ssa_def *desc_offset = res.desc_offset_base;445if (desc_type != VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {446/* Compute the actual descriptor offset. For inline uniform blocks,447* the array index is ignored as they are only allowed to be a single448* descriptor (not an array) and there is no concept of a "stride".449*450* We use the bind_layout, if available, because it provides a more451* accurate descriptor size.452*/453const unsigned stride = bind_layout ?454anv_descriptor_size(bind_layout) :455anv_descriptor_type_size(state->pdevice, desc_type);456457desc_offset =458nir_iadd(b, desc_offset, nir_imul_imm(b, res.array_index, stride));459}460461switch (state->desc_addr_format) {462case nir_address_format_64bit_global_32bit_offset: {463nir_ssa_def *base_addr =464nir_load_desc_set_address_intel(b, res.set_idx);465return nir_vec4(b, nir_unpack_64_2x32_split_x(b, base_addr),466nir_unpack_64_2x32_split_y(b, base_addr),467nir_imm_int(b, UINT32_MAX),468desc_offset);469}470471case nir_address_format_32bit_index_offset:472return nir_vec2(b, res.set_idx, desc_offset);473474default:475unreachable("Unhandled address format");476}477}478479case nir_address_format_32bit_index_offset:480assert(desc_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT);481assert(state->desc_addr_format == nir_address_format_32bit_index_offset);482return index;483484default:485unreachable("Unhandled address format");486}487}488489/** Convert a Vulkan resource index into a buffer address490*491* In some cases, this does a memory load from the descriptor set and, in492* others, it simply converts from one form to another.493*494* See build_res_index for details about each resource index format.495*/496static nir_ssa_def *497build_buffer_addr_for_res_index(nir_builder *b,498const VkDescriptorType desc_type,499nir_ssa_def *res_index,500nir_address_format addr_format,501struct apply_pipeline_layout_state *state)502{503if (desc_type == VK_DESCRIPTOR_TYPE_INLINE_UNIFORM_BLOCK_EXT) {504assert(addr_format == state->desc_addr_format);505return build_desc_addr(b, NULL, desc_type, res_index, addr_format, state);506} else if (addr_format == nir_address_format_32bit_index_offset) {507nir_ssa_def *array_index = nir_channel(b, res_index, 0);508nir_ssa_def *packed = nir_channel(b, res_index, 1);509nir_ssa_def *array_max = nir_extract_u16(b, packed, nir_imm_int(b, 1));510nir_ssa_def *surface_index = nir_extract_u16(b, packed, nir_imm_int(b, 0));511512if (state->add_bounds_checks)513array_index = nir_umin(b, array_index, array_max);514515return nir_vec2(b, nir_iadd(b, surface_index, array_index),516nir_imm_int(b, 0));517}518519nir_ssa_def *desc_addr =520build_desc_addr(b, NULL, desc_type, res_index, addr_format, state);521522nir_ssa_def *desc = build_load_descriptor_mem(b, desc_addr, 0, 4, 32, state);523524if (state->has_dynamic_buffers) {525struct res_index_defs res = unpack_res_index(b, res_index);526527/* This shader has dynamic offsets and we have no way of knowing528* (save from the dynamic offset base index) if this buffer has a529* dynamic offset.530*/531nir_ssa_def *dyn_offset_idx =532nir_iadd(b, res.dyn_offset_base, res.array_index);533if (state->add_bounds_checks) {534dyn_offset_idx = nir_umin(b, dyn_offset_idx,535nir_imm_int(b, MAX_DYNAMIC_BUFFERS));536}537538nir_ssa_def *dyn_load =539nir_load_push_constant(b, 1, 32, nir_imul_imm(b, dyn_offset_idx, 4),540.base = offsetof(struct anv_push_constants, dynamic_offsets),541.range = MAX_DYNAMIC_BUFFERS * 4);542543nir_ssa_def *dynamic_offset =544nir_bcsel(b, nir_ieq_imm(b, res.dyn_offset_base, 0xff),545nir_imm_int(b, 0), dyn_load);546547/* The dynamic offset gets added to the base pointer so that we548* have a sliding window range.549*/550nir_ssa_def *base_ptr =551nir_pack_64_2x32(b, nir_channels(b, desc, 0x3));552base_ptr = nir_iadd(b, base_ptr, nir_u2u64(b, dynamic_offset));553desc = nir_vec4(b, nir_unpack_64_2x32_split_x(b, base_ptr),554nir_unpack_64_2x32_split_y(b, base_ptr),555nir_channel(b, desc, 2),556nir_channel(b, desc, 3));557}558559/* The last element of the vec4 is always zero.560*561* See also struct anv_address_range_descriptor562*/563return nir_vec4(b, nir_channel(b, desc, 0),564nir_channel(b, desc, 1),565nir_channel(b, desc, 2),566nir_imm_int(b, 0));567}568569/** Loads descriptor memory for a variable-based deref chain570*571* The deref chain has to terminate at a variable with a descriptor_set and572* binding set. This is used for images, textures, and samplers.573*/574static nir_ssa_def *575build_load_var_deref_descriptor_mem(nir_builder *b, nir_deref_instr *deref,576unsigned desc_offset,577unsigned num_components, unsigned bit_size,578struct apply_pipeline_layout_state *state)579{580nir_variable *var = nir_deref_instr_get_variable(deref);581582const uint32_t set = var->data.descriptor_set;583const uint32_t binding = var->data.binding;584const struct anv_descriptor_set_binding_layout *bind_layout =585&state->layout->set[set].layout->binding[binding];586587nir_ssa_def *array_index;588if (deref->deref_type != nir_deref_type_var) {589assert(deref->deref_type == nir_deref_type_array);590assert(nir_deref_instr_parent(deref)->deref_type == nir_deref_type_var);591assert(deref->arr.index.is_ssa);592array_index = deref->arr.index.ssa;593} else {594array_index = nir_imm_int(b, 0);595}596597/* It doesn't really matter what address format we choose as everything598* will constant-fold nicely. Choose one that uses the actual descriptor599* buffer so we don't run into issues index/offset assumptions.600*/601const nir_address_format addr_format =602nir_address_format_64bit_bounded_global;603604nir_ssa_def *res_index =605build_res_index(b, set, binding, array_index, addr_format, state);606607nir_ssa_def *desc_addr =608build_desc_addr(b, bind_layout, bind_layout->type,609res_index, addr_format, state);610611return build_load_descriptor_mem(b, desc_addr, desc_offset,612num_components, bit_size, state);613}614615/** A recursive form of build_res_index()616*617* This recursively walks a resource [re]index chain and builds the resource618* index. It places the new code with the resource [re]index operation in the619* hopes of better CSE. This means the cursor is not where you left it when620* this function returns.621*/622static nir_ssa_def *623build_res_index_for_chain(nir_builder *b, nir_intrinsic_instr *intrin,624nir_address_format addr_format,625uint32_t *set, uint32_t *binding,626struct apply_pipeline_layout_state *state)627{628if (intrin->intrinsic == nir_intrinsic_vulkan_resource_index) {629b->cursor = nir_before_instr(&intrin->instr);630assert(intrin->src[0].is_ssa);631*set = nir_intrinsic_desc_set(intrin);632*binding = nir_intrinsic_binding(intrin);633return build_res_index(b, *set, *binding, intrin->src[0].ssa,634addr_format, state);635} else {636assert(intrin->intrinsic == nir_intrinsic_vulkan_resource_reindex);637nir_intrinsic_instr *parent = nir_src_as_intrinsic(intrin->src[0]);638nir_ssa_def *index =639build_res_index_for_chain(b, parent, addr_format,640set, binding, state);641642b->cursor = nir_before_instr(&intrin->instr);643644assert(intrin->src[1].is_ssa);645return build_res_reindex(b, index, intrin->src[1].ssa, addr_format);646}647}648649/** Builds a buffer address for a given vulkan [re]index intrinsic650*651* The cursor is not where you left it when this function returns.652*/653static nir_ssa_def *654build_buffer_addr_for_idx_intrin(nir_builder *b,655nir_intrinsic_instr *idx_intrin,656nir_address_format addr_format,657struct apply_pipeline_layout_state *state)658{659uint32_t set = UINT32_MAX, binding = UINT32_MAX;660nir_ssa_def *res_index =661build_res_index_for_chain(b, idx_intrin, addr_format,662&set, &binding, state);663664const struct anv_descriptor_set_binding_layout *bind_layout =665&state->layout->set[set].layout->binding[binding];666667return build_buffer_addr_for_res_index(b, bind_layout->type,668res_index, addr_format, state);669}670671/** Builds a buffer address for deref chain672*673* This assumes that you can chase the chain all the way back to the original674* vulkan_resource_index intrinsic.675*676* The cursor is not where you left it when this function returns.677*/678static nir_ssa_def *679build_buffer_addr_for_deref(nir_builder *b, nir_deref_instr *deref,680nir_address_format addr_format,681struct apply_pipeline_layout_state *state)682{683nir_deref_instr *parent = nir_deref_instr_parent(deref);684if (parent) {685nir_ssa_def *addr =686build_buffer_addr_for_deref(b, parent, addr_format, state);687688b->cursor = nir_before_instr(&deref->instr);689return nir_explicit_io_address_from_deref(b, deref, addr, addr_format);690}691692nir_intrinsic_instr *load_desc = nir_src_as_intrinsic(deref->parent);693assert(load_desc->intrinsic == nir_intrinsic_load_vulkan_descriptor);694695nir_intrinsic_instr *idx_intrin = nir_src_as_intrinsic(load_desc->src[0]);696697b->cursor = nir_before_instr(&deref->instr);698699return build_buffer_addr_for_idx_intrin(b, idx_intrin, addr_format, state);700}701702static bool703try_lower_direct_buffer_intrinsic(nir_builder *b,704nir_intrinsic_instr *intrin, bool is_atomic,705struct apply_pipeline_layout_state *state)706{707nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);708if (!nir_deref_mode_is_one_of(deref, nir_var_mem_ubo | nir_var_mem_ssbo))709return false;710711nir_intrinsic_instr *desc = nir_deref_find_descriptor(deref, state);712if (desc == NULL) {713/* We should always be able to find the descriptor for UBO access. */714assert(nir_deref_mode_is_one_of(deref, nir_var_mem_ssbo));715return false;716}717718nir_address_format addr_format = descriptor_address_format(desc, state);719720if (nir_deref_mode_is(deref, nir_var_mem_ssbo)) {721/* 64-bit atomics only support A64 messages so we can't lower them to722* the index+offset model.723*/724if (is_atomic && nir_dest_bit_size(intrin->dest) == 64)725return false;726727/* Normal binding table-based messages can't handle non-uniform access728* so we have to fall back to A64.729*/730if (nir_intrinsic_access(intrin) & ACCESS_NON_UNIFORM)731return false;732733if (!descriptor_has_bti(desc, state))734return false;735736/* Rewrite to 32bit_index_offset whenever we can */737addr_format = nir_address_format_32bit_index_offset;738} else {739assert(nir_deref_mode_is(deref, nir_var_mem_ubo));740741/* Rewrite to 32bit_index_offset whenever we can */742if (descriptor_has_bti(desc, state))743addr_format = nir_address_format_32bit_index_offset;744}745746nir_ssa_def *addr =747build_buffer_addr_for_deref(b, deref, addr_format, state);748749b->cursor = nir_before_instr(&intrin->instr);750nir_lower_explicit_io_instr(b, intrin, addr, addr_format);751752return true;753}754755static bool756lower_load_accel_struct_desc(nir_builder *b,757nir_intrinsic_instr *load_desc,758struct apply_pipeline_layout_state *state)759{760assert(load_desc->intrinsic == nir_intrinsic_load_vulkan_descriptor);761762nir_intrinsic_instr *idx_intrin = nir_src_as_intrinsic(load_desc->src[0]);763764/* It doesn't really matter what address format we choose as765* everything will constant-fold nicely. Choose one that uses the766* actual descriptor buffer.767*/768const nir_address_format addr_format =769nir_address_format_64bit_bounded_global;770771uint32_t set = UINT32_MAX, binding = UINT32_MAX;772nir_ssa_def *res_index =773build_res_index_for_chain(b, idx_intrin, addr_format,774&set, &binding, state);775776const struct anv_descriptor_set_binding_layout *bind_layout =777&state->layout->set[set].layout->binding[binding];778779b->cursor = nir_before_instr(&load_desc->instr);780781nir_ssa_def *desc_addr =782build_desc_addr(b, bind_layout, bind_layout->type,783res_index, addr_format, state);784785/* Acceleration structure descriptors are always uint64_t */786nir_ssa_def *desc = build_load_descriptor_mem(b, desc_addr, 0, 1, 64, state);787788assert(load_desc->dest.is_ssa);789assert(load_desc->dest.ssa.bit_size == 64);790assert(load_desc->dest.ssa.num_components == 1);791nir_ssa_def_rewrite_uses(&load_desc->dest.ssa, desc);792nir_instr_remove(&load_desc->instr);793794return true;795}796797static bool798lower_direct_buffer_instr(nir_builder *b, nir_instr *instr, void *_state)799{800struct apply_pipeline_layout_state *state = _state;801802if (instr->type != nir_instr_type_intrinsic)803return false;804805nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);806switch (intrin->intrinsic) {807case nir_intrinsic_load_deref:808case nir_intrinsic_store_deref:809return try_lower_direct_buffer_intrinsic(b, intrin, false, state);810811case nir_intrinsic_deref_atomic_add:812case nir_intrinsic_deref_atomic_imin:813case nir_intrinsic_deref_atomic_umin:814case nir_intrinsic_deref_atomic_imax:815case nir_intrinsic_deref_atomic_umax:816case nir_intrinsic_deref_atomic_and:817case nir_intrinsic_deref_atomic_or:818case nir_intrinsic_deref_atomic_xor:819case nir_intrinsic_deref_atomic_exchange:820case nir_intrinsic_deref_atomic_comp_swap:821case nir_intrinsic_deref_atomic_fmin:822case nir_intrinsic_deref_atomic_fmax:823case nir_intrinsic_deref_atomic_fcomp_swap:824return try_lower_direct_buffer_intrinsic(b, intrin, true, state);825826case nir_intrinsic_get_ssbo_size: {827/* The get_ssbo_size intrinsic always just takes a828* index/reindex intrinsic.829*/830nir_intrinsic_instr *idx_intrin =831find_descriptor_for_index_src(intrin->src[0], state);832if (idx_intrin == NULL || !descriptor_has_bti(idx_intrin, state))833return false;834835b->cursor = nir_before_instr(&intrin->instr);836837/* We just checked that this is a BTI descriptor */838const nir_address_format addr_format =839nir_address_format_32bit_index_offset;840841nir_ssa_def *buffer_addr =842build_buffer_addr_for_idx_intrin(b, idx_intrin, addr_format, state);843844b->cursor = nir_before_instr(&intrin->instr);845nir_ssa_def *bti = nir_channel(b, buffer_addr, 0);846847nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],848nir_src_for_ssa(bti));849_mesa_set_add(state->lowered_instrs, intrin);850return true;851}852853case nir_intrinsic_load_vulkan_descriptor:854if (nir_intrinsic_desc_type(intrin) ==855VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)856return lower_load_accel_struct_desc(b, intrin, state);857return false;858859default:860return false;861}862}863864static bool865lower_res_index_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin,866struct apply_pipeline_layout_state *state)867{868b->cursor = nir_before_instr(&intrin->instr);869870nir_address_format addr_format =871addr_format_for_desc_type(nir_intrinsic_desc_type(intrin), state);872873assert(intrin->src[0].is_ssa);874nir_ssa_def *index =875build_res_index(b, nir_intrinsic_desc_set(intrin),876nir_intrinsic_binding(intrin),877intrin->src[0].ssa,878addr_format, state);879880assert(intrin->dest.is_ssa);881assert(intrin->dest.ssa.bit_size == index->bit_size);882assert(intrin->dest.ssa.num_components == index->num_components);883nir_ssa_def_rewrite_uses(&intrin->dest.ssa, index);884nir_instr_remove(&intrin->instr);885886return true;887}888889static bool890lower_res_reindex_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin,891struct apply_pipeline_layout_state *state)892{893b->cursor = nir_before_instr(&intrin->instr);894895nir_address_format addr_format =896addr_format_for_desc_type(nir_intrinsic_desc_type(intrin), state);897898assert(intrin->src[0].is_ssa && intrin->src[1].is_ssa);899nir_ssa_def *index =900build_res_reindex(b, intrin->src[0].ssa,901intrin->src[1].ssa,902addr_format);903904assert(intrin->dest.is_ssa);905assert(intrin->dest.ssa.bit_size == index->bit_size);906assert(intrin->dest.ssa.num_components == index->num_components);907nir_ssa_def_rewrite_uses(&intrin->dest.ssa, index);908nir_instr_remove(&intrin->instr);909910return true;911}912913static bool914lower_load_vulkan_descriptor(nir_builder *b, nir_intrinsic_instr *intrin,915struct apply_pipeline_layout_state *state)916{917b->cursor = nir_before_instr(&intrin->instr);918919const VkDescriptorType desc_type = nir_intrinsic_desc_type(intrin);920nir_address_format addr_format = addr_format_for_desc_type(desc_type, state);921922assert(intrin->dest.is_ssa);923nir_foreach_use(src, &intrin->dest.ssa) {924if (src->parent_instr->type != nir_instr_type_deref)925continue;926927nir_deref_instr *cast = nir_instr_as_deref(src->parent_instr);928assert(cast->deref_type == nir_deref_type_cast);929switch (desc_type) {930case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:931case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:932cast->cast.align_mul = ANV_UBO_ALIGNMENT;933cast->cast.align_offset = 0;934break;935936case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:937case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:938cast->cast.align_mul = ANV_SSBO_ALIGNMENT;939cast->cast.align_offset = 0;940break;941942default:943break;944}945}946947assert(intrin->src[0].is_ssa);948nir_ssa_def *desc =949build_buffer_addr_for_res_index(b, desc_type, intrin->src[0].ssa,950addr_format, state);951952assert(intrin->dest.is_ssa);953assert(intrin->dest.ssa.bit_size == desc->bit_size);954assert(intrin->dest.ssa.num_components == desc->num_components);955nir_ssa_def_rewrite_uses(&intrin->dest.ssa, desc);956nir_instr_remove(&intrin->instr);957958return true;959}960961static bool962lower_get_ssbo_size(nir_builder *b, nir_intrinsic_instr *intrin,963struct apply_pipeline_layout_state *state)964{965if (_mesa_set_search(state->lowered_instrs, intrin))966return false;967968b->cursor = nir_before_instr(&intrin->instr);969970nir_address_format addr_format =971addr_format_for_desc_type(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, state);972973assert(intrin->src[0].is_ssa);974nir_ssa_def *desc =975build_buffer_addr_for_res_index(b, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,976intrin->src[0].ssa, addr_format, state);977978switch (addr_format) {979case nir_address_format_64bit_global_32bit_offset:980case nir_address_format_64bit_bounded_global: {981nir_ssa_def *size = nir_channel(b, desc, 2);982nir_ssa_def_rewrite_uses(&intrin->dest.ssa, size);983nir_instr_remove(&intrin->instr);984break;985}986987case nir_address_format_32bit_index_offset:988/* The binding table index is the first component of the address. The989* back-end wants a scalar binding table index source.990*/991nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],992nir_src_for_ssa(nir_channel(b, desc, 0)));993break;994995default:996unreachable("Unsupported address format");997}998999return true;1000}10011002static bool1003lower_image_intrinsic(nir_builder *b, nir_intrinsic_instr *intrin,1004struct apply_pipeline_layout_state *state)1005{1006nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);1007nir_variable *var = nir_deref_instr_get_variable(deref);10081009unsigned set = var->data.descriptor_set;1010unsigned binding = var->data.binding;1011unsigned binding_offset = state->set[set].surface_offsets[binding];10121013b->cursor = nir_before_instr(&intrin->instr);10141015ASSERTED const bool use_bindless = state->pdevice->has_bindless_images;10161017if (intrin->intrinsic == nir_intrinsic_image_deref_load_param_intel) {1018b->cursor = nir_instr_remove(&intrin->instr);10191020assert(!use_bindless); /* Otherwise our offsets would be wrong */1021const unsigned param = nir_intrinsic_base(intrin);10221023nir_ssa_def *desc =1024build_load_var_deref_descriptor_mem(b, deref, param * 16,1025intrin->dest.ssa.num_components,1026intrin->dest.ssa.bit_size, state);10271028nir_ssa_def_rewrite_uses(&intrin->dest.ssa, desc);1029} else if (binding_offset > MAX_BINDING_TABLE_SIZE) {1030const bool write_only =1031(var->data.access & ACCESS_NON_READABLE) != 0;1032nir_ssa_def *desc =1033build_load_var_deref_descriptor_mem(b, deref, 0, 2, 32, state);1034nir_ssa_def *handle = nir_channel(b, desc, write_only ? 1 : 0);1035nir_rewrite_image_intrinsic(intrin, handle, true);1036} else {1037unsigned array_size =1038state->layout->set[set].layout->binding[binding].array_size;10391040nir_ssa_def *index = NULL;1041if (deref->deref_type != nir_deref_type_var) {1042assert(deref->deref_type == nir_deref_type_array);1043index = nir_ssa_for_src(b, deref->arr.index, 1);1044if (state->add_bounds_checks)1045index = nir_umin(b, index, nir_imm_int(b, array_size - 1));1046} else {1047index = nir_imm_int(b, 0);1048}10491050index = nir_iadd_imm(b, index, binding_offset);1051nir_rewrite_image_intrinsic(intrin, index, false);1052}10531054return true;1055}10561057static bool1058lower_load_constant(nir_builder *b, nir_intrinsic_instr *intrin,1059struct apply_pipeline_layout_state *state)1060{1061b->cursor = nir_instr_remove(&intrin->instr);10621063/* Any constant-offset load_constant instructions should have been removed1064* by constant folding.1065*/1066assert(!nir_src_is_const(intrin->src[0]));1067nir_ssa_def *offset = nir_iadd_imm(b, nir_ssa_for_src(b, intrin->src[0], 1),1068nir_intrinsic_base(intrin));10691070nir_ssa_def *data;1071if (state->pdevice->use_softpin) {1072unsigned load_size = intrin->dest.ssa.num_components *1073intrin->dest.ssa.bit_size / 8;1074unsigned load_align = intrin->dest.ssa.bit_size / 8;10751076assert(load_size < b->shader->constant_data_size);1077unsigned max_offset = b->shader->constant_data_size - load_size;1078offset = nir_umin(b, offset, nir_imm_int(b, max_offset));10791080nir_ssa_def *const_data_base_addr = nir_pack_64_2x32_split(b,1081nir_load_reloc_const_intel(b, BRW_SHADER_RELOC_CONST_DATA_ADDR_LOW),1082nir_load_reloc_const_intel(b, BRW_SHADER_RELOC_CONST_DATA_ADDR_HIGH));10831084data = nir_load_global_constant(b, nir_iadd(b, const_data_base_addr,1085nir_u2u64(b, offset)),1086load_align,1087intrin->dest.ssa.num_components,1088intrin->dest.ssa.bit_size);1089} else {1090nir_ssa_def *index = nir_imm_int(b, state->constants_offset);10911092data = nir_load_ubo(b, intrin->num_components, intrin->dest.ssa.bit_size,1093index, offset,1094.align_mul = intrin->dest.ssa.bit_size / 8,1095.align_offset = 0,1096.range_base = nir_intrinsic_base(intrin),1097.range = nir_intrinsic_range(intrin));1098}10991100nir_ssa_def_rewrite_uses(&intrin->dest.ssa, data);11011102return true;1103}11041105static void1106lower_tex_deref(nir_builder *b, nir_tex_instr *tex,1107nir_tex_src_type deref_src_type,1108unsigned *base_index, unsigned plane,1109struct apply_pipeline_layout_state *state)1110{1111int deref_src_idx = nir_tex_instr_src_index(tex, deref_src_type);1112if (deref_src_idx < 0)1113return;11141115nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_src_idx].src);1116nir_variable *var = nir_deref_instr_get_variable(deref);11171118unsigned set = var->data.descriptor_set;1119unsigned binding = var->data.binding;1120unsigned array_size =1121state->layout->set[set].layout->binding[binding].array_size;11221123unsigned binding_offset;1124if (deref_src_type == nir_tex_src_texture_deref) {1125binding_offset = state->set[set].surface_offsets[binding];1126} else {1127assert(deref_src_type == nir_tex_src_sampler_deref);1128binding_offset = state->set[set].sampler_offsets[binding];1129}11301131nir_tex_src_type offset_src_type;1132nir_ssa_def *index = NULL;1133if (binding_offset > MAX_BINDING_TABLE_SIZE) {1134const unsigned plane_offset =1135plane * sizeof(struct anv_sampled_image_descriptor);11361137nir_ssa_def *desc =1138build_load_var_deref_descriptor_mem(b, deref, plane_offset,11392, 32, state);11401141if (deref_src_type == nir_tex_src_texture_deref) {1142offset_src_type = nir_tex_src_texture_handle;1143index = nir_channel(b, desc, 0);1144} else {1145assert(deref_src_type == nir_tex_src_sampler_deref);1146offset_src_type = nir_tex_src_sampler_handle;1147index = nir_channel(b, desc, 1);1148}1149} else {1150if (deref_src_type == nir_tex_src_texture_deref) {1151offset_src_type = nir_tex_src_texture_offset;1152} else {1153assert(deref_src_type == nir_tex_src_sampler_deref);1154offset_src_type = nir_tex_src_sampler_offset;1155}11561157*base_index = binding_offset + plane;11581159if (deref->deref_type != nir_deref_type_var) {1160assert(deref->deref_type == nir_deref_type_array);11611162if (nir_src_is_const(deref->arr.index)) {1163unsigned arr_index = MIN2(nir_src_as_uint(deref->arr.index), array_size - 1);1164struct anv_sampler **immutable_samplers =1165state->layout->set[set].layout->binding[binding].immutable_samplers;1166if (immutable_samplers) {1167/* Array of YCbCr samplers are tightly packed in the binding1168* tables, compute the offset of an element in the array by1169* adding the number of planes of all preceding elements.1170*/1171unsigned desc_arr_index = 0;1172for (int i = 0; i < arr_index; i++)1173desc_arr_index += immutable_samplers[i]->n_planes;1174*base_index += desc_arr_index;1175} else {1176*base_index += arr_index;1177}1178} else {1179/* From VK_KHR_sampler_ycbcr_conversion:1180*1181* If sampler Y’CBCR conversion is enabled, the combined image1182* sampler must be indexed only by constant integral expressions1183* when aggregated into arrays in shader code, irrespective of1184* the shaderSampledImageArrayDynamicIndexing feature.1185*/1186assert(nir_tex_instr_src_index(tex, nir_tex_src_plane) == -1);11871188index = nir_ssa_for_src(b, deref->arr.index, 1);11891190if (state->add_bounds_checks)1191index = nir_umin(b, index, nir_imm_int(b, array_size - 1));1192}1193}1194}11951196if (index) {1197nir_instr_rewrite_src(&tex->instr, &tex->src[deref_src_idx].src,1198nir_src_for_ssa(index));1199tex->src[deref_src_idx].src_type = offset_src_type;1200} else {1201nir_tex_instr_remove_src(tex, deref_src_idx);1202}1203}12041205static uint32_t1206tex_instr_get_and_remove_plane_src(nir_tex_instr *tex)1207{1208int plane_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_plane);1209if (plane_src_idx < 0)1210return 0;12111212unsigned plane = nir_src_as_uint(tex->src[plane_src_idx].src);12131214nir_tex_instr_remove_src(tex, plane_src_idx);12151216return plane;1217}12181219static nir_ssa_def *1220build_def_array_select(nir_builder *b, nir_ssa_def **srcs, nir_ssa_def *idx,1221unsigned start, unsigned end)1222{1223if (start == end - 1) {1224return srcs[start];1225} else {1226unsigned mid = start + (end - start) / 2;1227return nir_bcsel(b, nir_ilt(b, idx, nir_imm_int(b, mid)),1228build_def_array_select(b, srcs, idx, start, mid),1229build_def_array_select(b, srcs, idx, mid, end));1230}1231}12321233static void1234lower_gfx7_tex_swizzle(nir_builder *b, nir_tex_instr *tex, unsigned plane,1235struct apply_pipeline_layout_state *state)1236{1237assert(state->pdevice->info.verx10 == 70);1238if (tex->sampler_dim == GLSL_SAMPLER_DIM_BUF ||1239nir_tex_instr_is_query(tex) ||1240tex->op == nir_texop_tg4 || /* We can't swizzle TG4 */1241(tex->is_shadow && tex->is_new_style_shadow))1242return;12431244int deref_src_idx = nir_tex_instr_src_index(tex, nir_tex_src_texture_deref);1245assert(deref_src_idx >= 0);12461247nir_deref_instr *deref = nir_src_as_deref(tex->src[deref_src_idx].src);1248nir_variable *var = nir_deref_instr_get_variable(deref);12491250unsigned set = var->data.descriptor_set;1251unsigned binding = var->data.binding;1252const struct anv_descriptor_set_binding_layout *bind_layout =1253&state->layout->set[set].layout->binding[binding];12541255if ((bind_layout->data & ANV_DESCRIPTOR_TEXTURE_SWIZZLE) == 0)1256return;12571258b->cursor = nir_before_instr(&tex->instr);12591260const unsigned plane_offset =1261plane * sizeof(struct anv_texture_swizzle_descriptor);1262nir_ssa_def *swiz =1263build_load_var_deref_descriptor_mem(b, deref, plane_offset,12641, 32, state);12651266b->cursor = nir_after_instr(&tex->instr);12671268assert(tex->dest.ssa.bit_size == 32);1269assert(tex->dest.ssa.num_components == 4);12701271/* Initializing to undef is ok; nir_opt_undef will clean it up. */1272nir_ssa_def *undef = nir_ssa_undef(b, 1, 32);1273nir_ssa_def *comps[8];1274for (unsigned i = 0; i < ARRAY_SIZE(comps); i++)1275comps[i] = undef;12761277comps[ISL_CHANNEL_SELECT_ZERO] = nir_imm_int(b, 0);1278if (nir_alu_type_get_base_type(tex->dest_type) == nir_type_float)1279comps[ISL_CHANNEL_SELECT_ONE] = nir_imm_float(b, 1);1280else1281comps[ISL_CHANNEL_SELECT_ONE] = nir_imm_int(b, 1);1282comps[ISL_CHANNEL_SELECT_RED] = nir_channel(b, &tex->dest.ssa, 0);1283comps[ISL_CHANNEL_SELECT_GREEN] = nir_channel(b, &tex->dest.ssa, 1);1284comps[ISL_CHANNEL_SELECT_BLUE] = nir_channel(b, &tex->dest.ssa, 2);1285comps[ISL_CHANNEL_SELECT_ALPHA] = nir_channel(b, &tex->dest.ssa, 3);12861287nir_ssa_def *swiz_comps[4];1288for (unsigned i = 0; i < 4; i++) {1289nir_ssa_def *comp_swiz = nir_extract_u8(b, swiz, nir_imm_int(b, i));1290swiz_comps[i] = build_def_array_select(b, comps, comp_swiz, 0, 8);1291}1292nir_ssa_def *swiz_tex_res = nir_vec(b, swiz_comps, 4);12931294/* Rewrite uses before we insert so we don't rewrite this use */1295nir_ssa_def_rewrite_uses_after(&tex->dest.ssa,1296swiz_tex_res,1297swiz_tex_res->parent_instr);1298}12991300static bool1301lower_tex(nir_builder *b, nir_tex_instr *tex,1302struct apply_pipeline_layout_state *state)1303{1304unsigned plane = tex_instr_get_and_remove_plane_src(tex);13051306/* On Ivy Bridge and Bay Trail, we have to swizzle in the shader. Do this1307* before we lower the derefs away so we can still find the descriptor.1308*/1309if (state->pdevice->info.verx10 == 70)1310lower_gfx7_tex_swizzle(b, tex, plane, state);13111312b->cursor = nir_before_instr(&tex->instr);13131314lower_tex_deref(b, tex, nir_tex_src_texture_deref,1315&tex->texture_index, plane, state);13161317lower_tex_deref(b, tex, nir_tex_src_sampler_deref,1318&tex->sampler_index, plane, state);13191320return true;1321}13221323static bool1324apply_pipeline_layout(nir_builder *b, nir_instr *instr, void *_state)1325{1326struct apply_pipeline_layout_state *state = _state;13271328switch (instr->type) {1329case nir_instr_type_intrinsic: {1330nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);1331switch (intrin->intrinsic) {1332case nir_intrinsic_vulkan_resource_index:1333return lower_res_index_intrinsic(b, intrin, state);1334case nir_intrinsic_vulkan_resource_reindex:1335return lower_res_reindex_intrinsic(b, intrin, state);1336case nir_intrinsic_load_vulkan_descriptor:1337return lower_load_vulkan_descriptor(b, intrin, state);1338case nir_intrinsic_get_ssbo_size:1339return lower_get_ssbo_size(b, intrin, state);1340case nir_intrinsic_image_deref_load:1341case nir_intrinsic_image_deref_store:1342case nir_intrinsic_image_deref_atomic_add:1343case nir_intrinsic_image_deref_atomic_imin:1344case nir_intrinsic_image_deref_atomic_umin:1345case nir_intrinsic_image_deref_atomic_imax:1346case nir_intrinsic_image_deref_atomic_umax:1347case nir_intrinsic_image_deref_atomic_and:1348case nir_intrinsic_image_deref_atomic_or:1349case nir_intrinsic_image_deref_atomic_xor:1350case nir_intrinsic_image_deref_atomic_exchange:1351case nir_intrinsic_image_deref_atomic_comp_swap:1352case nir_intrinsic_image_deref_size:1353case nir_intrinsic_image_deref_samples:1354case nir_intrinsic_image_deref_load_param_intel:1355case nir_intrinsic_image_deref_load_raw_intel:1356case nir_intrinsic_image_deref_store_raw_intel:1357return lower_image_intrinsic(b, intrin, state);1358case nir_intrinsic_load_constant:1359return lower_load_constant(b, intrin, state);1360default:1361return false;1362}1363break;1364}1365case nir_instr_type_tex:1366return lower_tex(b, nir_instr_as_tex(instr), state);1367default:1368return false;1369}1370}13711372struct binding_info {1373uint32_t binding;1374uint8_t set;1375uint16_t score;1376};13771378static int1379compare_binding_infos(const void *_a, const void *_b)1380{1381const struct binding_info *a = _a, *b = _b;1382if (a->score != b->score)1383return b->score - a->score;13841385if (a->set != b->set)1386return a->set - b->set;13871388return a->binding - b->binding;1389}13901391void1392anv_nir_apply_pipeline_layout(const struct anv_physical_device *pdevice,1393bool robust_buffer_access,1394const struct anv_pipeline_layout *layout,1395nir_shader *shader,1396struct anv_pipeline_bind_map *map)1397{1398void *mem_ctx = ralloc_context(NULL);13991400struct apply_pipeline_layout_state state = {1401.pdevice = pdevice,1402.layout = layout,1403.add_bounds_checks = robust_buffer_access,1404.desc_addr_format = brw_shader_stage_is_bindless(shader->info.stage) ?1405nir_address_format_64bit_global_32bit_offset :1406nir_address_format_32bit_index_offset,1407.ssbo_addr_format = anv_nir_ssbo_addr_format(pdevice, robust_buffer_access),1408.ubo_addr_format = anv_nir_ubo_addr_format(pdevice, robust_buffer_access),1409.lowered_instrs = _mesa_pointer_set_create(mem_ctx),1410};14111412for (unsigned s = 0; s < layout->num_sets; s++) {1413const unsigned count = layout->set[s].layout->binding_count;1414state.set[s].use_count = rzalloc_array(mem_ctx, uint8_t, count);1415state.set[s].surface_offsets = rzalloc_array(mem_ctx, uint8_t, count);1416state.set[s].sampler_offsets = rzalloc_array(mem_ctx, uint8_t, count);1417}14181419nir_shader_instructions_pass(shader, get_used_bindings,1420nir_metadata_all, &state);14211422for (unsigned s = 0; s < layout->num_sets; s++) {1423if (state.desc_addr_format != nir_address_format_32bit_index_offset) {1424state.set[s].desc_offset = BINDLESS_OFFSET;1425} else if (state.set[s].desc_buffer_used) {1426map->surface_to_descriptor[map->surface_count] =1427(struct anv_pipeline_binding) {1428.set = ANV_DESCRIPTOR_SET_DESCRIPTORS,1429.index = s,1430};1431state.set[s].desc_offset = map->surface_count;1432map->surface_count++;1433}1434}14351436if (state.uses_constants && !pdevice->use_softpin) {1437state.constants_offset = map->surface_count;1438map->surface_to_descriptor[map->surface_count].set =1439ANV_DESCRIPTOR_SET_SHADER_CONSTANTS;1440map->surface_count++;1441}14421443unsigned used_binding_count = 0;1444for (uint32_t set = 0; set < layout->num_sets; set++) {1445struct anv_descriptor_set_layout *set_layout = layout->set[set].layout;1446for (unsigned b = 0; b < set_layout->binding_count; b++) {1447if (state.set[set].use_count[b] == 0)1448continue;14491450used_binding_count++;1451}1452}14531454struct binding_info *infos =1455rzalloc_array(mem_ctx, struct binding_info, used_binding_count);1456used_binding_count = 0;1457for (uint32_t set = 0; set < layout->num_sets; set++) {1458const struct anv_descriptor_set_layout *set_layout = layout->set[set].layout;1459for (unsigned b = 0; b < set_layout->binding_count; b++) {1460if (state.set[set].use_count[b] == 0)1461continue;14621463const struct anv_descriptor_set_binding_layout *binding =1464&layout->set[set].layout->binding[b];14651466/* Do a fixed-point calculation to generate a score based on the1467* number of uses and the binding array size. We shift by 7 instead1468* of 8 because we're going to use the top bit below to make1469* everything which does not support bindless super higher priority1470* than things which do.1471*/1472uint16_t score = ((uint16_t)state.set[set].use_count[b] << 7) /1473binding->array_size;14741475/* If the descriptor type doesn't support bindless then put it at the1476* beginning so we guarantee it gets a slot.1477*/1478if (!anv_descriptor_supports_bindless(pdevice, binding, true) ||1479!anv_descriptor_supports_bindless(pdevice, binding, false))1480score |= 1 << 15;14811482infos[used_binding_count++] = (struct binding_info) {1483.set = set,1484.binding = b,1485.score = score,1486};1487}1488}14891490/* Order the binding infos based on score with highest scores first. If1491* scores are equal we then order by set and binding.1492*/1493qsort(infos, used_binding_count, sizeof(struct binding_info),1494compare_binding_infos);14951496for (unsigned i = 0; i < used_binding_count; i++) {1497unsigned set = infos[i].set, b = infos[i].binding;1498const struct anv_descriptor_set_binding_layout *binding =1499&layout->set[set].layout->binding[b];15001501const uint32_t array_size = binding->array_size;15021503if (binding->dynamic_offset_index >= 0)1504state.has_dynamic_buffers = true;15051506if (binding->data & ANV_DESCRIPTOR_SURFACE_STATE) {1507if (map->surface_count + array_size > MAX_BINDING_TABLE_SIZE ||1508anv_descriptor_requires_bindless(pdevice, binding, false) ||1509brw_shader_stage_is_bindless(shader->info.stage)) {1510/* If this descriptor doesn't fit in the binding table or if it1511* requires bindless for some reason, flag it as bindless.1512*/1513assert(anv_descriptor_supports_bindless(pdevice, binding, false));1514state.set[set].surface_offsets[b] = BINDLESS_OFFSET;1515} else {1516state.set[set].surface_offsets[b] = map->surface_count;1517if (binding->dynamic_offset_index < 0) {1518struct anv_sampler **samplers = binding->immutable_samplers;1519for (unsigned i = 0; i < binding->array_size; i++) {1520uint8_t planes = samplers ? samplers[i]->n_planes : 1;1521for (uint8_t p = 0; p < planes; p++) {1522map->surface_to_descriptor[map->surface_count++] =1523(struct anv_pipeline_binding) {1524.set = set,1525.index = binding->descriptor_index + i,1526.plane = p,1527};1528}1529}1530} else {1531for (unsigned i = 0; i < binding->array_size; i++) {1532map->surface_to_descriptor[map->surface_count++] =1533(struct anv_pipeline_binding) {1534.set = set,1535.index = binding->descriptor_index + i,1536.dynamic_offset_index =1537layout->set[set].dynamic_offset_start +1538binding->dynamic_offset_index + i,1539};1540}1541}1542}1543assert(map->surface_count <= MAX_BINDING_TABLE_SIZE);1544}15451546if (binding->data & ANV_DESCRIPTOR_SAMPLER_STATE) {1547if (map->sampler_count + array_size > MAX_SAMPLER_TABLE_SIZE ||1548anv_descriptor_requires_bindless(pdevice, binding, true) ||1549brw_shader_stage_is_bindless(shader->info.stage)) {1550/* If this descriptor doesn't fit in the binding table or if it1551* requires bindless for some reason, flag it as bindless.1552*1553* We also make large sampler arrays bindless because we can avoid1554* using indirect sends thanks to bindless samplers being packed1555* less tightly than the sampler table.1556*/1557assert(anv_descriptor_supports_bindless(pdevice, binding, true));1558state.set[set].sampler_offsets[b] = BINDLESS_OFFSET;1559} else {1560state.set[set].sampler_offsets[b] = map->sampler_count;1561struct anv_sampler **samplers = binding->immutable_samplers;1562for (unsigned i = 0; i < binding->array_size; i++) {1563uint8_t planes = samplers ? samplers[i]->n_planes : 1;1564for (uint8_t p = 0; p < planes; p++) {1565map->sampler_to_descriptor[map->sampler_count++] =1566(struct anv_pipeline_binding) {1567.set = set,1568.index = binding->descriptor_index + i,1569.plane = p,1570};1571}1572}1573}1574}1575}15761577nir_foreach_uniform_variable(var, shader) {1578const struct glsl_type *glsl_type = glsl_without_array(var->type);15791580if (!glsl_type_is_image(glsl_type))1581continue;15821583enum glsl_sampler_dim dim = glsl_get_sampler_dim(glsl_type);15841585const uint32_t set = var->data.descriptor_set;1586const uint32_t binding = var->data.binding;1587const struct anv_descriptor_set_binding_layout *bind_layout =1588&layout->set[set].layout->binding[binding];1589const uint32_t array_size = bind_layout->array_size;15901591if (state.set[set].use_count[binding] == 0)1592continue;15931594if (state.set[set].surface_offsets[binding] >= MAX_BINDING_TABLE_SIZE)1595continue;15961597struct anv_pipeline_binding *pipe_binding =1598&map->surface_to_descriptor[state.set[set].surface_offsets[binding]];1599for (unsigned i = 0; i < array_size; i++) {1600assert(pipe_binding[i].set == set);1601assert(pipe_binding[i].index == bind_layout->descriptor_index + i);16021603if (dim == GLSL_SAMPLER_DIM_SUBPASS ||1604dim == GLSL_SAMPLER_DIM_SUBPASS_MS)1605pipe_binding[i].input_attachment_index = var->data.index + i;16061607/* NOTE: This is a uint8_t so we really do need to != 0 here */1608pipe_binding[i].write_only =1609(var->data.access & ACCESS_NON_READABLE) != 0;1610}1611}16121613/* Before we do the normal lowering, we look for any SSBO operations1614* that we can lower to the BTI model and lower them up-front. The BTI1615* model can perform better than the A64 model for a couple reasons:1616*1617* 1. 48-bit address calculations are potentially expensive and using1618* the BTI model lets us simply compute 32-bit offsets and the1619* hardware adds the 64-bit surface base address.1620*1621* 2. The BTI messages, because they use surface states, do bounds1622* checking for us. With the A64 model, we have to do our own1623* bounds checking and this means wider pointers and extra1624* calculations and branching in the shader.1625*1626* The solution to both of these is to convert things to the BTI model1627* opportunistically. The reason why we need to do this as a pre-pass1628* is for two reasons:1629*1630* 1. The BTI model requires nir_address_format_32bit_index_offset1631* pointers which are not the same type as the pointers needed for1632* the A64 model. Because all our derefs are set up for the A641633* model (in case we have variable pointers), we have to crawl all1634* the way back to the vulkan_resource_index intrinsic and build a1635* completely fresh index+offset calculation.1636*1637* 2. Because the variable-pointers-capable lowering that we do as part1638* of apply_pipeline_layout_block is destructive (It really has to1639* be to handle variable pointers properly), we've lost the deref1640* information by the time we get to the load/store/atomic1641* intrinsics in that pass.1642*/1643nir_shader_instructions_pass(shader, lower_direct_buffer_instr,1644nir_metadata_block_index |1645nir_metadata_dominance,1646&state);16471648/* We just got rid of all the direct access. Delete it so it's not in the1649* way when we do our indirect lowering.1650*/1651nir_opt_dce(shader);16521653nir_shader_instructions_pass(shader, apply_pipeline_layout,1654nir_metadata_block_index |1655nir_metadata_dominance,1656&state);16571658ralloc_free(mem_ctx);16591660if (brw_shader_stage_is_bindless(shader->info.stage)) {1661assert(map->surface_count == 0);1662assert(map->sampler_count == 0);1663}16641665/* Now that we're done computing the surface and sampler portions of the1666* bind map, hash them. This lets us quickly determine if the actual1667* mapping has changed and not just a no-op pipeline change.1668*/1669_mesa_sha1_compute(map->surface_to_descriptor,1670map->surface_count * sizeof(struct anv_pipeline_binding),1671map->surface_sha1);1672_mesa_sha1_compute(map->sampler_to_descriptor,1673map->sampler_count * sizeof(struct anv_pipeline_binding),1674map->sampler_sha1);1675}167616771678