Blender V5.3
node_shader_tex_image.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "node_shader_util.hh"
10#include "node_util.hh"
11
12#include "BKE_image.hh"
13#include "BKE_node_runtime.hh"
14#include "BKE_texture.h"
15
17
19
20namespace blender {
21
23
25{
26 b.is_function_node();
27 b.add_input<decl::Vector>("Vector"_ustr).default_input_type(NODE_DEFAULT_INPUT_POSITION_FIELD);
28 b.add_output<decl::Color>("Color"_ustr).no_muted_links();
29 b.add_output<decl::Float>("Alpha"_ustr).no_muted_links();
30}
31
33{
34 NodeTexImage *tex = MEM_new<NodeTexImage>(__func__);
38
39 node->storage = tex;
40}
41
43 bNode *node,
44 bNodeExecData * /*execdata*/,
47{
48 Image *ima = id_cast<Image *>(node->id);
49 NodeTexImage *tex = static_cast<NodeTexImage *>(node->storage);
50
51 /* We get the image user from the original node, since GPU image keeps
52 * a pointer to it and the dependency refreshes the original. */
53 bNode *node_original = node->runtime->original ? node->runtime->original : node;
54 NodeTexImage *tex_original = static_cast<NodeTexImage *>(node_original->storage);
55 ImageUser *iuser = &tex_original->iuser;
56
57 if (!ima) {
58 return GPU_stack_link(mat, node, "node_tex_image_empty", in, out);
59 }
60
61 GPUNodeLink **texco = &in[0].link;
62 if (!*texco) {
63 *texco = GPU_attribute(mat, CD_AUTO_FROM_NAME, "");
65 }
66
68
70
71 switch (tex->extension) {
75 break;
79 break;
83 break;
87 break;
88 default:
89 break;
90 }
91
93 /* TODO(fclem): For now assume mipmap is always enabled. */
94 /* Setting the GPU_SAMPLER_FILTERING_ANISOTROPIC_ENABLE enables anisotropic filtering. The
95 * exact number of samples are being determined at bind time by the engine.
96 * See #blender::draw::PassBase<T>::material_set */
97 sampler_state.filtering = GPU_SAMPLER_FILTERING_ANISOTROPIC_ENABLE |
99 }
100 const bool use_cubic = ELEM(tex->interpolation, SHD_INTERP_CUBIC, SHD_INTERP_SMART);
101
102 /* Only use UDIM tiles if projection is flat.
103 * Otherwise treat the first tile as a single image. (See #141776). */
104 const bool use_udim = ima->source == IMA_SRC_TILED && tex->projection == SHD_PROJ_FLAT;
105 if (use_udim) {
106 const char *gpu_node_name = use_cubic ? "node_tex_tile_cubic" : "node_tex_tile_linear";
107 GPUNodeLink *gpu_image, *gpu_image_tile_mapping;
108 GPU_image_tiled(mat, ima, iuser, sampler_state, &gpu_image, &gpu_image_tile_mapping);
109 /* UDIM tiles needs a `sampler2DArray` and `sampler1DArray` for tile mapping. */
110 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image, gpu_image_tile_mapping);
111 }
112 else {
113 const char *gpu_node_name = use_cubic ? "node_tex_image_cubic" : "node_tex_image_linear";
114
115 switch (tex->projection) {
116 case SHD_PROJ_FLAT: {
117 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
118 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image, GPU_kernel_globals());
119 break;
120 }
121 case SHD_PROJ_BOX: {
122 gpu_node_name = use_cubic ? "tex_box_sample_cubic" : "tex_box_sample_linear";
123 GPUNodeLink *vnor, *wnor, *col1, *col2, *col3;
125 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
126 GPU_link(mat, "world_normals_get", GPU_shading_data(), &vnor);
127 GPU_link(mat,
128 "normal_transform_world_to_object",
129 vnor,
132 &wnor);
133 GPU_link(mat,
134 gpu_node_name,
135 in[0].link,
136 wnor,
137 gpu_image,
139 &col1,
140 &col2,
141 &col3);
142 GPU_link(mat, "tex_box_blend", wnor, col1, col2, col3, blend, &out[0].link, &out[1].link);
143 break;
144 }
145 case SHD_PROJ_SPHERE: {
146 /* This projection is known to have a derivative discontinuity.
147 * Hide it by turning off mipmapping. */
149 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
150 GPU_link(mat, "point_texco_remap_square", *texco, texco);
151 GPU_link(mat, "point_map_to_sphere", *texco, texco);
152 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image, GPU_kernel_globals());
153 break;
154 }
155 case SHD_PROJ_TUBE: {
156 /* This projection is known to have a derivative discontinuity.
157 * Hide it by turning off mipmapping. */
159 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
160 GPU_link(mat, "point_texco_remap_square", *texco, texco);
161 GPU_link(mat, "point_map_to_tube", *texco, texco);
162 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image, GPU_kernel_globals());
163 break;
164 }
165 }
166 }
167
168 if (out[0].hasoutput && out[0].link) {
169 if (ELEM(ima->alpha_mode, IMA_ALPHA_IGNORE, IMA_ALPHA_CHANNEL_PACKED) ||
170 IMB_colormanagement_space_name_is_data(ima->colorspace_settings.name))
171 {
172 /* Don't let alpha affect color output in these cases. */
173 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
174 }
175 else {
176 /* Output premultiplied alpha depending on alpha socket usage. This makes
177 * it so that if we blend the color with a transparent shader using alpha as
178 * a factor, we don't multiply alpha into the color twice. And if we do
179 * not, then there will be no artifacts from zero alpha areas. */
180 if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
181 if (out[1].hasoutput) {
182 GPU_link(mat, "color_alpha_unpremultiply", out[0].link, &out[0].link);
183 }
184 else {
185 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
186 }
187 }
188 else {
189 if (out[1].hasoutput) {
190 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
191 }
192 else {
193 GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
194 }
195 }
196 }
197 }
198
199 return true;
200}
201
203#ifdef WITH_MATERIALX
204{
205 /* Getting node name for Color output. This name will be used for <image> node. */
206 std::string image_node_name = node_name("Color");
207
208 NodeItem res = graph_.get_node(image_node_name);
209 if (!res.node) {
210 res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
211
212 Image *image = (Image *)node_->id;
213 if (image) {
214 NodeTexImage *tex_image = static_cast<NodeTexImage *>(node_->storage);
215
216 std::string image_path = image->id.name;
217 if (graph_.export_params.image_fn) {
218 Scene *scene = DEG_get_input_scene(graph_.depsgraph);
219 Main *bmain = DEG_get_bmain(graph_.depsgraph);
220 image_path = graph_.export_params.image_fn(bmain, scene, image, &tex_image->iuser);
221 }
222
223 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
224 if (!vector) {
225 vector = texcoord_node();
226 }
227 /* TODO: add math to vector depending of tex_image->projection */
228
229 std::string filtertype;
230 switch (tex_image->interpolation) {
232 filtertype = "linear";
233 break;
235 filtertype = "closest";
236 break;
237 case SHD_INTERP_CUBIC:
238 case SHD_INTERP_SMART:
239 filtertype = "cubic";
240 break;
241 default:
243 }
244 std::string addressmode;
245 switch (tex_image->extension) {
247 addressmode = "periodic";
248 break;
250 addressmode = "clamp";
251 break;
253 addressmode = "constant";
254 break;
256 addressmode = "mirror";
257 break;
258 default:
260 }
261
262 NodeItem::Type node_type = NodeItem::Type::Color4;
263 const char *node_colorspace = nullptr;
264
265 const char *image_colorspace = image->colorspace_settings.name;
266 if (IMB_colormanagement_space_name_is_data(image_colorspace)) {
267 node_type = NodeItem::Type::Vector4;
268 }
269 else if (IMB_colormanagement_space_name_is_scene_linear(image_colorspace)) {
270 node_colorspace = "lin_rec709";
271 }
272 else if (IMB_colormanagement_space_name_is_srgb(image_colorspace)) {
273 node_colorspace = "srgb_texture";
274 }
275
276 res = create_node("image",
277 node_type,
278 {{"texcoord", vector},
279 {"filtertype", val(filtertype)},
280 {"uaddressmode", val(addressmode)},
281 {"vaddressmode", val(addressmode)}});
282 res.set_input("file", image_path, NodeItem::Type::Filename);
283 res.node->setName(image_node_name);
284 if (node_colorspace) {
285 res.node->setAttribute("colorspace", node_colorspace);
286 }
287 }
288 }
289
290 if (STREQ(socket_out_->identifier, "Alpha")) {
291 res = res[3];
292 }
293 return res;
294}
295#endif
297
298} // namespace nodes::node_shader_tex_image_cc
299
301{
302 namespace file_ns = nodes::node_shader_tex_image_cc;
303
304 static bke::bNodeType ntype;
305
306 sh_node_type_base(&ntype, "ShaderNodeTexImage"_ustr, SH_NODE_TEX_IMAGE);
307 ntype.ui_name = "Image Texture";
308 ntype.ui_description = "Sample an image file as a texture";
309 ntype.enum_name_legacy = "TEX_IMAGE";
311 ntype.declare = file_ns::sh_node_tex_image_declare;
312 ntype.initfunc = file_ns::node_shader_init_tex_image;
315 ntype.gpu_fn = file_ns::node_shader_gpu_tex_image;
318 ntype.materialx_fn = file_ns::node_shader_materialx;
319
321}
322
323} // namespace blender
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:488
#define SH_NODE_TEX_IMAGE
#define BLI_assert_unreachable()
#define ELEM(...)
#define STREQ(a, b)
#define image(slot, qualifiers, format)
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 node_shader_init_tex_image(bNodeTree *, bNode *node)
static int node_shader_gpu_tex_image(GPUMaterial *mat, bNode *node, bNodeExecData *, GPUNodeStack *in, GPUNodeStack *out)
static void sh_node_tex_image_declare(NodeDeclarationBuilder &b)
BMesh Iterators.
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
@ IMA_ALPHA_CHANNEL_PACKED
void register_node_type_sh_tex_image()
@ SHD_IMAGE_EXTENSION_EXTEND
@ SHD_IMAGE_EXTENSION_CLIP
@ SHD_IMAGE_EXTENSION_MIRROR
@ SHD_IMAGE_EXTENSION_REPEAT
GPUNodeLink * GPU_image(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state)
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_FILTERING_LINEAR
@ GPU_SAMPLER_FILTERING_MIPMAP
void sh_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
GPUNodeLink * GPU_uniform(const float *num)
bool GPU_link(GPUMaterial *mat, const char *name,...)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
Main * DEG_get_bmain(const Depsgraph *graph)
void GPU_image_tiled(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state, GPUNodeLink **r_image_tiled_link, GPUNodeLink **r_image_tiled_mapping_link)
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:234
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
GPUNodeLink * GPU_shading_data()
Scene * DEG_get_input_scene(const Depsgraph *graph)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
GPUNodeLink * GPU_kernel_globals()
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
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:337
bool IMB_colormanagement_space_name_is_data(const char *name)
void node_image_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:146
void BKE_imageuser_default(ImageUser *iuser)
bool IMB_colormanagement_space_name_is_scene_linear(const char *name)
bool IMB_colormanagement_space_name_is_srgb(const char *name)
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void disable_filtering_flag(GPUSamplerFiltering filtering_flags)
GPUSamplerExtendMode extend_yz
GPUSamplerFiltering filtering
GPUSamplerExtendMode extend_x
static constexpr GPUSamplerState default_sampler()
static constexpr int _240
Definition BKE_node.hh:250
Defines a node type.
Definition BKE_node.hh:265
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:375
std::string ui_description
Definition BKE_node.hh:271
void(*) labelfunc(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:301
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:373
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