3 cvar_t r_max_size = {CVAR_SAVE, "r_max_size", "2048"};
4 cvar_t r_max_scrapsize = {CVAR_SAVE, "r_max_scrapsize", "256"};
5 cvar_t r_picmip = {CVAR_SAVE, "r_picmip", "0"};
6 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
7 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
9 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR; //NEAREST;
10 int gl_filter_mag = GL_LINEAR;
13 static mempool_t *texturemempool;
14 static mempool_t *texturedatamempool;
15 static mempool_t *textureprocessingmempool;
17 // note: this must not conflict with TEXF_ flags in r_textures.h
18 // cleared when a texture is uploaded
19 #define GLTEXF_UPLOAD 0x00010000
20 // texture generated by code, also causes permanent GLTEXF_UPLOAD effect
21 #define GLTEXF_PROCEDURAL 0x00020000
22 // bitmask for mismatch checking
23 #define GLTEXF_IMPORTANTBITS (GLTEXF_PROCEDURAL)
24 // set when image is uploaded and freed
25 #define GLTEXF_DESTROYED 0x00040000
27 // size of images which hold fragment textures, ignores picmip and max_size
28 //#define BLOCK_SIZE 256
29 static int block_size;
31 // really this number only governs gltexnuminuse
32 #define MAX_GLTEXTURES 65536
34 // since there is only one set of GL texture numbers, we have to track them
35 // globally, everything else is per texture pool
36 static qbyte *gltexnuminuse;
41 int inputbytesperpixel;
42 int internalbytesperpixel;
49 static textypeinfo_t textype_qpalette = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3, 1};
50 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3, 3};
51 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3, 1};
52 static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4, 1};
53 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4, 1};
55 // a tiling texture (most common type)
56 #define GLIMAGETYPE_TILE 0
57 // a fragments texture (contains one or more fragment textures)
58 #define GLIMAGETYPE_FRAGMENTS 1
60 // a gltextureimage can have one (or more if fragments) gltextures inside
61 typedef struct gltextureimage_s
63 struct gltextureimage_s *imagechain;
65 int type; // one of the GLIMAGETYPE_ values
66 int texnum; // GL texture slot number
68 int bytesperpixel; // bytes per pixel
69 int glformat; // GL_RGB or GL_RGBA
70 int glinternalformat; // 3 or 4
72 short *blockallocation; // fragment allocation
76 typedef struct gltexture_s
78 // pointer to texturepool (check this to see if the texture is allocated)
79 struct gltexturepool_s *pool;
80 // pointer to next texture in texturepool chain
81 struct gltexture_s *chain;
82 // pointer into gltextureimage array
83 gltextureimage_t *image;
84 // name of the texture (this might be removed someday), no duplicates
86 // location in the image, and size
87 int x, y, width, height;
88 // copy of the original texture supplied to the upload function, for re-uploading or deferred uploads (non-precached)
90 // to identify cache mismatchs (this might be removed someday)
92 // flags supplied to the LoadTexture/ProceduralTexture functions
93 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
95 // procedural texture generation function, called once per frame if the texture is used
96 int (*generate)(qbyte *buffer, int width, int height, void *parameterdata, int parameterdatasize);
97 // data provided to generate, persistent from call to call
98 qbyte *proceduraldata;
100 int proceduraldatasize;
101 // used only to avoid updating the texture more than once per frame
102 int proceduralframecount;
103 // pointer to one of the textype_ structs
104 textypeinfo_t *textype;
108 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
110 typedef struct gltexturepool_s
113 struct gltextureimage_s *imagechain;
114 struct gltexture_s *gltchain;
115 struct gltexturepool_s *next;
119 static gltexturepool_t *gltexturepoolchain = NULL;
121 static qbyte *resizebuffer = NULL, *colorconvertbuffer;
122 static int resizebuffersize = 0;
123 static qbyte *texturebuffer;
124 static int texturebuffersize = 0;
126 static int realmaxsize = 0;
128 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
130 if (flags & TEXF_ALPHA)
134 case TEXTYPE_QPALETTE:
135 return &textype_qpalette_alpha;
137 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
140 return &textype_rgba_alpha;
142 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
150 case TEXTYPE_QPALETTE:
151 return &textype_qpalette;
155 return &textype_rgba;
157 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
163 static void R_UploadTexture(gltexture_t *t);
165 static void R_PrecacheTexture(gltexture_t *glt)
169 if (glt->flags & TEXF_ALWAYSPRECACHE)
171 else if (r_precachetextures.integer >= 2)
173 else if (r_precachetextures.integer >= 1)
174 if (glt->flags & TEXF_PRECACHE)
178 R_UploadTexture(glt);
181 int R_GetTexture(rtexture_t *rt)
186 glt = (gltexture_t *)rt;
187 if (glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL))
189 if (glt->flags & GLTEXF_PROCEDURAL)
191 if (glt->proceduralframecount != r_framecount)
193 glt->proceduralframecount = r_framecount;
194 R_UploadTexture(glt);
198 R_UploadTexture(glt);
200 return glt->image->texnum;
203 static void R_FreeTexture(gltexture_t *glt)
205 gltexture_t **gltpointer;
206 gltextureimage_t *image, **gltimagepointer;
210 Host_Error("R_FreeTexture: texture == NULL\n");
212 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
213 if (*gltpointer == glt)
214 *gltpointer = glt->chain;
216 Host_Error("R_FreeTexture: texture \"%s\" not linked in pool\n", glt->identifier);
218 // note: if freeing a fragment texture, this will not make the claimed
219 // space available for new textures unless all other fragments in the
220 // image are also freed
222 image->texturecount--;
223 if (image->texturecount < 1)
225 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
226 if (*gltimagepointer == image)
227 *gltimagepointer = image->imagechain;
229 Host_Error("R_FreeTexture: image not linked in pool\n");
232 texnum = image->texnum;
233 gltexnuminuse[image->texnum] = 0;
234 glDeleteTextures(1, &texnum);
236 if (image->blockallocation)
237 Mem_Free(image->blockallocation);
242 Mem_Free(glt->identifier);
243 if (glt->inputtexels)
244 Mem_Free(glt->inputtexels);
245 if (glt->proceduraldata)
246 Mem_Free(glt->proceduraldata);
250 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
257 for (glt = pool->gltchain;glt;glt = glt->chain)
258 if (glt->identifier && !strcmp (identifier, glt->identifier))
264 rtexturepool_t *R_AllocTexturePool(void)
266 gltexturepool_t *pool;
267 if (texturemempool == NULL)
269 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
272 //memset(pool, 0, 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 glGetIntegerv(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);
475 //memset(gltexnuminuse, 0, MAX_GLTEXTURES);
478 static void r_textures_shutdown(void)
480 rtexturepool_t *temp;
481 while(gltexturepoolchain)
483 temp = (rtexturepool_t *) gltexturepoolchain;
484 R_FreeTexturePool(&temp);
488 if (resizebuffer) Mem_Free(resizebuffer);resizebuffer = NULL;
489 if (colorconvertbuffer) Mem_Free(colorconvertbuffer);colorconvertbuffer = NULL;
490 if (texturebuffer) Mem_Free(texturebuffer);texturebuffer = NULL;
491 if (gltexnuminuse) Mem_Free(gltexnuminuse);gltexnuminuse = NULL;
493 resizebuffersize = 0;
494 texturebuffersize = 0;
496 colorconvertbuffer = NULL;
497 texturebuffer = NULL;
498 gltexnuminuse = NULL;
499 Mem_FreePool(&texturemempool);
500 Mem_FreePool(&texturedatamempool);
501 Mem_FreePool(&textureprocessingmempool);
504 static void r_textures_newmap(void)
508 void R_Textures_Init (void)
510 Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
511 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
512 Cvar_RegisterVariable (&r_max_scrapsize);
513 Cvar_RegisterVariable (&r_max_size);
514 Cvar_RegisterVariable (&r_picmip);
515 Cvar_RegisterVariable (&r_lerpimages);
516 Cvar_RegisterVariable (&r_precachetextures);
517 gltexnuminuse = NULL;
519 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
522 static void R_Upload(gltexture_t *glt, qbyte *data)
524 int mip, width, height, internalformat;
528 qglBindTexture(GL_TEXTURE_2D, glt->image->texnum);
531 gl_backend_rebindtextures = true;
533 glt->flags &= ~GLTEXF_UPLOAD;
535 if (glt->flags & TEXF_FRAGMENT)
537 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
539 resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
541 Mem_Free(resizebuffer);
542 if (colorconvertbuffer)
543 Mem_Free(colorconvertbuffer);
544 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
545 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
546 if (!resizebuffer || !colorconvertbuffer)
547 Host_Error("R_Upload: out of memory\n");
550 if (glt->image->flags & GLTEXF_UPLOAD)
552 Con_DPrintf("uploaded new fragments image\n");
553 glt->image->flags &= ~GLTEXF_UPLOAD;
554 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
555 qglTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
557 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
559 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
563 if (prevbuffer == NULL)
565 memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
566 prevbuffer = resizebuffer;
568 else if (glt->textype->textype == TEXTYPE_QPALETTE)
570 // promote paletted to RGBA, so we only have to worry about RGB and
571 // RGBA in the rest of this code
572 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
573 prevbuffer = colorconvertbuffer;
576 qglTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
581 glt->image->flags &= ~GLTEXF_UPLOAD;
583 // these are rounded up versions of the size to do better resampling
584 for (width = 1;width < glt->width;width <<= 1);
585 for (height = 1;height < glt->height;height <<= 1);
587 if (resizebuffersize < width * height * glt->image->bytesperpixel)
589 resizebuffersize = width * height * glt->image->bytesperpixel;
591 Mem_Free(resizebuffer);
592 if (colorconvertbuffer)
593 Mem_Free(colorconvertbuffer);
594 resizebuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
595 colorconvertbuffer = Mem_Alloc(textureprocessingmempool, resizebuffersize);
596 if (!resizebuffer || !colorconvertbuffer)
597 Host_Error("R_Upload: out of memory\n");
600 if (prevbuffer == NULL)
602 width = glt->image->width;
603 height = glt->image->height;
604 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
605 prevbuffer = resizebuffer;
609 if (glt->textype->textype == TEXTYPE_QPALETTE)
611 // promote paletted to RGBA, so we only have to worry about RGB and
612 // RGBA in the rest of this code
613 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
614 prevbuffer = colorconvertbuffer;
617 if (glt->width != width || glt->height != height)
619 Image_Resample(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel, r_lerpimages.integer);
620 prevbuffer = resizebuffer;
623 // apply picmip/max_size limitations
624 while (width > glt->image->width || height > glt->image->height)
626 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
627 prevbuffer = resizebuffer;
631 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
633 if (glt->flags & TEXF_ALPHA)
637 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
639 if (glt->flags & TEXF_MIPMAP)
641 while (width > 1 || height > 1)
643 Image_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
644 prevbuffer = resizebuffer;
646 qglTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
650 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
652 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
657 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
659 qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
664 static void R_FindImageForTexture(gltexture_t *glt)
666 int i, j, best, best2, x, y, w, h;
667 textypeinfo_t *texinfo;
668 gltexturepool_t *pool;
669 gltextureimage_t *image, **imagechainpointer;
670 texinfo = glt->textype;
677 if (glt->flags & TEXF_FRAGMENT)
679 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
681 image = *imagechainpointer;
682 if (image->type != GLIMAGETYPE_FRAGMENTS)
684 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
687 // got a fragments texture, find a place in it if we can
689 for (best = block_size, i = 0;i < block_size - w;i += texinfo->align)
691 for (best2 = 0, j = 0;j < w;j++)
693 if (image->blockallocation[i+j] >= best)
695 if (best2 < image->blockallocation[i+j])
696 best2 = image->blockallocation[i+j];
700 // this is a valid spot
706 if (best + h > block_size)
709 for (i = 0;i < w;i++)
710 image->blockallocation[x + i] = best + h;
715 image->texturecount++;
719 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
721 Sys_Error("R_FindImageForTexture: ran out of memory\n");
722 //memset(image, 0, sizeof(*image));
723 image->type = GLIMAGETYPE_FRAGMENTS;
724 image->width = block_size;
725 image->height = block_size;
726 image->blockallocation = Mem_Alloc(texturemempool, block_size * sizeof(short));
727 memset(image->blockallocation, 0, block_size * sizeof(short));
731 for (i = 0;i < w;i++)
732 image->blockallocation[x + i] = y + h;
736 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
738 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
740 Sys_Error("R_FindImageForTexture: ran out of memory\n");
741 //memset(image, 0, sizeof(*image));
742 image->type = GLIMAGETYPE_TILE;
743 image->blockallocation = NULL;
745 // calculate final size
746 if (r_max_size.integer > realmaxsize)
747 Cvar_SetValue("r_max_size", realmaxsize);
748 for (image->width = 1;image->width < glt->width;image->width <<= 1);
749 for (image->height = 1;image->height < glt->height;image->height <<= 1);
750 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
751 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
752 if (image->width < 1) image->width = 1;
753 if (image->height < 1) image->height = 1;
755 image->glinternalformat = texinfo->glinternalformat;
756 image->glformat = texinfo->glformat;
757 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
758 image->bytesperpixel = texinfo->internalbytesperpixel;
759 for (i = 1;i < MAX_GLTEXTURES;i++)
760 if (!gltexnuminuse[i])
762 if (i < MAX_GLTEXTURES)
763 gltexnuminuse[image->texnum = i] = true;
765 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
766 *imagechainpointer = image;
767 image->texturecount++;
774 // note: R_FindImageForTexture must be called before this
775 static void R_UploadTexture (gltexture_t *glt)
777 if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
780 if (glt->flags & GLTEXF_PROCEDURAL)
784 if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
787 Mem_Free(texturebuffer);
788 texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
789 texturebuffer = Mem_Alloc(textureprocessingmempool, texturebuffersize);
792 glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
797 R_Upload(glt, glt->inputtexels);
798 if (glt->inputtexels)
800 Mem_Free(glt->inputtexels);
801 glt->inputtexels = NULL;
802 glt->flags |= GLTEXF_DESTROYED;
804 else if (glt->flags & GLTEXF_DESTROYED)
805 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
809 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)
812 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
813 //memset(glt, 0, sizeof(gltexture_t));
816 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
817 strcpy (glt->identifier, identifier);
820 glt->identifier = NULL;
822 glt->chain = pool->gltchain;
823 pool->gltchain = glt;
826 glt->height = height;
828 glt->textype = texinfo;
832 glt->inputtexels = Mem_Alloc(texturedatamempool, glt->width * glt->height * texinfo->inputbytesperpixel);
833 if (glt->inputtexels == NULL)
834 Sys_Error("R_SetupTexture: out of memory\n");
835 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
838 glt->inputtexels = NULL;
840 glt->generate = generate;
841 glt->proceduraldatasize = proceduraldatasize;
842 if (proceduraldatasize)
844 glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
845 if (glt->proceduraldata == NULL)
846 Sys_Error("R_SetupTexture: out of memory\n");
849 glt->proceduraldata = NULL;
851 R_FindImageForTexture(glt);
852 R_PrecacheTexture(glt);
862 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, qbyte *data, int textype, int flags)
866 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
867 textypeinfo_t *texinfo;
870 if (cls.state == ca_dedicated)
873 texinfo = R_GetTexTypeInfo(textype, flags);
877 // Host_Error("R_LoadTexture: \"%s\" has no data\n", identifier);
879 if (flags & TEXF_FRAGMENT)
881 if (width > block_size || height > block_size)
882 Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
883 if ((width * texinfo->internalbytesperpixel) & 3)
884 Host_Error("R_LoadTexture: incompatible width for fragment");
887 // clear the alpha flag if the texture has no transparent pixels
890 case TEXTYPE_QPALETTE:
891 if (flags & TEXF_ALPHA)
893 flags &= ~TEXF_ALPHA;
894 for (i = 0;i < width * height;i++)
905 if (flags & TEXF_ALPHA)
906 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
909 if (flags & TEXF_ALPHA)
911 flags &= ~TEXF_ALPHA;
912 for (i = 0;i < width * height;i++)
914 if (data[i * 4 + 3] < 255)
923 Host_Error("R_LoadTexture: unknown texture type\n");
926 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
930 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
932 // see if the texture is already present
933 if (identifier && (glt = R_FindTexture(pool, identifier)))
935 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)
937 Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier);
938 return (rtexture_t *)glt; // exact match, use existing
940 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
944 return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
947 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)
950 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
951 textypeinfo_t *texinfo;
953 if (cls.state == ca_dedicated)
956 texinfo = R_GetTexTypeInfo(textype, flags);
958 // no function is supported, for odd uses
959 // if (generate == NULL)
960 // Host_Error("R_ProceduralTexture: \"%s\" has no generate function\n", identifier);
961 if (flags & TEXF_FRAGMENT)
963 if (width > block_size || height > block_size)
964 Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
965 if ((width * texinfo->internalbytesperpixel) & 3)
966 Host_Error("R_ProceduralTexture: incompatible width for fragment");
969 // see if the texture is already present
970 if (identifier && (glt = R_FindTexture(pool, identifier)))
972 if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
974 Con_Printf("R_LoadTexture: exact match with existing texture %s\n", identifier);
975 return (rtexture_t *)glt; // exact match, use existing
977 Con_Printf("R_LoadTexture: cache mismatch, replacing old texture\n");
981 return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
984 int R_TextureHasAlpha(rtexture_t *rt)
989 glt = (gltexture_t *)rt;
990 return (glt->flags & TEXF_ALPHA) != 0;
993 int R_TextureWidth(rtexture_t *rt)
997 return ((gltexture_t *)rt)->width;
1000 int R_TextureHeight(rtexture_t *rt)
1004 return ((gltexture_t *)rt)->height;
1007 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1010 float iwidth, iheight;
1011 if (cls.state == ca_dedicated)
1017 if (fx1 || fy1 || fx2 || fy2)
1031 Host_Error("R_FragmentLocation: no texture supplied\n");
1032 glt = (gltexture_t *)rt;
1033 if (glt->flags & TEXF_FRAGMENT)
1039 if (fx1 || fy1 || fx2 || fy2)
1041 iwidth = 1.0f / glt->image->width;
1042 iheight = 1.0f / glt->image->height;
1044 *fx1 = glt->x * iwidth;
1046 *fy1 = glt->y * iheight;
1048 *fx2 = (glt->x + glt->width) * iwidth;
1050 *fy2 = (glt->y + glt->height) * iheight;
1059 if (fx1 || fy1 || fx2 || fy2)
1073 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1075 textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
1076 while ((width * texinfo->internalbytesperpixel) & 3)
1081 void R_UpdateTexture(rtexture_t *rt, qbyte *data)
1085 Host_Error("R_UpdateTexture: no texture supplied\n");
1087 Host_Error("R_UpdateTexture: no data supplied\n");
1088 glt = (gltexture_t *)rt;
1090 if (!(glt->flags & GLTEXF_PROCEDURAL))
1092 if (glt->inputtexels == NULL)
1094 glt->inputtexels = Mem_Alloc(texturedatamempool, glt->width * glt->height * glt->textype->inputbytesperpixel);
1095 if (glt->inputtexels == NULL)
1096 Host_Error("R_UpdateTexture: ran out of memory\n");
1098 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1100 R_Upload(glt, data);
1102 // if it has not been uploaded yet, update the data that will be used when it is
1103 if (glt->inputtexels)
1104 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1106 R_Upload(glt, data);