7 cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048"};
8 cvar_t gl_max_scrapsize = {CVAR_SAVE, "gl_max_scrapsize", "256"};
9 cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0"};
10 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
11 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
12 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1"};
14 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
15 int gl_filter_mag = GL_LINEAR;
18 static mempool_t *texturemempool;
20 // note: this must not conflict with TEXF_ flags in r_textures.h
21 // cleared when a texture is uploaded
22 #define GLTEXF_UPLOAD 0x00010000
23 // bitmask for mismatch checking
24 #define GLTEXF_IMPORTANTBITS (0)
25 // set when image is uploaded and freed
26 #define GLTEXF_DESTROYED 0x00040000
28 // size of images which hold fragment textures, ignores picmip and max_size
29 static int block_size;
31 typedef struct textypeinfo_s
34 int inputbytesperpixel;
35 int internalbytesperpixel;
41 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 3};
42 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3};
43 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 3};
44 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 4};
45 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 4};
46 static textypeinfo_t textype_dsdt = {TEXTYPE_DSDT , 2, 2, GL_DSDT_NV, GL_DSDT8_NV};
48 // a tiling texture (most common type)
49 #define GLIMAGETYPE_TILE 0
50 // a fragments texture (contains one or more fragment textures)
51 #define GLIMAGETYPE_FRAGMENTS 1
53 #define GLTEXTURETYPE_1D 0
54 #define GLTEXTURETYPE_2D 1
55 #define GLTEXTURETYPE_3D 2
56 #define GLTEXTURETYPE_CUBEMAP 3
58 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
59 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
60 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
61 static int cubemapside[6] =
63 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
64 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
65 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
66 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
67 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
68 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
71 // a gltextureimage can have one (or more if fragments) gltextures inside
72 typedef struct gltextureimage_s
74 struct gltextureimage_s *imagechain;
76 int type; // one of the GLIMAGETYPE_ values
77 int texturetype; // one of the GLTEXTURETYPE_ values
78 int sides; // 1 or 6 depending on texturetype
79 int texnum; // GL texture slot number
80 int width, height, depth; // 3D texture support
81 int bytesperpixel; // bytes per pixel
82 int glformat; // GL_RGB or GL_RGBA
83 int glinternalformat; // 3 or 4
85 short *blockallocation; // fragment allocation (2D only)
89 typedef struct gltexture_s
91 // this field is exposed to the R_GetTexture macro, for speed reasons
92 // (must be identical in rtexture_t)
93 int texnum; // GL texture slot number
95 // pointer to texturepool (check this to see if the texture is allocated)
96 struct gltexturepool_s *pool;
97 // pointer to next texture in texturepool chain
98 struct gltexture_s *chain;
99 // pointer into gltextureimage array
100 gltextureimage_t *image;
101 // name of the texture (this might be removed someday), no duplicates
103 // location in the image, and size
104 int x, y, z, width, height, depth;
105 // copy of the original texture(s) supplied to the upload function, for
106 // delayed uploads (non-precached)
107 unsigned char *inputtexels;
108 // original data size in *inputtexels
110 // flags supplied to the LoadTexture function
111 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
113 // pointer to one of the textype_ structs
114 textypeinfo_t *textype;
115 // one of the GLTEXTURETYPE_ values
117 // palette if the texture is TEXTYPE_PALETTE
118 const unsigned int *palette;
122 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
124 typedef struct gltexturepool_s
126 unsigned int sentinel;
127 struct gltextureimage_s *imagechain;
128 struct gltexture_s *gltchain;
129 struct gltexturepool_s *next;
133 static gltexturepool_t *gltexturepoolchain = NULL;
135 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
136 static int resizebuffersize = 0;
137 static unsigned char *texturebuffer;
138 static int texturebuffersize = 0;
140 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
142 if ((flags & (TEXF_PICMIP | TEXF_FRAGMENT)) == (TEXF_PICMIP | TEXF_FRAGMENT))
144 Host_Error("R_GetTexTypeInfo: TEXF_PICMIP can not be used with TEXF_FRAGMENT");
147 if (flags & TEXF_ALPHA)
151 case TEXTYPE_PALETTE:
152 return &textype_palette_alpha;
154 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
157 return &textype_rgba_alpha;
159 Host_Error("R_GetTexTypeInfo: unknown texture format");
167 case TEXTYPE_PALETTE:
168 return &textype_palette;
172 return &textype_rgba;
174 return &textype_dsdt;
176 Host_Error("R_GetTexTypeInfo: unknown texture format");
182 static void R_UploadTexture(gltexture_t *t);
184 static void R_PrecacheTexture(gltexture_t *glt)
188 if (glt->flags & TEXF_ALWAYSPRECACHE)
190 else if (r_precachetextures.integer >= 2)
192 else if (r_precachetextures.integer >= 1)
193 if (glt->flags & TEXF_PRECACHE)
197 R_UploadTexture(glt);
200 int R_RealGetTexture(rtexture_t *rt)
205 glt = (gltexture_t *)rt;
206 if (glt->flags & GLTEXF_UPLOAD)
207 R_UploadTexture(glt);
208 glt->texnum = glt->image->texnum;
209 return glt->image->texnum;
215 void R_FreeTexture(rtexture_t *rt)
217 gltexture_t *glt, **gltpointer;
218 gltextureimage_t *image, **gltimagepointer;
220 glt = (gltexture_t *)rt;
222 Host_Error("R_FreeTexture: texture == NULL");
224 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
225 if (*gltpointer == glt)
226 *gltpointer = glt->chain;
228 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
230 // note: if freeing a fragment texture, this will not make the claimed
231 // space available for new textures unless all other fragments in the
232 // image are also freed
236 image->texturecount--;
237 if (image->texturecount < 1)
239 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
240 if (*gltimagepointer == image)
241 *gltimagepointer = image->imagechain;
243 Host_Error("R_FreeTexture: image not linked in pool");
245 qglDeleteTextures(1, (GLuint *)&image->texnum);
246 if (image->blockallocation)
247 Mem_Free(image->blockallocation);
252 if (glt->inputtexels)
253 Mem_Free(glt->inputtexels);
257 rtexturepool_t *R_AllocTexturePool(void)
259 gltexturepool_t *pool;
260 if (texturemempool == NULL)
262 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
265 pool->next = gltexturepoolchain;
266 gltexturepoolchain = pool;
267 pool->sentinel = TEXTUREPOOL_SENTINEL;
268 return (rtexturepool_t *)pool;
271 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
273 gltexturepool_t *pool, **poolpointer;
274 if (rtexturepool == NULL)
276 if (*rtexturepool == NULL)
278 pool = (gltexturepool_t *)(*rtexturepool);
279 *rtexturepool = NULL;
280 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
281 Host_Error("R_FreeTexturePool: pool already freed");
282 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
283 if (*poolpointer == pool)
284 *poolpointer = pool->next;
286 Host_Error("R_FreeTexturePool: pool not linked");
287 while (pool->gltchain)
288 R_FreeTexture((rtexture_t *)pool->gltchain);
289 if (pool->imagechain)
290 Con_Printf("R_FreeTexturePool: not all images freed\n");
295 typedef struct glmode_s
298 int minification, magnification;
302 static glmode_t modes[] =
304 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
305 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
306 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
307 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
308 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
309 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
312 static void GL_TextureMode_f (void)
316 gltextureimage_t *image;
317 gltexturepool_t *pool;
321 for (i = 0;i < 6;i++)
323 if (gl_filter_min == modes[i].minification)
325 Con_Printf("%s\n", modes[i].name);
329 Con_Print("current filter is unknown???\n");
333 for (i = 0;i < 6;i++)
334 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
338 Con_Print("bad filter name\n");
342 gl_filter_min = modes[i].minification;
343 gl_filter_mag = modes[i].magnification;
345 // change all the existing mipmap texture objects
346 // FIXME: force renderer(/client/something?) restart instead?
347 for (pool = gltexturepoolchain;pool;pool = pool->next)
349 for (image = pool->imagechain;image;image = image->imagechain)
351 // only update already uploaded images
352 if (!(image->flags & GLTEXF_UPLOAD) && !(image->flags & (TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
354 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
355 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
356 if (image->flags & TEXF_MIPMAP)
357 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
359 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
360 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
361 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
367 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
369 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
371 if (gl_max_size.integer > gl_max_texture_size)
372 Cvar_SetValue("gl_max_size", gl_max_texture_size);
377 case GLTEXTURETYPE_1D:
378 case GLTEXTURETYPE_2D:
379 maxsize = gl_max_texture_size;
381 case GLTEXTURETYPE_3D:
382 maxsize = gl_max_3d_texture_size;
384 case GLTEXTURETYPE_CUBEMAP:
385 maxsize = gl_max_cube_map_texture_size;
389 if (flags & TEXF_PICMIP)
391 maxsize = min(maxsize, gl_max_size.integer);
392 picmip = gl_picmip.integer;
397 for (width2 = 1;width2 < inwidth;width2 <<= 1);
398 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
399 *outwidth = max(1, width2);
403 for (height2 = 1;height2 < inheight;height2 <<= 1);
404 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
405 *outheight = max(1, height2);
409 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
410 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
411 *outdepth = max(1, depth2);
416 static int R_CalcTexelDataSize (gltexture_t *glt)
418 int width2, height2, depth2, size;
420 if (glt->flags & TEXF_FRAGMENT)
421 return glt->width * glt->height * glt->depth * glt->textype->internalbytesperpixel * glt->image->sides;
423 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->width, glt->height, glt->depth, &width2, &height2, &depth2);
425 size = width2 * height2 * depth2;
427 if (glt->flags & TEXF_MIPMAP)
429 while (width2 > 1 || height2 > 1 || depth2 > 1)
437 size += width2 * height2 * depth2;
441 return size * glt->textype->internalbytesperpixel * glt->image->sides;
444 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
448 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
449 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
451 gltexturepool_t *pool;
453 Con_Print("glsize input loaded mip alpha name\n");
454 for (pool = gltexturepoolchain;pool;pool = pool->next)
462 for (glt = pool->gltchain;glt;glt = glt->chain)
464 glsize = R_CalcTexelDataSize(glt);
465 isloaded = !(glt->flags & GLTEXF_UPLOAD);
467 pooltotalt += glsize;
468 pooltotalp += glt->inputdatasize;
472 poolloadedt += glsize;
473 poolloadedp += glt->inputdatasize;
476 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);
479 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);
480 sumtotal += pooltotal;
481 sumtotalt += pooltotalt;
482 sumtotalp += pooltotalp;
483 sumloaded += poolloaded;
484 sumloadedt += poolloadedt;
485 sumloadedp += poolloadedp;
488 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);
491 static void R_TextureStats_f(void)
493 R_TextureStats_Print(true, true, true);
496 static void r_textures_start(void)
498 // LordHavoc: allow any alignment
499 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
502 // use the largest scrap texture size we can (not sure if this is really a good idea)
503 for (block_size = 1;block_size * 2 <= gl_max_texture_size && block_size * 2 <= gl_max_scrapsize.integer;block_size *= 2);
505 texturemempool = Mem_AllocPool("texture management", 0, NULL);
507 // Disable JPEG screenshots if the DLL isn't loaded
508 if (! JPEG_OpenLibrary ())
509 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
510 // TODO: support png screenshots?
514 static void r_textures_shutdown(void)
516 rtexturepool_t *temp;
518 JPEG_CloseLibrary ();
520 while(gltexturepoolchain)
522 temp = (rtexturepool_t *) gltexturepoolchain;
523 R_FreeTexturePool(&temp);
526 resizebuffersize = 0;
527 texturebuffersize = 0;
529 colorconvertbuffer = NULL;
530 texturebuffer = NULL;
531 Mem_FreePool(&texturemempool);
534 static void r_textures_newmap(void)
538 void R_Textures_Init (void)
540 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
541 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
542 Cvar_RegisterVariable (&gl_max_scrapsize);
543 Cvar_RegisterVariable (&gl_max_size);
544 Cvar_RegisterVariable (&gl_picmip);
545 Cvar_RegisterVariable (&r_lerpimages);
546 Cvar_RegisterVariable (&r_precachetextures);
547 Cvar_RegisterVariable (&gl_texture_anisotropy);
549 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
552 void R_Textures_Frame (void)
554 static int old_aniso = 0;
556 // could do procedural texture animation here, if we keep track of which
557 // textures were accessed this frame...
559 // free the resize buffers
560 resizebuffersize = 0;
563 Mem_Free(resizebuffer);
566 if (colorconvertbuffer)
568 Mem_Free(colorconvertbuffer);
569 colorconvertbuffer = NULL;
572 if (old_aniso != gl_texture_anisotropy.integer)
574 gltextureimage_t *image;
575 gltexturepool_t *pool;
578 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
580 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
582 for (pool = gltexturepoolchain;pool;pool = pool->next)
584 for (image = pool->imagechain;image;image = image->imagechain)
586 // only update already uploaded images
587 if (!(image->flags & GLTEXF_UPLOAD) && (image->flags & TEXF_MIPMAP))
589 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
591 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
592 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
594 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
601 void R_MakeResizeBufferBigger(int size)
603 if (resizebuffersize < size)
605 resizebuffersize = size;
607 Mem_Free(resizebuffer);
608 if (colorconvertbuffer)
609 Mem_Free(colorconvertbuffer);
610 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
611 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
612 if (!resizebuffer || !colorconvertbuffer)
613 Host_Error("R_Upload: out of memory");
617 static void GL_SetupTextureParameters(int flags, int texturetype)
619 int textureenum = gltexturetypeenums[texturetype];
620 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
624 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
626 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
627 if (gl_texture_anisotropy.integer != aniso)
628 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
629 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
631 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
632 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
633 if (gltexturetypedimensions[texturetype] >= 3)
635 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
639 if (flags & TEXF_FORCENEAREST)
641 if (flags & TEXF_MIPMAP)
643 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
647 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
649 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
651 else if (flags & TEXF_FORCELINEAR)
653 if (flags & TEXF_MIPMAP)
655 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
657 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
661 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
666 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
668 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
672 if (flags & TEXF_MIPMAP)
674 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
678 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
680 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
686 static void R_Upload(gltexture_t *glt, unsigned char *data)
688 int i, mip, width, height, depth;
690 unsigned char *prevbuffer;
695 glt->texnum = glt->image->texnum;
696 // we need to restore the texture binding after finishing the upload
697 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
698 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
700 glt->flags &= ~GLTEXF_UPLOAD;
702 if (glt->flags & TEXF_FRAGMENT)
704 if (glt->image->flags & GLTEXF_UPLOAD)
706 glt->image->flags &= ~GLTEXF_UPLOAD;
707 Con_DPrint("uploaded new fragments image\n");
708 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
709 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
710 switch(glt->image->texturetype)
712 case GLTEXTURETYPE_1D:
713 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
716 case GLTEXTURETYPE_2D:
717 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
720 case GLTEXTURETYPE_3D:
721 qglTexImage3D(GL_TEXTURE_3D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, glt->image->depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
725 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
728 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
731 if (prevbuffer == NULL)
733 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
734 memset(resizebuffer, 0, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
735 prevbuffer = resizebuffer;
737 else if (glt->textype->textype == TEXTYPE_PALETTE)
739 // promote paletted to RGBA, so we only have to worry about RGB and
740 // RGBA in the rest of this code
741 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
742 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
743 prevbuffer = colorconvertbuffer;
746 switch(glt->image->texturetype)
748 case GLTEXTURETYPE_1D:
749 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
752 case GLTEXTURETYPE_2D:
753 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
756 case GLTEXTURETYPE_3D:
757 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
761 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
767 glt->image->flags &= ~GLTEXF_UPLOAD;
769 // these are rounded up versions of the size to do better resampling
770 for (width = 1;width < glt->width ;width <<= 1);
771 for (height = 1;height < glt->height;height <<= 1);
772 for (depth = 1;depth < glt->depth ;depth <<= 1);
774 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
776 if (prevbuffer == NULL)
778 width = glt->image->width;
779 height = glt->image->height;
780 depth = glt->image->depth;
781 memset(resizebuffer, 0, width * height * depth * glt->image->bytesperpixel);
782 prevbuffer = resizebuffer;
786 if (glt->textype->textype == TEXTYPE_PALETTE)
788 // promote paletted to RGBA, so we only have to worry about RGB and
789 // RGBA in the rest of this code
790 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
791 prevbuffer = colorconvertbuffer;
795 // cubemaps contain multiple images and thus get processed a bit differently
796 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
798 if (glt->width != width || glt->height != height || glt->depth != depth)
800 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
801 prevbuffer = resizebuffer;
804 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
806 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
807 prevbuffer = resizebuffer;
811 switch(glt->image->texturetype)
813 case GLTEXTURETYPE_1D:
814 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
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->image->bytesperpixel);
821 prevbuffer = resizebuffer;
822 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
827 case GLTEXTURETYPE_2D:
828 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
830 if (glt->flags & TEXF_MIPMAP)
832 while (width > 1 || height > 1 || depth > 1)
834 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
835 prevbuffer = resizebuffer;
836 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
841 case GLTEXTURETYPE_3D:
842 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
844 if (glt->flags & TEXF_MIPMAP)
846 while (width > 1 || height > 1 || depth > 1)
848 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
849 prevbuffer = resizebuffer;
850 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
855 case GLTEXTURETYPE_CUBEMAP:
856 // convert and upload each side in turn,
857 // from a continuous block of input texels
858 texturebuffer = prevbuffer;
859 for (i = 0;i < 6;i++)
861 prevbuffer = texturebuffer;
862 texturebuffer += glt->width * glt->height * glt->depth * glt->textype->inputbytesperpixel;
863 if (glt->width != width || glt->height != height || glt->depth != depth)
865 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
866 prevbuffer = resizebuffer;
869 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
871 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
872 prevbuffer = resizebuffer;
875 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
877 if (glt->flags & TEXF_MIPMAP)
879 while (width > 1 || height > 1 || depth > 1)
881 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
882 prevbuffer = resizebuffer;
883 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
890 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
892 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
895 static void R_FindImageForTexture(gltexture_t *glt)
897 int i, j, best, best2, x, y, z, w, h, d;
898 textypeinfo_t *texinfo;
899 gltexturepool_t *pool;
900 gltextureimage_t *image, **imagechainpointer;
901 texinfo = glt->textype;
904 // remains -1 until uploaded
913 if (glt->flags & TEXF_FRAGMENT)
915 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
917 image = *imagechainpointer;
918 if (image->type != GLIMAGETYPE_FRAGMENTS)
920 if (image->texturetype != glt->texturetype)
922 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_FORCENEAREST | TEXF_FORCELINEAR))
924 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
926 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
929 // got a fragments texture, find a place in it if we can
930 for (best = image->width, i = 0;i < image->width - w;i++)
932 for (best2 = 0, j = 0;j < w;j++)
934 if (image->blockallocation[i+j] >= best)
936 if (best2 < image->blockallocation[i+j])
937 best2 = image->blockallocation[i+j];
941 // this is a valid spot
947 if (best + h > image->height)
950 for (i = 0;i < w;i++)
951 image->blockallocation[x + i] = best + h;
957 image->texturecount++;
961 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
964 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
967 image->type = GLIMAGETYPE_FRAGMENTS;
968 // make sure the created image is big enough for the fragment
969 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
971 if (gltexturetypedimensions[glt->texturetype] >= 2)
972 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
974 if (gltexturetypedimensions[glt->texturetype] >= 3)
975 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
976 image->blockallocation = (short int *)Mem_Alloc(texturemempool, image->width * sizeof(short));
977 memset(image->blockallocation, 0, image->width * sizeof(short));
982 for (i = 0;i < w;i++)
983 image->blockallocation[x + i] = y + h;
987 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
989 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
992 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
995 image->type = GLIMAGETYPE_TILE;
996 image->blockallocation = NULL;
998 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->width, glt->height, glt->depth, &image->width, &image->height, &image->depth);
1000 image->texturetype = glt->texturetype;
1001 image->glinternalformat = texinfo->glinternalformat;
1002 image->glformat = texinfo->glformat;
1003 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_PICMIP | TEXF_FORCENEAREST | TEXF_FORCELINEAR)) | GLTEXF_UPLOAD;
1004 image->bytesperpixel = texinfo->internalbytesperpixel;
1005 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1006 // get a texture number to use
1007 qglGenTextures(1, (GLuint *)&image->texnum);
1008 *imagechainpointer = image;
1009 image->texturecount++;
1017 // note: R_FindImageForTexture must be called before this
1018 static void R_UploadTexture (gltexture_t *glt)
1020 if (!(glt->flags & GLTEXF_UPLOAD))
1023 R_Upload(glt, glt->inputtexels);
1024 if (glt->inputtexels)
1026 Mem_Free(glt->inputtexels);
1027 glt->inputtexels = NULL;
1028 glt->flags |= GLTEXF_DESTROYED;
1030 else if (glt->flags & GLTEXF_DESTROYED)
1031 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1034 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)
1038 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1039 textypeinfo_t *texinfo;
1041 if (cls.state == ca_dedicated)
1044 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
1046 Con_Printf ("R_LoadTexture: only 2D fragment textures implemented\n");
1049 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1051 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
1054 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1056 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
1060 texinfo = R_GetTexTypeInfo(textype, flags);
1061 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1064 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
1068 // clear the alpha flag if the texture has no transparent pixels
1071 case TEXTYPE_PALETTE:
1072 if (flags & TEXF_ALPHA)
1074 flags &= ~TEXF_ALPHA;
1077 for (i = 0;i < size;i++)
1079 if (((unsigned char *)&palette[data[i]])[3] < 255)
1081 flags |= TEXF_ALPHA;
1089 if (flags & TEXF_ALPHA)
1090 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
1093 if (flags & TEXF_ALPHA)
1095 flags &= ~TEXF_ALPHA;
1098 for (i = 3;i < size;i += 4)
1102 flags |= TEXF_ALPHA;
1112 Host_Error("R_LoadTexture: unknown texture type");
1115 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1117 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1119 glt->chain = pool->gltchain;
1120 pool->gltchain = glt;
1122 glt->height = height;
1124 glt->flags = flags | GLTEXF_UPLOAD;
1125 glt->textype = texinfo;
1126 glt->texturetype = texturetype;
1127 glt->inputdatasize = size;
1128 glt->palette = palette;
1132 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1133 if (glt->inputtexels == NULL)
1134 Con_Printf ("R_LoadTexture: out of memory\n");
1136 memcpy(glt->inputtexels, data, size);
1139 glt->inputtexels = NULL;
1141 R_FindImageForTexture(glt);
1142 R_PrecacheTexture(glt);
1144 return (rtexture_t *)glt;
1147 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1149 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1152 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)
1154 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1157 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)
1159 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1162 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1164 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1167 int R_TextureHasAlpha(rtexture_t *rt)
1169 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1172 int R_TextureWidth(rtexture_t *rt)
1174 return rt ? ((gltexture_t *)rt)->width : 0;
1177 int R_TextureHeight(rtexture_t *rt)
1179 return rt ? ((gltexture_t *)rt)->height : 0;
1182 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1185 float iwidth, iheight, idepth;
1186 if (cls.state == ca_dedicated)
1194 if (fx1 || fy1 || fx2 || fy2)
1212 Host_Error("R_FragmentLocation: no texture supplied");
1213 glt = (gltexture_t *)rt;
1214 if (glt->flags & TEXF_FRAGMENT)
1220 if (fx1 || fy1 || fx2 || fy2)
1222 iwidth = 1.0f / glt->image->width;
1223 iheight = 1.0f / glt->image->height;
1224 idepth = 1.0f / glt->image->depth;
1226 *fx1 = glt->x * iwidth;
1228 *fy1 = glt->y * iheight;
1230 *fz1 = glt->z * idepth;
1232 *fx2 = (glt->x + glt->width) * iwidth;
1234 *fy2 = (glt->y + glt->height) * iheight;
1236 *fz2 = (glt->z + glt->depth) * idepth;
1247 if (fx1 || fy1 || fx2 || fy2)
1265 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1267 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1270 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1275 void R_UpdateTexture(rtexture_t *rt, unsigned char *data)
1279 Host_Error("R_UpdateTexture: no texture supplied");
1281 Host_Error("R_UpdateTexture: no data supplied");
1282 glt = (gltexture_t *)rt;
1284 // if it has not been uploaded yet, update the data that will be used when it is
1285 if (glt->inputtexels)
1286 memcpy(glt->inputtexels, data, glt->inputdatasize);
1288 R_Upload(glt, data);