merge from trunk #37722
[blender-staging.git] / source / blender / gpu / intern / gpu_material.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2006 Blender Foundation.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Brecht Van Lommel.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /** \file blender/gpu/intern/gpu_material.c
31  *  \ingroup gpu
32  */
33
34
35 #include <math.h>
36 #include <string.h>
37
38 #include "GL/glew.h"
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_lamp_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_world_types.h"
47
48 #include "BLI_math.h"
49 #include "BLI_blenlib.h"
50 #include "BLI_utildefines.h"
51
52 #include "BKE_anim.h"
53 #include "BKE_colortools.h"
54 #include "BKE_DerivedMesh.h"
55 #include "BKE_global.h"
56 #include "BKE_image.h"
57 #include "BKE_main.h"
58 #include "BKE_node.h"
59 #include "BKE_scene.h"
60 #include "BKE_texture.h"
61
62 #include "IMB_imbuf_types.h"
63
64 #include "GPU_extensions.h"
65 #include "GPU_material.h"
66
67 #include "gpu_codegen.h"
68
69 #include <string.h>
70
71 /* Structs */
72
73 typedef enum DynMatProperty {
74         DYN_LAMP_CO = 1,
75         DYN_LAMP_VEC = 2,
76         DYN_LAMP_IMAT = 4,
77         DYN_LAMP_PERSMAT = 8,
78 } DynMatProperty;
79
80 struct GPUMaterial {
81         Scene *scene;
82         Material *ma;
83
84         /* for creating the material */
85         ListBase nodes;
86         GPUNodeLink *outlink;
87
88         /* for binding the material */
89         GPUPass *pass;
90         GPUVertexAttribs attribs;
91         int bound;
92         int builtins;
93         int alpha, obcolalpha;
94         int dynproperty;
95
96         /* for passing uniforms */
97         int viewmatloc, invviewmatloc;
98         int obmatloc, invobmatloc;
99         int obcolloc;
100
101         ListBase lamps;
102 };
103
104 struct GPULamp {
105         Scene *scene;
106         Object *ob;
107         Object *par;
108         Lamp *la;
109
110         int type, mode, lay, hide;
111
112         float dynenergy, dyncol[3];
113         float energy, col[3];
114
115         float co[3], vec[3];
116         float dynco[3], dynvec[3];
117         float obmat[4][4];
118         float imat[4][4];
119         float dynimat[4][4];
120
121         float spotsi, spotbl, k;
122         float dist, att1, att2;
123
124         float bias, d, clipend;
125         int size;
126
127         int falloff_type;
128         struct CurveMapping *curfalloff;
129
130         float winmat[4][4];
131         float viewmat[4][4];
132         float persmat[4][4];
133         float dynpersmat[4][4];
134
135         GPUFrameBuffer *fb;
136         GPUTexture *tex;
137
138         ListBase materials;
139 };
140
141 /* Functions */
142
143 static GPUMaterial *GPU_material_construct_begin(Material *ma)
144 {
145         GPUMaterial *material = MEM_callocN(sizeof(GPUMaterial), "GPUMaterial");
146
147         material->ma= ma;
148
149         return material;
150 }
151
152 static void gpu_material_set_attrib_id(GPUMaterial *material)
153 {
154         GPUVertexAttribs *attribs;
155         GPUShader *shader;
156         GPUPass *pass;
157         char name[32];
158         int a, b;
159
160         attribs= &material->attribs;
161         pass= material->pass;
162         if(!pass) {
163                 attribs->totlayer = 0;
164                 return;
165         }
166         
167         shader= GPU_pass_shader(pass);
168         if(!shader) {
169                 attribs->totlayer = 0;
170                 return;
171         }
172
173         /* convert from attribute number to the actual id assigned by opengl,
174          * in case the attrib does not get a valid index back, it was probably
175          * removed by the glsl compiler by dead code elimination */
176
177         for(a=0, b=0; a<attribs->totlayer; a++) {
178                 sprintf(name, "att%d", attribs->layer[a].glindex);
179                 attribs->layer[a].glindex = GPU_shader_get_attribute(shader, name);
180
181                 if(attribs->layer[a].glindex >= 0) {
182                         attribs->layer[b] = attribs->layer[a];
183                         b++;
184                 }
185         }
186
187         attribs->totlayer = b;
188 }
189
190 static int GPU_material_construct_end(GPUMaterial *material)
191 {
192         if (material->outlink) {
193                 GPUNodeLink *outlink;
194                 GPUShader *shader;
195
196                 outlink = material->outlink;
197                 material->pass = GPU_generate_pass(&material->nodes, outlink,
198                         &material->attribs, &material->builtins, material->ma->id.name);
199
200                 if(!material->pass)
201                         return 0;
202
203                 gpu_material_set_attrib_id(material);
204                 
205                 shader = GPU_pass_shader(material->pass);
206
207                 if(material->builtins & GPU_VIEW_MATRIX)
208                         material->viewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_VIEW_MATRIX));
209                 if(material->builtins & GPU_INVERSE_VIEW_MATRIX)
210                         material->invviewmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_INVERSE_VIEW_MATRIX));
211                 if(material->builtins & GPU_OBJECT_MATRIX)
212                         material->obmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_OBJECT_MATRIX));
213                 if(material->builtins & GPU_INVERSE_OBJECT_MATRIX)
214                         material->invobmatloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_INVERSE_OBJECT_MATRIX));
215                 if(material->builtins & GPU_OBCOLOR)
216                         material->obcolloc = GPU_shader_get_uniform(shader, GPU_builtin_name(GPU_OBCOLOR));
217
218                 return 1;
219         }
220
221         return 0;
222 }
223
224 void GPU_material_free(Material *ma)
225 {
226         LinkData *link;
227         LinkData *nlink, *mlink, *next;
228
229         for(link=ma->gpumaterial.first; link; link=link->next) {
230                 GPUMaterial *material = link->data;
231
232                 if(material->pass)
233                         GPU_pass_free(material->pass);
234
235                 for(nlink=material->lamps.first; nlink; nlink=nlink->next) {
236                         GPULamp *lamp = nlink->data;
237
238                         for(mlink=lamp->materials.first; mlink; mlink=next) {
239                                 next = mlink->next;
240                                 if(mlink->data == ma)
241                                         BLI_freelinkN(&lamp->materials, mlink);
242                         }
243                 }
244
245                 BLI_freelistN(&material->lamps);
246
247                 MEM_freeN(material);
248         }
249
250         BLI_freelistN(&ma->gpumaterial);
251 }
252
253 void GPU_material_bind(GPUMaterial *material, int oblay, int viewlay, double time, int mipmap)
254 {
255         if(material->pass) {
256                 LinkData *nlink;
257                 GPULamp *lamp;
258
259                 /* handle layer lamps */
260                 for(nlink=material->lamps.first; nlink; nlink=nlink->next) {
261                         lamp= nlink->data;
262
263                         if(!lamp->hide && (lamp->lay & viewlay) && (!(lamp->mode & LA_LAYER) || (lamp->lay & oblay))) {
264                                 lamp->dynenergy = lamp->energy;
265                                 VECCOPY(lamp->dyncol, lamp->col);
266                         }
267                         else {
268                                 lamp->dynenergy = 0.0f;
269                                 lamp->dyncol[0]= lamp->dyncol[1]= lamp->dyncol[2] = 0.0f;
270                         }
271                 }
272
273                 GPU_pass_bind(material->pass, time, mipmap);
274                 material->bound = 1;
275         }
276 }
277
278 void GPU_material_bind_uniforms(GPUMaterial *material, float obmat[][4], float viewmat[][4], float viewinv[][4], float obcol[4])
279 {
280         if(material->pass) {
281                 GPUShader *shader = GPU_pass_shader(material->pass);
282                 LinkData *nlink;
283                 GPULamp *lamp;
284                 float invmat[4][4], col[4];
285
286                 /* handle builtins */
287                 if(material->builtins & GPU_VIEW_MATRIX) {
288                         GPU_shader_uniform_vector(shader, material->viewmatloc, 16, 1, (float*)viewmat);
289                 }
290                 if(material->builtins & GPU_INVERSE_VIEW_MATRIX) {
291                         GPU_shader_uniform_vector(shader, material->invviewmatloc, 16, 1, (float*)viewinv);
292                 }
293                 if(material->builtins & GPU_OBJECT_MATRIX) {
294                         GPU_shader_uniform_vector(shader, material->obmatloc, 16, 1, (float*)obmat);
295                 }
296                 if(material->builtins & GPU_INVERSE_OBJECT_MATRIX) {
297                         invert_m4_m4(invmat, obmat);
298                         GPU_shader_uniform_vector(shader, material->invobmatloc, 16, 1, (float*)invmat);
299                 }
300                 if(material->builtins & GPU_OBCOLOR) {
301                         QUATCOPY(col, obcol);
302                         CLAMP(col[3], 0.0f, 1.0f);
303                         GPU_shader_uniform_vector(shader, material->obcolloc, 4, 1, col);
304                 }
305
306                 /* update lamps */
307                 for(nlink=material->lamps.first; nlink; nlink=nlink->next) {
308                         lamp= nlink->data;
309
310                         if(material->dynproperty & DYN_LAMP_VEC) {
311                                 VECCOPY(lamp->dynvec, lamp->vec);
312                                 normalize_v3(lamp->dynvec);
313                                 negate_v3(lamp->dynvec);
314                                 mul_mat3_m4_v3(viewmat, lamp->dynvec);
315                         }
316
317                         if(material->dynproperty & DYN_LAMP_CO) {
318                                 VECCOPY(lamp->dynco, lamp->co);
319                                 mul_m4_v3(viewmat, lamp->dynco);
320                         }
321
322                         if(material->dynproperty & DYN_LAMP_IMAT)
323                                 mul_m4_m4m4(lamp->dynimat, viewinv, lamp->imat);
324                         if(material->dynproperty & DYN_LAMP_PERSMAT)
325                                 mul_m4_m4m4(lamp->dynpersmat, viewinv, lamp->persmat);
326                 }
327
328                 GPU_pass_update_uniforms(material->pass);
329         }
330 }
331
332 void GPU_material_unbind(GPUMaterial *material)
333 {
334         if (material->pass) {
335                 material->bound = 0;
336                 GPU_pass_unbind(material->pass);
337         }
338 }
339
340 int GPU_material_bound(GPUMaterial *material)
341 {
342         return material->bound;
343 }
344
345 void GPU_material_vertex_attributes(GPUMaterial *material, GPUVertexAttribs *attribs)
346 {
347         *attribs = material->attribs;
348 }
349
350 void GPU_material_output_link(GPUMaterial *material, GPUNodeLink *link)
351 {
352         if(!material->outlink)
353                 material->outlink= link;
354 }
355
356 void GPU_material_enable_alpha(GPUMaterial *material)
357 {
358         material->alpha= 1;
359 }
360
361 GPUBlendMode GPU_material_blend_mode(GPUMaterial *material, float obcol[3])
362 {
363         if(material->alpha || (material->obcolalpha && obcol[3] < 1.0f))
364                 return GPU_BLEND_ALPHA;
365         else
366                 return GPU_BLEND_SOLID;
367 }
368
369 void gpu_material_add_node(GPUMaterial *material, GPUNode *node)
370 {
371         BLI_addtail(&material->nodes, node);
372 }
373
374 /* Code generation */
375
376 static int gpu_do_color_management(GPUMaterial *mat)
377 {
378         return ((mat->scene->r.color_mgt_flag & R_COLOR_MANAGEMENT) &&
379            !((mat->scene->gm.flag & GAME_GLSL_NO_COLOR_MANAGEMENT)));
380 }
381
382 static GPUNodeLink *lamp_get_visibility(GPUMaterial *mat, GPULamp *lamp, GPUNodeLink **lv, GPUNodeLink **dist)
383 {
384         GPUNodeLink *visifac, *inpr;
385
386         /* from get_lamp_visibility */
387         if(lamp->type==LA_SUN || lamp->type==LA_HEMI) {
388                 mat->dynproperty |= DYN_LAMP_VEC;
389                 GPU_link(mat, "lamp_visibility_sun_hemi", GPU_dynamic_uniform(lamp->dynvec), lv, dist, &visifac);
390                 return visifac;
391         }
392         else {
393                 mat->dynproperty |= DYN_LAMP_CO;
394                 GPU_link(mat, "lamp_visibility_other", GPU_builtin(GPU_VIEW_POSITION), GPU_dynamic_uniform(lamp->dynco), lv, dist, &visifac);
395
396                 if(lamp->type==LA_AREA)
397                         return visifac;
398
399                 switch(lamp->falloff_type)
400                 {
401                         case LA_FALLOFF_CONSTANT:
402                                 break;
403                         case LA_FALLOFF_INVLINEAR:
404                                 GPU_link(mat, "lamp_falloff_invlinear", GPU_uniform(&lamp->dist), *dist, &visifac);
405                                 break;
406                         case LA_FALLOFF_INVSQUARE:
407                                 GPU_link(mat, "lamp_falloff_invsquare", GPU_uniform(&lamp->dist), *dist, &visifac);
408                                 break;
409                         case LA_FALLOFF_SLIDERS:
410                                 GPU_link(mat, "lamp_falloff_sliders", GPU_uniform(&lamp->dist), GPU_uniform(&lamp->att1), GPU_uniform(&lamp->att2), *dist, &visifac);
411                                 break;
412                         case LA_FALLOFF_CURVE:
413                                 {
414                                         float *array;
415                                         int size;
416
417                                         curvemapping_table_RGBA(lamp->curfalloff, &array, &size);
418                                         GPU_link(mat, "lamp_falloff_curve", GPU_uniform(&lamp->dist), GPU_texture(size, array), *dist, &visifac);
419                                 }
420                                 break;
421                 }
422
423                 if(lamp->mode & LA_SPHERE)
424                         GPU_link(mat, "lamp_visibility_sphere", GPU_uniform(&lamp->dist), *dist, visifac, &visifac);
425
426                 if(lamp->type == LA_SPOT) {
427                         if(lamp->mode & LA_SQUARE) {
428                                 mat->dynproperty |= DYN_LAMP_VEC|DYN_LAMP_IMAT;
429                                 GPU_link(mat, "lamp_visibility_spot_square", GPU_dynamic_uniform(lamp->dynvec), GPU_dynamic_uniform((float*)lamp->dynimat), *lv, &inpr);
430                         }
431                         else {
432                                 mat->dynproperty |= DYN_LAMP_VEC;
433                                 GPU_link(mat, "lamp_visibility_spot_circle", GPU_dynamic_uniform(lamp->dynvec), *lv, &inpr);
434                         }
435                         
436                         GPU_link(mat, "lamp_visibility_spot", GPU_uniform(&lamp->spotsi), GPU_uniform(&lamp->spotbl), inpr, visifac, &visifac);
437                 }
438
439                 GPU_link(mat, "lamp_visibility_clamp", visifac, &visifac);
440
441                 return visifac;
442         }
443 }
444
445 #if 0
446 static void area_lamp_vectors(LampRen *lar)
447 {
448         float xsize= 0.5*lar->area_size, ysize= 0.5*lar->area_sizey, multifac;
449
450         /* make it smaller, so area light can be multisampled */
451         multifac= 1.0f/sqrt((float)lar->ray_totsamp);
452         xsize *= multifac;
453         ysize *= multifac;
454
455         /* corner vectors */
456         lar->area[0][0]= lar->co[0] - xsize*lar->mat[0][0] - ysize*lar->mat[1][0];
457         lar->area[0][1]= lar->co[1] - xsize*lar->mat[0][1] - ysize*lar->mat[1][1];
458         lar->area[0][2]= lar->co[2] - xsize*lar->mat[0][2] - ysize*lar->mat[1][2];
459
460         /* corner vectors */
461         lar->area[1][0]= lar->co[0] - xsize*lar->mat[0][0] + ysize*lar->mat[1][0];
462         lar->area[1][1]= lar->co[1] - xsize*lar->mat[0][1] + ysize*lar->mat[1][1];
463         lar->area[1][2]= lar->co[2] - xsize*lar->mat[0][2] + ysize*lar->mat[1][2];
464
465         /* corner vectors */
466         lar->area[2][0]= lar->co[0] + xsize*lar->mat[0][0] + ysize*lar->mat[1][0];
467         lar->area[2][1]= lar->co[1] + xsize*lar->mat[0][1] + ysize*lar->mat[1][1];
468         lar->area[2][2]= lar->co[2] + xsize*lar->mat[0][2] + ysize*lar->mat[1][2];
469
470         /* corner vectors */
471         lar->area[3][0]= lar->co[0] + xsize*lar->mat[0][0] - ysize*lar->mat[1][0];
472         lar->area[3][1]= lar->co[1] + xsize*lar->mat[0][1] - ysize*lar->mat[1][1];
473         lar->area[3][2]= lar->co[2] + xsize*lar->mat[0][2] - ysize*lar->mat[1][2];
474         /* only for correction button size, matrix size works on energy */
475         lar->areasize= lar->dist*lar->dist/(4.0*xsize*ysize);
476 }
477 #endif
478
479 static void ramp_blend(GPUMaterial *mat, GPUNodeLink *fac, GPUNodeLink *col1, GPUNodeLink *col2, int type, GPUNodeLink **outcol)
480 {
481         static const char *names[] = {"mix_blend", "mix_add", "mix_mult", "mix_sub",
482                 "mix_screen", "mix_div", "mix_diff", "mix_dark", "mix_light",
483                 "mix_overlay", "mix_dodge", "mix_burn", "mix_hue", "mix_sat",
484                 "mix_val", "mix_color", "mix_soft", "mix_linear"};
485
486         GPU_link(mat, names[type], fac, col1, col2, outcol);
487 }
488
489 static void do_colorband_blend(GPUMaterial *mat, ColorBand *coba, GPUNodeLink *fac, float rampfac, int type, GPUNodeLink *incol, GPUNodeLink **outcol)
490 {
491         GPUNodeLink *tmp, *alpha, *col;
492         float *array;
493         int size;
494
495         /* do colorband */
496         colorband_table_RGBA(coba, &array, &size);
497         GPU_link(mat, "valtorgb", fac, GPU_texture(size, array), &col, &tmp);
498
499         /* use alpha in fac */
500         GPU_link(mat, "mtex_alpha_from_col", col, &alpha);
501         GPU_link(mat, "math_multiply", alpha, GPU_uniform(&rampfac), &fac);
502
503         /* blending method */
504         ramp_blend(mat, fac, incol, col, type, outcol);
505 }
506
507 static void ramp_diffuse_result(GPUShadeInput *shi, GPUNodeLink **diff)
508 {
509         Material *ma= shi->mat;
510         GPUMaterial *mat= shi->gpumat;
511         GPUNodeLink *fac;
512
513         if(!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS)) {
514                 if(ma->ramp_col) {
515                         if(ma->rampin_col==MA_RAMP_IN_RESULT) {
516                                 GPU_link(mat, "ramp_rgbtobw", *diff, &fac);
517                                 
518                                 /* colorband + blend */
519                                 do_colorband_blend(mat, ma->ramp_col, fac, ma->rampfac_col, ma->rampblend_col, *diff, diff);
520                         }
521                 }
522         }
523 }
524
525 static void add_to_diffuse(GPUMaterial *mat, Material *ma, GPUShadeInput *shi, GPUNodeLink *is, GPUNodeLink *rgb, GPUNodeLink **diff)
526 {
527         GPUNodeLink *fac, *tmp, *addcol;
528         
529         if(!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS) &&
530            ma->ramp_col && (ma->mode & MA_RAMP_COL)) {
531                 /* MA_RAMP_IN_RESULT is exceptional */
532                 if(ma->rampin_col==MA_RAMP_IN_RESULT) {
533                         addcol = shi->rgb;
534                 }
535                 else {
536                         /* input */
537                         switch(ma->rampin_col) {
538                         case MA_RAMP_IN_ENERGY:
539                                 GPU_link(mat, "ramp_rgbtobw", rgb, &fac);
540                                 break;
541                         case MA_RAMP_IN_SHADER:
542                                 fac= is;
543                                 break;
544                         case MA_RAMP_IN_NOR:
545                                 GPU_link(mat, "vec_math_dot", shi->view, shi->vn, &tmp, &fac);
546                                 break;
547                         default:
548                                 GPU_link(mat, "set_value_zero", &fac);
549                                 break;
550                         }
551
552                         /* colorband + blend */
553                         do_colorband_blend(mat, ma->ramp_col, fac, ma->rampfac_col, ma->rampblend_col, shi->rgb, &addcol);
554                 }
555         }
556         else
557                 addcol = shi->rgb;
558
559         /* output to */
560         GPU_link(mat, "shade_madd_clamped", *diff, rgb, addcol, diff);
561 }
562
563 static void ramp_spec_result(GPUShadeInput *shi, GPUNodeLink **spec)
564 {
565         Material *ma= shi->mat;
566         GPUMaterial *mat= shi->gpumat;
567         GPUNodeLink *fac;
568
569         if(!(mat->scene->gm.flag & GAME_GLSL_NO_RAMPS) &&
570            ma->ramp_spec && ma->rampin_spec==MA_RAMP_IN_RESULT) {
571                 GPU_link(mat, "ramp_rgbtobw", *spec, &fac);
572                 
573                 /* colorband + blend */
574                 do_colorband_blend(mat, ma->ramp_spec, fac, ma->rampfac_spec, ma->rampblend_spec, *spec, spec);
575         }
576 }
577
578 static void do_specular_ramp(GPUShadeInput *shi, GPUNodeLink *is, GPUNodeLink *t, GPUNodeLink **spec)
579 {
580         Material *ma= shi->mat;
581         GPUMaterial *mat= shi->gpumat;
582         GPUNodeLink *fac, *tmp;
583
584         *spec = shi->specrgb;
585
586         /* MA_RAMP_IN_RESULT is exception */
587         if(ma->ramp_spec && (ma->rampin_spec!=MA_RAMP_IN_RESULT)) {
588                 
589                 /* input */
590                 switch(ma->rampin_spec) {
591                 case MA_RAMP_IN_ENERGY:
592                         fac = t;
593                         break;
594                 case MA_RAMP_IN_SHADER:
595                         fac = is;
596                         break;
597                 case MA_RAMP_IN_NOR:
598                         GPU_link(mat, "vec_math_dot", shi->view, shi->vn, &tmp, &fac);
599                         break;
600                 default:
601                         GPU_link(mat, "set_value_zero", &fac);
602                         break;
603                 }
604                 
605                 /* colorband + blend */
606                 do_colorband_blend(mat, ma->ramp_spec, fac, ma->rampfac_spec, ma->rampblend_spec, *spec, spec);
607         }
608 }
609
610 static void add_user_list(ListBase *list, void *data)
611 {
612         LinkData *link = MEM_callocN(sizeof(LinkData), "GPULinkData");
613         link->data = data;
614         BLI_addtail(list, link);
615 }
616
617 static void shade_one_light(GPUShadeInput *shi, GPUShadeResult *shr, GPULamp *lamp)
618 {
619         Material *ma= shi->mat;
620         GPUMaterial *mat= shi->gpumat;
621         GPUNodeLink *lv, *dist, *visifac, *is, *inp, *i, *vn, *view;
622         GPUNodeLink *outcol, *specfac, *t, *shadfac= NULL;
623         float one = 1.0f;
624
625         if((lamp->mode & LA_ONLYSHADOW) && !(ma->mode & MA_SHADOW))
626                 return;
627         
628         vn= shi->vn;
629         view= shi->view;
630
631         visifac= lamp_get_visibility(mat, lamp, &lv, &dist);
632
633         /*if(ma->mode & MA_TANGENT_V)
634                 GPU_link(mat, "shade_tangent_v", lv, GPU_attribute(CD_TANGENT, ""), &vn);*/
635         
636         GPU_link(mat, "shade_inp", vn, lv, &inp);
637
638         if(lamp->mode & LA_NO_DIFF) {
639                 GPU_link(mat, "shade_is_no_diffuse", &is);
640         }
641         else if(lamp->type == LA_HEMI) {
642                 GPU_link(mat, "shade_is_hemi", inp, &is);
643         }
644         else {
645                 if(lamp->type == LA_AREA) {
646                         float area[4][4]= {{0.0f}}, areasize= 0.0f;
647
648                         mat->dynproperty |= DYN_LAMP_VEC|DYN_LAMP_CO;
649                         GPU_link(mat, "shade_inp_area", GPU_builtin(GPU_VIEW_POSITION), GPU_dynamic_uniform(lamp->dynco), GPU_dynamic_uniform(lamp->dynvec), vn, GPU_uniform((float*)area),
650                                 GPU_uniform(&areasize), GPU_uniform(&lamp->k), &inp);
651                 }
652
653                 is= inp; /* Lambert */
654
655                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_SHADERS)) {
656                         if(ma->diff_shader==MA_DIFF_ORENNAYAR)
657                                 GPU_link(mat, "shade_diffuse_oren_nayer", inp, vn, lv, view, GPU_uniform(&ma->roughness), &is);
658                         else if(ma->diff_shader==MA_DIFF_TOON)
659                                 GPU_link(mat, "shade_diffuse_toon", vn, lv, view, GPU_uniform(&ma->param[0]), GPU_uniform(&ma->param[1]), &is);
660                         else if(ma->diff_shader==MA_DIFF_MINNAERT)
661                                 GPU_link(mat, "shade_diffuse_minnaert", inp, vn, view, GPU_uniform(&ma->darkness), &is);
662                         else if(ma->diff_shader==MA_DIFF_FRESNEL)
663                                 GPU_link(mat, "shade_diffuse_fresnel", vn, lv, view, GPU_uniform(&ma->param[0]), GPU_uniform(&ma->param[1]), &is);
664                 }
665         }
666
667         if(!(mat->scene->gm.flag & GAME_GLSL_NO_SHADERS))
668                 if(ma->shade_flag & MA_CUBIC)
669                         GPU_link(mat, "shade_cubic", is, &is);
670         
671         i = is;
672         GPU_link(mat, "shade_visifac", i, visifac, shi->refl, &i);
673
674         vn = shi->vn;
675         /*if(ma->mode & MA_TANGENT_VN)
676                 GPU_link(mat, "shade_tangent_v_spec", GPU_attribute(CD_TANGENT, ""), &vn);*/
677
678         /* this replaces if(i > 0.0) conditional until that is supported */
679         // done in shade_visifac now, GPU_link(mat, "mtex_value_clamp_positive", i, &i);
680
681         if((ma->mode & MA_SHADOW) && GPU_lamp_has_shadow_buffer(lamp)) {
682                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_SHADOWS)) {
683                         mat->dynproperty |= DYN_LAMP_PERSMAT;
684
685                         GPU_link(mat, "test_shadowbuf",
686                                 GPU_builtin(GPU_VIEW_POSITION),
687                                 GPU_dynamic_texture(lamp->tex),
688                                 GPU_dynamic_uniform((float*)lamp->dynpersmat),
689                                 GPU_uniform(&lamp->bias), inp, &shadfac);
690                         
691                         if(lamp->mode & LA_ONLYSHADOW) {
692                                 GPU_link(mat, "shade_only_shadow", i, shadfac,
693                                         GPU_dynamic_uniform(&lamp->dynenergy), &shadfac);
694                                 
695                                 if(!(lamp->mode & LA_NO_DIFF))
696                                         GPU_link(mat, "shade_only_shadow_diffuse", shadfac, shi->rgb,
697                                                 shr->diff, &shr->diff);
698
699                                 if(!(lamp->mode & LA_NO_SPEC))
700                                         GPU_link(mat, "shade_only_shadow_specular", shadfac, shi->specrgb,
701                                                 shr->spec, &shr->spec);
702                                 
703                                 add_user_list(&mat->lamps, lamp);
704                                 add_user_list(&lamp->materials, shi->gpumat->ma);
705                                 return;
706                         }
707                         
708                         GPU_link(mat, "math_multiply", i, shadfac, &i);
709                 }
710         }
711         else if((mat->scene->gm.flag & GAME_GLSL_NO_SHADOWS) && (lamp->mode & LA_ONLYSHADOW)) {
712                 add_user_list(&mat->lamps, lamp);
713                 add_user_list(&lamp->materials, shi->gpumat->ma);
714                 return;
715         }
716         else
717                 GPU_link(mat, "set_value", GPU_uniform(&one), &shadfac);
718
719         if(GPU_link_changed(shi->refl) || ma->ref != 0.0f) {
720                 if(!(lamp->mode & LA_NO_DIFF)) {
721                         GPUNodeLink *rgb;
722                         GPU_link(mat, "shade_mul_value", i, GPU_dynamic_uniform(lamp->dyncol), &rgb);
723                         add_to_diffuse(mat, ma, shi, is, rgb, &shr->diff);
724                 }
725         }
726
727         if(mat->scene->gm.flag & GAME_GLSL_NO_SHADERS);
728         else if(!(lamp->mode & LA_NO_SPEC) && !(lamp->mode & LA_ONLYSHADOW) &&
729            (GPU_link_changed(shi->spec) || ma->spec != 0.0f)) {
730                 if(lamp->type == LA_HEMI) {
731                         GPU_link(mat, "shade_hemi_spec", vn, lv, view, GPU_uniform(&ma->spec), shi->har, visifac, &t);
732                         GPU_link(mat, "shade_add_spec", t, GPU_dynamic_uniform(lamp->dyncol), shi->specrgb, &outcol);
733                         GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
734                 }
735                 else {
736                         if(ma->spec_shader==MA_SPEC_PHONG)
737                                 GPU_link(mat, "shade_phong_spec", vn, lv, view, shi->har, &specfac);
738                         else if(ma->spec_shader==MA_SPEC_COOKTORR)
739                                 GPU_link(mat, "shade_cooktorr_spec", vn, lv, view, shi->har, &specfac);
740                         else if(ma->spec_shader==MA_SPEC_BLINN)
741                                 GPU_link(mat, "shade_blinn_spec", vn, lv, view, GPU_uniform(&ma->refrac), shi->har, &specfac);
742                         else if(ma->spec_shader==MA_SPEC_WARDISO)
743                                 GPU_link(mat, "shade_wardiso_spec", vn, lv, view, GPU_uniform(&ma->rms), &specfac);
744                         else
745                                 GPU_link(mat, "shade_toon_spec", vn, lv, view, GPU_uniform(&ma->param[2]), GPU_uniform(&ma->param[3]), &specfac);
746
747                         if(lamp->type==LA_AREA)
748                                 GPU_link(mat, "shade_spec_area_inp", specfac, inp, &specfac);
749
750                         GPU_link(mat, "shade_spec_t", shadfac, shi->spec, visifac, specfac, &t);
751
752                         if(ma->mode & MA_RAMP_SPEC) {
753                                 GPUNodeLink *spec;
754                                 do_specular_ramp(shi, specfac, t, &spec);
755                                 GPU_link(mat, "shade_add_spec", t, GPU_dynamic_uniform(lamp->dyncol), spec, &outcol);
756                                 GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
757                         }
758                         else {
759                                 GPU_link(mat, "shade_add_spec", t, GPU_dynamic_uniform(lamp->dyncol), shi->specrgb, &outcol);
760                                 GPU_link(mat, "shade_add_clamped", shr->spec, outcol, &shr->spec);
761                         }
762                 }
763         }
764
765         add_user_list(&mat->lamps, lamp);
766         add_user_list(&lamp->materials, shi->gpumat->ma);
767 }
768
769 static void material_lights(GPUShadeInput *shi, GPUShadeResult *shr)
770 {
771         Base *base;
772         Object *ob;
773         Scene *sce_iter;
774         GPULamp *lamp;
775         
776         for(SETLOOPER(shi->gpumat->scene, sce_iter, base)) {
777                 ob= base->object;
778
779                 if(ob->type==OB_LAMP) {
780                         lamp = GPU_lamp_from_blender(shi->gpumat->scene, ob, NULL);
781                         if(lamp)
782                                 shade_one_light(shi, shr, lamp);
783                 }
784
785                 if (ob->transflag & OB_DUPLI) {
786                         DupliObject *dob;
787                         ListBase *lb = object_duplilist(shi->gpumat->scene, ob);
788                         
789                         for(dob=lb->first; dob; dob=dob->next) {
790                                 Object *ob_iter = dob->ob;
791
792                                 if(ob_iter->type==OB_LAMP) {
793                                         copy_m4_m4(ob_iter->obmat, dob->mat);
794
795                                         lamp = GPU_lamp_from_blender(shi->gpumat->scene, ob_iter, ob);
796                                         if(lamp)
797                                                 shade_one_light(shi, shr, lamp);
798                                 }
799                         }
800                         
801                         free_object_duplilist(lb);
802                 }
803         }
804 }
805
806 static void texture_rgb_blend(GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg, int blendtype, GPUNodeLink **in)
807 {
808         switch(blendtype) {
809         case MTEX_BLEND:
810                 GPU_link(mat, "mtex_rgb_blend", out, tex, fact, facg, in);
811                 break;
812         case MTEX_MUL:
813                 GPU_link(mat, "mtex_rgb_mul", out, tex, fact, facg, in);
814                 break;
815         case MTEX_SCREEN:
816                 GPU_link(mat, "mtex_rgb_screen", out, tex, fact, facg, in);
817                 break;
818         case MTEX_OVERLAY:
819                 GPU_link(mat, "mtex_rgb_overlay", out, tex, fact, facg, in);
820                 break;
821         case MTEX_SUB:
822                 GPU_link(mat, "mtex_rgb_sub", out, tex, fact, facg, in);
823                 break;
824         case MTEX_ADD:
825                 GPU_link(mat, "mtex_rgb_add", out, tex, fact, facg, in);
826                 break;
827         case MTEX_DIV:
828                 GPU_link(mat, "mtex_rgb_div", out, tex, fact, facg, in);
829                 break;
830         case MTEX_DIFF:
831                 GPU_link(mat, "mtex_rgb_diff", out, tex, fact, facg, in);
832                 break;
833         case MTEX_DARK:
834                 GPU_link(mat, "mtex_rgb_dark", out, tex, fact, facg, in);
835                 break;
836         case MTEX_LIGHT:
837                 GPU_link(mat, "mtex_rgb_light", out, tex, fact, facg, in);
838                 break;
839         case MTEX_BLEND_HUE:
840                 GPU_link(mat, "mtex_rgb_hue", out, tex, fact, facg, in);
841                 break;
842         case MTEX_BLEND_SAT:
843                 GPU_link(mat, "mtex_rgb_sat", out, tex, fact, facg, in);
844                 break;
845         case MTEX_BLEND_VAL:
846                 GPU_link(mat, "mtex_rgb_val", out, tex, fact, facg, in);
847                 break;
848         case MTEX_BLEND_COLOR:
849                 GPU_link(mat, "mtex_rgb_color", out, tex, fact, facg, in);
850                 break;
851         default:
852                 GPU_link(mat, "set_rgb_zero", &in);
853                 break;
854         }
855 }
856
857 static void texture_value_blend(GPUMaterial *mat, GPUNodeLink *tex, GPUNodeLink *out, GPUNodeLink *fact, GPUNodeLink *facg, int blendtype, GPUNodeLink **in)
858 {
859         switch(blendtype) {
860         case MTEX_BLEND:
861                 GPU_link(mat, "mtex_value_blend", out, tex, fact, facg, in);
862                 break;
863         case MTEX_MUL:
864                 GPU_link(mat, "mtex_value_mul", out, tex, fact, facg, in);
865                 break;
866         case MTEX_SCREEN:
867                 GPU_link(mat, "mtex_value_screen", out, tex, fact, facg, in);
868                 break;
869         case MTEX_SUB:
870                 GPU_link(mat, "mtex_value_sub", out, tex, fact, facg, in);
871                 break;
872         case MTEX_ADD:
873                 GPU_link(mat, "mtex_value_add", out, tex, fact, facg, in);
874                 break;
875         case MTEX_DIV:
876                 GPU_link(mat, "mtex_value_div", out, tex, fact, facg, in);
877                 break;
878         case MTEX_DIFF:
879                 GPU_link(mat, "mtex_value_diff", out, tex, fact, facg, in);
880                 break;
881         case MTEX_DARK:
882                 GPU_link(mat, "mtex_value_dark", out, tex, fact, facg, in);
883                 break;
884         case MTEX_LIGHT:
885                 GPU_link(mat, "mtex_value_light", out, tex, fact, facg, in);
886                 break;
887         default:
888                 GPU_link(mat, "set_value_zero", &in);
889                 break;
890         }
891 }
892
893 static void do_material_tex(GPUShadeInput *shi)
894 {
895         Material *ma= shi->mat;
896         GPUMaterial *mat= shi->gpumat;
897         MTex *mtex;
898         Tex *tex;
899         GPUNodeLink *texco, *tin, *trgb, *tnor, *tcol, *stencil, *tnorfac;
900         GPUNodeLink *texco_norm, *texco_orco, *texco_object;
901         GPUNodeLink *texco_global, *texco_uv = NULL;
902         GPUNodeLink *newnor, *orn;
903         /*char *lastuvname = NULL;*/ /*UNUSED*/
904         float one = 1.0f, norfac, ofs[3];
905         int tex_nr, rgbnor, talpha;
906         int init_done = 0, iBumpSpacePrev;
907         GPUNodeLink *vNorg, *vNacc, *fPrevMagnitude;
908         int iFirstTimeNMap=1;
909
910         GPU_link(mat, "set_value", GPU_uniform(&one), &stencil);
911
912         GPU_link(mat, "texco_norm", GPU_builtin(GPU_VIEW_NORMAL), &texco_norm);
913         GPU_link(mat, "texco_orco", GPU_attribute(CD_ORCO, ""), &texco_orco);
914         GPU_link(mat, "texco_object", GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
915                 GPU_builtin(GPU_INVERSE_OBJECT_MATRIX),
916                 GPU_builtin(GPU_VIEW_POSITION), &texco_object);
917         //GPU_link(mat, "texco_tangent", GPU_attribute(CD_TANGENT, ""), &texco_tangent);
918         GPU_link(mat, "texco_global", GPU_builtin(GPU_INVERSE_VIEW_MATRIX),
919                 GPU_builtin(GPU_VIEW_POSITION), &texco_global);
920
921         orn= texco_norm;
922
923         /* go over texture slots */
924         for(tex_nr=0; tex_nr<MAX_MTEX; tex_nr++) {
925                 /* separate tex switching */
926                 if(ma->septex & (1<<tex_nr)) continue;
927                 
928                 if(ma->mtex[tex_nr]) {
929                         mtex= ma->mtex[tex_nr];
930                         
931                         tex= mtex->tex;
932                         if(tex == NULL) continue;
933
934                         /* which coords */
935                         if(mtex->texco==TEXCO_ORCO)
936                                 texco= texco_orco;
937                         else if(mtex->texco==TEXCO_OBJECT)
938                                 texco= texco_object;
939                         else if(mtex->texco==TEXCO_NORM)
940                                 texco= orn;
941                         else if(mtex->texco==TEXCO_TANGENT)
942                                 texco= texco_object;
943                         else if(mtex->texco==TEXCO_GLOB)
944                                 texco= texco_global;
945                         else if(mtex->texco==TEXCO_REFL) {
946                                 GPU_link(mat, "texco_refl", shi->vn, shi->view, &shi->ref);
947                                 texco= shi->ref;
948                         }
949                         else if(mtex->texco==TEXCO_UV) {
950                                 if(1) { //!(texco_uv && strcmp(mtex->uvname, lastuvname) == 0)) {
951                                         GPU_link(mat, "texco_uv", GPU_attribute(CD_MTFACE, mtex->uvname), &texco_uv);
952                                         /*lastuvname = mtex->uvname;*/ /*UNUSED*/
953                                 }
954                                 texco= texco_uv;
955                         }
956                         else
957                                 continue;
958
959                         /* in case of uv, this would just undo a multiplication in texco_uv */
960                         if(mtex->texco != TEXCO_UV)
961                                 GPU_link(mat, "mtex_2d_mapping", texco, &texco);
962
963                         if(mtex->size[0] != 1.0f || mtex->size[1] != 1.0f || mtex->size[2] != 1.0f)
964                                 GPU_link(mat, "mtex_mapping_size", texco, GPU_uniform(mtex->size), &texco);
965
966                         ofs[0] = mtex->ofs[0] + 0.5f - 0.5f*mtex->size[0];
967                         ofs[1] = mtex->ofs[1] + 0.5f - 0.5f*mtex->size[1];
968                         ofs[2] = 0.0f;
969                         if(ofs[0] != 0.0f || ofs[1] != 0.0f || ofs[2] != 0.0f)
970                                 GPU_link(mat, "mtex_mapping_ofs", texco, GPU_uniform(ofs), &texco);
971
972                         talpha = 0;
973                         rgbnor = 0;
974
975                         if(tex && tex->type == TEX_IMAGE && tex->ima) {
976                                 GPU_link(mat, "mtex_image", texco, GPU_image(tex->ima, &tex->iuser), &tin, &trgb);
977                                 rgbnor= TEX_RGB;
978
979                                 if(tex->imaflag & TEX_USEALPHA)
980                                         talpha= 1;
981                         }
982                         else continue;
983
984                         /* texture output */
985                         if((rgbnor & TEX_RGB) && (mtex->texflag & MTEX_RGBTOINT)) {
986                                 GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
987                                 rgbnor -= TEX_RGB;
988                         }
989
990                         if(mtex->texflag & MTEX_NEGATIVE) {
991                                 if(rgbnor & TEX_RGB)
992                                         GPU_link(mat, "mtex_rgb_invert", trgb, &trgb);
993                                 else
994                                         GPU_link(mat, "mtex_value_invert", tin, &tin);
995                         }
996
997                         if(mtex->texflag & MTEX_STENCIL) {
998                                 if(rgbnor & TEX_RGB)
999                                         GPU_link(mat, "mtex_rgb_stencil", stencil, trgb, &stencil, &trgb);
1000                                 else
1001                                         GPU_link(mat, "mtex_value_stencil", stencil, tin, &stencil, &tin);
1002                         }
1003
1004                         /* mapping */
1005                         if(mtex->mapto & (MAP_COL+MAP_COLSPEC)) {
1006                                 /* stencil maps on the texture control slider, not texture intensity value */
1007                                 if((rgbnor & TEX_RGB)==0) {
1008                                         GPU_link(mat, "set_rgb", GPU_uniform(&mtex->r), &tcol);
1009                                 }
1010                                 else {
1011                                         GPU_link(mat, "set_rgba", trgb, &tcol);
1012
1013                                         if(mtex->mapto & MAP_ALPHA)
1014                                                 GPU_link(mat, "set_value", stencil, &tin);
1015                                         else if(talpha)
1016                                                 GPU_link(mat, "mtex_alpha_from_col", trgb, &tin);
1017                                         else
1018                                                 GPU_link(mat, "set_value_one", &tin);
1019                                 }
1020
1021                                 if(tex->type==TEX_IMAGE)
1022                                         if(gpu_do_color_management(mat))
1023                                                 GPU_link(mat, "srgb_to_linearrgb", tcol, &tcol);
1024                                 
1025                                 if(mtex->mapto & MAP_COL) {
1026                                         GPUNodeLink *colfac;
1027
1028                                         if(mtex->colfac == 1.0f) colfac = stencil;
1029                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->colfac), stencil, &colfac);
1030
1031                                         texture_rgb_blend(mat, tcol, shi->rgb, tin, colfac, mtex->blendtype, &shi->rgb);
1032                                 }
1033                                 
1034                                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && (mtex->mapto & MAP_COLSPEC)) {
1035                                         GPUNodeLink *colspecfac;
1036
1037                                         if(mtex->colspecfac == 1.0f) colspecfac = stencil;
1038                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->colspecfac), stencil, &colspecfac);
1039
1040                                         texture_rgb_blend(mat, tcol, shi->specrgb, tin, colspecfac, mtex->blendtype, &shi->specrgb);
1041                                 }
1042                         }
1043
1044                         if(!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && (mtex->mapto & MAP_NORM)) {
1045                                 if(tex->type==TEX_IMAGE) {
1046                                         if(tex->imaflag & TEX_NORMALMAP) {
1047                                                 /* normalmap image */
1048                                                 GPU_link(mat, "mtex_normal", texco, GPU_image(tex->ima, &tex->iuser), &tnor );
1049                                                 
1050                                                 if(mtex->norfac < 0.0f)
1051                                                         GPU_link(mat, "mtex_negate_texnormal", tnor, &tnor);
1052
1053                                                 if(mtex->normapspace == MTEX_NSPACE_TANGENT)
1054                                                 {
1055                                                         if(iFirstTimeNMap!=0)
1056                                                         {
1057                                                                 // use unnormalized normal (this is how we bake it - closer to gamedev)
1058                                                                 GPUNodeLink *vNegNorm;
1059                                                                 GPU_link(mat, "vec_math_negate", GPU_builtin(GPU_VIEW_NORMAL), &vNegNorm);
1060                                                                 GPU_link(mat, "mtex_nspace_tangent", GPU_attribute(CD_TANGENT, ""), vNegNorm, tnor, &newnor);
1061                                                                 iFirstTimeNMap = 0;
1062                                                         }
1063                                                         else    // otherwise use accumulated perturbations
1064                                                         {
1065                                                                 GPU_link(mat, "mtex_nspace_tangent", GPU_attribute(CD_TANGENT, ""), shi->vn, tnor, &newnor);
1066                                                         }
1067                                                 }
1068                                                 else
1069                                                         newnor = tnor;
1070                                                 
1071                                                 norfac = MIN2(fabsf(mtex->norfac), 1.0f);
1072                                                 
1073                                                 if(norfac == 1.0f && !GPU_link_changed(stencil)) {
1074                                                         shi->vn = newnor;
1075                                                 }
1076                                                 else {
1077                                                         tnorfac = GPU_uniform(&norfac);
1078         
1079                                                         if(GPU_link_changed(stencil))
1080                                                                 GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
1081         
1082                                                         GPU_link(mat, "mtex_blend_normal", tnorfac, shi->vn, newnor, &shi->vn);
1083                                                 }
1084                                                 
1085                                         } else if( mtex->texflag & (MTEX_3TAP_BUMP|MTEX_5TAP_BUMP)) {
1086                                                 /* ntap bumpmap image */
1087                                                 int iBumpSpace;
1088                                                 float hScale = 0.1f; // compatibility adjustment factor for all bumpspace types
1089                                                 float hScaleTex = 13.0f; // factor for scaling texspace bumps
1090                                                 
1091                                                 GPUNodeLink *surf_pos = GPU_builtin(GPU_VIEW_POSITION);
1092                                                 GPUNodeLink *vR1, *vR2;
1093                                                 GPUNodeLink *dBs, *dBt, *fDet;
1094                                                 
1095                                                 if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
1096                                                         hScale = hScaleTex;
1097                                                 norfac = hScale * mtex->norfac;
1098                                                 tnorfac = GPU_uniform(&norfac);
1099                                                 
1100                                                 if(GPU_link_changed(stencil))
1101                                                         GPU_link(mat, "math_multiply", tnorfac, stencil, &tnorfac);
1102                                                 
1103                                                 if( !init_done ) {
1104                                                         // copy shi->vn to vNorg and vNacc, set magnitude to 1
1105                                                         GPU_link(mat, "mtex_bump_normals_init", shi->vn, &vNorg, &vNacc, &fPrevMagnitude);
1106                                                         iBumpSpacePrev = 0;
1107                                                         init_done = 1;
1108                                                 }
1109                                                 
1110                                                 // find current bump space
1111                                                 if( mtex->texflag & MTEX_BUMP_OBJECTSPACE )
1112                                                         iBumpSpace = 1;
1113                                                 else if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
1114                                                         iBumpSpace = 2;
1115                                                 else
1116                                                         iBumpSpace = 4; // ViewSpace
1117                                                 
1118                                                 // re-initialize if bump space changed
1119                                                 if( iBumpSpacePrev != iBumpSpace ) {
1120                                                         
1121                                                         if( mtex->texflag & MTEX_BUMP_OBJECTSPACE ) 
1122                                                                 GPU_link( mat, "mtex_bump_init_objspace",
1123                                                                                   surf_pos, vNorg, 
1124                                                                           GPU_builtin(GPU_VIEW_MATRIX), GPU_builtin(GPU_INVERSE_VIEW_MATRIX), GPU_builtin(GPU_OBJECT_MATRIX),  GPU_builtin(GPU_INVERSE_OBJECT_MATRIX), 
1125                                                                           fPrevMagnitude, vNacc,
1126                                                                                   &fPrevMagnitude, &vNacc, 
1127                                                                           &vR1, &vR2, &fDet );
1128                                                                 
1129                                                         else if( mtex->texflag & MTEX_BUMP_TEXTURESPACE )
1130                                                                 GPU_link( mat, "mtex_bump_init_texturespace",
1131                                                                                   surf_pos, vNorg, 
1132                                                                           fPrevMagnitude, vNacc,
1133                                                                                   &fPrevMagnitude, &vNacc, 
1134                                                                           &vR1, &vR2, &fDet );
1135                                                                 
1136                                                         else
1137                                                                 GPU_link( mat, "mtex_bump_init_viewspace",
1138                                                                                   surf_pos, vNorg, 
1139                                                                           fPrevMagnitude, vNacc,
1140                                                                                   &fPrevMagnitude, &vNacc, 
1141                                                                           &vR1, &vR2, &fDet );
1142                                                         
1143                                                         iBumpSpacePrev = iBumpSpace;
1144                                                 }
1145                                                 
1146                                                 
1147                                                 if( mtex->texflag & MTEX_3TAP_BUMP )
1148                                                         GPU_link( mat, "mtex_bump_tap3", 
1149                                                                   texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
1150                                                                   &dBs, &dBt );
1151                                                 else
1152                                                         GPU_link( mat, "mtex_bump_tap5", 
1153                                                                   texco, GPU_image(tex->ima, &tex->iuser), tnorfac,
1154                                                                   &dBs, &dBt );
1155                                                 
1156                                                 
1157                                                 if( mtex->texflag & MTEX_BUMP_TEXTURESPACE ) {
1158                                                         float ima_x= 512.0f, ima_y= 512.f;              // prevent calling textureSize, glsl 1.3 only
1159                                                         ImBuf *ibuf= BKE_image_get_ibuf(tex->ima, &tex->iuser);
1160                                                         if(ibuf) {
1161                                                                 ima_x= ibuf->x;
1162                                                                 ima_y= ibuf->y;
1163                                                         }
1164                                                         
1165                                                         GPU_link( mat, "mtex_bump_apply_texspace",
1166                                                                   fDet, dBs, dBt, vR1, vR2, 
1167                                                                   GPU_image(tex->ima, &tex->iuser), texco, GPU_uniform(&ima_x), GPU_uniform(&ima_y), vNacc,
1168                                                                   &vNacc, &shi->vn );
1169                                                 } else
1170                                                         GPU_link( mat, "mtex_bump_apply",
1171                                                                   fDet, dBs, dBt, vR1, vR2, vNacc,
1172                                                                   &vNacc, &shi->vn );
1173                                                 
1174                                         }
1175                                 }
1176                                 
1177                                 GPU_link(mat, "vec_math_negate", shi->vn, &orn);
1178                         }
1179
1180                         if((mtex->mapto & MAP_VARS)) {
1181                                 if(rgbnor & TEX_RGB) {
1182                                         if(talpha)
1183                                                 GPU_link(mat, "mtex_alpha_from_col", trgb, &tin);
1184                                         else
1185                                                 GPU_link(mat, "mtex_rgbtoint", trgb, &tin);
1186                                 }
1187
1188                                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_REF) {
1189                                         GPUNodeLink *difffac;
1190
1191                                         if(mtex->difffac == 1.0f) difffac = stencil;
1192                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->difffac), stencil, &difffac);
1193
1194                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->refl, tin, difffac, mtex->blendtype, &shi->refl);
1195                                         GPU_link(mat, "mtex_value_clamp_positive", shi->refl, &shi->refl);
1196                                 }
1197                                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_SPEC) {
1198                                         GPUNodeLink *specfac;
1199
1200                                         if(mtex->specfac == 1.0f) specfac = stencil;
1201                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->specfac), stencil, &specfac);
1202
1203                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->spec, tin, specfac, mtex->blendtype, &shi->spec);
1204                                         GPU_link(mat, "mtex_value_clamp_positive", shi->spec, &shi->spec);
1205                                 }
1206                                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_EMIT) {
1207                                         GPUNodeLink *emitfac;
1208
1209                                         if(mtex->emitfac == 1.0f) emitfac = stencil;
1210                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->emitfac), stencil, &emitfac);
1211
1212                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->emit, tin, emitfac, mtex->blendtype, &shi->emit);
1213                                         GPU_link(mat, "mtex_value_clamp_positive", shi->emit, &shi->emit);
1214                                 }
1215                                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_HAR) {
1216                                         GPUNodeLink *hardfac;
1217
1218                                         if(mtex->hardfac == 1.0f) hardfac = stencil;
1219                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->hardfac), stencil, &hardfac);
1220
1221                                         GPU_link(mat, "mtex_har_divide", shi->har, &shi->har);
1222                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->har, tin, hardfac, mtex->blendtype, &shi->har);
1223                                         GPU_link(mat, "mtex_har_multiply_clamp", shi->har, &shi->har);
1224                                 }
1225                                 if(mtex->mapto & MAP_ALPHA) {
1226                                         GPUNodeLink *alphafac;
1227
1228                                         if(mtex->alphafac == 1.0f) alphafac = stencil;
1229                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->alphafac), stencil, &alphafac);
1230
1231                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->alpha, tin, alphafac, mtex->blendtype, &shi->alpha);
1232                                         GPU_link(mat, "mtex_value_clamp", shi->alpha, &shi->alpha);
1233                                 }
1234                                 if(!(mat->scene->gm.flag & GAME_GLSL_NO_EXTRA_TEX) && mtex->mapto & MAP_AMB) {
1235                                         GPUNodeLink *ambfac;
1236
1237                                         if(mtex->ambfac == 1.0f) ambfac = stencil;
1238                                         else GPU_link(mat, "math_multiply", GPU_uniform(&mtex->ambfac), stencil, &ambfac);
1239
1240                                         texture_value_blend(mat, GPU_uniform(&mtex->def_var), shi->amb, tin, ambfac, mtex->blendtype, &shi->amb);
1241                                         GPU_link(mat, "mtex_value_clamp", shi->amb, &shi->amb);
1242                                 }
1243                         }
1244                 }
1245         }
1246 }
1247
1248 void GPU_shadeinput_set(GPUMaterial *mat, Material *ma, GPUShadeInput *shi)
1249 {
1250         float hard = ma->har;
1251
1252         memset(shi, 0, sizeof(*shi));
1253
1254         shi->gpumat = mat;
1255         shi->mat = ma;
1256
1257         GPU_link(mat, "set_rgb", GPU_uniform(&ma->r), &shi->rgb);
1258         GPU_link(mat, "set_rgb", GPU_uniform(&ma->specr), &shi->specrgb);
1259         GPU_link(mat, "shade_norm", GPU_builtin(GPU_VIEW_NORMAL), &shi->vn);
1260         GPU_link(mat, "set_value", GPU_uniform(&ma->alpha), &shi->alpha);
1261         GPU_link(mat, "set_value", GPU_uniform(&ma->ref), &shi->refl);
1262         GPU_link(mat, "set_value", GPU_uniform(&ma->spec), &shi->spec);
1263         GPU_link(mat, "set_value", GPU_uniform(&ma->emit), &shi->emit);
1264         GPU_link(mat, "set_value", GPU_uniform(&hard), &shi->har);
1265         GPU_link(mat, "set_value", GPU_uniform(&ma->amb), &shi->amb);
1266         GPU_link(mat, "shade_view", GPU_builtin(GPU_VIEW_POSITION), &shi->view);
1267         GPU_link(mat, "vcol_attribute", GPU_attribute(CD_MCOL, ""), &shi->vcol);
1268         if(gpu_do_color_management(mat))
1269                 GPU_link(mat, "srgb_to_linearrgb", shi->vcol, &shi->vcol);
1270         GPU_link(mat, "texco_refl", shi->vn, shi->view, &shi->ref);
1271 }
1272
1273 void GPU_shaderesult_set(GPUShadeInput *shi, GPUShadeResult *shr)
1274 {
1275         GPUMaterial *mat= shi->gpumat;
1276         GPUNodeLink *emit, *ulinfac, *ulogfac, *mistfac;
1277         Material *ma= shi->mat;
1278         World *world= mat->scene->world;
1279         float linfac, logfac, misttype;
1280
1281         memset(shr, 0, sizeof(*shr));
1282
1283         if(ma->mode & MA_VERTEXCOLP)
1284                 shi->rgb = shi->vcol;
1285
1286         do_material_tex(shi);
1287
1288         if((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))
1289                 GPU_material_enable_alpha(mat);
1290
1291         if((mat->scene->gm.flag & GAME_GLSL_NO_LIGHTS) || (ma->mode & MA_SHLESS)) {
1292                 shr->combined = shi->rgb;
1293                 shr->alpha = shi->alpha;
1294                 GPU_link(mat, "set_rgb", shi->rgb, &shr->diff);
1295                 GPU_link(mat, "set_rgb_zero", &shr->spec);
1296         }
1297         else {
1298                 if(GPU_link_changed(shi->emit) || ma->emit != 0.0f) {
1299                         if((ma->mode & (MA_VERTEXCOL|MA_VERTEXCOLP))== MA_VERTEXCOL) {
1300                                 GPU_link(mat, "shade_add", shi->emit, shi->vcol, &emit);
1301                                 GPU_link(mat, "shade_mul", emit, shi->rgb, &shr->diff);
1302                         }
1303                         else
1304                                 GPU_link(mat, "shade_mul_value", shi->emit, shi->rgb, &shr->diff);
1305                 }
1306                 else
1307                         GPU_link(mat, "set_rgb_zero", &shr->diff);
1308
1309                 GPU_link(mat, "set_rgb_zero", &shr->spec);
1310
1311                 material_lights(shi, shr);
1312
1313                 shr->combined = shr->diff;
1314                 shr->alpha = shi->alpha;
1315
1316                 if(world) {
1317                         /* exposure correction */
1318                         if(world->exp!=0.0f || world->range!=1.0f) {
1319                                 linfac= 1.0 + pow((2.0*world->exp + 0.5), -10);
1320                                 logfac= log((linfac-1.0f)/linfac)/world->range;
1321
1322                                 GPU_link(mat, "set_value", GPU_uniform(&linfac), &ulinfac);
1323                                 GPU_link(mat, "set_value", GPU_uniform(&logfac), &ulogfac);
1324
1325                                 GPU_link(mat, "shade_exposure_correct", shr->combined,
1326                                         ulinfac, ulogfac, &shr->combined);
1327                                 GPU_link(mat, "shade_exposure_correct", shr->spec,
1328                                         ulinfac, ulogfac, &shr->spec);
1329                         }
1330
1331                         /* ambient color */
1332                         if(world->ambr!=0.0f || world->ambg!=0.0f || world->ambb!=0.0f) {
1333                                 if(GPU_link_changed(shi->amb) || ma->amb != 0.0f)
1334                                         GPU_link(mat, "shade_maddf", shr->combined, GPU_uniform(&ma->amb),
1335                                                 GPU_uniform(&world->ambr), &shr->combined);
1336                         }
1337                 }
1338
1339                 if(ma->mode & MA_RAMP_COL) ramp_diffuse_result(shi, &shr->combined);
1340                 if(ma->mode & MA_RAMP_SPEC) ramp_spec_result(shi, &shr->spec);
1341
1342                 if(GPU_link_changed(shi->spec) || ma->spec != 0.0f)
1343                         GPU_link(mat, "shade_add", shr->combined, shr->spec, &shr->combined);
1344         }
1345
1346         GPU_link(mat, "mtex_alpha_to_col", shr->combined, shr->alpha, &shr->combined);
1347
1348         if(ma->shade_flag & MA_OBCOLOR)
1349                 GPU_link(mat, "shade_obcolor", shr->combined, GPU_builtin(GPU_OBCOLOR), &shr->combined);
1350
1351         if(world && (world->mode & WO_MIST) && !(ma->mode & MA_NOMIST)) {
1352                 misttype = world->mistype;
1353
1354                 GPU_link(mat, "shade_mist_factor", GPU_builtin(GPU_VIEW_POSITION),
1355                         GPU_uniform(&world->miststa), GPU_uniform(&world->mistdist),
1356                         GPU_uniform(&misttype), GPU_uniform(&world->misi), &mistfac);
1357
1358                 GPU_link(mat, "mix_blend", mistfac, shr->combined,
1359                         GPU_uniform(&world->horr), &shr->combined);
1360         }
1361
1362         if(!((ma->mode & MA_TRANSP) && (ma->mode & MA_ZTRANSP))) {
1363                 if(world && (GPU_link_changed(shr->alpha) || ma->alpha != 1.0f))
1364                         GPU_link(mat, "shade_world_mix", GPU_uniform(&world->horr),
1365                                 shr->combined, &shr->combined);
1366
1367                 GPU_link(mat, "shade_alpha_opaque", shr->combined, &shr->combined);
1368         }
1369
1370         if(ma->shade_flag & MA_OBCOLOR) {
1371                 mat->obcolalpha = 1;
1372                 GPU_link(mat, "shade_alpha_obcolor", shr->combined, GPU_builtin(GPU_OBCOLOR), &shr->combined);
1373         }
1374
1375         if(gpu_do_color_management(mat))
1376                 GPU_link(mat, "linearrgb_to_srgb", shr->combined, &shr->combined);
1377 }
1378
1379 static GPUNodeLink *GPU_blender_material(GPUMaterial *mat, Material *ma)
1380 {
1381         GPUShadeInput shi;
1382         GPUShadeResult shr;
1383
1384         GPU_shadeinput_set(mat, ma, &shi);
1385         GPU_shaderesult_set(&shi, &shr);
1386
1387         return shr.combined;
1388 }
1389
1390 GPUMaterial *GPU_material_from_blender(Scene *scene, Material *ma)
1391 {
1392         GPUMaterial *mat;
1393         GPUNodeLink *outlink;
1394         LinkData *link;
1395
1396         for(link=ma->gpumaterial.first; link; link=link->next)
1397                 if(((GPUMaterial*)link->data)->scene == scene)
1398                         return link->data;
1399
1400         mat = GPU_material_construct_begin(ma);
1401         mat->scene = scene;
1402
1403         if(!(scene->gm.flag & GAME_GLSL_NO_NODES) && ma->nodetree && ma->use_nodes) {
1404                 ntreeGPUMaterialNodes(ma->nodetree, mat);
1405         }
1406         else {
1407                 outlink = GPU_blender_material(mat, ma);
1408                 GPU_material_output_link(mat, outlink);
1409         }
1410
1411         /*if(!GPU_material_construct_end(mat)) {
1412                 GPU_material_free(mat);
1413                 mat= NULL;
1414                 return 0;
1415         }*/
1416
1417         GPU_material_construct_end(mat);
1418
1419         link = MEM_callocN(sizeof(LinkData), "GPUMaterialLink");
1420         link->data = mat;
1421         BLI_addtail(&ma->gpumaterial, link);
1422
1423         return mat;
1424 }
1425
1426 void GPU_materials_free(void)
1427 {
1428         Object *ob;
1429         Material *ma;
1430         extern Material defmaterial;
1431
1432         for(ma=G.main->mat.first; ma; ma=ma->id.next)
1433                 GPU_material_free(ma);
1434
1435         GPU_material_free(&defmaterial);
1436
1437         for(ob=G.main->object.first; ob; ob=ob->id.next)
1438                 GPU_lamp_free(ob);
1439 }
1440
1441 /* Lamps and shadow buffers */
1442
1443 void GPU_lamp_update(GPULamp *lamp, int lay, int hide, float obmat[][4])
1444 {
1445         float mat[4][4];
1446
1447         lamp->lay = lay;
1448         lamp->hide = hide;
1449
1450         copy_m4_m4(mat, obmat);
1451         normalize_m4(mat);
1452
1453         VECCOPY(lamp->vec, mat[2]);
1454         VECCOPY(lamp->co, mat[3]);
1455         copy_m4_m4(lamp->obmat, mat);
1456         invert_m4_m4(lamp->imat, mat);
1457 }
1458
1459 void GPU_lamp_update_colors(GPULamp *lamp, float r, float g, float b, float energy)
1460 {
1461         lamp->energy = energy;
1462         if(lamp->mode & LA_NEG) lamp->energy= -lamp->energy;
1463
1464         lamp->col[0]= r* lamp->energy;
1465         lamp->col[1]= g* lamp->energy;
1466         lamp->col[2]= b* lamp->energy;
1467 }
1468
1469 static void gpu_lamp_from_blender(Scene *scene, Object *ob, Object *par, Lamp *la, GPULamp *lamp)
1470 {
1471         float temp, angle, pixsize, wsize;
1472
1473         lamp->scene = scene;
1474         lamp->ob = ob;
1475         lamp->par = par;
1476         lamp->la = la;
1477
1478         /* add_render_lamp */
1479         lamp->mode = la->mode;
1480         lamp->type = la->type;
1481
1482         lamp->energy = la->energy;
1483         if(lamp->mode & LA_NEG) lamp->energy= -lamp->energy;
1484
1485         lamp->col[0]= la->r*lamp->energy;
1486         lamp->col[1]= la->g*lamp->energy;
1487         lamp->col[2]= la->b*lamp->energy;
1488
1489         GPU_lamp_update(lamp, ob->lay, (ob->restrictflag & OB_RESTRICT_RENDER), ob->obmat);
1490
1491         lamp->spotsi= la->spotsize;
1492         if(lamp->mode & LA_HALO)
1493                 if(lamp->spotsi > 170.0f)
1494                         lamp->spotsi = 170.0f;
1495         lamp->spotsi= cos(M_PI*lamp->spotsi/360.0);
1496         lamp->spotbl= (1.0f - lamp->spotsi)*la->spotblend;
1497         lamp->k= la->k;
1498
1499         lamp->dist= la->dist;
1500         lamp->falloff_type= la->falloff_type;
1501         lamp->att1= la->att1;
1502         lamp->att2= la->att2;
1503         lamp->curfalloff= la->curfalloff;
1504
1505         /* initshadowbuf */
1506         lamp->bias = 0.02f*la->bias;
1507         lamp->size = la->bufsize;
1508         lamp->d= la->clipsta;
1509         lamp->clipend= la->clipend;
1510
1511         /* arbitrary correction for the fact we do no soft transition */
1512         lamp->bias *= 0.25f;
1513
1514         /* makeshadowbuf */
1515         angle= saacos(lamp->spotsi);
1516         temp= 0.5f*lamp->size*cos(angle)/sin(angle);
1517         pixsize= (lamp->d)/temp;
1518         wsize= pixsize*0.5f*lamp->size;
1519                 
1520         perspective_m4( lamp->winmat,-wsize, wsize, -wsize, wsize, lamp->d, lamp->clipend);
1521 }
1522
1523 static void gpu_lamp_shadow_free(GPULamp *lamp)
1524 {
1525         if(lamp->tex) {
1526                 GPU_texture_free(lamp->tex);
1527                 lamp->tex= NULL;
1528         }
1529         if(lamp->fb) {
1530                 GPU_framebuffer_free(lamp->fb);
1531                 lamp->fb= NULL;
1532         }
1533 }
1534
1535 GPULamp *GPU_lamp_from_blender(Scene *scene, Object *ob, Object *par)
1536 {
1537         Lamp *la;
1538         GPULamp *lamp;
1539         LinkData *link;
1540
1541         for(link=ob->gpulamp.first; link; link=link->next) {
1542                 lamp = (GPULamp*)link->data;
1543
1544                 if(lamp->par == par && lamp->scene == scene)
1545                         return link->data;
1546         }
1547
1548         lamp = MEM_callocN(sizeof(GPULamp), "GPULamp");
1549
1550         link = MEM_callocN(sizeof(LinkData), "GPULampLink");
1551         link->data = lamp;
1552         BLI_addtail(&ob->gpulamp, link);
1553
1554         la = ob->data;
1555         gpu_lamp_from_blender(scene, ob, par, la, lamp);
1556
1557         if(la->type==LA_SPOT && (la->mode & LA_SHAD_BUF)) {
1558                 /* opengl */
1559                 lamp->fb = GPU_framebuffer_create();
1560                 if(!lamp->fb) {
1561                         gpu_lamp_shadow_free(lamp);
1562                         return lamp;
1563                 }
1564
1565                 lamp->tex = GPU_texture_create_depth(lamp->size, lamp->size, NULL);
1566                 if(!lamp->tex) {
1567                         gpu_lamp_shadow_free(lamp);
1568                         return lamp;
1569                 }
1570
1571                 if(!GPU_framebuffer_texture_attach(lamp->fb, lamp->tex, NULL)) {
1572                         gpu_lamp_shadow_free(lamp);
1573                         return lamp;
1574                 }
1575
1576                 GPU_framebuffer_restore();
1577         }
1578
1579         return lamp;
1580 }
1581
1582 void GPU_lamp_free(Object *ob)
1583 {
1584         GPULamp *lamp;
1585         LinkData *link;
1586         LinkData *nlink;
1587         Material *ma;
1588
1589         for(link=ob->gpulamp.first; link; link=link->next) {
1590                 lamp = link->data;
1591
1592                 while(lamp->materials.first) {
1593                         nlink = lamp->materials.first;
1594                         ma = nlink->data;
1595                         BLI_freelinkN(&lamp->materials, nlink);
1596
1597                         if(ma->gpumaterial.first)
1598                                 GPU_material_free(ma);
1599                 }
1600
1601                 gpu_lamp_shadow_free(lamp);
1602
1603                 MEM_freeN(lamp);
1604         }
1605
1606         BLI_freelistN(&ob->gpulamp);
1607 }
1608
1609 int GPU_lamp_has_shadow_buffer(GPULamp *lamp)
1610 {
1611         return (!(lamp->scene->gm.flag & GAME_GLSL_NO_SHADOWS) &&
1612                         !(lamp->scene->gm.flag & GAME_GLSL_NO_LIGHTS) &&
1613                         lamp->tex && lamp->fb);
1614 }
1615
1616 void GPU_lamp_shadow_buffer_bind(GPULamp *lamp, float viewmat[][4], int *winsize, float winmat[][4])
1617 {
1618         float rangemat[4][4], persmat[4][4];
1619
1620         /* initshadowbuf */
1621         invert_m4_m4(lamp->viewmat, lamp->obmat);
1622         normalize_v3(lamp->viewmat[0]);
1623         normalize_v3(lamp->viewmat[1]);
1624         normalize_v3(lamp->viewmat[2]);
1625
1626         /* makeshadowbuf */
1627         mul_m4_m4m4(persmat, lamp->viewmat, lamp->winmat);
1628
1629         /* opengl depth buffer is range 0.0..1.0 instead of -1.0..1.0 in blender */
1630         unit_m4(rangemat);
1631         rangemat[0][0] = 0.5f;
1632         rangemat[1][1] = 0.5f;
1633         rangemat[2][2] = 0.5f;
1634         rangemat[3][0] = 0.5f;
1635         rangemat[3][1] = 0.5f;
1636         rangemat[3][2] = 0.5f;
1637
1638         mul_m4_m4m4(lamp->persmat, persmat, rangemat);
1639
1640         /* opengl */
1641         glDisable(GL_SCISSOR_TEST);
1642         GPU_framebuffer_texture_bind(lamp->fb, lamp->tex);
1643
1644         /* set matrices */
1645         copy_m4_m4(viewmat, lamp->viewmat);
1646         copy_m4_m4(winmat, lamp->winmat);
1647         *winsize = lamp->size;
1648 }
1649
1650 void GPU_lamp_shadow_buffer_unbind(GPULamp *lamp)
1651 {
1652         GPU_framebuffer_texture_unbind(lamp->fb, lamp->tex);
1653         GPU_framebuffer_restore();
1654         glEnable(GL_SCISSOR_TEST);
1655 }
1656
1657 int GPU_lamp_shadow_layer(GPULamp *lamp)
1658 {
1659         if(lamp->fb && lamp->tex && (lamp->mode & (LA_LAYER|LA_LAYER_SHADOW)))
1660                 return lamp->lay;
1661         else
1662                 return -1;
1663 }
1664