Blender V4.3
magic.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8
9/* Magic */
10
11ccl_device_noinline_cpu float3 svm_magic(float3 p, float scale, int n, float distortion)
12{
13 /*
14 * Prevent NaNs due to input p
15 * Sin and Cosine are periodic about [0 2*PI) so the following
16 * will yield a more accurate result. As it stops the input values
17 * going out of range for floats which caused a NaN. The
18 * calculation of (px + py + pz)*5 can cause an Inf when one or more
19 * values are very large the cos or sin of this results in a NaN
20 * It also addresses the case where one dimension is large relative
21 * to another which caused banding due to the loss of precision in the
22 * smaller value. This is due to the value in the -2*PI to 2*PI range
23 * effectively being lost due to floating point precision.
24 */
25 float px = fmodf(p.x, M_2PI_F);
26 float py = fmodf(p.y, M_2PI_F);
27 float pz = fmodf(p.z, M_2PI_F);
28
29 float x = sinf((px + py + pz) * 5.0f * scale);
30 float y = cosf((-px + py - pz) * 5.0f * scale);
31 float z = -cosf((-px - py + pz) * 5.0f * scale);
32
33 if (n > 0) {
34 x *= distortion;
35 y *= distortion;
36 z *= distortion;
37 y = -cosf(x - y + z);
38 y *= distortion;
39
40 if (n > 1) {
41 x = cosf(x - y - z);
42 x *= distortion;
43
44 if (n > 2) {
45 z = sinf(-x - y - z);
46 z *= distortion;
47
48 if (n > 3) {
49 x = -cosf(-x + y - z);
50 x *= distortion;
51
52 if (n > 4) {
53 y = -sinf(-x + y + z);
54 y *= distortion;
55
56 if (n > 5) {
57 y = -cosf(-x + y + z);
58 y *= distortion;
59
60 if (n > 6) {
61 x = cosf(x + y + z);
62 x *= distortion;
63
64 if (n > 7) {
65 z = sinf(x + y - z);
66 z *= distortion;
67
68 if (n > 8) {
69 x = -cosf(-x - y + z);
70 x *= distortion;
71
72 if (n > 9) {
73 y = -sinf(x - y + z);
74 y *= distortion;
75 }
76 }
77 }
78 }
79 }
80 }
81 }
82 }
83 }
84 }
85
86 if (distortion != 0.0f) {
87 distortion *= 2.0f;
88 x /= distortion;
89 y /= distortion;
90 z /= distortion;
91 }
92
93 return make_float3(0.5f - x, 0.5f - y, 0.5f - z);
94}
95
97 KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
98{
99 uint depth;
100 uint scale_offset, distortion_offset, co_offset, fac_offset, color_offset;
101
102 svm_unpack_node_uchar3(node.y, &depth, &color_offset, &fac_offset);
103 svm_unpack_node_uchar3(node.z, &co_offset, &scale_offset, &distortion_offset);
104
105 uint4 node2 = read_node(kg, &offset);
106 float3 co = stack_load_float3(stack, co_offset);
107 float scale = stack_load_float_default(stack, scale_offset, node2.x);
108 float distortion = stack_load_float_default(stack, distortion_offset, node2.y);
109
110 float3 color = svm_magic(co, scale, depth, distortion);
111
112 if (stack_valid(fac_offset))
113 stack_store_float(stack, fac_offset, average(color));
114 if (stack_valid(color_offset))
115 stack_store_float3(stack, color_offset, color);
116 return offset;
117}
118
unsigned int uint
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define sinf(x)
#define cosf(x)
#define ccl_private
#define ccl_device_noinline_cpu
#define ccl_device_noinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
#define fmodf(x, y)
ccl_device_inline void stack_store_float3(ccl_private float *stack, uint a, float3 f)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 stack_load_float3(ccl_private float *stack, uint a)
ccl_device_inline uint4 read_node(KernelGlobals kg, ccl_private int *offset)
ccl_device_forceinline void svm_unpack_node_uchar3(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z)
ccl_device_inline float stack_load_float_default(ccl_private float *stack, uint a, uint value)
ccl_device_inline void stack_store_float(ccl_private float *stack, uint a, float f)
ccl_device_inline bool stack_valid(uint a)
ShaderData
CCL_NAMESPACE_BEGIN ccl_device_noinline_cpu float3 svm_magic(float3 p, float scale, int n, float distortion)
Definition magic.h:11
ccl_device_noinline int svm_node_tex_magic(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
Definition magic.h:96
ccl_device_inline float average(const float2 a)
#define M_2PI_F
Definition sky_float3.h:23
float z
Definition sky_float3.h:27
float y
Definition sky_float3.h:27
float x
Definition sky_float3.h:27
uint x
Definition types_uint4.h:15
uint y
Definition types_uint4.h:15