Blender  V2.93
node_shader_tex_image.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 #include "../node_shader_util.h"
21 
22 /* **************** OUTPUT ******************** */
23 
25  {SOCK_VECTOR, N_("Vector"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_HIDE_VALUE},
26  {-1, ""},
27 };
28 
30  {SOCK_RGBA, N_("Color"), 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_NONE, SOCK_NO_INTERNAL_LINK},
31  {SOCK_FLOAT,
32  N_("Alpha"),
33  0.0f,
34  0.0f,
35  0.0f,
36  0.0f,
37  0.0f,
38  1.0f,
39  PROP_NONE,
41  {-1, ""},
42 };
43 
45 {
46  NodeTexImage *tex = MEM_callocN(sizeof(NodeTexImage), "NodeTexImage");
48  BKE_texture_colormapping_default(&tex->base.color_mapping);
50 
51  node->storage = tex;
52 }
53 
55  bNode *node,
56  bNodeExecData *UNUSED(execdata),
57  GPUNodeStack *in,
58  GPUNodeStack *out)
59 {
60  Image *ima = (Image *)node->id;
61  NodeTexImage *tex = node->storage;
62 
63  /* We get the image user from the original node, since GPU image keeps
64  * a pointer to it and the dependency refreshes the original. */
65  bNode *node_original = node->original ? node->original : node;
66  NodeTexImage *tex_original = node_original->storage;
67  ImageUser *iuser = &tex_original->iuser;
68 
69  if (!ima) {
70  return GPU_stack_link(mat, node, "node_tex_image_empty", in, out);
71  }
72 
73  GPUNodeLink **texco = &in[0].link;
74  if (!*texco) {
75  *texco = GPU_attribute(mat, CD_MTFACE, "");
77  }
78 
79  node_shader_gpu_tex_mapping(mat, node, in, out);
80 
81  eGPUSamplerState sampler_state = 0;
82 
83  switch (tex->extension) {
85  sampler_state |= GPU_SAMPLER_REPEAT;
86  break;
88  sampler_state |= GPU_SAMPLER_CLAMP_BORDER;
89  break;
90  default:
91  break;
92  }
93 
94  if (tex->interpolation != SHD_INTERP_CLOSEST) {
95  sampler_state |= GPU_SAMPLER_ANISO | GPU_SAMPLER_FILTER;
96  /* TODO(fclem): For now assume mipmap is always enabled. */
97  sampler_state |= true ? GPU_SAMPLER_MIPMAP : 0;
98  }
99  const bool use_cubic = ELEM(tex->interpolation, SHD_INTERP_CUBIC, SHD_INTERP_SMART);
100 
101  if (ima->source == IMA_SRC_TILED) {
102  const char *gpu_node_name = use_cubic ? "node_tex_tile_cubic" : "node_tex_tile_linear";
103  GPUNodeLink *gpu_image = GPU_image_tiled(mat, ima, iuser, sampler_state);
104  GPUNodeLink *gpu_image_tile_mapping = GPU_image_tiled_mapping(mat, ima, iuser);
105  /* UDIM tiles needs a samper2DArray and sampler1DArray for tile mapping. */
106  GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image, gpu_image_tile_mapping);
107  }
108  else {
109  const char *gpu_node_name = use_cubic ? "node_tex_image_cubic" : "node_tex_image_linear";
110 
111  switch (tex->projection) {
112  case SHD_PROJ_FLAT: {
113  GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
114  GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image);
115  break;
116  }
117  case SHD_PROJ_BOX: {
118  gpu_node_name = use_cubic ? "tex_box_sample_cubic" : "tex_box_sample_linear";
119  GPUNodeLink *wnor, *col1, *col2, *col3;
122  GPUNodeLink *blend = GPU_uniform(&tex->projection_blend);
123  GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
124  /* equivalent to normal_world_to_object */
125  GPU_link(mat, "normal_transform_transposed_m4v3", vnor, ob_mat, &wnor);
126  GPU_link(mat, gpu_node_name, in[0].link, wnor, gpu_image, &col1, &col2, &col3);
127  GPU_link(mat, "tex_box_blend", wnor, col1, col2, col3, blend, &out[0].link, &out[1].link);
128  break;
129  }
130  case SHD_PROJ_SPHERE: {
131  /* This projection is known to have a derivative discontinuity.
132  * Hide it by turning off mipmapping. */
133  sampler_state &= ~GPU_SAMPLER_MIPMAP;
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_sphere", *texco, texco);
137  GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image);
138  break;
139  }
140  case SHD_PROJ_TUBE: {
141  /* This projection is known to have a derivative discontinuity.
142  * Hide it by turning off mipmapping. */
143  sampler_state &= ~GPU_SAMPLER_MIPMAP;
144  GPUNodeLink *gpu_image = GPU_image(mat, ima, iuser, sampler_state);
145  GPU_link(mat, "point_texco_remap_square", *texco, texco);
146  GPU_link(mat, "point_map_to_tube", *texco, texco);
147  GPU_stack_link(mat, node, gpu_node_name, in, out, gpu_image);
148  break;
149  }
150  }
151  }
152 
153  if (out[0].hasoutput) {
156  /* Don't let alpha affect color output in these cases. */
157  GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
158  }
159  else {
160  /* Output premultiplied alpha depending on alpha socket usage. This makes
161  * it so that if we blend the color with a transparent shader using alpha as
162  * a factor, we don't multiply alpha into the color twice. And if we do
163  * not, then there will be no artifacts from zero alpha areas. */
164  if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
165  if (out[1].hasoutput) {
166  GPU_link(mat, "color_alpha_unpremultiply", out[0].link, &out[0].link);
167  }
168  else {
169  GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
170  }
171  }
172  else {
173  if (out[1].hasoutput) {
174  GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
175  }
176  else {
177  GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
178  }
179  }
180  }
181  }
182 
183  return true;
184 }
185 
186 /* node type definition */
188 {
189  static bNodeType ntype;
190 
191  sh_node_type_base(&ntype, SH_NODE_TEX_IMAGE, "Image Texture", NODE_CLASS_TEXTURE, 0);
195  &ntype, "NodeTexImage", node_free_standard_storage, node_copy_standard_storage);
199 
200  nodeRegisterType(&ntype);
201 }
void BKE_imageuser_default(struct ImageUser *iuser)
Definition: image.c:3451
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4645
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4577
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
#define NODE_CLASS_TEXTURE
Definition: BKE_node.h:346
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
void node_type_label(struct bNodeType *ntype, void(*labelfunc)(struct bNodeTree *ntree, struct bNode *, char *label, int maxlen))
@ NODE_SIZE_LARGE
Definition: BKE_node.h:373
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition: texture.c:252
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition: texture.c:354
#define UNUSED(x)
#define ELEM(...)
#define N_(msgid)
@ IMA_ALPHA_IGNORE
@ IMA_ALPHA_PREMUL
@ IMA_ALPHA_CHANNEL_PACKED
@ IMA_SRC_TILED
#define SHD_PROJ_SPHERE
#define SHD_PROJ_BOX
#define SHD_IMAGE_EXTENSION_CLIP
#define SHD_INTERP_SMART
#define SHD_IMAGE_EXTENSION_REPEAT
#define SHD_PROJ_FLAT
@ SOCK_HIDE_VALUE
@ SOCK_NO_INTERNAL_LINK
@ SOCK_VECTOR
@ SOCK_FLOAT
@ SOCK_RGBA
#define SHD_PROJ_TUBE
#define SHD_INTERP_CLOSEST
#define SHD_INTERP_CUBIC
#define TEXMAP_TYPE_POINT
GPUNodeLink * GPU_builtin(eGPUBuiltin builtin)
GPUNodeLink * GPU_image(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser, eGPUSamplerState sampler_state)
GPUNodeLink * GPU_image_tiled_mapping(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser)
GPUNodeLink * GPU_attribute(GPUMaterial *mat, CustomDataType type, const char *name)
@ GPU_WORLD_NORMAL
Definition: GPU_material.h:105
@ GPU_OBJECT_MATRIX
Definition: GPU_material.h:88
GPUNodeLink * GPU_uniform(const float *num)
GPUNodeLink * GPU_image_tiled(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser, eGPUSamplerState sampler_state)
bool GPU_link(GPUMaterial *mat, const char *name,...)
bool GPU_stack_link(GPUMaterial *mat, struct bNode *node, const char *name, GPUNodeStack *in, GPUNodeStack *out,...)
eGPUSamplerState
Definition: GPU_texture.h:40
@ GPU_SAMPLER_ANISO
Definition: GPU_texture.h:49
@ GPU_SAMPLER_REPEAT
Definition: GPU_texture.h:52
@ GPU_SAMPLER_MIPMAP
Definition: GPU_texture.h:43
@ GPU_SAMPLER_FILTER
Definition: GPU_texture.h:42
@ GPU_SAMPLER_CLAMP_BORDER
Definition: GPU_texture.h:47
bool IMB_colormanagement_space_name_is_data(const char *name)
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume SH_NODE_TEX_IMAGE
@ PROP_NONE
Definition: RNA_types.h:113
ColorManagedColorspaceSettings colorspace_settings
short source
char alpha_mode
OperationNode * node
bNodeTree * ntree
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void node_shader_init_tex_image(bNodeTree *UNUSED(ntree), bNode *node)
static bNodeSocketTemplate sh_node_tex_image_out[]
void register_node_type_sh_tex_image(void)
static bNodeSocketTemplate sh_node_tex_image_in[]
static int node_shader_gpu_tex_image(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *UNUSED(out))
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *node, GPUNodeLink **link)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
void node_image_label(bNodeTree *UNUSED(ntree), bNode *node, char *label, int maxlen)
Definition: node_util.c:199
struct GPUNodeLink * link
Definition: GPU_material.h:119
struct ImageUser iuser
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
static int blend(const Tex *tex, const float texvec[3], TexResult *texres)