replace macros with bli math functions for nodes
[blender.git] / source / blender / nodes / shader / node_shader_util.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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. 
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2005 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/shader/node_shader_util.c
29  *  \ingroup nodes
30  */
31
32
33 #include "DNA_node_types.h"
34
35 #include "node_shader_util.h"
36
37 #include "node_exec.h"
38
39 /* ****** */
40
41 void nodestack_get_vec(float *in, short type_in, bNodeStack *ns)
42 {
43         float *from= ns->vec;
44                 
45         if(type_in==SOCK_FLOAT) {
46                 if(ns->sockettype==SOCK_FLOAT)
47                         *in= *from;
48                 else 
49                         *in= 0.333333f*(from[0]+from[1]+from[2]);
50         }
51         else if(type_in==SOCK_VECTOR) {
52                 if(ns->sockettype==SOCK_FLOAT) {
53                         in[0]= from[0];
54                         in[1]= from[0];
55                         in[2]= from[0];
56                 }
57                 else {
58                         copy_v3_v3(in, from);
59                 }
60         }
61         else { /* type_in==SOCK_RGBA */
62                 if(ns->sockettype==SOCK_RGBA) {
63                         copy_v4_v4(in, from);
64                 }
65                 else if(ns->sockettype==SOCK_FLOAT) {
66                         in[0]= from[0];
67                         in[1]= from[0];
68                         in[2]= from[0];
69                         in[3]= 1.0f;
70                 }
71                 else {
72                         copy_v3_v3(in, from);
73                         in[3]= 1.0f;
74                 }
75         }
76 }
77
78
79 /* ******************* execute and parse ************ */
80
81 /* go over all used Geometry and Texture nodes, and return a texco flag */
82 /* no group inside needed, this function is called for groups too */
83 void ntreeShaderGetTexcoMode(bNodeTree *ntree, int r_mode, short *texco, int *mode)
84 {
85         bNode *node;
86         bNodeSocket *sock;
87         int a;
88         
89         ntreeSocketUseFlags(ntree);
90
91         for(node= ntree->nodes.first; node; node= node->next) {
92                 if(node->type==SH_NODE_TEXTURE) {
93                         if((r_mode & R_OSA) && node->id) {
94                                 Tex *tex= (Tex *)node->id;
95                                 if ELEM3(tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) 
96                                         *texco |= TEXCO_OSA|NEED_UV;
97                         }
98                         /* usability exception... without input we still give the node orcos */
99                         sock= node->inputs.first;
100                         if(sock==NULL || sock->link==NULL)
101                                 *texco |= TEXCO_ORCO|NEED_UV;
102                 }
103                 else if(node->type==SH_NODE_GEOMETRY) {
104                         /* note; sockets always exist for the given type! */
105                         for(a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
106                                 if(sock->flag & SOCK_IN_USE) {
107                                         switch(a) {
108                                                 case GEOM_OUT_GLOB: 
109                                                         *texco |= TEXCO_GLOB|NEED_UV; break;
110                                                 case GEOM_OUT_VIEW: 
111                                                         *texco |= TEXCO_VIEW|NEED_UV; break;
112                                                 case GEOM_OUT_ORCO: 
113                                                         *texco |= TEXCO_ORCO|NEED_UV; break;
114                                                 case GEOM_OUT_UV: 
115                                                         *texco |= TEXCO_UV|NEED_UV; break;
116                                                 case GEOM_OUT_NORMAL: 
117                                                         *texco |= TEXCO_NORM|NEED_UV; break;
118                                                 case GEOM_OUT_VCOL:
119                                                         *texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
120                                         }
121                                 }
122                         }
123                 }
124         }
125 }
126
127 /* nodes that use ID data get synced with local data */
128 void nodeShaderSynchronizeID(bNode *node, int copyto)
129 {
130         if(node->id==NULL) return;
131         
132         if(ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
133                 bNodeSocket *sock;
134                 Material *ma= (Material *)node->id;
135                 int a;
136                 
137                 /* hrmf, case in loop isnt super fast, but we dont edit 100s of material at same time either! */
138                 for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
139                         if(!(sock->flag & SOCK_HIDDEN)) {
140                                 if(copyto) {
141                                         switch(a) {
142                                                 case MAT_IN_COLOR:
143                                                         copy_v3_v3(&ma->r, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
144                                                 case MAT_IN_SPEC:
145                                                         copy_v3_v3(&ma->specr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
146                                                 case MAT_IN_REFL:
147                                                         ma->ref= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
148                                                 case MAT_IN_MIR:
149                                                         copy_v3_v3(&ma->mirr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
150                                                 case MAT_IN_AMB:
151                                                         ma->amb= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
152                                                 case MAT_IN_EMIT:
153                                                         ma->emit= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
154                                                 case MAT_IN_SPECTRA:
155                                                         ma->spectra= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
156                                                 case MAT_IN_RAY_MIRROR:
157                                                         ma->ray_mirror= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
158                                                 case MAT_IN_ALPHA:
159                                                         ma->alpha= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
160                                                 case MAT_IN_TRANSLUCENCY:
161                                                         ma->translucency= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
162                                         }
163                                 }
164                                 else {
165                                         switch(a) {
166                                                 case MAT_IN_COLOR:
167                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->r); break;
168                                                 case MAT_IN_SPEC:
169                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->specr); break;
170                                                 case MAT_IN_REFL:
171                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ref; break;
172                                                 case MAT_IN_MIR:
173                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->mirr); break;
174                                                 case MAT_IN_AMB:
175                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->amb; break;
176                                                 case MAT_IN_EMIT:
177                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->emit; break;
178                                                 case MAT_IN_SPECTRA:
179                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->spectra; break;
180                                                 case MAT_IN_RAY_MIRROR:
181                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ray_mirror; break;
182                                                 case MAT_IN_ALPHA:
183                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->alpha; break;
184                                                 case MAT_IN_TRANSLUCENCY:
185                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->translucency; break;
186                                         }
187                                 }
188                         }
189                 }
190         }
191         
192 }
193
194
195 void node_gpu_stack_from_data(struct GPUNodeStack *gs, int type, bNodeStack *ns)
196 {
197         memset(gs, 0, sizeof(*gs));
198         
199         copy_v4_v4(gs->vec, ns->vec);
200         gs->link= ns->data;
201         
202         if (type == SOCK_FLOAT)
203                 gs->type= GPU_FLOAT;
204         else if (type == SOCK_VECTOR)
205                 gs->type= GPU_VEC3;
206         else if (type == SOCK_RGBA)
207                 gs->type= GPU_VEC4;
208         else if (type == SOCK_SHADER)
209                 gs->type= GPU_VEC4;
210         else
211                 gs->type= GPU_NONE;
212         
213         gs->name = "";
214         gs->hasinput= ns->hasinput && ns->data;
215         gs->hasoutput= ns->hasoutput && ns->data;
216         gs->sockettype= ns->sockettype;
217 }
218
219 void node_data_from_gpu_stack(bNodeStack *ns, GPUNodeStack *gs)
220 {
221         ns->data= gs->link;
222         ns->sockettype= gs->sockettype;
223 }
224
225 static void gpu_stack_from_data_list(GPUNodeStack *gs, ListBase *sockets, bNodeStack **ns)
226 {
227         bNodeSocket *sock;
228         int i;
229         
230         for (sock=sockets->first, i=0; sock; sock=sock->next, i++)
231                 node_gpu_stack_from_data(&gs[i], sock->type, ns[i]);
232         
233         gs[i].type= GPU_NONE;
234 }
235
236 static void data_from_gpu_stack_list(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
237 {
238         bNodeSocket *sock;
239         int i;
240
241         for (sock=sockets->first, i=0; sock; sock=sock->next, i++)
242                 node_data_from_gpu_stack(ns[i], &gs[i]);
243 }
244
245 void ntreeExecGPUNodes(bNodeTreeExec *exec, GPUMaterial *mat, int do_outputs)
246 {
247         bNodeExec *nodeexec;
248         bNode *node;
249         int n;
250         bNodeStack *stack;
251         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
252         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
253         GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
254         int doit;
255
256         stack= exec->stack;
257
258         for(n=0, nodeexec= exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
259                 node = nodeexec->node;
260                 
261                 doit = 0;
262                 /* for groups, only execute outputs for edited group */
263                 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
264                         if(do_outputs && (node->flag & NODE_DO_OUTPUT))
265                                 doit = 1;
266                 }
267                 else
268                         doit = 1;
269
270                 if (doit) {
271                         if(node->typeinfo->gpufunc) {
272                                 node_get_stack(node, stack, nsin, nsout);
273                                 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
274                                 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
275                                 if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
276                                         data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
277                         }
278                         else if(node->typeinfo->gpuextfunc) {
279                                 node_get_stack(node, stack, nsin, nsout);
280                                 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
281                                 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
282                                 if(node->typeinfo->gpuextfunc(mat, node, nodeexec->data, gpuin, gpuout))
283                                         data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
284                         }
285                 }
286         }
287 }