nir: Make image lowering optionally handle the !bindless case as well.
authorEric Anholt <eric@anholt.net>
Thu, 23 Jan 2020 18:49:02 +0000 (10:49 -0800)
committerMarge Bot <eric+marge@anholt.net>
Mon, 24 Feb 2020 18:25:02 +0000 (18:25 +0000)
iris was doing this internally, but let's rename the function and move the
iris code there.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3728>

src/compiler/glsl/gl_nir.h
src/compiler/glsl/gl_nir_lower_images.c
src/mesa/state_tracker/st_glsl_to_nir.cpp
src/panfrost/bifrost/cmdline.c

index 5a5e5d28747207a524dc668673d288c057be4660..4fe3d1df0bbe2f0ab61724628aa54afc7f3c3e31 100644 (file)
@@ -35,7 +35,7 @@ bool gl_nir_lower_atomics(nir_shader *shader,
                           const struct gl_shader_program *shader_program,
                           bool use_binding_as_idx);
 
-bool gl_nir_lower_bindless_images(nir_shader *shader);
+bool gl_nir_lower_images(nir_shader *shader, bool bindless_only);
 bool gl_nir_lower_samplers(nir_shader *shader,
                            const struct gl_shader_program *shader_program);
 bool gl_nir_lower_samplers_as_deref(nir_shader *shader,
index 345d8f606700ecca7b82f6f09ae00f318cb08c40..a02e25093683641676732cd4c2f1c772b56865db 100644 (file)
 /**
  * \file
  *
- * Lower bindless image operations by turning the image_deref_* into a
- * bindless_image_* intrinsic and adding a load_deref on the previous deref
- * source. All applicable indicies are also set so that fetching the variable
- * in the backend wouldn't be needed anymore.
+ * Lower image operations by turning the image_deref_* into a image_* on an
+ * index number or bindless_image_* intrinsic on a load_deref of the previous
+ * deref source. All applicable indicies are also set so that fetching the
+ * variable in the backend wouldn't be needed anymore.
  */
 
 #include "compiler/nir/nir.h"
 
 #include "compiler/glsl/gl_nir.h"
 
+static void
+type_size_align_1(const struct glsl_type *type, unsigned *size, unsigned *align)
+{
+   *size = 1;
+   *align = 1;
+}
+
 static bool
-lower_impl(nir_builder *b, nir_instr *instr) {
+lower_impl(nir_builder *b, nir_instr *instr, bool bindless_only)
+{
    if (instr->type != nir_instr_type_intrinsic)
       return false;
 
@@ -70,17 +78,28 @@ lower_impl(nir_builder *b, nir_instr *instr) {
       return false;
    }
 
-   if (deref->mode == nir_var_uniform && !var->data.bindless)
-      return false;
+   if (bindless_only) {
+      if (deref->mode == nir_var_uniform && !var->data.bindless)
+         return false;
+   }
 
    b->cursor = nir_before_instr(instr);
-   nir_ssa_def *handle = nir_load_deref(b, deref);
-   nir_rewrite_image_intrinsic(intrinsic, handle, true);
+
+   nir_ssa_def *src;
+   if (var->data.bindless) {
+      src = nir_load_deref(b, deref);
+   } else {
+      src = nir_iadd_imm(b,
+                         nir_build_deref_offset(b, deref, type_size_align_1),
+                         var->data.driver_location);
+   }
+   nir_rewrite_image_intrinsic(intrinsic, src, var->data.bindless);
+
    return true;
 }
 
 bool
-gl_nir_lower_bindless_images(nir_shader *shader)
+gl_nir_lower_images(nir_shader *shader, bool bindless_only)
 {
    bool progress = false;
 
@@ -91,7 +110,7 @@ gl_nir_lower_bindless_images(nir_shader *shader)
 
          nir_foreach_block(block, function->impl)
             nir_foreach_instr(instr, block)
-               progress |= lower_impl(&b, instr);
+               progress |= lower_impl(&b, instr, bindless_only);
       }
    }
 
index 796bfb0317e5fc30b7b310c7a49ec01befc2c111..ad104686310da7962b3edbef652e7dadc4b8b09e 100644 (file)
@@ -401,7 +401,7 @@ st_nir_preprocess(struct st_context *st, struct gl_program *prog,
    }
 
    /* before buffers and vars_to_ssa */
-   NIR_PASS_V(nir, gl_nir_lower_bindless_images);
+   NIR_PASS_V(nir, gl_nir_lower_images, true);
 
    /* TODO: Change GLSL to not lower shared memory. */
    if (prog->nir->info.stage == MESA_SHADER_COMPUTE &&
index 243f432275b594d60bdb82acc7ed43185b976c5c..2c0d1a235530f43ea0d8dcbcc1edbdae638648cf 100644 (file)
@@ -62,7 +62,7 @@ compile_shader(char **argv)
                 NIR_PASS_V(nir[i], nir_lower_alu_to_scalar, NULL, NULL);
 
                 /* before buffers and vars_to_ssa */
-                NIR_PASS_V(nir[i], gl_nir_lower_bindless_images);
+                NIR_PASS_V(nir[i], gl_nir_lower_images, true);
 
                 NIR_PASS_V(nir[i], gl_nir_lower_buffers, prog);
                 NIR_PASS_V(nir[i], nir_opt_constant_folding);