8abe7d5534ae8cdd7b16c101ce7afeb4c5cc583d
[blender.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 "GL/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                 glGetIntegerv(GL_UNPACK_ROW_LENGTH, &row_length);
1043                 glGetIntegerv(GL_UNPACK_SKIP_PIXELS, &skip_pixels);
1044                 glGetIntegerv(GL_UNPACK_SKIP_ROWS, &skip_rows);
1045
1046                 /* if color correction is needed, we must update the part that needs updating. */
1047                 if (ibuf->rect_float) {
1048                         float *buffer = MEM_mallocN(w*h*sizeof(float)*4, "temp_texpaint_float_buf");
1049                         bool is_data = (ima->tpageflag & IMA_GLBIND_IS_DATA) != 0;
1050                         IMB_partial_rect_from_float(ibuf, buffer, x, y, w, h, is_data);
1051
1052                         if (GPU_check_scaled_image(ibuf, ima, buffer, x, y, w, h)) {
1053                                 MEM_freeN(buffer);
1054                                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
1055                                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
1056                                 glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
1057                                 BKE_image_release_ibuf(ima, ibuf, NULL);
1058                                 return;
1059                         }
1060
1061                         glBindTexture(GL_TEXTURE_2D, ima->bindcode);
1062                         glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA,
1063                                         GL_FLOAT, buffer);
1064
1065                         MEM_freeN(buffer);
1066
1067                         /* we have already accounted for the case where GTS.gpu_mipmap is false
1068                          * so we will be using GPU mipmap generation here */
1069                         if (GPU_get_mipmap()) {
1070                                 gpu_generate_mipmap(GL_TEXTURE_2D);
1071                         }
1072                         else {
1073                                 ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
1074                         }
1075
1076                         BKE_image_release_ibuf(ima, ibuf, NULL);
1077                         return;
1078                 }
1079
1080                 if (GPU_check_scaled_image(ibuf, ima, NULL, x, y, w, h)) {
1081                         glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
1082                         glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
1083                         glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
1084                         BKE_image_release_ibuf(ima, ibuf, NULL);
1085                         return;
1086                 }
1087
1088                 glBindTexture(GL_TEXTURE_2D, ima->bindcode);
1089
1090                 glPixelStorei(GL_UNPACK_ROW_LENGTH, ibuf->x);
1091                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
1092                 glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
1093
1094                 glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, w, h, GL_RGBA,
1095                         GL_UNSIGNED_BYTE, ibuf->rect);
1096
1097                 glPixelStorei(GL_UNPACK_ROW_LENGTH, row_length);
1098                 glPixelStorei(GL_UNPACK_SKIP_PIXELS, skip_pixels);
1099                 glPixelStorei(GL_UNPACK_SKIP_ROWS, skip_rows);
1100
1101                 /* see comment above as to why we are using gpu mipmap generation here */
1102                 if (GPU_get_mipmap()) {
1103                         gpu_generate_mipmap(GL_TEXTURE_2D);
1104                 }
1105                 else {
1106                         ima->tpageflag &= ~IMA_MIPMAP_COMPLETE;
1107                 }
1108         }
1109
1110         BKE_image_release_ibuf(ima, ibuf, NULL);
1111 }
1112
1113 void GPU_update_images_framechange(void)
1114 {
1115         Image *ima;
1116         
1117         for (ima=G.main->image.first; ima; ima=ima->id.next) {
1118                 if (ima->tpageflag & IMA_TWINANIM) {
1119                         if (ima->twend >= ima->xrep*ima->yrep)
1120                                 ima->twend= ima->xrep*ima->yrep-1;
1121                 
1122                         /* check: is bindcode not in the array? free. (to do) */
1123                         
1124                         ima->lastframe++;
1125                         if (ima->lastframe > ima->twend)
1126                                 ima->lastframe= ima->twsta;
1127                 }
1128         }
1129 }
1130
1131 int GPU_update_image_time(Image *ima, double time)
1132 {
1133         int     inc = 0;
1134         float   diff;
1135         int     newframe;
1136
1137         if (!ima)
1138                 return 0;
1139
1140         if (ima->lastupdate<0)
1141                 ima->lastupdate = 0;
1142
1143         if (ima->lastupdate > (float)time)
1144                 ima->lastupdate=(float)time;
1145
1146         if (ima->tpageflag & IMA_TWINANIM) {
1147                 if (ima->twend >= ima->xrep*ima->yrep) ima->twend= ima->xrep*ima->yrep-1;
1148                 
1149                 /* check: is the bindcode not in the array? Then free. (still to do) */
1150                 
1151                 diff = (float)((float)time - ima->lastupdate);
1152                 inc = (int)(diff*(float)ima->animspeed);
1153
1154                 ima->lastupdate+=((float)inc/(float)ima->animspeed);
1155
1156                 newframe = ima->lastframe+inc;
1157
1158                 if (newframe > (int)ima->twend) {
1159                         if (ima->twend-ima->twsta != 0)
1160                                 newframe = (int)ima->twsta-1 + (newframe-ima->twend)%(ima->twend-ima->twsta);
1161                         else
1162                                 newframe = ima->twsta;
1163                 }
1164
1165                 ima->lastframe = newframe;
1166         }
1167
1168         return inc;
1169 }
1170
1171
1172 void GPU_free_smoke(SmokeModifierData *smd)
1173 {
1174         if (smd->type & MOD_SMOKE_TYPE_DOMAIN && smd->domain) {
1175                 if (smd->domain->tex)
1176                         GPU_texture_free(smd->domain->tex);
1177                 smd->domain->tex = NULL;
1178
1179                 if (smd->domain->tex_shadow)
1180                         GPU_texture_free(smd->domain->tex_shadow);
1181                 smd->domain->tex_shadow = NULL;
1182
1183                 if (smd->domain->tex_flame)
1184                         GPU_texture_free(smd->domain->tex_flame);
1185                 smd->domain->tex_flame = NULL;
1186         }
1187 }
1188
1189 void GPU_create_smoke(SmokeModifierData *smd, int highres)
1190 {
1191 #ifdef WITH_SMOKE
1192         if (smd->type & MOD_SMOKE_TYPE_DOMAIN) {
1193                 SmokeDomainSettings *sds = smd->domain;
1194                 if (!sds->tex && !highres) {
1195                         /* rgba texture for color + density */
1196                         if (smoke_has_colors(sds->fluid)) {
1197                                 float *data = MEM_callocN(sizeof(float)*sds->total_cells*4, "smokeColorTexture");
1198                                 smoke_get_rgba(sds->fluid, data, 0);
1199                                 sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 4, data);
1200                                 MEM_freeN(data);
1201                         }
1202                         /* density only */
1203                         else {
1204                                 sds->tex = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 1, smoke_get_density(sds->fluid));
1205                         }
1206                         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;
1207                 }
1208                 else if (!sds->tex && highres) {
1209                         /* rgba texture for color + density */
1210                         if (smoke_turbulence_has_colors(sds->wt)) {
1211                                 float *data = MEM_callocN(sizeof(float)*smoke_turbulence_get_cells(sds->wt)*4, "smokeColorTexture");
1212                                 smoke_turbulence_get_rgba(sds->wt, data, 0);
1213                                 sds->tex = GPU_texture_create_3D(sds->res_wt[0], sds->res_wt[1], sds->res_wt[2], 4, data);
1214                                 MEM_freeN(data);
1215                         }
1216                         /* density only */
1217                         else {
1218                                 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));
1219                         }
1220                         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;
1221                 }
1222
1223                 sds->tex_shadow = GPU_texture_create_3D(sds->res[0], sds->res[1], sds->res[2], 1, sds->shadow);
1224         }
1225 #else // WITH_SMOKE
1226         (void)highres;
1227         smd->domain->tex= NULL;
1228         smd->domain->tex_flame= NULL;
1229         smd->domain->tex_shadow= NULL;
1230 #endif // WITH_SMOKE
1231 }
1232
1233 static LinkNode *image_free_queue = NULL;
1234
1235 static void gpu_queue_image_for_free(Image *ima)
1236 {
1237         BLI_lock_thread(LOCK_OPENGL);
1238         BLI_linklist_prepend(&image_free_queue, ima);
1239         BLI_unlock_thread(LOCK_OPENGL);
1240 }
1241
1242 void GPU_free_unused_buffers(void)
1243 {
1244         LinkNode *node;
1245         Image *ima;
1246
1247         if (!BLI_thread_is_main())
1248                 return;
1249
1250         BLI_lock_thread(LOCK_OPENGL);
1251
1252         /* images */
1253         for (node=image_free_queue; node; node=node->next) {
1254                 ima = node->link;
1255
1256                 /* check in case it was freed in the meantime */
1257                 if (G.main && BLI_findindex(&G.main->image, ima) != -1)
1258                         GPU_free_image(ima);
1259         }
1260
1261         BLI_linklist_free(image_free_queue, NULL);
1262         image_free_queue = NULL;
1263
1264         /* vbo buffers */
1265         GPU_global_buffer_pool_free_unused();
1266
1267         BLI_unlock_thread(LOCK_OPENGL);
1268 }
1269
1270 void GPU_free_image(Image *ima)
1271 {
1272         if (!BLI_thread_is_main()) {
1273                 gpu_queue_image_for_free(ima);
1274                 return;
1275         }
1276
1277         /* free regular image binding */
1278         if (ima->bindcode) {
1279                 glDeleteTextures(1, (GLuint *)&ima->bindcode);
1280                 ima->bindcode= 0;
1281         }
1282
1283         /* free glsl image binding */
1284         if (ima->gputexture) {
1285                 GPU_texture_free(ima->gputexture);
1286                 ima->gputexture= NULL;
1287         }
1288
1289         /* free repeated image binding */
1290         if (ima->repbind) {
1291                 glDeleteTextures(ima->totbind, (GLuint *)ima->repbind);
1292         
1293                 MEM_freeN(ima->repbind);
1294                 ima->repbind= NULL;
1295         }
1296
1297         ima->tpageflag &= ~(IMA_MIPMAP_COMPLETE|IMA_GLBIND_IS_DATA);
1298 }
1299
1300 void GPU_free_images(void)
1301 {
1302         Image* ima;
1303
1304         if (G.main)
1305                 for (ima=G.main->image.first; ima; ima=ima->id.next)
1306                         GPU_free_image(ima);
1307 }
1308
1309 /* same as above but only free animated images */
1310 void GPU_free_images_anim(void)
1311 {
1312         Image* ima;
1313
1314         if (G.main)
1315                 for (ima=G.main->image.first; ima; ima=ima->id.next)
1316                         if (BKE_image_is_animated(ima))
1317                                 GPU_free_image(ima);
1318 }
1319
1320
1321 void GPU_free_images_old(void)
1322 {
1323         Image *ima;
1324         static int lasttime = 0;
1325         int ctime = (int)PIL_check_seconds_timer();
1326
1327         /*
1328          * Run garbage collector once for every collecting period of time
1329          * if textimeout is 0, that's the option to NOT run the collector
1330          */
1331         if (U.textimeout == 0 || ctime % U.texcollectrate || ctime == lasttime)
1332                 return;
1333
1334         /* of course not! */
1335         if (G.is_rendering)
1336                 return;
1337
1338         lasttime = ctime;
1339
1340         ima = G.main->image.first;
1341         while (ima) {
1342                 if ((ima->flag & IMA_NOCOLLECT) == 0 && ctime - ima->lastused > U.textimeout) {
1343                         /* If it's in GL memory, deallocate and set time tag to current time
1344                          * This gives textures a "second chance" to be used before dying. */
1345                         if (ima->bindcode || ima->repbind) {
1346                                 GPU_free_image(ima);
1347                                 ima->lastused = ctime;
1348                         }
1349                         /* Otherwise, just kill the buffers */
1350                         else {
1351                                 BKE_image_free_buffers(ima);
1352                         }
1353                 }
1354                 ima = ima->id.next;
1355         }
1356 }
1357
1358
1359 /* OpenGL Materials */
1360
1361 #define FIXEDMAT        8
1362
1363 /* OpenGL state caching for materials */
1364
1365 typedef struct GPUMaterialFixed {
1366         float diff[4];
1367         float spec[4];
1368         int hard;
1369 } GPUMaterialFixed; 
1370
1371 static struct GPUMaterialState {
1372         GPUMaterialFixed (*matbuf);
1373         GPUMaterialFixed matbuf_fixed[FIXEDMAT];
1374         int totmat;
1375
1376         Material **gmatbuf;
1377         Material *gmatbuf_fixed[FIXEDMAT];
1378         Material *gboundmat;
1379         Object *gob;
1380         Scene *gscene;
1381         int glay;
1382         bool gscenelock;
1383         float (*gviewmat)[4];
1384         float (*gviewinv)[4];
1385
1386         bool backface_culling;
1387
1388         GPUBlendMode *alphablend;
1389         GPUBlendMode alphablend_fixed[FIXEDMAT];
1390         bool use_alpha_pass, is_alpha_pass;
1391         bool use_matcaps;
1392
1393         int lastmatnr, lastretval;
1394         GPUBlendMode lastalphablend;
1395 } GMS = {NULL};
1396
1397 /* fixed function material, alpha handed by caller */
1398 static void gpu_material_to_fixed(GPUMaterialFixed *smat, const Material *bmat, const int gamma, const Object *ob, const int new_shading_nodes)
1399 {
1400         if (new_shading_nodes || bmat->mode & MA_SHLESS) {
1401                 copy_v3_v3(smat->diff, &bmat->r);
1402                 smat->diff[3]= 1.0;
1403
1404                 if (gamma)
1405                         linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
1406
1407                 zero_v4(smat->spec);
1408                 smat->hard= 0;
1409         }
1410         else {
1411                 mul_v3_v3fl(smat->diff, &bmat->r, bmat->ref + bmat->emit);
1412                 smat->diff[3]= 1.0; /* caller may set this to bmat->alpha */
1413
1414                 if (bmat->shade_flag & MA_OBCOLOR)
1415                         mul_v3_v3(smat->diff, ob->col);
1416                 
1417                 mul_v3_v3fl(smat->spec, &bmat->specr, bmat->spec);
1418                 smat->spec[3]= 1.0; /* always 1 */
1419                 smat->hard= CLAMPIS(bmat->har, 0, 128);
1420
1421                 if (gamma) {
1422                         linearrgb_to_srgb_v3_v3(smat->diff, smat->diff);
1423                         linearrgb_to_srgb_v3_v3(smat->spec, smat->spec);
1424                 }
1425         }
1426 }
1427
1428 static Material *gpu_active_node_material(Material *ma)
1429 {
1430         if (ma && ma->use_nodes && ma->nodetree) {
1431                 bNode *node = nodeGetActiveID(ma->nodetree, ID_MA);
1432
1433                 if (node)
1434                         return (Material *)node->id;
1435                 else
1436                         return NULL;
1437         }
1438
1439         return ma;
1440 }
1441
1442 void GPU_begin_object_materials(View3D *v3d, RegionView3D *rv3d, Scene *scene, Object *ob, bool glsl, bool *do_alpha_after)
1443 {
1444         Material *ma;
1445         GPUMaterial *gpumat;
1446         GPUBlendMode alphablend;
1447         int a;
1448         const bool gamma = BKE_scene_check_color_management_enabled(scene);
1449         const bool new_shading_nodes = BKE_scene_use_new_shading_nodes(scene);
1450         const bool use_matcap = (v3d->flag2 & V3D_SHOW_SOLID_MATCAP) != 0;  /* assumes v3d->defmaterial->preview is set */
1451
1452 #ifdef WITH_GAMEENGINE
1453         if (rv3d->rflag & RV3D_IS_GAME_ENGINE) {
1454                 ob = BKE_object_lod_matob_get(ob, scene);
1455         }
1456 #endif
1457
1458         /* initialize state */
1459         memset(&GMS, 0, sizeof(GMS));
1460         GMS.lastmatnr = -1;
1461         GMS.lastretval = -1;
1462         GMS.lastalphablend = GPU_BLEND_SOLID;
1463         GMS.use_matcaps = use_matcap;
1464
1465         GMS.backface_culling = (v3d->flag2 & V3D_BACKFACE_CULLING) != 0;
1466
1467         GMS.gob = ob;
1468         GMS.gscene = scene;
1469         GMS.totmat = use_matcap ? 1 : ob->totcol + 1;  /* materials start from 1, default material is 0 */
1470         GMS.glay= (v3d->localvd)? v3d->localvd->lay: v3d->lay; /* keep lamps visible in local view */
1471         GMS.gscenelock = (v3d->scenelock != 0);
1472         GMS.gviewmat= rv3d->viewmat;
1473         GMS.gviewinv= rv3d->viewinv;
1474
1475         /* alpha pass setup. there's various cases to handle here:
1476          * - object transparency on: only solid materials draw in the first pass,
1477          * and only transparent in the second 'alpha' pass.
1478          * - object transparency off: for glsl we draw both in a single pass, and
1479          * for solid we don't use transparency at all. */
1480         GMS.use_alpha_pass = (do_alpha_after != NULL);
1481         GMS.is_alpha_pass = (v3d->transp != false);
1482         if (GMS.use_alpha_pass)
1483                 *do_alpha_after = false;
1484         
1485         if (GMS.totmat > FIXEDMAT) {
1486                 GMS.matbuf= MEM_callocN(sizeof(GPUMaterialFixed)*GMS.totmat, "GMS.matbuf");
1487                 GMS.gmatbuf= MEM_callocN(sizeof(*GMS.gmatbuf)*GMS.totmat, "GMS.matbuf");
1488                 GMS.alphablend= MEM_callocN(sizeof(*GMS.alphablend)*GMS.totmat, "GMS.matbuf");
1489         }
1490         else {
1491                 GMS.matbuf= GMS.matbuf_fixed;
1492                 GMS.gmatbuf= GMS.gmatbuf_fixed;
1493                 GMS.alphablend= GMS.alphablend_fixed;
1494         }
1495
1496         /* viewport material, setup in space_view3d, defaults to matcap using ma->preview now */
1497         if (use_matcap) {
1498                 GMS.gmatbuf[0] = v3d->defmaterial;
1499                 GPU_material_matcap(scene, v3d->defmaterial);
1500
1501                 /* do material 1 too, for displists! */
1502                 memcpy(&GMS.matbuf[1], &GMS.matbuf[0], sizeof(GPUMaterialFixed));
1503         
1504                 GMS.alphablend[0]= GPU_BLEND_SOLID;
1505         }
1506         else {
1507         
1508                 /* no materials assigned? */
1509                 if (ob->totcol==0) {
1510                         gpu_material_to_fixed(&GMS.matbuf[0], &defmaterial, 0, ob, new_shading_nodes);
1511
1512                         /* do material 1 too, for displists! */
1513                         memcpy(&GMS.matbuf[1], &GMS.matbuf[0], sizeof(GPUMaterialFixed));
1514
1515                         if (glsl) {
1516                                 GMS.gmatbuf[0]= &defmaterial;
1517                                 GPU_material_from_blender(GMS.gscene, &defmaterial);
1518                         }
1519
1520                         GMS.alphablend[0]= GPU_BLEND_SOLID;
1521                 }
1522                 
1523                 /* setup materials */
1524                 for (a=1; a<=ob->totcol; a++) {
1525                         /* find a suitable material */
1526                         ma= give_current_material(ob, a);
1527                         if (!glsl && !new_shading_nodes) ma= gpu_active_node_material(ma);
1528                         if (ma==NULL) ma= &defmaterial;
1529
1530                         /* create glsl material if requested */
1531                         gpumat = (glsl)? GPU_material_from_blender(GMS.gscene, ma): NULL;
1532
1533                         if (gpumat) {
1534                                 /* do glsl only if creating it succeed, else fallback */
1535                                 GMS.gmatbuf[a]= ma;
1536                                 alphablend = GPU_material_alpha_blend(gpumat, ob->col);
1537                         }
1538                         else {
1539                                 /* fixed function opengl materials */
1540                                 gpu_material_to_fixed(&GMS.matbuf[a], ma, gamma, ob, new_shading_nodes);
1541
1542                                 if (GMS.use_alpha_pass && ((ma->mode & MA_TRANSP) || (new_shading_nodes && ma->alpha != 1.0f))) {
1543                                         GMS.matbuf[a].diff[3]= ma->alpha;
1544                                         alphablend = (ma->alpha == 1.0f)? GPU_BLEND_SOLID: GPU_BLEND_ALPHA;
1545                                 }
1546                                 else {
1547                                         GMS.matbuf[a].diff[3]= 1.0f;
1548                                         alphablend = GPU_BLEND_SOLID;
1549                                 }
1550                         }
1551
1552                         /* setting 'do_alpha_after = true' indicates this object needs to be
1553                          * drawn in a second alpha pass for improved blending */
1554                         if (do_alpha_after && !GMS.is_alpha_pass)
1555                                 if (ELEM(alphablend, GPU_BLEND_ALPHA, GPU_BLEND_ADD, GPU_BLEND_ALPHA_SORT))
1556                                         *do_alpha_after = true;
1557
1558                         GMS.alphablend[a]= alphablend;
1559                 }
1560         }
1561         
1562         /* let's start with a clean state */
1563         GPU_disable_material();
1564 }
1565
1566 int GPU_enable_material(int nr, void *attribs)
1567 {
1568         GPUVertexAttribs *gattribs = attribs;
1569         GPUMaterial *gpumat;
1570         GPUBlendMode alphablend;
1571
1572         /* no GPU_begin_object_materials, use default material */
1573         if (!GMS.matbuf) {
1574                 float diff[4], spec[4];
1575
1576                 memset(&GMS, 0, sizeof(GMS));
1577
1578                 mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit);
1579                 diff[3]= 1.0;
1580
1581                 mul_v3_v3fl(spec, &defmaterial.specr, defmaterial.spec);
1582                 spec[3]= 1.0;
1583
1584                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diff);
1585                 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
1586                 glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35); /* blender default */
1587
1588                 return 0;
1589         }
1590
1591         /* prevent index to use un-initialized array items */
1592         if (nr>=GMS.totmat)
1593                 nr= 0;
1594
1595         if (gattribs)
1596                 memset(gattribs, 0, sizeof(*gattribs));
1597
1598         /* keep current material */
1599         if (nr==GMS.lastmatnr)
1600                 return GMS.lastretval;
1601
1602         /* unbind glsl material */
1603         if (GMS.gboundmat) {
1604                 if (GMS.is_alpha_pass) glDepthMask(0);
1605                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
1606                 GMS.gboundmat= NULL;
1607         }
1608
1609         /* draw materials with alpha in alpha pass */
1610         GMS.lastmatnr = nr;
1611         GMS.lastretval = 1;
1612
1613         if (GMS.use_alpha_pass) {
1614                 GMS.lastretval = ELEM(GMS.alphablend[nr], GPU_BLEND_SOLID, GPU_BLEND_CLIP);
1615                 if (GMS.is_alpha_pass)
1616                         GMS.lastretval = !GMS.lastretval;
1617         }
1618         else
1619                 GMS.lastretval = !GMS.is_alpha_pass;
1620
1621         if (GMS.lastretval) {
1622                 /* for alpha pass, use alpha blend */
1623                 alphablend = GMS.alphablend[nr];
1624
1625                 if (gattribs && GMS.gmatbuf[nr]) {
1626                         /* bind glsl material and get attributes */
1627                         Material *mat = GMS.gmatbuf[nr];
1628                         float auto_bump_scale;
1629
1630                         gpumat = GPU_material_from_blender(GMS.gscene, mat);
1631                         GPU_material_vertex_attributes(gpumat, gattribs);
1632                         GPU_material_bind(gpumat, GMS.gob->lay, GMS.glay, 1.0, !(GMS.gob->mode & OB_MODE_TEXTURE_PAINT), GMS.gviewmat, GMS.gviewinv, GMS.gscenelock);
1633
1634                         auto_bump_scale = GMS.gob->derivedFinal != NULL ? GMS.gob->derivedFinal->auto_bump_scale : 1.0f;
1635                         GPU_material_bind_uniforms(gpumat, GMS.gob->obmat, GMS.gob->col, auto_bump_scale);
1636                         GMS.gboundmat= mat;
1637
1638                         /* for glsl use alpha blend mode, unless it's set to solid and
1639                          * we are already drawing in an alpha pass */
1640                         if (mat->game.alpha_blend != GPU_BLEND_SOLID)
1641                                 alphablend= mat->game.alpha_blend;
1642
1643                         if (GMS.is_alpha_pass) glDepthMask(1);
1644
1645                         if (GMS.backface_culling) {
1646                                 if (mat->game.flag)
1647                                         glEnable(GL_CULL_FACE);
1648                                 else
1649                                         glDisable(GL_CULL_FACE);
1650                         }
1651
1652                         if (GMS.use_matcaps)
1653                                 glColor3f(1.0, 1.0, 1.0f);
1654                 }
1655                 else {
1656                         /* or do fixed function opengl material */
1657                         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, GMS.matbuf[nr].diff);
1658                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, GMS.matbuf[nr].spec);
1659                         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, GMS.matbuf[nr].hard);
1660                 }
1661
1662                 /* set (alpha) blending mode */
1663                 GPU_set_material_alpha_blend(alphablend);
1664         }
1665
1666         return GMS.lastretval;
1667 }
1668
1669 void GPU_set_material_alpha_blend(int alphablend)
1670 {
1671         if (GMS.lastalphablend == alphablend)
1672                 return;
1673         
1674         gpu_set_alpha_blend(alphablend);
1675         GMS.lastalphablend = alphablend;
1676 }
1677
1678 int GPU_get_material_alpha_blend(void)
1679 {
1680         return GMS.lastalphablend;
1681 }
1682
1683 void GPU_disable_material(void)
1684 {
1685         GMS.lastmatnr= -1;
1686         GMS.lastretval= 1;
1687
1688         if (GMS.gboundmat) {
1689                 if (GMS.backface_culling)
1690                         glDisable(GL_CULL_FACE);
1691
1692                 if (GMS.is_alpha_pass) glDepthMask(0);
1693                 GPU_material_unbind(GPU_material_from_blender(GMS.gscene, GMS.gboundmat));
1694                 GMS.gboundmat= NULL;
1695         }
1696
1697         GPU_set_material_alpha_blend(GPU_BLEND_SOLID);
1698 }
1699
1700 void GPU_material_diffuse_get(int nr, float diff[4])
1701 {
1702         /* prevent index to use un-initialized array items */
1703         if (nr >= GMS.totmat)
1704                 nr = 0;
1705
1706         /* no GPU_begin_object_materials, use default material */
1707         if (!GMS.matbuf) {
1708                 mul_v3_v3fl(diff, &defmaterial.r, defmaterial.ref + defmaterial.emit);
1709         }
1710         else {
1711                 copy_v4_v4(diff, GMS.matbuf[nr].diff);
1712         }
1713 }
1714
1715 bool GPU_material_use_matcaps_get(void)
1716 {
1717         return GMS.use_matcaps;
1718 }
1719
1720
1721 void GPU_end_object_materials(void)
1722 {
1723         GPU_disable_material();
1724
1725         if (GMS.matbuf && GMS.matbuf != GMS.matbuf_fixed) {
1726                 MEM_freeN(GMS.matbuf);
1727                 MEM_freeN(GMS.gmatbuf);
1728                 MEM_freeN(GMS.alphablend);
1729         }
1730
1731         GMS.matbuf= NULL;
1732         GMS.gmatbuf= NULL;
1733         GMS.alphablend= NULL;
1734
1735         /* resetting the texture matrix after the scaling needed for tiled textures */
1736         if (GTS.tilemode) {
1737                 glMatrixMode(GL_TEXTURE);
1738                 glLoadIdentity();
1739                 glMatrixMode(GL_MODELVIEW);
1740         }
1741 }
1742
1743 /* Lights */
1744
1745 int GPU_default_lights(void)
1746 {
1747         float zero[4] = {0.0f, 0.0f, 0.0f, 0.0f}, position[4];
1748         int a, count = 0;
1749         
1750         /* initialize */
1751         if (U.light[0].flag==0 && U.light[1].flag==0 && U.light[2].flag==0) {
1752                 U.light[0].flag= 1;
1753                 U.light[0].vec[0]= -0.3; U.light[0].vec[1]= 0.3; U.light[0].vec[2]= 0.9;
1754                 U.light[0].col[0]= 0.8; U.light[0].col[1]= 0.8; U.light[0].col[2]= 0.8;
1755                 U.light[0].spec[0]= 0.5; U.light[0].spec[1]= 0.5; U.light[0].spec[2]= 0.5;
1756                 U.light[0].spec[3]= 1.0;
1757                 
1758                 U.light[1].flag= 0;
1759                 U.light[1].vec[0]= 0.5; U.light[1].vec[1]= 0.5; U.light[1].vec[2]= 0.1;
1760                 U.light[1].col[0]= 0.4; U.light[1].col[1]= 0.4; U.light[1].col[2]= 0.8;
1761                 U.light[1].spec[0]= 0.3; U.light[1].spec[1]= 0.3; U.light[1].spec[2]= 0.5;
1762                 U.light[1].spec[3]= 1.0;
1763         
1764                 U.light[2].flag= 0;
1765                 U.light[2].vec[0]= 0.3; U.light[2].vec[1]= -0.3; U.light[2].vec[2]= -0.2;
1766                 U.light[2].col[0]= 0.8; U.light[2].col[1]= 0.5; U.light[2].col[2]= 0.4;
1767                 U.light[2].spec[0]= 0.5; U.light[2].spec[1]= 0.4; U.light[2].spec[2]= 0.3;
1768                 U.light[2].spec[3]= 1.0;
1769         }
1770
1771         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
1772
1773         for (a=0; a<8; a++) {
1774                 if (a<3) {
1775                         if (U.light[a].flag) {
1776                                 glEnable(GL_LIGHT0+a);
1777
1778                                 normalize_v3_v3(position, U.light[a].vec);
1779                                 position[3]= 0.0f;
1780                                 
1781                                 glLightfv(GL_LIGHT0+a, GL_POSITION, position); 
1782                                 glLightfv(GL_LIGHT0+a, GL_DIFFUSE, U.light[a].col); 
1783                                 glLightfv(GL_LIGHT0+a, GL_SPECULAR, U.light[a].spec); 
1784
1785                                 count++;
1786                         }
1787                         else {
1788                                 glDisable(GL_LIGHT0+a);
1789
1790                                 glLightfv(GL_LIGHT0+a, GL_POSITION, zero); 
1791                                 glLightfv(GL_LIGHT0+a, GL_DIFFUSE, zero); 
1792                                 glLightfv(GL_LIGHT0+a, GL_SPECULAR, zero);
1793                         }
1794
1795                         // clear stuff from other opengl lamp usage
1796                         glLightf(GL_LIGHT0+a, GL_SPOT_CUTOFF, 180.0);
1797                         glLightf(GL_LIGHT0+a, GL_CONSTANT_ATTENUATION, 1.0);
1798                         glLightf(GL_LIGHT0+a, GL_LINEAR_ATTENUATION, 0.0);
1799                 }
1800                 else
1801                         glDisable(GL_LIGHT0+a);
1802         }
1803         
1804         glDisable(GL_LIGHTING);
1805
1806         glDisable(GL_COLOR_MATERIAL);
1807
1808         return count;
1809 }
1810
1811 int GPU_scene_object_lights(Scene *scene, Object *ob, int lay, float viewmat[4][4], int ortho)
1812 {
1813         Base *base;
1814         Lamp *la;
1815         int count;
1816         float position[4], direction[4], energy[4];
1817         
1818         /* disable all lights */
1819         for (count=0; count<8; count++)
1820                 glDisable(GL_LIGHT0+count);
1821         
1822         /* view direction for specular is not compute correct by default in
1823          * opengl, so we set the settings ourselfs */
1824         glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, (ortho)? GL_FALSE: GL_TRUE);
1825
1826         count= 0;
1827         
1828         for (base=scene->base.first; base; base=base->next) {
1829                 if (base->object->type!=OB_LAMP)
1830                         continue;
1831
1832                 if (!(base->lay & lay) || !(base->lay & ob->lay))
1833                         continue;
1834
1835                 la= base->object->data;
1836                 
1837                 /* setup lamp transform */
1838                 glPushMatrix();
1839                 glLoadMatrixf((float *)viewmat);
1840                 
1841                 if (la->type==LA_SUN) {
1842                         /* sun lamp */
1843                         copy_v3_v3(direction, base->object->obmat[2]);
1844                         direction[3]= 0.0;
1845
1846                         glLightfv(GL_LIGHT0+count, GL_POSITION, direction); 
1847                 }
1848                 else {
1849                         /* other lamps with attenuation */
1850                         copy_v3_v3(position, base->object->obmat[3]);
1851                         position[3]= 1.0f;
1852
1853                         glLightfv(GL_LIGHT0+count, GL_POSITION, position); 
1854                         glLightf(GL_LIGHT0+count, GL_CONSTANT_ATTENUATION, 1.0);
1855                         glLightf(GL_LIGHT0+count, GL_LINEAR_ATTENUATION, la->att1/la->dist);
1856                         glLightf(GL_LIGHT0+count, GL_QUADRATIC_ATTENUATION, la->att2/(la->dist*la->dist));
1857                         
1858                         if (la->type==LA_SPOT) {
1859                                 /* spot lamp */
1860                                 negate_v3_v3(direction, base->object->obmat[2]);
1861                                 glLightfv(GL_LIGHT0+count, GL_SPOT_DIRECTION, direction);
1862                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, RAD2DEGF(la->spotsize * 0.5f));
1863                                 glLightf(GL_LIGHT0+count, GL_SPOT_EXPONENT, 128.0f*la->spotblend);
1864                         }
1865                         else
1866                                 glLightf(GL_LIGHT0+count, GL_SPOT_CUTOFF, 180.0);
1867                 }
1868                 
1869                 /* setup energy */
1870                 mul_v3_v3fl(energy, &la->r, la->energy);
1871                 energy[3]= 1.0;
1872
1873                 glLightfv(GL_LIGHT0+count, GL_DIFFUSE, energy); 
1874                 glLightfv(GL_LIGHT0+count, GL_SPECULAR, energy);
1875                 glEnable(GL_LIGHT0+count);
1876                 
1877                 glPopMatrix();
1878                 
1879                 count++;
1880                 if (count==8)
1881                         break;
1882         }
1883
1884         return count;
1885 }
1886
1887 /* Default OpenGL State */
1888
1889 void GPU_state_init(void)
1890 {
1891         /* also called when doing opengl rendering and in the game engine */
1892         float mat_ambient[] = { 0.0, 0.0, 0.0, 0.0 };
1893         float mat_specular[] = { 0.5, 0.5, 0.5, 1.0 };
1894         int a, x, y;
1895         GLubyte pat[32*32];
1896         const GLubyte *patc= pat;
1897         
1898         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
1899         glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_specular);
1900         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
1901         glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 35);
1902
1903         GPU_default_lights();
1904         
1905         glDepthFunc(GL_LEQUAL);
1906         /* scaling matrices */
1907         glEnable(GL_NORMALIZE);
1908
1909         glShadeModel(GL_FLAT);
1910
1911         glDisable(GL_ALPHA_TEST);
1912         glDisable(GL_BLEND);
1913         glDisable(GL_DEPTH_TEST);
1914         glDisable(GL_FOG);
1915         glDisable(GL_LIGHTING);
1916         glDisable(GL_LOGIC_OP);
1917         glDisable(GL_STENCIL_TEST);
1918         glDisable(GL_TEXTURE_1D);
1919         glDisable(GL_TEXTURE_2D);
1920
1921         /* default disabled, enable should be local per function */
1922         glDisableClientState(GL_VERTEX_ARRAY);
1923         glDisableClientState(GL_NORMAL_ARRAY);
1924         glDisableClientState(GL_COLOR_ARRAY);
1925         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
1926         
1927         glPixelTransferi(GL_MAP_COLOR, GL_FALSE);
1928         glPixelTransferi(GL_RED_SCALE, 1);
1929         glPixelTransferi(GL_RED_BIAS, 0);
1930         glPixelTransferi(GL_GREEN_SCALE, 1);
1931         glPixelTransferi(GL_GREEN_BIAS, 0);
1932         glPixelTransferi(GL_BLUE_SCALE, 1);
1933         glPixelTransferi(GL_BLUE_BIAS, 0);
1934         glPixelTransferi(GL_ALPHA_SCALE, 1);
1935         glPixelTransferi(GL_ALPHA_BIAS, 0);
1936         
1937         glPixelTransferi(GL_DEPTH_BIAS, 0);
1938         glPixelTransferi(GL_DEPTH_SCALE, 1);
1939         glDepthRange(0.0, 1.0);
1940         
1941         a= 0;
1942         for (x=0; x<32; x++) {
1943                 for (y=0; y<4; y++) {
1944                         if ( (x) & 1) pat[a++]= 0x88;
1945                         else pat[a++]= 0x22;
1946                 }
1947         }
1948         
1949         glPolygonStipple(patc);
1950
1951         glMatrixMode(GL_TEXTURE);
1952         glLoadIdentity();
1953         glMatrixMode(GL_MODELVIEW);
1954
1955         glFrontFace(GL_CCW);
1956         glCullFace(GL_BACK);
1957         glDisable(GL_CULL_FACE);
1958
1959         /* calling this makes drawing very slow when AA is not set up in ghost
1960          * on Linux/NVIDIA. */
1961         // glDisable(GL_MULTISAMPLE);
1962 }
1963
1964 #ifdef DEBUG
1965 /* debugging aid */
1966 static void gpu_state_print_fl_ex(const char *name, GLenum type)
1967 {
1968         const unsigned char err_mark[4] = {0xff, 0xff, 0xff, 0xff};
1969
1970         float value[32];
1971         int a;
1972
1973         memset(value, 0xff, sizeof(value));
1974         glGetFloatv(type, value);
1975
1976         printf("%s: ", name);
1977         for (a = 0; a < 32; a++) {
1978                 if (memcmp(&value[a], err_mark, sizeof(value[a])) == 0) {
1979                         break;
1980                 }
1981                 printf("%.2f ", value[a]);
1982         }
1983         printf("\n");
1984 }
1985
1986 #define gpu_state_print_fl(val) gpu_state_print_fl_ex(#val, val)
1987
1988 void GPU_state_print(void)
1989 {
1990         gpu_state_print_fl(GL_ACCUM_ALPHA_BITS);
1991         gpu_state_print_fl(GL_ACCUM_BLUE_BITS);
1992         gpu_state_print_fl(GL_ACCUM_CLEAR_VALUE);
1993         gpu_state_print_fl(GL_ACCUM_GREEN_BITS);
1994         gpu_state_print_fl(GL_ACCUM_RED_BITS);
1995         gpu_state_print_fl(GL_ACTIVE_TEXTURE);
1996         gpu_state_print_fl(GL_ALIASED_LINE_WIDTH_RANGE);
1997         gpu_state_print_fl(GL_ALIASED_POINT_SIZE_RANGE);
1998         gpu_state_print_fl(GL_ALPHA_BIAS);
1999         gpu_state_print_fl(GL_ALPHA_BITS);
2000         gpu_state_print_fl(GL_ALPHA_SCALE);
2001         gpu_state_print_fl(GL_ALPHA_TEST);
2002         gpu_state_print_fl(GL_ALPHA_TEST_FUNC);
2003         gpu_state_print_fl(GL_ALPHA_TEST_REF);
2004         gpu_state_print_fl(GL_ARRAY_BUFFER_BINDING);
2005         gpu_state_print_fl(GL_ATTRIB_STACK_DEPTH);
2006         gpu_state_print_fl(GL_AUTO_NORMAL);
2007         gpu_state_print_fl(GL_AUX_BUFFERS);
2008         gpu_state_print_fl(GL_BLEND);
2009         gpu_state_print_fl(GL_BLEND_COLOR);
2010         gpu_state_print_fl(GL_BLEND_DST_ALPHA);
2011         gpu_state_print_fl(GL_BLEND_DST_RGB);
2012         gpu_state_print_fl(GL_BLEND_EQUATION_ALPHA);
2013         gpu_state_print_fl(GL_BLEND_EQUATION_RGB);
2014         gpu_state_print_fl(GL_BLEND_SRC_ALPHA);
2015         gpu_state_print_fl(GL_BLEND_SRC_RGB);
2016         gpu_state_print_fl(GL_BLUE_BIAS);
2017         gpu_state_print_fl(GL_BLUE_BITS);
2018         gpu_state_print_fl(GL_BLUE_SCALE);
2019         gpu_state_print_fl(GL_CLIENT_ACTIVE_TEXTURE);
2020         gpu_state_print_fl(GL_CLIENT_ATTRIB_STACK_DEPTH);
2021         gpu_state_print_fl(GL_CLIP_PLANE0);
2022         gpu_state_print_fl(GL_COLOR_ARRAY);
2023         gpu_state_print_fl(GL_COLOR_ARRAY_BUFFER_BINDING);
2024         gpu_state_print_fl(GL_COLOR_ARRAY_SIZE);
2025         gpu_state_print_fl(GL_COLOR_ARRAY_STRIDE);
2026         gpu_state_print_fl(GL_COLOR_ARRAY_TYPE);
2027         gpu_state_print_fl(GL_COLOR_CLEAR_VALUE);
2028         gpu_state_print_fl(GL_COLOR_LOGIC_OP);
2029         gpu_state_print_fl(GL_COLOR_MATERIAL);
2030         gpu_state_print_fl(GL_COLOR_MATERIAL_FACE);
2031         gpu_state_print_fl(GL_COLOR_MATERIAL_PARAMETER);
2032         gpu_state_print_fl(GL_COLOR_MATRIX);
2033         gpu_state_print_fl(GL_COLOR_MATRIX_STACK_DEPTH);
2034         gpu_state_print_fl(GL_COLOR_SUM);
2035         gpu_state_print_fl(GL_COLOR_TABLE);
2036         gpu_state_print_fl(GL_COLOR_WRITEMASK);
2037         gpu_state_print_fl(GL_COMPRESSED_TEXTURE_FORMATS);
2038         gpu_state_print_fl(GL_CONVOLUTION_1D);
2039         gpu_state_print_fl(GL_CONVOLUTION_2D);
2040         gpu_state_print_fl(GL_CULL_FACE);
2041         gpu_state_print_fl(GL_CULL_FACE_MODE);
2042         gpu_state_print_fl(GL_CURRENT_COLOR);
2043         gpu_state_print_fl(GL_CURRENT_FOG_COORD);
2044         gpu_state_print_fl(GL_CURRENT_INDEX);
2045         gpu_state_print_fl(GL_CURRENT_NORMAL);
2046         gpu_state_print_fl(GL_CURRENT_PROGRAM);
2047         gpu_state_print_fl(GL_CURRENT_RASTER_COLOR);
2048         gpu_state_print_fl(GL_CURRENT_RASTER_DISTANCE);
2049         gpu_state_print_fl(GL_CURRENT_RASTER_INDEX);
2050         gpu_state_print_fl(GL_CURRENT_RASTER_POSITION);
2051         gpu_state_print_fl(GL_CURRENT_RASTER_POSITION_VALID);
2052         gpu_state_print_fl(GL_CURRENT_RASTER_SECONDARY_COLOR);
2053         gpu_state_print_fl(GL_CURRENT_RASTER_TEXTURE_COORDS);
2054         gpu_state_print_fl(GL_CURRENT_SECONDARY_COLOR);
2055         gpu_state_print_fl(GL_CURRENT_TEXTURE_COORDS);
2056         gpu_state_print_fl(GL_DEPTH_BIAS);
2057         gpu_state_print_fl(GL_DEPTH_BITS);
2058         gpu_state_print_fl(GL_DEPTH_CLEAR_VALUE);
2059         gpu_state_print_fl(GL_DEPTH_FUNC);
2060         gpu_state_print_fl(GL_DEPTH_RANGE);
2061         gpu_state_print_fl(GL_DEPTH_SCALE);
2062         gpu_state_print_fl(GL_DEPTH_TEST);
2063         gpu_state_print_fl(GL_DEPTH_WRITEMASK);
2064         gpu_state_print_fl(GL_DITHER);
2065         gpu_state_print_fl(GL_DOUBLEBUFFER);
2066         gpu_state_print_fl(GL_DRAW_BUFFER);
2067         gpu_state_print_fl(GL_DRAW_BUFFER0);
2068         gpu_state_print_fl(GL_EDGE_FLAG);
2069         gpu_state_print_fl(GL_EDGE_FLAG_ARRAY);
2070         gpu_state_print_fl(GL_EDGE_FLAG_ARRAY_BUFFER_BINDING);
2071         gpu_state_print_fl(GL_EDGE_FLAG_ARRAY_STRIDE);
2072         gpu_state_print_fl(GL_ELEMENT_ARRAY_BUFFER_BINDING);
2073         gpu_state_print_fl(GL_FEEDBACK_BUFFER_SIZE);
2074         gpu_state_print_fl(GL_FEEDBACK_BUFFER_TYPE);
2075         gpu_state_print_fl(GL_FOG);
2076         gpu_state_print_fl(GL_FOG_COLOR);
2077         gpu_state_print_fl(GL_FOG_COORD_ARRAY);
2078         gpu_state_print_fl(GL_FOG_COORD_ARRAY_BUFFER_BINDING);
2079         gpu_state_print_fl(GL_FOG_COORD_ARRAY_STRIDE);
2080         gpu_state_print_fl(GL_FOG_COORD_ARRAY_TYPE);
2081         gpu_state_print_fl(GL_FOG_COORD_SRC);
2082         gpu_state_print_fl(GL_FOG_DENSITY);
2083         gpu_state_print_fl(GL_FOG_END);
2084         gpu_state_print_fl(GL_FOG_HINT);
2085         gpu_state_print_fl(GL_FOG_INDEX);
2086         gpu_state_print_fl(GL_FOG_MODE);
2087         gpu_state_print_fl(GL_FOG_START);
2088         gpu_state_print_fl(GL_FRAGMENT_PROGRAM_ARB);
2089         gpu_state_print_fl(GL_FRAGMENT_SHADER_DERIVATIVE_HINT);
2090         gpu_state_print_fl(GL_FRONT_FACE);
2091         gpu_state_print_fl(GL_GENERATE_MIPMAP_HINT);
2092         gpu_state_print_fl(GL_GREEN_BIAS);
2093         gpu_state_print_fl(GL_GREEN_BITS);
2094         gpu_state_print_fl(GL_GREEN_SCALE);
2095         gpu_state_print_fl(GL_HISTOGRAM);
2096         gpu_state_print_fl(GL_INDEX_ARRAY);
2097         gpu_state_print_fl(GL_INDEX_ARRAY_BUFFER_BINDING);
2098         gpu_state_print_fl(GL_INDEX_ARRAY_STRIDE);
2099         gpu_state_print_fl(GL_INDEX_ARRAY_TYPE);
2100         gpu_state_print_fl(GL_INDEX_BITS);
2101         gpu_state_print_fl(GL_INDEX_CLEAR_VALUE);
2102         gpu_state_print_fl(GL_INDEX_LOGIC_OP);
2103         gpu_state_print_fl(GL_INDEX_MODE);
2104         gpu_state_print_fl(GL_INDEX_OFFSET);
2105         gpu_state_print_fl(GL_INDEX_SHIFT);
2106         gpu_state_print_fl(GL_INDEX_WRITEMASK);
2107         gpu_state_print_fl(GL_LIGHT0);
2108         gpu_state_print_fl(GL_LIGHT1);
2109         gpu_state_print_fl(GL_LIGHT2);
2110         gpu_state_print_fl(GL_LIGHT3);
2111         gpu_state_print_fl(GL_LIGHT4);
2112         gpu_state_print_fl(GL_LIGHT5);
2113         gpu_state_print_fl(GL_LIGHT6);
2114         gpu_state_print_fl(GL_LIGHT7);
2115         gpu_state_print_fl(GL_LIGHTING);
2116         gpu_state_print_fl(GL_LIGHT_MODEL_AMBIENT);
2117         gpu_state_print_fl(GL_LIGHT_MODEL_COLOR_CONTROL);
2118         gpu_state_print_fl(GL_LIGHT_MODEL_LOCAL_VIEWER);
2119         gpu_state_print_fl(GL_LIGHT_MODEL_TWO_SIDE);
2120         gpu_state_print_fl(GL_LINE_SMOOTH);
2121         gpu_state_print_fl(GL_LINE_SMOOTH_HINT);
2122         gpu_state_print_fl(GL_LINE_STIPPLE);
2123         gpu_state_print_fl(GL_LINE_STIPPLE_PATTERN);
2124         gpu_state_print_fl(GL_LINE_STIPPLE_REPEAT);
2125         gpu_state_print_fl(GL_LINE_WIDTH);
2126         gpu_state_print_fl(GL_LINE_WIDTH_GRANULARITY);
2127         gpu_state_print_fl(GL_LINE_WIDTH_RANGE);
2128         gpu_state_print_fl(GL_LIST_BASE);
2129         gpu_state_print_fl(GL_LIST_INDEX);
2130         gpu_state_print_fl(GL_LIST_MODE);
2131         gpu_state_print_fl(GL_LOGIC_OP);
2132         gpu_state_print_fl(GL_LOGIC_OP_MODE);
2133         gpu_state_print_fl(GL_MAP1_COLOR_4);
2134         gpu_state_print_fl(GL_MAP1_GRID_DOMAIN);
2135         gpu_state_print_fl(GL_MAP1_GRID_SEGMENTS);
2136         gpu_state_print_fl(GL_MAP1_INDEX);
2137         gpu_state_print_fl(GL_MAP1_NORMAL);
2138         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_1);
2139         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_2);
2140         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_3);
2141         gpu_state_print_fl(GL_MAP1_TEXTURE_COORD_4);
2142         gpu_state_print_fl(GL_MAP1_VERTEX_3);
2143         gpu_state_print_fl(GL_MAP1_VERTEX_4);
2144         gpu_state_print_fl(GL_MAP2_COLOR_4);
2145         gpu_state_print_fl(GL_MAP2_GRID_DOMAIN);
2146         gpu_state_print_fl(GL_MAP2_GRID_SEGMENTS);
2147         gpu_state_print_fl(GL_MAP2_INDEX);
2148         gpu_state_print_fl(GL_MAP2_NORMAL);
2149         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_1);
2150         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_2);
2151         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_3);
2152         gpu_state_print_fl(GL_MAP2_TEXTURE_COORD_4);
2153         gpu_state_print_fl(GL_MAP2_VERTEX_3);
2154         gpu_state_print_fl(GL_MAP2_VERTEX_4);
2155         gpu_state_print_fl(GL_MAP_COLOR);
2156         gpu_state_print_fl(GL_MAP_STENCIL);
2157         gpu_state_print_fl(GL_MATRIX_MODE);
2158         gpu_state_print_fl(GL_MAX_3D_TEXTURE_SIZE);
2159         gpu_state_print_fl(GL_MAX_ATTRIB_STACK_DEPTH);
2160         gpu_state_print_fl(GL_MAX_CLIENT_ATTRIB_STACK_DEPTH);
2161         gpu_state_print_fl(GL_MAX_CLIP_PLANES);
2162         gpu_state_print_fl(GL_MAX_COLOR_MATRIX_STACK_DEPTH);
2163         gpu_state_print_fl(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
2164         gpu_state_print_fl(GL_MAX_CUBE_MAP_TEXTURE_SIZE);
2165         gpu_state_print_fl(GL_MAX_DRAW_BUFFERS);
2166         gpu_state_print_fl(GL_MAX_ELEMENTS_INDICES);
2167         gpu_state_print_fl(GL_MAX_ELEMENTS_VERTICES);
2168         gpu_state_print_fl(GL_MAX_EVAL_ORDER);
2169         gpu_state_print_fl(GL_MAX_FRAGMENT_UNIFORM_COMPONENTS);
2170         gpu_state_print_fl(GL_MAX_LIGHTS);
2171         gpu_state_print_fl(GL_MAX_LIST_NESTING);
2172         gpu_state_print_fl(GL_MAX_MODELVIEW_STACK_DEPTH);
2173         gpu_state_print_fl(GL_MAX_NAME_STACK_DEPTH);
2174         gpu_state_print_fl(GL_MAX_PIXEL_MAP_TABLE);
2175         gpu_state_print_fl(GL_MAX_PROJECTION_STACK_DEPTH);
2176         gpu_state_print_fl(GL_MAX_TEXTURE_COORDS);
2177         gpu_state_print_fl(GL_MAX_TEXTURE_IMAGE_UNITS);
2178         gpu_state_print_fl(GL_MAX_TEXTURE_LOD_BIAS);
2179         gpu_state_print_fl(GL_MAX_TEXTURE_SIZE);
2180         gpu_state_print_fl(GL_MAX_TEXTURE_STACK_DEPTH);
2181         gpu_state_print_fl(GL_MAX_TEXTURE_UNITS);
2182         gpu_state_print_fl(GL_MAX_VARYING_FLOATS);
2183         gpu_state_print_fl(GL_MAX_VERTEX_ATTRIBS);
2184         gpu_state_print_fl(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
2185         gpu_state_print_fl(GL_MAX_VERTEX_UNIFORM_COMPONENTS);
2186         gpu_state_print_fl(GL_MAX_VIEWPORT_DIMS);
2187         gpu_state_print_fl(GL_MINMAX);
2188         gpu_state_print_fl(GL_MODELVIEW_MATRIX);
2189         gpu_state_print_fl(GL_MODELVIEW_STACK_DEPTH);
2190         gpu_state_print_fl(GL_MULTISAMPLE);
2191         gpu_state_print_fl(GL_MULTISAMPLE_ARB);
2192         gpu_state_print_fl(GL_NAME_STACK_DEPTH);
2193         gpu_state_print_fl(GL_NORMALIZE);
2194         gpu_state_print_fl(GL_NORMAL_ARRAY);
2195         gpu_state_print_fl(GL_NORMAL_ARRAY_BUFFER_BINDING);
2196         gpu_state_print_fl(GL_NORMAL_ARRAY_STRIDE);
2197         gpu_state_print_fl(GL_NORMAL_ARRAY_TYPE);
2198         gpu_state_print_fl(GL_NUM_COMPRESSED_TEXTURE_FORMATS);
2199         gpu_state_print_fl(GL_PACK_ALIGNMENT);
2200         gpu_state_print_fl(GL_PACK_IMAGE_HEIGHT);
2201         gpu_state_print_fl(GL_PACK_LSB_FIRST);
2202         gpu_state_print_fl(GL_PACK_ROW_LENGTH);
2203         gpu_state_print_fl(GL_PACK_SKIP_IMAGES);
2204         gpu_state_print_fl(GL_PACK_SKIP_PIXELS);
2205         gpu_state_print_fl(GL_PACK_SKIP_ROWS);
2206         gpu_state_print_fl(GL_PACK_SWAP_BYTES);
2207         gpu_state_print_fl(GL_PERSPECTIVE_CORRECTION_HINT);
2208         gpu_state_print_fl(GL_PIXEL_MAP_A_TO_A_SIZE);
2209         gpu_state_print_fl(GL_PIXEL_MAP_B_TO_B_SIZE);
2210         gpu_state_print_fl(GL_PIXEL_MAP_G_TO_G_SIZE);
2211         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_A_SIZE);
2212         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_B_SIZE);
2213         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_G_SIZE);
2214         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_I_SIZE);
2215         gpu_state_print_fl(GL_PIXEL_MAP_I_TO_R_SIZE);
2216         gpu_state_print_fl(GL_PIXEL_MAP_R_TO_R_SIZE);
2217         gpu_state_print_fl(GL_PIXEL_MAP_S_TO_S_SIZE);
2218         gpu_state_print_fl(GL_PIXEL_PACK_BUFFER_BINDING);
2219         gpu_state_print_fl(GL_PIXEL_UNPACK_BUFFER_BINDING);
2220         gpu_state_print_fl(GL_POINT_DISTANCE_ATTENUATION);
2221         gpu_state_print_fl(GL_POINT_FADE_THRESHOLD_SIZE);
2222         gpu_state_print_fl(GL_POINT_SIZE);
2223         gpu_state_print_fl(GL_POINT_SIZE_GRANULARITY);
2224         gpu_state_print_fl(GL_POINT_SIZE_MAX);
2225         gpu_state_print_fl(GL_POINT_SIZE_MIN);
2226         gpu_state_print_fl(GL_POINT_SIZE_RANGE);
2227         gpu_state_print_fl(GL_POINT_SMOOTH);
2228         gpu_state_print_fl(GL_POINT_SMOOTH_HINT);
2229         gpu_state_print_fl(GL_POINT_SPRITE);
2230         gpu_state_print_fl(GL_POLYGON_MODE);
2231         gpu_state_print_fl(GL_POLYGON_OFFSET_FACTOR);
2232         gpu_state_print_fl(GL_POLYGON_OFFSET_FILL);
2233         gpu_state_print_fl(GL_POLYGON_OFFSET_LINE);
2234         gpu_state_print_fl(GL_POLYGON_OFFSET_POINT);
2235         gpu_state_print_fl(GL_POLYGON_OFFSET_UNITS);
2236         gpu_state_print_fl(GL_POLYGON_SMOOTH);
2237         gpu_state_print_fl(GL_POLYGON_SMOOTH_HINT);
2238         gpu_state_print_fl(GL_POLYGON_STIPPLE);
2239         gpu_state_print_fl(GL_POST_COLOR_MATRIX_ALPHA_BIAS);
2240         gpu_state_print_fl(GL_POST_COLOR_MATRIX_ALPHA_SCALE);
2241         gpu_state_print_fl(GL_POST_COLOR_MATRIX_BLUE_BIAS);
2242         gpu_state_print_fl(GL_POST_COLOR_MATRIX_BLUE_SCALE);
2243         gpu_state_print_fl(GL_POST_COLOR_MATRIX_COLOR_TABLE);
2244         gpu_state_print_fl(GL_POST_COLOR_MATRIX_GREEN_BIAS);
2245         gpu_state_print_fl(GL_POST_COLOR_MATRIX_GREEN_SCALE);
2246         gpu_state_print_fl(GL_POST_COLOR_MATRIX_RED_BIAS);
2247         gpu_state_print_fl(GL_POST_COLOR_MATRIX_RED_SCALE);
2248         gpu_state_print_fl(GL_POST_CONVOLUTION_ALPHA_BIAS);
2249         gpu_state_print_fl(GL_POST_CONVOLUTION_ALPHA_SCALE);
2250         gpu_state_print_fl(GL_POST_CONVOLUTION_BLUE_BIAS);
2251         gpu_state_print_fl(GL_POST_CONVOLUTION_BLUE_SCALE);
2252         gpu_state_print_fl(GL_POST_CONVOLUTION_COLOR_TABLE);
2253         gpu_state_print_fl(GL_POST_CONVOLUTION_GREEN_BIAS);
2254         gpu_state_print_fl(GL_POST_CONVOLUTION_GREEN_SCALE);
2255         gpu_state_print_fl(GL_POST_CONVOLUTION_RED_BIAS);
2256         gpu_state_print_fl(GL_POST_CONVOLUTION_RED_SCALE);
2257         gpu_state_print_fl(GL_PROJECTION_MATRIX);
2258         gpu_state_print_fl(GL_PROJECTION_STACK_DEPTH);
2259         gpu_state_print_fl(GL_READ_BUFFER);
2260         gpu_state_print_fl(GL_RED_BIAS);
2261         gpu_state_print_fl(GL_RED_BITS);
2262         gpu_state_print_fl(GL_RED_SCALE);
2263         gpu_state_print_fl(GL_RENDER_MODE);
2264         gpu_state_print_fl(GL_RESCALE_NORMAL);
2265         gpu_state_print_fl(GL_RGBA_MODE);
2266         gpu_state_print_fl(GL_SAMPLES);
2267         gpu_state_print_fl(GL_SAMPLE_BUFFERS);
2268         gpu_state_print_fl(GL_SAMPLE_COVERAGE_INVERT);
2269         gpu_state_print_fl(GL_SAMPLE_COVERAGE_VALUE);
2270         gpu_state_print_fl(GL_SCISSOR_BOX);
2271         gpu_state_print_fl(GL_SCISSOR_TEST);
2272         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY);
2273         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_BUFFER_BINDING);
2274         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_SIZE);
2275         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_STRIDE);
2276         gpu_state_print_fl(GL_SECONDARY_COLOR_ARRAY_TYPE);
2277         gpu_state_print_fl(GL_SELECTION_BUFFER_SIZE);
2278         gpu_state_print_fl(GL_SEPARABLE_2D);
2279         gpu_state_print_fl(GL_SHADE_MODEL);
2280         gpu_state_print_fl(GL_SMOOTH_LINE_WIDTH_GRANULARITY);
2281         gpu_state_print_fl(GL_SMOOTH_LINE_WIDTH_RANGE);
2282         gpu_state_print_fl(GL_SMOOTH_POINT_SIZE_GRANULARITY);
2283         gpu_state_print_fl(GL_SMOOTH_POINT_SIZE_RANGE);
2284         gpu_state_print_fl(GL_STENCIL_BACK_FAIL);
2285         gpu_state_print_fl(GL_STENCIL_BACK_FUNC);
2286         gpu_state_print_fl(GL_STENCIL_BACK_PASS_DEPTH_FAIL);
2287         gpu_state_print_fl(GL_STENCIL_BACK_PASS_DEPTH_PASS);
2288         gpu_state_print_fl(GL_STENCIL_BACK_REF);
2289         gpu_state_print_fl(GL_STENCIL_BACK_VALUE_MASK);
2290         gpu_state_print_fl(GL_STENCIL_BACK_WRITEMASK);
2291         gpu_state_print_fl(GL_STENCIL_BITS);
2292         gpu_state_print_fl(GL_STENCIL_CLEAR_VALUE);
2293         gpu_state_print_fl(GL_STENCIL_FAIL);
2294         gpu_state_print_fl(GL_STENCIL_FUNC);
2295         gpu_state_print_fl(GL_STENCIL_PASS_DEPTH_FAIL);
2296         gpu_state_print_fl(GL_STENCIL_PASS_DEPTH_PASS);
2297         gpu_state_print_fl(GL_STENCIL_REF);
2298         gpu_state_print_fl(GL_STENCIL_TEST);
2299         gpu_state_print_fl(GL_STENCIL_VALUE_MASK);
2300         gpu_state_print_fl(GL_STENCIL_WRITEMASK);
2301         gpu_state_print_fl(GL_STEREO);
2302         gpu_state_print_fl(GL_SUBPIXEL_BITS);
2303         gpu_state_print_fl(GL_TEXTURE_1D);
2304         gpu_state_print_fl(GL_TEXTURE_2D);
2305         gpu_state_print_fl(GL_TEXTURE_3D);
2306         gpu_state_print_fl(GL_TEXTURE_BINDING_1D);
2307         gpu_state_print_fl(GL_TEXTURE_BINDING_2D);
2308         gpu_state_print_fl(GL_TEXTURE_BINDING_3D);
2309         gpu_state_print_fl(GL_TEXTURE_BINDING_CUBE_MAP);
2310         gpu_state_print_fl(GL_TEXTURE_COMPRESSION_HINT);
2311         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY);
2312         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_BUFFER_BINDING);
2313         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_SIZE);
2314         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_STRIDE);
2315         gpu_state_print_fl(GL_TEXTURE_COORD_ARRAY_TYPE);
2316         gpu_state_print_fl(GL_TEXTURE_CUBE_MAP);
2317         gpu_state_print_fl(GL_TEXTURE_CUBE_MAP_ARB);
2318         gpu_state_print_fl(GL_TEXTURE_GEN_Q);
2319         gpu_state_print_fl(GL_TEXTURE_GEN_R);
2320         gpu_state_print_fl(GL_TEXTURE_GEN_S);
2321         gpu_state_print_fl(GL_TEXTURE_GEN_T);
2322         gpu_state_print_fl(GL_TEXTURE_MATRIX);
2323         gpu_state_print_fl(GL_TEXTURE_STACK_DEPTH);
2324         gpu_state_print_fl(GL_TRANSPOSE_COLOR_MATRIX);
2325         gpu_state_print_fl(GL_TRANSPOSE_MODELVIEW_MATRIX);
2326         gpu_state_print_fl(GL_TRANSPOSE_PROJECTION_MATRIX);
2327         gpu_state_print_fl(GL_TRANSPOSE_TEXTURE_MATRIX);
2328         gpu_state_print_fl(GL_UNPACK_ALIGNMENT);
2329         gpu_state_print_fl(GL_UNPACK_IMAGE_HEIGHT);
2330         gpu_state_print_fl(GL_UNPACK_LSB_FIRST);
2331         gpu_state_print_fl(GL_UNPACK_ROW_LENGTH);
2332         gpu_state_print_fl(GL_UNPACK_SKIP_IMAGES);
2333         gpu_state_print_fl(GL_UNPACK_SKIP_PIXELS);
2334         gpu_state_print_fl(GL_UNPACK_SKIP_ROWS);
2335         gpu_state_print_fl(GL_UNPACK_SWAP_BYTES);
2336         gpu_state_print_fl(GL_VERTEX_ARRAY);
2337         gpu_state_print_fl(GL_VERTEX_ARRAY_BUFFER_BINDING);
2338         gpu_state_print_fl(GL_VERTEX_ARRAY_SIZE);
2339         gpu_state_print_fl(GL_VERTEX_ARRAY_STRIDE);
2340         gpu_state_print_fl(GL_VERTEX_ARRAY_TYPE);
2341         gpu_state_print_fl(GL_VERTEX_PROGRAM_POINT_SIZE);
2342         gpu_state_print_fl(GL_VERTEX_PROGRAM_TWO_SIDE);
2343         gpu_state_print_fl(GL_VIEWPORT);
2344         gpu_state_print_fl(GL_ZOOM_X);
2345         gpu_state_print_fl(GL_ZOOM_Y);
2346 }
2347
2348 #undef gpu_state_print_fl
2349
2350 #endif