3 cvar_t gl_max_size = {"gl_max_size", "1024"};
4 cvar_t gl_picmip = {"gl_picmip", "0"};
5 cvar_t gl_lerpimages = {"gl_lerpimages", "1"};
6 cvar_t r_upload = {"r_upload", "1"};
8 int gl_filter_min = GL_LINEAR_MIPMAP_LINEAR; //NEAREST;
9 int gl_filter_max = GL_LINEAR;
21 byte *texels[MAXMIPS];
22 unsigned short texelsize[MAXMIPS][2];
25 // LordHavoc: CRC to identify cache mismatchs
30 char lerped; // whether this texture was uploaded with or without interpolation
33 #define MAX_GLTEXTURES 4096
34 gltexture_t gltextures[MAX_GLTEXTURES];
40 int minimize, maximize;
44 {"GL_NEAREST", GL_NEAREST, GL_NEAREST},
45 {"GL_LINEAR", GL_LINEAR, GL_LINEAR},
46 {"GL_NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST},
47 {"GL_LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR},
48 {"GL_NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST},
49 {"GL_LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR}
57 void Draw_TextureMode_f (void)
64 for (i=0 ; i< 6 ; i++)
65 if (gl_filter_min == modes[i].minimize)
67 Con_Printf ("%s\n", modes[i].name);
70 Con_Printf ("current filter is unknown???\n");
74 for (i=0 ; i< 6 ; i++)
76 if (!Q_strcasecmp (modes[i].name, Cmd_Argv(1) ) )
81 Con_Printf ("bad filter name\n");
85 gl_filter_min = modes[i].minimize;
86 gl_filter_max = modes[i].maximize;
90 // change all the existing mipmap texture objects
91 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
95 glBindTexture(GL_TEXTURE_2D, glt->texnum);
96 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
97 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
102 void GL_TextureStats_Print(char *name, int total, int crc, int mip, int alpha)
108 while (name[c] && c < 28)
113 Con_Printf("%s %5i %04X %s %s\n", n, total, crc, mip ? "yes" : "no ", alpha ? "yes " : "no ");
116 void GL_TextureStats_f(void)
118 int i, s = 0, sc = 0, t = 0;
120 Con_Printf("name kbytes crc mip alpha\n");
121 for (i = 0, glt = gltextures;i < numgltextures;i++, glt++)
123 GL_TextureStats_Print(glt->identifier, ((glt->totaltexels * 4) + 512) >> 10, glt->crc, glt->mipmap, glt->alpha);
124 t += glt->totaltexels;
125 if (glt->identifier[0] == '&')
128 s += glt->totaltexels;
131 Con_Printf("%i textures, totalling %.3fMB, %i are (usually) unnecessary model skins totalling %.3fMB\n", numgltextures, t / 1048576.0, sc, s / 1048576.0);
134 void GL_TextureStats_PrintTotal(void)
136 int i, s = 0, sc = 0, t = 0;
138 for (i = 0, glt = gltextures;i < numgltextures;i++, glt++)
140 t += glt->totaltexels;
141 if (glt->identifier[0] == '&')
144 s += glt->totaltexels;
147 Con_Printf("%i textures, totalling %.3fMB, %i are (usually) unnecessary model skins totalling %.3fMB\n", numgltextures, t / 1048576.0, sc, s / 1048576.0);
150 extern int buildnumber;
152 char engineversion[40];
154 void GL_UploadTexture (gltexture_t *glt);
155 void gl_textures_start()
159 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
160 GL_UploadTexture(glt);
163 void gl_textures_shutdown()
167 void GL_Textures_Init (void)
169 Cmd_AddCommand("r_texturestats", GL_TextureStats_f);
170 Cvar_RegisterVariable (&gl_max_size);
171 Cvar_RegisterVariable (&gl_picmip);
172 Cvar_RegisterVariable (&gl_lerpimages);
173 Cvar_RegisterVariable (&r_upload);
178 // 3dfx can only handle 256 wide textures
179 if (!Q_strncasecmp ((char *)gl_renderer, "3dfx",4) || strstr((char *)gl_renderer, "Glide"))
180 Cvar_Set ("gl_max_size", "256");
182 Cmd_AddCommand ("gl_texturemode", &Draw_TextureMode_f);
184 R_RegisterModule("GL_Textures", gl_textures_start, gl_textures_shutdown);
192 int GL_FindTexture (char *identifier)
197 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
199 if (!strcmp (identifier, glt->identifier))
200 return gltextures[i].texnum;
206 void GL_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth)
208 int j, xi, oldx = 0, f, fstep, l1, l2, endx;
209 fstep = (int) (inwidth*65536.0f/outwidth);
211 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
216 in += (xi - oldx) * 4;
223 *out++ = (byte) ((in[0] * l1 + in[4] * l2) >> 16);
224 *out++ = (byte) ((in[1] * l1 + in[5] * l2) >> 16);
225 *out++ = (byte) ((in[2] * l1 + in[6] * l2) >> 16);
226 *out++ = (byte) ((in[3] * l1 + in[7] * l2) >> 16);
228 else // last pixel of the line has no pixel to lerp to
243 void GL_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
245 if (gl_lerpimages.value)
247 int i, j, yi, oldy, f, fstep, l1, l2, endy = (inheight-1);
248 byte *inrow, *out, *row1, *row2;
250 fstep = (int) (inheight*65536.0f/outheight);
252 row1 = qmalloc(outwidth*4);
253 row2 = qmalloc(outwidth*4);
256 GL_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth);
257 GL_ResampleTextureLerpLine (inrow + inwidth*4, row2, inwidth, outwidth);
258 for (i = 0, f = 0;i < outheight;i++,f += fstep)
263 inrow = (byte *)indata + inwidth*4*yi;
265 memcpy(row1, row2, outwidth*4);
267 GL_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth);
269 GL_ResampleTextureLerpLine (inrow + inwidth*4, row2, inwidth, outwidth);
271 memcpy(row2, row1, outwidth*4);
278 for (j = 0;j < outwidth;j++)
280 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
281 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
282 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
283 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
288 else // last line has no pixels to lerp to
290 for (j = 0;j < outwidth;j++)
306 unsigned frac, fracstep;
307 byte *inrow, *out, *inpix;
310 fracstep = inwidth*0x10000/outwidth;
311 for (i=0 ; i<outheight ; i++)
313 inrow = (byte *)indata + inwidth*(i*inheight/outheight)*4;
314 frac = fracstep >> 1;
315 for (j=0 ; j<outwidth ; j+=4)
317 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
318 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
319 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
320 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
326 void GL_FreeTexels(gltexture_t *glt)
329 qfree(glt->texels[0]);
333 void GL_AllocTexels(gltexture_t *glt, int width, int height, int mipmapped)
335 int i, w, h, size, done;
338 glt->texelsize[0][0] = width;
339 glt->texelsize[0][1] = height;
343 w = width;h = height;
346 for (i = 0;i < MAXMIPS;i++)
348 glt->texelsize[i][0] = w;
349 glt->texelsize[i][1] = h;
350 glt->texels[i] = (void *)size;
366 glt->totaltexels = size;
368 glt->texels[i++] = NULL;
369 glt->texels[0] = qmalloc(size);
370 for (i = 1;i < MAXMIPS && glt->texels[i];i++)
371 glt->texels[i] += (int) glt->texels[0];
375 size = width*height*4;
376 glt->totaltexels = size;
377 glt->texels[0] = qmalloc(size);
378 for (i = 1;i < MAXMIPS;i++)
379 glt->texels[i] = NULL;
382 Sys_Error("GL_AllocTexels: out of memory\n");
385 // in can be the same as out
386 void GL_MipReduce(byte *in, byte *out, int width, int height, int destwidth, int destheight)
388 int x, y, width2, height2, nextrow;
389 if (width > destwidth)
391 if (height > destheight)
395 height2 = height >> 1;
396 nextrow = width << 2;
397 for (y = 0;y < height2;y++)
399 for (x = 0;x < width2;x++)
401 out[0] = (byte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
402 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
403 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
404 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
408 in += nextrow; // skip a line
415 for (y = 0;y < height;y++)
417 for (x = 0;x < width2;x++)
419 out[0] = (byte) ((in[0] + in[4]) >> 1);
420 out[1] = (byte) ((in[1] + in[5]) >> 1);
421 out[2] = (byte) ((in[2] + in[6]) >> 1);
422 out[3] = (byte) ((in[3] + in[7]) >> 1);
431 if (height > destheight)
434 height2 = height >> 1;
435 nextrow = width << 2;
436 for (y = 0;y < height2;y++)
438 for (x = 0;x < width;x++)
440 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
441 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
442 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
443 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
447 in += nextrow; // skip a line
451 Sys_Error("GL_MipReduce: desired size already achieved\n");
455 void GL_UploadTexture (gltexture_t *glt)
457 int mip, width, height;
460 glBindTexture(GL_TEXTURE_2D, glt->texnum);
462 height = glt->height;
463 for (mip = 0;mip < MAXMIPS && glt->texels[mip];mip++)
464 glTexImage2D(GL_TEXTURE_2D, mip, glt->alpha ? 4 : 3, glt->texelsize[mip][0], glt->texelsize[mip][1], 0, GL_RGBA, GL_UNSIGNED_BYTE, glt->texels[mip]);
467 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
468 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
472 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
473 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
475 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
483 int GL_LoadTexture (char *identifier, int width, int height, byte *data, qboolean mipmap, qboolean alpha, int bytesperpixel)
486 int i, width2, height2, width3, height3, w, h, mip;
492 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
493 crc = CRC_Block(data, width*height*bytesperpixel);
494 // see if the texture is already present
497 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
499 if (!strcmp (identifier, glt->identifier))
501 // LordHavoc: everyone hates cache mismatchs, so I fixed it
502 if (crc != glt->crc || width != glt->width || height != glt->height)
504 Con_DPrintf("GL_LoadTexture: cache mismatch, replacing old texture\n");
505 goto GL_LoadTexture_setup; // drop out with glt pointing to the texture to replace
506 //Sys_Error ("GL_LoadTexture: cache mismatch");
508 if ((gl_lerpimages.value != 0) != glt->lerped)
509 goto GL_LoadTexture_setup; // drop out with glt pointing to the texture to replace
514 // LordHavoc: although this could be an else condition as it was in the original id code,
515 // it is more clear this way
516 // LordHavoc: check if there are still slots available
517 if (numgltextures >= MAX_GLTEXTURES)
518 Sys_Error ("GL_LoadTexture: ran out of texture slots (%d)\n", MAX_GLTEXTURES);
519 glt = &gltextures[numgltextures++];
521 strcpy (glt->identifier, identifier);
522 glt->texnum = texture_extension_number;
523 texture_extension_number++;
524 // LordHavoc: label to drop out of the loop into the setup code
525 GL_LoadTexture_setup:
526 // calculate power of 2 size
527 width2 = 1;while (width2 < width) width2 <<= 1;
528 height2 = 1;while (height2 < height) height2 <<= 1;
529 // calculate final size (mipmapped downward to this)
530 width3 = width2 >> (int) gl_picmip.value;
531 height3 = height2 >> (int) gl_picmip.value;
532 while (width3 > (int) gl_max_size.value) width3 >>= 1;
533 while (height3 > (int) gl_max_size.value) height3 >>= 1;
534 if (width3 < 1) width3 = 1;
535 if (height3 < 1) height3 = 1;
538 GL_AllocTexels(glt, width3, height3, mipmap);
539 glt->crc = crc; // LordHavoc: used to verify textures are identical
541 glt->height = height;
542 glt->mipmap = mipmap;
543 glt->bytesperpixel = bytesperpixel;
544 glt->lerped = gl_lerpimages.value != 0;
545 glt->alpha = false; // updated later
546 if (width == width3 && height == height3) // perfect match
548 if (bytesperpixel == 1) // 8bit
549 Image_Copy8bitRGBA(data, glt->texels[0], width*height, d_8to24table);
551 Image_CopyRGBAGamma(data, glt->texels[0], width*height);
553 else if (width == width2 && height == height2) // perfect match for top level, but needs to be reduced
556 temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
557 if (bytesperpixel == 1) // 8bit
558 Image_Copy8bitRGBA(data, temptexels2, width*height, d_8to24table);
560 Image_CopyRGBAGamma(data, temptexels2, width*height);
561 while (width2 > width3 || height2 > height3)
563 w = width2;h = height2;
564 if (width2 > width3) width2 >>= 1;
565 if (height2 > height3) height2 >>= 1;
566 if (width2 <= width3 && height2 <= height3) // size achieved
567 GL_MipReduce(temptexels2, glt->texels[0], w, h, width3, height3);
569 GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
576 // pre-scaleup buffer
577 temptexels = qmalloc(width*height*4);
578 if (bytesperpixel == 1) // 8bit
579 Image_Copy8bitRGBA(data, temptexels, width*height, d_8to24table);
581 Image_CopyRGBAGamma(data, temptexels, width*height);
582 if (width2 != width3 || height2 != height3) // reduced by gl_pic_mip or gl_max_size
585 temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
586 GL_ResampleTexture(temptexels, width, height, temptexels2, width2, height2);
587 while (width2 > width3 || height2 > height3)
589 w = width2;h = height2;
590 if (width2 > width3) width2 >>= 1;
591 if (height2 > height3) height2 >>= 1;
592 if (width2 <= width3 && height2 <= height3) // size achieved
593 GL_MipReduce(temptexels2, glt->texels[0], w, h, width3, height3);
595 GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
599 else // copy directly
600 GL_ResampleTexture(temptexels, width, height, glt->texels[0], width2, height2);
605 byte *in = glt->texels[0] + 3;
606 for (i = 0;i < width*height;i++, in += 4)
613 // this loop is skipped if there are no mipmaps to generate
614 for (mip = 1;mip < MAXMIPS && glt->texels[mip];mip++)
615 GL_MipReduce(glt->texels[mip-1], glt->texels[mip], glt->texelsize[mip-1][0], glt->texelsize[mip-1][1], 1, 1);
616 GL_UploadTexture(glt);
619 // if (bytesperpixel == 1) // 8bit
620 // GL_Upload8 (data, width, height, mipmap, alpha);
622 // GL_Upload32 (data, width, height, mipmap, true);
632 int GL_LoadPicTexture (qpic_t *pic)
634 return GL_LoadTexture ("", pic->width, pic->height, pic->data, false, true, 1);
637 int GL_GetTextureSlots (int count)
639 gltexture_t *glt, *first;
641 first = glt = &gltextures[numgltextures];
644 glt->identifier[0] = 0;
645 glt->texnum = texture_extension_number++;
649 glt->bytesperpixel = 0;
653 return first->texnum;