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 float glinternalbytesperpixel;
48 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA , 3};
49 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, 4.0f, GL_RGB , 3};
50 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , 3};
51 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA , 4};
52 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, 4.0f, GL_RGBA , 4};
53 static textypeinfo_t textype_palette_compress = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_RGBA , GL_COMPRESSED_RGB_ARB};
54 static textypeinfo_t textype_rgb_compress = {TEXTYPE_RGB , 3, 3, 0.5f, GL_RGB , 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 // 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");
206 return NULL; // this line only to hush compiler warnings
209 static void R_UploadTexture(gltexture_t *t);
211 static void R_PrecacheTexture(gltexture_t *glt)
215 if (glt->flags & TEXF_ALWAYSPRECACHE)
217 else if (r_precachetextures.integer >= 2)
219 else if (r_precachetextures.integer >= 1)
220 if (glt->flags & TEXF_PRECACHE)
224 R_UploadTexture(glt);
227 int R_RealGetTexture(rtexture_t *rt)
232 glt = (gltexture_t *)rt;
233 if (glt->flags & GLTEXF_UPLOAD)
234 R_UploadTexture(glt);
241 void R_FreeTexture(rtexture_t *rt)
243 gltexture_t *glt, **gltpointer;
245 glt = (gltexture_t *)rt;
247 Host_Error("R_FreeTexture: texture == NULL");
249 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
250 if (*gltpointer == glt)
251 *gltpointer = glt->chain;
253 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
258 qglDeleteTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
261 if (glt->inputtexels)
262 Mem_Free(glt->inputtexels);
266 rtexturepool_t *R_AllocTexturePool(void)
268 gltexturepool_t *pool;
269 if (texturemempool == NULL)
271 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
274 pool->next = gltexturepoolchain;
275 gltexturepoolchain = pool;
276 pool->sentinel = TEXTUREPOOL_SENTINEL;
277 return (rtexturepool_t *)pool;
280 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
282 gltexturepool_t *pool, **poolpointer;
283 if (rtexturepool == NULL)
285 if (*rtexturepool == NULL)
287 pool = (gltexturepool_t *)(*rtexturepool);
288 *rtexturepool = NULL;
289 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
290 Host_Error("R_FreeTexturePool: pool already freed");
291 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
292 if (*poolpointer == pool)
293 *poolpointer = pool->next;
295 Host_Error("R_FreeTexturePool: pool not linked");
296 while (pool->gltchain)
297 R_FreeTexture((rtexture_t *)pool->gltchain);
302 typedef struct glmode_s
305 int minification, magnification;
309 static glmode_t modes[6] =
311 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
312 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
313 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
314 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
315 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
316 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
319 static void GL_TextureMode_f (void)
324 gltexturepool_t *pool;
328 for (i = 0;i < 6;i++)
330 if (gl_filter_min == modes[i].minification)
332 Con_Printf("%s\n", modes[i].name);
336 Con_Print("current filter is unknown???\n");
340 for (i = 0;i < 6;i++)
341 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
345 Con_Print("bad filter name\n");
349 gl_filter_min = modes[i].minification;
350 gl_filter_mag = modes[i].magnification;
352 // change all the existing mipmap texture objects
353 // FIXME: force renderer(/client/something?) restart instead?
355 for (pool = gltexturepoolchain;pool;pool = pool->next)
357 for (glt = pool->gltchain;glt;glt = glt->chain)
359 // only update already uploaded images
360 if (!(glt->flags & (GLTEXF_UPLOAD | TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
362 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
363 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
364 if (glt->flags & TEXF_MIPMAP)
366 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
370 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
372 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
373 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
379 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
381 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
383 if (gl_max_size.integer > gl_max_texture_size)
384 Cvar_SetValue("gl_max_size", gl_max_texture_size);
389 case GLTEXTURETYPE_1D:
390 case GLTEXTURETYPE_2D:
391 maxsize = gl_max_texture_size;
393 case GLTEXTURETYPE_3D:
394 maxsize = gl_max_3d_texture_size;
396 case GLTEXTURETYPE_CUBEMAP:
397 maxsize = gl_max_cube_map_texture_size;
401 if (flags & TEXF_PICMIP)
403 maxsize = min(maxsize, gl_max_size.integer);
404 picmip = gl_picmip.integer;
409 if (gl_support_arb_texture_non_power_of_two)
410 width2 = min(inwidth >> picmip, maxsize);
413 for (width2 = 1;width2 < inwidth;width2 <<= 1);
414 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
416 *outwidth = max(1, width2);
420 if (gl_support_arb_texture_non_power_of_two)
421 height2 = min(inheight >> picmip, maxsize);
424 for (height2 = 1;height2 < inheight;height2 <<= 1);
425 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
427 *outheight = max(1, height2);
431 if (gl_support_arb_texture_non_power_of_two)
432 depth2 = min(indepth >> picmip, maxsize);
435 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
436 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
438 *outdepth = max(1, depth2);
443 static int R_CalcTexelDataSize (gltexture_t *glt)
445 int width2, height2, depth2, size;
447 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &width2, &height2, &depth2);
449 size = width2 * height2 * depth2;
451 if (glt->flags & TEXF_MIPMAP)
453 while (width2 > 1 || height2 > 1 || depth2 > 1)
461 size += width2 * height2 * depth2;
465 return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
468 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
472 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
473 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
475 gltexturepool_t *pool;
477 Con_Print("glsize input loaded mip alpha name\n");
478 for (pool = gltexturepoolchain;pool;pool = pool->next)
486 for (glt = pool->gltchain;glt;glt = glt->chain)
488 glsize = R_CalcTexelDataSize(glt);
489 isloaded = !(glt->flags & GLTEXF_UPLOAD);
491 pooltotalt += glsize;
492 pooltotalp += glt->inputdatasize;
496 poolloadedt += glsize;
497 poolloadedp += glt->inputdatasize;
500 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);
503 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);
504 sumtotal += pooltotal;
505 sumtotalt += pooltotalt;
506 sumtotalp += pooltotalp;
507 sumloaded += poolloaded;
508 sumloadedt += poolloadedt;
509 sumloadedp += poolloadedp;
512 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);
515 static void R_TextureStats_f(void)
517 R_TextureStats_Print(true, true, true);
520 static void r_textures_start(void)
522 // LordHavoc: allow any alignment
524 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);CHECKGLERROR
525 qglPixelStorei(GL_PACK_ALIGNMENT, 1);CHECKGLERROR
527 texturemempool = Mem_AllocPool("texture management", 0, NULL);
529 // Disable JPEG screenshots if the DLL isn't loaded
530 if (! JPEG_OpenLibrary ())
531 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
532 // TODO: support png screenshots?
536 static void r_textures_shutdown(void)
538 rtexturepool_t *temp;
540 JPEG_CloseLibrary ();
542 while(gltexturepoolchain)
544 temp = (rtexturepool_t *) gltexturepoolchain;
545 R_FreeTexturePool(&temp);
548 resizebuffersize = 0;
549 texturebuffersize = 0;
551 colorconvertbuffer = NULL;
552 texturebuffer = NULL;
553 Mem_FreePool(&texturemempool);
556 static void r_textures_newmap(void)
560 void R_Textures_Init (void)
562 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f, "set texture filtering mode (GL_NEAREST, GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, etc)");
563 Cmd_AddCommand("r_texturestats", R_TextureStats_f, "print information about all loaded textures and some statistics");
564 Cvar_RegisterVariable (&gl_max_size);
565 Cvar_RegisterVariable (&gl_picmip);
566 Cvar_RegisterVariable (&r_lerpimages);
567 Cvar_RegisterVariable (&r_precachetextures);
568 Cvar_RegisterVariable (&gl_texture_anisotropy);
569 Cvar_RegisterVariable (&gl_texturecompression);
570 Cvar_RegisterVariable (&gl_texturecompression_color);
571 Cvar_RegisterVariable (&gl_texturecompression_normal);
572 Cvar_RegisterVariable (&gl_texturecompression_gloss);
573 Cvar_RegisterVariable (&gl_texturecompression_glow);
574 Cvar_RegisterVariable (&gl_texturecompression_2d);
575 Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
576 Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
577 Cvar_RegisterVariable (&gl_texturecompression_sky);
578 Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
580 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
583 void R_Textures_Frame (void)
585 static int old_aniso = 0;
587 // could do procedural texture animation here, if we keep track of which
588 // textures were accessed this frame...
590 // free the resize buffers
591 resizebuffersize = 0;
594 Mem_Free(resizebuffer);
597 if (colorconvertbuffer)
599 Mem_Free(colorconvertbuffer);
600 colorconvertbuffer = NULL;
603 if (old_aniso != gl_texture_anisotropy.integer)
606 gltexturepool_t *pool;
609 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
611 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
614 for (pool = gltexturepoolchain;pool;pool = pool->next)
616 for (glt = pool->gltchain;glt;glt = glt->chain)
618 // only update already uploaded images
619 if ((glt->flags & (GLTEXF_UPLOAD | TEXF_MIPMAP)) == TEXF_MIPMAP)
621 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
623 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
624 qglTexParameteri(gltexturetypeenums[glt->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
626 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
633 void R_MakeResizeBufferBigger(int size)
635 if (resizebuffersize < size)
637 resizebuffersize = size;
639 Mem_Free(resizebuffer);
640 if (colorconvertbuffer)
641 Mem_Free(colorconvertbuffer);
642 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
643 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
644 if (!resizebuffer || !colorconvertbuffer)
645 Host_Error("R_Upload: out of memory");
649 static void GL_SetupTextureParameters(int flags, int texturetype)
651 int textureenum = gltexturetypeenums[texturetype];
652 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
656 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
658 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
659 if (gl_texture_anisotropy.integer != aniso)
660 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
661 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
663 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
664 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
665 if (gltexturetypedimensions[texturetype] >= 3)
667 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
671 if (flags & TEXF_FORCENEAREST)
673 if (flags & TEXF_MIPMAP)
675 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
679 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
681 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
683 else if (flags & TEXF_FORCELINEAR)
685 if (flags & TEXF_MIPMAP)
687 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
689 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
693 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
698 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
700 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
704 if (flags & TEXF_MIPMAP)
706 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
710 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
712 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
718 static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy, int fragz, int fragwidth, int fragheight, int fragdepth)
720 int i, mip, width, height, depth;
722 unsigned char *prevbuffer;
727 // we need to restore the texture binding after finishing the upload
728 qglGetIntegerv(gltexturetypebindingenums[glt->texturetype], &oldbindtexnum);CHECKGLERROR
729 qglBindTexture(gltexturetypeenums[glt->texturetype], glt->texnum);CHECKGLERROR
731 // these are rounded up versions of the size to do better resampling
732 if (gl_support_arb_texture_non_power_of_two)
734 width = glt->inputwidth;
735 height = glt->inputheight;
736 depth = glt->inputdepth;
740 for (width = 1;width < glt->inputwidth ;width <<= 1);
741 for (height = 1;height < glt->inputheight;height <<= 1);
742 for (depth = 1;depth < glt->inputdepth ;depth <<= 1);
745 R_MakeResizeBufferBigger(width * height * depth * glt->sides * glt->bytesperpixel);
746 R_MakeResizeBufferBigger(fragwidth * fragheight * fragdepth * glt->sides * glt->bytesperpixel);
748 if (prevbuffer == NULL)
750 memset(resizebuffer, 0, fragwidth * fragheight * fragdepth * glt->bytesperpixel);
751 prevbuffer = resizebuffer;
753 else if (glt->textype->textype == TEXTYPE_PALETTE)
755 // promote paletted to RGBA, so we only have to worry about RGB and
756 // RGBA in the rest of this code
757 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, fragwidth * fragheight * fragdepth * glt->sides, glt->palette);
758 prevbuffer = colorconvertbuffer;
761 if ((glt->flags & (TEXF_MIPMAP | TEXF_PICMIP | GLTEXF_UPLOAD)) == 0 && glt->inputwidth == glt->tilewidth && glt->inputheight == glt->tileheight && glt->inputdepth == glt->tiledepth)
763 // update a portion of the image
764 switch(glt->texturetype)
766 case GLTEXTURETYPE_1D:
767 qglTexSubImage1D(GL_TEXTURE_1D, 0, fragx, fragwidth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
769 case GLTEXTURETYPE_2D:
770 qglTexSubImage2D(GL_TEXTURE_2D, 0, fragx, fragy, fragwidth, fragheight, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
772 case GLTEXTURETYPE_3D:
773 qglTexSubImage3D(GL_TEXTURE_3D, 0, fragx, fragy, fragz, fragwidth, fragheight, fragdepth, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
776 Host_Error("R_Upload: partial update of type other than 1D, 2D, or 3D");
782 if (fragx || fragy || fragz || glt->inputwidth != fragwidth || glt->inputheight != fragheight || glt->inputdepth != fragdepth)
783 Host_Error("R_Upload: partial update not allowed on initial upload or in combination with PICMIP or MIPMAP\n");
785 // upload the image for the first time
786 glt->flags &= ~GLTEXF_UPLOAD;
788 // cubemaps contain multiple images and thus get processed a bit differently
789 if (glt->texturetype != GLTEXTURETYPE_CUBEMAP)
791 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
793 Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
794 prevbuffer = resizebuffer;
797 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
799 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
800 prevbuffer = resizebuffer;
804 if (gl_support_texture_compression)
806 if (gl_texturecompression.integer >= 2)
807 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
809 qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
812 switch(glt->texturetype)
814 case GLTEXTURETYPE_1D:
815 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
816 if (glt->flags & TEXF_MIPMAP)
818 while (width > 1 || height > 1 || depth > 1)
820 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
821 prevbuffer = resizebuffer;
822 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->glinternalformat, width, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
826 case GLTEXTURETYPE_2D:
827 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
828 if (glt->flags & TEXF_MIPMAP)
830 while (width > 1 || height > 1 || depth > 1)
832 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
833 prevbuffer = resizebuffer;
834 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
838 case GLTEXTURETYPE_3D:
839 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
840 if (glt->flags & TEXF_MIPMAP)
842 while (width > 1 || height > 1 || depth > 1)
844 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
845 prevbuffer = resizebuffer;
846 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->glinternalformat, width, height, depth, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
850 case GLTEXTURETYPE_CUBEMAP:
851 // convert and upload each side in turn,
852 // from a continuous block of input texels
853 texturebuffer = prevbuffer;
854 for (i = 0;i < 6;i++)
856 prevbuffer = texturebuffer;
857 texturebuffer += glt->inputwidth * glt->inputheight * glt->inputdepth * glt->textype->inputbytesperpixel;
858 if (glt->inputwidth != width || glt->inputheight != height || glt->inputdepth != depth)
860 Image_Resample(prevbuffer, glt->inputwidth, glt->inputheight, glt->inputdepth, resizebuffer, width, height, depth, glt->bytesperpixel, r_lerpimages.integer);
861 prevbuffer = resizebuffer;
864 while (width > glt->tilewidth || height > glt->tileheight || depth > glt->tiledepth)
866 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->tilewidth, glt->tileheight, glt->tiledepth, glt->bytesperpixel);
867 prevbuffer = resizebuffer;
870 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
871 if (glt->flags & TEXF_MIPMAP)
873 while (width > 1 || height > 1 || depth > 1)
875 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->bytesperpixel);
876 prevbuffer = resizebuffer;
877 qglTexImage2D(cubemapside[i], mip++, glt->glinternalformat, width, height, 0, glt->glformat, GL_UNSIGNED_BYTE, prevbuffer);CHECKGLERROR
883 GL_SetupTextureParameters(glt->flags, glt->texturetype);
885 qglBindTexture(gltexturetypeenums[glt->texturetype], oldbindtexnum);CHECKGLERROR
888 static void R_UploadTexture (gltexture_t *glt)
890 if (!(glt->flags & GLTEXF_UPLOAD))
894 qglGenTextures(1, (GLuint *)&glt->texnum);CHECKGLERROR
895 R_Upload(glt, glt->inputtexels, 0, 0, 0, glt->inputwidth, glt->inputheight, glt->inputdepth);
896 if (glt->inputtexels)
898 Mem_Free(glt->inputtexels);
899 glt->inputtexels = NULL;
900 glt->flags |= GLTEXF_DESTROYED;
902 else if (glt->flags & GLTEXF_DESTROYED)
903 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
906 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)
910 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
911 textypeinfo_t *texinfo;
913 if (cls.state == ca_dedicated)
916 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
918 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
921 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
923 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
927 texinfo = R_GetTexTypeInfo(textype, flags);
928 size = width * height * depth * sides * texinfo->inputbytesperpixel;
931 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
935 // clear the alpha flag if the texture has no transparent pixels
938 case TEXTYPE_PALETTE:
939 if (flags & TEXF_ALPHA)
941 flags &= ~TEXF_ALPHA;
944 for (i = 0;i < size;i++)
946 if (((unsigned char *)&palette[data[i]])[3] < 255)
956 if (flags & TEXF_ALPHA)
957 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
960 if (flags & TEXF_ALPHA)
962 flags &= ~TEXF_ALPHA;
965 for (i = 3;i < size;i += 4)
977 Host_Error("R_LoadTexture: unknown texture type");
980 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
982 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
984 glt->chain = pool->gltchain;
985 pool->gltchain = glt;
986 glt->inputwidth = width;
987 glt->inputheight = height;
988 glt->inputdepth = depth;
989 glt->flags = flags | GLTEXF_UPLOAD;
990 glt->textype = texinfo;
991 glt->texturetype = texturetype;
992 glt->inputdatasize = size;
993 glt->palette = palette;
994 glt->glinternalformat = texinfo->glinternalformat;
995 glt->glformat = texinfo->glformat;
996 glt->bytesperpixel = texinfo->internalbytesperpixel;
997 glt->sides = glt->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1002 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1003 memcpy(glt->inputtexels, data, size);
1006 glt->inputtexels = NULL;
1008 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->inputwidth, glt->inputheight, glt->inputdepth, &glt->tilewidth, &glt->tileheight, &glt->tiledepth);
1009 R_PrecacheTexture(glt);
1011 return (rtexture_t *)glt;
1014 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1016 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1019 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)
1021 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1024 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)
1026 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1029 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1031 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1034 int R_TextureHasAlpha(rtexture_t *rt)
1036 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1039 int R_TextureWidth(rtexture_t *rt)
1041 return rt ? ((gltexture_t *)rt)->inputwidth : 0;
1044 int R_TextureHeight(rtexture_t *rt)
1046 return rt ? ((gltexture_t *)rt)->inputheight : 0;
1049 void R_UpdateTexture(rtexture_t *rt, unsigned char *data, int x, int y, int width, int height)
1053 Host_Error("R_UpdateTexture: no texture supplied");
1055 Host_Error("R_UpdateTexture: no data supplied");
1056 glt = (gltexture_t *)rt;
1058 // we need it to be uploaded before we can update a part of it
1059 if (glt->flags & GLTEXF_UPLOAD)
1060 R_UploadTexture(glt);
1062 // update part of the texture
1063 R_Upload(glt, data, x, y, 0, width, height, 1);