Blender V5.3
gpu_shader_shared_exponent.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
17
18#define RGB9E5_EXPONENT_BITS 5
19#define RGB9E5_MANTISSA_BITS 9
20#define RGB9E5_EXP_BIAS 15
21#define RGB9E5_MAX_VALID_BIASED_EXP 31
22
23#define MAX_RGB9E5_EXP (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)
24#define RGB9E5_MANTISSA_VALUES (1 << RGB9E5_MANTISSA_BITS)
25#define MAX_RGB9E5_MANTISSA (RGB9E5_MANTISSA_VALUES - 1)
26
28{
29 /* Ok, rgb9e5_floor_log2 is not correct for the denorm and zero values, but we
30 * are going to do a max of this value with the minimum rgb9e5 exponent
31 * that will hide these problem cases. */
32 int biased_exponent = floatBitsToInt(x) >> 23;
33 return biased_exponent - 127;
34}
35
36float rgb9e5_exponent_factor(int exponent)
37{
38 /* This pow function could be replaced by a table. There is only 32 values. */
39 return exp2(float(exponent - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS));
40}
41
46
48{
49 constexpr float max_rgb9e5 = float(0xFF80u);
50 color = clamp(color, 0.0f, max_rgb9e5);
51
52 float max_component = max(max(color.r, color.g), color.b);
53 int log2_floored = rgb9e5_floor_log2(max_component);
54 int exp_shared = max(-RGB9E5_EXP_BIAS - 1, log2_floored) + (1 + RGB9E5_EXP_BIAS);
55 float denom = rgb9e5_exponent_factor(exp_shared);
56 int maxm = int(max_component / denom + 0.5f);
57 if (maxm == MAX_RGB9E5_MANTISSA + 1) {
58 denom *= 2.0f;
59 exp_shared += 1;
60 }
61
62 rgb9e5_t result;
63 result.exp_shared = uint(exp_shared);
64 result.mantissa = uint3(color / denom + 0.5f);
65 return result;
66}
67
69{
70 rgb9e5_t result = rgb9e5_from_float3(color);
71 result.exp_shared <<= RGB9E5_MANTISSA_BITS * 3;
72 result.mantissa <<= RGB9E5_MANTISSA_BITS * uint3(0, 1, 2);
73 return result.mantissa.r | result.mantissa.g | result.mantissa.b | result.exp_shared;
74}
75
77{
78 int exp_shared = int(data >> (RGB9E5_MANTISSA_BITS * 3));
79 uint3 mantissa = (uint3(data) >> (RGB9E5_MANTISSA_BITS * uint3(0, 1, 2))) &
81 return float3(mantissa) * rgb9e5_exponent_factor(exp_shared);
82}
btScalar max
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
#define exp2
constexpr T clamp(T, U, U) RET
rgb9e5_t rgb9e5_from_float3(float3 color)
#define MAX_RGB9E5_MANTISSA
#define RGB9E5_EXP_BIAS
uint rgb9e5_encode(float3 color)
float3 rgb9e5_decode(uint data)
#define RGB9E5_MANTISSA_BITS
float rgb9e5_exponent_factor(int exponent)
int rgb9e5_floor_log2(float x)
VecBase< uint32_t, 3 > uint3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float