[Mesa-dev] [PATCH] gallivm: use nearest rounding for float->unorm24 conversion

sroland at vmware.com sroland at vmware.com
Wed Jul 31 06:42:11 PDT 2013


From: Roland Scheidegger <sroland at vmware.com>

Previously we were using truncation, which gives the correct result
only for numbers in [0.5-1.0] range (because there's no mantissa bits
to do any rounding there).
This is frequently hit (and probably only used there) when converting
fragment depth to depth format (d24s8 etc.) or otherwise dealing with
depth format.
---
 src/gallium/auxiliary/gallivm/lp_bld_conv.c |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/gallium/auxiliary/gallivm/lp_bld_conv.c b/src/gallium/auxiliary/gallivm/lp_bld_conv.c
index ba51ff7..29d888d 100644
--- a/src/gallium/auxiliary/gallivm/lp_bld_conv.c
+++ b/src/gallium/auxiliary/gallivm/lp_bld_conv.c
@@ -266,17 +266,21 @@ lp_build_clamped_float_to_unsigned_norm(struct gallivm_state *gallivm,
    else if (dst_width == (mantissa + 1)) {
       /*
        * The destination width matches exactly what can be represented in
-       * floating point (i.e., mantissa + 1 bits). So do a straight
-       * multiplication followed by casting. No further rounding is necessary.
+       * floating point (i.e., mantissa + 1 bits). Even so correct rounding
+       * still needs to be applied (only for numbers in [0.5-1.0] would
+       * conversion using truncation after scaling be sufficient).
        */
-
       double scale;
+      struct lp_build_context uf32_bld;
+      struct lp_type type = src_type;
+      type.sign = 0;
 
+      lp_build_context_init(&uf32_bld, gallivm, type);
       scale = (double)((1ULL << dst_width) - 1);
 
       res = LLVMBuildFMul(builder, src,
                           lp_build_const_vec(gallivm, src_type, scale), "");
-      res = LLVMBuildFPToSI(builder, res, int_vec_type, "");
+      res = lp_build_iround(&uf32_bld, res);
    }
    else {
       /*
-- 
1.7.9.5


More information about the mesa-dev mailing list