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