Blender  V3.3
magic.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 #pragma once
5 
7 
8 /* Magic */
9 
10 ccl_device_noinline_cpu float3 svm_magic(float3 p, float scale, int n, float distortion)
11 {
12  /*
13  * Prevent NaNs due to input p
14  * Sin and Cosine are periodic about [0 2*PI) so the following
15  * will yield a more accurate result. As it stops the input values
16  * going out of range for floats which caused a NaN. The
17  * calculation of (px + py + pz)*5 can cause an Inf when one or more
18  * values are very large the cos or sin of this results in a NaN
19  * It also addresses the case where one dimension is large relative
20  * to another which caused banding due to the loss of precision in the
21  * smaller value. This is due to the value in the -2*PI to 2*PI range
22  * effectively being lost due to floating point precision.
23  */
24  float px = fmodf(p.x, M_2PI_F);
25  float py = fmodf(p.y, M_2PI_F);
26  float pz = fmodf(p.z, M_2PI_F);
27 
28  float x = sinf((px + py + pz) * 5.0f * scale);
29  float y = cosf((-px + py - pz) * 5.0f * scale);
30  float z = -cosf((-px - py + pz) * 5.0f * scale);
31 
32  if (n > 0) {
33  x *= distortion;
34  y *= distortion;
35  z *= distortion;
36  y = -cosf(x - y + z);
37  y *= distortion;
38 
39  if (n > 1) {
40  x = cosf(x - y - z);
41  x *= distortion;
42 
43  if (n > 2) {
44  z = sinf(-x - y - z);
45  z *= distortion;
46 
47  if (n > 3) {
48  x = -cosf(-x + y - z);
49  x *= distortion;
50 
51  if (n > 4) {
52  y = -sinf(-x + y + z);
53  y *= distortion;
54 
55  if (n > 5) {
56  y = -cosf(-x + y + z);
57  y *= distortion;
58 
59  if (n > 6) {
60  x = cosf(x + y + z);
61  x *= distortion;
62 
63  if (n > 7) {
64  z = sinf(x + y - z);
65  z *= distortion;
66 
67  if (n > 8) {
68  x = -cosf(-x - y + z);
69  x *= distortion;
70 
71  if (n > 9) {
72  y = -sinf(x - y + z);
73  y *= distortion;
74  }
75  }
76  }
77  }
78  }
79  }
80  }
81  }
82  }
83  }
84 
85  if (distortion != 0.0f) {
86  distortion *= 2.0f;
87  x /= distortion;
88  y /= distortion;
89  z /= distortion;
90  }
91 
92  return make_float3(0.5f - x, 0.5f - y, 0.5f - z);
93 }
94 
96  KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
97 {
98  uint depth;
99  uint scale_offset, distortion_offset, co_offset, fac_offset, color_offset;
100 
101  svm_unpack_node_uchar3(node.y, &depth, &color_offset, &fac_offset);
102  svm_unpack_node_uchar3(node.z, &co_offset, &scale_offset, &distortion_offset);
103 
104  uint4 node2 = read_node(kg, &offset);
105  float3 co = stack_load_float3(stack, co_offset);
106  float scale = stack_load_float_default(stack, scale_offset, node2.x);
107  float distortion = stack_load_float_default(stack, distortion_offset, node2.y);
108 
109  float3 color = svm_magic(co, scale, depth, distortion);
110 
111  if (stack_valid(fac_offset))
112  stack_store_float(stack, fac_offset, average(color));
113  if (stack_valid(color_offset))
114  stack_store_float3(stack, color_offset, color);
115  return offset;
116 }
117 
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble z
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint y
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
#define sinf(x)
Definition: cuda/compat.h:102
#define cosf(x)
Definition: cuda/compat.h:101
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_noinline_cpu
Definition: cuda/compat.h:41
#define ccl_device_noinline
Definition: cuda/compat.h:40
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
OperationNode * node
const KernelGlobalsCPU *ccl_restrict KernelGlobals
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
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
Definition: kernel/types.h:925
CCL_NAMESPACE_BEGIN ccl_device_noinline_cpu float3 svm_magic(float3 p, float scale, int n, float distortion)
Definition: magic.h:10
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:95
ccl_device_inline float average(const float2 &a)
Definition: math_float2.h:170
#define fmodf(x, y)
Definition: metal/compat.h:230
#define make_float3(x, y, z)
Definition: metal/compat.h:204
float z
float y
float x
uint x
Definition: types_uint4.h:15
uint y
Definition: types_uint4.h:15
#define M_2PI_F
Definition: util/math.h:60