Blender  V3.3
wave.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 /* Wave */
9 
11  NodeWaveBandsDirection bands_dir,
12  NodeWaveRingsDirection rings_dir,
13  NodeWaveProfile profile,
14  float3 p,
15  float distortion,
16  float detail,
17  float dscale,
18  float droughness,
19  float phase)
20 {
21  /* Prevent precision issues on unit coordinates. */
22  p = (p + 0.000001f) * 0.999999f;
23 
24  float n;
25 
26  if (type == NODE_WAVE_BANDS) {
27  if (bands_dir == NODE_WAVE_BANDS_DIRECTION_X) {
28  n = p.x * 20.0f;
29  }
30  else if (bands_dir == NODE_WAVE_BANDS_DIRECTION_Y) {
31  n = p.y * 20.0f;
32  }
33  else if (bands_dir == NODE_WAVE_BANDS_DIRECTION_Z) {
34  n = p.z * 20.0f;
35  }
36  else { /* NODE_WAVE_BANDS_DIRECTION_DIAGONAL */
37  n = (p.x + p.y + p.z) * 10.0f;
38  }
39  }
40  else { /* NODE_WAVE_RINGS */
41  float3 rp = p;
42  if (rings_dir == NODE_WAVE_RINGS_DIRECTION_X) {
43  rp *= make_float3(0.0f, 1.0f, 1.0f);
44  }
45  else if (rings_dir == NODE_WAVE_RINGS_DIRECTION_Y) {
46  rp *= make_float3(1.0f, 0.0f, 1.0f);
47  }
48  else if (rings_dir == NODE_WAVE_RINGS_DIRECTION_Z) {
49  rp *= make_float3(1.0f, 1.0f, 0.0f);
50  }
51  /* else: NODE_WAVE_RINGS_DIRECTION_SPHERICAL */
52 
53  n = len(rp) * 20.0f;
54  }
55 
56  n += phase;
57 
58  if (distortion != 0.0f)
59  n += distortion * (fractal_noise_3d(p * dscale, detail, droughness) * 2.0f - 1.0f);
60 
61  if (profile == NODE_WAVE_PROFILE_SIN) {
62  return 0.5f + 0.5f * sinf(n - M_PI_2_F);
63  }
64  else if (profile == NODE_WAVE_PROFILE_SAW) {
65  n /= M_2PI_F;
66  return n - floorf(n);
67  }
68  else { /* NODE_WAVE_PROFILE_TRI */
69  n /= M_2PI_F;
70  return fabsf(n - floorf(n + 0.5f)) * 2.0f;
71  }
72 }
73 
75  KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
76 {
77  uint4 node2 = read_node(kg, &offset);
78  uint4 node3 = read_node(kg, &offset);
79 
80  /* RNA properties */
81  uint type_offset, bands_dir_offset, rings_dir_offset, profile_offset;
82  /* Inputs, Outputs */
83  uint co_offset, scale_offset, distortion_offset, detail_offset, dscale_offset, droughness_offset,
84  phase_offset;
85  uint color_offset, fac_offset;
86 
88  node.y, &type_offset, &bands_dir_offset, &rings_dir_offset, &profile_offset);
89  svm_unpack_node_uchar3(node.z, &co_offset, &scale_offset, &distortion_offset);
91  node.w, &detail_offset, &dscale_offset, &droughness_offset, &phase_offset);
92  svm_unpack_node_uchar2(node2.x, &color_offset, &fac_offset);
93 
94  float3 co = stack_load_float3(stack, co_offset);
95  float scale = stack_load_float_default(stack, scale_offset, node2.y);
96  float distortion = stack_load_float_default(stack, distortion_offset, node2.z);
97  float detail = stack_load_float_default(stack, detail_offset, node2.w);
98  float dscale = stack_load_float_default(stack, dscale_offset, node3.x);
99  float droughness = stack_load_float_default(stack, droughness_offset, node3.y);
100  float phase = stack_load_float_default(stack, phase_offset, node3.z);
101 
102  float f = svm_wave((NodeWaveType)type_offset,
103  (NodeWaveBandsDirection)bands_dir_offset,
104  (NodeWaveRingsDirection)rings_dir_offset,
105  (NodeWaveProfile)profile_offset,
106  co * scale,
107  distortion,
108  detail,
109  dscale,
110  droughness,
111  phase);
112 
113  if (stack_valid(fac_offset))
114  stack_store_float(stack, fac_offset, f);
115  if (stack_valid(color_offset))
116  stack_store_float3(stack, color_offset, make_float3(f, f, f));
117  return offset;
118 }
119 
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 type
#define sinf(x)
Definition: cuda/compat.h:102
#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
int len
Definition: draw_manager.c:108
ccl_device_noinline float fractal_noise_3d(float3 p, float octaves, float roughness)
Definition: fractal_noise.h:69
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_forceinline void svm_unpack_node_uchar2(uint i, ccl_private uint *x, ccl_private uint *y)
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_forceinline void svm_unpack_node_uchar4(uint i, ccl_private uint *x, ccl_private uint *y, ccl_private uint *z, ccl_private uint *w)
ccl_device_inline bool stack_valid(uint a)
NodeWaveBandsDirection
@ NODE_WAVE_BANDS_DIRECTION_Z
@ NODE_WAVE_BANDS_DIRECTION_Y
@ NODE_WAVE_BANDS_DIRECTION_X
NodeWaveType
@ NODE_WAVE_BANDS
NodeWaveProfile
@ NODE_WAVE_PROFILE_SIN
@ NODE_WAVE_PROFILE_SAW
NodeWaveRingsDirection
@ NODE_WAVE_RINGS_DIRECTION_Y
@ NODE_WAVE_RINGS_DIRECTION_X
@ NODE_WAVE_RINGS_DIRECTION_Z
ShaderData
Definition: kernel/types.h:925
#define floorf(x)
Definition: metal/compat.h:224
#define fabsf(x)
Definition: metal/compat.h:219
#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
uint z
Definition: types_uint4.h:15
uint w
Definition: types_uint4.h:15
#define M_PI_2_F
Definition: util/math.h:37
#define M_2PI_F
Definition: util/math.h:60
ccl_device_noinline int svm_node_tex_wave(KernelGlobals kg, ccl_private ShaderData *sd, ccl_private float *stack, uint4 node, int offset)
Definition: wave.h:74
CCL_NAMESPACE_BEGIN ccl_device_noinline_cpu float svm_wave(NodeWaveType type, NodeWaveBandsDirection bands_dir, NodeWaveRingsDirection rings_dir, NodeWaveProfile profile, float3 p, float distortion, float detail, float dscale, float droughness, float phase)
Definition: wave.h:10