2011-04-27 12:37:27 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Marek Olšák <maraeo@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.
|
|
|
|
*/
|
|
|
|
|
2017-03-20 16:04:40 +00:00
|
|
|
#ifndef FORMAT_R11G11B10F_H
|
|
|
|
#define FORMAT_R11G11B10F_H
|
|
|
|
|
2016-08-03 17:58:13 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2011-10-27 00:11:27 +01:00
|
|
|
#define UF11(e, m) ((e << 6) | (m))
|
2011-04-27 12:37:27 +01:00
|
|
|
#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)
|
|
|
|
|
2011-10-27 00:11:27 +01:00
|
|
|
#define UF10(e, m) ((e << 5) | (m))
|
2011-04-27 12:37:27 +01:00
|
|
|
#define UF10_EXPONENT_BIAS 15
|
|
|
|
#define UF10_EXPONENT_BITS 0x1F
|
|
|
|
#define UF10_EXPONENT_SHIFT 5
|
2011-10-26 00:50:44 +01:00
|
|
|
#define UF10_MANTISSA_BITS 0x1F
|
2011-04-27 12:37:27 +01:00
|
|
|
#define UF10_MANTISSA_SHIFT (23 - UF10_EXPONENT_SHIFT)
|
|
|
|
#define UF10_MAX_EXPONENT (UF10_EXPONENT_BITS << UF10_EXPONENT_SHIFT)
|
|
|
|
|
|
|
|
#define F32_INFINITY 0x7f800000
|
|
|
|
|
2016-08-03 17:58:13 +01:00
|
|
|
static inline uint32_t f32_to_uf11(float val)
|
2011-04-27 12:37:27 +01:00
|
|
|
{
|
2011-06-09 00:11:52 +01:00
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
uint32_t ui;
|
|
|
|
} f32 = {val};
|
|
|
|
|
2011-04-27 12:37:27 +01:00
|
|
|
uint16_t uf11 = 0;
|
|
|
|
|
|
|
|
/* Decode little-endian 32-bit floating-point value */
|
2011-06-09 00:11:52 +01:00
|
|
|
int sign = (f32.ui >> 16) & 0x8000;
|
2011-04-27 12:37:27 +01:00
|
|
|
/* Map exponent to the range [-127,128] */
|
2011-06-09 00:11:52 +01:00
|
|
|
int exponent = ((f32.ui >> 23) & 0xff) - 127;
|
|
|
|
int mantissa = f32.ui & 0x007fffff;
|
2011-04-27 12:37:27 +01:00
|
|
|
|
|
|
|
if (exponent == 128) { /* Infinity or NaN */
|
2011-10-27 00:23:50 +01:00
|
|
|
/* 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."
|
|
|
|
*/
|
2011-04-27 12:37:27 +01:00
|
|
|
uf11 = UF11_MAX_EXPONENT;
|
2011-10-27 00:23:50 +01:00
|
|
|
if (mantissa) {
|
2013-07-27 02:45:30 +01:00
|
|
|
uf11 |= 1; /* NaN */
|
2011-10-27 00:23:50 +01:00
|
|
|
} else {
|
2013-07-27 02:45:30 +01:00
|
|
|
if (sign)
|
|
|
|
uf11 = 0; /* 0.0 */
|
2011-10-27 00:23:50 +01:00
|
|
|
}
|
|
|
|
} else if (sign) {
|
|
|
|
return 0;
|
|
|
|
} else if (val > 65024.0f) {
|
2011-10-27 00:11:27 +01:00
|
|
|
/* 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);
|
2016-08-03 19:06:19 +01:00
|
|
|
} else if (exponent > -15) { /* Representable value */
|
2011-04-27 12:37:27 +01:00
|
|
|
exponent += UF11_EXPONENT_BIAS;
|
|
|
|
mantissa >>= UF11_MANTISSA_SHIFT;
|
|
|
|
uf11 = exponent << UF11_EXPONENT_SHIFT | mantissa;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uf11;
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float uf11_to_f32(uint16_t val)
|
2011-04-27 12:37:27 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2016-08-03 19:06:19 +01:00
|
|
|
} else if (exponent == 31) {
|
2011-04-27 12:37:27 +01:00
|
|
|
f32.ui = F32_INFINITY | mantissa;
|
2016-08-03 19:06:19 +01:00
|
|
|
} else {
|
2011-04-27 12:37:27 +01:00
|
|
|
float scale, decimal;
|
|
|
|
exponent -= 15;
|
|
|
|
if (exponent < 0) {
|
2012-01-07 21:16:27 +00:00
|
|
|
scale = 1.0f / (1 << -exponent);
|
2016-08-03 19:06:19 +01:00
|
|
|
} else {
|
2012-01-07 21:16:27 +00:00
|
|
|
scale = (float) (1 << exponent);
|
2011-04-27 12:37:27 +01:00
|
|
|
}
|
2012-01-07 21:16:27 +00:00
|
|
|
decimal = 1.0f + (float) mantissa / 64;
|
2011-04-27 12:37:27 +01:00
|
|
|
f32.f = scale * decimal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return f32.f;
|
|
|
|
}
|
|
|
|
|
2016-08-03 17:58:13 +01:00
|
|
|
static inline uint32_t f32_to_uf10(float val)
|
2011-04-27 12:37:27 +01:00
|
|
|
{
|
2011-06-09 00:11:52 +01:00
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
uint32_t ui;
|
|
|
|
} f32 = {val};
|
|
|
|
|
2011-04-27 12:37:27 +01:00
|
|
|
uint16_t uf10 = 0;
|
|
|
|
|
|
|
|
/* Decode little-endian 32-bit floating-point value */
|
2011-06-09 00:11:52 +01:00
|
|
|
int sign = (f32.ui >> 16) & 0x8000;
|
2011-04-27 12:37:27 +01:00
|
|
|
/* Map exponent to the range [-127,128] */
|
2011-06-09 00:11:52 +01:00
|
|
|
int exponent = ((f32.ui >> 23) & 0xff) - 127;
|
|
|
|
int mantissa = f32.ui & 0x007fffff;
|
2011-04-27 12:37:27 +01:00
|
|
|
|
2011-10-27 00:23:50 +01:00
|
|
|
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."
|
|
|
|
*/
|
2011-04-27 12:37:27 +01:00
|
|
|
uf10 = UF10_MAX_EXPONENT;
|
2011-10-27 00:23:50 +01:00
|
|
|
if (mantissa) {
|
2013-07-27 02:45:30 +01:00
|
|
|
uf10 |= 1; /* NaN */
|
2011-10-27 00:23:50 +01:00
|
|
|
} else {
|
2013-07-27 02:45:30 +01:00
|
|
|
if (sign)
|
|
|
|
uf10 = 0; /* 0.0 */
|
2011-10-27 00:23:50 +01:00
|
|
|
}
|
|
|
|
} else if (sign) {
|
|
|
|
return 0;
|
2013-07-27 02:45:30 +01:00
|
|
|
} else if (val > 64512.0f) {
|
2011-10-27 00:11:27 +01:00
|
|
|
/* 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);
|
2016-08-03 19:06:19 +01:00
|
|
|
} else if (exponent > -15) { /* Representable value */
|
2011-04-27 12:37:27 +01:00
|
|
|
exponent += UF10_EXPONENT_BIAS;
|
|
|
|
mantissa >>= UF10_MANTISSA_SHIFT;
|
|
|
|
uf10 = exponent << UF10_EXPONENT_SHIFT | mantissa;
|
|
|
|
}
|
|
|
|
|
|
|
|
return uf10;
|
|
|
|
}
|
|
|
|
|
2015-07-21 00:58:43 +01:00
|
|
|
static inline float uf10_to_f32(uint16_t val)
|
2011-04-27 12:37:27 +01:00
|
|
|
{
|
|
|
|
union {
|
|
|
|
float f;
|
|
|
|
uint32_t ui;
|
|
|
|
} f32;
|
|
|
|
|
2011-10-26 00:53:00 +01:00
|
|
|
int exponent = (val & 0x03e0) >> UF10_EXPONENT_SHIFT;
|
|
|
|
int mantissa = (val & 0x001f);
|
2011-04-27 12:37:27 +01:00
|
|
|
|
|
|
|
f32.f = 0.0;
|
|
|
|
|
|
|
|
if (exponent == 0) {
|
|
|
|
if (mantissa != 0) {
|
mesa: Fix uf10_to_f32() scale factor in the E == 0 and M != 0 case.
GL_EXT_packed_float, 2.1.B Unsigned 10-Bit Floating-Point Numbers:
0.0, if E == 0 and M == 0,
2^-14 * (M / 32), if E == 0 and M != 0,
2^(E-15) * (1 + M/32), if 0 < E < 31,
INF, if E == 31 and M == 0, or
NaN, if E == 31 and M != 0,
In the second case (E == 0 and M != 0), we were multiplying the mantissa
by 2^-20, when we should have been multiplying by 2^-19 (which is
2^(-14 + -5), or 2^-14 * 2^-5, or 2^-14 / 32).
The previous section defines the formula for 11-bit numbers, which is:
2^-14 * (M / 64), if E == 0 and M != 0,
In other words, we had accidentally copy and pasted the 11-bit code
to the 10-bit case, and neglected to change the exponent.
Fixes dEQP-GLES3.functional.pbo.renderbuffer.r11f_g11f_b10f_triangles
when run with surface dimensions of 1536x1152 or 1920x1080.
Cc: mesa-stable@lists.freedesktop.org
References: https://code.google.com/p/chrome-os-partner/issues/detail?id=56244
Signed-off-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Stephane Marchesin <stephane.marchesin@gmail.com>
Reviewed-by: Antia Puentes <apuentes@igalia.com>
2016-08-16 06:26:42 +01:00
|
|
|
const float scale = 1.0 / (1 << 19);
|
2011-04-27 12:37:27 +01:00
|
|
|
f32.f = scale * mantissa;
|
|
|
|
}
|
2016-08-03 19:06:19 +01:00
|
|
|
} else if (exponent == 31) {
|
2011-04-27 12:37:27 +01:00
|
|
|
f32.ui = F32_INFINITY | mantissa;
|
2016-08-03 19:06:19 +01:00
|
|
|
} else {
|
2011-04-27 12:37:27 +01:00
|
|
|
float scale, decimal;
|
|
|
|
exponent -= 15;
|
|
|
|
if (exponent < 0) {
|
2012-10-17 01:32:57 +01:00
|
|
|
scale = 1.0f / (1 << -exponent);
|
2011-04-27 12:37:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2012-01-07 21:16:27 +00:00
|
|
|
scale = (float) (1 << exponent);
|
2011-04-27 12:37:27 +01:00
|
|
|
}
|
2012-01-07 21:16:27 +00:00
|
|
|
decimal = 1.0f + (float) mantissa / 32;
|
2011-04-27 12:37:27 +01:00
|
|
|
f32.f = scale * decimal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return f32.f;
|
|
|
|
}
|
|
|
|
|
2016-08-03 17:58:13 +01:00
|
|
|
static inline uint32_t float3_to_r11g11b10f(const float rgb[3])
|
2011-04-27 12:37:27 +01:00
|
|
|
{
|
|
|
|
return ( f32_to_uf11(rgb[0]) & 0x7ff) |
|
|
|
|
((f32_to_uf11(rgb[1]) & 0x7ff) << 11) |
|
|
|
|
((f32_to_uf10(rgb[2]) & 0x3ff) << 22);
|
|
|
|
}
|
|
|
|
|
2016-08-03 17:58:13 +01:00
|
|
|
static inline void r11g11b10f_to_float3(uint32_t rgb, float retval[3])
|
2011-04-27 12:37:27 +01:00
|
|
|
{
|
|
|
|
retval[0] = uf11_to_f32( rgb & 0x7ff);
|
|
|
|
retval[1] = uf11_to_f32((rgb >> 11) & 0x7ff);
|
|
|
|
retval[2] = uf10_to_f32((rgb >> 22) & 0x3ff);
|
|
|
|
}
|
2017-03-20 16:04:40 +00:00
|
|
|
|
|
|
|
#endif /* FORMAT_R11G11B10F_H */
|