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 int realmaxsize = 0;
141 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
143 if ((flags & (TEXF_PICMIP | TEXF_FRAGMENT)) == (TEXF_PICMIP | TEXF_FRAGMENT))
145 Host_Error("R_GetTexTypeInfo: TEXF_PICMIP can not be used with TEXF_FRAGMENT");
148 if (flags & TEXF_ALPHA)
152 case TEXTYPE_PALETTE:
153 return &textype_palette_alpha;
155 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
158 return &textype_rgba_alpha;
160 Host_Error("R_GetTexTypeInfo: unknown texture format");
168 case TEXTYPE_PALETTE:
169 return &textype_palette;
173 return &textype_rgba;
175 return &textype_dsdt;
177 Host_Error("R_GetTexTypeInfo: unknown texture format");
183 static void R_UploadTexture(gltexture_t *t);
185 static void R_PrecacheTexture(gltexture_t *glt)
189 if (glt->flags & TEXF_ALWAYSPRECACHE)
191 else if (r_precachetextures.integer >= 2)
193 else if (r_precachetextures.integer >= 1)
194 if (glt->flags & TEXF_PRECACHE)
198 R_UploadTexture(glt);
201 int R_RealGetTexture(rtexture_t *rt)
206 glt = (gltexture_t *)rt;
207 if (glt->flags & GLTEXF_UPLOAD)
208 R_UploadTexture(glt);
209 glt->texnum = glt->image->texnum;
210 return glt->image->texnum;
216 void R_FreeTexture(rtexture_t *rt)
218 gltexture_t *glt, **gltpointer;
219 gltextureimage_t *image, **gltimagepointer;
221 glt = (gltexture_t *)rt;
223 Host_Error("R_FreeTexture: texture == NULL");
225 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
226 if (*gltpointer == glt)
227 *gltpointer = glt->chain;
229 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool", glt->identifier);
231 // note: if freeing a fragment texture, this will not make the claimed
232 // space available for new textures unless all other fragments in the
233 // image are also freed
237 image->texturecount--;
238 if (image->texturecount < 1)
240 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
241 if (*gltimagepointer == image)
242 *gltimagepointer = image->imagechain;
244 Host_Error("R_FreeTexture: image not linked in pool");
246 qglDeleteTextures(1, (GLuint *)&image->texnum);
247 if (image->blockallocation)
248 Mem_Free(image->blockallocation);
253 if (glt->inputtexels)
254 Mem_Free(glt->inputtexels);
258 rtexturepool_t *R_AllocTexturePool(void)
260 gltexturepool_t *pool;
261 if (texturemempool == NULL)
263 pool = (gltexturepool_t *)Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
266 pool->next = gltexturepoolchain;
267 gltexturepoolchain = pool;
268 pool->sentinel = TEXTUREPOOL_SENTINEL;
269 return (rtexturepool_t *)pool;
272 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
274 gltexturepool_t *pool, **poolpointer;
275 if (rtexturepool == NULL)
277 if (*rtexturepool == NULL)
279 pool = (gltexturepool_t *)(*rtexturepool);
280 *rtexturepool = NULL;
281 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
282 Host_Error("R_FreeTexturePool: pool already freed");
283 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
284 if (*poolpointer == pool)
285 *poolpointer = pool->next;
287 Host_Error("R_FreeTexturePool: pool not linked");
288 while (pool->gltchain)
289 R_FreeTexture((rtexture_t *)pool->gltchain);
290 if (pool->imagechain)
291 Con_Printf("R_FreeTexturePool: not all images freed\n");
296 typedef struct glmode_s
299 int minification, magnification;
303 static glmode_t modes[] =
305 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
306 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
307 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
308 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
309 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
310 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
313 static void GL_TextureMode_f (void)
317 gltextureimage_t *image;
318 gltexturepool_t *pool;
322 for (i = 0;i < 6;i++)
324 if (gl_filter_min == modes[i].minification)
326 Con_Printf("%s\n", modes[i].name);
330 Con_Print("current filter is unknown???\n");
334 for (i = 0;i < 6;i++)
335 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
339 Con_Print("bad filter name\n");
343 gl_filter_min = modes[i].minification;
344 gl_filter_mag = modes[i].magnification;
346 // change all the existing mipmap texture objects
347 // FIXME: force renderer(/client/something?) restart instead?
348 for (pool = gltexturepoolchain;pool;pool = pool->next)
350 for (image = pool->imagechain;image;image = image->imagechain)
352 // only update already uploaded images
353 if (!(image->flags & GLTEXF_UPLOAD) && !(image->flags & (TEXF_FORCENEAREST | TEXF_FORCELINEAR)))
355 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
356 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
357 if (image->flags & TEXF_MIPMAP)
358 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_min);
360 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MIN_FILTER, gl_filter_mag);
361 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAG_FILTER, gl_filter_mag);
362 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
368 static int R_CalcTexelDataSize (gltexture_t *glt)
370 int width2, height2, depth2, size, picmip;
371 if (glt->flags & TEXF_FRAGMENT)
372 size = glt->width * glt->height * glt->depth;
376 if (glt->flags & TEXF_PICMIP)
377 picmip = gl_picmip.integer;
378 if (gl_max_size.integer > realmaxsize)
379 Cvar_SetValue("gl_max_size", realmaxsize);
380 // calculate final size
381 for (width2 = 1;width2 < glt->width;width2 <<= 1);
382 for (height2 = 1;height2 < glt->height;height2 <<= 1);
383 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
384 for (width2 >>= picmip;width2 > gl_max_size.integer;width2 >>= 1);
385 for (height2 >>= picmip;height2 > gl_max_size.integer;height2 >>= 1);
386 for (depth2 >>= picmip;depth2 > gl_max_size.integer;depth2 >>= 1);
387 if (width2 < 1) width2 = 1;
388 if (height2 < 1) height2 = 1;
389 if (depth2 < 1) depth2 = 1;
392 if (glt->flags & TEXF_MIPMAP)
394 while (width2 > 1 || height2 > 1 || depth2 > 1)
396 size += width2 * height2 * depth2;
404 size++; // count the last 1x1 mipmap
407 size = width2 * height2 * depth2;
409 size *= glt->textype->internalbytesperpixel * glt->image->sides;
414 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
418 int pooltotal = 0, pooltotalt = 0, pooltotalp = 0, poolloaded = 0, poolloadedt = 0, poolloadedp = 0;
419 int sumtotal = 0, sumtotalt = 0, sumtotalp = 0, sumloaded = 0, sumloadedt = 0, sumloadedp = 0;
421 gltexturepool_t *pool;
423 Con_Print("glsize input loaded mip alpha name\n");
424 for (pool = gltexturepoolchain;pool;pool = pool->next)
432 for (glt = pool->gltchain;glt;glt = glt->chain)
434 glsize = R_CalcTexelDataSize(glt);
435 isloaded = !(glt->flags & GLTEXF_UPLOAD);
437 pooltotalt += glsize;
438 pooltotalp += glt->inputdatasize;
442 poolloadedt += glsize;
443 poolloadedp += glt->inputdatasize;
446 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);
449 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);
450 sumtotal += pooltotal;
451 sumtotalt += pooltotalt;
452 sumtotalp += pooltotalp;
453 sumloaded += poolloaded;
454 sumloadedt += poolloadedt;
455 sumloadedp += poolloadedp;
458 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);
461 static void R_TextureStats_f(void)
463 R_TextureStats_Print(true, true, true);
466 static void r_textures_start(void)
468 // deal with size limits of various drivers (3dfx in particular)
469 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
471 // LordHavoc: allow any alignment
472 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
475 // use the largest scrap texture size we can (not sure if this is really a good idea)
476 for (block_size = 1;block_size < realmaxsize && block_size < gl_max_scrapsize.integer;block_size <<= 1);
478 texturemempool = Mem_AllocPool("texture management", 0, NULL);
480 // Disable JPEG screenshots if the DLL isn't loaded
481 if (! JPEG_OpenLibrary ())
482 Cvar_SetValueQuick (&scr_screenshot_jpeg, 0);
485 static void r_textures_shutdown(void)
487 rtexturepool_t *temp;
489 JPEG_CloseLibrary ();
491 while(gltexturepoolchain)
493 temp = (rtexturepool_t *) gltexturepoolchain;
494 R_FreeTexturePool(&temp);
497 resizebuffersize = 0;
498 texturebuffersize = 0;
500 colorconvertbuffer = NULL;
501 texturebuffer = NULL;
502 Mem_FreePool(&texturemempool);
505 static void r_textures_newmap(void)
509 void R_Textures_Init (void)
511 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
512 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
513 Cvar_RegisterVariable (&gl_max_scrapsize);
514 Cvar_RegisterVariable (&gl_max_size);
515 Cvar_RegisterVariable (&gl_picmip);
516 Cvar_RegisterVariable (&r_lerpimages);
517 Cvar_RegisterVariable (&r_precachetextures);
518 Cvar_RegisterVariable (&gl_texture_anisotropy);
520 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
523 void R_Textures_Frame (void)
525 static int old_aniso = 0;
527 // could do procedural texture animation here, if we keep track of which
528 // textures were accessed this frame...
530 // free the resize buffers
531 resizebuffersize = 0;
534 Mem_Free(resizebuffer);
537 if (colorconvertbuffer)
539 Mem_Free(colorconvertbuffer);
540 colorconvertbuffer = NULL;
543 if (old_aniso != gl_texture_anisotropy.integer)
545 gltextureimage_t *image;
546 gltexturepool_t *pool;
549 old_aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
551 Cvar_SetValueQuick(&gl_texture_anisotropy, old_aniso);
553 for (pool = gltexturepoolchain;pool;pool = pool->next)
555 for (image = pool->imagechain;image;image = image->imagechain)
557 // only update already uploaded images
558 if (!(image->flags & GLTEXF_UPLOAD) && (image->flags & TEXF_MIPMAP))
560 qglGetIntegerv(gltexturetypebindingenums[image->texturetype], &oldbindtexnum);
562 qglBindTexture(gltexturetypeenums[image->texturetype], image->texnum);
563 qglTexParameteri(gltexturetypeenums[image->texturetype], GL_TEXTURE_MAX_ANISOTROPY_EXT, old_aniso);CHECKGLERROR
565 qglBindTexture(gltexturetypeenums[image->texturetype], oldbindtexnum);
572 void R_MakeResizeBufferBigger(int size)
574 if (resizebuffersize < size)
576 resizebuffersize = size;
578 Mem_Free(resizebuffer);
579 if (colorconvertbuffer)
580 Mem_Free(colorconvertbuffer);
581 resizebuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
582 colorconvertbuffer = (unsigned char *)Mem_Alloc(texturemempool, resizebuffersize);
583 if (!resizebuffer || !colorconvertbuffer)
584 Host_Error("R_Upload: out of memory");
588 static void GL_SetupTextureParameters(int flags, int texturetype)
590 int textureenum = gltexturetypeenums[texturetype];
591 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
595 if (gl_support_anisotropy && (flags & TEXF_MIPMAP))
597 int aniso = bound(1, gl_texture_anisotropy.integer, gl_max_anisotropy);
598 if (gl_texture_anisotropy.integer != aniso)
599 Cvar_SetValueQuick(&gl_texture_anisotropy, aniso);
600 qglTexParameteri(textureenum, GL_TEXTURE_MAX_ANISOTROPY_EXT, aniso);CHECKGLERROR
602 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);CHECKGLERROR
603 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);CHECKGLERROR
604 if (gltexturetypedimensions[texturetype] >= 3)
606 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);CHECKGLERROR
610 if (flags & TEXF_FORCENEAREST)
612 if (flags & TEXF_MIPMAP)
614 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);CHECKGLERROR
618 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);CHECKGLERROR
620 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);CHECKGLERROR
622 else if (flags & TEXF_FORCELINEAR)
624 if (flags & TEXF_MIPMAP)
626 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
628 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);CHECKGLERROR
632 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);CHECKGLERROR
637 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);CHECKGLERROR
639 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);CHECKGLERROR
643 if (flags & TEXF_MIPMAP)
645 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);CHECKGLERROR
649 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);CHECKGLERROR
651 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);CHECKGLERROR
657 static void R_Upload(gltexture_t *glt, unsigned char *data)
659 int i, mip, width, height, depth;
661 unsigned char *prevbuffer;
666 glt->texnum = glt->image->texnum;
667 // we need to restore the texture binding after finishing the upload
668 qglGetIntegerv(gltexturetypebindingenums[glt->image->texturetype], &oldbindtexnum);
669 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
671 glt->flags &= ~GLTEXF_UPLOAD;
673 if (glt->flags & TEXF_FRAGMENT)
675 if (glt->image->flags & GLTEXF_UPLOAD)
677 glt->image->flags &= ~GLTEXF_UPLOAD;
678 Con_DPrint("uploaded new fragments image\n");
679 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
680 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
681 switch(glt->image->texturetype)
683 case GLTEXTURETYPE_1D:
684 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
687 case GLTEXTURETYPE_2D:
688 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
691 case GLTEXTURETYPE_3D:
692 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);
696 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
699 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
702 if (prevbuffer == NULL)
704 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
705 memset(resizebuffer, 0, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
706 prevbuffer = resizebuffer;
708 else if (glt->textype->textype == TEXTYPE_PALETTE)
710 // promote paletted to RGBA, so we only have to worry about RGB and
711 // RGBA in the rest of this code
712 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
713 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
714 prevbuffer = colorconvertbuffer;
717 switch(glt->image->texturetype)
719 case GLTEXTURETYPE_1D:
720 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
723 case GLTEXTURETYPE_2D:
724 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
727 case GLTEXTURETYPE_3D:
728 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
732 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D");
738 glt->image->flags &= ~GLTEXF_UPLOAD;
740 // these are rounded up versions of the size to do better resampling
741 for (width = 1;width < glt->width ;width <<= 1);
742 for (height = 1;height < glt->height;height <<= 1);
743 for (depth = 1;depth < glt->depth ;depth <<= 1);
745 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
747 if (prevbuffer == NULL)
749 width = glt->image->width;
750 height = glt->image->height;
751 depth = glt->image->depth;
752 memset(resizebuffer, 0, width * height * depth * glt->image->bytesperpixel);
753 prevbuffer = resizebuffer;
757 if (glt->textype->textype == TEXTYPE_PALETTE)
759 // promote paletted to RGBA, so we only have to worry about RGB and
760 // RGBA in the rest of this code
761 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
762 prevbuffer = colorconvertbuffer;
766 // cubemaps contain multiple images and thus get processed a bit differently
767 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
769 if (glt->width != width || glt->height != height || glt->depth != depth)
771 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
772 prevbuffer = resizebuffer;
775 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
777 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
778 prevbuffer = resizebuffer;
782 switch(glt->image->texturetype)
784 case GLTEXTURETYPE_1D:
785 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
787 if (glt->flags & TEXF_MIPMAP)
789 while (width > 1 || height > 1 || depth > 1)
791 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
792 prevbuffer = resizebuffer;
793 qglTexImage1D(GL_TEXTURE_1D, mip++, glt->image->glinternalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
798 case GLTEXTURETYPE_2D:
799 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
801 if (glt->flags & TEXF_MIPMAP)
803 while (width > 1 || height > 1 || depth > 1)
805 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
806 prevbuffer = resizebuffer;
807 qglTexImage2D(GL_TEXTURE_2D, mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
812 case GLTEXTURETYPE_3D:
813 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
815 if (glt->flags & TEXF_MIPMAP)
817 while (width > 1 || height > 1 || depth > 1)
819 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
820 prevbuffer = resizebuffer;
821 qglTexImage3D(GL_TEXTURE_3D, mip++, glt->image->glinternalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
826 case GLTEXTURETYPE_CUBEMAP:
827 // convert and upload each side in turn,
828 // from a continuous block of input texels
829 texturebuffer = prevbuffer;
830 for (i = 0;i < 6;i++)
832 prevbuffer = texturebuffer;
833 texturebuffer += glt->width * glt->height * glt->depth * glt->textype->inputbytesperpixel;
834 if (glt->width != width || glt->height != height || glt->depth != depth)
836 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
837 prevbuffer = resizebuffer;
840 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
842 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
843 prevbuffer = resizebuffer;
846 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
848 if (glt->flags & TEXF_MIPMAP)
850 while (width > 1 || height > 1 || depth > 1)
852 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
853 prevbuffer = resizebuffer;
854 qglTexImage2D(cubemapside[i], mip++, glt->image->glinternalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
861 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
863 qglBindTexture(gltexturetypeenums[glt->image->texturetype], oldbindtexnum);
866 static void R_FindImageForTexture(gltexture_t *glt)
868 int i, j, best, best2, x, y, z, w, h, d, picmip;
869 textypeinfo_t *texinfo;
870 gltexturepool_t *pool;
871 gltextureimage_t *image, **imagechainpointer;
872 texinfo = glt->textype;
875 // remains -1 until uploaded
884 if (glt->flags & TEXF_FRAGMENT)
886 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
888 image = *imagechainpointer;
889 if (image->type != GLIMAGETYPE_FRAGMENTS)
891 if (image->texturetype != glt->texturetype)
893 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_FORCENEAREST | TEXF_FORCELINEAR))
895 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
897 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
900 // got a fragments texture, find a place in it if we can
901 for (best = image->width, i = 0;i < image->width - w;i++)
903 for (best2 = 0, j = 0;j < w;j++)
905 if (image->blockallocation[i+j] >= best)
907 if (best2 < image->blockallocation[i+j])
908 best2 = image->blockallocation[i+j];
912 // this is a valid spot
918 if (best + h > image->height)
921 for (i = 0;i < w;i++)
922 image->blockallocation[x + i] = best + h;
928 image->texturecount++;
932 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
935 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
938 image->type = GLIMAGETYPE_FRAGMENTS;
939 // make sure the created image is big enough for the fragment
940 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
942 if (gltexturetypedimensions[glt->texturetype] >= 2)
943 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
945 if (gltexturetypedimensions[glt->texturetype] >= 3)
946 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
947 image->blockallocation = (short int *)Mem_Alloc(texturemempool, image->width * sizeof(short));
948 memset(image->blockallocation, 0, image->width * sizeof(short));
953 for (i = 0;i < w;i++)
954 image->blockallocation[x + i] = y + h;
958 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
960 image = (gltextureimage_t *)Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
963 Con_Printf ("R_FindImageForTexture: ran out of memory\n");
966 image->type = GLIMAGETYPE_TILE;
967 image->blockallocation = NULL;
970 if (glt->flags & TEXF_PICMIP)
971 picmip = gl_picmip.integer;
972 // calculate final size
973 if (gl_max_size.integer > realmaxsize)
974 Cvar_SetValue("gl_max_size", realmaxsize);
975 for (image->width = 1;image->width < glt->width;image->width <<= 1);
976 for (image->height = 1;image->height < glt->height;image->height <<= 1);
977 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
978 for (image->width >>= picmip;image->width > gl_max_size.integer;image->width >>= 1);
979 for (image->height >>= picmip;image->height > gl_max_size.integer;image->height >>= 1);
980 for (image->depth >>= picmip;image->depth > gl_max_size.integer;image->depth >>= 1);
981 if (image->width < 1) image->width = 1;
982 if (image->height < 1) image->height = 1;
983 if (image->depth < 1) image->depth = 1;
985 image->texturetype = glt->texturetype;
986 image->glinternalformat = texinfo->glinternalformat;
987 image->glformat = texinfo->glformat;
988 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP | TEXF_PICMIP | TEXF_FORCENEAREST | TEXF_FORCELINEAR)) | GLTEXF_UPLOAD;
989 image->bytesperpixel = texinfo->internalbytesperpixel;
990 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
991 // get a texture number to use
992 qglGenTextures(1, (GLuint *)&image->texnum);
993 *imagechainpointer = image;
994 image->texturecount++;
1002 // note: R_FindImageForTexture must be called before this
1003 static void R_UploadTexture (gltexture_t *glt)
1005 if (!(glt->flags & GLTEXF_UPLOAD))
1008 R_Upload(glt, glt->inputtexels);
1009 if (glt->inputtexels)
1011 Mem_Free(glt->inputtexels);
1012 glt->inputtexels = NULL;
1013 glt->flags |= GLTEXF_DESTROYED;
1015 else if (glt->flags & GLTEXF_DESTROYED)
1016 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1019 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)
1023 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1024 textypeinfo_t *texinfo;
1026 if (cls.state == ca_dedicated)
1029 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
1031 Con_Printf ("R_LoadTexture: only 2D fragment textures implemented\n");
1034 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
1036 Con_Printf ("R_LoadTexture: cubemap texture not supported by driver\n");
1039 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
1041 Con_Printf ("R_LoadTexture: 3d texture not supported by driver\n");
1045 texinfo = R_GetTexTypeInfo(textype, flags);
1046 size = width * height * depth * sides * texinfo->inputbytesperpixel;
1049 Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
1053 // clear the alpha flag if the texture has no transparent pixels
1056 case TEXTYPE_PALETTE:
1057 if (flags & TEXF_ALPHA)
1059 flags &= ~TEXF_ALPHA;
1062 for (i = 0;i < size;i++)
1064 if (((unsigned char *)&palette[data[i]])[3] < 255)
1066 flags |= TEXF_ALPHA;
1074 if (flags & TEXF_ALPHA)
1075 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA");
1078 if (flags & TEXF_ALPHA)
1080 flags &= ~TEXF_ALPHA;
1083 for (i = 3;i < size;i += 4)
1087 flags |= TEXF_ALPHA;
1097 Host_Error("R_LoadTexture: unknown texture type");
1100 glt = (gltexture_t *)Mem_Alloc(texturemempool, sizeof(gltexture_t));
1102 strlcpy (glt->identifier, identifier, sizeof(glt->identifier));
1104 glt->chain = pool->gltchain;
1105 pool->gltchain = glt;
1107 glt->height = height;
1109 glt->flags = flags | GLTEXF_UPLOAD;
1110 glt->textype = texinfo;
1111 glt->texturetype = texturetype;
1112 glt->inputdatasize = size;
1113 glt->palette = palette;
1117 glt->inputtexels = (unsigned char *)Mem_Alloc(texturemempool, size);
1118 if (glt->inputtexels == NULL)
1119 Con_Printf ("R_LoadTexture: out of memory\n");
1121 memcpy(glt->inputtexels, data, size);
1124 glt->inputtexels = NULL;
1126 R_FindImageForTexture(glt);
1127 R_PrecacheTexture(glt);
1129 return (rtexture_t *)glt;
1132 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const unsigned char *data, int textype, int flags, const unsigned int *palette)
1134 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1137 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)
1139 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1142 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)
1144 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1147 rtexture_t *R_LoadTextureCubeMap(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, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1152 int R_TextureHasAlpha(rtexture_t *rt)
1154 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1157 int R_TextureWidth(rtexture_t *rt)
1159 return rt ? ((gltexture_t *)rt)->width : 0;
1162 int R_TextureHeight(rtexture_t *rt)
1164 return rt ? ((gltexture_t *)rt)->height : 0;
1167 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1170 float iwidth, iheight, idepth;
1171 if (cls.state == ca_dedicated)
1179 if (fx1 || fy1 || fx2 || fy2)
1197 Host_Error("R_FragmentLocation: no texture supplied");
1198 glt = (gltexture_t *)rt;
1199 if (glt->flags & TEXF_FRAGMENT)
1205 if (fx1 || fy1 || fx2 || fy2)
1207 iwidth = 1.0f / glt->image->width;
1208 iheight = 1.0f / glt->image->height;
1209 idepth = 1.0f / glt->image->depth;
1211 *fx1 = glt->x * iwidth;
1213 *fy1 = glt->y * iheight;
1215 *fz1 = glt->z * idepth;
1217 *fx2 = (glt->x + glt->width) * iwidth;
1219 *fy2 = (glt->y + glt->height) * iheight;
1221 *fz2 = (glt->z + glt->depth) * idepth;
1232 if (fx1 || fy1 || fx2 || fy2)
1250 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1252 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1255 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1260 void R_UpdateTexture(rtexture_t *rt, unsigned char *data)
1264 Host_Error("R_UpdateTexture: no texture supplied");
1266 Host_Error("R_UpdateTexture: no data supplied");
1267 glt = (gltexture_t *)rt;
1269 // if it has not been uploaded yet, update the data that will be used when it is
1270 if (glt->inputtexels)
1271 memcpy(glt->inputtexels, data, glt->inputdatasize);
1273 R_Upload(glt, data);