Blender V5.3
types_normal.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2026 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include "util/defines.h"
8#include "util/math_float3.h"
9#include "util/math_int4.h"
10
12
13/* Packed normal using octahedral encoding into 2 x 16-bit signed integers.
14 *
15 * References:
16 * "A Survey of Efficient Representations for Independent Unit Vectors"
17 * JCGT 2014.
18 * "Octahedron normal vector encoding"
19 * https://knarkowicz.wordpress.com/2014/04/16/octahedron-normal-vector-encoding/
20 * */
22 uint value;
23
24 packed_normal() = default;
25
27 {
28 constexpr uint mu = (1u << 16) - 1u;
29 constexpr float hmu = float(mu) / 2.0f;
30 const float inv_l1 = 1.0f / (fabsf(in.x) + fabsf(in.y) + fabsf(in.z) + 1e-6f);
31 float vx = in.x * inv_l1;
32 float vy = in.y * inv_l1;
33 const float wrapx = (1.0f - fabsf(vy)) * copysignf(1.0f, vx);
34 const float wrapy = (1.0f - fabsf(vx)) * copysignf(1.0f, vy);
35 vx = (in.z >= 0.0f) ? vx : wrapx;
36 vy = (in.z >= 0.0f) ? vy : wrapy;
37 const uint dx = uint(clamp(vx * hmu + (hmu + 0.5f), 0.0f, float(mu)));
38 const uint dy = uint(clamp(vy * hmu + (hmu + 0.5f), 0.0f, float(mu)));
39 value = dx | (dy << 16);
40 }
41
43 {
44 constexpr uint mu = (1u << 16) - 1u;
45 constexpr float inv_hmu = 2.0f / float(mu);
46 float nx = float(value & mu) * inv_hmu - 1.0f;
47 float ny = float((value >> 16) & mu) * inv_hmu - 1.0f;
48 const float nz = 1.0f - fabsf(nx) - fabsf(ny);
49 const float t = max(-nz, 0.0f);
50 nx += copysignf(t, -nx);
51 ny += copysignf(t, -ny);
52 return normalize(make_float3(nx, ny, nz));
53 }
54
56 {
57 return value == other.value;
58 }
59
61 {
62 return value != other.value;
63 }
64};
65
66#ifndef __KERNEL_GPU__
67/* SIMD version of octahedral normal decoding. Decodes 4 normals at once. */
72{
73 const int4 mu = make_int4(0xFFFF);
74 const float4 inv_hmu = make_float4(2.0f / 65535.0f);
75 const float4 one = make_float4(1.0f);
76
77 nx = make_float4(value & mu) * inv_hmu - one;
78 ny = make_float4((value >> 16) & mu) * inv_hmu - one;
79 nz = one - fabs(nx) - fabs(ny);
80
81 const float4 t = max(-nz, zero_float4());
82
83 const int4 nx_pos = nx > zero_float4();
84 const int4 ny_pos = ny > zero_float4();
85
86 nx += select(nx_pos, -t, t);
87 ny += select(ny_pos, -t, t);
88
89 const float4 inv_len = one / sqrt(nx * nx + ny * ny + nz * nz);
90 nx *= inv_len;
91 ny *= inv_len;
92 nz *= inv_len;
93}
94#endif
95
btScalar max
#define ccl_private
#define ccl_device_inline
#define ccl_device_inline_method
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define copysignf(x, y)
ccl_device_forceinline int4 make_int4(const int x, const int y, const int z, const int w)
VecBase< float, D > normalize(VecOp< float, D >) RET
#define select(A, B, C)
#define sqrt
constexpr T clamp(T, U, U) RET
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
CCL_NAMESPACE_BEGIN ccl_device_inline float4 zero_float4()
Definition math_float4.h:13
#define fabsf
#define make_float4
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
ccl_device_inline_method bool operator!=(const packed_normal other) const
ccl_device_inline_method float3 decode() const
ccl_device_inline_method bool operator==(const packed_normal other) const
ccl_device_inline_method packed_normal(const float3 in)
packed_normal()=default
ccl_device_inline void packed_normal_decode_simd(const int4 value, ccl_private float4 &nx, ccl_private float4 &ny, ccl_private float4 &nz)