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
37 typedef struct textypeinfo_s
40 int inputbytesperpixel;
41 int internalbytesperpixel;
42 int glinternalbytesperpixel;
48 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, 4, GL_RGBA , 3};
49 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, 4, GL_RGB , 3};
50 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, 4, GL_RGBA , 3};
51 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, 4, GL_RGBA , 4};
52 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, 4, GL_RGBA , 4};
53 static textypeinfo_t textype_palette_compress = {TEXTYPE_PALETTE, 1, 4, 1, GL_RGBA , GL_COMPRESSED_RGB_ARB};
54 static textypeinfo_t textype_rgb_compress = {TEXTYPE_RGB , 3, 3, 1, GL_RGB , GL_COMPRESSED_RGB_ARB};
55 static textypeinfo_t textype_rgba_compress = {TEXTYPE_RGBA , 4, 4, 1, GL_RGBA , GL_COMPRESSED_RGB_ARB};
56 static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1, GL_RGBA , GL_COMPRESSED_RGBA_ARB};
57 static textypeinfo_t textype_rgba_alpha_compress = {TEXTYPE_RGBA , 4, 4, 1, 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 // pointer to texturepool (check this to see if the texture is allocated)
84 struct gltexturepool_s *pool;
85 // pointer to next texture in texturepool chain
86 struct gltexture_s *chain;
87 // name of the texture (this might be removed someday), no duplicates
88 char identifier[MAX_QPATH + 32];
89 // original data size in *inputtexels
90 int inputwidth, inputheight, inputdepth;
91 // copy of the original texture(s) supplied to the upload function, for
92 // delayed uploads (non-precached)
93 unsigned char *inputtexels;
94 // original data size in *inputtexels
96 // flags supplied to the LoadTexture function
97 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
99 // pointer to one of the textype_ structs
100 textypeinfo_t *textype;
101 // one of the GLTEXTURETYPE_ values
103 // palette if the texture is TEXTYPE_PALETTE
104 const unsigned int *palette;
105 // actual stored texture size after gl_picmip and gl_max_size are applied
106 // (power of 2 if gl_support_arb_texture_non_power_of_two is not supported)
107 int tilewidth, tileheight, tiledepth;
108 // 1 or 6 depending on texturetype
115 int glinternalformat;
119 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
121 typedef struct gltexturepool_s
123 unsigned int sentinel;
124 struct gltexture_s *gltchain;
125 struct gltexturepool_s *next;
129 static gltexturepool_t *gltexturepoolchain = NULL;
131 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
132 static int resizebuffersize = 0;
133 static unsigned char *texturebuffer;
134 static int texturebuffersize = 0;
136 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
138 if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
140 if (flags & TEXF_ALPHA)
144 case TEXTYPE_PALETTE:
145 return &textype_palette_alpha_compress;
147 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
150 return &textype_rgba_alpha_compress;
152 Host_Error("R_GetTexTypeInfo: unknown texture format");
160 case TEXTYPE_PALETTE:
161 return &textype_palette_compress;
163 return &textype_rgb_compress;
165 return &textype_rgba_compress;
167 Host_Error("R_GetTexTypeInfo: unknown texture format");
174 if (flags & TEXF_ALPHA)
178 case TEXTYPE_PALETTE:
179 return &textype_palette_alpha;
181 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
184 return &textype_rgba_alpha;
186 Host_Error("R_GetTexTypeInfo: unknown texture format");
194 case TEXTYPE_PALETTE:
195 return &textype_palette;
199 return &textype_rgba;
201 Host_Error("R_GetTexTypeInfo: unknown texture format");
208 static void R_UploadTexture(gltexture_t *t);
210 static void R_PrecacheTexture(gltexture_t *glt)
214 if (glt->flags & TEXF_ALWAYSPRECACHE)
216 else if (r_precachetextures.integer >= 2)
218 else if (r_precachetextures.integer >= 1)
219 if (glt->flags & TEXF_PRECACHE)
223 R_UploadTexture(glt);
226 int R_RealGetTexture(rtexture_t *rt)
231 glt = (gltexture_t *)rt;
232 if (glt->flags & GLTEXF_UPLOAD)
233 R_UploadTexture(glt);
240 void R_FreeTexture(rtexture_t *rt)
242 gltexture_t *glt, **gltpointer;
244 glt = (gltexture_t *)rt;
246 Host_Error("R_FreeTexture: texture == NULL");
248 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
249 if (*gltpointer == glt)
250 *gltpointer = glt->chain;
252 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
257 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
260 if (glt->inputtexels)
261 Mem_Free(glt->inputtexels);
265 rtexturepool_t *R_AllocTexturePool(void)
267 gltexturepool_t *pool;
268 if (texturemempool == NULL)
270 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
273 pool->next = gltexturepoolchain;
274 gltexturepoolchain = pool;
275 pool->sentinel = TEXTUREPOOL_SENTINEL;
276 return (rtexturepool_t *)pool;
279 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
281 gltexturepool_t *pool, **poolpointer;
282 if (rtexturepool == NULL)
284 if (*rtexturepool == NULL)
286 pool = (gltexturepool_t *)(*rtexturepool);
287 *rtexturepool = NULL;
288 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
289 Host_Error("R_FreeTexturePool: pool already freed");
290 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
291 if (*poolpointer == pool)
292 *poolpointer = pool->next;
294 Host_Error("R_FreeTexturePool: pool not linked");
295 while (pool->gltchain)
296 R_FreeTexture((rtexture_t *)pool->gltchain);
301 typedef struct glmode_s
304 int minification, magnification;
308 static glmode_t modes[6] =
310 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
311 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
312 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
313 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
314 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
315 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
318 static void GL_TextureMode_f (void)
323 gltexturepool_t *pool;
327 for (i = 0;i < 6;i++)
329 if (gl_filter_min == modes[i].minification)
331 Con_Printf("%s\n", modes[i].name);
335 Con_Print("current filter is unknown???\n");
339 for (i = 0;i < 6;i++)
340 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
344 Con_Print("bad filter name\n");
348 gl_filter_min = modes[i].minification;
349 gl_filter_mag = modes[i].magnification;
351 // change all the existing mipmap texture objects
352 // FIXME: force renderer(/client/something?) restart instead?
354 for (pool = gltexturepoolchain;pool;pool = pool->next)
356 for (glt = pool->gltchain;glt;glt = glt->chain)
358 // only update already uploaded images
359 if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
361 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
362 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
363 if (glt->flags & TEXF_MIPMAP)
365 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
369 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
371 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
372 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
378 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
380 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
382 if (gl_max_size.integer > gl_max_texture_size)
383 Cvar_SetValue("gl_max_size", gl_max_texture_size);
388 case GLTEXTURETYPE_1D:
389 case GLTEXTURETYPE_2D:
390 maxsize = gl_max_texture_size;
392 case GLTEXTURETYPE_3D:
393 maxsize = gl_max_3d_texture_size;
395 case GLTEXTURETYPE_CUBEMAP:
396 maxsize = gl_max_cube_map_texture_size;
400 if (flags & TEXF_PICMIP)
402 maxsize = min(maxsize, gl_max_size.integer);
403 picmip = gl_picmip.integer;
408 if (gl_support_arb_texture_non_power_of_two)
409 width2 = min(inwidth >> picmip, maxsize);
412 for (width2 = 1;width2 < inwidth;width2 <<= 1);
413 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
415 *outwidth = max(1, width2);
419 if (gl_support_arb_texture_non_power_of_two)
420 height2 = min(inheight >> picmip, maxsize);
423 for (height2 = 1;height2 < inheight;height2 <<= 1);
424 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
426 *outheight = max(1, height2);
430 if (gl_support_arb_texture_non_power_of_two)
431 depth2 = min(indepth >> picmip, maxsize);
434 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
435 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
437 *outdepth = max(1, depth2);
442 static int R_CalcTexelDataSize (gltexture_t *glt)
444 int width2, height2, depth2, size;
446 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
448 size = width2 * height2 * depth2;
450 if (glt->flags & TEXF_MIPMAP)
452 while (width2 > 1 || height2 > 1 || depth2 > 1)
460 size += width2 * height2 * depth2;
464 return size * glt->textype->glinternalbytesperpixel * glt->sides;
467 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
471 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
472 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
474 gltexturepool_t *pool;
476 Con_Print("glsize input loaded mip alpha name\n");
477 for (pool = gltexturepoolchain;pool;pool = pool->next)
485 for (glt = pool->gltchain;glt;glt = glt->chain)
487 glsize = R_CalcTexelDataSize(glt);
488 isloaded = !(glt->flags & GLTEXF_UPLOAD);
490 pooltotalt += glsize;
491 pooltotalp += glt->inputdatasize;
495 poolloadedt += glsize;
496 poolloadedp += glt->inputdatasize;
499 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);
502 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);
503 sumtotal += pooltotal;
504 sumtotalt += pooltotalt;
505 sumtotalp += pooltotalp;
506 sumloaded += poolloaded;
507 sumloadedt += poolloadedt;
508 sumloadedp += poolloadedp;
511 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);
514 static void R_TextureStats_f(void)
516 R_TextureStats_Print(true, true, true);
519 static void r_textures_start(void)
521 // LordHavoc: allow any alignment
523 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
524 qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
526 texturemempool = Mem_AllocPool("texture management", 0, NULL);
528 // Disable JPEG screenshots if the DLL isn't loaded
529 if (! JPEG_OpenLibrary ())
530 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
531 // TODO: support png screenshots?
535 static void r_textures_shutdown(void)
537 rtexturepool_t *temp;
539 JPEG_CloseLibrary ();
541 while(gltexturepoolchain)
543 temp = (rtexturepool_t *) gltexturepoolchain;
544 R_FreeTexturePool(&temp);
547 resizebuffersize = 0;
548 texturebuffersize = 0;
550 colorconvertbuffer = NULL;
551 texturebuffer = NULL;
552 Mem_FreePool(&texturemempool);
555 static void r_textures_newmap(void)
559 void R_Textures_Init (void)
561 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
562 Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
563 Cvar_RegisterVariable (&gl_max_size);
564 Cvar_RegisterVariable (&gl_picmip);
565 Cvar_RegisterVariable (&r_lerpimages);
566 Cvar_RegisterVariable (&r_precachetextures);
567 Cvar_RegisterVariable (&gl_texture_anisotropy);
568 Cvar_RegisterVariable (&gl_texturecompression);
569 Cvar_RegisterVariable (&gl_texturecompression_color);
570 Cvar_RegisterVariable (&gl_texturecompression_normal);
571 Cvar_RegisterVariable (&gl_texturecompression_gloss);
572 Cvar_RegisterVariable (&gl_texturecompression_glow);
573 Cvar_RegisterVariable (&gl_texturecompression_2d);
574 Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
575 Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
576 Cvar_RegisterVariable (&gl_texturecompression_sky);
577 Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
579 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
582 void R_Textures_Frame (void)
584 static int old_aniso = 0;
586 // could do procedural texture animation here, if we keep track of which
587 // textures were accessed this frame...
589 // free the resize buffers
590 resizebuffersize = 0;
593 Mem_Free(resizebuffer);
596 if (colorconvertbuffer)
598 Mem_Free(colorconvertbuffer);
599 colorconvertbuffer = NULL;
602 if (old_aniso != gl_texture_anisotropy.integer)
605 gltexturepool_t *pool;
608 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
610 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
613 for (pool = gltexturepoolchain;pool;pool = pool->next)
615 for (glt = pool->gltchain;glt;glt = glt->chain)
617 // only update already uploaded images
618 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
620 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
622 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
623 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
625 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
632 void R_MakeResizeBufferBigger(int size)
634 if (resizebuffersize < size)
636 resizebuffersize = size;
638 Mem_Free(resizebuffer);
639 if (colorconvertbuffer)
640 Mem_Free(colorconvertbuffer);
641 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
642 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
643 if (!resizebuffer || !colorconvertbuffer)
644 Host_Error("R_Upload: out of memory");
648 static void GL_SetupTextureParameters(int flags, int texturetype)
650 int textureenum = gltexturetypeenums[texturetype];
651 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
655 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
657 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
658 if (gl_texture_anisotropy.integer != aniso)
659 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
660 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
662 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
663 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
664 if (gltexturetypedimensions[texturetype] >= 3)
666 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
670 if (flags & TEXF_FORCENEAREST)
672 if (flags & TEXF_MIPMAP)
674 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
678 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
680 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
682 else if (flags & TEXF_FORCELINEAR)
684 if (flags & TEXF_MIPMAP)
686 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
688 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
692 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
697 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
699 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
703 if (flags & TEXF_MIPMAP)
705 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
709 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
711 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
717 static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
719 int i, mip, width, height, depth;
721 unsigned char *prevbuffer;
726 // we need to restore the texture binding after finishing the upload
727 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
728 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
730 // these are rounded up versions of the size to do better resampling
731 if (gl_support_arb_texture_non_power_of_two)
733 width = glt->inputwidth;
734 height = glt->inputheight;
735 depth = glt->inputdepth;
739 for (width = 1;width < glt->inputwidth ;width <<= 1);
740 for (height = 1;height < glt->inputheight;height <<= 1);
741 for (depth = 1;depth < glt->inputdepth ;depth <<= 1);
744 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
745 R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
747 if (prevbuffer == NULL)
749 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
750 prevbuffer = resizebuffer;
752 else if (glt->textype->textype == TEXTYPE_PALETTE)
754 // promote paletted to RGBA, so we only have to worry about RGB and
755 // RGBA in the rest of this code
756 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
757 prevbuffer = colorconvertbuffer;
760 if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
762 // update a portion of the image
763 switch(glt->texturetype)
765 case GLTEXTURETYPE_1D:
766 qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
768 case GLTEXTURETYPE_2D:
769 qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
771 case GLTEXTURETYPE_3D:
772 qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
775 Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
781 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
782 Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
784 // upload the image for the first time
785 glt->flags &= ~GLTEXF_UPLOAD;
787 // cubemaps contain multiple images and thus get processed a bit differently
788 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
790 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
792 Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
793 prevbuffer = resizebuffer;
796 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
798 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
799 prevbuffer = resizebuffer;
803 if (gl_support_texture_compression)
805 if (gl_texturecompression.integer >= 2)
806 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
808 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
811 switch(glt->texturetype)
813 case GLTEXTURETYPE_1D:
814 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
815 if (glt->flags & TEXF_MIPMAP)
817 while (width > 1 || height > 1 || depth > 1)
819 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
820 prevbuffer = resizebuffer;
821 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
825 case GLTEXTURETYPE_2D:
826 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
827 if (glt->flags & TEXF_MIPMAP)
829 while (width > 1 || height > 1 || depth > 1)
831 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
832 prevbuffer = resizebuffer;
833 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
837 case GLTEXTURETYPE_3D:
838 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
839 if (glt->flags & TEXF_MIPMAP)
841 while (width > 1 || height > 1 || depth > 1)
843 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
844 prevbuffer = resizebuffer;
845 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
849 case GLTEXTURETYPE_CUBEMAP:
850 // convert and upload each side in turn,
851 // from a continuous block of input texels
852 texturebuffer = prevbuffer;
853 for (i = 0;i < 6;i++)
855 prevbuffer = texturebuffer;
856 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
857 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
859 Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
860 prevbuffer = resizebuffer;
863 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
865 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
866 prevbuffer = resizebuffer;
869 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
870 if (glt->flags & TEXF_MIPMAP)
872 while (width > 1 || height > 1 || depth > 1)
874 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
875 prevbuffer = resizebuffer;
876 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
882 GL_SetupTextureParameters(glt->flags, glt->texturetype);
884 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
887 static void R_UploadTexture (gltexture_t *glt)
889 if (!(glt->flags & GLTEXF_UPLOAD))
893 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
894 R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
895 if (glt->inputtexels)
897 Mem_Free(glt->inputtexels);
898 glt->inputtexels = NULL;
899 glt->flags |= GLTEXF_DESTROYED;
901 else if (glt->flags & GLTEXF_DESTROYED)
902 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
905 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)
909 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
910 textypeinfo_t *texinfo;
912 if (cls.state == ca_dedicated)
915 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
917 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
920 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
922 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
926 texinfo = R_GetTexTypeInfo(textype, flags);
927 size = width * height * depth * sides * texinfo->inputbytesperpixel;
930 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
934 // clear the alpha flag if the texture has no transparent pixels
937 case TEXTYPE_PALETTE:
938 if (flags & TEXF_ALPHA)
940 flags &= ~TEXF_ALPHA;
943 for (i = 0;i < size;i++)
945 if (((unsigned char *)&palette[data[i]])[3] < 255)
955 if (flags & TEXF_ALPHA)
956 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
959 if (flags & TEXF_ALPHA)
961 flags &= ~TEXF_ALPHA;
964 for (i = 3;i < size;i += 4)
976 Host_Error("R_LoadTexture: unknown texture type");
979 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
981 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
983 glt->chain = pool->gltchain;
984 pool->gltchain = glt;
985 glt->inputwidth = width;
986 glt->inputheight = height;
987 glt->inputdepth = depth;
988 glt->flags = flags | GLTEXF_UPLOAD;
989 glt->textype = texinfo;
990 glt->texturetype = texturetype;
991 glt->inputdatasize = size;
992 glt->palette = palette;
993 glt->glinternalformat = texinfo->glinternalformat;
994 glt->glformat = texinfo->glformat;
995 glt->bytesperpixel = texinfo->internalbytesperpixel;
996 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1001 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1002 memcpy(glt->inputtexels, data, size);
1005 glt->inputtexels = NULL;
1007 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1008 R_PrecacheTexture(glt);
1010 return (rtexture_t *)glt;
1013 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1015 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1018 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)
1020 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1023 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)
1025 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1028 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1030 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1033 int R_TextureHasAlpha(rtexture_t *rt)
1035 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1038 int R_TextureWidth(rtexture_t *rt)
1040 return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1043 int R_TextureHeight(rtexture_t *rt)
1045 return rt ? ((gltexture_t *)rt)->inputheight : 0;
1048 void R_UpdateTexture(rtexture_t *rt, unsigned char *data, int x, int y, int width, int height)
1052 Host_Error("R_UpdateTexture: no texture supplied");
1054 Host_Error("R_UpdateTexture: no data supplied");
1055 glt = (gltexture_t *)rt;
1057 // we need it to be uploaded before we can update a part of it
1058 if (glt->flags & GLTEXF_UPLOAD)
1059 R_UploadTexture(glt);
1061 // update part of the texture
1062 R_Upload(glt, data, x, y, 0, width, height, 1);