Blender V4.5
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
5#include "node_shader_util.hh"
6#include "node_util.hh"
7
8#include "BKE_image.hh"
9#include "BKE_node_runtime.hh"
10#include "BKE_texture.h"
11
13
15
17
19{
20 b.is_function_node();
21 b.add_input<decl::Vector>("Vector").implicit_field(NODE_DEFAULT_INPUT_POSITION_FIELD);
22 b.add_output<decl::Color>("Color").no_muted_links();
23 b.add_output<decl::Float>("Alpha").no_muted_links();
24}
25
35
37 bNode *node,
38 bNodeExecData * /*execdata*/,
41{
42 Image *ima = (Image *)node->id;
43 NodeTexImage *tex = (NodeTexImage *)node->storage;
44
45 /* We get the image user from the original node, since GPU image keeps
46 * a pointer to it and the dependency refreshes the original. */
47 bNode *node_original = node->runtime->original ? node->runtime->original : node;
48 NodeTexImage *tex_original = (NodeTexImage *)node_original->storage;
49 ImageUser *iuser = &tex_original->iuser;
50
51 if (!ima) {
52 return GPU_stack_link(mat, node, "node_tex_image_empty", in, out);
53 }
54
55 GPUNodeLink **texco = &in[0].link;
56 if (!*texco) {
57 *texco = GPU_attribute(mat, CD_AUTO_FROM_NAME, "");
58 node_shader_gpu_bump_tex_coord(mat, node, texco);
59 }
60
62
64
65 switch (tex->extension) {
69 break;
73 break;
77 break;
81 break;
82 default:
83 break;
84 }
85
87 /* TODO(fclem): For now assume mipmap is always enabled. */
90 }
91 const bool use_cubic = ELEM(tex->interpolation, SHD_INTERP_CUBIC, SHD_INTERP_SMART);
92
93 if (ima->source == IMA_SRC_TILED) {
94 const char *gpu_node_name = use_cubic ? "node_tex_tile_cubic" : "node_tex_tile_linear";
95 GPUNodeLink *gpu_image, *gpu_image_tile_mapping;
96 GPU_image_tiled(mat, ima, iuser, sampler_state, &gpu_image, &gpu_image_tile_mapping);
97 /* UDIM tiles needs a `sampler2DArray` and `sampler1DArray` for tile mapping. */
98 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image, gpu_image_tile_mapping);
99 }
100 else {
101 const char *gpu_node_name = use_cubic ? "node_tex_image_cubic" : "node_tex_image_linear";
102
103 switch (tex->projection) {
104 case SHD_PROJ_FLAT: {
105 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
106 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image);
107 break;
108 }
109 case SHD_PROJ_BOX: {
110 gpu_node_name = use_cubic ? "tex_box_sample_cubic" : "tex_box_sample_linear";
111 GPUNodeLink *vnor, *wnor, *col1, *col2, *col3;
113 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
114 GPU_link(mat, "world_normals_get", &vnor);
115 GPU_link(mat, "normal_transform_world_to_object", vnor, &wnor);
116 GPU_link(mat, gpu_node_name, in[0].link, wnor, gpu_image, &col1, &col2, &col3);
117 GPU_link(mat, "tex_box_blend", wnor, col1, col2, col3, blend, &out[0].link, &out[1].link);
118 break;
119 }
120 case SHD_PROJ_SPHERE: {
121 /* This projection is known to have a derivative discontinuity.
122 * Hide it by turning off mipmapping. */
124 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
125 GPU_link(mat, "point_texco_remap_square", *texco, texco);
126 GPU_link(mat, "point_map_to_sphere", *texco, texco);
127 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image);
128 break;
129 }
130 case SHD_PROJ_TUBE: {
131 /* This projection is known to have a derivative discontinuity.
132 * Hide it by turning off mipmapping. */
134 GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
135 GPU_link(mat, "point_texco_remap_square", *texco, texco);
136 GPU_link(mat, "point_map_to_tube", *texco, texco);
137 GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image);
138 break;
139 }
140 }
141 }
142
143 if (out[0].hasoutput) {
146 {
147 /* Don't let alpha affect color output in these cases. */
148 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
149 }
150 else {
151 /* Output premultiplied alpha depending on alpha socket usage. This makes
152 * it so that if we blend the color with a transparent shader using alpha as
153 * a factor, we don't multiply alpha into the color twice. And if we do
154 * not, then there will be no artifacts from zero alpha areas. */
155 if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
156 if (out[1].hasoutput) {
157 GPU_link(mat, "color_alpha_unpremultiply", out[0].link, &out[0].link);
158 }
159 else {
160 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
161 }
162 }
163 else {
164 if (out[1].hasoutput) {
165 GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
166 }
167 else {
168 GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
169 }
170 }
171 }
172 }
173
174 return true;
175}
176
178#ifdef WITH_MATERIALX
179{
180 /* Getting node name for Color output. This name will be used for <image> node. */
181 std::string image_node_name = node_name("Color");
182
183 NodeItem res = graph_.get_node(image_node_name);
184 if (!res.node) {
185 res = val(MaterialX::Color4(1.0f, 0.0f, 1.0f, 1.0f));
186
187 Image *image = (Image *)node_->id;
188 if (image) {
189 NodeTexImage *tex_image = static_cast<NodeTexImage *>(node_->storage);
190
191 std::string image_path = image->id.name;
192 if (graph_.export_params.image_fn) {
193 Scene *scene = DEG_get_input_scene(graph_.depsgraph);
194 Main *bmain = DEG_get_bmain(graph_.depsgraph);
195 image_path = graph_.export_params.image_fn(bmain, scene, image, &tex_image->iuser);
196 }
197
198 NodeItem vector = get_input_link("Vector", NodeItem::Type::Vector2);
199 if (!vector) {
200 vector = texcoord_node();
201 }
202 /* TODO: add math to vector depending of tex_image->projection */
203
204 std::string filtertype;
205 switch (tex_image->interpolation) {
207 filtertype = "linear";
208 break;
210 filtertype = "closest";
211 break;
212 case SHD_INTERP_CUBIC:
213 case SHD_INTERP_SMART:
214 filtertype = "cubic";
215 break;
216 default:
218 }
219 std::string addressmode;
220 switch (tex_image->extension) {
222 addressmode = "periodic";
223 break;
225 addressmode = "clamp";
226 break;
228 addressmode = "constant";
229 break;
231 addressmode = "mirror";
232 break;
233 default:
235 }
236
237 NodeItem::Type node_type = NodeItem::Type::Color4;
238 const char *node_colorspace = nullptr;
239
240 const char *image_colorspace = image->colorspace_settings.name;
241 if (IMB_colormanagement_space_name_is_data(image_colorspace)) {
242 node_type = NodeItem::Type::Vector4;
243 }
244 else if (IMB_colormanagement_space_name_is_scene_linear(image_colorspace)) {
245 node_colorspace = "lin_rec709";
246 }
247 else if (IMB_colormanagement_space_name_is_srgb(image_colorspace)) {
248 node_colorspace = "srgb_texture";
249 }
250
251 res = create_node("image",
252 node_type,
253 {{"texcoord", vector},
254 {"filtertype", val(filtertype)},
255 {"uaddressmode", val(addressmode)},
256 {"vaddressmode", val(addressmode)}});
257 res.set_input("file", image_path, NodeItem::Type::Filename);
258 res.node->setName(image_node_name);
259 if (node_colorspace) {
260 res.node->setAttribute("colorspace", node_colorspace);
261 }
262 }
263 }
264
265 if (STREQ(socket_out_->name, "Alpha")) {
266 res = res[3];
267 }
268 return res;
269}
270#endif
272
273} // namespace blender::nodes::node_shader_tex_image_cc
274
276{
278
279 static blender::bke::bNodeType ntype;
280
281 sh_node_type_base(&ntype, "ShaderNodeTexImage", SH_NODE_TEX_IMAGE);
282 ntype.ui_name = "Image Texture";
283 ntype.ui_description = "Sample an image file as a texture";
284 ntype.enum_name_legacy = "TEX_IMAGE";
286 ntype.declare = file_ns::sh_node_tex_image_declare;
287 ntype.initfunc = file_ns::node_shader_init_tex_image;
290 ntype.gpu_fn = file_ns::node_shader_gpu_tex_image;
293 ntype.materialx_fn = file_ns::node_shader_materialx;
294
296}
void BKE_imageuser_default(ImageUser *iuser)
#define NODE_CLASS_TEXTURE
Definition BKE_node.hh:443
#define SH_NODE_TEX_IMAGE
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition texture.cc:238
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition texture.cc:341
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define ELEM(...)
#define STREQ(a, b)
Main * DEG_get_bmain(const Depsgraph *graph)
Scene * DEG_get_input_scene(const Depsgraph *graph)
@ CD_AUTO_FROM_NAME
@ IMA_ALPHA_IGNORE
@ IMA_ALPHA_PREMUL
@ IMA_ALPHA_CHANNEL_PACKED
@ IMA_SRC_TILED
@ NODE_DEFAULT_INPUT_POSITION_FIELD
@ SHD_INTERP_LINEAR
@ SHD_INTERP_SMART
@ SHD_INTERP_CUBIC
@ SHD_INTERP_CLOSEST
@ SHD_PROJ_TUBE
@ SHD_PROJ_SPHERE
@ SHD_PROJ_BOX
@ SHD_PROJ_FLAT
@ SHD_IMAGE_EXTENSION_MIRROR
@ SHD_IMAGE_EXTENSION_CLIP
@ SHD_IMAGE_EXTENSION_REPEAT
@ SHD_IMAGE_EXTENSION_EXTEND
@ TEXMAP_TYPE_POINT
bool GPU_stack_link(GPUMaterial *mat, const bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
GPUNodeLink * GPU_image(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state)
void GPU_image_tiled(GPUMaterial *mat, Image *ima, ImageUser *iuser, GPUSamplerState sampler_state, GPUNodeLink **r_image_tiled_link, GPUNodeLink **r_image_tiled_mapping_link)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, eCustomDataType type, const char *name)
bool GPU_link(GPUMaterial *mat, const char *name,...)
GPUNodeLink * GPU_uniform(const float *num)
@ GPU_SAMPLER_EXTEND_MODE_MIRRORED_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_REPEAT
@ GPU_SAMPLER_EXTEND_MODE_EXTEND
@ GPU_SAMPLER_EXTEND_MODE_CLAMP_TO_BORDER
@ GPU_SAMPLER_FILTERING_MIPMAP
@ GPU_SAMPLER_FILTERING_ANISOTROPIC
@ GPU_SAMPLER_FILTERING_LINEAR
bool IMB_colormanagement_space_name_is_srgb(const char *name)
bool IMB_colormanagement_space_name_is_scene_linear(const char *name)
bool IMB_colormanagement_space_name_is_data(const char *name)
#define in
#define out
void * MEM_callocN(size_t len, const char *str)
Definition mallocn.cc:118
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
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:5603
void node_type_size_preset(bNodeType &ntype, eNodeSizePreset size)
Definition node.cc:5585
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)
std::vector< ElementType, Eigen::aligned_allocator< ElementType > > vector
#define NODE_SHADER_MATERIALX_BEGIN
#define NODE_SHADER_MATERIALX_END
void register_node_type_sh_tex_image()
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *)
void sh_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *, GPUNodeLink **link)
void node_free_standard_storage(bNode *node)
Definition node_util.cc:42
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:54
void node_image_label(const bNodeTree *, const bNode *node, char *label, int label_maxncpy)
Definition node_util.cc:189
GPUSamplerExtendMode extend_yz
static constexpr GPUSamplerState default_sampler()
GPUSamplerFiltering filtering
void disable_filtering_flag(GPUSamplerFiltering filtering_flags)
GPUSamplerExtendMode extend_x
ColorManagedColorspaceSettings colorspace_settings
short source
char alpha_mode
TexMapping tex_mapping
ColorMapping color_mapping
NodeTexBase base
struct ID * id
bNodeRuntimeHandle * runtime
void * storage
Defines a node type.
Definition BKE_node.hh:226
NodeMaterialXFunction materialx_fn
Definition BKE_node.hh:332
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
void(* labelfunc)(const bNodeTree *ntree, const bNode *node, char *label, int label_maxncpy)
Definition BKE_node.hh:258
NodeGPUExecFunction gpu_fn
Definition BKE_node.hh:330
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355
static int blend(const Tex *tex, const float texvec[3], TexResult *texres)