43 return std::bit_cast<float>(
static_cast<std::uint32_t
>(exponent +
FLOAT32_BIAS)
58template <std::
int32_t Mantissa>
61 const auto bits = std::bit_cast<std::uint32_t>(value);
72 const std::int32_t shift =
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);
95template <std::
int32_t Mantissa>
98 const auto exponent =
static_cast<std::int32_t
>(bits >> Mantissa);
99 const std::uint32_t mantissa = bits & ((1u << Mantissa) - 1);
102 return mantissa == 0 ? std::numeric_limits<float>::infinity()
103 : std::numeric_limits<float>::quiet_NaN();
117template <std::
int32_t Mantissa>
127template <std::
int32_t Mantissa>
139constexpr std::uint32_t
to_unorm(
const float value,
const std::uint32_t max)
141 return static_cast<std::uint32_t
>(
clamp(value) *
static_cast<float>(max) + 0.5f);
150constexpr float from_unorm(
const std::uint32_t value,
const std::uint32_t max)
152 return static_cast<float>(value) /
static_cast<float>(max);
166template <LinearRGBSpace Space>
185export namespace aest::colors
213constexpr std::uint16_t
to_half(
const float value)
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;
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
>(
237 return (half & 0x8000u) != 0 ? -magnitude : magnitude;
252template <PackableRGB Color>
253constexpr std::array<std::uint8_t, 4>
pack_rgba8(
const Color& color)
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))};
267template <PackableRGB Color>
286template <PackableRGB Color>
299template <PackableRGB Color>
318template <PackableLinearRGB Color>
321 const auto half = [](
const float channel) {
324 return {half(color.ch1()), half(color.ch2()), half(color.ch3()), half(color.alpha())};
334template <PackableLinearRGB Color>
356template <PackableLinearRGB Color>
371template <PackableLinearRGB Color>
394template <PackableLinearRGB Color>
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});
403 const auto floorLog2 =
404 static_cast<std::int32_t
>(std::bit_cast<std::uint32_t>(largest) >>
FLOAT32_MANTISSA)
409 const auto quantize = [&exponent](
const float channel) {
410 return static_cast<std::uint32_t
>(
416 return quantize(red) | quantize(green) << 9 | quantize(blue) << 18
417 |
static_cast<std::uint32_t
>(exponent) << 27;
427template <PackableLinearRGB Color>
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};
constexpr float clamp(const float value)
Clamp a floating-point value to the 0-1 range.
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.
constexpr std::int32_t FLOAT5_MAX_EXPONENT
Largest biased exponent of a finite 5-bit-exponent float.
constexpr std::int32_t FLOAT5_BIAS
Exponent bias of the 5-bit exponent shared by half, 11-bit and 10-bit floats.
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...
constexpr std::uint16_t to_half(const float value)
Converts a float to an IEEE 754 half, rounding to nearest even.
constexpr float decode_float5(const std::uint32_t bits)
Decodes an unsigned float with a 5-bit exponent (bias 15) and Mantissa bits.
constexpr bool IS_PREMULTIPLIED_RGB
Whether a type is the premultiplied form of a linear RGB space.
constexpr Color unpack_rgba16f(const std::array< std::uint16_t, 4 > &halves)
Unpacks RGBA16F halves, in memory order, into a linear color.
constexpr std::int32_t FLOAT10_MANTISSA
Mantissa width of the blue channel of B10G11R11.
constexpr Color unpack_r11g11b10f(const std::uint32_t bits)
Unpacks a B10G11R11_UFLOAT_PACK32 word into a linear color.
constexpr float RGB9E5_MAX
Largest value of a channel of E5B9G9R9: 511 / 512 * 2^16.
constexpr std::int32_t FLOAT11_MANTISSA
Mantissa width of the red and green channels of B10G11R11.
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.
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...
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,...
constexpr Color unpack_rgba8(const std::array< std::uint8_t, 4 > &bytes)
Unpacks RGBA8 unorm bytes, in memory order, into a color.
constexpr std::int32_t FLOAT32_BIAS
Exponent bias of a 32-bit float.
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.
constexpr float from_half(const std::uint16_t half)
Converts an IEEE 754 half to a float, exactly.
constexpr float FLOAT5_MAX
Largest finite value of an unsigned float with a 5-bit exponent and Mantissa bits.
constexpr float power_of_two(const std::int32_t exponent)
A float with value 2 raised to exponent.
constexpr std::int32_t HALF_MANTISSA
Mantissa width of a half float.
constexpr float clamp_float5(const float value)
Clamps a channel to the finite range of an unsigned float format.
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...
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...
constexpr Color unpack_rgb9e5(const std::uint32_t bits)
Unpacks an E5B9G9R9_UFLOAT_PACK32 word into a linear color.
constexpr std::int32_t RGB9E5_MANTISSA
Mantissa width of each channel of E5B9G9R9.
constexpr Color unpack_rgb10a2(const std::uint32_t bits)
Unpacks an A2B10G10R10_UNORM_PACK32 word into a color.
constexpr std::int32_t FLOAT32_MANTISSA
Width of the mantissa of a 32-bit float.
A BasicRGB space with linear channels, tf::Linear.
A color whose channels are linear red, green and blue: a linear RGB space, or its premultiplied form.
A color whose channels are red, green and blue: any RGB space, or the premultiplied form of a linear ...
Any BasicRGB color space.