]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texturecube.cpp
Allow texture mipmap levels to be specified in datafiles
[libs/gl.git] / source / texturecube.cpp
index 19c3bf074b9a3557adf7668c4752f22bda3bcb8c..0613a39a8ebba335a56f42a9ec958c9a796ac45e 100644 (file)
@@ -51,7 +51,7 @@ TextureCube::TextureCube():
        static Require _req(ARB_texture_cube_map);
 }
 
-void TextureCube::storage(PixelFormat fmt, unsigned sz)
+void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
 {
        if(size>0)
                throw invalid_operation("TextureCube::storage");
@@ -60,19 +60,25 @@ void TextureCube::storage(PixelFormat fmt, unsigned sz)
 
        set_internal_format(fmt);
        size = sz;
+       levels = get_n_levels();
+       if(lv>0)
+               levels = min(levels, lv);
 }
 
 void TextureCube::allocate(unsigned level)
 {
-       if(allocated&(1<<level))
+       if(size==0)
+               throw invalid_operation("TextureCube::allocate");
+       if(level>=levels)
+               throw invalid_argument("TextureCube::allocate");
+       if(allocated&(64<<level))
                return;
 
        if(ARB_texture_storage)
        {
                BindRestore _bind(this);
-               unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
-               glTexStorage2D(target, n_levels, ifmt, size, size);
-               allocated |= (1<<n_levels)-1;
+               glTexStorage2D(target, levels, ifmt, size, size);
+               allocated |= (1<<levels)-1;
        }
        else
        {
@@ -96,15 +102,31 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, D
                return sub_image(face, level, 0, 0, s, s, fmt, type, data);
 
        BindRestore _bind(this);
-       glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
 
-       // XXX Allocation should be tracked per-face, but we'll run out of bits
-       allocated |= 1<<level;
-       if(gen_mipmap && level==0)
+       if(!allocated)
+               glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
+       glTexImage2D(face, level, ifmt, s, s, 0, get_upload_format(fmt), type, data);
+
+       if(level==0)
        {
-               // TODO Only do this once all faces are created
-               auto_generate_mipmap();
-               allocated |= (1<<get_n_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;
        }
 }
 
@@ -116,10 +138,10 @@ void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y,
        BindRestore _bind(this);
        allocate(level);
 
-       glTexSubImage2D(face, level, x, y, wd, ht, fmt, type, data);
+       glTexSubImage2D(face, level, x, y, wd, ht, get_upload_format(fmt), type, data);
 
-       if(gen_mipmap && level==0)
-               auto_generate_mipmap();
+       if(auto_gen_mipmap && level==0)
+               generate_mipmap();
 }
 
 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
@@ -143,7 +165,7 @@ void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool s
        image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
 }
 
-void TextureCube::image(const Graphics::Image &img, bool srgb)
+void TextureCube::image(const Graphics::Image &img, unsigned lv, bool srgb)
 {
        unsigned w = img.get_width();
        unsigned h = img.get_height();
@@ -154,7 +176,7 @@ void TextureCube::image(const Graphics::Image &img, bool srgb)
 
        PixelFormat fmt = pixelformat_from_graphics(img.get_format());
        if(size==0)
-               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, lv);
        else if(w!=size || h!=size)
                throw incompatible_data("TextureCube::image");
 
@@ -249,6 +271,7 @@ void TextureCube::Loader::init()
        add("image_data", &Loader::image_data);
        add("raw_data", &Loader::raw_data);
        add("storage", &Loader::storage);
+       add("storage", &Loader::storage_levels);
 }
 
 void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
@@ -276,11 +299,14 @@ void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
 
 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
 {
-       if(srgb)
-               fmt = get_srgb_pixelformat(fmt);
        obj.storage(fmt, s);
 }
 
+void TextureCube::Loader::storage_levels(PixelFormat fmt, unsigned s, unsigned l)
+{
+       obj.storage(fmt, s, l);
+}
+
 
 void operator>>(const LexicalConverter &conv, TextureCubeFace &face)
 {