6 cvar_t gl_max_size = {CVAR_SAVE, "gl_max_size", "2048"};
7 cvar_t gl_max_scrapsize = {CVAR_SAVE, "gl_max_scrapsize", "256"};
8 cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
11 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1"};
13 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
14 int gl_filter_mag = GL_LINEAR;
17 static mempool_t *texturemempool;
19 // note: this must not conflict with TEXF_ flags in r_textures.h
20 // cleared when a texture is uploaded
21 #define GLTEXF_UPLOAD 0x00010000
22 // bitmask for mismatch checking
23 #define GLTEXF_IMPORTANTBITS (0)
24 // set when image is uploaded and freed
25 #define GLTEXF_DESTROYED 0x00040000
27 // size of images which hold fragment textures, ignores picmip and max_size
28 static int block_size;
30 typedef struct textypeinfo_s
33 int inputbytesperpixel;
34 int internalbytesperpixel;
40 static textypeinfo_t textype_palette = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 3};
41 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3};
42 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 3};
43 static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, GL_RGBA , 4};
44 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA , 4};
45 static textypeinfo_t textype_dsdt = {TEXTYPE_DSDT , 2, 2, GL_DSDT_NV, GL_DSDT8_NV};
47 // a tiling texture (most common type)
48 #define GLIMAGETYPE_TILE 0
49 // a fragments texture (contains one or more fragment textures)
50 #define GLIMAGETYPE_FRAGMENTS 1
52 #define GLTEXTURETYPE_1D 0
53 #define GLTEXTURETYPE_2D 1
54 #define GLTEXTURETYPE_3D 2
55 #define GLTEXTURETYPE_CUBEMAP 3
57 static int gltexturetypeenums[4] = {GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP_ARB};
58 static int gltexturetypebindingenums[4] = {GL_TEXTURE_BINDING_1D, GL_TEXTURE_BINDING_2D, GL_TEXTURE_BINDING_3D, GL_TEXTURE_BINDING_CUBE_MAP_ARB};
59 static int gltexturetypedimensions[4] = {1, 2, 3, 2};
60 static int cubemapside[6] =
62 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
63 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
64 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
65 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
66 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
67 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
70 // a gltextureimage can have one (or more if fragments) gltextures inside
71 typedef struct gltextureimage_s
73 struct gltextureimage_s *imagechain;
75 int type; // one of the GLIMAGETYPE_ values
76 int texturetype; // one of the GLTEXTURETYPE_ values
77 int sides; // 1 or 6 depending on texturetype
78 int texnum; // GL texture slot number
79 int width, height, depth; // 3D texture support
80 int bytesperpixel; // bytes per pixel
81 int glformat; // GL_RGB or GL_RGBA
82 int glinternalformat; // 3 or 4
84 short *blockallocation; // fragment allocation (2D only)
88 typedef struct gltexture_s
90 // this field is exposed to the R_GetTexture macro, for speed reasons
91 // (must be identical in rtexture_t)
92 int texnum; // GL texture slot number
94 // pointer to texturepool (check this to see if the texture is allocated)
95 struct gltexturepool_s *pool;
96 // pointer to next texture in texturepool chain
97 struct gltexture_s *chain;
98 // pointer into gltextureimage array
99 gltextureimage_t *image;
100 // name of the texture (this might be removed someday), no duplicates
102 // location in the image, and size
103 int x, y, z, width, height, depth;
104 // copy of the original texture(s) supplied to the upload function, for
105 // delayed uploads (non-precached)
106 unsigned char *inputtexels;
107 // original data size in *inputtexels
109 // flags supplied to the LoadTexture function
110 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
112 // pointer to one of the textype_ structs
113 textypeinfo_t *textype;
114 // one of the GLTEXTURETYPE_ values
116 // palette if the texture is TEXTYPE_PALETTE
117 const unsigned int *palette;
121 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
123 typedef struct gltexturepool_s
125 unsigned int sentinel;
126 struct gltextureimage_s *imagechain;
127 struct gltexture_s *gltchain;
128 struct gltexturepool_s *next;
132 static gltexturepool_t *gltexturepoolchain = NULL;
134 static unsigned char *resizebuffer = NULL, *colorconvertbuffer;
135 static int resizebuffersize = 0;
136 static unsigned char *texturebuffer;
137 static int texturebuffersize = 0;
139 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
141 if ((flags & (TEXF_PICMIP | TEXF_FRAGMENT)) == (TEXF_PICMIP | TEXF_FRAGMENT))
143 Host_Error("R_GetTexTypeInfo: TEXF_PICMIP can not be used with TEXF_FRAGMENT");
146 if (flags & TEXF_ALPHA)
150 case TEXTYPE_PALETTE:
151 return &textype_palette_alpha;
153 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
156 return &textype_rgba_alpha;
158 Host_Error("R_GetTexTypeInfo: unknown texture format");
166 case TEXTYPE_PALETTE:
167 return &textype_palette;
171 return &textype_rgba;
173 return &textype_dsdt;
175 Host_Error("R_GetTexTypeInfo: unknown texture format");
181 static void R_UploadTexture(gltexture_t *t);
183 static void R_PrecacheTexture(gltexture_t *glt)
187 if (glt->flags & TEXF_ALWAYSPRECACHE)
189 else if (r_precachetextures.integer >= 2)
191 else if (r_precachetextures.integer >= 1)
192 if (glt->flags & TEXF_PRECACHE)
196 R_UploadTexture(glt);
199 int R_RealGetTexture(rtexture_t *rt)
204 glt = (gltexture_t *)rt;
205 if (glt->flags & GLTEXF_UPLOAD)
206 R_UploadTexture(glt);
207 glt->texnum = glt->image->texnum;
208 return glt->image->texnum;
214 void R_FreeTexture(rtexture_t *rt)
216 gltexture_t *glt, **gltpointer;
217 gltextureimage_t *image, **gltimagepointer;
219 glt = (gltexture_t *)rt;
221 Host_Error("R_FreeTexture: texture == NULL");
223 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
224 if (*gltpointer == glt)
225 *gltpointer = glt->chain;
227 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
229 // note: if freeing a fragment texture, this will not make the claimed
230 // space available for new textures unless all other fragments in the
231 // image are also freed
235 image->texturecount--;
236 if (image->texturecount < 1)
238 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
239 if (*gltimagepointer == image)
240 *gltimagepointer = image->imagechain;
242 Host_Error("R_FreeTexture: image not linked in pool");
244 qglDeleteTextures(1, (GLuint *)&image->texnum);
245 if (image->blockallocation)
246 Mem_Free(image->blockallocation);
251 if (glt->inputtexels)
252 Mem_Free(glt->inputtexels);
256 rtexturepool_t *R_AllocTexturePool(void)
258 gltexturepool_t *pool;
259 if (texturemempool == NULL)
261 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
264 pool->next = gltexturepoolchain;
265 gltexturepoolchain = pool;
266 pool->sentinel = TEXTUREPOOL_SENTINEL;
267 return (rtexturepool_t *)pool;
270 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
272 gltexturepool_t *pool, **poolpointer;
273 if (rtexturepool == NULL)
275 if (*rtexturepool == NULL)
277 pool = (gltexturepool_t *)(*rtexturepool);
278 *rtexturepool = NULL;
279 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
280 Host_Error("R_FreeTexturePool: pool already freed");
281 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
282 if (*poolpointer == pool)
283 *poolpointer = pool->next;
285 Host_Error("R_FreeTexturePool: pool not linked");
286 while (pool->gltchain)
287 R_FreeTexture((rtexture_t *)pool->gltchain);
288 if (pool->imagechain)
289 Con_Printf("R_FreeTexturePool: not all images freed\n");
294 typedef struct glmode_s
297 int minification, magnification;
301 static glmode_t modes[] =
303 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
304 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
305 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
306 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
307 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
308 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
311 static void GL_TextureMode_f (void)
315 gltextureimage_t *image;
316 gltexturepool_t *pool;
320 for (i = 0;i < 6;i++)
322 if (gl_filter_min == modes[i].minification)
324 Con_Printf("%s\n", modes[i].name);
328 Con_Print("current filter is unknown???\n");
332 for (i = 0;i < 6;i++)
333 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
337 Con_Print("bad filter name\n");
341 gl_filter_min = modes[i].minification;
342 gl_filter_mag = modes[i].magnification;
344 // change all the existing mipmap texture objects
345 // FIXME: force renderer(/client/something?) restart instead?
346 for (pool = gltexturepoolchain;pool;pool = pool->next)
348 for (image = pool->imagechain;image;image = image->imagechain)
350 // only update already uploaded images
351 if (!(image->flags & GLTEXF_UPLOAD) && !(image->flags & (TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
353 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
354 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
355 if (image->flags & TEXF_MIPMAP)
356 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
358 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
359 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
360 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
366 static void GL_Texture_CalcImageSize(int texturetype, int flags, int inwidth, int inheight, int indepth, int *outwidth, int *outheight, int *outdepth)
368 int picmip = 0, maxsize = 0, width2 = 1, height2 = 1, depth2 = 1;
370 if (gl_max_size.integer > gl_max_texture_size)
371 Cvar_SetValue("gl_max_size", gl_max_texture_size);
376 case GLTEXTURETYPE_1D:
377 case GLTEXTURETYPE_2D:
378 maxsize = gl_max_texture_size;
380 case GLTEXTURETYPE_3D:
381 maxsize = gl_max_3d_texture_size;
383 case GLTEXTURETYPE_CUBEMAP:
384 maxsize = gl_max_cube_map_texture_size;
388 if (flags & TEXF_PICMIP)
390 maxsize = min(maxsize, gl_max_size.integer);
391 picmip = gl_picmip.integer;
396 for (width2 = 1;width2 < inwidth;width2 <<= 1);
397 for (width2 >>= picmip;width2 > maxsize;width2 >>= 1);
398 *outwidth = max(1, width2);
402 for (height2 = 1;height2 < inheight;height2 <<= 1);
403 for (height2 >>= picmip;height2 > maxsize;height2 >>= 1);
404 *outheight = max(1, height2);
408 for (depth2 = 1;depth2 < indepth;depth2 <<= 1);
409 for (depth2 >>= picmip;depth2 > maxsize;depth2 >>= 1);
410 *outdepth = max(1, depth2);
415 static int R_CalcTexelDataSize (gltexture_t *glt)
417 int width2, height2, depth2, size;
419 if (glt->flags & TEXF_FRAGMENT)
420 return glt->width * glt->height * glt->depth * glt->textype->internalbytesperpixel * glt->image->sides;
422 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->width, glt->height, glt->depth, &width2, &height2, &depth2);
424 size = width2 * height2 * depth2;
426 if (glt->flags & TEXF_MIPMAP)
428 while (width2 > 1 || height2 > 1 || depth2 > 1)
436 size += width2 * height2 * depth2;
440 return size * glt->textype->internalbytesperpixel * glt->image->sides;
443 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
447 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
448 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
450 gltexturepool_t *pool;
452 Con_Print("glsize input loaded mip alpha name\n");
453 for (pool = gltexturepoolchain;pool;pool = pool->next)
461 for (glt = pool->gltchain;glt;glt = glt->chain)
463 glsize = R_CalcTexelDataSize(glt);
464 isloaded = !(glt->flags & GLTEXF_UPLOAD);
466 pooltotalt += glsize;
467 pooltotalp += glt->inputdatasize;
471 poolloadedt += glsize;
472 poolloadedp += glt->inputdatasize;
475 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);
478 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);
479 sumtotal += pooltotal;
480 sumtotalt += pooltotalt;
481 sumtotalp += pooltotalp;
482 sumloaded += poolloaded;
483 sumloadedt += poolloadedt;
484 sumloadedp += poolloadedp;
487 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);
490 static void R_TextureStats_f(void)
492 R_TextureStats_Print(true, true, true);
495 static void r_textures_start(void)
497 // LordHavoc: allow any alignment
498 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
501 // use the largest scrap texture size we can (not sure if this is really a good idea)
502 for (block_size = 1;block_size * 2 <= gl_max_texture_size && block_size * 2 <= gl_max_scrapsize.integer;block_size *= 2);
504 texturemempool = Mem_AllocPool("texture management", 0, NULL);
506 // Disable JPEG screenshots if the DLL isn't loaded
507 if (! JPEG_OpenLibrary ())
508 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
511 static void r_textures_shutdown(void)
513 rtexturepool_t *temp;
515 JPEG_CloseLibrary ();
517 while(gltexturepoolchain)
519 temp = (rtexturepool_t *) gltexturepoolchain;
520 R_FreeTexturePool(&temp);
523 resizebuffersize = 0;
524 texturebuffersize = 0;
526 colorconvertbuffer = NULL;
527 texturebuffer = NULL;
528 Mem_FreePool(&texturemempool);
531 static void r_textures_newmap(void)
535 void R_Textures_Init (void)
537 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
538 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
539 Cvar_RegisterVariable (&gl_max_scrapsize);
540 Cvar_RegisterVariable (&gl_max_size);
541 Cvar_RegisterVariable (&gl_picmip);
542 Cvar_RegisterVariable (&r_lerpimages);
543 Cvar_RegisterVariable (&r_precachetextures);
544 Cvar_RegisterVariable (&gl_texture_anisotropy);
546 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
549 void R_Textures_Frame (void)
551 static int old_aniso = 0;
553 // could do procedural texture animation here, if we keep track of which
554 // textures were accessed this frame...
556 // free the resize buffers
557 resizebuffersize = 0;
560 Mem_Free(resizebuffer);
563 if (colorconvertbuffer)
565 Mem_Free(colorconvertbuffer);
566 colorconvertbuffer = NULL;
569 if (old_aniso != gl_texture_anisotropy.integer)
571 gltextureimage_t *image;
572 gltexturepool_t *pool;
575 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
577 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
579 for (pool = gltexturepoolchain;pool;pool = pool->next)
581 for (image = pool->imagechain;image;image = image->imagechain)
583 // only update already uploaded images
584 if (!(image->flags & GLTEXF_UPLOAD) && (image->flags & TEXF_MIPMAP))
586 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
588 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
589 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
591 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
598 void R_MakeResizeBufferBigger(int size)
600 if (resizebuffersize < size)
602 resizebuffersize = size;
604 Mem_Free(resizebuffer);
605 if (colorconvertbuffer)
606 Mem_Free(colorconvertbuffer);
607 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
608 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
609 if (!resizebuffer || !colorconvertbuffer)
610 Host_Error("R_Upload: out of memory");
614 static void GL_SetupTextureParameters(int flags, int texturetype)
616 int textureenum = gltexturetypeenums[texturetype];
617 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
621 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
623 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
624 if (gl_texture_anisotropy.integer != aniso)
625 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
626 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
628 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
629 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
630 if (gltexturetypedimensions[texturetype] >= 3)
632 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
636 if (flags & TEXF_FORCENEAREST)
638 if (flags & TEXF_MIPMAP)
640 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
644 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
646 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
648 else if (flags & TEXF_FORCELINEAR)
650 if (flags & TEXF_MIPMAP)
652 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
654 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
658 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
663 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
665 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
669 if (flags & TEXF_MIPMAP)
671 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
675 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
677 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
683 static void R_Upload(gltexture_t *glt, unsigned char *data)
685 int i, mip, width, height, depth;
687 unsigned char *prevbuffer;
692 glt->texnum = glt->image->texnum;
693 // we need to restore the texture binding after finishing the upload
694 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
695 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
697 glt->flags &= ~GLTEXF_UPLOAD;
699 if (glt->flags & TEXF_FRAGMENT)
701 if (glt->image->flags & GLTEXF_UPLOAD)
703 glt->image->flags &= ~GLTEXF_UPLOAD;
704 Con_DPrint("uploaded new fragments image\n");
705 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
706 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
707 switch(glt->image->texturetype)
709 case GLTEXTURETYPE_1D:
710 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
713 case GLTEXTURETYPE_2D:
714 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
717 case GLTEXTURETYPE_3D:
718 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);
722 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
725 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
728 if (prevbuffer == NULL)
730 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
731 memset(resizebuffer, 0, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
732 prevbuffer = resizebuffer;
734 else if (glt->textype->textype == TEXTYPE_PALETTE)
736 // promote paletted to RGBA, so we only have to worry about RGB and
737 // RGBA in the rest of this code
738 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
739 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
740 prevbuffer = colorconvertbuffer;
743 switch(glt->image->texturetype)
745 case GLTEXTURETYPE_1D:
746 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
749 case GLTEXTURETYPE_2D:
750 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
753 case GLTEXTURETYPE_3D:
754 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
758 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
764 glt->image->flags &= ~GLTEXF_UPLOAD;
766 // these are rounded up versions of the size to do better resampling
767 for (width = 1;width < glt->width ;width <<= 1);
768 for (height = 1;height < glt->height;height <<= 1);
769 for (depth = 1;depth < glt->depth ;depth <<= 1);
771 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
773 if (prevbuffer == NULL)
775 width = glt->image->width;
776 height = glt->image->height;
777 depth = glt->image->depth;
778 memset(resizebuffer, 0, width * height * depth * glt->image->bytesperpixel);
779 prevbuffer = resizebuffer;
783 if (glt->textype->textype == TEXTYPE_PALETTE)
785 // promote paletted to RGBA, so we only have to worry about RGB and
786 // RGBA in the rest of this code
787 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
788 prevbuffer = colorconvertbuffer;
792 // cubemaps contain multiple images and thus get processed a bit differently
793 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
795 if (glt->width != width || glt->height != height || glt->depth != depth)
797 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
798 prevbuffer = resizebuffer;
801 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
803 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
804 prevbuffer = resizebuffer;
808 switch(glt->image->texturetype)
810 case GLTEXTURETYPE_1D:
811 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
813 if (glt->flags & TEXF_MIPMAP)
815 while (width > 1 || height > 1 || depth > 1)
817 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
818 prevbuffer = resizebuffer;
819 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
824 case GLTEXTURETYPE_2D:
825 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
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->image->bytesperpixel);
832 prevbuffer = resizebuffer;
833 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
838 case GLTEXTURETYPE_3D:
839 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
841 if (glt->flags & TEXF_MIPMAP)
843 while (width > 1 || height > 1 || depth > 1)
845 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
846 prevbuffer = resizebuffer;
847 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
852 case GLTEXTURETYPE_CUBEMAP:
853 // convert and upload each side in turn,
854 // from a continuous block of input texels
855 texturebuffer = prevbuffer;
856 for (i = 0;i < 6;i++)
858 prevbuffer = texturebuffer;
859 texturebuffer += glt->width * glt->height * glt->depth * glt->textype->inputbytesperpixel;
860 if (glt->width != width || glt->height != height || glt->depth != depth)
862 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
863 prevbuffer = resizebuffer;
866 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
868 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
869 prevbuffer = resizebuffer;
872 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
874 if (glt->flags & TEXF_MIPMAP)
876 while (width > 1 || height > 1 || depth > 1)
878 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
879 prevbuffer = resizebuffer;
880 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
887 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
889 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
892 static void R_FindImageForTexture(gltexture_t *glt)
894 int i, j, best, best2, x, y, z, w, h, d;
895 textypeinfo_t *texinfo;
896 gltexturepool_t *pool;
897 gltextureimage_t *image, **imagechainpointer;
898 texinfo = glt->textype;
901 // remains -1 until uploaded
910 if (glt->flags & TEXF_FRAGMENT)
912 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
914 image = *imagechainpointer;
915 if (image->type != GLIMAGETYPE_FRAGMENTS)
917 if (image->texturetype != glt->texturetype)
919 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_FORCENEAREST | TEXF_FORCELINEAR))
921 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
923 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
926 // got a fragments texture, find a place in it if we can
927 for (best = image->width, i = 0;i < image->width - w;i++)
929 for (best2 = 0, j = 0;j < w;j++)
931 if (image->blockallocation[i+j] >= best)
933 if (best2 < image->blockallocation[i+j])
934 best2 = image->blockallocation[i+j];
938 // this is a valid spot
944 if (best + h > image->height)
947 for (i = 0;i < w;i++)
948 image->blockallocation[x + i] = best + h;
954 image->texturecount++;
958 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
961 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
964 image->type = GLIMAGETYPE_FRAGMENTS;
965 // make sure the created image is big enough for the fragment
966 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
968 if (gltexturetypedimensions[glt->texturetype] >= 2)
969 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
971 if (gltexturetypedimensions[glt->texturetype] >= 3)
972 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
973 image->blockallocation = (short int *)Mem_Alloc(texturemempool, image->width * sizeof(short));
974 memset(image->blockallocation, 0, image->width * sizeof(short));
979 for (i = 0;i < w;i++)
980 image->blockallocation[x + i] = y + h;
984 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
986 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
989 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
992 image->type = GLIMAGETYPE_TILE;
993 image->blockallocation = NULL;
995 GL_Texture_CalcImageSize(glt->texturetype, glt->flags, glt->width, glt->height, glt->depth, &image->width, &image->height, &image->depth);
997 image->texturetype = glt->texturetype;
998 image->glinternalformat = texinfo->glinternalformat;
999 image->glformat = texinfo->glformat;
1000 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_PICMIP | TEXF_FORCENEAREST | TEXF_FORCELINEAR)) | GLTEXF_UPLOAD;
1001 image->bytesperpixel = texinfo->internalbytesperpixel;
1002 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
1003 // get a texture number to use
1004 qglGenTextures(1, (GLuint *)&image->texnum);
1005 *imagechainpointer = image;
1006 image->texturecount++;
1014 // note: R_FindImageForTexture must be called before this
1015 static void R_UploadTexture (gltexture_t *glt)
1017 if (!(glt->flags & GLTEXF_UPLOAD))
1020 R_Upload(glt, glt->inputtexels);
1021 if (glt->inputtexels)
1023 Mem_Free(glt->inputtexels);
1024 glt->inputtexels = NULL;
1025 glt->flags |= GLTEXF_DESTROYED;
1027 else if (glt->flags & GLTEXF_DESTROYED)
1028 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1031 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)
1035 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1036 textypeinfo_t *texinfo;
1038 if (cls.state == ca_dedicated)
1041 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
1043 Con_Printf ("R_LoadTexture: only 2D fragment textures implemented\n");
1046 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1048 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
1051 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1053 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
1057 texinfo = R_GetTexTypeInfo(textype, flags);
1058 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1061 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
1065 // clear the alpha flag if the texture has no transparent pixels
1068 case TEXTYPE_PALETTE:
1069 if (flags & TEXF_ALPHA)
1071 flags &= ~TEXF_ALPHA;
1074 for (i = 0;i < size;i++)
1076 if (((unsigned char *)&palette[data[i]])[3] < 255)
1078 flags |= TEXF_ALPHA;
1086 if (flags & TEXF_ALPHA)
1087 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
1090 if (flags & TEXF_ALPHA)
1092 flags &= ~TEXF_ALPHA;
1095 for (i = 3;i < size;i += 4)
1099 flags |= TEXF_ALPHA;
1109 Host_Error("R_LoadTexture: unknown texture type");
1112 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1114 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1116 glt->chain = pool->gltchain;
1117 pool->gltchain = glt;
1119 glt->height = height;
1121 glt->flags = flags | GLTEXF_UPLOAD;
1122 glt->textype = texinfo;
1123 glt->texturetype = texturetype;
1124 glt->inputdatasize = size;
1125 glt->palette = palette;
1129 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1130 if (glt->inputtexels == NULL)
1131 Con_Printf ("R_LoadTexture: out of memory\n");
1133 memcpy(glt->inputtexels, data, size);
1136 glt->inputtexels = NULL;
1138 R_FindImageForTexture(glt);
1139 R_PrecacheTexture(glt);
1141 return (rtexture_t *)glt;
1144 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1146 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1149 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)
1151 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1154 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)
1156 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1159 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1161 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1164 int R_TextureHasAlpha(rtexture_t *rt)
1166 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1169 int R_TextureWidth(rtexture_t *rt)
1171 return rt ? ((gltexture_t *)rt)->width : 0;
1174 int R_TextureHeight(rtexture_t *rt)
1176 return rt ? ((gltexture_t *)rt)->height : 0;
1179 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1182 float iwidth, iheight, idepth;
1183 if (cls.state == ca_dedicated)
1191 if (fx1 || fy1 || fx2 || fy2)
1209 Host_Error("R_FragmentLocation: no texture supplied");
1210 glt = (gltexture_t *)rt;
1211 if (glt->flags & TEXF_FRAGMENT)
1217 if (fx1 || fy1 || fx2 || fy2)
1219 iwidth = 1.0f / glt->image->width;
1220 iheight = 1.0f / glt->image->height;
1221 idepth = 1.0f / glt->image->depth;
1223 *fx1 = glt->x * iwidth;
1225 *fy1 = glt->y * iheight;
1227 *fz1 = glt->z * idepth;
1229 *fx2 = (glt->x + glt->width) * iwidth;
1231 *fy2 = (glt->y + glt->height) * iheight;
1233 *fz2 = (glt->z + glt->depth) * idepth;
1244 if (fx1 || fy1 || fx2 || fy2)
1262 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1264 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1267 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1272 void R_UpdateTexture(rtexture_t *rt, unsigned char *data)
1276 Host_Error("R_UpdateTexture: no texture supplied");
1278 Host_Error("R_UpdateTexture: no data supplied");
1279 glt = (gltexture_t *)rt;
1281 // if it has not been uploaded yet, update the data that will be used when it is
1282 if (glt->inputtexels)
1283 memcpy(glt->inputtexels, data, glt->inputdatasize);
1285 R_Upload(glt, data);