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, 1};
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);
449 // LordHavoc: allow any alignment
450 qglPixelStorei(GL_UNPACK_ALIGNMENT, 1);
453 // use the largest scrap texture size we can (not sure if this is really a good idea)
454 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
456 texturemempool = Mem_AllocPool("Texture Info");
457 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
458 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
459 gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
462 static void r_textures_shutdown(void)
464 rtexturepool_t *temp;
465 while(gltexturepoolchain)
467 temp = (rtexturepool_t *) gltexturepoolchain;
468 R_FreeTexturePool(&temp);
471 resizebuffersize = 0;
472 texturebuffersize = 0;
474 colorconvertbuffer = NULL;
475 texturebuffer = NULL;
476 gltexnuminuse = NULL;
477 Mem_FreePool(&texturemempool);
478 Mem_FreePool(&texturedatamempool);
479 Mem_FreePool(&textureprocessingmempool);
482 static void r_textures_newmap(void)
486 void R_Textures_Init (void)
488 Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
489 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
490 Cvar_RegisterVariable (&r_max_scrapsize);
491 Cvar_RegisterVariable (&r_max_size);
492 Cvar_RegisterVariable (&r_picmip);
493 Cvar_RegisterVariable (&r_lerpimages);
494 Cvar_RegisterVariable (&r_precachetextures);
495 gltexnuminuse = NULL;
497 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
500 void R_Textures_Frame (void)
502 // could do procedural texture animation here, if we keep track of which
503 // textures were accessed this frame...
505 // free the resize buffers
506 resizebuffersize = 0;
509 Mem_Free(resizebuffer);
512 if (colorconvertbuffer)
514 Mem_Free(colorconvertbuffer);
515 colorconvertbuffer = NULL;
519 void R_MakeResizeBufferBigger(int size)
521 if (resizebuffersize < size)
523 resizebuffersize = size;
525 Mem_Free(resizebuffer);
526 if (colorconvertbuffer)
527 Mem_Free(colorconvertbuffer);
528 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
529 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
530 if (!resizebuffer || !colorconvertbuffer)
531 Host_Error("R_Upload: out of memory\n");
535 static void R_Upload(gltexture_t *glt, qbyte *data)
537 int mip, width, height, internalformat;
541 qglBindTexture(GL_TEXTURE_2D, glt->image->texnum);
544 gl_backend_rebindtextures = true;
546 glt->flags &= ~GLTEXF_UPLOAD;
548 if (glt->flags & TEXF_FRAGMENT)
550 if (glt->image->flags & GLTEXF_UPLOAD)
552 Con_DPrintf("uploaded new fragments image\n");
553 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->bytesperpixel);
554 glt->image->flags &= ~GLTEXF_UPLOAD;
555 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
556 qglTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
558 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
560 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
564 if (prevbuffer == NULL)
566 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->bytesperpixel);
567 memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
568 prevbuffer = resizebuffer;
570 else if (glt->textype->textype == TEXTYPE_QPALETTE)
572 // promote paletted to RGBA, so we only have to worry about RGB and
573 // RGBA in the rest of this code
574 R_MakeResizeBufferBigger(glt->image->width * glt->image->height * glt->image->bytesperpixel);
575 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
576 prevbuffer = colorconvertbuffer;
579 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
581 glt->texnum = glt->image->texnum;
585 glt->image->flags &= ~GLTEXF_UPLOAD;
587 // these are rounded up versions of the size to do better resampling
588 for (width = 1;width < glt->width;width <<= 1);
589 for (height = 1;height < glt->height;height <<= 1);
591 R_MakeResizeBufferBigger(width * height * glt->image->bytesperpixel);
593 if (prevbuffer == NULL)
595 width = glt->image->width;
596 height = glt->image->height;
597 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
598 prevbuffer = resizebuffer;
602 if (glt->textype->textype == TEXTYPE_QPALETTE)
604 // promote paletted to RGBA, so we only have to worry about RGB and
605 // RGBA in the rest of this code
606 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
607 prevbuffer = colorconvertbuffer;
610 if (glt->width != width || glt->height != height)
612 Image_Resample(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel, r_lerpimages.integer);
613 prevbuffer = resizebuffer;
616 // apply picmip/max_size limitations
617 while (width > glt->image->width || height > glt->image->height)
619 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
620 prevbuffer = resizebuffer;
624 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
626 if (glt->flags & TEXF_ALPHA)
630 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
632 if (glt->flags & TEXF_MIPMAP)
634 while (width > 1 || height > 1)
636 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
637 prevbuffer = resizebuffer;
639 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
643 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
645 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
650 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
652 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
655 glt->texnum = glt->image->texnum;
658 static void R_FindImageForTexture(gltexture_t *glt)
660 int i, j, best, best2, x, y, w, h;
661 textypeinfo_t *texinfo;
662 gltexturepool_t *pool;
663 gltextureimage_t *image, **imagechainpointer;
664 texinfo = glt->textype;
667 // remains -1 until uploaded
674 if (glt->flags & TEXF_FRAGMENT)
676 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
678 image = *imagechainpointer;
679 if (image->type != GLIMAGETYPE_FRAGMENTS)
681 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
683 if (glt->width > image->width || glt->height > image->height)
686 // got a fragments texture, find a place in it if we can
687 for (best = image->width, i = 0;i < image->width - w;i += texinfo->align)
689 for (best2 = 0, j = 0;j < w;j++)
691 if (image->blockallocation[i+j] >= best)
693 if (best2 < image->blockallocation[i+j])
694 best2 = image->blockallocation[i+j];
698 // this is a valid spot
704 if (best + h > image->height)
707 for (i = 0;i < w;i++)
708 image->blockallocation[x + i] = best + h;
713 image->texturecount++;
717 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
719 Sys_Error("R_FindImageForTexture: ran out of memory\n");
720 image->type = GLIMAGETYPE_FRAGMENTS;
721 // make sure the created image is big enough for the fragment
722 for (image->width = block_size;image->width < glt->width;image->width <<= 1);
723 for (image->height = block_size;image->height < glt->height;image->height <<= 1);
724 image->blockallocation = Mem_Alloc(texturemempool, image->width * sizeof(short));
725 memset(image->blockallocation, 0, image->width * sizeof(short));
729 for (i = 0;i < w;i++)
730 image->blockallocation[x + i] = y + h;
734 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
736 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
738 Sys_Error("R_FindImageForTexture: ran out of memory\n");
739 image->type = GLIMAGETYPE_TILE;
740 image->blockallocation = NULL;
742 // calculate final size
743 if (r_max_size.integer > realmaxsize)
744 Cvar_SetValue("r_max_size", realmaxsize);
745 for (image->width = 1;image->width < glt->width;image->width <<= 1);
746 for (image->height = 1;image->height < glt->height;image->height <<= 1);
747 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
748 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
749 if (image->width < 1) image->width = 1;
750 if (image->height < 1) image->height = 1;
752 image->glinternalformat = texinfo->glinternalformat;
753 image->glformat = texinfo->glformat;
754 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
755 image->bytesperpixel = texinfo->internalbytesperpixel;
756 for (i = 1;i < MAX_GLTEXTURES;i++)
757 if (!gltexnuminuse[i])
759 if (i < MAX_GLTEXTURES)
760 gltexnuminuse[image->texnum = i] = true;
762 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
763 *imagechainpointer = image;
764 image->texturecount++;
771 // note: R_FindImageForTexture must be called before this
772 static void R_UploadTexture (gltexture_t *glt)
774 if (!(glt->flags & GLTEXF_UPLOAD))
777 R_Upload(glt, glt->inputtexels);
778 if (glt->inputtexels)
780 Mem_Free(glt->inputtexels);
781 glt->inputtexels = NULL;
782 glt->flags |= GLTEXF_DESTROYED;
784 else if (glt->flags & GLTEXF_DESTROYED)
785 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
788 static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, qbyte *data)
791 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
794 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
795 strcpy (glt->identifier, identifier);
798 glt->identifier = NULL;
800 glt->chain = pool->gltchain;
801 pool->gltchain = glt;
804 glt->height = height;
806 glt->textype = texinfo;
810 glt->inputtexels = Mem_Alloc(texturedatamempool, glt->width * glt->height * texinfo->inputbytesperpixel);
811 if (glt->inputtexels == NULL)
812 Sys_Error("R_SetupTexture: out of memory\n");
813 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
816 glt->inputtexels = NULL;
818 R_FindImageForTexture(glt);
819 R_PrecacheTexture(glt);
829 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
833 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
834 textypeinfo_t *texinfo;
837 if (cls.state == ca_dedicated)
840 texinfo = R_GetTexTypeInfo(textype, flags);
842 if (flags & TEXF_FRAGMENT)
843 if ((width * texinfo->internalbytesperpixel) & 3)
844 Host_Error("R_LoadTexture: incompatible width for fragment");
846 // clear the alpha flag if the texture has no transparent pixels
849 case TEXTYPE_QPALETTE:
850 if (flags & TEXF_ALPHA)
852 flags &= ~TEXF_ALPHA;
853 for (i = 0;i < width * height;i++)
864 if (flags & TEXF_ALPHA)
865 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
868 if (flags & TEXF_ALPHA)
870 flags &= ~TEXF_ALPHA;
871 for (i = 0;i < width * height;i++)
873 if (data[i * 4 + 3] < 255)
882 Host_Error("R_LoadTexture: unknown texture type\n");
885 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
889 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
891 // see if the texture is already present
892 if (identifier && (glt = R_FindTexture(pool, identifier)))
894 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)
896 Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier);
897 return (rtexture_t *)glt; // exact match, use existing
899 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
900 R_FreeTexture((rtexture_t *)glt);
903 return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data);
906 int R_TextureHasAlpha(rtexture_t *rt)
911 glt = (gltexture_t *)rt;
912 return (glt->flags & TEXF_ALPHA) != 0;
915 int R_TextureWidth(rtexture_t *rt)
919 return ((gltexture_t *)rt)->width;
922 int R_TextureHeight(rtexture_t *rt)
926 return ((gltexture_t *)rt)->height;
929 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
932 float iwidth, iheight;
933 if (cls.state == ca_dedicated)
939 if (fx1 || fy1 || fx2 || fy2)
953 Host_Error("R_FragmentLocation: no texture supplied\n");
954 glt = (gltexture_t *)rt;
955 if (glt->flags & TEXF_FRAGMENT)
961 if (fx1 || fy1 || fx2 || fy2)
963 iwidth = 1.0f / glt->image->width;
964 iheight = 1.0f / glt->image->height;
966 *fx1 = glt->x * iwidth;
968 *fy1 = glt->y * iheight;
970 *fx2 = (glt->x + glt->width) * iwidth;
972 *fy2 = (glt->y + glt->height) * iheight;
981 if (fx1 || fy1 || fx2 || fy2)
995 int R_CompatibleFragmentWidth(int width, int textype, int flags)
997 textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
998 while ((width * texinfo->internalbytesperpixel) & 3)
1003 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1007 Host_Error("R_UpdateTexture: no texture supplied\n");
1009 Host_Error("R_UpdateTexture: no data supplied\n");
1010 glt = (gltexture_t *)rt;
1012 // if it has not been uploaded yet, update the data that will be used when it is
1013 if (glt->inputtexels)
1014 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1016 R_Upload(glt, data);