Blender  V2.93
node_texture_util.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 
24 /*
25  * HOW TEXTURE NODES WORK
26  *
27  * In contrast to Shader nodes, which place a color into the output
28  * stack when executed, Texture nodes place a TexDelegate* there. To
29  * obtain a color value from this, a node further up the chain reads
30  * the TexDelegate* from its input stack, and uses tex_call_delegate to
31  * retrieve the color from the delegate.
32  *
33  * comments: (ton)
34  *
35  * This system needs recode, a node system should rely on the stack, and
36  * callbacks for nodes only should evaluate own node, not recursively go
37  * over other previous ones.
38  */
39 
40 #include "node_texture_util.h"
41 
44  const char **r_disabled_hint)
45 {
46  if (!STREQ(ntree->idname, "TextureNodeTree")) {
47  *r_disabled_hint = "Not a texture node tree";
48  return false;
49  }
50  return true;
51 }
52 
54  struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
55 {
56  node_type_base(ntype, type, name, nclass, flag);
57 
58  ntype->poll = tex_node_poll_default;
61 }
62 
63 static void tex_call_delegate(TexDelegate *dg, float *out, TexParams *params, short thread)
64 {
65  if (dg->node->need_exec) {
66  dg->fn(out, params, dg->node, dg->in, thread);
67 
68  if (dg->cdata->do_preview) {
69  tex_do_preview(dg->preview, params->previewco, out, dg->cdata->do_manage);
70  }
71  }
72 }
73 
74 static void tex_input(float *out, int sz, bNodeStack *in, TexParams *params, short thread)
75 {
76  TexDelegate *dg = in->data;
77  if (dg) {
79 
80  if (in->hasoutput && in->sockettype == SOCK_FLOAT) {
81  in->vec[1] = in->vec[2] = in->vec[0];
82  }
83  }
84  memcpy(out, in->vec, sz * sizeof(float));
85 }
86 
87 void tex_input_vec(float *out, bNodeStack *in, TexParams *params, short thread)
88 {
89  tex_input(out, 3, in, params, thread);
90 }
91 
92 void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
93 {
94  tex_input(out, 4, in, params, thread);
95 
96  if (in->hasoutput && in->sockettype == SOCK_FLOAT) {
97  out[1] = out[2] = out[0];
98  out[3] = 1;
99  }
100 
101  if (in->hasoutput && in->sockettype == SOCK_VECTOR) {
102  out[0] = out[0] * 0.5f + 0.5f;
103  out[1] = out[1] * 0.5f + 0.5f;
104  out[2] = out[2] * 0.5f + 0.5f;
105  out[3] = 1;
106  }
107 }
108 
110 {
111  float out[4];
112  tex_input_vec(out, in, params, thread);
113  return out[0];
114 }
115 
117 {
118  out->co = in->co;
119  out->dxt = in->dxt;
120  out->dyt = in->dyt;
121  out->previewco = in->co;
122  out->osatex = in->osatex;
123  out->cfra = in->cfra;
124  out->mtex = in->mtex;
125 }
126 
128  const float coord[2],
129  const float col[4],
130  bool do_manage)
131 {
132  if (preview) {
133  int xs = ((coord[0] + 1.0f) * 0.5f) * preview->xsize;
134  int ys = ((coord[1] + 1.0f) * 0.5f) * preview->ysize;
135 
136  BKE_node_preview_set_pixel(preview, col, xs, ys, do_manage);
137  }
138 }
139 
141  bNodeExecData *execdata,
142  bNodeStack **in,
143  bNodeStack *out,
144  TexFn texfn,
145  TexCallData *cdata)
146 {
147  TexDelegate *dg;
148 
149  if (node->flag & NODE_MUTED) {
150  /* do not add a delegate if the node is muted */
151  return;
152  }
153 
154  if (!out->data) {
155  /* Freed in tex_end_exec (node.cc) */
156  dg = out->data = MEM_mallocN(sizeof(TexDelegate), "tex delegate");
157  }
158  else {
159  dg = out->data;
160  }
161 
162  dg->cdata = cdata;
163  dg->fn = texfn;
164  dg->node = node;
165  dg->preview = execdata->preview;
166  memcpy(dg->in, in, MAX_SOCKET * sizeof(bNodeStack *));
167  dg->type = out->sockettype;
168 }
169 
171 {
172  bNode *node;
173  for (node = ntree->nodes.first; node; node = node->next) {
174 
175  if (node->type == TEX_NODE_TEXTURE && node->id) {
176  /* custom2 stops the node from rendering */
177  if (node->custom1) {
178  node->custom2 = 1;
179  node->custom1 = 0;
180  }
181  else {
182  Tex *tex = (Tex *)node->id;
183 
184  node->custom2 = 0;
185 
186  node->custom1 = 1;
187  if (tex->use_nodes && tex->nodetree) {
189  }
190  node->custom1 = 0;
191  }
192  }
193  }
194 }
void node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
Definition: node.cc:4443
void BKE_node_preview_set_pixel(struct bNodePreview *preview, const float col[4], int x, int y, bool do_manage)
Definition: node.cc:2809
#define TEX_NODE_TEXTURE
Definition: BKE_node.h:1327
#define MAX_SOCKET
Definition: BKE_node.h:41
#define UNUSED(x)
#define STREQ(a, b)
#define NODE_MUTED
@ SOCK_VECTOR
@ SOCK_FLOAT
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
OperationNode * node
bNodeTree * ntree
uint col
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
static void texfn(float *result, TexParams *p, bNode *node, bNodeStack **in, char is_normal, MapFn map_inputs, short thread)
void tex_do_preview(bNodePreview *preview, const float coord[2], const float col[4], bool do_manage)
void tex_input_vec(float *out, bNodeStack *in, TexParams *params, short thread)
void tex_node_type_base(struct bNodeType *ntype, int type, const char *name, short nclass, short flag)
float tex_input_value(bNodeStack *in, TexParams *params, short thread)
void tex_input_rgba(float *out, bNodeStack *in, TexParams *params, short thread)
static void tex_call_delegate(TexDelegate *dg, float *out, TexParams *params, short thread)
bool tex_node_poll_default(bNodeType *UNUSED(ntype), bNodeTree *ntree, const char **r_disabled_hint)
void tex_output(bNode *node, bNodeExecData *execdata, bNodeStack **in, bNodeStack *out, TexFn texfn, TexCallData *cdata)
static void tex_input(float *out, int sz, bNodeStack *in, TexParams *params, short thread)
void ntreeTexCheckCyclics(struct bNodeTree *ntree)
void params_from_cdata(TexParams *out, TexCallData *in)
void(* TexFn)(float *out, TexParams *params, bNode *node, bNodeStack **in, short thread)
void node_insert_link_default(bNodeTree *ntree, bNode *node, bNodeLink *link)
Definition: node_util.c:305
void node_update_internal_links_default(bNodeTree *ntree, bNode *node)
Definition: node_util.c:503
void * first
Definition: DNA_listBase.h:47
const float * co
bNodePreview * preview
bNodeStack * in[MAX_SOCKET]
TexCallData * cdata
const float * co
const float * previewco
char use_nodes
struct bNodeTree * nodetree
struct bNodePreview * preview
Definition: node_util.h:54
short hasoutput
short sockettype
float vec[4]
char idname[64]
ListBase nodes
Defines a node type.
Definition: BKE_node.h:221
bool(* poll)(struct bNodeType *ntype, struct bNodeTree *nodetree, const char **r_disabled_hint)
Definition: BKE_node.h:301
void(* update_internal_links)(struct bNodeTree *, struct bNode *node)
Definition: BKE_node.h:312
void(* insert_link)(struct bNodeTree *ntree, struct bNode *node, struct bNodeLink *link)
Definition: BKE_node.h:310
uint8_t need_exec