From 5b9ac9a68f001ab5f1244ac027e7841114aed67f Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Mon, 17 Jun 2024 16:14:23 -0500 Subject: [PATCH] nir/format_convert: Use fmin/fmax to clamp R9G9B9E5 data As long as drivers implement an fmin/fmax that do the right thing with NaN, there's no reason for the integer comparison. Reviewed-by: Alyssa Rosenzweig Reviewed-by: Boris Brezillon Part-of: --- src/compiler/nir/nir_format_convert.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/compiler/nir/nir_format_convert.h b/src/compiler/nir/nir_format_convert.h index 4c28cd206b4..87c235be3dd 100644 --- a/src/compiler/nir/nir_format_convert.h +++ b/src/compiler/nir/nir_format_convert.h @@ -408,12 +408,16 @@ nir_format_pack_r9g9b9e5(nir_builder *b, nir_def *color) { /* See also float3_to_rgb9e5 */ - /* First, get rid of negatives and NaN */ - nir_def *clamped = nir_bcsel(b, nir_ugt_imm(b, color, 0x7f800000), - nir_imm_float(b, 0), clamped); - - /* Clamp it to range. */ - clamped = nir_fmin(b, color, nir_imm_float(b, MAX_RGB9E5)); + /* First, we need to clamp it to range. The fmax(color, 0) will also flush + * NaN to 0. We set exact to ensure that nothing optimizes this behavior + * away from us. + */ + float exact_save = b->exact; + b->exact = true; + nir_def *clamped = + nir_fmin(b, nir_fmax(b, color, nir_imm_float(b, 0)), + nir_imm_float(b, MAX_RGB9E5)); + b->exact = exact_save; /* maxrgb.u = MAX3(rc.u, gc.u, bc.u); */ nir_def *maxu = nir_umax(b, nir_channel(b, clamped, 0),