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