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", "1", "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", "1", "whether to compress 2d (hud/menu) textures other than the font"};
18 cvar_t gl_texturecompression_q3bsplightmaps = {CVAR_SAVE, "gl_texturecompression_q3bsplightmaps", "1", "whether to compress lightmaps in q3bsp format levels"};
19 cvar_t gl_texturecompression_q3bspdeluxemaps = {CVAR_SAVE, "gl_texturecompression_q3bspdeluxemaps", "1", "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;
50 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_BGRA , 3};
51 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_BGRA , 4};
52 static textypeinfo_t textype_palette_compress = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_BGRA , GL_COMPRESSED_RGB_ARB};
53 static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_BGRA , GL_COMPRESSED_RGBA_ARB};
54 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , 3};
55 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , 4};
56 static textypeinfo_t textype_rgba_compress = {TEXTYPE_RGBA , 4, 4, 0.5f, GL_RGBA , GL_COMPRESSED_RGB_ARB};
57 static textypeinfo_t textype_rgba_alpha_compress = {TEXTYPE_RGBA , 4, 4, 1.0f, GL_RGBA , GL_COMPRESSED_RGBA_ARB};
58 static textypeinfo_t textype_bgra = {TEXTYPE_BGRA , 4, 4, 4.0f, GL_BGRA , 3};
59 static textypeinfo_t textype_bgra_alpha = {TEXTYPE_BGRA , 4, 4, 4.0f, GL_BGRA , 4};
60 static textypeinfo_t textype_bgra_compress = {TEXTYPE_BGRA , 4, 4, 0.5f, GL_BGRA , GL_COMPRESSED_RGB_ARB};
61 static textypeinfo_t textype_bgra_alpha_compress = {TEXTYPE_BGRA , 4, 4, 1.0f, GL_BGRA , GL_COMPRESSED_RGBA_ARB};
63 #define GLTEXTURETYPE_1D 0
64 #define GLTEXTURETYPE_2D 1
65 #define GLTEXTURETYPE_3D 2
66 #define GLTEXTURETYPE_CUBEMAP 3
68 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
69 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
70 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
71 static int cubemapside[6] =
73 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
74 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
75 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
76 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
77 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
78 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
81 typedef struct gltexture_s
83 // this field is exposed to the R_GetTexture macro, for speed reasons
84 // (must be identical in rtexture_t)
85 int texnum; // GL texture slot number
87 // dynamic texture stuff [11/22/2007 Black]
88 // used to hold the texture number of dirty textures
90 updatecallback_t updatecallback;
91 void *updatacallback_data;
92 // --- [11/22/2007 Black]
94 // pointer to texturepool (check this to see if the texture is allocated)
95 struct gltexturepool_s *pool;
96 // pointer to next texture in texturepool chain
97 struct gltexture_s *chain;
98 // name of the texture (this might be removed someday), no duplicates
99 char identifier[MAX_QPATH + 32];
100 // original data size in *inputtexels
101 int inputwidth, inputheight, inputdepth;
102 // copy of the original texture(s) supplied to the upload function, for
103 // delayed uploads (non-precached)
104 unsigned char *inputtexels;
105 // original data size in *inputtexels
107 // flags supplied to the LoadTexture function
108 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
110 // pointer to one of the textype_ structs
111 textypeinfo_t *textype;
112 // one of the GLTEXTURETYPE_ values
114 // palette if the texture is TEXTYPE_PALETTE
115 const unsigned int *palette;
116 // actual stored texture size after gl_picmip and gl_max_size are applied
117 // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
118 int tilewidth, tileheight, tiledepth;
119 // 1 or 6 depending on texturetype
126 int glinternalformat;
130 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
132 typedef struct gltexturepool_s
134 unsigned int sentinel;
135 struct gltexture_s *gltchain;
136 struct gltexturepool_s *next;
140 static gltexturepool_t *gltexturepoolchain = NULL;
142 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
143 static int resizebuffersize = 0;
144 static const unsigned char *texturebuffer;
145 static int texturebuffersize = 0;
147 static textypeinfo_t *R_GetTexTypeInfo(textype_t textype, int flags)
149 if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
151 if (flags & TEXF_ALPHA)
155 case TEXTYPE_PALETTE:
156 return &textype_palette_alpha_compress;
158 return &textype_rgba_alpha_compress;
160 return &textype_bgra_alpha_compress;
162 Host_Error("R_GetTexTypeInfo: unknown texture format");
170 case TEXTYPE_PALETTE:
171 return &textype_palette_compress;
173 return &textype_rgba_compress;
175 return &textype_bgra_compress;
177 Host_Error("R_GetTexTypeInfo: unknown texture format");
184 if (flags & TEXF_ALPHA)
188 case TEXTYPE_PALETTE:
189 return &textype_palette_alpha;
191 return &textype_rgba_alpha;
193 return &textype_bgra_alpha;
195 Host_Error("R_GetTexTypeInfo: unknown texture format");
203 case TEXTYPE_PALETTE:
204 return &textype_palette;
206 return &textype_rgba;
208 return &textype_bgra;
210 Host_Error("R_GetTexTypeInfo: unknown texture format");
215 return NULL; // this line only to hush compiler warnings
218 // dynamic texture code [11/22/2007 Black]
219 void R_MarkDirtyTexture(rtexture_t *rt) {
220 gltexture_t *glt = (gltexture_t*) rt;
225 // dont do anything if the texture is already dirty (and make sure this *is* a dynamic texture after all!)
226 if( !glt->dirtytexnum && glt->flags & GLTEXF_DYNAMIC ) {
227 glt->dirtytexnum = glt->texnum;
228 // mark it as dirty, so R_RealGetTexture gets called
233 void R_MakeTextureDynamic(rtexture_t *rt, updatecallback_t updatecallback, void *data) {
234 gltexture_t *glt = (gltexture_t*) rt;
239 glt->flags |= GLTEXF_DYNAMIC;
240 glt->updatecallback = updatecallback;
241 glt->updatacallback_data = data;
242 glt->dirtytexnum = 0;
245 static void R_UpdateDynamicTexture(gltexture_t *glt) {
246 glt->texnum = glt->dirtytexnum;
247 // reset dirtytexnum again (not dirty anymore)
248 glt->dirtytexnum = 0;
249 // TODO: now assert that t->texnum != 0 ?
250 if( glt->updatecallback ) {
251 glt->updatecallback( (rtexture_t*) glt, glt->updatacallback_data );
255 static void R_UploadTexture(gltexture_t *t);
257 static void R_PrecacheTexture(gltexture_t *glt)
261 if (glt->flags & TEXF_ALWAYSPRECACHE)
263 else if (r_precachetextures.integer >= 2)
265 else if (r_precachetextures.integer >= 1)
266 if (glt->flags & TEXF_PRECACHE)
270 R_UploadTexture(glt);
273 int R_RealGetTexture(rtexture_t *rt)
278 glt = (gltexture_t *)rt;
279 if (glt->flags & GLTEXF_DYNAMIC)
280 R_UpdateDynamicTexture(glt);
281 if (glt->flags & GLTEXF_UPLOAD)
282 R_UploadTexture(glt);
290 void R_PurgeTexture(rtexture_t *rt)
292 if(rt && !(((gltexture_t*) rt)->flags & TEXF_PERSISTENT)) {
297 void R_FreeTexture(rtexture_t *rt)
299 gltexture_t *glt, **gltpointer;
301 glt = (gltexture_t *)rt;
303 Host_Error("R_FreeTexture: texture == NULL");
305 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
306 if (*gltpointer == glt)
307 *gltpointer = glt->chain;
309 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
314 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
317 if (glt->inputtexels)
318 Mem_Free(glt->inputtexels);
322 rtexturepool_t *R_AllocTexturePool(void)
324 gltexturepool_t *pool;
325 if (texturemempool == NULL)
327 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
330 pool->next = gltexturepoolchain;
331 gltexturepoolchain = pool;
332 pool->sentinel = TEXTUREPOOL_SENTINEL;
333 return (rtexturepool_t *)pool;
336 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
338 gltexturepool_t *pool, **poolpointer;
339 if (rtexturepool == NULL)
341 if (*rtexturepool == NULL)
343 pool = (gltexturepool_t *)(*rtexturepool);
344 *rtexturepool = NULL;
345 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
346 Host_Error("R_FreeTexturePool: pool already freed");
347 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
348 if (*poolpointer == pool)
349 *poolpointer = pool->next;
351 Host_Error("R_FreeTexturePool: pool not linked");
352 while (pool->gltchain)
353 R_FreeTexture((rtexture_t *)pool->gltchain);
358 typedef struct glmode_s
361 int minification, magnification;
365 static glmode_t modes[6] =
367 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
368 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
369 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
370 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
371 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
372 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
375 static void GL_TextureMode_f (void)
380 gltexturepool_t *pool;
384 for (i = 0;i < 6;i++)
386 if (gl_filter_min == modes[i].minification)
388 Con_Printf("%s\n", modes[i].name);
392 Con_Print("current filter is unknown???\n");
396 for (i = 0;i < 6;i++)
397 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
401 Con_Print("bad filter name\n");
405 gl_filter_min = modes[i].minification;
406 gl_filter_mag = modes[i].magnification;
408 // change all the existing mipmap texture objects
409 // FIXME: force renderer(/client/something?) restart instead?
411 for (pool = gltexturepoolchain;pool;pool = pool->next)
413 for (glt = pool->gltchain;glt;glt = glt->chain)
415 // only update already uploaded images
416 if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
418 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
419 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
420 if (glt->flags & TEXF_MIPMAP)
422 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
426 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
428 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
429 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
435 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
437 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
439 if (gl_max_size.integer > gl_max_texture_size)
440 Cvar_SetValue("gl_max_size", gl_max_texture_size);
445 case GLTEXTURETYPE_1D:
446 case GLTEXTURETYPE_2D:
447 maxsize = gl_max_texture_size;
449 case GLTEXTURETYPE_3D:
450 maxsize = gl_max_3d_texture_size;
452 case GLTEXTURETYPE_CUBEMAP:
453 maxsize = gl_max_cube_map_texture_size;
457 if (flags & TEXF_PICMIP)
459 maxsize = min(maxsize, gl_max_size.integer);
460 picmip = gl_picmip.integer;
465 if (gl_support_arb_texture_non_power_of_two)
466 width2 = min(inwidth >> picmip, maxsize);
469 for (width2 = 1;width2 < inwidth;width2 <<= 1);
470 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
472 *outwidth = max(1, width2);
476 if (gl_support_arb_texture_non_power_of_two)
477 height2 = min(inheight >> picmip, maxsize);
480 for (height2 = 1;height2 < inheight;height2 <<= 1);
481 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
483 *outheight = max(1, height2);
487 if (gl_support_arb_texture_non_power_of_two)
488 depth2 = min(indepth >> picmip, maxsize);
491 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
492 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
494 *outdepth = max(1, depth2);
499 static int R_CalcTexelDataSize (gltexture_t *glt)
501 int width2, height2, depth2, size;
503 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
505 size = width2 * height2 * depth2;
507 if (glt->flags & TEXF_MIPMAP)
509 while (width2 > 1 || height2 > 1 || depth2 > 1)
517 size += width2 * height2 * depth2;
521 return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
524 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
528 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
529 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
531 gltexturepool_t *pool;
533 Con_Print("glsize input loaded mip alpha name\n");
534 for (pool = gltexturepoolchain;pool;pool = pool->next)
542 for (glt = pool->gltchain;glt;glt = glt->chain)
544 glsize = R_CalcTexelDataSize(glt);
545 isloaded = !(glt->flags & GLTEXF_UPLOAD);
547 pooltotalt += glsize;
548 pooltotalp += glt->inputdatasize;
552 poolloadedt += glsize;
553 poolloadedp += glt->inputdatasize;
556 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);
559 Con_Printf("texturepool %10p total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", 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);
560 sumtotal += pooltotal;
561 sumtotalt += pooltotalt;
562 sumtotalp += pooltotalp;
563 sumloaded += poolloaded;
564 sumloadedt += poolloadedt;
565 sumloadedp += poolloadedp;
568 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);
571 static void R_TextureStats_f(void)
573 R_TextureStats_Print(true, true, true);
576 static void r_textures_start(void)
578 // LordHavoc: allow any alignment
580 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
581 qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
583 texturemempool = Mem_AllocPool("texture management", 0, NULL);
585 // Disable JPEG screenshots if the DLL isn't loaded
586 if (! JPEG_OpenLibrary ())
587 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
588 // TODO: support png screenshots?
592 static void r_textures_shutdown(void)
594 rtexturepool_t *temp;
596 JPEG_CloseLibrary ();
598 while(gltexturepoolchain)
600 temp = (rtexturepool_t *) gltexturepoolchain;
601 R_FreeTexturePool(&temp);
604 resizebuffersize = 0;
605 texturebuffersize = 0;
607 colorconvertbuffer = NULL;
608 texturebuffer = NULL;
609 Mem_FreePool(&texturemempool);
612 static void r_textures_newmap(void)
616 void R_Textures_Init (void)
618 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
619 Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
620 Cvar_RegisterVariable (&gl_max_size);
621 Cvar_RegisterVariable (&gl_picmip);
622 Cvar_RegisterVariable (&r_lerpimages);
623 Cvar_RegisterVariable (&r_precachetextures);
624 Cvar_RegisterVariable (&gl_texture_anisotropy);
625 Cvar_RegisterVariable (&gl_texturecompression);
626 Cvar_RegisterVariable (&gl_texturecompression_color);
627 Cvar_RegisterVariable (&gl_texturecompression_normal);
628 Cvar_RegisterVariable (&gl_texturecompression_gloss);
629 Cvar_RegisterVariable (&gl_texturecompression_glow);
630 Cvar_RegisterVariable (&gl_texturecompression_2d);
631 Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
632 Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
633 Cvar_RegisterVariable (&gl_texturecompression_sky);
634 Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
636 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
639 void R_Textures_Frame (void)
641 static int old_aniso = 0;
643 // could do procedural texture animation here, if we keep track of which
644 // textures were accessed this frame...
646 // free the resize buffers
647 resizebuffersize = 0;
650 Mem_Free(resizebuffer);
653 if (colorconvertbuffer)
655 Mem_Free(colorconvertbuffer);
656 colorconvertbuffer = NULL;
659 if (old_aniso != gl_texture_anisotropy.integer)
662 gltexturepool_t *pool;
665 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
667 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
670 for (pool = gltexturepoolchain;pool;pool = pool->next)
672 for (glt = pool->gltchain;glt;glt = glt->chain)
674 // only update already uploaded images
675 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
677 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
679 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
680 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
682 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
689 void R_MakeResizeBufferBigger(int size)
691 if (resizebuffersize < size)
693 resizebuffersize = size;
695 Mem_Free(resizebuffer);
696 if (colorconvertbuffer)
697 Mem_Free(colorconvertbuffer);
698 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
699 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
700 if (!resizebuffer || !colorconvertbuffer)
701 Host_Error("R_Upload: out of memory");
705 static void GL_SetupTextureParameters(int flags, int texturetype)
707 int textureenum = gltexturetypeenums[texturetype];
708 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
712 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
714 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
715 if (gl_texture_anisotropy.integer != aniso)
716 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
717 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
719 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
720 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
721 if (gltexturetypedimensions[texturetype] >= 3)
723 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
727 if (flags & TEXF_FORCENEAREST)
729 if (flags & TEXF_MIPMAP)
731 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
735 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
737 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
739 else if (flags & TEXF_FORCELINEAR)
741 if (flags & TEXF_MIPMAP)
743 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
745 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
749 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
754 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
756 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
760 if (flags & TEXF_MIPMAP)
762 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
766 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
768 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
774 static void R_Upload(gltexture_t *glt, const unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
776 int i, mip, width, height, depth;
778 const unsigned char *prevbuffer;
783 // we need to restore the texture binding after finishing the upload
784 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
785 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
787 // these are rounded up versions of the size to do better resampling
788 if (gl_support_arb_texture_non_power_of_two)
790 width = glt->inputwidth;
791 height = glt->inputheight;
792 depth = glt->inputdepth;
796 for (width = 1;width < glt->inputwidth ;width <<= 1);
797 for (height = 1;height < glt->inputheight;height <<= 1);
798 for (depth = 1;depth < glt->inputdepth ;depth <<= 1);
801 if (prevbuffer != NULL)
803 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
804 R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
805 if (glt->textype->textype == TEXTYPE_PALETTE)
807 // promote paletted to BGRA, so we only have to worry about BGRA in the rest of this code
808 Image_Copy8bitBGRA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
809 prevbuffer = colorconvertbuffer;
813 if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
815 // update a portion of the image
816 switch(glt->texturetype)
818 case GLTEXTURETYPE_1D:
819 qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
821 case GLTEXTURETYPE_2D:
822 qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
824 case GLTEXTURETYPE_3D:
825 qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
828 Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
834 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
835 Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
837 // upload the image for the first time
838 glt->flags &= ~GLTEXF_UPLOAD;
840 // cubemaps contain multiple images and thus get processed a bit differently
841 if ((prevbuffer != NULL) && (glt->texturetype != GLTEXTURETYPE_CUBEMAP))
843 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
845 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
846 prevbuffer = resizebuffer;
849 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
851 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
852 prevbuffer = resizebuffer;
856 if (gl_support_texture_compression)
858 if (gl_texturecompression.integer >= 2)
859 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
861 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
864 switch(glt->texturetype)
866 case GLTEXTURETYPE_1D:
867 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
868 if (glt->flags & TEXF_MIPMAP)
870 while (width > 1 || height > 1 || depth > 1)
872 if (prevbuffer != NULL)
874 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
875 prevbuffer = resizebuffer;
877 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
881 case GLTEXTURETYPE_2D:
882 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
883 if (glt->flags & TEXF_MIPMAP)
885 while (width > 1 || height > 1 || depth > 1)
887 if (prevbuffer != NULL)
889 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
890 prevbuffer = resizebuffer;
892 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
896 case GLTEXTURETYPE_3D:
897 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
898 if (glt->flags & TEXF_MIPMAP)
900 while (width > 1 || height > 1 || depth > 1)
902 if (prevbuffer != NULL)
904 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
905 prevbuffer = resizebuffer;
907 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
911 case GLTEXTURETYPE_CUBEMAP:
912 // convert and upload each side in turn,
913 // from a continuous block of input texels
914 texturebuffer = prevbuffer ? (unsigned char *)prevbuffer : NULL;
915 for (i = 0;i < 6;i++)
917 prevbuffer = texturebuffer;
918 if (prevbuffer != NULL)
920 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
921 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
923 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
924 prevbuffer = resizebuffer;
927 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
929 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
930 prevbuffer = resizebuffer;
934 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
935 if (glt->flags & TEXF_MIPMAP)
937 while (width > 1 || height > 1 || depth > 1)
939 if (prevbuffer != NULL)
941 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
942 prevbuffer = resizebuffer;
944 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
950 GL_SetupTextureParameters(glt->flags, glt->texturetype);
952 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
955 static void R_UploadTexture (gltexture_t *glt)
957 if (!(glt->flags & GLTEXF_UPLOAD))
961 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
962 R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
963 if (glt->inputtexels)
965 Mem_Free(glt->inputtexels);
966 glt->inputtexels = NULL;
967 glt->flags |= GLTEXF_DESTROYED;
969 else if (glt->flags & GLTEXF_DESTROYED)
970 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
973 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)
977 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
978 textypeinfo_t *texinfo;
980 if (cls.state == ca_dedicated)
983 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
985 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
988 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
990 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
994 texinfo = R_GetTexTypeInfo(textype, flags);
995 size = width * height * depth * sides * texinfo->inputbytesperpixel;
998 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
1002 // clear the alpha flag if the texture has no transparent pixels
1005 case TEXTYPE_PALETTE:
1006 if (flags & TEXF_ALPHA)
1008 flags &= ~TEXF_ALPHA;
1011 for (i = 0;i < size;i++)
1013 if (((unsigned char *)&palette[data[i]])[3] < 255)
1015 flags |= TEXF_ALPHA;
1024 if (flags & TEXF_ALPHA)
1026 flags &= ~TEXF_ALPHA;
1029 for (i = 3;i < size;i += 4)
1033 flags |= TEXF_ALPHA;
1041 Host_Error("R_LoadTexture: unknown texture type");
1044 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1046 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1048 glt->chain = pool->gltchain;
1049 pool->gltchain = glt;
1050 glt->inputwidth = width;
1051 glt->inputheight = height;
1052 glt->inputdepth = depth;
1053 glt->flags = flags | GLTEXF_UPLOAD;
1054 glt->textype = texinfo;
1055 glt->texturetype = texturetype;
1056 glt->inputdatasize = size;
1057 glt->palette = palette;
1058 glt->glinternalformat = texinfo->glinternalformat;
1059 glt->glformat = texinfo->glformat;
1060 glt->bytesperpixel = texinfo->internalbytesperpixel;
1061 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1063 // init the dynamic texture attributes, too [11/22/2007 Black]
1064 glt->dirtytexnum = 0;
1065 glt->updatecallback = NULL;
1066 glt->updatacallback_data = NULL;
1070 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1071 memcpy(glt->inputtexels, data, size);
1074 glt->inputtexels = NULL;
1076 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1077 R_PrecacheTexture(glt);
1079 return (rtexture_t *)glt;
1082 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)
1084 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1087 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)
1089 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1092 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)
1094 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1097 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)
1099 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1102 int R_TextureHasAlpha(rtexture_t *rt)
1104 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1107 int R_TextureWidth(rtexture_t *rt)
1109 return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1112 int R_TextureHeight(rtexture_t *rt)
1114 return rt ? ((gltexture_t *)rt)->inputheight : 0;
1117 void R_UpdateTexture(rtexture_t *rt, const unsigned char *data, int x, int y, int width, int height)
1121 Host_Error("R_UpdateTexture: no texture supplied");
1123 Host_Error("R_UpdateTexture: no data supplied");
1124 glt = (gltexture_t *)rt;
1126 // we need it to be uploaded before we can update a part of it
1127 if (glt->flags & GLTEXF_UPLOAD)
1128 R_UploadTexture(glt);
1130 // update part of the texture
1131 R_Upload(glt, data, x, y, 0, width, height, 1);
1134 void R_ClearTexture (rtexture_t *rt)
1136 gltexture_t *glt = (gltexture_t *)rt;
1138 R_Upload( glt, NULL, 0, 0, 0, glt->tilewidth, glt->tileheight, glt->tiledepth );