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