Skip to content

Commit 2f87eaf

Browse files
committed
Add B10G11R11 encode/decode
1 parent c3d5031 commit 2f87eaf

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

include/math/color-space.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,33 @@ static f32x4 logLuvToRgb(uint32 logLuv)
234234
return logLuv > 0 ? max(dot3x3(logLuvToRgbMat, luv), f32x4::zero) : f32x4::zero;
235235
}
236236

237+
/**
238+
* @brief Converts floating point value to the B10G11R11 value.
239+
*
240+
* @param value target floating point value
241+
* @param bits packed mantissa bit count
242+
* @param mask packed mantissa mask
243+
*/
244+
static uint32 encodeB10G11R11(float value, uint32 bits, uint32 mask) noexcept
245+
{
246+
// TODO: use here SIMD vector instead, it all can be ported.
247+
auto valueBits = *(const uint32*)&value;
248+
auto exponent = (int32)((valueBits >> 23u) & 0xFFu) - 127 + 15;
249+
if (exponent <= 0) return 0; // 5-bit exponent bias 15
250+
auto mantissa = (valueBits >> 12u) & mask;
251+
return (exponent << bits) | mantissa;
252+
}
253+
/**
254+
* @brief Converts 3D floating point vector to the B10G11R11 value.
255+
* @param rgb target 3D floating point vector
256+
*/
257+
static uint32 encodeB10G11R11(f32x4 rgb) noexcept
258+
{
259+
rgb = clamp(rgb, f32x4::zero, f32x4(FLOAT_BIG_16));
260+
auto r = encodeB10G11R11(rgb.getX(), 6, 0b111111);
261+
auto g = encodeB10G11R11(rgb.getY(), 6, 0b111111);
262+
auto b = encodeB10G11R11(rgb.getZ(), 5, 0b11111);
263+
return (b << 22u) | (g << 11u) | r;
264+
}
265+
237266
} // namespace math

include/math/types.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define _Float16 uint16_t // Note: MSVC fix
2929
#endif
3030

31+
#define FLOAT_SMALL_16 0.00006103515625f
3132
#define FLOAT_BIG_16 65504.0f
3233

3334
namespace math

0 commit comments

Comments
 (0)