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