2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2005 Blender Foundation.
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/nodes/shader/node_shader_util.c
33 #include "DNA_node_types.h"
35 #include "node_shader_util.h"
37 #include "node_exec.h"
41 void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
45 if(type_in==SOCK_FLOAT) {
46 if(ns->sockettype==SOCK_FLOAT)
49 *in= 0.333333f*(from[0]+from[1]+from[2]);
51 else if(type_in==SOCK_VECTOR) {
52 if(ns->sockettype==SOCK_FLOAT) {
61 else { /* type_in==SOCK_RGBA */
62 if(ns->sockettype==SOCK_RGBA) {
65 else if(ns->sockettype==SOCK_FLOAT) {
79 /* ******************* execute and parse ************ */
81 /* Used for muted nodes, just copy the vec data from input to output… */
82 void node_shader_pass_on(void *UNUSED(data), int UNUSED(thread), struct bNode *node, void *UNUSED(nodedata),
83 struct bNodeStack **in, struct bNodeStack **out)
86 LinkInOutsMuteNode *lnk;
89 if(node->typeinfo->mutelinksfunc == NULL)
92 /* Get default muting links (as bNodeStack pointers). */
93 links = node->typeinfo->mutelinksfunc(NULL, node, in, out, NULL, NULL);
95 for(lnk = links.first; lnk; lnk = lnk->next) {
96 for(i = 0; i < lnk->num_outs; i++) {
97 copy_v4_v4((((bNodeStack*)(lnk->outs))+i)->vec, ((bNodeStack*)(lnk->in))->vec);
99 /* If num_outs > 1, lnk->outs was an allocated table of pointers... */
101 MEM_freeN(lnk->outs);
103 BLI_freelistN(&links);
106 int gpu_shader_pass_on(struct GPUMaterial *mat, struct bNode *node, void *UNUSED(nodedata),
107 struct GPUNodeStack *in, struct GPUNodeStack *out)
110 LinkInOutsMuteNode *lnk;
112 if(node->typeinfo->mutelinksfunc == NULL)
115 /* Get default muting links (as GPUNodeStack pointers). */
116 links = node->typeinfo->mutelinksfunc(NULL, node, NULL, NULL, in, out);
118 for(lnk = links.first; lnk; lnk = lnk->next) {
119 GPU_stack_link_mute(mat, "copy_raw", lnk);
120 /* If num_outs > 1, lnk->outs was an allocated table of pointers... */
121 if(lnk->num_outs > 1)
122 MEM_freeN(lnk->outs);
125 BLI_freelistN(&links);
129 /* go over all used Geometry and Texture nodes, and return a texco flag */
130 /* no group inside needed, this function is called for groups too */
131 void ntreeShaderGetTexcoMode(bNodeTree *ntree, int r_mode, short *texco, int *mode)
137 for(node= ntree->nodes.first; node; node= node->next) {
138 if(node->type==SH_NODE_TEXTURE) {
139 if((r_mode & R_OSA) && node->id) {
140 Tex *tex= (Tex *)node->id;
141 if ELEM3(tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP)
142 *texco |= TEXCO_OSA|NEED_UV;
144 /* usability exception... without input we still give the node orcos */
145 sock= node->inputs.first;
146 if(sock==NULL || sock->link==NULL)
147 *texco |= TEXCO_ORCO|NEED_UV;
149 else if(node->type==SH_NODE_GEOMETRY) {
150 /* note; sockets always exist for the given type! */
151 for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
152 if(sock->flag & SOCK_IN_USE) {
155 *texco |= TEXCO_GLOB|NEED_UV; break;
157 *texco |= TEXCO_VIEW|NEED_UV; break;
159 *texco |= TEXCO_ORCO|NEED_UV; break;
161 *texco |= TEXCO_UV|NEED_UV; break;
162 case GEOM_OUT_NORMAL:
163 *texco |= TEXCO_NORM|NEED_UV; break;
165 *texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
166 case GEOM_OUT_VCOL_ALPHA:
167 *texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
175 /* nodes that use ID data get synced with local data */
176 void nodeShaderSynchronizeID(bNode *node, int copyto)
178 if(node->id==NULL) return;
180 if(ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
182 Material *ma= (Material *)node->id;
185 /* hrmf, case in loop isnt super fast, but we dont edit 100s of material at same time either! */
186 for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
187 if(!nodeSocketIsHidden(sock)) {
191 copy_v3_v3(&ma->r, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
193 copy_v3_v3(&ma->specr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
195 ma->ref= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
197 copy_v3_v3(&ma->mirr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
199 ma->amb= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
201 ma->emit= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
203 ma->spectra= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
204 case MAT_IN_RAY_MIRROR:
205 ma->ray_mirror= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
207 ma->alpha= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
208 case MAT_IN_TRANSLUCENCY:
209 ma->translucency= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
215 copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->r); break;
217 copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->specr); break;
219 ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ref; break;
221 copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->mirr); break;
223 ((bNodeSocketValueFloat*)sock->default_value)->value= ma->amb; break;
225 ((bNodeSocketValueFloat*)sock->default_value)->value= ma->emit; break;
227 ((bNodeSocketValueFloat*)sock->default_value)->value= ma->spectra; break;
228 case MAT_IN_RAY_MIRROR:
229 ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ray_mirror; break;
231 ((bNodeSocketValueFloat*)sock->default_value)->value= ma->alpha; break;
232 case MAT_IN_TRANSLUCENCY:
233 ((bNodeSocketValueFloat*)sock->default_value)->value= ma->translucency; break;
243 void node_gpu_stack_from_data(struct GPUNodeStack *gs, int type, bNodeStack *ns)
245 memset(gs, 0, sizeof(*gs));
247 copy_v4_v4(gs->vec, ns->vec);
250 if (type == SOCK_FLOAT)
252 else if (type == SOCK_VECTOR)
254 else if (type == SOCK_RGBA)
256 else if (type == SOCK_SHADER)
262 gs->hasinput= ns->hasinput && ns->data;
263 /* XXX Commented out the ns->data check here, as it seems it’s not alwas set,
264 * even though there *is* a valid connection/output... But that might need
265 * further investigation.
267 gs->hasoutput= ns->hasoutput /*&& ns->data*/;
268 gs->sockettype= ns->sockettype;
271 void node_data_from_gpu_stack(bNodeStack *ns, GPUNodeStack *gs)
274 ns->sockettype= gs->sockettype;
277 static void gpu_stack_from_data_list(GPUNodeStack *gs, ListBase *sockets, bNodeStack **ns)
282 for (sock=sockets->first, i=0; sock; sock=sock->next, i++)
283 node_gpu_stack_from_data(&gs[i], sock->type, ns[i]);
285 gs[i].type= GPU_NONE;
288 static void data_from_gpu_stack_list(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
293 for (sock=sockets->first, i=0; sock; sock=sock->next, i++)
294 node_data_from_gpu_stack(ns[i], &gs[i]);
297 bNode *nodeGetActiveTexture(bNodeTree *ntree)
299 /* this is the node we texture paint and draw in textured draw */
305 /* check for group edit */
306 for(node= ntree->nodes.first; node; node= node->next)
307 if(node->flag & NODE_GROUP_EDIT)
311 ntree= (bNodeTree*)node->id;
313 for(node= ntree->nodes.first; node; node= node->next)
314 if(node->flag & NODE_ACTIVE_TEXTURE)
320 void ntreeExecGPUNodes(bNodeTreeExec *exec, GPUMaterial *mat, int do_outputs)
326 bNodeStack *nsin[MAX_SOCKET]; /* arbitrary... watch this */
327 bNodeStack *nsout[MAX_SOCKET]; /* arbitrary... watch this */
328 GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
333 for(n=0, nodeexec= exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
334 node = nodeexec->node;
337 /* for groups, only execute outputs for edited group */
338 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
339 if(do_outputs && (node->flag & NODE_DO_OUTPUT))
346 if((node->flag & NODE_MUTED) && node->typeinfo->gpumutefunc) {
347 node_get_stack(node, stack, nsin, nsout);
348 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
349 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
350 if(node->typeinfo->gpumutefunc(mat, node, nodeexec->data, gpuin, gpuout))
351 data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
353 else if(node->typeinfo->gpufunc) {
354 node_get_stack(node, stack, nsin, nsout);
355 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
356 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
357 if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
358 data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
360 else if(node->typeinfo->gpuextfunc) {
361 node_get_stack(node, stack, nsin, nsout);
362 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
363 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
364 if(node->typeinfo->gpuextfunc(mat, node, nodeexec->data, gpuin, gpuout))
365 data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
371 void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *UNUSED(out))
373 NodeTexBase *base= node->storage;
374 TexMapping *texmap= &base->tex_mapping;
375 float domin= (texmap->flag & TEXMAP_CLIP_MIN) != 0;
376 float domax= (texmap->flag & TEXMAP_CLIP_MAX) != 0;
378 if(domin || domax || !(texmap->flag & TEXMAP_UNIT_MATRIX)) {
379 GPUNodeLink *tmat = GPU_uniform((float*)texmap->mat);
380 GPUNodeLink *tmin = GPU_uniform(texmap->min);
381 GPUNodeLink *tmax = GPU_uniform(texmap->max);
382 GPUNodeLink *tdomin = GPU_uniform(&domin);
383 GPUNodeLink *tdomax = GPU_uniform(&domax);
385 GPU_link(mat, "mapping", in[0].link, tmat, tmin, tmax, tdomin, tdomax, &in[0].link);