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_palette_alpha = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA , 4};
53 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , 4};
54 static textypeinfo_t textype_palette_compress = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_RGBA , GL_COMPRESSED_RGB_ARB};
55 static textypeinfo_t textype_rgba_compress = {TEXTYPE_RGBA , 4, 4, 0.5f, GL_RGBA , GL_COMPRESSED_RGB_ARB};
56 static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_RGBA , GL_COMPRESSED_RGBA_ARB};
57 static textypeinfo_t textype_rgba_alpha_compress = {TEXTYPE_RGBA , 4, 4, 1.0f, GL_RGBA , GL_COMPRESSED_RGBA_ARB};
59 #define GLTEXTURETYPE_1D 0
60 #define GLTEXTURETYPE_2D 1
61 #define GLTEXTURETYPE_3D 2
62 #define GLTEXTURETYPE_CUBEMAP 3
64 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
65 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
66 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
67 static int cubemapside[6] =
69 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
70 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
71 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
72 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
73 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
74 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
77 typedef struct gltexture_s
79 // this field is exposed to the R_GetTexture macro, for speed reasons
80 // (must be identical in rtexture_t)
81 int texnum; // GL texture slot number
83 // dynamic texture stuff [11/22/2007 Black]
84 // used to hold the texture number of dirty textures
86 updatecallback_t updatecallback;
87 void *updatacallback_data;
88 // --- [11/22/2007 Black]
90 // pointer to texturepool (check this to see if the texture is allocated)
91 struct gltexturepool_s *pool;
92 // pointer to next texture in texturepool chain
93 struct gltexture_s *chain;
94 // name of the texture (this might be removed someday), no duplicates
95 char identifier[MAX_QPATH + 32];
96 // original data size in *inputtexels
97 int inputwidth, inputheight, inputdepth;
98 // copy of the original texture(s) supplied to the upload function, for
99 // delayed uploads (non-precached)
100 unsigned char *inputtexels;
101 // original data size in *inputtexels
103 // flags supplied to the LoadTexture function
104 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
106 // pointer to one of the textype_ structs
107 textypeinfo_t *textype;
108 // one of the GLTEXTURETYPE_ values
110 // palette if the texture is TEXTYPE_PALETTE
111 const unsigned int *palette;
112 // actual stored texture size after gl_picmip and gl_max_size are applied
113 // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
114 int tilewidth, tileheight, tiledepth;
115 // 1 or 6 depending on texturetype
122 int glinternalformat;
126 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
128 typedef struct gltexturepool_s
130 unsigned int sentinel;
131 struct gltexture_s *gltchain;
132 struct gltexturepool_s *next;
136 static gltexturepool_t *gltexturepoolchain = NULL;
138 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
139 static int resizebuffersize = 0;
140 static unsigned char *texturebuffer;
141 static int texturebuffersize = 0;
143 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
145 if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
147 if (flags & TEXF_ALPHA)
151 case TEXTYPE_PALETTE:
152 return &textype_palette_alpha_compress;
154 return &textype_rgba_alpha_compress;
156 Host_Error("R_GetTexTypeInfo: unknown texture format");
164 case TEXTYPE_PALETTE:
165 return &textype_palette_compress;
167 return &textype_rgba_compress;
169 Host_Error("R_GetTexTypeInfo: unknown texture format");
176 if (flags & TEXF_ALPHA)
180 case TEXTYPE_PALETTE:
181 return &textype_palette_alpha;
183 return &textype_rgba_alpha;
185 Host_Error("R_GetTexTypeInfo: unknown texture format");
193 case TEXTYPE_PALETTE:
194 return &textype_palette;
196 return &textype_rgba;
198 Host_Error("R_GetTexTypeInfo: unknown texture format");
203 return NULL; // this line only to hush compiler warnings
206 // dynamic texture code [11/22/2007 Black]
207 void R_MarkDirtyTexture(rtexture_t *rt) {
208 gltexture_t *glt = (gltexture_t*) rt;
209 // dont do anything if the texture is already dirty (and make sure this *is* a dynamic texture after all!)
210 if( !glt->dirtytexnum && glt->flags & GLTEXF_DYNAMIC ) {
211 glt->dirtytexnum = glt->texnum;
212 // mark it as dirty, so R_RealGetTexture gets called
217 void R_MakeTextureDynamic(rtexture_t *rt, updatecallback_t updatecallback, void *data) {
218 gltexture_t *glt = (gltexture_t*) rt;
219 glt->flags |= GLTEXF_DYNAMIC;
220 glt->updatecallback = updatecallback;
221 glt->updatacallback_data = data;
224 static void R_UpdateDynamicTexture(gltexture_t *glt) {
225 glt->texnum = glt->dirtytexnum;
226 // reset dirtytexnum again (not dirty anymore)
227 glt->dirtytexnum = 0;
228 // TODO: now assert that t->texnum != 0 ?
229 if( glt->updatecallback ) {
230 glt->updatecallback( (rtexture_t*) glt, glt->updatacallback_data );
234 static void R_UploadTexture(gltexture_t *t);
236 static void R_PrecacheTexture(gltexture_t *glt)
240 if (glt->flags & TEXF_ALWAYSPRECACHE)
242 else if (r_precachetextures.integer >= 2)
244 else if (r_precachetextures.integer >= 1)
245 if (glt->flags & TEXF_PRECACHE)
249 R_UploadTexture(glt);
252 int R_RealGetTexture(rtexture_t *rt)
257 glt = (gltexture_t *)rt;
258 if (glt->flags & GLTEXF_UPLOAD)
259 R_UploadTexture(glt);
260 if (glt->flags & GLTEXF_DYNAMIC)
261 R_UpdateDynamicTexture(glt);
269 void R_FreeTexture(rtexture_t *rt)
271 gltexture_t *glt, **gltpointer;
273 glt = (gltexture_t *)rt;
275 Host_Error("R_FreeTexture: texture == NULL");
277 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
278 if (*gltpointer == glt)
279 *gltpointer = glt->chain;
281 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
286 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
289 if (glt->inputtexels)
290 Mem_Free(glt->inputtexels);
294 rtexturepool_t *R_AllocTexturePool(void)
296 gltexturepool_t *pool;
297 if (texturemempool == NULL)
299 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
302 pool->next = gltexturepoolchain;
303 gltexturepoolchain = pool;
304 pool->sentinel = TEXTUREPOOL_SENTINEL;
305 return (rtexturepool_t *)pool;
308 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
310 gltexturepool_t *pool, **poolpointer;
311 if (rtexturepool == NULL)
313 if (*rtexturepool == NULL)
315 pool = (gltexturepool_t *)(*rtexturepool);
316 *rtexturepool = NULL;
317 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
318 Host_Error("R_FreeTexturePool: pool already freed");
319 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
320 if (*poolpointer == pool)
321 *poolpointer = pool->next;
323 Host_Error("R_FreeTexturePool: pool not linked");
324 while (pool->gltchain)
325 R_FreeTexture((rtexture_t *)pool->gltchain);
330 typedef struct glmode_s
333 int minification, magnification;
337 static glmode_t modes[6] =
339 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
340 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
341 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
342 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
343 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
344 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
347 static void GL_TextureMode_f (void)
352 gltexturepool_t *pool;
356 for (i = 0;i < 6;i++)
358 if (gl_filter_min == modes[i].minification)
360 Con_Printf("%s\n", modes[i].name);
364 Con_Print("current filter is unknown???\n");
368 for (i = 0;i < 6;i++)
369 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
373 Con_Print("bad filter name\n");
377 gl_filter_min = modes[i].minification;
378 gl_filter_mag = modes[i].magnification;
380 // change all the existing mipmap texture objects
381 // FIXME: force renderer(/client/something?) restart instead?
383 for (pool = gltexturepoolchain;pool;pool = pool->next)
385 for (glt = pool->gltchain;glt;glt = glt->chain)
387 // only update already uploaded images
388 if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
390 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
391 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
392 if (glt->flags & TEXF_MIPMAP)
394 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
398 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
400 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
401 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
407 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
409 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
411 if (gl_max_size.integer > gl_max_texture_size)
412 Cvar_SetValue("gl_max_size", gl_max_texture_size);
417 case GLTEXTURETYPE_1D:
418 case GLTEXTURETYPE_2D:
419 maxsize = gl_max_texture_size;
421 case GLTEXTURETYPE_3D:
422 maxsize = gl_max_3d_texture_size;
424 case GLTEXTURETYPE_CUBEMAP:
425 maxsize = gl_max_cube_map_texture_size;
429 if (flags & TEXF_PICMIP)
431 maxsize = min(maxsize, gl_max_size.integer);
432 picmip = gl_picmip.integer;
437 if (gl_support_arb_texture_non_power_of_two)
438 width2 = min(inwidth >> picmip, maxsize);
441 for (width2 = 1;width2 < inwidth;width2 <<= 1);
442 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
444 *outwidth = max(1, width2);
448 if (gl_support_arb_texture_non_power_of_two)
449 height2 = min(inheight >> picmip, maxsize);
452 for (height2 = 1;height2 < inheight;height2 <<= 1);
453 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
455 *outheight = max(1, height2);
459 if (gl_support_arb_texture_non_power_of_two)
460 depth2 = min(indepth >> picmip, maxsize);
463 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
464 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
466 *outdepth = max(1, depth2);
471 static int R_CalcTexelDataSize (gltexture_t *glt)
473 int width2, height2, depth2, size;
475 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
477 size = width2 * height2 * depth2;
479 if (glt->flags & TEXF_MIPMAP)
481 while (width2 > 1 || height2 > 1 || depth2 > 1)
489 size += width2 * height2 * depth2;
493 return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
496 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
500 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
501 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
503 gltexturepool_t *pool;
505 Con_Print("glsize input loaded mip alpha name\n");
506 for (pool = gltexturepoolchain;pool;pool = pool->next)
514 for (glt = pool->gltchain;glt;glt = glt->chain)
516 glsize = R_CalcTexelDataSize(glt);
517 isloaded = !(glt->flags & GLTEXF_UPLOAD);
519 pooltotalt += glsize;
520 pooltotalp += glt->inputdatasize;
524 poolloadedt += glsize;
525 poolloadedp += glt->inputdatasize;
528 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);
531 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);
532 sumtotal += pooltotal;
533 sumtotalt += pooltotalt;
534 sumtotalp += pooltotalp;
535 sumloaded += poolloaded;
536 sumloadedt += poolloadedt;
537 sumloadedp += poolloadedp;
540 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);
543 static void R_TextureStats_f(void)
545 R_TextureStats_Print(true, true, true);
548 static void r_textures_start(void)
550 // LordHavoc: allow any alignment
552 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
553 qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
555 texturemempool = Mem_AllocPool("texture management", 0, NULL);
557 // Disable JPEG screenshots if the DLL isn't loaded
558 if (! JPEG_OpenLibrary ())
559 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
560 // TODO: support png screenshots?
564 static void r_textures_shutdown(void)
566 rtexturepool_t *temp;
568 JPEG_CloseLibrary ();
570 while(gltexturepoolchain)
572 temp = (rtexturepool_t *) gltexturepoolchain;
573 R_FreeTexturePool(&temp);
576 resizebuffersize = 0;
577 texturebuffersize = 0;
579 colorconvertbuffer = NULL;
580 texturebuffer = NULL;
581 Mem_FreePool(&texturemempool);
584 static void r_textures_newmap(void)
588 void R_Textures_Init (void)
590 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
591 Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
592 Cvar_RegisterVariable (&gl_max_size);
593 Cvar_RegisterVariable (&gl_picmip);
594 Cvar_RegisterVariable (&r_lerpimages);
595 Cvar_RegisterVariable (&r_precachetextures);
596 Cvar_RegisterVariable (&gl_texture_anisotropy);
597 Cvar_RegisterVariable (&gl_texturecompression);
598 Cvar_RegisterVariable (&gl_texturecompression_color);
599 Cvar_RegisterVariable (&gl_texturecompression_normal);
600 Cvar_RegisterVariable (&gl_texturecompression_gloss);
601 Cvar_RegisterVariable (&gl_texturecompression_glow);
602 Cvar_RegisterVariable (&gl_texturecompression_2d);
603 Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
604 Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
605 Cvar_RegisterVariable (&gl_texturecompression_sky);
606 Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
608 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
611 void R_Textures_Frame (void)
613 static int old_aniso = 0;
615 // could do procedural texture animation here, if we keep track of which
616 // textures were accessed this frame...
618 // free the resize buffers
619 resizebuffersize = 0;
622 Mem_Free(resizebuffer);
625 if (colorconvertbuffer)
627 Mem_Free(colorconvertbuffer);
628 colorconvertbuffer = NULL;
631 if (old_aniso != gl_texture_anisotropy.integer)
634 gltexturepool_t *pool;
637 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
639 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
642 for (pool = gltexturepoolchain;pool;pool = pool->next)
644 for (glt = pool->gltchain;glt;glt = glt->chain)
646 // only update already uploaded images
647 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
649 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
651 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
652 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
654 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
661 void R_MakeResizeBufferBigger(int size)
663 if (resizebuffersize < size)
665 resizebuffersize = size;
667 Mem_Free(resizebuffer);
668 if (colorconvertbuffer)
669 Mem_Free(colorconvertbuffer);
670 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
671 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
672 if (!resizebuffer || !colorconvertbuffer)
673 Host_Error("R_Upload: out of memory");
677 static void GL_SetupTextureParameters(int flags, int texturetype)
679 int textureenum = gltexturetypeenums[texturetype];
680 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
684 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
686 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
687 if (gl_texture_anisotropy.integer != aniso)
688 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
689 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
691 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
692 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
693 if (gltexturetypedimensions[texturetype] >= 3)
695 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
699 if (flags & TEXF_FORCENEAREST)
701 if (flags & TEXF_MIPMAP)
703 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
707 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
709 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
711 else if (flags & TEXF_FORCELINEAR)
713 if (flags & TEXF_MIPMAP)
715 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
717 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
721 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
726 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
728 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
732 if (flags & TEXF_MIPMAP)
734 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
738 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
740 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
746 static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
748 int i, mip, width, height, depth;
750 unsigned char *prevbuffer;
755 // we need to restore the texture binding after finishing the upload
756 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
757 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
759 // these are rounded up versions of the size to do better resampling
760 if (gl_support_arb_texture_non_power_of_two)
762 width = glt->inputwidth;
763 height = glt->inputheight;
764 depth = glt->inputdepth;
768 for (width = 1;width < glt->inputwidth ;width <<= 1);
769 for (height = 1;height < glt->inputheight;height <<= 1);
770 for (depth = 1;depth < glt->inputdepth ;depth <<= 1);
773 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
774 R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
776 if (prevbuffer == NULL)
778 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
779 prevbuffer = resizebuffer;
781 else if (glt->textype->textype == TEXTYPE_PALETTE)
783 // promote paletted to RGBA, so we only have to worry about RGB and
784 // RGBA in the rest of this code
785 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
786 prevbuffer = colorconvertbuffer;
789 if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
791 // update a portion of the image
792 switch(glt->texturetype)
794 case GLTEXTURETYPE_1D:
795 qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
797 case GLTEXTURETYPE_2D:
798 qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
800 case GLTEXTURETYPE_3D:
801 qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
804 Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
810 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
811 Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
813 // upload the image for the first time
814 glt->flags &= ~GLTEXF_UPLOAD;
816 // cubemaps contain multiple images and thus get processed a bit differently
817 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
819 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
821 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
822 prevbuffer = resizebuffer;
825 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
827 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
828 prevbuffer = resizebuffer;
832 if (gl_support_texture_compression)
834 if (gl_texturecompression.integer >= 2)
835 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
837 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
840 switch(glt->texturetype)
842 case GLTEXTURETYPE_1D:
843 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
844 if (glt->flags & TEXF_MIPMAP)
846 while (width > 1 || height > 1 || depth > 1)
848 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
849 prevbuffer = resizebuffer;
850 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
854 case GLTEXTURETYPE_2D:
855 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
856 if (glt->flags & TEXF_MIPMAP)
858 while (width > 1 || height > 1 || depth > 1)
860 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
861 prevbuffer = resizebuffer;
862 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
866 case GLTEXTURETYPE_3D:
867 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
868 if (glt->flags & TEXF_MIPMAP)
870 while (width > 1 || height > 1 || depth > 1)
872 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
873 prevbuffer = resizebuffer;
874 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
878 case GLTEXTURETYPE_CUBEMAP:
879 // convert and upload each side in turn,
880 // from a continuous block of input texels
881 texturebuffer = prevbuffer;
882 for (i = 0;i < 6;i++)
884 prevbuffer = texturebuffer;
885 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
886 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
888 Image_Resample32(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, r_lerpimages.integer);
889 prevbuffer = resizebuffer;
892 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
894 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth);
895 prevbuffer = resizebuffer;
898 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
899 if (glt->flags & TEXF_MIPMAP)
901 while (width > 1 || height > 1 || depth > 1)
903 Image_MipReduce32(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1);
904 prevbuffer = resizebuffer;
905 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
911 GL_SetupTextureParameters(glt->flags, glt->texturetype);
913 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
916 static void R_UploadTexture (gltexture_t *glt)
918 if (!(glt->flags & GLTEXF_UPLOAD))
922 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
923 R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
924 if (glt->inputtexels)
926 Mem_Free(glt->inputtexels);
927 glt->inputtexels = NULL;
928 glt->flags |= GLTEXF_DESTROYED;
930 else if (glt->flags & GLTEXF_DESTROYED)
931 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
934 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)
938 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
939 textypeinfo_t *texinfo;
941 if (cls.state == ca_dedicated)
944 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
946 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
949 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
951 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
955 texinfo = R_GetTexTypeInfo(textype, flags);
956 size = width * height * depth * sides * texinfo->inputbytesperpixel;
959 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
963 // clear the alpha flag if the texture has no transparent pixels
966 case TEXTYPE_PALETTE:
967 if (flags & TEXF_ALPHA)
969 flags &= ~TEXF_ALPHA;
972 for (i = 0;i < size;i++)
974 if (((unsigned char *)&palette[data[i]])[3] < 255)
984 if (flags & TEXF_ALPHA)
986 flags &= ~TEXF_ALPHA;
989 for (i = 3;i < size;i += 4)
1001 Host_Error("R_LoadTexture: unknown texture type");
1004 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1006 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1008 glt->chain = pool->gltchain;
1009 pool->gltchain = glt;
1010 glt->inputwidth = width;
1011 glt->inputheight = height;
1012 glt->inputdepth = depth;
1013 glt->flags = flags | GLTEXF_UPLOAD;
1014 glt->textype = texinfo;
1015 glt->texturetype = texturetype;
1016 glt->inputdatasize = size;
1017 glt->palette = palette;
1018 glt->glinternalformat = texinfo->glinternalformat;
1019 glt->glformat = texinfo->glformat;
1020 glt->bytesperpixel = texinfo->internalbytesperpixel;
1021 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1023 // init the dynamic texture attributes, too [11/22/2007 Black]
1024 glt->dirtytexnum = 0;
1025 glt->updatecallback = NULL;
1026 glt->updatacallback_data = NULL;
1030 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1031 memcpy(glt->inputtexels, data, size);
1034 glt->inputtexels = NULL;
1036 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1037 R_PrecacheTexture(glt);
1039 return (rtexture_t *)glt;
1042 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1044 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1047 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)
1049 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1052 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)
1054 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1057 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1059 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1062 int R_TextureHasAlpha(rtexture_t *rt)
1064 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1067 int R_TextureWidth(rtexture_t *rt)
1069 return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1072 int R_TextureHeight(rtexture_t *rt)
1074 return rt ? ((gltexture_t *)rt)->inputheight : 0;
1077 void R_UpdateTexture(rtexture_t *rt, unsigned char *data, int x, int y, int width, int height)
1081 Host_Error("R_UpdateTexture: no texture supplied");
1083 Host_Error("R_UpdateTexture: no data supplied");
1084 glt = (gltexture_t *)rt;
1086 // we need it to be uploaded before we can update a part of it
1087 if (glt->flags & GLTEXF_UPLOAD)
1088 R_UploadTexture(glt);
1090 // update part of the texture
1091 R_Upload(glt, data, x, y, 0, width, height, 1);