radeonsi/nir: Use correct location for uniform access bound
[mesa.git] / src / gallium / drivers / radeonsi / si_shader_nir.c
index 7e98eb391525333fe0cb4493f472b2e9c5b8512e..3a5e17c6b29bf730b2a73d2541eba8da182d3164 100644 (file)
@@ -171,6 +171,9 @@ static void scan_instruction(const struct nir_shader *nir,
                case nir_intrinsic_load_base_vertex:
                        info->uses_basevertex = 1;
                        break;
+               case nir_intrinsic_load_draw_id:
+                       info->uses_drawid = 1;
+                       break;
                case nir_intrinsic_load_primitive_id:
                        info->uses_primid = 1;
                        break;
@@ -181,39 +184,48 @@ static void scan_instruction(const struct nir_shader *nir,
                case nir_intrinsic_load_tess_level_outer:
                        info->reads_tess_factors = true;
                        break;
-               case nir_intrinsic_image_deref_load: {
-                       nir_variable *var = intrinsic_get_var(intr);
-                       if (var->data.bindless) {
-                               info->uses_bindless_images = true;
+               case nir_intrinsic_bindless_image_load:
+                       info->uses_bindless_images = true;
 
-                               if (glsl_get_sampler_dim(var->type) == GLSL_SAMPLER_DIM_BUF)
-                                       info->uses_bindless_buffer_load = true;
-                               else
-                                       info->uses_bindless_image_load = true;
-                       }
+                       if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
+                               info->uses_bindless_buffer_load = true;
+                       else
+                               info->uses_bindless_image_load = true;
                        break;
-               }
-               case nir_intrinsic_image_deref_size:
-               case nir_intrinsic_image_deref_samples: {
-                       nir_variable *var = intrinsic_get_var(intr);
-                       if (var->data.bindless)
-                               info->uses_bindless_images = true;
+               case nir_intrinsic_bindless_image_size:
+               case nir_intrinsic_bindless_image_samples:
+                       info->uses_bindless_images = true;
                        break;
-               }
-               case nir_intrinsic_image_deref_store: {
-                       const nir_deref_instr *image_deref = nir_instr_as_deref(intr->src[0].ssa->parent_instr);
-                       nir_variable *var = intrinsic_get_var(intr);
-                       if (var->data.bindless) {
-                               info->uses_bindless_images = true;
+               case nir_intrinsic_bindless_image_store:
+                       info->uses_bindless_images = true;
+
+                       if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
+                               info->uses_bindless_buffer_store = true;
+                       else
+                               info->uses_bindless_image_store = true;
+
+                       info->writes_memory = true;
+                       break;
+               case nir_intrinsic_image_deref_store:
+                       info->writes_memory = true;
+                       break;
+               case nir_intrinsic_bindless_image_atomic_add:
+               case nir_intrinsic_bindless_image_atomic_min:
+               case nir_intrinsic_bindless_image_atomic_max:
+               case nir_intrinsic_bindless_image_atomic_and:
+               case nir_intrinsic_bindless_image_atomic_or:
+               case nir_intrinsic_bindless_image_atomic_xor:
+               case nir_intrinsic_bindless_image_atomic_exchange:
+               case nir_intrinsic_bindless_image_atomic_comp_swap:
+                       info->uses_bindless_images = true;
+
+                       if (nir_intrinsic_image_dim(intr) == GLSL_SAMPLER_DIM_BUF)
+                               info->uses_bindless_buffer_atomic = true;
+                       else
+                               info->uses_bindless_image_atomic = true;
 
-                               if (glsl_get_sampler_dim(image_deref->type) == GLSL_SAMPLER_DIM_BUF)
-                                       info->uses_bindless_buffer_store = true;
-                               else
-                                       info->uses_bindless_image_store = true;
-                       }
                        info->writes_memory = true;
                        break;
-               }
                case nir_intrinsic_image_deref_atomic_add:
                case nir_intrinsic_image_deref_atomic_min:
                case nir_intrinsic_image_deref_atomic_max:
@@ -221,19 +233,9 @@ static void scan_instruction(const struct nir_shader *nir,
                case nir_intrinsic_image_deref_atomic_or:
                case nir_intrinsic_image_deref_atomic_xor:
                case nir_intrinsic_image_deref_atomic_exchange:
-               case nir_intrinsic_image_deref_atomic_comp_swap: {
-                       nir_variable *var = intrinsic_get_var(intr);
-                       if (var->data.bindless) {
-                               info->uses_bindless_images = true;
-
-                               if (glsl_get_sampler_dim(var->type) == GLSL_SAMPLER_DIM_BUF)
-                                       info->uses_bindless_buffer_atomic = true;
-                               else
-                                       info->uses_bindless_image_atomic = true;
-                       }
+               case nir_intrinsic_image_deref_atomic_comp_swap:
                        info->writes_memory = true;
                        break;
-               }
                case nir_intrinsic_store_ssbo:
                case nir_intrinsic_ssbo_atomic_add:
                case nir_intrinsic_ssbo_atomic_imin:
@@ -343,6 +345,11 @@ void si_nir_scan_shader(const struct nir_shader *nir,
        info->properties[TGSI_PROPERTY_NEXT_SHADER] =
                pipe_shader_type_from_mesa(nir->info.next_stage);
 
+       if (nir->info.stage == MESA_SHADER_VERTEX) {
+               info->properties[TGSI_PROPERTY_VS_WINDOW_SPACE_POSITION] =
+                       nir->info.vs.window_space_position;
+       }
+
        if (nir->info.stage == MESA_SHADER_TESS_CTRL) {
                info->properties[TGSI_PROPERTY_TCS_VERTICES_OUT] =
                        nir->info.tess.tcs_vertices_out;
@@ -440,7 +447,7 @@ void si_nir_scan_shader(const struct nir_shader *nir,
                /* Fragment shader position is a system value. */
                if (nir->info.stage == MESA_SHADER_FRAGMENT &&
                    variable->data.location == VARYING_SLOT_POS) {
-                       if (variable->data.pixel_center_integer)
+                       if (nir->info.fs.pixel_center_integer)
                                info->properties[TGSI_PROPERTY_FS_COORD_PIXEL_CENTER] =
                                        TGSI_FS_COORD_PIXEL_CENTER_INTEGER;
 
@@ -687,17 +694,22 @@ void si_nir_scan_shader(const struct nir_shader *nir,
 
        struct set *ubo_set = _mesa_set_create(NULL, _mesa_hash_pointer,
                                               _mesa_key_pointer_equal);
+       struct set *ssbo_set = _mesa_set_create(NULL, _mesa_hash_pointer,
+                                               _mesa_key_pointer_equal);
 
        /* Intialise const_file_max[0] */
        info->const_file_max[0] = -1;
 
+       /* The first 8 are reserved for atomic counters using ssbo */
+       unsigned ssbo_idx = 8;
+
        unsigned ubo_idx = 1;
        nir_foreach_variable(variable, &nir->uniforms) {
                const struct glsl_type *type = variable->type;
                enum glsl_base_type base_type =
                        glsl_get_base_type(glsl_without_array(type));
                unsigned aoa_size = MAX2(1, glsl_get_aoa_size(type));
-               unsigned loc = variable->data.location;
+               unsigned loc = variable->data.driver_location / 4;
                int slot_count = glsl_count_attribute_slots(type, false);
                int max_slot = MAX2(info->const_file_max[0], (int) loc) + slot_count;
 
@@ -708,12 +720,16 @@ void si_nir_scan_shader(const struct nir_shader *nir,
                 */
                if (variable->interface_type != NULL) {
                        if (variable->data.mode == nir_var_uniform ||
-                           variable->data.mode == nir_var_mem_ubo) {
+                           variable->data.mode == nir_var_mem_ubo ||
+                           variable->data.mode == nir_var_mem_ssbo) {
+
+                               struct set *buf_set = variable->data.mode == nir_var_mem_ssbo ?
+                                       ssbo_set : ubo_set;
 
                                unsigned block_count;
                                if (base_type != GLSL_TYPE_INTERFACE) {
                                        struct set_entry *entry =
-                                               _mesa_set_search(ubo_set, variable->interface_type);
+                                               _mesa_set_search(buf_set, variable->interface_type);
 
                                        /* Check if we have already processed
                                         * a member from this ubo.
@@ -726,16 +742,18 @@ void si_nir_scan_shader(const struct nir_shader *nir,
                                        block_count = aoa_size;
                                }
 
-                               info->const_buffers_declared |= u_bit_consecutive(ubo_idx, block_count);
-                               ubo_idx += block_count;
+                               if (variable->data.mode == nir_var_uniform ||
+                                   variable->data.mode == nir_var_mem_ubo) {
+                                       info->const_buffers_declared |= u_bit_consecutive(ubo_idx, block_count);
+                                       ubo_idx += block_count;
+                               } else {
+                                       assert(variable->data.mode == nir_var_mem_ssbo);
 
-                               _mesa_set_add(ubo_set, variable->interface_type);
-                       }
+                                       info->shader_buffers_declared |= u_bit_consecutive(ssbo_idx, block_count);
+                                       ssbo_idx += block_count;
+                               }
 
-                       if (variable->data.mode == nir_var_mem_ssbo) {
-                               /* TODO: make this more accurate */
-                               info->shader_buffers_declared =
-                                       u_bit_consecutive(0, SI_NUM_SHADER_BUFFERS);
+                               _mesa_set_add(buf_set, variable->interface_type);
                        }
 
                        continue;
@@ -776,6 +794,7 @@ void si_nir_scan_shader(const struct nir_shader *nir,
        }
 
        _mesa_set_destroy(ubo_set, NULL);
+       _mesa_set_destroy(ssbo_set, NULL);
 
        info->num_written_clipdistance = nir->info.clip_distance_array_size;
        info->num_written_culldistance = nir->info.cull_distance_array_size;
@@ -792,6 +811,71 @@ void si_nir_scan_shader(const struct nir_shader *nir,
        }
 }
 
+void
+si_nir_opts(struct nir_shader *nir)
+{
+       bool progress;
+        unsigned lower_flrp =
+                (nir->options->lower_flrp16 ? 16 : 0) |
+                (nir->options->lower_flrp32 ? 32 : 0) |
+                (nir->options->lower_flrp64 ? 64 : 0);
+
+       do {
+               progress = false;
+
+               NIR_PASS_V(nir, nir_lower_vars_to_ssa);
+
+               NIR_PASS(progress, nir, nir_opt_copy_prop_vars);
+               NIR_PASS(progress, nir, nir_opt_dead_write_vars);
+
+               NIR_PASS_V(nir, nir_lower_alu_to_scalar, NULL);
+               NIR_PASS_V(nir, nir_lower_phis_to_scalar);
+
+               /* (Constant) copy propagation is needed for txf with offsets. */
+               NIR_PASS(progress, nir, nir_copy_prop);
+               NIR_PASS(progress, nir, nir_opt_remove_phis);
+               NIR_PASS(progress, nir, nir_opt_dce);
+               if (nir_opt_trivial_continues(nir)) {
+                       progress = true;
+                       NIR_PASS(progress, nir, nir_copy_prop);
+                       NIR_PASS(progress, nir, nir_opt_dce);
+               }
+               NIR_PASS(progress, nir, nir_opt_if, true);
+               NIR_PASS(progress, nir, nir_opt_dead_cf);
+               NIR_PASS(progress, nir, nir_opt_cse);
+               NIR_PASS(progress, nir, nir_opt_peephole_select, 8, true, true);
+
+               /* Needed for algebraic lowering */
+               NIR_PASS(progress, nir, nir_opt_algebraic);
+               NIR_PASS(progress, nir, nir_opt_constant_folding);
+
+               if (lower_flrp != 0) {
+                       bool lower_flrp_progress = false;
+
+                       NIR_PASS(lower_flrp_progress, nir, nir_lower_flrp,
+                                lower_flrp,
+                                false /* always_precise */,
+                                nir->options->lower_ffma);
+                       if (lower_flrp_progress) {
+                               NIR_PASS(progress, nir,
+                                        nir_opt_constant_folding);
+                               progress = true;
+                       }
+
+                       /* Nothing should rematerialize any flrps, so we only
+                        * need to do this lowering once.
+                        */
+                       lower_flrp = 0;
+               }
+
+               NIR_PASS(progress, nir, nir_opt_undef);
+               NIR_PASS(progress, nir, nir_opt_conditional_discard);
+               if (nir->options->max_unroll_iterations) {
+                       NIR_PASS(progress, nir, nir_opt_loop_unroll, 0);
+               }
+       } while (progress);
+}
+
 /**
  * Perform "lowering" operations on the NIR that are run once when the shader
  * selector is created.
@@ -824,10 +908,6 @@ si_lower_nir(struct si_shader_selector* sel)
         * - ensure constant offsets for texture instructions are folded
         *   and copy-propagated
         */
-       NIR_PASS_V(sel->nir, nir_lower_returns);
-       NIR_PASS_V(sel->nir, nir_lower_vars_to_ssa);
-       NIR_PASS_V(sel->nir, nir_lower_alu_to_scalar);
-       NIR_PASS_V(sel->nir, nir_lower_phis_to_scalar);
 
        static const struct nir_lower_tex_options lower_tex_options = {
                .lower_txp = ~0u,
@@ -846,36 +926,14 @@ si_lower_nir(struct si_shader_selector* sel)
 
        ac_lower_indirect_derefs(sel->nir, sel->screen->info.chip_class);
 
-       bool progress;
-       do {
-               progress = false;
-
-               /* (Constant) copy propagation is needed for txf with offsets. */
-               NIR_PASS(progress, sel->nir, nir_copy_prop);
-               NIR_PASS(progress, sel->nir, nir_opt_remove_phis);
-               NIR_PASS(progress, sel->nir, nir_opt_dce);
-               if (nir_opt_trivial_continues(sel->nir)) {
-                       progress = true;
-                       NIR_PASS(progress, sel->nir, nir_copy_prop);
-                       NIR_PASS(progress, sel->nir, nir_opt_dce);
-               }
-               NIR_PASS(progress, sel->nir, nir_opt_if);
-               NIR_PASS(progress, sel->nir, nir_opt_dead_cf);
-               NIR_PASS(progress, sel->nir, nir_opt_cse);
-               NIR_PASS(progress, sel->nir, nir_opt_peephole_select, 8, true, true);
-
-               /* Needed for algebraic lowering */
-               NIR_PASS(progress, sel->nir, nir_opt_algebraic);
-               NIR_PASS(progress, sel->nir, nir_opt_constant_folding);
-
-               NIR_PASS(progress, sel->nir, nir_opt_undef);
-               NIR_PASS(progress, sel->nir, nir_opt_conditional_discard);
-               if (sel->nir->options->max_unroll_iterations) {
-                       NIR_PASS(progress, sel->nir, nir_opt_loop_unroll, 0);
-               }
-       } while (progress);
+       si_nir_opts(sel->nir);
 
        NIR_PASS_V(sel->nir, nir_lower_bool_to_int32);
+
+       /* Strip the resulting shader so that the shader cache is more likely
+        * to hit from other similar shaders.
+        */
+       nir_strip(sel->nir);
 }
 
 static void declare_nir_input_vs(struct si_shader_context *ctx,