Module: Mesa
Branch: main
Commit: 1462a61b5d626381a083dcda4985975d07da9578
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=1462a61b5d626381a083dcda4985975d07da9578

Author: Kenneth Graunke <[email protected]>
Date:   Wed Oct 19 03:43:51 2022 -0700

st/mesa: Let nir_opt_access() infer non-readable

In issue #3278, Danylo noted that nir_opt_access() could desynchronize
the prog->sh.ImageAccess[] and prog->sh.BindlessImage[].access fields,
which are filled out as part of uniform linking, prior to running this
optimization pass.  Those fields are used to fill out pipe_image_view's
shader_access field, which is used by a lot of drivers these days.

There's an easy solution to this issue however: we can simply call the
pass prior to linking, a few lines earlier.

This lets us infer that images are non-readable, which may let drivers
do additional optimizations.

Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3278
Reviewed-by: Emma Anholt <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/19162>

---

 src/mesa/state_tracker/st_glsl_to_nir.cpp | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/src/mesa/state_tracker/st_glsl_to_nir.cpp 
b/src/mesa/state_tracker/st_glsl_to_nir.cpp
index 868f4371d01..c2eaf4e6a94 100644
--- a/src/mesa/state_tracker/st_glsl_to_nir.cpp
+++ b/src/mesa/state_tracker/st_glsl_to_nir.cpp
@@ -762,6 +762,18 @@ st_link_nir(struct gl_context *ctx,
    if (num_shaders == 1)
       gl_nir_opts(linked_shader[0]->Program->nir);
 
+   /* nir_opt_access() needs to run before linking so that ImageAccess[]
+    * and BindlessImage[].access are filled out with the correct modes.
+    */
+   for (unsigned i = 0; i < num_shaders; i++) {
+      nir_shader *nir = linked_shader[i]->Program->nir;
+
+      nir_opt_access_options opt_access_options;
+      opt_access_options.is_vulkan = false;
+      opt_access_options.infer_non_readable = true;
+      NIR_PASS_V(nir, nir_opt_access, &opt_access_options);
+   }
+
    if (shader_program->data->spirv) {
       static const gl_nir_linker_options opts = {
          true /*fill_parameters */
@@ -808,14 +820,6 @@ st_link_nir(struct gl_context *ctx,
          nir_lower_indirect_derefs(nir, mode, UINT32_MAX);
       }
 
-      /* don't infer ACCESS_NON_READABLE so that Program->sh.ImageAccess is
-       * correct: https://gitlab.freedesktop.org/mesa/mesa/-/issues/3278
-       */
-      nir_opt_access_options opt_access_options;
-      opt_access_options.is_vulkan = false;
-      opt_access_options.infer_non_readable = false;
-      NIR_PASS_V(nir, nir_opt_access, &opt_access_options);
-
       /* This needs to run after the initial pass of nir_lower_vars_to_ssa, so
        * that the buffer indices are constants in nir where they where
        * constants in GLSL. */

Reply via email to