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