style cleanup: follow style guide for formatting of if/for/while loops, and else...
[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 /* go over all used Geometry and Texture nodes, and return a texco flag */
80 /* no group inside needed, this function is called for groups too */
81 void ntreeShaderGetTexcoMode(bNodeTree *ntree, int r_mode, short *texco, int *mode)
82 {
83         bNode *node;
84         bNodeSocket *sock;
85         int a;
86         
87         for (node= ntree->nodes.first; node; node= node->next) {
88                 if (node->type==SH_NODE_TEXTURE) {
89                         if ((r_mode & R_OSA) && node->id) {
90                                 Tex *tex= (Tex *)node->id;
91                                 if ELEM3(tex->type, TEX_IMAGE, TEX_PLUGIN, TEX_ENVMAP) 
92                                         *texco |= TEXCO_OSA|NEED_UV;
93                         }
94                         /* usability exception... without input we still give the node orcos */
95                         sock= node->inputs.first;
96                         if (sock==NULL || sock->link==NULL)
97                                 *texco |= TEXCO_ORCO|NEED_UV;
98                 }
99                 else if (node->type==SH_NODE_GEOMETRY) {
100                         /* note; sockets always exist for the given type! */
101                         for (a=0, sock= node->outputs.first; sock; sock= sock->next, a++) {
102                                 if (sock->flag & SOCK_IN_USE) {
103                                         switch(a) {
104                                                 case GEOM_OUT_GLOB: 
105                                                         *texco |= TEXCO_GLOB|NEED_UV; break;
106                                                 case GEOM_OUT_VIEW: 
107                                                         *texco |= TEXCO_VIEW|NEED_UV; break;
108                                                 case GEOM_OUT_ORCO: 
109                                                         *texco |= TEXCO_ORCO|NEED_UV; break;
110                                                 case GEOM_OUT_UV: 
111                                                         *texco |= TEXCO_UV|NEED_UV; break;
112                                                 case GEOM_OUT_NORMAL: 
113                                                         *texco |= TEXCO_NORM|NEED_UV; break;
114                                                 case GEOM_OUT_VCOL:
115                                                         *texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
116                                                 case GEOM_OUT_VCOL_ALPHA:
117                                                         *texco |= NEED_UV; *mode |= MA_VERTEXCOL; break;
118                                         }
119                                 }
120                         }
121                 }
122         }
123 }
124
125 /* nodes that use ID data get synced with local data */
126 void nodeShaderSynchronizeID(bNode *node, int copyto)
127 {
128         if (node->id==NULL) return;
129         
130         if (ELEM(node->type, SH_NODE_MATERIAL, SH_NODE_MATERIAL_EXT)) {
131                 bNodeSocket *sock;
132                 Material *ma= (Material *)node->id;
133                 int a;
134                 
135                 /* hrmf, case in loop isn't super fast, but we don't edit 100s of material at same time either! */
136                 for (a=0, sock= node->inputs.first; sock; sock= sock->next, a++) {
137                         if (!nodeSocketIsHidden(sock)) {
138                                 if (copyto) {
139                                         switch(a) {
140                                                 case MAT_IN_COLOR:
141                                                         copy_v3_v3(&ma->r, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
142                                                 case MAT_IN_SPEC:
143                                                         copy_v3_v3(&ma->specr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
144                                                 case MAT_IN_REFL:
145                                                         ma->ref= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
146                                                 case MAT_IN_MIR:
147                                                         copy_v3_v3(&ma->mirr, ((bNodeSocketValueRGBA*)sock->default_value)->value); break;
148                                                 case MAT_IN_AMB:
149                                                         ma->amb= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
150                                                 case MAT_IN_EMIT:
151                                                         ma->emit= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
152                                                 case MAT_IN_SPECTRA:
153                                                         ma->spectra= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
154                                                 case MAT_IN_RAY_MIRROR:
155                                                         ma->ray_mirror= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
156                                                 case MAT_IN_ALPHA:
157                                                         ma->alpha= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
158                                                 case MAT_IN_TRANSLUCENCY:
159                                                         ma->translucency= ((bNodeSocketValueFloat*)sock->default_value)->value; break;
160                                         }
161                                 }
162                                 else {
163                                         switch(a) {
164                                                 case MAT_IN_COLOR:
165                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->r); break;
166                                                 case MAT_IN_SPEC:
167                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->specr); break;
168                                                 case MAT_IN_REFL:
169                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ref; break;
170                                                 case MAT_IN_MIR:
171                                                         copy_v3_v3(((bNodeSocketValueRGBA*)sock->default_value)->value, &ma->mirr); break;
172                                                 case MAT_IN_AMB:
173                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->amb; break;
174                                                 case MAT_IN_EMIT:
175                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->emit; break;
176                                                 case MAT_IN_SPECTRA:
177                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->spectra; break;
178                                                 case MAT_IN_RAY_MIRROR:
179                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->ray_mirror; break;
180                                                 case MAT_IN_ALPHA:
181                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->alpha; break;
182                                                 case MAT_IN_TRANSLUCENCY:
183                                                         ((bNodeSocketValueFloat*)sock->default_value)->value= ma->translucency; break;
184                                         }
185                                 }
186                         }
187                 }
188         }
189         
190 }
191
192
193 void node_gpu_stack_from_data(struct GPUNodeStack *gs, int type, bNodeStack *ns)
194 {
195         memset(gs, 0, sizeof(*gs));
196         
197         copy_v4_v4(gs->vec, ns->vec);
198         gs->link= ns->data;
199         
200         if (type == SOCK_FLOAT)
201                 gs->type= GPU_FLOAT;
202         else if (type == SOCK_VECTOR)
203                 gs->type= GPU_VEC3;
204         else if (type == SOCK_RGBA)
205                 gs->type= GPU_VEC4;
206         else if (type == SOCK_SHADER)
207                 gs->type= GPU_VEC4;
208         else
209                 gs->type= GPU_NONE;
210         
211         gs->name = "";
212         gs->hasinput= ns->hasinput && ns->data;
213         /* XXX Commented out the ns->data check here, as it seems it's not alwas set,
214          *     even though there *is* a valid connection/output... But that might need
215          *     further investigation.
216          */
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 bNode *nodeGetActiveTexture(bNodeTree *ntree)
248 {
249         /* this is the node we texture paint and draw in textured draw */
250         bNode *node;
251
252         if (!ntree)
253                 return NULL;
254
255         /* check for group edit */
256         for (node= ntree->nodes.first; node; node= node->next)
257                 if (node->flag & NODE_GROUP_EDIT)
258                         break;
259
260         if (node)
261                 ntree= (bNodeTree*)node->id;
262
263         for (node= ntree->nodes.first; node; node= node->next)
264                 if (node->flag & NODE_ACTIVE_TEXTURE)
265                         return node;
266         
267         return NULL;
268 }
269
270 void ntreeExecGPUNodes(bNodeTreeExec *exec, GPUMaterial *mat, int do_outputs)
271 {
272         bNodeExec *nodeexec;
273         bNode *node;
274         int n;
275         bNodeStack *stack;
276         bNodeStack *nsin[MAX_SOCKET];   /* arbitrary... watch this */
277         bNodeStack *nsout[MAX_SOCKET];  /* arbitrary... watch this */
278         GPUNodeStack gpuin[MAX_SOCKET+1], gpuout[MAX_SOCKET+1];
279         int doit;
280
281         stack= exec->stack;
282
283         for (n=0, nodeexec= exec->nodeexec; n < exec->totnodes; ++n, ++nodeexec) {
284                 node = nodeexec->node;
285                 
286                 doit = 0;
287                 /* for groups, only execute outputs for edited group */
288                 if (node->typeinfo->nclass==NODE_CLASS_OUTPUT) {
289                         if (do_outputs && (node->flag & NODE_DO_OUTPUT))
290                                 doit = 1;
291                 }
292                 else
293                         doit = 1;
294
295                 if (doit) {
296                         if (node->typeinfo->gpufunc) {
297                                 node_get_stack(node, stack, nsin, nsout);
298                                 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
299                                 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
300                                 if (node->typeinfo->gpufunc(mat, node, gpuin, gpuout))
301                                         data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
302                         }
303                         else if (node->typeinfo->gpuextfunc) {
304                                 node_get_stack(node, stack, nsin, nsout);
305                                 gpu_stack_from_data_list(gpuin, &node->inputs, nsin);
306                                 gpu_stack_from_data_list(gpuout, &node->outputs, nsout);
307                                 if (node->typeinfo->gpuextfunc(mat, node, nodeexec->data, gpuin, gpuout))
308                                         data_from_gpu_stack_list(&node->outputs, nsout, gpuout);
309                         }
310                 }
311         }
312 }
313
314 void node_shader_gpu_tex_mapping(GPUMaterial *mat, bNode *node, GPUNodeStack *in, GPUNodeStack *UNUSED(out))
315 {
316         NodeTexBase *base= node->storage;
317         TexMapping *texmap= &base->tex_mapping;
318         float domin= (texmap->flag & TEXMAP_CLIP_MIN) != 0;
319         float domax= (texmap->flag & TEXMAP_CLIP_MAX) != 0;
320
321         if (domin || domax || !(texmap->flag & TEXMAP_UNIT_MATRIX)) {
322                 GPUNodeLink *tmat = GPU_uniform((float*)texmap->mat);
323                 GPUNodeLink *tmin = GPU_uniform(texmap->min);
324                 GPUNodeLink *tmax = GPU_uniform(texmap->max);
325                 GPUNodeLink *tdomin = GPU_uniform(&domin);
326                 GPUNodeLink *tdomax = GPU_uniform(&domax);
327
328                 GPU_link(mat, "mapping", in[0].link, tmat, tmin, tmax, tdomin, tdomax, &in[0].link);
329         }
330 }
331