Merge branch 'master' into 28
[blender.git] / source / blender / gpu / intern / gpu_material.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version. 
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2006 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Brecht Van Lommel.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/gpu/intern/gpu_material.c
29  *  \ingroup gpu
30  *
31  * Manages materials, lights and textures.
32  */
33
34 #include <math.h>
35 #include <string.h>
36
37 #include "MEM_guardedalloc.h"
38
39 #include "DNA_lamp_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_object_types.h"
42 #include "DNA_scene_types.h"
43 #include "DNA_world_types.h"
44
45 #include "BLI_math.h"
46 #include "BLI_blenlib.h"
47 #include "BLI_utildefines.h"
48
49 #include "BKE_anim.h"
50 #include "BKE_colortools.h"
51 #include "BKE_global.h"
52 #include "BKE_image.h"
53 #include "BKE_main.h"
54 #include "BKE_node.h"
55 #include "BKE_scene.h"
56 #include "BKE_texture.h"
57 #include "BKE_group.h"
58
59 #include "IMB_imbuf_types.h"
60
61 #include "GPU_extensions.h"
62 #include "GPU_framebuffer.h"
63 #include "GPU_lamp.h"
64 #include "GPU_material.h"
65 #include "GPU_shader.h"
66 #include "GPU_texture.h"
67
68 #include "gpu_codegen.h"
69 #include "gpu_lamp_private.h"
70
71 #ifdef WITH_OPENSUBDIV
72 #  include "BKE_DerivedMesh.h"
73 #endif
74
75 /* Structs */
76
77 typedef enum DynMatProperty {
78         DYN_LAMP_CO = 1,
79         DYN_LAMP_VEC = 2,
80         DYN_LAMP_IMAT = 4,
81         DYN_LAMP_PERSMAT = 8,
82 } DynMatProperty;
83
84 static struct GPUWorld {
85         float mistenabled;
86         float mistype;
87         float miststart;
88         float mistdistance;
89         float mistintensity;
90         float mistcol[4];
91         float horicol[3];
92         float ambcol[4];
93         float zencol[3];
94 } GPUWorld;
95
96 struct GPUMaterial {
97         Scene *scene;
98         Material *ma;
99
100         /* material for mesh surface, worlds or something else.
101          * some code generation is done differently depending on the use case */
102         int type;
103         
104         /* for creating the material */
105         ListBase nodes;
106         GPUNodeLink *outlink;
107
108         /* for binding the material */
109         GPUPass *pass;
110         GPUVertexAttribs attribs;
111         int builtins;
112         int alpha, obcolalpha;
113         int dynproperty;
114
115         /* for passing uniforms */
116         int viewmatloc, invviewmatloc;
117         int obmatloc, invobmatloc;
118         int localtoviewmatloc, invlocaltoviewmatloc;
119         int obcolloc, obautobumpscaleloc;
120         int cameratexcofacloc;
121
122         int partscalarpropsloc;
123         int partcoloc;
124         int partvel;
125         int partangvel;
126
127         ListBase lamps;
128         bool bound;
129
130         bool is_opensubdiv;
131 };
132
133 /* Forward declaration so shade_light_textures() can use this, while still keeping the code somewhat organized */
134 static void texture_rgb_blend(
135         GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg,
136         int blendtype, GPUNodeLink **in);
137
138 /* Functions */
139
140 static GPUMaterial *GPU_material_construct_begin(Material *ma)
141 {
142         GPUMaterial *material = MEM_callocN(sizeof(GPUMaterial), "GPUMaterial");
143
144         material->ma = ma;
145
146         return material;
147 }
148
149 static void gpu_material_set_attrib_id(GPUMaterial *material)
150 {
151         GPUVertexAttribs *attribs = &material->attribs;
152         GPUPass *pass = material->pass;
153         if (!pass) {
154                 attribs->totlayer = 0;
155                 return;
156         }
157         
158         GPUShader *shader = GPU_pass_shader(pass);
159         if (!shader) {
160                 attribs->totlayer = 0;
161                 return;
162         }
163
164         /* convert from attribute number to the actual id assigned by opengl,
165          * in case the attrib does not get a valid index back, it was probably
166          * removed by the glsl compiler by dead code elimination */
167
168         int b = 0;
169         for (int a = 0; a < attribs->totlayer; a++) {
170                 char name[32];
171                 BLI_snprintf(name, sizeof(name), "att%d", attribs->layer[a].attribid);
172                 attribs->layer[a].glindex = GPU_shader_get_attribute(shader, name);
173
174                 BLI_snprintf(name, sizeof(name), "att%d_info", attribs->layer[a].attribid);
175                 attribs->layer[a].glinfoindoex = GPU_shader_get_uniform(shader, name);
176
177                 if (attribs->layer[a].glindex >= 0) {
178                         attribs->layer[b] = attribs->layer[a];
179                         b++;
180                 }
181         }
182
183         attribs->totlayer = b;
184 }
185
186 static int gpu_material_construct_end(GPUMaterial *material, const char *passname)
187 {
188         if (material->outlink) {
189                 GPUNodeLink *outlink = material->outlink;
190                 material->pass = GPU_generate_pass(&material->nodes, outlink,
191                         &material->attribs, &material->builtins, material->type,
192                         passname,
193                         material->is_opensubdiv,
194                         GPU_material_use_new_shading_nodes(material));
195
196                 if (!material->pass)
197                         return 0;
198
199                 gpu_material_set_attrib_id(material);
200                 
201                 GPUShader *shader = GPU_pass_shader(material->pass);
202
203                 if (material->builtins & GPU_VIEW_MATRIX)
204                         material->viewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_VIEW_MATRIX));
205                 if (material->builtins & GPU_INVERSE_VIEW_MATRIX)
206                         material->invviewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_INVERSE_VIEW_MATRIX));
207                 if (material->builtins & GPU_OBJECT_MATRIX)
208                         material->obmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_OBJECT_MATRIX));
209                 if (material->builtins & GPU_INVERSE_OBJECT_MATRIX)
210                         material->invobmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_INVERSE_OBJECT_MATRIX));
211                 if (material->builtins & GPU_LOC_TO_VIEW_MATRIX)
212                         material->localtoviewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_LOC_TO_VIEW_MATRIX));
213                 if (material->builtins & GPU_INVERSE_LOC_TO_VIEW_MATRIX)
214                         material->invlocaltoviewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_INVERSE_LOC_TO_VIEW_MATRIX));
215                 if (material->builtins & GPU_OBCOLOR)
216                         material->obcolloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_OBCOLOR));
217                 if (material->builtins & GPU_AUTO_BUMPSCALE)
218                         material->obautobumpscaleloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_AUTO_BUMPSCALE));
219                 if (material->builtins & GPU_CAMERA_TEXCO_FACTORS)
220                         material->cameratexcofacloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_CAMERA_TEXCO_FACTORS));
221                 if (material->builtins & GPU_PARTICLE_SCALAR_PROPS)
222                         material->partscalarpropsloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_SCALAR_PROPS));
223                 if (material->builtins & GPU_PARTICLE_LOCATION)
224                         material->partcoloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_LOCATION));
225                 if (material->builtins & GPU_PARTICLE_VELOCITY)
226                         material->partvel = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_VELOCITY));
227                 if (material->builtins & GPU_PARTICLE_ANG_VELOCITY)
228                         material->partangvel = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_PARTICLE_ANG_VELOCITY));
229                 return 1;
230         }
231         else {
232                 GPU_pass_free_nodes(&material->nodes);
233         }
234
235         return 0;
236 }
237
238 void GPU_material_free(ListBase *gpumaterial)
239 {
240         for (LinkData *link = gpumaterial->first; link; link = link->next) {
241                 GPUMaterial *material = link->data;
242
243                 if (material->pass)
244                         GPU_pass_free(material->pass);
245
246                 for (LinkData *nlink = material->lamps.first; nlink; nlink = nlink->next) {
247                         GPULamp *lamp = nlink->data;
248
249                         if (material->ma) {
250                                 Material *ma = material->ma;
251                                 
252                                 LinkData *next = NULL;
253                                 for (LinkData *mlink = lamp->materials.first; mlink; mlink = next) {
254                                         next = mlink->next;
255                                         if (mlink->data == ma)
256                                                 BLI_freelinkN(&lamp->materials, mlink);
257                                 }
258                         }
259                 }
260                 
261                 BLI_freelistN(&material->lamps);
262
263                 MEM_freeN(material);
264         }
265
266         BLI_freelistN(gpumaterial);
267 }
268
269 void GPU_material_bind(
270         GPUMaterial *material, int oblay, int viewlay, double time, int mipmap,
271         float viewmat[4][4], float viewinv[4][4], float camerafactors[4], bool scenelock)
272 {
273         if (material->pass) {
274                 GPUShader *shader = GPU_pass_shader(material->pass);
275                 SceneRenderLayer *srl = scenelock ? BLI_findlink(&material->scene->r.layers, material->scene->r.actlay) : NULL;
276
277                 if (srl)
278                         viewlay &= srl->lay;
279
280                 /* handle layer lamps */
281                 if (material->type == GPU_MATERIAL_TYPE_MESH) {
282                         for (LinkData *nlink = material->lamps.first; nlink; nlink = nlink->next) {
283                                 GPULamp *lamp = nlink->data;
284                                 
285                                 if (!lamp->hide && (lamp->lay & viewlay) && (!(lamp->mode & LA_LAYER) || (lamp->lay & oblay)) &&
286                                     GPU_lamp_override_visible(lamp, srl, material->ma))
287                                 {
288                                         lamp->dynenergy = lamp->energy;
289                                         copy_v3_v3(lamp->dyncol, lamp->col);
290                                 }
291                                 else {
292                                         lamp->dynenergy = 0.0f;
293                                         lamp->dyncol[0] = lamp->dyncol[1] = lamp->dyncol[2] = 0.0f;
294                                 }
295                                 
296                                 if (material->dynproperty & DYN_LAMP_VEC) {
297                                         copy_v3_v3(lamp->dynvec, lamp->vec);
298                                         normalize_v3(lamp->dynvec);
299                                         negate_v3(lamp->dynvec);
300                                         mul_mat3_m4_v3(viewmat, lamp->dynvec);
301                                 }
302                                 
303                                 if (material->dynproperty & DYN_LAMP_CO) {
304                                         copy_v3_v3(lamp->dynco, lamp->co);
305                                         mul_m4_v3(viewmat, lamp->dynco);
306                                 }
307                                 
308                                 if (material->dynproperty & DYN_LAMP_IMAT) {
309                                         mul_m4_m4m4(lamp->dynimat, lamp->imat, viewinv);
310                                 }
311                                 
312                                 if (material->dynproperty & DYN_LAMP_PERSMAT) {
313                                         /* The lamp matrices are already updated if we're using shadow buffers */
314                                         if (!GPU_lamp_has_shadow_buffer(lamp)) {
315                                                 GPU_lamp_update_buffer_mats(lamp);
316                                         }
317                                         mul_m4_m4m4(lamp->dynpersmat, lamp->persmat, viewinv);
318                                 }
319                         }
320                 }
321                 
322                 /* note material must be bound before setting uniforms */
323                 GPU_pass_bind(material->pass, time, mipmap);
324
325                 /* handle per material built-ins */
326                 if (material->builtins & GPU_VIEW_MATRIX) {
327                         GPU_shader_uniform_vector(shader, material->viewmatloc, 16, 1, (float *)viewmat);
328                 }
329                 if (material->builtins & GPU_INVERSE_VIEW_MATRIX) {
330                         GPU_shader_uniform_vector(shader, material->invviewmatloc, 16, 1, (float *)viewinv);
331                 }
332                 if (material->builtins & GPU_CAMERA_TEXCO_FACTORS) {
333                         if (camerafactors) {
334                                 GPU_shader_uniform_vector(shader, material->cameratexcofacloc, 4, 1, (float *)camerafactors);
335                         }
336                         else {
337                                 /* use default, no scaling no offset */
338                                 float borders[4] = {1.0f, 1.0f, 0.0f, 0.0f};
339                                 GPU_shader_uniform_vector(shader, material->cameratexcofacloc, 4, 1, (float *)borders);
340                         }
341                 }
342
343                 GPU_pass_update_uniforms(material->pass);
344
345                 material->bound = 1;
346         }
347 }
348
349 void GPU_material_bind_uniforms(
350         GPUMaterial *material, float obmat[4][4], float viewmat[4][4], float obcol[4],
351         float autobumpscale, GPUParticleInfo *pi)
352 {
353         if (material->pass) {
354                 GPUShader *shader = GPU_pass_shader(material->pass);
355                 float invmat[4][4], col[4];
356                 float localtoviewmat[4][4];
357                 float invlocaltoviewmat[4][4];
358
359                 /* handle per object builtins */
360                 if (material->builtins & GPU_OBJECT_MATRIX) {
361                         GPU_shader_uniform_vector(shader, material->obmatloc, 16, 1, (float *)obmat);
362                 }
363                 if (material->builtins & GPU_INVERSE_OBJECT_MATRIX) {
364                         invert_m4_m4(invmat, obmat);
365                         GPU_shader_uniform_vector(shader, material->invobmatloc, 16, 1, (float *)invmat);
366                 }
367                 if (material->builtins & GPU_LOC_TO_VIEW_MATRIX) {
368                         if (viewmat) {
369                                 mul_m4_m4m4(localtoviewmat, viewmat, obmat);
370                                 GPU_shader_uniform_vector(shader, material->localtoviewmatloc, 16, 1, (float *)localtoviewmat);
371                         }
372                 }
373                 if (material->builtins & GPU_INVERSE_LOC_TO_VIEW_MATRIX) {
374                         if (viewmat) {
375                                 mul_m4_m4m4(localtoviewmat, viewmat, obmat);
376                                 invert_m4_m4(invlocaltoviewmat, localtoviewmat);
377                                 GPU_shader_uniform_vector(shader, material->invlocaltoviewmatloc, 16, 1, (float *)invlocaltoviewmat);
378                         }
379                 }
380                 if (material->builtins & GPU_OBCOLOR) {
381                         copy_v4_v4(col, obcol);
382                         CLAMP(col[3], 0.0f, 1.0f);
383                         GPU_shader_uniform_vector(shader, material->obcolloc, 4, 1, col);
384                 }
385                 if (material->builtins & GPU_AUTO_BUMPSCALE) {
386                         GPU_shader_uniform_vector(shader, material->obautobumpscaleloc, 1, 1, &autobumpscale);
387                 }
388                 if (material->builtins & GPU_PARTICLE_SCALAR_PROPS) {
389                         GPU_shader_uniform_vector(shader, material->partscalarpropsloc, 4, 1, pi->scalprops);
390                 }
391                 if (material->builtins & GPU_PARTICLE_LOCATION) {
392                         GPU_shader_uniform_vector(shader, material->partcoloc, 3, 1, pi->location);
393                 }
394                 if (material->builtins & GPU_PARTICLE_VELOCITY) {
395                         GPU_shader_uniform_vector(shader, material->partvel, 3, 1, pi->velocity);
396                 }
397                 if (material->builtins & GPU_PARTICLE_ANG_VELOCITY) {
398                         GPU_shader_uniform_vector(shader, material->partangvel, 3, 1, pi->angular_velocity);
399                 }
400
401         }
402 }
403
404 void GPU_material_unbind(GPUMaterial *material)
405 {
406         if (material->pass) {
407                 material->bound = 0;
408                 GPU_pass_unbind(material->pass);
409         }
410 }
411
412 bool GPU_material_bound(GPUMaterial *material)
413 {
414         return material->bound;
415 }
416
417 Scene *GPU_material_scene(GPUMaterial *material)
418 {
419         return material->scene;
420 }
421
422 GPUMatType GPU_Material_get_type(GPUMaterial *material)
423 {
424         return material->type;
425 }
426
427
428 void GPU_material_vertex_attributes(GPUMaterial *material, GPUVertexAttribs *attribs)
429 {
430         *attribs = material->attribs;
431 }
432
433 void GPU_material_output_link(GPUMaterial *material, GPUNodeLink *link)
434 {
435         if (!material->outlink)
436                 material->outlink = link;
437 }
438
439 void GPU_material_enable_alpha(GPUMaterial *material)
440 {
441         material->alpha = 1;
442 }
443
444 GPUBlendMode GPU_material_alpha_blend(GPUMaterial *material, float obcol[4])
445 {
446         if (material->alpha || (material->obcolalpha && obcol[3] < 1.0f))
447                 return GPU_BLEND_ALPHA;
448         else
449                 return GPU_BLEND_SOLID;
450 }
451
452 void gpu_material_add_node(GPUMaterial *material, GPUNode *node)
453 {
454         BLI_addtail(&material->nodes, node);
455 }
456
457 /* Code generation */
458
459 bool GPU_material_do_color_management(GPUMaterial *mat)
460 {
461         if (!BKE_scene_check_color_management_enabled(mat->scene))
462                 return false;
463
464         return true;
465 }
466
467 bool GPU_material_use_new_shading_nodes(GPUMaterial *mat)
468 {
469         return BKE_scene_use_new_shading_nodes(mat->scene);
470 }
471
472 bool GPU_material_use_world_space_shading(GPUMaterial *mat)
473 {
474         return BKE_scene_use_world_space_shading(mat->scene);
475 }
476
477 static GPUNodeLink *lamp_get_visibility(GPUMaterial *mat, GPULamp *lamp, GPUNodeLink **lv, GPUNodeLink **dist)
478 {
479         GPUNodeLink *visifac;
480
481         /* from get_lamp_visibility */
482         if (lamp->type == LA_SUN || lamp->type == LA_HEMI) {
483                 mat->dynproperty |= DYN_LAMP_VEC;
484                 GPU_link(mat, "lamp_visibility_sun_hemi",
485                          GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob), lv, dist, &visifac);
486                 return visifac;
487         }
488         else {
489                 mat->dynproperty |= DYN_LAMP_CO;
490                 GPU_link(mat, "lamp_visibility_other",
491                          GPU_builtin(GPU_VIEW_POSITION),
492                          GPU_dynamic_uniform(lamp->dynco, GPU_DYNAMIC_LAMP_DYNCO, lamp->ob), lv, dist, &visifac);
493
494                 if (lamp->type == LA_AREA)
495                         return visifac;
496
497                 switch (lamp->falloff_type) {
498                         case LA_FALLOFF_CONSTANT:
499                                 break;
500                         case LA_FALLOFF_INVLINEAR:
501                                 GPU_link(mat, "lamp_falloff_invlinear",
502                                          GPU_dynamic_uniform(&lamp->dist, GPU_DYNAMIC_LAMP_DISTANCE, lamp->ob), *dist, &visifac);
503                                 break;
504                         case LA_FALLOFF_INVSQUARE:
505                                 GPU_link(mat, "lamp_falloff_invsquare",
506                                          GPU_dynamic_uniform(&lamp->dist, GPU_DYNAMIC_LAMP_DISTANCE, lamp->ob), *dist, &visifac);
507                                 break;
508                         case LA_FALLOFF_SLIDERS:
509                                 GPU_link(mat, "lamp_falloff_sliders",
510                                          GPU_dynamic_uniform(&lamp->dist, GPU_DYNAMIC_LAMP_DISTANCE, lamp->ob),
511                                          GPU_dynamic_uniform(&lamp->att1, GPU_DYNAMIC_LAMP_ATT1, lamp->ob),
512                                          GPU_dynamic_uniform(&lamp->att2, GPU_DYNAMIC_LAMP_ATT2, lamp->ob), *dist, &visifac);
513                                 break;
514                         case LA_FALLOFF_INVCOEFFICIENTS:
515                                 GPU_link(mat, "lamp_falloff_invcoefficients",
516                                              GPU_dynamic_uniform(&lamp->coeff_const, GPU_DYNAMIC_LAMP_COEFFCONST, lamp->ob),
517                                              GPU_dynamic_uniform(&lamp->coeff_lin, GPU_DYNAMIC_LAMP_COEFFLIN, lamp->ob),
518                                              GPU_dynamic_uniform(&lamp->coeff_quad, GPU_DYNAMIC_LAMP_COEFFQUAD, lamp->ob), *dist, &visifac);
519                                 break;
520                         case LA_FALLOFF_CURVE:
521                         {
522                                 float *array;
523                                 int size;
524
525                                 curvemapping_initialize(lamp->curfalloff);
526                                 curvemapping_table_RGBA(lamp->curfalloff, &array, &size);
527                                 GPU_link(mat, "lamp_falloff_curve",
528                                          GPU_dynamic_uniform(&lamp->dist, GPU_DYNAMIC_LAMP_DISTANCE, lamp->ob),
529                                          GPU_texture(size, array), *dist, &visifac);
530
531                                 break;
532                         }
533                 }
534
535                 if (lamp->mode & LA_SPHERE)
536                         GPU_link(mat, "lamp_visibility_sphere",
537                                  GPU_dynamic_uniform(&lamp->dist, GPU_DYNAMIC_LAMP_DISTANCE, lamp->ob),
538                                  *dist, visifac, &visifac);
539
540                 if (lamp->type == LA_SPOT) {
541                         GPUNodeLink *inpr;
542
543                         if (lamp->mode & LA_SQUARE) {
544                                 mat->dynproperty |= DYN_LAMP_VEC | DYN_LAMP_IMAT;
545                                 GPU_link(mat, "lamp_visibility_spot_square",
546                                          GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob),
547                                          GPU_dynamic_uniform((float *)lamp->dynimat, GPU_DYNAMIC_LAMP_DYNIMAT, lamp->ob),
548                                 GPU_dynamic_uniform((float *)lamp->spotvec, GPU_DYNAMIC_LAMP_SPOTSCALE, lamp->ob), *lv, &inpr);
549                         }
550                         else {
551                                 mat->dynproperty |= DYN_LAMP_VEC | DYN_LAMP_IMAT;
552                                 GPU_link(mat, "lamp_visibility_spot_circle",
553                                          GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob),
554                                 GPU_dynamic_uniform((float *)lamp->dynimat, GPU_DYNAMIC_LAMP_DYNIMAT, lamp->ob),
555                                 GPU_dynamic_uniform((float *)lamp->spotvec, GPU_DYNAMIC_LAMP_SPOTSCALE, lamp->ob), *lv, &inpr);
556                         }
557                         
558                         GPU_link(mat, "lamp_visibility_spot",
559                                  GPU_dynamic_uniform(&lamp->spotsi, GPU_DYNAMIC_LAMP_SPOTSIZE, lamp->ob),
560                                  GPU_dynamic_uniform(&lamp->spotbl, GPU_DYNAMIC_LAMP_SPOTBLEND, lamp->ob),
561                                  inpr, visifac, &visifac);
562                 }
563
564                 GPU_link(mat, "lamp_visibility_clamp", visifac, &visifac);
565
566                 return visifac;
567         }
568 }
569
570 #if 0
571 static void area_lamp_vectors(LampRen *lar)
572 {
573         float xsize = 0.5f * lar->area_size, ysize = 0.5f * lar->area_sizey;
574
575         /* make it smaller, so area light can be multisampled */
576         float multifac = 1.0f / sqrtf((float)lar->ray_totsamp);
577         xsize *= multifac;
578         ysize *= multifac;
579
580         /* corner vectors */
581         lar->area[0][0] = lar->co[0] - xsize * lar->mat[0][0] - ysize * lar->mat[1][0];
582         lar->area[0][1] = lar->co[1] - xsize * lar->mat[0][1] - ysize * lar->mat[1][1];
583         lar->area[0][2] = lar->co[2] - xsize * lar->mat[0][2] - ysize * lar->mat[1][2];
584
585         /* corner vectors */
586         lar->area[1][0] = lar->co[0] - xsize * lar->mat[0][0] + ysize * lar->mat[1][0];
587         lar->area[1][1] = lar->co[1] - xsize * lar->mat[0][1] + ysize * lar->mat[1][1];
588         lar->area[1][2] = lar->co[2] - xsize * lar->mat[0][2] + ysize * lar->mat[1][2];
589
590         /* corner vectors */
591         lar->area[2][0] = lar->co[0] + xsize * lar->mat[0][0] + ysize * lar->mat[1][0];
592         lar->area[2][1] = lar->co[1] + xsize * lar->mat[0][1] + ysize * lar->mat[1][1];
593         lar->area[2][2] = lar->co[2] + xsize * lar->mat[0][2] + ysize * lar->mat[1][2];
594
595         /* corner vectors */
596         lar->area[3][0] = lar->co[0] + xsize * lar->mat[0][0] - ysize * lar->mat[1][0];
597         lar->area[3][1] = lar->co[1] + xsize * lar->mat[0][1] - ysize * lar->mat[1][1];
598         lar->area[3][2] = lar->co[2] + xsize * lar->mat[0][2] - ysize * lar->mat[1][2];
599         /* only for correction button size, matrix size works on energy */
600         lar->areasize = lar->dist * lar->dist / (4.0f * xsize * ysize);
601 }
602 #endif
603
604 static void ramp_blend(
605         GPUMaterial *mat, GPUNodeLink *fac, GPUNodeLink *col1, GPUNodeLink *col2, int type,
606         GPUNodeLink **r_col)
607 {
608         static const char *names[] = {"mix_blend", "mix_add", "mix_mult", "mix_sub",
609                 "mix_screen", "mix_div", "mix_diff", "mix_dark", "mix_light",
610                 "mix_overlay", "mix_dodge", "mix_burn", "mix_hue", "mix_sat",
611                 "mix_val", "mix_color", "mix_soft", "mix_linear"};
612
613         GPU_link(mat, names[type], fac, col1, col2, r_col);
614 }
615
616 static void do_colorband_blend(
617         GPUMaterial *mat, ColorBand *coba, GPUNodeLink *fac, float rampfac, int type,
618         GPUNodeLink *incol, GPUNodeLink **r_col)
619 {
620         GPUNodeLink *tmp, *alpha, *col;
621         float *array;
622         int size;
623
624         /* do colorband */
625         colorband_table_RGBA(coba, &array, &size);
626         GPU_link(mat, "valtorgb", fac, GPU_texture(size, array), &col, &tmp);
627
628         /* use alpha in fac */
629         GPU_link(mat, "mtex_alpha_from_col", col, &alpha);
630         GPU_link(mat, "math_multiply", alpha, GPU_uniform(&rampfac), &fac);
631
632         /* blending method */
633         ramp_blend(mat, fac, incol, col, type, r_col);
634 }
635
636 static void ramp_diffuse_result(GPUShadeInput *shi, GPUNodeLink **diff)
637 {
638         Material *ma = shi->mat;
639         GPUMaterial *mat = shi->gpumat;
640
641         if (!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS)) {
642                 if (ma->ramp_col) {
643                         if (ma->rampin_col == MA_RAMP_IN_RESULT) {
644                                 GPUNodeLink *fac;
645                                 GPU_link(mat, "ramp_rgbtobw", *diff, &fac);
646                                 
647                                 /* colorband + blend */
648                                 do_colorband_blend(mat, ma->ramp_col, fac, ma->rampfac_col, ma->rampblend_col, *diff, diff);
649                         }
650                 }
651         }
652 }
653
654 static void add_to_diffuse(
655         GPUMaterial *mat, Material *ma, GPUShadeInput *shi, GPUNodeLink *is, GPUNodeLink *rgb,
656         GPUNodeLink **r_diff)
657 {
658         GPUNodeLink *fac, *tmp, *addcol;
659         
660         if (!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS) &&
661             ma->ramp_col && (ma->mode & MA_RAMP_COL))
662         {
663                 /* MA_RAMP_IN_RESULT is exceptional */
664                 if (ma->rampin_col == MA_RAMP_IN_RESULT) {
665                         addcol = shi->rgb;
666                 }
667                 else {
668                         /* input */
669                         switch (ma->rampin_col) {
670                                 case MA_RAMP_IN_ENERGY:
671                                         GPU_link(mat, "ramp_rgbtobw", rgb, &fac);
672                                         break;
673                                 case MA_RAMP_IN_SHADER:
674                                         fac = is;
675                                         break;
676                                 case MA_RAMP_IN_NOR:
677                                         GPU_link(mat, "vec_math_dot", shi->view, shi->vn, &tmp, &fac);
678                                         break;
679                                 default:
680                                         GPU_link(mat, "set_value_zero", &fac);
681                                         break;
682                         }
683
684                         /* colorband + blend */
685                         do_colorband_blend(mat, ma->ramp_col, fac, ma->rampfac_col, ma->rampblend_col, shi->rgb, &addcol);
686                 }
687         }
688         else
689                 addcol = shi->rgb;
690
691         /* output to */
692         GPU_link(mat, "shade_madd", *r_diff, rgb, addcol, r_diff);
693 }
694
695 static void ramp_spec_result(GPUShadeInput *shi, GPUNodeLink **spec)
696 {
697         Material *ma = shi->mat;
698         GPUMaterial *mat = shi->gpumat;
699
700         if (!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS) &&
701             ma->ramp_spec && ma->rampin_spec == MA_RAMP_IN_RESULT)
702         {
703                 GPUNodeLink *fac;
704                 GPU_link(mat, "ramp_rgbtobw", *spec, &fac);
705                 
706                 /* colorband + blend */
707                 do_colorband_blend(mat, ma->ramp_spec, fac, ma->rampfac_spec, ma->rampblend_spec, *spec, spec);
708         }
709 }
710
711 static void do_specular_ramp(GPUShadeInput *shi, GPUNodeLink *is, GPUNodeLink *t, GPUNodeLink **spec)
712 {
713         Material *ma = shi->mat;
714         GPUMaterial *mat = shi->gpumat;
715         GPUNodeLink *fac, *tmp;
716
717         *spec = shi->specrgb;
718
719         /* MA_RAMP_IN_RESULT is exception */
720         if (ma->ramp_spec && (ma->rampin_spec != MA_RAMP_IN_RESULT)) {
721                 
722                 /* input */
723                 switch (ma->rampin_spec) {
724                         case MA_RAMP_IN_ENERGY:
725                                 fac = t;
726                                 break;
727                         case MA_RAMP_IN_SHADER:
728                                 fac = is;
729                                 break;
730                         case MA_RAMP_IN_NOR:
731                                 GPU_link(mat, "vec_math_dot", shi->view, shi->vn, &tmp, &fac);
732                                 break;
733                         default:
734                                 GPU_link(mat, "set_value_zero", &fac);
735                                 break;
736                 }
737                 
738                 /* colorband + blend */
739                 do_colorband_blend(mat, ma->ramp_spec, fac, ma->rampfac_spec, ma->rampblend_spec, *spec, spec);
740         }
741 }
742
743 static void add_user_list(ListBase *list, void *data)
744 {
745         LinkData *link = MEM_callocN(sizeof(LinkData), "GPULinkData");
746         link->data = data;
747         BLI_addtail(list, link);
748 }
749
750 static void shade_light_textures(GPUMaterial *mat, GPULamp *lamp, GPUNodeLink **rgb)
751 {
752         for (int i = 0; i < MAX_MTEX; ++i) {
753                 MTex *mtex = lamp->la->mtex[i];
754
755                 if (mtex && mtex->tex && (mtex->tex->type & TEX_IMAGE) && mtex->tex->ima) {
756                         mat->dynproperty |= DYN_LAMP_PERSMAT;
757
758                         float one = 1.0f;
759                         GPUNodeLink *tex_rgb;
760
761                         GPU_link(mat, "shade_light_texture",
762                                  GPU_builtin(GPU_VIEW_POSITION),
763                                  GPU_image(mtex->tex->ima, &mtex->tex->iuser, false),
764                                  GPU_dynamic_uniform((float *)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
765                                  &tex_rgb);
766                         texture_rgb_blend(mat, tex_rgb, *rgb, GPU_uniform(&one), GPU_uniform(&mtex->colfac), mtex->blendtype, rgb);
767                 }
768         }
769 }
770
771 static void shade_one_light(GPUShadeInput *shi, GPUShadeResult *shr, GPULamp *lamp)
772 {
773         Material *ma = shi->mat;
774         GPUMaterial *mat = shi->gpumat;
775         GPUNodeLink *lv, *dist, *is, *inp, *i;
776         GPUNodeLink *outcol, *specfac, *t, *shadfac = NULL, *lcol;
777         float one = 1.0f;
778
779         if ((lamp->mode & LA_ONLYSHADOW) && !(ma->mode & MA_SHADOW))
780                 return;
781         
782         GPUNodeLink *vn = shi->vn;
783         GPUNodeLink *view = shi->view;
784
785         GPUNodeLink *visifac = lamp_get_visibility(mat, lamp, &lv, &dist);
786
787 #if 0
788         if (ma->mode & MA_TANGENT_V)
789                 GPU_link(mat, "shade_tangent_v", lv, GPU_attribute(CD_TANGENT, ""), &vn);
790 #endif
791         
792         GPU_link(mat, "shade_inp", vn, lv, &inp);
793
794         if (lamp->mode & LA_NO_DIFF) {
795                 GPU_link(mat, "shade_is_no_diffuse", &is);
796         }
797         else if (lamp->type == LA_HEMI) {
798                 GPU_link(mat, "shade_is_hemi", inp, &is);
799         }
800         else {
801                 if (lamp->type == LA_AREA) {
802                         float area[4][4] = {{0.0f}}, areasize = 0.0f;
803
804                         mat->dynproperty |= DYN_LAMP_VEC | DYN_LAMP_CO;
805                         GPU_link(mat, "shade_inp_area",
806                                  GPU_builtin(GPU_VIEW_POSITION),
807                                  GPU_dynamic_uniform(lamp->dynco, GPU_DYNAMIC_LAMP_DYNCO, lamp->ob),
808                                  GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob), vn,
809                                  GPU_uniform((float *)area),
810                                  GPU_uniform(&areasize),
811                                  GPU_uniform(&lamp->k), &inp);
812                 }
813
814                 is = inp; /* Lambert */
815
816                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_SHADERS)) {
817                         if (ma->diff_shader == MA_DIFF_ORENNAYAR)
818                                 GPU_link(mat, "shade_diffuse_oren_nayer", inp, vn, lv, view,
819                                          GPU_uniform(&ma->roughness), &is);
820                         else if (ma->diff_shader == MA_DIFF_TOON)
821                                 GPU_link(mat, "shade_diffuse_toon", vn, lv, view,
822                                          GPU_uniform(&ma->param[0]), GPU_uniform(&ma->param[1]), &is);
823                         else if (ma->diff_shader == MA_DIFF_MINNAERT)
824                                 GPU_link(mat, "shade_diffuse_minnaert", inp, vn, view,
825                                          GPU_uniform(&ma->darkness), &is);
826                         else if (ma->diff_shader == MA_DIFF_FRESNEL)
827                                 GPU_link(mat, "shade_diffuse_fresnel", vn, lv, view,
828                                          GPU_uniform(&ma->param[0]), GPU_uniform(&ma->param[1]), &is);
829                 }
830         }
831
832         if (!(mat->scene->gm.flag & GAME_GLSL_NO_SHADERS))
833                 if (ma->shade_flag & MA_CUBIC)
834                         GPU_link(mat, "shade_cubic", is, &is);
835         
836         i = is;
837         GPU_link(mat, "shade_visifac", i, visifac, shi->refl, &i);
838         
839         GPU_link(mat, "set_rgb", GPU_dynamic_uniform(lamp->dyncol, GPU_DYNAMIC_LAMP_DYNCOL, lamp->ob), &lcol);
840         shade_light_textures(mat, lamp, &lcol);
841         GPU_link(mat, "shade_mul_value_v3",
842                  GPU_dynamic_uniform(&lamp->dynenergy, GPU_DYNAMIC_LAMP_DYNENERGY, lamp->ob), lcol, &lcol);
843
844 #if 0
845         if (ma->mode & MA_TANGENT_VN)
846                 GPU_link(mat, "shade_tangent_v_spec", GPU_attribute(CD_TANGENT, ""), &vn);
847 #endif
848
849         /* this replaces if (i > 0.0) conditional until that is supported */
850         /* done in shade_visifac now, GPU_link(mat, "mtex_value_clamp_positive", i, &i); */
851
852         if ((ma->mode & MA_SHADOW) && GPU_lamp_has_shadow_buffer(lamp)) {
853                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_SHADOWS)) {
854                         mat->dynproperty |= DYN_LAMP_PERSMAT;
855                         
856                         if (lamp->la->shadowmap_type == LA_SHADMAP_VARIANCE) {
857                                 GPU_link(mat, "test_shadowbuf_vsm",
858                                          GPU_builtin(GPU_VIEW_POSITION),
859                                          GPU_dynamic_texture(lamp->tex, GPU_DYNAMIC_SAMPLER_2DSHADOW, lamp->ob),
860                                          GPU_dynamic_uniform((float *)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
861                                          GPU_uniform(&lamp->bias), GPU_uniform(&lamp->la->bleedbias), inp, &shadfac);
862                         }
863                         else {
864                                 GPU_link(mat, "test_shadowbuf",
865                                          GPU_builtin(GPU_VIEW_POSITION),
866                                          GPU_dynamic_texture(lamp->tex, GPU_DYNAMIC_SAMPLER_2DSHADOW, lamp->ob),
867                                          GPU_dynamic_uniform((float *)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
868                                          GPU_uniform(&lamp->bias), inp, &shadfac);
869                         }
870                         
871                         if (lamp->mode & LA_ONLYSHADOW) {
872                                 GPUNodeLink *shadrgb;
873                                 GPU_link(mat, "shade_only_shadow", i, shadfac,
874                                         GPU_dynamic_uniform(&lamp->dynenergy, GPU_DYNAMIC_LAMP_DYNENERGY, lamp->ob),
875                                         GPU_uniform(lamp->shadow_color), &shadrgb);
876                                 
877                                 if (!(lamp->mode & LA_NO_DIFF)) {
878                                         GPU_link(mat, "shade_only_shadow_diffuse", shadrgb, shi->rgb,
879                                                  shr->diff, &shr->diff);
880                                 }
881
882                                 if (!(lamp->mode & LA_NO_SPEC)) {
883                                         GPU_link(mat, "shade_only_shadow_specular", shadrgb, shi->specrgb,
884                                                  shr->spec, &shr->spec);
885                                 }
886                                 
887                                 add_user_list(&mat->lamps, lamp);
888                                 add_user_list(&lamp->materials, shi->gpumat->ma);
889                                 return;
890                         }
891                 }
892         }
893         else if ((mat->scene->gm.flag & GAME_GLSL_NO_SHADOWS) && (lamp->mode & LA_ONLYSHADOW)) {
894                 add_user_list(&mat->lamps, lamp);
895                 add_user_list(&lamp->materials, shi->gpumat->ma);
896                 return;
897         }
898         else
899                 GPU_link(mat, "set_value", GPU_uniform(&one), &shadfac);
900
901         if (GPU_link_changed(shi->refl) || ma->ref != 0.0f) {
902                 if (!(lamp->mode & LA_NO_DIFF)) {
903                         GPUNodeLink *rgb;
904                         GPU_link(mat, "shade_mul_value", i, lcol, &rgb);
905                         GPU_link(mat, "mtex_value_invert", shadfac, &shadfac);
906                         GPU_link(mat, "mix_mult",  shadfac, rgb, GPU_uniform(lamp->shadow_color), &rgb);
907                         GPU_link(mat, "mtex_value_invert", shadfac, &shadfac);
908                         add_to_diffuse(mat, ma, shi, is, rgb, &shr->diff);
909                 }
910         }
911
912         if (mat->scene->gm.flag & GAME_GLSL_NO_SHADERS) {
913                 /* pass */
914         }
915         else if (!(lamp->mode & LA_NO_SPEC) && !(lamp->mode & LA_ONLYSHADOW) &&
916                  (GPU_link_changed(shi->spec) || ma->spec != 0.0f))
917         {
918                 if (lamp->type == LA_HEMI) {
919                         GPU_link(mat, "shade_hemi_spec", vn, lv, view, GPU_uniform(&ma->spec), shi->har, visifac, &t);
920                         GPU_link(mat, "shade_add_spec", t, lcol, shi->specrgb, &outcol);
921                         GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
922                 }
923                 else {
924                         if (ma->spec_shader == MA_SPEC_PHONG) {
925                                 GPU_link(mat, "shade_phong_spec", vn, lv, view, shi->har, &specfac);
926                         }
927                         else if (ma->spec_shader == MA_SPEC_COOKTORR) {
928                                 GPU_link(mat, "shade_cooktorr_spec", vn, lv, view, shi->har, &specfac);
929                         }
930                         else if (ma->spec_shader == MA_SPEC_BLINN) {
931                                 GPU_link(mat, "shade_blinn_spec", vn, lv, view,
932                                          GPU_uniform(&ma->refrac), shi->har, &specfac);
933                         }
934                         else if (ma->spec_shader == MA_SPEC_WARDISO) {
935                                 GPU_link(mat, "shade_wardiso_spec", vn, lv, view,
936                                          GPU_uniform(&ma->rms), &specfac);
937                         }
938                         else {
939                                 GPU_link(mat, "shade_toon_spec", vn, lv, view,
940                                          GPU_uniform(&ma->param[2]), GPU_uniform(&ma->param[3]), &specfac);
941                         }
942
943                         if (lamp->type == LA_AREA)
944                                 GPU_link(mat, "shade_spec_area_inp", specfac, inp, &specfac);
945
946                         GPU_link(mat, "shade_spec_t", shadfac, shi->spec, visifac, specfac, &t); 
947
948                         if (ma->mode & MA_RAMP_SPEC) {
949                                 GPUNodeLink *spec;
950                                 do_specular_ramp(shi, specfac, t, &spec);
951                                 GPU_link(mat, "shade_add_spec", t, lcol, spec, &outcol);
952                                 GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
953                         }
954                         else {
955                                 GPU_link(mat, "shade_add_spec", t, lcol, shi->specrgb, &outcol);
956                                 GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
957                         }
958                 }
959         }
960
961         add_user_list(&mat->lamps, lamp);
962         add_user_list(&lamp->materials, shi->gpumat->ma);
963 }
964
965 static void material_lights(GPUShadeInput *shi, GPUShadeResult *shr)
966 {
967         Base *base;
968         Scene *sce_iter;
969         
970         for (SETLOOPER(shi->gpumat->scene, sce_iter, base)) {
971                 Object *ob = base->object;
972
973                 if (ob->type == OB_LAMP) {
974                         GPULamp *lamp = GPU_lamp_from_blender(shi->gpumat->scene, ob, NULL);
975                         if (lamp)
976                                 shade_one_light(shi, shr, lamp);
977                 }
978
979                 if (ob->transflag & OB_DUPLI) {
980                         ListBase *lb = object_duplilist(G.main->eval_ctx, shi->gpumat->scene, ob);
981                         
982                         for (DupliObject *dob = lb->first; dob; dob = dob->next) {
983                                 Object *ob_iter = dob->ob;
984
985                                 if (ob_iter->type == OB_LAMP) {
986                                         float omat[4][4];
987                                         copy_m4_m4(omat, ob_iter->obmat);
988                                         copy_m4_m4(ob_iter->obmat, dob->mat);
989
990                                         GPULamp *lamp = GPU_lamp_from_blender(shi->gpumat->scene, ob_iter, ob);
991                                         if (lamp)
992                                                 shade_one_light(shi, shr, lamp);
993
994                                         copy_m4_m4(ob_iter->obmat, omat);
995                                 }
996                         }
997                         
998                         free_object_duplilist(lb);
999                 }
1000         }
1001
1002         /* prevent only shadow lamps from producing negative colors.*/
1003         GPU_link(shi->gpumat, "shade_clamp_positive", shr->spec, &shr->spec);
1004         GPU_link(shi->gpumat, "shade_clamp_positive", shr->diff, &shr->diff);
1005 }
1006
1007 static void texture_rgb_blend(
1008         GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg,
1009         int blendtype, GPUNodeLink **in)
1010 {
1011         switch (blendtype) {
1012                 case MTEX_BLEND:
1013                         GPU_link(mat, "mtex_rgb_blend", out, tex, fact, facg, in);
1014                         break;
1015                 case MTEX_MUL:
1016                         GPU_link(mat, "mtex_rgb_mul", out, tex, fact, facg, in);
1017                         break;
1018                 case MTEX_SCREEN:
1019                         GPU_link(mat, "mtex_rgb_screen", out, tex, fact, facg, in);
1020                         break;
1021                 case MTEX_OVERLAY:
1022                         GPU_link(mat, "mtex_rgb_overlay", out, tex, fact, facg, in);
1023                         break;
1024                 case MTEX_SUB:
1025                         GPU_link(mat, "mtex_rgb_sub", out, tex, fact, facg, in);
1026                         break;
1027                 case MTEX_ADD:
1028                         GPU_link(mat, "mtex_rgb_add", out, tex, fact, facg, in);
1029                         break;
1030                 case MTEX_DIV:
1031                         GPU_link(mat, "mtex_rgb_div", out, tex, fact, facg, in);
1032                         break;
1033                 case MTEX_DIFF:
1034                         GPU_link(mat, "mtex_rgb_diff", out, tex, fact, facg, in);
1035                         break;
1036                 case MTEX_DARK:
1037                         GPU_link(mat, "mtex_rgb_dark", out, tex, fact, facg, in);
1038                         break;
1039                 case MTEX_LIGHT:
1040                         GPU_link(mat, "mtex_rgb_light", out, tex, fact, facg, in);
1041                         break;
1042                 case MTEX_BLEND_HUE:
1043                         GPU_link(mat, "mtex_rgb_hue", out, tex, fact, facg, in);
1044                         break;
1045                 case MTEX_BLEND_SAT:
1046                         GPU_link(mat, "mtex_rgb_sat", out, tex, fact, facg, in);
1047                         break;
1048                 case MTEX_BLEND_VAL:
1049                         GPU_link(mat, "mtex_rgb_val", out, tex, fact, facg, in);
1050                         break;
1051                 case MTEX_BLEND_COLOR:
1052                         GPU_link(mat, "mtex_rgb_color", out, tex, fact, facg, in);
1053                         break;
1054                 case MTEX_SOFT_LIGHT:
1055                         GPU_link(mat, "mtex_rgb_soft", out, tex, fact, facg, in);
1056                         break;
1057                 case MTEX_LIN_LIGHT:
1058                         GPU_link(mat, "mtex_rgb_linear", out, tex, fact, facg, in);
1059                         break;
1060                 default:
1061                         GPU_link(mat, "set_rgb_zero", &in);
1062                         break;
1063         }
1064 }
1065
1066 static void texture_value_blend(
1067         GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg,
1068         int blendtype, GPUNodeLink **in)
1069 {
1070         switch (blendtype) {
1071                 case MTEX_BLEND:
1072                         GPU_link(mat, "mtex_value_blend", out, tex, fact, facg, in);
1073                         break;
1074                 case MTEX_MUL:
1075                         GPU_link(mat, "mtex_value_mul", out, tex, fact, facg, in);
1076                         break;
1077                 case MTEX_SCREEN:
1078                         GPU_link(mat, "mtex_value_screen", out, tex, fact, facg, in);
1079                         break;
1080                 case MTEX_SUB:
1081                         GPU_link(mat, "mtex_value_sub", out, tex, fact, facg, in);
1082                         break;
1083                 case MTEX_ADD:
1084                         GPU_link(mat, "mtex_value_add", out, tex, fact, facg, in);
1085                         break;
1086                 case MTEX_DIV:
1087                         GPU_link(mat, "mtex_value_div", out, tex, fact, facg, in);
1088                         break;
1089                 case MTEX_DIFF:
1090                         GPU_link(mat, "mtex_value_diff", out, tex, fact, facg, in);
1091                         break;
1092                 case MTEX_DARK:
1093                         GPU_link(mat, "mtex_value_dark", out, tex, fact, facg, in);
1094                         break;
1095                 case MTEX_LIGHT:
1096                         GPU_link(mat, "mtex_value_light", out, tex, fact, facg, in);
1097                         break;
1098                 default:
1099                         GPU_link(mat, "set_value_zero", &in);
1100                         break;
1101         }
1102 }
1103
1104 static void do_material_tex(GPUShadeInput *shi)
1105 {
1106         Material *ma = shi->mat;
1107         GPUMaterial *mat = shi->gpumat;
1108         MTex *mtex;
1109         Tex *tex;
1110         GPUNodeLink *texco, *tin, *trgb, *tnor, *tcol, *stencil, *tnorfac;
1111         GPUNodeLink *texco_norm, *texco_orco, *texco_object;
1112         GPUNodeLink *texco_global, *texco_uv = NULL;
1113         GPUNodeLink *newnor, *orn;
1114         float one = 1.0f;
1115         int rgbnor, talpha;
1116         bool init_done = false;
1117         int iBumpSpacePrev = 0; /* Not necessary, quieting gcc warning. */
1118         GPUNodeLink *vNorg, *vNacc, *fPrevMagnitude;
1119         int iFirstTimeNMap = 1;
1120         bool found_deriv_map = false;
1121
1122         GPU_link(mat, "set_value", GPU_uniform(&one), &stencil);
1123
1124         GPU_link(mat, "texco_norm", GPU_builtin(GPU_VIEW_NORMAL), &texco_norm);
1125         GPU_link(mat, "texco_orco", GPU_attribute(CD_ORCO, ""), &texco_orco);
1126         GPU_link(mat, "texco_object", GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
1127                 GPU_builtin(GPU_INVERSE_OBJECT_MATRIX),
1128                 GPU_builtin(GPU_VIEW_POSITION), &texco_object);
1129 #if 0
1130         GPU_link(mat, "texco_tangent", GPU_attribute(CD_TANGENT, ""), &texco_tangent);
1131 #endif
1132         GPU_link(mat, "texco_global", GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
1133                 GPU_builtin(GPU_VIEW_POSITION), &texco_global);
1134
1135         orn = texco_norm;
1136
1137         /* go over texture slots */
1138         for (int tex_nr = 0; tex_nr < MAX_MTEX; tex_nr++) {
1139                 /* separate tex switching */
1140                 if (ma->septex & (1 << tex_nr)) continue;
1141                 
1142                 if (ma->mtex[tex_nr]) {
1143                         mtex = ma->mtex[tex_nr];
1144                         
1145                         tex = mtex->tex;
1146                         if (tex == NULL) continue;
1147
1148                         /* which coords */
1149                         if (mtex->texco == TEXCO_ORCO)
1150                                 texco = texco_orco;
1151                         else if (mtex->texco == TEXCO_OBJECT)
1152                                 texco = texco_object;
1153                         else if (mtex->texco == TEXCO_NORM)
1154                                 texco = orn;
1155                         else if (mtex->texco == TEXCO_TANGENT)
1156                                 texco = texco_object;
1157                         else if (mtex->texco == TEXCO_GLOB)
1158                                 texco = texco_global;
1159                         else if (mtex->texco == TEXCO_REFL) {
1160                                 GPU_link(mat, "texco_refl", shi->vn, shi->view, &shi->ref);
1161                                 texco = shi->ref;
1162                         }
1163                         else if (mtex->texco == TEXCO_UV) {
1164                                 if (1) { //!(texco_uv && strcmp(mtex->uvname, lastuvname) == 0)) {
1165                                         GPU_link(mat, "texco_uv", GPU_attribute(CD_MTFACE, mtex->uvname), &texco_uv);
1166                                         /*lastuvname = mtex->uvname;*/ /*UNUSED*/
1167                                 }
1168                                 texco = texco_uv;
1169                         }
1170                         else
1171                                 continue;
1172
1173                         /* in case of uv, this would just undo a multiplication in texco_uv */
1174                         if (mtex->texco != TEXCO_UV)
1175                                 GPU_link(mat, "mtex_2d_mapping", texco, &texco);
1176
1177                         if (mtex->size[0] != 1.0f || mtex->size[1] != 1.0f || mtex->size[2] != 1.0f)
1178                                 GPU_link(mat, "mtex_mapping_size", texco, GPU_uniform(mtex->size), &texco);
1179
1180                         float ofs[3] = {
1181                                 mtex->ofs[0] + 0.5f - 0.5f * mtex->size[0],
1182                                 mtex->ofs[1] + 0.5f - 0.5f * mtex->size[1],
1183                                 0.0f
1184                         };
1185
1186                         if (ofs[0] != 0.0f || ofs[1] != 0.0f || ofs[2] != 0.0f)
1187                                 GPU_link(mat, "mtex_mapping_ofs", texco, GPU_uniform(ofs), &texco);
1188
1189                         talpha = 0;
1190
1191                         if (tex && tex->ima &&
1192                             ((tex->type == TEX_IMAGE) ||
1193                              ((tex->type == TEX_ENVMAP) && (mtex->texco == TEXCO_REFL))))
1194                         {
1195                                 if (tex->type == TEX_IMAGE) {
1196                                         GPU_link(mat, "mtex_image", texco, GPU_image(tex->ima, &tex->iuser, false), &tin, &trgb);
1197                                 }
1198                                 else {
1199                                         GPU_link(mat, "mtex_cube_map_refl",
1200                                                  GPU_cube_map(tex->ima, &tex->iuser, false), shi->view, shi->vn,
1201                                                  GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
1202                                                  GPU_builtin(GPU_VIEW_MATRIX), &tin, &trgb);
1203                                 }
1204                                 rgbnor = TEX_RGB;
1205
1206                                 talpha = ((tex->imaflag & TEX_USEALPHA) && tex->ima && (tex->ima->flag & IMA_IGNORE_ALPHA) == 0);
1207                         }
1208                         else {
1209                                 continue;
1210                         }
1211
1212                         /* texture output */
1213                         if ((rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1214                                 GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
1215                                 rgbnor -= TEX_RGB;
1216                         }
1217
1218                         if (mtex->texflag & MTEX_NEGATIVE) {
1219                                 if (rgbnor & TEX_RGB)
1220                                         GPU_link(mat, "mtex_rgb_invert", trgb, &trgb);
1221                                 else
1222                                         GPU_link(mat, "mtex_value_invert", tin, &tin);
1223                         }
1224
1225                         if (mtex->texflag & MTEX_STENCIL) {
1226                                 if (rgbnor & TEX_RGB)
1227                                         GPU_link(mat, "mtex_rgb_stencil", stencil, trgb, &stencil, &trgb);
1228                                 else
1229                                         GPU_link(mat, "mtex_value_stencil", stencil, tin, &stencil, &tin);
1230                         }
1231
1232                         /* mapping */
1233                         if (mtex->mapto & (MAP_COL | MAP_COLSPEC | MAP_COLMIR)) {
1234                                 /* stencil maps on the texture control slider, not texture intensity value */
1235                                 if ((rgbnor & TEX_RGB) == 0) {
1236                                         GPU_link(mat, "set_rgb", GPU_uniform(&mtex->r), &tcol);
1237                                 }
1238                                 else {
1239                                         GPU_link(mat, "set_rgba", trgb, &tcol);
1240
1241                                         if (mtex->mapto & MAP_ALPHA)
1242                                                 GPU_link(mat, "set_value", stencil, &tin);
1243                                         else if (talpha)
1244                                                 GPU_link(mat, "mtex_alpha_from_col", trgb, &tin);
1245                                         else
1246                                                 GPU_link(mat, "set_value_one", &tin);
1247                                 }
1248
1249                                 if ((tex->type == TEX_IMAGE) ||
1250                                     ((tex->type == TEX_ENVMAP) && (mtex->texco == TEXCO_REFL)))
1251                                 {
1252                                         if (GPU_material_do_color_management(mat)) {
1253                                                 GPU_link(mat, "srgb_to_linearrgb", tcol, &tcol);
1254                                         }
1255                                 }
1256                                 
1257                                 if (mtex->mapto & MAP_COL) {
1258                                         GPUNodeLink *colfac;
1259
1260                                         if (mtex->colfac == 1.0f) colfac = stencil;
1261                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->colfac), stencil, &colfac);
1262
1263                                         texture_rgb_blend(mat, tcol, shi->rgb, tin, colfac, mtex->blendtype, &shi->rgb);
1264                                 }
1265                                 
1266                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && (mtex->mapto & MAP_COLSPEC)) {
1267                                         GPUNodeLink *colspecfac;
1268
1269                                         if (mtex->colspecfac == 1.0f) colspecfac = stencil;
1270                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->colspecfac), stencil, &colspecfac);
1271
1272                                         texture_rgb_blend(mat, tcol, shi->specrgb, tin, colspecfac, mtex->blendtype, &shi->specrgb);
1273                                 }
1274
1275                                 if (mtex->mapto & MAP_COLMIR) {
1276                                         GPUNodeLink *colmirfac;
1277
1278                                         if (mtex->mirrfac == 1.0f) colmirfac = stencil;
1279                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->mirrfac), stencil, &colmirfac);
1280
1281                                         /* exception for envmap only */
1282                                         if (tex->type == TEX_ENVMAP && mtex->blendtype == MTEX_BLEND) {
1283                                                 GPU_link(mat, "mtex_mirror", tcol, shi->refcol, tin, colmirfac, &shi->refcol);
1284                                         }
1285                                         else
1286                                                 texture_rgb_blend(mat, tcol, shi->mir, tin, colmirfac, mtex->blendtype, &shi->mir);
1287                                 }
1288                         }
1289
1290                         if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && (mtex->mapto & MAP_NORM)) {
1291                                 if (tex->type == TEX_IMAGE) {
1292                                         found_deriv_map = tex->imaflag & TEX_DERIVATIVEMAP;
1293
1294                                         if (tex->imaflag & TEX_NORMALMAP) {
1295                                                 /* normalmap image */
1296                                                 GPU_link(mat, "mtex_normal", texco, GPU_image(tex->ima, &tex->iuser, true), &tnor);
1297                                                 
1298                                                 if (mtex->norfac < 0.0f)
1299                                                         GPU_link(mat, "mtex_negate_texnormal", tnor, &tnor);
1300
1301                                                 if (mtex->normapspace == MTEX_NSPACE_TANGENT) {
1302                                                         if (iFirstTimeNMap != 0) {
1303                                                                 // use unnormalized normal (this is how we bake it - closer to gamedev)
1304                                                                 GPUNodeLink *vNegNorm;
1305                                                                 GPU_link(mat, "vec_math_negate",
1306                                                                          GPU_builtin(GPU_VIEW_NORMAL), &vNegNorm);
1307                                                                 GPU_link(mat, "mtex_nspace_tangent",
1308                                                                          GPU_attribute(CD_TANGENT, ""), vNegNorm, tnor, &newnor);
1309                                                                 iFirstTimeNMap = 0;
1310                                                         }
1311                                                         else { /* otherwise use accumulated perturbations */
1312                                                                 GPU_link(mat, "mtex_nspace_tangent",
1313                                                                          GPU_attribute(CD_TANGENT, ""), shi->vn, tnor, &newnor);
1314                                                         }
1315                                                 }
1316                                                 else if (mtex->normapspace == MTEX_NSPACE_OBJECT) {
1317                                                         /* transform normal by object then view matrix */
1318                                                         GPU_link(mat, "mtex_nspace_object", tnor, &newnor);
1319                                                 }
1320                                                 else if (mtex->normapspace == MTEX_NSPACE_WORLD) {
1321                                                         /* transform normal by view matrix */
1322                                                         GPU_link(mat, "mtex_nspace_world", GPU_builtin(GPU_VIEW_MATRIX), tnor, &newnor);
1323                                                 }
1324                                                 else {
1325                                                         /* no transform, normal in camera space */
1326                                                         newnor = tnor;
1327                                                 }
1328                                                 
1329                                                 float norfac = min_ff(fabsf(mtex->norfac), 1.0f);
1330                                                 
1331                                                 if (norfac == 1.0f && !GPU_link_changed(stencil)) {
1332                                                         shi->vn = newnor;
1333                                                 }
1334                                                 else {
1335                                                         tnorfac = GPU_uniform(&norfac);
1336         
1337                                                         if (GPU_link_changed(stencil))
1338                                                                 GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
1339         
1340                                                         GPU_link(mat, "mtex_blend_normal", tnorfac, shi->vn, newnor, &shi->vn);
1341                                                 }
1342                                                 
1343                                         }
1344                                         else if (found_deriv_map ||
1345                                                  (mtex->texflag & (MTEX_3TAP_BUMP | MTEX_5TAP_BUMP | MTEX_BICUBIC_BUMP)))
1346                                         {
1347                                                 /* ntap bumpmap image */
1348                                                 int iBumpSpace;
1349                                                 float ima_x, ima_y;
1350
1351                                                 float imag_tspace_dimension_x = 1024.0f; /* only used for texture space variant */
1352                                                 float aspect = 1.0f;
1353                                                 
1354                                                 GPUNodeLink *vR1, *vR2;
1355                                                 GPUNodeLink *dBs, *dBt, *fDet;
1356
1357                                                 float hScale = 0.1f; /* compatibility adjustment factor for all bumpspace types */
1358                                                 if (mtex->texflag & MTEX_BUMP_TEXTURESPACE)
1359                                                         hScale = 13.0f; /* factor for scaling texspace bumps */
1360                                                 else if (found_deriv_map)
1361                                                         hScale = 1.0f;
1362
1363                                                 /* resolve texture resolution */
1364                                                 if ((mtex->texflag & MTEX_BUMP_TEXTURESPACE) || found_deriv_map) {
1365                                                         ImBuf *ibuf = BKE_image_acquire_ibuf(tex->ima, &tex->iuser, NULL);
1366                                                         ima_x = 512.0f; ima_y = 512.0f; /* prevent calling textureSize, glsl 1.3 only */
1367                                                         if (ibuf) {
1368                                                                 ima_x = ibuf->x;
1369                                                                 ima_y = ibuf->y;
1370                                                                 aspect = (float)ima_y / ima_x;
1371                                                         }
1372                                                         BKE_image_release_ibuf(tex->ima, ibuf, NULL);
1373                                                 }
1374
1375                                                 /* The negate on norfac is done because the
1376                                                  * normal in the renderer points inward which corresponds
1377                                                  * to inverting the bump map. Should this ever change
1378                                                  * this negate must be removed. */
1379                                                 float norfac = -hScale * mtex->norfac;
1380                                                 if (found_deriv_map) {
1381                                                         float fVirtDim = sqrtf(fabsf(ima_x * mtex->size[0] * ima_y * mtex->size[1]));
1382                                                         norfac /= MAX2(fVirtDim, FLT_EPSILON);
1383                                                 }
1384
1385                                                 tnorfac = GPU_uniform(&norfac);
1386
1387                                                 if (found_deriv_map)
1388                                                         GPU_link(mat, "math_multiply", tnorfac, GPU_builtin(GPU_AUTO_BUMPSCALE), &tnorfac);
1389                                                 
1390                                                 if (GPU_link_changed(stencil))
1391                                                         GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
1392                                                 
1393                                                 if (!init_done) {
1394                                                         /* copy shi->vn to vNorg and vNacc, set magnitude to 1 */
1395                                                         GPU_link(mat, "mtex_bump_normals_init", shi->vn, &vNorg, &vNacc, &fPrevMagnitude);
1396                                                         iBumpSpacePrev = 0;
1397                                                         init_done = true;
1398                                                 }
1399                                                 
1400                                                 // find current bump space
1401                                                 if (mtex->texflag & MTEX_BUMP_OBJECTSPACE)
1402                                                         iBumpSpace = 1;
1403                                                 else if (mtex->texflag & MTEX_BUMP_TEXTURESPACE)
1404                                                         iBumpSpace = 2;
1405                                                 else
1406                                                         iBumpSpace = 4; /* ViewSpace */
1407                                                 
1408                                                 /* re-initialize if bump space changed */
1409                                                 if (iBumpSpacePrev != iBumpSpace) {
1410                                                         GPUNodeLink *surf_pos = GPU_builtin(GPU_VIEW_POSITION);
1411
1412                                                         if (mtex->texflag & MTEX_BUMP_OBJECTSPACE)
1413                                                                 GPU_link(mat, "mtex_bump_init_objspace",
1414                                                                          surf_pos, vNorg,
1415                                                                          GPU_builtin(GPU_VIEW_MATRIX),
1416                                                                          GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
1417                                                                          GPU_builtin(GPU_OBJECT_MATRIX),
1418                                                                          GPU_builtin(GPU_INVERSE_OBJECT_MATRIX),
1419                                                                          fPrevMagnitude, vNacc,
1420                                                                          &fPrevMagnitude, &vNacc,
1421                                                                          &vR1, &vR2, &fDet);
1422                                                                 
1423                                                         else if (mtex->texflag & MTEX_BUMP_TEXTURESPACE)
1424                                                                 GPU_link(mat, "mtex_bump_init_texturespace",
1425                                                                          surf_pos, vNorg,
1426                                                                          fPrevMagnitude, vNacc,
1427                                                                          &fPrevMagnitude, &vNacc,
1428                                                                          &vR1, &vR2, &fDet);
1429                                                                 
1430                                                         else
1431                                                                 GPU_link(mat, "mtex_bump_init_viewspace",
1432                                                                          surf_pos, vNorg,
1433                                                                          fPrevMagnitude, vNacc,
1434                                                                          &fPrevMagnitude, &vNacc,
1435                                                                          &vR1, &vR2, &fDet);
1436                                                         
1437                                                         iBumpSpacePrev = iBumpSpace;
1438                                                 }
1439                                                 
1440                                                 
1441                                                 if (found_deriv_map) {
1442                                                         GPU_link(mat, "mtex_bump_deriv",
1443                                                                  texco, GPU_image(tex->ima, &tex->iuser, true),
1444                                                                  GPU_uniform(&ima_x), GPU_uniform(&ima_y), tnorfac,
1445                                                                  &dBs, &dBt);
1446                                                 }
1447                                                 else if (mtex->texflag & MTEX_3TAP_BUMP)
1448                                                         GPU_link(mat, "mtex_bump_tap3",
1449                                                                  texco, GPU_image(tex->ima, &tex->iuser, true), tnorfac,
1450                                                                  &dBs, &dBt);
1451                                                 else if (mtex->texflag & MTEX_5TAP_BUMP)
1452                                                         GPU_link(mat, "mtex_bump_tap5",
1453                                                                  texco, GPU_image(tex->ima, &tex->iuser, true), tnorfac,
1454                                                                  &dBs, &dBt);
1455                                                 else if (mtex->texflag & MTEX_BICUBIC_BUMP) {
1456                                                         if (GPU_bicubic_bump_support()) {
1457                                                                 GPU_link(mat, "mtex_bump_bicubic",
1458                                                                          texco, GPU_image(tex->ima, &tex->iuser, true), tnorfac,
1459                                                                          &dBs, &dBt);
1460                                                         }
1461                                                         else {
1462                                                                 GPU_link(mat, "mtex_bump_tap5",
1463                                                                          texco, GPU_image(tex->ima, &tex->iuser, true), tnorfac,
1464                                                                          &dBs, &dBt);
1465                                                         }
1466                                                 }
1467                                                 
1468                                                 
1469                                                 if (mtex->texflag & MTEX_BUMP_TEXTURESPACE) {
1470                                                         float imag_tspace_dimension_y = aspect * imag_tspace_dimension_x;
1471                                                         GPU_link(mat, "mtex_bump_apply_texspace",
1472                                                                  fDet, dBs, dBt, vR1, vR2,
1473                                                                  GPU_image(tex->ima, &tex->iuser, true), texco,
1474                                                                  GPU_uniform(&imag_tspace_dimension_x),
1475                                                                  GPU_uniform(&imag_tspace_dimension_y), vNacc,
1476                                                                  &vNacc, &shi->vn);
1477                                                 }
1478                                                 else
1479                                                         GPU_link(mat, "mtex_bump_apply",
1480                                                                  fDet, dBs, dBt, vR1, vR2, vNacc,
1481                                                                  &vNacc, &shi->vn);
1482                                                 
1483                                         }
1484                                 }
1485                                 
1486                                 GPU_link(mat, "vec_math_negate", shi->vn, &orn);
1487                         }
1488
1489                         if ((mtex->mapto & MAP_VARS)) {
1490                                 if (rgbnor & TEX_RGB) {
1491                                         if (talpha)
1492                                                 GPU_link(mat, "mtex_alpha_from_col", trgb, &tin);
1493                                         else
1494                                                 GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
1495                                 }
1496
1497                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_REF) {
1498                                         GPUNodeLink *difffac;
1499
1500                                         if (mtex->difffac == 1.0f) difffac = stencil;
1501                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->difffac), stencil, &difffac);
1502
1503                                         texture_value_blend(
1504                                                 mat, GPU_uniform(&mtex->def_var), shi->refl, tin, difffac,
1505                                                 mtex->blendtype, &shi->refl);
1506                                         GPU_link(mat, "mtex_value_clamp_positive", shi->refl, &shi->refl);
1507                                 }
1508                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_SPEC) {
1509                                         GPUNodeLink *specfac;
1510
1511                                         if (mtex->specfac == 1.0f) specfac = stencil;
1512                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->specfac), stencil, &specfac);
1513
1514                                         texture_value_blend(
1515                                                 mat, GPU_uniform(&mtex->def_var), shi->spec, tin, specfac,
1516                                                 mtex->blendtype, &shi->spec);
1517                                         GPU_link(mat, "mtex_value_clamp_positive", shi->spec, &shi->spec);
1518                                 }
1519                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_EMIT) {
1520                                         GPUNodeLink *emitfac;
1521
1522                                         if (mtex->emitfac == 1.0f) emitfac = stencil;
1523                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->emitfac), stencil, &emitfac);
1524
1525                                         texture_value_blend(
1526                                                 mat, GPU_uniform(&mtex->def_var), shi->emit, tin, emitfac,
1527                                                 mtex->blendtype, &shi->emit);
1528                                         GPU_link(mat, "mtex_value_clamp_positive", shi->emit, &shi->emit);
1529                                 }
1530                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_HAR) {
1531                                         GPUNodeLink *hardfac;
1532
1533                                         if (mtex->hardfac == 1.0f) hardfac = stencil;
1534                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->hardfac), stencil, &hardfac);
1535
1536                                         GPU_link(mat, "mtex_har_divide", shi->har, &shi->har);
1537                                         texture_value_blend(
1538                                                 mat, GPU_uniform(&mtex->def_var), shi->har, tin, hardfac,
1539                                                 mtex->blendtype, &shi->har);
1540                                         GPU_link(mat, "mtex_har_multiply_clamp", shi->har, &shi->har);
1541                                 }
1542                                 if (mtex->mapto & MAP_ALPHA) {
1543                                         GPUNodeLink *alphafac;
1544
1545                                         if (mtex->alphafac == 1.0f) alphafac = stencil;
1546                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->alphafac), stencil, &alphafac);
1547
1548                                         texture_value_blend(
1549                                                 mat, GPU_uniform(&mtex->def_var), shi->alpha, tin, alphafac,
1550                                                 mtex->blendtype, &shi->alpha);
1551                                         GPU_link(mat, "mtex_value_clamp", shi->alpha, &shi->alpha);
1552                                 }
1553                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_AMB) {
1554                                         GPUNodeLink *ambfac;
1555
1556                                         if (mtex->ambfac == 1.0f) ambfac = stencil;
1557                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->ambfac), stencil, &ambfac);
1558
1559                                         texture_value_blend(
1560                                                 mat, GPU_uniform(&mtex->def_var), shi->amb, tin, ambfac,
1561                                                 mtex->blendtype, &shi->amb);
1562                                         GPU_link(mat, "mtex_value_clamp", shi->amb, &shi->amb);
1563                                 }
1564                         }
1565                 }
1566         }
1567 }
1568
1569 void GPU_shadeinput_set(GPUMaterial *mat, Material *ma, GPUShadeInput *shi)
1570 {
1571         float one = 1.0f;
1572
1573         memset(shi, 0, sizeof(*shi));
1574
1575         shi->gpumat = mat;
1576         shi->mat = ma;
1577
1578         GPU_link(mat, "set_rgb", GPU_dynamic_uniform(&ma->r, GPU_DYNAMIC_MAT_DIFFRGB, ma), &shi->rgb);
1579         GPU_link(mat, "set_rgb", GPU_dynamic_uniform(&ma->specr, GPU_DYNAMIC_MAT_SPECRGB, ma), &shi->specrgb);
1580         GPU_link(mat, "set_rgb", GPU_dynamic_uniform(&ma->mirr, GPU_DYNAMIC_MAT_MIR, ma), &shi->mir);
1581         GPU_link(mat, "set_rgba_zero", &shi->refcol);
1582         GPU_link(mat, "shade_norm", GPU_builtin(GPU_VIEW_NORMAL), &shi->vn);
1583
1584         if (mat->alpha)
1585                 GPU_link(mat, "set_value", GPU_dynamic_uniform(&ma->alpha, GPU_DYNAMIC_MAT_ALPHA, ma), &shi->alpha);
1586         else
1587                 GPU_link(mat, "set_value", GPU_uniform(&one), &shi->alpha);
1588
1589         GPU_link(mat, "set_value", GPU_dynamic_uniform(&ma->ref, GPU_DYNAMIC_MAT_REF, ma), &shi->refl);
1590         GPU_link(mat, "set_value", GPU_dynamic_uniform(&ma->spec, GPU_DYNAMIC_MAT_SPEC, ma), &shi->spec);
1591         GPU_link(mat, "set_value", GPU_dynamic_uniform(&ma->emit, GPU_DYNAMIC_MAT_EMIT, ma), &shi->emit);
1592         GPU_link(mat, "set_value", GPU_dynamic_uniform((float *)&ma->har, GPU_DYNAMIC_MAT_HARD, ma), &shi->har);
1593         GPU_link(mat, "set_value", GPU_dynamic_uniform(&ma->amb, GPU_DYNAMIC_MAT_AMB, ma), &shi->amb);
1594         GPU_link(mat, "set_value", GPU_uniform(&ma->spectra), &shi->spectra);
1595         GPU_link(mat, "shade_view", GPU_builtin(GPU_VIEW_POSITION), &shi->view);
1596         GPU_link(mat, "vcol_attribute", GPU_attribute(CD_MCOL, ""), &shi->vcol);
1597         if (GPU_material_do_color_management(mat))
1598                 GPU_link(mat, "srgb_to_linearrgb", shi->vcol, &shi->vcol);
1599         GPU_link(mat, "texco_refl", shi->vn, shi->view, &shi->ref);
1600 }
1601
1602 void GPU_mist_update_enable(short enable)
1603 {
1604         GPUWorld.mistenabled = (float)enable;
1605 }
1606
1607 void GPU_mist_update_values(int type, float start, float dist, float inten, float color[3])
1608 {
1609         GPUWorld.mistype = (float)type;
1610         GPUWorld.miststart = start;
1611         GPUWorld.mistdistance = dist;
1612         GPUWorld.mistintensity = inten;
1613         copy_v3_v3(GPUWorld.mistcol, color);
1614         GPUWorld.mistcol[3] = 1.0f;
1615 }
1616
1617 void GPU_horizon_update_color(float color[3])
1618 {
1619         copy_v3_v3(GPUWorld.horicol, color);
1620 }
1621
1622 void GPU_ambient_update_color(float color[3])
1623 {
1624         copy_v3_v3(GPUWorld.ambcol, color);
1625         GPUWorld.ambcol[3] = 1.0f;
1626 }
1627
1628 void GPU_zenith_update_color(float color[3])
1629 {
1630         copy_v3_v3(GPUWorld.zencol, color);
1631 }
1632
1633 void GPU_shaderesult_set(GPUShadeInput *shi, GPUShadeResult *shr)
1634 {
1635         GPUMaterial *mat = shi->gpumat;
1636         GPUNodeLink *emit, *ulinfac, *ulogfac, *mistfac;
1637         Material *ma = shi->mat;
1638         World *world = mat->scene->world;
1639         float linfac, logfac;
1640
1641         memset(shr, 0, sizeof(*shr));
1642
1643         if (ma->mode & MA_VERTEXCOLP)
1644                 shi->rgb = shi->vcol;
1645
1646         do_material_tex(shi);
1647
1648         if ((mat->scene->gm.flag & GAME_GLSL_NO_LIGHTS) || (ma->mode & MA_SHLESS)) {
1649                 GPU_link(mat, "set_rgb", shi->rgb, &shr->diff);
1650                 GPU_link(mat, "set_rgb_zero", &shr->spec);
1651                 GPU_link(mat, "set_value", shi->alpha, &shr->alpha);
1652                 shr->combined = shr->diff;
1653         }
1654         else {
1655                 if (GPU_link_changed(shi->emit) || ma->emit != 0.0f) {
1656                         if ((ma->mode & (MA_VERTEXCOL | MA_VERTEXCOLP)) == MA_VERTEXCOL) {
1657                                 GPU_link(mat, "shade_add", shi->emit, shi->vcol, &emit);
1658                                 GPU_link(mat, "shade_mul", emit, shi->rgb, &shr->diff);
1659                         }
1660                         else
1661                                 GPU_link(mat, "shade_mul_value", shi->emit, shi->rgb, &shr->diff);
1662                 }
1663                 else
1664                         GPU_link(mat, "set_rgb_zero", &shr->diff);
1665
1666                 GPU_link(mat, "set_rgb_zero", &shr->spec);
1667
1668                 material_lights(shi, shr);
1669
1670                 shr->combined = shr->diff;
1671
1672                 GPU_link(mat, "set_value", shi->alpha, &shr->alpha);
1673
1674                 if (world) {
1675                         /* exposure correction */
1676                         if (world->exp != 0.0f || world->range != 1.0f) {
1677                                 linfac = 1.0f + powf((2.0f * world->exp + 0.5f), -10);
1678                                 logfac = logf((linfac - 1.0f) / linfac) / world->range;
1679
1680                                 GPU_link(mat, "set_value", GPU_uniform(&linfac), &ulinfac);
1681                                 GPU_link(mat, "set_value", GPU_uniform(&logfac), &ulogfac);
1682
1683                                 GPU_link(mat, "shade_exposure_correct", shr->combined,
1684                                         ulinfac, ulogfac, &shr->combined);
1685                                 GPU_link(mat, "shade_exposure_correct", shr->spec,
1686                                         ulinfac, ulogfac, &shr->spec);
1687                         }
1688
1689                         /* environment lighting */
1690                         if (!(mat->scene->gm.flag & GAME_GLSL_NO_ENV_LIGHTING) &&
1691                             (world->mode & WO_ENV_LIGHT) &&
1692                             (mat->scene->r.mode & R_SHADOW) &&
1693                             !BKE_scene_use_new_shading_nodes(mat->scene))
1694                         {
1695                                 if ((world->ao_env_energy != 0.0f) && (GPU_link_changed(shi->amb) || ma->amb != 0.0f) &&
1696                                     (GPU_link_changed(shi->refl) || ma->ref != 0.0f))
1697                                 {
1698                                         if (world->aocolor != WO_AOPLAIN) {
1699                                                 if (!(is_zero_v3(&world->horr) & is_zero_v3(&world->zenr))) {
1700                                                         GPUNodeLink *fcol, *f;
1701                                                         GPU_link(mat, "math_multiply", shi->amb, shi->refl, &f);
1702                                                         GPU_link(mat, "math_multiply", f, GPU_uniform(&world->ao_env_energy), &f);
1703                                                         GPU_link(mat, "shade_mul_value", f, shi->rgb, &fcol);
1704                                                         GPU_link(mat, "env_apply", shr->combined,
1705                                                                  GPU_dynamic_uniform(GPUWorld.horicol, GPU_DYNAMIC_HORIZON_COLOR, NULL),
1706                                                                  GPU_dynamic_uniform(GPUWorld.zencol, GPU_DYNAMIC_ZENITH_COLOR, NULL), fcol,
1707                                                                  GPU_builtin(GPU_VIEW_MATRIX), shi->vn, &shr->combined);
1708                                                 }
1709                                         }
1710                                         else {
1711                                                 GPUNodeLink *f;
1712                                                 GPU_link(mat, "math_multiply", shi->amb, shi->refl, &f);
1713                                                 GPU_link(mat, "math_multiply", f, GPU_uniform(&world->ao_env_energy), &f);
1714                                                 GPU_link(mat, "shade_maddf", shr->combined, f, shi->rgb, &shr->combined);
1715                                         }
1716                                 }
1717                         }
1718
1719                         /* ambient color */
1720                         if (GPU_link_changed(shi->amb) || ma->amb != 0.0f) {
1721                                 GPU_link(mat, "shade_maddf", shr->combined, GPU_uniform(&ma->amb),
1722                                          GPU_dynamic_uniform(GPUWorld.ambcol, GPU_DYNAMIC_AMBIENT_COLOR, NULL),
1723                                          &shr->combined);
1724                         }
1725                 }
1726
1727                 if (ma->mode & MA_TRANSP && (ma->mode & (MA_ZTRANSP | MA_RAYTRANSP))) {
1728                         if (GPU_link_changed(shi->spectra) || ma->spectra != 0.0f) {
1729                                 GPU_link(mat, "alpha_spec_correction", shr->spec, shi->spectra,
1730                                          shi->alpha, &shr->alpha);
1731                         }
1732                 }
1733
1734                 if (ma->mode & MA_RAMP_COL) ramp_diffuse_result(shi, &shr->combined);
1735                 if (ma->mode & MA_RAMP_SPEC) ramp_spec_result(shi, &shr->spec);
1736
1737                 if (GPU_link_changed(shi->refcol))
1738                         GPU_link(mat, "shade_add_mirror", shi->mir, shi->refcol, shr->combined, &shr->combined);
1739
1740                 if (GPU_link_changed(shi->spec) || ma->spec != 0.0f)
1741                         GPU_link(mat, "shade_add", shr->combined, shr->spec, &shr->combined);
1742         }
1743
1744         GPU_link(mat, "mtex_alpha_to_col", shr->combined, shr->alpha, &shr->combined);
1745
1746         if (ma->shade_flag & MA_OBCOLOR)
1747                 GPU_link(mat, "shade_obcolor", shr->combined, GPU_builtin(GPU_OBCOLOR), &shr->combined);
1748
1749         if (!(ma->mode & MA_NOMIST)) {
1750                 GPU_link(mat, "shade_mist_factor", GPU_builtin(GPU_VIEW_POSITION),
1751                          GPU_dynamic_uniform(&GPUWorld.mistenabled, GPU_DYNAMIC_MIST_ENABLE, NULL),
1752                          GPU_dynamic_uniform(&GPUWorld.miststart, GPU_DYNAMIC_MIST_START, NULL),
1753                          GPU_dynamic_uniform(&GPUWorld.mistdistance, GPU_DYNAMIC_MIST_DISTANCE, NULL),
1754                          GPU_dynamic_uniform(&GPUWorld.mistype, GPU_DYNAMIC_MIST_TYPE, NULL),
1755                          GPU_dynamic_uniform(&GPUWorld.mistintensity, GPU_DYNAMIC_MIST_INTENSITY, NULL), &mistfac);
1756
1757                 GPU_link(mat, "mix_blend", mistfac, shr->combined,
1758                          GPU_dynamic_uniform(GPUWorld.mistcol, GPU_DYNAMIC_MIST_COLOR, NULL), &shr->combined);
1759         }
1760
1761         if (!mat->alpha) {
1762                 if (world && (GPU_link_changed(shr->alpha) || ma->alpha != 1.0f))
1763                         GPU_link(mat, "shade_world_mix", GPU_dynamic_uniform(GPUWorld.horicol, GPU_DYNAMIC_HORIZON_COLOR, NULL),
1764                                  shr->combined, &shr->combined);
1765
1766                 GPU_link(mat, "shade_alpha_opaque", shr->combined, &shr->combined);
1767         }
1768
1769         if (ma->shade_flag & MA_OBCOLOR) {
1770                 mat->obcolalpha = 1;
1771                 GPU_link(mat, "shade_alpha_obcolor", shr->combined, GPU_builtin(GPU_OBCOLOR), &shr->combined);
1772         }
1773 }
1774
1775 static GPUNodeLink *GPU_blender_material(GPUMaterial *mat, Material *ma)
1776 {
1777         GPUShadeInput shi;
1778         GPUShadeResult shr;
1779
1780         GPU_shadeinput_set(mat, ma, &shi);
1781         GPU_shaderesult_set(&shi, &shr);
1782
1783         return shr.combined;
1784 }
1785
1786 static GPUNodeLink *gpu_material_diffuse_bsdf(GPUMaterial *mat, Material *ma)
1787 {
1788         static float roughness = 0.0f;
1789         GPUNodeLink *outlink;
1790
1791         GPU_link(mat, "node_bsdf_diffuse",
1792                  GPU_uniform(&ma->r), GPU_uniform(&roughness), GPU_builtin(GPU_VIEW_NORMAL), &outlink);
1793
1794         return outlink;
1795 }
1796
1797 static GPUNodeLink *gpu_material_preview_matcap(GPUMaterial *mat, Material *ma)
1798 {
1799         GPUNodeLink *outlink;
1800         
1801         /* some explanations here:
1802          * matcap normal holds the normal remapped to the 0.0 - 1.0 range. To take advantage of flat shading, we abuse
1803          * the built in secondary color of opengl. Color is just the regular color, which should include mask value too.
1804          * This also needs flat shading so we use the primary opengl color built-in */
1805         GPU_link(mat, "material_preview_matcap", GPU_uniform(&ma->r), GPU_image_preview(ma->preview),
1806                  GPU_opengl_builtin(GPU_MATCAP_NORMAL), GPU_opengl_builtin(GPU_COLOR), &outlink);
1807         
1808         return outlink;
1809 }
1810
1811 /* new solid draw mode with glsl matcaps */
1812 GPUMaterial *GPU_material_matcap(Scene *scene, Material *ma, bool use_opensubdiv)
1813 {
1814         GPUMaterial *mat;
1815         GPUNodeLink *outlink;
1816         LinkData *link;
1817         
1818         for (link = ma->gpumaterial.first; link; link = link->next) {
1819                 GPUMaterial *current_material = (GPUMaterial *)link->data;
1820                 if (current_material->scene == scene &&
1821                     current_material->is_opensubdiv == use_opensubdiv)
1822                 {
1823                         return current_material;
1824                 }
1825         }
1826         
1827         /* allocate material */
1828         mat = GPU_material_construct_begin(ma);
1829         mat->scene = scene;
1830         mat->type = GPU_MATERIAL_TYPE_MESH;
1831         mat->is_opensubdiv = use_opensubdiv;
1832
1833         if (ma->preview && ma->preview->rect[0]) {
1834                 outlink = gpu_material_preview_matcap(mat, ma);
1835         }
1836         else {
1837                 outlink = gpu_material_diffuse_bsdf(mat, ma);
1838         }
1839                 
1840         GPU_material_output_link(mat, outlink);
1841
1842         gpu_material_construct_end(mat, "matcap_pass");
1843         
1844         /* note that even if building the shader fails in some way, we still keep
1845          * it to avoid trying to compile again and again, and simple do not use
1846          * the actual shader on drawing */
1847         
1848         link = MEM_callocN(sizeof(LinkData), "GPUMaterialLink");
1849         link->data = mat;
1850         BLI_addtail(&ma->gpumaterial, link);
1851         
1852         return mat;
1853 }
1854
1855 static void do_world_tex(GPUShadeInput *shi, struct World *wo, GPUNodeLink **hor, GPUNodeLink **zen, GPUNodeLink **blend)
1856 {
1857         GPUMaterial *mat = shi->gpumat;
1858         GPUNodeLink *texco, *tin, *trgb, *stencil, *tcol, *zenfac;
1859         MTex *mtex;
1860         Tex *tex;
1861         float ofs[3], zero = 0.0f;
1862         int tex_nr, rgbnor;
1863
1864         GPU_link(mat, "set_value_one", &stencil);
1865         /* go over texture slots */
1866         for (tex_nr = 0; tex_nr < MAX_MTEX; tex_nr++) {
1867                 if (wo->mtex[tex_nr]) {
1868                         mtex = wo->mtex[tex_nr];
1869                         tex = mtex->tex;
1870                         if (tex == NULL || !tex->ima || (tex->type != TEX_IMAGE && tex->type != TEX_ENVMAP))
1871                                 continue;
1872                         /* which coords */
1873                         if (mtex->texco == TEXCO_VIEW || mtex->texco == TEXCO_GLOB) {
1874                                 if (tex->type == TEX_IMAGE)
1875                                         texco = GPU_builtin(GPU_VIEW_POSITION);
1876                                 else if (tex->type == TEX_ENVMAP)
1877                                         GPU_link(mat, "background_transform_to_world", GPU_builtin(GPU_VIEW_POSITION), &texco);
1878                         }
1879                         else if (mtex->texco == TEXCO_EQUIRECTMAP || mtex->texco == TEXCO_ANGMAP) {
1880                                 if ((tex->type == TEX_IMAGE && wo->skytype & WO_SKYREAL) || tex->type == TEX_ENVMAP)
1881                                         GPU_link(mat, "background_transform_to_world", GPU_builtin(GPU_VIEW_POSITION), &texco);
1882                                 else
1883                                         texco = GPU_builtin(GPU_VIEW_POSITION);
1884                         }
1885                         else
1886                                 continue;
1887                         GPU_link(mat, "texco_norm", texco, &texco);
1888                         if (tex->type == TEX_IMAGE && !(wo->skytype & WO_SKYREAL)) {
1889                                 GPU_link(mat, "mtex_2d_mapping", texco, &texco);
1890                         }
1891                         if (mtex->size[0] != 1.0f || mtex->size[1] != 1.0f || mtex->size[2] != 1.0f) {
1892                                 float size[3] = { mtex->size[0], mtex->size[1], mtex->size[2] };
1893                                 if (tex->type == TEX_ENVMAP) {
1894                                         size[1] = mtex->size[2];
1895                                         size[2] = mtex->size[1];
1896                                 }
1897                                 GPU_link(mat, "mtex_mapping_size", texco, GPU_uniform(size), &texco);
1898                         }
1899                         ofs[0] = mtex->ofs[0] + 0.5f - 0.5f * mtex->size[0];
1900                         if (tex->type == TEX_ENVMAP) {
1901                                 ofs[1] = -mtex->ofs[2] + 0.5f - 0.5f * mtex->size[2];
1902                                 ofs[2] = mtex->ofs[1] + 0.5f - 0.5f * mtex->size[1];
1903                         }
1904                         else {
1905                                 ofs[1] = mtex->ofs[1] + 0.5f - 0.5f * mtex->size[1];
1906                                 ofs[2] = 0.0;
1907                         }
1908                         if (ofs[0] != 0.0f || ofs[1] != 0.0f || ofs[2] != 0.0f)
1909                                 GPU_link(mat, "mtex_mapping_ofs", texco, GPU_uniform(ofs), &texco);
1910                         if (mtex->texco == TEXCO_EQUIRECTMAP) {
1911                                 GPU_link(mat, "node_tex_environment_equirectangular", texco, GPU_image(tex->ima, &tex->iuser, false), &trgb);
1912                         }
1913                         else if (mtex->texco == TEXCO_ANGMAP) {
1914                                 GPU_link(mat, "node_tex_environment_mirror_ball", texco, GPU_image(tex->ima, &tex->iuser, false), &trgb);
1915                         }
1916                         else {
1917                                 if (tex->type == TEX_ENVMAP)
1918                                         GPU_link(mat, "mtex_cube_map", texco, GPU_cube_map(tex->ima, &tex->iuser, false), &tin, &trgb);
1919                                 else if (tex->type == TEX_IMAGE)
1920                                         GPU_link(mat, "mtex_image", texco, GPU_image(tex->ima, &tex->iuser, false), &tin, &trgb);
1921                         }
1922                         rgbnor = TEX_RGB;
1923                         if (tex->type == TEX_IMAGE || tex->type == TEX_ENVMAP)
1924                                 if (GPU_material_do_color_management(mat))
1925                                         GPU_link(mat, "srgb_to_linearrgb", trgb, &trgb);
1926                         /* texture output */
1927                         if ((rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1928                                 GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
1929                                 rgbnor -= TEX_RGB;
1930                         }
1931                         if (mtex->texflag & MTEX_NEGATIVE) {
1932                                 if (rgbnor & TEX_RGB)
1933                                         GPU_link(mat, "mtex_rgb_invert", trgb, &trgb);
1934                                 else
1935                                         GPU_link(mat, "mtex_value_invert", tin, &tin);
1936                         }
1937                         if (mtex->texflag & MTEX_STENCIL) {
1938                                 if (rgbnor & TEX_RGB)
1939                                         GPU_link(mat, "mtex_rgb_stencil", stencil, trgb, &stencil, &trgb);
1940                                 else
1941                                         GPU_link(mat, "mtex_value_stencil", stencil, tin, &stencil, &tin);
1942                         }
1943                         else {
1944                                 if (rgbnor & TEX_RGB)
1945                                         GPU_link(mat, "mtex_alpha_multiply_value", trgb, stencil, &trgb);
1946                                 else
1947                                         GPU_link(mat, "math_multiply", stencil, tin, &tin);
1948                         }
1949                         /* color mapping */
1950                         if (mtex->mapto & (WOMAP_HORIZ + WOMAP_ZENUP + WOMAP_ZENDOWN)) {
1951                                 if ((rgbnor & TEX_RGB) == 0)
1952                                         GPU_link(mat, "set_rgb", GPU_uniform(&mtex->r), &trgb);
1953                                 else
1954                                         GPU_link(mat, "mtex_alpha_from_col", trgb, &tin);
1955                                 GPU_link(mat, "set_rgb", trgb, &tcol);
1956                                 if (mtex->mapto & WOMAP_HORIZ) {
1957                                         texture_rgb_blend(mat, tcol, *hor, tin, GPU_uniform(&mtex->colfac), mtex->blendtype, hor);
1958                                 }
1959                                 if (mtex->mapto & (WOMAP_ZENUP + WOMAP_ZENDOWN)) {
1960                                         GPU_link(mat, "set_value_zero", &zenfac);
1961                                         if (wo->skytype & WO_SKYREAL) {
1962                                                 if (mtex->mapto & WOMAP_ZENUP) {
1963                                                         if (mtex->mapto & WOMAP_ZENDOWN) {
1964                                                                 GPU_link(mat, "world_zen_mapping", shi->view, GPU_uniform(&mtex->zenupfac),
1965                                                                          GPU_uniform(&mtex->zendownfac), &zenfac);
1966                                                         }
1967                                                         else {
1968                                                                 GPU_link(mat, "world_zen_mapping", shi->view, GPU_uniform(&mtex->zenupfac),
1969                                                                          GPU_uniform(&zero), &zenfac);
1970                                                         }
1971                                                 }
1972                                                 else if (mtex->mapto & WOMAP_ZENDOWN) {
1973                                                         GPU_link(mat, "world_zen_mapping", shi->view, GPU_uniform(&zero),
1974                                                                  GPU_uniform(&mtex->zendownfac), &zenfac);
1975                                                 }
1976                                         }
1977                                         else {
1978                                                 if (mtex->mapto & WOMAP_ZENUP)
1979                                                         GPU_link(mat, "set_value", GPU_uniform(&mtex->zenupfac), &zenfac);
1980                                                 else if (mtex->mapto & WOMAP_ZENDOWN)
1981                                                         GPU_link(mat, "set_value", GPU_uniform(&mtex->zendownfac), &zenfac);
1982                                         }
1983                                         texture_rgb_blend(mat, tcol, *zen, tin, zenfac, mtex->blendtype, zen);
1984                                 }
1985                         }
1986                         if (mtex->mapto & WOMAP_BLEND && wo->skytype & WO_SKYBLEND) {
1987                                 if (rgbnor & TEX_RGB)
1988                                         GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
1989                                 texture_value_blend(mat, GPU_uniform(&mtex->def_var), *blend, tin, GPU_uniform(&mtex->blendfac), mtex->blendtype, blend);
1990                         }
1991                 }
1992         }
1993 }
1994
1995 static void gpu_material_old_world(struct GPUMaterial *mat, struct World *wo)
1996 {
1997         GPUShadeInput shi;
1998         GPUShadeResult shr;
1999         GPUNodeLink *hor, *zen, *ray, *blend;
2000
2001         shi.gpumat = mat;
2002
2003         for (int i = 0; i < MAX_MTEX; i++) {
2004                 if (wo->mtex[i] && wo->mtex[i]->tex) {
2005                         wo->skytype |= WO_SKYTEX;
2006                         break;
2007                 }
2008         }
2009         if ((wo->skytype & (WO_SKYBLEND + WO_SKYTEX)) == 0) {
2010                 GPU_link(mat, "set_rgb", GPU_dynamic_uniform(&wo->horr, GPU_DYNAMIC_HORIZON_COLOR, NULL), &shr.combined);
2011         }
2012         else {
2013                 GPU_link(mat, "set_rgb_zero", &shi.rgb);
2014                 GPU_link(mat, "background_transform_to_world", GPU_builtin(GPU_VIEW_POSITION), &ray);
2015                 if (wo->skytype & WO_SKYPAPER)
2016                         GPU_link(mat, "world_paper_view", GPU_builtin(GPU_VIEW_POSITION), &shi.view);
2017                 else
2018                         GPU_link(mat, "shade_view", ray, &shi.view);
2019                 if (wo->skytype & WO_SKYBLEND) {
2020                         if (wo->skytype & WO_SKYPAPER) {
2021                                 if (wo->skytype & WO_SKYREAL)
2022                                         GPU_link(mat, "world_blend_paper_real", GPU_builtin(GPU_VIEW_POSITION), &blend);
2023                                 else
2024                                         GPU_link(mat, "world_blend_paper", GPU_builtin(GPU_VIEW_POSITION), &blend);
2025                         }
2026                         else {
2027                                 if (wo->skytype & WO_SKYREAL)
2028                                         GPU_link(mat, "world_blend_real", ray, &blend);
2029                                 else
2030                                         GPU_link(mat, "world_blend", ray, &blend);
2031                         }
2032                 }
2033                 else {
2034                         GPU_link(mat, "set_value_zero", &blend);
2035                 }
2036                 GPU_link(mat, "set_rgb", GPU_dynamic_uniform(&wo->horr, GPU_DYNAMIC_HORIZON_COLOR, NULL), &hor);
2037                 GPU_link(mat, "set_rgb", GPU_dynamic_uniform(&wo->zenr, GPU_DYNAMIC_ZENITH_COLOR, NULL), &zen);
2038                 do_world_tex(&shi, wo, &hor, &zen, &blend);
2039                 if (wo->skytype & WO_SKYBLEND)
2040                         GPU_link(mat, "node_mix_shader", blend, hor, zen, &shi.rgb);
2041                 else
2042                         GPU_link(mat, "set_rgb", hor, &shi.rgb);
2043                 GPU_link(mat, "set_rgb", shi.rgb, &shr.combined);
2044         }
2045         GPU_material_output_link(mat, shr.combined);
2046 }
2047
2048 GPUMaterial *GPU_material_world(struct Scene *scene, struct World *wo)
2049 {
2050         LinkData *link;
2051         GPUMaterial *mat;
2052
2053         for (link = wo->gpumaterial.first; link; link = link->next)
2054                 if (((GPUMaterial *)link->data)->scene == scene)
2055                         return link->data;
2056
2057         /* allocate material */
2058         mat = GPU_material_construct_begin(NULL);
2059         mat->scene = scene;
2060         mat->type = GPU_MATERIAL_TYPE_WORLD;
2061         
2062         /* create nodes */
2063         if (BKE_scene_use_new_shading_nodes(scene) && wo->nodetree && wo->use_nodes) {
2064                 ntreeGPUMaterialNodes(wo->nodetree, mat, NODE_NEW_SHADING);
2065         }
2066         else {
2067                 gpu_material_old_world(mat, wo);
2068         }
2069
2070         if (GPU_material_do_color_management(mat))
2071                 if (mat->outlink)
2072                         GPU_link(mat, "linearrgb_to_srgb", mat->outlink, &mat->outlink);
2073
2074         gpu_material_construct_end(mat, wo->id.name);
2075         
2076         /* note that even if building the shader fails in some way, we still keep
2077          * it to avoid trying to compile again and again, and simple do not use
2078          * the actual shader on drawing */
2079
2080         link = MEM_callocN(sizeof(LinkData), "GPUMaterialLink");
2081         link->data = mat;
2082         BLI_addtail(&wo->gpumaterial, link);
2083
2084         return mat;
2085 }
2086
2087
2088 GPUMaterial *GPU_material_from_blender(Scene *scene, Material *ma, bool use_opensubdiv)
2089 {
2090         GPUMaterial *mat;
2091         GPUNodeLink *outlink;
2092         LinkData *link;
2093
2094         for (link = ma->gpumaterial.first; link; link = link->next) {
2095                 GPUMaterial *current_material = (GPUMaterial *)link->data;
2096                 if (current_material->scene == scene &&
2097                     current_material->is_opensubdiv == use_opensubdiv)
2098                 {
2099                         return current_material;
2100                 }
2101         }
2102
2103         /* allocate material */
2104         mat = GPU_material_construct_begin(ma);
2105         mat->scene = scene;
2106         mat->type = GPU_MATERIAL_TYPE_MESH;
2107         mat->is_opensubdiv = use_opensubdiv;
2108
2109         /* render pipeline option */
2110         bool new_shading_nodes = BKE_scene_use_new_shading_nodes(scene);
2111         if (!new_shading_nodes && (ma->mode & MA_TRANSP))
2112                 GPU_material_enable_alpha(mat);
2113         else if (new_shading_nodes && ma->alpha < 1.0f)
2114                 GPU_material_enable_alpha(mat);
2115
2116         if (!(scene->gm.flag & GAME_GLSL_NO_NODES) && ma->nodetree && ma->use_nodes) {
2117                 /* create nodes */
2118                 if (new_shading_nodes)
2119                         ntreeGPUMaterialNodes(ma->nodetree, mat, NODE_NEW_SHADING);
2120                 else
2121                         ntreeGPUMaterialNodes(ma->nodetree, mat, NODE_OLD_SHADING);
2122         }
2123         else {
2124                 if (new_shading_nodes) {
2125                         /* create simple diffuse material instead of nodes */
2126                         outlink = gpu_material_diffuse_bsdf(mat, ma);
2127                 }
2128                 else {
2129                         /* create blender material */
2130                         outlink = GPU_blender_material(mat, ma);
2131                 }
2132
2133                 GPU_material_output_link(mat, outlink);
2134         }
2135
2136         if (GPU_material_do_color_management(mat))
2137                 if (mat->outlink)
2138                         GPU_link(mat, "linearrgb_to_srgb", mat->outlink, &mat->outlink);
2139
2140         gpu_material_construct_end(mat, ma->id.name);
2141
2142         /* note that even if building the shader fails in some way, we still keep
2143          * it to avoid trying to compile again and again, and simple do not use
2144          * the actual shader on drawing */
2145
2146         link = MEM_callocN(sizeof(LinkData), "GPUMaterialLink");
2147         link->data = mat;
2148         BLI_addtail(&ma->gpumaterial, link);
2149
2150         return mat;
2151 }
2152
2153 void GPU_materials_free(void)
2154 {
2155         Object *ob;
2156         Material *ma;
2157         World *wo;
2158         extern Material defmaterial;
2159
2160         for (ma = G.main->mat.first; ma; ma = ma->id.next)
2161                 GPU_material_free(&ma->gpumaterial);
2162
2163         for (wo = G.main->world.first; wo; wo = wo->id.next)
2164                 GPU_material_free(&wo->gpumaterial);
2165         
2166         GPU_material_free(&defmaterial.gpumaterial);
2167
2168         for (ob = G.main->object.first; ob; ob = ob->id.next)
2169                 GPU_lamp_free(ob);
2170 }
2171
2172 /* Lamps and shadow buffers */
2173
2174 GPUNodeLink *GPU_lamp_get_data(
2175         GPUMaterial *mat, GPULamp *lamp,
2176         GPUNodeLink **r_col, GPUNodeLink **r_lv, GPUNodeLink **r_dist, GPUNodeLink **r_shadow, GPUNodeLink **r_energy)
2177 {
2178         GPUNodeLink *visifac;
2179
2180         *r_col = GPU_dynamic_uniform(lamp->dyncol, GPU_DYNAMIC_LAMP_DYNCOL, lamp->ob);
2181         *r_energy = GPU_dynamic_uniform(&lamp->dynenergy, GPU_DYNAMIC_LAMP_DYNENERGY, lamp->ob);
2182         visifac = lamp_get_visibility(mat, lamp, r_lv, r_dist);
2183
2184         shade_light_textures(mat, lamp, r_col);
2185
2186         if (GPU_lamp_has_shadow_buffer(lamp)) {
2187                 GPUNodeLink *vn, *inp;
2188
2189                 GPU_link(mat, "shade_norm", GPU_builtin(GPU_VIEW_NORMAL), &vn);
2190                 GPU_link(mat, "shade_inp", vn, *r_lv, &inp);
2191                 mat->dynproperty |= DYN_LAMP_PERSMAT;
2192
2193                 if (lamp->la->shadowmap_type == LA_SHADMAP_VARIANCE) {
2194                         GPU_link(mat, "shadows_only_vsm",
2195                                  GPU_builtin(GPU_VIEW_POSITION),
2196                                  GPU_dynamic_texture(lamp->tex, GPU_DYNAMIC_SAMPLER_2DSHADOW, lamp->ob),
2197                                  GPU_dynamic_uniform((float *)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
2198                                  GPU_uniform(&lamp->bias), GPU_uniform(&lamp->la->bleedbias),
2199                                  GPU_uniform(lamp->shadow_color), inp, r_shadow);
2200                 }
2201                 else {
2202                         GPU_link(mat, "shadows_only",
2203                                  GPU_builtin(GPU_VIEW_POSITION),
2204                                  GPU_dynamic_texture(lamp->tex, GPU_DYNAMIC_SAMPLER_2DSHADOW, lamp->ob),
2205                                  GPU_dynamic_uniform((float *)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
2206                                  GPU_uniform(&lamp->bias), GPU_uniform(lamp->shadow_color), inp, r_shadow);
2207                 }
2208         }
2209         else {
2210                 GPU_link(mat, "set_rgb_one", r_shadow);
2211         }
2212
2213         /* ensure shadow buffer and lamp textures will be updated */
2214         add_user_list(&mat->lamps, lamp);
2215         add_user_list(&lamp->materials, mat->ma);
2216
2217         return visifac;
2218 }
2219
2220 /* export the GLSL shader */
2221
2222 GPUShaderExport *GPU_shader_export(struct Scene *scene, struct Material *ma)
2223 {
2224         static struct {
2225                 GPUBuiltin gputype;
2226                 GPUDynamicType dynamictype;
2227                 GPUDataType datatype;
2228         } builtins[] = {
2229                 { GPU_VIEW_MATRIX, GPU_DYNAMIC_OBJECT_VIEWMAT, GPU_DATA_16F },
2230                 { GPU_INVERSE_VIEW_MATRIX, GPU_DYNAMIC_OBJECT_VIEWIMAT, GPU_DATA_16F },
2231                 { GPU_OBJECT_MATRIX, GPU_DYNAMIC_OBJECT_MAT, GPU_DATA_16F },
2232                 { GPU_INVERSE_OBJECT_MATRIX, GPU_DYNAMIC_OBJECT_IMAT, GPU_DATA_16F },
2233                 { GPU_LOC_TO_VIEW_MATRIX, GPU_DYNAMIC_OBJECT_LOCTOVIEWMAT, GPU_DATA_16F },
2234                 { GPU_INVERSE_LOC_TO_VIEW_MATRIX, GPU_DYNAMIC_OBJECT_LOCTOVIEWIMAT, GPU_DATA_16F },
2235                 { GPU_OBCOLOR, GPU_DYNAMIC_OBJECT_COLOR, GPU_DATA_4F },
2236                 { GPU_AUTO_BUMPSCALE, GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE, GPU_DATA_1F },
2237                 { 0 }
2238         };
2239
2240         GPUShaderExport *shader = NULL;
2241         GPUInput *input;
2242         int liblen, fraglen;
2243
2244         /* TODO(sergey): How to determine whether we need OSD or not here? */
2245         GPUMaterial *mat = GPU_material_from_blender(scene, ma, false);
2246         GPUPass *pass = (mat) ? mat->pass : NULL;
2247
2248         if (pass && pass->fragmentcode && pass->vertexcode) {
2249                 shader = MEM_callocN(sizeof(GPUShaderExport), "GPUShaderExport");
2250
2251                 for (input = pass->inputs.first; input; input = input->next) {
2252                         GPUInputUniform *uniform = MEM_callocN(sizeof(GPUInputUniform), "GPUInputUniform");
2253
2254                         if (input->ima) {
2255                                 /* image sampler uniform */
2256                                 uniform->type = GPU_DYNAMIC_SAMPLER_2DIMAGE;
2257                                 uniform->datatype = GPU_DATA_1I;
2258                                 uniform->image = input->ima;
2259                                 uniform->texnumber = input->texid;
2260                                 BLI_strncpy(uniform->varname, input->shadername, sizeof(uniform->varname));
2261                         }
2262                         else if (input->tex) {
2263                                 /* generated buffer */
2264                                 uniform->texnumber = input->texid;
2265                                 uniform->datatype = GPU_DATA_1I;
2266                                 BLI_strncpy(uniform->varname, input->shadername, sizeof(uniform->varname));
2267
2268                                 switch (input->textype) {
2269                                         case GPU_SHADOW2D:
2270                                                 uniform->type = GPU_DYNAMIC_SAMPLER_2DSHADOW;
2271                                                 uniform->lamp = input->dynamicdata;
2272                                                 break;
2273                                         case GPU_TEX2D:
2274                                                 if (GPU_texture_opengl_bindcode(input->tex)) {
2275                                                         uniform->type = GPU_DYNAMIC_SAMPLER_2DBUFFER;
2276                                                         glBindTexture(GL_TEXTURE_2D, GPU_texture_opengl_bindcode(input->tex));
2277                                                         uniform->texsize = GPU_texture_width(input->tex) * GPU_texture_height(input->tex);
2278                                                         uniform->texpixels = MEM_mallocN(uniform->texsize * 4, "RGBApixels");
2279                                                         glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, uniform->texpixels);
2280                                                         glBindTexture(GL_TEXTURE_2D, 0);
2281                                                 }
2282                                                 break;
2283
2284                                         case GPU_NONE:
2285                                         case GPU_TEXCUBE:
2286                                         case GPU_FLOAT:
2287                                         case GPU_VEC2:
2288                                         case GPU_VEC3:
2289                                         case GPU_VEC4:
2290                                         case GPU_MAT3:
2291                                         case GPU_MAT4:
2292                                         case GPU_ATTRIB:
2293                                                 break;
2294                                 }
2295                         }
2296                         else {
2297                                 uniform->type = input->dynamictype;
2298                                 BLI_strncpy(uniform->varname, input->shadername, sizeof(uniform->varname));
2299                                 switch (input->type) {
2300                                         case GPU_FLOAT:
2301                                                 uniform->datatype = GPU_DATA_1F;
2302                                                 break;
2303                                         case GPU_VEC2:
2304                                                 uniform->datatype = GPU_DATA_2F;
2305                                                 break;
2306                                         case GPU_VEC3:
2307                                                 uniform->datatype = GPU_DATA_3F;
2308                                                 break;
2309                                         case GPU_VEC4:
2310                                                 uniform->datatype = GPU_DATA_4F;
2311                                                 break;
2312                                         case GPU_MAT3:
2313                                                 uniform->datatype = GPU_DATA_9F;
2314                                                 break;
2315                                         case GPU_MAT4:
2316                                                 uniform->datatype = GPU_DATA_16F;
2317                                                 break;
2318
2319                                         case GPU_NONE:
2320                                         case GPU_TEX2D:
2321                                         case GPU_TEXCUBE:
2322                                         case GPU_SHADOW2D:
2323                                         case GPU_ATTRIB:
2324                                                 break;
2325                                 }
2326
2327                                 if (GPU_DYNAMIC_GROUP_FROM_TYPE(uniform->type) == GPU_DYNAMIC_GROUP_LAMP)
2328                                         uniform->lamp = input->dynamicdata;
2329
2330                                 if (GPU_DYNAMIC_GROUP_FROM_TYPE(uniform->type) == GPU_DYNAMIC_GROUP_MAT)
2331                                         uniform->material = input->dynamicdata;
2332                         }
2333
2334                         if (uniform->type != GPU_DYNAMIC_NONE)
2335                                 BLI_addtail(&shader->uniforms, uniform);
2336                         else
2337                                 MEM_freeN(uniform);
2338                 }
2339
2340                 /* process builtin uniform */
2341                 for (int i = 0; builtins[i].gputype; i++) {
2342                         if (mat->builtins & builtins[i].gputype) {
2343                                 GPUInputUniform *uniform = MEM_callocN(sizeof(GPUInputUniform), "GPUInputUniform");
2344                                 uniform->type = builtins[i].dynamictype;
2345                                 uniform->datatype = builtins[i].datatype;
2346                                 BLI_strncpy(uniform->varname, GPU_builtin_name(builtins[i].gputype), sizeof(uniform->varname));
2347                                 BLI_addtail(&shader->uniforms, uniform);
2348                         }
2349                 }
2350
2351                 /* now link fragment shader with library shader */
2352                 /* TBD: remove the function that are not used in the main function */
2353                 liblen = (pass->libcode) ? strlen(pass->libcode) : 0;
2354                 fraglen = strlen(pass->fragmentcode);
2355                 shader->fragment = (char *)MEM_mallocN(liblen + fraglen + 1, "GPUFragShader");
2356                 if (pass->libcode)
2357                         memcpy(shader->fragment, pass->libcode, liblen);
2358                 memcpy(&shader->fragment[liblen], pass->fragmentcode, fraglen);
2359                 shader->fragment[liblen + fraglen] = 0;
2360
2361                 // export the attribute
2362                 for (int i = 0; i < mat->attribs.totlayer; i++) {
2363                         GPUInputAttribute *attribute = MEM_callocN(sizeof(GPUInputAttribute), "GPUInputAttribute");
2364                         attribute->type = mat->attribs.layer[i].type;
2365                         attribute->number = mat->attribs.layer[i].glindex;
2366                         BLI_snprintf(attribute->varname, sizeof(attribute->varname), "att%d", mat->attribs.layer[i].attribid);
2367
2368                         switch (attribute->type) {
2369                                 case CD_TANGENT:
2370                                         attribute->datatype = GPU_DATA_4F;
2371                                         break;
2372                                 case CD_MTFACE:
2373                                         attribute->datatype = GPU_DATA_2F;
2374                                         attribute->name = mat->attribs.layer[i].name;
2375                                         break;
2376                                 case CD_MCOL:
2377                                         attribute->datatype = GPU_DATA_4UB;
2378                                         attribute->name = mat->attribs.layer[i].name;
2379                                         break;
2380                                 case CD_ORCO:
2381                                         attribute->datatype = GPU_DATA_3F;
2382                                         break;
2383                         }
2384
2385                         if (attribute->datatype != GPU_DATA_NONE)
2386                                 BLI_addtail(&shader->attributes, attribute);
2387                         else
2388                                 MEM_freeN(attribute);
2389                 }
2390
2391                 /* export the vertex shader */
2392                 shader->vertex = BLI_strdup(pass->vertexcode);
2393         }
2394
2395         return shader;
2396 }
2397
2398 void GPU_free_shader_export(GPUShaderExport *shader)
2399 {
2400         if (shader == NULL)
2401                 return;
2402
2403         for (GPUInputUniform *uniform = shader->uniforms.first; uniform; uniform = uniform->next)
2404                 if (uniform->texpixels)
2405                         MEM_freeN(uniform->texpixels);
2406
2407         BLI_freelistN(&shader->uniforms);
2408         BLI_freelistN(&shader->attributes);
2409
2410         if (shader->vertex)
2411                 MEM_freeN(shader->vertex);
2412         if (shader->fragment)
2413                 MEM_freeN(shader->fragment);
2414
2415         MEM_freeN(shader);
2416 }
2417
2418 #ifdef WITH_OPENSUBDIV
2419 void GPU_material_update_fvar_offset(GPUMaterial *gpu_material,
2420                                      DerivedMesh *dm)
2421 {
2422         GPUPass *pass = gpu_material->pass;
2423         GPUShader *shader = (pass != NULL ? pass->shader : NULL);
2424         ListBase *inputs = (pass != NULL ? &pass->inputs : NULL);
2425         GPUInput *input;
2426
2427         if (shader == NULL) {
2428                 return;
2429         }
2430
2431         GPU_shader_bind(shader);
2432
2433         for (input = inputs->first;
2434              input != NULL;
2435              input = input->next)
2436         {
2437                 if (input->source == GPU_SOURCE_ATTRIB &&
2438                     input->attribtype == CD_MTFACE)
2439                 {
2440                         char name[64];
2441                         /* TODO(sergey): This will work for until names are
2442                          * consistent, we'll need to solve this somehow in the future.
2443                          */
2444                         int layer_index;
2445                         int location;
2446
2447                         if (input->attribname[0] != '\0') {
2448                                 layer_index = CustomData_get_named_layer(&dm->loopData,
2449                                                                          CD_MLOOPUV,
2450                                                                          input->attribname);
2451                         }
2452                         else {
2453                                 layer_index = CustomData_get_active_layer(&dm->loopData,
2454                                                                           CD_MLOOPUV);
2455                         }
2456
2457                         BLI_snprintf(name, sizeof(name),
2458                                      "fvar%d_offset",
2459                                      input->attribid);
2460                         location = GPU_shader_get_uniform(shader, name);
2461                         GPU_shader_uniform_int(shader, location, layer_index);
2462                 }
2463         }
2464
2465         GPU_shader_unbind();
2466 }
2467 #endif