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_RGBA , 3};
51 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , 3};
52 static textypeinfo_t textype_bgra = {TEXTYPE_BGRA , 4, 4, 4.0f, GL_BGRA , 3};
53 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA , 4};
54 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , 4};
55 static textypeinfo_t textype_bgra_alpha = {TEXTYPE_BGRA , 4, 4, 4.0f, GL_BGRA , 4};
56 static textypeinfo_t textype_palette_compress = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_RGBA , GL_COMPRESSED_RGB_ARB};
57 static textypeinfo_t textype_rgba_compress = {TEXTYPE_RGBA , 4, 4, 0.5f, GL_RGBA , GL_COMPRESSED_RGB_ARB};
58 static textypeinfo_t textype_bgra_compress = {TEXTYPE_BGRA , 4, 4, 0.5f, GL_BGRA , GL_COMPRESSED_RGB_ARB};
59 static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_RGBA , GL_COMPRESSED_RGBA_ARB};
60 static textypeinfo_t textype_rgba_alpha_compress = {TEXTYPE_RGBA , 4, 4, 1.0f, GL_RGBA , GL_COMPRESSED_RGBA_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 unsigned char *texturebuffer;
145 static int texturebuffersize = 0;
147 static textypeinfo_t *R_GetTexTypeInfo(int 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;
244 static void R_UpdateDynamicTexture(gltexture_t *glt) {
245 glt->texnum = glt->dirtytexnum;
246 // reset dirtytexnum again (not dirty anymore)
247 glt->dirtytexnum = 0;
248 // TODO: now assert that t->texnum != 0 ?
249 if( glt->updatecallback ) {
250 glt->updatecallback( (rtexture_t*) glt, glt->updatacallback_data );
254 static void R_UploadTexture(gltexture_t *t);
256 static void R_PrecacheTexture(gltexture_t *glt)
260 if (glt->flags & TEXF_ALWAYSPRECACHE)
262 else if (r_precachetextures.integer >= 2)
264 else if (r_precachetextures.integer >= 1)
265 if (glt->flags & TEXF_PRECACHE)
269 R_UploadTexture(glt);
272 int R_RealGetTexture(rtexture_t *rt)
277 glt = (gltexture_t *)rt;
278 if (glt->flags & GLTEXF_DYNAMIC)
279 R_UpdateDynamicTexture(glt);
280 if (glt->flags & GLTEXF_UPLOAD)
281 R_UploadTexture(glt);
289 void R_FreeTexture(rtexture_t *rt)
291 gltexture_t *glt, **gltpointer;
293 glt = (gltexture_t *)rt;
295 Host_Error("R_FreeTexture: texture == NULL");
297 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
298 if (*gltpointer == glt)
299 *gltpointer = glt->chain;
301 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
306 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
309 if (glt->inputtexels)
310 Mem_Free(glt->inputtexels);
314 rtexturepool_t *R_AllocTexturePool(void)
316 gltexturepool_t *pool;
317 if (texturemempool == NULL)
319 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
322 pool->next = gltexturepoolchain;
323 gltexturepoolchain = pool;
324 pool->sentinel = TEXTUREPOOL_SENTINEL;
325 return (rtexturepool_t *)pool;
328 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
330 gltexturepool_t *pool, **poolpointer;
331 if (rtexturepool == NULL)
333 if (*rtexturepool == NULL)
335 pool = (gltexturepool_t *)(*rtexturepool);
336 *rtexturepool = NULL;
337 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
338 Host_Error("R_FreeTexturePool: pool already freed");
339 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
340 if (*poolpointer == pool)
341 *poolpointer = pool->next;
343 Host_Error("R_FreeTexturePool: pool not linked");
344 while (pool->gltchain)
345 R_FreeTexture((rtexture_t *)pool->gltchain);
350 typedef struct glmode_s
353 int minification, magnification;
357 static glmode_t modes[6] =
359 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
360 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
361 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
362 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
363 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
364 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
367 static void GL_TextureMode_f (void)
372 gltexturepool_t *pool;
376 for (i = 0;i < 6;i++)
378 if (gl_filter_min == modes[i].minification)
380 Con_Printf("%s\n", modes[i].name);
384 Con_Print("current filter is unknown???\n");
388 for (i = 0;i < 6;i++)
389 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
393 Con_Print("bad filter name\n");
397 gl_filter_min = modes[i].minification;
398 gl_filter_mag = modes[i].magnification;
400 // change all the existing mipmap texture objects
401 // FIXME: force renderer(/client/something?) restart instead?
403 for (pool = gltexturepoolchain;pool;pool = pool->next)
405 for (glt = pool->gltchain;glt;glt = glt->chain)
407 // only update already uploaded images
408 if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
410 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
411 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
412 if (glt->flags & TEXF_MIPMAP)
414 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
418 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
420 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
421 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
427 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
429 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
431 if (gl_max_size.integer > gl_max_texture_size)
432 Cvar_SetValue("gl_max_size", gl_max_texture_size);
437 case GLTEXTURETYPE_1D:
438 case GLTEXTURETYPE_2D:
439 maxsize = gl_max_texture_size;
441 case GLTEXTURETYPE_3D:
442 maxsize = gl_max_3d_texture_size;
444 case GLTEXTURETYPE_CUBEMAP:
445 maxsize = gl_max_cube_map_texture_size;
449 if (flags & TEXF_PICMIP)
451 maxsize = min(maxsize, gl_max_size.integer);
452 picmip = gl_picmip.integer;
457 if (gl_support_arb_texture_non_power_of_two)
458 width2 = min(inwidth >> picmip, maxsize);
461 for (width2 = 1;width2 < inwidth;width2 <<= 1);
462 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
464 *outwidth = max(1, width2);
468 if (gl_support_arb_texture_non_power_of_two)
469 height2 = min(inheight >> picmip, maxsize);
472 for (height2 = 1;height2 < inheight;height2 <<= 1);
473 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
475 *outheight = max(1, height2);
479 if (gl_support_arb_texture_non_power_of_two)
480 depth2 = min(indepth >> picmip, maxsize);
483 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
484 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
486 *outdepth = max(1, depth2);
491 static int R_CalcTexelDataSize (gltexture_t *glt)
493 int width2, height2, depth2, size;
495 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
497 size = width2 * height2 * depth2;
499 if (glt->flags & TEXF_MIPMAP)
501 while (width2 > 1 || height2 > 1 || depth2 > 1)
509 size += width2 * height2 * depth2;
513 return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
516 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
520 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
521 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
523 gltexturepool_t *pool;
525 Con_Print("glsize input loaded mip alpha name\n");
526 for (pool = gltexturepoolchain;pool;pool = pool->next)
534 for (glt = pool->gltchain;glt;glt = glt->chain)
536 glsize = R_CalcTexelDataSize(glt);
537 isloaded = !(glt->flags & GLTEXF_UPLOAD);
539 pooltotalt += glsize;
540 pooltotalp += glt->inputdatasize;
544 poolloadedt += glsize;
545 poolloadedp += glt->inputdatasize;
548 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);
551 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);
552 sumtotal += pooltotal;
553 sumtotalt += pooltotalt;
554 sumtotalp += pooltotalp;
555 sumloaded += poolloaded;
556 sumloadedt += poolloadedt;
557 sumloadedp += poolloadedp;
560 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);
563 static void R_TextureStats_f(void)
565 R_TextureStats_Print(true, true, true);
568 static void r_textures_start(void)
570 // LordHavoc: allow any alignment
572 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
573 qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
575 texturemempool = Mem_AllocPool("texture management", 0, NULL);
577 // Disable JPEG screenshots if the DLL isn't loaded
578 if (! JPEG_OpenLibrary ())
579 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
580 // TODO: support png screenshots?
584 static void r_textures_shutdown(void)
586 rtexturepool_t *temp;
588 JPEG_CloseLibrary ();
590 while(gltexturepoolchain)
592 temp = (rtexturepool_t *) gltexturepoolchain;
593 R_FreeTexturePool(&temp);
596 resizebuffersize = 0;
597 texturebuffersize = 0;
599 colorconvertbuffer = NULL;
600 texturebuffer = NULL;
601 Mem_FreePool(&texturemempool);
604 static void r_textures_newmap(void)
608 void R_Textures_Init (void)
610 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
611 Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
612 Cvar_RegisterVariable (&gl_max_size);
613 Cvar_RegisterVariable (&gl_picmip);
614 Cvar_RegisterVariable (&r_lerpimages);
615 Cvar_RegisterVariable (&r_precachetextures);
616 Cvar_RegisterVariable (&gl_texture_anisotropy);
617 Cvar_RegisterVariable (&gl_texturecompression);
618 Cvar_RegisterVariable (&gl_texturecompression_color);
619 Cvar_RegisterVariable (&gl_texturecompression_normal);
620 Cvar_RegisterVariable (&gl_texturecompression_gloss);
621 Cvar_RegisterVariable (&gl_texturecompression_glow);
622 Cvar_RegisterVariable (&gl_texturecompression_2d);
623 Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
624 Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
625 Cvar_RegisterVariable (&gl_texturecompression_sky);
626 Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
628 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
631 void R_Textures_Frame (void)
633 static int old_aniso = 0;
635 // could do procedural texture animation here, if we keep track of which
636 // textures were accessed this frame...
638 // free the resize buffers
639 resizebuffersize = 0;
642 Mem_Free(resizebuffer);
645 if (colorconvertbuffer)
647 Mem_Free(colorconvertbuffer);
648 colorconvertbuffer = NULL;
651 if (old_aniso != gl_texture_anisotropy.integer)
654 gltexturepool_t *pool;
657 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
659 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
662 for (pool = gltexturepoolchain;pool;pool = pool->next)
664 for (glt = pool->gltchain;glt;glt = glt->chain)
666 // only update already uploaded images
667 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
669 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
671 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
672 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
674 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
681 void R_MakeResizeBufferBigger(int size)
683 if (resizebuffersize < size)
685 resizebuffersize = size;
687 Mem_Free(resizebuffer);
688 if (colorconvertbuffer)
689 Mem_Free(colorconvertbuffer);
690 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
691 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
692 if (!resizebuffer || !colorconvertbuffer)
693 Host_Error("R_Upload: out of memory");
697 static void GL_SetupTextureParameters(int flags, int texturetype)
699 int textureenum = gltexturetypeenums[texturetype];
700 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
704 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
706 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
707 if (gl_texture_anisotropy.integer != aniso)
708 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
709 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
711 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
712 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
713 if (gltexturetypedimensions[texturetype] >= 3)
715 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
719 if (flags & TEXF_FORCENEAREST)
721 if (flags & TEXF_MIPMAP)
723 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
727 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
729 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
731 else if (flags & TEXF_FORCELINEAR)
733 if (flags & TEXF_MIPMAP)
735 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
737 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
741 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
746 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
748 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
752 if (flags & TEXF_MIPMAP)
754 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
758 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
760 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
766 static void R_Upload(gltexture_t *glt, const unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
768 int i, mip, width, height, depth;
770 const unsigned char *prevbuffer;
775 // we need to restore the texture binding after finishing the upload
776 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
777 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
779 // these are rounded up versions of the size to do better resampling
780 if (gl_support_arb_texture_non_power_of_two)
782 width = glt->inputwidth;
783 height = glt->inputheight;
784 depth = glt->inputdepth;
788 for (width = 1;width < glt->inputwidth ;width <<= 1);
789 for (height = 1;height < glt->inputheight;height <<= 1);
790 for (depth = 1;depth < glt->inputdepth ;depth <<= 1);
793 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
794 R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
796 if (prevbuffer == NULL)
798 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
799 prevbuffer = resizebuffer;
801 else if (glt->textype->textype == TEXTYPE_PALETTE)
803 // promote paletted to RGBA, so we only have to worry about RGB and
804 // RGBA in the rest of this code
805 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
806 prevbuffer = colorconvertbuffer;
809 if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
811 // update a portion of the image
812 switch(glt->texturetype)
814 case GLTEXTURETYPE_1D:
815 qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
817 case GLTEXTURETYPE_2D:
818 qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
820 case GLTEXTURETYPE_3D:
821 qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
824 Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
830 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
831 Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
833 // upload the image for the first time
834 glt->flags &= ~GLTEXF_UPLOAD;
836 // cubemaps contain multiple images and thus get processed a bit differently
837 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
839 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
841 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
842 prevbuffer = resizebuffer;
845 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
847 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
848 prevbuffer = resizebuffer;
852 if (gl_support_texture_compression)
854 if (gl_texturecompression.integer >= 2)
855 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
857 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
860 switch(glt->texturetype)
862 case GLTEXTURETYPE_1D:
863 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
864 if (glt->flags & TEXF_MIPMAP)
866 while (width > 1 || height > 1 || depth > 1)
868 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
869 prevbuffer = resizebuffer;
870 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
874 case GLTEXTURETYPE_2D:
875 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
876 if (glt->flags & TEXF_MIPMAP)
878 while (width > 1 || height > 1 || depth > 1)
880 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
881 prevbuffer = resizebuffer;
882 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
886 case GLTEXTURETYPE_3D:
887 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
888 if (glt->flags & TEXF_MIPMAP)
890 while (width > 1 || height > 1 || depth > 1)
892 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
893 prevbuffer = resizebuffer;
894 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
898 case GLTEXTURETYPE_CUBEMAP:
899 // convert and upload each side in turn,
900 // from a continuous block of input texels
901 texturebuffer = prevbuffer;
902 for (i = 0;i < 6;i++)
904 prevbuffer = texturebuffer;
905 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
906 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
908 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
909 prevbuffer = resizebuffer;
912 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
914 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
915 prevbuffer = resizebuffer;
918 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
919 if (glt->flags & TEXF_MIPMAP)
921 while (width > 1 || height > 1 || depth > 1)
923 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
924 prevbuffer = resizebuffer;
925 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
931 GL_SetupTextureParameters(glt->flags, glt->texturetype);
933 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
936 static void R_UploadTexture (gltexture_t *glt)
938 if (!(glt->flags & GLTEXF_UPLOAD))
942 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
943 R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
944 if (glt->inputtexels)
946 Mem_Free(glt->inputtexels);
947 glt->inputtexels = NULL;
948 glt->flags |= GLTEXF_DESTROYED;
950 else if (glt->flags & GLTEXF_DESTROYED)
951 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
954 static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, int sides, int flags, int textype, int texturetype, const unsigned char *data, const unsigned int *palette)
958 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
959 textypeinfo_t *texinfo;
961 if (cls.state == ca_dedicated)
964 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
966 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
969 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
971 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
975 texinfo = R_GetTexTypeInfo(textype, flags);
976 size = width * height * depth * sides * texinfo->inputbytesperpixel;
979 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
983 // clear the alpha flag if the texture has no transparent pixels
986 case TEXTYPE_PALETTE:
987 if (flags & TEXF_ALPHA)
989 flags &= ~TEXF_ALPHA;
992 for (i = 0;i < size;i++)
994 if (((unsigned char *)&palette[data[i]])[3] < 255)
1005 if (flags & TEXF_ALPHA)
1007 flags &= ~TEXF_ALPHA;
1010 for (i = 3;i < size;i += 4)
1014 flags |= TEXF_ALPHA;
1022 Host_Error("R_LoadTexture: unknown texture type");
1025 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1027 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1029 glt->chain = pool->gltchain;
1030 pool->gltchain = glt;
1031 glt->inputwidth = width;
1032 glt->inputheight = height;
1033 glt->inputdepth = depth;
1034 glt->flags = flags | GLTEXF_UPLOAD;
1035 glt->textype = texinfo;
1036 glt->texturetype = texturetype;
1037 glt->inputdatasize = size;
1038 glt->palette = palette;
1039 glt->glinternalformat = texinfo->glinternalformat;
1040 glt->glformat = texinfo->glformat;
1041 glt->bytesperpixel = texinfo->internalbytesperpixel;
1042 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1044 // init the dynamic texture attributes, too [11/22/2007 Black]
1045 glt->dirtytexnum = 0;
1046 glt->updatecallback = NULL;
1047 glt->updatacallback_data = NULL;
1051 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1052 memcpy(glt->inputtexels, data, size);
1055 glt->inputtexels = NULL;
1057 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1058 R_PrecacheTexture(glt);
1060 return (rtexture_t *)glt;
1063 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1065 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1068 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1070 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1073 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1075 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1078 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1080 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1083 int R_TextureHasAlpha(rtexture_t *rt)
1085 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1088 int R_TextureWidth(rtexture_t *rt)
1090 return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1093 int R_TextureHeight(rtexture_t *rt)
1095 return rt ? ((gltexture_t *)rt)->inputheight : 0;
1098 void R_UpdateTexture(rtexture_t *rt, const unsigned char *data, int x, int y, int width, int height)
1102 Host_Error("R_UpdateTexture: no texture supplied");
1104 Host_Error("R_UpdateTexture: no data supplied");
1105 glt = (gltexture_t *)rt;
1107 // we need it to be uploaded before we can update a part of it
1108 if (glt->flags & GLTEXF_UPLOAD)
1109 R_UploadTexture(glt);
1111 // update part of the texture
1112 R_Upload(glt, data, x, y, 0, width, height, 1);