nir: Gather texture bitmasks in gl_nir_lower_samplers_as_deref.
authorKenneth Graunke <kenneth@whitecape.org>
Sun, 13 Jan 2019 18:39:41 +0000 (10:39 -0800)
committerKenneth Graunke <kenneth@whitecape.org>
Tue, 12 Feb 2019 05:34:45 +0000 (21:34 -0800)
Eric and I would like a bitmask of which samplers are used, similar to
prog->SamplersUsed, but available in NIR.  The linker uses SamplersUsed
for resource limit checking, but later optimizations may eliminate more
samplers.  So instead of propagating it through, we gather a new one.
While there, we also gather the existing textures_used_by_txf bitmask.

Gathering these bitfields in nir_shader_gather_info is awkward at best.
The main reason is that it introduces an ordering dependency between the
two passes.  If gathering runs before lower_samplers_as_deref, it can't
look at var->data.binding.  If the driver doesn't use the full lowering
to texture_index/texture_array_size (like radeonsi), then the gathering
can't use those fields.  Gathering might be run early /and/ late, first
to get varying info, and later to update it after variant lowering.  At
this point, should gathering work on pre-lowered or post-lowered code?
Pre-lowered is also harder due to the presence of structure types.

Just doing the gathering when we do the lowering alleviates these
ordering problems.  This fixes ordering issues in i965 and makes the
txf info gathering work for radeonsi (though they don't use it).

Reviewed-by: Eric Anholt <eric@anholt.net>
src/compiler/glsl/gl_nir_lower_samplers_as_deref.c
src/compiler/nir/nir_gather_info.c
src/compiler/shader_info.h
src/mesa/drivers/dri/i965/brw_link.cpp
src/mesa/state_tracker/st_glsl_to_nir.cpp
src/mesa/state_tracker/st_nir.h
src/mesa/state_tracker/st_nir_builtins.c

index c8f17ef9aaa184c06b46dd17cf88f000273bd100..ea32195d42f8a840ca9c284eda543de07a6b2232 100644 (file)
@@ -197,6 +197,26 @@ lower_deref(nir_builder *b, struct lower_samplers_as_deref_state *state,
    return new_deref;
 }
 
+static void
+record_textures_used(struct shader_info *info,
+                     nir_deref_instr *deref,
+                     nir_texop op)
+{
+   nir_variable *var = nir_deref_instr_get_variable(deref);
+
+   /* Structs have been lowered already, so get_aoa_size is sufficient. */
+   const unsigned size =
+      glsl_type_is_array(var->type) ? glsl_get_aoa_size(var->type) : 1;
+   unsigned mask = ((1ull << MAX2(size, 1)) - 1) << var->data.binding;
+
+   info->textures_used |= mask;
+
+   if (op == nir_texop_txf ||
+       op == nir_texop_txf_ms ||
+       op == nir_texop_txf_ms_mcs)
+      info->textures_used_by_txf |= mask;
+}
+
 static bool
 lower_sampler(nir_tex_instr *instr, struct lower_samplers_as_deref_state *state,
               nir_builder *b)
@@ -217,6 +237,7 @@ lower_sampler(nir_tex_instr *instr, struct lower_samplers_as_deref_state *state,
       if (texture_deref) {
          nir_instr_rewrite_src(&instr->instr, &instr->src[texture_idx].src,
                                nir_src_for_ssa(&texture_deref->dest.ssa));
+         record_textures_used(&b->shader->info, texture_deref, instr->op);
       }
    }
 
@@ -297,6 +318,9 @@ gl_nir_lower_samplers_as_deref(nir_shader *shader,
    state.remap_table = _mesa_hash_table_create(NULL, _mesa_key_hash_string,
                                                _mesa_key_string_equal);
 
+   shader->info.textures_used = 0;
+   shader->info.textures_used_by_txf = 0;
+
    nir_foreach_function(function, shader) {
       if (function->impl)
          progress |= lower_impl(function->impl, &state);
index 18a74a135d927634f72796ce422652af7a8f4db2..19438912ff018590866426ca3930f83c443c1932 100644 (file)
@@ -286,13 +286,6 @@ gather_tex_info(nir_tex_instr *instr, nir_shader *shader)
    case nir_texop_tg4:
       shader->info.uses_texture_gather = true;
       break;
-   case nir_texop_txf:
-   case nir_texop_txf_ms:
-   case nir_texop_txf_ms_mcs:
-      shader->info.textures_used_by_txf |=
-         ((1 << MAX2(instr->texture_array_size, 1)) - 1) <<
-         instr->texture_index;
-      break;
    default:
       break;
    }
index 3d87193875148a4b4e8e5a6b7695e2c90ecddd61..ea6f9a163759dd67369f62b8b9fe4e6d1b2f5726 100644 (file)
@@ -115,6 +115,9 @@ typedef struct shader_info {
    /* Whether or not this shader ever uses textureGather() */
    bool uses_texture_gather;
 
+   /** Bitfield of which textures are used */
+   uint32_t textures_used;
+
    /** Bitfield of which textures are used by texelFetch() */
    uint32_t textures_used_by_txf;
 
index 2cbb1e0b8796894e664df9f351191a83ebca5cfb..66581b21f61fb947dac1d26e5bebb02c2f25c795 100644 (file)
@@ -324,6 +324,8 @@ brw_link_shader(struct gl_context *ctx, struct gl_shader_program *shProg)
       brw_shader_gather_info(prog->nir, prog);
 
       NIR_PASS_V(prog->nir, gl_nir_lower_samplers, shProg);
+      prog->info.textures_used = prog->nir->info.textures_used;
+      prog->info.textures_used_by_txf = prog->nir->info.textures_used_by_txf;
       NIR_PASS_V(prog->nir, gl_nir_lower_atomics, shProg, false);
       NIR_PASS_V(prog->nir, nir_lower_atomics_to_ssbo,
                  prog->nir->info.num_abos);
index b904060528ba403dfa0f84ac24a9e1c925ddef89..89f7424e4dbc846cb0a275a258b55e7e29a3a4a7 100644 (file)
@@ -848,12 +848,18 @@ st_nir_assign_varying_locations(struct st_context *st, nir_shader *nir)
 
 void
 st_nir_lower_samplers(struct pipe_screen *screen, nir_shader *nir,
-                      struct gl_shader_program *shader_program)
+                      struct gl_shader_program *shader_program,
+                      struct gl_program *prog)
 {
    if (screen->get_param(screen, PIPE_CAP_NIR_SAMPLERS_AS_DEREF))
       NIR_PASS_V(nir, gl_nir_lower_samplers_as_deref, shader_program);
    else
       NIR_PASS_V(nir, gl_nir_lower_samplers, shader_program);
+
+   if (prog) {
+      prog->info.textures_used = nir->info.textures_used;
+      prog->info.textures_used_by_txf = nir->info.textures_used_by_txf;
+   }
 }
 
 /* Last third of preparing nir from glsl, which happens after shader
@@ -891,7 +897,7 @@ st_finalize_nir(struct st_context *st, struct gl_program *prog,
       NIR_PASS_V(nir, st_nir_lower_uniforms_to_ubo);
    }
 
-   st_nir_lower_samplers(screen, nir, shader_program);
+   st_nir_lower_samplers(screen, nir, shader_program, prog);
 }
 
 } /* extern "C" */
index 42a04a4676bb461b5b196237e509c0b7e777662c..d45ab3c4474fcfdca3ab1de8bfeb01bc9139249b 100644 (file)
@@ -56,7 +56,8 @@ void st_nir_assign_varying_locations(struct st_context *st,
                                      struct nir_shader *nir);
 
 void st_nir_lower_samplers(struct pipe_screen *screen, struct nir_shader *nir,
-                           struct gl_shader_program *shader_program);
+                           struct gl_shader_program *shader_program,
+                           struct gl_program *prog);
 
 struct pipe_shader_state *
 st_nir_finish_builtin_shader(struct st_context *st,
index 18dc2095d6f8f7f8c5eaf9e724720885d709833f..3826d96a88820819844a56cff59dbf0c39bfa81c 100644 (file)
@@ -61,7 +61,7 @@ st_nir_finish_builtin_shader(struct st_context *st,
 
    st_nir_assign_varying_locations(st, nir);
 
-   st_nir_lower_samplers(screen, nir, NULL);
+   st_nir_lower_samplers(screen, nir, NULL, NULL);
 
    if (st->ctx->Const.PackedDriverUniformStorage) {
       NIR_PASS_V(nir, nir_lower_io, nir_var_uniform, st_glsl_type_dword_size,