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