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 // texture generated by code, also causes permanent GLTEXF_UPLOAD effect
22 #define GLTEXF_PROCEDURAL 0x00020000
23 // bitmask for mismatch checking
24 #define GLTEXF_IMPORTANTBITS (GLTEXF_PROCEDURAL)
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 //#define BLOCK_SIZE 256
30 static int block_size;
32 // really this number only governs gltexnuminuse
33 #define MAX_GLTEXTURES 65536
35 // since there is only one set of GL texture numbers, we have to track them
36 // globally, everything else is per texture pool
37 static qbyte *gltexnuminuse;
42 int inputbytesperpixel;
43 int internalbytesperpixel;
50 static textypeinfo_t textype_qpalette = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3, 1};
51 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3, 3};
52 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3, 1};
53 static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4, 1};
54 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4, 1};
56 // a tiling texture (most common type)
57 #define GLIMAGETYPE_TILE 0
58 // a fragments texture (contains one or more fragment textures)
59 #define GLIMAGETYPE_FRAGMENTS 1
61 // a gltextureimage can have one (or more if fragments) gltextures inside
62 typedef struct gltextureimage_s
64 struct gltextureimage_s *imagechain;
66 int type; // one of the GLIMAGETYPE_ values
67 int texnum; // GL texture slot number
69 int bytesperpixel; // bytes per pixel
70 int glformat; // GL_RGB or GL_RGBA
71 int glinternalformat; // 3 or 4
73 short *blockallocation; // fragment allocation
77 typedef struct gltexture_s
79 // pointer to texturepool (check this to see if the texture is allocated)
80 struct gltexturepool_s *pool;
81 // pointer to next texture in texturepool chain
82 struct gltexture_s *chain;
83 // pointer into gltextureimage array
84 gltextureimage_t *image;
85 // name of the texture (this might be removed someday), no duplicates
87 // location in the image, and size
88 int x, y, width, height;
89 // copy of the original texture supplied to the upload function, for re-uploading or deferred uploads (non-precached)
91 // to identify cache mismatchs (this might be removed someday)
93 // flags supplied to the LoadTexture/ProceduralTexture functions
94 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
96 // procedural texture generation function, called once per frame if the texture is used
97 int (*generate)(qbyte *buffer, int width, int height, void *parameterdata, int parameterdatasize);
98 // data provided to generate, persistent from call to call
99 qbyte *proceduraldata;
101 int proceduraldatasize;
102 // used only to avoid updating the texture more than once per frame
103 int proceduralframecount;
104 // pointer to one of the textype_ structs
105 textypeinfo_t *textype;
109 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
111 typedef struct gltexturepool_s
114 struct gltextureimage_s *imagechain;
115 struct gltexture_s *gltchain;
116 struct gltexturepool_s *next;
120 static gltexturepool_t *gltexturepoolchain = NULL;
122 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
123 static int resizebuffersize = 0;
124 static qbyte *texturebuffer;
125 static int texturebuffersize = 0;
127 static int realmaxsize = 0;
129 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
131 if (flags & TEXF_ALPHA)
135 case TEXTYPE_QPALETTE:
136 return &textype_qpalette_alpha;
138 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
141 return &textype_rgba_alpha;
143 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
151 case TEXTYPE_QPALETTE:
152 return &textype_qpalette;
156 return &textype_rgba;
158 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
164 static void R_UploadTexture(gltexture_t *t);
166 static void R_PrecacheTexture(gltexture_t *glt)
170 if (glt->flags & TEXF_ALWAYSPRECACHE)
172 else if (r_precachetextures.integer >= 2)
174 else if (r_precachetextures.integer >= 1)
175 if (glt->flags & TEXF_PRECACHE)
179 R_UploadTexture(glt);
182 int R_GetTexture(rtexture_t *rt)
187 glt = (gltexture_t *)rt;
188 if (glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL))
190 if (glt->flags & GLTEXF_PROCEDURAL)
192 if (glt->proceduralframecount != r_framecount)
194 glt->proceduralframecount = r_framecount;
195 R_UploadTexture(glt);
199 R_UploadTexture(glt);
201 return glt->image->texnum;
204 static void R_FreeTexture(gltexture_t *glt)
206 gltexture_t **gltpointer;
207 gltextureimage_t *image, **gltimagepointer;
211 Host_Error("R_FreeTexture: texture == NULL\n");
213 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
214 if (*gltpointer == glt)
215 *gltpointer = glt->chain;
217 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
219 // note: if freeing a fragment texture, this will not make the claimed
220 // space available for new textures unless all other fragments in the
221 // image are also freed
223 image->texturecount--;
224 if (image->texturecount < 1)
226 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
227 if (*gltimagepointer == image)
228 *gltimagepointer = image->imagechain;
230 Host_Error("R_FreeTexture: image not linked in pool\n");
233 texnum = image->texnum;
234 gltexnuminuse[image->texnum] = 0;
235 qglDeleteTextures(1, &texnum);
237 if (image->blockallocation)
238 Mem_Free(image->blockallocation);
243 Mem_Free(glt->identifier);
244 if (glt->inputtexels)
245 Mem_Free(glt->inputtexels);
246 if (glt->proceduraldata)
247 Mem_Free(glt->proceduraldata);
251 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
258 for (glt = pool->gltchain;glt;glt = glt->chain)
259 if (glt->identifier && !strcmp (identifier, glt->identifier))
265 rtexturepool_t *R_AllocTexturePool(void)
267 gltexturepool_t *pool;
268 if (texturemempool == NULL)
270 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
273 pool->next = gltexturepoolchain;
274 gltexturepoolchain = pool;
275 pool->sentinel = TEXTUREPOOL_SENTINEL;
276 return (rtexturepool_t *)pool;
279 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
281 gltexturepool_t *pool, **poolpointer;
282 if (rtexturepool == NULL)
284 if (*rtexturepool == NULL)
286 pool = (gltexturepool_t *)(*rtexturepool);
287 *rtexturepool = NULL;
288 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
289 Host_Error("R_FreeTexturePool: pool already freed\n");
290 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
291 if (*poolpointer == pool)
292 *poolpointer = pool->next;
294 Host_Error("R_FreeTexturePool: pool not linked\n");
295 while (pool->gltchain)
296 R_FreeTexture(pool->gltchain);
297 if (pool->imagechain)
298 Sys_Error("R_FreeTexturePool: not all images freed\n");
306 int minification, magnification;
310 static glmode_t modes[] =
312 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
313 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
314 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
315 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
316 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
317 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
320 extern int gl_backend_rebindtextures;
322 static void GL_TextureMode_f (void)
325 gltextureimage_t *image;
326 gltexturepool_t *pool;
330 for (i = 0;i < 6;i++)
332 if (gl_filter_min == modes[i].minification)
334 Con_Printf ("%s\n", modes[i].name);
338 Con_Printf ("current filter is unknown???\n");
342 for (i = 0;i < 6;i++)
343 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
347 Con_Printf ("bad filter name\n");
351 gl_filter_min = modes[i].minification;
352 gl_filter_mag = modes[i].magnification;
354 // change all the existing mipmap texture objects
355 // FIXME: force renderer(/client/something?) restart instead?
356 for (pool = gltexturepoolchain;pool;pool = pool->next)
358 for (image = pool->imagechain;image;image = image->imagechain)
360 // only update already uploaded images
361 if (!(image->flags & GLTEXF_UPLOAD))
363 qglBindTexture(GL_TEXTURE_2D, image->texnum);
364 if (image->flags & TEXF_MIPMAP)
365 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
367 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
368 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
372 gl_backend_rebindtextures = true;
375 static int R_CalcTexelDataSize (gltexture_t *glt)
377 int width2, height2, size;
378 if (glt->flags & TEXF_FRAGMENT)
379 size = glt->width * glt->height;
382 if (r_max_size.integer > realmaxsize)
383 Cvar_SetValue("r_max_size", realmaxsize);
384 // calculate final size
385 for (width2 = 1;width2 < glt->width;width2 <<= 1);
386 for (height2 = 1;height2 < glt->height;height2 <<= 1);
387 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
388 for (height2 >>= r_picmip.integer;height2 > r_max_size.integer;height2 >>= 1);
389 if (width2 < 1) width2 = 1;
390 if (height2 < 1) height2 = 1;
393 if (glt->flags & TEXF_MIPMAP)
395 while (width2 > 1 || height2 > 1)
397 size += width2 * height2;
403 size++; // count the last 1x1 mipmap
406 size = width2*height2;
408 size *= glt->textype->internalbytesperpixel;
413 void R_TextureStats_PrintTotal(void)
415 int glsize, inputsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
417 gltexturepool_t *pool;
418 for (pool = gltexturepoolchain;pool;pool = pool->next)
420 for (glt = pool->gltchain;glt;glt = glt->chain)
422 glsize = R_CalcTexelDataSize(glt);
423 inputsize = glt->width * glt->height * glt->textype->inputbytesperpixel;
428 if (!(glt->flags & GLTEXF_UPLOAD))
432 loadedp += inputsize;
436 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);
439 static void R_TextureStats_f(void)
443 gltexturepool_t *pool;
444 Con_Printf("glsize input crc loaded mip alpha name\n");
445 for (pool = gltexturepoolchain;pool;pool = pool->next)
447 for (glt = pool->gltchain;glt;glt = glt->chain)
449 loaded = !(glt->flags & GLTEXF_UPLOAD);
450 if (glt->flags & GLTEXF_PROCEDURAL)
451 Con_Printf("%c%4i%c %4i PROC %s %s %s %s\n" , loaded ? '[' : ' ', (R_CalcTexelDataSize(glt) + 1023) / 1024, loaded ? ']' : ' ', (glt->width * glt->height * glt->textype->inputbytesperpixel + 1023) / 1024, loaded ? "loaded" : " ", (glt->flags & TEXF_MIPMAP) ? "mip" : " ", (glt->flags & TEXF_ALPHA) ? "alpha" : " ", glt->identifier ? glt->identifier : "<unnamed>");
453 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>");
455 Con_Printf("pool %10p\n", pool);
457 R_TextureStats_PrintTotal();
460 char engineversion[40];
462 static void r_textures_start(void)
464 // deal with size limits of various drivers (3dfx in particular)
465 qglGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
468 // use the largest scrap texture size we can (not sure if this is really a good idea)
469 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
471 texturemempool = Mem_AllocPool("Texture Info");
472 texturedatamempool = Mem_AllocPool("Texture Storage (not yet uploaded)");
473 textureprocessingmempool = Mem_AllocPool("Texture Processing Buffers");
474 gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
477 static void r_textures_shutdown(void)
479 rtexturepool_t *temp;
480 while(gltexturepoolchain)
482 temp = (rtexturepool_t *) gltexturepoolchain;
483 R_FreeTexturePool(&temp);
486 resizebuffersize = 0;
487 texturebuffersize = 0;
489 colorconvertbuffer = NULL;
490 texturebuffer = NULL;
491 gltexnuminuse = NULL;
492 Mem_FreePool(&texturemempool);
493 Mem_FreePool(&texturedatamempool);
494 Mem_FreePool(&textureprocessingmempool);
497 static void r_textures_newmap(void)
501 void R_Textures_Init (void)
503 Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
504 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
505 Cvar_RegisterVariable (&r_max_scrapsize);
506 Cvar_RegisterVariable (&r_max_size);
507 Cvar_RegisterVariable (&r_picmip);
508 Cvar_RegisterVariable (&r_lerpimages);
509 Cvar_RegisterVariable (&r_precachetextures);
510 gltexnuminuse = NULL;
512 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
515 static void R_Upload(gltexture_t *glt, qbyte *data)
517 int mip, width, height, internalformat;
521 qglBindTexture(GL_TEXTURE_2D, glt->image->texnum);
524 gl_backend_rebindtextures = true;
526 glt->flags &= ~GLTEXF_UPLOAD;
528 if (glt->flags & TEXF_FRAGMENT)
530 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
532 resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
534 Mem_Free(resizebuffer);
535 if (colorconvertbuffer)
536 Mem_Free(colorconvertbuffer);
537 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
538 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
539 if (!resizebuffer || !colorconvertbuffer)
540 Host_Error("R_Upload: out of memory\n");
543 if (glt->image->flags & GLTEXF_UPLOAD)
545 Con_DPrintf("uploaded new fragments image\n");
546 glt->image->flags &= ~GLTEXF_UPLOAD;
547 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
548 qglTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
550 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
552 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
556 if (prevbuffer == NULL)
558 memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
559 prevbuffer = resizebuffer;
561 else if (glt->textype->textype == TEXTYPE_QPALETTE)
563 // promote paletted to RGBA, so we only have to worry about RGB and
564 // RGBA in the rest of this code
565 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
566 prevbuffer = colorconvertbuffer;
569 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
574 glt->image->flags &= ~GLTEXF_UPLOAD;
576 // these are rounded up versions of the size to do better resampling
577 for (width = 1;width < glt->width;width <<= 1);
578 for (height = 1;height < glt->height;height <<= 1);
580 if (resizebuffersize < width * height * glt->image->bytesperpixel)
582 resizebuffersize = width * height * glt->image->bytesperpixel;
584 Mem_Free(resizebuffer);
585 if (colorconvertbuffer)
586 Mem_Free(colorconvertbuffer);
587 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
588 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
589 if (!resizebuffer || !colorconvertbuffer)
590 Host_Error("R_Upload: out of memory\n");
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);
657 static void R_FindImageForTexture(gltexture_t *glt)
659 int i, j, best, best2, x, y, w, h;
660 textypeinfo_t *texinfo;
661 gltexturepool_t *pool;
662 gltextureimage_t *image, **imagechainpointer;
663 texinfo = glt->textype;
670 if (glt->flags & TEXF_FRAGMENT)
672 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
674 image = *imagechainpointer;
675 if (image->type != GLIMAGETYPE_FRAGMENTS)
677 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
680 // got a fragments texture, find a place in it if we can
682 for (best = block_size, i = 0;i < block_size - w;i += texinfo->align)
684 for (best2 = 0, j = 0;j < w;j++)
686 if (image->blockallocation[i+j] >= best)
688 if (best2 < image->blockallocation[i+j])
689 best2 = image->blockallocation[i+j];
693 // this is a valid spot
699 if (best + h > block_size)
702 for (i = 0;i < w;i++)
703 image->blockallocation[x + i] = best + h;
708 image->texturecount++;
712 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
714 Sys_Error("R_FindImageForTexture: ran out of memory\n");
715 image->type = GLIMAGETYPE_FRAGMENTS;
716 image->width = block_size;
717 image->height = block_size;
718 image->blockallocation = Mem_Alloc(texturemempool, block_size * sizeof(short));
719 memset(image->blockallocation, 0, block_size * sizeof(short));
723 for (i = 0;i < w;i++)
724 image->blockallocation[x + i] = y + h;
728 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
730 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
732 Sys_Error("R_FindImageForTexture: ran out of memory\n");
733 image->type = GLIMAGETYPE_TILE;
734 image->blockallocation = NULL;
736 // calculate final size
737 if (r_max_size.integer > realmaxsize)
738 Cvar_SetValue("r_max_size", realmaxsize);
739 for (image->width = 1;image->width < glt->width;image->width <<= 1);
740 for (image->height = 1;image->height < glt->height;image->height <<= 1);
741 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
742 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
743 if (image->width < 1) image->width = 1;
744 if (image->height < 1) image->height = 1;
746 image->glinternalformat = texinfo->glinternalformat;
747 image->glformat = texinfo->glformat;
748 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
749 image->bytesperpixel = texinfo->internalbytesperpixel;
750 for (i = 1;i < MAX_GLTEXTURES;i++)
751 if (!gltexnuminuse[i])
753 if (i < MAX_GLTEXTURES)
754 gltexnuminuse[image->texnum = i] = true;
756 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
757 *imagechainpointer = image;
758 image->texturecount++;
765 // note: R_FindImageForTexture must be called before this
766 static void R_UploadTexture (gltexture_t *glt)
768 if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
771 if (glt->flags & GLTEXF_PROCEDURAL)
775 if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
778 Mem_Free(texturebuffer);
779 texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
780 texturebuffer = Mem_Alloc(textureprocessingmempool, texturebuffersize);
783 glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
788 R_Upload(glt, glt->inputtexels);
789 if (glt->inputtexels)
791 Mem_Free(glt->inputtexels);
792 glt->inputtexels = NULL;
793 glt->flags |= GLTEXF_DESTROYED;
795 else if (glt->flags & GLTEXF_DESTROYED)
796 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
800 static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, qbyte *data, int (*generate)(qbyte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
803 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
806 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
807 strcpy (glt->identifier, identifier);
810 glt->identifier = NULL;
812 glt->chain = pool->gltchain;
813 pool->gltchain = glt;
816 glt->height = height;
818 glt->textype = texinfo;
822 glt->inputtexels = Mem_Alloc(texturedatamempool, glt->width * glt->height * texinfo->inputbytesperpixel);
823 if (glt->inputtexels == NULL)
824 Sys_Error("R_SetupTexture: out of memory\n");
825 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
828 glt->inputtexels = NULL;
830 glt->generate = generate;
831 glt->proceduraldatasize = proceduraldatasize;
832 if (proceduraldatasize)
834 glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
835 if (glt->proceduraldata == NULL)
836 Sys_Error("R_SetupTexture: out of memory\n");
839 glt->proceduraldata = NULL;
841 R_FindImageForTexture(glt);
842 R_PrecacheTexture(glt);
852 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
856 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
857 textypeinfo_t *texinfo;
860 if (cls.state == ca_dedicated)
863 texinfo = R_GetTexTypeInfo(textype, flags);
865 if (flags & TEXF_FRAGMENT)
867 if (width > block_size || height > block_size)
868 Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
869 if ((width * texinfo->internalbytesperpixel) & 3)
870 Host_Error("R_LoadTexture: incompatible width for fragment");
873 // clear the alpha flag if the texture has no transparent pixels
876 case TEXTYPE_QPALETTE:
877 if (flags & TEXF_ALPHA)
879 flags &= ~TEXF_ALPHA;
880 for (i = 0;i < width * height;i++)
891 if (flags & TEXF_ALPHA)
892 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
895 if (flags & TEXF_ALPHA)
897 flags &= ~TEXF_ALPHA;
898 for (i = 0;i < width * height;i++)
900 if (data[i * 4 + 3] < 255)
909 Host_Error("R_LoadTexture: unknown texture type\n");
912 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
916 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
918 // see if the texture is already present
919 if (identifier && (glt = R_FindTexture(pool, identifier)))
921 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)
923 Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier);
924 return (rtexture_t *)glt; // exact match, use existing
926 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
930 return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
933 rtexture_t *R_ProceduralTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, int textype, int flags, int (*generate)(qbyte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
936 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
937 textypeinfo_t *texinfo;
939 if (cls.state == ca_dedicated)
942 texinfo = R_GetTexTypeInfo(textype, flags);
944 if (flags & TEXF_FRAGMENT)
946 if (width > block_size || height > block_size)
947 Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
948 if ((width * texinfo->internalbytesperpixel) & 3)
949 Host_Error("R_ProceduralTexture: incompatible width for fragment");
952 // see if the texture is already present
953 if (identifier && (glt = R_FindTexture(pool, identifier)))
955 if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
957 Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier);
958 return (rtexture_t *)glt; // exact match, use existing
960 Con_Printf("R_LoadTexture: cache mismatch, replacing old texture\n");
964 return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
967 int R_TextureHasAlpha(rtexture_t *rt)
972 glt = (gltexture_t *)rt;
973 return (glt->flags & TEXF_ALPHA) != 0;
976 int R_TextureWidth(rtexture_t *rt)
980 return ((gltexture_t *)rt)->width;
983 int R_TextureHeight(rtexture_t *rt)
987 return ((gltexture_t *)rt)->height;
990 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
993 float iwidth, iheight;
994 if (cls.state == ca_dedicated)
1000 if (fx1 || fy1 || fx2 || fy2)
1014 Host_Error("R_FragmentLocation: no texture supplied\n");
1015 glt = (gltexture_t *)rt;
1016 if (glt->flags & TEXF_FRAGMENT)
1022 if (fx1 || fy1 || fx2 || fy2)
1024 iwidth = 1.0f / glt->image->width;
1025 iheight = 1.0f / glt->image->height;
1027 *fx1 = glt->x * iwidth;
1029 *fy1 = glt->y * iheight;
1031 *fx2 = (glt->x + glt->width) * iwidth;
1033 *fy2 = (glt->y + glt->height) * iheight;
1042 if (fx1 || fy1 || fx2 || fy2)
1056 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1058 textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
1059 while ((width * texinfo->internalbytesperpixel) & 3)
1064 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1068 Host_Error("R_UpdateTexture: no texture supplied\n");
1070 Host_Error("R_UpdateTexture: no data supplied\n");
1071 glt = (gltexture_t *)rt;
1073 // if it has not been uploaded yet, update the data that will be used when it is
1074 if (glt->inputtexels)
1075 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1077 R_Upload(glt, data);