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