Ghost Context Refactor
[blender-staging.git] / source / blender / gpu / intern / gpu_draw.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) 2005 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_draw.c
29  *  \ingroup gpu
30  *
31  * Utility functions for dealing with OpenGL texture & material context,
32  * mipmap generation and light objects.
33  *
34  * These are some obscure rendering functions shared between the
35  * game engine and the blender, in this module to avoid duplication
36  * and abstract them away from the rest a bit.
37  */
38
39 #include <string.h>
40
41 #include "GPU_glew.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_linklist.h"
45 #include "BLI_math.h"
46 #include "BLI_threads.h"
47 #include "BLI_utildefines.h"
48
49 #include "DNA_lamp_types.h"
50 #include "DNA_material_types.h"
51 #include "DNA_meshdata_types.h"
52 #include "DNA_modifier_types.h"
53 #include "DNA_node_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_scene_types.h"
56 #include "DNA_smoke_types.h"
57 #include "DNA_view3d_types.h"
58
59 #include "MEM_guardedalloc.h"
60
61 #include "IMB_imbuf.h"
62 #include "IMB_imbuf_types.h"
63
64 #include "BKE_bmfont.h"
65 #include "BKE_global.h"
66 #include "BKE_image.h"
67 #include "BKE_main.h"
68 #include "BKE_material.h"
69 #include "BKE_node.h"
70 #include "BKE_object.h"
71 #include "BKE_scene.h"
72 #include "BKE_DerivedMesh.h"
73
74 #include "GPU_buffers.h"
75 #include "GPU_draw.h"
76 #include "GPU_extensions.h"
77 #include "GPU_material.h"
78
79 #include "PIL_time.h"
80
81 #include "smoke_API.h"
82
83 extern Material defmaterial; /* from material.c */
84
85 /* Text Rendering */
86
87 static void gpu_mcol(unsigned int ucol)
88 {
89         /* mcol order is swapped */
90         const char *cp= (char *)&ucol;
91         glColor3ub(cp[3], cp[2], cp[1]);
92 }
93
94 void GPU_render_text(MTFace *tface, int mode,
95         const char *textstr, int textlen, unsigned int *col,
96         float *v1, float *v2, float *v3, float *v4, int glattrib)
97 {
98         if ((mode & GEMAT_TEXT) && (textlen>0) && tface->tpage) {
99                 Image* ima = (Image *)tface->tpage;
100                 ImBuf *first_ibuf;
101                 const size_t textlen_st = textlen;
102                 size_t index;
103                 float centerx, centery, sizex, sizey, transx, transy, movex, movey, advance;
104                 float advance_tab;
105                 
106                 /* multiline */
107                 float line_start= 0.0f, line_height;
108                 
109                 if (v4)
110                         line_height = max_ffff(v1[1], v2[1], v3[1], v4[2]) - min_ffff(v1[1], v2[1], v3[1], v4[2]);
111                 else
112                         line_height = max_fff(v1[1], v2[1], v3[1]) - min_fff(v1[1], v2[1], v3[1]);
113                 line_height *= 1.2f; /* could be an option? */
114                 /* end multiline */
115
116                 
117                 /* color has been set */
118                 if (tface->mode & TF_OBCOL)
119                         col= NULL;
120                 else if (!col)
121                         glColor3f(1.0f, 1.0f, 1.0f);
122
123                 glPushMatrix();
124                 
125                 /* get the tab width */
126                 first_ibuf = BKE_image_get_first_ibuf(ima);
127                 matrixGlyph(first_ibuf, ' ', &centerx, &centery,
128                         &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
129                 
130                 advance_tab= advance * 4; /* tab width could also be an option */
131                 
132                 
133                 for (index = 0; index < textlen_st; ) {
134                         unsigned int character;
135                         float uv[4][2];
136
137                         // lets calculate offset stuff
138                         character = BLI_str_utf8_as_unicode_and_size_safe(textstr + index, &index);
139                         
140                         if (character=='\n') {
141                                 glTranslatef(line_start, -line_height, 0.0);
142                                 line_start = 0.0f;
143                                 continue;
144                         }
145                         else if (character=='\t') {
146                                 glTranslatef(advance_tab, 0.0, 0.0);
147                                 line_start -= advance_tab; /* so we can go back to the start of the line */
148                                 continue;
149                                 
150                         }
151                         else if (character > USHRT_MAX) {
152                                 /* not much we can do here bmfonts take ushort */
153                                 character = '?';
154                         }
155                         
156                         // space starts at offset 1
157                         // character = character - ' ' + 1;
158                         matrixGlyph(first_ibuf, character, & centerx, &centery,
159                                 &sizex, &sizey, &transx, &transy, &movex, &movey, &advance);
160
161                         uv[0][0] = (tface->uv[0][0] - centerx) * sizex + transx;
162                         uv[0][1] = (tface->uv[0][1] - centery) * sizey + transy;
163                         uv[1][0] = (tface->uv[1][0] - centerx) * sizex + transx;
164                         uv[1][1] = (tface->uv[1][1] - centery) * sizey + transy;
165                         uv[2][0] = (tface->uv[2][0] - centerx) * sizex + transx;
166                         uv[2][1] = (tface->uv[2][1] - centery) * sizey + transy;
167                         
168                         glBegin(GL_POLYGON);
169                         if (glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[0]);
170                         else glTexCoord2fv(uv[0]);
171                         if (col) gpu_mcol(col[0]);
172                         glVertex3f(sizex * v1[0] + movex, sizey * v1[1] + movey, v1[2]);
173                         
174                         if (glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[1]);
175                         else glTexCoord2fv(uv[1]);
176                         if (col) gpu_mcol(col[1]);
177                         glVertex3f(sizex * v2[0] + movex, sizey * v2[1] + movey, v2[2]);
178
179                         if (glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[2]);
180                         else glTexCoord2fv(uv[2]);
181                         if (col) gpu_mcol(col[2]);
182                         glVertex3f(sizex * v3[0] + movex, sizey * v3[1] + movey, v3[2]);
183
184                         if (v4) {
185                                 uv[3][0] = (tface->uv[3][0] - centerx) * sizex + transx;
186                                 uv[3][1] = (tface->uv[3][1] - centery) * sizey + transy;
187
188                                 if (glattrib >= 0) glVertexAttrib2fvARB(glattrib, uv[3]);
189                                 else glTexCoord2fv(uv[3]);
190                                 if (col) gpu_mcol(col[3]);
191                                 glVertex3f(sizex * v4[0] + movex, sizey * v4[1] + movey, v4[2]);
192                         }
193                         glEnd();
194
195                         glTranslatef(advance, 0.0, 0.0);
196                         line_start -= advance; /* so we can go back to the start of the line */
197                 }
198                 glPopMatrix();
199
200                 BKE_image_release_ibuf(ima, first_ibuf, NULL);
201         }
202 }
203
204 /* Checking powers of two for images since opengl 1.x requires it */
205
206 static bool is_power_of_2_resolution(int w, int h)
207 {
208         return is_power_of_2_i(w) && is_power_of_2_i(h);
209 }
210
211 static bool is_over_resolution_limit(int w, int h)
212 {
213         int reslimit = (U.glreslimit != 0)?
214                 min_ii(U.glreslimit, GPU_max_texture_size()) :
215                 GPU_max_texture_size();
216
217         return (w > reslimit || h > reslimit);
218 }
219
220 static int smaller_power_of_2_limit(int num)
221 {
222         int reslimit = (U.glreslimit != 0)?
223                 min_ii(U.glreslimit, GPU_max_texture_size()) :
224                 GPU_max_texture_size();
225         /* take texture clamping into account */
226         if (num > reslimit)
227                 return reslimit;
228
229         return power_of_2_min_i(num);
230 }
231
232 /* Current OpenGL state caching for GPU_set_tpage */
233
234 static struct GPUTextureState {
235         int curtile, tile;
236         int curtilemode, tilemode;
237         int curtileXRep, tileXRep;
238         int curtileYRep, tileYRep;
239         Image *ima, *curima;
240
241         int domipmap, linearmipmap;
242         int texpaint; /* store this so that new images created while texture painting won't be set to mipmapped */
243
244         int alphablend;
245         float anisotropic;
246         int gpu_mipmap;
247         MTFace *lasttface;
248 } GTS = {0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, 1, 0, 0, -1, 1.f, 0, NULL};
249
250 /* Mipmap settings */
251
252 void GPU_set_gpu_mipmapping(int gpu_mipmap)
253 {
254         int old_value = GTS.gpu_mipmap;
255
256         /* only actually enable if it's supported */
257         GTS.gpu_mipmap = gpu_mipmap && GLEW_EXT_framebuffer_object;
258
259         if (old_value != GTS.gpu_mipmap) {
260                 GPU_free_images();
261         }
262 }
263
264 static void gpu_generate_mipmap(GLenum target)
265 {
266         const bool is_ati = GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY);
267         int target_enabled = 0;
268
269         /* work around bug in ATI driver, need to have GL_TEXTURE_2D enabled
270          * http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation */
271         if (is_ati) {
272                 target_enabled = glIsEnabled(target);
273                 if (!target_enabled)
274                         glEnable(target);
275         }
276
277         glGenerateMipmapEXT(target);
278
279         if (is_ati && !target_enabled)
280                 glDisable(target);
281 }
282
283 void GPU_set_mipmap(int mipmap)
284 {
285         if (GTS.domipmap != (mipmap != 0)) {
286                 GPU_free_images();
287                 GTS.domipmap = mipmap != 0;
288         }
289 }
290
291 void GPU_set_linear_mipmap(int linear)
292 {
293         if (GTS.linearmipmap != (linear != 0)) {
294                 GPU_free_images();
295                 GTS.linearmipmap = linear != 0;
296         }
297 }
298
299 int GPU_get_mipmap(void)
300 {
301         return GTS.domipmap && !GTS.texpaint;
302 }
303
304 int GPU_get_linear_mipmap(void)
305 {
306         return GTS.linearmipmap;
307 }
308
309 static GLenum gpu_get_mipmap_filter(int mag)
310 {
311         /* linearmipmap is off by default *when mipmapping is off,
312          * use unfiltered display */
313         if (mag) {
314                 if (GTS.linearmipmap || GTS.domipmap)
315                         return GL_LINEAR;
316                 else
317                         return GL_NEAREST;
318         }
319         else {
320                 if (GTS.linearmipmap)
321                         return GL_LINEAR_MIPMAP_LINEAR;
322                 else if (GTS.domipmap)
323                         return GL_LINEAR_MIPMAP_NEAREST;
324                 else
325                         return GL_NEAREST;
326         }
327 }
328
329 /* Anisotropic filtering settings */
330 void GPU_set_anisotropic(float value)
331 {
332         if (GTS.anisotropic != value) {
333                 GPU_free_images();
334
335                 /* Clamp value to the maximum value the graphics card supports */
336                 if (value > GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)
337                         value = GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT;
338
339                 GTS.anisotropic = value;
340         }
341 }
342
343 float GPU_get_anisotropic(void)
344 {
345         return GTS.anisotropic;
346 }
347
348 /* Set OpenGL state for an MTFace */
349
350 static void gpu_make_repbind(Image *ima)
351 {
352         ImBuf *ibuf;
353         
354         ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
355         if (ibuf==NULL)
356                 return;
357
358         if (ima->repbind) {
359                 glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
360                 MEM_freeN(ima->repbind);
361                 ima->repbind= NULL;
362                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
363         }
364
365         ima->totbind= ima->xrep*ima->yrep;
366
367         if (ima->totbind>1)
368                 ima->repbind= MEM_callocN(sizeof(int)*ima->totbind, "repbind");
369
370         BKE_image_release_ibuf(ima, ibuf, NULL);
371 }
372
373 void GPU_clear_tpage(bool force)
374 {
375         if (GTS.lasttface==NULL && !force)
376                 return;
377         
378         GTS.lasttface= NULL;
379         GTS.curtile= 0;
380         GTS.curima= NULL;
381         if (GTS.curtilemode!=0) {
382                 glMatrixMode(GL_TEXTURE);
383                 glLoadIdentity();
384                 glMatrixMode(GL_MODELVIEW);
385         }
386         GTS.curtilemode= 0;
387         GTS.curtileXRep=0;
388         GTS.curtileYRep=0;
389         GTS.alphablend= -1;
390         
391         glDisable(GL_BLEND);
392         glDisable(GL_TEXTURE_2D);
393         glDisable(GL_TEXTURE_GEN_S);
394         glDisable(GL_TEXTURE_GEN_T);
395         glDisable(GL_ALPHA_TEST);
396 }
397
398 static void gpu_set_alpha_blend(GPUBlendMode alphablend)
399 {
400         if (alphablend == GPU_BLEND_SOLID) {
401                 glDisable(GL_BLEND);
402                 glDisable(GL_ALPHA_TEST);
403                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
404         }
405         else if (alphablend==GPU_BLEND_ADD) {
406                 glEnable(GL_BLEND);
407                 glBlendFunc(GL_ONE, GL_ONE);
408                 glDisable(GL_ALPHA_TEST);
409         }
410         else if (ELEM(alphablend, GPU_BLEND_ALPHA, GPU_BLEND_ALPHA_SORT)) {
411                 glEnable(GL_BLEND);
412
413                 /* for OpenGL render we use the alpha channel, this makes alpha blend correct */
414                 if (GLEW_VERSION_1_4)
415                         glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
416                 else
417                         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
418                 
419                 /* if U.glalphaclip == 1.0, some cards go bonkers...
420                  * turn off alpha test in this case */
421
422                 /* added after 2.45 to clip alpha */
423                 if (U.glalphaclip == 1.0f) {
424                         glDisable(GL_ALPHA_TEST);
425                 }
426                 else {
427                         glEnable(GL_ALPHA_TEST);
428                         glAlphaFunc(GL_GREATER, U.glalphaclip);
429                 }
430         }
431         else if (alphablend==GPU_BLEND_CLIP) {
432                 glDisable(GL_BLEND); 
433                 glEnable(GL_ALPHA_TEST);
434                 glAlphaFunc(GL_GREATER, 0.5f);
435         }
436 }
437
438 static void gpu_verify_alpha_blend(int alphablend)
439 {
440         /* verify alpha blending modes */
441         if (GTS.alphablend == alphablend)
442                 return;
443
444         gpu_set_alpha_blend(alphablend);
445         GTS.alphablend= alphablend;
446 }
447
448 static void gpu_verify_reflection(Image *ima)
449 {
450         if (ima && (ima->flag & IMA_REFLECT)) {
451                 /* enable reflection mapping */
452                 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
453                 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
454
455                 glEnable(GL_TEXTURE_GEN_S);
456                 glEnable(GL_TEXTURE_GEN_T);
457         }
458         else {
459                 /* disable reflection mapping */
460                 glDisable(GL_TEXTURE_GEN_S);
461                 glDisable(GL_TEXTURE_GEN_T);
462         }
463 }
464
465 int GPU_verify_image(Image *ima, ImageUser *iuser, int tftile, bool compare, bool mipmap, bool is_data)
466 {
467         ImBuf *ibuf = NULL;
468         unsigned int *bind = NULL;
469         int rectw, recth, tpx=0, tpy=0, y;
470         unsigned int *tilerect= NULL, *rect= NULL;
471         float *ftilerect= NULL, *frect = NULL;
472         float *srgb_frect = NULL;
473         short texwindx, texwindy, texwinsx, texwinsy;
474         /* flag to determine whether high resolution format is used */
475         bool use_high_bit_depth = false, do_color_management = false;
476
477         /* initialize tile mode and number of repeats */
478         GTS.ima = ima;
479         GTS.tilemode= (ima && (ima->tpageflag & (IMA_TILES|IMA_TWINANIM)));
480         GTS.tileXRep = 0;
481         GTS.tileYRep = 0;
482
483         /* setting current tile according to frame */
484         if (ima && (ima->tpageflag & IMA_TWINANIM))
485                 GTS.tile= ima->lastframe;
486         else
487                 GTS.tile= tftile;
488
489         GTS.tile = MAX2(0, GTS.tile);
490
491         if (ima) {
492                 GTS.tileXRep = ima->xrep;
493                 GTS.tileYRep = ima->yrep;
494         }
495
496         /* if same image & tile, we're done */
497         if (compare && ima == GTS.curima && GTS.curtile == GTS.tile &&
498             GTS.tilemode == GTS.curtilemode && GTS.curtileXRep == GTS.tileXRep &&
499             GTS.curtileYRep == GTS.tileYRep)
500         {
501                 return (ima != NULL);
502         }
503
504         /* if tiling mode or repeat changed, change texture matrix to fit */
505         if (GTS.tilemode!=GTS.curtilemode || GTS.curtileXRep!=GTS.tileXRep ||
506             GTS.curtileYRep != GTS.tileYRep)
507         {
508                 glMatrixMode(GL_TEXTURE);
509                 glLoadIdentity();
510
511                 if (ima && (ima->tpageflag & IMA_TILES))
512                         glScalef(ima->xrep, ima->yrep, 1.0);
513
514                 glMatrixMode(GL_MODELVIEW);
515         }
516
517         /* check if we have a valid image */
518         if (ima==NULL || ima->ok==0)
519                 return 0;
520
521         /* check if we have a valid image buffer */
522         ibuf= BKE_image_acquire_ibuf(ima, iuser, NULL);
523
524         if (ibuf==NULL)
525                 return 0;
526
527         if (ibuf->rect_float) {
528                 if (U.use_16bit_textures) {
529                         /* use high precision textures. This is relatively harmless because OpenGL gives us
530                          * a high precision format only if it is available */
531                         use_high_bit_depth = true;
532                 }
533                 /* we may skip this in high precision, but if not, we need to have a valid buffer here */
534                 else if (ibuf->userflags & IB_RECT_INVALID) {
535                         IMB_rect_from_float(ibuf);
536                 }
537
538                 /* TODO unneeded when float images are correctly treated as linear always */
539                 if (!is_data)
540                         do_color_management = true;
541
542                 if (ibuf->rect == NULL)
543                         IMB_rect_from_float(ibuf);
544         }
545
546         /* currently, tpage refresh is used by ima sequences */
547         if (ima->tpageflag & IMA_TPAGE_REFRESH) {
548                 GPU_free_image(ima);
549                 ima->tpageflag &= ~IMA_TPAGE_REFRESH;
550         }
551         
552         if (GTS.tilemode) {
553                 /* tiled mode */
554                 if (ima->repbind==NULL) gpu_make_repbind(ima);
555                 if (GTS.tile>=ima->totbind) GTS.tile= 0;
556                 
557                 /* this happens when you change repeat buttons */
558                 if (ima->repbind) bind= &ima->repbind[GTS.tile];
559                 else bind= &ima->bindcode;
560                 
561                 if (*bind==0) {
562                         
563                         texwindx= ibuf->x/ima->xrep;
564                         texwindy= ibuf->y/ima->yrep;
565                         
566                         if (GTS.tile>=ima->xrep*ima->yrep)
567                                 GTS.tile= ima->xrep*ima->yrep-1;
568         
569                         texwinsy= GTS.tile / ima->xrep;
570                         texwinsx= GTS.tile - texwinsy*ima->xrep;
571         
572                         texwinsx*= texwindx;
573                         texwinsy*= texwindy;
574         
575                         tpx= texwindx;
576                         tpy= texwindy;
577
578                         if (use_high_bit_depth) {
579                                 if (do_color_management) {
580                                         srgb_frect = MEM_mallocN(ibuf->x*ibuf->y*sizeof(float)*4, "floar_buf_col_cor");
581                                         IMB_buffer_float_from_float(srgb_frect, ibuf->rect_float,
582                                                 ibuf->channels, IB_PROFILE_SRGB, IB_PROFILE_LINEAR_RGB, true,
583                                                 ibuf->x, ibuf->y, ibuf->x, ibuf->x);
584                                         IMB_buffer_float_unpremultiply(srgb_frect, ibuf->x, ibuf->y);
585                                         /* clamp buffer colors to 1.0 to avoid artifacts due to glu for hdr images */
586                                         IMB_buffer_float_clamp(srgb_frect, ibuf->x, ibuf->y);
587                                         frect= srgb_frect + texwinsy*ibuf->x + texwinsx;
588                                 }
589                                 else
590                                         frect= ibuf->rect_float + texwinsy*ibuf->x + texwinsx;
591                         }
592                         else
593                                 rect= ibuf->rect + texwinsy*ibuf->x + texwinsx;
594                 }
595         }
596         else {
597                 /* regular image mode */
598                 bind= &ima->bindcode;
599
600                 if (*bind==0) {
601                         tpx= ibuf->x;
602                         tpy= ibuf->y;
603                         rect= ibuf->rect;
604                         if (use_high_bit_depth) {
605                                 if (do_color_management) {
606                                         frect = srgb_frect = MEM_mallocN(ibuf->x*ibuf->y*sizeof(*srgb_frect)*4, "floar_buf_col_cor");
607                                         IMB_buffer_float_from_float(srgb_frect, ibuf->rect_float,
608                                                         ibuf->channels, IB_PROFILE_SRGB, IB_PROFILE_LINEAR_RGB, true,
609                                                         ibuf->x, ibuf->y, ibuf->x, ibuf->x);
610                                         IMB_buffer_float_unpremultiply(srgb_frect, ibuf->x, ibuf->y);
611                                         /* clamp buffer colors to 1.0 to avoid artifacts due to glu for hdr images */
612                                         IMB_buffer_float_clamp(srgb_frect, ibuf->x, ibuf->y);
613                                 }
614                                 else
615                                         frect= ibuf->rect_float;
616                         }
617                 }
618         }
619
620         if (*bind != 0) {
621                 /* enable opengl drawing with textures */
622                 glBindTexture(GL_TEXTURE_2D, *bind);
623                 BKE_image_release_ibuf(ima, ibuf, NULL);
624                 return *bind;
625         }
626
627         rectw = tpx;
628         recth = tpy;
629
630         /* for tiles, copy only part of image into buffer */
631         if (GTS.tilemode) {
632                 if (use_high_bit_depth) {
633                         float *frectrow, *ftilerectrow;
634
635                         ftilerect= MEM_mallocN(rectw*recth*sizeof(*ftilerect), "tilerect");
636
637                         for (y=0; y<recth; y++) {
638                                 frectrow= &frect[y*ibuf->x];
639                                 ftilerectrow= &ftilerect[y*rectw];
640
641                                 memcpy(ftilerectrow, frectrow, tpx*sizeof(*frectrow));
642                         }
643
644                         frect= ftilerect;
645                 }
646                 else {
647                         unsigned int *rectrow, *tilerectrow;
648
649                         tilerect= MEM_mallocN(rectw*recth*sizeof(*tilerect), "tilerect");
650
651                         for (y=0; y<recth; y++) {
652                                 rectrow= &rect[y*ibuf->x];
653                                 tilerectrow= &tilerect[y*rectw];
654
655                                 memcpy(tilerectrow, rectrow, tpx*sizeof(*rectrow));
656                         }
657                         
658                         rect= tilerect;
659                 }
660         }
661
662 #ifdef WITH_DDS
663         if (ibuf->ftype & DDS)
664                 GPU_create_gl_tex_compressed(bind, rect, rectw, recth, mipmap, ima, ibuf);
665         else
666 #endif
667                 GPU_create_gl_tex(bind, rect, frect, rectw, recth, mipmap, use_high_bit_depth, ima);
668         
669         /* mark as non-color data texture */
670         if (*bind) {
671                 if (is_data)
672                         ima->tpageflag |= IMA_GLBIND_IS_DATA;   
673                 else
674                         ima->tpageflag &= ~IMA_GLBIND_IS_DATA;  
675         }
676
677         /* clean up */
678         if (tilerect)
679                 MEM_freeN(tilerect);
680         if (ftilerect)
681                 MEM_freeN(ftilerect);
682         if (srgb_frect)
683                 MEM_freeN(srgb_frect);
684
685         BKE_image_release_ibuf(ima, ibuf, NULL);
686
687         return *bind;
688 }
689
690 /* Image *ima can be NULL */
691 void GPU_create_gl_tex(unsigned int *bind, unsigned int *pix, float *frect, int rectw, int recth,
692                        bool mipmap, bool use_high_bit_depth, Image *ima)
693 {
694         unsigned int *scalerect = NULL;
695         float *fscalerect = NULL;
696
697         int tpx = rectw;
698         int tpy = recth;
699
700         /* scale if not a power of two. this is not strictly necessary for newer
701          * GPUs (OpenGL version >= 2.0) since they support non-power-of-two-textures 
702          * Then don't bother scaling for hardware that supports NPOT textures! */
703         if ((!GPU_non_power_of_two_support() && !is_power_of_2_resolution(rectw, recth)) ||
704                 is_over_resolution_limit(rectw, recth)) {
705                 rectw= smaller_power_of_2_limit(rectw);
706                 recth= smaller_power_of_2_limit(recth);
707                 
708                 if (use_high_bit_depth) {
709                         fscalerect= MEM_mallocN(rectw*recth*sizeof(*fscalerect)*4, "fscalerect");
710                         gluScaleImage(GL_RGBA, tpx, tpy, GL_FLOAT, frect, rectw, recth, GL_FLOAT, fscalerect);
711
712                         frect = fscalerect;
713                 }
714                 else {
715                         scalerect= MEM_mallocN(rectw*recth*sizeof(*scalerect), "scalerect");
716                         gluScaleImage(GL_RGBA, tpx, tpy, GL_UNSIGNED_BYTE, pix, rectw, recth, GL_UNSIGNED_BYTE, scalerect);
717
718                         pix= scalerect;
719                 }
720         }
721
722         /* create image */
723         glGenTextures(1, (GLuint *)bind);
724         glBindTexture(GL_TEXTURE_2D, *bind);
725
726         if (!(GPU_get_mipmap() && mipmap)) {
727                 if (use_high_bit_depth)
728                         glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA16,  rectw, recth, 0, GL_RGBA, GL_FLOAT, frect);
729                 else
730                         glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA,  rectw, recth, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix);
731                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
732                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
733         }
734         else {
735                 if (GTS.gpu_mipmap) {
736                         if (use_high_bit_depth)
737                                 glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA16,  rectw, recth, 0, GL_RGBA, GL_FLOAT, frect);
738                         else
739                                 glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA,  rectw, recth, 0, GL_RGBA, GL_UNSIGNED_BYTE, pix);
740
741                         gpu_generate_mipmap(GL_TEXTURE_2D);
742                 }
743                 else {
744                         if (use_high_bit_depth)
745                                 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA16, rectw, recth, GL_RGBA, GL_FLOAT, frect);
746                         else
747                                 gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, rectw, recth, GL_RGBA, GL_UNSIGNED_BYTE, pix);
748                 }
749                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
750                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
751
752                 if (ima)
753                         ima->tpageflag |= IMA_MIPMAP_COMPLETE;
754         }
755
756         if (GLEW_EXT_texture_filter_anisotropic)
757                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, GPU_get_anisotropic());
758         /* set to modulate with vertex color */
759         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
760
761         if (scalerect)
762                 MEM_freeN(scalerect);
763         if (fscalerect)
764                 MEM_freeN(fscalerect);
765 }
766
767 /**
768  * GPU_upload_dxt_texture() assumes that the texture is already bound and ready to go.
769  * This is so the viewport and the BGE can share some code.
770  * Returns false if the provided ImBuf doesn't have a supported DXT compression format
771  */
772 bool GPU_upload_dxt_texture(ImBuf *ibuf)
773 {
774 #ifdef WITH_DDS
775         GLint format = 0;
776         int blocksize, height, width, i, size, offset = 0;
777
778         width = ibuf->x;
779         height = ibuf->y;
780
781         if (GLEW_EXT_texture_compression_s3tc) {
782                 if (ibuf->dds_data.fourcc == FOURCC_DXT1)
783                         format = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
784                 else if (ibuf->dds_data.fourcc == FOURCC_DXT3)
785                         format = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
786                 else if (ibuf->dds_data.fourcc == FOURCC_DXT5)
787                         format = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
788         }
789
790         if (format == 0) {
791                 printf("Unable to find a suitable DXT compression, falling back to uncompressed\n");
792                 return false;
793         }
794
795         if (!is_power_of_2_resolution(width, height)) {
796                 printf("Unable to load non-power-of-two DXT image resolution, falling back to uncompressed\n");
797                 return false;
798         }
799
800         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
801         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
802
803         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
804
805         if (GLEW_EXT_texture_filter_anisotropic)
806                 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, GPU_get_anisotropic());
807
808         blocksize = (ibuf->dds_data.fourcc == FOURCC_DXT1) ? 8 : 16;
809         for (i=0; i<ibuf->dds_data.nummipmaps && (width||height); ++i) {
810                 if (width == 0)
811                         width = 1;
812                 if (height == 0)
813                         height = 1;
814
815                 size = ((width+3)/4)*((height+3)/4)*blocksize;
816
817                 glCompressedTexImage2D(GL_TEXTURE_2D, i, format, width, height,
818                         0, size, ibuf->dds_data.data + offset);
819
820                 offset += size;
821                 width >>= 1;
822                 height >>= 1;
823         }
824
825         /* set number of mipmap levels we have, needed in case they don't go down to 1x1 */
826         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, i-1);
827
828         return true;
829 #else
830         (void)ibuf;
831         return false;
832 #endif
833 }
834
835 void GPU_create_gl_tex_compressed(unsigned int *bind, unsigned int *pix, int x, int y, int mipmap, Image *ima, ImBuf *ibuf)
836 {
837 #ifndef WITH_DDS
838         (void)ibuf;
839         /* Fall back to uncompressed if DDS isn't enabled */
840         GPU_create_gl_tex(bind, pix, NULL, x, y, mipmap, 0, ima);
841 #else
842
843
844         glGenTextures(1, (GLuint *)bind);
845         glBindTexture(GL_TEXTURE_2D, *bind);
846
847         if (GPU_upload_dxt_texture(ibuf) == 0) {
848                 glDeleteTextures(1, (GLuint *)bind);
849                 GPU_create_gl_tex(bind, pix, NULL, x, y, mipmap, 0, ima);
850         }
851 #endif
852 }
853 static void gpu_verify_repeat(Image *ima)
854 {
855         /* set either clamp or repeat in X/Y */
856         if (ima->tpageflag & IMA_CLAMP_U)
857                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
858         else
859                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
860
861         if (ima->tpageflag & IMA_CLAMP_V)
862                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
863         else
864                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
865 }
866
867 int GPU_set_tpage(MTFace *tface, int mipmap, int alphablend)
868 {
869         Image *ima;
870         
871         /* check if we need to clear the state */
872         if (tface==NULL) {
873                 GPU_clear_tpage(false);
874                 return 0;
875         }
876
877         ima= tface->tpage;
878         GTS.lasttface= tface;
879
880         gpu_verify_alpha_blend(alphablend);
881         gpu_verify_reflection(ima);
882
883         if (GPU_verify_image(ima, NULL, tface->tile, 1, mipmap, false)) {
884                 GTS.curtile= GTS.tile;
885                 GTS.curima= GTS.ima;
886                 GTS.curtilemode= GTS.tilemode;
887                 GTS.curtileXRep = GTS.tileXRep;
888                 GTS.curtileYRep = GTS.tileYRep;
889
890                 glEnable(GL_TEXTURE_2D);
891         }
892         else {
893                 glDisable(GL_TEXTURE_2D);
894                 
895                 GTS.curtile= 0;
896                 GTS.curima= NULL;
897                 GTS.curtilemode= 0;
898                 GTS.curtileXRep = 0;
899                 GTS.curtileYRep = 0;
900
901                 return 0;
902         }
903         
904         gpu_verify_repeat(ima);
905         
906         /* Did this get lost in the image recode? */
907         /* BKE_image_tag_time(ima);*/
908
909         return 1;
910 }
911
912 /* these two functions are called on entering and exiting texture paint mode,
913  * temporary disabling/enabling mipmapping on all images for quick texture
914  * updates with glTexSubImage2D. images that didn't change don't have to be
915  * re-uploaded to OpenGL */
916 void GPU_paint_set_mipmap(int mipmap)
917 {
918         Image* ima;
919         
920         if (!GTS.domipmap)
921                 return;
922
923         GTS.texpaint = !mipmap;
924
925         if (mipmap) {
926                 for (ima=G.main->image.first; ima; ima=ima->id.next) {
927                         if (ima->bindcode) {
928                                 if (ima->tpageflag & IMA_MIPMAP_COMPLETE) {
929                                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
930                                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gpu_get_mipmap_filter(0));
931                                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
932                                 }
933                                 else
934                                         GPU_free_image(ima);
935                         }
936                         else
937                                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
938                 }
939
940         }
941         else {
942                 for (ima=G.main->image.first; ima; ima=ima->id.next) {
943                         if (ima->bindcode) {
944                                 glBindTexture(GL_TEXTURE_2D, ima->bindcode);
945                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
946                                 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gpu_get_mipmap_filter(1));
947                         }
948                         else
949                                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
950                 }
951         }
952 }
953
954
955 /* check if image has been downscaled and do scaled partial update */
956 static bool GPU_check_scaled_image(ImBuf *ibuf, Image *ima, float *frect, int x, int y, int w, int h)
957 {
958         if ((!GPU_non_power_of_two_support() && !is_power_of_2_resolution(ibuf->x, ibuf->y)) ||
959             is_over_resolution_limit(ibuf->x, ibuf->y))
960         {
961                 int x_limit = smaller_power_of_2_limit(ibuf->x);
962                 int y_limit = smaller_power_of_2_limit(ibuf->y);
963
964                 float xratio = x_limit / (float)ibuf->x;
965                 float yratio = y_limit / (float)ibuf->y;
966
967                 /* find new width, height and x,y gpu texture coordinates */
968
969                 /* take ceiling because we will be losing 1 pixel due to rounding errors in x,y... */
970                 int rectw = (int)ceil(xratio * w);
971                 int recth = (int)ceil(yratio * h);
972
973                 x *= xratio;
974                 y *= yratio;
975
976                 /* ...but take back if we are over the limit! */
977                 if (rectw + x > x_limit) rectw--;
978                 if (recth + y > y_limit) recth--;
979
980                 /* float rectangles are already continuous in memory so we can use gluScaleImage */
981                 if (frect) {
982                         float *fscalerect = MEM_mallocN(rectw*recth*sizeof(*fscalerect)*4, "fscalerect");
983                         gluScaleImage(GL_RGBA, w, h, GL_FLOAT, frect, rectw, recth, GL_FLOAT, fscalerect);
984
985                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
986                         glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, rectw, recth, GL_RGBA,
987                                         GL_FLOAT, fscalerect);
988
989                         MEM_freeN(fscalerect);
990                 }
991                 /* byte images are not continuous in memory so do manual interpolation */
992                 else {
993                         unsigned char *scalerect = MEM_mallocN(rectw * recth * sizeof(*scalerect) * 4, "scalerect");
994                         unsigned int *p = (unsigned int *)scalerect;
995                         int i, j;
996                         float inv_xratio = 1.0f / xratio;
997                         float inv_yratio = 1.0f / yratio;
998                         for (i = 0; i < rectw; i++) {
999                                 float u = (x + i) * inv_xratio;
1000                                 for (j = 0; j < recth; j++) {
1001                                         float v = (y + j) * inv_yratio;
1002                                         bilinear_interpolation_color_wrap(ibuf, (unsigned char *)(p + i + j * (rectw)), NULL, u, v);
1003                                 }
1004                         }
1005                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
1006                         glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, rectw, recth, GL_RGBA,
1007                                         GL_UNSIGNED_BYTE, scalerect);
1008
1009                         MEM_freeN(scalerect);
1010                 }
1011
1012                 if (GPU_get_mipmap()) {
1013                         gpu_generate_mipmap(GL_TEXTURE_2D);
1014                 }
1015                 else {
1016                         ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
1017                 }
1018
1019                 return true;
1020         }
1021
1022         return false;
1023 }
1024
1025 void GPU_paint_update_image(Image *ima, int x, int y, int w, int h)
1026 {
1027         ImBuf *ibuf;
1028         
1029         ibuf = BKE_image_acquire_ibuf(ima, NULL, NULL);
1030         
1031         if (ima->repbind || (GPU_get_mipmap() && !GTS.gpu_mipmap) || !ima->bindcode || !ibuf ||
1032                 (w == 0) || (h == 0))
1033         {
1034                 /* these cases require full reload still */
1035                 GPU_free_image(ima);
1036         }
1037         else {
1038                 /* for the special case, we can do a partial update
1039                  * which is much quicker for painting */
1040                 GLint row_length, skip_pixels, skip_rows;
1041
1042                 /* if color correction is needed, we must update the part that needs updating. */
1043                 if (ibuf->rect_float) {
1044                         float *buffer = MEM_mallocN(w * h * sizeof(float) * 4, "temp_texpaint_float_buf");
1045                         bool is_data = (ima->tpageflag & IMA_GLBIND_IS_DATA) != 0;
1046                         IMB_partial_rect_from_float(ibuf, buffer, x, y, w, h, is_data);
1047                         
1048                         if (GPU_check_scaled_image(ibuf, ima, buffer, x, y, w, h)) {
1049                                 MEM_freeN(buffer);
1050                                 BKE_image_release_ibuf(ima, ibuf, NULL);
1051                                 return;
1052                         }
1053
1054                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
1055                         glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA,
1056                                         GL_FLOAT, buffer);
1057
1058                         MEM_freeN(buffer);
1059
1060                         /* we have already accounted for the case where GTS.gpu_mipmap is false
1061                          * so we will be using GPU mipmap generation here */
1062                         if (GPU_get_mipmap()) {
1063                                 gpu_generate_mipmap(GL_TEXTURE_2D);
1064                         }
1065                         else {
1066                                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
1067                         }
1068
1069                         BKE_image_release_ibuf(ima, ibuf, NULL);
1070                         return;
1071                 }
1072
1073                 if (GPU_check_scaled_image(ibuf, ima, NULL, x, y, w, h)) {
1074                         BKE_image_release_ibuf(ima, ibuf, NULL);
1075                         return;
1076                 }
1077
1078                 glBindTexture(GL_TEXTURE_2D, ima->bindcode);
1079
1080                 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
1081                 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
1082                 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
1083
1084                 glPixelStorei(GL_UNPACK_ROW_LENGTH, ibuf->x);
1085                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
1086                 glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
1087
1088                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA,
1089                         GL_UNSIGNED_BYTE, ibuf->rect);
1090
1091                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
1092                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
1093                 glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
1094
1095                 /* see comment above as to why we are using gpu mipmap generation here */
1096                 if (GPU_get_mipmap()) {
1097                         gpu_generate_mipmap(GL_TEXTURE_2D);
1098                 }
1099                 else {
1100                         ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
1101                 }
1102         }
1103
1104         BKE_image_release_ibuf(ima, ibuf, NULL);
1105 }
1106
1107 void GPU_update_images_framechange(void)
1108 {
1109         Image *ima;
1110         
1111         for (ima=G.main->image.first; ima; ima=ima->id.next) {
1112                 if (ima->tpageflag & IMA_TWINANIM) {
1113                         if (ima->twend >= ima->xrep*ima->yrep)
1114                                 ima->twend= ima->xrep*ima->yrep-1;
1115                 
1116                         /* check: is bindcode not in the array? free. (to do) */
1117                         
1118                         ima->lastframe++;
1119                         if (ima->lastframe > ima->twend)
1120                                 ima->lastframe= ima->twsta;
1121                 }
1122         }
1123 }
1124
1125 int GPU_update_image_time(Image *ima, double time)
1126 {
1127         int     inc = 0;
1128         float   diff;
1129         int     newframe;
1130
1131         if (!ima)
1132                 return 0;
1133
1134         if (ima->lastupdate<0)
1135                 ima->lastupdate = 0;
1136
1137         if (ima->lastupdate > (float)time)
1138                 ima->lastupdate=(float)time;
1139
1140         if (ima->tpageflag & IMA_TWINANIM) {
1141                 if (ima->twend >= ima->xrep*ima->yrep) ima->twend= ima->xrep*ima->yrep-1;
1142                 
1143                 /* check: is the bindcode not in the array? Then free. (still to do) */
1144                 
1145                 diff = (float)((float)time - ima->lastupdate);
1146                 inc = (int)(diff*(float)ima->animspeed);
1147
1148                 ima->lastupdate+=((float)inc/(float)ima->animspeed);
1149
1150                 newframe = ima->lastframe+inc;
1151
1152                 if (newframe > (int)ima->twend) {
1153                         if (ima->twend-ima->twsta != 0)
1154                                 newframe = (int)ima->twsta-1 + (newframe-ima->twend)%(ima->twend-ima->twsta);
1155                         else
1156                                 newframe = ima->twsta;
1157                 }
1158
1159                 ima->lastframe = newframe;
1160         }
1161
1162         return inc;
1163 }
1164
1165
1166 void GPU_free_smoke(SmokeModifierData *smd)
1167 {
1168         if (smd->type & MOD_SMOKE_TYPE_DOMAIN && smd->domain) {
1169                 if (smd->domain->tex)
1170                         GPU_texture_free(smd->domain->tex);
1171                 smd->domain->tex = NULL;
1172
1173                 if (smd->domain->tex_shadow)
1174                         GPU_texture_free(smd->domain->tex_shadow);
1175                 smd->domain->tex_shadow = NULL;
1176
1177                 if (smd->domain->tex_flame)
1178                         GPU_texture_free(smd->domain->tex_flame);
1179                 smd->domain->tex_flame = NULL;
1180         }
1181 }
1182
1183 void GPU_create_smoke(SmokeModifierData *smd, int highres)
1184 {
1185 #ifdef WITH_SMOKE
1186         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1187                 SmokeDomainSettings *sds = smd->domain;
1188                 if (!sds->tex && !highres) {
1189                         /* rgba texture for color + density */
1190                         if (smoke_has_colors(sds->fluid)) {
1191                                 float *data = MEM_callocN(sizeof(float)*sds->total_cells*4, "smokeColorTexture");
1192                                 smoke_get_rgba(sds->fluid, data, 0);
1193                                 sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 4, data);
1194                                 MEM_freeN(data);
1195                         }
1196                         /* density only */
1197                         else {
1198                                 sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 1, smoke_get_density(sds->fluid));
1199                         }
1200                         sds->tex_flame = (smoke_has_fuel(sds->fluid)) ? GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 1, smoke_get_flame(sds->fluid)) : NULL;
1201                 }
1202                 else if (!sds->tex && highres) {
1203                         /* rgba texture for color + density */
1204                         if (smoke_turbulence_has_colors(sds->wt)) {
1205                                 float *data = MEM_callocN(sizeof(float)*smoke_turbulence_get_cells(sds->wt)*4, "smokeColorTexture");
1206                                 smoke_turbulence_get_rgba(sds->wt, data, 0);
1207                                 sds->tex = GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 4, data);
1208                                 MEM_freeN(data);
1209                         }
1210                         /* density only */
1211                         else {
1212                                 sds->tex = GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 1, smoke_turbulence_get_density(sds->wt));
1213                         }
1214                         sds->tex_flame = (smoke_turbulence_has_fuel(sds->wt)) ? GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 1, smoke_turbulence_get_flame(sds->wt)) : NULL;
1215                 }
1216
1217                 sds->tex_shadow = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 1, sds->shadow);
1218         }
1219 #else // WITH_SMOKE
1220         (void)highres;
1221         smd->domain->tex= NULL;
1222         smd->domain->tex_flame= NULL;
1223         smd->domain->tex_shadow= NULL;
1224 #endif // WITH_SMOKE
1225 }
1226
1227 static LinkNode *image_free_queue = NULL;
1228
1229 static void gpu_queue_image_for_free(Image *ima)
1230 {
1231         BLI_lock_thread(LOCK_OPENGL);
1232         BLI_linklist_prepend(&image_free_queue, ima);
1233         BLI_unlock_thread(LOCK_OPENGL);
1234 }
1235
1236 void GPU_free_unused_buffers(void)
1237 {
1238         LinkNode *node;
1239         Image *ima;
1240
1241         if (!BLI_thread_is_main())
1242                 return;
1243
1244         BLI_lock_thread(LOCK_OPENGL);
1245
1246         /* images */
1247         for (node=image_free_queue; node; node=node->next) {
1248                 ima = node->link;
1249
1250                 /* check in case it was freed in the meantime */
1251                 if (G.main && BLI_findindex(&G.main->image, ima) != -1)
1252                         GPU_free_image(ima);
1253         }
1254
1255         BLI_linklist_free(image_free_queue, NULL);
1256         image_free_queue = NULL;
1257
1258         /* vbo buffers */
1259         GPU_global_buffer_pool_free_unused();
1260
1261         BLI_unlock_thread(LOCK_OPENGL);
1262 }
1263
1264 void GPU_free_image(Image *ima)
1265 {
1266         if (!BLI_thread_is_main()) {
1267                 gpu_queue_image_for_free(ima);
1268                 return;
1269         }
1270
1271         /* free regular image binding */
1272         if (ima->bindcode) {
1273                 glDeleteTextures(1, (GLuint *)&ima->bindcode);
1274                 ima->bindcode= 0;
1275         }
1276
1277         /* free glsl image binding */
1278         if (ima->gputexture) {
1279                 GPU_texture_free(ima->gputexture);
1280                 ima->gputexture= NULL;
1281         }
1282
1283         /* free repeated image binding */
1284         if (ima->repbind) {
1285                 glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
1286         
1287                 MEM_freeN(ima->repbind);
1288                 ima->repbind= NULL;
1289         }
1290
1291         ima->tpageflag &= ~(IMA_MIPMAP_COMPLETE|IMA_GLBIND_IS_DATA);
1292 }
1293
1294 void GPU_free_images(void)
1295 {
1296         Image* ima;
1297
1298         if (G.main)
1299                 for (ima=G.main->image.first; ima; ima=ima->id.next)
1300                         GPU_free_image(ima);
1301 }
1302
1303 /* same as above but only free animated images */
1304 void GPU_free_images_anim(void)
1305 {
1306         Image* ima;
1307
1308         if (G.main)
1309                 for (ima=G.main->image.first; ima; ima=ima->id.next)
1310                         if (BKE_image_is_animated(ima))
1311                                 GPU_free_image(ima);
1312 }
1313
1314
1315 void GPU_free_images_old(void)
1316 {
1317         Image *ima;
1318         static int lasttime = 0;
1319         int ctime = (int)PIL_check_seconds_timer();
1320
1321         /*
1322          * Run garbage collector once for every collecting period of time
1323          * if textimeout is 0, that's the option to NOT run the collector
1324          */
1325         if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
1326                 return;
1327
1328         /* of course not! */
1329         if (G.is_rendering)
1330                 return;
1331
1332         lasttime = ctime;
1333
1334         ima = G.main->image.first;
1335         while (ima) {
1336                 if ((ima->flag & IMA_NOCOLLECT) == 0 && ctime - ima->lastused > U.textimeout) {
1337                         /* If it's in GL memory, deallocate and set time tag to current time
1338                          * This gives textures a "second chance" to be used before dying. */
1339                         if (ima->bindcode || ima->repbind) {
1340                                 GPU_free_image(ima);
1341                                 ima->lastused = ctime;
1342                         }
1343                         /* Otherwise, just kill the buffers */
1344                         else {
1345                                 BKE_image_free_buffers(ima);
1346                         }
1347                 }
1348                 ima = ima->id.next;
1349         }
1350 }
1351
1352
1353 /* OpenGL Materials */
1354
1355 #define FIXEDMAT        8
1356
1357 /* OpenGL state caching for materials */
1358
1359 typedef struct GPUMaterialFixed {
1360         float diff[4];
1361         float spec[4];
1362         int hard;
1363 } GPUMaterialFixed; 
1364
1365 static struct GPUMaterialState {
1366         GPUMaterialFixed (*matbuf);
1367         GPUMaterialFixed matbuf_fixed[FIXEDMAT];
1368         int totmat;
1369
1370         Material **gmatbuf;
1371         Material *gmatbuf_fixed[FIXEDMAT];
1372         Material *gboundmat;
1373         Object *gob;
1374         Scene *gscene;
1375         int glay;
1376         bool gscenelock;
1377         float (*gviewmat)[4];
1378         float (*gviewinv)[4];
1379
1380         bool backface_culling;
1381
1382         GPUBlendMode *alphablend;
1383         GPUBlendMode alphablend_fixed[FIXEDMAT];
1384         bool use_alpha_pass, is_alpha_pass;
1385         bool use_matcaps;
1386
1387         int lastmatnr, lastretval;
1388         GPUBlendMode lastalphablend;
1389 } GMS = {NULL};
1390
1391 /* fixed function material, alpha handed by caller */
1392 static void gpu_material_to_fixed(GPUMaterialFixed *smat, const Material *bmat, const int gamma, const Object *ob, const int new_shading_nodes)
1393 {
1394         if (new_shading_nodes || bmat->mode & MA_SHLESS) {
1395                 copy_v3_v3(smat->diff, &bmat->r);
1396                 smat->diff[3]= 1.0;
1397
1398                 if (gamma)
1399                         linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
1400
1401                 zero_v4(smat->spec);
1402                 smat->hard= 0;
1403         }
1404         else {
1405                 mul_v3_v3fl(smat->diff, &bmat->r, bmat->ref + bmat->emit);
1406                 smat->diff[3]= 1.0; /* caller may set this to bmat->alpha */
1407
1408                 if (bmat->shade_flag & MA_OBCOLOR)
1409                         mul_v3_v3(smat->diff, ob->col);
1410                 
1411                 mul_v3_v3fl(smat->spec, &bmat->specr, bmat->spec);
1412                 smat->spec[3]= 1.0; /* always 1 */
1413                 smat->hard= CLAMPIS(bmat->har, 0, 128);
1414
1415                 if (gamma) {
1416                         linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
1417                         linearrgb_to_srgb_v3_v3(smat->spec, smat->spec);
1418                 }
1419         }
1420 }
1421
1422 static Material *gpu_active_node_material(Material *ma)
1423 {
1424         if (ma && ma->use_nodes && ma->nodetree) {
1425                 bNode *node = nodeGetActiveID(ma->nodetree, ID_MA);
1426
1427                 if (node)
1428                         return (Material *)node->id;
1429                 else
1430                         return NULL;
1431         }
1432
1433         return ma;
1434 }
1435
1436 void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, Object *ob, bool glsl, bool *do_alpha_after)
1437 {
1438         Material *ma;
1439         GPUMaterial *gpumat;
1440         GPUBlendMode alphablend;
1441         int a;
1442         const bool gamma = BKE_scene_check_color_management_enabled(scene);
1443         const bool new_shading_nodes = BKE_scene_use_new_shading_nodes(scene);
1444         const bool use_matcap = (v3d->flag2 & V3D_SHOW_SOLID_MATCAP) != 0;  /* assumes v3d->defmaterial->preview is set */
1445
1446 #ifdef WITH_GAMEENGINE
1447         if (rv3d->rflag & RV3D_IS_GAME_ENGINE) {
1448                 ob = BKE_object_lod_matob_get(ob, scene);
1449         }
1450 #endif
1451
1452         /* initialize state */
1453         memset(&GMS, 0, sizeof(GMS));
1454         GMS.lastmatnr = -1;
1455         GMS.lastretval = -1;
1456         GMS.lastalphablend = GPU_BLEND_SOLID;
1457         GMS.use_matcaps = use_matcap;
1458
1459         GMS.backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING) != 0;
1460
1461         GMS.gob = ob;
1462         GMS.gscene = scene;
1463         GMS.totmat = use_matcap ? 1 : ob->totcol + 1;  /* materials start from 1, default material is 0 */
1464         GMS.glay= (v3d->localvd)? v3d->localvd->lay: v3d->lay; /* keep lamps visible in local view */
1465         GMS.gscenelock = (v3d->scenelock != 0);
1466         GMS.gviewmat= rv3d->viewmat;
1467         GMS.gviewinv= rv3d->viewinv;
1468
1469         /* alpha pass setup. there's various cases to handle here:
1470          * - object transparency on: only solid materials draw in the first pass,
1471          * and only transparent in the second 'alpha' pass.
1472          * - object transparency off: for glsl we draw both in a single pass, and
1473          * for solid we don't use transparency at all. */
1474         GMS.use_alpha_pass = (do_alpha_after != NULL);
1475         GMS.is_alpha_pass = (v3d->transp != false);
1476         if (GMS.use_alpha_pass)
1477                 *do_alpha_after = false;
1478         
1479         if (GMS.totmat > FIXEDMAT) {
1480                 GMS.matbuf= MEM_callocN(sizeof(GPUMaterialFixed)*GMS.totmat, "GMS.matbuf");
1481                 GMS.gmatbuf= MEM_callocN(sizeof(*GMS.gmatbuf)*GMS.totmat, "GMS.matbuf");
1482                 GMS.alphablend= MEM_callocN(sizeof(*GMS.alphablend)*GMS.totmat, "GMS.matbuf");
1483         }
1484         else {
1485                 GMS.matbuf= GMS.matbuf_fixed;
1486                 GMS.gmatbuf= GMS.gmatbuf_fixed;
1487                 GMS.alphablend= GMS.alphablend_fixed;
1488         }
1489
1490         /* viewport material, setup in space_view3d, defaults to matcap using ma->preview now */
1491         if (use_matcap) {
1492                 GMS.gmatbuf[0] = v3d->defmaterial;
1493                 GPU_material_matcap(scene, v3d->defmaterial);
1494
1495                 /* do material 1 too, for displists! */
1496                 memcpy(&GMS.matbuf[1], &GMS.matbuf[0], sizeof(GPUMaterialFixed));
1497         
1498                 GMS.alphablend[0]= GPU_BLEND_SOLID;
1499         }
1500         else {
1501         
1502                 /* no materials assigned? */
1503                 if (ob->totcol==0) {
1504                         gpu_material_to_fixed(&GMS.matbuf[0], &defmaterial, 0, ob, new_shading_nodes);
1505
1506                         /* do material 1 too, for displists! */
1507                         memcpy(&GMS.matbuf[1], &GMS.matbuf[0], sizeof(GPUMaterialFixed));
1508
1509                         if (glsl) {
1510                                 GMS.gmatbuf[0]= &defmaterial;
1511                                 GPU_material_from_blender(GMS.gscene, &defmaterial);
1512                         }
1513
1514                         GMS.alphablend[0]= GPU_BLEND_SOLID;
1515                 }
1516                 
1517                 /* setup materials */
1518                 for (a=1; a<=ob->totcol; a++) {
1519                         /* find a suitable material */
1520                         ma= give_current_material(ob, a);
1521                         if (!glsl && !new_shading_nodes) ma= gpu_active_node_material(ma);
1522                         if (ma==NULL) ma= &defmaterial;
1523
1524                         /* create glsl material if requested */
1525                         gpumat = (glsl)? GPU_material_from_blender(GMS.gscene, ma): NULL;
1526
1527                         if (gpumat) {
1528                                 /* do glsl only if creating it succeed, else fallback */
1529                                 GMS.gmatbuf[a]= ma;
1530                                 alphablend = GPU_material_alpha_blend(gpumat, ob->col);
1531                         }
1532                         else {
1533                                 /* fixed function opengl materials */
1534                                 gpu_material_to_fixed(&GMS.matbuf[a], ma, gamma, ob, new_shading_nodes);
1535
1536                                 if (GMS.use_alpha_pass && ((ma->mode & MA_TRANSP) || (new_shading_nodes && ma->alpha != 1.0f))) {
1537                                         GMS.matbuf[a].diff[3]= ma->alpha;
1538                                         alphablend = (ma->alpha == 1.0f)? GPU_BLEND_SOLID: GPU_BLEND_ALPHA;
1539                                 }
1540                                 else {
1541                                         GMS.matbuf[a].diff[3]= 1.0f;
1542                                         alphablend = GPU_BLEND_SOLID;
1543                                 }
1544                         }
1545
1546                         /* setting 'do_alpha_after = true' indicates this object needs to be
1547                          * drawn in a second alpha pass for improved blending */
1548                         if (do_alpha_after && !GMS.is_alpha_pass)
1549                                 if (ELEM(alphablend, GPU_BLEND_ALPHA, GPU_BLEND_ADD, GPU_BLEND_ALPHA_SORT))
1550                                         *do_alpha_after = true;
1551
1552                         GMS.alphablend[a]= alphablend;
1553                 }
1554         }
1555         
1556         /* let's start with a clean state */
1557         GPU_disable_material();
1558 }
1559
1560 int GPU_enable_material(int nr, void *attribs)
1561 {
1562         GPUVertexAttribs *gattribs = attribs;
1563         GPUMaterial *gpumat;
1564         GPUBlendMode alphablend;
1565
1566         /* no GPU_begin_object_materials, use default material */
1567         if (!GMS.matbuf) {
1568                 float diff[4], spec[4];
1569
1570                 memset(&GMS, 0, sizeof(GMS));
1571
1572                 mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit);
1573                 diff[3]= 1.0;
1574
1575                 mul_v3_v3fl(spec, &defmaterial.specr, defmaterial.spec);
1576                 spec[3]= 1.0;
1577
1578                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff);
1579                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
1580                 glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35); /* blender default */
1581
1582                 return 0;
1583         }
1584
1585         /* prevent index to use un-initialized array items */
1586         if (nr>=GMS.totmat)
1587                 nr= 0;
1588
1589         if (gattribs)
1590                 memset(gattribs, 0, sizeof(*gattribs));
1591
1592         /* keep current material */
1593         if (nr==GMS.lastmatnr)
1594                 return GMS.lastretval;
1595
1596         /* unbind glsl material */
1597         if (GMS.gboundmat) {
1598                 if (GMS.is_alpha_pass) glDepthMask(0);
1599                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
1600                 GMS.gboundmat= NULL;
1601         }
1602
1603         /* draw materials with alpha in alpha pass */
1604         GMS.lastmatnr = nr;
1605         GMS.lastretval = 1;
1606
1607         if (GMS.use_alpha_pass) {
1608                 GMS.lastretval = ELEM(GMS.alphablend[nr], GPU_BLEND_SOLID, GPU_BLEND_CLIP);
1609                 if (GMS.is_alpha_pass)
1610                         GMS.lastretval = !GMS.lastretval;
1611         }
1612         else
1613                 GMS.lastretval = !GMS.is_alpha_pass;
1614
1615         if (GMS.lastretval) {
1616                 /* for alpha pass, use alpha blend */
1617                 alphablend = GMS.alphablend[nr];
1618
1619                 if (gattribs && GMS.gmatbuf[nr]) {
1620                         /* bind glsl material and get attributes */
1621                         Material *mat = GMS.gmatbuf[nr];
1622                         float auto_bump_scale;
1623
1624                         gpumat = GPU_material_from_blender(GMS.gscene, mat);
1625                         GPU_material_vertex_attributes(gpumat, gattribs);
1626                         GPU_material_bind(gpumat, GMS.gob->lay, GMS.glay, 1.0, !(GMS.gob->mode & OB_MODE_TEXTURE_PAINT), GMS.gviewmat, GMS.gviewinv, GMS.gscenelock);
1627
1628                         auto_bump_scale = GMS.gob->derivedFinal != NULL ? GMS.gob->derivedFinal->auto_bump_scale : 1.0f;
1629                         GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gob->col, auto_bump_scale);
1630                         GMS.gboundmat= mat;
1631
1632                         /* for glsl use alpha blend mode, unless it's set to solid and
1633                          * we are already drawing in an alpha pass */
1634                         if (mat->game.alpha_blend != GPU_BLEND_SOLID)
1635                                 alphablend= mat->game.alpha_blend;
1636
1637                         if (GMS.is_alpha_pass) glDepthMask(1);
1638
1639                         if (GMS.backface_culling) {
1640                                 if (mat->game.flag)
1641                                         glEnable(GL_CULL_FACE);
1642                                 else
1643                                         glDisable(GL_CULL_FACE);
1644                         }
1645
1646                         if (GMS.use_matcaps)
1647                                 glColor3f(1.0, 1.0, 1.0f);
1648                 }
1649                 else {
1650                         /* or do fixed function opengl material */
1651                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, GMS.matbuf[nr].diff);
1652                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, GMS.matbuf[nr].spec);
1653                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, GMS.matbuf[nr].hard);
1654                 }
1655
1656                 /* set (alpha) blending mode */
1657                 GPU_set_material_alpha_blend(alphablend);
1658         }
1659
1660         return GMS.lastretval;
1661 }
1662
1663 void GPU_set_material_alpha_blend(int alphablend)
1664 {
1665         if (GMS.lastalphablend == alphablend)
1666                 return;
1667         
1668         gpu_set_alpha_blend(alphablend);
1669         GMS.lastalphablend = alphablend;
1670 }
1671
1672 int GPU_get_material_alpha_blend(void)
1673 {
1674         return GMS.lastalphablend;
1675 }
1676
1677 void GPU_disable_material(void)
1678 {
1679         GMS.lastmatnr= -1;
1680         GMS.lastretval= 1;
1681
1682         if (GMS.gboundmat) {
1683                 if (GMS.backface_culling)
1684                         glDisable(GL_CULL_FACE);
1685
1686                 if (GMS.is_alpha_pass) glDepthMask(0);
1687                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
1688                 GMS.gboundmat= NULL;
1689         }
1690
1691         GPU_set_material_alpha_blend(GPU_BLEND_SOLID);
1692 }
1693
1694 void GPU_material_diffuse_get(int nr, float diff[4])
1695 {
1696         /* prevent index to use un-initialized array items */
1697         if (nr >= GMS.totmat)
1698                 nr = 0;
1699
1700         /* no GPU_begin_object_materials, use default material */
1701         if (!GMS.matbuf) {
1702                 mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit);
1703         }
1704         else {
1705                 copy_v4_v4(diff, GMS.matbuf[nr].diff);
1706         }
1707 }
1708
1709 bool GPU_material_use_matcaps_get(void)
1710 {
1711         return GMS.use_matcaps;
1712 }
1713
1714
1715 void GPU_end_object_materials(void)
1716 {
1717         GPU_disable_material();
1718
1719         if (GMS.matbuf && GMS.matbuf != GMS.matbuf_fixed) {
1720                 MEM_freeN(GMS.matbuf);
1721                 MEM_freeN(GMS.gmatbuf);
1722                 MEM_freeN(GMS.alphablend);
1723         }
1724
1725         GMS.matbuf= NULL;
1726         GMS.gmatbuf= NULL;
1727         GMS.alphablend= NULL;
1728
1729         /* resetting the texture matrix after the scaling needed for tiled textures */
1730         if (GTS.tilemode) {
1731                 glMatrixMode(GL_TEXTURE);
1732                 glLoadIdentity();
1733                 glMatrixMode(GL_MODELVIEW);
1734         }
1735 }
1736
1737 /* Lights */
1738
1739 int GPU_default_lights(void)
1740 {
1741         float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f}, position[4];
1742         int a, count = 0;
1743         
1744         /* initialize */
1745         if (U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
1746                 U.light[0].flag= 1;
1747                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
1748                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
1749                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
1750                 U.light[0].spec[3]= 1.0;
1751                 
1752                 U.light[1].flag= 0;
1753                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
1754                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
1755                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
1756                 U.light[1].spec[3]= 1.0;
1757         
1758                 U.light[2].flag= 0;
1759                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
1760                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
1761                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
1762                 U.light[2].spec[3]= 1.0;
1763         }
1764
1765         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
1766
1767         for (a=0; a<8; a++) {
1768                 if (a<3) {
1769                         if (U.light[a].flag) {
1770                                 glEnable(GL_LIGHT0+a);
1771
1772                                 normalize_v3_v3(position, U.light[a].vec);
1773                                 position[3]= 0.0f;
1774                                 
1775                                 glLightfv(GL_LIGHT0+a, GL_POSITION, position); 
1776                                 glLightfv(GL_LIGHT0+a, GL_DIFFUSE, U.light[a].col); 
1777                                 glLightfv(GL_LIGHT0+a, GL_SPECULAR, U.light[a].spec); 
1778
1779                                 count++;
1780                         }
1781                         else {
1782                                 glDisable(GL_LIGHT0+a);
1783
1784                                 glLightfv(GL_LIGHT0+a, GL_POSITION, zero); 
1785                                 glLightfv(GL_LIGHT0+a, GL_DIFFUSE, zero); 
1786                                 glLightfv(GL_LIGHT0+a, GL_SPECULAR, zero);
1787                         }
1788
1789                         // clear stuff from other opengl lamp usage
1790                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
1791                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
1792                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
1793                 }
1794                 else
1795                         glDisable(GL_LIGHT0+a);
1796         }
1797         
1798         glDisable(GL_LIGHTING);
1799
1800         glDisable(GL_COLOR_MATERIAL);
1801
1802         return count;
1803 }
1804
1805 int GPU_scene_object_lights(Scene *scene, Object *ob, int lay, float viewmat[4][4], int ortho)
1806 {
1807         Base *base;
1808         Lamp *la;
1809         int count;
1810         float position[4], direction[4], energy[4];
1811         
1812         /* disable all lights */
1813         for (count=0; count<8; count++)
1814                 glDisable(GL_LIGHT0+count);
1815         
1816         /* view direction for specular is not compute correct by default in
1817          * opengl, so we set the settings ourselfs */
1818         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (ortho)? GL_FALSE: GL_TRUE);
1819
1820         count= 0;
1821         
1822         for (base=scene->base.first; base; base=base->next) {
1823                 if (base->object->type!=OB_LAMP)
1824                         continue;
1825
1826                 if (!(base->lay & lay) || !(base->lay & ob->lay))
1827                         continue;
1828
1829                 la= base->object->data;
1830                 
1831                 /* setup lamp transform */
1832                 glPushMatrix();
1833                 glLoadMatrixf((float *)viewmat);
1834                 
1835                 if (la->type==LA_SUN) {
1836                         /* sun lamp */
1837                         copy_v3_v3(direction, base->object->obmat[2]);
1838                         direction[3]= 0.0;
1839
1840                         glLightfv(GL_LIGHT0+count, GL_POSITION, direction); 
1841                 }
1842                 else {
1843                         /* other lamps with attenuation */
1844                         copy_v3_v3(position, base->object->obmat[3]);
1845                         position[3]= 1.0f;
1846
1847                         glLightfv(GL_LIGHT0+count, GL_POSITION, position); 
1848                         glLightf(GL_LIGHT0+count, GL_CONSTANT_ATTENUATION, 1.0);
1849                         glLightf(GL_LIGHT0+count, GL_LINEAR_ATTENUATION, la->att1/la->dist);
1850                         glLightf(GL_LIGHT0+count, GL_QUADRATIC_ATTENUATION, la->att2/(la->dist*la->dist));
1851                         
1852                         if (la->type==LA_SPOT) {
1853                                 /* spot lamp */
1854                                 negate_v3_v3(direction, base->object->obmat[2]);
1855                                 glLightfv(GL_LIGHT0+count, GL_SPOT_DIRECTION, direction);
1856                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, RAD2DEGF(la->spotsize * 0.5f));
1857                                 glLightf(GL_LIGHT0+count, GL_SPOT_EXPONENT, 128.0f*la->spotblend);
1858                         }
1859                         else
1860                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, 180.0);
1861                 }
1862                 
1863                 /* setup energy */
1864                 mul_v3_v3fl(energy, &la->r, la->energy);
1865                 energy[3]= 1.0;
1866
1867                 glLightfv(GL_LIGHT0+count, GL_DIFFUSE, energy); 
1868                 glLightfv(GL_LIGHT0+count, GL_SPECULAR, energy);
1869                 glEnable(GL_LIGHT0+count);
1870                 
1871                 glPopMatrix();
1872                 
1873                 count++;
1874                 if (count==8)
1875                         break;
1876         }
1877
1878         return count;
1879 }
1880
1881 static void gpu_multisample(bool enable)
1882 {
1883         if (GLEW_VERSION_1_3 || GLEW_ARB_multisample) {
1884 #ifdef __linux__
1885                 /* changing multisample enablement from the default (enabled) causes problems on some
1886                  * systems (NVIDIA/Linux) when the pixel format doesn't have a multisample buffer */
1887                 bool toggle_ok = true;
1888
1889                 if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
1890                         int samples = 0;
1891                         glGetIntegerv(GL_SAMPLES, &samples);
1892
1893                         if (samples == 0)
1894                                 toggle_ok = false;
1895                 }
1896
1897                 if (toggle_ok) {
1898                         if (enable)
1899                                 glEnable(GL_MULTISAMPLE);
1900                         else
1901                                 glDisable(GL_MULTISAMPLE);
1902                 }
1903 #else
1904                 if (enable)
1905                         glEnable(GL_MULTISAMPLE);
1906                 else
1907                         glDisable(GL_MULTISAMPLE);
1908 #endif
1909         }
1910 }
1911
1912 /* Default OpenGL State */
1913
1914 void GPU_state_init(void)
1915 {
1916         /* also called when doing opengl rendering and in the game engine */
1917         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
1918         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
1919         int a, x, y;
1920         GLubyte pat[32*32];
1921         const GLubyte *patc= pat;
1922         
1923         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
1924         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
1925         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1926         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35);
1927
1928         GPU_default_lights();
1929         
1930         glDepthFunc(GL_LEQUAL);
1931         /* scaling matrices */
1932         glEnable(GL_NORMALIZE);
1933
1934         glShadeModel(GL_FLAT);
1935
1936         glDisable(GL_ALPHA_TEST);
1937         glDisable(GL_BLEND);
1938         glDisable(GL_DEPTH_TEST);
1939         glDisable(GL_FOG);
1940         glDisable(GL_LIGHTING);
1941         glDisable(GL_LOGIC_OP);
1942         glDisable(GL_STENCIL_TEST);
1943         glDisable(GL_TEXTURE_1D);
1944         glDisable(GL_TEXTURE_2D);
1945
1946         /* default disabled, enable should be local per function */
1947         glDisableClientState(GL_VERTEX_ARRAY);
1948         glDisableClientState(GL_NORMAL_ARRAY);
1949         glDisableClientState(GL_COLOR_ARRAY);
1950         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1951         
1952         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
1953         glPixelTransferi(GL_RED_SCALE, 1);
1954         glPixelTransferi(GL_RED_BIAS, 0);
1955         glPixelTransferi(GL_GREEN_SCALE, 1);
1956         glPixelTransferi(GL_GREEN_BIAS, 0);
1957         glPixelTransferi(GL_BLUE_SCALE, 1);
1958         glPixelTransferi(GL_BLUE_BIAS, 0);
1959         glPixelTransferi(GL_ALPHA_SCALE, 1);
1960         glPixelTransferi(GL_ALPHA_BIAS, 0);
1961         
1962         glPixelTransferi(GL_DEPTH_BIAS, 0);
1963         glPixelTransferi(GL_DEPTH_SCALE, 1);
1964         glDepthRange(0.0, 1.0);
1965         
1966         a= 0;
1967         for (x=0; x<32; x++) {
1968                 for (y=0; y<4; y++) {
1969                         if ( (x) & 1) pat[a++]= 0x88;
1970                         else pat[a++]= 0x22;
1971                 }
1972         }
1973         
1974         glPolygonStipple(patc);
1975
1976         glMatrixMode(GL_TEXTURE);
1977         glLoadIdentity();
1978         glMatrixMode(GL_MODELVIEW);
1979
1980         glFrontFace(GL_CCW);
1981         glCullFace(GL_BACK);
1982         glDisable(GL_CULL_FACE);
1983
1984         gpu_multisample(false);
1985 }
1986
1987 #ifdef DEBUG
1988 /* debugging aid */
1989 static void gpu_state_print_fl_ex(const char *name, GLenum type)
1990 {
1991         const unsigned char err_mark[4] = {0xff, 0xff, 0xff, 0xff};
1992
1993         float value[32];
1994         int a;
1995
1996         memset(value, 0xff, sizeof(value));
1997         glGetFloatv(type, value);
1998
1999         printf("%s: ", name);
2000         for (a = 0; a < 32; a++) {
2001                 if (memcmp(&value[a], err_mark, sizeof(value[a])) == 0) {
2002                         break;
2003                 }
2004                 printf("%.2f ", value[a]);
2005         }
2006         printf("\n");
2007 }
2008
2009 #define gpu_state_print_fl(val) gpu_state_print_fl_ex(#val, val)
2010
2011 void GPU_state_print(void)
2012 {
2013         gpu_state_print_fl(GL_ACCUM_ALPHA_BITS);
2014         gpu_state_print_fl(GL_ACCUM_BLUE_BITS);
2015         gpu_state_print_fl(GL_ACCUM_CLEAR_VALUE);
2016         gpu_state_print_fl(GL_ACCUM_GREEN_BITS);
2017         gpu_state_print_fl(GL_ACCUM_RED_BITS);
2018         gpu_state_print_fl(GL_ACTIVE_TEXTURE);
2019         gpu_state_print_fl(GL_ALIASED_LINE_WIDTH_RANGE);
2020         gpu_state_print_fl(GL_ALIASED_POINT_SIZE_RANGE);
2021         gpu_state_print_fl(GL_ALPHA_BIAS);
2022         gpu_state_print_fl(GL_ALPHA_BITS);
2023         gpu_state_print_fl(GL_ALPHA_SCALE);
2024         gpu_state_print_fl(GL_ALPHA_TEST);
2025         gpu_state_print_fl(GL_ALPHA_TEST_FUNC);
2026         gpu_state_print_fl(GL_ALPHA_TEST_REF);
2027         gpu_state_print_fl(GL_ARRAY_BUFFER_BINDING);
2028         gpu_state_print_fl(GL_ATTRIB_STACK_DEPTH);
2029         gpu_state_print_fl(GL_AUTO_NORMAL);
2030         gpu_state_print_fl(GL_AUX_BUFFERS);
2031         gpu_state_print_fl(GL_BLEND);
2032         gpu_state_print_fl(GL_BLEND_COLOR);
2033         gpu_state_print_fl(GL_BLEND_DST_ALPHA);
2034         gpu_state_print_fl(GL_BLEND_DST_RGB);
2035         gpu_state_print_fl(GL_BLEND_EQUATION_ALPHA);
2036         gpu_state_print_fl(GL_BLEND_EQUATION_RGB);
2037         gpu_state_print_fl(GL_BLEND_SRC_ALPHA);
2038         gpu_state_print_fl(GL_BLEND_SRC_RGB);
2039         gpu_state_print_fl(GL_BLUE_BIAS);
2040         gpu_state_print_fl(GL_BLUE_BITS);
2041         gpu_state_print_fl(GL_BLUE_SCALE);
2042         gpu_state_print_fl(GL_CLIENT_ACTIVE_TEXTURE);
2043         gpu_state_print_fl(GL_CLIENT_ATTRIB_STACK_DEPTH);
2044         gpu_state_print_fl(GL_CLIP_PLANE0);
2045         gpu_state_print_fl(GL_COLOR_ARRAY);
2046         gpu_state_print_fl(GL_COLOR_ARRAY_BUFFER_BINDING);
2047         gpu_state_print_fl(GL_COLOR_ARRAY_SIZE);
2048         gpu_state_print_fl(GL_COLOR_ARRAY_STRIDE);
2049         gpu_state_print_fl(GL_COLOR_ARRAY_TYPE);
2050         gpu_state_print_fl(GL_COLOR_CLEAR_VALUE);
2051         gpu_state_print_fl(GL_COLOR_LOGIC_OP);
2052         gpu_state_print_fl(GL_COLOR_MATERIAL);
2053         gpu_state_print_fl(GL_COLOR_MATERIAL_FACE);
2054         gpu_state_print_fl(GL_COLOR_MATERIAL_PARAMETER);
2055         gpu_state_print_fl(GL_COLOR_MATRIX);
2056         gpu_state_print_fl(GL_COLOR_MATRIX_STACK_DEPTH);
2057         gpu_state_print_fl(GL_COLOR_SUM);
2058         gpu_state_print_fl(GL_COLOR_TABLE);
2059         gpu_state_print_fl(GL_COLOR_WRITEMASK);
2060         gpu_state_print_fl(GL_COMPRESSED_TEXTURE_FORMATS);
2061         gpu_state_print_fl(GL_CONVOLUTION_1D);
2062         gpu_state_print_fl(GL_CONVOLUTION_2D);
2063         gpu_state_print_fl(GL_CULL_FACE);
2064         gpu_state_print_fl(GL_CULL_FACE_MODE);
2065         gpu_state_print_fl(GL_CURRENT_COLOR);
2066         gpu_state_print_fl(GL_CURRENT_FOG_COORD);
2067         gpu_state_print_fl(GL_CURRENT_INDEX);
2068         gpu_state_print_fl(GL_CURRENT_NORMAL);
2069         gpu_state_print_fl(GL_CURRENT_PROGRAM);
2070         gpu_state_print_fl(GL_CURRENT_RASTER_COLOR);
2071         gpu_state_print_fl(GL_CURRENT_RASTER_DISTANCE);
2072         gpu_state_print_fl(GL_CURRENT_RASTER_INDEX);
2073         gpu_state_print_fl(GL_CURRENT_RASTER_POSITION);
2074         gpu_state_print_fl(GL_CURRENT_RASTER_POSITION_VALID);
2075         gpu_state_print_fl(GL_CURRENT_RASTER_SECONDARY_COLOR);
2076         gpu_state_print_fl(GL_CURRENT_RASTER_TEXTURE_COORDS);
2077         gpu_state_print_fl(GL_CURRENT_SECONDARY_COLOR);
2078         gpu_state_print_fl(GL_CURRENT_TEXTURE_COORDS);
2079         gpu_state_print_fl(GL_DEPTH_BIAS);
2080         gpu_state_print_fl(GL_DEPTH_BITS);
2081         gpu_state_print_fl(GL_DEPTH_CLEAR_VALUE);
2082         gpu_state_print_fl(GL_DEPTH_FUNC);
2083         gpu_state_print_fl(GL_DEPTH_RANGE);
2084         gpu_state_print_fl(GL_DEPTH_SCALE);
2085         gpu_state_print_fl(GL_DEPTH_TEST);
2086         gpu_state_print_fl(GL_DEPTH_WRITEMASK);
2087         gpu_state_print_fl(GL_DITHER);
2088         gpu_state_print_fl(GL_DOUBLEBUFFER);
2089         gpu_state_print_fl(GL_DRAW_BUFFER);
2090         gpu_state_print_fl(GL_DRAW_BUFFER0);
2091         gpu_state_print_fl(GL_EDGE_FLAG);
2092         gpu_state_print_fl(GL_EDGE_FLAG_ARRAY);
2093         gpu_state_print_fl(GL_EDGE_FLAG_ARRAY_BUFFER_BINDING);
2094         gpu_state_print_fl(GL_EDGE_FLAG_ARRAY_STRIDE);
2095         gpu_state_print_fl(GL_ELEMENT_ARRAY_BUFFER_BINDING);
2096         gpu_state_print_fl(GL_FEEDBACK_BUFFER_SIZE);
2097         gpu_state_print_fl(GL_FEEDBACK_BUFFER_TYPE);
2098         gpu_state_print_fl(GL_FOG);
2099         gpu_state_print_fl(GL_FOG_COLOR);
2100         gpu_state_print_fl(GL_FOG_COORD_ARRAY);
2101         gpu_state_print_fl(GL_FOG_COORD_ARRAY_BUFFER_BINDING);
2102         gpu_state_print_fl(GL_FOG_COORD_ARRAY_STRIDE);
2103         gpu_state_print_fl(GL_FOG_COORD_ARRAY_TYPE);
2104         gpu_state_print_fl(GL_FOG_COORD_SRC);
2105         gpu_state_print_fl(GL_FOG_DENSITY);
2106         gpu_state_print_fl(GL_FOG_END);
2107         gpu_state_print_fl(GL_FOG_HINT);
2108         gpu_state_print_fl(GL_FOG_INDEX);
2109         gpu_state_print_fl(GL_FOG_MODE);
2110         gpu_state_print_fl(GL_FOG_START);
2111         gpu_state_print_fl(GL_FRAGMENT_PROGRAM_ARB);
2112         gpu_state_print_fl(GL_FRAGMENT_SHADER_DERIVATIVE_HINT);
2113         gpu_state_print_fl(GL_FRONT_FACE);
2114         gpu_state_print_fl(GL_GENERATE_MIPMAP_HINT);
2115         gpu_state_print_fl(GL_GREEN_BIAS);
2116         gpu_state_print_fl(GL_GREEN_BITS);
2117         gpu_state_print_fl(GL_GREEN_SCALE);
2118         gpu_state_print_fl(GL_HISTOGRAM);
2119         gpu_state_print_fl(GL_INDEX_ARRAY);
2120         gpu_state_print_fl(GL_INDEX_ARRAY_BUFFER_BINDING);
2121         gpu_state_print_fl(GL_INDEX_ARRAY_STRIDE);
2122         gpu_state_print_fl(GL_INDEX_ARRAY_TYPE);
2123         gpu_state_print_fl(GL_INDEX_BITS);
2124         gpu_state_print_fl(GL_INDEX_CLEAR_VALUE);
2125         gpu_state_print_fl(GL_INDEX_LOGIC_OP);
2126         gpu_state_print_fl(GL_INDEX_MODE);
2127         gpu_state_print_fl(GL_INDEX_OFFSET);
2128         gpu_state_print_fl(GL_INDEX_SHIFT);
2129         gpu_state_print_fl(GL_INDEX_WRITEMASK);
2130         gpu_state_print_fl(GL_LIGHT0);
2131         gpu_state_print_fl(GL_LIGHT1);
2132         gpu_state_print_fl(GL_LIGHT2);
2133         gpu_state_print_fl(GL_LIGHT3);
2134         gpu_state_print_fl(GL_LIGHT4);
2135         gpu_state_print_fl(GL_LIGHT5);
2136         gpu_state_print_fl(GL_LIGHT6);
2137         gpu_state_print_fl(GL_LIGHT7);
2138         gpu_state_print_fl(GL_LIGHTING);
2139         gpu_state_print_fl(GL_LIGHT_MODEL_AMBIENT);
2140         gpu_state_print_fl(GL_LIGHT_MODEL_COLOR_CONTROL);
2141         gpu_state_print_fl(GL_LIGHT_MODEL_LOCAL_VIEWER);
2142         gpu_state_print_fl(GL_LIGHT_MODEL_TWO_SIDE);
2143         gpu_state_print_fl(GL_LINE_SMOOTH);
2144         gpu_state_print_fl(GL_LINE_SMOOTH_HINT);
2145         gpu_state_print_fl(GL_LINE_STIPPLE);
2146         gpu_state_print_fl(GL_LINE_STIPPLE_PATTERN);
2147         gpu_state_print_fl(GL_LINE_STIPPLE_REPEAT);
2148         gpu_state_print_fl(GL_LINE_WIDTH);
2149         gpu_state_print_fl(GL_LINE_WIDTH_GRANULARITY);
2150         gpu_state_print_fl(GL_LINE_WIDTH_RANGE);
2151         gpu_state_print_fl(GL_LIST_BASE);
2152         gpu_state_print_fl(GL_LIST_INDEX);
2153         gpu_state_print_fl(GL_LIST_MODE);
2154         gpu_state_print_fl(GL_LOGIC_OP);
2155         gpu_state_print_fl(GL_LOGIC_OP_MODE);
2156         gpu_state_print_fl(GL_MAP1_COLOR_4);
2157         gpu_state_print_fl(GL_MAP1_GRID_DOMAIN);
2158         gpu_state_print_fl(GL_MAP1_GRID_SEGMENTS);
2159         gpu_state_print_fl(GL_MAP1_INDEX);
2160         gpu_state_print_fl(GL_MAP1_NORMAL);
2161         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_1);
2162         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_2);
2163         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_3);
2164         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_4);
2165         gpu_state_print_fl(GL_MAP1_VERTEX_3);
2166         gpu_state_print_fl(GL_MAP1_VERTEX_4);
2167         gpu_state_print_fl(GL_MAP2_COLOR_4);
2168         gpu_state_print_fl(GL_MAP2_GRID_DOMAIN);
2169         gpu_state_print_fl(GL_MAP2_GRID_SEGMENTS);
2170         gpu_state_print_fl(GL_MAP2_INDEX);
2171         gpu_state_print_fl(GL_MAP2_NORMAL);
2172         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_1);
2173         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_2);
2174         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_3);
2175         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_4);
2176         gpu_state_print_fl(GL_MAP2_VERTEX_3);
2177         gpu_state_print_fl(GL_MAP2_VERTEX_4);
2178         gpu_state_print_fl(GL_MAP_COLOR);
2179         gpu_state_print_fl(GL_MAP_STENCIL);
2180         gpu_state_print_fl(GL_MATRIX_MODE);
2181         gpu_state_print_fl(GL_MAX_3D_TEXTURE_SIZE);
2182         gpu_state_print_fl(GL_MAX_ATTRIB_STACK_DEPTH);
2183         gpu_state_print_fl(GL_MAX_CLIENT_ATTRIB_STACK_DEPTH);
2184         gpu_state_print_fl(GL_MAX_CLIP_PLANES);
2185         gpu_state_print_fl(GL_MAX_COLOR_MATRIX_STACK_DEPTH);
2186         gpu_state_print_fl(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
2187         gpu_state_print_fl(GL_MAX_CUBE_MAP_TEXTURE_SIZE);
2188         gpu_state_print_fl(GL_MAX_DRAW_BUFFERS);
2189         gpu_state_print_fl(GL_MAX_ELEMENTS_INDICES);
2190         gpu_state_print_fl(GL_MAX_ELEMENTS_VERTICES);
2191         gpu_state_print_fl(GL_MAX_EVAL_ORDER);
2192         gpu_state_print_fl(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS);
2193         gpu_state_print_fl(GL_MAX_LIGHTS);
2194         gpu_state_print_fl(GL_MAX_LIST_NESTING);
2195         gpu_state_print_fl(GL_MAX_MODELVIEW_STACK_DEPTH);
2196         gpu_state_print_fl(GL_MAX_NAME_STACK_DEPTH);
2197         gpu_state_print_fl(GL_MAX_PIXEL_MAP_TABLE);
2198         gpu_state_print_fl(GL_MAX_PROJECTION_STACK_DEPTH);
2199         gpu_state_print_fl(GL_MAX_TEXTURE_COORDS);
2200         gpu_state_print_fl(GL_MAX_TEXTURE_IMAGE_UNITS);
2201         gpu_state_print_fl(GL_MAX_TEXTURE_LOD_BIAS);
2202         gpu_state_print_fl(GL_MAX_TEXTURE_SIZE);
2203         gpu_state_print_fl(GL_MAX_TEXTURE_STACK_DEPTH);
2204         gpu_state_print_fl(GL_MAX_TEXTURE_UNITS);
2205         gpu_state_print_fl(GL_MAX_VARYING_FLOATS);
2206         gpu_state_print_fl(GL_MAX_VERTEX_ATTRIBS);
2207         gpu_state_print_fl(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2208         gpu_state_print_fl(GL_MAX_VERTEX_UNIFORM_COMPONENTS);
2209         gpu_state_print_fl(GL_MAX_VIEWPORT_DIMS);
2210         gpu_state_print_fl(GL_MINMAX);
2211         gpu_state_print_fl(GL_MODELVIEW_MATRIX);
2212         gpu_state_print_fl(GL_MODELVIEW_STACK_DEPTH);
2213         gpu_state_print_fl(GL_MULTISAMPLE);
2214         gpu_state_print_fl(GL_MULTISAMPLE_ARB);
2215         gpu_state_print_fl(GL_NAME_STACK_DEPTH);
2216         gpu_state_print_fl(GL_NORMALIZE);
2217         gpu_state_print_fl(GL_NORMAL_ARRAY);
2218         gpu_state_print_fl(GL_NORMAL_ARRAY_BUFFER_BINDING);
2219         gpu_state_print_fl(GL_NORMAL_ARRAY_STRIDE);
2220         gpu_state_print_fl(GL_NORMAL_ARRAY_TYPE);
2221         gpu_state_print_fl(GL_NUM_COMPRESSED_TEXTURE_FORMATS);
2222         gpu_state_print_fl(GL_PACK_ALIGNMENT);
2223         gpu_state_print_fl(GL_PACK_IMAGE_HEIGHT);
2224         gpu_state_print_fl(GL_PACK_LSB_FIRST);
2225         gpu_state_print_fl(GL_PACK_ROW_LENGTH);
2226         gpu_state_print_fl(GL_PACK_SKIP_IMAGES);
2227         gpu_state_print_fl(GL_PACK_SKIP_PIXELS);
2228         gpu_state_print_fl(GL_PACK_SKIP_ROWS);
2229         gpu_state_print_fl(GL_PACK_SWAP_BYTES);
2230         gpu_state_print_fl(GL_PERSPECTIVE_CORRECTION_HINT);
2231         gpu_state_print_fl(GL_PIXEL_MAP_A_TO_A_SIZE);
2232         gpu_state_print_fl(GL_PIXEL_MAP_B_TO_B_SIZE);
2233         gpu_state_print_fl(GL_PIXEL_MAP_G_TO_G_SIZE);
2234         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_A_SIZE);
2235         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_B_SIZE);
2236         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_G_SIZE);
2237         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_I_SIZE);
2238         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_R_SIZE);
2239         gpu_state_print_fl(GL_PIXEL_MAP_R_TO_R_SIZE);
2240         gpu_state_print_fl(GL_PIXEL_MAP_S_TO_S_SIZE);
2241         gpu_state_print_fl(GL_PIXEL_PACK_BUFFER_BINDING);
2242         gpu_state_print_fl(GL_PIXEL_UNPACK_BUFFER_BINDING);
2243         gpu_state_print_fl(GL_POINT_DISTANCE_ATTENUATION);
2244         gpu_state_print_fl(GL_POINT_FADE_THRESHOLD_SIZE);
2245         gpu_state_print_fl(GL_POINT_SIZE);
2246         gpu_state_print_fl(GL_POINT_SIZE_GRANULARITY);
2247         gpu_state_print_fl(GL_POINT_SIZE_MAX);
2248         gpu_state_print_fl(GL_POINT_SIZE_MIN);
2249         gpu_state_print_fl(GL_POINT_SIZE_RANGE);
2250         gpu_state_print_fl(GL_POINT_SMOOTH);
2251         gpu_state_print_fl(GL_POINT_SMOOTH_HINT);
2252         gpu_state_print_fl(GL_POINT_SPRITE);
2253         gpu_state_print_fl(GL_POLYGON_MODE);
2254         gpu_state_print_fl(GL_POLYGON_OFFSET_FACTOR);
2255         gpu_state_print_fl(GL_POLYGON_OFFSET_FILL);
2256         gpu_state_print_fl(GL_POLYGON_OFFSET_LINE);
2257         gpu_state_print_fl(GL_POLYGON_OFFSET_POINT);
2258         gpu_state_print_fl(GL_POLYGON_OFFSET_UNITS);
2259         gpu_state_print_fl(GL_POLYGON_SMOOTH);
2260         gpu_state_print_fl(GL_POLYGON_SMOOTH_HINT);
2261         gpu_state_print_fl(GL_POLYGON_STIPPLE);
2262         gpu_state_print_fl(GL_POST_COLOR_MATRIX_ALPHA_BIAS);
2263         gpu_state_print_fl(GL_POST_COLOR_MATRIX_ALPHA_SCALE);
2264         gpu_state_print_fl(GL_POST_COLOR_MATRIX_BLUE_BIAS);
2265         gpu_state_print_fl(GL_POST_COLOR_MATRIX_BLUE_SCALE);
2266         gpu_state_print_fl(GL_POST_COLOR_MATRIX_COLOR_TABLE);
2267         gpu_state_print_fl(GL_POST_COLOR_MATRIX_GREEN_BIAS);
2268         gpu_state_print_fl(GL_POST_COLOR_MATRIX_GREEN_SCALE);
2269         gpu_state_print_fl(GL_POST_COLOR_MATRIX_RED_BIAS);
2270         gpu_state_print_fl(GL_POST_COLOR_MATRIX_RED_SCALE);
2271         gpu_state_print_fl(GL_POST_CONVOLUTION_ALPHA_BIAS);
2272         gpu_state_print_fl(GL_POST_CONVOLUTION_ALPHA_SCALE);
2273         gpu_state_print_fl(GL_POST_CONVOLUTION_BLUE_BIAS);
2274         gpu_state_print_fl(GL_POST_CONVOLUTION_BLUE_SCALE);
2275         gpu_state_print_fl(GL_POST_CONVOLUTION_COLOR_TABLE);
2276         gpu_state_print_fl(GL_POST_CONVOLUTION_GREEN_BIAS);
2277         gpu_state_print_fl(GL_POST_CONVOLUTION_GREEN_SCALE);
2278         gpu_state_print_fl(GL_POST_CONVOLUTION_RED_BIAS);
2279         gpu_state_print_fl(GL_POST_CONVOLUTION_RED_SCALE);
2280         gpu_state_print_fl(GL_PROJECTION_MATRIX);
2281         gpu_state_print_fl(GL_PROJECTION_STACK_DEPTH);
2282         gpu_state_print_fl(GL_READ_BUFFER);
2283         gpu_state_print_fl(GL_RED_BIAS);
2284         gpu_state_print_fl(GL_RED_BITS);
2285         gpu_state_print_fl(GL_RED_SCALE);
2286         gpu_state_print_fl(GL_RENDER_MODE);
2287         gpu_state_print_fl(GL_RESCALE_NORMAL);
2288         gpu_state_print_fl(GL_RGBA_MODE);
2289         gpu_state_print_fl(GL_SAMPLES);
2290         gpu_state_print_fl(GL_SAMPLE_BUFFERS);
2291         gpu_state_print_fl(GL_SAMPLE_COVERAGE_INVERT);
2292         gpu_state_print_fl(GL_SAMPLE_COVERAGE_VALUE);
2293         gpu_state_print_fl(GL_SCISSOR_BOX);
2294         gpu_state_print_fl(GL_SCISSOR_TEST);
2295         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY);
2296         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_BUFFER_BINDING);
2297         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_SIZE);
2298         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_STRIDE);
2299         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_TYPE);
2300         gpu_state_print_fl(GL_SELECTION_BUFFER_SIZE);
2301         gpu_state_print_fl(GL_SEPARABLE_2D);
2302         gpu_state_print_fl(GL_SHADE_MODEL);
2303         gpu_state_print_fl(GL_SMOOTH_LINE_WIDTH_GRANULARITY);
2304         gpu_state_print_fl(GL_SMOOTH_LINE_WIDTH_RANGE);
2305         gpu_state_print_fl(GL_SMOOTH_POINT_SIZE_GRANULARITY);
2306         gpu_state_print_fl(GL_SMOOTH_POINT_SIZE_RANGE);
2307         gpu_state_print_fl(GL_STENCIL_BACK_FAIL);
2308         gpu_state_print_fl(GL_STENCIL_BACK_FUNC);
2309         gpu_state_print_fl(GL_STENCIL_BACK_PASS_DEPTH_FAIL);
2310         gpu_state_print_fl(GL_STENCIL_BACK_PASS_DEPTH_PASS);
2311         gpu_state_print_fl(GL_STENCIL_BACK_REF);
2312         gpu_state_print_fl(GL_STENCIL_BACK_VALUE_MASK);
2313         gpu_state_print_fl(GL_STENCIL_BACK_WRITEMASK);
2314         gpu_state_print_fl(GL_STENCIL_BITS);
2315         gpu_state_print_fl(GL_STENCIL_CLEAR_VALUE);
2316         gpu_state_print_fl(GL_STENCIL_FAIL);
2317         gpu_state_print_fl(GL_STENCIL_FUNC);
2318         gpu_state_print_fl(GL_STENCIL_PASS_DEPTH_FAIL);
2319         gpu_state_print_fl(GL_STENCIL_PASS_DEPTH_PASS);
2320         gpu_state_print_fl(GL_STENCIL_REF);
2321         gpu_state_print_fl(GL_STENCIL_TEST);
2322         gpu_state_print_fl(GL_STENCIL_VALUE_MASK);
2323         gpu_state_print_fl(GL_STENCIL_WRITEMASK);
2324         gpu_state_print_fl(GL_STEREO);
2325         gpu_state_print_fl(GL_SUBPIXEL_BITS);
2326         gpu_state_print_fl(GL_TEXTURE_1D);
2327         gpu_state_print_fl(GL_TEXTURE_2D);
2328         gpu_state_print_fl(GL_TEXTURE_3D);
2329         gpu_state_print_fl(GL_TEXTURE_BINDING_1D);
2330         gpu_state_print_fl(GL_TEXTURE_BINDING_2D);
2331         gpu_state_print_fl(GL_TEXTURE_BINDING_3D);
2332         gpu_state_print_fl(GL_TEXTURE_BINDING_CUBE_MAP);
2333         gpu_state_print_fl(GL_TEXTURE_COMPRESSION_HINT);
2334         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY);
2335         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING);
2336         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_SIZE);
2337         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_STRIDE);
2338         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_TYPE);
2339         gpu_state_print_fl(GL_TEXTURE_CUBE_MAP);
2340         gpu_state_print_fl(GL_TEXTURE_CUBE_MAP_ARB);
2341         gpu_state_print_fl(GL_TEXTURE_GEN_Q);
2342         gpu_state_print_fl(GL_TEXTURE_GEN_R);
2343         gpu_state_print_fl(GL_TEXTURE_GEN_S);
2344         gpu_state_print_fl(GL_TEXTURE_GEN_T);
2345         gpu_state_print_fl(GL_TEXTURE_MATRIX);
2346         gpu_state_print_fl(GL_TEXTURE_STACK_DEPTH);
2347         gpu_state_print_fl(GL_TRANSPOSE_COLOR_MATRIX);
2348         gpu_state_print_fl(GL_TRANSPOSE_MODELVIEW_MATRIX);
2349         gpu_state_print_fl(GL_TRANSPOSE_PROJECTION_MATRIX);
2350         gpu_state_print_fl(GL_TRANSPOSE_TEXTURE_MATRIX);
2351         gpu_state_print_fl(GL_UNPACK_ALIGNMENT);
2352         gpu_state_print_fl(GL_UNPACK_IMAGE_HEIGHT);
2353         gpu_state_print_fl(GL_UNPACK_LSB_FIRST);
2354         gpu_state_print_fl(GL_UNPACK_ROW_LENGTH);
2355         gpu_state_print_fl(GL_UNPACK_SKIP_IMAGES);
2356         gpu_state_print_fl(GL_UNPACK_SKIP_PIXELS);
2357         gpu_state_print_fl(GL_UNPACK_SKIP_ROWS);
2358         gpu_state_print_fl(GL_UNPACK_SWAP_BYTES);
2359         gpu_state_print_fl(GL_VERTEX_ARRAY);
2360         gpu_state_print_fl(GL_VERTEX_ARRAY_BUFFER_BINDING);
2361         gpu_state_print_fl(GL_VERTEX_ARRAY_SIZE);
2362         gpu_state_print_fl(GL_VERTEX_ARRAY_STRIDE);
2363         gpu_state_print_fl(GL_VERTEX_ARRAY_TYPE);
2364         gpu_state_print_fl(GL_VERTEX_PROGRAM_POINT_SIZE);
2365         gpu_state_print_fl(GL_VERTEX_PROGRAM_TWO_SIDE);
2366         gpu_state_print_fl(GL_VIEWPORT);
2367         gpu_state_print_fl(GL_ZOOM_X);
2368         gpu_state_print_fl(GL_ZOOM_Y);
2369 }
2370
2371 #undef gpu_state_print_fl
2372
2373 #endif