]> git.tdb.fi Git - libs/gl.git/commitdiff
Improve allocation handling in cube map textures
authorMikko Rasa <tdb@tdb.fi>
Sat, 30 Jan 2021 18:03:47 +0000 (20:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sun, 31 Jan 2021 00:26:45 +0000 (02:26 +0200)
While there isn't enough bits to track all face allocations, tracking
the base level per face is possible.  This allows mipmap generation to
work more sensibly.

source/texturecube.cpp
source/texturecube.h

index 82447e65b8f91cb53f2170aa6e44617ccbbf8aff..14ddf43d41c2cdc076e4489a62e2ed2e7341dd36 100644 (file)
@@ -71,7 +71,7 @@ void TextureCube::allocate(unsigned level)
                throw invalid_operation("TextureCube::allocate");
        if(level>=levels)
                throw invalid_argument("TextureCube::allocate");
-       if(allocated&(1<<level))
+       if(allocated&(64<<level))
                return;
 
        if(ARB_texture_storage)
@@ -107,13 +107,26 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, D
                glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
        glTexImage2D(face, level, ifmt, s, s, 0, get_upload_format(fmt), type, data);
 
-       // XXX Allocation should be tracked per-face, but we'll run out of bits
-       allocated |= 1<<level;
-       if(auto_gen_mipmap && level==0)
+       if(level==0)
        {
-               // TODO Only do this once all faces are created
-               generate_mipmap();
-               allocated |= (1<<levels)-1;
+               allocated |= 1<<get_face_index(face);
+               if((allocated&63)==63)
+               {
+                       allocated |= 64;
+                       if(auto_gen_mipmap)
+                       {
+                               generate_mipmap();
+                               allocated |= (64<<levels)-1;
+                       }
+               }
+       }
+       else if(!(allocated&(64<<level)))
+       {
+               for(unsigned i=0; i<6; ++i)
+                       if(enumerate_faces(i)!=face)
+                               glTexImage2D(enumerate_faces(i), level, ifmt, s, s, 0, get_upload_format(fmt), type, 0);
+
+               allocated |= 64<<level;
        }
 }
 
index f5a364047874aa10569e2fe1a4c727e69b47063b..dabf2b8883690228c9e12e9babe232a95326015e 100644 (file)
@@ -51,6 +51,8 @@ public:
 private:
        unsigned size;
        unsigned levels;
+       /* Lowest six bits track allocation status of faces on the base level.  Bit
+       seven is set if the entire base level is allocated. */
        unsigned allocated;
 
        static TextureCubeFace face_order[6];