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)"};
23 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
24 int gl_filter_mag = GL_LINEAR;
27 static mempool_t *texturemempool;
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
39 typedef struct textypeinfo_s
42 int inputbytesperpixel;
43 int internalbytesperpixel;
44 float glinternalbytesperpixel;
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};
65 typedef enum gltexturetype_e
70 GLTEXTURETYPE_CUBEMAP,
71 GLTEXTURETYPE_RECTANGLE,
76 static int gltexturetypeenums[GLTEXTURETYPE_TOTAL] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB, GL_TEXTURE_RECTANGLE_ARB};
77 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};
78 static int gltexturetypedimensions[GLTEXTURETYPE_TOTAL] = {1, 2, 3, 2, 2};
79 static int cubemapside[6] =
81 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
82 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
83 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
84 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
85 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
86 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
89 typedef struct gltexture_s
91 // this field is exposed to the R_GetTexture macro, for speed reasons
92 // (must be identical in rtexture_t)
93 int texnum; // GL texture slot number
95 // dynamic texture stuff [11/22/2007 Black]
96 // used to hold the texture number of dirty textures
98 updatecallback_t updatecallback;
99 void *updatacallback_data;
100 // --- [11/22/2007 Black]
102 // pointer to texturepool (check this to see if the texture is allocated)
103 struct gltexturepool_s *pool;
104 // pointer to next texture in texturepool chain
105 struct gltexture_s *chain;
106 // name of the texture (this might be removed someday), no duplicates
107 char identifier[MAX_QPATH + 32];
108 // original data size in *inputtexels
109 int inputwidth, inputheight, inputdepth;
110 // copy of the original texture(s) supplied to the upload function, for
111 // delayed uploads (non-precached)
112 unsigned char *inputtexels;
113 // original data size in *inputtexels
115 // flags supplied to the LoadTexture function
116 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
118 // pointer to one of the textype_ structs
119 textypeinfo_t *textype;
120 // one of the GLTEXTURETYPE_ values
122 // palette if the texture is TEXTYPE_PALETTE
123 const unsigned int *palette;
124 // actual stored texture size after gl_picmip and gl_max_size are applied
125 // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
126 int tilewidth, tileheight, tiledepth;
127 // 1 or 6 depending on texturetype
131 // GL_RGB or GL_RGBA or GL_DEPTH_COMPONENT
134 int glinternalformat;
135 // GL_UNSIGNED_BYTE or GL_UNSIGNED_INT or GL_UNSIGNED_SHORT or GL_FLOAT
140 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
142 typedef struct gltexturepool_s
144 unsigned int sentinel;
145 struct gltexture_s *gltchain;
146 struct gltexturepool_s *next;
150 static gltexturepool_t *gltexturepoolchain = NULL;
152 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
153 static int resizebuffersize = 0;
154 static const unsigned char *texturebuffer;
155 static int texturebuffersize = 0;
157 static textypeinfo_t *R_GetTexTypeInfo(textype_t textype, int flags)
159 if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
161 if (flags & TEXF_ALPHA)
165 case TEXTYPE_PALETTE:
166 return &textype_palette_alpha_compress;
168 return &textype_rgba_alpha_compress;
170 return &textype_bgra_alpha_compress;
172 Host_Error("R_GetTexTypeInfo: unknown texture format");
180 case TEXTYPE_PALETTE:
181 return &textype_palette_compress;
183 return &textype_rgba_compress;
185 return &textype_bgra_compress;
187 Host_Error("R_GetTexTypeInfo: unknown texture format");
194 if (flags & TEXF_ALPHA)
198 case TEXTYPE_PALETTE:
199 return &textype_palette_alpha;
201 return &textype_rgba_alpha;
203 return &textype_bgra_alpha;
205 Host_Error("R_GetTexTypeInfo: unknown texture format");
213 case TEXTYPE_PALETTE:
214 return &textype_palette;
216 return &textype_rgba;
218 return &textype_bgra;
219 case TEXTYPE_SHADOWMAP:
220 return &textype_shadowmap;
222 Host_Error("R_GetTexTypeInfo: unknown texture format");
227 return NULL; // this line only to hush compiler warnings
230 // dynamic texture code [11/22/2007 Black]
231 void R_MarkDirtyTexture(rtexture_t *rt) {
232 gltexture_t *glt = (gltexture_t*) rt;
237 // dont do anything if the texture is already dirty (and make sure this *is* a dynamic texture after all!)
238 if( !glt->dirtytexnum && glt->flags & GLTEXF_DYNAMIC ) {
239 glt->dirtytexnum = glt->texnum;
240 // mark it as dirty, so R_RealGetTexture gets called
245 void R_MakeTextureDynamic(rtexture_t *rt, updatecallback_t updatecallback, void *data) {
246 gltexture_t *glt = (gltexture_t*) rt;
251 glt->flags |= GLTEXF_DYNAMIC;
252 glt->updatecallback = updatecallback;
253 glt->updatacallback_data = data;
254 glt->dirtytexnum = 0;
257 static void R_UpdateDynamicTexture(gltexture_t *glt) {
258 glt->texnum = glt->dirtytexnum;
259 // reset dirtytexnum again (not dirty anymore)
260 glt->dirtytexnum = 0;
261 // TODO: now assert that t->texnum != 0 ?
262 if( glt->updatecallback ) {
263 glt->updatecallback( (rtexture_t*) glt, glt->updatacallback_data );
267 static void R_UploadTexture(gltexture_t *t);
269 static void R_PrecacheTexture(gltexture_t *glt)
273 if (glt->flags & TEXF_ALWAYSPRECACHE)
275 else if (r_precachetextures.integer >= 2)
277 else if (r_precachetextures.integer >= 1)
278 if (glt->flags & TEXF_PRECACHE)
282 R_UploadTexture(glt);
285 int R_RealGetTexture(rtexture_t *rt)
290 glt = (gltexture_t *)rt;
291 if (glt->flags & GLTEXF_DYNAMIC)
292 R_UpdateDynamicTexture(glt);
293 if (glt->flags & GLTEXF_UPLOAD)
294 R_UploadTexture(glt);
302 void R_PurgeTexture(rtexture_t *rt)
304 if(rt && !(((gltexture_t*) rt)->flags & TEXF_PERSISTENT)) {
309 void R_FreeTexture(rtexture_t *rt)
311 gltexture_t *glt, **gltpointer;
313 glt = (gltexture_t *)rt;
315 Host_Error("R_FreeTexture: texture == NULL");
317 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
318 if (*gltpointer == glt)
319 *gltpointer = glt->chain;
321 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
323 if (!(glt->flags & GLTEXF_UPLOAD))
326 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
329 if (glt->inputtexels)
330 Mem_Free(glt->inputtexels);
334 rtexturepool_t *R_AllocTexturePool(void)
336 gltexturepool_t *pool;
337 if (texturemempool == NULL)
339 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
342 pool->next = gltexturepoolchain;
343 gltexturepoolchain = pool;
344 pool->sentinel = TEXTUREPOOL_SENTINEL;
345 return (rtexturepool_t *)pool;
348 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
350 gltexturepool_t *pool, **poolpointer;
351 if (rtexturepool == NULL)
353 if (*rtexturepool == NULL)
355 pool = (gltexturepool_t *)(*rtexturepool);
356 *rtexturepool = NULL;
357 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
358 Host_Error("R_FreeTexturePool: pool already freed");
359 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
360 if (*poolpointer == pool)
361 *poolpointer = pool->next;
363 Host_Error("R_FreeTexturePool: pool not linked");
364 while (pool->gltchain)
365 R_FreeTexture((rtexture_t *)pool->gltchain);
370 typedef struct glmode_s
373 int minification, magnification;
377 static glmode_t modes[6] =
379 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
380 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
381 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
382 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
383 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
384 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
387 static void GL_TextureMode_f (void)
392 gltexturepool_t *pool;
396 for (i = 0;i < 6;i++)
398 if (gl_filter_min == modes[i].minification)
400 Con_Printf("%s\n", modes[i].name);
404 Con_Print("current filter is unknown???\n");
408 for (i = 0;i < 6;i++)
409 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
413 Con_Print("bad filter name\n");
417 gl_filter_min = modes[i].minification;
418 gl_filter_mag = modes[i].magnification;
420 // change all the existing mipmap texture objects
421 // FIXME: force renderer(/client/something?) restart instead?
423 for (pool = gltexturepoolchain;pool;pool = pool->next)
425 for (glt = pool->gltchain;glt;glt = glt->chain)
427 // only update already uploaded images
428 if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
430 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
431 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
432 if (glt->flags & TEXF_MIPMAP)
434 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
438 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
440 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
441 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
447 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
449 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
451 if (gl_max_size.integer > gl_max_texture_size)
452 Cvar_SetValue("gl_max_size", gl_max_texture_size);
457 case GLTEXTURETYPE_1D:
458 case GLTEXTURETYPE_2D:
459 maxsize = gl_max_texture_size;
461 case GLTEXTURETYPE_3D:
462 maxsize = gl_max_3d_texture_size;
464 case GLTEXTURETYPE_CUBEMAP:
465 maxsize = gl_max_cube_map_texture_size;
469 if (flags & TEXF_PICMIP)
471 maxsize = min(maxsize, gl_max_size.integer);
472 picmip = gl_picmip.integer;
477 if (gl_support_arb_texture_non_power_of_two)
478 width2 = min(inwidth >> picmip, maxsize);
481 for (width2 = 1;width2 < inwidth;width2 <<= 1);
482 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
484 *outwidth = max(1, width2);
488 if (gl_support_arb_texture_non_power_of_two)
489 height2 = min(inheight >> picmip, maxsize);
492 for (height2 = 1;height2 < inheight;height2 <<= 1);
493 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
495 *outheight = max(1, height2);
499 if (gl_support_arb_texture_non_power_of_two)
500 depth2 = min(indepth >> picmip, maxsize);
503 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
504 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
506 *outdepth = max(1, depth2);
511 static int R_CalcTexelDataSize (gltexture_t *glt)
513 int width2, height2, depth2, size;
515 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
517 size = width2 * height2 * depth2;
519 if (glt->flags & TEXF_MIPMAP)
521 while (width2 > 1 || height2 > 1 || depth2 > 1)
529 size += width2 * height2 * depth2;
533 return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
536 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
540 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
541 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
543 gltexturepool_t *pool;
545 Con_Print("glsize input loaded mip alpha name\n");
546 for (pool = gltexturepoolchain;pool;pool = pool->next)
554 for (glt = pool->gltchain;glt;glt = glt->chain)
556 glsize = R_CalcTexelDataSize(glt);
557 isloaded = !(glt->flags & GLTEXF_UPLOAD);
559 pooltotalt += glsize;
560 pooltotalp += glt->inputdatasize;
564 poolloadedt += glsize;
565 poolloadedp += glt->inputdatasize;
568 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);
571 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);
572 sumtotal += pooltotal;
573 sumtotalt += pooltotalt;
574 sumtotalp += pooltotalp;
575 sumloaded += poolloaded;
576 sumloadedt += poolloadedt;
577 sumloadedp += poolloadedp;
580 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);
583 static void R_TextureStats_f(void)
585 R_TextureStats_Print(true, true, true);
588 static void r_textures_start(void)
590 // LordHavoc: allow any alignment
592 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
593 qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
595 texturemempool = Mem_AllocPool("texture management", 0, NULL);
597 // Disable JPEG screenshots if the DLL isn't loaded
598 if (! JPEG_OpenLibrary ())
599 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
600 // TODO: support png screenshots?
604 static void r_textures_shutdown(void)
606 rtexturepool_t *temp;
608 JPEG_CloseLibrary ();
610 while(gltexturepoolchain)
612 temp = (rtexturepool_t *) gltexturepoolchain;
613 R_FreeTexturePool(&temp);
616 resizebuffersize = 0;
617 texturebuffersize = 0;
619 colorconvertbuffer = NULL;
620 texturebuffer = NULL;
621 Mem_FreePool(&texturemempool);
624 static void r_textures_newmap(void)
628 void R_Textures_Init (void)
630 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
631 Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
632 Cvar_RegisterVariable (&gl_max_size);
633 Cvar_RegisterVariable (&gl_picmip);
634 Cvar_RegisterVariable (&r_lerpimages);
635 Cvar_RegisterVariable (&r_precachetextures);
636 Cvar_RegisterVariable (&gl_texture_anisotropy);
637 Cvar_RegisterVariable (&gl_texturecompression);
638 Cvar_RegisterVariable (&gl_texturecompression_color);
639 Cvar_RegisterVariable (&gl_texturecompression_normal);
640 Cvar_RegisterVariable (&gl_texturecompression_gloss);
641 Cvar_RegisterVariable (&gl_texturecompression_glow);
642 Cvar_RegisterVariable (&gl_texturecompression_2d);
643 Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
644 Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
645 Cvar_RegisterVariable (&gl_texturecompression_sky);
646 Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
648 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
651 void R_Textures_Frame (void)
653 static int old_aniso = 0;
655 // could do procedural texture animation here, if we keep track of which
656 // textures were accessed this frame...
658 // free the resize buffers
659 resizebuffersize = 0;
662 Mem_Free(resizebuffer);
665 if (colorconvertbuffer)
667 Mem_Free(colorconvertbuffer);
668 colorconvertbuffer = NULL;
671 if (old_aniso != gl_texture_anisotropy.integer)
674 gltexturepool_t *pool;
677 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
679 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
682 for (pool = gltexturepoolchain;pool;pool = pool->next)
684 for (glt = pool->gltchain;glt;glt = glt->chain)
686 // only update already uploaded images
687 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
689 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
691 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
692 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
694 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
701 void R_MakeResizeBufferBigger(int size)
703 if (resizebuffersize < size)
705 resizebuffersize = size;
707 Mem_Free(resizebuffer);
708 if (colorconvertbuffer)
709 Mem_Free(colorconvertbuffer);
710 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
711 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
712 if (!resizebuffer || !colorconvertbuffer)
713 Host_Error("R_Upload: out of memory");
717 static void GL_SetupTextureParameters(int flags, textype_t textype, int texturetype)
719 int textureenum = gltexturetypeenums[texturetype];
720 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
724 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
726 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
727 if (gl_texture_anisotropy.integer != aniso)
728 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
729 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
731 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
732 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
733 if (gltexturetypedimensions[texturetype] >= 3)
735 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
739 if (flags & TEXF_FORCENEAREST)
741 if (flags & TEXF_MIPMAP)
743 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
747 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
749 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
751 else if (flags & TEXF_FORCELINEAR)
753 if (flags & TEXF_MIPMAP)
755 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
757 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
761 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
766 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
768 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
772 if (flags & TEXF_MIPMAP)
774 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
778 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
780 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
783 if (textype == TEXTYPE_SHADOWMAP)
786 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);CHECKGLERROR
787 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);CHECKGLERROR
788 qglTexParameteri(textureenum, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);CHECKGLERROR
790 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE);CHECKGLERROR
791 qglTexParameteri(textureenum, GL_TEXTURE_COMPARE_FUNC_ARB, GL_ALWAYS);CHECKGLERROR
792 qglTexParameteri(textureenum, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY);CHECKGLERROR
799 static void R_Upload(gltexture_t *glt, const unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
801 int i, mip, width, height, depth;
803 const unsigned char *prevbuffer;
808 // we need to restore the texture binding after finishing the upload
809 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
810 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
812 // these are rounded up versions of the size to do better resampling
813 if (gl_support_arb_texture_non_power_of_two || glt->texturetype == GLTEXTURETYPE_RECTANGLE)
815 width = glt->inputwidth;
816 height = glt->inputheight;
817 depth = glt->inputdepth;
821 for (width = 1;width < glt->inputwidth ;width <<= 1);
822 for (height = 1;height < glt->inputheight;height <<= 1);
823 for (depth = 1;depth < glt->inputdepth ;depth <<= 1);
826 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
827 R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
829 if (prevbuffer == NULL)
831 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
832 prevbuffer = resizebuffer;
834 else if (glt->textype->textype == TEXTYPE_PALETTE)
836 // promote paletted to BGRA, so we only have to worry about BGRA in the rest of this code
837 Image_Copy8bitBGRA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
838 prevbuffer = colorconvertbuffer;
841 if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
843 // update a portion of the image
844 switch(glt->texturetype)
846 case GLTEXTURETYPE_1D:
847 qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
849 case GLTEXTURETYPE_2D:
850 qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
852 case GLTEXTURETYPE_3D:
853 qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
856 Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
862 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
863 Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
865 // upload the image for the first time
866 glt->flags &= ~GLTEXF_UPLOAD;
868 // cubemaps contain multiple images and thus get processed a bit differently
869 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
871 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
873 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
874 prevbuffer = resizebuffer;
877 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
879 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
880 prevbuffer = resizebuffer;
884 if (gl_support_texture_compression)
886 if (gl_texturecompression.integer >= 2)
887 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
889 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
892 switch(glt->texturetype)
894 case GLTEXTURETYPE_1D:
895 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
896 if (glt->flags & TEXF_MIPMAP)
898 while (width > 1 || height > 1 || depth > 1)
900 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
901 prevbuffer = resizebuffer;
902 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
906 case GLTEXTURETYPE_2D:
907 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
908 if (glt->flags & TEXF_MIPMAP)
910 while (width > 1 || height > 1 || depth > 1)
912 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
913 prevbuffer = resizebuffer;
914 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
918 case GLTEXTURETYPE_3D:
919 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
920 if (glt->flags & TEXF_MIPMAP)
922 while (width > 1 || height > 1 || depth > 1)
924 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
925 prevbuffer = resizebuffer;
926 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
930 case GLTEXTURETYPE_CUBEMAP:
931 // convert and upload each side in turn,
932 // from a continuous block of input texels
933 texturebuffer = (unsigned char *)prevbuffer;
934 for (i = 0;i < 6;i++)
936 prevbuffer = texturebuffer;
937 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
938 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
940 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
941 prevbuffer = resizebuffer;
944 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
946 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
947 prevbuffer = resizebuffer;
950 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
951 if (glt->flags & TEXF_MIPMAP)
953 while (width > 1 || height > 1 || depth > 1)
955 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
956 prevbuffer = resizebuffer;
957 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, prevbuffer);CHECKGLERROR
962 case GLTEXTURETYPE_RECTANGLE:
963 qglTexImage2D(GL_TEXTURE_RECTANGLE_ARB, mip++, glt->glinternalformat, width, height, 0, glt->glformat, glt->gltype, NULL);CHECKGLERROR
966 GL_SetupTextureParameters(glt->flags, glt->textype->textype, glt->texturetype);
968 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
971 static void R_UploadTexture (gltexture_t *glt)
973 if (!(glt->flags & GLTEXF_UPLOAD))
977 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
978 R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
979 if (glt->inputtexels)
981 Mem_Free(glt->inputtexels);
982 glt->inputtexels = NULL;
983 glt->flags |= GLTEXF_DESTROYED;
985 else if (glt->flags & GLTEXF_DESTROYED)
986 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
989 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)
993 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
994 textypeinfo_t *texinfo;
996 if (cls.state == ca_dedicated)
999 if (texturetype == GLTEXTURETYPE_RECTANGLE && !gl_texturerectangle)
1001 Con_Printf ("R_LoadTexture: rectangle texture not supported by driver\n");
1004 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1006 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
1009 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1011 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
1015 texinfo = R_GetTexTypeInfo(textype, flags);
1016 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1019 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
1023 // clear the alpha flag if the texture has no transparent pixels
1026 case TEXTYPE_PALETTE:
1027 if (flags & TEXF_ALPHA)
1029 flags &= ~TEXF_ALPHA;
1032 for (i = 0;i < size;i++)
1034 if (((unsigned char *)&palette[data[i]])[3] < 255)
1036 flags |= TEXF_ALPHA;
1045 if (flags & TEXF_ALPHA)
1047 flags &= ~TEXF_ALPHA;
1050 for (i = 3;i < size;i += 4)
1054 flags |= TEXF_ALPHA;
1061 case TEXTYPE_SHADOWMAP:
1064 Host_Error("R_LoadTexture: unknown texture type");
1067 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1069 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1071 glt->chain = pool->gltchain;
1072 pool->gltchain = glt;
1073 glt->inputwidth = width;
1074 glt->inputheight = height;
1075 glt->inputdepth = depth;
1076 glt->flags = flags | GLTEXF_UPLOAD;
1077 glt->textype = texinfo;
1078 glt->texturetype = texturetype;
1079 glt->inputdatasize = size;
1080 glt->palette = palette;
1081 glt->glinternalformat = texinfo->glinternalformat;
1082 glt->glformat = texinfo->glformat;
1083 glt->gltype = texinfo->gltype;
1084 glt->bytesperpixel = texinfo->internalbytesperpixel;
1085 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1087 // init the dynamic texture attributes, too [11/22/2007 Black]
1088 glt->dirtytexnum = 0;
1089 glt->updatecallback = NULL;
1090 glt->updatacallback_data = NULL;
1094 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1095 memcpy(glt->inputtexels, data, size);
1098 glt->inputtexels = NULL;
1100 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1101 R_PrecacheTexture(glt);
1103 // texture converting and uploading can take a while, so make sure we're sending keepalives
1104 CL_KeepaliveMessage(false);
1106 return (rtexture_t *)glt;
1109 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)
1111 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1114 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)
1116 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1119 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)
1121 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1124 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)
1126 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1129 rtexture_t *R_LoadTextureShadowMapRectangle(rtexturepool_t *rtexturepool, const char *identifier, int width, int height)
1131 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, TEXF_ALWAYSPRECACHE | TEXF_FORCENEAREST | TEXF_CLAMP, TEXTYPE_SHADOWMAP, GLTEXTURETYPE_RECTANGLE, NULL, NULL);
1134 rtexture_t *R_LoadTextureShadowMapCube(rtexturepool_t *rtexturepool, const char *identifier, int width)
1136 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, TEXF_ALWAYSPRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, TEXTYPE_SHADOWMAP, GLTEXTURETYPE_CUBEMAP, NULL, NULL);
1139 rtexture_t *R_LoadTextureShadowMap2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height)
1141 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, TEXF_ALWAYSPRECACHE | TEXF_FORCENEAREST | TEXF_CLAMP, TEXTYPE_SHADOWMAP, GLTEXTURETYPE_2D, NULL, NULL);
1144 rtexture_t *R_LoadTextureCubeProjection(rtexturepool_t *rtexturepool, const char *identifier)
1146 return R_SetupTexture(rtexturepool, identifier, 2, 2, 1, 6, TEXF_ALWAYSPRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, TEXTYPE_BGRA, GLTEXTURETYPE_CUBEMAP, NULL, NULL);
1149 int R_TextureHasAlpha(rtexture_t *rt)
1151 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1154 int R_TextureWidth(rtexture_t *rt)
1156 return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1159 int R_TextureHeight(rtexture_t *rt)
1161 return rt ? ((gltexture_t *)rt)->inputheight : 0;
1164 void R_UpdateTexture(rtexture_t *rt, const unsigned char *data, int x, int y, int width, int height)
1168 Host_Error("R_UpdateTexture: no texture supplied");
1170 Host_Error("R_UpdateTexture: no data supplied");
1171 glt = (gltexture_t *)rt;
1173 // we need it to be uploaded before we can update a part of it
1174 if (glt->flags & GLTEXF_UPLOAD)
1175 R_UploadTexture(glt);
1177 // update part of the texture
1178 R_Upload(glt, data, x, y, 0, width, height, 1);
1181 void R_ClearTexture (rtexture_t *rt)
1183 gltexture_t *glt = (gltexture_t *)rt;
1185 R_Upload( glt, NULL, 0, 0, 0, glt->tilewidth, glt->tileheight, glt->tiledepth );