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

Author: antonino <antonino.manisca...@collabora.com>
Date:   Thu Nov  2 18:03:41 2023 +0100

nir: don't take the derivative of the array index in `nir_lower_tex`

Previosuly when lowering to txd for sampler array the index would be
derived as well, therefore the resulting derivative would have been a
vec with one more component than what the txd instruction expects.

This patch truncates the coordinate vector in this case to make sure the
index is not derived.

Fixes: b154a4154b4 ("nir/lower_tex: rewrite tex/txb -> txd/txl before 
saturating srcs")
Reviewed-by: Faith Ekstrand <faith.ekstr...@collabora.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26012>

---

 src/compiler/nir/nir_lower_tex.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/compiler/nir/nir_lower_tex.c b/src/compiler/nir/nir_lower_tex.c
index 83f3ebb5c9e..716c5dda7a8 100644
--- a/src/compiler/nir/nir_lower_tex.c
+++ b/src/compiler/nir/nir_lower_tex.c
@@ -872,10 +872,14 @@ lower_tex_to_txd(nir_builder *b, nir_tex_instr *tex)
       txd->src[i].src = nir_src_for_ssa(tex->src[i].src.ssa);
       txd->src[i].src_type = tex->src[i].src_type;
    }
-   int coord = nir_tex_instr_src_index(tex, nir_tex_src_coord);
-   assert(coord >= 0);
-   nir_def *dfdx = nir_fddx(b, tex->src[coord].src.ssa);
-   nir_def *dfdy = nir_fddy(b, tex->src[coord].src.ssa);
+   int coord_idx = nir_tex_instr_src_index(tex, nir_tex_src_coord);
+   assert(coord_idx >= 0);
+   nir_def *coord = tex->src[coord_idx].src.ssa;
+   /* don't take the derivative of the array index */
+   if (tex->is_array)
+      coord = nir_channels(b, coord, nir_component_mask(coord->num_components 
- 1));
+   nir_def *dfdx = nir_fddx(b, coord);
+   nir_def *dfdy = nir_fddy(b, coord);
    txd->src[tex->num_srcs] = nir_tex_src_for_ssa(nir_tex_src_ddx, dfdx);
    txd->src[tex->num_srcs + 1] = nir_tex_src_for_ssa(nir_tex_src_ddy, dfdy);
 

Reply via email to