summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Rudolph <siro@das-labor.org>2016-09-02 15:04:00 +0200
committerJose Fonseca <jfonseca@vmware.com>2016-09-21 11:48:49 +0100
commit26be65c194445d87602712f4e85b2ac44739a583 (patch)
tree60eba5549491eda3b267b70c13fdf91aa8856342
parent3fcc2a9fae0dc4625b0610d4b64ceedb81546e3f (diff)
d3dretrace: Add support for half float formats
Use MESA half-float to float conversion function. Mesa uses MIT compatibel license which should fit apitrace license. Copyright header remains untouched. Signed-off-by: Patrick Rudolph <siro@das-labor.org>
-rw-r--r--helpers/halffloat.hpp133
-rw-r--r--retrace/d3d9state_formats.cpp7
2 files changed, 140 insertions, 0 deletions
diff --git a/helpers/halffloat.hpp b/helpers/halffloat.hpp
new file mode 100644
index 00000000..eacb9f91
--- /dev/null
+++ b/helpers/halffloat.hpp
@@ -0,0 +1,133 @@
+/**************************************************************************
+ *
+ * Copyright 2010 Luca Barbieri
+ *
+ * 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 COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS 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.
+ *
+ **************************************************************************/
+
+
+#ifndef U_HALF_H
+#define U_HALF_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+union fi {
+ float f;
+ int32_t i;
+ uint32_t ui;
+};
+
+/*
+ * References for float <-> half conversions
+ *
+ * http://fgiesen.wordpress.com/2012/03/28/half-to-float-done-quic/
+ * https://gist.github.com/2156668
+ * https://gist.github.com/2144712
+ */
+
+static inline uint16_t
+util_float_to_half(float f)
+{
+ uint32_t sign_mask = 0x80000000;
+ uint32_t round_mask = ~0xfff;
+ uint32_t f32inf = 0xff << 23;
+ uint32_t f16inf = 0x1f << 23;
+ uint32_t sign;
+ union fi magic;
+ union fi f32;
+ uint16_t f16;
+
+ magic.ui = 0xf << 23;
+
+ f32.f = f;
+
+ /* Sign */
+ sign = f32.ui & sign_mask;
+ f32.ui ^= sign;
+
+ if (f32.ui == f32inf) {
+ /* Inf */
+ f16 = 0x7c00;
+ } else if (f32.ui > f32inf) {
+ /* NaN */
+ f16 = 0x7e00;
+ } else {
+ /* Number */
+ f32.ui &= round_mask;
+ f32.f *= magic.f;
+ f32.ui -= round_mask;
+
+ /*
+ * Clamp to max finite value if overflowed.
+ * OpenGL has completely undefined rounding behavior for float to
+ * half-float conversions, and this matches what is mandated for float
+ * to fp11/fp10, which recommend round-to-nearest-finite too.
+ * (d3d10 is deeply unhappy about flushing such values to infinity, and
+ * while it also mandates round-to-zero it doesn't care nearly as much
+ * about that.)
+ */
+ if (f32.ui > f16inf)
+ f32.ui = f16inf - 1;
+
+ f16 = f32.ui >> 13;
+ }
+
+ /* Sign */
+ f16 |= sign >> 16;
+
+ return f16;
+}
+
+static inline float
+util_half_to_float(uint16_t f16)
+{
+ union fi infnan;
+ union fi magic;
+ union fi f32;
+
+ infnan.ui = 0x8f << 23;
+ infnan.f = 65536.0f;
+ magic.ui = 0xef << 23;
+
+ /* Exponent / Mantissa */
+ f32.ui = (f16 & 0x7fff) << 13;
+
+ /* Adjust */
+ f32.f *= magic.f;
+
+ /* Inf / NaN */
+ if (f32.f >= infnan.f)
+ f32.ui |= 0xff << 23;
+
+ /* Sign */
+ f32.ui |= (f16 & 0x8000) << 16;
+
+ return f32.f;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* U_HALF_H */
diff --git a/retrace/d3d9state_formats.cpp b/retrace/d3d9state_formats.cpp
index 845ae26d..bd16f567 100644
--- a/retrace/d3d9state_formats.cpp
+++ b/retrace/d3d9state_formats.cpp
@@ -30,6 +30,7 @@
#include <stdint.h>
#include "image.hpp"
+#include "halffloat.hpp"
#ifdef HAVE_DXGI
#include "dxgistate.hpp"
@@ -181,6 +182,7 @@ ConvertImage(D3DFORMAT SrcFormat,
channelType = image::TYPE_UNORM8;
break;
case D3DFMT_R32F:
+ case D3DFMT_R16F:
case D3DFMT_D16:
case D3DFMT_D16_LOCKABLE:
case D3DFMT_D24S8:
@@ -251,6 +253,11 @@ ConvertImage(D3DFORMAT SrcFormat,
case D3DFMT_R32F:
memcpy(dst, src, Width * sizeof(float));
break;
+ case D3DFMT_R16F:
+ for (unsigned x = 0; x < Width; ++x) {
+ ((float *)dst)[x] = util_half_to_float(((const uint16_t *)src)[x]);
+ }
+ break;
default:
assert(0);
break;