]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texturecube.cpp
Be less eager to optimize constant conditions in loops
[libs/gl.git] / source / texturecube.cpp
index 833ef28f0ebd78add08ccfae4c8555df87dff288..03e65167d73fee7a1d89ba9ea3cea7ef2c68d81d 100644 (file)
@@ -45,39 +45,40 @@ unsigned TextureCube::orientations[12] =
 
 TextureCube::TextureCube():
        Texture(GL_TEXTURE_CUBE_MAP),
-       ifmt(RGB),
        size(0),
        allocated(0)
 {
        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");
        if(sz==0)
                throw invalid_argument("TextureCube::storage");
 
-       if(MSP_sized_internal_formats)
-               fmt = get_sized_pixelformat(fmt);
-       require_pixelformat(fmt);
-
-       ifmt = fmt;
+       set_internal_format(fmt);
        size = sz;
+       levels = get_n_levels();
+       if(lv>0)
+               levels = min(levels, lv);
 }
 
 void TextureCube::allocate(unsigned level)
 {
+       if(size==0)
+               throw invalid_operation("TextureCube::allocate");
+       if(level>=levels)
+               throw invalid_argument("TextureCube::allocate");
        if(allocated&(1<<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
        {
@@ -101,7 +102,7 @@ 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);
+       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;
@@ -121,7 +122,7 @@ 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();
@@ -159,7 +160,10 @@ 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);
+       {
+               unsigned l = (is_mipmapped(min_filter) ? mipmap_levels ? mipmap_levels : 0 : 1);
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, l);
+       }
        else if(w!=size || h!=size)
                throw incompatible_data("TextureCube::image");