Blender  V3.3
node_shader_tex_environment.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 
7 
9 {
10  b.add_input<decl::Vector>(N_("Vector")).hide_value();
11  b.add_output<decl::Color>(N_("Color")).no_muted_links();
12 }
13 
15 {
16  NodeTexEnvironment *tex = MEM_cnew<NodeTexEnvironment>("NodeTexEnvironment");
18  BKE_texture_colormapping_default(&tex->base.color_mapping);
19  tex->projection = SHD_PROJ_EQUIRECTANGULAR;
21 
22  node->storage = tex;
23 }
24 
26  bNode *node,
27  bNodeExecData *UNUSED(execdata),
28  GPUNodeStack *in,
30 {
31  Image *ima = (Image *)node->id;
33 
34  /* We get the image user from the original node, since GPU image keeps
35  * a pointer to it and the dependency refreshes the original. */
36  bNode *node_original = node->original ? node->original : node;
37  NodeTexImage *tex_original = (NodeTexImage *)node_original->storage;
38  ImageUser *iuser = &tex_original->iuser;
40  /* TODO(@fclem): For now assume mipmap is always enabled. */
41  if (true) {
43  }
44 
45  GPUNodeLink *outalpha;
46 
47  /* HACK(@fclem): For lookdev mode: do not compile an empty environment and just create an empty
48  * texture entry point. We manually bind to it after #DRW_shgroup_add_material_resources(). */
50  return GPU_stack_link(mat, node, "node_tex_environment_empty", in, out);
51  }
52 
53  if (!in[0].link) {
54  GPU_link(mat, "node_tex_coord_position", &in[0].link);
55  node_shader_gpu_bump_tex_coord(mat, node, &in[0].link);
56  }
57 
59 
60  /* Compute texture coordinate. */
61  if (tex->projection == SHD_PROJ_EQUIRECTANGULAR) {
62  GPU_link(mat, "node_tex_environment_equirectangular", in[0].link, &in[0].link);
63  /* To fix pole issue we clamp the v coordinate. */
65  /* Force the highest mipmap and don't do anisotropic filtering.
66  * This is to fix the artifact caused by derivatives discontinuity. */
68  }
69  else {
70  GPU_link(mat, "node_tex_environment_mirror_ball", in[0].link, &in[0].link);
71  /* Fix pole issue. */
73  }
74 
75  const char *gpu_fn;
76  static const char *names[] = {
77  "node_tex_image_linear",
78  "node_tex_image_cubic",
79  };
80 
81  switch (tex->interpolation) {
82  case SHD_INTERP_LINEAR:
83  gpu_fn = names[0];
84  break;
85  case SHD_INTERP_CLOSEST:
87  gpu_fn = names[0];
88  break;
89  default:
90  gpu_fn = names[1];
91  break;
92  }
93 
94  /* Sample texture with correct interpolation. */
95  GPU_link(mat, gpu_fn, in[0].link, GPU_image(mat, ima, iuser, sampler), &out[0].link, &outalpha);
96 
97  if (out[0].hasoutput && ima) {
100  /* Don't let alpha affect color output in these cases. */
101  GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
102  }
103  else {
104  /* Always output with premultiplied alpha. */
105  if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
106  GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
107  }
108  else {
109  GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
110  }
111  }
112  }
113 
114  return true;
115 }
116 
117 } // namespace blender::nodes::node_shader_tex_environment_cc
118 
119 /* node type definition */
121 {
123 
124  static bNodeType ntype;
125 
126  sh_node_type_base(&ntype, SH_NODE_TEX_ENVIRONMENT, "Environment Texture", NODE_CLASS_TEXTURE);
130  &ntype, "NodeTexEnvironment", node_free_standard_storage, node_copy_standard_storage);
132  ntype.labelfunc = node_image_label;
134 
135  nodeRegisterType(&ntype);
136 }
void BKE_imageuser_default(struct ImageUser *iuser)
void node_type_gpu(struct bNodeType *ntype, NodeGPUExecFunction gpu_fn)
Definition: node.cc:4465
#define SH_NODE_TEX_ENVIRONMENT
Definition: BKE_node.h:1128
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4390
void node_type_size_preset(struct bNodeType *ntype, eNodeSizePreset size)
Definition: node.cc:4408
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:4426
#define NODE_CLASS_TEXTURE
Definition: BKE_node.h:355
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
@ NODE_SIZE_LARGE
Definition: BKE_node.h:367
void BKE_texture_mapping_default(struct TexMapping *texmap, int type)
Definition: texture.c:238
void BKE_texture_colormapping_default(struct ColorMapping *colormap)
Definition: texture.c:340
#define UNUSED(x)
#define ELEM(...)
@ IMA_ALPHA_IGNORE
@ IMA_ALPHA_PREMUL
@ IMA_ALPHA_CHANNEL_PACKED
#define SHD_PROJ_EQUIRECTANGULAR
#define SHD_INTERP_CLOSEST
#define SHD_INTERP_LINEAR
#define TEXMAP_TYPE_POINT
GPUNodeLink * GPU_image(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser, eGPUSamplerState sampler_state)
bool GPU_material_flag_get(const GPUMaterial *mat, eGPUMaterialFlag flag)
Definition: gpu_material.c:601
@ GPU_MATFLAG_LOOKDEV_HACK
Definition: GPU_material.h:98
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:25
@ GPU_SAMPLER_ANISO
Definition: GPU_texture.h:34
@ GPU_SAMPLER_REPEAT
Definition: GPU_texture.h:37
@ GPU_SAMPLER_MIPMAP
Definition: GPU_texture.h:28
@ GPU_SAMPLER_FILTER
Definition: GPU_texture.h:27
@ GPU_SAMPLER_REPEAT_T
Definition: GPU_texture.h:30
bool IMB_colormanagement_space_name_is_data(const char *name)
ColorManagedColorspaceSettings colorspace_settings
char alpha_mode
OperationNode * node
depth_tx sampler(1, ImageType::FLOAT_2D, "combined_tx") .sampler(2
bNodeTree * ntree
static char ** names
Definition: makesdna.c:65
static int node_shader_gpu_tex_environment(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
static void node_declare(NodeDeclarationBuilder &b)
static void node_shader_init_tex_environment(bNodeTree *UNUSED(ntree), bNode *node)
static const pxr::TfToken out("out", pxr::TfToken::Immortal)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_sh_tex_environment()
void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *UNUSED(out))
void node_shader_gpu_bump_tex_coord(GPUMaterial *mat, bNode *UNUSED(node), GPUNodeLink **link)
void sh_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:55
void node_free_standard_storage(bNode *node)
Definition: node_util.c:43
void node_image_label(const bNodeTree *UNUSED(ntree), const bNode *node, char *label, int maxlen)
Definition: node_util.c:189
ImageUser iuser
struct ImageUser iuser
Defines a node type.
Definition: BKE_node.h:226
void(* labelfunc)(const struct bNodeTree *ntree, const struct bNode *node, char *label, int maxlen)
Definition: BKE_node.h:256
NodeDeclareFunction declare
Definition: BKE_node.h:324
void * storage
#define N_(msgid)