]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_textures.c
fix some warnings
[xonotic/darkplaces.git] / gl_textures.c
index c3e200fb72e15f2b43f661e4b0dac446efe430ac..f26b44ade3bd90ff29d83817ae6cf905d806c3e5 100644 (file)
@@ -9,6 +9,16 @@ cvar_t gl_picmip = {CVAR_SAVE, "gl_picmip", "0", "reduces resolution of textures
 cvar_t r_lerpimages = {CVAR_SAVE, "r_lerpimages", "1", "bilinear filters images when scaling them up to power of 2 size (mode 1), looks better than glquake (mode 0)"};
 cvar_t r_precachetextures = {CVAR_SAVE, "r_precachetextures", "1", "0 = never upload textures until used, 1 = upload most textures before use (exceptions: rarely used skin colormap layers), 2 = upload all textures before use (can increase texture memory usage significantly)"};
 cvar_t gl_texture_anisotropy = {CVAR_SAVE, "gl_texture_anisotropy", "1", "anisotropic filtering quality (if supported by hardware), 1 sample (no anisotropy) and 8 sample (8 tap anisotropy) are recommended values"};
+cvar_t gl_texturecompression = {CVAR_SAVE, "gl_texturecompression", "0", "whether to compress textures, a value of 0 disables compression (even if the individual cvars are 1), 1 enables fast (low quality) compression at startup, 2 enables slow (high quality) compression at startup"};
+cvar_t gl_texturecompression_color = {CVAR_SAVE, "gl_texturecompression_color", "1", "whether to compress colormap (diffuse) textures"};
+cvar_t gl_texturecompression_normal = {CVAR_SAVE, "gl_texturecompression_normal", "1", "whether to compress normalmap (normalmap) textures"};
+cvar_t gl_texturecompression_gloss = {CVAR_SAVE, "gl_texturecompression_gloss", "1", "whether to compress glossmap (specular) textures"};
+cvar_t gl_texturecompression_glow = {CVAR_SAVE, "gl_texturecompression_glow", "1", "whether to compress glowmap (luma) textures"};
+cvar_t gl_texturecompression_2d = {CVAR_SAVE, "gl_texturecompression_2d", "1", "whether to compress 2d (hud/menu) textures other than the font"};
+cvar_t gl_texturecompression_q3bsplightmaps = {CVAR_SAVE, "gl_texturecompression_q3bsplightmaps", "1", "whether to compress lightmaps in q3bsp format levels"};
+cvar_t gl_texturecompression_q3bspdeluxemaps = {CVAR_SAVE, "gl_texturecompression_q3bspdeluxemaps", "1", "whether to compress deluxemaps in q3bsp format levels (only levels compiled with q3map2 -deluxe have these)"};
+cvar_t gl_texturecompression_sky = {CVAR_SAVE, "gl_texturecompression_sky", "0", "whether to compress sky textures"};
+cvar_t gl_texturecompression_lightcubemaps = {CVAR_SAVE, "gl_texturecompression_lightcubemaps", "1", "whether to compress light cubemaps (spotlights and other light projection images)"};
 
 int            gl_filter_min = GL_LINEAR_MIPMAP_LINEAR;
 int            gl_filter_mag = GL_LINEAR;
@@ -29,16 +39,22 @@ typedef struct textypeinfo_s
        int textype;
        int inputbytesperpixel;
        int internalbytesperpixel;
+       float glinternalbytesperpixel;
        int glformat;
        int glinternalformat;
 }
 textypeinfo_t;
 
-static textypeinfo_t textype_palette       = {TEXTYPE_PALETTE, 1, 4, GL_RGBA   , 3};
-static textypeinfo_t textype_rgb           = {TEXTYPE_RGB    , 3, 3, GL_RGB    , 3};
-static textypeinfo_t textype_rgba          = {TEXTYPE_RGBA   , 4, 4, GL_RGBA   , 3};
-static textypeinfo_t textype_palette_alpha = {TEXTYPE_PALETTE, 1, 4, GL_RGBA   , 4};
-static textypeinfo_t textype_rgba_alpha    = {TEXTYPE_RGBA   , 4, 4, GL_RGBA   , 4};
+static textypeinfo_t textype_palette                = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA   , 3};
+static textypeinfo_t textype_rgb                    = {TEXTYPE_RGB    , 3, 3, 4.0f, GL_RGB    , 3};
+static textypeinfo_t textype_rgba                   = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 3};
+static textypeinfo_t textype_palette_alpha          = {TEXTYPE_PALETTE, 1, 4, 4.0f, GL_RGBA   , 4};
+static textypeinfo_t textype_rgba_alpha             = {TEXTYPE_RGBA   , 4, 4, 4.0f, GL_RGBA   , 4};
+static textypeinfo_t textype_palette_compress       = {TEXTYPE_PALETTE, 1, 4, 0.5f, GL_RGBA   , GL_COMPRESSED_RGB_ARB};
+static textypeinfo_t textype_rgb_compress           = {TEXTYPE_RGB    , 3, 3, 0.5f, GL_RGB    , GL_COMPRESSED_RGB_ARB};
+static textypeinfo_t textype_rgba_compress          = {TEXTYPE_RGBA   , 4, 4, 0.5f, GL_RGBA   , GL_COMPRESSED_RGB_ARB};
+static textypeinfo_t textype_palette_alpha_compress = {TEXTYPE_PALETTE, 1, 4, 1.0f, GL_RGBA   , GL_COMPRESSED_RGBA_ARB};
+static textypeinfo_t textype_rgba_alpha_compress    = {TEXTYPE_RGBA   , 4, 4, 1.0f, GL_RGBA   , GL_COMPRESSED_RGBA_ARB};
 
 #define GLTEXTURETYPE_1D 0
 #define GLTEXTURETYPE_2D 1
@@ -69,7 +85,7 @@ typedef struct gltexture_s
        // pointer to next texture in texturepool chain
        struct gltexture_s *chain;
        // name of the texture (this might be removed someday), no duplicates
-       char identifier[32];
+       char identifier[MAX_QPATH + 32];
        // original data size in *inputtexels
        int inputwidth, inputheight, inputdepth;
        // copy of the original texture(s) supplied to the upload function, for
@@ -119,37 +135,75 @@ static int texturebuffersize = 0;
 
 static textypeinfo_t *R_GetTexTypeInfo(int textype, int flags)
 {
-       if (flags & TEXF_ALPHA)
+       if ((flags & TEXF_COMPRESS) && gl_texturecompression.integer >= 1 && gl_support_texture_compression)
        {
-               switch(textype)
+               if (flags & TEXF_ALPHA)
                {
-               case TEXTYPE_PALETTE:
-                       return &textype_palette_alpha;
-               case TEXTYPE_RGB:
-                       Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
-                       return NULL;
-               case TEXTYPE_RGBA:
-                       return &textype_rgba_alpha;
-               default:
-                       Host_Error("R_GetTexTypeInfo: unknown texture format");
-                       return NULL;
+                       switch(textype)
+                       {
+                       case TEXTYPE_PALETTE:
+                               return &textype_palette_alpha_compress;
+                       case TEXTYPE_RGB:
+                               Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
+                               return NULL;
+                       case TEXTYPE_RGBA:
+                               return &textype_rgba_alpha_compress;
+                       default:
+                               Host_Error("R_GetTexTypeInfo: unknown texture format");
+                               return NULL;
+                       }
+               }
+               else
+               {
+                       switch(textype)
+                       {
+                       case TEXTYPE_PALETTE:
+                               return &textype_palette_compress;
+                       case TEXTYPE_RGB:
+                               return &textype_rgb_compress;
+                       case TEXTYPE_RGBA:
+                               return &textype_rgba_compress;
+                       default:
+                               Host_Error("R_GetTexTypeInfo: unknown texture format");
+                               return NULL;
+                       }
                }
        }
        else
        {
-               switch(textype)
+               if (flags & TEXF_ALPHA)
                {
-               case TEXTYPE_PALETTE:
-                       return &textype_palette;
-               case TEXTYPE_RGB:
-                       return &textype_rgb;
-               case TEXTYPE_RGBA:
-                       return &textype_rgba;
-               default:
-                       Host_Error("R_GetTexTypeInfo: unknown texture format");
-                       return NULL;
+                       switch(textype)
+                       {
+                       case TEXTYPE_PALETTE:
+                               return &textype_palette_alpha;
+                       case TEXTYPE_RGB:
+                               Host_Error("R_GetTexTypeInfo: RGB format has no alpha, TEXF_ALPHA not allowed");
+                               return NULL;
+                       case TEXTYPE_RGBA:
+                               return &textype_rgba_alpha;
+                       default:
+                               Host_Error("R_GetTexTypeInfo: unknown texture format");
+                               return NULL;
+                       }
+               }
+               else
+               {
+                       switch(textype)
+                       {
+                       case TEXTYPE_PALETTE:
+                               return &textype_palette;
+                       case TEXTYPE_RGB:
+                               return &textype_rgb;
+                       case TEXTYPE_RGBA:
+                               return &textype_rgba;
+                       default:
+                               Host_Error("R_GetTexTypeInfo: unknown texture format");
+                               return NULL;
+                       }
                }
        }
+       return NULL; // this line only to hush compiler warnings
 }
 
 static void R_UploadTexture(gltexture_t *t);
@@ -408,7 +462,7 @@ static int R_CalcTexelDataSize (gltexture_t *glt)
                }
        }
 
-       return size * glt->textype->internalbytesperpixel * glt->sides;
+       return (int)(size * glt->textype->glinternalbytesperpixel) * glt->sides;
 }
 
 void R_TextureStats_Print(qboolean printeach, qboolean printpool, qboolean printtotal)
@@ -512,6 +566,16 @@ void R_Textures_Init (void)
        Cvar_RegisterVariable (&r_lerpimages);
        Cvar_RegisterVariable (&r_precachetextures);
        Cvar_RegisterVariable (&gl_texture_anisotropy);
+       Cvar_RegisterVariable (&gl_texturecompression);
+       Cvar_RegisterVariable (&gl_texturecompression_color);
+       Cvar_RegisterVariable (&gl_texturecompression_normal);
+       Cvar_RegisterVariable (&gl_texturecompression_gloss);
+       Cvar_RegisterVariable (&gl_texturecompression_glow);
+       Cvar_RegisterVariable (&gl_texturecompression_2d);
+       Cvar_RegisterVariable (&gl_texturecompression_q3bsplightmaps);
+       Cvar_RegisterVariable (&gl_texturecompression_q3bspdeluxemaps);
+       Cvar_RegisterVariable (&gl_texturecompression_sky);
+       Cvar_RegisterVariable (&gl_texturecompression_lightcubemaps);
 
        R_RegisterModule("R_Textures", r_textures_start, r_textures_shutdown, r_textures_newmap);
 }
@@ -737,6 +801,14 @@ static void R_Upload(gltexture_t *glt, unsigned char *data, int fragx, int fragy
                        }
                }
                mip = 0;
+               if (gl_support_texture_compression)
+               {
+                       if (gl_texturecompression.integer >= 2)
+                               qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_NICEST);
+                       else
+                               qglHint(GL_TEXTURE_COMPRESSION_HINT_ARB, GL_FASTEST);
+                       CHECKGLERROR
+               }
                switch(glt->texturetype)
                {
                case GLTEXTURETYPE_1D:
@@ -856,7 +928,7 @@ static rtexture_t *R_SetupTexture(rtexturepool_t *rtexturepool, const char *iden
        size = width * height * depth * sides * texinfo->inputbytesperpixel;
        if (size < 1)
        {
-               Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides);
+               Con_Printf ("R_LoadTexture: bogus texture size (%dx%dx%dx%dbppx%dsides = %d bytes)\n", width, height, depth, texinfo->inputbytesperpixel * 8, sides, size);
                return NULL;
        }