nir: fix several crashes in `nir_lower_tex`
authorantonino <antonino.maniscalco@collabora.com>
Mon, 9 Oct 2023 14:51:11 +0000 (16:51 +0200)
committerMarge Bot <emma+marge@anholt.net>
Mon, 9 Oct 2023 17:31:34 +0000 (17:31 +0000)
This patch fixes the following issues that lead to crashes in some cases:

* an instruction is inserted to get texture lod that depends on a
  texture instruction that hasn't been inserted yet.
* this code tries to read channel 1 of the lod, but lod is scalar
* the code assumed there would only be 2 srcs, this isn't the case when
  bindless is used.

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

src/compiler/nir/nir_lower_tex.c

index 432eacc..c454018 100644 (file)
@@ -906,17 +906,17 @@ lower_txb_to_txl(nir_builder *b, nir_tex_instr *tex)
    txl->is_new_style_shadow = tex->is_new_style_shadow;
 
    /* reuse all but bias src */
-   for (int i = 0; i < 2; i++) {
+   for (int i = 0; i < tex->num_srcs; i++) {
       if (tex->src[i].src_type != nir_tex_src_bias) {
          txl->src[i].src = nir_src_for_ssa(tex->src[i].src.ssa);
          txl->src[i].src_type = tex->src[i].src_type;
       }
    }
-   nir_def *lod = nir_get_texture_lod(b, txl);
+   nir_def *lod = nir_get_texture_lod(b, tex);
 
    int bias_idx = nir_tex_instr_src_index(tex, nir_tex_src_bias);
    assert(bias_idx >= 0);
-   lod = nir_fadd(b, nir_channel(b, lod, 1), tex->src[bias_idx].src.ssa);
+   lod = nir_fadd(b, lod, tex->src[bias_idx].src.ssa);
    txl->src[tex->num_srcs - 1] = nir_tex_src_for_ssa(nir_tex_src_lod, lod);
 
    nir_def_init(&txl->instr, &txl->def,