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