Welcome to mirror list, hosted at ThFree Co, Russian Federation.

node_composite_common.c « nodes « composite « nodes « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d5ae442c25f422cda5cf944a4083555e2e109682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. 
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2006 Blender Foundation.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Campbell Barton, Alfredo de Greef, David Millan Escriva,
 * Juho Vepsäläinen
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/nodes/composite/nodes/node_composite_common.c
 *  \ingroup cmpnodes
 */


#include "DNA_node_types.h"

#include "BKE_node.h"

#include "node_composite_util.h"
#include "node_common.h"
#include "node_exec.h"

#if 0
static void PRINT_BUFFERS(bNodeTreeExec *exec)
{
	bNodeTree *ntree= exec->nodetree;
	bNode *node;
	bNodeSocket *sock;
	bNodeStack *ns;
	int i;
	
	printf("-------------- DEBUG --------------\n");
	for (sock=ntree->inputs.first, i=0; sock; sock=sock->next, ++i) {
		ns = node_get_socket_stack(exec->stack, sock);
		printf("%d. Tree Input %s", i, sock->name);
		if (ns->external)
			printf(" (external)");
		printf(": data=%p\n", ns->data);
	}
	for (sock=ntree->outputs.first, i=0; sock; sock=sock->next, ++i) {
		ns = node_get_socket_stack(exec->stack, sock);
		printf("%d. Tree Output %s", i, sock->name);
		if (ns->external)
			printf(" (external)");
		printf(": data=%p\n", ns->data);
	}
	for (node=ntree->nodes.first; node; node=node->next) {
		printf("Node %s:\n", node->name);
		for (sock=node->inputs.first, i=0; sock; sock=sock->next, ++i) {
			ns = node_get_socket_stack(exec->stack, sock);
			printf("\t%d. Input %s", i, sock->name);
			if (ns->external)
				printf(" (external)");
			printf(": data=%p\n", ns->data);
		}
		for (sock=node->outputs.first, i=0; sock; sock=sock->next, ++i) {
			ns = node_get_socket_stack(exec->stack, sock);
			printf("\t%d. Output %s", i, sock->name);
			if (ns->external)
				printf(" (external)");
			printf(": data=%p\n", ns->data);
		}
	}
}
#endif

static void copy_stack(bNodeStack *to, bNodeStack *from)
{
	if (to != from) {
		copy_v4_v4(to->vec, from->vec);
		to->data = from->data;
		to->datatype = from->datatype;
		
		/* tag as copy to prevent freeing */
		to->is_copy = 1;
	}
}

static void move_stack(bNodeStack *to, bNodeStack *from)
{
	if (to != from) {
		copy_v4_v4(to->vec, from->vec);
		to->data = from->data;
		to->datatype = from->datatype;
		to->is_copy = from->is_copy;
		
		zero_v4(from->vec);
		from->data = NULL;
		from->datatype = 0;
		from->is_copy = 0;
	}
}

/**** GROUP ****/

static void *group_initexec(bNode *node)
{
	bNodeTree *ngroup= (bNodeTree*)node->id;
	bNodeTreeExec *exec;
	bNodeSocket *sock;
	bNodeStack *ns;
	
	/* initialize the internal node tree execution */
	exec = ntreeCompositBeginExecTree(ngroup, 0);
	
	/* tag group outputs as external to prevent freeing */
	for (sock=ngroup->outputs.first; sock; sock=sock->next) {
		if (!(sock->flag & SOCK_INTERNAL)) {
			ns = node_get_socket_stack(exec->stack, sock);
			ns->external = 1;
		}
	}

	return exec;
}

static void group_freeexec(bNode *UNUSED(node), void *nodedata)
{
	bNodeTreeExec *gexec= (bNodeTreeExec*)nodedata;
	
	ntreeCompositEndExecTree(gexec, 0);
}

/* Copy inputs to the internal stack.
 * This is a shallow copy, no buffers are duplicated here!
 */
static void group_copy_inputs(bNode *node, bNodeStack **in, bNodeStack *gstack)
{
	bNodeSocket *sock;
	bNodeStack *ns;
	int a;
	for (sock=node->inputs.first, a=0; sock; sock=sock->next, ++a) {
		if (sock->groupsock) {
			ns = node_get_socket_stack(gstack, sock->groupsock);
			copy_stack(ns, in[a]);
		}
	}
}

/* Copy internal results to the external outputs.
 */
static void group_move_outputs(bNode *node, bNodeStack **out, bNodeStack *gstack)
{
	bNodeSocket *sock;
	bNodeStack *ns;
	int a;
	for (sock=node->outputs.first, a=0; sock; sock=sock->next, ++a) {
		if (sock->groupsock) {
			ns = node_get_socket_stack(gstack, sock->groupsock);
			move_stack(out[a], ns);
		}
	}
}

/* Free internal buffers */
static void group_free_internal(bNodeTreeExec *gexec) {
	bNodeStack *ns;
	int i;
	
	for (i=0, ns=gexec->stack; i < gexec->stacksize; ++i, ++ns) {
		if (!ns->external && !ns->is_copy) {
			if (ns->data) {
				free_compbuf(ns->data);
				ns->data = NULL;
			}
		}
	}
}

static void group_execute(void *data, int thread, struct bNode *node, void *nodedata, struct bNodeStack **in, struct bNodeStack **out)
{
	bNodeTreeExec *exec= (bNodeTreeExec*)nodedata;
	
	/* XXX same behavior as trunk: all nodes inside group are executed.
	 * it's stupid, but just makes it work. compo redesign will do this better.
	 */
	{
		bNode *inode;
		for (inode=exec->nodetree->nodes.first; inode; inode=inode->next)
			inode->need_exec = 1;
	}
	
	group_copy_inputs(node, in, exec->stack);
	ntreeExecNodes(exec, data, thread);
	group_free_internal(exec);
	group_move_outputs(node, out, exec->stack);
}

void register_node_type_cmp_group(ListBase *lb)
{
	static bNodeType ntype;

	node_type_base(&ntype, NODE_GROUP, "Group", NODE_CLASS_GROUP, NODE_OPTIONS|NODE_CONST_OUTPUT);
	node_type_socket_templates(&ntype, NULL, NULL);
	node_type_size(&ntype, 120, 60, 200);
	node_type_label(&ntype, node_group_label);
	node_type_init(&ntype, node_group_init);
	node_type_valid(&ntype, node_group_valid);
	node_type_template(&ntype, node_group_template);
	node_type_update(&ntype, NULL, node_group_verify);
	node_type_group_edit(&ntype, node_group_edit_get, node_group_edit_set, node_group_edit_clear);
	node_type_exec_new(&ntype, group_initexec, group_freeexec, group_execute);
	
	nodeRegisterType(lb, &ntype);
}


/**** FOR LOOP ****/

#if 0 /* XXX loop nodes don't work nicely with current trees */
/* Move the results from the previous iteration back to the input sockets. */
static void loop_iteration_reset(bNodeTree *ngroup, bNodeStack *gstack)
{
	bNodeSocket *gin, *gout;
	bNodeStack *nsin, *nsout;
	
	gin = ngroup->inputs.first;
	gout = ngroup->outputs.first;
	
	while (gin && gout) {
		/* skip static (non-looping) sockets */
		while (gin && !(gin->flag & SOCK_DYNAMIC))
			gin=gin->next;
		while (gout && !(gout->flag & SOCK_DYNAMIC))
			gout=gout->next;
		
		if (gin && gout) {
			nsin = node_get_socket_stack(gstack, gin);
			nsout = node_get_socket_stack(gstack, gout);
			
			move_stack(nsin, nsout);
			
			gin=gin->next;
			gout=gout->next;
		}
	}
}

static void forloop_execute(void *data, int thread, struct bNode *node, void *nodedata, struct bNodeStack **in, struct bNodeStack **out)
{
	bNodeTreeExec *exec= (bNodeTreeExec*)nodedata;
	int totiterations= (int)in[0]->vec[0];
	bNodeSocket *sock;
	bNodeStack *ns;
	int iteration;
	
	/* XXX same behavior as trunk: all nodes inside group are executed.
	 * it's stupid, but just makes it work. compo redesign will do this better.
	 */
	{
		bNode *inode;
		for (inode=exec->nodetree->nodes.first; inode; inode=inode->next)
			inode->need_exec = 1;
	}
	
	/* "Iteration" socket */
	sock = exec->nodetree->inputs.first;
	ns = node_get_socket_stack(exec->stack, sock);
	
	group_copy_inputs(node, in, exec->stack);
	for (iteration=0; iteration < totiterations; ++iteration) {
		/* first input contains current iteration counter */
		ns->vec[0] = (float)iteration;
		
		if (iteration > 0)
			loop_iteration_reset(exec->nodetree, exec->stack);
		ntreeExecNodes(exec, data, thread);
		group_free_internal(exec);
	}
	group_move_outputs(node, out, exec->stack);
}

void register_node_type_cmp_forloop(ListBase *lb)
{
	static bNodeType ntype;

	node_type_base(&ntype, NODE_FORLOOP, "For", NODE_CLASS_GROUP, NODE_OPTIONS);
	node_type_socket_templates(&ntype, NULL, NULL);
	node_type_size(&ntype, 120, 60, 200);
	node_type_label(&ntype, node_group_label);
	node_type_init(&ntype, node_forloop_init);
	node_type_valid(&ntype, node_group_valid);
	node_type_template(&ntype, node_forloop_template);
	node_type_update(&ntype, NULL, node_group_verify);
	node_type_tree(&ntype, node_forloop_init_tree, node_loop_update_tree);
	node_type_group_edit(&ntype, node_group_edit_get, node_group_edit_set, node_group_edit_clear);
	node_type_exec_new(&ntype, group_initexec, group_freeexec, forloop_execute);
	
	nodeRegisterType(lb, &ntype);
}
#endif


/**** WHILE LOOP ****/

#if 0 /* XXX loop nodes don't work nicely with current trees */
static void whileloop_execute(void *data, int thread, struct bNode *node, void *nodedata, struct bNodeStack **in, struct bNodeStack **out)
{
	bNodeTreeExec *exec= (bNodeTreeExec*)nodedata;
	int condition= (in[0]->vec[0] > 0.0f);
	bNodeSocket *sock;
	bNodeStack *ns;
	int iteration;
	
	/* XXX same behavior as trunk: all nodes inside group are executed.
	 * it's stupid, but just makes it work. compo redesign will do this better.
	 */
	{
		bNode *inode;
		for (inode=exec->nodetree->nodes.first; inode; inode=inode->next)
			inode->need_exec = 1;
	}
	
	/* "Condition" socket */
	sock = exec->nodetree->outputs.first;
	ns = node_get_socket_stack(exec->stack, sock);
	
	iteration = 0;
	group_copy_inputs(node, in, exec->stack);
	while (condition && iteration < node->custom1) {
		if (iteration > 0)
			loop_iteration_reset(exec->nodetree, exec->stack);
		ntreeExecNodes(exec, data, thread);
		group_free_internal(exec);
		
//		PRINT_BUFFERS(exec);
		
		condition = (ns->vec[0] > 0.0f);
		++iteration;
	}
	group_move_outputs(node, out, exec->stack);
}

void register_node_type_cmp_whileloop(ListBase *lb)
{
	static bNodeType ntype;

	node_type_base(&ntype, NODE_WHILELOOP, "While", NODE_CLASS_GROUP, NODE_OPTIONS);
	node_type_socket_templates(&ntype, NULL, NULL);
	node_type_size(&ntype, 120, 60, 200);
	node_type_label(&ntype, node_group_label);
	node_type_init(&ntype, node_whileloop_init);
	node_type_valid(&ntype, node_group_valid);
	node_type_template(&ntype, node_whileloop_template);
	node_type_update(&ntype, NULL, node_group_verify);
	node_type_tree(&ntype, node_whileloop_init_tree, node_loop_update_tree);
	node_type_group_edit(&ntype, node_group_edit_get, node_group_edit_set, node_group_edit_clear);
	node_type_exec_new(&ntype, group_initexec, group_freeexec, whileloop_execute);
	
	nodeRegisterType(lb, &ntype);
}
#endif