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