0fad5e47445897f9db8613ce9aa8b59c6245d1af
[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
32
33 #include <math.h>
34 #include <string.h>
35
36 #include "GL/glew.h"
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_lamp_types.h"
41 #include "DNA_material_types.h"
42 #include "DNA_object_types.h"
43 #include "DNA_scene_types.h"
44 #include "DNA_world_types.h"
45
46 #include "BLI_math.h"
47 #include "BLI_blenlib.h"
48 #include "BLI_utildefines.h"
49
50 #include "BKE_anim.h"
51 #include "BKE_colortools.h"
52 #include "BKE_DerivedMesh.h"
53 #include "BKE_global.h"
54 #include "BKE_image.h"
55 #include "BKE_main.h"
56 #include "BKE_node.h"
57 #include "BKE_scene.h"
58 #include "BKE_texture.h"
59
60 #include "IMB_imbuf_types.h"
61
62 #include "GPU_extensions.h"
63 #include "GPU_material.h"
64
65 #include "gpu_codegen.h"
66
67 #include <string.h>
68
69 /* Structs */
70
71 typedef enum DynMatProperty {
72         DYN_LAMP_CO = 1,
73         DYN_LAMP_VEC = 2,
74         DYN_LAMP_IMAT = 4,
75         DYN_LAMP_PERSMAT = 8,
76 } DynMatProperty;
77
78 struct GPUMaterial {
79         Scene *scene;
80         Material *ma;
81
82         /* for creating the material */
83         ListBase nodes;
84         GPUNodeLink *outlink;
85
86         /* for binding the material */
87         GPUPass *pass;
88         GPUVertexAttribs attribs;
89         int bound;
90         int builtins;
91         int alpha, obcolalpha;
92         int dynproperty;
93
94         /* for passing uniforms */
95         int viewmatloc, invviewmatloc;
96         int obmatloc, invobmatloc;
97         int obcolloc, obautobumpscaleloc;
98
99         ListBase lamps;
100 };
101
102 struct GPULamp {
103         Scene *scene;
104         Object *ob;
105         Object *par;
106         Lamp *la;
107
108         int type, mode, lay, hide;
109
110         float dynenergy, dyncol[3];
111         float energy, col[3];
112
113         float co[3], vec[3];
114         float dynco[3], dynvec[3];
115         float obmat[4][4];
116         float imat[4][4];
117         float dynimat[4][4];
118
119         float spotsi, spotbl, k;
120         float dist, att1, att2;
121         float shadow_color[3];
122
123         float bias, d, clipend;
124         int size;
125
126         int falloff_type;
127         struct CurveMapping *curfalloff;
128
129         float winmat[4][4];
130         float viewmat[4][4];
131         float persmat[4][4];
132         float dynpersmat[4][4];
133
134         GPUFrameBuffer *fb;
135         GPUFrameBuffer *blurfb;
136         GPUTexture *tex;
137         GPUTexture *depthtex;
138         GPUTexture *blurtex;
139
140         ListBase materials;
141 };
142
143 /* Forward declaration so shade_light_textures() can use this, while still keeping the code somewhat organized */
144 static void texture_rgb_blend(GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg, int blendtype, GPUNodeLink **in);
145
146 /* Functions */
147
148 static GPUMaterial *GPU_material_construct_begin(Material *ma)
149 {
150         GPUMaterial *material = MEM_callocN(sizeof(GPUMaterial), "GPUMaterial");
151
152         material->ma= ma;
153
154         return material;
155 }
156
157 static void gpu_material_set_attrib_id(GPUMaterial *material)
158 {
159         GPUVertexAttribs *attribs;
160         GPUShader *shader;
161         GPUPass *pass;
162         char name[32];
163         int a, b;
164
165         attribs= &material->attribs;
166         pass= material->pass;
167         if (!pass) {
168                 attribs->totlayer = 0;
169                 return;
170         }
171         
172         shader= GPU_pass_shader(pass);
173         if (!shader) {
174                 attribs->totlayer = 0;
175                 return;
176         }
177
178         /* convert from attribute number to the actual id assigned by opengl,
179          * in case the attrib does not get a valid index back, it was probably
180          * removed by the glsl compiler by dead code elimination */
181
182         for (a=0, b=0; a<attribs->totlayer; a++) {
183                 BLI_snprintf(name, sizeof(name), "att%d", attribs->layer[a].attribid);
184                 attribs->layer[a].glindex = GPU_shader_get_attribute(shader, name);
185
186                 if (attribs->layer[a].glindex >= 0) {
187                         attribs->layer[b] = attribs->layer[a];
188                         b++;
189                 }
190         }
191
192         attribs->totlayer = b;
193 }
194
195 static int GPU_material_construct_end(GPUMaterial *material)
196 {
197         if (material->outlink) {
198                 GPUNodeLink *outlink;
199                 GPUShader *shader;
200
201                 outlink = material->outlink;
202                 material->pass = GPU_generate_pass(&material->nodes, outlink,
203                         &material->attribs, &material->builtins, material->ma->id.name);
204
205                 if (!material->pass)
206                         return 0;
207
208                 gpu_material_set_attrib_id(material);
209                 
210                 shader = GPU_pass_shader(material->pass);
211
212                 if (material->builtins & GPU_VIEW_MATRIX)
213                         material->viewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_VIEW_MATRIX));
214                 if (material->builtins & GPU_INVERSE_VIEW_MATRIX)
215                         material->invviewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_INVERSE_VIEW_MATRIX));
216                 if (material->builtins & GPU_OBJECT_MATRIX)
217                         material->obmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_OBJECT_MATRIX));
218                 if (material->builtins & GPU_INVERSE_OBJECT_MATRIX)
219                         material->invobmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_INVERSE_OBJECT_MATRIX));
220                 if (material->builtins & GPU_OBCOLOR)
221                         material->obcolloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_OBCOLOR));
222                 if (material->builtins & GPU_AUTO_BUMPSCALE)
223                         material->obautobumpscaleloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_AUTO_BUMPSCALE));
224                 return 1;
225         }
226
227         return 0;
228 }
229
230 void GPU_material_free(Material *ma)
231 {
232         LinkData *link;
233         LinkData *nlink, *mlink, *next;
234
235         for (link=ma->gpumaterial.first; link; link=link->next) {
236                 GPUMaterial *material = link->data;
237
238                 if (material->pass)
239                         GPU_pass_free(material->pass);
240
241                 for (nlink=material->lamps.first; nlink; nlink=nlink->next) {
242                         GPULamp *lamp = nlink->data;
243
244                         for (mlink=lamp->materials.first; mlink; mlink=next) {
245                                 next = mlink->next;
246                                 if (mlink->data == ma)
247                                         BLI_freelinkN(&lamp->materials, mlink);
248                         }
249                 }
250
251                 BLI_freelistN(&material->lamps);
252
253                 MEM_freeN(material);
254         }
255
256         BLI_freelistN(&ma->gpumaterial);
257 }
258
259 void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double time, int mipmap)
260 {
261         if (material->pass) {
262                 LinkData *nlink;
263                 GPULamp *lamp;
264
265                 /* handle layer lamps */
266                 for (nlink=material->lamps.first; nlink; nlink=nlink->next) {
267                         lamp= nlink->data;
268
269                         if (!lamp->hide && (lamp->lay & viewlay) && (!(lamp->mode & LA_LAYER) || (lamp->lay & oblay))) {
270                                 lamp->dynenergy = lamp->energy;
271                                 copy_v3_v3(lamp->dyncol, lamp->col);
272                         }
273                         else {
274                                 lamp->dynenergy = 0.0f;
275                                 lamp->dyncol[0]= lamp->dyncol[1]= lamp->dyncol[2] = 0.0f;
276                         }
277                 }
278
279                 GPU_pass_bind(material->pass, time, mipmap);
280                 material->bound = 1;
281         }
282 }
283
284 void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[][4], float viewmat[][4], float viewinv[][4], float obcol[4], float autobumpscale)
285 {
286         if (material->pass) {
287                 GPUShader *shader = GPU_pass_shader(material->pass);
288                 LinkData *nlink;
289                 GPULamp *lamp;
290                 float invmat[4][4], col[4];
291
292                 /* handle builtins */
293                 if (material->builtins & GPU_VIEW_MATRIX) {
294                         GPU_shader_uniform_vector(shader, material->viewmatloc, 16, 1, (float*)viewmat);
295                 }
296                 if (material->builtins & GPU_INVERSE_VIEW_MATRIX) {
297                         GPU_shader_uniform_vector(shader, material->invviewmatloc, 16, 1, (float*)viewinv);
298                 }
299                 if (material->builtins & GPU_OBJECT_MATRIX) {
300                         GPU_shader_uniform_vector(shader, material->obmatloc, 16, 1, (float*)obmat);
301                 }
302                 if (material->builtins & GPU_INVERSE_OBJECT_MATRIX) {
303                         invert_m4_m4(invmat, obmat);
304                         GPU_shader_uniform_vector(shader, material->invobmatloc, 16, 1, (float*)invmat);
305                 }
306                 if (material->builtins & GPU_OBCOLOR) {
307                         copy_v4_v4(col, obcol);
308                         CLAMP(col[3], 0.0f, 1.0f);
309                         GPU_shader_uniform_vector(shader, material->obcolloc, 4, 1, col);
310                 }
311                 if (material->builtins & GPU_AUTO_BUMPSCALE) {
312                         GPU_shader_uniform_vector(shader, material->obautobumpscaleloc, 1, 1, &autobumpscale);
313                 }
314                 /* update lamps */
315                 for (nlink=material->lamps.first; nlink; nlink=nlink->next) {
316                         lamp= nlink->data;
317
318                         if (material->dynproperty & DYN_LAMP_VEC) {
319                                 copy_v3_v3(lamp->dynvec, lamp->vec);
320                                 normalize_v3(lamp->dynvec);
321                                 negate_v3(lamp->dynvec);
322                                 mul_mat3_m4_v3(viewmat, lamp->dynvec);
323                         }
324
325                         if (material->dynproperty & DYN_LAMP_CO) {
326                                 copy_v3_v3(lamp->dynco, lamp->co);
327                                 mul_m4_v3(viewmat, lamp->dynco);
328                         }
329
330                         if (material->dynproperty & DYN_LAMP_IMAT) {
331                                 mult_m4_m4m4(lamp->dynimat, lamp->imat, viewinv);
332                         }
333
334                         if (material->dynproperty & DYN_LAMP_PERSMAT) {
335                                 if (!GPU_lamp_has_shadow_buffer(lamp)) /* The lamp matrices are already updated if we're using shadow buffers */
336                                         GPU_lamp_update_buffer_mats(lamp);
337                                 mult_m4_m4m4(lamp->dynpersmat, lamp->persmat, viewinv);
338                         }
339                 }
340
341                 GPU_pass_update_uniforms(material->pass);
342         }
343 }
344
345 void GPU_material_unbind(GPUMaterial *material)
346 {
347         if (material->pass) {
348                 material->bound = 0;
349                 GPU_pass_unbind(material->pass);
350         }
351 }
352
353 int GPU_material_bound(GPUMaterial *material)
354 {
355         return material->bound;
356 }
357
358 void GPU_material_vertex_attributes(GPUMaterial *material, GPUVertexAttribs *attribs)
359 {
360         *attribs = material->attribs;
361 }
362
363 void GPU_material_output_link(GPUMaterial *material, GPUNodeLink *link)
364 {
365         if (!material->outlink)
366                 material->outlink= link;
367 }
368
369 void GPU_material_enable_alpha(GPUMaterial *material)
370 {
371         material->alpha= 1;
372 }
373
374 GPUBlendMode GPU_material_alpha_blend(GPUMaterial *material, float obcol[4])
375 {
376         if (material->alpha || (material->obcolalpha && obcol[3] < 1.0f))
377                 return GPU_BLEND_ALPHA;
378         else
379                 return GPU_BLEND_SOLID;
380 }
381
382 void gpu_material_add_node(GPUMaterial *material, GPUNode *node)
383 {
384         BLI_addtail(&material->nodes, node);
385 }
386
387 /* Code generation */
388
389 static int gpu_do_color_management(GPUMaterial *mat)
390 {
391         return ((mat->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT) &&
392            !((mat->scene->gm.flag & GAME_GLSL_NO_COLOR_MANAGEMENT)));
393 }
394
395 static GPUNodeLink *lamp_get_visibility(GPUMaterial *mat, GPULamp *lamp, GPUNodeLink **lv, GPUNodeLink **dist)
396 {
397         GPUNodeLink *visifac, *inpr;
398
399         /* from get_lamp_visibility */
400         if (lamp->type==LA_SUN || lamp->type==LA_HEMI) {
401                 mat->dynproperty |= DYN_LAMP_VEC;
402                 GPU_link(mat, "lamp_visibility_sun_hemi", GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob), lv, dist, &visifac);
403                 return visifac;
404         }
405         else {
406                 mat->dynproperty |= DYN_LAMP_CO;
407                 GPU_link(mat, "lamp_visibility_other", GPU_builtin(GPU_VIEW_POSITION), GPU_dynamic_uniform(lamp->dynco, GPU_DYNAMIC_LAMP_DYNCO, lamp->ob), lv, dist, &visifac);
408
409                 if (lamp->type==LA_AREA)
410                         return visifac;
411
412                 switch (lamp->falloff_type) {
413                         case LA_FALLOFF_CONSTANT:
414                                 break;
415                         case LA_FALLOFF_INVLINEAR:
416                                 GPU_link(mat, "lamp_falloff_invlinear", GPU_uniform(&lamp->dist), *dist, &visifac);
417                                 break;
418                         case LA_FALLOFF_INVSQUARE:
419                                 GPU_link(mat, "lamp_falloff_invsquare", GPU_uniform(&lamp->dist), *dist, &visifac);
420                                 break;
421                         case LA_FALLOFF_SLIDERS:
422                                 GPU_link(mat, "lamp_falloff_sliders", GPU_uniform(&lamp->dist), GPU_uniform(&lamp->att1), GPU_uniform(&lamp->att2), *dist, &visifac);
423                                 break;
424                         case LA_FALLOFF_CURVE:
425                                 {
426                                         float *array;
427                                         int size;
428
429                                         curvemapping_table_RGBA(lamp->curfalloff, &array, &size);
430                                         GPU_link(mat, "lamp_falloff_curve", GPU_uniform(&lamp->dist), GPU_texture(size, array), *dist, &visifac);
431                                 }
432                                 break;
433                 }
434
435                 if (lamp->mode & LA_SPHERE)
436                         GPU_link(mat, "lamp_visibility_sphere", GPU_uniform(&lamp->dist), *dist, visifac, &visifac);
437
438                 if (lamp->type == LA_SPOT) {
439                         if (lamp->mode & LA_SQUARE) {
440                                 mat->dynproperty |= DYN_LAMP_VEC|DYN_LAMP_IMAT;
441                                 GPU_link(mat, "lamp_visibility_spot_square", GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob), GPU_dynamic_uniform((float*)lamp->dynimat, GPU_DYNAMIC_LAMP_DYNIMAT, lamp->ob), *lv, &inpr);
442                         }
443                         else {
444                                 mat->dynproperty |= DYN_LAMP_VEC;
445                                 GPU_link(mat, "lamp_visibility_spot_circle", GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob), *lv, &inpr);
446                         }
447                         
448                         GPU_link(mat, "lamp_visibility_spot", GPU_uniform(&lamp->spotsi), GPU_uniform(&lamp->spotbl), inpr, visifac, &visifac);
449                 }
450
451                 GPU_link(mat, "lamp_visibility_clamp", visifac, &visifac);
452
453                 return visifac;
454         }
455 }
456
457 #if 0
458 static void area_lamp_vectors(LampRen *lar)
459 {
460         float xsize= 0.5*lar->area_size, ysize= 0.5*lar->area_sizey, multifac;
461
462         /* make it smaller, so area light can be multisampled */
463         multifac= 1.0f/sqrt((float)lar->ray_totsamp);
464         xsize *= multifac;
465         ysize *= multifac;
466
467         /* corner vectors */
468         lar->area[0][0]= lar->co[0] - xsize*lar->mat[0][0] - ysize*lar->mat[1][0];
469         lar->area[0][1]= lar->co[1] - xsize*lar->mat[0][1] - ysize*lar->mat[1][1];
470         lar->area[0][2]= lar->co[2] - xsize*lar->mat[0][2] - ysize*lar->mat[1][2];
471
472         /* corner vectors */
473         lar->area[1][0]= lar->co[0] - xsize*lar->mat[0][0] + ysize*lar->mat[1][0];
474         lar->area[1][1]= lar->co[1] - xsize*lar->mat[0][1] + ysize*lar->mat[1][1];
475         lar->area[1][2]= lar->co[2] - xsize*lar->mat[0][2] + ysize*lar->mat[1][2];
476
477         /* corner vectors */
478         lar->area[2][0]= lar->co[0] + xsize*lar->mat[0][0] + ysize*lar->mat[1][0];
479         lar->area[2][1]= lar->co[1] + xsize*lar->mat[0][1] + ysize*lar->mat[1][1];
480         lar->area[2][2]= lar->co[2] + xsize*lar->mat[0][2] + ysize*lar->mat[1][2];
481
482         /* corner vectors */
483         lar->area[3][0]= lar->co[0] + xsize*lar->mat[0][0] - ysize*lar->mat[1][0];
484         lar->area[3][1]= lar->co[1] + xsize*lar->mat[0][1] - ysize*lar->mat[1][1];
485         lar->area[3][2]= lar->co[2] + xsize*lar->mat[0][2] - ysize*lar->mat[1][2];
486         /* only for correction button size, matrix size works on energy */
487         lar->areasize= lar->dist*lar->dist/(4.0*xsize*ysize);
488 }
489 #endif
490
491 static void ramp_blend(GPUMaterial *mat, GPUNodeLink *fac, GPUNodeLink *col1, GPUNodeLink *col2, int type, GPUNodeLink **outcol)
492 {
493         static const char *names[] = {"mix_blend", "mix_add", "mix_mult", "mix_sub",
494                 "mix_screen", "mix_div", "mix_diff", "mix_dark", "mix_light",
495                 "mix_overlay", "mix_dodge", "mix_burn", "mix_hue", "mix_sat",
496                 "mix_val", "mix_color", "mix_soft", "mix_linear"};
497
498         GPU_link(mat, names[type], fac, col1, col2, outcol);
499 }
500
501 static void do_colorband_blend(GPUMaterial *mat, ColorBand *coba, GPUNodeLink *fac, float rampfac, int type, GPUNodeLink *incol, GPUNodeLink **outcol)
502 {
503         GPUNodeLink *tmp, *alpha, *col;
504         float *array;
505         int size;
506
507         /* do colorband */
508         colorband_table_RGBA(coba, &array, &size);
509         GPU_link(mat, "valtorgb", fac, GPU_texture(size, array), &col, &tmp);
510
511         /* use alpha in fac */
512         GPU_link(mat, "mtex_alpha_from_col", col, &alpha);
513         GPU_link(mat, "math_multiply", alpha, GPU_uniform(&rampfac), &fac);
514
515         /* blending method */
516         ramp_blend(mat, fac, incol, col, type, outcol);
517 }
518
519 static void ramp_diffuse_result(GPUShadeInput *shi, GPUNodeLink **diff)
520 {
521         Material *ma= shi->mat;
522         GPUMaterial *mat= shi->gpumat;
523         GPUNodeLink *fac;
524
525         if (!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS)) {
526                 if (ma->ramp_col) {
527                         if (ma->rampin_col==MA_RAMP_IN_RESULT) {
528                                 GPU_link(mat, "ramp_rgbtobw", *diff, &fac);
529                                 
530                                 /* colorband + blend */
531                                 do_colorband_blend(mat, ma->ramp_col, fac, ma->rampfac_col, ma->rampblend_col, *diff, diff);
532                         }
533                 }
534         }
535 }
536
537 static void add_to_diffuse(GPUMaterial *mat, Material *ma, GPUShadeInput *shi, GPUNodeLink *is, GPUNodeLink *rgb, GPUNodeLink **diff)
538 {
539         GPUNodeLink *fac, *tmp, *addcol;
540         
541         if (!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS) &&
542             ma->ramp_col && (ma->mode & MA_RAMP_COL))
543         {
544                 /* MA_RAMP_IN_RESULT is exceptional */
545                 if (ma->rampin_col==MA_RAMP_IN_RESULT) {
546                         addcol = shi->rgb;
547                 }
548                 else {
549                         /* input */
550                         switch (ma->rampin_col) {
551                         case MA_RAMP_IN_ENERGY:
552                                 GPU_link(mat, "ramp_rgbtobw", rgb, &fac);
553                                 break;
554                         case MA_RAMP_IN_SHADER:
555                                 fac= is;
556                                 break;
557                         case MA_RAMP_IN_NOR:
558                                 GPU_link(mat, "vec_math_dot", shi->view, shi->vn, &tmp, &fac);
559                                 break;
560                         default:
561                                 GPU_link(mat, "set_value_zero", &fac);
562                                 break;
563                         }
564
565                         /* colorband + blend */
566                         do_colorband_blend(mat, ma->ramp_col, fac, ma->rampfac_col, ma->rampblend_col, shi->rgb, &addcol);
567                 }
568         }
569         else
570                 addcol = shi->rgb;
571
572         /* output to */
573         GPU_link(mat, "shade_madd_clamped", *diff, rgb, addcol, diff);
574 }
575
576 static void ramp_spec_result(GPUShadeInput *shi, GPUNodeLink **spec)
577 {
578         Material *ma= shi->mat;
579         GPUMaterial *mat= shi->gpumat;
580         GPUNodeLink *fac;
581
582         if (!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS) &&
583             ma->ramp_spec && ma->rampin_spec==MA_RAMP_IN_RESULT)
584         {
585                 GPU_link(mat, "ramp_rgbtobw", *spec, &fac);
586                 
587                 /* colorband + blend */
588                 do_colorband_blend(mat, ma->ramp_spec, fac, ma->rampfac_spec, ma->rampblend_spec, *spec, spec);
589         }
590 }
591
592 static void do_specular_ramp(GPUShadeInput *shi, GPUNodeLink *is, GPUNodeLink *t, GPUNodeLink **spec)
593 {
594         Material *ma= shi->mat;
595         GPUMaterial *mat= shi->gpumat;
596         GPUNodeLink *fac, *tmp;
597
598         *spec = shi->specrgb;
599
600         /* MA_RAMP_IN_RESULT is exception */
601         if (ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
602                 
603                 /* input */
604                 switch (ma->rampin_spec) {
605                 case MA_RAMP_IN_ENERGY:
606                         fac = t;
607                         break;
608                 case MA_RAMP_IN_SHADER:
609                         fac = is;
610                         break;
611                 case MA_RAMP_IN_NOR:
612                         GPU_link(mat, "vec_math_dot", shi->view, shi->vn, &tmp, &fac);
613                         break;
614                 default:
615                         GPU_link(mat, "set_value_zero", &fac);
616                         break;
617                 }
618                 
619                 /* colorband + blend */
620                 do_colorband_blend(mat, ma->ramp_spec, fac, ma->rampfac_spec, ma->rampblend_spec, *spec, spec);
621         }
622 }
623
624 static void add_user_list(ListBase *list, void *data)
625 {
626         LinkData *link = MEM_callocN(sizeof(LinkData), "GPULinkData");
627         link->data = data;
628         BLI_addtail(list, link);
629 }
630
631 static void shade_light_textures(GPUMaterial *mat, GPULamp *lamp, GPUNodeLink **rgb)
632 {
633         GPUNodeLink *tex_rgb;
634         MTex *mtex = NULL;
635         int i;
636         float one = 1.f;
637
638         for (i=0; i<MAX_MTEX; ++i) {
639                 mtex = lamp->la->mtex[i];
640
641                 if (mtex && mtex->tex->type & TEX_IMAGE && mtex->tex->ima) {
642                         mat->dynproperty |= DYN_LAMP_PERSMAT;
643
644                         GPU_link(mat, "shade_light_texture",
645                                 GPU_builtin(GPU_VIEW_POSITION),
646                                 GPU_image(mtex->tex->ima, &mtex->tex->iuser),
647                                 GPU_dynamic_uniform((float*)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
648                                 &tex_rgb);
649                         texture_rgb_blend(mat, tex_rgb, *rgb, GPU_uniform(&one), GPU_uniform(&mtex->colfac), mtex->blendtype, rgb); 
650                 }
651         }
652 }
653
654 static void shade_one_light(GPUShadeInput *shi, GPUShadeResult *shr, GPULamp *lamp)
655 {
656         Material *ma= shi->mat;
657         GPUMaterial *mat= shi->gpumat;
658         GPUNodeLink *lv, *dist, *visifac, *is, *inp, *i, *vn, *view;
659         GPUNodeLink *outcol, *specfac, *t, *shadfac= NULL, *lcol;
660         float one = 1.0f;
661
662         if ((lamp->mode & LA_ONLYSHADOW) && !(ma->mode & MA_SHADOW))
663                 return;
664         
665         vn= shi->vn;
666         view= shi->view;
667
668         visifac= lamp_get_visibility(mat, lamp, &lv, &dist);
669
670         /*if (ma->mode & MA_TANGENT_V)
671                 GPU_link(mat, "shade_tangent_v", lv, GPU_attribute(CD_TANGENT, ""), &vn);*/
672         
673         GPU_link(mat, "shade_inp", vn, lv, &inp);
674
675         if (lamp->mode & LA_NO_DIFF) {
676                 GPU_link(mat, "shade_is_no_diffuse", &is);
677         }
678         else if (lamp->type == LA_HEMI) {
679                 GPU_link(mat, "shade_is_hemi", inp, &is);
680         }
681         else {
682                 if (lamp->type == LA_AREA) {
683                         float area[4][4]= {{0.0f}}, areasize= 0.0f;
684
685                         mat->dynproperty |= DYN_LAMP_VEC|DYN_LAMP_CO;
686                         GPU_link(mat, "shade_inp_area", GPU_builtin(GPU_VIEW_POSITION), GPU_dynamic_uniform(lamp->dynco, GPU_DYNAMIC_LAMP_DYNCO, lamp->ob), GPU_dynamic_uniform(lamp->dynvec, GPU_DYNAMIC_LAMP_DYNVEC, lamp->ob), vn, GPU_uniform((float*)area),
687                                 GPU_uniform(&areasize), GPU_uniform(&lamp->k), &inp);
688                 }
689
690                 is= inp; /* Lambert */
691
692                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_SHADERS)) {
693                         if (ma->diff_shader==MA_DIFF_ORENNAYAR)
694                                 GPU_link(mat, "shade_diffuse_oren_nayer", inp, vn, lv, view, GPU_uniform(&ma->roughness), &is);
695                         else if (ma->diff_shader==MA_DIFF_TOON)
696                                 GPU_link(mat, "shade_diffuse_toon", vn, lv, view, GPU_uniform(&ma->param[0]), GPU_uniform(&ma->param[1]), &is);
697                         else if (ma->diff_shader==MA_DIFF_MINNAERT)
698                                 GPU_link(mat, "shade_diffuse_minnaert", inp, vn, view, GPU_uniform(&ma->darkness), &is);
699                         else if (ma->diff_shader==MA_DIFF_FRESNEL)
700                                 GPU_link(mat, "shade_diffuse_fresnel", vn, lv, view, GPU_uniform(&ma->param[0]), GPU_uniform(&ma->param[1]), &is);
701                 }
702         }
703
704         if (!(mat->scene->gm.flag & GAME_GLSL_NO_SHADERS))
705                 if (ma->shade_flag & MA_CUBIC)
706                         GPU_link(mat, "shade_cubic", is, &is);
707         
708         i = is;
709         GPU_link(mat, "shade_visifac", i, visifac, shi->refl, &i);
710         
711         GPU_link(mat, "set_value", GPU_dynamic_uniform(lamp->dyncol, GPU_DYNAMIC_LAMP_DYNCOL, lamp->ob), &lcol);
712         shade_light_textures(mat, lamp, &lcol);
713
714 #if 0
715         if (ma->mode & MA_TANGENT_VN)
716                 GPU_link(mat, "shade_tangent_v_spec", GPU_attribute(CD_TANGENT, ""), &vn);
717 #endif
718
719         /* this replaces if (i > 0.0) conditional until that is supported */
720         // done in shade_visifac now, GPU_link(mat, "mtex_value_clamp_positive", i, &i);
721
722         if ((ma->mode & MA_SHADOW) && GPU_lamp_has_shadow_buffer(lamp)) {
723                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_SHADOWS)) {
724                         mat->dynproperty |= DYN_LAMP_PERSMAT;
725                         
726                         if (lamp->la->shadowmap_type == LA_SHADMAP_VARIANCE) {
727                                 GPU_link(mat, "test_shadowbuf_vsm",
728                                         GPU_builtin(GPU_VIEW_POSITION),
729                                         GPU_dynamic_texture(lamp->tex, GPU_DYNAMIC_SAMPLER_2DSHADOW, lamp->ob),
730                                         GPU_dynamic_uniform((float*)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
731                                         GPU_uniform(&lamp->bias), GPU_uniform(&lamp->la->bleedbias), inp, &shadfac);
732                         }
733                         else {
734                                 GPU_link(mat, "test_shadowbuf",
735                                         GPU_builtin(GPU_VIEW_POSITION),
736                                         GPU_dynamic_texture(lamp->tex, GPU_DYNAMIC_SAMPLER_2DSHADOW, lamp->ob),
737                                         GPU_dynamic_uniform((float*)lamp->dynpersmat, GPU_DYNAMIC_LAMP_DYNPERSMAT, lamp->ob),
738                                         GPU_uniform(&lamp->bias), inp, &shadfac);
739                         }
740                         
741                         if (lamp->mode & LA_ONLYSHADOW) {
742                                 GPU_link(mat, "shade_only_shadow", i, shadfac,
743                                         GPU_dynamic_uniform(&lamp->dynenergy, GPU_DYNAMIC_LAMP_DYNENERGY, lamp->ob), &shadfac);
744                                 
745                                 if (!(lamp->mode & LA_NO_DIFF)) {
746                                         GPU_link(mat, "mix_mult", shadfac, shr->diff,
747                                                 GPU_uniform(lamp->shadow_color), &shr->diff);
748                                 }
749
750                                 if (!(lamp->mode & LA_NO_SPEC))
751                                         GPU_link(mat, "shade_only_shadow_specular", shadfac, shi->specrgb,
752                                                 shr->spec, &shr->spec);
753                                 
754                                 add_user_list(&mat->lamps, lamp);
755                                 add_user_list(&lamp->materials, shi->gpumat->ma);
756                                 return;
757                         }
758                 }
759         }
760         else if ((mat->scene->gm.flag & GAME_GLSL_NO_SHADOWS) && (lamp->mode & LA_ONLYSHADOW)) {
761                 add_user_list(&mat->lamps, lamp);
762                 add_user_list(&lamp->materials, shi->gpumat->ma);
763                 return;
764         }
765         else
766                 GPU_link(mat, "set_value", GPU_uniform(&one), &shadfac);
767
768         if (GPU_link_changed(shi->refl) || ma->ref != 0.0f) {
769                 if (!(lamp->mode & LA_NO_DIFF)) {
770                         GPUNodeLink *rgb;
771                         GPU_link(mat, "shade_mul_value", i, lcol, &rgb);
772                         GPU_link(mat, "mtex_value_invert", shadfac, &shadfac);
773                         GPU_link(mat, "mix_mult",  shadfac, rgb, GPU_uniform(lamp->shadow_color), &rgb);
774                         GPU_link(mat, "mtex_value_invert", shadfac, &shadfac);
775                         add_to_diffuse(mat, ma, shi, is, rgb, &shr->diff);
776                 }
777         }
778
779         if (mat->scene->gm.flag & GAME_GLSL_NO_SHADERS);
780         else if (!(lamp->mode & LA_NO_SPEC) && !(lamp->mode & LA_ONLYSHADOW) &&
781                  (GPU_link_changed(shi->spec) || ma->spec != 0.0f))
782         {
783                 if (lamp->type == LA_HEMI) {
784                         GPU_link(mat, "shade_hemi_spec", vn, lv, view, GPU_uniform(&ma->spec), shi->har, visifac, &t);
785                         GPU_link(mat, "shade_add_spec", t, lcol, shi->specrgb, &outcol);
786                         GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
787                 }
788                 else {
789                         if (ma->spec_shader==MA_SPEC_PHONG)
790                                 GPU_link(mat, "shade_phong_spec", vn, lv, view, shi->har, &specfac);
791                         else if (ma->spec_shader==MA_SPEC_COOKTORR)
792                                 GPU_link(mat, "shade_cooktorr_spec", vn, lv, view, shi->har, &specfac);
793                         else if (ma->spec_shader==MA_SPEC_BLINN)
794                                 GPU_link(mat, "shade_blinn_spec", vn, lv, view, GPU_uniform(&ma->refrac), shi->har, &specfac);
795                         else if (ma->spec_shader==MA_SPEC_WARDISO)
796                                 GPU_link(mat, "shade_wardiso_spec", vn, lv, view, GPU_uniform(&ma->rms), &specfac);
797                         else
798                                 GPU_link(mat, "shade_toon_spec", vn, lv, view, GPU_uniform(&ma->param[2]), GPU_uniform(&ma->param[3]), &specfac);
799
800                         if (lamp->type==LA_AREA)
801                                 GPU_link(mat, "shade_spec_area_inp", specfac, inp, &specfac);
802
803                         GPU_link(mat, "shade_spec_t", shadfac, shi->spec, visifac, specfac, &t); 
804
805                         if (ma->mode & MA_RAMP_SPEC) {
806                                 GPUNodeLink *spec;
807                                 do_specular_ramp(shi, specfac, t, &spec);
808                                 GPU_link(mat, "shade_add_spec", t, lcol, spec, &outcol);
809                                 GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
810                         }
811                         else {
812                                 GPU_link(mat, "shade_add_spec", t, lcol, shi->specrgb, &outcol);
813                                 GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
814                         }
815                 }
816         }
817
818         add_user_list(&mat->lamps, lamp);
819         add_user_list(&lamp->materials, shi->gpumat->ma);
820 }
821
822 static void material_lights(GPUShadeInput *shi, GPUShadeResult *shr)
823 {
824         Base *base;
825         Object *ob;
826         Scene *sce_iter;
827         GPULamp *lamp;
828         
829         for (SETLOOPER(shi->gpumat->scene, sce_iter, base)) {
830                 ob= base->object;
831
832                 if (ob->type==OB_LAMP) {
833                         lamp = GPU_lamp_from_blender(shi->gpumat->scene, ob, NULL);
834                         if (lamp)
835                                 shade_one_light(shi, shr, lamp);
836                 }
837
838                 if (ob->transflag & OB_DUPLI) {
839                         DupliObject *dob;
840                         ListBase *lb = object_duplilist(shi->gpumat->scene, ob);
841                         
842                         for (dob=lb->first; dob; dob=dob->next) {
843                                 Object *ob_iter = dob->ob;
844
845                                 if (ob_iter->type==OB_LAMP) {
846                                         copy_m4_m4(ob_iter->obmat, dob->mat);
847
848                                         lamp = GPU_lamp_from_blender(shi->gpumat->scene, ob_iter, ob);
849                                         if (lamp)
850                                                 shade_one_light(shi, shr, lamp);
851                                 }
852                         }
853                         
854                         free_object_duplilist(lb);
855                 }
856         }
857 }
858
859 static void texture_rgb_blend(GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg, int blendtype, GPUNodeLink **in)
860 {
861         switch (blendtype) {
862         case MTEX_BLEND:
863                 GPU_link(mat, "mtex_rgb_blend", out, tex, fact, facg, in);
864                 break;
865         case MTEX_MUL:
866                 GPU_link(mat, "mtex_rgb_mul", out, tex, fact, facg, in);
867                 break;
868         case MTEX_SCREEN:
869                 GPU_link(mat, "mtex_rgb_screen", out, tex, fact, facg, in);
870                 break;
871         case MTEX_OVERLAY:
872                 GPU_link(mat, "mtex_rgb_overlay", out, tex, fact, facg, in);
873                 break;
874         case MTEX_SUB:
875                 GPU_link(mat, "mtex_rgb_sub", out, tex, fact, facg, in);
876                 break;
877         case MTEX_ADD:
878                 GPU_link(mat, "mtex_rgb_add", out, tex, fact, facg, in);
879                 break;
880         case MTEX_DIV:
881                 GPU_link(mat, "mtex_rgb_div", out, tex, fact, facg, in);
882                 break;
883         case MTEX_DIFF:
884                 GPU_link(mat, "mtex_rgb_diff", out, tex, fact, facg, in);
885                 break;
886         case MTEX_DARK:
887                 GPU_link(mat, "mtex_rgb_dark", out, tex, fact, facg, in);
888                 break;
889         case MTEX_LIGHT:
890                 GPU_link(mat, "mtex_rgb_light", out, tex, fact, facg, in);
891                 break;
892         case MTEX_BLEND_HUE:
893                 GPU_link(mat, "mtex_rgb_hue", out, tex, fact, facg, in);
894                 break;
895         case MTEX_BLEND_SAT:
896                 GPU_link(mat, "mtex_rgb_sat", out, tex, fact, facg, in);
897                 break;
898         case MTEX_BLEND_VAL:
899                 GPU_link(mat, "mtex_rgb_val", out, tex, fact, facg, in);
900                 break;
901         case MTEX_BLEND_COLOR:
902                 GPU_link(mat, "mtex_rgb_color", out, tex, fact, facg, in);
903                 break;
904         default:
905                 GPU_link(mat, "set_rgb_zero", &in);
906                 break;
907         }
908 }
909
910 static void texture_value_blend(GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg, int blendtype, GPUNodeLink **in)
911 {
912         switch (blendtype) {
913         case MTEX_BLEND:
914                 GPU_link(mat, "mtex_value_blend", out, tex, fact, facg, in);
915                 break;
916         case MTEX_MUL:
917                 GPU_link(mat, "mtex_value_mul", out, tex, fact, facg, in);
918                 break;
919         case MTEX_SCREEN:
920                 GPU_link(mat, "mtex_value_screen", out, tex, fact, facg, in);
921                 break;
922         case MTEX_SUB:
923                 GPU_link(mat, "mtex_value_sub", out, tex, fact, facg, in);
924                 break;
925         case MTEX_ADD:
926                 GPU_link(mat, "mtex_value_add", out, tex, fact, facg, in);
927                 break;
928         case MTEX_DIV:
929                 GPU_link(mat, "mtex_value_div", out, tex, fact, facg, in);
930                 break;
931         case MTEX_DIFF:
932                 GPU_link(mat, "mtex_value_diff", out, tex, fact, facg, in);
933                 break;
934         case MTEX_DARK:
935                 GPU_link(mat, "mtex_value_dark", out, tex, fact, facg, in);
936                 break;
937         case MTEX_LIGHT:
938                 GPU_link(mat, "mtex_value_light", out, tex, fact, facg, in);
939                 break;
940         default:
941                 GPU_link(mat, "set_value_zero", &in);
942                 break;
943         }
944 }
945
946 static void do_material_tex(GPUShadeInput *shi)
947 {
948         Material *ma= shi->mat;
949         GPUMaterial *mat= shi->gpumat;
950         MTex *mtex;
951         Tex *tex;
952         GPUNodeLink *texco, *tin, *trgb, *tnor, *tcol, *stencil, *tnorfac;
953         GPUNodeLink *texco_norm, *texco_orco, *texco_object;
954         GPUNodeLink *texco_global, *texco_uv = NULL;
955         GPUNodeLink *newnor, *orn;
956         /*char *lastuvname = NULL;*/ /*UNUSED*/
957         float one = 1.0f, norfac, ofs[3];
958         int tex_nr, rgbnor, talpha;
959         int init_done = FALSE, iBumpSpacePrev = 0; /* Not necessary, quiting gcc warning. */
960         GPUNodeLink *vNorg, *vNacc, *fPrevMagnitude;
961         int iFirstTimeNMap=1;
962         int found_deriv_map = 0;
963
964         GPU_link(mat, "set_value", GPU_uniform(&one), &stencil);
965
966         GPU_link(mat, "texco_norm", GPU_builtin(GPU_VIEW_NORMAL), &texco_norm);
967         GPU_link(mat, "texco_orco", GPU_attribute(CD_ORCO, ""), &texco_orco);
968         GPU_link(mat, "texco_object", GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
969                 GPU_builtin(GPU_INVERSE_OBJECT_MATRIX),
970                 GPU_builtin(GPU_VIEW_POSITION), &texco_object);
971         //GPU_link(mat, "texco_tangent", GPU_attribute(CD_TANGENT, ""), &texco_tangent);
972         GPU_link(mat, "texco_global", GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
973                 GPU_builtin(GPU_VIEW_POSITION), &texco_global);
974
975         orn= texco_norm;
976
977         /* go over texture slots */
978         for (tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
979                 /* separate tex switching */
980                 if (ma->septex & (1<<tex_nr)) continue;
981                 
982                 if (ma->mtex[tex_nr]) {
983                         mtex= ma->mtex[tex_nr];
984                         
985                         tex= mtex->tex;
986                         if (tex == NULL) continue;
987
988                         /* which coords */
989                         if (mtex->texco==TEXCO_ORCO)
990                                 texco= texco_orco;
991                         else if (mtex->texco==TEXCO_OBJECT)
992                                 texco= texco_object;
993                         else if (mtex->texco==TEXCO_NORM)
994                                 texco= orn;
995                         else if (mtex->texco==TEXCO_TANGENT)
996                                 texco= texco_object;
997                         else if (mtex->texco==TEXCO_GLOB)
998                                 texco= texco_global;
999                         else if (mtex->texco==TEXCO_REFL) {
1000                                 GPU_link(mat, "texco_refl", shi->vn, shi->view, &shi->ref);
1001                                 texco= shi->ref;
1002                         }
1003                         else if (mtex->texco==TEXCO_UV) {
1004                                 if (1) { //!(texco_uv && strcmp(mtex->uvname, lastuvname) == 0)) {
1005                                         GPU_link(mat, "texco_uv", GPU_attribute(CD_MTFACE, mtex->uvname), &texco_uv);
1006                                         /*lastuvname = mtex->uvname;*/ /*UNUSED*/
1007                                 }
1008                                 texco= texco_uv;
1009                         }
1010                         else
1011                                 continue;
1012
1013                         /* in case of uv, this would just undo a multiplication in texco_uv */
1014                         if (mtex->texco != TEXCO_UV)
1015                                 GPU_link(mat, "mtex_2d_mapping", texco, &texco);
1016
1017                         if (mtex->size[0] != 1.0f || mtex->size[1] != 1.0f || mtex->size[2] != 1.0f)
1018                                 GPU_link(mat, "mtex_mapping_size", texco, GPU_uniform(mtex->size), &texco);
1019
1020                         ofs[0] = mtex->ofs[0] + 0.5f - 0.5f*mtex->size[0];
1021                         ofs[1] = mtex->ofs[1] + 0.5f - 0.5f*mtex->size[1];
1022                         ofs[2] = 0.0f;
1023                         if (ofs[0] != 0.0f || ofs[1] != 0.0f || ofs[2] != 0.0f)
1024                                 GPU_link(mat, "mtex_mapping_ofs", texco, GPU_uniform(ofs), &texco);
1025
1026                         talpha = 0;
1027
1028                         if (tex && tex->type == TEX_IMAGE && tex->ima) {
1029                                 GPU_link(mat, "mtex_image", texco, GPU_image(tex->ima, &tex->iuser), &tin, &trgb);
1030                                 rgbnor= TEX_RGB;
1031
1032                                 if (tex->imaflag & TEX_USEALPHA)
1033                                         talpha= 1;
1034                         }
1035                         else {
1036                                 continue;
1037                         }
1038
1039                         /* texture output */
1040                         if ((rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
1041                                 GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
1042                                 rgbnor -= TEX_RGB;
1043                         }
1044
1045                         if (mtex->texflag & MTEX_NEGATIVE) {
1046                                 if (rgbnor & TEX_RGB)
1047                                         GPU_link(mat, "mtex_rgb_invert", trgb, &trgb);
1048                                 else
1049                                         GPU_link(mat, "mtex_value_invert", tin, &tin);
1050                         }
1051
1052                         if (mtex->texflag & MTEX_STENCIL) {
1053                                 if (rgbnor & TEX_RGB)
1054                                         GPU_link(mat, "mtex_rgb_stencil", stencil, trgb, &stencil, &trgb);
1055                                 else
1056                                         GPU_link(mat, "mtex_value_stencil", stencil, tin, &stencil, &tin);
1057                         }
1058
1059                         /* mapping */
1060                         if (mtex->mapto & (MAP_COL+MAP_COLSPEC)) {
1061                                 /* stencil maps on the texture control slider, not texture intensity value */
1062                                 if ((rgbnor & TEX_RGB)==0) {
1063                                         GPU_link(mat, "set_rgb", GPU_uniform(&mtex->r), &tcol);
1064                                 }
1065                                 else {
1066                                         GPU_link(mat, "set_rgba", trgb, &tcol);
1067
1068                                         if (mtex->mapto & MAP_ALPHA)
1069                                                 GPU_link(mat, "set_value", stencil, &tin);
1070                                         else if (talpha)
1071                                                 GPU_link(mat, "mtex_alpha_from_col", trgb, &tin);
1072                                         else
1073                                                 GPU_link(mat, "set_value_one", &tin);
1074                                 }
1075
1076                                 if (tex->type==TEX_IMAGE)
1077                                         if (gpu_do_color_management(mat))
1078                                                 GPU_link(mat, "srgb_to_linearrgb", tcol, &tcol);
1079                                 
1080                                 if (mtex->mapto & MAP_COL) {
1081                                         GPUNodeLink *colfac;
1082
1083                                         if (mtex->colfac == 1.0f) colfac = stencil;
1084                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->colfac), stencil, &colfac);
1085
1086                                         texture_rgb_blend(mat, tcol, shi->rgb, tin, colfac, mtex->blendtype, &shi->rgb);
1087                                 }
1088                                 
1089                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && (mtex->mapto & MAP_COLSPEC)) {
1090                                         GPUNodeLink *colspecfac;
1091
1092                                         if (mtex->colspecfac == 1.0f) colspecfac = stencil;
1093                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->colspecfac), stencil, &colspecfac);
1094
1095                                         texture_rgb_blend(mat, tcol, shi->specrgb, tin, colspecfac, mtex->blendtype, &shi->specrgb);
1096                                 }
1097                         }
1098
1099                         if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && (mtex->mapto & MAP_NORM)) {
1100                                 if (tex->type==TEX_IMAGE) {
1101                                         found_deriv_map = tex->imaflag & TEX_DERIVATIVEMAP;
1102
1103                                         if (tex->imaflag & TEX_NORMALMAP) {
1104                                                 /* normalmap image */
1105                                                 GPU_link(mat, "mtex_normal", texco, GPU_image(tex->ima, &tex->iuser), &tnor);
1106                                                 
1107                                                 if (mtex->norfac < 0.0f)
1108                                                         GPU_link(mat, "mtex_negate_texnormal", tnor, &tnor);
1109
1110                                                 if (mtex->normapspace == MTEX_NSPACE_TANGENT) {
1111                                                         if (iFirstTimeNMap != 0) {
1112                                                                 // use unnormalized normal (this is how we bake it - closer to gamedev)
1113                                                                 GPUNodeLink *vNegNorm;
1114                                                                 GPU_link(mat, "vec_math_negate", GPU_builtin(GPU_VIEW_NORMAL), &vNegNorm);
1115                                                                 GPU_link(mat, "mtex_nspace_tangent", GPU_attribute(CD_TANGENT, ""), vNegNorm, tnor, &newnor);
1116                                                                 iFirstTimeNMap = 0;
1117                                                         }
1118                                                         else { /* otherwise use accumulated perturbations */
1119                                                                 GPU_link(mat, "mtex_nspace_tangent", GPU_attribute(CD_TANGENT, ""), shi->vn, tnor, &newnor);
1120                                                         }
1121                                                 }
1122                                                 else {
1123                                                         newnor = tnor;
1124                                                 }
1125                                                 
1126                                                 norfac = minf(fabsf(mtex->norfac), 1.0f);
1127                                                 
1128                                                 if (norfac == 1.0f && !GPU_link_changed(stencil)) {
1129                                                         shi->vn = newnor;
1130                                                 }
1131                                                 else {
1132                                                         tnorfac = GPU_uniform(&norfac);
1133         
1134                                                         if (GPU_link_changed(stencil))
1135                                                                 GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
1136         
1137                                                         GPU_link(mat, "mtex_blend_normal", tnorfac, shi->vn, newnor, &shi->vn);
1138                                                 }
1139                                                 
1140                                         }
1141                                         else if ( (mtex->texflag & (MTEX_3TAP_BUMP|MTEX_5TAP_BUMP|MTEX_BICUBIC_BUMP)) || found_deriv_map) {
1142                                                 /* ntap bumpmap image */
1143                                                 int iBumpSpace;
1144                                                 float ima_x, ima_y;
1145                                                 float hScale; 
1146
1147                                                 float imag_tspace_dimension_x = 1024.0f;                // only used for texture space variant
1148                                                 float aspect = 1.0f;
1149                                                 
1150                                                 GPUNodeLink *surf_pos = GPU_builtin(GPU_VIEW_POSITION);
1151                                                 GPUNodeLink *vR1, *vR2;
1152                                                 GPUNodeLink *dBs, *dBt, *fDet;
1153
1154                                                 hScale = 0.1;           // compatibility adjustment factor for all bumpspace types
1155                                                 if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
1156                                                         hScale = 13.0f;         // factor for scaling texspace bumps
1157                                                 else if (found_deriv_map!=0)
1158                                                         hScale = 1.0f;
1159
1160                                                 // resolve texture resolution
1161                                                 if ( (mtex->texflag & MTEX_BUMP_TEXTURESPACE) || found_deriv_map ) {
1162                                                         ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, &tex->iuser);
1163                                                         ima_x= 512.0f; ima_y= 512.f;            // prevent calling textureSize, glsl 1.3 only
1164                                                         if (ibuf) {
1165                                                                 ima_x= ibuf->x;
1166                                                                 ima_y= ibuf->y;
1167                                                                 aspect = ((float) ima_y) / ima_x;
1168                                                         }
1169                                                 }
1170
1171                                                 // The negate on norfac is done because the
1172                                                 // normal in the renderer points inward which corresponds
1173                                                 // to inverting the bump map. Should this ever change
1174                                                 // this negate must be removed.
1175                                                 norfac = -hScale * mtex->norfac;
1176                                                 if (found_deriv_map) {
1177                                                         float fVirtDim = sqrtf(fabsf(ima_x*mtex->size[0]*ima_y*mtex->size[1]));
1178                                                         norfac /= MAX2(fVirtDim, FLT_EPSILON);
1179                                                 }
1180
1181                                                 tnorfac = GPU_uniform(&norfac);
1182
1183                                                 if (found_deriv_map)
1184                                                         GPU_link(mat, "math_multiply", tnorfac, GPU_builtin(GPU_AUTO_BUMPSCALE), &tnorfac);
1185                                                 
1186                                                 if (GPU_link_changed(stencil))
1187                                                         GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
1188                                                 
1189                                                 if ( !init_done ) {
1190                                                         // copy shi->vn to vNorg and vNacc, set magnitude to 1
1191                                                         GPU_link(mat, "mtex_bump_normals_init", shi->vn, &vNorg, &vNacc, &fPrevMagnitude);
1192                                                         iBumpSpacePrev = 0;
1193                                                         init_done = TRUE;
1194                                                 }
1195                                                 
1196                                                 // find current bump space
1197                                                 if ( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
1198                                                         iBumpSpace = 1;
1199                                                 else if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
1200                                                         iBumpSpace = 2;
1201                                                 else
1202                                                         iBumpSpace = 4; // ViewSpace
1203                                                 
1204                                                 // re-initialize if bump space changed
1205                                                 if ( iBumpSpacePrev != iBumpSpace ) {
1206                                                         
1207                                                         if ( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
1208                                                                 GPU_link(mat, "mtex_bump_init_objspace",
1209                                                                          surf_pos, vNorg,
1210                                                                          GPU_builtin(GPU_VIEW_MATRIX), GPU_builtin(GPU_INVERSE_VIEW_MATRIX), GPU_builtin(GPU_OBJECT_MATRIX),  GPU_builtin(GPU_INVERSE_OBJECT_MATRIX),
1211                                                                          fPrevMagnitude, vNacc,
1212                                                                          &fPrevMagnitude, &vNacc,
1213                                                                          &vR1, &vR2, &fDet);
1214                                                                 
1215                                                         else if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
1216                                                                 GPU_link(mat, "mtex_bump_init_texturespace",
1217                                                                          surf_pos, vNorg,
1218                                                                          fPrevMagnitude, vNacc,
1219                                                                          &fPrevMagnitude, &vNacc,
1220                                                                          &vR1, &vR2, &fDet);
1221                                                                 
1222                                                         else
1223                                                                 GPU_link(mat, "mtex_bump_init_viewspace",
1224                                                                          surf_pos, vNorg,
1225                                                                          fPrevMagnitude, vNacc,
1226                                                                          &fPrevMagnitude, &vNacc,
1227                                                                          &vR1, &vR2, &fDet);
1228                                                         
1229                                                         iBumpSpacePrev = iBumpSpace;
1230                                                 }
1231                                                 
1232                                                 
1233                                                 if (found_deriv_map) {
1234                                                         GPU_link(mat, "mtex_bump_deriv",
1235                                                                  texco, GPU_image(tex->ima, &tex->iuser), GPU_uniform(&ima_x), GPU_uniform(&ima_y), tnorfac,
1236                                                                  &dBs, &dBt );
1237                                                 }
1238                                                 else if ( mtex->texflag & MTEX_3TAP_BUMP)
1239                                                         GPU_link(mat, "mtex_bump_tap3",
1240                                                                  texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
1241                                                                  &dBs, &dBt );
1242                                                 else if ( mtex->texflag & MTEX_5TAP_BUMP)
1243                                                         GPU_link(mat, "mtex_bump_tap5",
1244                                                                  texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
1245                                                                  &dBs, &dBt );
1246                                                 else if ( mtex->texflag & MTEX_BICUBIC_BUMP ) {
1247                                                         if (GPU_bicubic_bump_support()) {
1248                                                                 GPU_link(mat, "mtex_bump_bicubic",
1249                                                                          texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
1250                                                                          &dBs, &dBt);
1251                                                         }
1252                                                         else {
1253                                                                 GPU_link(mat, "mtex_bump_tap5",
1254                                                                          texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
1255                                                                          &dBs, &dBt);
1256                                                         }
1257                                                 }
1258                                                 
1259                                                 
1260                                                 if ( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
1261                                                         float imag_tspace_dimension_y = aspect*imag_tspace_dimension_x;
1262                                                         GPU_link(mat, "mtex_bump_apply_texspace",
1263                                                                  fDet, dBs, dBt, vR1, vR2,
1264                                                                  GPU_image(tex->ima, &tex->iuser), texco,
1265                                                                  GPU_uniform(&imag_tspace_dimension_x), GPU_uniform(&imag_tspace_dimension_y), vNacc,
1266                                                                  &vNacc, &shi->vn );
1267                                                 }
1268                                                 else
1269                                                         GPU_link(mat, "mtex_bump_apply",
1270                                                                  fDet, dBs, dBt, vR1, vR2, vNacc,
1271                                                                  &vNacc, &shi->vn );
1272                                                 
1273                                         }
1274                                 }
1275                                 
1276                                 GPU_link(mat, "vec_math_negate", shi->vn, &orn);
1277                         }
1278
1279                         if ((mtex->mapto & MAP_VARS)) {
1280                                 if (rgbnor & TEX_RGB) {
1281                                         if (talpha)
1282                                                 GPU_link(mat, "mtex_alpha_from_col", trgb, &tin);
1283                                         else
1284                                                 GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
1285                                 }
1286
1287                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_REF) {
1288                                         GPUNodeLink *difffac;
1289
1290                                         if (mtex->difffac == 1.0f) difffac = stencil;
1291                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->difffac), stencil, &difffac);
1292
1293                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->refl, tin, difffac, mtex->blendtype, &shi->refl);
1294                                         GPU_link(mat, "mtex_value_clamp_positive", shi->refl, &shi->refl);
1295                                 }
1296                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_SPEC) {
1297                                         GPUNodeLink *specfac;
1298
1299                                         if (mtex->specfac == 1.0f) specfac = stencil;
1300                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->specfac), stencil, &specfac);
1301
1302                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->spec, tin, specfac, mtex->blendtype, &shi->spec);
1303                                         GPU_link(mat, "mtex_value_clamp_positive", shi->spec, &shi->spec);
1304                                 }
1305                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_EMIT) {
1306                                         GPUNodeLink *emitfac;
1307
1308                                         if (mtex->emitfac == 1.0f) emitfac = stencil;
1309                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->emitfac), stencil, &emitfac);
1310
1311                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->emit, tin, emitfac, mtex->blendtype, &shi->emit);
1312                                         GPU_link(mat, "mtex_value_clamp_positive", shi->emit, &shi->emit);
1313                                 }
1314                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_HAR) {
1315                                         GPUNodeLink *hardfac;
1316
1317                                         if (mtex->hardfac == 1.0f) hardfac = stencil;
1318                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->hardfac), stencil, &hardfac);
1319
1320                                         GPU_link(mat, "mtex_har_divide", shi->har, &shi->har);
1321                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->har, tin, hardfac, mtex->blendtype, &shi->har);
1322                                         GPU_link(mat, "mtex_har_multiply_clamp", shi->har, &shi->har);
1323                                 }
1324                                 if (mtex->mapto & MAP_ALPHA) {
1325                                         GPUNodeLink *alphafac;
1326
1327                                         if (mtex->alphafac == 1.0f) alphafac = stencil;
1328                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->alphafac), stencil, &alphafac);
1329
1330                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->alpha, tin, alphafac, mtex->blendtype, &shi->alpha);
1331                                         GPU_link(mat, "mtex_value_clamp", shi->alpha, &shi->alpha);
1332                                 }
1333                                 if (!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_AMB) {
1334                                         GPUNodeLink *ambfac;
1335
1336                                         if (mtex->ambfac == 1.0f) ambfac = stencil;
1337                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->ambfac), stencil, &ambfac);
1338
1339                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->amb, tin, ambfac, mtex->blendtype, &shi->amb);
1340                                         GPU_link(mat, "mtex_value_clamp", shi->amb, &shi->amb);
1341                                 }
1342                         }
1343                 }
1344         }
1345 }
1346
1347 void GPU_shadeinput_set(GPUMaterial *mat, Material *ma, GPUShadeInput *shi)
1348 {
1349         float hard = ma->har;
1350
1351         memset(shi, 0, sizeof(*shi));
1352
1353         shi->gpumat = mat;
1354         shi->mat = ma;
1355
1356         GPU_link(mat, "set_rgb", GPU_uniform(&ma->r), &shi->rgb);
1357         GPU_link(mat, "set_rgb", GPU_uniform(&ma->specr), &shi->specrgb);
1358         GPU_link(mat, "shade_norm", GPU_builtin(GPU_VIEW_NORMAL), &shi->vn);
1359         GPU_link(mat, "set_value", GPU_uniform(&ma->alpha), &shi->alpha);
1360         GPU_link(mat, "set_value", GPU_uniform(&ma->ref), &shi->refl);
1361         GPU_link(mat, "set_value", GPU_uniform(&ma->spec), &shi->spec);
1362         GPU_link(mat, "set_value", GPU_uniform(&ma->emit), &shi->emit);
1363         GPU_link(mat, "set_value", GPU_uniform(&hard), &shi->har);
1364         GPU_link(mat, "set_value", GPU_uniform(&ma->amb), &shi->amb);
1365         GPU_link(mat, "shade_view", GPU_builtin(GPU_VIEW_POSITION), &shi->view);
1366         GPU_link(mat, "vcol_attribute", GPU_attribute(CD_MCOL, ""), &shi->vcol);
1367         if (gpu_do_color_management(mat))
1368                 GPU_link(mat, "srgb_to_linearrgb", shi->vcol, &shi->vcol);
1369         GPU_link(mat, "texco_refl", shi->vn, shi->view, &shi->ref);
1370 }
1371
1372 void GPU_shaderesult_set(GPUShadeInput *shi, GPUShadeResult *shr)
1373 {
1374         GPUMaterial *mat= shi->gpumat;
1375         GPUNodeLink *emit, *ulinfac, *ulogfac, *mistfac;
1376         Material *ma= shi->mat;
1377         World *world= mat->scene->world;
1378         float linfac, logfac, misttype;
1379
1380         memset(shr, 0, sizeof(*shr));
1381
1382         if (ma->mode & MA_VERTEXCOLP)
1383                 shi->rgb = shi->vcol;
1384
1385         do_material_tex(shi);
1386
1387         if ((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
1388                 GPU_material_enable_alpha(mat);
1389
1390         if ((mat->scene->gm.flag & GAME_GLSL_NO_LIGHTS) || (ma->mode & MA_SHLESS)) {
1391                 shr->combined = shi->rgb;
1392                 shr->alpha = shi->alpha;
1393                 GPU_link(mat, "set_rgb", shi->rgb, &shr->diff);
1394                 GPU_link(mat, "set_rgb_zero", &shr->spec);
1395         }
1396         else {
1397                 if (GPU_link_changed(shi->emit) || ma->emit != 0.0f) {
1398                         if ((ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL) {
1399                                 GPU_link(mat, "shade_add", shi->emit, shi->vcol, &emit);
1400                                 GPU_link(mat, "shade_mul", emit, shi->rgb, &shr->diff);
1401                         }
1402                         else
1403                                 GPU_link(mat, "shade_mul_value", shi->emit, shi->rgb, &shr->diff);
1404                 }
1405                 else
1406                         GPU_link(mat, "set_rgb_zero", &shr->diff);
1407
1408                 GPU_link(mat, "set_rgb_zero", &shr->spec);
1409
1410                 material_lights(shi, shr);
1411
1412                 shr->combined = shr->diff;
1413                 shr->alpha = shi->alpha;
1414
1415                 if (world) {
1416                         /* exposure correction */
1417                         if (world->exp!=0.0f || world->range!=1.0f) {
1418                                 linfac= 1.0f + powf((2.0f*world->exp + 0.5f), -10);
1419                                 logfac= logf((linfac-1.0f)/linfac)/world->range;
1420
1421                                 GPU_link(mat, "set_value", GPU_uniform(&linfac), &ulinfac);
1422                                 GPU_link(mat, "set_value", GPU_uniform(&logfac), &ulogfac);
1423
1424                                 GPU_link(mat, "shade_exposure_correct", shr->combined,
1425                                         ulinfac, ulogfac, &shr->combined);
1426                                 GPU_link(mat, "shade_exposure_correct", shr->spec,
1427                                         ulinfac, ulogfac, &shr->spec);
1428                         }
1429
1430                         /* ambient color */
1431                         if (world->ambr!=0.0f || world->ambg!=0.0f || world->ambb!=0.0f) {
1432                                 if (GPU_link_changed(shi->amb) || ma->amb != 0.0f)
1433                                         GPU_link(mat, "shade_maddf", shr->combined, GPU_uniform(&ma->amb),
1434                                                 GPU_uniform(&world->ambr), &shr->combined);
1435                         }
1436                 }
1437
1438                 if (ma->mode & MA_RAMP_COL) ramp_diffuse_result(shi, &shr->combined);
1439                 if (ma->mode & MA_RAMP_SPEC) ramp_spec_result(shi, &shr->spec);
1440
1441                 if (GPU_link_changed(shi->spec) || ma->spec != 0.0f)
1442                         GPU_link(mat, "shade_add", shr->combined, shr->spec, &shr->combined);
1443         }
1444
1445         GPU_link(mat, "mtex_alpha_to_col", shr->combined, shr->alpha, &shr->combined);
1446
1447         if (ma->shade_flag & MA_OBCOLOR)
1448                 GPU_link(mat, "shade_obcolor", shr->combined, GPU_builtin(GPU_OBCOLOR), &shr->combined);
1449
1450         if (world && (world->mode & WO_MIST) && !(ma->mode & MA_NOMIST)) {
1451                 misttype = world->mistype;
1452
1453                 GPU_link(mat, "shade_mist_factor", GPU_builtin(GPU_VIEW_POSITION),
1454                         GPU_uniform(&world->miststa), GPU_uniform(&world->mistdist),
1455                         GPU_uniform(&misttype), GPU_uniform(&world->misi), &mistfac);
1456
1457                 GPU_link(mat, "mix_blend", mistfac, shr->combined,
1458                         GPU_uniform(&world->horr), &shr->combined);
1459         }
1460
1461         if (!((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))) {
1462                 if (world && (GPU_link_changed(shr->alpha) || ma->alpha != 1.0f))
1463                         GPU_link(mat, "shade_world_mix", GPU_uniform(&world->horr),
1464                                 shr->combined, &shr->combined);
1465
1466                 GPU_link(mat, "shade_alpha_opaque", shr->combined, &shr->combined);
1467         }
1468
1469         if (ma->shade_flag & MA_OBCOLOR) {
1470                 mat->obcolalpha = 1;
1471                 GPU_link(mat, "shade_alpha_obcolor", shr->combined, GPU_builtin(GPU_OBCOLOR), &shr->combined);
1472         }
1473 }
1474
1475 static GPUNodeLink *GPU_blender_material(GPUMaterial *mat, Material *ma)
1476 {
1477         GPUShadeInput shi;
1478         GPUShadeResult shr;
1479
1480         GPU_shadeinput_set(mat, ma, &shi);
1481         GPU_shaderesult_set(&shi, &shr);
1482
1483         return shr.combined;
1484 }
1485
1486 GPUMaterial *GPU_material_from_blender(Scene *scene, Material *ma)
1487 {
1488         GPUMaterial *mat;
1489         GPUNodeLink *outlink;
1490         LinkData *link;
1491
1492         for (link=ma->gpumaterial.first; link; link=link->next)
1493                 if (((GPUMaterial*)link->data)->scene == scene)
1494                         return link->data;
1495
1496         /* allocate material */
1497         mat = GPU_material_construct_begin(ma);
1498         mat->scene = scene;
1499
1500         if (!(scene->gm.flag & GAME_GLSL_NO_NODES) && ma->nodetree && ma->use_nodes) {
1501                 /* create nodes */
1502                 ntreeGPUMaterialNodes(ma->nodetree, mat);
1503         }
1504         else {
1505                 /* create material */
1506                 outlink = GPU_blender_material(mat, ma);
1507                 GPU_material_output_link(mat, outlink);
1508         }
1509
1510         if (!BKE_scene_use_new_shading_nodes(scene)) {
1511                 if (gpu_do_color_management(mat))
1512                         if (mat->outlink)
1513                                 GPU_link(mat, "linearrgb_to_srgb", mat->outlink, &mat->outlink);
1514         }
1515
1516
1517         GPU_material_construct_end(mat);
1518
1519         /* note that even if building the shader fails in some way, we still keep
1520          * it to avoid trying to compile again and again, and simple do not use
1521          * the actual shader on drawing */
1522
1523         link = MEM_callocN(sizeof(LinkData), "GPUMaterialLink");
1524         link->data = mat;
1525         BLI_addtail(&ma->gpumaterial, link);
1526
1527         return mat;
1528 }
1529
1530 void GPU_materials_free(void)
1531 {
1532         Object *ob;
1533         Material *ma;
1534         extern Material defmaterial;
1535
1536         for (ma=G.main->mat.first; ma; ma=ma->id.next)
1537                 GPU_material_free(ma);
1538
1539         GPU_material_free(&defmaterial);
1540
1541         for (ob=G.main->object.first; ob; ob=ob->id.next)
1542                 GPU_lamp_free(ob);
1543 }
1544
1545 /* Lamps and shadow buffers */
1546
1547 void GPU_lamp_update(GPULamp *lamp, int lay, int hide, float obmat[][4])
1548 {
1549         float mat[4][4];
1550
1551         lamp->lay = lay;
1552         lamp->hide = hide;
1553
1554         copy_m4_m4(mat, obmat);
1555         normalize_m4(mat);
1556
1557         copy_v3_v3(lamp->vec, mat[2]);
1558         copy_v3_v3(lamp->co, mat[3]);
1559         copy_m4_m4(lamp->obmat, mat);
1560         invert_m4_m4(lamp->imat, mat);
1561 }
1562
1563 void GPU_lamp_update_colors(GPULamp *lamp, float r, float g, float b, float energy)
1564 {
1565         lamp->energy = energy;
1566         if (lamp->mode & LA_NEG) lamp->energy= -lamp->energy;
1567
1568         lamp->col[0]= r* lamp->energy;
1569         lamp->col[1]= g* lamp->energy;
1570         lamp->col[2]= b* lamp->energy;
1571 }
1572
1573 static void gpu_lamp_from_blender(Scene *scene, Object *ob, Object *par, Lamp *la, GPULamp *lamp)
1574 {
1575         float temp, angle, pixsize, wsize;
1576
1577         lamp->scene = scene;
1578         lamp->ob = ob;
1579         lamp->par = par;
1580         lamp->la = la;
1581
1582         /* add_render_lamp */
1583         lamp->mode = la->mode;
1584         lamp->type = la->type;
1585
1586         lamp->energy = la->energy;
1587         if (lamp->mode & LA_NEG) lamp->energy= -lamp->energy;
1588
1589         lamp->col[0]= la->r*lamp->energy;
1590         lamp->col[1]= la->g*lamp->energy;
1591         lamp->col[2]= la->b*lamp->energy;
1592
1593         GPU_lamp_update(lamp, ob->lay, (ob->restrictflag & OB_RESTRICT_RENDER), ob->obmat);
1594
1595         lamp->spotsi= la->spotsize;
1596         if (lamp->mode & LA_HALO)
1597                 if (lamp->spotsi > 170.0f)
1598                         lamp->spotsi = 170.0f;
1599         lamp->spotsi= cosf((float)M_PI*lamp->spotsi/360.0f);
1600         lamp->spotbl= (1.0f - lamp->spotsi)*la->spotblend;
1601         lamp->k= la->k;
1602
1603         lamp->dist= la->dist;
1604         lamp->falloff_type= la->falloff_type;
1605         lamp->att1= la->att1;
1606         lamp->att2= la->att2;
1607         lamp->curfalloff= la->curfalloff;
1608
1609         /* initshadowbuf */
1610         lamp->bias = 0.02f*la->bias;
1611         lamp->size = la->bufsize;
1612         lamp->d= la->clipsta;
1613         lamp->clipend= la->clipend;
1614
1615         /* arbitrary correction for the fact we do no soft transition */
1616         lamp->bias *= 0.25f;
1617
1618         /* makeshadowbuf */
1619         if (lamp->type == LA_SUN) {
1620                 wsize = la->shadow_frustum_size;
1621                 orthographic_m4(lamp->winmat, -wsize, wsize, -wsize, wsize, lamp->d, lamp->clipend);
1622         }
1623         else {
1624                 angle= saacos(lamp->spotsi);
1625                 temp= 0.5f*lamp->size*cosf(angle)/sinf(angle);
1626                 pixsize= (lamp->d)/temp;
1627                 wsize= pixsize*0.5f*lamp->size;
1628                 perspective_m4(lamp->winmat, -wsize, wsize, -wsize, wsize, lamp->d, lamp->clipend);
1629         }
1630 }
1631
1632 static void gpu_lamp_shadow_free(GPULamp *lamp)
1633 {
1634         if (lamp->tex) {
1635                 GPU_texture_free(lamp->tex);
1636                 lamp->tex= NULL;
1637         }
1638         if (lamp->depthtex) {
1639                 GPU_texture_free(lamp->depthtex);
1640                 lamp->depthtex= NULL;
1641         }
1642         if (lamp->fb) {
1643                 GPU_framebuffer_free(lamp->fb);
1644                 lamp->fb= NULL;
1645         }
1646         if (lamp->blurtex) {
1647                 GPU_texture_free(lamp->blurtex);
1648                 lamp->blurtex= NULL;
1649         }
1650         if (lamp->blurfb) {
1651                 GPU_framebuffer_free(lamp->blurfb);
1652                 lamp->blurfb= NULL;
1653         }
1654 }
1655
1656 GPULamp *GPU_lamp_from_blender(Scene *scene, Object *ob, Object *par)
1657 {
1658         Lamp *la;
1659         GPULamp *lamp;
1660         LinkData *link;
1661
1662         for (link=ob->gpulamp.first; link; link=link->next) {
1663                 lamp = (GPULamp*)link->data;
1664
1665                 if (lamp->par == par && lamp->scene == scene)
1666                         return link->data;
1667         }
1668
1669         lamp = MEM_callocN(sizeof(GPULamp), "GPULamp");
1670
1671         link = MEM_callocN(sizeof(LinkData), "GPULampLink");
1672         link->data = lamp;
1673         BLI_addtail(&ob->gpulamp, link);
1674
1675         la = ob->data;
1676         gpu_lamp_from_blender(scene, ob, par, la, lamp);
1677
1678         if ((la->type==LA_SPOT && (la->mode & LA_SHAD_BUF)) || (la->type==LA_SUN && (la->mode & LA_SHAD_RAY))) {
1679                 /* opengl */
1680                 lamp->fb = GPU_framebuffer_create();
1681                 if (!lamp->fb) {
1682                         gpu_lamp_shadow_free(lamp);
1683                         return lamp;
1684                 }
1685
1686                 if (lamp->la->shadowmap_type == LA_SHADMAP_VARIANCE) {
1687                         /* Shadow depth map */
1688                         lamp->depthtex = GPU_texture_create_depth(lamp->size, lamp->size, NULL);
1689                         if (!lamp->depthtex) {
1690                                 gpu_lamp_shadow_free(lamp);
1691                                 return lamp;
1692                         }
1693                 
1694                         if (!GPU_framebuffer_texture_attach(lamp->fb, lamp->depthtex, NULL)) {
1695                                 gpu_lamp_shadow_free(lamp);
1696                                 return lamp;
1697                         }
1698
1699                         /* Shadow color map */
1700                         lamp->tex = GPU_texture_create_vsm_shadow_map(lamp->size, NULL);
1701                         if (!lamp->tex) {
1702                                 gpu_lamp_shadow_free(lamp);
1703                                 return lamp;
1704                         }
1705
1706                         if (!GPU_framebuffer_texture_attach(lamp->fb, lamp->tex, NULL)) {
1707                                 gpu_lamp_shadow_free(lamp);
1708                                 return lamp;
1709                         }
1710
1711                         /* FBO and texture for blurring */
1712                         lamp->blurfb = GPU_framebuffer_create();
1713                         if (!lamp->blurfb) {
1714                                 gpu_lamp_shadow_free(lamp);
1715                                 return lamp;
1716                         }
1717
1718                         lamp->blurtex = GPU_texture_create_vsm_shadow_map(lamp->size*0.5, NULL);
1719                         if (!lamp->blurtex) {
1720                                 gpu_lamp_shadow_free(lamp);
1721                                 return lamp;
1722                         }
1723                 
1724                         if (!GPU_framebuffer_texture_attach(lamp->blurfb, lamp->blurtex, NULL)) {
1725                                 gpu_lamp_shadow_free(lamp);
1726                                 return lamp;
1727                         }
1728                 }
1729                 else {
1730                         lamp->tex = GPU_texture_create_depth(lamp->size, lamp->size, NULL);
1731                         if (!lamp->tex) {
1732                                 gpu_lamp_shadow_free(lamp);
1733                                 return lamp;
1734                         }
1735
1736                         if (!GPU_framebuffer_texture_attach(lamp->fb, lamp->tex, NULL)) {
1737                                 gpu_lamp_shadow_free(lamp);
1738                                 return lamp;
1739                         }
1740                 }
1741
1742                 GPU_framebuffer_restore();
1743
1744                 lamp->shadow_color[0] = la->shdwr;
1745                 lamp->shadow_color[1] = la->shdwg;
1746                 lamp->shadow_color[2] = la->shdwb;
1747         }
1748         else {
1749                 lamp->shadow_color[0] = 1.0;
1750                 lamp->shadow_color[1] = 1.0;
1751                 lamp->shadow_color[2] = 1.0;
1752         }
1753
1754         return lamp;
1755 }
1756
1757 void GPU_lamp_free(Object *ob)
1758 {
1759         GPULamp *lamp;
1760         LinkData *link;
1761         LinkData *nlink;
1762         Material *ma;
1763
1764         for (link=ob->gpulamp.first; link; link=link->next) {
1765                 lamp = link->data;
1766
1767                 while (lamp->materials.first) {
1768                         nlink = lamp->materials.first;
1769                         ma = nlink->data;
1770                         BLI_freelinkN(&lamp->materials, nlink);
1771
1772                         if (ma->gpumaterial.first)
1773                                 GPU_material_free(ma);
1774                 }
1775
1776                 gpu_lamp_shadow_free(lamp);
1777
1778                 MEM_freeN(lamp);
1779         }
1780
1781         BLI_freelistN(&ob->gpulamp);
1782 }
1783
1784 int GPU_lamp_has_shadow_buffer(GPULamp *lamp)
1785 {
1786         return (!(lamp->scene->gm.flag & GAME_GLSL_NO_SHADOWS) &&
1787                         !(lamp->scene->gm.flag & GAME_GLSL_NO_LIGHTS) &&
1788                         lamp->tex && lamp->fb);
1789 }
1790
1791 void GPU_lamp_update_buffer_mats(GPULamp *lamp)
1792 {
1793         float rangemat[4][4], persmat[4][4];
1794
1795         /* initshadowbuf */
1796         invert_m4_m4(lamp->viewmat, lamp->obmat);
1797         normalize_v3(lamp->viewmat[0]);
1798         normalize_v3(lamp->viewmat[1]);
1799         normalize_v3(lamp->viewmat[2]);
1800
1801         /* makeshadowbuf */
1802         mult_m4_m4m4(persmat, lamp->winmat, lamp->viewmat);
1803
1804         /* opengl depth buffer is range 0.0..1.0 instead of -1.0..1.0 in blender */
1805         unit_m4(rangemat);
1806         rangemat[0][0] = 0.5f;
1807         rangemat[1][1] = 0.5f;
1808         rangemat[2][2] = 0.5f;
1809         rangemat[3][0] = 0.5f;
1810         rangemat[3][1] = 0.5f;
1811         rangemat[3][2] = 0.5f;
1812
1813         mult_m4_m4m4(lamp->persmat, rangemat, persmat);
1814 }
1815
1816 void GPU_lamp_shadow_buffer_bind(GPULamp *lamp, float viewmat[][4], int *winsize, float winmat[][4])
1817 {
1818         GPU_lamp_update_buffer_mats(lamp);
1819
1820         /* opengl */
1821         glDisable(GL_SCISSOR_TEST);
1822         GPU_framebuffer_texture_bind(lamp->fb, lamp->tex,
1823                 GPU_texture_opengl_width(lamp->tex), GPU_texture_opengl_height(lamp->tex));
1824         if (lamp->la->shadowmap_type == LA_SHADMAP_VARIANCE)
1825                 GPU_shader_bind(GPU_shader_get_builtin_shader(GPU_SHADER_VSM_STORE));
1826
1827         /* set matrices */
1828         copy_m4_m4(viewmat, lamp->viewmat);
1829         copy_m4_m4(winmat, lamp->winmat);
1830         *winsize = lamp->size;
1831 }
1832
1833 void GPU_lamp_shadow_buffer_unbind(GPULamp *lamp)
1834 {
1835         if (lamp->la->shadowmap_type == LA_SHADMAP_VARIANCE) {
1836                 GPU_shader_unbind(GPU_shader_get_builtin_shader(GPU_SHADER_VSM_STORE));
1837                 GPU_framebuffer_blur(lamp->fb, lamp->tex, lamp->blurfb, lamp->blurtex);
1838         }
1839
1840         GPU_framebuffer_texture_unbind(lamp->fb, lamp->tex);
1841         GPU_framebuffer_restore();
1842         glEnable(GL_SCISSOR_TEST);
1843 }
1844
1845 int GPU_lamp_shadow_layer(GPULamp *lamp)
1846 {
1847         if (lamp->fb && lamp->tex && (lamp->mode & (LA_LAYER|LA_LAYER_SHADOW)))
1848                 return lamp->lay;
1849         else
1850                 return -1;
1851 }
1852
1853 /* export the GLSL shader */
1854
1855 GPUShaderExport *GPU_shader_export(struct Scene *scene, struct Material *ma)
1856 {
1857         static struct {
1858                 GPUBuiltin gputype;
1859                 GPUDynamicType dynamictype;
1860                 GPUDataType datatype;
1861         } builtins[] = { 
1862                 { GPU_VIEW_MATRIX, GPU_DYNAMIC_OBJECT_VIEWMAT, GPU_DATA_16F },
1863                 { GPU_INVERSE_VIEW_MATRIX, GPU_DYNAMIC_OBJECT_VIEWIMAT, GPU_DATA_16F },
1864                 { GPU_OBJECT_MATRIX, GPU_DYNAMIC_OBJECT_MAT, GPU_DATA_16F },
1865                 { GPU_INVERSE_OBJECT_MATRIX, GPU_DYNAMIC_OBJECT_IMAT, GPU_DATA_16F },
1866                 { GPU_OBCOLOR, GPU_DYNAMIC_OBJECT_COLOR, GPU_DATA_4F },
1867                 { GPU_AUTO_BUMPSCALE, GPU_DYNAMIC_OBJECT_AUTOBUMPSCALE, GPU_DATA_1F },
1868                 { 0 }
1869         };
1870
1871         GPUShaderExport *shader = NULL;
1872         GPUPass *pass;
1873         GPUInput *input;
1874         GPUMaterial *mat;
1875         GPUInputUniform *uniform;
1876         GPUInputAttribute *attribute;
1877         GLint lastbindcode;
1878         int i, liblen, fraglen;
1879
1880         if (!GPU_glsl_support())
1881                 return NULL;
1882
1883         mat = GPU_material_from_blender(scene, ma);
1884         pass = (mat)? mat->pass: NULL;
1885
1886         if (pass && pass->fragmentcode && pass->vertexcode) {
1887                 shader = MEM_callocN(sizeof(GPUShaderExport), "GPUShaderExport");
1888
1889                 for (input = pass->inputs.first; input; input = input->next) {
1890                         uniform = MEM_callocN(sizeof(GPUInputUniform), "GPUInputUniform");
1891
1892                         if (input->ima) {
1893                                 /* image sampler uniform */
1894                                 uniform->type = GPU_DYNAMIC_SAMPLER_2DIMAGE;
1895                                 uniform->datatype = GPU_DATA_1I;
1896                                 uniform->image = input->ima;
1897                                 uniform->texnumber = input->texid;
1898                                 BLI_strncpy(uniform->varname, input->shadername, sizeof(uniform->varname));
1899                         }
1900                         else if (input->tex) {
1901                                 /* generated buffer */
1902                                 uniform->texnumber = input->texid;
1903                                 uniform->datatype = GPU_DATA_1I;
1904                                 BLI_strncpy(uniform->varname, input->shadername, sizeof(uniform->varname));
1905
1906                                 switch (input->textype) {
1907                                 case GPU_SHADOW2D:
1908                                         uniform->type = GPU_DYNAMIC_SAMPLER_2DSHADOW;
1909                                         uniform->lamp = input->dynamicdata;
1910                                         break;
1911                                 case GPU_TEX2D:
1912                                         if (GPU_texture_opengl_bindcode(input->tex)) {
1913                                                 uniform->type = GPU_DYNAMIC_SAMPLER_2DBUFFER;
1914                                                 glGetIntegerv(GL_TEXTURE_BINDING_2D, &lastbindcode);
1915                                                 glBindTexture(GL_TEXTURE_2D, GPU_texture_opengl_bindcode(input->tex));
1916                                                 uniform->texsize = GPU_texture_opengl_width(input->tex) * GPU_texture_opengl_height(input->tex);
1917                                                 uniform->texpixels = MEM_mallocN(uniform->texsize*4, "RGBApixels");
1918                                                 glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, uniform->texpixels); 
1919                                                 glBindTexture(GL_TEXTURE_2D, lastbindcode);
1920                                         }
1921                                         break;
1922                                 }
1923                         }
1924                         else {
1925                                 uniform->type = input->dynamictype;
1926                                 BLI_strncpy(uniform->varname, input->shadername, sizeof(uniform->varname));
1927                                 switch (input->type) {
1928                                 case 1:
1929                                         uniform->datatype = GPU_DATA_1F;
1930                                         break;
1931                                 case 2:
1932                                         uniform->datatype = GPU_DATA_2F;
1933                                         break;
1934                                 case 3:
1935                                         uniform->datatype = GPU_DATA_3F;
1936                                         break;
1937                                 case 4:
1938                                         uniform->datatype = GPU_DATA_4F;
1939                                         break;
1940                                 case 9:
1941                                         uniform->datatype = GPU_DATA_9F;
1942                                         break;
1943                                 case 16:
1944                                         uniform->datatype = GPU_DATA_16F;
1945                                         break;
1946                                 }
1947
1948                                 if (uniform->type >= GPU_DYNAMIC_LAMP_FIRST && uniform->type <= GPU_DYNAMIC_LAMP_LAST)
1949                                         uniform->lamp = input->dynamicdata;
1950                         }
1951
1952                         if (uniform->type != GPU_DYNAMIC_NONE)
1953                                 BLI_addtail(&shader->uniforms, uniform);
1954                         else
1955                                 MEM_freeN(uniform);
1956                 }
1957
1958                 /* process builtin uniform */
1959                 for (i=0; builtins[i].gputype; i++) {
1960                         if (mat->builtins & builtins[i].gputype) {
1961                                 uniform = MEM_callocN(sizeof(GPUInputUniform), "GPUInputUniform");
1962                                 uniform->type = builtins[i].dynamictype;
1963                                 uniform->datatype = builtins[i].datatype;
1964                                 BLI_strncpy(uniform->varname, GPU_builtin_name(builtins[i].gputype), sizeof(uniform->varname));
1965                                 BLI_addtail(&shader->uniforms, uniform);
1966                         }
1967                 }
1968
1969                 /* now link fragment shader with library shader */
1970                 /* TBD: remove the function that are not used in the main function */
1971                 liblen = (pass->libcode) ? strlen(pass->libcode) : 0;
1972                 fraglen = strlen(pass->fragmentcode);
1973                 shader->fragment = (char *)MEM_mallocN(liblen+fraglen+1, "GPUFragShader");
1974                 if (pass->libcode)
1975                         memcpy(shader->fragment, pass->libcode, liblen);
1976                 memcpy(&shader->fragment[liblen], pass->fragmentcode, fraglen);
1977                 shader->fragment[liblen+fraglen] = 0;
1978
1979                 // export the attribute
1980                 for (i=0; i<mat->attribs.totlayer; i++) {
1981                         attribute = MEM_callocN(sizeof(GPUInputAttribute), "GPUInputAttribute");
1982                         attribute->type = mat->attribs.layer[i].type;
1983                         attribute->number = mat->attribs.layer[i].glindex;
1984                         BLI_snprintf(attribute->varname, sizeof(attribute->varname), "att%d", mat->attribs.layer[i].attribid);
1985
1986                         switch (attribute->type) {
1987                         case CD_TANGENT:
1988                                 attribute->datatype = GPU_DATA_4F;
1989                                 break;
1990                         case CD_MTFACE:
1991                                 attribute->datatype = GPU_DATA_2F;
1992                                 attribute->name = mat->attribs.layer[i].name;
1993                                 break;
1994                         case CD_MCOL:
1995                                 attribute->datatype = GPU_DATA_4UB;
1996                                 attribute->name = mat->attribs.layer[i].name;
1997                                 break;
1998                         case CD_ORCO:
1999                                 attribute->datatype = GPU_DATA_3F;
2000                                 break;
2001                         }
2002
2003                         if (attribute->datatype != GPU_DATA_NONE)
2004                                 BLI_addtail(&shader->attributes, attribute);
2005                         else
2006                                 MEM_freeN(attribute);
2007                 }
2008
2009                 // export the vertex shader
2010                 shader->vertex = BLI_strdup(pass->vertexcode);
2011         }
2012
2013         return shader;
2014 }
2015
2016 void GPU_free_shader_export(GPUShaderExport *shader)
2017 {
2018         GPUInputUniform *uniform;
2019
2020         if (shader == NULL)
2021                 return;
2022
2023         for (uniform = shader->uniforms.first; uniform; uniform=uniform->next)
2024                 if (uniform->texpixels)
2025                         MEM_freeN(uniform->texpixels);
2026
2027         BLI_freelistN(&shader->uniforms);
2028         BLI_freelistN(&shader->attributes);
2029
2030         if (shader->vertex)
2031                 MEM_freeN(shader->vertex);
2032         if (shader->fragment)
2033                 MEM_freeN(shader->fragment);
2034
2035         MEM_freeN(shader);
2036 }
2037