Merge branch 'blender2.7'
[blender.git] / source / blender / gpu / intern / gpu_codegen.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup gpu
22  *
23  * Convert material node-trees to GLSL.
24  */
25
26 #include "MEM_guardedalloc.h"
27
28 #include "DNA_customdata_types.h"
29 #include "DNA_image_types.h"
30 #include "DNA_material_types.h"
31 #include "DNA_node_types.h"
32
33 #include "BLI_blenlib.h"
34 #include "BLI_hash_mm2a.h"
35 #include "BLI_link_utils.h"
36 #include "BLI_utildefines.h"
37 #include "BLI_dynstr.h"
38 #include "BLI_ghash.h"
39 #include "BLI_threads.h"
40
41 #include "PIL_time.h"
42
43 #include "GPU_extensions.h"
44 #include "GPU_glew.h"
45 #include "GPU_material.h"
46 #include "GPU_shader.h"
47 #include "GPU_texture.h"
48 #include "GPU_uniformbuffer.h"
49
50 #include "BLI_sys_types.h" /* for intptr_t support */
51
52 #include "gpu_codegen.h"
53
54 #include <string.h>
55 #include <stdarg.h>
56
57 extern char datatoc_gpu_shader_material_glsl[];
58 extern char datatoc_gpu_shader_geometry_glsl[];
59
60 static char *glsl_material_library = NULL;
61
62 /* -------------------- GPUPass Cache ------------------ */
63 /**
64  * Internal shader cache: This prevent the shader recompilation / stall when
65  * using undo/redo AND also allows for GPUPass reuse if the Shader code is the
66  * same for 2 different Materials. Unused GPUPasses are free by Garbage collection.
67  */
68
69 /* Only use one linklist that contains the GPUPasses grouped by hash. */
70 static GPUPass *pass_cache = NULL;
71 static SpinLock pass_cache_spin;
72
73 static uint32_t gpu_pass_hash(const char *frag_gen, const char *defs, GPUVertAttrLayers *attrs)
74 {
75         BLI_HashMurmur2A hm2a;
76         BLI_hash_mm2a_init(&hm2a, 0);
77         BLI_hash_mm2a_add(&hm2a, (uchar *)frag_gen, strlen(frag_gen));
78         if (attrs) {
79                 for (int att_idx = 0; att_idx < attrs->totlayer; att_idx++) {
80                         char *name = attrs->layer[att_idx].name;
81                         BLI_hash_mm2a_add(&hm2a, (uchar *)name, strlen(name));
82                 }
83         }
84         if (defs)
85                 BLI_hash_mm2a_add(&hm2a, (uchar *)defs, strlen(defs));
86
87         return BLI_hash_mm2a_end(&hm2a);
88 }
89
90 /* Search by hash only. Return first pass with the same hash.
91  * There is hash collision if (pass->next && pass->next->hash == hash) */
92 static GPUPass *gpu_pass_cache_lookup(uint32_t hash)
93 {
94         BLI_spin_lock(&pass_cache_spin);
95         /* Could be optimized with a Lookup table. */
96         for (GPUPass *pass = pass_cache; pass; pass = pass->next) {
97                 if (pass->hash == hash) {
98                         BLI_spin_unlock(&pass_cache_spin);
99                         return pass;
100                 }
101         }
102         BLI_spin_unlock(&pass_cache_spin);
103         return NULL;
104 }
105
106 /* Check all possible passes with the same hash. */
107 static GPUPass *gpu_pass_cache_resolve_collision(
108         GPUPass *pass, const char *vert, const char *geom, const char *frag, const char *defs, uint32_t hash)
109 {
110         BLI_spin_lock(&pass_cache_spin);
111         /* Collision, need to strcmp the whole shader. */
112         for (; pass && (pass->hash == hash); pass = pass->next) {
113                 if ((defs != NULL) && (strcmp(pass->defines, defs) != 0)) { /* Pass */ }
114                 else if ((geom != NULL) && (strcmp(pass->geometrycode, geom) != 0)) { /* Pass */ }
115                 else if ((strcmp(pass->fragmentcode, frag) == 0) &&
116                          (strcmp(pass->vertexcode, vert) == 0))
117                 {
118                         BLI_spin_unlock(&pass_cache_spin);
119                         return pass;
120                 }
121         }
122         BLI_spin_unlock(&pass_cache_spin);
123         return NULL;
124 }
125
126 /* -------------------- GPU Codegen ------------------ */
127
128 /* type definitions and constants */
129
130 #define MAX_FUNCTION_NAME    64
131 #define MAX_PARAMETER        32
132
133 typedef enum {
134         FUNCTION_QUAL_IN,
135         FUNCTION_QUAL_OUT,
136         FUNCTION_QUAL_INOUT
137 } GPUFunctionQual;
138
139 typedef struct GPUFunction {
140         char name[MAX_FUNCTION_NAME];
141         eGPUType paramtype[MAX_PARAMETER];
142         GPUFunctionQual paramqual[MAX_PARAMETER];
143         int totparam;
144 } GPUFunction;
145
146 /* Indices match the eGPUType enum */
147 static const char *GPU_DATATYPE_STR[17] = {
148         "", "float", "vec2", "vec3", "vec4",
149         NULL, NULL, NULL, NULL, "mat3", NULL, NULL, NULL, NULL, NULL, NULL, "mat4",
150 };
151
152 /* GLSL code parsing for finding function definitions.
153  * These are stored in a hash for lookup when creating a material. */
154
155 static GHash *FUNCTION_HASH = NULL;
156 #if 0
157 static char *FUNCTION_PROTOTYPES = NULL;
158 static GPUShader *FUNCTION_LIB = NULL;
159 #endif
160
161 static int gpu_str_prefix(const char *str, const char *prefix)
162 {
163         while (*str && *prefix) {
164                 if (*str != *prefix)
165                         return 0;
166
167                 str++;
168                 prefix++;
169         }
170
171         return (*prefix == '\0');
172 }
173
174 static char *gpu_str_skip_token(char *str, char *token, int max)
175 {
176         int len = 0;
177
178         /* skip a variable/function name */
179         while (*str) {
180                 if (ELEM(*str, ' ', '(', ')', ',', ';', '\t', '\n', '\r'))
181                         break;
182                 else {
183                         if (token && len < max - 1) {
184                                 *token = *str;
185                                 token++;
186                                 len++;
187                         }
188                         str++;
189                 }
190         }
191
192         if (token)
193                 *token = '\0';
194
195         /* skip the next special characters:
196          * note the missing ')' */
197         while (*str) {
198                 if (ELEM(*str, ' ', '(', ',', ';', '\t', '\n', '\r'))
199                         str++;
200                 else
201                         break;
202         }
203
204         return str;
205 }
206
207 static void gpu_parse_functions_string(GHash *hash, char *code)
208 {
209         GPUFunction *function;
210         eGPUType type;
211         GPUFunctionQual qual;
212         int i;
213
214         while ((code = strstr(code, "void "))) {
215                 function = MEM_callocN(sizeof(GPUFunction), "GPUFunction");
216
217                 code = gpu_str_skip_token(code, NULL, 0);
218                 code = gpu_str_skip_token(code, function->name, MAX_FUNCTION_NAME);
219
220                 /* get parameters */
221                 while (*code && *code != ')') {
222                         /* test if it's an input or output */
223                         qual = FUNCTION_QUAL_IN;
224                         if (gpu_str_prefix(code, "out "))
225                                 qual = FUNCTION_QUAL_OUT;
226                         if (gpu_str_prefix(code, "inout "))
227                                 qual = FUNCTION_QUAL_INOUT;
228                         if ((qual != FUNCTION_QUAL_IN) || gpu_str_prefix(code, "in "))
229                                 code = gpu_str_skip_token(code, NULL, 0);
230
231                         /* test for type */
232                         type = GPU_NONE;
233                         for (i = 1; i < ARRAY_SIZE(GPU_DATATYPE_STR); i++) {
234                                 if (GPU_DATATYPE_STR[i] && gpu_str_prefix(code, GPU_DATATYPE_STR[i])) {
235                                         type = i;
236                                         break;
237                                 }
238                         }
239
240                         if (!type && gpu_str_prefix(code, "samplerCube")) {
241                                 type = GPU_TEXCUBE;
242                         }
243                         if (!type && gpu_str_prefix(code, "sampler2DShadow")) {
244                                 type = GPU_SHADOW2D;
245                         }
246                         if (!type && gpu_str_prefix(code, "sampler1DArray")) {
247                                 type = GPU_TEX1D_ARRAY;
248                         }
249                         if (!type && gpu_str_prefix(code, "sampler2D")) {
250                                 type = GPU_TEX2D;
251                         }
252                         if (!type && gpu_str_prefix(code, "sampler3D")) {
253                                 type = GPU_TEX3D;
254                         }
255
256                         if (!type && gpu_str_prefix(code, "Closure")) {
257                                 type = GPU_CLOSURE;
258                         }
259
260                         if (type) {
261                                 /* add parameter */
262                                 code = gpu_str_skip_token(code, NULL, 0);
263                                 code = gpu_str_skip_token(code, NULL, 0);
264                                 function->paramqual[function->totparam] = qual;
265                                 function->paramtype[function->totparam] = type;
266                                 function->totparam++;
267                         }
268                         else {
269                                 fprintf(stderr, "GPU invalid function parameter in %s.\n", function->name);
270                                 break;
271                         }
272                 }
273
274                 if (function->name[0] == '\0' || function->totparam == 0) {
275                         fprintf(stderr, "GPU functions parse error.\n");
276                         MEM_freeN(function);
277                         break;
278                 }
279
280                 BLI_ghash_insert(hash, function->name, function);
281         }
282 }
283
284 #if 0
285 static char *gpu_generate_function_prototyps(GHash *hash)
286 {
287         DynStr *ds = BLI_dynstr_new();
288         GHashIterator *ghi;
289         GPUFunction *function;
290         char *name, *prototypes;
291         int a;
292
293         /* automatically generate function prototypes to add to the top of the
294          * generated code, to avoid have to add the actual code & recompile all */
295         ghi = BLI_ghashIterator_new(hash);
296
297         for (; !BLI_ghashIterator_done(ghi); BLI_ghashIterator_step(ghi)) {
298                 name = BLI_ghashIterator_getValue(ghi);
299                 function = BLI_ghashIterator_getValue(ghi);
300
301                 BLI_dynstr_appendf(ds, "void %s(", name);
302                 for (a = 0; a < function->totparam; a++) {
303                         if (function->paramqual[a] == FUNCTION_QUAL_OUT)
304                                 BLI_dynstr_append(ds, "out ");
305                         else if (function->paramqual[a] == FUNCTION_QUAL_INOUT)
306                                 BLI_dynstr_append(ds, "inout ");
307
308                         if (function->paramtype[a] == GPU_TEX2D)
309                                 BLI_dynstr_append(ds, "sampler2D");
310                         else if (function->paramtype[a] == GPU_SHADOW2D)
311                                 BLI_dynstr_append(ds, "sampler2DShadow");
312                         else
313                                 BLI_dynstr_append(ds, GPU_DATATYPE_STR[function->paramtype[a]]);
314 #  if 0
315                         BLI_dynstr_appendf(ds, " param%d", a);
316 #  endif
317
318                         if (a != function->totparam - 1)
319                                 BLI_dynstr_append(ds, ", ");
320                 }
321                 BLI_dynstr_append(ds, ");\n");
322         }
323
324         BLI_dynstr_append(ds, "\n");
325
326         prototypes = BLI_dynstr_get_cstring(ds);
327         BLI_dynstr_free(ds);
328
329         return prototypes;
330 }
331 #endif
332
333 static GPUFunction *gpu_lookup_function(const char *name)
334 {
335         if (!FUNCTION_HASH) {
336                 FUNCTION_HASH = BLI_ghash_str_new("GPU_lookup_function gh");
337                 gpu_parse_functions_string(FUNCTION_HASH, glsl_material_library);
338         }
339
340         return BLI_ghash_lookup(FUNCTION_HASH, (const void *)name);
341 }
342
343 void gpu_codegen_init(void)
344 {
345         GPU_code_generate_glsl_lib();
346 }
347
348 void gpu_codegen_exit(void)
349 {
350         extern Material defmaterial; /* render module abuse... */
351
352         if (defmaterial.gpumaterial.first)
353                 GPU_material_free(&defmaterial.gpumaterial);
354
355         if (FUNCTION_HASH) {
356                 BLI_ghash_free(FUNCTION_HASH, NULL, MEM_freeN);
357                 FUNCTION_HASH = NULL;
358         }
359
360         GPU_shader_free_builtin_shaders();
361
362         if (glsl_material_library) {
363                 MEM_freeN(glsl_material_library);
364                 glsl_material_library = NULL;
365         }
366
367 #if 0
368         if (FUNCTION_PROTOTYPES) {
369                 MEM_freeN(FUNCTION_PROTOTYPES);
370                 FUNCTION_PROTOTYPES = NULL;
371         }
372         if (FUNCTION_LIB) {
373                 GPU_shader_free(FUNCTION_LIB);
374                 FUNCTION_LIB = NULL;
375         }
376 #endif
377 }
378
379 /* GLSL code generation */
380
381 static void codegen_convert_datatype(DynStr *ds, int from, int to, const char *tmp, int id)
382 {
383         char name[1024];
384
385         BLI_snprintf(name, sizeof(name), "%s%d", tmp, id);
386
387         if (from == to) {
388                 BLI_dynstr_append(ds, name);
389         }
390         else if (to == GPU_FLOAT) {
391                 if (from == GPU_VEC4)
392                         BLI_dynstr_appendf(ds, "convert_rgba_to_float(%s)", name);
393                 else if (from == GPU_VEC3)
394                         BLI_dynstr_appendf(ds, "(%s.r + %s.g + %s.b) / 3.0", name, name, name);
395                 else if (from == GPU_VEC2)
396                         BLI_dynstr_appendf(ds, "%s.r", name);
397         }
398         else if (to == GPU_VEC2) {
399                 if (from == GPU_VEC4)
400                         BLI_dynstr_appendf(ds, "vec2((%s.r + %s.g + %s.b) / 3.0, %s.a)", name, name, name, name);
401                 else if (from == GPU_VEC3)
402                         BLI_dynstr_appendf(ds, "vec2((%s.r + %s.g + %s.b) / 3.0, 1.0)", name, name, name);
403                 else if (from == GPU_FLOAT)
404                         BLI_dynstr_appendf(ds, "vec2(%s, 1.0)", name);
405         }
406         else if (to == GPU_VEC3) {
407                 if (from == GPU_VEC4)
408                         BLI_dynstr_appendf(ds, "%s.rgb", name);
409                 else if (from == GPU_VEC2)
410                         BLI_dynstr_appendf(ds, "vec3(%s.r, %s.r, %s.r)", name, name, name);
411                 else if (from == GPU_FLOAT)
412                         BLI_dynstr_appendf(ds, "vec3(%s, %s, %s)", name, name, name);
413         }
414         else if (to == GPU_VEC4) {
415                 if (from == GPU_VEC3)
416                         BLI_dynstr_appendf(ds, "vec4(%s, 1.0)", name);
417                 else if (from == GPU_VEC2)
418                         BLI_dynstr_appendf(ds, "vec4(%s.r, %s.r, %s.r, %s.g)", name, name, name, name);
419                 else if (from == GPU_FLOAT)
420                         BLI_dynstr_appendf(ds, "vec4(%s, %s, %s, 1.0)", name, name, name);
421         }
422         else if (to == GPU_CLOSURE) {
423                 if (from == GPU_VEC4)
424                         BLI_dynstr_appendf(ds, "closure_emission(%s.rgb)", name);
425                 else if (from == GPU_VEC3)
426                         BLI_dynstr_appendf(ds, "closure_emission(%s.rgb)", name);
427                 else if (from == GPU_VEC2)
428                         BLI_dynstr_appendf(ds, "closure_emission(%s.rrr)", name);
429                 else if (from == GPU_FLOAT)
430                         BLI_dynstr_appendf(ds, "closure_emission(vec3(%s, %s, %s))", name, name, name);
431         }
432         else {
433                 BLI_dynstr_append(ds, name);
434         }
435 }
436
437 static void codegen_print_datatype(DynStr *ds, const eGPUType type, float *data)
438 {
439         int i;
440
441         BLI_dynstr_appendf(ds, "%s(", GPU_DATATYPE_STR[type]);
442
443         for (i = 0; i < type; i++) {
444                 BLI_dynstr_appendf(ds, "%.12f", data[i]);
445                 if (i == type - 1)
446                         BLI_dynstr_append(ds, ")");
447                 else
448                         BLI_dynstr_append(ds, ", ");
449         }
450 }
451
452 static int codegen_input_has_texture(GPUInput *input)
453 {
454         if (input->link)
455                 return 0;
456         else
457                 return (input->source == GPU_SOURCE_TEX);
458 }
459
460 const char *GPU_builtin_name(eGPUBuiltin builtin)
461 {
462         if (builtin == GPU_VIEW_MATRIX)
463                 return "unfviewmat";
464         else if (builtin == GPU_OBJECT_MATRIX)
465                 return "unfobmat";
466         else if (builtin == GPU_INVERSE_VIEW_MATRIX)
467                 return "unfinvviewmat";
468         else if (builtin == GPU_INVERSE_OBJECT_MATRIX)
469                 return "unfinvobmat";
470         else if (builtin == GPU_INVERSE_NORMAL_MATRIX)
471                 return "unfinvnormat";
472         else if (builtin == GPU_LOC_TO_VIEW_MATRIX)
473                 return "unflocaltoviewmat";
474         else if (builtin == GPU_INVERSE_LOC_TO_VIEW_MATRIX)
475                 return "unfinvlocaltoviewmat";
476         else if (builtin == GPU_VIEW_POSITION)
477                 return "varposition";
478         else if (builtin == GPU_VIEW_NORMAL)
479                 return "varnormal";
480         else if (builtin == GPU_OBCOLOR)
481                 return "unfobcolor";
482         else if (builtin == GPU_AUTO_BUMPSCALE)
483                 return "unfobautobumpscale";
484         else if (builtin == GPU_CAMERA_TEXCO_FACTORS)
485                 return "unfcameratexfactors";
486         else if (builtin == GPU_PARTICLE_SCALAR_PROPS)
487                 return "unfparticlescalarprops";
488         else if (builtin == GPU_PARTICLE_LOCATION)
489                 return "unfparticleco";
490         else if (builtin == GPU_PARTICLE_VELOCITY)
491                 return "unfparticlevel";
492         else if (builtin == GPU_PARTICLE_ANG_VELOCITY)
493                 return "unfparticleangvel";
494         else if (builtin == GPU_OBJECT_INFO)
495                 return "unfobjectinfo";
496         else if (builtin == GPU_VOLUME_DENSITY)
497                 return "sampdensity";
498         else if (builtin == GPU_VOLUME_FLAME)
499                 return "sampflame";
500         else if (builtin == GPU_VOLUME_TEMPERATURE)
501                 return "unftemperature";
502         else if (builtin == GPU_BARYCENTRIC_TEXCO)
503                 return "unfbarycentrictex";
504         else if (builtin == GPU_BARYCENTRIC_DIST)
505                 return "unfbarycentricdist";
506         else
507                 return "";
508 }
509
510 /* assign only one texid per buffer to avoid sampling the same texture twice */
511 static void codegen_set_texid(GHash *bindhash, GPUInput *input, int *texid, void *key)
512 {
513         if (BLI_ghash_haskey(bindhash, key)) {
514                 /* Reuse existing texid */
515                 input->texid = POINTER_AS_INT(BLI_ghash_lookup(bindhash, key));
516         }
517         else {
518                 /* Allocate new texid */
519                 input->texid = *texid;
520                 (*texid)++;
521                 input->bindtex = true;
522                 BLI_ghash_insert(bindhash, key, POINTER_FROM_INT(input->texid));
523         }
524 }
525
526 static void codegen_set_unique_ids(ListBase *nodes)
527 {
528         GHash *bindhash;
529         GPUNode *node;
530         GPUInput *input;
531         GPUOutput *output;
532         int id = 1, texid = 0;
533
534         bindhash = BLI_ghash_ptr_new("codegen_set_unique_ids1 gh");
535
536         for (node = nodes->first; node; node = node->next) {
537                 for (input = node->inputs.first; input; input = input->next) {
538                         /* set id for unique names of uniform variables */
539                         input->id = id++;
540
541                         /* set texid used for settings texture slot */
542                         if (codegen_input_has_texture(input)) {
543                                 input->bindtex = false;
544                                 if (input->ima) {
545                                         /* input is texture from image */
546                                         codegen_set_texid(bindhash, input, &texid, input->ima);
547                                 }
548                                 else if (input->coba) {
549                                         /* input is color band texture, check coba pointer */
550                                         codegen_set_texid(bindhash, input, &texid, input->coba);
551                                 }
552                                 else {
553                                         /* Either input->ima or input->coba should be non-NULL. */
554                                         BLI_assert(0);
555                                 }
556                         }
557                 }
558
559                 for (output = node->outputs.first; output; output = output->next) {
560                         /* set id for unique names of tmp variables storing output */
561                         output->id = id++;
562                 }
563         }
564
565         BLI_ghash_free(bindhash, NULL, NULL);
566 }
567
568 /**
569  * It will create an UBO for GPUMaterial if there is any GPU_DYNAMIC_UBO.
570  */
571 static int codegen_process_uniforms_functions(GPUMaterial *material, DynStr *ds, ListBase *nodes)
572 {
573         GPUNode *node;
574         GPUInput *input;
575         const char *name;
576         int builtins = 0;
577         ListBase ubo_inputs = {NULL, NULL};
578
579         /* print uniforms */
580         for (node = nodes->first; node; node = node->next) {
581                 for (input = node->inputs.first; input; input = input->next) {
582                         if (input->source == GPU_SOURCE_TEX) {
583                                 /* create exactly one sampler for each texture */
584                                 if (codegen_input_has_texture(input) && input->bindtex) {
585                                         BLI_dynstr_appendf(
586                                                 ds, "uniform %s samp%d;\n",
587                                                 (input->coba) ? "sampler1DArray" : "sampler2D",
588                                                 input->texid);
589                                 }
590                         }
591                         else if (input->source == GPU_SOURCE_BUILTIN) {
592                                 /* only define each builtin uniform/varying once */
593                                 if (!(builtins & input->builtin)) {
594                                         builtins |= input->builtin;
595                                         name = GPU_builtin_name(input->builtin);
596
597                                         if (gpu_str_prefix(name, "samp")) {
598                                                 if ((input->builtin == GPU_VOLUME_DENSITY) ||
599                                                     (input->builtin == GPU_VOLUME_FLAME))
600                                                 {
601                                                         BLI_dynstr_appendf(ds, "uniform sampler3D %s;\n", name);
602                                                 }
603                                         }
604                                         else if (gpu_str_prefix(name, "unf")) {
605                                                 BLI_dynstr_appendf(
606                                                         ds, "uniform %s %s;\n",
607                                                         GPU_DATATYPE_STR[input->type], name);
608                                         }
609                                         else {
610                                                 BLI_dynstr_appendf(
611                                                         ds, "in %s %s;\n",
612                                                         GPU_DATATYPE_STR[input->type], name);
613                                         }
614                                 }
615                         }
616                         else if (input->source == GPU_SOURCE_STRUCT) {
617                                 /* Add other struct here if needed. */
618                                 BLI_dynstr_appendf(ds, "Closure strct%d = CLOSURE_DEFAULT;\n", input->id);
619                         }
620                         else if (input->source == GPU_SOURCE_UNIFORM) {
621                                 if (!input->link) {
622                                         /* We handle the UBOuniforms separately. */
623                                         BLI_addtail(&ubo_inputs, BLI_genericNodeN(input));
624                                 }
625                         }
626                         else if (input->source == GPU_SOURCE_CONSTANT) {
627                                 BLI_dynstr_appendf(
628                                         ds, "const %s cons%d = ",
629                                         GPU_DATATYPE_STR[input->type], input->id);
630                                 codegen_print_datatype(ds, input->type, input->vec);
631                                 BLI_dynstr_append(ds, ";\n");
632                         }
633                         else if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
634                                 BLI_dynstr_appendf(
635                                         ds, "in %s var%d;\n",
636                                         GPU_DATATYPE_STR[input->type], input->attr_id);
637                         }
638                 }
639         }
640
641         /* Handle the UBO block separately. */
642         if ((material != NULL) && !BLI_listbase_is_empty(&ubo_inputs)) {
643                 GPU_material_uniform_buffer_create(material, &ubo_inputs);
644
645                 /* Inputs are sorted */
646                 BLI_dynstr_appendf(ds, "\nlayout (std140) uniform %s {\n", GPU_UBO_BLOCK_NAME);
647
648                 for (LinkData *link = ubo_inputs.first; link; link = link->next) {
649                         input = link->data;
650                         BLI_dynstr_appendf(
651                                 ds, "\t%s unf%d;\n",
652                                 GPU_DATATYPE_STR[input->type], input->id);
653                 }
654                 BLI_dynstr_append(ds, "};\n");
655                 BLI_freelistN(&ubo_inputs);
656         }
657
658         BLI_dynstr_append(ds, "\n");
659
660         return builtins;
661 }
662
663 static void codegen_declare_tmps(DynStr *ds, ListBase *nodes)
664 {
665         GPUNode *node;
666         GPUOutput *output;
667
668         for (node = nodes->first; node; node = node->next) {
669                 /* declare temporary variables for node output storage */
670                 for (output = node->outputs.first; output; output = output->next) {
671                         if (output->type == GPU_CLOSURE) {
672                                 BLI_dynstr_appendf(
673                                         ds, "\tClosure tmp%d;\n", output->id);
674                         }
675                         else {
676                                 BLI_dynstr_appendf(
677                                         ds, "\t%s tmp%d;\n",
678                                         GPU_DATATYPE_STR[output->type], output->id);
679                         }
680                 }
681         }
682
683         BLI_dynstr_append(ds, "\n");
684 }
685
686 static void codegen_call_functions(DynStr *ds, ListBase *nodes, GPUOutput *finaloutput)
687 {
688         GPUNode *node;
689         GPUInput *input;
690         GPUOutput *output;
691
692         for (node = nodes->first; node; node = node->next) {
693                 BLI_dynstr_appendf(ds, "\t%s(", node->name);
694
695                 for (input = node->inputs.first; input; input = input->next) {
696                         if (input->source == GPU_SOURCE_TEX) {
697                                 BLI_dynstr_appendf(ds, "samp%d", input->texid);
698                         }
699                         else if (input->source == GPU_SOURCE_OUTPUT) {
700                                 codegen_convert_datatype(
701                                         ds, input->link->output->type, input->type,
702                                         "tmp", input->link->output->id);
703                         }
704                         else if (input->source == GPU_SOURCE_BUILTIN) {
705                                 /* TODO(fclem) get rid of that. */
706                                 if (input->builtin == GPU_INVERSE_VIEW_MATRIX)
707                                         BLI_dynstr_append(ds, "viewinv");
708                                 else if (input->builtin == GPU_VIEW_MATRIX)
709                                         BLI_dynstr_append(ds, "viewmat");
710                                 else if (input->builtin == GPU_CAMERA_TEXCO_FACTORS)
711                                         BLI_dynstr_append(ds, "camtexfac");
712                                 else if (input->builtin == GPU_LOC_TO_VIEW_MATRIX)
713                                         BLI_dynstr_append(ds, "localtoviewmat");
714                                 else if (input->builtin == GPU_INVERSE_LOC_TO_VIEW_MATRIX)
715                                         BLI_dynstr_append(ds, "invlocaltoviewmat");
716                                 else if (input->builtin == GPU_BARYCENTRIC_DIST)
717                                         BLI_dynstr_append(ds, "barycentricDist");
718                                 else if (input->builtin == GPU_BARYCENTRIC_TEXCO)
719                                         BLI_dynstr_append(ds, "barytexco");
720                                 else if (input->builtin == GPU_OBJECT_MATRIX)
721                                         BLI_dynstr_append(ds, "objmat");
722                                 else if (input->builtin == GPU_INVERSE_OBJECT_MATRIX)
723                                         BLI_dynstr_append(ds, "objinv");
724                                 else if (input->builtin == GPU_INVERSE_NORMAL_MATRIX)
725                                         BLI_dynstr_append(ds, "norinv");
726                                 else if (input->builtin == GPU_VIEW_POSITION)
727                                         BLI_dynstr_append(ds, "viewposition");
728                                 else if (input->builtin == GPU_VIEW_NORMAL)
729                                         BLI_dynstr_append(ds, "facingnormal");
730                                 else
731                                         BLI_dynstr_append(ds, GPU_builtin_name(input->builtin));
732                         }
733                         else if (input->source == GPU_SOURCE_STRUCT) {
734                                 BLI_dynstr_appendf(ds, "strct%d", input->id);
735                         }
736                         else if (input->source == GPU_SOURCE_UNIFORM) {
737                                 BLI_dynstr_appendf(ds, "unf%d", input->id);
738                         }
739                         else if (input->source == GPU_SOURCE_CONSTANT) {
740                                 BLI_dynstr_appendf(ds, "cons%d", input->id);
741                         }
742                         else if (input->source == GPU_SOURCE_ATTR) {
743                                 BLI_dynstr_appendf(ds, "var%d", input->attr_id);
744                         }
745
746                         BLI_dynstr_append(ds, ", ");
747                 }
748
749                 for (output = node->outputs.first; output; output = output->next) {
750                         BLI_dynstr_appendf(ds, "tmp%d", output->id);
751                         if (output->next)
752                                 BLI_dynstr_append(ds, ", ");
753                 }
754
755                 BLI_dynstr_append(ds, ");\n");
756         }
757
758         BLI_dynstr_appendf(ds, "\n\treturn tmp%d", finaloutput->id);
759         BLI_dynstr_append(ds, ";\n");
760 }
761
762 static char *code_generate_fragment(GPUMaterial *material, ListBase *nodes, GPUOutput *output, int *rbuiltins)
763 {
764         DynStr *ds = BLI_dynstr_new();
765         char *code;
766         int builtins;
767
768 #if 0
769         BLI_dynstr_append(ds, FUNCTION_PROTOTYPES);
770 #endif
771
772         codegen_set_unique_ids(nodes);
773         *rbuiltins = builtins = codegen_process_uniforms_functions(material, ds, nodes);
774
775         if (builtins & GPU_BARYCENTRIC_TEXCO)
776                 BLI_dynstr_append(ds, "in vec2 barycentricTexCo;\n");
777
778         if (builtins & GPU_BARYCENTRIC_DIST)
779                 BLI_dynstr_append(ds, "flat in vec3 barycentricDist;\n");
780
781         BLI_dynstr_append(ds, "Closure nodetree_exec(void)\n{\n");
782
783         if (builtins & GPU_BARYCENTRIC_TEXCO) {
784                 BLI_dynstr_append(ds, "#ifdef HAIR_SHADER\n");
785                 BLI_dynstr_append(ds, "\tvec2 barytexco = vec2((fract(barycentricTexCo.y) != 0.0)\n"
786                                       "\t                      ? barycentricTexCo.x\n"
787                                       "\t                      : 1.0 - barycentricTexCo.x,\n"
788                                       "\t                      0.0);\n");
789                 BLI_dynstr_append(ds, "#else\n");
790                 BLI_dynstr_append(ds, "\tvec2 barytexco = barycentricTexCo;\n");
791                 BLI_dynstr_append(ds, "#endif\n");
792         }
793         /* TODO(fclem) get rid of that. */
794         if (builtins & GPU_VIEW_MATRIX)
795                 BLI_dynstr_append(ds, "\t#define viewmat ViewMatrix\n");
796         if (builtins & GPU_CAMERA_TEXCO_FACTORS)
797                 BLI_dynstr_append(ds, "\t#define camtexfac CameraTexCoFactors\n");
798         if (builtins & GPU_OBJECT_MATRIX)
799                 BLI_dynstr_append(ds, "\t#define objmat ModelMatrix\n");
800         if (builtins & GPU_INVERSE_OBJECT_MATRIX)
801                 BLI_dynstr_append(ds, "\t#define objinv ModelMatrixInverse\n");
802         if (builtins & GPU_INVERSE_NORMAL_MATRIX)
803                 BLI_dynstr_append(ds, "\t#define norinv NormalMatrixInverse\n");
804         if (builtins & GPU_INVERSE_VIEW_MATRIX)
805                 BLI_dynstr_append(ds, "\t#define viewinv ViewMatrixInverse\n");
806         if (builtins & GPU_LOC_TO_VIEW_MATRIX)
807                 BLI_dynstr_append(ds, "\t#define localtoviewmat ModelViewMatrix\n");
808         if (builtins & GPU_INVERSE_LOC_TO_VIEW_MATRIX)
809                 BLI_dynstr_append(ds, "\t#define invlocaltoviewmat ModelViewMatrixInverse\n");
810         if (builtins & GPU_VIEW_NORMAL)
811                 BLI_dynstr_append(ds, "\tvec3 facingnormal = gl_FrontFacing? viewNormal: -viewNormal;\n");
812         if (builtins & GPU_VIEW_POSITION)
813                 BLI_dynstr_append(ds, "\t#define viewposition viewPosition\n");
814
815         codegen_declare_tmps(ds, nodes);
816         codegen_call_functions(ds, nodes, output);
817
818         BLI_dynstr_append(ds, "}\n");
819
820         /* XXX This cannot go into gpu_shader_material.glsl because main() would be parsed and generate error */
821         /* Old glsl mode compat. */
822         BLI_dynstr_append(ds, "#ifndef NODETREE_EXEC\n");
823         BLI_dynstr_append(ds, "out vec4 fragColor;\n");
824         BLI_dynstr_append(ds, "void main()\n");
825         BLI_dynstr_append(ds, "{\n");
826         BLI_dynstr_append(ds, "\tClosure cl = nodetree_exec();\n");
827         BLI_dynstr_append(ds, "\tfragColor = vec4(cl.radiance, cl.opacity);\n");
828         BLI_dynstr_append(ds, "}\n");
829         BLI_dynstr_append(ds, "#endif\n\n");
830
831         /* create shader */
832         code = BLI_dynstr_get_cstring(ds);
833         BLI_dynstr_free(ds);
834
835 #if 0
836         if (G.debug & G_DEBUG) printf("%s\n", code);
837 #endif
838
839         return code;
840 }
841
842 static const char *attr_prefix_get(CustomDataType type)
843 {
844         switch (type) {
845                 case CD_ORCO:           return "orco";
846                 case CD_MTFACE:         return "u";
847                 case CD_TANGENT:        return "t";
848                 case CD_MCOL:           return "c";
849                 case CD_AUTO_FROM_NAME: return "a";
850                 default: BLI_assert(false && "GPUVertAttr Prefix type not found : This should not happen!"); return "";
851         }
852 }
853
854 static char *code_generate_vertex(ListBase *nodes, const char *vert_code, bool use_geom)
855 {
856         DynStr *ds = BLI_dynstr_new();
857         GPUNode *node;
858         GPUInput *input;
859         char *code;
860         int builtins = 0;
861
862         /* Hairs uv and col attributes are passed by bufferTextures. */
863         BLI_dynstr_append(
864                 ds,
865                 "#ifdef HAIR_SHADER\n"
866                 "#define DEFINE_ATTR(type, attr) uniform samplerBuffer attr\n"
867                 "#else\n"
868                 "#define DEFINE_ATTR(type, attr) in type attr\n"
869                 "#endif\n"
870         );
871
872         for (node = nodes->first; node; node = node->next) {
873                 for (input = node->inputs.first; input; input = input->next) {
874                         if (input->source == GPU_SOURCE_BUILTIN) {
875                                 builtins |= input->builtin;
876                         }
877                         if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
878                                 /* XXX FIXME : see notes in mesh_render_data_create() */
879                                 /* NOTE : Replicate changes to mesh_render_data_create() in draw_cache_impl_mesh.c */
880                                 if (input->attr_type == CD_ORCO) {
881                                         /* OPTI : orco is computed from local positions, but only if no modifier is present. */
882                                         BLI_dynstr_append(ds, "uniform vec3 OrcoTexCoFactors[2];\n");
883                                         BLI_dynstr_append(ds, "DEFINE_ATTR(vec4, orco);\n");
884                                 }
885                                 else if (input->attr_name[0] == '\0') {
886                                         BLI_dynstr_appendf(ds, "DEFINE_ATTR(%s, %s);\n", GPU_DATATYPE_STR[input->type], attr_prefix_get(input->attr_type));
887                                         BLI_dynstr_appendf(ds, "#define att%d %s\n", input->attr_id, attr_prefix_get(input->attr_type));
888                                 }
889                                 else {
890                                         uint hash = BLI_ghashutil_strhash_p(input->attr_name);
891                                         BLI_dynstr_appendf(
892                                                 ds, "DEFINE_ATTR(%s, %s%u);\n",
893                                                 GPU_DATATYPE_STR[input->type], attr_prefix_get(input->attr_type), hash);
894                                         BLI_dynstr_appendf(
895                                                 ds, "#define att%d %s%u\n",
896                                                 input->attr_id, attr_prefix_get(input->attr_type), hash);
897                                         /* Auto attribute can be vertex color byte buffer.
898                                          * We need to know and convert them to linear space in VS. */
899                                         if (input->attr_type == CD_AUTO_FROM_NAME) {
900                                                 BLI_dynstr_appendf(ds, "uniform bool ba%u;\n", hash);
901                                                 BLI_dynstr_appendf(ds, "#define att%d_is_srgb ba%u\n", input->attr_id, hash);
902                                         }
903                                 }
904                                 BLI_dynstr_appendf(
905                                         ds, "out %s var%d%s;\n",
906                                         GPU_DATATYPE_STR[input->type], input->attr_id, use_geom ? "g" : "");
907                         }
908                 }
909         }
910
911         if (builtins & GPU_BARYCENTRIC_TEXCO) {
912                 BLI_dynstr_append(ds, "#ifdef HAIR_SHADER\n");
913                 BLI_dynstr_appendf(
914                         ds, "out vec2 barycentricTexCo%s;\n",
915                         use_geom ? "g" : "");
916                 BLI_dynstr_append(ds, "#endif\n");
917         }
918
919         if (builtins & GPU_BARYCENTRIC_DIST) {
920                 BLI_dynstr_append(ds, "out vec3 barycentricPosg;\n");
921         }
922
923
924         BLI_dynstr_append(ds, "\n");
925
926         BLI_dynstr_append(
927                 ds,
928                 "#define USE_ATTR\n"
929                 "uniform mat3 NormalMatrix;\n"
930                 "uniform mat4 ModelMatrixInverse;\n"
931                 "uniform mat4 ModelMatrix;\n"
932                 "vec3 srgb_to_linear_attr(vec3 c) {\n"
933                 "\tc = max(c, vec3(0.0));\n"
934                 "\tvec3 c1 = c * (1.0 / 12.92);\n"
935                 "\tvec3 c2 = pow((c + 0.055) * (1.0 / 1.055), vec3(2.4));\n"
936                 "\treturn mix(c1, c2, step(vec3(0.04045), c));\n"
937                 "}\n\n"
938         );
939
940         /* Prototype because defined later. */
941         BLI_dynstr_append(
942                 ds,
943                 "vec2 hair_get_customdata_vec2(const samplerBuffer);\n"
944                 "vec3 hair_get_customdata_vec3(const samplerBuffer);\n"
945                 "vec4 hair_get_customdata_vec4(const samplerBuffer);\n"
946                 "vec3 hair_get_strand_pos(void);\n"
947                 "int hair_get_base_id(void);\n"
948                 "\n"
949         );
950
951         BLI_dynstr_append(ds, "void pass_attr(in vec3 position) {\n");
952
953         BLI_dynstr_append(ds, "#ifdef HAIR_SHADER\n");
954
955         if (builtins & GPU_BARYCENTRIC_TEXCO) {
956                 /* To match cycles without breaking into individual segment we encode if we need to invert
957                  * the first component into the second component. We invert if the barycentricTexCo.y
958                  * is NOT 0.0 or 1.0. */
959                 BLI_dynstr_append(
960                         ds, "\tint _base_id = hair_get_base_id();\n");
961                 BLI_dynstr_appendf(
962                         ds, "\tbarycentricTexCo%s.x = float((_base_id %% 2) == 1);\n",
963                         use_geom ? "g" : "");
964                 BLI_dynstr_appendf(
965                         ds, "\tbarycentricTexCo%s.y = float(((_base_id %% 4) %% 3) > 0);\n",
966                         use_geom ? "g" : "");
967         }
968
969         if (builtins & GPU_BARYCENTRIC_DIST) {
970                 BLI_dynstr_append(ds, "\tbarycentricPosg = position;\n");
971         }
972
973         for (node = nodes->first; node; node = node->next) {
974                 for (input = node->inputs.first; input; input = input->next) {
975                         if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
976                                 if (input->attr_type == CD_TANGENT) {
977                                         /* Not supported by hairs */
978                                         BLI_dynstr_appendf(
979                                                 ds, "\tvar%d%s = vec4(0.0);\n",
980                                                 input->attr_id, use_geom ? "g" : "");
981                                 }
982                                 else if (input->attr_type == CD_ORCO) {
983                                         BLI_dynstr_appendf(
984                                                 ds, "\tvar%d%s = OrcoTexCoFactors[0] + (ModelMatrixInverse * vec4(hair_get_strand_pos(), 1.0)).xyz * OrcoTexCoFactors[1];\n",
985                                                 input->attr_id, use_geom ? "g" : "");
986                                         /* TODO: fix ORCO with modifiers. */
987                                 }
988                                 else {
989                                         BLI_dynstr_appendf(
990                                                 ds, "\tvar%d%s = hair_get_customdata_%s(att%d);\n",
991                                                 input->attr_id, use_geom ? "g" : "", GPU_DATATYPE_STR[input->type], input->attr_id);
992                                 }
993                         }
994                 }
995         }
996
997         BLI_dynstr_append(ds, "#else /* MESH_SHADER */\n");
998
999         /* GPU_BARYCENTRIC_TEXCO cannot be computed based on gl_VertexID
1000          * for MESH_SHADER because of indexed drawing. In this case a
1001          * geometry shader is needed. */
1002
1003         if (builtins & GPU_BARYCENTRIC_DIST) {
1004                 BLI_dynstr_append(ds, "\tbarycentricPosg = (ModelMatrix * vec4(position, 1.0)).xyz;\n");
1005         }
1006
1007         for (node = nodes->first; node; node = node->next) {
1008                 for (input = node->inputs.first; input; input = input->next) {
1009                         if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
1010                                 if (input->attr_type == CD_TANGENT) { /* silly exception */
1011                                         BLI_dynstr_appendf(
1012                                                 ds, "\tvar%d%s.xyz = NormalMatrix * att%d.xyz;\n",
1013                                                 input->attr_id, use_geom ? "g" : "", input->attr_id);
1014                                         BLI_dynstr_appendf(
1015                                                 ds, "\tvar%d%s.w = att%d.w;\n",
1016                                                 input->attr_id, use_geom ? "g" : "", input->attr_id);
1017                                         /* Normalize only if vector is not null. */
1018                                         BLI_dynstr_appendf(
1019                                                 ds, "\tfloat lvar%d = dot(var%d%s.xyz, var%d%s.xyz);\n",
1020                                                 input->attr_id, input->attr_id, use_geom ? "g" : "", input->attr_id, use_geom ? "g" : "");
1021                                         BLI_dynstr_appendf(
1022                                                 ds, "\tvar%d%s.xyz *= (lvar%d > 0.0) ? inversesqrt(lvar%d) : 1.0;\n",
1023                                                 input->attr_id, use_geom ? "g" : "", input->attr_id, input->attr_id);
1024                                 }
1025                                 else if (input->attr_type == CD_ORCO) {
1026                                         BLI_dynstr_appendf(
1027                                                 ds, "\tvar%d%s = OrcoTexCoFactors[0] + position * OrcoTexCoFactors[1];\n",
1028                                                 input->attr_id, use_geom ? "g" : "");
1029                                         /* See mesh_create_loop_orco() for explanation. */
1030                                         BLI_dynstr_appendf(
1031                                                 ds, "\tif (orco.w == 0.0) { var%d%s = orco.xyz * 0.5 + 0.5; }\n",
1032                                                 input->attr_id, use_geom ? "g" : "");
1033                                 }
1034                                 else if (input->attr_type == CD_MCOL) {
1035                                         BLI_dynstr_appendf(
1036                                                 ds, "\tvar%d%s = srgb_to_linear_attr(att%d);\n",
1037                                                 input->attr_id, use_geom ? "g" : "", input->attr_id);
1038                                 }
1039                                 else if (input->attr_type == CD_AUTO_FROM_NAME) {
1040                                         BLI_dynstr_appendf(
1041                                                 ds, "\tvar%d%s = (att%d_is_srgb) ? srgb_to_linear_attr(att%d) : att%d;\n",
1042                                                 input->attr_id, use_geom ? "g" : "",
1043                                                 input->attr_id, input->attr_id, input->attr_id);
1044                                 }
1045                                 else {
1046                                         BLI_dynstr_appendf(
1047                                                 ds, "\tvar%d%s = att%d;\n",
1048                                                 input->attr_id, use_geom ? "g" : "", input->attr_id);
1049                                 }
1050                         }
1051                 }
1052         }
1053         BLI_dynstr_append(ds, "#endif /* HAIR_SHADER */\n");
1054
1055         BLI_dynstr_append(ds, "}\n");
1056
1057         if (use_geom) {
1058                 /* XXX HACK: Eevee specific. */
1059                 char *vert_new, *vert_new2;
1060                 vert_new = BLI_str_replaceN(vert_code, "worldPosition", "worldPositiong");
1061                 vert_new2 = vert_new;
1062                 vert_new = BLI_str_replaceN(vert_new2, "viewPosition", "viewPositiong");
1063                 MEM_freeN(vert_new2);
1064                 vert_new2 = vert_new;
1065                 vert_new = BLI_str_replaceN(vert_new2, "worldNormal", "worldNormalg");
1066                 MEM_freeN(vert_new2);
1067                 vert_new2 = vert_new;
1068                 vert_new = BLI_str_replaceN(vert_new2, "viewNormal", "viewNormalg");
1069                 MEM_freeN(vert_new2);
1070
1071                 BLI_dynstr_append(ds, vert_new);
1072
1073                 MEM_freeN(vert_new);
1074         }
1075         else {
1076                 BLI_dynstr_append(ds, vert_code);
1077         }
1078
1079         code = BLI_dynstr_get_cstring(ds);
1080
1081         BLI_dynstr_free(ds);
1082
1083 #if 0
1084         if (G.debug & G_DEBUG) printf("%s\n", code);
1085 #endif
1086
1087         return code;
1088 }
1089
1090 static char *code_generate_geometry(ListBase *nodes, const char *geom_code, const char *defines)
1091 {
1092         DynStr *ds = BLI_dynstr_new();
1093         GPUNode *node;
1094         GPUInput *input;
1095         char *code;
1096         int builtins = 0;
1097
1098         /* XXX we should not make specific eevee cases here. */
1099         bool is_hair_shader = (strstr(defines, "HAIR_SHADER") != NULL);
1100
1101         /* Create prototype because attributes cannot be declared before layout. */
1102         BLI_dynstr_append(ds, "void pass_attr(in int vert);\n");
1103         BLI_dynstr_append(ds, "void calc_barycentric_distances(vec3 pos0, vec3 pos1, vec3 pos2);\n");
1104         BLI_dynstr_append(ds, "#define USE_ATTR\n");
1105
1106         /* Generate varying declarations. */
1107         for (node = nodes->first; node; node = node->next) {
1108                 for (input = node->inputs.first; input; input = input->next) {
1109                         if (input->source == GPU_SOURCE_BUILTIN) {
1110                                 builtins |= input->builtin;
1111                         }
1112                         if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
1113                                 BLI_dynstr_appendf(
1114                                         ds, "in %s var%dg[];\n",
1115                                         GPU_DATATYPE_STR[input->type],
1116                                         input->attr_id);
1117                                 BLI_dynstr_appendf(
1118                                         ds, "out %s var%d;\n",
1119                                         GPU_DATATYPE_STR[input->type],
1120                                         input->attr_id);
1121                         }
1122                 }
1123         }
1124
1125         if (builtins & GPU_BARYCENTRIC_TEXCO) {
1126                 BLI_dynstr_append(ds, "#ifdef HAIR_SHADER\n");
1127                 BLI_dynstr_append(ds, "in vec2 barycentricTexCog[];\n");
1128                 BLI_dynstr_append(ds, "#endif\n");
1129
1130                 BLI_dynstr_append(ds, "out vec2 barycentricTexCo;\n");
1131         }
1132
1133         if (builtins & GPU_BARYCENTRIC_DIST) {
1134                 BLI_dynstr_append(ds, "in vec3 barycentricPosg[];\n");
1135                 BLI_dynstr_append(ds, "flat out vec3 barycentricDist;\n");
1136         }
1137
1138         if (geom_code == NULL) {
1139                 /* Force geometry usage if GPU_BARYCENTRIC_DIST or GPU_BARYCENTRIC_TEXCO are used.
1140                  * Note: GPU_BARYCENTRIC_TEXCO only requires it if the shader is not drawing hairs. */
1141                 if ((builtins & (GPU_BARYCENTRIC_DIST | GPU_BARYCENTRIC_TEXCO)) == 0 || is_hair_shader) {
1142                         /* Early out */
1143                         BLI_dynstr_free(ds);
1144                         return NULL;
1145                 }
1146                 else {
1147                         /* Force geom shader usage */
1148                         /* TODO put in external file. */
1149                         BLI_dynstr_append(ds, "layout(triangles) in;\n");
1150                         BLI_dynstr_append(ds, "layout(triangle_strip, max_vertices=3) out;\n");
1151
1152                         BLI_dynstr_append(ds, "in vec3 worldPositiong[];\n");
1153                         BLI_dynstr_append(ds, "in vec3 viewPositiong[];\n");
1154                         BLI_dynstr_append(ds, "in vec3 worldNormalg[];\n");
1155                         BLI_dynstr_append(ds, "in vec3 viewNormalg[];\n");
1156
1157                         BLI_dynstr_append(ds, "out vec3 worldPosition;\n");
1158                         BLI_dynstr_append(ds, "out vec3 viewPosition;\n");
1159                         BLI_dynstr_append(ds, "out vec3 worldNormal;\n");
1160                         BLI_dynstr_append(ds, "out vec3 viewNormal;\n");
1161
1162                         BLI_dynstr_append(ds, "void main(){\n");
1163
1164                         if (builtins & GPU_BARYCENTRIC_DIST) {
1165                                 BLI_dynstr_append(ds, "\tcalc_barycentric_distances(barycentricPosg[0], barycentricPosg[1], barycentricPosg[2]);\n");
1166                         }
1167
1168                         BLI_dynstr_append(ds, "\tgl_Position = gl_in[0].gl_Position;\n");
1169                         BLI_dynstr_append(ds, "\tpass_attr(0);\n");
1170                         BLI_dynstr_append(ds, "\tEmitVertex();\n");
1171
1172                         BLI_dynstr_append(ds, "\tgl_Position = gl_in[1].gl_Position;\n");
1173                         BLI_dynstr_append(ds, "\tpass_attr(1);\n");
1174                         BLI_dynstr_append(ds, "\tEmitVertex();\n");
1175
1176                         BLI_dynstr_append(ds, "\tgl_Position = gl_in[2].gl_Position;\n");
1177                         BLI_dynstr_append(ds, "\tpass_attr(2);\n");
1178                         BLI_dynstr_append(ds, "\tEmitVertex();\n");
1179                         BLI_dynstr_append(ds, "};\n");
1180                 }
1181         }
1182         else {
1183                 BLI_dynstr_append(ds, geom_code);
1184         }
1185
1186         if (builtins & GPU_BARYCENTRIC_DIST) {
1187                 BLI_dynstr_append(ds, "void calc_barycentric_distances(vec3 pos0, vec3 pos1, vec3 pos2) {\n");
1188                 BLI_dynstr_append(ds, "\tvec3 edge21 = pos2 - pos1;\n");
1189                 BLI_dynstr_append(ds, "\tvec3 edge10 = pos1 - pos0;\n");
1190                 BLI_dynstr_append(ds, "\tvec3 edge02 = pos0 - pos2;\n");
1191                 BLI_dynstr_append(ds, "\tvec3 d21 = normalize(edge21);\n");
1192                 BLI_dynstr_append(ds, "\tvec3 d10 = normalize(edge10);\n");
1193                 BLI_dynstr_append(ds, "\tvec3 d02 = normalize(edge02);\n");
1194
1195                 BLI_dynstr_append(ds, "\tfloat d = dot(d21, edge02);\n");
1196                 BLI_dynstr_append(ds, "\tbarycentricDist.x = sqrt(dot(edge02, edge02) - d * d);\n");
1197                 BLI_dynstr_append(ds, "\td = dot(d02, edge10);\n");
1198                 BLI_dynstr_append(ds, "\tbarycentricDist.y = sqrt(dot(edge10, edge10) - d * d);\n");
1199                 BLI_dynstr_append(ds, "\td = dot(d10, edge21);\n");
1200                 BLI_dynstr_append(ds, "\tbarycentricDist.z = sqrt(dot(edge21, edge21) - d * d);\n");
1201                 BLI_dynstr_append(ds, "}\n");
1202         }
1203
1204         /* Generate varying assignments. */
1205         BLI_dynstr_append(ds, "void pass_attr(in int vert) {\n");
1206
1207         /* XXX HACK: Eevee specific. */
1208         if (geom_code == NULL) {
1209                 BLI_dynstr_append(ds, "\tworldPosition = worldPositiong[vert];\n");
1210                 BLI_dynstr_append(ds, "\tviewPosition = viewPositiong[vert];\n");
1211                 BLI_dynstr_append(ds, "\tworldNormal = worldNormalg[vert];\n");
1212                 BLI_dynstr_append(ds, "\tviewNormal = viewNormalg[vert];\n");
1213         }
1214
1215         if (builtins & GPU_BARYCENTRIC_TEXCO) {
1216                 BLI_dynstr_append(ds, "#ifdef HAIR_SHADER\n");
1217                 BLI_dynstr_append(ds, "\tbarycentricTexCo = barycentricTexCog[vert];\n");
1218                 BLI_dynstr_append(ds, "#else\n");
1219                 BLI_dynstr_append(ds, "\tbarycentricTexCo.x = float((vert % 3) == 0);\n");
1220                 BLI_dynstr_append(ds, "\tbarycentricTexCo.y = float((vert % 3) == 1);\n");
1221                 BLI_dynstr_append(ds, "#endif\n");
1222         }
1223
1224         for (node = nodes->first; node; node = node->next) {
1225                 for (input = node->inputs.first; input; input = input->next) {
1226                         if (input->source == GPU_SOURCE_ATTR && input->attr_first) {
1227                                 /* TODO let shader choose what to do depending on what the attribute is. */
1228                                 BLI_dynstr_appendf(ds, "\tvar%d = var%dg[vert];\n", input->attr_id, input->attr_id);
1229                         }
1230                 }
1231         }
1232         BLI_dynstr_append(ds, "}\n");
1233
1234         code = BLI_dynstr_get_cstring(ds);
1235         BLI_dynstr_free(ds);
1236
1237         return code;
1238 }
1239
1240 void GPU_code_generate_glsl_lib(void)
1241 {
1242         DynStr *ds;
1243
1244         /* only initialize the library once */
1245         if (glsl_material_library)
1246                 return;
1247
1248         ds = BLI_dynstr_new();
1249
1250         BLI_dynstr_append(ds, datatoc_gpu_shader_material_glsl);
1251
1252
1253         glsl_material_library = BLI_dynstr_get_cstring(ds);
1254
1255         BLI_dynstr_free(ds);
1256 }
1257
1258
1259 /* GPU pass binding/unbinding */
1260
1261 GPUShader *GPU_pass_shader_get(GPUPass *pass)
1262 {
1263         return pass->shader;
1264 }
1265
1266 void GPU_nodes_extract_dynamic_inputs(GPUShader *shader, ListBase *inputs, ListBase *nodes)
1267 {
1268         GPUNode *node;
1269         GPUInput *next, *input;
1270
1271         BLI_listbase_clear(inputs);
1272
1273         if (!shader)
1274                 return;
1275
1276         for (node = nodes->first; node; node = node->next) {
1277                 int z = 0;
1278                 for (input = node->inputs.first; input; input = next, z++) {
1279                         next = input->next;
1280
1281                         /* attributes don't need to be bound, they already have
1282                          * an id that the drawing functions will use. Builtins have
1283                          * constant names. */
1284                         if (ELEM(input->source, GPU_SOURCE_ATTR, GPU_SOURCE_BUILTIN)) {
1285                                 continue;
1286                         }
1287
1288                         if (input->source == GPU_SOURCE_TEX)
1289                                 BLI_snprintf(input->shadername, sizeof(input->shadername), "samp%d", input->texid);
1290                         else {
1291                                 BLI_snprintf(input->shadername, sizeof(input->shadername), "unf%d", input->id);
1292                         }
1293
1294                         if (input->source == GPU_SOURCE_TEX) {
1295                                 if (input->bindtex) {
1296                                         input->shaderloc = GPU_shader_get_uniform_ensure(shader, input->shadername);
1297                                         /* extract nodes */
1298                                         BLI_remlink(&node->inputs, input);
1299                                         BLI_addtail(inputs, input);
1300                                 }
1301                         }
1302                 }
1303         }
1304 }
1305
1306 /* Node Link Functions */
1307
1308 static GPUNodeLink *GPU_node_link_create(void)
1309 {
1310         GPUNodeLink *link = MEM_callocN(sizeof(GPUNodeLink), "GPUNodeLink");
1311         link->users++;
1312
1313         return link;
1314 }
1315
1316 static void gpu_node_link_free(GPUNodeLink *link)
1317 {
1318         link->users--;
1319
1320         if (link->users < 0)
1321                 fprintf(stderr, "GPU_node_link_free: negative refcount\n");
1322
1323         if (link->users == 0) {
1324                 if (link->output)
1325                         link->output->link = NULL;
1326                 MEM_freeN(link);
1327         }
1328 }
1329
1330 /* Node Functions */
1331
1332 static GPUNode *GPU_node_begin(const char *name)
1333 {
1334         GPUNode *node = MEM_callocN(sizeof(GPUNode), "GPUNode");
1335
1336         node->name = name;
1337
1338         return node;
1339 }
1340
1341 static void gpu_node_input_link(GPUNode *node, GPUNodeLink *link, const eGPUType type)
1342 {
1343         GPUInput *input;
1344         GPUNode *outnode;
1345         const char *name;
1346
1347         if (link->link_type == GPU_NODE_LINK_OUTPUT) {
1348                 outnode = link->output->node;
1349                 name = outnode->name;
1350                 input = outnode->inputs.first;
1351
1352                 if ((STREQ(name, "set_value") || STREQ(name, "set_rgb") || STREQ(name, "set_rgba")) &&
1353                     (input->type == type))
1354                 {
1355                         input = MEM_dupallocN(outnode->inputs.first);
1356                         if (input->link)
1357                                 input->link->users++;
1358                         BLI_addtail(&node->inputs, input);
1359                         return;
1360                 }
1361         }
1362
1363         input = MEM_callocN(sizeof(GPUInput), "GPUInput");
1364         input->node = node;
1365         input->type = type;
1366
1367         switch (link->link_type) {
1368                 case GPU_NODE_LINK_BUILTIN:
1369                         input->source = GPU_SOURCE_BUILTIN;
1370                         input->builtin = link->builtin;
1371                         break;
1372                 case GPU_NODE_LINK_OUTPUT:
1373                         input->source = GPU_SOURCE_OUTPUT;
1374                         input->link = link;
1375                         link->users++;
1376                         break;
1377                 case GPU_NODE_LINK_COLORBAND:
1378                         input->source = GPU_SOURCE_TEX;
1379                         input->coba = link->coba;
1380                         break;
1381                 case GPU_NODE_LINK_IMAGE_BLENDER:
1382                         input->source = GPU_SOURCE_TEX;
1383                         input->ima = link->ima;
1384                         input->iuser = link->iuser;
1385                         input->image_isdata = link->image_isdata;
1386                         break;
1387                 case GPU_NODE_LINK_ATTR:
1388                         input->source = GPU_SOURCE_ATTR;
1389                         input->attr_type = link->attr_type;
1390                         BLI_strncpy(input->attr_name, link->attr_name, sizeof(input->attr_name));
1391                         break;
1392                 case GPU_NODE_LINK_CONSTANT:
1393                         input->source = (type == GPU_CLOSURE) ? GPU_SOURCE_STRUCT : GPU_SOURCE_CONSTANT;
1394                         break;
1395                 case GPU_NODE_LINK_UNIFORM:
1396                         input->source = GPU_SOURCE_UNIFORM;
1397                         break;
1398                 default:
1399                         break;
1400         }
1401
1402         if (ELEM(input->source, GPU_SOURCE_CONSTANT, GPU_SOURCE_UNIFORM)) {
1403                 memcpy(input->vec, link->data, type * sizeof(float));
1404         }
1405
1406         if (link->link_type != GPU_NODE_LINK_OUTPUT) {
1407                 MEM_freeN(link);
1408         }
1409         BLI_addtail(&node->inputs, input);
1410 }
1411
1412
1413 static const char *gpu_uniform_set_function_from_type(eNodeSocketDatatype type)
1414 {
1415         switch (type) {
1416                 /* For now INT is supported as float. */
1417                 case SOCK_INT:
1418                 case SOCK_FLOAT:
1419                         return "set_value";
1420                 case SOCK_VECTOR:
1421                         return "set_rgb";
1422                 case SOCK_RGBA:
1423                         return "set_rgba";
1424                 default:
1425                         BLI_assert(!"No gpu function for non-supported eNodeSocketDatatype");
1426                         return NULL;
1427         }
1428 }
1429
1430 /**
1431  * Link stack uniform buffer.
1432  * This is called for the input/output sockets that are note connected.
1433  */
1434 static GPUNodeLink *gpu_uniformbuffer_link(
1435         GPUMaterial *mat, bNode *node, GPUNodeStack *stack, const int index, const eNodeSocketInOut in_out)
1436 {
1437         bNodeSocket *socket;
1438
1439         if (in_out == SOCK_IN) {
1440                 socket = BLI_findlink(&node->inputs, index);
1441         }
1442         else {
1443                 socket = BLI_findlink(&node->outputs, index);
1444         }
1445
1446         BLI_assert(socket != NULL);
1447         BLI_assert(socket->in_out == in_out);
1448
1449         if ((socket->flag & SOCK_HIDE_VALUE) == 0) {
1450                 GPUNodeLink *link;
1451                 switch (socket->type) {
1452                         case SOCK_FLOAT:
1453                         {
1454                                 bNodeSocketValueFloat *socket_data = socket->default_value;
1455                                 link = GPU_uniform(&socket_data->value);
1456                                 break;
1457                         }
1458                         case SOCK_VECTOR:
1459                         {
1460                                 bNodeSocketValueVector *socket_data = socket->default_value;
1461                                 link = GPU_uniform(socket_data->value);
1462                                 break;
1463                         }
1464                         case SOCK_RGBA:
1465                         {
1466                                 bNodeSocketValueRGBA *socket_data = socket->default_value;
1467                                 link = GPU_uniform(socket_data->value);
1468                                 break;
1469                         }
1470                         default:
1471                                 return NULL;
1472                                 break;
1473                 }
1474
1475                 if (in_out == SOCK_IN) {
1476                         GPU_link(mat, gpu_uniform_set_function_from_type(socket->type), link, &stack->link);
1477                 }
1478                 return link;
1479         }
1480         return NULL;
1481 }
1482
1483 static void gpu_node_input_socket(GPUMaterial *material, bNode *bnode, GPUNode *node, GPUNodeStack *sock, const int index)
1484 {
1485         if (sock->link) {
1486                 gpu_node_input_link(node, sock->link, sock->type);
1487         }
1488         else if ((material != NULL) && (gpu_uniformbuffer_link(material, bnode, sock, index, SOCK_IN) != NULL)) {
1489                 gpu_node_input_link(node, sock->link, sock->type);
1490         }
1491         else {
1492                 gpu_node_input_link(node, GPU_constant(sock->vec), sock->type);
1493         }
1494 }
1495
1496 static void gpu_node_output(GPUNode *node, const eGPUType type, GPUNodeLink **link)
1497 {
1498         GPUOutput *output = MEM_callocN(sizeof(GPUOutput), "GPUOutput");
1499
1500         output->type = type;
1501         output->node = node;
1502
1503         if (link) {
1504                 *link = output->link = GPU_node_link_create();
1505                 output->link->link_type = GPU_NODE_LINK_OUTPUT;
1506                 output->link->output = output;
1507
1508                 /* note: the caller owns the reference to the link, GPUOutput
1509                  * merely points to it, and if the node is destroyed it will
1510                  * set that pointer to NULL */
1511         }
1512
1513         BLI_addtail(&node->outputs, output);
1514 }
1515
1516 void GPU_inputs_free(ListBase *inputs)
1517 {
1518         GPUInput *input;
1519
1520         for (input = inputs->first; input; input = input->next) {
1521                 if (input->link)
1522                         gpu_node_link_free(input->link);
1523         }
1524
1525         BLI_freelistN(inputs);
1526 }
1527
1528 static void gpu_node_free(GPUNode *node)
1529 {
1530         GPUOutput *output;
1531
1532         GPU_inputs_free(&node->inputs);
1533
1534         for (output = node->outputs.first; output; output = output->next)
1535                 if (output->link) {
1536                         output->link->output = NULL;
1537                         gpu_node_link_free(output->link);
1538                 }
1539
1540         BLI_freelistN(&node->outputs);
1541         MEM_freeN(node);
1542 }
1543
1544 static void gpu_nodes_free(ListBase *nodes)
1545 {
1546         GPUNode *node;
1547
1548         while ((node = BLI_pophead(nodes))) {
1549                 gpu_node_free(node);
1550         }
1551 }
1552
1553 /* vertex attributes */
1554
1555 void GPU_nodes_get_vertex_attrs(ListBase *nodes, GPUVertAttrLayers *attrs)
1556 {
1557         GPUNode *node;
1558         GPUInput *input;
1559         int a;
1560
1561         /* convert attributes requested by node inputs to an array of layers,
1562          * checking for duplicates and assigning id's starting from zero. */
1563
1564         memset(attrs, 0, sizeof(*attrs));
1565
1566         for (node = nodes->first; node; node = node->next) {
1567                 for (input = node->inputs.first; input; input = input->next) {
1568                         if (input->source == GPU_SOURCE_ATTR) {
1569                                 for (a = 0; a < attrs->totlayer; a++) {
1570                                         if (attrs->layer[a].type == input->attr_type &&
1571                                             STREQ(attrs->layer[a].name, input->attr_name))
1572                                         {
1573                                                 break;
1574                                         }
1575                                 }
1576
1577                                 if (a < GPU_MAX_ATTR) {
1578                                         if (a == attrs->totlayer) {
1579                                                 input->attr_id = attrs->totlayer++;
1580                                                 input->attr_first = true;
1581
1582                                                 attrs->layer[a].type = input->attr_type;
1583                                                 attrs->layer[a].attr_id = input->attr_id;
1584                                                 BLI_strncpy(
1585                                                         attrs->layer[a].name, input->attr_name,
1586                                                         sizeof(attrs->layer[a].name));
1587                                         }
1588                                         else {
1589                                                 input->attr_id = attrs->layer[a].attr_id;
1590                                         }
1591                                 }
1592                         }
1593                 }
1594         }
1595 }
1596
1597 /* varargs linking  */
1598
1599 GPUNodeLink *GPU_attribute(const CustomDataType type, const char *name)
1600 {
1601         GPUNodeLink *link = GPU_node_link_create();
1602         link->link_type = GPU_NODE_LINK_ATTR;
1603         link->attr_name = name;
1604         /* Fall back to the UV layer, which matches old behavior. */
1605         if (type == CD_AUTO_FROM_NAME && name[0] == '\0') {
1606                 link->attr_type = CD_MTFACE;
1607         }
1608         else {
1609                 link->attr_type = type;
1610         }
1611         return link;
1612 }
1613
1614 GPUNodeLink *GPU_constant(float *num)
1615 {
1616         GPUNodeLink *link = GPU_node_link_create();
1617         link->link_type = GPU_NODE_LINK_CONSTANT;
1618         link->data = num;
1619         return link;
1620 }
1621
1622 GPUNodeLink *GPU_uniform(float *num)
1623 {
1624         GPUNodeLink *link = GPU_node_link_create();
1625         link->link_type = GPU_NODE_LINK_UNIFORM;
1626         link->data = num;
1627         return link;
1628 }
1629
1630 GPUNodeLink *GPU_image(Image *ima, ImageUser *iuser, bool is_data)
1631 {
1632         GPUNodeLink *link = GPU_node_link_create();
1633         link->link_type = GPU_NODE_LINK_IMAGE_BLENDER;
1634         link->ima = ima;
1635         link->iuser = iuser;
1636         link->image_isdata = is_data;
1637         return link;
1638 }
1639
1640 GPUNodeLink *GPU_color_band(GPUMaterial *mat, int size, float *pixels, float *row)
1641 {
1642         GPUNodeLink *link = GPU_node_link_create();
1643         link->link_type = GPU_NODE_LINK_COLORBAND;
1644         link->coba = gpu_material_ramp_texture_row_set(mat, size, pixels, row);
1645         MEM_freeN(pixels);
1646         return link;
1647 }
1648
1649 GPUNodeLink *GPU_builtin(eGPUBuiltin builtin)
1650 {
1651         GPUNodeLink *link = GPU_node_link_create();
1652         link->link_type = GPU_NODE_LINK_BUILTIN;
1653         link->builtin = builtin;
1654         return link;
1655 }
1656
1657 bool GPU_link(GPUMaterial *mat, const char *name, ...)
1658 {
1659         GPUNode *node;
1660         GPUFunction *function;
1661         GPUNodeLink *link, **linkptr;
1662         va_list params;
1663         int i;
1664
1665         function = gpu_lookup_function(name);
1666         if (!function) {
1667                 fprintf(stderr, "GPU failed to find function %s\n", name);
1668                 return false;
1669         }
1670
1671         node = GPU_node_begin(name);
1672
1673         va_start(params, name);
1674         for (i = 0; i < function->totparam; i++) {
1675                 if (function->paramqual[i] != FUNCTION_QUAL_IN) {
1676                         linkptr = va_arg(params, GPUNodeLink **);
1677                         gpu_node_output(node, function->paramtype[i], linkptr);
1678                 }
1679                 else {
1680                         link = va_arg(params, GPUNodeLink *);
1681                         gpu_node_input_link(node, link, function->paramtype[i]);
1682                 }
1683         }
1684         va_end(params);
1685
1686         gpu_material_add_node(mat, node);
1687
1688         return true;
1689 }
1690
1691 bool GPU_stack_link(GPUMaterial *material, bNode *bnode, const char *name, GPUNodeStack *in, GPUNodeStack *out, ...)
1692 {
1693         GPUNode *node;
1694         GPUFunction *function;
1695         GPUNodeLink *link, **linkptr;
1696         va_list params;
1697         int i, totin, totout;
1698
1699         function = gpu_lookup_function(name);
1700         if (!function) {
1701                 fprintf(stderr, "GPU failed to find function %s\n", name);
1702                 return false;
1703         }
1704
1705         node = GPU_node_begin(name);
1706         totin = 0;
1707         totout = 0;
1708
1709         if (in) {
1710                 for (i = 0; !in[i].end; i++) {
1711                         if (in[i].type != GPU_NONE) {
1712                                 gpu_node_input_socket(material, bnode, node, &in[i], i);
1713                                 totin++;
1714                         }
1715                 }
1716         }
1717
1718         if (out) {
1719                 for (i = 0; !out[i].end; i++) {
1720                         if (out[i].type != GPU_NONE) {
1721                                 gpu_node_output(node, out[i].type, &out[i].link);
1722                                 totout++;
1723                         }
1724                 }
1725         }
1726
1727         va_start(params, out);
1728         for (i = 0; i < function->totparam; i++) {
1729                 if (function->paramqual[i] != FUNCTION_QUAL_IN) {
1730                         if (totout == 0) {
1731                                 linkptr = va_arg(params, GPUNodeLink **);
1732                                 gpu_node_output(node, function->paramtype[i], linkptr);
1733                         }
1734                         else
1735                                 totout--;
1736                 }
1737                 else {
1738                         if (totin == 0) {
1739                                 link = va_arg(params, GPUNodeLink *);
1740                                 if (link->socket)
1741                                         gpu_node_input_socket(NULL, NULL, node, link->socket, -1);
1742                                 else
1743                                         gpu_node_input_link(node, link, function->paramtype[i]);
1744                         }
1745                         else
1746                                 totin--;
1747                 }
1748         }
1749         va_end(params);
1750
1751         gpu_material_add_node(material, node);
1752
1753         return true;
1754 }
1755
1756 GPUNodeLink *GPU_uniformbuffer_link_out(GPUMaterial *mat, bNode *node, GPUNodeStack *stack, const int index)
1757 {
1758         return gpu_uniformbuffer_link(mat, node, stack, index, SOCK_OUT);
1759 }
1760
1761 /* Pass create/free */
1762
1763 static void gpu_nodes_tag(GPUNodeLink *link)
1764 {
1765         GPUNode *node;
1766         GPUInput *input;
1767
1768         if (!link->output)
1769                 return;
1770
1771         node = link->output->node;
1772         if (node->tag)
1773                 return;
1774
1775         node->tag = true;
1776         for (input = node->inputs.first; input; input = input->next)
1777                 if (input->link)
1778                         gpu_nodes_tag(input->link);
1779 }
1780
1781 void GPU_nodes_prune(ListBase *nodes, GPUNodeLink *outlink)
1782 {
1783         GPUNode *node, *next;
1784
1785         for (node = nodes->first; node; node = node->next)
1786                 node->tag = false;
1787
1788         gpu_nodes_tag(outlink);
1789
1790         for (node = nodes->first; node; node = next) {
1791                 next = node->next;
1792
1793                 if (!node->tag) {
1794                         BLI_remlink(nodes, node);
1795                         gpu_node_free(node);
1796                 }
1797         }
1798 }
1799
1800 static bool gpu_pass_is_valid(GPUPass *pass)
1801 {
1802         /* Shader is not null if compilation is successful. */
1803         return (pass->compiled == false || pass->shader != NULL);
1804 }
1805
1806 GPUPass *GPU_generate_pass(
1807         GPUMaterial *material,
1808         GPUNodeLink *frag_outlink,
1809         struct GPUVertAttrLayers *attrs,
1810         ListBase *nodes,
1811         int *builtins,
1812         const char *vert_code,
1813         const char *geom_code,
1814         const char *frag_lib,
1815         const char *defines)
1816 {
1817         char *vertexcode, *geometrycode, *fragmentcode;
1818         GPUPass *pass = NULL, *pass_hash = NULL;
1819
1820         /* prune unused nodes */
1821         GPU_nodes_prune(nodes, frag_outlink);
1822
1823         GPU_nodes_get_vertex_attrs(nodes, attrs);
1824
1825         /* generate code */
1826         char *fragmentgen = code_generate_fragment(material, nodes, frag_outlink->output, builtins);
1827
1828         /* Cache lookup: Reuse shaders already compiled */
1829         uint32_t hash = gpu_pass_hash(fragmentgen, defines, attrs);
1830         pass_hash = gpu_pass_cache_lookup(hash);
1831
1832         if (pass_hash && (pass_hash->next == NULL || pass_hash->next->hash != hash)) {
1833                 /* No collision, just return the pass. */
1834                 MEM_freeN(fragmentgen);
1835                 if (!gpu_pass_is_valid(pass_hash)) {
1836                         /* Shader has already been created but failed to compile. */
1837                         return NULL;
1838                 }
1839                 pass_hash->refcount += 1;
1840                 return pass_hash;
1841         }
1842
1843         /* Either the shader is not compiled or there is a hash collision...
1844          * continue generating the shader strings. */
1845         char *tmp = BLI_strdupcat(frag_lib, glsl_material_library);
1846
1847         geometrycode = code_generate_geometry(nodes, geom_code, defines);
1848         vertexcode = code_generate_vertex(nodes, vert_code, (geometrycode != NULL));
1849         fragmentcode = BLI_strdupcat(tmp, fragmentgen);
1850
1851         MEM_freeN(fragmentgen);
1852         MEM_freeN(tmp);
1853
1854         if (pass_hash) {
1855                 /* Cache lookup: Reuse shaders already compiled */
1856                 pass = gpu_pass_cache_resolve_collision(pass_hash, vertexcode, geometrycode, fragmentcode, defines, hash);
1857         }
1858
1859         if (pass) {
1860                 /* Cache hit. Reuse the same GPUPass and GPUShader. */
1861                 if (!gpu_pass_is_valid(pass)) {
1862                         /* Shader has already been created but failed to compile. */
1863                         return NULL;
1864                 }
1865
1866                 MEM_SAFE_FREE(vertexcode);
1867                 MEM_SAFE_FREE(fragmentcode);
1868                 MEM_SAFE_FREE(geometrycode);
1869
1870                 pass->refcount += 1;
1871         }
1872         else {
1873                 /* We still create a pass even if shader compilation
1874                  * fails to avoid trying to compile again and again. */
1875                 pass = MEM_callocN(sizeof(GPUPass), "GPUPass");
1876                 pass->shader = NULL;
1877                 pass->refcount = 1;
1878                 pass->hash = hash;
1879                 pass->vertexcode = vertexcode;
1880                 pass->fragmentcode = fragmentcode;
1881                 pass->geometrycode = geometrycode;
1882                 pass->defines = (defines) ? BLI_strdup(defines) : NULL;
1883                 pass->compiled = false;
1884
1885                 BLI_spin_lock(&pass_cache_spin);
1886                 if (pass_hash != NULL) {
1887                         /* Add after the first pass having the same hash. */
1888                         pass->next = pass_hash->next;
1889                         pass_hash->next = pass;
1890                 }
1891                 else {
1892                         /* No other pass have same hash, just prepend to the list. */
1893                         BLI_LINKS_PREPEND(pass_cache, pass);
1894                 }
1895                 BLI_spin_unlock(&pass_cache_spin);
1896         }
1897
1898         return pass;
1899 }
1900
1901 static int count_active_texture_sampler(GPUShader *shader, char *source)
1902 {
1903         char *code = source;
1904         int samplers_id[64]; /* Remember this is per stage. */
1905         int sampler_len = 0;
1906
1907         while ((code = strstr(code, "uniform "))) {
1908                 /* Move past "uniform". */
1909                 code += 7;
1910                 /* Skip following spaces. */
1911                 while (*code == ' ') { code++; }
1912                 /* Skip "i" from potential isamplers. */
1913                 if (*code == 'i') { code++; }
1914                 /* Skip following spaces. */
1915                 if (gpu_str_prefix(code, "sampler")) {
1916                         /* Move past "uniform". */
1917                         code += 7;
1918                         /* Skip sampler type suffix. */
1919                         while (*code != ' ' && *code != '\0') { code++; }
1920                         /* Skip following spaces. */
1921                         while (*code == ' ') { code++; }
1922
1923                         if (*code != '\0') {
1924                                 char sampler_name[64];
1925                                 code = gpu_str_skip_token(code, sampler_name, sizeof(sampler_name));
1926                                 int id = GPU_shader_get_uniform_ensure(shader, sampler_name);
1927
1928                                 if (id == -1) {
1929                                         continue;
1930                                 }
1931                                 /* Catch duplicates. */
1932                                 bool is_duplicate = false;
1933                                 for (int i = 0; i < sampler_len; ++i) {
1934                                         if (samplers_id[i] == id) {
1935                                                 is_duplicate = true;
1936                                         }
1937                                 }
1938
1939                                 if (!is_duplicate) {
1940                                         samplers_id[sampler_len] = id;
1941                                         sampler_len++;
1942                                 }
1943                         }
1944                 }
1945         }
1946
1947         return sampler_len;
1948 }
1949
1950 static bool gpu_pass_shader_validate(GPUPass *pass)
1951 {
1952         if (pass->shader == NULL) {
1953                 return false;
1954         }
1955
1956         /* NOTE: The only drawback of this method is that it will count a sampler
1957          * used in the fragment shader and only declared (but not used) in the vertex
1958          * shader as used by both. But this corner case is not happening for now. */
1959         int vert_samplers_len = count_active_texture_sampler(pass->shader, pass->vertexcode);
1960         int frag_samplers_len = count_active_texture_sampler(pass->shader, pass->fragmentcode);
1961
1962         int total_samplers_len = vert_samplers_len + frag_samplers_len;
1963
1964         /* Validate against opengl limit. */
1965         if ((frag_samplers_len > GPU_max_textures_frag()) ||
1966             (vert_samplers_len > GPU_max_textures_vert()))
1967         {
1968                 return false;
1969         }
1970
1971         if (pass->geometrycode) {
1972                 int geom_samplers_len = count_active_texture_sampler(pass->shader, pass->geometrycode);
1973                 total_samplers_len += geom_samplers_len;
1974                 if (geom_samplers_len > GPU_max_textures_geom()) {
1975                         return false;
1976                 }
1977         }
1978
1979         return (total_samplers_len <= GPU_max_textures());
1980 }
1981
1982 void GPU_pass_compile(GPUPass *pass, const char *shname)
1983 {
1984         if (!pass->compiled) {
1985                 pass->shader = GPU_shader_create(
1986                         pass->vertexcode,
1987                         pass->fragmentcode,
1988                         pass->geometrycode,
1989                         NULL,
1990                         pass->defines,
1991                         shname);
1992
1993                 /* NOTE: Some drivers / gpu allows more active samplers than the opengl limit.
1994                  * We need to make sure to count active samplers to avoid undefined behavior. */
1995                 if (!gpu_pass_shader_validate(pass)) {
1996                         if (pass->shader != NULL) {
1997                                 fprintf(stderr, "GPUShader: error: too many samplers in shader.\n");
1998                                 GPU_shader_free(pass->shader);
1999                         }
2000                         pass->shader = NULL;
2001                 }
2002                 else if (!BLI_thread_is_main()) {
2003                         /* For some Intel drivers, you must use the program at least once
2004                          * in the rendering context that it is linked. */
2005                         glUseProgram(GPU_shader_get_program(pass->shader));
2006                         glUseProgram(0);
2007                 }
2008
2009                 pass->compiled = true;
2010         }
2011 }
2012
2013 void GPU_pass_release(GPUPass *pass)
2014 {
2015         BLI_assert(pass->refcount > 0);
2016         pass->refcount--;
2017 }
2018
2019 static void gpu_pass_free(GPUPass *pass)
2020 {
2021         BLI_assert(pass->refcount == 0);
2022         if (pass->shader) {
2023                 GPU_shader_free(pass->shader);
2024         }
2025         MEM_SAFE_FREE(pass->fragmentcode);
2026         MEM_SAFE_FREE(pass->geometrycode);
2027         MEM_SAFE_FREE(pass->vertexcode);
2028         MEM_SAFE_FREE(pass->defines);
2029         MEM_freeN(pass);
2030 }
2031
2032 void GPU_pass_free_nodes(ListBase *nodes)
2033 {
2034         gpu_nodes_free(nodes);
2035 }
2036
2037 void GPU_pass_cache_garbage_collect(void)
2038 {
2039         static int lasttime = 0;
2040         const int shadercollectrate = 60; /* hardcoded for now. */
2041         int ctime = (int)PIL_check_seconds_timer();
2042
2043         if (ctime < shadercollectrate + lasttime)
2044                 return;
2045
2046         lasttime = ctime;
2047
2048         BLI_spin_lock(&pass_cache_spin);
2049         GPUPass *next, **prev_pass = &pass_cache;
2050         for (GPUPass *pass = pass_cache; pass; pass = next) {
2051                 next = pass->next;
2052                 if (pass->refcount == 0) {
2053                         /* Remove from list */
2054                         *prev_pass = next;
2055                         gpu_pass_free(pass);
2056                 }
2057                 else {
2058                         prev_pass = &pass->next;
2059                 }
2060         }
2061         BLI_spin_unlock(&pass_cache_spin);
2062 }
2063
2064 void GPU_pass_cache_init(void)
2065 {
2066         BLI_spin_init(&pass_cache_spin);
2067 }
2068
2069 void GPU_pass_cache_free(void)
2070 {
2071         BLI_spin_lock(&pass_cache_spin);
2072         while (pass_cache) {
2073                 GPUPass *next = pass_cache->next;
2074                 gpu_pass_free(pass_cache);
2075                 pass_cache = next;
2076         }
2077         BLI_spin_unlock(&pass_cache_spin);
2078
2079         BLI_spin_end(&pass_cache_spin);
2080 }