Cleanup: Adhere to our naming convention for BKE_linestyle.h API
[blender.git] / source / blender / nodes / texture / node_texture_tree.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) 2007 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s):
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/nodes/texture/node_texture_tree.c
29  *  \ingroup nodes
30  */
31
32
33 #include <string.h>
34
35 #include "DNA_texture_types.h"
36 #include "DNA_node_types.h"
37 #include "DNA_space_types.h"
38
39 #include "BLI_listbase.h"
40 #include "BLI_threads.h"
41 #include "BLI_utildefines.h"
42
43 #include "BLF_translation.h"
44
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_linestyle.h"
48 #include "BKE_main.h"
49 #include "BKE_node.h"
50 #include "BKE_paint.h"
51
52 #include "node_common.h"
53 #include "node_exec.h"
54 #include "node_util.h"
55 #include "NOD_texture.h"
56 #include "node_texture_util.h"
57
58 #include "RNA_access.h"
59
60 #include "RE_pipeline.h"
61 #include "RE_shader_ext.h"
62
63
64 static void texture_get_from_context(const bContext *C, bNodeTreeType *UNUSED(treetype), bNodeTree **r_ntree, ID **r_id, ID **r_from)
65 {
66         SpaceNode *snode = CTX_wm_space_node(C);
67         Scene *scene = CTX_data_scene(C);
68         Object *ob = OBACT;
69         Tex *tx = NULL;
70
71         if (snode->texfrom == SNODE_TEX_OBJECT) {
72                 if (ob) {
73                         tx = give_current_object_texture(ob);
74                         if (tx) {
75                                 if (ob->type == OB_LAMP)
76                                         *r_from = (ID *)ob->data;
77                                 else
78                                         *r_from = (ID *)give_current_material(ob, ob->actcol);
79                                 
80                                 /* from is not set fully for material nodes, should be ID + Node then */
81                                 *r_id = &tx->id;
82                                 *r_ntree = tx->nodetree;
83                         }
84                 }
85         }
86         else if (snode->texfrom == SNODE_TEX_WORLD) {
87                 if (scene->world) {
88                         *r_from = (ID *)scene->world;
89                         tx = give_current_world_texture(scene->world);
90                         if (tx) {
91                                 *r_id = &tx->id;
92                                 *r_ntree = tx->nodetree;
93                         }
94                 }
95         }
96         else if (snode->texfrom == SNODE_TEX_BRUSH) {
97                 struct Brush *brush = NULL;
98                 
99                 if (ob && (ob->mode & OB_MODE_SCULPT))
100                         brush = BKE_paint_brush(&scene->toolsettings->sculpt->paint);
101                 else
102                         brush = BKE_paint_brush(&scene->toolsettings->imapaint.paint);
103
104                 if (brush) {
105                         *r_from = (ID *)brush;
106                         tx = give_current_brush_texture(brush);
107                         if (tx) {
108                                 *r_id = &tx->id;
109                                 *r_ntree = tx->nodetree;
110                         }
111                 }
112         }
113         else if (snode->texfrom == SNODE_TEX_LINESTYLE) {
114                 FreestyleLineStyle *linestyle = BKE_linestyle_active_from_scene(scene);
115                 if (linestyle) {
116                         *r_from = (ID *)linestyle;
117                         tx = give_current_linestyle_texture(linestyle);
118                         if (tx) {
119                                 *r_id = &tx->id;
120                                 *r_ntree = tx->nodetree;
121                         }
122                 }
123         }
124 }
125
126 static void foreach_nodeclass(Scene *UNUSED(scene), void *calldata, bNodeClassCallback func)
127 {
128         func(calldata, NODE_CLASS_INPUT, N_("Input"));
129         func(calldata, NODE_CLASS_OUTPUT, N_("Output"));
130         func(calldata, NODE_CLASS_OP_COLOR, N_("Color"));
131         func(calldata, NODE_CLASS_PATTERN, N_("Patterns"));
132         func(calldata, NODE_CLASS_TEXTURE, N_("Textures"));
133         func(calldata, NODE_CLASS_CONVERTOR, N_("Convertor"));
134         func(calldata, NODE_CLASS_DISTORT, N_("Distort"));
135         func(calldata, NODE_CLASS_GROUP, N_("Group"));
136         func(calldata, NODE_CLASS_INTERFACE, N_("Interface"));
137         func(calldata, NODE_CLASS_LAYOUT, N_("Layout"));
138 }
139
140 static void localize(bNodeTree *localtree, bNodeTree *UNUSED(ntree))
141 {
142         bNode *node, *node_next;
143         
144         /* replace muted nodes and reroute nodes by internal links */
145         for (node = localtree->nodes.first; node; node = node_next) {
146                 node_next = node->next;
147                 
148                 if (node->flag & NODE_MUTED || node->type == NODE_REROUTE) {
149                         nodeInternalRelink(localtree, node);
150                         nodeFreeNode(localtree, node);
151                 }
152         }
153 }
154
155 static void local_sync(bNodeTree *localtree, bNodeTree *ntree)
156 {
157         BKE_node_preview_sync_tree(ntree, localtree);
158 }
159
160 static void local_merge(bNodeTree *localtree, bNodeTree *ntree)
161 {
162         BKE_node_preview_merge_tree(ntree, localtree, true);
163 }
164
165 static void update(bNodeTree *ntree)
166 {
167         ntree_update_reroute_nodes(ntree);
168         
169         if (ntree->update & NTREE_UPDATE_NODES) {
170                 /* clean up preview cache, in case nodes have been removed */
171                 BKE_node_preview_remove_unused(ntree);
172         }
173 }
174
175 bNodeTreeType *ntreeType_Texture;
176
177 void register_node_tree_type_tex(void)
178 {
179         bNodeTreeType *tt = ntreeType_Texture = MEM_callocN(sizeof(bNodeTreeType), "texture node tree type");
180         
181         tt->type = NTREE_TEXTURE;
182         strcpy(tt->idname, "TextureNodeTree");
183         strcpy(tt->ui_name, "Texture");
184         tt->ui_icon = 0;    /* defined in drawnode.c */
185         strcpy(tt->ui_description, "Texture nodes");
186         
187         tt->foreach_nodeclass = foreach_nodeclass;
188         tt->update = update;
189         tt->localize = localize;
190         tt->local_sync = local_sync;
191         tt->local_merge = local_merge;
192         tt->get_from_context = texture_get_from_context;
193         
194         tt->ext.srna = &RNA_TextureNodeTree;
195         
196         ntreeTypeAdd(tt);
197 }
198
199 int ntreeTexTagAnimated(bNodeTree *ntree)
200 {
201         bNode *node;
202         
203         if (ntree == NULL) return 0;
204         
205         for (node = ntree->nodes.first; node; node = node->next) {
206                 if (node->type == TEX_NODE_CURVE_TIME) {
207                         nodeUpdate(ntree, node);
208                         return 1;
209                 }
210                 else if (node->type == NODE_GROUP) {
211                         if (ntreeTexTagAnimated((bNodeTree *)node->id) ) {
212                                 return 1;
213                         }
214                 }
215         }
216         
217         return 0;
218 }
219
220 bNodeTreeExec *ntreeTexBeginExecTree_internal(bNodeExecContext *context, bNodeTree *ntree, bNodeInstanceKey parent_key)
221 {
222         bNodeTreeExec *exec;
223         bNode *node;
224         
225         /* common base initialization */
226         exec = ntree_exec_begin(context, ntree, parent_key);
227         
228         /* allocate the thread stack listbase array */
229         exec->threadstack = MEM_callocN(BLENDER_MAX_THREADS * sizeof(ListBase), "thread stack array");
230         
231         for (node = exec->nodetree->nodes.first; node; node = node->next)
232                 node->need_exec = 1;
233         
234         return exec;
235 }
236
237 bNodeTreeExec *ntreeTexBeginExecTree(bNodeTree *ntree)
238 {
239         bNodeExecContext context;
240         bNodeTreeExec *exec;
241         
242         /* XXX hack: prevent exec data from being generated twice.
243          * this should be handled by the renderer!
244          */
245         if (ntree->execdata)
246                 return ntree->execdata;
247         
248         context.previews = ntree->previews;
249         
250         exec = ntreeTexBeginExecTree_internal(&context, ntree, NODE_INSTANCE_KEY_BASE);
251         
252         /* XXX this should not be necessary, but is still used for cmp/sha/tex nodes,
253          * which only store the ntree pointer. Should be fixed at some point!
254          */
255         ntree->execdata = exec;
256         
257         return exec;
258 }
259
260 /* free texture delegates */
261 static void tex_free_delegates(bNodeTreeExec *exec)
262 {
263         bNodeThreadStack *nts;
264         bNodeStack *ns;
265         int th, a;
266         
267         for (th = 0; th < BLENDER_MAX_THREADS; th++)
268                 for (nts = exec->threadstack[th].first; nts; nts = nts->next)
269                         for (ns = nts->stack, a = 0; a < exec->stacksize; a++, ns++)
270                                 if (ns->data && !ns->is_copy)
271                                         MEM_freeN(ns->data);
272 }
273
274 void ntreeTexEndExecTree_internal(bNodeTreeExec *exec)
275 {
276         bNodeThreadStack *nts;
277         int a;
278         
279         if (exec->threadstack) {
280                 tex_free_delegates(exec);
281                 
282                 for (a = 0; a < BLENDER_MAX_THREADS; a++) {
283                         for (nts = exec->threadstack[a].first; nts; nts = nts->next)
284                                 if (nts->stack) MEM_freeN(nts->stack);
285                         BLI_freelistN(&exec->threadstack[a]);
286                 }
287                 
288                 MEM_freeN(exec->threadstack);
289                 exec->threadstack = NULL;
290         }
291         
292         ntree_exec_end(exec);
293 }
294
295 void ntreeTexEndExecTree(bNodeTreeExec *exec)
296 {
297         if (exec) {
298                 /* exec may get freed, so assign ntree */
299                 bNodeTree *ntree = exec->nodetree;
300                 ntreeTexEndExecTree_internal(exec);
301                 
302                 /* XXX clear nodetree backpointer to exec data, same problem as noted in ntreeBeginExecTree */
303                 ntree->execdata = NULL;
304         }
305 }
306
307 int ntreeTexExecTree(
308         bNodeTree *nodes,
309         TexResult *texres,
310         float co[3],
311         float dxt[3], float dyt[3],
312         int osatex,
313         const short thread,
314         Tex *UNUSED(tex),
315         short which_output,
316         int cfra,
317         int preview,
318         ShadeInput *shi,
319         MTex *mtex)
320 {
321         TexCallData data;
322         float *nor = texres->nor;
323         int retval = TEX_INT;
324         bNodeThreadStack *nts = NULL;
325         bNodeTreeExec *exec = nodes->execdata;
326
327         data.co = co;
328         data.dxt = dxt;
329         data.dyt = dyt;
330         data.osatex = osatex;
331         data.target = texres;
332         data.do_preview = preview;
333         data.do_manage = (shi) ? shi->do_manage : 0;
334         data.thread = thread;
335         data.which_output = which_output;
336         data.cfra = cfra;
337         data.mtex = mtex;
338         data.shi = shi;
339         
340         /* ensure execdata is only initialized once */
341         if (!exec) {
342                 BLI_lock_thread(LOCK_NODES);
343                 if (!nodes->execdata)
344                         ntreeTexBeginExecTree(nodes);
345                 BLI_unlock_thread(LOCK_NODES);
346
347                 exec = nodes->execdata;
348         }
349         
350         nts = ntreeGetThreadStack(exec, thread);
351         ntreeExecThreadNodes(exec, nts, &data, thread);
352         ntreeReleaseThreadStack(nts);
353
354         if (texres->nor) retval |= TEX_NOR;
355         retval |= TEX_RGB;
356         /* confusing stuff; the texture output node sets this to NULL to indicate no normal socket was set
357          * however, the texture code checks this for other reasons (namely, a normal is required for material) */
358         texres->nor = nor;
359
360         return retval;
361 }