4 cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"};
5 cvar_t r_max_scrapsize = {CVAR_SAVE, "r_max_scrapsize", "256"};
6 cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"};
7 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
8 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
10 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
11 int gl_filter_mag = GL_LINEAR;
14 static mempool_t *texturemempool;
15 static mempool_t *texturedatamempool;
16 static mempool_t *textureprocessingmempool;
18 // note: this must not conflict with TEXF_ flags in r_textures.h
19 // cleared when a texture is uploaded
20 #define GLTEXF_UPLOAD 0x00010000
21 // bitmask for mismatch checking
22 #define GLTEXF_IMPORTANTBITS (0)
23 // set when image is uploaded and freed
24 #define GLTEXF_DESTROYED 0x00040000
26 // size of images which hold fragment textures, ignores picmip and max_size
27 static int block_size;
29 // really this number only governs gltexnuminuse
30 #define MAX_GLTEXTURES 65536
32 // since there is only one set of GL texture numbers, we have to track them
33 // globally, everything else is per texture pool
34 static qbyte *gltexnuminuse;
39 int inputbytesperpixel;
40 int internalbytesperpixel;
47 static textypeinfo_t textype_qpalette = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3, 1};
48 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3, 3};
49 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3, 1};
50 static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4, 1};
51 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4, 1};
53 // a tiling texture (most common type)
54 #define GLIMAGETYPE_TILE 0
55 // a fragments texture (contains one or more fragment textures)
56 #define GLIMAGETYPE_FRAGMENTS 1
58 // a gltextureimage can have one (or more if fragments) gltextures inside
59 typedef struct gltextureimage_s
61 struct gltextureimage_s *imagechain;
63 int type; // one of the GLIMAGETYPE_ values
64 int texnum; // GL texture slot number
66 int bytesperpixel; // bytes per pixel
67 int glformat; // GL_RGB or GL_RGBA
68 int glinternalformat; // 3 or 4
70 short *blockallocation; // fragment allocation
74 typedef struct gltexture_s
76 // this field is exposed to the R_GetTexture macro, for speed reasons
77 // (must be identical in rtexture_t)
78 int texnum; // GL texture slot number
80 // pointer to texturepool (check this to see if the texture is allocated)
81 struct gltexturepool_s *pool;
82 // pointer to next texture in texturepool chain
83 struct gltexture_s *chain;
84 // pointer into gltextureimage array
85 gltextureimage_t *image;
86 // name of the texture (this might be removed someday), no duplicates
88 // location in the image, and size
89 int x, y, width, height;
90 // copy of the original texture supplied to the upload function, for re-uploading or deferred uploads (non-precached)
92 // to identify cache mismatchs (this might be removed someday)
94 // flags supplied to the LoadTexture function
95 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
97 // pointer to one of the textype_ structs
98 textypeinfo_t *textype;
102 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
104 typedef struct gltexturepool_s
107 struct gltextureimage_s *imagechain;
108 struct gltexture_s *gltchain;
109 struct gltexturepool_s *next;
113 static gltexturepool_t *gltexturepoolchain = NULL;
115 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
116 static int resizebuffersize = 0;
117 static qbyte *texturebuffer;
118 static int texturebuffersize = 0;
120 static int realmaxsize = 0;
122 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
124 if (flags & TEXF_ALPHA)
128 case TEXTYPE_QPALETTE:
129 return &textype_qpalette_alpha;
131 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
134 return &textype_rgba_alpha;
136 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
144 case TEXTYPE_QPALETTE:
145 return &textype_qpalette;
149 return &textype_rgba;
151 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
157 static void R_UploadTexture(gltexture_t *t);
159 static void R_PrecacheTexture(gltexture_t *glt)
163 if (glt->flags & TEXF_ALWAYSPRECACHE)
165 else if (r_precachetextures.integer >= 2)
167 else if (r_precachetextures.integer >= 1)
168 if (glt->flags & TEXF_PRECACHE)
172 R_UploadTexture(glt);
175 int R_RealGetTexture(rtexture_t *rt)
180 glt = (gltexture_t *)rt;
181 if (glt->flags & GLTEXF_UPLOAD)
182 R_UploadTexture(glt);
183 glt->texnum = glt->image->texnum;
184 return glt->image->texnum;
190 void R_FreeTexture(rtexture_t *rt)
192 gltexture_t *glt, **gltpointer;
193 gltextureimage_t *image, **gltimagepointer;
196 glt = (gltexture_t *)rt;
198 Host_Error("R_FreeTexture: texture == NULL\n");
200 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
201 if (*gltpointer == glt)
202 *gltpointer = glt->chain;
204 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
206 // note: if freeing a fragment texture, this will not make the claimed
207 // space available for new textures unless all other fragments in the
208 // image are also freed
210 image->texturecount--;
211 if (image->texturecount < 1)
213 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
214 if (*gltimagepointer == image)
215 *gltimagepointer = image->imagechain;
217 Host_Error("R_FreeTexture: image not linked in pool\n");
220 texnum = image->texnum;
221 gltexnuminuse[image->texnum] = 0;
222 qglDeleteTextures(1, &texnum);
224 if (image->blockallocation)
225 Mem_Free(image->blockallocation);
230 Mem_Free(glt->identifier);
231 if (glt->inputtexels)
232 Mem_Free(glt->inputtexels);
236 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
243 for (glt = pool->gltchain;glt;glt = glt->chain)
244 if (glt->identifier && !strcmp (identifier, glt->identifier))
250 rtexturepool_t *R_AllocTexturePool(void)
252 gltexturepool_t *pool;
253 if (texturemempool == NULL)
255 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
258 pool->next = gltexturepoolchain;
259 gltexturepoolchain = pool;
260 pool->sentinel = TEXTUREPOOL_SENTINEL;
261 return (rtexturepool_t *)pool;
264 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
266 gltexturepool_t *pool, **poolpointer;
267 if (rtexturepool == NULL)
269 if (*rtexturepool == NULL)
271 pool = (gltexturepool_t *)(*rtexturepool);
272 *rtexturepool = NULL;
273 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
274 Host_Error("R_FreeTexturePool: pool already freed\n");
275 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
276 if (*poolpointer == pool)
277 *poolpointer = pool->next;
279 Host_Error("R_FreeTexturePool: pool not linked\n");
280 while (pool->gltchain)
281 R_FreeTexture((rtexture_t *)pool->gltchain);
282 if (pool->imagechain)
283 Sys_Error("R_FreeTexturePool: not all images freed\n");
291 int minification, magnification;
295 static glmode_t modes[] =
297 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
298 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
299 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
300 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
301 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
302 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
305 extern int gl_backend_rebindtextures;
307 static void GL_TextureMode_f (void)
310 gltextureimage_t *image;
311 gltexturepool_t *pool;
315 for (i = 0;i < 6;i++)
317 if (gl_filter_min == modes[i].minification)
319 Con_Printf ("%s\n", modes[i].name);
323 Con_Printf ("current filter is unknown???\n");
327 for (i = 0;i < 6;i++)
328 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
332 Con_Printf ("bad filter name\n");
336 gl_filter_min = modes[i].minification;
337 gl_filter_mag = modes[i].magnification;
339 // change all the existing mipmap texture objects
340 // FIXME: force renderer(/client/something?) restart instead?
341 for (pool = gltexturepoolchain;pool;pool = pool->next)
343 for (image = pool->imagechain;image;image = image->imagechain)
345 // only update already uploaded images
346 if (!(image->flags & GLTEXF_UPLOAD))
348 qglBindTexture(GL_TEXTURE_2D, image->texnum);
349 if (image->flags & TEXF_MIPMAP)
350 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
352 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
353 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
357 gl_backend_rebindtextures = true;
360 static int R_CalcTexelDataSize (gltexture_t *glt)
362 int width2, height2, size;
363 if (glt->flags & TEXF_FRAGMENT)
364 size = glt->width * glt->height;
367 if (r_max_size.integer > realmaxsize)
368 Cvar_SetValue("r_max_size", realmaxsize);
369 // calculate final size
370 for (width2 = 1;width2 < glt->width;width2 <<= 1);
371 for (height2 = 1;height2 < glt->height;height2 <<= 1);
372 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
373 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
374 if (width2 < 1) width2 = 1;
375 if (height2 < 1) height2 = 1;
378 if (glt->flags & TEXF_MIPMAP)
380 while (width2 > 1 || height2 > 1)
382 size += width2 * height2;
388 size++; // count the last 1x1 mipmap
391 size = width2*height2;
393 size *= glt->textype->internalbytesperpixel;
398 void R_TextureStats_PrintTotal(void)
400 int glsize, inputsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
402 gltexturepool_t *pool;
403 for (pool = gltexturepoolchain;pool;pool = pool->next)
405 for (glt = pool->gltchain;glt;glt = glt->chain)
407 glsize = R_CalcTexelDataSize(glt);
408 inputsize = glt->width * glt->height * glt->textype->inputbytesperpixel;
413 if (!(glt->flags & GLTEXF_UPLOAD))
417 loadedp += inputsize;
421 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);
424 static void R_TextureStats_f(void)
428 gltexturepool_t *pool;
429 Con_Printf("glsize input crc loaded mip alpha name\n");
430 for (pool = gltexturepoolchain;pool;pool = pool->next)
432 for (glt = pool->gltchain;glt;glt = glt->chain)
434 loaded = !(glt->flags & GLTEXF_UPLOAD);
435 Con_Printf("%c%4i%c%c%4i%c %04X %s %s %s %s\n", loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', glt->inputtexels ? '[' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, glt->inputtexels ? ']' : ' ', glt->crc, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : "<unnamed>");
437 Con_Printf("pool %10p\n", pool);
439 R_TextureStats_PrintTotal();
442 char engineversion[40];
444 static void r_textures_start(void)
446 // deal with size limits of various drivers (3dfx in particular)
447 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
450 // use the largest scrap texture size we can (not sure if this is really a good idea)
451 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
453 texturemempool = Mem_AllocPool("Texture Info");
454 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
455 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
456 gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
459 static void r_textures_shutdown(void)
461 rtexturepool_t *temp;
462 while(gltexturepoolchain)
464 temp = (rtexturepool_t *) gltexturepoolchain;
465 R_FreeTexturePool(&temp);
468 resizebuffersize = 0;
469 texturebuffersize = 0;
471 colorconvertbuffer = NULL;
472 texturebuffer = NULL;
473 gltexnuminuse = NULL;
474 Mem_FreePool(&texturemempool);
475 Mem_FreePool(&texturedatamempool);
476 Mem_FreePool(&textureprocessingmempool);
479 static void r_textures_newmap(void)
483 void R_Textures_Init (void)
485 Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
486 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
487 Cvar_RegisterVariable (&r_max_scrapsize);
488 Cvar_RegisterVariable (&r_max_size);
489 Cvar_RegisterVariable (&r_picmip);
490 Cvar_RegisterVariable (&r_lerpimages);
491 Cvar_RegisterVariable (&r_precachetextures);
492 gltexnuminuse = NULL;
494 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
497 static void R_Upload(gltexture_t *glt, qbyte *data)
499 int mip, width, height, internalformat;
503 qglBindTexture(GL_TEXTURE_2D, glt->image->texnum);
506 gl_backend_rebindtextures = true;
508 glt->flags &= ~GLTEXF_UPLOAD;
510 if (glt->flags & TEXF_FRAGMENT)
512 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
514 resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
516 Mem_Free(resizebuffer);
517 if (colorconvertbuffer)
518 Mem_Free(colorconvertbuffer);
519 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
520 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
521 if (!resizebuffer || !colorconvertbuffer)
522 Host_Error("R_Upload: out of memory\n");
525 if (glt->image->flags & GLTEXF_UPLOAD)
527 Con_DPrintf("uploaded new fragments image\n");
528 glt->image->flags &= ~GLTEXF_UPLOAD;
529 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
530 qglTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
532 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
534 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
538 if (prevbuffer == NULL)
540 memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
541 prevbuffer = resizebuffer;
543 else if (glt->textype->textype == TEXTYPE_QPALETTE)
545 // promote paletted to RGBA, so we only have to worry about RGB and
546 // RGBA in the rest of this code
547 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
548 prevbuffer = colorconvertbuffer;
551 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
553 glt->texnum = glt->image->texnum;
557 glt->image->flags &= ~GLTEXF_UPLOAD;
559 // these are rounded up versions of the size to do better resampling
560 for (width = 1;width < glt->width;width <<= 1);
561 for (height = 1;height < glt->height;height <<= 1);
563 if (resizebuffersize < width * height * glt->image->bytesperpixel)
565 resizebuffersize = width * height * glt->image->bytesperpixel;
567 Mem_Free(resizebuffer);
568 if (colorconvertbuffer)
569 Mem_Free(colorconvertbuffer);
570 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
571 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
572 if (!resizebuffer || !colorconvertbuffer)
573 Host_Error("R_Upload: out of memory\n");
576 if (prevbuffer == NULL)
578 width = glt->image->width;
579 height = glt->image->height;
580 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
581 prevbuffer = resizebuffer;
585 if (glt->textype->textype == TEXTYPE_QPALETTE)
587 // promote paletted to RGBA, so we only have to worry about RGB and
588 // RGBA in the rest of this code
589 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
590 prevbuffer = colorconvertbuffer;
593 if (glt->width != width || glt->height != height)
595 Image_Resample(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel, r_lerpimages.integer);
596 prevbuffer = resizebuffer;
599 // apply picmip/max_size limitations
600 while (width > glt->image->width || height > glt->image->height)
602 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
603 prevbuffer = resizebuffer;
607 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
609 if (glt->flags & TEXF_ALPHA)
613 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
615 if (glt->flags & TEXF_MIPMAP)
617 while (width > 1 || height > 1)
619 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
620 prevbuffer = resizebuffer;
622 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
626 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
628 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
633 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
635 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
638 glt->texnum = glt->image->texnum;
641 static void R_FindImageForTexture(gltexture_t *glt)
643 int i, j, best, best2, x, y, w, h;
644 textypeinfo_t *texinfo;
645 gltexturepool_t *pool;
646 gltextureimage_t *image, **imagechainpointer;
647 texinfo = glt->textype;
650 // remains -1 until uploaded
657 if (glt->flags & TEXF_FRAGMENT)
659 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
661 image = *imagechainpointer;
662 if (image->type != GLIMAGETYPE_FRAGMENTS)
664 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
666 if (glt->width > image->width || glt->height > image->height)
669 // got a fragments texture, find a place in it if we can
670 for (best = image->width, i = 0;i < image->width - w;i += texinfo->align)
672 for (best2 = 0, j = 0;j < w;j++)
674 if (image->blockallocation[i+j] >= best)
676 if (best2 < image->blockallocation[i+j])
677 best2 = image->blockallocation[i+j];
681 // this is a valid spot
687 if (best + h > image->height)
690 for (i = 0;i < w;i++)
691 image->blockallocation[x + i] = best + h;
696 image->texturecount++;
700 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
702 Sys_Error("R_FindImageForTexture: ran out of memory\n");
703 image->type = GLIMAGETYPE_FRAGMENTS;
704 // make sure the created image is big enough for the fragment
705 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
706 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
707 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
708 memset(image->blockallocation, 0, image->width * sizeof(short));
712 for (i = 0;i < w;i++)
713 image->blockallocation[x + i] = y + h;
717 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
719 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
721 Sys_Error("R_FindImageForTexture: ran out of memory\n");
722 image->type = GLIMAGETYPE_TILE;
723 image->blockallocation = NULL;
725 // calculate final size
726 if (r_max_size.integer > realmaxsize)
727 Cvar_SetValue("r_max_size", realmaxsize);
728 for (image->width = 1;image->width < glt->width;image->width <<= 1);
729 for (image->height = 1;image->height < glt->height;image->height <<= 1);
730 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
731 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
732 if (image->width < 1) image->width = 1;
733 if (image->height < 1) image->height = 1;
735 image->glinternalformat = texinfo->glinternalformat;
736 image->glformat = texinfo->glformat;
737 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
738 image->bytesperpixel = texinfo->internalbytesperpixel;
739 for (i = 1;i < MAX_GLTEXTURES;i++)
740 if (!gltexnuminuse[i])
742 if (i < MAX_GLTEXTURES)
743 gltexnuminuse[image->texnum = i] = true;
745 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
746 *imagechainpointer = image;
747 image->texturecount++;
754 // note: R_FindImageForTexture must be called before this
755 static void R_UploadTexture (gltexture_t *glt)
757 if (!(glt->flags & GLTEXF_UPLOAD))
760 R_Upload(glt, glt->inputtexels);
761 if (glt->inputtexels)
763 Mem_Free(glt->inputtexels);
764 glt->inputtexels = NULL;
765 glt->flags |= GLTEXF_DESTROYED;
767 else if (glt->flags & GLTEXF_DESTROYED)
768 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
771 static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, qbyte *data)
774 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
777 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
778 strcpy (glt->identifier, identifier);
781 glt->identifier = NULL;
783 glt->chain = pool->gltchain;
784 pool->gltchain = glt;
787 glt->height = height;
789 glt->textype = texinfo;
793 glt->inputtexels = Mem_Alloc(texturedatamempool, glt->width * glt->height * texinfo->inputbytesperpixel);
794 if (glt->inputtexels == NULL)
795 Sys_Error("R_SetupTexture: out of memory\n");
796 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
799 glt->inputtexels = NULL;
801 R_FindImageForTexture(glt);
802 R_PrecacheTexture(glt);
812 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
816 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
817 textypeinfo_t *texinfo;
820 if (cls.state == ca_dedicated)
823 texinfo = R_GetTexTypeInfo(textype, flags);
825 if (flags & TEXF_FRAGMENT)
826 if ((width * texinfo->internalbytesperpixel) & 3)
827 Host_Error("R_LoadTexture: incompatible width for fragment");
829 // clear the alpha flag if the texture has no transparent pixels
832 case TEXTYPE_QPALETTE:
833 if (flags & TEXF_ALPHA)
835 flags &= ~TEXF_ALPHA;
836 for (i = 0;i < width * height;i++)
847 if (flags & TEXF_ALPHA)
848 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
851 if (flags & TEXF_ALPHA)
853 flags &= ~TEXF_ALPHA;
854 for (i = 0;i < width * height;i++)
856 if (data[i * 4 + 3] < 255)
865 Host_Error("R_LoadTexture: unknown texture type\n");
868 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
872 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
874 // see if the texture is already present
875 if (identifier && (glt = R_FindTexture(pool, identifier)))
877 if (crc == glt->crc && width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
879 Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier);
880 return (rtexture_t *)glt; // exact match, use existing
882 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
883 R_FreeTexture((rtexture_t *)glt);
886 return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data);
889 int R_TextureHasAlpha(rtexture_t *rt)
894 glt = (gltexture_t *)rt;
895 return (glt->flags & TEXF_ALPHA) != 0;
898 int R_TextureWidth(rtexture_t *rt)
902 return ((gltexture_t *)rt)->width;
905 int R_TextureHeight(rtexture_t *rt)
909 return ((gltexture_t *)rt)->height;
912 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
915 float iwidth, iheight;
916 if (cls.state == ca_dedicated)
922 if (fx1 || fy1 || fx2 || fy2)
936 Host_Error("R_FragmentLocation: no texture supplied\n");
937 glt = (gltexture_t *)rt;
938 if (glt->flags & TEXF_FRAGMENT)
944 if (fx1 || fy1 || fx2 || fy2)
946 iwidth = 1.0f / glt->image->width;
947 iheight = 1.0f / glt->image->height;
949 *fx1 = glt->x * iwidth;
951 *fy1 = glt->y * iheight;
953 *fx2 = (glt->x + glt->width) * iwidth;
955 *fy2 = (glt->y + glt->height) * iheight;
964 if (fx1 || fy1 || fx2 || fy2)
978 int R_CompatibleFragmentWidth(int width, int textype, int flags)
980 textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
981 while ((width * texinfo->internalbytesperpixel) & 3)
986 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
990 Host_Error("R_UpdateTexture: no texture supplied\n");
992 Host_Error("R_UpdateTexture: no data supplied\n");
993 glt = (gltexture_t *)rt;
995 // if it has not been uploaded yet, update the data that will be used when it is
996 if (glt->inputtexels)
997 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);