3 cvar_t r_max_size = {0, "r_max_size", "2048"};
4 cvar_t r_picmip = {0, "r_picmip", "0"};
5 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1"};
6 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1"};
8 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR; //NEAREST;
9 int gl_filter_mag = GL_LINEAR;
12 static mempool_t *texturemempool;
14 // note: this must not conflict with TEXF_ flags in r_textures.h
15 // cleared when a texture is uploaded
16 #define GLTEXF_UPLOAD 0x00010000
17 // texture generated by code, also causes permanent GLTEXF_UPLOAD effect
18 #define GLTEXF_PROCEDURAL 0x00020000
19 // bitmask for mismatch checking
20 #define GLTEXF_IMPORTANTBITS (GLTEXF_PROCEDURAL)
21 // set when image is uploaded and freed
22 #define GLTEXF_DESTROYED 0x00040000
24 // size of images which hold fragment textures, ignores picmip and max_size
25 #define BLOCK_SIZE 256
27 // really this number only governs gltexnuminuse
28 #define MAX_GLTEXTURES 65536
30 // since there is only one set of GL texture numbers, we have to track them
31 // globally, everything else is per texture pool
32 static byte *gltexnuminuse;
37 int inputbytesperpixel;
38 int internalbytesperpixel;
45 static textypeinfo_t textype_qpalette = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3, 1};
46 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3, 3};
47 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3, 1};
48 static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4, 1};
49 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4, 1};
51 // a tiling texture (most common type)
52 #define GLIMAGETYPE_TILE 0
53 // a fragments texture (contains one or more fragment textures)
54 #define GLIMAGETYPE_FRAGMENTS 1
56 // a gltextureimage can have one (or more if fragments) gltextures inside
57 typedef struct gltextureimage_s
59 struct gltextureimage_s *imagechain;
61 int type; // one of the GLIMAGETYPE_ values
62 int texnum; // GL texture slot number
64 int bytesperpixel; // bytes per pixel
65 int glformat; // GL_RGB or GL_RGBA
66 int glinternalformat; // 3 or 4
68 short *blockallocation; // fragment allocation
72 typedef struct gltexture_s
74 // pointer to texturepool (check this to see if the texture is allocated)
75 struct gltexturepool_s *pool;
76 // pointer to next texture in texturepool chain
77 struct gltexture_s *chain;
78 // pointer into gltextureimage array
79 gltextureimage_t *image;
80 // name of the texture (this might be removed someday), no duplicates
82 // location in the image, and size
83 int x, y, width, height;
84 // copy of the original texture supplied to the upload function, for re-uploading or deferred uploads (non-precached)
86 // to identify cache mismatchs (this might be removed someday)
88 // flags supplied to the LoadTexture/ProceduralTexture functions
89 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
91 // procedural texture generation function, called once per frame if the texture is used
92 int (*generate)(byte *buffer, int width, int height, void *parameterdata, int parameterdatasize);
93 // data provided to generate, persistent from call to call
96 int proceduraldatasize;
97 // used only to avoid updating the texture more than once per frame
98 int proceduralframecount;
99 // pointer to one of the textype_ structs
100 textypeinfo_t *textype;
104 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
106 typedef struct gltexturepool_s
109 struct gltextureimage_s *imagechain;
110 struct gltexture_s *gltchain;
111 struct gltexturepool_s *next;
115 static gltexturepool_t *gltexturepoolchain = NULL;
117 static byte *resamplerow1 = NULL, *resamplerow2 = NULL;
118 static int resamplerowsize = 0;
119 static byte *resizebuffer = NULL, *colorconvertbuffer;
120 static int resizebuffersize = 0;
121 static byte *texturebuffer;
122 static int texturebuffersize = 0;
124 static int realmaxsize = 0;
126 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
128 if (flags & TEXF_ALPHA)
132 case TEXTYPE_QPALETTE:
133 return &textype_qpalette_alpha;
135 Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed\n");
138 return &textype_rgba_alpha;
140 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
148 case TEXTYPE_QPALETTE:
149 return &textype_qpalette;
153 return &textype_rgba;
155 Host_Error("R_GetTexTypeInfo: unknown texture format\n");
161 static void R_UploadTexture(gltexture_t *t);
163 static void R_PrecacheTexture(gltexture_t *glt)
167 if (glt->flags & TEXF_ALWAYSPRECACHE)
169 else if (r_precachetextures.integer >= 2)
171 else if (r_precachetextures.integer >= 1)
172 if (glt->flags & TEXF_PRECACHE)
176 R_UploadTexture(glt);
179 int R_GetTexture(rtexture_t *rt)
184 glt = (gltexture_t *)rt;
185 if (glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL))
187 if (glt->flags & GLTEXF_PROCEDURAL)
189 if (glt->proceduralframecount != r_framecount)
191 glt->proceduralframecount = r_framecount;
192 R_UploadTexture(glt);
196 R_UploadTexture(glt);
198 return glt->image->texnum;
201 static void R_FreeTexture(gltexture_t *glt)
203 gltexture_t **gltpointer;
204 gltextureimage_t *image, **gltimagepointer;
207 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
208 if (*gltpointer == glt)
209 *gltpointer = glt->chain;
211 Host_Error("R_FreeTexture: texture not linked in pool\n");
213 // note: if freeing a fragment texture, this will not make the claimed
214 // space available for new textures unless all other fragments in the
215 // image are also freed
217 image->texturecount--;
218 if (image->texturecount < 1)
220 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
221 if (*gltimagepointer == image)
222 *gltimagepointer = image->imagechain;
224 Host_Error("R_FreeTexture: image not linked in pool\n");
227 texnum = image->texnum;
228 gltexnuminuse[image->texnum] = 0;
229 glDeleteTextures(1, &texnum);
231 if (image->blockallocation)
232 Mem_Free(image->blockallocation);
237 Mem_Free(glt->identifier);
238 if (glt->inputtexels)
239 Mem_Free(glt->inputtexels);
240 if (glt->proceduraldata)
241 Mem_Free(glt->proceduraldata);
245 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
252 for (glt = pool->gltchain;glt;glt = glt->chain)
253 if (glt->identifier && !strcmp (identifier, glt->identifier))
259 rtexturepool_t *R_AllocTexturePool(void)
261 gltexturepool_t *pool;
262 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
265 //memset(pool, 0, sizeof(gltexturepool_t));
266 pool->next = gltexturepoolchain;
267 gltexturepoolchain = pool;
268 pool->sentinel = TEXTUREPOOL_SENTINEL;
269 return (rtexturepool_t *)pool;
272 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
274 gltexturepool_t *pool, **poolpointer;
275 if (rtexturepool == NULL)
277 if (*rtexturepool == NULL)
279 pool = (gltexturepool_t *)(*rtexturepool);
280 *rtexturepool = NULL;
281 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
282 Host_Error("R_FreeTexturePool: pool already freed\n");
283 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
284 if (*poolpointer == pool)
285 *poolpointer = pool->next;
287 Host_Error("R_FreeTexturePool: pool not linked\n");
288 while (pool->gltchain)
289 R_FreeTexture(pool->gltchain);
297 int minification, magnification;
301 static glmode_t modes[] =
303 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
304 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
305 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
306 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
307 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
308 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
311 static void GL_TextureMode_f (void)
314 gltextureimage_t *image;
315 gltexturepool_t *pool;
319 for (i = 0;i < 6;i++)
321 if (gl_filter_min == modes[i].minification)
323 Con_Printf ("%s\n", modes[i].name);
327 Con_Printf ("current filter is unknown???\n");
331 for (i = 0;i < 6;i++)
332 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
336 Con_Printf ("bad filter name\n");
340 gl_filter_min = modes[i].minification;
341 gl_filter_mag = modes[i].magnification;
343 // change all the existing mipmap texture objects
344 // FIXME: force renderer(/client/something?) restart instead?
345 for (pool = gltexturepoolchain;pool;pool = pool->next)
347 for (image = pool->imagechain;image;image = image->imagechain)
349 // only update already uploaded images
350 if (!(image->flags & GLTEXF_UPLOAD))
352 glBindTexture(GL_TEXTURE_2D, image->texnum);
353 if (image->flags & TEXF_MIPMAP)
354 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
356 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
357 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
363 static int R_CalcTexelDataSize (gltexture_t *glt)
365 int width2, height2, size;
366 if (glt->flags & TEXF_FRAGMENT)
367 size = glt->width * glt->height;
370 if (r_max_size.integer > realmaxsize)
371 Cvar_SetValue("r_max_size", realmaxsize);
372 // calculate final size
373 width2 = 1;while (width2 < glt->width) width2 <<= 1;
374 height2 = 1;while (height2 < glt->height) height2 <<= 1;
375 width2 >>= (int) r_picmip.integer;
376 height2 >>= (int) r_picmip.integer;
377 while (width2 > (int) r_max_size.integer) width2 >>= 1;
378 while (height2 > (int) r_max_size.integer) height2 >>= 1;
379 if (width2 < 1) width2 = 1;
380 if (height2 < 1) height2 = 1;
383 if (glt->flags & TEXF_MIPMAP)
385 while (width2 > 1 || height2 > 1)
387 size += width2 * height2;
393 size++; // count the last 1x1 mipmap
396 size = width2*height2;
398 size *= glt->textype->internalbytesperpixel;
403 void R_TextureStats_PrintTotal(void)
405 int glsize, inputsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
407 gltexturepool_t *pool;
408 for (pool = gltexturepoolchain;pool;pool = pool->next)
410 for (glt = pool->gltchain;glt;glt = glt->chain)
412 glsize = R_CalcTexelDataSize(glt);
413 inputsize = glt->width * glt->height * glt->textype->inputbytesperpixel;
418 if (!(glt->flags & GLTEXF_UPLOAD))
422 loadedp += inputsize;
426 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);
429 static void R_TextureStats_f(void)
433 gltexturepool_t *pool;
434 Con_Printf("glsize input crc loaded mip alpha name\n");
435 for (pool = gltexturepoolchain;pool;pool = pool->next)
437 for (glt = pool->gltchain;glt;glt = glt->chain)
439 loaded = !(glt->flags & GLTEXF_UPLOAD);
440 if (glt->flags & GLTEXF_PROCEDURAL)
441 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>");
443 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>");
445 Con_Printf("pool %10p\n", pool);
447 R_TextureStats_PrintTotal();
450 char engineversion[40];
452 static void r_textures_start(void)
454 // deal with size limits of various drivers (3dfx in particular)
455 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
458 texturemempool = Mem_AllocPool("Textures");
459 gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
460 //memset(gltexnuminuse, 0, MAX_GLTEXTURES);
463 static void r_textures_shutdown(void)
465 rtexturepool_t *temp;
466 while(gltexturepoolchain)
468 temp = (rtexturepool_t *) gltexturepoolchain;
469 R_FreeTexturePool(&temp);
473 if (resizebuffer) Mem_Free(resizebuffer);resizebuffer = NULL;
474 if (colorconvertbuffer) Mem_Free(colorconvertbuffer);colorconvertbuffer = NULL;
475 if (resamplerow1) Mem_Free(resamplerow1);resamplerow1 = NULL;
476 if (resamplerow2) Mem_Free(resamplerow2);resamplerow2 = NULL;
477 if (texturebuffer) Mem_Free(texturebuffer);texturebuffer = NULL;
478 if (gltexnuminuse) Mem_Free(gltexnuminuse);gltexnuminuse = NULL;
480 resizebuffersize = 0;
482 texturebuffersize = 0;
484 colorconvertbuffer = NULL;
487 texturebuffer = NULL;
488 gltexnuminuse = NULL;
489 Mem_FreePool(&texturemempool);
492 static void r_textures_newmap(void)
496 void R_Textures_Init (void)
498 Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
499 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
500 Cvar_RegisterVariable (&r_max_size);
501 Cvar_RegisterVariable (&r_picmip);
502 Cvar_RegisterVariable (&r_lerpimages);
503 Cvar_RegisterVariable (&r_precachetextures);
504 gltexnuminuse = NULL;
506 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
509 static void R_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth, int bytesperpixel)
511 int j, xi, oldx = 0, f, fstep, endx, lerp;
512 fstep = (int) (inwidth*65536.0f/outwidth);
514 if (bytesperpixel == 4)
516 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
521 in += (xi - oldx) * 4;
527 *out++ = (byte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
528 *out++ = (byte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
529 *out++ = (byte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
530 *out++ = (byte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
532 else // last pixel of the line has no pixel to lerp to
541 else if (bytesperpixel == 3)
543 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
548 in += (xi - oldx) * 3;
554 *out++ = (byte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
555 *out++ = (byte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
556 *out++ = (byte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
558 else // last pixel of the line has no pixel to lerp to
567 Sys_Error("R_ResampleTextureLerpLine: unsupported bytesperpixel %i\n", bytesperpixel);
575 static void R_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight, int bytesperpixel)
577 if (resamplerowsize < outwidth*4)
580 Mem_Free(resamplerow1);
582 Mem_Free(resamplerow2);
583 resamplerowsize = outwidth*4;
584 resamplerow1 = Mem_Alloc(texturemempool, resamplerowsize);
585 resamplerow2 = Mem_Alloc(texturemempool, resamplerowsize);
587 #define row1 resamplerow1
588 #define row2 resamplerow2
589 if (bytesperpixel == 4)
591 if (r_lerpimages.integer)
593 int i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
596 fstep = (int) (inheight*65536.0f/outheight);
600 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
601 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
602 for (i = 0, f = 0;i < outheight;i++,f += fstep)
610 inrow = (byte *)indata + inwidth4*yi;
612 memcpy(row1, row2, outwidth4);
614 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
615 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
621 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
674 inrow = (byte *)indata + inwidth4*yi;
676 memcpy(row1, row2, outwidth4);
678 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
681 memcpy(out, row1, outwidth4);
688 unsigned frac, fracstep;
689 // relies on int being 4 bytes
693 fracstep = inwidth*0x10000/outwidth;
694 for (i = 0;i < outheight;i++)
696 inrow = (int *)indata + inwidth*(i*inheight/outheight);
697 frac = fracstep >> 1;
701 out[0] = inrow[frac >> 16];frac += fracstep;
702 out[1] = inrow[frac >> 16];frac += fracstep;
703 out[2] = inrow[frac >> 16];frac += fracstep;
704 out[3] = inrow[frac >> 16];frac += fracstep;
710 out[0] = inrow[frac >> 16];frac += fracstep;
711 out[1] = inrow[frac >> 16];frac += fracstep;
716 out[0] = inrow[frac >> 16];frac += fracstep;
722 else if (bytesperpixel == 3)
724 if (r_lerpimages.integer)
726 int i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
729 fstep = (int) (inheight*65536.0f/outheight);
733 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
734 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
735 for (i = 0, f = 0;i < outheight;i++,f += fstep)
743 inrow = (byte *)indata + inwidth3*yi;
745 memcpy(row1, row2, outwidth3);
747 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
748 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
754 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
800 inrow = (byte *)indata + inwidth3*yi;
802 memcpy(row1, row2, outwidth3);
804 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
807 memcpy(out, row1, outwidth3);
813 int i, j, f, inwidth3 = inwidth * 3;
814 unsigned frac, fracstep;
818 fracstep = inwidth*0x10000/outwidth;
819 for (i = 0;i < outheight;i++)
821 inrow = indata + inwidth3*(i*inheight/outheight);
822 frac = fracstep >> 1;
826 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
827 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
828 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
829 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
834 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
835 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
840 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
847 Sys_Error("R_ResampleTexture: unsupported bytesperpixel %i\n", bytesperpixel);
852 // in can be the same as out
853 static void R_MipReduce(byte *in, byte *out, int *width, int *height, int destwidth, int destheight, int bytesperpixel)
856 nextrow = *width * bytesperpixel;
857 if (*width > destwidth)
860 if (*height > destheight)
864 if (bytesperpixel == 4)
866 for (y = 0;y < *height;y++)
868 for (x = 0;x < *width;x++)
870 out[0] = (byte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
871 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
872 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
873 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
877 in += nextrow; // skip a line
880 else if (bytesperpixel == 3)
882 for (y = 0;y < *height;y++)
884 for (x = 0;x < *width;x++)
886 out[0] = (byte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
887 out[1] = (byte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
888 out[2] = (byte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
892 in += nextrow; // skip a line
896 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
901 if (bytesperpixel == 4)
903 for (y = 0;y < *height;y++)
905 for (x = 0;x < *width;x++)
907 out[0] = (byte) ((in[0] + in[4]) >> 1);
908 out[1] = (byte) ((in[1] + in[5]) >> 1);
909 out[2] = (byte) ((in[2] + in[6]) >> 1);
910 out[3] = (byte) ((in[3] + in[7]) >> 1);
916 else if (bytesperpixel == 3)
918 for (y = 0;y < *height;y++)
920 for (x = 0;x < *width;x++)
922 out[0] = (byte) ((in[0] + in[3]) >> 1);
923 out[1] = (byte) ((in[1] + in[4]) >> 1);
924 out[2] = (byte) ((in[2] + in[5]) >> 1);
931 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
936 if (*height > destheight)
940 if (bytesperpixel == 4)
942 for (y = 0;y < *height;y++)
944 for (x = 0;x < *width;x++)
946 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
947 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
948 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
949 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
953 in += nextrow; // skip a line
956 else if (bytesperpixel == 3)
958 for (y = 0;y < *height;y++)
960 for (x = 0;x < *width;x++)
962 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
963 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
964 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
968 in += nextrow; // skip a line
972 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
975 Sys_Error("R_MipReduce: desired size already achieved\n");
979 static void R_Upload(gltexture_t *glt, byte *data)
981 int mip, width, height, internalformat;
985 glBindTexture(GL_TEXTURE_2D, glt->image->texnum);
988 glt->flags &= ~GLTEXF_UPLOAD;
990 if (glt->flags & TEXF_FRAGMENT)
992 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
994 resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
996 Mem_Free(resizebuffer);
997 if (colorconvertbuffer)
998 Mem_Free(colorconvertbuffer);
999 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1000 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1001 if (!resizebuffer || !colorconvertbuffer)
1002 Host_Error("R_Upload: out of memory\n");
1005 if (glt->image->flags & GLTEXF_UPLOAD)
1007 glt->image->flags &= ~GLTEXF_UPLOAD;
1008 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
1009 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1011 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1013 glTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
1017 if (prevbuffer == NULL)
1019 memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
1020 prevbuffer = resizebuffer;
1022 else if (glt->textype->textype == TEXTYPE_QPALETTE)
1024 // promote paletted to RGBA, so we only have to worry about RGB and
1025 // RGBA in the rest of this code
1026 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1027 prevbuffer = colorconvertbuffer;
1030 glTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1035 glt->image->flags &= ~GLTEXF_UPLOAD;
1037 // these are rounded up versions of the size to do better resampling
1038 width = 1;while(width < glt->width) width *= 2;
1039 height = 1;while(height < glt->height) height *= 2;
1041 if (resizebuffersize < width * height * glt->image->bytesperpixel)
1043 resizebuffersize = width * height * glt->image->bytesperpixel;
1045 Mem_Free(resizebuffer);
1046 if (colorconvertbuffer)
1047 Mem_Free(colorconvertbuffer);
1048 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1049 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1050 if (!resizebuffer || !colorconvertbuffer)
1051 Host_Error("R_Upload: out of memory\n");
1054 if (prevbuffer == NULL)
1056 width = glt->image->width;
1057 height = glt->image->height;
1058 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
1059 prevbuffer = resizebuffer;
1063 if (glt->textype->textype == TEXTYPE_QPALETTE)
1065 // promote paletted to RGBA, so we only have to worry about RGB and
1066 // RGBA in the rest of this code
1067 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1068 prevbuffer = colorconvertbuffer;
1071 if (glt->width != width || glt->height != height)
1073 R_ResampleTexture(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel);
1074 prevbuffer = resizebuffer;
1077 // apply picmip/max_size limitations
1078 while (width > glt->image->width || height > glt->image->height)
1080 R_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
1081 prevbuffer = resizebuffer;
1085 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
1087 if (glt->flags & TEXF_ALPHA)
1091 glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1093 if (glt->flags & TEXF_MIPMAP)
1095 while (width > 1 || height > 1)
1097 R_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
1098 prevbuffer = resizebuffer;
1100 glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1104 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1106 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1111 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1113 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1118 static void R_FindImageForTexture(gltexture_t *glt)
1120 int i, j, best, best2, x, y, w, h;
1121 textypeinfo_t *texinfo;
1122 gltexturepool_t *pool;
1123 gltextureimage_t *image, **imagechainpointer;
1124 texinfo = glt->textype;
1131 if (glt->flags & TEXF_FRAGMENT)
1133 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
1135 image = *imagechainpointer;
1136 if (image->type != GLIMAGETYPE_FRAGMENTS)
1138 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
1141 // got a fragments texture, find a place in it if we can
1143 for (best = BLOCK_SIZE, i = 0;i < BLOCK_SIZE - w;i += texinfo->align)
1145 for (best2 = 0, j = 0;j < w;j++)
1147 if (image->blockallocation[i+j] >= best)
1149 if (best2 < image->blockallocation[i+j])
1150 best2 = image->blockallocation[i+j];
1154 // this is a valid spot
1160 if (best + h > BLOCK_SIZE)
1163 for (i = 0;i < w;i++)
1164 image->blockallocation[x + i] = best + h;
1169 image->texturecount++;
1173 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1175 Sys_Error("R_FindImageForTexture: ran out of memory\n");
1176 //memset(image, 0, sizeof(*image));
1177 image->type = GLIMAGETYPE_FRAGMENTS;
1178 image->width = BLOCK_SIZE;
1179 image->height = BLOCK_SIZE;
1180 image->blockallocation = Mem_Alloc(texturemempool, BLOCK_SIZE * sizeof(short));
1181 memset(image->blockallocation, 0, BLOCK_SIZE * sizeof(short));
1185 for (i = 0;i < w;i++)
1186 image->blockallocation[x + i] = y + h;
1190 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
1192 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1194 Sys_Error("R_FindImageForTexture: ran out of memory\n");
1195 //memset(image, 0, sizeof(*image));
1196 image->type = GLIMAGETYPE_TILE;
1197 image->blockallocation = NULL;
1199 // calculate final size
1200 if (r_max_size.integer > realmaxsize)
1201 Cvar_SetValue("r_max_size", realmaxsize);
1202 image->width = 1;while (image->width < glt->width) image->width <<= 1;
1203 image->height = 1;while (image->height < glt->height) image->height <<= 1;
1204 image->width >>= r_picmip.integer;while (image->width > r_max_size.integer) image->width >>= 1;
1205 image->height >>= r_picmip.integer;while (image->height > r_max_size.integer) image->height >>= 1;
1206 if (image->width < 1) image->width = 1;
1207 if (image->height < 1) image->height = 1;
1209 image->glinternalformat = texinfo->glinternalformat;
1210 image->glformat = texinfo->glformat;
1211 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
1212 image->bytesperpixel = texinfo->internalbytesperpixel;
1213 for (i = 1;i < MAX_GLTEXTURES;i++)
1214 if (!gltexnuminuse[i])
1216 if (i < MAX_GLTEXTURES)
1217 gltexnuminuse[image->texnum = i] = true;
1219 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
1220 *imagechainpointer = image;
1221 image->texturecount++;
1228 // note: R_FindImageForTexture must be called before this
1229 static void R_UploadTexture (gltexture_t *glt)
1231 if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
1234 if (glt->flags & GLTEXF_PROCEDURAL)
1238 if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
1241 Mem_Free(texturebuffer);
1242 texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
1243 texturebuffer = Mem_Alloc(texturemempool, texturebuffersize);
1246 glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
1251 R_Upload(glt, glt->inputtexels);
1252 if (glt->inputtexels)
1254 Mem_Free(glt->inputtexels);
1255 glt->inputtexels = NULL;
1256 glt->flags |= GLTEXF_DESTROYED;
1258 else if (glt->flags & GLTEXF_DESTROYED)
1259 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1263 static gltexture_t *R_SetupTexture(gltexturepool_t *pool, char *identifier, int crc, int width, int height, int flags, textypeinfo_t *texinfo, byte *data, int (*generate)(byte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
1266 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1267 //memset(glt, 0, sizeof(gltexture_t));
1270 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1271 strcpy (glt->identifier, identifier);
1274 glt->identifier = NULL;
1276 glt->chain = pool->gltchain;
1277 pool->gltchain = glt;
1280 glt->height = height;
1282 glt->textype = texinfo;
1286 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * texinfo->inputbytesperpixel);
1287 if (glt->inputtexels == NULL)
1288 Sys_Error("R_SetupTexture: out of memory\n");
1289 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
1292 glt->inputtexels = NULL;
1294 glt->generate = generate;
1295 glt->proceduraldatasize = proceduraldatasize;
1296 if (proceduraldatasize)
1298 glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
1299 if (glt->proceduraldata == NULL)
1300 Sys_Error("R_SetupTexture: out of memory\n");
1303 glt->proceduraldata = NULL;
1305 R_FindImageForTexture(glt);
1306 R_PrecacheTexture(glt);
1316 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, byte *data, int textype, int flags)
1320 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1321 textypeinfo_t *texinfo;
1324 if (cls.state == ca_dedicated)
1327 texinfo = R_GetTexTypeInfo(textype, flags);
1330 // if (data == NULL)
1331 // Host_Error("R_LoadTexture: \"%s\" has no data\n", identifier);
1333 if (flags & TEXF_FRAGMENT)
1335 if (width > BLOCK_SIZE || height > BLOCK_SIZE)
1336 Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", BLOCK_SIZE, BLOCK_SIZE);
1337 if ((width * texinfo->internalbytesperpixel) & 3)
1338 Host_Error("R_LoadTexture: incompatible width for fragment");
1341 // clear the alpha flag if the texture has no transparent pixels
1344 case TEXTYPE_QPALETTE:
1345 if (flags & TEXF_ALPHA)
1347 flags &= ~TEXF_ALPHA;
1348 for (i = 0;i < width * height;i++)
1352 flags |= TEXF_ALPHA;
1359 if (flags & TEXF_ALPHA)
1360 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1363 if (flags & TEXF_ALPHA)
1365 flags &= ~TEXF_ALPHA;
1366 for (i = 0;i < width * height;i++)
1368 if (data[i * 4 + 3] < 255)
1370 flags |= TEXF_ALPHA;
1377 Host_Error("R_LoadTexture: unknown texture type\n");
1380 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
1384 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
1386 // see if the texture is already present
1387 if (identifier && (glt = R_FindTexture(pool, identifier)))
1389 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)
1390 return (rtexture_t *)glt; // exact match, use existing
1391 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
1395 return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
1398 rtexture_t *R_ProceduralTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, int textype, int flags, int (*generate)(byte *buffer, int width, int height, void *proceduraldata, int proceduraldatasize), void *proceduraldata, int proceduraldatasize)
1401 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1402 textypeinfo_t *texinfo;
1404 if (cls.state == ca_dedicated)
1407 texinfo = R_GetTexTypeInfo(textype, flags);
1409 // no function is supported, for odd uses
1410 // if (generate == NULL)
1411 // Host_Error("R_ProceduralTexture: \"%s\" has no generate function\n", identifier);
1412 if (flags & TEXF_FRAGMENT)
1414 if (width > BLOCK_SIZE || height > BLOCK_SIZE)
1415 Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", BLOCK_SIZE, BLOCK_SIZE);
1416 if ((width * texinfo->internalbytesperpixel) & 3)
1417 Host_Error("R_ProceduralTexture: incompatible width for fragment");
1420 // see if the texture is already present
1421 if (identifier && (glt = R_FindTexture(pool, identifier)))
1423 if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
1424 return (rtexture_t *)glt; // exact match, use existing
1425 Con_DPrintf("R_LoadTexture: cache mismatch, replacing old texture\n");
1429 return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
1432 int R_TextureHasAlpha(rtexture_t *rt)
1437 glt = (gltexture_t *)rt;
1438 return (glt->flags & TEXF_ALPHA) != 0;
1441 int R_TextureWidth(rtexture_t *rt)
1445 return ((gltexture_t *)rt)->width;
1448 int R_TextureHeight(rtexture_t *rt)
1452 return ((gltexture_t *)rt)->height;
1455 void R_GetFragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1458 float iwidth, iheight;
1460 Host_Error("R_GetFragmentLocation: no texture supplied\n");
1461 glt = (gltexture_t *)rt;
1462 if (glt->flags & TEXF_FRAGMENT)
1468 if (fx1 || fy1 || fx2 || fy2)
1470 iwidth = 1.0f / glt->image->width;
1471 iheight = 1.0f / glt->image->height;
1473 *fx1 = glt->x * iwidth;
1475 *fy1 = glt->y * iheight;
1477 *fx2 = (glt->x + glt->width) * iwidth;
1479 *fy2 = (glt->y + glt->height) * iheight;
1488 if (fx1 || fy1 || fx2 || fy2)
1502 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1504 textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
1505 while ((width * texinfo->internalbytesperpixel) & 3)
1510 void R_UpdateTexture(rtexture_t *rt, byte *data)
1514 Host_Error("R_UpdateTexture: no texture supplied\n");
1516 Host_Error("R_UpdateTexture: no data supplied\n");
1517 glt = (gltexture_t *)rt;
1518 if (!(glt->flags & GLTEXF_PROCEDURAL))
1520 if (glt->inputtexels == NULL)
1522 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * glt->textype->inputbytesperpixel);
1523 if (glt->inputtexels == NULL)
1524 Host_Error("R_UpdateTexture: ran out of memory\n");
1526 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1528 R_Upload(glt, data);