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