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

Author: Jason Ekstrand <[email protected]>
Date:   Tue Apr 26 11:34:35 2022 -0500

nir: Constant fold sampler/texture offsets

Reviewed-by: Alyssa Rosenzweig <[email protected]>
Reviewed-by: Jesse Natalie <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16171>

---

 src/compiler/nir/nir_opt_constant_folding.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/src/compiler/nir/nir_opt_constant_folding.c 
b/src/compiler/nir/nir_opt_constant_folding.c
index 72e1f2ffe29..ea1e0977e29 100644
--- a/src/compiler/nir/nir_opt_constant_folding.c
+++ b/src/compiler/nir/nir_opt_constant_folding.c
@@ -329,11 +329,33 @@ try_fold_txb_to_tex(nir_builder *b, nir_tex_instr *tex)
    return false;
 }
 
+static bool
+try_fold_tex_offset(nir_tex_instr *tex, unsigned *index,
+                    nir_tex_src_type src_type)
+{
+   const int src_idx = nir_tex_instr_src_index(tex, src_type);
+   if (src_idx < 0)
+      return false;
+
+   if (!nir_src_is_const(tex->src[src_idx].src))
+      return false;
+
+   *index += nir_src_as_uint(tex->src[src_idx].src);
+   nir_tex_instr_remove_src(tex, src_idx);
+
+   return true;
+}
+
 static bool
 try_fold_tex(nir_builder *b, nir_tex_instr *tex)
 {
    bool progress = false;
 
+   progress |= try_fold_tex_offset(tex, &tex->texture_index,
+                                   nir_tex_src_texture_offset);
+   progress |= try_fold_tex_offset(tex, &tex->sampler_index,
+                                   nir_tex_src_sampler_offset);
+
    /* txb with a bias of constant zero is just tex. */
    if (tex->op == nir_texop_txb)
       progress |= try_fold_txb_to_tex(b, tex);

Reply via email to