3 cvar_t r_max_size = {0, "r_max_size", "2048"};
4 cvar_t r_max_scrapsize = {0, "r_max_scrapsize", "1024"};
5 cvar_t r_picmip = {0, "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;
15 // note: this must not conflict with TEXF_ flags in r_textures.h
16 // cleared when a texture is uploaded
17 #define GLTEXF_UPLOAD 0x00010000
18 // texture generated by code, also causes permanent GLTEXF_UPLOAD effect
19 #define GLTEXF_PROCEDURAL 0x00020000
20 // bitmask for mismatch checking
21 #define GLTEXF_IMPORTANTBITS (GLTEXF_PROCEDURAL)
22 // set when image is uploaded and freed
23 #define GLTEXF_DESTROYED 0x00040000
25 // size of images which hold fragment textures, ignores picmip and max_size
26 //#define BLOCK_SIZE 256
27 static int block_size;
29 // really this number only governs gltexnuminuse
30 #define MAX_GLTEXTURES 65536
32 // since there is only one set of GL texture numbers, we have to track them
33 // globally, everything else is per texture pool
34 static byte *gltexnuminuse;
39 int inputbytesperpixel;
40 int internalbytesperpixel;
47 static textypeinfo_t textype_qpalette = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 3, 1};
48 static textypeinfo_t textype_rgb = {TEXTYPE_RGB , 3, 3, GL_RGB , 3, 3};
49 static textypeinfo_t textype_rgba = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 3, 1};
50 static textypeinfo_t textype_qpalette_alpha = {TEXTYPE_QPALETTE, 1, 4, GL_RGBA, 4, 1};
51 static textypeinfo_t textype_rgba_alpha = {TEXTYPE_RGBA , 4, 4, GL_RGBA, 4, 1};
53 // a tiling texture (most common type)
54 #define GLIMAGETYPE_TILE 0
55 // a fragments texture (contains one or more fragment textures)
56 #define GLIMAGETYPE_FRAGMENTS 1
58 // a gltextureimage can have one (or more if fragments) gltextures inside
59 typedef struct gltextureimage_s
61 struct gltextureimage_s *imagechain;
63 int type; // one of the GLIMAGETYPE_ values
64 int texnum; // GL texture slot number
66 int bytesperpixel; // bytes per pixel
67 int glformat; // GL_RGB or GL_RGBA
68 int glinternalformat; // 3 or 4
70 short *blockallocation; // fragment allocation
74 typedef struct gltexture_s
76 // pointer to texturepool (check this to see if the texture is allocated)
77 struct gltexturepool_s *pool;
78 // pointer to next texture in texturepool chain
79 struct gltexture_s *chain;
80 // pointer into gltextureimage array
81 gltextureimage_t *image;
82 // name of the texture (this might be removed someday), no duplicates
84 // location in the image, and size
85 int x, y, width, height;
86 // copy of the original texture supplied to the upload function, for re-uploading or deferred uploads (non-precached)
88 // to identify cache mismatchs (this might be removed someday)
90 // flags supplied to the LoadTexture/ProceduralTexture functions
91 // (might be altered to remove TEXF_ALPHA), and GLTEXF_ private flags
93 // procedural texture generation function, called once per frame if the texture is used
94 int (*generate)(byte *buffer, int width, int height, void *parameterdata, int parameterdatasize);
95 // data provided to generate, persistent from call to call
98 int proceduraldatasize;
99 // used only to avoid updating the texture more than once per frame
100 int proceduralframecount;
101 // pointer to one of the textype_ structs
102 textypeinfo_t *textype;
106 #define TEXTUREPOOL_SENTINEL 0xC0DEDBAD
108 typedef struct gltexturepool_s
111 struct gltextureimage_s *imagechain;
112 struct gltexture_s *gltchain;
113 struct gltexturepool_s *next;
117 static gltexturepool_t *gltexturepoolchain = NULL;
119 static byte *resamplerow1 = NULL, *resamplerow2 = NULL;
120 static int resamplerowsize = 0;
121 static byte *resizebuffer = NULL, *colorconvertbuffer;
122 static int resizebuffersize = 0;
123 static byte *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;
209 for (gltpointer = &glt->pool->gltchain;*gltpointer && *gltpointer != glt;gltpointer = &(*gltpointer)->chain);
210 if (*gltpointer == glt)
211 *gltpointer = glt->chain;
213 Host_Error("R_FreeTexture: texture not linked in pool\n");
215 // note: if freeing a fragment texture, this will not make the claimed
216 // space available for new textures unless all other fragments in the
217 // image are also freed
219 image->texturecount--;
220 if (image->texturecount < 1)
222 for (gltimagepointer = &glt->pool->imagechain;*gltimagepointer && *gltimagepointer != image;gltimagepointer = &(*gltimagepointer)->imagechain);
223 if (*gltimagepointer == image)
224 *gltimagepointer = image->imagechain;
226 Host_Error("R_FreeTexture: image not linked in pool\n");
229 texnum = image->texnum;
230 gltexnuminuse[image->texnum] = 0;
231 glDeleteTextures(1, &texnum);
233 if (image->blockallocation)
234 Mem_Free(image->blockallocation);
239 Mem_Free(glt->identifier);
240 if (glt->inputtexels)
241 Mem_Free(glt->inputtexels);
242 if (glt->proceduraldata)
243 Mem_Free(glt->proceduraldata);
247 static gltexture_t *R_FindTexture (gltexturepool_t *pool, char *identifier)
254 for (glt = pool->gltchain;glt;glt = glt->chain)
255 if (glt->identifier && !strcmp (identifier, glt->identifier))
261 rtexturepool_t *R_AllocTexturePool(void)
263 gltexturepool_t *pool;
264 pool = Mem_Alloc(texturemempool, sizeof(gltexturepool_t));
267 //memset(pool, 0, sizeof(gltexturepool_t));
268 pool->next = gltexturepoolchain;
269 gltexturepoolchain = pool;
270 pool->sentinel = TEXTUREPOOL_SENTINEL;
271 return (rtexturepool_t *)pool;
274 void R_FreeTexturePool(rtexturepool_t **rtexturepool)
276 gltexturepool_t *pool, **poolpointer;
277 if (rtexturepool == NULL)
279 if (*rtexturepool == NULL)
281 pool = (gltexturepool_t *)(*rtexturepool);
282 *rtexturepool = NULL;
283 if (pool->sentinel != TEXTUREPOOL_SENTINEL)
284 Host_Error("R_FreeTexturePool: pool already freed\n");
285 for (poolpointer = &gltexturepoolchain;*poolpointer && *poolpointer != pool;poolpointer = &(*poolpointer)->next);
286 if (*poolpointer == pool)
287 *poolpointer = pool->next;
289 Host_Error("R_FreeTexturePool: pool not linked\n");
290 while (pool->gltchain)
291 R_FreeTexture(pool->gltchain);
299 int minification, magnification;
303 static glmode_t modes[] =
305 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
306 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
307 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
308 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
309 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
310 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
313 static void GL_TextureMode_f (void)
316 gltextureimage_t *image;
317 gltexturepool_t *pool;
321 for (i = 0;i < 6;i++)
323 if (gl_filter_min == modes[i].minification)
325 Con_Printf ("%s\n", modes[i].name);
329 Con_Printf ("current filter is unknown???\n");
333 for (i = 0;i < 6;i++)
334 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
338 Con_Printf ("bad filter name\n");
342 gl_filter_min = modes[i].minification;
343 gl_filter_mag = modes[i].magnification;
345 // change all the existing mipmap texture objects
346 // FIXME: force renderer(/client/something?) restart instead?
347 for (pool = gltexturepoolchain;pool;pool = pool->next)
349 for (image = pool->imagechain;image;image = image->imagechain)
351 // only update already uploaded images
352 if (!(image->flags & GLTEXF_UPLOAD))
354 glBindTexture(GL_TEXTURE_2D, image->texnum);
355 if (image->flags & TEXF_MIPMAP)
356 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
358 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
359 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
365 static int R_CalcTexelDataSize (gltexture_t *glt)
367 int width2, height2, size;
368 if (glt->flags & TEXF_FRAGMENT)
369 size = glt->width * glt->height;
372 if (r_max_size.integer > realmaxsize)
373 Cvar_SetValue("r_max_size", realmaxsize);
374 // calculate final size
375 for (width2 = 1;width2 < glt->width;width2 <<= 1);
376 for (height2 = 1;height2 < glt->height;height2 <<= 1);
377 for (width2 >>= r_picmip.integer;width2 > r_max_size.integer;width2 >>= 1);
378 for (height2 >>= r_picmip.integer;height2 > 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 // use the largest scrap texture size we can (not sure if this is really a good idea)
459 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
461 texturemempool = Mem_AllocPool("Textures");
462 gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
463 //memset(gltexnuminuse, 0, MAX_GLTEXTURES);
466 static void r_textures_shutdown(void)
468 rtexturepool_t *temp;
469 while(gltexturepoolchain)
471 temp = (rtexturepool_t *) gltexturepoolchain;
472 R_FreeTexturePool(&temp);
476 if (resizebuffer) Mem_Free(resizebuffer);resizebuffer = NULL;
477 if (colorconvertbuffer) Mem_Free(colorconvertbuffer);colorconvertbuffer = NULL;
478 if (resamplerow1) Mem_Free(resamplerow1);resamplerow1 = NULL;
479 if (resamplerow2) Mem_Free(resamplerow2);resamplerow2 = NULL;
480 if (texturebuffer) Mem_Free(texturebuffer);texturebuffer = NULL;
481 if (gltexnuminuse) Mem_Free(gltexnuminuse);gltexnuminuse = NULL;
483 resizebuffersize = 0;
485 texturebuffersize = 0;
487 colorconvertbuffer = NULL;
490 texturebuffer = NULL;
491 gltexnuminuse = NULL;
492 Mem_FreePool(&texturemempool);
495 static void r_textures_newmap(void)
499 void R_Textures_Init (void)
501 Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
502 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
503 Cvar_RegisterVariable (&r_max_scrapsize);
504 Cvar_RegisterVariable (&r_max_size);
505 Cvar_RegisterVariable (&r_picmip);
506 Cvar_RegisterVariable (&r_lerpimages);
507 Cvar_RegisterVariable (&r_precachetextures);
508 gltexnuminuse = NULL;
510 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
513 static void R_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth, int bytesperpixel)
515 int j, xi, oldx = 0, f, fstep, endx, lerp;
516 fstep = (int) (inwidth*65536.0f/outwidth);
518 if (bytesperpixel == 4)
520 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
525 in += (xi - oldx) * 4;
531 *out++ = (byte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
532 *out++ = (byte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
533 *out++ = (byte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
534 *out++ = (byte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
536 else // last pixel of the line has no pixel to lerp to
545 else if (bytesperpixel == 3)
547 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
552 in += (xi - oldx) * 3;
558 *out++ = (byte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
559 *out++ = (byte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
560 *out++ = (byte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
562 else // last pixel of the line has no pixel to lerp to
571 Sys_Error("R_ResampleTextureLerpLine: unsupported bytesperpixel %i\n", bytesperpixel);
579 static void R_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight, int bytesperpixel)
581 if (resamplerowsize < outwidth*4)
584 Mem_Free(resamplerow1);
586 Mem_Free(resamplerow2);
587 resamplerowsize = outwidth*4;
588 resamplerow1 = Mem_Alloc(texturemempool, resamplerowsize);
589 resamplerow2 = Mem_Alloc(texturemempool, resamplerowsize);
591 #define row1 resamplerow1
592 #define row2 resamplerow2
593 if (bytesperpixel == 4)
595 if (r_lerpimages.integer)
597 int i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
600 fstep = (int) (inheight*65536.0f/outheight);
604 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
605 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
606 for (i = 0, f = 0;i < outheight;i++,f += fstep)
614 inrow = (byte *)indata + inwidth4*yi;
616 memcpy(row1, row2, outwidth4);
618 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
619 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
625 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
678 inrow = (byte *)indata + inwidth4*yi;
680 memcpy(row1, row2, outwidth4);
682 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
685 memcpy(out, row1, outwidth4);
692 unsigned frac, fracstep;
693 // relies on int being 4 bytes
697 fracstep = inwidth*0x10000/outwidth;
698 for (i = 0;i < outheight;i++)
700 inrow = (int *)indata + inwidth*(i*inheight/outheight);
701 frac = fracstep >> 1;
705 out[0] = inrow[frac >> 16];frac += fracstep;
706 out[1] = inrow[frac >> 16];frac += fracstep;
707 out[2] = inrow[frac >> 16];frac += fracstep;
708 out[3] = inrow[frac >> 16];frac += fracstep;
714 out[0] = inrow[frac >> 16];frac += fracstep;
715 out[1] = inrow[frac >> 16];frac += fracstep;
720 out[0] = inrow[frac >> 16];frac += fracstep;
726 else if (bytesperpixel == 3)
728 if (r_lerpimages.integer)
730 int i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
733 fstep = (int) (inheight*65536.0f/outheight);
737 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
738 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
739 for (i = 0, f = 0;i < outheight;i++,f += fstep)
747 inrow = (byte *)indata + inwidth3*yi;
749 memcpy(row1, row2, outwidth3);
751 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
752 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
758 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
804 inrow = (byte *)indata + inwidth3*yi;
806 memcpy(row1, row2, outwidth3);
808 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
811 memcpy(out, row1, outwidth3);
817 int i, j, f, inwidth3 = inwidth * 3;
818 unsigned frac, fracstep;
822 fracstep = inwidth*0x10000/outwidth;
823 for (i = 0;i < outheight;i++)
825 inrow = (byte *)indata + inwidth3*(i*inheight/outheight);
826 frac = fracstep >> 1;
830 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
831 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
832 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
833 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
838 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
839 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
844 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
851 Sys_Error("R_ResampleTexture: unsupported bytesperpixel %i\n", bytesperpixel);
856 // in can be the same as out
857 static void R_MipReduce(byte *in, byte *out, int *width, int *height, int destwidth, int destheight, int bytesperpixel)
860 nextrow = *width * bytesperpixel;
861 if (*width > destwidth)
864 if (*height > destheight)
868 if (bytesperpixel == 4)
870 for (y = 0;y < *height;y++)
872 for (x = 0;x < *width;x++)
874 out[0] = (byte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
875 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
876 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
877 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
881 in += nextrow; // skip a line
884 else if (bytesperpixel == 3)
886 for (y = 0;y < *height;y++)
888 for (x = 0;x < *width;x++)
890 out[0] = (byte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
891 out[1] = (byte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
892 out[2] = (byte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
896 in += nextrow; // skip a line
900 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
905 if (bytesperpixel == 4)
907 for (y = 0;y < *height;y++)
909 for (x = 0;x < *width;x++)
911 out[0] = (byte) ((in[0] + in[4]) >> 1);
912 out[1] = (byte) ((in[1] + in[5]) >> 1);
913 out[2] = (byte) ((in[2] + in[6]) >> 1);
914 out[3] = (byte) ((in[3] + in[7]) >> 1);
920 else if (bytesperpixel == 3)
922 for (y = 0;y < *height;y++)
924 for (x = 0;x < *width;x++)
926 out[0] = (byte) ((in[0] + in[3]) >> 1);
927 out[1] = (byte) ((in[1] + in[4]) >> 1);
928 out[2] = (byte) ((in[2] + in[5]) >> 1);
935 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
940 if (*height > destheight)
944 if (bytesperpixel == 4)
946 for (y = 0;y < *height;y++)
948 for (x = 0;x < *width;x++)
950 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
951 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
952 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
953 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
957 in += nextrow; // skip a line
960 else if (bytesperpixel == 3)
962 for (y = 0;y < *height;y++)
964 for (x = 0;x < *width;x++)
966 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
967 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
968 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
972 in += nextrow; // skip a line
976 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
979 Sys_Error("R_MipReduce: desired size already achieved\n");
983 static void R_Upload(gltexture_t *glt, byte *data)
985 int mip, width, height, internalformat;
989 glBindTexture(GL_TEXTURE_2D, glt->image->texnum);
992 glt->flags &= ~GLTEXF_UPLOAD;
994 if (glt->flags & TEXF_FRAGMENT)
996 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
998 resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
1000 Mem_Free(resizebuffer);
1001 if (colorconvertbuffer)
1002 Mem_Free(colorconvertbuffer);
1003 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1004 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1005 if (!resizebuffer || !colorconvertbuffer)
1006 Host_Error("R_Upload: out of memory\n");
1009 if (glt->image->flags & GLTEXF_UPLOAD)
1011 glt->image->flags &= ~GLTEXF_UPLOAD;
1012 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
1013 glTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
1015 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1017 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1021 if (prevbuffer == NULL)
1023 memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
1024 prevbuffer = resizebuffer;
1026 else if (glt->textype->textype == TEXTYPE_QPALETTE)
1028 // promote paletted to RGBA, so we only have to worry about RGB and
1029 // RGBA in the rest of this code
1030 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1031 prevbuffer = colorconvertbuffer;
1034 glTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1039 glt->image->flags &= ~GLTEXF_UPLOAD;
1041 // these are rounded up versions of the size to do better resampling
1042 for (width = 1;width < glt->width;width <<= 1);
1043 for (height = 1;height < glt->height;height <<= 1);
1045 if (resizebuffersize < width * height * glt->image->bytesperpixel)
1047 resizebuffersize = width * height * glt->image->bytesperpixel;
1049 Mem_Free(resizebuffer);
1050 if (colorconvertbuffer)
1051 Mem_Free(colorconvertbuffer);
1052 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1053 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1054 if (!resizebuffer || !colorconvertbuffer)
1055 Host_Error("R_Upload: out of memory\n");
1058 if (prevbuffer == NULL)
1060 width = glt->image->width;
1061 height = glt->image->height;
1062 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
1063 prevbuffer = resizebuffer;
1067 if (glt->textype->textype == TEXTYPE_QPALETTE)
1069 // promote paletted to RGBA, so we only have to worry about RGB and
1070 // RGBA in the rest of this code
1071 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1072 prevbuffer = colorconvertbuffer;
1075 if (glt->width != width || glt->height != height)
1077 R_ResampleTexture(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel);
1078 prevbuffer = resizebuffer;
1081 // apply picmip/max_size limitations
1082 while (width > glt->image->width || height > glt->image->height)
1084 R_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
1085 prevbuffer = resizebuffer;
1089 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
1091 if (glt->flags & TEXF_ALPHA)
1095 glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1097 if (glt->flags & TEXF_MIPMAP)
1099 while (width > 1 || height > 1)
1101 R_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
1102 prevbuffer = resizebuffer;
1104 glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1108 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1115 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1117 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1122 static void R_FindImageForTexture(gltexture_t *glt)
1124 int i, j, best, best2, x, y, w, h;
1125 textypeinfo_t *texinfo;
1126 gltexturepool_t *pool;
1127 gltextureimage_t *image, **imagechainpointer;
1128 texinfo = glt->textype;
1135 if (glt->flags & TEXF_FRAGMENT)
1137 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
1139 image = *imagechainpointer;
1140 if (image->type != GLIMAGETYPE_FRAGMENTS)
1142 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
1145 // got a fragments texture, find a place in it if we can
1147 for (best = block_size, i = 0;i < block_size - w;i += texinfo->align)
1149 for (best2 = 0, j = 0;j < w;j++)
1151 if (image->blockallocation[i+j] >= best)
1153 if (best2 < image->blockallocation[i+j])
1154 best2 = image->blockallocation[i+j];
1158 // this is a valid spot
1164 if (best + h > block_size)
1167 for (i = 0;i < w;i++)
1168 image->blockallocation[x + i] = best + h;
1173 image->texturecount++;
1177 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1179 Sys_Error("R_FindImageForTexture: ran out of memory\n");
1180 //memset(image, 0, sizeof(*image));
1181 image->type = GLIMAGETYPE_FRAGMENTS;
1182 image->width = block_size;
1183 image->height = block_size;
1184 image->blockallocation = Mem_Alloc(texturemempool, block_size * sizeof(short));
1185 memset(image->blockallocation, 0, block_size * sizeof(short));
1189 for (i = 0;i < w;i++)
1190 image->blockallocation[x + i] = y + h;
1194 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
1196 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1198 Sys_Error("R_FindImageForTexture: ran out of memory\n");
1199 //memset(image, 0, sizeof(*image));
1200 image->type = GLIMAGETYPE_TILE;
1201 image->blockallocation = NULL;
1203 // calculate final size
1204 if (r_max_size.integer > realmaxsize)
1205 Cvar_SetValue("r_max_size", realmaxsize);
1206 for (image->width = 1;image->width < glt->width;image->width <<= 1);
1207 for (image->height = 1;image->height < glt->height;image->height <<= 1);
1208 for (image->width >>= r_picmip.integer;image->width > r_max_size.integer;image->width >>= 1);
1209 for (image->height >>= r_picmip.integer;image->height > r_max_size.integer;image->height >>= 1);
1210 if (image->width < 1) image->width = 1;
1211 if (image->height < 1) image->height = 1;
1213 image->glinternalformat = texinfo->glinternalformat;
1214 image->glformat = texinfo->glformat;
1215 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
1216 image->bytesperpixel = texinfo->internalbytesperpixel;
1217 for (i = 1;i < MAX_GLTEXTURES;i++)
1218 if (!gltexnuminuse[i])
1220 if (i < MAX_GLTEXTURES)
1221 gltexnuminuse[image->texnum = i] = true;
1223 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
1224 *imagechainpointer = image;
1225 image->texturecount++;
1232 // note: R_FindImageForTexture must be called before this
1233 static void R_UploadTexture (gltexture_t *glt)
1235 if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
1238 if (glt->flags & GLTEXF_PROCEDURAL)
1242 if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
1245 Mem_Free(texturebuffer);
1246 texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
1247 texturebuffer = Mem_Alloc(texturemempool, texturebuffersize);
1250 glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
1255 R_Upload(glt, glt->inputtexels);
1256 if (glt->inputtexels)
1258 Mem_Free(glt->inputtexels);
1259 glt->inputtexels = NULL;
1260 glt->flags |= GLTEXF_DESTROYED;
1262 else if (glt->flags & GLTEXF_DESTROYED)
1263 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1267 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)
1270 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1271 //memset(glt, 0, sizeof(gltexture_t));
1274 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1275 strcpy (glt->identifier, identifier);
1278 glt->identifier = NULL;
1280 glt->chain = pool->gltchain;
1281 pool->gltchain = glt;
1284 glt->height = height;
1286 glt->textype = texinfo;
1290 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * texinfo->inputbytesperpixel);
1291 if (glt->inputtexels == NULL)
1292 Sys_Error("R_SetupTexture: out of memory\n");
1293 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
1296 glt->inputtexels = NULL;
1298 glt->generate = generate;
1299 glt->proceduraldatasize = proceduraldatasize;
1300 if (proceduraldatasize)
1302 glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
1303 if (glt->proceduraldata == NULL)
1304 Sys_Error("R_SetupTexture: out of memory\n");
1307 glt->proceduraldata = NULL;
1309 R_FindImageForTexture(glt);
1310 R_PrecacheTexture(glt);
1320 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, byte *data, int textype, int flags)
1324 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1325 textypeinfo_t *texinfo;
1328 if (cls.state == ca_dedicated)
1331 texinfo = R_GetTexTypeInfo(textype, flags);
1334 // if (data == NULL)
1335 // Host_Error("R_LoadTexture: \"%s\" has no data\n", identifier);
1337 if (flags & TEXF_FRAGMENT)
1339 if (width > block_size || height > block_size)
1340 Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1341 if ((width * texinfo->internalbytesperpixel) & 3)
1342 Host_Error("R_LoadTexture: incompatible width for fragment");
1345 // clear the alpha flag if the texture has no transparent pixels
1348 case TEXTYPE_QPALETTE:
1349 if (flags & TEXF_ALPHA)
1351 flags &= ~TEXF_ALPHA;
1352 for (i = 0;i < width * height;i++)
1356 flags |= TEXF_ALPHA;
1363 if (flags & TEXF_ALPHA)
1364 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1367 if (flags & TEXF_ALPHA)
1369 flags &= ~TEXF_ALPHA;
1370 for (i = 0;i < width * height;i++)
1372 if (data[i * 4 + 3] < 255)
1374 flags |= TEXF_ALPHA;
1381 Host_Error("R_LoadTexture: unknown texture type\n");
1384 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
1388 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
1390 // see if the texture is already present
1391 if (identifier && (glt = R_FindTexture(pool, identifier)))
1393 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)
1394 return (rtexture_t *)glt; // exact match, use existing
1395 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
1399 return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
1402 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)
1405 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1406 textypeinfo_t *texinfo;
1408 if (cls.state == ca_dedicated)
1411 texinfo = R_GetTexTypeInfo(textype, flags);
1413 // no function is supported, for odd uses
1414 // if (generate == NULL)
1415 // Host_Error("R_ProceduralTexture: \"%s\" has no generate function\n", identifier);
1416 if (flags & TEXF_FRAGMENT)
1418 if (width > block_size || height > block_size)
1419 Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1420 if ((width * texinfo->internalbytesperpixel) & 3)
1421 Host_Error("R_ProceduralTexture: incompatible width for fragment");
1424 // see if the texture is already present
1425 if (identifier && (glt = R_FindTexture(pool, identifier)))
1427 if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
1428 return (rtexture_t *)glt; // exact match, use existing
1429 Con_DPrintf("R_LoadTexture: cache mismatch, replacing old texture\n");
1433 return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
1436 int R_TextureHasAlpha(rtexture_t *rt)
1441 glt = (gltexture_t *)rt;
1442 return (glt->flags & TEXF_ALPHA) != 0;
1445 int R_TextureWidth(rtexture_t *rt)
1449 return ((gltexture_t *)rt)->width;
1452 int R_TextureHeight(rtexture_t *rt)
1456 return ((gltexture_t *)rt)->height;
1459 void R_FragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1462 float iwidth, iheight;
1463 if (cls.state == ca_dedicated)
1469 if (fx1 || fy1 || fx2 || fy2)
1483 Host_Error("R_FragmentLocation: no texture supplied\n");
1484 glt = (gltexture_t *)rt;
1485 if (glt->flags & TEXF_FRAGMENT)
1491 if (fx1 || fy1 || fx2 || fy2)
1493 iwidth = 1.0f / glt->image->width;
1494 iheight = 1.0f / glt->image->height;
1496 *fx1 = glt->x * iwidth;
1498 *fy1 = glt->y * iheight;
1500 *fx2 = (glt->x + glt->width) * iwidth;
1502 *fy2 = (glt->y + glt->height) * iheight;
1511 if (fx1 || fy1 || fx2 || fy2)
1525 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1527 textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
1528 while ((width * texinfo->internalbytesperpixel) & 3)
1533 void R_UpdateTexture(rtexture_t *rt, byte *data)
1537 Host_Error("R_UpdateTexture: no texture supplied\n");
1539 Host_Error("R_UpdateTexture: no data supplied\n");
1540 glt = (gltexture_t *)rt;
1541 if (!(glt->flags & GLTEXF_PROCEDURAL))
1543 if (glt->inputtexels == NULL)
1545 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * glt->textype->inputbytesperpixel);
1546 if (glt->inputtexels == NULL)
1547 Host_Error("R_UpdateTexture: ran out of memory\n");
1549 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1551 R_Upload(glt, data);