Blender  V2.93
node_shader_tex_environment.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  {-1, ""},
32 };
33 
35 {
36  NodeTexEnvironment *tex = MEM_callocN(sizeof(NodeTexEnvironment), "NodeTexEnvironment");
38  BKE_texture_colormapping_default(&tex->base.color_mapping);
39  tex->projection = SHD_PROJ_EQUIRECTANGULAR;
41 
42  node->storage = tex;
43 }
44 
46  bNode *node,
47  bNodeExecData *UNUSED(execdata),
48  GPUNodeStack *in,
49  GPUNodeStack *out)
50 {
51  Image *ima = (Image *)node->id;
52  NodeTexEnvironment *tex = node->storage;
53 
54  /* We get the image user from the original node, since GPU image keeps
55  * a pointer to it and the dependency refreshes the original. */
56  bNode *node_original = node->original ? node->original : node;
57  NodeTexImage *tex_original = node_original->storage;
58  ImageUser *iuser = &tex_original->iuser;
60  /* TODO(fclem): For now assume mipmap is always enabled. */
61  if (true) {
62  sampler |= GPU_SAMPLER_MIPMAP;
63  }
64 
65  GPUNodeLink *outalpha;
66 
67  if (!ima) {
68  return GPU_stack_link(mat, node, "node_tex_environment_empty", in, out);
69  }
70 
71  if (!in[0].link) {
72  GPU_link(mat, "node_tex_environment_texco", GPU_builtin(GPU_VIEW_POSITION), &in[0].link);
73  node_shader_gpu_bump_tex_coord(mat, node, &in[0].link);
74  }
75 
76  node_shader_gpu_tex_mapping(mat, node, in, out);
77 
78  /* Compute texture coordinate. */
79  if (tex->projection == SHD_PROJ_EQUIRECTANGULAR) {
80  GPU_link(mat, "node_tex_environment_equirectangular", in[0].link, &in[0].link);
81  /* To fix pole issue we clamp the v coordinate. */
82  sampler &= ~GPU_SAMPLER_REPEAT_T;
83  /* Force the highest mipmap and don't do anisotropic filtering.
84  * This is to fix the artifact caused by derivatives discontinuity. */
85  sampler &= ~(GPU_SAMPLER_MIPMAP | GPU_SAMPLER_ANISO);
86  }
87  else {
88  GPU_link(mat, "node_tex_environment_mirror_ball", in[0].link, &in[0].link);
89  /* Fix pole issue. */
90  sampler &= ~GPU_SAMPLER_REPEAT;
91  }
92 
93  const char *gpu_fn;
94  static const char *names[] = {
95  "node_tex_image_linear",
96  "node_tex_image_cubic",
97  };
98 
99  switch (tex->interpolation) {
100  case SHD_INTERP_LINEAR:
101  gpu_fn = names[0];
102  break;
103  case SHD_INTERP_CLOSEST:
104  sampler &= ~(GPU_SAMPLER_FILTER | GPU_SAMPLER_MIPMAP);
105  gpu_fn = names[0];
106  break;
107  default:
108  gpu_fn = names[1];
109  break;
110  }
111 
112  /* Sample texture with correct interpolation. */
113  GPU_link(mat, gpu_fn, in[0].link, GPU_image(mat, ima, iuser, sampler), &out[0].link, &outalpha);
114 
115  if (out[0].hasoutput) {
118  /* Don't let alpha affect color output in these cases. */
119  GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
120  }
121  else {
122  /* Always output with premultiplied alpha. */
123  if (ima->alpha_mode == IMA_ALPHA_PREMUL) {
124  GPU_link(mat, "color_alpha_clear", out[0].link, &out[0].link);
125  }
126  else {
127  GPU_link(mat, "color_alpha_premultiply", out[0].link, &out[0].link);
128  }
129  }
130  }
131 
132  return true;
133 }
134 
135 /* node type definition */
137 {
138  static bNodeType ntype;
139 
140  sh_node_type_base(&ntype, SH_NODE_TEX_ENVIRONMENT, "Environment Texture", NODE_CLASS_TEXTURE, 0);
144  &ntype, "NodeTexEnvironment", node_free_standard_storage, node_copy_standard_storage);
148 
149  nodeRegisterType(&ntype);
150 }
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
#define SH_NODE_TEX_ENVIRONMENT
Definition: BKE_node.h:1022
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
#define SHD_PROJ_EQUIRECTANGULAR
@ SOCK_HIDE_VALUE
@ SOCK_NO_INTERNAL_LINK
@ SOCK_VECTOR
@ SOCK_RGBA
#define SHD_INTERP_CLOSEST
#define SHD_INTERP_LINEAR
#define TEXMAP_TYPE_POINT
GPUNodeLink * GPU_builtin(eGPUBuiltin builtin)
GPUNodeLink * GPU_image(GPUMaterial *mat, struct Image *ima, struct ImageUser *iuser, eGPUSamplerState sampler_state)
@ GPU_VIEW_POSITION
Definition: GPU_material.h:91
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_REPEAT_T
Definition: GPU_texture.h:45
bool IMB_colormanagement_space_name_is_data(const char *name)
@ PROP_NONE
Definition: RNA_types.h:113
ColorManagedColorspaceSettings colorspace_settings
char alpha_mode
OperationNode * node
bNodeTree * ntree
static char ** names
Definition: makesdna.c:162
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static int node_shader_gpu_tex_environment(GPUMaterial *mat, bNode *node, bNodeExecData *UNUSED(execdata), GPUNodeStack *in, GPUNodeStack *out)
void register_node_type_sh_tex_environment(void)
static void node_shader_init_tex_environment(bNodeTree *UNUSED(ntree), bNode *node)
static bNodeSocketTemplate sh_node_tex_environment_in[]
static bNodeSocketTemplate sh_node_tex_environment_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 ImageUser iuser
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221