Fix again for the NVidia driver bug. This time I'm just giving up
[blender.git] / source / blender / gpu / intern / gpu_codegen.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2005 Blender Foundation.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): Brecht Van Lommel.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #include "GL/glew.h"
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_customdata_types.h"
38 #include "DNA_image_types.h"
39 #include "DNA_listBase.h"
40 #include "DNA_material_types.h"
41
42 #include "BLI_dynstr.h"
43 #include "BLI_blenlib.h"
44 #include "BLI_ghash.h"
45 #include "BLI_heap.h"
46
47 #include "BKE_global.h"
48 #include "BKE_utildefines.h"
49
50 #include "GPU_material.h"
51 #include "GPU_extensions.h"
52
53 #include "BLO_sys_types.h" // for intptr_t support
54
55 #include "gpu_codegen.h"
56
57 #include <string.h>
58 #include <stdarg.h>
59
60 #ifdef _WIN32
61 #ifndef vsnprintf
62 #define vsnprintf _vsnprintf
63 #endif
64 #ifndef snprintf
65 #define snprintf _snprintf
66 #endif
67 #endif
68
69 extern char datatoc_gpu_shader_material_glsl[];
70 extern char datatoc_gpu_shader_vertex_glsl[];
71
72 /* structs and defines */
73
74 typedef enum GPUDataSource {
75         GPU_SOURCE_VEC_UNIFORM,
76         GPU_SOURCE_BUILTIN,
77         GPU_SOURCE_TEX_PIXEL,
78         GPU_SOURCE_TEX,
79         GPU_SOURCE_ATTRIB
80 } GPUDataSource;
81
82 static char* GPU_DATATYPE_STR[17] = {"", "float", "vec2", "vec3", "vec4",
83         0, 0, 0, 0, "mat3", 0, 0, 0, 0, 0, 0, "mat4"};
84
85 struct GPUNode {
86         struct GPUNode *next, *prev;
87
88         char *name;
89         int tag;
90
91         ListBase inputs;
92         ListBase outputs;
93 };
94
95 struct GPUNodeLink {
96         GPUNodeStack *socket;
97
98         int attribtype;
99         char *attribname;
100
101         int image;
102
103         int texture;
104         int texturesize;
105
106         void *ptr1, *ptr2;
107
108         int dynamic;
109
110         int type;
111         int users;
112
113         GPUTexture *dynamictex;
114
115         GPUBuiltin builtin;
116
117         struct GPUOutput *output;
118 };
119
120 typedef struct GPUOutput {
121         struct GPUOutput *next, *prev;
122
123         GPUNode *node;
124         int type;                               /* data type = length of vector/matrix */
125         GPUNodeLink *link;              /* output link */
126         int id;                                 /* unique id as created by code generator */
127 } GPUOutput;
128
129 typedef struct GPUInput {
130         struct GPUInput *next, *prev;
131
132         GPUNode *node;
133
134         int type;                               /* datatype */
135         int source;                             /* data source */
136
137         int id;                                 /* unique id as created by code generator */
138         int texid;                              /* number for multitexture */
139         int attribid;                   /* id for vertex attributes */
140         int bindtex;                    /* input is responsible for binding the texture? */
141         int definetex;                  /* input is responsible for defining the pixel? */
142         int textarget;                  /* GL_TEXTURE_* */
143         int textype;                    /* datatype */
144
145         struct Image *ima;              /* image */
146         struct ImageUser *iuser;/* image user */
147         float *dynamicvec;              /* vector data in case it is dynamic */
148         GPUTexture *tex;                /* input texture, only set at runtime */
149         int shaderloc;                  /* id from opengl */
150         char shadername[32];    /* name in shader */
151
152         float vec[16];                  /* vector data */
153         GPUNodeLink *link;
154         int dynamictex;                 /* dynamic? */
155         int attribtype;                 /* attribute type */
156         char attribname[32];    /* attribute name */
157         int attribfirst;                /* this is the first one that is bound */
158         GPUBuiltin builtin;             /* builtin uniform */
159 } GPUInput;
160
161 struct GPUPass {
162         struct GPUPass *next, *prev;
163
164         ListBase inputs;
165         struct GPUOutput *output;
166         struct GPUShader *shader;
167 };
168
169 /* Strings utility */
170
171 static void BLI_dynstr_printf(DynStr *dynstr, const char *format, ...)
172 {
173         va_list args;
174         int retval;
175         char str[2048];
176
177         va_start(args, format);
178         retval = vsnprintf(str, sizeof(str), format, args);
179         va_end(args);
180
181         if (retval >= sizeof(str))
182                 fprintf(stderr, "BLI_dynstr_printf: limit exceeded\n");
183         else
184                 BLI_dynstr_append(dynstr, str);
185 }
186
187 /* GLSL code parsing for finding function definitions.
188  * These are stored in a hash for lookup when creating a material. */
189
190 static GHash *FUNCTION_HASH= NULL;
191 /*static char *FUNCTION_PROTOTYPES= NULL;
192 static GPUShader *FUNCTION_LIB= NULL;*/
193
194 static int gpu_str_prefix(char *str, char *prefix)
195 {
196         while(*str && *prefix) {
197                 if(*str != *prefix)
198                         return 0;
199
200                 str++;
201                 prefix++;
202         }
203         
204         return (*prefix == '\0');
205 }
206
207 static char *gpu_str_skip_token(char *str, char *token, int max)
208 {
209         int len = 0;
210
211         /* skip a variable/function name */
212         while(*str) {
213                 if(ELEM6(*str, ' ', '(', ')', ',', '\t', '\n'))
214                         break;
215                 else {
216                         if(token && len < max-1) {
217                                 *token= *str;
218                                 token++;
219                                 len++;
220                         }
221                         str++;
222                 }
223         }
224
225         if(token)
226                 *token= '\0';
227
228         /* skip the next special characters:
229          * note the missing ')' */
230         while(*str) {
231                 if(ELEM5(*str, ' ', '(', ',', '\t', '\n'))
232                         str++;
233                 else
234                         break;
235         }
236
237         return str;
238 }
239
240 static void gpu_parse_functions_string(GHash *hash, char *code)
241 {
242         GPUFunction *function;
243         int i, type, qual;
244
245         while((code = strstr(code, "void "))) {
246                 function = MEM_callocN(sizeof(GPUFunction), "GPUFunction");
247
248                 code = gpu_str_skip_token(code, NULL, 0);
249                 code = gpu_str_skip_token(code, function->name, MAX_FUNCTION_NAME);
250
251                 /* get parameters */
252                 while(*code && *code != ')') {
253                         /* test if it's an input or output */
254                         qual = FUNCTION_QUAL_IN;
255                         if(gpu_str_prefix(code, "out "))
256                                 qual = FUNCTION_QUAL_OUT;
257                         if(gpu_str_prefix(code, "inout "))
258                                 qual = FUNCTION_QUAL_INOUT;
259                         if((qual != FUNCTION_QUAL_IN) || gpu_str_prefix(code, "in "))
260                                 code = gpu_str_skip_token(code, NULL, 0);
261
262                         /* test for type */
263                         type= 0;
264                         for(i=1; i<=16; i++) {
265                                 if(GPU_DATATYPE_STR[i] && gpu_str_prefix(code, GPU_DATATYPE_STR[i])) {
266                                         type= i;
267                                         break;
268                                 }
269                         }
270
271                         if(!type && gpu_str_prefix(code, "sampler2DShadow"))
272                                 type= GPU_SHADOW2D;
273                         if(!type && gpu_str_prefix(code, "sampler1D"))
274                                 type= GPU_TEX1D;
275                         if(!type && gpu_str_prefix(code, "sampler2D"))
276                                 type= GPU_TEX2D;
277
278                         if(type) {
279                                 /* add paramater */
280                                 code = gpu_str_skip_token(code, NULL, 0);
281                                 code = gpu_str_skip_token(code, NULL, 0);
282                                 function->paramqual[function->totparam]= qual;
283                                 function->paramtype[function->totparam]= type;
284                                 function->totparam++;
285                         }
286                         else {
287                                 fprintf(stderr, "GPU invalid function parameter in %s.\n", function->name);
288                                 break;
289                         }
290                 }
291
292                 if(strlen(function->name) == 0 || function->totparam == 0) {
293                         fprintf(stderr, "GPU functions parse error.\n");
294                         MEM_freeN(function);
295                         break;
296                 }
297
298                 BLI_ghash_insert(hash, function->name, function);
299         }
300 }
301
302 #if 0
303 static char *gpu_generate_function_prototyps(GHash *hash)
304 {
305         DynStr *ds = BLI_dynstr_new();
306         GHashIterator *ghi;
307         GPUFunction *function;
308         char *name, *prototypes;
309         int a;
310         
311         /* automatically generate function prototypes to add to the top of the
312          * generated code, to avoid have to add the actual code & recompile all */
313         ghi = BLI_ghashIterator_new(hash);
314
315         for(; !BLI_ghashIterator_isDone(ghi); BLI_ghashIterator_step(ghi)) {
316                 name = BLI_ghashIterator_getValue(ghi);
317                 function = BLI_ghashIterator_getValue(ghi);
318
319                 BLI_dynstr_printf(ds, "void %s(", name);
320                 for(a=0; a<function->totparam; a++) {
321                         if(function->paramqual[a] == FUNCTION_QUAL_OUT)
322                                 BLI_dynstr_append(ds, "out ");
323                         else if(function->paramqual[a] == FUNCTION_QUAL_INOUT)
324                                 BLI_dynstr_append(ds, "inout ");
325
326                         if(function->paramtype[a] == GPU_TEX1D)
327                                 BLI_dynstr_append(ds, "sampler1D");
328                         else if(function->paramtype[a] == GPU_TEX2D)
329                                 BLI_dynstr_append(ds, "sampler2D");
330                         else if(function->paramtype[a] == GPU_SHADOW2D)
331                                 BLI_dynstr_append(ds, "sampler2DShadow");
332                         else
333                                 BLI_dynstr_append(ds, GPU_DATATYPE_STR[function->paramtype[a]]);
334                                 
335                         //BLI_dynstr_printf(ds, " param%d", a);
336                         
337                         if(a != function->totparam-1)
338                                 BLI_dynstr_append(ds, ", ");
339                 }
340                 BLI_dynstr_append(ds, ");\n");
341         }
342
343         BLI_dynstr_append(ds, "\n");
344
345         prototypes = BLI_dynstr_get_cstring(ds);
346         BLI_dynstr_free(ds);
347
348         return prototypes;
349 }
350 #endif
351
352 GPUFunction *GPU_lookup_function(char *name)
353 {
354         if(!FUNCTION_HASH) {
355                 FUNCTION_HASH = BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp);
356                 gpu_parse_functions_string(FUNCTION_HASH, datatoc_gpu_shader_material_glsl);
357                 /*FUNCTION_PROTOTYPES = gpu_generate_function_prototyps(FUNCTION_HASH);
358                 FUNCTION_LIB = GPU_shader_create_lib(datatoc_gpu_shader_material_glsl);*/
359         }
360
361         return (GPUFunction*)BLI_ghash_lookup(FUNCTION_HASH, name);
362 }
363
364 void GPU_extensions_exit(void)
365 {
366         extern Material defmaterial;    // render module abuse...
367
368         if(defmaterial.gpumaterial.first)
369                 GPU_material_free(&defmaterial);
370
371         if(FUNCTION_HASH) {
372                 BLI_ghash_free(FUNCTION_HASH, NULL, (GHashValFreeFP)MEM_freeN);
373                 FUNCTION_HASH = NULL;
374         }
375         /*if(FUNCTION_PROTOTYPES) {
376                 MEM_freeN(FUNCTION_PROTOTYPES);
377                 FUNCTION_PROTOTYPES = NULL;
378         }*/
379         /*if(FUNCTION_LIB) {
380                 GPU_shader_free(FUNCTION_LIB);
381                 FUNCTION_LIB = NULL;
382         }*/
383 }
384
385 /* GLSL code generation */
386
387 static void codegen_convert_datatype(DynStr *ds, int from, int to, char *tmp, int id)
388 {
389         char name[1024];
390
391         snprintf(name, sizeof(name), "%s%d", tmp, id);
392
393         if (from == to) {
394                 BLI_dynstr_append(ds, name);
395         }
396         else if (to == GPU_FLOAT) {
397                 if (from == GPU_VEC4)
398                         BLI_dynstr_printf(ds, "dot(%s.rgb, vec3(0.35, 0.45, 0.2))", name);
399                 else if (from == GPU_VEC3)
400                         BLI_dynstr_printf(ds, "dot(%s, vec3(0.33))", name);
401                 else if (from == GPU_VEC2)
402                         BLI_dynstr_printf(ds, "%s.r", name);
403         }
404         else if (to == GPU_VEC2) {
405                 if (from == GPU_VEC4)
406                         BLI_dynstr_printf(ds, "vec2(dot(%s.rgb, vec3(0.35, 0.45, 0.2)), %s.a)", name, name);
407                 else if (from == GPU_VEC3)
408                         BLI_dynstr_printf(ds, "vec2(dot(%s.rgb, vec3(0.33)), 1.0)", name);
409                 else if (from == GPU_FLOAT)
410                         BLI_dynstr_printf(ds, "vec2(%s, 1.0)", name);
411         }
412         else if (to == GPU_VEC3) {
413                 if (from == GPU_VEC4)
414                         BLI_dynstr_printf(ds, "%s.rgb", name);
415                 else if (from == GPU_VEC2)
416                         BLI_dynstr_printf(ds, "vec3(%s.r, %s.r, %s.r)", name, name, name);
417                 else if (from == GPU_FLOAT)
418                         BLI_dynstr_printf(ds, "vec3(%s, %s, %s)", name, name, name);
419         }
420         else {
421                 if (from == GPU_VEC3)
422                         BLI_dynstr_printf(ds, "vec4(%s, 1.0)", name);
423                 else if (from == GPU_VEC2)
424                         BLI_dynstr_printf(ds, "vec4(%s.r, %s.r, %s.r, %s.g)", name, name, name, name);
425                 else if (from == GPU_FLOAT)
426                         BLI_dynstr_printf(ds, "vec4(%s, %s, %s, 1.0)", name, name, name);
427         }
428 }
429
430 static void codegen_print_datatype(DynStr *ds, int type, float *data)
431 {
432         int i;
433
434         BLI_dynstr_printf(ds, "%s(", GPU_DATATYPE_STR[type]);
435
436         for(i=0; i<type; i++) {
437                 BLI_dynstr_printf(ds, "%f", data[i]);
438                 if(i == type-1)
439                         BLI_dynstr_append(ds, ")");
440                 else
441                         BLI_dynstr_append(ds, ", ");
442         }
443 }
444
445 static int codegen_input_has_texture(GPUInput *input)
446 {
447         if (input->link)
448                 return 0;
449         else if(input->ima)
450                 return 1;
451         else
452                 return input->tex != 0;
453 }
454
455 char *GPU_builtin_name(GPUBuiltin builtin)
456 {
457         if(builtin == GPU_VIEW_MATRIX)
458                 return "unfviewmat";
459         else if(builtin == GPU_OBJECT_MATRIX)
460                 return "unfobmat";
461         else if(builtin == GPU_INVERSE_VIEW_MATRIX)
462                 return "unfinvviewmat";
463         else if(builtin == GPU_INVERSE_OBJECT_MATRIX)
464                 return "unfinvobmat";
465         else if(builtin == GPU_VIEW_POSITION)
466                 return "varposition";
467         else if(builtin == GPU_VIEW_NORMAL)
468                 return "varnormal";
469         else if(builtin == GPU_OBCOLOR)
470                 return "unfobcolor";
471         else
472                 return "";
473 }
474
475 static void codegen_set_unique_ids(ListBase *nodes)
476 {
477         GHash *bindhash, *definehash;
478         GPUNode *node;
479         GPUInput *input;
480         GPUOutput *output;
481         int id = 1, texid = 0;
482
483         bindhash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
484         definehash= BLI_ghash_new(BLI_ghashutil_ptrhash, BLI_ghashutil_ptrcmp);
485
486         for (node=nodes->first; node; node=node->next) {
487                 for (input=node->inputs.first; input; input=input->next) {
488                         /* set id for unique names of uniform variables */
489                         input->id = id++;
490                         input->bindtex = 0;
491                         input->definetex = 0;
492
493                         /* set texid used for settings texture slot with multitexture */
494                         if (codegen_input_has_texture(input) &&
495                             ((input->source == GPU_SOURCE_TEX) || (input->source == GPU_SOURCE_TEX_PIXEL))) {
496                                 if (input->link) {
497                                         /* input is texture from buffer, assign only one texid per
498                                            buffer to avoid sampling the same texture twice */
499                                         if (!BLI_ghash_haskey(bindhash, input->link)) {
500                                                 input->texid = texid++;
501                                                 input->bindtex = 1;
502                                                 BLI_ghash_insert(bindhash, input->link, SET_INT_IN_POINTER(input->texid));
503                                         }
504                                         else
505                                                 input->texid = GET_INT_FROM_POINTER(BLI_ghash_lookup(bindhash, input->link));
506                                 }
507                                 else if(input->ima) {
508                                         /* input is texture from image, assign only one texid per
509                                            buffer to avoid sampling the same texture twice */
510                                         if (!BLI_ghash_haskey(bindhash, input->ima)) {
511                                                 input->texid = texid++;
512                                                 input->bindtex = 1;
513                                                 BLI_ghash_insert(bindhash, input->ima, SET_INT_IN_POINTER(input->texid));
514                                         }
515                                         else
516                                                 input->texid = GET_INT_FROM_POINTER(BLI_ghash_lookup(bindhash, input->ima));
517                                 }
518                                 else {
519                                         /* input is user created texture, we know there there is
520                                            only one, so assign new texid */
521                                         input->bindtex = 1;
522                                         input->texid = texid++;
523                                 }
524
525                                 /* make sure this pixel is defined exactly once */
526                                 if (input->source == GPU_SOURCE_TEX_PIXEL) {
527                                         if(input->ima) {
528                                                 if (!BLI_ghash_haskey(definehash, input->ima)) {
529                                                         input->definetex = 1;
530                                                         BLI_ghash_insert(definehash, input->ima, SET_INT_IN_POINTER(input->texid));
531                                                 }
532                                         }
533                                         else {
534                                                 if (!BLI_ghash_haskey(definehash, input->link)) {
535                                                         input->definetex = 1;
536                                                         BLI_ghash_insert(definehash, input->link, SET_INT_IN_POINTER(input->texid));
537                                                 }
538                                         }
539                                 }
540                         }
541                 }
542
543                 for (output=node->outputs.first; output; output=output->next)
544                         /* set id for unique names of tmp variables storing output */
545                         output->id = id++;
546         }
547
548         BLI_ghash_free(bindhash, NULL, NULL);
549         BLI_ghash_free(definehash, NULL, NULL);
550 }
551
552 static void codegen_print_uniforms_functions(DynStr *ds, ListBase *nodes)
553 {
554         GPUNode *node;
555         GPUInput *input;
556         char *name;
557         int builtins = 0;
558
559         /* print uniforms */
560         for (node=nodes->first; node; node=node->next) {
561                 for (input=node->inputs.first; input; input=input->next) {
562                         if ((input->source == GPU_SOURCE_TEX) || (input->source == GPU_SOURCE_TEX_PIXEL)) {
563                                 /* create exactly one sampler for each texture */
564                                 if (codegen_input_has_texture(input) && input->bindtex)
565                                         BLI_dynstr_printf(ds, "uniform %s samp%d;\n",
566                                                 (input->textype == GPU_TEX1D)? "sampler1D":
567                                                 (input->textype == GPU_TEX2D)? "sampler2D": "sampler2DShadow",
568                                                 input->texid);
569                         }
570                         else if(input->source == GPU_SOURCE_BUILTIN) {
571                                 /* only define each builting uniform/varying once */
572                                 if(!(builtins & input->builtin)) {
573                                         builtins |= input->builtin;
574                                         name = GPU_builtin_name(input->builtin);
575
576                                         if(gpu_str_prefix(name, "unf")) {
577                                                 BLI_dynstr_printf(ds, "uniform %s %s;\n",
578                                                         GPU_DATATYPE_STR[input->type], name);
579                                         }
580                                         else {
581                                                 BLI_dynstr_printf(ds, "varying %s %s;\n",
582                                                         GPU_DATATYPE_STR[input->type], name);
583                                         }
584                                 }
585                         }
586                         else if (input->source == GPU_SOURCE_VEC_UNIFORM) {
587                                 if(input->dynamicvec) {
588                                         /* only create uniforms for dynamic vectors */
589                                         BLI_dynstr_printf(ds, "uniform %s unf%d;\n",
590                                                 GPU_DATATYPE_STR[input->type], input->id);
591                                 }
592                                 else {
593                                         /* for others use const so the compiler can do folding */
594                                         BLI_dynstr_printf(ds, "const %s cons%d = ",
595                                                 GPU_DATATYPE_STR[input->type], input->id);
596                                         codegen_print_datatype(ds, input->type, input->vec);
597                                         BLI_dynstr_append(ds, ";\n");
598                                 }
599                         }
600                         else if (input->source == GPU_SOURCE_ATTRIB && input->attribfirst) {
601                                 BLI_dynstr_printf(ds, "varying %s var%d;\n",
602                                         GPU_DATATYPE_STR[input->type], input->attribid);
603                         }
604                 }
605         }
606
607         BLI_dynstr_append(ds, "\n");
608 }
609
610 static void codegen_declare_tmps(DynStr *ds, ListBase *nodes)
611 {
612         GPUNode *node;
613         GPUInput *input;
614         GPUOutput *output;
615
616         for (node=nodes->first; node; node=node->next) {
617                 /* load pixels from textures */
618                 for (input=node->inputs.first; input; input=input->next) {
619                         if (input->source == GPU_SOURCE_TEX_PIXEL) {
620                                 if (codegen_input_has_texture(input) && input->definetex) {
621                                         BLI_dynstr_printf(ds, "\tvec4 tex%d = texture2D(", input->texid);
622                                         BLI_dynstr_printf(ds, "samp%d, gl_TexCoord[%d].st);\n",
623                                                 input->texid, input->texid);
624                                 }
625                         }
626                 }
627
628                 /* declare temporary variables for node output storage */
629                 for (output=node->outputs.first; output; output=output->next)
630                         BLI_dynstr_printf(ds, "\t%s tmp%d;\n",
631                                 GPU_DATATYPE_STR[output->type], output->id);
632         }
633
634         BLI_dynstr_append(ds, "\n");
635 }
636
637 static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *finaloutput)
638 {
639         GPUNode *node;
640         GPUInput *input;
641         GPUOutput *output;
642
643         for (node=nodes->first; node; node=node->next) {
644                 BLI_dynstr_printf(ds, "\t%s(", node->name);
645                 
646                 for (input=node->inputs.first; input; input=input->next) {
647                         if (input->source == GPU_SOURCE_TEX) {
648                                 BLI_dynstr_printf(ds, "samp%d", input->texid);
649                                 if (input->link)
650                                         BLI_dynstr_printf(ds, ", gl_TexCoord[%d].st", input->texid);
651                         }
652                         else if (input->source == GPU_SOURCE_TEX_PIXEL) {
653                                 if (input->link && input->link->output)
654                                         codegen_convert_datatype(ds, input->link->output->type, input->type,
655                                                 "tmp", input->link->output->id);
656                                 else
657                                         codegen_convert_datatype(ds, input->link->output->type, input->type,
658                                                 "tex", input->texid);
659                         }
660                         else if(input->source == GPU_SOURCE_BUILTIN)
661                                 BLI_dynstr_printf(ds, "%s", GPU_builtin_name(input->builtin));
662                         else if(input->source == GPU_SOURCE_VEC_UNIFORM) {
663                                 if(input->dynamicvec)
664                                         BLI_dynstr_printf(ds, "unf%d", input->id);
665                                 else
666                                         BLI_dynstr_printf(ds, "cons%d", input->id);
667                         }
668                         else if (input->source == GPU_SOURCE_ATTRIB)
669                                 BLI_dynstr_printf(ds, "var%d", input->attribid);
670
671                         BLI_dynstr_append(ds, ", ");
672                 }
673
674                 for (output=node->outputs.first; output; output=output->next) {
675                         BLI_dynstr_printf(ds, "tmp%d", output->id);
676                         if (output->next)
677                                 BLI_dynstr_append(ds, ", ");
678                 }
679
680                 BLI_dynstr_append(ds, ");\n");
681         }
682
683         BLI_dynstr_append(ds, "\n\tgl_FragColor = ");
684         codegen_convert_datatype(ds, finaloutput->type, GPU_VEC4, "tmp", finaloutput->id);
685         BLI_dynstr_append(ds, ";\n");
686 }
687
688 static char *code_generate_fragment(ListBase *nodes, GPUOutput *output, const char *name)
689 {
690         DynStr *ds = BLI_dynstr_new();
691         char *code;
692
693         /*BLI_dynstr_append(ds, FUNCTION_PROTOTYPES);*/
694
695         codegen_set_unique_ids(nodes);
696         codegen_print_uniforms_functions(ds, nodes);
697
698         //if(G.f & G_DEBUG)
699         //      BLI_dynstr_printf(ds, "/* %s */\n", name);
700
701         BLI_dynstr_append(ds, "void main(void)\n");
702         BLI_dynstr_append(ds, "{\n");
703
704         codegen_declare_tmps(ds, nodes);
705         codegen_call_functions(ds, nodes, output);
706
707         BLI_dynstr_append(ds, "}\n");
708
709         /* create shader */
710         code = BLI_dynstr_get_cstring(ds);
711         BLI_dynstr_free(ds);
712
713         //if(G.f & G_DEBUG) printf("%s\n", code);
714
715         return code;
716 }
717
718 static char *code_generate_vertex(ListBase *nodes)
719 {
720         DynStr *ds = BLI_dynstr_new();
721         GPUNode *node;
722         GPUInput *input;
723         char *code;
724         
725         for (node=nodes->first; node; node=node->next) {
726                 for (input=node->inputs.first; input; input=input->next) {
727                         if (input->source == GPU_SOURCE_ATTRIB && input->attribfirst) {
728                                 BLI_dynstr_printf(ds, "attribute %s att%d;\n",
729                                         GPU_DATATYPE_STR[input->type], input->attribid);
730                                 BLI_dynstr_printf(ds, "varying %s var%d;\n",
731                                         GPU_DATATYPE_STR[input->type], input->attribid);
732                         }
733                 }
734         }
735
736         BLI_dynstr_append(ds, "\n");
737         BLI_dynstr_append(ds, datatoc_gpu_shader_vertex_glsl);
738
739         for (node=nodes->first; node; node=node->next)
740                 for (input=node->inputs.first; input; input=input->next)
741                         if (input->source == GPU_SOURCE_ATTRIB && input->attribfirst) {
742                                 if(input->attribtype == CD_TANGENT) /* silly exception */
743                                         BLI_dynstr_printf(ds, "\tvar%d = gl_NormalMatrix * ", input->attribid);
744                                 else
745                                         BLI_dynstr_printf(ds, "\tvar%d = ", input->attribid);
746
747                                 BLI_dynstr_printf(ds, "att%d;\n", input->attribid);
748                         }
749
750         BLI_dynstr_append(ds, "}\n\n");
751
752         code = BLI_dynstr_get_cstring(ds);
753
754         BLI_dynstr_free(ds);
755
756         //if(G.f & G_DEBUG) printf("%s\n", code);
757
758         return code;
759 }
760
761 /* GPU pass binding/unbinding */
762
763 GPUShader *GPU_pass_shader(GPUPass *pass)
764 {
765         return pass->shader;
766 }
767
768 void GPU_nodes_extract_dynamic_inputs(GPUPass *pass, ListBase *nodes)
769 {
770         GPUShader *shader = pass->shader;
771         GPUNode *node;
772         GPUInput *next, *input;
773         ListBase *inputs = &pass->inputs;
774         int extract, z;
775
776         memset(inputs, 0, sizeof(*inputs));
777
778         if(!shader)
779                 return;
780
781         GPU_shader_bind(shader);
782
783         for (node=nodes->first; node; node=node->next) {
784                 z = 0;
785                 for (input=node->inputs.first; input; input=next, z++) {
786                         next = input->next;
787
788                         /* attributes don't need to be bound, they already have
789                          * an id that the drawing functions will use */
790                         if(input->source == GPU_SOURCE_ATTRIB ||
791                            input->source == GPU_SOURCE_BUILTIN)
792                                 continue;
793
794                         if (input->ima || input->tex)
795                                 snprintf(input->shadername, sizeof(input->shadername), "samp%d", input->texid);
796                         else
797                                 snprintf(input->shadername, sizeof(input->shadername), "unf%d", input->id);
798
799                         /* pass non-dynamic uniforms to opengl */
800                         extract = 0;
801
802                         if(input->ima || input->tex) {
803                                 if (input->bindtex)
804                                         extract = 1;
805                         }
806                         else if(input->dynamicvec)
807                                 extract = 1;
808
809                         if(extract)
810                                 input->shaderloc = GPU_shader_get_uniform(shader, input->shadername);
811
812                         /* extract nodes */
813                         if(extract) {
814                                 BLI_remlink(&node->inputs, input);
815                                 BLI_addtail(inputs, input);
816                         }
817                 }
818         }
819
820         GPU_shader_unbind(shader);
821 }
822
823 void GPU_pass_bind(GPUPass *pass, double time)
824 {
825         GPUInput *input;
826         GPUShader *shader = pass->shader;
827         ListBase *inputs = &pass->inputs;
828
829         if (!shader)
830                 return;
831
832         GPU_shader_bind(shader);
833
834         /* now bind the textures */
835         for (input=inputs->first; input; input=input->next) {
836                 if (input->ima)
837                         input->tex = GPU_texture_from_blender(input->ima, input->iuser, time);
838
839                 if(input->ima || input->tex) {
840                         if(input->tex) {
841                                 GPU_texture_bind(input->tex, input->texid);
842                                 GPU_shader_uniform_texture(shader, input->shaderloc, input->tex);
843                         }
844                 }
845         }
846 }
847
848 void GPU_pass_update_uniforms(GPUPass *pass)
849 {
850         GPUInput *input;
851         GPUShader *shader = pass->shader;
852         ListBase *inputs = &pass->inputs;
853
854         if (!shader)
855                 return;
856
857         /* pass dynamic inputs to opengl, others were removed */
858         for (input=inputs->first; input; input=input->next)
859                 if(!(input->ima || input->tex))
860                         GPU_shader_uniform_vector(shader, input->shaderloc, input->type, 1,
861                                 input->dynamicvec);
862 }
863
864 void GPU_pass_unbind(GPUPass *pass)
865 {
866         GPUInput *input;
867         GPUShader *shader = pass->shader;
868         ListBase *inputs = &pass->inputs;
869
870         if (!shader)
871                 return;
872
873         for (input=inputs->first; input; input=input->next) {
874                 if (input->tex)
875                         if(input->bindtex)
876                                 GPU_texture_unbind(input->tex);
877                 if (input->ima)
878                         input->tex = 0;
879         }
880         
881         GPU_shader_unbind(shader);
882 }
883
884 /* Node Link Functions */
885
886 GPUNodeLink *GPU_node_link_create(int type)
887 {
888         GPUNodeLink *link = MEM_callocN(sizeof(GPUNodeLink), "GPUNodeLink");
889         link->type = type;
890         link->users++;
891
892         return link;
893 }
894
895 void GPU_node_link_free(GPUNodeLink *link)
896 {
897         link->users--;
898
899         if (link->users < 0)
900                 fprintf(stderr, "GPU_node_link_free: negative refcount\n");
901         
902         if (link->users == 0) {
903                 if (link->output)
904                         link->output->link = NULL;
905                 MEM_freeN(link);
906         }
907 }
908
909 /* Node Functions */
910
911 GPUNode *GPU_node_begin(char *name)
912 {
913         GPUNode *node = MEM_callocN(sizeof(GPUNode), "GPUNode");
914
915         node->name = name;
916
917         return node;
918 }
919
920 void GPU_node_end(GPUNode *node)
921 {
922         /* empty */
923 }
924
925 static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, int type)
926 {
927         GPUInput *input;
928         GPUNode *outnode;
929         char *name;
930
931         if(link->output) {
932                 outnode = link->output->node;
933                 name = outnode->name;
934
935                 if(strcmp(name, "set_value")==0 || strcmp(name, "set_rgb")==0) {
936                         input = MEM_dupallocN(outnode->inputs.first);
937                         input->type = type;
938                         if(input->link)
939                                 input->link->users++;
940                         BLI_addtail(&node->inputs, input);
941                         return;
942                 }
943         }
944         
945         input = MEM_callocN(sizeof(GPUInput), "GPUInput");
946         input->node = node;
947
948         if(link->builtin) {
949                 /* builtin uniform */
950                 input->type = type;
951                 input->source = GPU_SOURCE_BUILTIN;
952                 input->builtin = link->builtin;
953
954                 MEM_freeN(link);
955         }
956         else if(link->output) {
957                 /* link to a node output */
958                 input->type = type;
959                 input->source = GPU_SOURCE_TEX_PIXEL;
960                 input->link = link;
961                 link->users++;
962         }
963         else if(link->dynamictex) {
964                 /* dynamic texture, GPUTexture is updated/deleted externally */
965                 input->type = type;
966                 input->source = GPU_SOURCE_TEX;
967
968                 input->tex = link->dynamictex;
969                 input->textarget = GL_TEXTURE_2D;
970                 input->textype = type;
971                 input->dynamictex = 1;
972                 MEM_freeN(link);
973         }
974         else if(link->texture) {
975                 /* small texture created on the fly, like for colorbands */
976                 input->type = GPU_VEC4;
977                 input->source = GPU_SOURCE_TEX;
978                 input->textype = type;
979
980                 if (type == GPU_TEX1D) {
981                         input->tex = GPU_texture_create_1D(link->texturesize, link->ptr1);
982                         input->textarget = GL_TEXTURE_1D;
983                 }
984                 else {
985                         input->tex = GPU_texture_create_2D(link->texturesize, link->texturesize, link->ptr2);
986                         input->textarget = GL_TEXTURE_2D;
987                 }
988
989                 MEM_freeN(link->ptr1);
990                 MEM_freeN(link);
991         }
992         else if(link->image) {
993                 /* blender image */
994                 input->type = GPU_VEC4;
995                 input->source = GPU_SOURCE_TEX;
996
997                 input->ima = link->ptr1;
998                 input->textarget = GL_TEXTURE_2D;
999                 input->textype = GPU_TEX2D;
1000                 MEM_freeN(link);
1001         }
1002         else if(link->attribtype) {
1003                 /* vertex attribute */
1004                 input->type = type;
1005                 input->source = GPU_SOURCE_ATTRIB;
1006
1007                 input->attribtype = link->attribtype;
1008                 BLI_strncpy(input->attribname, link->attribname, sizeof(input->attribname));
1009                 MEM_freeN(link);
1010         }
1011         else {
1012                 /* uniform vector */
1013                 input->type = type;
1014                 input->source = GPU_SOURCE_VEC_UNIFORM;
1015
1016                 memcpy(input->vec, link->ptr1, type*sizeof(float));
1017                 if(link->dynamic)
1018                         input->dynamicvec= link->ptr1;
1019                 MEM_freeN(link);
1020         }
1021
1022         BLI_addtail(&node->inputs, input);
1023 }
1024
1025 static void gpu_node_input_socket(GPUNode *node, GPUNodeStack *sock)
1026 {
1027         GPUNodeLink *link;
1028
1029         if(sock->link) {
1030                 gpu_node_input_link(node, sock->link, sock->type);
1031         }
1032         else {
1033                 link = GPU_node_link_create(0);
1034                 link->ptr1 = sock->vec;
1035                 gpu_node_input_link(node, link, sock->type);
1036         }
1037 }
1038
1039 void GPU_node_output(GPUNode *node, int type, char *name, GPUNodeLink **link)
1040 {
1041         GPUOutput *output = MEM_callocN(sizeof(GPUOutput), "GPUOutput");
1042
1043         output->type = type;
1044         output->node = node;
1045
1046         if (link) {
1047                 *link = output->link = GPU_node_link_create(type);
1048                 output->link->output = output;
1049
1050                 /* note: the caller owns the reference to the linkfer, GPUOutput
1051                    merely points to it, and if the node is destroyed it will
1052                    set that pointer to NULL */
1053         }
1054
1055         BLI_addtail(&node->outputs, output);
1056 }
1057
1058 void GPU_inputs_free(ListBase *inputs)
1059 {
1060         GPUInput *input;
1061
1062         for(input=inputs->first; input; input=input->next) {
1063                 if(input->link)
1064                         GPU_node_link_free(input->link);
1065                 else if(input->tex && !input->dynamictex)
1066                         GPU_texture_free(input->tex);
1067         }
1068
1069         BLI_freelistN(inputs);
1070 }
1071
1072 void GPU_node_free(GPUNode *node)
1073 {
1074         GPUOutput *output;
1075
1076         GPU_inputs_free(&node->inputs);
1077
1078         for (output=node->outputs.first; output; output=output->next)
1079                 if (output->link) {
1080                         output->link->output = NULL;
1081                         GPU_node_link_free(output->link);
1082                 }
1083
1084         BLI_freelistN(&node->outputs);
1085         MEM_freeN(node);
1086 }
1087
1088 void GPU_nodes_free(ListBase *nodes)
1089 {
1090         GPUNode *node;
1091
1092         while (nodes->first) {
1093                 node = nodes->first;
1094                 BLI_remlink(nodes, node);
1095                 GPU_node_free(node);
1096         }
1097 }
1098
1099 /* vertex attributes */
1100
1101 void gpu_nodes_get_vertex_attributes(ListBase *nodes, GPUVertexAttribs *attribs)
1102 {
1103         GPUNode *node;
1104         GPUInput *input;
1105         int a;
1106
1107         /* convert attributes requested by node inputs to an array of layers,
1108          * checking for duplicates and assigning id's starting from zero. */
1109
1110         memset(attribs, 0, sizeof(*attribs));
1111
1112         for(node=nodes->first; node; node=node->next) {
1113                 for(input=node->inputs.first; input; input=input->next) {
1114                         if(input->source == GPU_SOURCE_ATTRIB) {
1115                                 for(a=0; a<attribs->totlayer; a++) {
1116                                         if(attribs->layer[a].type == input->attribtype &&
1117                                                 strcmp(attribs->layer[a].name, input->attribname) == 0)
1118                                                 break;
1119                                 }
1120
1121                                 if(a == attribs->totlayer && a < GPU_MAX_ATTRIB) {
1122                                         input->attribid = attribs->totlayer++;
1123                                         input->attribfirst = 1;
1124
1125                                         attribs->layer[a].type = input->attribtype;
1126                                         attribs->layer[a].glindex = input->attribid;
1127                                         BLI_strncpy(attribs->layer[a].name, input->attribname,
1128                                                 sizeof(attribs->layer[a].name));
1129                                 }
1130                                 else
1131                                         input->attribid = attribs->layer[a].glindex;
1132                         }
1133                 }
1134         }
1135 }
1136
1137 void gpu_nodes_get_builtin_flag(ListBase *nodes, int *builtin)
1138 {
1139         GPUNode *node;
1140         GPUInput *input;
1141         
1142         *builtin= 0;
1143
1144         for(node=nodes->first; node; node=node->next)
1145                 for(input=node->inputs.first; input; input=input->next)
1146                         if(input->source == GPU_SOURCE_BUILTIN)
1147                                 *builtin |= input->builtin;
1148 }
1149
1150 /* varargs linking  */
1151
1152 GPUNodeLink *GPU_attribute(int type, char *name)
1153 {
1154         GPUNodeLink *link = GPU_node_link_create(0);
1155
1156         link->attribtype= type;
1157         link->attribname= name;
1158
1159         return link;
1160 }
1161
1162 GPUNodeLink *GPU_uniform(float *num)
1163 {
1164         GPUNodeLink *link = GPU_node_link_create(0);
1165
1166         link->ptr1= num;
1167         link->ptr2= NULL;
1168
1169         return link;
1170 }
1171
1172 GPUNodeLink *GPU_dynamic_uniform(float *num)
1173 {
1174         GPUNodeLink *link = GPU_node_link_create(0);
1175
1176         link->ptr1= num;
1177         link->ptr2= NULL;
1178         link->dynamic= 1;
1179
1180         return link;
1181 }
1182
1183 GPUNodeLink *GPU_image(Image *ima, ImageUser *iuser)
1184 {
1185         GPUNodeLink *link = GPU_node_link_create(0);
1186
1187         link->image= 1;
1188         link->ptr1= ima;
1189         link->ptr2= iuser;
1190
1191         return link;
1192 }
1193
1194 GPUNodeLink *GPU_texture(int size, float *pixels)
1195 {
1196         GPUNodeLink *link = GPU_node_link_create(0);
1197
1198         link->texture = 1;
1199         link->texturesize = size;
1200         link->ptr1= pixels;
1201
1202         return link;
1203 }
1204
1205 GPUNodeLink *GPU_dynamic_texture(GPUTexture *tex)
1206 {
1207         GPUNodeLink *link = GPU_node_link_create(0);
1208
1209         link->dynamic = 1;
1210         link->dynamictex = tex;
1211
1212         return link;
1213 }
1214
1215 GPUNodeLink *GPU_socket(GPUNodeStack *sock)
1216 {
1217         GPUNodeLink *link = GPU_node_link_create(0);
1218
1219         link->socket= sock;
1220
1221         return link;
1222 }
1223
1224 GPUNodeLink *GPU_builtin(GPUBuiltin builtin)
1225 {
1226         GPUNodeLink *link = GPU_node_link_create(0);
1227
1228         link->builtin= builtin;
1229
1230         return link;
1231 }
1232
1233 int GPU_link(GPUMaterial *mat, char *name, ...)
1234 {
1235         GPUNode *node;
1236         GPUFunction *function;
1237         GPUNodeLink *link, **linkptr;
1238         va_list params;
1239         int i;
1240
1241         function = GPU_lookup_function(name);
1242         if(!function) {
1243                 fprintf(stderr, "GPU failed to find function %s\n", name);
1244                 return 0;
1245         }
1246
1247         node = GPU_node_begin(name);
1248
1249         va_start(params, name);
1250         for(i=0; i<function->totparam; i++) {
1251                 if(function->paramqual[i] != FUNCTION_QUAL_IN) {
1252                         linkptr= va_arg(params, GPUNodeLink**);
1253                         GPU_node_output(node, function->paramtype[i], "", linkptr);
1254                 }
1255                 else {
1256                         link= va_arg(params, GPUNodeLink*);
1257                         gpu_node_input_link(node, link, function->paramtype[i]);
1258                 }
1259         }
1260         va_end(params);
1261
1262         GPU_node_end(node);
1263
1264         gpu_material_add_node(mat, node);
1265
1266         return 1;
1267 }
1268
1269 int GPU_stack_link(GPUMaterial *mat, char *name, GPUNodeStack *in, GPUNodeStack *out, ...)
1270 {
1271         GPUNode *node;
1272         GPUFunction *function;
1273         GPUNodeLink *link, **linkptr;
1274         va_list params;
1275         int i, totin, totout;
1276
1277         function = GPU_lookup_function(name);
1278         if(!function) {
1279                 fprintf(stderr, "GPU failed to find function %s\n", name);
1280                 return 0;
1281         }
1282
1283         node = GPU_node_begin(name);
1284         totin = 0;
1285         totout = 0;
1286
1287         if(in) {
1288                 for(i = 0; in[i].type != GPU_NONE; i++) {
1289                         gpu_node_input_socket(node, &in[i]);
1290                         totin++;
1291                 }
1292         }
1293         
1294         if(out) {
1295                 for(i = 0; out[i].type != GPU_NONE; i++) {
1296                         GPU_node_output(node, out[i].type, out[i].name, &out[i].link);
1297                         totout++;
1298                 }
1299         }
1300
1301         va_start(params, out);
1302         for(i=0; i<function->totparam; i++) {
1303                 if(function->paramqual[i] != FUNCTION_QUAL_IN) {
1304                         if(totout == 0) {
1305                                 linkptr= va_arg(params, GPUNodeLink**);
1306                                 GPU_node_output(node, function->paramtype[i], "", linkptr);
1307                         }
1308                         else
1309                                 totout--;
1310                 }
1311                 else {
1312                         if(totin == 0) {
1313                                 link= va_arg(params, GPUNodeLink*);
1314                                 if(link->socket)
1315                                         gpu_node_input_socket(node, link->socket);
1316                                 else
1317                                         gpu_node_input_link(node, link, function->paramtype[i]);
1318                         }
1319                         else
1320                                 totin--;
1321                 }
1322         }
1323         va_end(params);
1324
1325         GPU_node_end(node);
1326
1327         gpu_material_add_node(mat, node);
1328         
1329         return 1;
1330 }
1331
1332 int GPU_link_changed(GPUNodeLink *link)
1333 {
1334         GPUNode *node;
1335         GPUInput *input;
1336         char *name;
1337
1338         if(link->output) {
1339                 node = link->output->node;
1340                 name = node->name;
1341
1342                 if(strcmp(name, "set_value")==0 || strcmp(name, "set_rgb")==0) {
1343                         input = node->inputs.first;
1344                         return (input->link != NULL);
1345                 }
1346
1347                 return 1;
1348         }
1349         else
1350                 return 0;
1351 }
1352
1353 /* Pass create/free */
1354
1355 void gpu_nodes_tag(GPUNodeLink *link)
1356 {
1357         GPUNode *node;
1358         GPUInput *input;
1359
1360         if(!link->output)
1361                 return;
1362
1363         node = link->output->node;
1364         if(node->tag)
1365                 return;
1366         
1367         node->tag= 1;
1368         for(input=node->inputs.first; input; input=input->next)
1369                 if(input->link)
1370                         gpu_nodes_tag(input->link);
1371 }
1372
1373 void gpu_nodes_prune(ListBase *nodes, GPUNodeLink *outlink)
1374 {
1375         GPUNode *node, *next;
1376
1377         for(node=nodes->first; node; node=node->next)
1378                 node->tag= 0;
1379
1380         gpu_nodes_tag(outlink);
1381
1382         for(node=nodes->first; node; node=next) {
1383                 next = node->next;
1384
1385                 if(!node->tag) {
1386                         BLI_remlink(nodes, node);
1387                         GPU_node_free(node);
1388                 }
1389         }
1390 }
1391
1392 GPUPass *GPU_generate_pass(ListBase *nodes, GPUNodeLink *outlink, GPUVertexAttribs *attribs, int *builtins, const char *name)
1393 {
1394         GPUShader *shader;
1395         GPUPass *pass;
1396         char *vertexcode, *fragmentcode;
1397
1398         /*if(!FUNCTION_LIB) {
1399                 GPU_nodes_free(nodes);
1400                 return NULL;
1401         }*/
1402
1403         /* prune unused nodes */
1404         gpu_nodes_prune(nodes, outlink);
1405
1406         gpu_nodes_get_vertex_attributes(nodes, attribs);
1407         gpu_nodes_get_builtin_flag(nodes, builtins);
1408
1409         /* generate code and compile with opengl */
1410         fragmentcode = code_generate_fragment(nodes, outlink->output, name);
1411         vertexcode = code_generate_vertex(nodes);
1412         shader = GPU_shader_create(vertexcode, fragmentcode, datatoc_gpu_shader_material_glsl); /*FUNCTION_LIB);*/
1413         MEM_freeN(fragmentcode);
1414         MEM_freeN(vertexcode);
1415
1416         /* failed? */
1417         if (!shader) {
1418                 memset(attribs, 0, sizeof(*attribs));
1419                 memset(builtins, 0, sizeof(*builtins));
1420                 GPU_nodes_free(nodes);
1421                 return NULL;
1422         }
1423         
1424         /* create pass */
1425         pass = MEM_callocN(sizeof(GPUPass), "GPUPass");
1426
1427         pass->output = outlink->output;
1428         pass->shader = shader;
1429
1430         /* extract dynamic inputs and throw away nodes */
1431         GPU_nodes_extract_dynamic_inputs(pass, nodes);
1432         GPU_nodes_free(nodes);
1433
1434         return pass;
1435 }
1436
1437 void GPU_pass_free(GPUPass *pass)
1438 {
1439         GPU_shader_free(pass->shader);
1440         GPU_inputs_free(&pass->inputs);
1441         MEM_freeN(pass);
1442 }
1443