6 cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"};
7 cvar_t r_max_scrapsize = {CVAR_SAVE, "r_max_scrapsize", "256"};
8 cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"};
9 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
10 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
12 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
13 int gl_filter_mag = GL_LINEAR;
16 static mempool_t *texturemempool;
17 static mempool_t *texturedatamempool;
18 static mempool_t *textureprocessingmempool;
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;
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};
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 gltexturetypedimensions[4] = {1, 2, 3, 2};
59 static int cubemapside[6] =
61 GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB,
62 GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB,
63 GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB,
64 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB,
65 GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB,
66 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
69 // a gltextureimage can have one (or more if fragments) gltextures inside
70 typedef struct gltextureimage_s
72 struct gltextureimage_s *imagechain;
74 int type; // one of the GLIMAGETYPE_ values
75 int texturetype; // one of the GLTEXTURETYPE_ values
76 int sides; // 1 or 6 depending on texturetype
77 int texnum; // GL texture slot number
78 int width, height, depth; // 3D texture support
79 int bytesperpixel; // bytes per pixel
80 int glformat; // GL_RGB or GL_RGBA
81 int glinternalformat; // 3 or 4
83 short *blockallocation; // fragment allocation (2D only)
87 typedef struct gltexture_s
89 // this field is exposed to the R_GetTexture macro, for speed reasons
90 // (must be identical in rtexture_t)
91 int texnum; // GL texture slot number
93 // pointer to texturepool (check this to see if the texture is allocated)
94 struct gltexturepool_s *pool;
95 // pointer to next texture in texturepool chain
96 struct gltexture_s *chain;
97 // pointer into gltextureimage array
98 gltextureimage_t *image;
99 // name of the texture (this might be removed someday), no duplicates
101 // location in the image, and size
102 int x, y, z, width, height, depth;
103 // copy of the original texture(s) supplied to the upload function, for
104 // delayed uploads (non-precached)
106 // original data size in *inputtexels
108 // flags supplied to the LoadTexture function
109 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
111 // pointer to one of the textype_ structs
112 textypeinfo_t *textype;
113 // one of the GLTEXTURETYPE_ values
115 // palette if the texture is TEXTYPE_PALETTE
116 const unsigned int *palette;
120 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
122 typedef struct gltexturepool_s
124 unsigned int sentinel;
125 struct gltextureimage_s *imagechain;
126 struct gltexture_s *gltchain;
127 struct gltexturepool_s *next;
131 static gltexturepool_t *gltexturepoolchain = NULL;
133 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
134 static int resizebuffersize = 0;
135 static qbyte *texturebuffer;
136 static int texturebuffersize = 0;
138 static int realmaxsize = 0;
140 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
142 if (flags & TEXF_ALPHA)
146 case TEXTYPE_PALETTE:
147 return &textype_palette_alpha;
149 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
152 return &textype_rgba_alpha;
154 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
162 case TEXTYPE_PALETTE:
163 return &textype_palette;
167 return &textype_rgba;
169 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
175 static void R_UploadTexture(gltexture_t *t);
177 static void R_PrecacheTexture(gltexture_t *glt)
181 if (glt->flags & TEXF_ALWAYSPRECACHE)
183 else if (r_precachetextures.integer >= 2)
185 else if (r_precachetextures.integer >= 1)
186 if (glt->flags & TEXF_PRECACHE)
190 R_UploadTexture(glt);
193 int R_RealGetTexture(rtexture_t *rt)
198 glt = (gltexture_t *)rt;
199 if (glt->flags & GLTEXF_UPLOAD)
200 R_UploadTexture(glt);
201 glt->texnum = glt->image->texnum;
202 return glt->image->texnum;
208 void R_FreeTexture(rtexture_t *rt)
210 gltexture_t *glt, **gltpointer;
211 gltextureimage_t *image, **gltimagepointer;
213 glt = (gltexture_t *)rt;
215 Host_Error("R_FreeTexture: texture == NULL\n");
217 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
218 if (*gltpointer == glt)
219 *gltpointer = glt->chain;
221 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
223 // note: if freeing a fragment texture, this will not make the claimed
224 // space available for new textures unless all other fragments in the
225 // image are also freed
229 image->texturecount--;
230 if (image->texturecount < 1)
232 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
233 if (*gltimagepointer == image)
234 *gltimagepointer = image->imagechain;
236 Host_Error("R_FreeTexture: image not linked in pool\n");
238 qglDeleteTextures(1, &image->texnum);
239 if (image->blockallocation)
240 Mem_Free(image->blockallocation);
246 Mem_Free(glt->identifier);
247 if (glt->inputtexels)
248 Mem_Free(glt->inputtexels);
253 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
260 for (glt = pool->gltchain;glt;glt = glt->chain)
261 if (glt->identifier && !strcmp (identifier, glt->identifier))
268 rtexturepool_t *R_AllocTexturePool(void)
270 gltexturepool_t *pool;
271 if (texturemempool == NULL)
273 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
276 pool->next = gltexturepoolchain;
277 gltexturepoolchain = pool;
278 pool->sentinel = TEXTUREPOOL_SENTINEL;
279 return (rtexturepool_t *)pool;
282 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
284 gltexturepool_t *pool, **poolpointer;
285 if (rtexturepool == NULL)
287 if (*rtexturepool == NULL)
289 pool = (gltexturepool_t *)(*rtexturepool);
290 *rtexturepool = NULL;
291 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
292 Host_Error("R_FreeTexturePool: pool already freed\n");
293 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
294 if (*poolpointer == pool)
295 *poolpointer = pool->next;
297 Host_Error("R_FreeTexturePool: pool not linked\n");
298 while (pool->gltchain)
299 R_FreeTexture((rtexture_t *)pool->gltchain);
300 if (pool->imagechain)
301 Sys_Error("R_FreeTexturePool: not all images freed\n");
309 int minification, magnification;
313 static glmode_t modes[] =
315 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
316 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
317 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
318 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
319 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
320 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
323 extern int gl_backend_rebindtextures;
325 static void GL_TextureMode_f (void)
328 gltextureimage_t *image;
329 gltexturepool_t *pool;
333 for (i = 0;i < 6;i++)
335 if (gl_filter_min == modes[i].minification)
337 Con_Printf ("%s\n", modes[i].name);
341 Con_Printf ("current filter is unknown???\n");
345 for (i = 0;i < 6;i++)
346 if (!strcasecmp (modes[i].name, Cmd_Argv(1) ) )
350 Con_Printf ("bad filter name\n");
354 gl_filter_min = modes[i].minification;
355 gl_filter_mag = modes[i].magnification;
357 // change all the existing mipmap texture objects
358 // FIXME: force renderer(/client/something?) restart instead?
359 for (pool = gltexturepoolchain;pool;pool = pool->next)
361 for (image = pool->imagechain;image;image = image->imagechain)
363 // only update already uploaded images
364 if (!(image->flags & GLTEXF_UPLOAD))
366 qglBindTexture(GL_TEXTURE_2D, image->texnum);
367 if (image->flags & TEXF_MIPMAP)
368 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
370 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
371 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
377 static int R_CalcTexelDataSize (gltexture_t *glt)
379 int width2, height2, depth2, size;
380 if (glt->flags & TEXF_FRAGMENT)
381 size = glt->width * glt->height * glt->depth;
384 if (r_max_size.integer > realmaxsize)
385 Cvar_SetValue("r_max_size", realmaxsize);
386 // calculate final size
387 for (width2 = 1;width2 < glt->width;width2 <<= 1);
388 for (height2 = 1;height2 < glt->height;height2 <<= 1);
389 for (depth2 = 1;depth2 < glt->depth;depth2 <<= 1);
390 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
391 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
392 for (depth2 >>= r_picmip.integer;depth2 > r_max_size.integer;depth2 >>= 1);
393 if (width2 < 1) width2 = 1;
394 if (height2 < 1) height2 = 1;
395 if (depth2 < 1) depth2 = 1;
398 if (glt->flags & TEXF_MIPMAP)
400 while (width2 > 1 || height2 > 1 || depth2 > 1)
402 size += width2 * height2 * depth2;
410 size++; // count the last 1x1 mipmap
413 size = width2 * height2 * depth2;
415 size *= glt->textype->internalbytesperpixel * glt->image->sides;
420 void R_TextureStats_PrintTotal(void)
422 int glsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
424 gltexturepool_t *pool;
425 for (pool = gltexturepoolchain;pool;pool = pool->next)
427 for (glt = pool->gltchain;glt;glt = glt->chain)
429 glsize = R_CalcTexelDataSize(glt);
432 totalp += glt->inputdatasize;
433 if (!(glt->flags & GLTEXF_UPLOAD))
437 loadedp += glt->inputdatasize;
441 Con_Printf("total: %i (%.3fMB, %.3fMB original), uploaded %i (%.3fMB, %.3fMB original), upload on demand %i (%.3fMB, %.3fMB original)\n", total, totalt / 1048576.0, totalp / 1048576.0, loaded, loadedt / 1048576.0, loadedp / 1048576.0, total - loaded, (totalt - loadedt) / 1048576.0, (totalp - loadedp) / 1048576.0);
444 static void R_TextureStats_f(void)
448 gltexturepool_t *pool;
449 Con_Printf("glsize input loaded mip alpha name\n");
450 for (pool = gltexturepoolchain;pool;pool = pool->next)
452 for (glt = pool->gltchain;glt;glt = glt->chain)
454 loaded = !(glt->flags & GLTEXF_UPLOAD);
455 Con_Printf("%c%4i%c%c%4i%c %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->inputdatasize + 1023) / 1024, glt->inputtexels ? ']' : ' ', loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : "<unnamed>");
457 Con_Printf("pool %10p\n", pool);
459 R_TextureStats_PrintTotal();
462 char engineversion[40];
464 static void r_textures_start(void)
466 // deal with size limits of various drivers (3dfx in particular)
467 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
469 // LordHavoc: allow any alignment
470 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
473 // use the largest scrap texture size we can (not sure if this is really a good idea)
474 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
476 texturemempool = Mem_AllocPool("Texture Info");
477 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
478 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
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);
503 Mem_FreePool(&texturedatamempool);
504 Mem_FreePool(&textureprocessingmempool);
507 static void r_textures_newmap(void)
511 void R_Textures_Init (void)
513 Cmd_AddCommand("gl_texturemode", &GL_TextureMode_f);
514 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
515 Cvar_RegisterVariable (&r_max_scrapsize);
516 Cvar_RegisterVariable (&r_max_size);
517 Cvar_RegisterVariable (&r_picmip);
518 Cvar_RegisterVariable (&r_lerpimages);
519 Cvar_RegisterVariable (&r_precachetextures);
521 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
524 void R_Textures_Frame (void)
526 // could do procedural texture animation here, if we keep track of which
527 // textures were accessed this frame...
529 // free the resize buffers
530 resizebuffersize = 0;
533 Mem_Free(resizebuffer);
536 if (colorconvertbuffer)
538 Mem_Free(colorconvertbuffer);
539 colorconvertbuffer = NULL;
543 void R_MakeResizeBufferBigger(int size)
545 if (resizebuffersize < size)
547 resizebuffersize = size;
549 Mem_Free(resizebuffer);
550 if (colorconvertbuffer)
551 Mem_Free(colorconvertbuffer);
552 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
553 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
554 if (!resizebuffer || !colorconvertbuffer)
555 Host_Error("R_Upload: out of memory\n");
559 static void GL_SetupTextureParameters(int flags, int texturetype)
561 int textureenum = gltexturetypeenums[texturetype];
562 int wrapmode = ((flags & TEXF_CLAMP) && gl_support_clamptoedge) ? GL_CLAMP_TO_EDGE : GL_REPEAT;
566 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_S, wrapmode);
567 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_T, wrapmode);
568 if (gltexturetypedimensions[texturetype] >= 3)
569 qglTexParameteri(textureenum, GL_TEXTURE_WRAP_R, wrapmode);
571 if (flags & TEXF_FORCENEAREST)
573 if (flags & TEXF_MIPMAP)
574 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
576 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
577 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
579 else if (flags & TEXF_FORCELINEAR)
581 if (flags & TEXF_MIPMAP)
583 if (gl_filter_min == GL_NEAREST_MIPMAP_LINEAR || gl_filter_min == GL_LINEAR_MIPMAP_LINEAR)
584 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
586 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
589 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
590 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
594 if (flags & TEXF_MIPMAP)
595 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_min);
597 qglTexParameteri(textureenum, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
598 qglTexParameteri(textureenum, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
604 static void R_Upload(gltexture_t *glt, qbyte *data)
606 int i, mip, width, height, depth, internalformat;
614 glt->texnum = glt->image->texnum;
615 qglBindTexture(gltexturetypeenums[glt->image->texturetype], glt->image->texnum);
617 glt->flags &= ~GLTEXF_UPLOAD;
618 gl_backend_rebindtextures = true;
620 if (glt->flags & TEXF_FRAGMENT)
622 if (glt->image->flags & GLTEXF_UPLOAD)
624 glt->image->flags &= ~GLTEXF_UPLOAD;
625 Con_DPrintf("uploaded new fragments image\n");
626 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
627 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
628 switch(glt->image->texturetype)
630 case GLTEXTURETYPE_1D:
631 qglTexImage1D(GL_TEXTURE_1D, 0, glt->image->glinternalformat, glt->image->width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
634 case GLTEXTURETYPE_2D:
635 qglTexImage2D(GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
638 case GLTEXTURETYPE_3D:
639 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);
643 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
646 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
649 if (prevbuffer == NULL)
651 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->bytesperpixel);
652 memset(resizebuffer, 255, glt->width * glt->height * glt->image->depth * glt->image->bytesperpixel);
653 prevbuffer = resizebuffer;
655 else if (glt->textype->textype == TEXTYPE_PALETTE)
657 // promote paletted to RGBA, so we only have to worry about RGB and
658 // RGBA in the rest of this code
659 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->depth * glt->image->sides * glt->image->bytesperpixel);
660 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth, glt->palette);
661 prevbuffer = colorconvertbuffer;
664 switch(glt->image->texturetype)
666 case GLTEXTURETYPE_1D:
667 qglTexSubImage1D(GL_TEXTURE_1D, 0, glt->x, glt->width, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
670 case GLTEXTURETYPE_2D:
671 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
674 case GLTEXTURETYPE_3D:
675 qglTexSubImage3D(GL_TEXTURE_3D, 0, glt->x, glt->y, glt->z, glt->width, glt->height, glt->depth, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
679 Host_Error("R_Upload: fragment texture of type other than 1D, 2D, or 3D\n");
682 glt->texnum = glt->image->texnum;
686 glt->image->flags &= ~GLTEXF_UPLOAD;
688 // these are rounded up versions of the size to do better resampling
689 for (width = 1;width < glt->width ;width <<= 1);
690 for (height = 1;height < glt->height;height <<= 1);
691 for (depth = 1;depth < glt->depth ;depth <<= 1);
693 R_MakeResizeBufferBigger(width * height * depth * glt->image->sides * glt->image->bytesperpixel);
695 if (prevbuffer == NULL)
697 width = glt->image->width;
698 height = glt->image->height;
699 depth = glt->image->depth;
700 memset(resizebuffer, 255, width * height * depth * glt->image->bytesperpixel);
701 prevbuffer = resizebuffer;
705 if (glt->textype->textype == TEXTYPE_PALETTE)
707 // promote paletted to RGBA, so we only have to worry about RGB and
708 // RGBA in the rest of this code
709 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height * glt->depth * glt->image->sides, glt->palette);
710 prevbuffer = colorconvertbuffer;
714 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
716 if (glt->flags & TEXF_ALPHA)
719 // cubemaps contain multiple images and thus get processed a bit differently
720 if (glt->image->texturetype != GLTEXTURETYPE_CUBEMAP)
722 if (glt->width != width || glt->height != height || glt->depth != depth)
724 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
725 prevbuffer = resizebuffer;
728 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
730 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
731 prevbuffer = resizebuffer;
735 switch(glt->image->texturetype)
737 case GLTEXTURETYPE_1D:
738 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
740 if (glt->flags & TEXF_MIPMAP)
742 while (width > 1 || height > 1 || depth > 1)
744 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
745 prevbuffer = resizebuffer;
746 qglTexImage1D(GL_TEXTURE_1D, mip++, internalformat, width, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
751 case GLTEXTURETYPE_2D:
752 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
754 if (glt->flags & TEXF_MIPMAP)
756 while (width > 1 || height > 1 || depth > 1)
758 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
759 prevbuffer = resizebuffer;
760 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
765 case GLTEXTURETYPE_3D:
766 qglTexImage3D(GL_TEXTURE_3D, mip++, internalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
768 if (glt->flags & TEXF_MIPMAP)
770 while (width > 1 || height > 1 || depth > 1)
772 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, 1, 1, 1, glt->image->bytesperpixel);
773 prevbuffer = resizebuffer;
774 qglTexImage3D(GL_TEXTURE_3D, mip++, internalformat, width, height, depth, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
779 case GLTEXTURETYPE_CUBEMAP:
780 // convert and upload each side in turn,
781 // from a continuous block of input texels
782 texturebuffer = prevbuffer;
783 for (i = 0;i < 6;i++)
785 prevbuffer = texturebuffer;
786 texturebuffer += width * height * depth * glt->textype->inputbytesperpixel;
787 if (glt->width != width || glt->height != height || glt->depth != depth)
789 Image_Resample(prevbuffer, glt->width, glt->height, glt->depth, resizebuffer, width, height, depth, glt->image->bytesperpixel, r_lerpimages.integer);
790 prevbuffer = resizebuffer;
793 while (width > glt->image->width || height > glt->image->height || depth > glt->image->depth)
795 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, &depth, glt->image->width, glt->image->height, glt->image->depth, glt->image->bytesperpixel);
796 prevbuffer = resizebuffer;
799 qglTexImage2D(cubemapside[i], mip++, internalformat, 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(cubemapside[i], mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
814 GL_SetupTextureParameters(glt->image->flags, glt->image->texturetype);
817 static void R_FindImageForTexture(gltexture_t *glt)
819 int i, j, best, best2, x, y, z, w, h, d;
820 textypeinfo_t *texinfo;
821 gltexturepool_t *pool;
822 gltextureimage_t *image, **imagechainpointer;
823 texinfo = glt->textype;
826 // remains -1 until uploaded
835 if (glt->flags & TEXF_FRAGMENT)
837 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
839 image = *imagechainpointer;
840 if (image->type != GLIMAGETYPE_FRAGMENTS)
842 if (image->texturetype != glt->texturetype)
844 if ((image->flags ^ glt->flags) & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP))
846 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
848 if (glt->width > image->width || glt->height > image->height || glt->depth > image->depth)
851 // got a fragments texture, find a place in it if we can
852 for (best = image->width, i = 0;i < image->width - w;i++)
854 for (best2 = 0, j = 0;j < w;j++)
856 if (image->blockallocation[i+j] >= best)
858 if (best2 < image->blockallocation[i+j])
859 best2 = image->blockallocation[i+j];
863 // this is a valid spot
869 if (best + h > image->height)
872 for (i = 0;i < w;i++)
873 image->blockallocation[x + i] = best + h;
879 image->texturecount++;
883 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
885 Sys_Error("R_FindImageForTexture: ran out of memory\n");
886 image->type = GLIMAGETYPE_FRAGMENTS;
887 // make sure the created image is big enough for the fragment
888 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
890 if (gltexturetypedimensions[glt->texturetype] >= 2)
891 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
893 if (gltexturetypedimensions[glt->texturetype] >= 3)
894 for (image->depth = block_size;image->depth < glt->depth;image->depth <<= 1);
895 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
896 memset(image->blockallocation, 0, image->width * sizeof(short));
901 for (i = 0;i < w;i++)
902 image->blockallocation[x + i] = y + h;
906 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
908 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
910 Sys_Error("R_FindImageForTexture: ran out of memory\n");
911 image->type = GLIMAGETYPE_TILE;
912 image->blockallocation = NULL;
914 // calculate final size
915 if (r_max_size.integer > realmaxsize)
916 Cvar_SetValue("r_max_size", realmaxsize);
917 for (image->width = 1;image->width < glt->width;image->width <<= 1);
918 for (image->height = 1;image->height < glt->height;image->height <<= 1);
919 for (image->depth = 1;image->depth < glt->depth;image->depth <<= 1);
920 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
921 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
922 for (image->depth >>= r_picmip.integer;image->depth > r_max_size.integer;image->depth >>= 1);
923 if (image->width < 1) image->width = 1;
924 if (image->height < 1) image->height = 1;
925 if (image->depth < 1) image->depth = 1;
927 image->texturetype = glt->texturetype;
928 image->glinternalformat = texinfo->glinternalformat;
929 image->glformat = texinfo->glformat;
930 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA | TEXF_CLAMP)) | GLTEXF_UPLOAD;
931 image->bytesperpixel = texinfo->internalbytesperpixel;
932 image->sides = image->texturetype == GLTEXTURETYPE_CUBEMAP ? 6 : 1;
933 // get a texture number to use
934 qglGenTextures(1, &image->texnum);
935 *imagechainpointer = image;
936 image->texturecount++;
944 // note: R_FindImageForTexture must be called before this
945 static void R_UploadTexture (gltexture_t *glt)
947 if (!(glt->flags & GLTEXF_UPLOAD))
950 R_Upload(glt, glt->inputtexels);
951 if (glt->inputtexels)
953 Mem_Free(glt->inputtexels);
954 glt->inputtexels = NULL;
955 glt->flags |= GLTEXF_DESTROYED;
957 else if (glt->flags & GLTEXF_DESTROYED)
958 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
961 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 qbyte *data, const unsigned int *palette)
965 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
966 textypeinfo_t *texinfo;
968 if (cls.state == ca_dedicated)
971 if (flags & TEXF_FRAGMENT && texturetype != GLTEXTURETYPE_2D)
972 Sys_Error("R_LoadTexture: only 2D fragment textures implemented\n");
973 if (texturetype == GLTEXTURETYPE_CUBEMAP && !gl_texturecubemap)
974 Sys_Error("R_LoadTexture: cubemap texture not supported by driver\n");
975 if (texturetype == GLTEXTURETYPE_3D && !gl_texture3d)
976 Sys_Error("R_LoadTexture: 3d texture not supported by driver\n");
979 glt = R_FindTexture (pool, identifier);
982 Con_Printf("R_LoadTexture: replacing existing texture %s\n", identifier);
983 R_FreeTexture((rtexture_t *)glt);
987 texinfo = R_GetTexTypeInfo(textype, flags);
988 size = width * height * depth * sides * texinfo->inputbytesperpixel;
990 Sys_Error("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
992 // clear the alpha flag if the texture has no transparent pixels
995 case TEXTYPE_PALETTE:
996 if (flags & TEXF_ALPHA)
998 flags &= ~TEXF_ALPHA;
1001 for (i = 0;i < size;i++)
1003 if (((qbyte *)&palette[data[i]])[3] < 255)
1005 flags |= TEXF_ALPHA;
1013 if (flags & TEXF_ALPHA)
1014 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1017 if (flags & TEXF_ALPHA)
1019 flags &= ~TEXF_ALPHA;
1022 for (i = 3;i < size;i += 4)
1026 flags |= TEXF_ALPHA;
1034 Host_Error("R_LoadTexture: unknown texture type\n");
1037 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1040 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1041 strcpy (glt->identifier, identifier);
1044 glt->identifier = NULL;
1046 glt->chain = pool->gltchain;
1047 pool->gltchain = glt;
1049 glt->height = height;
1051 glt->flags = flags | GLTEXF_UPLOAD;
1052 glt->textype = texinfo;
1053 glt->texturetype = texturetype;
1054 glt->inputdatasize = size;
1055 glt->palette = palette;
1059 glt->inputtexels = Mem_Alloc(texturedatamempool, size);
1060 if (glt->inputtexels == NULL)
1061 Sys_Error("R_LoadTexture: out of memory\n");
1062 memcpy(glt->inputtexels, data, size);
1065 glt->inputtexels = NULL;
1067 R_FindImageForTexture(glt);
1068 R_PrecacheTexture(glt);
1070 return (rtexture_t *)glt;
1073 rtexture_t *R_LoadTexture1D(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1075 return R_SetupTexture(rtexturepool, identifier, width, 1, 1, 1, flags, textype, GLTEXTURETYPE_1D, data, palette);
1078 rtexture_t *R_LoadTexture2D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, const qbyte *data, int textype, int flags, const unsigned int *palette)
1080 return R_SetupTexture(rtexturepool, identifier, width, height, 1, 1, flags, textype, GLTEXTURETYPE_2D, data, palette);
1083 rtexture_t *R_LoadTexture3D(rtexturepool_t *rtexturepool, const char *identifier, int width, int height, int depth, const qbyte *data, int textype, int flags, const unsigned int *palette)
1085 return R_SetupTexture(rtexturepool, identifier, width, height, depth, 1, flags, textype, GLTEXTURETYPE_3D, data, palette);
1088 rtexture_t *R_LoadTextureCubeMap(rtexturepool_t *rtexturepool, const char *identifier, int width, const qbyte *data, int textype, int flags, const unsigned int *palette)
1090 return R_SetupTexture(rtexturepool, identifier, width, width, 1, 6, flags, textype, GLTEXTURETYPE_CUBEMAP, data, palette);
1093 int R_TextureHasAlpha(rtexture_t *rt)
1095 return rt ? (((gltexture_t *)rt)->flags & TEXF_ALPHA) != 0 : false;
1098 int R_TextureWidth(rtexture_t *rt)
1100 return rt ? ((gltexture_t *)rt)->width : 0;
1103 int R_TextureHeight(rtexture_t *rt)
1105 return rt ? ((gltexture_t *)rt)->height : 0;
1108 void R_FragmentLocation3D(rtexture_t *rt, int *x, int *y, int *z, float *fx1, float *fy1, float *fz1, float *fx2, float *fy2, float *fz2)
1111 float iwidth, iheight, idepth;
1112 if (cls.state == ca_dedicated)
1120 if (fx1 || fy1 || fx2 || fy2)
1138 Host_Error("R_FragmentLocation: no texture supplied\n");
1139 glt = (gltexture_t *)rt;
1140 if (glt->flags & TEXF_FRAGMENT)
1146 if (fx1 || fy1 || fx2 || fy2)
1148 iwidth = 1.0f / glt->image->width;
1149 iheight = 1.0f / glt->image->height;
1150 idepth = 1.0f / glt->image->depth;
1152 *fx1 = glt->x * iwidth;
1154 *fy1 = glt->y * iheight;
1156 *fz1 = glt->z * idepth;
1158 *fx2 = (glt->x + glt->width) * iwidth;
1160 *fy2 = (glt->y + glt->height) * iheight;
1162 *fz2 = (glt->z + glt->depth) * idepth;
1173 if (fx1 || fy1 || fx2 || fy2)
1191 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1193 R_FragmentLocation3D(rt, x, y, NULL, fx1, fy1, NULL, fx2, fy2, NULL);
1196 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1201 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1205 Host_Error("R_UpdateTexture: no texture supplied\n");
1207 Host_Error("R_UpdateTexture: no data supplied\n");
1208 glt = (gltexture_t *)rt;
1210 // if it has not been uploaded yet, update the data that will be used when it is
1211 if (glt->inputtexels)
1212 memcpy(glt->inputtexels, data, glt->inputdatasize);
1214 R_Upload(glt, data);