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