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 width2 = 1;while (width2 < glt->width) width2 <<= 1;
376 height2 = 1;while (height2 < glt->height) height2 <<= 1;
377 width2 >>= (int) r_picmip.integer;
378 height2 >>= (int) r_picmip.integer;
379 while (width2 > (int) r_max_size.integer) width2 >>= 1;
380 while (height2 > (int) r_max_size.integer) height2 >>= 1;
381 if (width2 < 1) width2 = 1;
382 if (height2 < 1) height2 = 1;
385 if (glt->flags & TEXF_MIPMAP)
387 while (width2 > 1 || height2 > 1)
389 size += width2 * height2;
395 size++; // count the last 1x1 mipmap
398 size = width2*height2;
400 size *= glt->textype->internalbytesperpixel;
405 void R_TextureStats_PrintTotal(void)
407 int glsize, inputsize, total = 0, totalt = 0, totalp = 0, loaded = 0, loadedt = 0, loadedp = 0;
409 gltexturepool_t *pool;
410 for (pool = gltexturepoolchain;pool;pool = pool->next)
412 for (glt = pool->gltchain;glt;glt = glt->chain)
414 glsize = R_CalcTexelDataSize(glt);
415 inputsize = glt->width * glt->height * glt->textype->inputbytesperpixel;
420 if (!(glt->flags & GLTEXF_UPLOAD))
424 loadedp += inputsize;
428 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);
431 static void R_TextureStats_f(void)
435 gltexturepool_t *pool;
436 Con_Printf("glsize input crc loaded mip alpha name\n");
437 for (pool = gltexturepoolchain;pool;pool = pool->next)
439 for (glt = pool->gltchain;glt;glt = glt->chain)
441 loaded = !(glt->flags & GLTEXF_UPLOAD);
442 if (glt->flags & GLTEXF_PROCEDURAL)
443 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>");
445 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>");
447 Con_Printf("pool %10p\n", pool);
449 R_TextureStats_PrintTotal();
452 char engineversion[40];
454 static void r_textures_start(void)
456 // deal with size limits of various drivers (3dfx in particular)
457 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &realmaxsize);
460 // use the largest scrap texture size we can (not sure if this is really a good idea)
461 for (block_size = 1;block_size < realmaxsize && block_size < r_max_scrapsize.integer;block_size <<= 1);
463 texturemempool = Mem_AllocPool("Textures");
464 gltexnuminuse = Mem_Alloc(texturemempool, MAX_GLTEXTURES);
465 //memset(gltexnuminuse, 0, MAX_GLTEXTURES);
468 static void r_textures_shutdown(void)
470 rtexturepool_t *temp;
471 while(gltexturepoolchain)
473 temp = (rtexturepool_t *) gltexturepoolchain;
474 R_FreeTexturePool(&temp);
478 if (resizebuffer) Mem_Free(resizebuffer);resizebuffer = NULL;
479 if (colorconvertbuffer) Mem_Free(colorconvertbuffer);colorconvertbuffer = NULL;
480 if (resamplerow1) Mem_Free(resamplerow1);resamplerow1 = NULL;
481 if (resamplerow2) Mem_Free(resamplerow2);resamplerow2 = NULL;
482 if (texturebuffer) Mem_Free(texturebuffer);texturebuffer = NULL;
483 if (gltexnuminuse) Mem_Free(gltexnuminuse);gltexnuminuse = NULL;
485 resizebuffersize = 0;
487 texturebuffersize = 0;
489 colorconvertbuffer = NULL;
492 texturebuffer = NULL;
493 gltexnuminuse = NULL;
494 Mem_FreePool(&texturemempool);
497 static void r_textures_newmap(void)
501 void R_Textures_Init (void)
503 Cmd_AddCommand ("gl_texturemode", &GL_TextureMode_f);
504 Cmd_AddCommand("r_texturestats", R_TextureStats_f);
505 Cvar_RegisterVariable (&r_max_scrapsize);
506 Cvar_RegisterVariable (&r_max_size);
507 Cvar_RegisterVariable (&r_picmip);
508 Cvar_RegisterVariable (&r_lerpimages);
509 Cvar_RegisterVariable (&r_precachetextures);
510 gltexnuminuse = NULL;
512 R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
515 static void R_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth, int bytesperpixel)
517 int j, xi, oldx = 0, f, fstep, endx, lerp;
518 fstep = (int) (inwidth*65536.0f/outwidth);
520 if (bytesperpixel == 4)
522 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
527 in += (xi - oldx) * 4;
533 *out++ = (byte) ((((in[4] - in[0]) * lerp) >> 16) + in[0]);
534 *out++ = (byte) ((((in[5] - in[1]) * lerp) >> 16) + in[1]);
535 *out++ = (byte) ((((in[6] - in[2]) * lerp) >> 16) + in[2]);
536 *out++ = (byte) ((((in[7] - in[3]) * lerp) >> 16) + in[3]);
538 else // last pixel of the line has no pixel to lerp to
547 else if (bytesperpixel == 3)
549 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
554 in += (xi - oldx) * 3;
560 *out++ = (byte) ((((in[3] - in[0]) * lerp) >> 16) + in[0]);
561 *out++ = (byte) ((((in[4] - in[1]) * lerp) >> 16) + in[1]);
562 *out++ = (byte) ((((in[5] - in[2]) * lerp) >> 16) + in[2]);
564 else // last pixel of the line has no pixel to lerp to
573 Sys_Error("R_ResampleTextureLerpLine: unsupported bytesperpixel %i\n", bytesperpixel);
581 static void R_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight, int bytesperpixel)
583 if (resamplerowsize < outwidth*4)
586 Mem_Free(resamplerow1);
588 Mem_Free(resamplerow2);
589 resamplerowsize = outwidth*4;
590 resamplerow1 = Mem_Alloc(texturemempool, resamplerowsize);
591 resamplerow2 = Mem_Alloc(texturemempool, resamplerowsize);
593 #define row1 resamplerow1
594 #define row2 resamplerow2
595 if (bytesperpixel == 4)
597 if (r_lerpimages.integer)
599 int i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth4 = inwidth*4, outwidth4 = outwidth*4;
602 fstep = (int) (inheight*65536.0f/outheight);
606 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
607 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
608 for (i = 0, f = 0;i < outheight;i++,f += fstep)
616 inrow = (byte *)indata + inwidth4*yi;
618 memcpy(row1, row2, outwidth4);
620 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
621 R_ResampleTextureLerpLine (inrow + inwidth4, row2, inwidth, outwidth, bytesperpixel);
627 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
680 inrow = (byte *)indata + inwidth4*yi;
682 memcpy(row1, row2, outwidth4);
684 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
687 memcpy(out, row1, outwidth4);
694 unsigned frac, fracstep;
695 // relies on int being 4 bytes
699 fracstep = inwidth*0x10000/outwidth;
700 for (i = 0;i < outheight;i++)
702 inrow = (int *)indata + inwidth*(i*inheight/outheight);
703 frac = fracstep >> 1;
707 out[0] = inrow[frac >> 16];frac += fracstep;
708 out[1] = inrow[frac >> 16];frac += fracstep;
709 out[2] = inrow[frac >> 16];frac += fracstep;
710 out[3] = inrow[frac >> 16];frac += fracstep;
716 out[0] = inrow[frac >> 16];frac += fracstep;
717 out[1] = inrow[frac >> 16];frac += fracstep;
722 out[0] = inrow[frac >> 16];frac += fracstep;
728 else if (bytesperpixel == 3)
730 if (r_lerpimages.integer)
732 int i, j, yi, oldy, f, fstep, lerp, endy = (inheight-1), inwidth3 = inwidth * 3, outwidth3 = outwidth * 3;
735 fstep = (int) (inheight*65536.0f/outheight);
739 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
740 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
741 for (i = 0, f = 0;i < outheight;i++,f += fstep)
749 inrow = (byte *)indata + inwidth3*yi;
751 memcpy(row1, row2, outwidth3);
753 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
754 R_ResampleTextureLerpLine (inrow + inwidth3, row2, inwidth, outwidth, bytesperpixel);
760 #define LERPBYTE(i) out[i] = (byte) ((((row2[i] - row1[i]) * lerp) >> 16) + row1[i])
806 inrow = (byte *)indata + inwidth3*yi;
808 memcpy(row1, row2, outwidth3);
810 R_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth, bytesperpixel);
813 memcpy(out, row1, outwidth3);
819 int i, j, f, inwidth3 = inwidth * 3;
820 unsigned frac, fracstep;
824 fracstep = inwidth*0x10000/outwidth;
825 for (i = 0;i < outheight;i++)
827 inrow = (byte *)indata + inwidth3*(i*inheight/outheight);
828 frac = fracstep >> 1;
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;
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;
841 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
846 f = (frac >> 16)*3;*out++ = inrow[f+0];*out++ = inrow[f+1];*out++ = inrow[f+2];frac += fracstep;
853 Sys_Error("R_ResampleTexture: unsupported bytesperpixel %i\n", bytesperpixel);
858 // in can be the same as out
859 static void R_MipReduce(byte *in, byte *out, int *width, int *height, int destwidth, int destheight, int bytesperpixel)
862 nextrow = *width * bytesperpixel;
863 if (*width > destwidth)
866 if (*height > destheight)
870 if (bytesperpixel == 4)
872 for (y = 0;y < *height;y++)
874 for (x = 0;x < *width;x++)
876 out[0] = (byte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
877 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
878 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
879 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
883 in += nextrow; // skip a line
886 else if (bytesperpixel == 3)
888 for (y = 0;y < *height;y++)
890 for (x = 0;x < *width;x++)
892 out[0] = (byte) ((in[0] + in[3] + in[nextrow ] + in[nextrow+3]) >> 2);
893 out[1] = (byte) ((in[1] + in[4] + in[nextrow+1] + in[nextrow+4]) >> 2);
894 out[2] = (byte) ((in[2] + in[5] + in[nextrow+2] + in[nextrow+5]) >> 2);
898 in += nextrow; // skip a line
902 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
907 if (bytesperpixel == 4)
909 for (y = 0;y < *height;y++)
911 for (x = 0;x < *width;x++)
913 out[0] = (byte) ((in[0] + in[4]) >> 1);
914 out[1] = (byte) ((in[1] + in[5]) >> 1);
915 out[2] = (byte) ((in[2] + in[6]) >> 1);
916 out[3] = (byte) ((in[3] + in[7]) >> 1);
922 else if (bytesperpixel == 3)
924 for (y = 0;y < *height;y++)
926 for (x = 0;x < *width;x++)
928 out[0] = (byte) ((in[0] + in[3]) >> 1);
929 out[1] = (byte) ((in[1] + in[4]) >> 1);
930 out[2] = (byte) ((in[2] + in[5]) >> 1);
937 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
942 if (*height > destheight)
946 if (bytesperpixel == 4)
948 for (y = 0;y < *height;y++)
950 for (x = 0;x < *width;x++)
952 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
953 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
954 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
955 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
959 in += nextrow; // skip a line
962 else if (bytesperpixel == 3)
964 for (y = 0;y < *height;y++)
966 for (x = 0;x < *width;x++)
968 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
969 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
970 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
974 in += nextrow; // skip a line
978 Sys_Error("R_MipReduce: unsupported bytesperpixel %i\n", bytesperpixel);
981 Sys_Error("R_MipReduce: desired size already achieved\n");
985 static void R_Upload(gltexture_t *glt, byte *data)
987 int mip, width, height, internalformat;
991 glBindTexture(GL_TEXTURE_2D, glt->image->texnum);
994 glt->flags &= ~GLTEXF_UPLOAD;
996 if (glt->flags & TEXF_FRAGMENT)
998 if (resizebuffersize < glt->image->width * glt->image->height * glt->image->bytesperpixel)
1000 resizebuffersize = glt->image->width * glt->image->height * glt->image->bytesperpixel;
1002 Mem_Free(resizebuffer);
1003 if (colorconvertbuffer)
1004 Mem_Free(colorconvertbuffer);
1005 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1006 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1007 if (!resizebuffer || !colorconvertbuffer)
1008 Host_Error("R_Upload: out of memory\n");
1011 if (glt->image->flags & GLTEXF_UPLOAD)
1013 glt->image->flags &= ~GLTEXF_UPLOAD;
1014 memset(resizebuffer, 255, glt->image->width * glt->image->height * glt->image->bytesperpixel);
1015 glTexImage2D (GL_TEXTURE_2D, 0, glt->image->glinternalformat, glt->image->width, glt->image->height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, resizebuffer);
1017 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1019 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1023 if (prevbuffer == NULL)
1025 memset(resizebuffer, 255, glt->width * glt->height * glt->image->bytesperpixel);
1026 prevbuffer = resizebuffer;
1028 else if (glt->textype->textype == TEXTYPE_QPALETTE)
1030 // promote paletted to RGBA, so we only have to worry about RGB and
1031 // RGBA in the rest of this code
1032 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1033 prevbuffer = colorconvertbuffer;
1036 glTexSubImage2D(GL_TEXTURE_2D, 0, glt->x, glt->y, glt->width, glt->height, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1041 glt->image->flags &= ~GLTEXF_UPLOAD;
1043 // these are rounded up versions of the size to do better resampling
1044 width = 1;while(width < glt->width) width *= 2;
1045 height = 1;while(height < glt->height) height *= 2;
1047 if (resizebuffersize < width * height * glt->image->bytesperpixel)
1049 resizebuffersize = width * height * glt->image->bytesperpixel;
1051 Mem_Free(resizebuffer);
1052 if (colorconvertbuffer)
1053 Mem_Free(colorconvertbuffer);
1054 resizebuffer = Mem_Alloc(texturemempool, resizebuffersize);
1055 colorconvertbuffer = Mem_Alloc(texturemempool, resizebuffersize);
1056 if (!resizebuffer || !colorconvertbuffer)
1057 Host_Error("R_Upload: out of memory\n");
1060 if (prevbuffer == NULL)
1062 width = glt->image->width;
1063 height = glt->image->height;
1064 memset(resizebuffer, 255, width * height * glt->image->bytesperpixel);
1065 prevbuffer = resizebuffer;
1069 if (glt->textype->textype == TEXTYPE_QPALETTE)
1071 // promote paletted to RGBA, so we only have to worry about RGB and
1072 // RGBA in the rest of this code
1073 Image_Copy8bitRGBA(prevbuffer, colorconvertbuffer, glt->width * glt->height, d_8to24table);
1074 prevbuffer = colorconvertbuffer;
1077 if (glt->width != width || glt->height != height)
1079 R_ResampleTexture(prevbuffer, glt->width, glt->height, resizebuffer, width, height, glt->image->bytesperpixel);
1080 prevbuffer = resizebuffer;
1083 // apply picmip/max_size limitations
1084 while (width > glt->image->width || height > glt->image->height)
1086 R_MipReduce(prevbuffer, resizebuffer, &width, &height, glt->image->width, glt->image->height, glt->image->bytesperpixel);
1087 prevbuffer = resizebuffer;
1091 // 3 and 4 are converted by the driver to it's preferred format for the current display mode
1093 if (glt->flags & TEXF_ALPHA)
1097 glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1099 if (glt->flags & TEXF_MIPMAP)
1101 while (width > 1 || height > 1)
1103 R_MipReduce(prevbuffer, resizebuffer, &width, &height, 1, 1, glt->image->bytesperpixel);
1104 prevbuffer = resizebuffer;
1106 glTexImage2D(GL_TEXTURE_2D, mip++, internalformat, width, height, 0, glt->image->glformat, GL_UNSIGNED_BYTE, prevbuffer);
1110 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
1112 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1117 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_mag);
1119 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_mag);
1124 static void R_FindImageForTexture(gltexture_t *glt)
1126 int i, j, best, best2, x, y, w, h;
1127 textypeinfo_t *texinfo;
1128 gltexturepool_t *pool;
1129 gltextureimage_t *image, **imagechainpointer;
1130 texinfo = glt->textype;
1137 if (glt->flags & TEXF_FRAGMENT)
1139 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain)
1141 image = *imagechainpointer;
1142 if (image->type != GLIMAGETYPE_FRAGMENTS)
1144 if (image->glformat != texinfo->glformat || image->glinternalformat != texinfo->glinternalformat)
1147 // got a fragments texture, find a place in it if we can
1149 for (best = block_size, i = 0;i < block_size - w;i += texinfo->align)
1151 for (best2 = 0, j = 0;j < w;j++)
1153 if (image->blockallocation[i+j] >= best)
1155 if (best2 < image->blockallocation[i+j])
1156 best2 = image->blockallocation[i+j];
1160 // this is a valid spot
1166 if (best + h > block_size)
1169 for (i = 0;i < w;i++)
1170 image->blockallocation[x + i] = best + h;
1175 image->texturecount++;
1179 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1181 Sys_Error("R_FindImageForTexture: ran out of memory\n");
1182 //memset(image, 0, sizeof(*image));
1183 image->type = GLIMAGETYPE_FRAGMENTS;
1184 image->width = block_size;
1185 image->height = block_size;
1186 image->blockallocation = Mem_Alloc(texturemempool, block_size * sizeof(short));
1187 memset(image->blockallocation, 0, block_size * sizeof(short));
1191 for (i = 0;i < w;i++)
1192 image->blockallocation[x + i] = y + h;
1196 for (imagechainpointer = &pool->imagechain;*imagechainpointer;imagechainpointer = &(*imagechainpointer)->imagechain);
1198 image = Mem_Alloc(texturemempool, sizeof(gltextureimage_t));
1200 Sys_Error("R_FindImageForTexture: ran out of memory\n");
1201 //memset(image, 0, sizeof(*image));
1202 image->type = GLIMAGETYPE_TILE;
1203 image->blockallocation = NULL;
1205 // calculate final size
1206 if (r_max_size.integer > realmaxsize)
1207 Cvar_SetValue("r_max_size", realmaxsize);
1208 image->width = 1;while (image->width < glt->width) image->width <<= 1;
1209 image->height = 1;while (image->height < glt->height) image->height <<= 1;
1210 image->width >>= r_picmip.integer;while (image->width > r_max_size.integer) image->width >>= 1;
1211 image->height >>= r_picmip.integer;while (image->height > r_max_size.integer) image->height >>= 1;
1212 if (image->width < 1) image->width = 1;
1213 if (image->height < 1) image->height = 1;
1215 image->glinternalformat = texinfo->glinternalformat;
1216 image->glformat = texinfo->glformat;
1217 image->flags = (glt->flags & (TEXF_MIPMAP | TEXF_ALPHA)) | GLTEXF_UPLOAD;
1218 image->bytesperpixel = texinfo->internalbytesperpixel;
1219 for (i = 1;i < MAX_GLTEXTURES;i++)
1220 if (!gltexnuminuse[i])
1222 if (i < MAX_GLTEXTURES)
1223 gltexnuminuse[image->texnum = i] = true;
1225 Sys_Error("R_FindImageForTexture: ran out of GL textures\n");
1226 *imagechainpointer = image;
1227 image->texturecount++;
1234 // note: R_FindImageForTexture must be called before this
1235 static void R_UploadTexture (gltexture_t *glt)
1237 if (!(glt->flags & (GLTEXF_UPLOAD | GLTEXF_PROCEDURAL)))
1240 if (glt->flags & GLTEXF_PROCEDURAL)
1244 if (texturebuffersize < glt->width * glt->height * glt->textype->inputbytesperpixel)
1247 Mem_Free(texturebuffer);
1248 texturebuffersize = glt->width * glt->height * glt->textype->inputbytesperpixel;
1249 texturebuffer = Mem_Alloc(texturemempool, texturebuffersize);
1252 glt->generate(texturebuffer, glt->width, glt->height, (void *)glt->proceduraldata, glt->proceduraldatasize);
1257 R_Upload(glt, glt->inputtexels);
1258 if (glt->inputtexels)
1260 Mem_Free(glt->inputtexels);
1261 glt->inputtexels = NULL;
1262 glt->flags |= GLTEXF_DESTROYED;
1264 else if (glt->flags & GLTEXF_DESTROYED)
1265 Con_Printf("R_UploadTexture: Texture %s already uploaded and destroyed. Can not upload original image again. Uploaded blank texture.\n", glt->identifier);
1269 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)
1272 glt = Mem_Alloc(texturemempool, sizeof(gltexture_t));
1273 //memset(glt, 0, sizeof(gltexture_t));
1276 glt->identifier = Mem_Alloc(texturemempool, strlen(identifier)+1);
1277 strcpy (glt->identifier, identifier);
1280 glt->identifier = NULL;
1282 glt->chain = pool->gltchain;
1283 pool->gltchain = glt;
1286 glt->height = height;
1288 glt->textype = texinfo;
1292 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * texinfo->inputbytesperpixel);
1293 if (glt->inputtexels == NULL)
1294 Sys_Error("R_SetupTexture: out of memory\n");
1295 memcpy(glt->inputtexels, data, glt->width * glt->height * texinfo->inputbytesperpixel);
1298 glt->inputtexels = NULL;
1300 glt->generate = generate;
1301 glt->proceduraldatasize = proceduraldatasize;
1302 if (proceduraldatasize)
1304 glt->proceduraldata = Mem_Alloc(texturemempool, proceduraldatasize);
1305 if (glt->proceduraldata == NULL)
1306 Sys_Error("R_SetupTexture: out of memory\n");
1309 glt->proceduraldata = NULL;
1311 R_FindImageForTexture(glt);
1312 R_PrecacheTexture(glt);
1322 rtexture_t *R_LoadTexture (rtexturepool_t *rtexturepool, char *identifier, int width, int height, byte *data, int textype, int flags)
1326 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1327 textypeinfo_t *texinfo;
1330 if (cls.state == ca_dedicated)
1333 texinfo = R_GetTexTypeInfo(textype, flags);
1336 // if (data == NULL)
1337 // Host_Error("R_LoadTexture: \"%s\" has no data\n", identifier);
1339 if (flags & TEXF_FRAGMENT)
1341 if (width > block_size || height > block_size)
1342 Host_Error("R_LoadTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1343 if ((width * texinfo->internalbytesperpixel) & 3)
1344 Host_Error("R_LoadTexture: incompatible width for fragment");
1347 // clear the alpha flag if the texture has no transparent pixels
1350 case TEXTYPE_QPALETTE:
1351 if (flags & TEXF_ALPHA)
1353 flags &= ~TEXF_ALPHA;
1354 for (i = 0;i < width * height;i++)
1358 flags |= TEXF_ALPHA;
1365 if (flags & TEXF_ALPHA)
1366 Host_Error("R_LoadTexture: RGB has no alpha, don't specify TEXF_ALPHA\n");
1369 if (flags & TEXF_ALPHA)
1371 flags &= ~TEXF_ALPHA;
1372 for (i = 0;i < width * height;i++)
1374 if (data[i * 4 + 3] < 255)
1376 flags |= TEXF_ALPHA;
1383 Host_Error("R_LoadTexture: unknown texture type\n");
1386 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
1390 crc = CRC_Block(data, width*height*texinfo->inputbytesperpixel);
1392 // see if the texture is already present
1393 if (identifier && (glt = R_FindTexture(pool, identifier)))
1395 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)
1396 return (rtexture_t *)glt; // exact match, use existing
1397 Con_Printf("R_LoadTexture: cache mismatch on %s, replacing old texture\n", identifier);
1401 return (rtexture_t *)R_SetupTexture(pool, identifier, crc, width, height, flags | GLTEXF_UPLOAD, texinfo, data, NULL, NULL, 0);
1404 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)
1407 gltexturepool_t *pool = (gltexturepool_t *)rtexturepool;
1408 textypeinfo_t *texinfo;
1410 if (cls.state == ca_dedicated)
1413 texinfo = R_GetTexTypeInfo(textype, flags);
1415 // no function is supported, for odd uses
1416 // if (generate == NULL)
1417 // Host_Error("R_ProceduralTexture: \"%s\" has no generate function\n", identifier);
1418 if (flags & TEXF_FRAGMENT)
1420 if (width > block_size || height > block_size)
1421 Host_Error("R_ProceduralTexture: fragment too big, must be no more than %dx%d\n", block_size, block_size);
1422 if ((width * texinfo->internalbytesperpixel) & 3)
1423 Host_Error("R_ProceduralTexture: incompatible width for fragment");
1426 // see if the texture is already present
1427 if (identifier && (glt = R_FindTexture(pool, identifier)))
1429 if (width == glt->width && height == glt->height && texinfo == glt->textype && ((flags ^ glt->flags) & TEXF_IMPORTANTBITS) == 0 && ((flags ^ glt->flags) & GLTEXF_IMPORTANTBITS) == 0)
1430 return (rtexture_t *)glt; // exact match, use existing
1431 Con_DPrintf("R_LoadTexture: cache mismatch, replacing old texture\n");
1435 return (rtexture_t *)R_SetupTexture(pool, identifier, 0, width, height, flags | GLTEXF_PROCEDURAL | GLTEXF_UPLOAD, texinfo, NULL, generate, proceduraldata, proceduraldatasize);
1438 int R_TextureHasAlpha(rtexture_t *rt)
1443 glt = (gltexture_t *)rt;
1444 return (glt->flags & TEXF_ALPHA) != 0;
1447 int R_TextureWidth(rtexture_t *rt)
1451 return ((gltexture_t *)rt)->width;
1454 int R_TextureHeight(rtexture_t *rt)
1458 return ((gltexture_t *)rt)->height;
1461 void R_GetFragmentLocation(rtexture_t *rt, int *x, int *y, float *fx1, float *fy1, float *fx2, float *fy2)
1464 float iwidth, iheight;
1465 if (cls.state == ca_dedicated)
1471 if (fx1 || fy1 || fx2 || fy2)
1485 Host_Error("R_GetFragmentLocation: no texture supplied\n");
1486 glt = (gltexture_t *)rt;
1487 if (glt->flags & TEXF_FRAGMENT)
1493 if (fx1 || fy1 || fx2 || fy2)
1495 iwidth = 1.0f / glt->image->width;
1496 iheight = 1.0f / glt->image->height;
1498 *fx1 = glt->x * iwidth;
1500 *fy1 = glt->y * iheight;
1502 *fx2 = (glt->x + glt->width) * iwidth;
1504 *fy2 = (glt->y + glt->height) * iheight;
1513 if (fx1 || fy1 || fx2 || fy2)
1527 int R_CompatibleFragmentWidth(int width, int textype, int flags)
1529 textypeinfo_t *texinfo = R_GetTexTypeInfo(textype, flags);
1530 while ((width * texinfo->internalbytesperpixel) & 3)
1535 void R_UpdateTexture(rtexture_t *rt, byte *data)
1539 Host_Error("R_UpdateTexture: no texture supplied\n");
1541 Host_Error("R_UpdateTexture: no data supplied\n");
1542 glt = (gltexture_t *)rt;
1543 if (!(glt->flags & GLTEXF_PROCEDURAL))
1545 if (glt->inputtexels == NULL)
1547 glt->inputtexels = Mem_Alloc(texturemempool, glt->width * glt->height * glt->textype->inputbytesperpixel);
1548 if (glt->inputtexels == NULL)
1549 Host_Error("R_UpdateTexture: ran out of memory\n");
1551 memcpy(glt->inputtexels, data, glt->width * glt->height * glt->textype->inputbytesperpixel);
1553 R_Upload(glt, data);