nir: fix several crashes in nir_lower_tex

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: b154a4154b ("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>
This commit is contained in:
antonino
2023-10-09 16:51:11 +02:00
committed by Marge Bot
parent e9e6a67600
commit a2e96a86e1

View 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,