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)
110 // no need to pad since the name was moved to last
114 Con_Printf("%5i %04X %s %s %s\n", total, crc, mip ? "yes" : "no ", alpha ? "yes " : "no ", n);
117 void GL_TextureStats_f(void)
119 int i, s = 0, sc = 0, t = 0;
121 Con_Printf("kbytes crc mip alpha name\n");
122 for (i = 0, glt = gltextures;i < numgltextures;i++, glt++)
124 GL_TextureStats_Print(glt->identifier, (glt->texeldatasize + 512) >> 10, glt->crc, glt->mipmap, glt->alpha);
125 t += glt->texeldatasize;
126 if (glt->identifier[0] == '&')
129 s += glt->texeldatasize;
132 Con_Printf("%i textures, totalling %.3fMB, %i are (usually) unnecessary model skins totalling %.3fMB\n", numgltextures, t / 1048576.0, sc, s / 1048576.0);
135 void GL_TextureStats_PrintTotal(void)
137 int i, s = 0, sc = 0, t = 0;
139 for (i = 0, glt = gltextures;i < numgltextures;i++, glt++)
141 t += glt->texeldatasize;
142 if (glt->identifier[0] == '&')
145 s += glt->texeldatasize;
148 Con_Printf("%i textures, totalling %.3fMB, %i are (usually) unnecessary model skins totalling %.3fMB\n", numgltextures, t / 1048576.0, sc, s / 1048576.0);
151 char engineversion[40];
153 void GL_UploadTexture (gltexture_t *glt);
154 void gl_textures_start()
158 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
159 GL_UploadTexture(glt);
162 void gl_textures_shutdown()
166 void GL_Textures_Init (void)
168 Cmd_AddCommand("r_texturestats", GL_TextureStats_f);
169 Cvar_RegisterVariable (&gl_max_size);
170 Cvar_RegisterVariable (&gl_picmip);
171 Cvar_RegisterVariable (&gl_lerpimages);
172 Cvar_RegisterVariable (&r_upload);
177 // 3dfx can only handle 256 wide textures
178 if (!Q_strncasecmp ((char *)gl_renderer, "3dfx",4) || strstr((char *)gl_renderer, "Glide"))
179 Cvar_Set ("gl_max_size", "256");
181 Cmd_AddCommand ("gl_texturemode", &Draw_TextureMode_f);
183 R_RegisterModule("GL_Textures", gl_textures_start, gl_textures_shutdown);
191 int GL_FindTexture (char *identifier)
196 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
198 if (!strcmp (identifier, glt->identifier))
199 return gltextures[i].texnum;
205 void GL_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth)
207 int j, xi, oldx = 0, f, fstep, l1, l2, endx;
208 fstep = (int) (inwidth*65536.0f/outwidth);
210 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
215 in += (xi - oldx) * 4;
222 *out++ = (byte) ((in[0] * l1 + in[4] * l2) >> 16);
223 *out++ = (byte) ((in[1] * l1 + in[5] * l2) >> 16);
224 *out++ = (byte) ((in[2] * l1 + in[6] * l2) >> 16);
225 *out++ = (byte) ((in[3] * l1 + in[7] * l2) >> 16);
227 else // last pixel of the line has no pixel to lerp to
242 void GL_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
244 if (gl_lerpimages.value)
246 int i, j, yi, oldy, f, fstep, l1, l2, endy = (inheight-1);
247 byte *inrow, *out, *row1, *row2;
249 fstep = (int) (inheight*65536.0f/outheight);
251 row1 = qmalloc(outwidth*4);
252 row2 = qmalloc(outwidth*4);
255 GL_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth);
256 GL_ResampleTextureLerpLine (inrow + inwidth*4, row2, inwidth, outwidth);
257 for (i = 0, f = 0;i < outheight;i++,f += fstep)
262 inrow = (byte *)indata + inwidth*4*yi;
264 memcpy(row1, row2, outwidth*4);
266 GL_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth);
268 GL_ResampleTextureLerpLine (inrow + inwidth*4, row2, inwidth, outwidth);
270 memcpy(row2, row1, outwidth*4);
277 for (j = 0;j < outwidth;j++)
279 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
280 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
281 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
282 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
287 else // last line has no pixels to lerp to
289 for (j = 0;j < outwidth;j++)
305 unsigned frac, fracstep;
306 byte *inrow, *out, *inpix;
309 fracstep = inwidth*0x10000/outwidth;
310 for (i=0 ; i<outheight ; i++)
312 inrow = (byte *)indata + inwidth*(i*inheight/outheight)*4;
313 frac = fracstep >> 1;
314 for (j=0 ; j<outwidth ; j+=4)
316 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
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;
325 void GL_FreeTexels(gltexture_t *glt)
328 qfree(glt->texels[0]);
332 void GL_AllocTexels(gltexture_t *glt, int width, int height, int mipmapped)
337 glt->texelsize[0][0] = width;
338 glt->texelsize[0][1] = height;
342 w = width;h = height;
346 glt->texelsize[i][0] = w;
347 glt->texelsize[i][1] = h;
348 glt->texels[i++] = (void *)size;
361 glt->texeldatasize = size;
363 glt->texels[i++] = NULL;
364 glt->texels[0] = qmalloc(size);
365 for (i = 1;i < MAXMIPS && glt->texels[i];i++)
366 glt->texels[i] += (int) glt->texels[0];
370 size = width*height*4;
371 glt->texeldatasize = size;
372 glt->texels[0] = qmalloc(size);
373 for (i = 1;i < MAXMIPS;i++)
374 glt->texels[i] = NULL;
377 Sys_Error("GL_AllocTexels: out of memory\n");
380 // in can be the same as out
381 void GL_MipReduce(byte *in, byte *out, int width, int height, int destwidth, int destheight)
383 int x, y, width2, height2, nextrow;
384 if (width > destwidth)
386 if (height > destheight)
390 height2 = height >> 1;
391 nextrow = width << 2;
392 for (y = 0;y < height2;y++)
394 for (x = 0;x < width2;x++)
396 out[0] = (byte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
397 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
398 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
399 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
403 in += nextrow; // skip a line
410 for (y = 0;y < height;y++)
412 for (x = 0;x < width2;x++)
414 out[0] = (byte) ((in[0] + in[4]) >> 1);
415 out[1] = (byte) ((in[1] + in[5]) >> 1);
416 out[2] = (byte) ((in[2] + in[6]) >> 1);
417 out[3] = (byte) ((in[3] + in[7]) >> 1);
426 if (height > destheight)
429 height2 = height >> 1;
430 nextrow = width << 2;
431 for (y = 0;y < height2;y++)
433 for (x = 0;x < width;x++)
435 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
436 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
437 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
438 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
442 in += nextrow; // skip a line
446 Sys_Error("GL_MipReduce: desired size already achieved\n");
450 void GL_UploadTexture (gltexture_t *glt)
452 int mip, width, height;
455 glBindTexture(GL_TEXTURE_2D, glt->texnum);
457 height = glt->height;
458 for (mip = 0;mip < MAXMIPS && glt->texels[mip];mip++)
459 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]);
462 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
463 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
467 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
468 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
470 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
478 int GL_LoadTexture (char *identifier, int width, int height, byte *data, qboolean mipmap, qboolean alpha, int bytesperpixel)
481 int i, width2, height2, width3, height3, w, h, mip;
487 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
488 crc = CRC_Block(data, width*height*bytesperpixel);
489 // see if the texture is already present
492 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
494 if (!strcmp (identifier, glt->identifier))
496 // LordHavoc: everyone hates cache mismatchs, so I fixed it
497 if (crc != glt->crc || width != glt->width || height != glt->height)
499 Con_DPrintf("GL_LoadTexture: cache mismatch, replacing old texture\n");
500 goto GL_LoadTexture_setup; // drop out with glt pointing to the texture to replace
502 if ((gl_lerpimages.value != 0) != glt->lerped)
503 goto GL_LoadTexture_setup; // drop out with glt pointing to the texture to replace
508 // LordHavoc: although this could be an else condition as it was in the original id code,
509 // it is more clear this way
510 // LordHavoc: check if there are still slots available
511 if (numgltextures >= MAX_GLTEXTURES)
512 Sys_Error ("GL_LoadTexture: ran out of texture slots (%d)\n", MAX_GLTEXTURES);
513 glt = &gltextures[numgltextures++];
515 strcpy (glt->identifier, identifier);
516 glt->texnum = texture_extension_number;
517 texture_extension_number++;
518 // LordHavoc: label to drop out of the loop into the setup code
519 GL_LoadTexture_setup:
520 // calculate power of 2 size
521 width2 = 1;while (width2 < width) width2 <<= 1;
522 height2 = 1;while (height2 < height) height2 <<= 1;
523 // calculate final size (mipmapped downward to this)
524 width3 = width2 >> (int) gl_picmip.value;
525 height3 = height2 >> (int) gl_picmip.value;
526 while (width3 > (int) gl_max_size.value) width3 >>= 1;
527 while (height3 > (int) gl_max_size.value) height3 >>= 1;
528 if (width3 < 1) width3 = 1;
529 if (height3 < 1) height3 = 1;
532 GL_AllocTexels(glt, width3, height3, mipmap);
533 glt->crc = crc; // LordHavoc: used to verify textures are identical
535 glt->height = height;
536 glt->mipmap = mipmap;
537 glt->bytesperpixel = bytesperpixel;
538 glt->lerped = gl_lerpimages.value != 0;
539 glt->alpha = false; // updated later
540 if (width == width3 && height == height3) // perfect match
542 if (bytesperpixel == 1) // 8bit
543 Image_Copy8bitRGBA(data, glt->texels[0], width*height, d_8to24table);
545 Image_CopyRGBAGamma(data, glt->texels[0], width*height);
547 else if (width == width2 && height == height2) // perfect match for top level, but needs to be reduced
550 temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
551 if (bytesperpixel == 1) // 8bit
552 Image_Copy8bitRGBA(data, temptexels2, width*height, d_8to24table);
554 Image_CopyRGBAGamma(data, temptexels2, width*height);
555 while (width2 > width3 || height2 > height3)
557 w = width2;h = height2;
558 if (width2 > width3) width2 >>= 1;
559 if (height2 > height3) height2 >>= 1;
560 if (width2 <= width3 && height2 <= height3) // size achieved
561 GL_MipReduce(temptexels2, glt->texels[0], w, h, width3, height3);
563 GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
570 // pre-scaleup buffer
571 temptexels = qmalloc(width*height*4);
572 if (bytesperpixel == 1) // 8bit
573 Image_Copy8bitRGBA(data, temptexels, width*height, d_8to24table);
575 Image_CopyRGBAGamma(data, temptexels, width*height);
576 if (width2 != width3 || height2 != height3) // reduced by gl_pic_mip or gl_max_size
579 temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
580 GL_ResampleTexture(temptexels, width, height, temptexels2, width2, height2);
581 while (width2 > width3 || height2 > height3)
583 w = width2;h = height2;
584 if (width2 > width3) width2 >>= 1;
585 if (height2 > height3) height2 >>= 1;
586 if (width2 <= width3 && height2 <= height3) // size achieved
587 GL_MipReduce(temptexels2, glt->texels[0], w, h, width3, height3);
589 GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
593 else // copy directly
594 GL_ResampleTexture(temptexels, width, height, glt->texels[0], width2, height2);
599 byte *in = glt->texels[0] + 3;
600 for (i = 0;i < width*height;i++, in += 4)
607 // this loop is skipped if there are no mipmaps to generate
608 for (mip = 1;mip < MAXMIPS && glt->texels[mip];mip++)
609 GL_MipReduce(glt->texels[mip-1], glt->texels[mip], glt->texelsize[mip-1][0], glt->texelsize[mip-1][1], 1, 1);
610 GL_UploadTexture(glt);
613 // if (bytesperpixel == 1) // 8bit
614 // GL_Upload8 (data, width, height, mipmap, alpha);
616 // GL_Upload32 (data, width, height, mipmap, true);
621 int GL_GetTextureSlots (int count)
623 gltexture_t *glt, *first;
625 first = glt = &gltextures[numgltextures];
628 glt->identifier[0] = 0;
629 glt->texnum = texture_extension_number++;
633 glt->bytesperpixel = 0;
637 return first->texnum;