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