1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* 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.
*/
/* Copied from EXT_texture_shared_exponent and edited, getting rid of
* expensive float math bits too. */
#ifndef RGB9E5_H
#define RGB9E5_H
#include <assert.h>
#include <stdint.h>
#include "c99_math.h"
#define RGB9E5_EXPONENT_BITS 5
#define RGB9E5_MANTISSA_BITS 9
#define RGB9E5_EXP_BIAS 15
#define RGB9E5_MAX_VALID_BIASED_EXP 31
#define MAX_RGB9E5_EXP (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)
#define RGB9E5_MANTISSA_VALUES (1<<RGB9E5_MANTISSA_BITS)
#define MAX_RGB9E5_MANTISSA (RGB9E5_MANTISSA_VALUES-1)
#define MAX_RGB9E5 (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP))
static inline int rgb9e5_ClampRange(float x)
{
union { float f; uint32_t u; } f, max;
f.f = x;
max.f = MAX_RGB9E5;
if (f.u > 0x7f800000)
/* catches neg, NaNs */
return 0;
else if (f.u >= max.u)
return max.u;
else
return f.u;
}
static inline unsigned int float3_to_rgb9e5(const float rgb[3])
{
int rm, gm, bm, exp_shared;
uint32_t revdenom_biasedexp;
union { float f; uint32_t u; } rc, bc, gc, maxrgb, revdenom;
rc.u = rgb9e5_ClampRange(rgb[0]);
gc.u = rgb9e5_ClampRange(rgb[1]);
bc.u = rgb9e5_ClampRange(rgb[2]);
maxrgb.u = MAX3(rc.u, gc.u, bc.u);
/*
* Compared to what the spec suggests, instead of conditionally adjusting
* the exponent after the fact do it here by doing the equivalent of +0.5 -
* the int add will spill over into the exponent in this case.
*/
maxrgb.u += maxrgb.u & (1 << (23-9));
exp_shared = MAX2((maxrgb.u >> 23), -RGB9E5_EXP_BIAS - 1 + 127) +
1 + RGB9E5_EXP_BIAS - 127;
revdenom_biasedexp = 127 - (exp_shared - RGB9E5_EXP_BIAS -
RGB9E5_MANTISSA_BITS) + 1;
revdenom.u = revdenom_biasedexp << 23;
assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
/*
* The spec uses strict round-up behavior (d3d10 disagrees, but in any case
* must match what is done above for figuring out exponent).
* We avoid the doubles ((int) rc * revdenom + 0.5) by doing the rounding
* ourselves (revdenom was adjusted by +1, above).
*/
rm = (int) (rc.f * revdenom.f);
gm = (int) (gc.f * revdenom.f);
bm = (int) (bc.f * revdenom.f);
rm = (rm & 1) + (rm >> 1);
gm = (gm & 1) + (gm >> 1);
bm = (bm & 1) + (bm >> 1);
assert(rm <= MAX_RGB9E5_MANTISSA);
assert(gm <= MAX_RGB9E5_MANTISSA);
assert(bm <= MAX_RGB9E5_MANTISSA);
assert(rm >= 0);
assert(gm >= 0);
assert(bm >= 0);
return (exp_shared << 27) | (bm << 18) | (gm << 9) | rm;
}
static inline void rgb9e5_to_float3(unsigned rgb, float retval[3])
{
int exponent;
union { float f; uint32_t u; } scale;
exponent = (rgb >> 27) - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
scale.u = (exponent + 127) << 23;
retval[0] = ( rgb & 0x1ff) * scale.f;
retval[1] = ((rgb >> 9) & 0x1ff) * scale.f;
retval[2] = ((rgb >> 18) & 0x1ff) * scale.f;
}
#endif
|