[Mesa-dev] [PATCH 2/6] util: Move format_r11g11b10f.h to src/util

Jason Ekstrand jason at jlekstrand.net
Wed Aug 3 18:09:16 UTC 2016


It's used from both mesa main and gallium.

Signed-off-by: Jason Ekstrand <jason at jlekstrand.net>
---
 src/gallium/auxiliary/Makefile.sources           |   1 -
 src/gallium/auxiliary/util/u_format_other.c      |   2 +-
 src/gallium/auxiliary/util/u_format_r11g11b10f.h | 232 -----------------------
 src/mesa/main/format_pack.py                     |   2 +-
 src/mesa/main/format_unpack.py                   |   2 +-
 src/mesa/main/mipmap.c                           |   2 +-
 src/mesa/main/texstore.c                         |   2 +-
 src/mesa/swrast/s_texfetch.c                     |   2 +-
 src/mesa/vbo/vbo_attrib_tmp.h                    |   2 +-
 src/util/Makefile.sources                        |   1 +
 src/util/format_r11g11b10f.h                     | 232 +++++++++++++++++++++++
 11 files changed, 240 insertions(+), 240 deletions(-)
 delete mode 100644 src/gallium/auxiliary/util/u_format_r11g11b10f.h
 create mode 100644 src/util/format_r11g11b10f.h

diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
index 230f52b..586f057 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -228,7 +228,6 @@ C_SOURCES := \
 	util/u_format_latc.h \
 	util/u_format_other.c \
 	util/u_format_other.h \
-	util/u_format_r11g11b10f.h \
 	util/u_format_rgtc.c \
 	util/u_format_rgtc.h \
 	util/u_format_s3tc.c \
diff --git a/src/gallium/auxiliary/util/u_format_other.c b/src/gallium/auxiliary/util/u_format_other.c
index 8197835..c53bf55 100644
--- a/src/gallium/auxiliary/util/u_format_other.c
+++ b/src/gallium/auxiliary/util/u_format_other.c
@@ -29,7 +29,7 @@
 #include "u_math.h"
 #include "u_format_other.h"
 #include "util/format_rgb9e5.h"
-#include "u_format_r11g11b10f.h"
+#include "util/format_r11g11b10f.h"
 
 
 void
diff --git a/src/gallium/auxiliary/util/u_format_r11g11b10f.h b/src/gallium/auxiliary/util/u_format_r11g11b10f.h
deleted file mode 100644
index 218822b..0000000
--- a/src/gallium/auxiliary/util/u_format_r11g11b10f.h
+++ /dev/null
@@ -1,232 +0,0 @@
-/*
- * Copyright (C) 2011 Marek Olšák <maraeo at gmail.com>
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-/* Based on code from The OpenGL Programming Guide / 7th Edition, Appendix J.
- * Available here: http://www.opengl-redbook.com/appendices/
- * The algorithm in the book contains a bug though, which is fixed in the code
- * below.
- */
-
-#define UF11(e, m)           ((e << 6) | (m))
-#define UF11_EXPONENT_BIAS   15
-#define UF11_EXPONENT_BITS   0x1F
-#define UF11_EXPONENT_SHIFT  6
-#define UF11_MANTISSA_BITS   0x3F
-#define UF11_MANTISSA_SHIFT  (23 - UF11_EXPONENT_SHIFT)
-#define UF11_MAX_EXPONENT    (UF11_EXPONENT_BITS << UF11_EXPONENT_SHIFT)
-
-#define UF10(e, m)           ((e << 5) | (m))
-#define UF10_EXPONENT_BIAS   15
-#define UF10_EXPONENT_BITS   0x1F
-#define UF10_EXPONENT_SHIFT  5
-#define UF10_MANTISSA_BITS   0x1F
-#define UF10_MANTISSA_SHIFT  (23 - UF10_EXPONENT_SHIFT)
-#define UF10_MAX_EXPONENT    (UF10_EXPONENT_BITS << UF10_EXPONENT_SHIFT)
-
-#define F32_INFINITY         0x7f800000
-
-static inline unsigned f32_to_uf11(float val)
-{
-   union {
-      float f;
-      uint32_t ui;
-   } f32 = {val};
-
-   uint16_t uf11 = 0;
-
-   /* Decode little-endian 32-bit floating-point value */
-   int sign = (f32.ui >> 16) & 0x8000;
-   /* Map exponent to the range [-127,128] */
-   int exponent = ((f32.ui >> 23) & 0xff) - 127;
-   int mantissa = f32.ui & 0x007fffff;
-
-   if (exponent == 128) { /* Infinity or NaN */
-      /* From the GL_EXT_packed_float spec:
-       *
-       *     "Additionally: negative infinity is converted to zero; positive
-       *      infinity is converted to positive infinity; and both positive and
-       *      negative NaN are converted to positive NaN."
-       */
-      uf11 = UF11_MAX_EXPONENT;
-      if (mantissa) {
-         uf11 |= 1; /* NaN */
-      } else {
-         if (sign)
-            uf11 = 0; /* 0.0 */
-      }
-   } else if (sign) {
-      return 0;
-   } else if (val > 65024.0f) {
-      /* From the GL_EXT_packed_float spec:
-       *
-       *     "Likewise, finite positive values greater than 65024 (the maximum
-       *      finite representable unsigned 11-bit floating-point value) are
-       *      converted to 65024."
-       */
-      uf11 = UF11(30, 63);
-   }
-   else if (exponent > -15) { /* Representable value */
-      exponent += UF11_EXPONENT_BIAS;
-      mantissa >>= UF11_MANTISSA_SHIFT;
-      uf11 = exponent << UF11_EXPONENT_SHIFT | mantissa;
-   }
-
-   return uf11;
-}
-
-static inline float uf11_to_f32(uint16_t val)
-{
-   union {
-      float f;
-      uint32_t ui;
-   } f32;
-
-   int exponent = (val & 0x07c0) >> UF11_EXPONENT_SHIFT;
-   int mantissa = (val & 0x003f);
-
-   f32.f = 0.0;
-
-   if (exponent == 0) {
-      if (mantissa != 0) {
-         const float scale = 1.0 / (1 << 20);
-         f32.f = scale * mantissa;
-      }
-   }
-   else if (exponent == 31) {
-      f32.ui = F32_INFINITY | mantissa;
-   }
-   else {
-      float scale, decimal;
-      exponent -= 15;
-      if (exponent < 0) {
-         scale = 1.0f / (1 << -exponent);
-      }
-      else {
-         scale = (float) (1 << exponent);
-      }
-      decimal = 1.0f + (float) mantissa / 64;
-      f32.f = scale * decimal;
-   }
-
-   return f32.f;
-}
-
-static inline unsigned f32_to_uf10(float val)
-{
-   union {
-      float f;
-      uint32_t ui;
-   } f32 = {val};
-
-   uint16_t uf10 = 0;
-
-   /* Decode little-endian 32-bit floating-point value */
-   int sign = (f32.ui >> 16) & 0x8000;
-   /* Map exponent to the range [-127,128] */
-   int exponent = ((f32.ui >> 23) & 0xff) - 127;
-   int mantissa = f32.ui & 0x007fffff;
-
-   if (exponent == 128) {
-      /* From the GL_EXT_packed_float spec:
-       *
-       *     "Additionally: negative infinity is converted to zero; positive
-       *      infinity is converted to positive infinity; and both positive and
-       *      negative NaN are converted to positive NaN."
-       */
-      uf10 = UF10_MAX_EXPONENT;
-      if (mantissa) {
-         uf10 |= 1; /* NaN */
-      } else {
-         if (sign)
-            uf10 = 0; /* 0.0 */
-      }
-   } else if (sign) {
-      return 0;
-   } else if (val > 64512.0f) {
-      /* From the GL_EXT_packed_float spec:
-       *
-       *     "Likewise, finite positive values greater than 64512 (the maximum
-       *      finite representable unsigned 10-bit floating-point value) are
-       *      converted to 64512."
-       */
-      uf10 = UF10(30, 31);
-   }
-   else if (exponent > -15) { /* Representable value */
-      exponent += UF10_EXPONENT_BIAS;
-      mantissa >>= UF10_MANTISSA_SHIFT;
-      uf10 = exponent << UF10_EXPONENT_SHIFT | mantissa;
-   }
-
-   return uf10;
-}
-
-static inline float uf10_to_f32(uint16_t val)
-{
-   union {
-      float f;
-      uint32_t ui;
-   } f32;
-
-   int exponent = (val & 0x03e0) >> UF10_EXPONENT_SHIFT;
-   int mantissa = (val & 0x001f);
-
-   f32.f = 0.0;
-
-   if (exponent == 0) {
-      if (mantissa != 0) {
-         const float scale = 1.0 / (1 << 20);
-         f32.f = scale * mantissa;
-      }
-   }
-   else if (exponent == 31) {
-      f32.ui = F32_INFINITY | mantissa;
-   }
-   else {
-      float scale, decimal;
-      exponent -= 15;
-      if (exponent < 0) {
-         scale = 1.0f / (1 << -exponent);
-      }
-      else {
-         scale = (float) (1 << exponent);
-      }
-      decimal = 1.0f + (float) mantissa / 32;
-      f32.f = scale * decimal;
-   }
-
-   return f32.f;
-}
-
-static inline unsigned float3_to_r11g11b10f(const float rgb[3])
-{
-   return ( f32_to_uf11(rgb[0]) & 0x7ff) |
-          ((f32_to_uf11(rgb[1]) & 0x7ff) << 11) |
-          ((f32_to_uf10(rgb[2]) & 0x3ff) << 22);
-}
-
-static inline void r11g11b10f_to_float3(unsigned rgb, float retval[3])
-{
-   retval[0] = uf11_to_f32( rgb        & 0x7ff);
-   retval[1] = uf11_to_f32((rgb >> 11) & 0x7ff);
-   retval[2] = uf10_to_f32((rgb >> 22) & 0x3ff);
-}
diff --git a/src/mesa/main/format_pack.py b/src/mesa/main/format_pack.py
index 4d45b09..6f2b373 100644
--- a/src/mesa/main/format_pack.py
+++ b/src/mesa/main/format_pack.py
@@ -47,7 +47,7 @@ string = """/*
 #include "format_utils.h"
 #include "macros.h"
 #include "util/format_rgb9e5.h"
-#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
+#include "util/format_r11g11b10f.h"
 #include "util/format_srgb.h"
 
 #define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))
diff --git a/src/mesa/main/format_unpack.py b/src/mesa/main/format_unpack.py
index fc7ea3b..b7fc4c3 100644
--- a/src/mesa/main/format_unpack.py
+++ b/src/mesa/main/format_unpack.py
@@ -47,7 +47,7 @@ string = """/*
 #include "format_utils.h"
 #include "macros.h"
 #include "util/format_rgb9e5.h"
-#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
+#include "util/format_r11g11b10f.h"
 #include "util/format_srgb.h"
 
 #define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))
diff --git a/src/mesa/main/mipmap.c b/src/mesa/main/mipmap.c
index d8fa305..05c13fb 100644
--- a/src/mesa/main/mipmap.c
+++ b/src/mesa/main/mipmap.c
@@ -39,7 +39,7 @@
 #include "macros.h"
 #include "util/half_float.h"
 #include "util/format_rgb9e5.h"
-#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
+#include "util/format_r11g11b10f.h"
 
 
 
diff --git a/src/mesa/main/texstore.c b/src/mesa/main/texstore.c
index 3c391ac..615ba63 100644
--- a/src/mesa/main/texstore.c
+++ b/src/mesa/main/texstore.c
@@ -74,7 +74,7 @@
 #include "glformats.h"
 #include "pixeltransfer.h"
 #include "util/format_rgb9e5.h"
-#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
+#include "util/format_r11g11b10f.h"
 
 
 enum {
diff --git a/src/mesa/swrast/s_texfetch.c b/src/mesa/swrast/s_texfetch.c
index 6856129..4353ea0 100644
--- a/src/mesa/swrast/s_texfetch.c
+++ b/src/mesa/swrast/s_texfetch.c
@@ -44,7 +44,7 @@
 #include "s_context.h"
 #include "s_texfetch.h"
 #include "util/format_rgb9e5.h"
-#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"
+#include "util/format_r11g11b10f.h"
 #include "util/format_srgb.h"
 
 
diff --git a/src/mesa/vbo/vbo_attrib_tmp.h b/src/mesa/vbo/vbo_attrib_tmp.h
index ed0b2de..4e2c874 100644
--- a/src/mesa/vbo/vbo_attrib_tmp.h
+++ b/src/mesa/vbo/vbo_attrib_tmp.h
@@ -25,7 +25,7 @@ USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 **************************************************************************/
 
-#include "util/u_format_r11g11b10f.h"
+#include "util/format_r11g11b10f.h"
 #include "main/varray.h"
 
 
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources
index 6bf0143..6def4f7 100644
--- a/src/util/Makefile.sources
+++ b/src/util/Makefile.sources
@@ -4,6 +4,7 @@ MESA_UTIL_FILES :=	\
 	bitset.h \
 	debug.c \
 	debug.h \
+	format_r11g11b10f.h \
 	format_rgb9e5.h \
 	format_srgb.h \
 	half_float.c \
diff --git a/src/util/format_r11g11b10f.h b/src/util/format_r11g11b10f.h
new file mode 100644
index 0000000..218822b
--- /dev/null
+++ b/src/util/format_r11g11b10f.h
@@ -0,0 +1,232 @@
+/*
+ * Copyright (C) 2011 Marek Olšák <maraeo at gmail.com>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/* Based on code from The OpenGL Programming Guide / 7th Edition, Appendix J.
+ * Available here: http://www.opengl-redbook.com/appendices/
+ * The algorithm in the book contains a bug though, which is fixed in the code
+ * below.
+ */
+
+#define UF11(e, m)           ((e << 6) | (m))
+#define UF11_EXPONENT_BIAS   15
+#define UF11_EXPONENT_BITS   0x1F
+#define UF11_EXPONENT_SHIFT  6
+#define UF11_MANTISSA_BITS   0x3F
+#define UF11_MANTISSA_SHIFT  (23 - UF11_EXPONENT_SHIFT)
+#define UF11_MAX_EXPONENT    (UF11_EXPONENT_BITS << UF11_EXPONENT_SHIFT)
+
+#define UF10(e, m)           ((e << 5) | (m))
+#define UF10_EXPONENT_BIAS   15
+#define UF10_EXPONENT_BITS   0x1F
+#define UF10_EXPONENT_SHIFT  5
+#define UF10_MANTISSA_BITS   0x1F
+#define UF10_MANTISSA_SHIFT  (23 - UF10_EXPONENT_SHIFT)
+#define UF10_MAX_EXPONENT    (UF10_EXPONENT_BITS << UF10_EXPONENT_SHIFT)
+
+#define F32_INFINITY         0x7f800000
+
+static inline unsigned f32_to_uf11(float val)
+{
+   union {
+      float f;
+      uint32_t ui;
+   } f32 = {val};
+
+   uint16_t uf11 = 0;
+
+   /* Decode little-endian 32-bit floating-point value */
+   int sign = (f32.ui >> 16) & 0x8000;
+   /* Map exponent to the range [-127,128] */
+   int exponent = ((f32.ui >> 23) & 0xff) - 127;
+   int mantissa = f32.ui & 0x007fffff;
+
+   if (exponent == 128) { /* Infinity or NaN */
+      /* From the GL_EXT_packed_float spec:
+       *
+       *     "Additionally: negative infinity is converted to zero; positive
+       *      infinity is converted to positive infinity; and both positive and
+       *      negative NaN are converted to positive NaN."
+       */
+      uf11 = UF11_MAX_EXPONENT;
+      if (mantissa) {
+         uf11 |= 1; /* NaN */
+      } else {
+         if (sign)
+            uf11 = 0; /* 0.0 */
+      }
+   } else if (sign) {
+      return 0;
+   } else if (val > 65024.0f) {
+      /* From the GL_EXT_packed_float spec:
+       *
+       *     "Likewise, finite positive values greater than 65024 (the maximum
+       *      finite representable unsigned 11-bit floating-point value) are
+       *      converted to 65024."
+       */
+      uf11 = UF11(30, 63);
+   }
+   else if (exponent > -15) { /* Representable value */
+      exponent += UF11_EXPONENT_BIAS;
+      mantissa >>= UF11_MANTISSA_SHIFT;
+      uf11 = exponent << UF11_EXPONENT_SHIFT | mantissa;
+   }
+
+   return uf11;
+}
+
+static inline float uf11_to_f32(uint16_t val)
+{
+   union {
+      float f;
+      uint32_t ui;
+   } f32;
+
+   int exponent = (val & 0x07c0) >> UF11_EXPONENT_SHIFT;
+   int mantissa = (val & 0x003f);
+
+   f32.f = 0.0;
+
+   if (exponent == 0) {
+      if (mantissa != 0) {
+         const float scale = 1.0 / (1 << 20);
+         f32.f = scale * mantissa;
+      }
+   }
+   else if (exponent == 31) {
+      f32.ui = F32_INFINITY | mantissa;
+   }
+   else {
+      float scale, decimal;
+      exponent -= 15;
+      if (exponent < 0) {
+         scale = 1.0f / (1 << -exponent);
+      }
+      else {
+         scale = (float) (1 << exponent);
+      }
+      decimal = 1.0f + (float) mantissa / 64;
+      f32.f = scale * decimal;
+   }
+
+   return f32.f;
+}
+
+static inline unsigned f32_to_uf10(float val)
+{
+   union {
+      float f;
+      uint32_t ui;
+   } f32 = {val};
+
+   uint16_t uf10 = 0;
+
+   /* Decode little-endian 32-bit floating-point value */
+   int sign = (f32.ui >> 16) & 0x8000;
+   /* Map exponent to the range [-127,128] */
+   int exponent = ((f32.ui >> 23) & 0xff) - 127;
+   int mantissa = f32.ui & 0x007fffff;
+
+   if (exponent == 128) {
+      /* From the GL_EXT_packed_float spec:
+       *
+       *     "Additionally: negative infinity is converted to zero; positive
+       *      infinity is converted to positive infinity; and both positive and
+       *      negative NaN are converted to positive NaN."
+       */
+      uf10 = UF10_MAX_EXPONENT;
+      if (mantissa) {
+         uf10 |= 1; /* NaN */
+      } else {
+         if (sign)
+            uf10 = 0; /* 0.0 */
+      }
+   } else if (sign) {
+      return 0;
+   } else if (val > 64512.0f) {
+      /* From the GL_EXT_packed_float spec:
+       *
+       *     "Likewise, finite positive values greater than 64512 (the maximum
+       *      finite representable unsigned 10-bit floating-point value) are
+       *      converted to 64512."
+       */
+      uf10 = UF10(30, 31);
+   }
+   else if (exponent > -15) { /* Representable value */
+      exponent += UF10_EXPONENT_BIAS;
+      mantissa >>= UF10_MANTISSA_SHIFT;
+      uf10 = exponent << UF10_EXPONENT_SHIFT | mantissa;
+   }
+
+   return uf10;
+}
+
+static inline float uf10_to_f32(uint16_t val)
+{
+   union {
+      float f;
+      uint32_t ui;
+   } f32;
+
+   int exponent = (val & 0x03e0) >> UF10_EXPONENT_SHIFT;
+   int mantissa = (val & 0x001f);
+
+   f32.f = 0.0;
+
+   if (exponent == 0) {
+      if (mantissa != 0) {
+         const float scale = 1.0 / (1 << 20);
+         f32.f = scale * mantissa;
+      }
+   }
+   else if (exponent == 31) {
+      f32.ui = F32_INFINITY | mantissa;
+   }
+   else {
+      float scale, decimal;
+      exponent -= 15;
+      if (exponent < 0) {
+         scale = 1.0f / (1 << -exponent);
+      }
+      else {
+         scale = (float) (1 << exponent);
+      }
+      decimal = 1.0f + (float) mantissa / 32;
+      f32.f = scale * decimal;
+   }
+
+   return f32.f;
+}
+
+static inline unsigned float3_to_r11g11b10f(const float rgb[3])
+{
+   return ( f32_to_uf11(rgb[0]) & 0x7ff) |
+          ((f32_to_uf11(rgb[1]) & 0x7ff) << 11) |
+          ((f32_to_uf10(rgb[2]) & 0x3ff) << 22);
+}
+
+static inline void r11g11b10f_to_float3(unsigned rgb, float retval[3])
+{
+   retval[0] = uf11_to_f32( rgb        & 0x7ff);
+   retval[1] = uf11_to_f32((rgb >> 11) & 0x7ff);
+   retval[2] = uf10_to_f32((rgb >> 22) & 0x3ff);
+}
-- 
2.5.0.400.gff86faf



More information about the mesa-dev mailing list