]> icculus.org git repositories - divverent/darkplaces.git/blob - gl_textures.c
mdl skin loading is now more memory-efficient (now stores the original
[divverent/darkplaces.git] / gl_textures.c
1
2 #include "quakedef.h"
3 #include "image.h"
4 #include "jpeg.h"
5 #include "image_png.h"
6
7 cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048", "maximum allowed texture size, can be used to reduce video memory usage, note: this is automatically reduced to match video card capabilities (such as 256 on 3Dfx cards before Voodoo4/5)"};
8 cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0", "reduces resolution of textures by powers of 2, for example 1 will halve width/height, reducing texture memory usage by 75%"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1", "bilinear filters images when scaling them up to power of 2 size (mode 1), looks better than glquake (mode 0)"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1", "0 = never upload textures until used, 1 = upload most textures before use (exceptions: rarely used skin colormap layers), 2 = upload all textures before use (can increase texture memory usage significantly)"};
11 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1", "anisotropic filtering quality (if supported by hardware), 1 sample (no anisotropy) and 8 sample (8 tap anisotropy) are recommended values"};
12 cvar_t gl_texturecompression = {CVAR_SAVE, "gl_texturecompression", "0", "whether to compress textures, a value of 0 disables compression (even if the individual cvars are 1), 1 enables fast (low quality) compression at startup, 2 enables slow (high quality) compression at startup"};
13 cvar_t gl_texturecompression_color = {CVAR_SAVE, "gl_texturecompression_color", "1", "whether to compress colormap (diffuse) textures"};
14 cvar_t gl_texturecompression_normal = {CVAR_SAVE, "gl_texturecompression_normal", "0", "whether to compress normalmap (normalmap) textures"};
15 cvar_t gl_texturecompression_gloss = {CVAR_SAVE, "gl_texturecompression_gloss", "1", "whether to compress glossmap (specular) textures"};
16 cvar_t gl_texturecompression_glow = {CVAR_SAVE, "gl_texturecompression_glow", "1", "whether to compress glowmap (luma) textures"};
17 cvar_t gl_texturecompression_2d = {CVAR_SAVE, "gl_texturecompression_2d", "0", "whether to compress 2d (hud/menu) textures other than the font"};
18 cvar_t gl_texturecompression_q3bsplightmaps = {CVAR_SAVE, "gl_texturecompression_q3bsplightmaps", "0", "whether to compress lightmaps in q3bsp format levels"};
19 cvar_t gl_texturecompression_q3bspdeluxemaps = {CVAR_SAVE, "gl_texturecompression_q3bspdeluxemaps", "0", "whether to compress deluxemaps in q3bsp format levels (only levels compiled with q3map2 -deluxe have these)"};
20 cvar_t gl_texturecompression_sky = {CVAR_SAVE, "gl_texturecompression_sky", "0", "whether to compress sky textures"};
21 cvar_t gl_texturecompression_lightcubemaps = {CVAR_SAVE, "gl_texturecompression_lightcubemaps", "1", "whether to compress light cubemaps (spotlights and other light projection images)"};
22
23 int             gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
24 int             gl_filter_mag = GL_LINEAR;
25
26
27 static mempool_t *texturemempool;
28
29 // note: this must not conflict with TEXF_ flags in r_textures.h
30 // cleared when a texture is uploaded
31 #define GLTEXF_UPLOAD           0x00010000
32 // bitmask for mismatch checking
33 #define GLTEXF_IMPORTANTBITS (0)
34 // set when image is uploaded and freed
35 #define GLTEXF_DESTROYED        0x00040000
36 // dynamic texture (treat texnum == 0 differently)
37 #define GLTEXF_DYNAMIC          0x00080000
38
39 typedef struct textypeinfo_s
40 {
41         textype_t textype;
42         int inputbytesperpixel;
43         int internalbytesperpixel;
44         float glinternalbytesperpixel;
45         int glformat;
46         int glinternalformat;
47         int gltype;
48 }
49 textypeinfo_t;
50
51 static textypeinfo_t textype_palette                = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_BGRA   , 3, GL_UNSIGNED_BYTE};
52 static textypeinfo_t textype_palette_alpha          = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_BGRA   , 4, GL_UNSIGNED_BYTE};
53 static textypeinfo_t textype_palette_compress       = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_BGRA   , GL_COMPRESSED_RGB_ARB, GL_UNSIGNED_BYTE};
54 static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_BGRA   , GL_COMPRESSED_RGBA_ARB, GL_UNSIGNED_BYTE};
55 static textypeinfo_t textype_rgba                   = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 3, GL_UNSIGNED_BYTE};
56 static textypeinfo_t textype_rgba_alpha             = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 4, GL_UNSIGNED_BYTE};
57 static textypeinfo_t textype_rgba_compress          = {TEXTYPE_RGBA   , 4, 4, 0.5f, GL_RGBA   , GL_COMPRESSED_RGB_ARB, GL_UNSIGNED_BYTE};
58 static textypeinfo_t textype_rgba_alpha_compress    = {TEXTYPE_RGBA   , 4, 4, 1.0f, GL_RGBA   , GL_COMPRESSED_RGBA_ARB, GL_UNSIGNED_BYTE};
59 static textypeinfo_t textype_bgra                   = {TEXTYPE_BGRA   , 4, 4, 4.0f, GL_BGRA   , 3, GL_UNSIGNED_BYTE};
60 static textypeinfo_t textype_bgra_alpha             = {TEXTYPE_BGRA   , 4, 4, 4.0f, GL_BGRA   , 4, GL_UNSIGNED_BYTE};
61 static textypeinfo_t textype_bgra_compress          = {TEXTYPE_BGRA   , 4, 4, 0.5f, GL_BGRA   , GL_COMPRESSED_RGB_ARB, GL_UNSIGNED_BYTE};
62 static textypeinfo_t textype_bgra_alpha_compress    = {TEXTYPE_BGRA   , 4, 4, 1.0f, GL_BGRA   , GL_COMPRESSED_RGBA_ARB, GL_UNSIGNED_BYTE};
63 static textypeinfo_t textype_shadowmap16            = {TEXTYPE_SHADOWMAP,2,2, 2.0f, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT16_ARB, GL_UNSIGNED_SHORT};
64 static textypeinfo_t textype_shadowmap24            = {TEXTYPE_SHADOWMAP,4,4, 4.0f, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT24_ARB, GL_UNSIGNED_INT};
65
66 typedef enum gltexturetype_e
67 {
68         GLTEXTURETYPE_2D,
69         GLTEXTURETYPE_3D,
70         GLTEXTURETYPE_CUBEMAP,
71         GLTEXTURETYPE_RECTANGLE,
72         GLTEXTURETYPE_TOTAL
73 }
74 gltexturetype_t;
75
76 static int gltexturetypeenums[GLTEXTURETYPE_TOTAL] = {GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_RECTANGLE_ARB};
77 static int gltexturetypebindingenums[GLTEXTURETYPE_TOTAL] = {GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB, GL_TEXTURE_BINDING_RECTANGLE_ARB};
78 static int gltexturetypedimensions[GLTEXTURETYPE_TOTAL] = {2, 3, 2, 2};
79 static int cubemapside[6] =
80 {
81         GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
82         GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
83         GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
84         GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
85         GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
86         GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
87 };
88
89 typedef struct gltexture_s
90 {
91         // this field is exposed to the R_GetTexture macro, for speed reasons
92         // (must be identical in rtexture_t)
93         int texnum; // GL texture slot number
94
95         // dynamic texture stuff [11/22/2007 Black]
96         // used to hold the texture number of dirty textures   
97         int dirtytexnum;
98         updatecallback_t updatecallback;
99         void *updatacallback_data;
100         // --- [11/22/2007 Black]
101
102         // pointer to texturepool (check this to see if the texture is allocated)
103         struct gltexturepool_s *pool;
104         // pointer to next texture in texturepool chain
105         struct gltexture_s *chain;
106         // name of the texture (this might be removed someday), no duplicates
107         char identifier[MAX_QPATH + 32];
108         // original data size in *inputtexels
109         int inputwidth, inputheight, inputdepth;
110         // copy of the original texture(s) supplied to the upload function, for
111         // delayed uploads (non-precached)
112         unsigned char *inputtexels;
113         // original data size in *inputtexels
114         int inputdatasize;
115         // flags supplied to the LoadTexture function
116         // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
117         int flags;
118         // pointer to one of the textype_ structs
119         textypeinfo_t *textype;
120         // one of the GLTEXTURETYPE_ values
121         int texturetype;
122         // palette if the texture is TEXTYPE_PALETTE
123         const unsigned int *palette;
124         // actual stored texture size after gl_picmip and gl_max_size are applied
125         // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
126         int tilewidth, tileheight, tiledepth;
127         // 1 or 6 depending on texturetype
128         int sides;
129         // bytes per pixel
130         int bytesperpixel;
131         // GL_RGB or GL_RGBA or GL_DEPTH_COMPONENT
132         int glformat;
133         // 3 or 4
134         int glinternalformat;
135         // GL_UNSIGNED_BYTE or GL_UNSIGNED_INT or GL_UNSIGNED_SHORT or GL_FLOAT
136         int gltype;
137 }
138 gltexture_t;
139
140 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
141
142 typedef struct gltexturepool_s
143 {
144         unsigned int sentinel;
145         struct gltexture_s *gltchain;
146         struct gltexturepool_s *next;
147 }
148 gltexturepool_t;
149
150 static gltexturepool_t *gltexturepoolchain = NULL;
151
152 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
153 static int resizebuffersize = 0;
154 static const unsigned char *texturebuffer;
155 static int texturebuffersize = 0;
156
157 static textypeinfo_t *R_GetTexTypeInfo(textype_t textype, int flags)
158 {
159         if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
160         {
161                 if (flags & TEXF_ALPHA)
162                 {
163                         switch(textype)
164                         {
165                         case TEXTYPE_PALETTE:
166                                 return &textype_palette_alpha_compress;
167                         case TEXTYPE_RGBA:
168                                 return &textype_rgba_alpha_compress;
169                         case TEXTYPE_BGRA:
170                                 return &textype_bgra_alpha_compress;
171                         default:
172                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
173                                 return NULL;
174                         }
175                 }
176                 else
177                 {
178                         switch(textype)
179                         {
180                         case TEXTYPE_PALETTE:
181                                 return &textype_palette_compress;
182                         case TEXTYPE_RGBA:
183                                 return &textype_rgba_compress;
184                         case TEXTYPE_BGRA:
185                                 return &textype_bgra_compress;
186                         default:
187                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
188                                 return NULL;
189                         }
190                 }
191         }
192         else
193         {
194                 if (flags & TEXF_ALPHA)
195                 {
196                         switch(textype)
197                         {
198                         case TEXTYPE_PALETTE:
199                                 return &textype_palette_alpha;
200                         case TEXTYPE_RGBA:
201                                 return &textype_rgba_alpha;
202                         case TEXTYPE_BGRA:
203                                 return &textype_bgra_alpha;
204                         default:
205                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
206                                 return NULL;
207                         }
208                 }
209                 else
210                 {
211                         switch(textype)
212                         {
213                         case TEXTYPE_PALETTE:
214                                 return &textype_palette;
215                         case TEXTYPE_RGBA:
216                                 return &textype_rgba;
217                         case TEXTYPE_BGRA:
218                                 return &textype_bgra;
219                         case TEXTYPE_SHADOWMAP:
220                                 return (flags & TEXF_LOWPRECISION) ? &textype_shadowmap16 : &textype_shadowmap24;
221                         default:
222                                 Host_Error("R_GetTexTypeInfo: unknown texture format");
223                                 return NULL;
224                         }
225                 }
226         }
227         return NULL; // this line only to hush compiler warnings
228 }
229
230 // dynamic texture code [11/22/2007 Black]
231 void R_MarkDirtyTexture(rtexture_t *rt) {
232         gltexture_t *glt = (gltexture_t*) rt;
233         if( !glt ) {
234                 return;
235         }
236
237         // dont do anything if the texture is already dirty (and make sure this *is* a dynamic texture after all!)
238         if( !glt->dirtytexnum && glt->flags & GLTEXF_DYNAMIC ) {
239                 glt->dirtytexnum = glt->texnum;
240                 // mark it as dirty, so R_RealGetTexture gets called
241                 glt->texnum = 0;
242         }
243 }
244
245 void R_MakeTextureDynamic(rtexture_t *rt, updatecallback_t updatecallback, void *data) {
246         gltexture_t *glt = (gltexture_t*) rt;
247         if( !glt ) {
248                 return;
249         }
250
251         glt->flags |= GLTEXF_DYNAMIC;
252         glt->updatecallback = updatecallback;
253         glt->updatacallback_data = data;
254         glt->dirtytexnum = 0;
255 }
256
257 static void R_UpdateDynamicTexture(gltexture_t *glt) {
258         glt->texnum = glt->dirtytexnum;
259         // reset dirtytexnum again (not dirty anymore)
260         glt->dirtytexnum = 0;
261         // TODO: now assert that t->texnum != 0 ?
262         if( glt->updatecallback ) {
263                 glt->updatecallback( (rtexture_t*) glt, glt->updatacallback_data );
264         }
265 }
266
267 void R_PurgeTexture(rtexture_t *rt)
268 {
269         if(rt && !(((gltexture_t*) rt)->flags & TEXF_PERSISTENT)) {
270                 R_FreeTexture(rt);
271         }
272 }
273
274 void R_FreeTexture(rtexture_t *rt)
275 {
276         gltexture_t *glt, **gltpointer;
277
278         glt = (gltexture_t *)rt;
279         if (glt == NULL)
280                 Host_Error("R_FreeTexture: texture == NULL");
281
282         for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
283         if (*gltpointer == glt)
284                 *gltpointer = glt->chain;
285         else
286                 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
287
288         if (!(glt->flags & GLTEXF_UPLOAD))
289         {
290                 CHECKGLERROR
291                 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
292         }
293
294         if (glt->inputtexels)
295                 Mem_Free(glt->inputtexels);
296         Mem_Free(glt);
297 }
298
299 rtexturepool_t *R_AllocTexturePool(void)
300 {
301         gltexturepool_t *pool;
302         if (texturemempool == NULL)
303                 return NULL;
304         pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
305         if (pool == NULL)
306                 return NULL;
307         pool->next = gltexturepoolchain;
308         gltexturepoolchain = pool;
309         pool->sentinel = TEXTUREPOOL_SENTINEL;
310         return (rtexturepool_t *)pool;
311 }
312
313 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
314 {
315         gltexturepool_t *pool, **poolpointer;
316         if (rtexturepool == NULL)
317                 return;
318         if (*rtexturepool == NULL)
319                 return;
320         pool = (gltexturepool_t *)(*rtexturepool);
321         *rtexturepool = NULL;
322         if (pool->sentinel != TEXTUREPOOL_SENTINEL)
323                 Host_Error("R_FreeTexturePool: pool already freed");
324         for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
325         if (*poolpointer == pool)
326                 *poolpointer = pool->next;
327         else
328                 Host_Error("R_FreeTexturePool: pool not linked");
329         while (pool->gltchain)
330                 R_FreeTexture((rtexture_t *)pool->gltchain);
331         Mem_Free(pool);
332 }
333
334
335 typedef struct glmode_s
336 {
337         char *name;
338         int minification, magnification;
339 }
340 glmode_t;
341
342 static glmode_t modes[6] =
343 {
344         {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
345         {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
346         {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
347         {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
348         {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
349         {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
350 };
351
352 static void GL_TextureMode_f (void)
353 {
354         int i;
355         GLint oldbindtexnum;
356         gltexture_t *glt;
357         gltexturepool_t *pool;
358
359         if (Cmd_Argc() == 1)
360         {
361                 for (i = 0;i < 6;i++)
362                 {
363                         if (gl_filter_min == modes[i].minification)
364                         {
365                                 Con_Printf("%s\n", modes[i].name);
366                                 return;
367                         }
368                 }
369                 Con_Print("current filter is unknown???\n");
370                 return;
371         }
372
373         for (i = 0;i < 6;i++)
374                 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
375                         break;
376         if (i == 6)
377         {
378                 Con_Print("bad filter name\n");
379                 return;
380         }
381
382         gl_filter_min = modes[i].minification;
383         gl_filter_mag = modes[i].magnification;
384
385         // change all the existing mipmap texture objects
386         // FIXME: force renderer(/client/something?) restart instead?
387         CHECKGLERROR
388         for (pool = gltexturepoolchain;pool;pool = pool->next)
389         {
390                 for (glt = pool->gltchain;glt;glt = glt->chain)
391                 {
392                         // only update already uploaded images
393                         if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
394                         {
395                                 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
396                                 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
397                                 if (glt->flags & TEXF_MIPMAP)
398                                 {
399                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
400                                 }
401                                 else
402                                 {
403                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
404                                 }
405                                 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
406                                 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
407                         }
408                 }
409         }
410 }
411
412 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
413 {
414         int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
415
416         if (gl_max_size.integer > gl_max_texture_size)
417                 Cvar_SetValue("gl_max_size", gl_max_texture_size);
418
419         switch (texturetype)
420         {
421         default:
422         case GLTEXTURETYPE_2D:
423                 maxsize = gl_max_texture_size;
424                 break;
425         case GLTEXTURETYPE_3D:
426                 maxsize = gl_max_3d_texture_size;
427                 break;
428         case GLTEXTURETYPE_CUBEMAP:
429                 maxsize = gl_max_cube_map_texture_size;
430                 break;
431         }
432
433         if (flags & TEXF_PICMIP)
434         {
435                 maxsize = min(maxsize, gl_max_size.integer);
436                 picmip = gl_picmip.integer;
437         }
438
439         if (outwidth)
440         {
441                 if (gl_support_arb_texture_non_power_of_two)
442                         width2 = min(inwidth >> picmip, maxsize);
443                 else
444                 {
445                         for (width2 = 1;width2 < inwidth;width2 <<= 1);
446                         for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
447                 }
448                 *outwidth = max(1, width2);
449         }
450         if (outheight)
451         {
452                 if (gl_support_arb_texture_non_power_of_two)
453                         height2 = min(inheight >> picmip, maxsize);
454                 else
455                 {
456                         for (height2 = 1;height2 < inheight;height2 <<= 1);
457                         for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
458                 }
459                 *outheight = max(1, height2);
460         }
461         if (outdepth)
462         {
463                 if (gl_support_arb_texture_non_power_of_two)
464                         depth2 = min(indepth >> picmip, maxsize);
465                 else
466                 {
467                         for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
468                         for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
469                 }
470                 *outdepth = max(1, depth2);
471         }
472 }
473
474
475 static int R_CalcTexelDataSize (gltexture_t *glt)
476 {
477         int width2, height2, depth2, size;
478
479         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
480
481         size = width2 * height2 * depth2;
482
483         if (glt->flags & TEXF_MIPMAP)
484         {
485                 while (width2 > 1 || height2 > 1 || depth2 > 1)
486                 {
487                         if (width2 > 1)
488                                 width2 >>= 1;
489                         if (height2 > 1)
490                                 height2 >>= 1;
491                         if (depth2 > 1)
492                                 depth2 >>= 1;
493                         size += width2 * height2 * depth2;
494                 }
495         }
496
497         return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
498 }
499
500 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
501 {
502         int glsize;
503         int isloaded;
504         int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
505         int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
506         gltexture_t *glt;
507         gltexturepool_t *pool;
508         if (printeach)
509                 Con_Print("glsize input loaded mip alpha name\n");
510         for (pool = gltexturepoolchain;pool;pool = pool->next)
511         {
512                 pooltotal = 0;
513                 pooltotalt = 0;
514                 pooltotalp = 0;
515                 poolloaded = 0;
516                 poolloadedt = 0;
517                 poolloadedp = 0;
518                 for (glt = pool->gltchain;glt;glt = glt->chain)
519                 {
520                         glsize = R_CalcTexelDataSize(glt);
521                         isloaded = !(glt->flags & GLTEXF_UPLOAD);
522                         pooltotal++;
523                         pooltotalt += glsize;
524                         pooltotalp += glt->inputdatasize;
525                         if (isloaded)
526                         {
527                                 poolloaded++;
528                                 poolloadedt += glsize;
529                                 poolloadedp += glt->inputdatasize;
530                         }
531                         if (printeach)
532                                 Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", isloaded ? '[' : ' ', (glsize + 1023) / 1024, isloaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', isloaded ? "loaded" : "      ", (glt->flags & TEXF_MIPMAP) ? "mip" : "   ", (glt->flags & TEXF_ALPHA) ? "alpha" : "     ", glt->identifier);
533                 }
534                 if (printpool)
535                         Con_Printf("texturepool %10p total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", (void *)pool, pooltotal, pooltotalt / 1048576.0, pooltotalp / 1048576.0, poolloaded, poolloadedt / 1048576.0, poolloadedp / 1048576.0, pooltotal - poolloaded, (pooltotalt - poolloadedt) / 1048576.0, (pooltotalp - poolloadedp) / 1048576.0);
536                 sumtotal += pooltotal;
537                 sumtotalt += pooltotalt;
538                 sumtotalp += pooltotalp;
539                 sumloaded += poolloaded;
540                 sumloadedt += poolloadedt;
541                 sumloadedp += poolloadedp;
542         }
543         if (printtotal)
544                 Con_Printf("textures total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", sumtotal, sumtotalt / 1048576.0, sumtotalp / 1048576.0, sumloaded, sumloadedt / 1048576.0, sumloadedp / 1048576.0, sumtotal - sumloaded, (sumtotalt - sumloadedt) / 1048576.0, (sumtotalp - sumloadedp) / 1048576.0);
545 }
546
547 static void R_TextureStats_f(void)
548 {
549         R_TextureStats_Print(true, true, true);
550 }
551
552 static void r_textures_start(void)
553 {
554         // LordHavoc: allow any alignment
555         CHECKGLERROR
556         qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
557         qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
558
559         texturemempool = Mem_AllocPool("texture management", 0, NULL);
560
561         // Disable JPEG screenshots if the DLL isn't loaded
562         if (! JPEG_OpenLibrary ())
563                 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
564         // TODO: support png screenshots?
565         PNG_OpenLibrary ();
566 }
567
568 static void r_textures_shutdown(void)
569 {
570         rtexturepool_t *temp;
571
572         JPEG_CloseLibrary ();
573
574         while(gltexturepoolchain)
575         {
576                 temp = (rtexturepool_t *) gltexturepoolchain;
577                 R_FreeTexturePool(&temp);
578         }
579
580         resizebuffersize = 0;
581         texturebuffersize = 0;
582         resizebuffer = NULL;
583         colorconvertbuffer = NULL;
584         texturebuffer = NULL;
585         Mem_FreePool(&texturemempool);
586 }
587
588 static void r_textures_newmap(void)
589 {
590 }
591
592 void R_Textures_Init (void)
593 {
594         Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
595         Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
596         Cvar_RegisterVariable (&gl_max_size);
597         Cvar_RegisterVariable (&gl_picmip);
598         Cvar_RegisterVariable (&r_lerpimages);
599         Cvar_RegisterVariable (&r_precachetextures);
600         Cvar_RegisterVariable (&gl_texture_anisotropy);
601         Cvar_RegisterVariable (&gl_texturecompression);
602         Cvar_RegisterVariable (&gl_texturecompression_color);
603         Cvar_RegisterVariable (&gl_texturecompression_normal);
604         Cvar_RegisterVariable (&gl_texturecompression_gloss);
605         Cvar_RegisterVariable (&gl_texturecompression_glow);
606         Cvar_RegisterVariable (&gl_texturecompression_2d);
607         Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
608         Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
609         Cvar_RegisterVariable (&gl_texturecompression_sky);
610         Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
611
612         R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
613 }
614
615 void R_Textures_Frame (void)
616 {
617         static int old_aniso = 0;
618
619         // could do procedural texture animation here, if we keep track of which
620         // textures were accessed this frame...
621
622         // free the resize buffers
623         resizebuffersize = 0;
624         if (resizebuffer)
625         {
626                 Mem_Free(resizebuffer);
627                 resizebuffer = NULL;
628         }
629         if (colorconvertbuffer)
630         {
631                 Mem_Free(colorconvertbuffer);
632                 colorconvertbuffer = NULL;
633         }
634
635         if (old_aniso != gl_texture_anisotropy.integer)
636         {
637                 gltexture_t *glt;
638                 gltexturepool_t *pool;
639                 GLint oldbindtexnum;
640
641                 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
642
643                 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
644
645                 CHECKGLERROR
646                 for (pool = gltexturepoolchain;pool;pool = pool->next)
647                 {
648                         for (glt = pool->gltchain;glt;glt = glt->chain)
649                         {
650                                 // only update already uploaded images
651                                 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
652                                 {
653                                         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
654
655                                         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
656                                         qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
657
658                                         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
659                                 }
660                         }
661                 }
662         }
663 }
664
665 void R_MakeResizeBufferBigger(int size)
666 {
667         if (resizebuffersize < size)
668         {
669                 resizebuffersize = size;
670                 if (resizebuffer)
671                         Mem_Free(resizebuffer);
672                 if (colorconvertbuffer)
673                         Mem_Free(colorconvertbuffer);
674                 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
675                 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
676                 if (!resizebuffer || !colorconvertbuffer)
677                         Host_Error("R_Upload: out of memory");
678         }
679 }
680
681 static void GL_SetupTextureParameters(int flags, textype_t textype, int texturetype)
682 {
683         int textureenum = gltexturetypeenums[texturetype];
684         int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
685
686         CHECKGLERROR
687
688         if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
689         {
690                 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
691                 if (gl_texture_anisotropy.integer != aniso)
692                         Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
693                 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
694         }
695         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
696         qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
697         if (gltexturetypedimensions[texturetype] >= 3)
698         {
699                 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
700         }
701
702         CHECKGLERROR
703         if (flags & TEXF_FORCENEAREST)
704         {
705                 if (flags & TEXF_MIPMAP)
706                 {
707                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
708                 }
709                 else
710                 {
711                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
712                 }
713                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
714         }
715         else if (flags & TEXF_FORCELINEAR)
716         {
717                 if (flags & TEXF_MIPMAP)
718                 {
719                         if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
720                         {
721                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
722                         }
723                         else
724                         {
725                                 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
726                         }
727                 }
728                 else
729                 {
730                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
731                 }
732                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
733         }
734         else
735         {
736                 if (flags & TEXF_MIPMAP)
737                 {
738                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
739                 }
740                 else
741                 {
742                         qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
743                 }
744                 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
745         }
746
747         if (textype == TEXTYPE_SHADOWMAP)
748         {
749                 if (gl_support_arb_shadow)
750                 {
751                         if (flags & TEXF_COMPARE)
752                         {
753                                 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);CHECKGLERROR
754                         }
755                         else
756                         {
757                                 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);CHECKGLERROR
758                         }
759                         qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);CHECKGLERROR
760                 }
761                 qglTexParameteri(textureenum, GL_DEPTH_TEXTURE_MODE_ARB, GL_LUMINANCE);CHECKGLERROR
762         }
763
764         CHECKGLERROR
765 }
766
767 static void R_Upload(gltexture_t *glt, const unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
768 {
769         int i, mip, width, height, depth;
770         GLint oldbindtexnum;
771         const unsigned char *prevbuffer;
772         prevbuffer = data;
773
774         CHECKGLERROR
775
776         // we need to restore the texture binding after finishing the upload
777         qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
778         qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
779
780         // these are rounded up versions of the size to do better resampling
781         if (gl_support_arb_texture_non_power_of_two || glt->texturetype == GLTEXTURETYPE_RECTANGLE)
782         {
783                 width = glt->inputwidth;
784                 height = glt->inputheight;
785                 depth = glt->inputdepth;
786         }
787         else
788         {
789                 for (width  = 1;width  < glt->inputwidth ;width  <<= 1);
790                 for (height = 1;height < glt->inputheight;height <<= 1);
791                 for (depth  = 1;depth  < glt->inputdepth ;depth  <<= 1);
792         }
793
794         R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
795         R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
796
797         if (prevbuffer == NULL)
798         {
799                 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
800                 prevbuffer = resizebuffer;
801         }
802         else if (glt->textype->textype == TEXTYPE_PALETTE)
803         {
804                 // promote paletted to BGRA, so we only have to worry about BGRA in the rest of this code
805                 Image_Copy8bitBGRA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
806                 prevbuffer = colorconvertbuffer;
807         }
808
809         if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
810         {
811                 // update a portion of the image
812                 switch(glt->texturetype)
813                 {
814                 case GLTEXTURETYPE_2D:
815                         qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
816                         break;
817                 case GLTEXTURETYPE_3D:
818                         qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
819                         break;
820                 default:
821                         Host_Error("R_Upload: partial update of type other than 2D");
822                         break;
823                 }
824         }
825         else
826         {
827                 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
828                         Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
829
830                 // upload the image for the first time
831                 glt->flags &= ~GLTEXF_UPLOAD;
832
833                 // cubemaps contain multiple images and thus get processed a bit differently
834                 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
835                 {
836                         if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
837                         {
838                                 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
839                                 prevbuffer = resizebuffer;
840                         }
841                         // picmip/max_size
842                         while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
843                         {
844                                 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
845                                 prevbuffer = resizebuffer;
846                         }
847                 }
848                 mip = 0;
849                 if (gl_support_texture_compression)
850                 {
851                         if (gl_texturecompression.integer >= 2)
852                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
853                         else
854                                 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
855                         CHECKGLERROR
856                 }
857                 switch(glt->texturetype)
858                 {
859                 case GLTEXTURETYPE_2D:
860                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
861                         if (glt->flags & TEXF_MIPMAP)
862                         {
863                                 while (width > 1 || height > 1 || depth > 1)
864                                 {
865                                         Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
866                                         prevbuffer = resizebuffer;
867                                         qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
868                                 }
869                         }
870                         break;
871                 case GLTEXTURETYPE_3D:
872                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
873                         if (glt->flags & TEXF_MIPMAP)
874                         {
875                                 while (width > 1 || height > 1 || depth > 1)
876                                 {
877                                         Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
878                                         prevbuffer = resizebuffer;
879                                         qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
880                                 }
881                         }
882                         break;
883                 case GLTEXTURETYPE_CUBEMAP:
884                         // convert and upload each side in turn,
885                         // from a continuous block of input texels
886                         texturebuffer = (unsigned char *)prevbuffer;
887                         for (i = 0;i < 6;i++)
888                         {
889                                 prevbuffer = texturebuffer;
890                                 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
891                                 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
892                                 {
893                                         Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
894                                         prevbuffer = resizebuffer;
895                                 }
896                                 // picmip/max_size
897                                 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
898                                 {
899                                         Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
900                                         prevbuffer = resizebuffer;
901                                 }
902                                 mip = 0;
903                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
904                                 if (glt->flags & TEXF_MIPMAP)
905                                 {
906                                         while (width > 1 || height > 1 || depth > 1)
907                                         {
908                                                 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
909                                                 prevbuffer = resizebuffer;
910                                                 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
911                                         }
912                                 }
913                         }
914                         break;
915                 case GLTEXTURETYPE_RECTANGLE:
916                         qglTexImage2D(GL_TEXTURE_RECTANGLE_ARB, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, NULL);CHECKGLERROR
917                         break;
918                 }
919                 GL_SetupTextureParameters(glt->flags, glt->textype->textype, glt->texturetype);
920         }
921         qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
922 }
923
924 int R_RealGetTexture(rtexture_t *rt)
925 {
926         if (rt)
927         {
928                 gltexture_t *glt;
929                 glt = (gltexture_t *)rt;
930                 if (glt->flags & GLTEXF_DYNAMIC)
931                         R_UpdateDynamicTexture(glt);
932                 if (glt->flags & GLTEXF_UPLOAD)
933                 {
934                         CHECKGLERROR
935                         qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
936                         R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
937                         if (glt->inputtexels)
938                         {
939                                 Mem_Free(glt->inputtexels);
940                                 glt->inputtexels = NULL;
941                                 glt->flags |= GLTEXF_DESTROYED;
942                         }
943                         else if (glt->flags & GLTEXF_DESTROYED)
944                                 Con_Printf("R_GetTexture: Texture %s already uploaded and destroyed.  Can not upload original image again.  Uploaded blank texture.\n", glt->identifier);
945                 }
946
947                 return glt->texnum;
948         }
949         else
950                 return 0;
951 }
952
953 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, int sides, int flags, textype_t textype, int texturetype, const unsigned char *data, const unsigned int *palette)
954 {
955         int i, size;
956         gltexture_t *glt;
957         gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
958         textypeinfo_t *texinfo;
959         int precache;
960
961         if (cls.state == ca_dedicated)
962                 return NULL;
963
964         if (texturetype == GLTEXTURETYPE_RECTANGLE && !gl_texturerectangle)
965         {
966                 Con_Printf ("R_LoadTexture: rectangle texture not supported by driver\n");
967                 return NULL;
968         }
969         if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
970         {
971                 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
972                 return NULL;
973         }
974         if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
975         {
976                 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
977                 return NULL;
978         }
979
980         texinfo = R_GetTexTypeInfo(textype, flags);
981         size = width * height * depth * sides * texinfo->inputbytesperpixel;
982         if (size < 1)
983         {
984                 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
985                 return NULL;
986         }
987
988         // clear the alpha flag if the texture has no transparent pixels
989         switch(textype)
990         {
991         case TEXTYPE_PALETTE:
992                 if (flags & TEXF_ALPHA)
993                 {
994                         flags &= ~TEXF_ALPHA;
995                         if (data)
996                         {
997                                 for (i = 0;i < size;i++)
998                                 {
999                                         if (((unsigned char *)&palette[data[i]])[3] < 255)
1000                                         {
1001                                                 flags |= TEXF_ALPHA;
1002                                                 break;
1003                                         }
1004                                 }
1005                         }
1006                 }
1007                 break;
1008         case TEXTYPE_RGBA:
1009         case TEXTYPE_BGRA:
1010                 if (flags & TEXF_ALPHA)
1011                 {
1012                         flags &= ~TEXF_ALPHA;
1013                         if (data)
1014                         {
1015                                 for (i = 3;i < size;i += 4)
1016                                 {
1017                                         if (data[i] < 255)
1018                                         {
1019                                                 flags |= TEXF_ALPHA;
1020                                                 break;
1021                                         }
1022                                 }
1023                         }
1024                 }
1025                 break;
1026         case TEXTYPE_SHADOWMAP:
1027                 break;
1028         default:
1029                 Host_Error("R_LoadTexture: unknown texture type");
1030         }
1031
1032         glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1033         if (identifier)
1034                 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1035         glt->pool = pool;
1036         glt->chain = pool->gltchain;
1037         pool->gltchain = glt;
1038         glt->inputwidth = width;
1039         glt->inputheight = height;
1040         glt->inputdepth = depth;
1041         glt->flags = flags | GLTEXF_UPLOAD;
1042         glt->textype = texinfo;
1043         glt->texturetype = texturetype;
1044         glt->inputdatasize = size;
1045         glt->palette = palette;
1046         glt->glinternalformat = texinfo->glinternalformat;
1047         glt->glformat = texinfo->glformat;
1048         glt->gltype = texinfo->gltype;
1049         glt->bytesperpixel = texinfo->internalbytesperpixel;
1050         glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1051         glt->texnum = 0;
1052         // init the dynamic texture attributes, too [11/22/2007 Black]
1053         glt->dirtytexnum = 0;
1054         glt->updatecallback = NULL;
1055         glt->updatacallback_data = NULL;
1056
1057         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1058
1059         precache = false;
1060         if (glt->flags & TEXF_ALWAYSPRECACHE)
1061                 precache = true;
1062         else if (r_precachetextures.integer >= 2)
1063                 precache = true;
1064         else if (r_precachetextures.integer >= 1)
1065                 if (glt->flags & TEXF_PRECACHE)
1066                         precache = true;
1067
1068         if (precache)
1069         {
1070                 // immediate upload (most common case)
1071                 // data may be NULL (blank texture for dynamic rendering)
1072                 CHECKGLERROR
1073                 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
1074                 R_Upload(glt, data, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
1075         }
1076         else if (data)
1077         {
1078                 // deferred texture upload (menu graphics)
1079                 // optimize first if possible
1080                 if ((textype == TEXTYPE_BGRA || textype == TEXTYPE_RGBA) && glt->inputwidth * glt->inputheight * glt->inputdepth > glt->tilewidth * glt->tileheight * glt->tiledepth)
1081                 {
1082                         glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, glt->tilewidth*glt->tileheight*glt->tiledepth*glt->sides*glt->bytesperpixel);
1083                         Image_Resample32(data, glt->inputwidth, glt->inputheight, glt->inputdepth, glt->inputtexels, glt->tilewidth, glt->tileheight, glt->tiledepth, r_lerpimages.integer);
1084                         // change texture size accordingly
1085                         glt->inputwidth = glt->tilewidth;
1086                         glt->inputheight = glt->tileheight;
1087                         glt->inputdepth = glt->tiledepth;
1088                         GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1089                 }
1090                 else
1091                 {
1092                         glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1093                         memcpy(glt->inputtexels, data, size);
1094                 }
1095         }
1096
1097         // texture converting and uploading can take a while, so make sure we're sending keepalives
1098         CL_KeepaliveMessage(false);
1099
1100         return (rtexture_t *)glt;
1101 }
1102
1103 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const unsigned char *data, textype_t textype, int flags, const unsigned int *palette)
1104 {
1105         return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1106 }
1107
1108 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, const unsigned char *data, textype_t textype, int flags, const unsigned int *palette)
1109 {
1110         return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1111 }
1112
1113 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, textype_t textype, int flags, const unsigned int *palette)
1114 {
1115         return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1116 }
1117
1118 static int R_ShadowMapTextureFlags(int precision, qboolean filter)
1119 {
1120         int flags = TEXF_ALWAYSPRECACHE | TEXF_CLAMP;
1121         if (filter)
1122                 flags |= TEXF_FORCELINEAR | TEXF_COMPARE;
1123         else
1124                 flags |= TEXF_FORCENEAREST;
1125         if (precision <= 16)
1126                 flags |= TEXF_LOWPRECISION;
1127         return flags;
1128 }
1129
1130 rtexture_t *R_LoadTextureShadowMapRectangle(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int precision, qboolean filter)
1131 {
1132         return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, R_ShadowMapTextureFlags(precision, filter), TEXTYPE_SHADOWMAP, GLTEXTURETYPE_RECTANGLE, NULL, NULL);
1133 }
1134
1135 rtexture_t *R_LoadTextureShadowMap2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int precision, qboolean filter)
1136 {
1137         return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, R_ShadowMapTextureFlags(precision, filter), TEXTYPE_SHADOWMAP, GLTEXTURETYPE_2D, NULL, NULL);
1138 }
1139
1140 rtexture_t *R_LoadTextureShadowMapCube(rtexturepool_t *rtexturepool, const char *identifier, int width, int precision, qboolean filter)
1141 {
1142     return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, R_ShadowMapTextureFlags(precision, filter), TEXTYPE_SHADOWMAP, GLTEXTURETYPE_CUBEMAP, NULL, NULL);
1143 }
1144
1145 int R_TextureWidth(rtexture_t *rt)
1146 {
1147         return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1148 }
1149
1150 int R_TextureHeight(rtexture_t *rt)
1151 {
1152         return rt ? ((gltexture_t *)rt)->inputheight : 0;
1153 }
1154
1155 void R_UpdateTexture(rtexture_t *rt, const unsigned char *data, int x, int y, int width, int height)
1156 {
1157         gltexture_t *glt;
1158         if (rt == NULL)
1159                 Host_Error("R_UpdateTexture: no texture supplied");
1160         if (data == NULL)
1161                 Host_Error("R_UpdateTexture: no data supplied");
1162
1163         // we need it to be uploaded before we can update a part of it
1164         R_RealGetTexture(rt);
1165
1166         // update part of the texture
1167         glt = (gltexture_t *)rt;
1168         R_Upload(glt, data, x, y, 0, width, height, 1);
1169 }
1170
1171 void R_ClearTexture (rtexture_t *rt)
1172 {
1173         gltexture_t *glt = (gltexture_t *)rt;
1174
1175         R_Upload( glt, NULL, 0, 0, 0, glt->tilewidth, glt->tileheight, glt->tiledepth );
1176 }