Blender V5.3
node_shader_tex_gabor.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <numbers>
10
11#include "BLI_noise.hh"
12
13#include "BKE_texture.h"
14
15#include "node_shader_util.hh"
16#include "node_util.hh"
17
18#include "NOD_multi_function.hh"
19
21#include "UI_resources.hh"
22
23namespace blender {
24
26
28
30{
31 b.is_function_node();
32
33 const bool is_compositor = b.tree_or_null() && b.tree_or_null()->type == NTREE_COMPOSIT;
34 const NodeDefaultInputType default_input_type =
37 b.add_input<decl::Vector>("Vector"_ustr)
38 .default_input_type(default_input_type)
39 .description(
40 "The coordinates at which Gabor noise will be evaluated. The Z component is ignored in "
41 "the 2D case");
42
43 b.add_input<decl::Float>("Scale"_ustr)
44 .default_value(5.0f)
45 .description("The scale of the Gabor noise");
46 b.add_input<decl::Float>("Frequency"_ustr)
47 .default_value(2.0f)
48 .min(0.0f)
49 .description(
50 "The rate at which the Gabor noise changes across space. This is different from the "
51 "Scale input in that it only scales perpendicular to the Gabor noise direction");
52 b.add_input<decl::Float>("Anisotropy"_ustr)
53 .default_value(1.0f)
54 .min(0.0f)
55 .max(1.0f)
56 .subtype(PROP_FACTOR)
57 .description(
58 "The directionality of Gabor noise. 1 means the noise is completely directional, while "
59 "0 means the noise is omnidirectional");
60
61 const NodeGaborType gabor_type = b.node_or_null() ? node_storage(*b.node_or_null()).type :
63 b.add_input<decl::Float>("Orientation"_ustr, "Orientation 2D"_ustr)
64 .default_value(std::numbers::pi / 4)
65 .subtype(PROP_ANGLE)
66 .available(gabor_type == SHD_GABOR_TYPE_2D)
67 .description("The direction of the anisotropic Gabor noise");
68 b.add_input<decl::Vector>("Orientation"_ustr, "Orientation 3D"_ustr)
69 .default_value({std::numbers::sqrt2, std::numbers::sqrt2, 0.0f})
70 .subtype(PROP_DIRECTION)
71 .available(gabor_type == SHD_GABOR_TYPE_3D)
72 .description("The direction of the anisotropic Gabor noise");
73
74 b.add_output<decl::Float>("Value"_ustr)
76 "The Gabor noise value with both random intensity and phase. This is equal to sine the "
77 "phase multiplied by the intensity");
78 b.add_output<decl::Float>("Phase"_ustr)
79 .description("The phase of the Gabor noise, which has no random intensity");
80 b.add_output<decl::Float>("Intensity"_ustr)
81 .description("The intensity of the Gabor noise, which has no random phase");
82}
83
84static void node_shader_buts_tex_gabor(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
85{
86 layout.prop(ptr, "gabor_type", ui::ITEM_R_SPLIT_EMPTY_NAME, "", ICON_NONE);
87}
88
90{
91 NodeTexGabor *storage = MEM_new<NodeTexGabor>(__func__);
93 BKE_texture_colormapping_default(&storage->base.color_mapping);
94
96
97 node->storage = storage;
98}
99
101 bNode *node,
102 bNodeExecData * /*execdata*/,
105{
106 node_shader_gpu_default_tex_coord(material, node, &in[0].link);
108
109 const float type = float(node_storage(*node).type);
110 return GPU_stack_link(material, node, "node_tex_gabor", in, out, GPU_constant(&type));
111}
112
113class GaborNoiseFunction : public mf::MultiFunction {
114 private:
115 NodeGaborType type_;
116
117 public:
118 GaborNoiseFunction(const NodeGaborType type) : type_(type)
119 {
120 static std::array<mf::Signature, 2> signatures{
123 };
124 this->set_signature(&signatures[type]);
125 }
126
127 static mf::Signature create_signature(const NodeGaborType type)
128 {
129 mf::Signature signature;
130 mf::SignatureBuilder builder{"GaborNoise", signature};
131
132 builder.single_input<float3>("Vector");
133 builder.single_input<float>("Scale");
134 builder.single_input<float>("Frequency");
135 builder.single_input<float>("Anisotropy");
136
137 if (type == SHD_GABOR_TYPE_2D) {
138 builder.single_input<float>("Orientation");
139 }
140 else {
141 builder.single_input<float3>("Orientation");
142 }
143
144 builder.single_output<float>("Value", mf::ParamFlag::SupportsUnusedOutput);
145 builder.single_output<float>("Phase", mf::ParamFlag::SupportsUnusedOutput);
146 builder.single_output<float>("Intensity", mf::ParamFlag::SupportsUnusedOutput);
147
148 return signature;
149 }
150
151 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
152 {
153 const VArray<float3> &vector = params.readonly_single_input<float3>(0, "Vector");
154 const VArray<float> &scale = params.readonly_single_input<float>(1, "Scale");
155 const VArray<float> &frequency = params.readonly_single_input<float>(2, "Frequency");
156 const VArray<float> &anisotropy = params.readonly_single_input<float>(3, "Anisotropy");
157 /* A parameter index of 4 is reserved for Orientation input below. */
158 MutableSpan<float> r_value = params.uninitialized_single_output_if_required<float>(5, "Value");
159 MutableSpan<float> r_phase = params.uninitialized_single_output_if_required<float>(6, "Phase");
160 MutableSpan<float> r_intensity = params.uninitialized_single_output_if_required<float>(
161 7, "Intensity");
162
163 switch (type_) {
164 case SHD_GABOR_TYPE_2D: {
165 const VArray<float> &orientation = params.readonly_single_input<float>(4, "Orientation");
166 mask.foreach_index([&](const int64_t i) {
167 noise::gabor(vector[i].xy(),
168 scale[i],
169 frequency[i],
170 anisotropy[i],
171 orientation[i],
172 r_value.is_empty() ? nullptr : &r_value[i],
173 r_phase.is_empty() ? nullptr : &r_phase[i],
174 r_intensity.is_empty() ? nullptr : &r_intensity[i]);
175 });
176 break;
177 }
178 case SHD_GABOR_TYPE_3D: {
179 const VArray<float3> &orientation = params.readonly_single_input<float3>(4, "Orientation");
180 mask.foreach_index([&](const int64_t i) {
181 noise::gabor(vector[i],
182 scale[i],
183 frequency[i],
184 anisotropy[i],
185 orientation[i],
186 r_value.is_empty() ? nullptr : &r_value[i],
187 r_phase.is_empty() ? nullptr : &r_phase[i],
188 r_intensity.is_empty() ? nullptr : &r_intensity[i]);
189 });
190 break;
191 }
192 }
193 }
194
195 void hash_unique(UniqueHashBytes &hash) const override
196 {
197 static constexpr int8_t id = 0;
198 hash.add(&id);
199 hash.add(type_);
200 }
201
203 {
204 ExecutionHints hints;
205 hints.allocates_array = false;
206 hints.min_grain_size = 100;
207 return hints;
208 }
209};
210
212{
213 const NodeTexGabor &storage = node_storage(builder.node());
215}
216
217} // namespace nodes::node_shader_tex_gabor_cc
218
220{
221 namespace file_ns = nodes::node_shader_tex_gabor_cc;
222
223 static bke::bNodeType ntype;
224
225 common_node_type_base(&ntype, "ShaderNodeTexGabor"_ustr, SH_NODE_TEX_GABOR);
226 ntype.ui_name = "Gabor Texture";
227 ntype.ui_description = "Generate Gabor noise";
228 ntype.enum_name_legacy = "TEX_GABOR";
230 ntype.declare = file_ns::sh_node_tex_gabor_declare;
231 ntype.draw_buttons = file_ns::node_shader_buts_tex_gabor;
232 ntype.initfunc = file_ns::node_shader_init_tex_gabor;
234 ntype.gpu_fn = file_ns::node_shader_gpu_tex_gabor;
235 ntype.build_multi_function = file_ns::build_multi_function;
236
237 node_register_type(ntype);
238}
239
240} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_GABOR
void set_signature(const Signature *signature)
static mf::Signature create_signature(const NodeGaborType type)
void call(const IndexMask &mask, mf::Params params, mf::Context) const override
#define storage(slot, qualifiers)
#define frequency(freq)
float3x3 scale(float3x3 mat, float2 scale)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
static void build_multi_function(NodeMultiFunctionBuilder &builder)
static void node_shader_init_tex_gabor(bNodeTree *, bNode *node)
static void node_shader_buts_tex_gabor(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void sh_node_tex_gabor_declare(NodeDeclarationBuilder &b)
static int node_shader_gpu_tex_gabor(GPUMaterial *material, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
void gabor(const float2 coordinates, const float scale, const float frequency, const float anisotropy, const float orientation, float *r_value, float *r_phase, float *r_intensity)
Definition noise.cc:2504
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
__int64 int64_t
Definition BKE_text.h:90
GPUNodeLink * GPU_constant(const float *num)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void node_shader_gpu_default_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
void register_node_type_sh_tex_gabor()
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
VecBase< float, 3 > float3
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
@ PROP_FACTOR
Definition RNA_types.hh:308
@ PROP_DIRECTION
Definition RNA_types.hh:319
signed char int8_t
Definition BKE_text.h:76
void common_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define hash
Definition noise_c.cc:156
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
NodeMultiFunctionBuildFunction build_multi_function
Definition BKE_node.hh:382
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)