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

Author: M Henning <[email protected]>
Date:   Tue Dec  5 23:13:46 2023 -0500

nak: Clamp negative texture array indices to zero

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26626>

---

 src/nouveau/compiler/nak_nir_lower_tex.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/src/nouveau/compiler/nak_nir_lower_tex.c 
b/src/nouveau/compiler/nak_nir_lower_tex.c
index b44fd61551c..a7521ceb233 100644
--- a/src/nouveau/compiler/nak_nir_lower_tex.c
+++ b/src/nouveau/compiler/nak_nir_lower_tex.c
@@ -66,8 +66,15 @@ lower_tex(nir_builder *b, nir_tex_instr *tex, const struct 
nak_compiler *nak)
           *
           * TODO: Use F2I.U32.RNE
           */
-         if (tex->op != nir_texop_txf && tex->op != nir_texop_txf_ms)
-            arr_idx = nir_f2u32(b, nir_fadd_imm(b, arr_idx, 0.5));
+         if (tex->op != nir_texop_txf && tex->op != nir_texop_txf_ms) {
+            arr_idx = nir_fadd_imm(b, arr_idx, 0.5);
+
+            // TODO: Hardware seems to clamp negative values to zero for us
+            // in f2u, but we still need this fmax for constant folding.
+            arr_idx = nir_fmax(b, arr_idx, nir_imm_float(b, 0.0));
+
+            arr_idx = nir_f2u32(b, arr_idx);
+         }
 
          arr_idx = nir_umin(b, arr_idx, nir_imm_int(b, UINT16_MAX));
       }

Reply via email to