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)
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;
126 Con_Printf("%i textures, totalling %.3f mbytes\n", numgltextures, t / 1024.0 / 1024.0);
129 void GL_TextureStats_PrintTotal(void)
133 for (i = 0, glt = gltextures;i < numgltextures;i++, glt++)
134 t += glt->totaltexels;
135 Con_Printf("%i textures, totalling %.3f mbytes\n", numgltextures, t / 1024.0 / 1024.0);
138 extern int buildnumber;
140 char engineversion[40];
142 void GL_UploadTexture (gltexture_t *glt);
143 void gl_textures_start()
147 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
148 GL_UploadTexture(glt);
151 void gl_textures_shutdown()
155 void GL_Textures_Init (void)
157 Cmd_AddCommand("r_texturestats", GL_TextureStats_f);
158 Cvar_RegisterVariable (&gl_max_size);
159 Cvar_RegisterVariable (&gl_picmip);
160 Cvar_RegisterVariable (&gl_lerpimages);
161 Cvar_RegisterVariable (&r_upload);
166 // 3dfx can only handle 256 wide textures
167 if (!Q_strncasecmp ((char *)gl_renderer, "3dfx",4) || strstr((char *)gl_renderer, "Glide"))
168 Cvar_Set ("gl_max_size", "256");
170 Cmd_AddCommand ("gl_texturemode", &Draw_TextureMode_f);
172 R_RegisterModule("GL_Textures", gl_textures_start, gl_textures_shutdown);
180 int GL_FindTexture (char *identifier)
185 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
187 if (!strcmp (identifier, glt->identifier))
188 return gltextures[i].texnum;
194 void GL_ResampleTextureLerpLine (byte *in, byte *out, int inwidth, int outwidth)
196 int j, xi, oldx = 0, f, fstep, l1, l2, endx;
197 fstep = (int) (inwidth*65536.0f/outwidth);
199 for (j = 0,f = 0;j < outwidth;j++, f += fstep)
204 in += (xi - oldx) * 4;
211 *out++ = (byte) ((in[0] * l1 + in[4] * l2) >> 16);
212 *out++ = (byte) ((in[1] * l1 + in[5] * l2) >> 16);
213 *out++ = (byte) ((in[2] * l1 + in[6] * l2) >> 16);
214 *out++ = (byte) ((in[3] * l1 + in[7] * l2) >> 16);
216 else // last pixel of the line has no pixel to lerp to
231 void GL_ResampleTexture (void *indata, int inwidth, int inheight, void *outdata, int outwidth, int outheight)
233 if (gl_lerpimages.value)
235 int i, j, yi, oldy, f, fstep, l1, l2, endy = (inheight-1);
236 byte *inrow, *out, *row1, *row2;
238 fstep = (int) (inheight*65536.0f/outheight);
240 row1 = qmalloc(outwidth*4);
241 row2 = qmalloc(outwidth*4);
244 GL_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth);
245 GL_ResampleTextureLerpLine (inrow + inwidth*4, row2, inwidth, outwidth);
246 for (i = 0, f = 0;i < outheight;i++,f += fstep)
251 inrow = (byte *)indata + inwidth*4*yi;
253 memcpy(row1, row2, outwidth*4);
255 GL_ResampleTextureLerpLine (inrow, row1, inwidth, outwidth);
257 GL_ResampleTextureLerpLine (inrow + inwidth*4, row2, inwidth, outwidth);
259 memcpy(row2, row1, outwidth*4);
266 for (j = 0;j < outwidth;j++)
268 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
269 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
270 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
271 *out++ = (byte) ((*row1++ * l1 + *row2++ * l2) >> 16);
276 else // last line has no pixels to lerp to
278 for (j = 0;j < outwidth;j++)
294 unsigned frac, fracstep;
295 byte *inrow, *out, *inpix;
298 fracstep = inwidth*0x10000/outwidth;
299 for (i=0 ; i<outheight ; i++)
301 inrow = (byte *)indata + inwidth*(i*inheight/outheight)*4;
302 frac = fracstep >> 1;
303 for (j=0 ; j<outwidth ; j+=4)
305 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
306 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
307 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
308 inpix = inrow + ((frac >> 14) & ~3);*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = qgamma[*inpix++];*out++ = *inpix++ ;frac += fracstep;
314 void GL_FreeTexels(gltexture_t *glt)
317 qfree(glt->texels[0]);
321 void GL_AllocTexels(gltexture_t *glt, int width, int height, int mipmapped)
323 int i, w, h, size, done;
326 glt->texelsize[0][0] = width;
327 glt->texelsize[0][1] = height;
331 w = width;h = height;
334 for (i = 0;i < MAXMIPS;i++)
336 glt->texelsize[i][0] = w;
337 glt->texelsize[i][1] = h;
338 glt->texels[i] = (void *)size;
354 glt->totaltexels = size;
356 glt->texels[i++] = NULL;
357 glt->texels[0] = qmalloc(size);
358 for (i = 1;i < MAXMIPS && glt->texels[i];i++)
359 glt->texels[i] += (int) glt->texels[0];
363 size = width*height*4;
364 glt->totaltexels = size;
365 glt->texels[0] = qmalloc(size);
366 for (i = 1;i < MAXMIPS;i++)
367 glt->texels[i] = NULL;
370 Sys_Error("GL_AllocTexels: out of memory\n");
373 // in can be the same as out
374 void GL_MipReduce(byte *in, byte *out, int width, int height, int destwidth, int destheight)
376 int x, y, width2, height2, nextrow;
377 if (width > destwidth)
379 if (height > destheight)
383 height2 = height >> 1;
384 nextrow = width << 2;
385 for (y = 0;y < height2;y++)
387 for (x = 0;x < width2;x++)
389 out[0] = (byte) ((in[0] + in[4] + in[nextrow ] + in[nextrow+4]) >> 2);
390 out[1] = (byte) ((in[1] + in[5] + in[nextrow+1] + in[nextrow+5]) >> 2);
391 out[2] = (byte) ((in[2] + in[6] + in[nextrow+2] + in[nextrow+6]) >> 2);
392 out[3] = (byte) ((in[3] + in[7] + in[nextrow+3] + in[nextrow+7]) >> 2);
396 in += nextrow; // skip a line
403 for (y = 0;y < height;y++)
405 for (x = 0;x < width2;x++)
407 out[0] = (byte) ((in[0] + in[4]) >> 1);
408 out[1] = (byte) ((in[1] + in[5]) >> 1);
409 out[2] = (byte) ((in[2] + in[6]) >> 1);
410 out[3] = (byte) ((in[3] + in[7]) >> 1);
419 if (height > destheight)
422 height2 = height >> 1;
423 nextrow = width << 2;
424 for (y = 0;y < height2;y++)
426 for (x = 0;x < width;x++)
428 out[0] = (byte) ((in[0] + in[nextrow ]) >> 1);
429 out[1] = (byte) ((in[1] + in[nextrow+1]) >> 1);
430 out[2] = (byte) ((in[2] + in[nextrow+2]) >> 1);
431 out[3] = (byte) ((in[3] + in[nextrow+3]) >> 1);
435 in += nextrow; // skip a line
439 Sys_Error("GL_MipReduce: desired size already achieved\n");
443 void GL_UploadTexture (gltexture_t *glt)
445 int mip, width, height;
448 glBindTexture(GL_TEXTURE_2D, glt->texnum);
450 height = glt->height;
451 for (mip = 0;mip < MAXMIPS && glt->texels[mip];mip++)
452 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]);
455 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min);
456 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
460 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_max);
461 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max);
463 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
471 int GL_LoadTexture (char *identifier, int width, int height, byte *data, qboolean mipmap, qboolean alpha, int bytesperpixel)
474 int i, width2, height2, width3, height3, w, h, mip;
480 // LordHavoc: do a CRC to confirm the data really is the same as previous occurances.
481 crc = CRC_Block(data, width*height*bytesperpixel);
482 // see if the texture is already present
485 for (i=0, glt=gltextures ; i<numgltextures ; i++, glt++)
487 if (!strcmp (identifier, glt->identifier))
489 // LordHavoc: everyone hates cache mismatchs, so I fixed it
490 if (crc != glt->crc || width != glt->width || height != glt->height)
492 Con_DPrintf("GL_LoadTexture: cache mismatch, replacing old texture\n");
493 goto GL_LoadTexture_setup; // drop out with glt pointing to the texture to replace
494 //Sys_Error ("GL_LoadTexture: cache mismatch");
496 if ((gl_lerpimages.value != 0) != glt->lerped)
497 goto GL_LoadTexture_setup; // drop out with glt pointing to the texture to replace
502 // LordHavoc: although this could be an else condition as it was in the original id code,
503 // it is more clear this way
504 // LordHavoc: check if there are still slots available
505 if (numgltextures >= MAX_GLTEXTURES)
506 Sys_Error ("GL_LoadTexture: ran out of texture slots (%d)\n", MAX_GLTEXTURES);
507 glt = &gltextures[numgltextures++];
509 strcpy (glt->identifier, identifier);
510 glt->texnum = texture_extension_number;
511 texture_extension_number++;
512 // LordHavoc: label to drop out of the loop into the setup code
513 GL_LoadTexture_setup:
514 // calculate power of 2 size
515 width2 = 1;while (width2 < width) width2 <<= 1;
516 height2 = 1;while (height2 < height) height2 <<= 1;
517 // calculate final size (mipmapped downward to this)
518 width3 = width2 >> (int) gl_picmip.value;
519 height3 = height2 >> (int) gl_picmip.value;
520 while (width3 > (int) gl_max_size.value) width3 >>= 1;
521 while (height3 > (int) gl_max_size.value) height3 >>= 1;
522 if (width3 < 1) width3 = 1;
523 if (height3 < 1) height3 = 1;
526 GL_AllocTexels(glt, width3, height3, mipmap);
527 glt->crc = crc; // LordHavoc: used to verify textures are identical
529 glt->height = height;
530 glt->mipmap = mipmap;
531 glt->bytesperpixel = bytesperpixel;
532 glt->lerped = gl_lerpimages.value != 0;
533 glt->alpha = false; // updated later
534 if (width == width3 && height == height3) // perfect match
536 if (bytesperpixel == 1) // 8bit
537 Image_Copy8bitRGBA(data, glt->texels[0], width*height, d_8to24table);
539 Image_CopyRGBAGamma(data, glt->texels[0], width*height);
541 else if (width == width2 && height == height2) // perfect match for top level, but needs to be reduced
544 temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
545 if (bytesperpixel == 1) // 8bit
546 Image_Copy8bitRGBA(data, temptexels2, width*height, d_8to24table);
548 Image_CopyRGBAGamma(data, temptexels2, width*height);
549 while (width2 > width3 || height2 > height3)
551 w = width2;h = height2;
552 if (width2 > width3) width2 >>= 1;
553 if (height2 > height3) height2 >>= 1;
554 if (width2 <= width3 && height2 <= height3) // size achieved
555 GL_MipReduce(temptexels2, glt->texels[0], w, h, width3, height3);
557 GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
564 // pre-scaleup buffer
565 temptexels = qmalloc(width*height*4);
566 if (bytesperpixel == 1) // 8bit
567 Image_Copy8bitRGBA(data, temptexels, width*height, d_8to24table);
569 Image_CopyRGBAGamma(data, temptexels, width*height);
570 if (width2 != width3 || height2 != height3) // reduced by gl_pic_mip or gl_max_size
573 temptexels2 = qmalloc(width2*height2*4); // scaleup buffer
574 GL_ResampleTexture(temptexels, width, height, temptexels2, width2, height2);
575 while (width2 > width3 || height2 > height3)
577 w = width2;h = height2;
578 if (width2 > width3) width2 >>= 1;
579 if (height2 > height3) height2 >>= 1;
580 if (width2 <= width3 && height2 <= height3) // size achieved
581 GL_MipReduce(temptexels2, glt->texels[0], w, h, width3, height3);
583 GL_MipReduce(temptexels2, temptexels2, w, h, width3, height3);
587 else // copy directly
588 GL_ResampleTexture(temptexels, width, height, glt->texels[0], width2, height2);
593 byte *in = glt->texels[0] + 3;
594 for (i = 0;i < width*height;i++, in += 4)
601 // this loop is skipped if there are no mipmaps to generate
602 for (mip = 1;mip < MAXMIPS && glt->texels[mip];mip++)
603 GL_MipReduce(glt->texels[mip-1], glt->texels[mip], glt->texelsize[mip-1][0], glt->texelsize[mip-1][1], 1, 1);
604 GL_UploadTexture(glt);
607 // if (bytesperpixel == 1) // 8bit
608 // GL_Upload8 (data, width, height, mipmap, alpha);
610 // GL_Upload32 (data, width, height, mipmap, true);
620 int GL_LoadPicTexture (qpic_t *pic)
622 return GL_LoadTexture ("", pic->width, pic->height, pic->data, false, true, 1);
625 int GL_GetTextureSlots (int count)
627 gltexture_t *glt, *first;
629 first = glt = &gltextures[numgltextures];
632 glt->identifier[0] = 0;
633 glt->texnum = texture_extension_number++;
637 glt->bytesperpixel = 0;
641 return first->texnum;