aest 0.1.0
Collection of C++ utility libraries
Loading...
Searching...
No Matches
Packed.cppm
Go to the documentation of this file.
1
9module;
10#include <algorithm>
11#include <array>
12#include <bit>
13#include <concepts>
14#include <cstdint>
15#include <limits>
16export module aest.colors:packed;
17
18import :color;
19import :compositing;
20import :gamut;
21
22namespace aest::colors
23{
25constexpr std::int32_t FLOAT5_BIAS = 15;
26
28constexpr std::int32_t FLOAT5_MAX_EXPONENT = 30;
29
31constexpr std::int32_t FLOAT32_BIAS = 127;
32
34constexpr std::int32_t FLOAT32_MANTISSA = 23;
35
41constexpr float power_of_two(const std::int32_t exponent)
42{
43 return std::bit_cast<float>(static_cast<std::uint32_t>(exponent + FLOAT32_BIAS)
45}
46
58template <std::int32_t Mantissa>
59constexpr std::uint32_t encode_float5(const float value)
60{
61 const auto bits = std::bit_cast<std::uint32_t>(value);
62 const auto exponent = static_cast<std::int32_t>(bits >> FLOAT32_MANTISSA) - FLOAT32_BIAS;
63 const std::uint32_t mantissa = bits & ((1u << FLOAT32_MANTISSA) - 1);
64
65 if (exponent > FLOAT5_MAX_EXPONENT - FLOAT5_BIAS)
66 return static_cast<std::uint32_t>(FLOAT5_MAX_EXPONENT + 1) << Mantissa; // infinity
67
68 // The significand with its implicit 1, and how far to shift it into the
69 // mantissa: further for subnormals, whose exponent is fixed at 1 - bias
70 const std::uint32_t significand = (1u << FLOAT32_MANTISSA) | mantissa;
71 const bool subnormal = exponent < 1 - FLOAT5_BIAS;
72 const std::int32_t shift =
73 FLOAT32_MANTISSA - Mantissa + (subnormal ? 1 - FLOAT5_BIAS - exponent : 0);
74 if (shift > FLOAT32_MANTISSA + 1)
75 return 0; // below half of the smallest subnormal, float zero included
76
77 // Biased exponent above the mantissa; a rounding carry moves into the
78 // exponent, up to infinity
79 const std::uint32_t biased =
80 subnormal ? 0u : static_cast<std::uint32_t>(exponent + FLOAT5_BIAS) << Mantissa;
81 const std::uint32_t kept = significand >> shift;
82 const std::uint32_t remainder = significand & ((1u << shift) - 1);
83 const std::uint32_t halfway = 1u << (shift - 1);
84 const bool roundUp = remainder > halfway || (remainder == halfway && (kept & 1u) != 0);
85 return (biased | (kept & ((1u << Mantissa) - 1))) + (roundUp ? 1u : 0u);
86}
87
95template <std::int32_t Mantissa>
96constexpr float decode_float5(const std::uint32_t bits)
97{
98 const auto exponent = static_cast<std::int32_t>(bits >> Mantissa);
99 const std::uint32_t mantissa = bits & ((1u << Mantissa) - 1);
100
101 if (exponent == FLOAT5_MAX_EXPONENT + 1)
102 return mantissa == 0 ? std::numeric_limits<float>::infinity()
103 : std::numeric_limits<float>::quiet_NaN();
104 if (exponent == 0)
105 return static_cast<float>(mantissa) * power_of_two(1 - FLOAT5_BIAS - Mantissa);
106
107 return std::bit_cast<float>(static_cast<std::uint32_t>(exponent - FLOAT5_BIAS + FLOAT32_BIAS)
109 | mantissa << (FLOAT32_MANTISSA - Mantissa));
110}
111
117template <std::int32_t Mantissa>
119 static_cast<std::uint32_t>(FLOAT5_MAX_EXPONENT) << Mantissa | ((1u << Mantissa) - 1));
120
127template <std::int32_t Mantissa>
128constexpr float clamp_float5(const float value)
129{
130 return std::clamp(value, 0.f, FLOAT5_MAX<Mantissa>);
131}
132
139constexpr std::uint32_t to_unorm(const float value, const std::uint32_t max)
140{
141 return static_cast<std::uint32_t>(clamp(value) * static_cast<float>(max) + 0.5f);
142}
143
150constexpr float from_unorm(const std::uint32_t value, const std::uint32_t max)
151{
152 return static_cast<float>(value) / static_cast<float>(max);
153}
154
159template <typename T>
160constexpr bool IS_PREMULTIPLIED_RGB = false;
161
166template <LinearRGBSpace Space>
168
170constexpr std::int32_t HALF_MANTISSA = 10;
171
173constexpr std::int32_t FLOAT11_MANTISSA = 6;
174
176constexpr std::int32_t FLOAT10_MANTISSA = 5;
177
179constexpr std::int32_t RGB9E5_MANTISSA = 9;
180
182constexpr float RGB9E5_MAX = 65408.f;
183} // namespace aest::colors
184
185export namespace aest::colors
186{
192template <typename T>
194
200template <typename T>
202
213constexpr std::uint16_t to_half(const float value)
214{
215 const auto bits = std::bit_cast<std::uint32_t>(value);
216 const std::uint32_t sign = (bits >> 16) & 0x8000u;
217 const std::uint32_t magnitude = bits & 0x7FFF'FFFFu;
218 constexpr std::uint32_t infBits = 0x7F80'0000u;
219
220 if (magnitude > infBits)
221 return static_cast<std::uint16_t>(sign | 0x7E00u);
222 if (magnitude == infBits)
223 return static_cast<std::uint16_t>(sign | 0x7C00u);
224 return static_cast<std::uint16_t>(
225 sign | encode_float5<HALF_MANTISSA>(std::bit_cast<float>(magnitude)));
226}
227
234constexpr float from_half(const std::uint16_t half)
235{
236 const float magnitude = decode_float5<HALF_MANTISSA>(half & 0x7FFFu);
237 return (half & 0x8000u) != 0 ? -magnitude : magnitude;
238}
239
252template <PackableRGB Color>
253constexpr std::array<std::uint8_t, 4> pack_rgba8(const Color& color)
254{
255 return {static_cast<std::uint8_t>(to_unorm(color.ch1(), 255)),
256 static_cast<std::uint8_t>(to_unorm(color.ch2(), 255)),
257 static_cast<std::uint8_t>(to_unorm(color.ch3(), 255)),
258 static_cast<std::uint8_t>(to_unorm(color.alpha(), 255))};
259}
260
267template <PackableRGB Color>
268constexpr Color unpack_rgba8(const std::array<std::uint8_t, 4>& bytes)
269{
270 return Color{from_unorm(bytes[0], 255), from_unorm(bytes[1], 255), from_unorm(bytes[2], 255),
271 from_unorm(bytes[3], 255)};
272}
273
286template <PackableRGB Color>
287constexpr std::uint32_t pack_rgb10a2(const Color& color)
288{
289 return to_unorm(color.ch1(), 1023) | to_unorm(color.ch2(), 1023) << 10
290 | to_unorm(color.ch3(), 1023) << 20 | to_unorm(color.alpha(), 3) << 30;
291}
292
299template <PackableRGB Color>
300constexpr Color unpack_rgb10a2(const std::uint32_t bits)
301{
302 return Color{from_unorm(bits & 0x3FFu, 1023), from_unorm(bits >> 10 & 0x3FFu, 1023),
303 from_unorm(bits >> 20 & 0x3FFu, 1023), from_unorm(bits >> 30, 3)};
304}
305
318template <PackableLinearRGB Color>
319constexpr std::array<std::uint16_t, 4> pack_rgba16f(const Color& color)
320{
321 const auto half = [](const float channel) {
322 return to_half(clamp_float5<HALF_MANTISSA>(channel));
323 };
324 return {half(color.ch1()), half(color.ch2()), half(color.ch3()), half(color.alpha())};
325}
326
334template <PackableLinearRGB Color>
335constexpr Color unpack_rgba16f(const std::array<std::uint16_t, 4>& halves)
336{
337 return Color{from_half(halves[0]), from_half(halves[1]), from_half(halves[2]),
338 from_half(halves[3])};
339}
340
356template <PackableLinearRGB Color>
357constexpr std::uint32_t pack_r11g11b10f(const Color& color)
358{
362}
363
371template <PackableLinearRGB Color>
372constexpr Color unpack_r11g11b10f(const std::uint32_t bits)
373{
374 return Color{decode_float5<FLOAT11_MANTISSA>(bits & 0x7FFu),
375 decode_float5<FLOAT11_MANTISSA>(bits >> 11 & 0x7FFu),
376 decode_float5<FLOAT10_MANTISSA>(bits >> 22), 1.0f};
377}
378
394template <PackableLinearRGB Color>
395constexpr std::uint32_t pack_rgb9e5(const Color& color)
396{
397 const float red = std::clamp(color.ch1(), 0.f, RGB9E5_MAX);
398 const float green = std::clamp(color.ch2(), 0.f, RGB9E5_MAX);
399 const float blue = std::clamp(color.ch3(), 0.f, RGB9E5_MAX);
400 const float largest = std::max({red, green, blue});
401
402 // floor(log2(largest)) from the float exponent, at least -bias - 1
403 const auto floorLog2 =
404 static_cast<std::int32_t>(std::bit_cast<std::uint32_t>(largest) >> FLOAT32_MANTISSA)
405 - FLOAT32_BIAS;
406 std::int32_t exponent = std::max(-FLOAT5_BIAS - 1, floorLog2) + 1 + FLOAT5_BIAS;
407
408 // Rounding the largest channel up to 2^9 needs the next exponent
409 const auto quantize = [&exponent](const float channel) {
410 return static_cast<std::uint32_t>(
411 channel / power_of_two(exponent - FLOAT5_BIAS - RGB9E5_MANTISSA) + 0.5f);
412 };
413 if (quantize(largest) == 1u << RGB9E5_MANTISSA)
414 ++exponent;
415
416 return quantize(red) | quantize(green) << 9 | quantize(blue) << 18
417 | static_cast<std::uint32_t>(exponent) << 27;
418}
419
427template <PackableLinearRGB Color>
428constexpr Color unpack_rgb9e5(const std::uint32_t bits)
429{
430 const float scale =
431 power_of_two(static_cast<std::int32_t>(bits >> 27) - FLOAT5_BIAS - RGB9E5_MANTISSA);
432 return Color{static_cast<float>(bits & 0x1FFu) * scale,
433 static_cast<float>(bits >> 9 & 0x1FFu) * scale,
434 static_cast<float>(bits >> 18 & 0x1FFu) * scale, 1.0f};
435}
436} // namespace aest::colors
constexpr float clamp(const float value)
Clamp a floating-point value to the 0-1 range.
Definition Color.cppm:29
constexpr std::uint32_t to_unorm(const float value, const std::uint32_t max)
Quantizes a value in the 0-1 range to an unsigned normalized integer.
Definition Packed.cppm:139
constexpr std::int32_t FLOAT5_MAX_EXPONENT
Largest biased exponent of a finite 5-bit-exponent float.
Definition Packed.cppm:28
constexpr std::int32_t FLOAT5_BIAS
Exponent bias of the 5-bit exponent shared by half, 11-bit and 10-bit floats.
Definition Packed.cppm:25
constexpr std::uint32_t pack_rgb10a2(const Color &color)
Packs a color into A2B10G10R10_UNORM_PACK32: red in bits 0-9, green in 10-19, blue in 20-29 and alpha...
Definition Packed.cppm:287
constexpr std::uint16_t to_half(const float value)
Converts a float to an IEEE 754 half, rounding to nearest even.
Definition Packed.cppm:213
constexpr float decode_float5(const std::uint32_t bits)
Decodes an unsigned float with a 5-bit exponent (bias 15) and Mantissa bits.
Definition Packed.cppm:96
constexpr bool IS_PREMULTIPLIED_RGB
Whether a type is the premultiplied form of a linear RGB space.
Definition Packed.cppm:160
constexpr Color unpack_rgba16f(const std::array< std::uint16_t, 4 > &halves)
Unpacks RGBA16F halves, in memory order, into a linear color.
Definition Packed.cppm:335
constexpr std::int32_t FLOAT10_MANTISSA
Mantissa width of the blue channel of B10G11R11.
Definition Packed.cppm:176
constexpr Color unpack_r11g11b10f(const std::uint32_t bits)
Unpacks a B10G11R11_UFLOAT_PACK32 word into a linear color.
Definition Packed.cppm:372
constexpr float RGB9E5_MAX
Largest value of a channel of E5B9G9R9: 511 / 512 * 2^16.
Definition Packed.cppm:182
constexpr std::int32_t FLOAT11_MANTISSA
Mantissa width of the red and green channels of B10G11R11.
Definition Packed.cppm:173
constexpr std::array< std::uint16_t, 4 > pack_rgba16f(const Color &color)
Packs a linear color into RGBA16F halves, in memory order, as R16G16B16A16_SFLOAT stores them.
Definition Packed.cppm:319
constexpr std::uint32_t encode_float5(const float value)
Encodes a non-negative float into an unsigned float with a 5-bit exponent (bias 15) and Mantissa bits...
Definition Packed.cppm:59
constexpr std::uint32_t pack_rgb9e5(const Color &color)
Packs a linear color into E5B9G9R9_UFLOAT_PACK32: 9-bit mantissas of red, green and blue in bits 0-8,...
Definition Packed.cppm:395
constexpr Color unpack_rgba8(const std::array< std::uint8_t, 4 > &bytes)
Unpacks RGBA8 unorm bytes, in memory order, into a color.
Definition Packed.cppm:268
constexpr std::int32_t FLOAT32_BIAS
Exponent bias of a 32-bit float.
Definition Packed.cppm:31
constexpr float from_unorm(const std::uint32_t value, const std::uint32_t max)
Converts an unsigned normalized integer back to the 0-1 range.
Definition Packed.cppm:150
constexpr float from_half(const std::uint16_t half)
Converts an IEEE 754 half to a float, exactly.
Definition Packed.cppm:234
constexpr float FLOAT5_MAX
Largest finite value of an unsigned float with a 5-bit exponent and Mantissa bits.
Definition Packed.cppm:118
constexpr float power_of_two(const std::int32_t exponent)
A float with value 2 raised to exponent.
Definition Packed.cppm:41
constexpr std::int32_t HALF_MANTISSA
Mantissa width of a half float.
Definition Packed.cppm:170
constexpr float clamp_float5(const float value)
Clamps a channel to the finite range of an unsigned float format.
Definition Packed.cppm:128
constexpr std::uint32_t pack_r11g11b10f(const Color &color)
Packs a linear color into B10G11R11_UFLOAT_PACK32: red in bits 0-10 and green in 11-21 as 11-bit floa...
Definition Packed.cppm:357
constexpr std::array< std::uint8_t, 4 > pack_rgba8(const Color &color)
Packs a color into RGBA8 unorm bytes, in memory order, as R8G8B8A8_UNORM and R8G8B8A8_SRGB store them...
Definition Packed.cppm:253
constexpr Color unpack_rgb9e5(const std::uint32_t bits)
Unpacks an E5B9G9R9_UFLOAT_PACK32 word into a linear color.
Definition Packed.cppm:428
constexpr std::int32_t RGB9E5_MANTISSA
Mantissa width of each channel of E5B9G9R9.
Definition Packed.cppm:179
constexpr Color unpack_rgb10a2(const std::uint32_t bits)
Unpacks an A2B10G10R10_UNORM_PACK32 word into a color.
Definition Packed.cppm:300
constexpr std::int32_t FLOAT32_MANTISSA
Width of the mantissa of a 32-bit float.
Definition Packed.cppm:34
A BasicRGB space with linear channels, tf::Linear.
Definition Gamut.cppm:414
A color whose channels are linear red, green and blue: a linear RGB space, or its premultiplied form.
Definition Packed.cppm:201
A color whose channels are red, green and blue: any RGB space, or the premultiplied form of a linear ...
Definition Packed.cppm:193
Any BasicRGB color space.
Definition Gamut.cppm:407