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