Mesa (master): util: when packing depth values, round to nearest.

Jose Fonseca jrfonseca at kemper.freedesktop.org
Fri Oct 4 10:31:04 UTC 2013


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

Author: Matthew McClure <mcclurem at vmware.com>
Date:   Thu Sep 26 16:57:26 2013 -0700

util: when packing depth values, round to nearest.

This patch adds the lrint, lrintf, llrint, and llrintf rounding utility
functions. When packing unorm depth values, we will round to nearest.

Reviewed-by: Roland Scheidegger <sroland at vmware.com>

---

 src/gallium/auxiliary/util/u_math.h       |   52 +++++++++++++++++++++++++++++
 src/gallium/auxiliary/util/u_pack_color.h |    8 ++--
 2 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/src/gallium/auxiliary/util/u_math.h b/src/gallium/auxiliary/util/u_math.h
index 702d4e9..478a4aa 100644
--- a/src/gallium/auxiliary/util/u_math.h
+++ b/src/gallium/auxiliary/util/u_math.h
@@ -162,7 +162,59 @@ float log2f(float f)
 #endif
 
 
+#if __STDC_VERSION__ < 199901L && !defined(__cplusplus)
+static INLINE long int
+lrint(double d)
+{
+   long int rounded = (long int)(d + 0.5);
+
+   if (d - floor(d) == 0.5) {
+      if (rounded % 2 != 0)
+         rounded += (d > 0) ? -1 : 1;
+   }
+
+   return rounded;
+}
+
+static INLINE long int
+lrintf(float f)
+{
+   long int rounded = (long int)(f + 0.5f);
+
+   if (f - floorf(f) == 0.5f) {
+      if (rounded % 2 != 0)
+         rounded += (f > 0) ? -1 : 1;
+   }
+
+   return rounded;
+}
 
+static INLINE long long int
+llrint(double d)
+{
+   long long int rounded = (long long int)(d + 0.5);
+
+   if (d - floor(d) == 0.5) {
+      if (rounded % 2 != 0)
+         rounded += (d > 0) ? -1 : 1;
+   }
+
+   return rounded;
+}
+
+static INLINE long long int
+llrintf(float f)
+{
+   long long int rounded = (long long int)(f + 0.5f);
+
+   if (f - floorf(f) == 0.5f) {
+      if (rounded % 2 != 0)
+         rounded += (f > 0) ? -1 : 1;
+   }
+
+   return rounded;
+}
+#endif /* C99 */
 
 #define POW2_TABLE_SIZE_LOG2 9
 #define POW2_TABLE_SIZE (1 << POW2_TABLE_SIZE_LOG2)
diff --git a/src/gallium/auxiliary/util/u_pack_color.h b/src/gallium/auxiliary/util/u_pack_color.h
index 102ad60..3625273 100644
--- a/src/gallium/auxiliary/util/u_pack_color.h
+++ b/src/gallium/auxiliary/util/u_pack_color.h
@@ -528,12 +528,12 @@ util_pack_z(enum pipe_format format, double z)
    case PIPE_FORMAT_Z16_UNORM:
       if (z == 1.0)
          return 0xffff;
-      return (uint32_t) (z * 0xffff);
+      return (uint32_t) lrint(z * 0xffff);
    case PIPE_FORMAT_Z32_UNORM:
       /* special-case to avoid overflow */
       if (z == 1.0)
          return 0xffffffff;
-      return (uint32_t) (z * 0xffffffff);
+      return (uint32_t) llrint(z * 0xffffffff);
    case PIPE_FORMAT_Z32_FLOAT:
       fui.f = (float)z;
       return fui.ui;
@@ -541,12 +541,12 @@ util_pack_z(enum pipe_format format, double z)
    case PIPE_FORMAT_Z24X8_UNORM:
       if (z == 1.0)
          return 0xffffff;
-      return (uint32_t) (z * 0xffffff);
+      return (uint32_t) lrint(z * 0xffffff);
    case PIPE_FORMAT_S8_UINT_Z24_UNORM:
    case PIPE_FORMAT_X8Z24_UNORM:
       if (z == 1.0)
          return 0xffffff00;
-      return ((uint32_t) (z * 0xffffff)) << 8;
+      return ((uint32_t) lrint(z * 0xffffff)) << 8;
    case PIPE_FORMAT_S8_UINT:
       /* this case can get it via util_pack_z_stencil() */
       return 0;




More information about the mesa-commit mailing list