Blender  V3.3
node_shader_tex_white_noise.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
4 #include "node_shader_util.hh"
5 
6 #include "BLI_noise.hh"
7 
8 #include "UI_interface.h"
9 #include "UI_resources.h"
10 
12 
14 {
15  b.is_function_node();
16  b.add_input<decl::Vector>(N_("Vector")).min(-10000.0f).max(10000.0f).implicit_field();
17  b.add_input<decl::Float>(N_("W")).min(-10000.0f).max(10000.0f).make_available([](bNode &node) {
18  /* Default to 1 instead of 4, because it is faster. */
19  node.custom1 = 1;
20  });
21  b.add_output<decl::Float>(N_("Value"));
22  b.add_output<decl::Color>(N_("Color"));
23 }
24 
26 {
27  uiItemR(layout, ptr, "noise_dimensions", UI_ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
28 }
29 
31 {
32  node->custom1 = 3;
33 }
34 
35 static const char *gpu_shader_get_name(const int dimensions)
36 {
37  BLI_assert(dimensions >= 1 && dimensions <= 4);
38  return std::array{"node_white_noise_1d",
39  "node_white_noise_2d",
40  "node_white_noise_3d",
41  "node_white_noise_4d"}[dimensions - 1];
42 }
43 
45  bNode *node,
46  bNodeExecData *UNUSED(execdata),
47  GPUNodeStack *in,
49 {
50  const char *name = gpu_shader_get_name(node->custom1);
51  return GPU_stack_link(mat, node, name, in, out);
52 }
53 
55 {
56  bNodeSocket *sockVector = nodeFindSocket(node, SOCK_IN, "Vector");
57  bNodeSocket *sockW = nodeFindSocket(node, SOCK_IN, "W");
58 
59  nodeSetSocketAvailability(ntree, sockVector, node->custom1 != 1);
60  nodeSetSocketAvailability(ntree, sockW, node->custom1 == 1 || node->custom1 == 4);
61 }
62 
64  private:
65  int dimensions_;
66 
67  public:
68  WhiteNoiseFunction(int dimensions) : dimensions_(dimensions)
69  {
70  BLI_assert(dimensions >= 1 && dimensions <= 4);
71  static std::array<fn::MFSignature, 4> signatures{
76  };
77  this->set_signature(&signatures[dimensions - 1]);
78  }
79 
80  static fn::MFSignature create_signature(int dimensions)
81  {
82  fn::MFSignatureBuilder signature{"WhiteNoise"};
83 
84  if (ELEM(dimensions, 2, 3, 4)) {
85  signature.single_input<float3>("Vector");
86  }
87  if (ELEM(dimensions, 1, 4)) {
88  signature.single_input<float>("W");
89  }
90 
91  signature.single_output<float>("Value");
92  signature.single_output<ColorGeometry4f>("Color");
93 
94  return signature.build();
95  }
96 
98  {
99  int param = ELEM(dimensions_, 2, 3, 4) + ELEM(dimensions_, 1, 4);
100 
101  MutableSpan<float> r_value = params.uninitialized_single_output_if_required<float>(param++,
102  "Value");
104  params.uninitialized_single_output_if_required<ColorGeometry4f>(param++, "Color");
105 
106  const bool compute_value = !r_value.is_empty();
107  const bool compute_color = !r_color.is_empty();
108 
109  switch (dimensions_) {
110  case 1: {
111  const VArray<float> &w = params.readonly_single_input<float>(0, "W");
112  if (compute_color) {
113  for (int64_t i : mask) {
115  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
116  }
117  }
118  if (compute_value) {
119  for (int64_t i : mask) {
120  r_value[i] = noise::hash_float_to_float(w[i]);
121  }
122  }
123  break;
124  }
125  case 2: {
126  const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
127  if (compute_color) {
128  for (int64_t i : mask) {
130  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
131  }
132  }
133  if (compute_value) {
134  for (int64_t i : mask) {
135  r_value[i] = noise::hash_float_to_float(float2(vector[i].x, vector[i].y));
136  }
137  }
138  break;
139  }
140  case 3: {
141  const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
142  if (compute_color) {
143  for (int64_t i : mask) {
145  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
146  }
147  }
148  if (compute_value) {
149  for (int64_t i : mask) {
150  r_value[i] = noise::hash_float_to_float(vector[i]);
151  }
152  }
153  break;
154  }
155  case 4: {
156  const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
157  const VArray<float> &w = params.readonly_single_input<float>(1, "W");
158  if (compute_color) {
159  for (int64_t i : mask) {
161  float4(vector[i].x, vector[i].y, vector[i].z, w[i]));
162  r_color[i] = ColorGeometry4f(c[0], c[1], c[2], 1.0f);
163  }
164  }
165  if (compute_value) {
166  for (int64_t i : mask) {
167  r_value[i] = noise::hash_float_to_float(
168  float4(vector[i].x, vector[i].y, vector[i].z, w[i]));
169  }
170  }
171  break;
172  }
173  }
174  }
175 };
176 
178 {
179  bNode &node = builder.node();
181 }
182 
183 } // namespace blender::nodes::node_shader_tex_white_noise_cc
184 
186 {
188 
189  static bNodeType ntype;
190 
191  sh_fn_node_type_base(&ntype, SH_NODE_TEX_WHITE_NOISE, "White Noise Texture", NODE_CLASS_TEXTURE);
198 
199  nodeRegisterType(&ntype);
200 }
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4465
void node_type_update(struct bNodeType *ntype, void(*updatefunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4443
void nodeSetSocketAvailability(struct bNodeTree *ntree, struct bNodeSocket *sock, bool is_available)
Definition: node.cc:3664
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4390
struct bNodeSocket * nodeFindSocket(const struct bNode *node, eNodeSocketInOut in_out, const char *identifier)
#define NODE_CLASS_TEXTURE
Definition: BKE_node.h:355
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define UNUSED(x)
#define ELEM(...)
@ SOCK_IN
_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
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a or normal between and object coordinate space Combine Create a color from its and value channels Color Retrieve a color or the default fallback if none is specified Separate Split a vector into its and Z components Generates normals with round corners and may slow down renders Vector Displace the surface along an arbitrary direction SH_NODE_TEX_WHITE_NOISE
@ UI_ITEM_R_SPLIT_EMPTY_NAME
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition: btQuadWord.h:119
constexpr bool is_empty() const
Definition: BLI_span.hh:519
void set_signature(const MFSignature *signature)
const MFSignature & signature() const
void call(IndexMask mask, fn::MFParams params, fn::MFContext UNUSED(context)) const override
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
static unsigned c
Definition: RandGen.cpp:83
static void node_shader_init_tex_white_noise(bNodeTree *UNUSED(ntree), bNode *node)
static void node_shader_buts_white_noise(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
static void sh_node_tex_white_noise_declare(NodeDeclarationBuilder &b)
static const char * gpu_shader_get_name(const int dimensions)
static int gpu_shader_tex_white_noise(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void node_shader_update_tex_white_noise(bNodeTree *ntree, bNode *node)
static void sh_node_noise_build_multi_function(NodeMultiFunctionBuilder &builder)
float hash_float_to_float(float k)
Definition: noise.cc:178
float3 hash_float_to_float3(float k)
Definition: noise.cc:203
vec_base< float, 4 > float4
vec_base< float, 2 > float2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
Definition: BLI_color.hh:346
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_sh_tex_white_noise()
void sh_fn_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
#define min(a, b)
Definition: sort.c:35
__int64 int64_t
Definition: stdint.h:89
Defines a node type.
Definition: BKE_node.h:226
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:244
NodeMultiFunctionBuildFunction build_multi_function
Definition: BKE_node.h:313
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)
PointerRNA * ptr
Definition: wm_files.c:3480