Merge with trunk r41625
[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                                                 case GEOM_OUT_VCOL_ALPHA:
121                                                         *texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
122                                         }
123                                 }
124                         }
125                 }
126         }
127 }
128
129 /* nodes that use ID data get synced with local data */
130 void nodeShaderSynchronizeID(bNode *node, int copyto)
131 {
132         if(node->id==NULL) return;
133         
134         if(ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
135                 bNodeSocket *sock;
136                 Material *ma= (Material *)node->id;
137                 int a;
138                 
139                 /* hrmf, case in loop isnt super fast, but we dont edit 100s of material at same time either! */
140                 for(a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
141                         if(!(sock->flag & SOCK_HIDDEN)) {
142                                 if(copyto) {
143                                         switch(a) {
144                                                 case MAT_IN_COLOR:
145                                                         copy_v3_v3(&ma->r, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
146                                                 case MAT_IN_SPEC:
147                                                         copy_v3_v3(&ma->specr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
148                                                 case MAT_IN_REFL:
149                                                         ma->ref= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
150                                                 case MAT_IN_MIR:
151                                                         copy_v3_v3(&ma->mirr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
152                                                 case MAT_IN_AMB:
153                                                         ma->amb= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
154                                                 case MAT_IN_EMIT:
155                                                         ma->emit= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
156                                                 case MAT_IN_SPECTRA:
157                                                         ma->spectra= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
158                                                 case MAT_IN_RAY_MIRROR:
159                                                         ma->ray_mirror= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
160                                                 case MAT_IN_ALPHA:
161                                                         ma->alpha= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
162                                                 case MAT_IN_TRANSLUCENCY:
163                                                         ma->translucency= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
164                                         }
165                                 }
166                                 else {
167                                         switch(a) {
168                                                 case MAT_IN_COLOR:
169                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->r); break;
170                                                 case MAT_IN_SPEC:
171                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->specr); break;
172                                                 case MAT_IN_REFL:
173                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ref; break;
174                                                 case MAT_IN_MIR:
175                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->mirr); break;
176                                                 case MAT_IN_AMB:
177                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->amb; break;
178                                                 case MAT_IN_EMIT:
179                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->emit; break;
180                                                 case MAT_IN_SPECTRA:
181                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->spectra; break;
182                                                 case MAT_IN_RAY_MIRROR:
183                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ray_mirror; break;
184                                                 case MAT_IN_ALPHA:
185                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->alpha; break;
186                                                 case MAT_IN_TRANSLUCENCY:
187                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->translucency; break;
188                                         }
189                                 }
190                         }
191                 }
192         }
193         
194 }
195
196
197 void node_gpu_stack_from_data(struct GPUNodeStack *gs, int type, bNodeStack *ns)
198 {
199         memset(gs, 0, sizeof(*gs));
200         
201         copy_v4_v4(gs->vec, ns->vec);
202         gs->link= ns->data;
203         
204         if (type == SOCK_FLOAT)
205                 gs->type= GPU_FLOAT;
206         else if (type == SOCK_VECTOR)
207                 gs->type= GPU_VEC3;
208         else if (type == SOCK_RGBA)
209                 gs->type= GPU_VEC4;
210         else if (type == SOCK_SHADER)
211                 gs->type= GPU_VEC4;
212         else
213                 gs->type= GPU_NONE;
214         
215         gs->name = "";
216         gs->hasinput= ns->hasinput && ns->data;
217         gs->hasoutput= ns->hasoutput && ns->data;
218         gs->sockettype= ns->sockettype;
219 }
220
221 void node_data_from_gpu_stack(bNodeStack *ns, GPUNodeStack *gs)
222 {
223         ns->data= gs->link;
224         ns->sockettype= gs->sockettype;
225 }
226
227 static void gpu_stack_from_data_list(GPUNodeStack *gs, ListBase *sockets, bNodeStack **ns)
228 {
229         bNodeSocket *sock;
230         int i;
231         
232         for (sock=sockets->first, i=0; sock; sock=sock->next, i++)
233                 node_gpu_stack_from_data(&gs[i], sock->type, ns[i]);
234         
235         gs[i].type= GPU_NONE;
236 }
237
238 static void data_from_gpu_stack_list(ListBase *sockets, bNodeStack **ns, GPUNodeStack *gs)
239 {
240         bNodeSocket *sock;
241         int i;
242
243         for (sock=sockets->first, i=0; sock; sock=sock->next, i++)
244                 node_data_from_gpu_stack(ns[i], &gs[i]);
245 }
246
247 void ntreeExecGPUNodes(bNodeTreeExec *exec, GPUMaterial *mat, int do_outputs)
248 {
249         bNodeExec *nodeexec;
250         bNode *node;
251         int n;
252         bNodeStack *stack;
253         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
254         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
255         GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
256         int doit;
257
258         stack= exec->stack;
259
260         for(n=0, nodeexec= exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
261                 node = nodeexec->node;
262                 
263                 doit = 0;
264                 /* for groups, only execute outputs for edited group */
265                 if(node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
266                         if(do_outputs && (node->flag & NODE_DO_OUTPUT))
267                                 doit = 1;
268                 }
269                 else
270                         doit = 1;
271
272                 if (doit) {
273                         if(node->typeinfo->gpufunc) {
274                                 node_get_stack(node, stack, nsin, nsout);
275                                 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
276                                 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
277                                 if(node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
278                                         data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
279                         }
280                         else if(node->typeinfo->gpuextfunc) {
281                                 node_get_stack(node, stack, nsin, nsout);
282                                 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
283                                 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
284                                 if(node->typeinfo->gpuextfunc(mat, node, nodeexec->data, gpuin, gpuout))
285                                         data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
286                         }
287                 }
288         }
289 }