Blender V5.3
workbench_common.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
11
12#define EPSILON 0.00001f
13
14#define CAVITY_BUFFER_RANGE 4.0f
15
16namespace workbench {
17
21
22/* From http://aras-p.info/texts/CompactNormalStorage.html
23 * Using Method #4: Sphere-map Transform */
25{
26 float2 fenc = enc.xy * 4.0f - 2.0f;
27 float f = dot(fenc, fenc);
28 float g = sqrt(1.0f - f / 4.0f);
29 float3 n;
30 n.xy = fenc * g;
31 n.z = 1 - f / 2;
32 return n;
33}
34
35/* From http://aras-p.info/texts/CompactNormalStorage.html
36 * Using Method #4: Sphere-map Transform */
37float2 normal_encode(bool front_face, float3 n)
38{
39 n = normalize(front_face ? n : -n);
40 float p = sqrt(n.z * 8.0f + 8.0f);
41 n.xy = clamp(n.xy / p + 0.5f, 0.0f, 1.0f);
42 return n.xy;
43}
44
45/* Encoding into the alpha of a RGBA16F texture. (10bit mantissa) */
46#define TARGET_BITCOUNT 8u
47#define METALLIC_BITS 3u /* Metallic channel is less important. */
48#define ROUGHNESS_BITS (TARGET_BITCOUNT - METALLIC_BITS)
49
50/* Encode 2 float into 1 with the desired precision. */
51float float_pair_encode(float v1, float v2)
52{
53 // constexpr uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS);
54 // constexpr uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS);
55 /* Same as above because some compiler are very dumb and think we use medium int. */
56 constexpr int v1_mask = 0x1F;
57 constexpr int v2_mask = 0x7;
58 int iv1 = int(v1 * float(v1_mask));
59 int iv2 = int(v2 * float(v2_mask)) << int(ROUGHNESS_BITS);
60 return float(iv1 | iv2);
61}
62
63void float_pair_decode(float data, float &v1, float &v2)
64{
65 // constexpr uint v1_mask = ~(0xFFFFFFFFu << ROUGHNESS_BITS);
66 // constexpr uint v2_mask = ~(0xFFFFFFFFu << METALLIC_BITS);
67 /* Same as above because some compiler are very dumb and think we use medium int. */
68 constexpr int v1_mask = 0x1F;
69 constexpr int v2_mask = 0x7;
70 int idata = int(data);
71 v1 = float(idata & v1_mask) * (1.0f / float(v1_mask));
72 v2 = float(idata >> int(ROUGHNESS_BITS)) * (1.0f / float(v2_mask));
73}
74
75} // namespace workbench
#define uniform(slot)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define sqrt
constexpr T clamp(T, U, U) RET
void float_pair_decode(float data, float &v1, float &v2)
float3 normal_decode(float4 enc)
float float_pair_encode(float v1, float v2)
float2 normal_encode(bool front_face, float3 n)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float z
Definition sky_math.h:136
const WorldData & world_data
#define ROUGHNESS_BITS
#define WB_WORLD_SLOT