1 /*
2 * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /* Copied from EXT_texture_shared_exponent and edited, getting rid of
25 * expensive float math bits too. */
26
27 #ifndef RGB9E5_H
28 #define RGB9E5_H
29
30 #include <assert.h>
31 #include <stdint.h>
32 #include <math.h>
33
34 #define RGB9E5_EXPONENT_BITS 5
35 #define RGB9E5_MANTISSA_BITS 9
36 #define RGB9E5_EXP_BIAS 15
37 #define RGB9E5_MAX_VALID_BIASED_EXP 31
38
39 #define MAX_RGB9E5_EXP (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)
40 #define RGB9E5_MANTISSA_VALUES (1<<RGB9E5_MANTISSA_BITS)
41 #define MAX_RGB9E5_MANTISSA (RGB9E5_MANTISSA_VALUES-1)
42 #define MAX_RGB9E5 (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP))
43
rgb9e5_ClampRange(float x)44 static inline int rgb9e5_ClampRange(float x)
45 {
46 union { float f; uint32_t u; } f, max;
47 f.f = x;
48 max.f = MAX_RGB9E5;
49
50 if (f.u > 0x7f800000)
51 /* catches neg, NaNs */
52 return 0;
53 else if (f.u >= max.u)
54 return max.u;
55 else
56 return f.u;
57 }
58
float3_to_rgb9e5(const float rgb[3])59 static inline uint32_t float3_to_rgb9e5(const float rgb[3])
60 {
61 int rm, gm, bm, exp_shared;
62 uint32_t revdenom_biasedexp;
63 union { float f; uint32_t u; } rc, bc, gc, maxrgb, revdenom;
64
65 rc.u = rgb9e5_ClampRange(rgb[0]);
66 gc.u = rgb9e5_ClampRange(rgb[1]);
67 bc.u = rgb9e5_ClampRange(rgb[2]);
68 maxrgb.u = MAX3(rc.u, gc.u, bc.u);
69
70 /*
71 * Compared to what the spec suggests, instead of conditionally adjusting
72 * the exponent after the fact do it here by doing the equivalent of +0.5 -
73 * the int add will spill over into the exponent in this case.
74 */
75 maxrgb.u += maxrgb.u & (1 << (23-9));
76 exp_shared = MAX2((maxrgb.u >> 23), -RGB9E5_EXP_BIAS - 1 + 127) +
77 1 + RGB9E5_EXP_BIAS - 127;
78 revdenom_biasedexp = 127 - (exp_shared - RGB9E5_EXP_BIAS -
79 RGB9E5_MANTISSA_BITS) + 1;
80 revdenom.u = revdenom_biasedexp << 23;
81 assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
82
83 /*
84 * The spec uses strict round-up behavior (d3d10 disagrees, but in any case
85 * must match what is done above for figuring out exponent).
86 * We avoid the doubles ((int) rc * revdenom + 0.5) by doing the rounding
87 * ourselves (revdenom was adjusted by +1, above).
88 */
89 rm = (int) (rc.f * revdenom.f);
90 gm = (int) (gc.f * revdenom.f);
91 bm = (int) (bc.f * revdenom.f);
92 rm = (rm & 1) + (rm >> 1);
93 gm = (gm & 1) + (gm >> 1);
94 bm = (bm & 1) + (bm >> 1);
95
96 assert(rm <= MAX_RGB9E5_MANTISSA);
97 assert(gm <= MAX_RGB9E5_MANTISSA);
98 assert(bm <= MAX_RGB9E5_MANTISSA);
99 assert(rm >= 0);
100 assert(gm >= 0);
101 assert(bm >= 0);
102
103 return (exp_shared << 27) | (bm << 18) | (gm << 9) | rm;
104 }
105
rgb9e5_to_float3(uint32_t rgb,float retval[3])106 static inline void rgb9e5_to_float3(uint32_t rgb, float retval[3])
107 {
108 int exponent;
109 union { float f; uint32_t u; } scale;
110
111 exponent = (rgb >> 27) - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
112 scale.u = (exponent + 127) << 23;
113
114 retval[0] = ( rgb & 0x1ff) * scale.f;
115 retval[1] = ((rgb >> 9) & 0x1ff) * scale.f;
116 retval[2] = ((rgb >> 18) & 0x1ff) * scale.f;
117 }
118
119 #endif
120