]> git.tdb.fi Git - libs/gl.git/commitdiff
Explicitly define the number of mipmap levels in textures
authorMikko Rasa <tdb@tdb.fi>
Mon, 2 Jul 2018 12:46:13 +0000 (15:46 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 2 Jul 2018 12:46:13 +0000 (15:46 +0300)
Storage() no longer uses the minification filter to decide whether to
allocate mipmaps or not.  That functionality has been moved to image(),
specifically the version taking a Graphics::Image.

source/texture1d.cpp
source/texture1d.h
source/texture2d.cpp
source/texture2d.h
source/texture3d.cpp
source/texture3d.h
source/texturecube.cpp
source/texturecube.h

index c5e57df22843d0e0dc84675c24d08bebcaaf1c74..a1ee418b22381a27916c407a6d8050459c02ddb0 100644 (file)
@@ -19,7 +19,7 @@ Texture1D::Texture1D():
        static Require _req(MSP_texture1D);
 }
 
-void Texture1D::storage(PixelFormat fmt, unsigned wd)
+void Texture1D::storage(PixelFormat fmt, unsigned wd, unsigned lv)
 {
        if(width>0)
                throw invalid_operation("Texture1D::storage");
@@ -28,26 +28,30 @@ void Texture1D::storage(PixelFormat fmt, unsigned wd)
 
        set_internal_format(fmt);
        width = wd;
+       levels = get_n_levels();
+       if(lv)
+               levels = min(levels, lv);
 }
 
 void Texture1D::allocate(unsigned level)
 {
        if(width==0)
                throw invalid_operation("Texture1D::allocate");
+       if(level>=levels)
+               throw invalid_argument("Texture1D::allocate");
        if(allocated&(1<<level))
                return;
 
        if(ARB_texture_storage)
        {
-               unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
                if(ARB_direct_state_access)
-                       glTextureStorage1D(id, n_levels, ifmt, width);
+                       glTextureStorage1D(id, levels, ifmt, width);
                else
                {
                        BindRestore _bind(this);
-                       glTexStorage1D(target, n_levels, ifmt, width);
+                       glTexStorage1D(target, levels, ifmt, width);
                }
-               allocated |= (1<<n_levels)-1;
+               allocated |= (1<<levels)-1;
        }
        else
        {
@@ -103,7 +107,10 @@ void Texture1D::image(const Graphics::Image &img, bool srgb)
        unsigned w = img.get_width();
        PixelFormat fmt = pixelformat_from_graphics(img.get_format());
        if(width==0)
-               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
+       {
+               unsigned l = (is_mipmapped(min_filter) ? 0 : 1);
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, l);
+       }
        else if(w!=width)
                throw incompatible_data("Texture1D::image");
 
index 5e6f254977f877a20565e131412bb333f6248ab4..506356c77676d98cea1a01d52d8a8f373a71f024 100644 (file)
@@ -23,12 +23,13 @@ public:
 
 private:
        unsigned width;
+       unsigned levels;
        unsigned allocated;
 
 public:
        Texture1D();
 
-       void storage(PixelFormat, unsigned);
+       void storage(PixelFormat, unsigned, unsigned = 0);
        void allocate(unsigned);
        void image(unsigned, PixelFormat, DataType, const void *);
        void sub_image(unsigned, int, unsigned, PixelFormat, DataType, const void *);
index 8d6e8471edb72574340e6e870514bfb652a13113..44ee6406420e85f261c988f5e8b14f678c90cfe3 100644 (file)
@@ -46,7 +46,7 @@ Texture2D::~Texture2D()
        set_manager(0);
 }
 
-void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
+void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
 {
        if(width>0)
                throw invalid_operation("Texture2D::storage");
@@ -56,26 +56,30 @@ void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht)
        set_internal_format(fmt);
        width = wd;
        height = ht;
+       levels = get_n_levels();
+       if(lv>0)
+               levels = min(levels, lv);
 }
 
 void Texture2D::allocate(unsigned level)
 {
        if(width==0 || height==0)
                throw invalid_operation("Texture2D::allocate");
+       if(level>=levels)
+               throw invalid_argument("Texture2D::allocate");
        if(allocated&(1<<level))
                return;
 
        if(ARB_texture_storage)
        {
-               unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
                if(ARB_direct_state_access)
-                       glTextureStorage2D(id, n_levels, ifmt, width, height);
+                       glTextureStorage2D(id, levels, ifmt, width, height);
                else
                {
                        BindRestore _bind(this);
-                       glTexStorage2D(target, n_levels, ifmt, width, height);
+                       glTexStorage2D(target, levels, ifmt, width, height);
                }
-               allocated |= (1<<n_levels)-1;
+               allocated |= (1<<levels)-1;
        }
        else
        {
@@ -136,7 +140,10 @@ void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
        unsigned h = img.get_height();
        PixelFormat fmt = pixelformat_from_graphics(img.get_format());
        if(width==0)
-               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h);
+       {
+               unsigned l = (is_mipmapped(min_filter) ? 0 : 1);
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, l);
+       }
        else if(w!=width || h!=height)
                throw incompatible_data("Texture2D::image");
 
index 1f8b5f7ea557e94abcded7d8f7fd423b7fea7cdd..2a49eca572ee06e25a7a770488b0639d6fd07fc9 100644 (file)
@@ -33,15 +33,19 @@ private:
 
        unsigned width;
        unsigned height;
+       unsigned levels;
        unsigned allocated;
 
 public:
        Texture2D(ResourceManager * = 0);
        virtual ~Texture2D();
 
-       /** Defines storage structure for the texture.  Must be called before an
-       image can be uploaded.  Once storage is defined, it can't be changed. */
-       void storage(PixelFormat fmt, unsigned wd, unsigned ht);
+       /** Defines storage structure for the texture.  If lv is zero, the number
+       of mipmap levels is automatically determined from storage dimensions.
+
+       Must be called before an image can be uploaded.  Once storage is defined,
+       it can't be changed. */
+       void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv = 0);
 
        /** Allocates storage for the texture.  The contents are initially
        undefined.  If storage has already been allocated, does nothing. */
index 5e1f9866e185e6ecdb047f167a26ffa8054d03ea..36fdfa0f3b8f6b591fd19b7f4d4047fa45c0ba06 100644 (file)
@@ -33,7 +33,7 @@ Texture3D::Texture3D():
        static Require _req(EXT_texture3D);
 }
 
-void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
+void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv)
 {
        if(width>0)
                throw invalid_operation("Texture3D::storage");
@@ -44,26 +44,30 @@ void Texture3D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp)
        width = wd;
        height = ht;
        depth = dp;
+       levels = get_n_levels();
+       if(lv>0)
+               levels = min(levels, lv);
 }
 
 void Texture3D::allocate(unsigned level)
 {
        if(width==0 || height==0 || depth==0)
                throw invalid_operation("Texture3D::allocate");
+       if(level>=levels)
+               throw invalid_argument("Texture3D::allocate");
        if(allocated&(1<<level))
                return;
 
        if(ARB_texture_storage)
        {
-               unsigned n_levels = (is_mipmapped(min_filter) ? get_n_levels() : 1);
                if(ARB_direct_state_access)
-                       glTextureStorage3D(id, n_levels, ifmt, width, height, depth);
+                       glTextureStorage3D(id, levels, ifmt, width, height, depth);
                else
                {
                        BindRestore _bind(this);
-                       glTexStorage3D(target, n_levels, ifmt, width, height, depth);
+                       glTexStorage3D(target, levels, ifmt, width, height, depth);
                }
-               allocated |= (1<<n_levels)-1;
+               allocated |= (1<<levels)-1;
        }
        else
        {
@@ -183,7 +187,10 @@ void Texture3D::image(const Graphics::Image &img, bool srgb)
 
        PixelFormat fmt = pixelformat_from_graphics(img.get_format());
        if(width==0)
-               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d);
+       {
+               unsigned l = (is_mipmapped(min_filter) ? 0 : 1);
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, h, d, l);
+       }
        else if(w!=width || h!=height || d!=depth)
                throw incompatible_data("Texture3D::load_image");
 
index cf336e52b0227fb859d2125cb686f07d70db46b3..437c852ab92f943ed569de54eb4c61f6b34402bf 100644 (file)
@@ -30,6 +30,7 @@ private:
        unsigned width;
        unsigned height;
        unsigned depth;
+       unsigned levels;
        unsigned allocated;
 
 protected:
@@ -37,9 +38,12 @@ protected:
 public:
        Texture3D();
 
-       /** Defines storage structure for the texture.  Must be called before an
-       image can be uploaded.  Once storage is defined, it can't be changed. */
-       void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp);
+       /** Defines storage structure for the texture.  If lv is zero, the number
+       of mipmap levels is automatically determined from storage dimensions.
+
+       Must be called before an image can be uploaded.  Once storage is defined,
+       it can't be changed. */
+       void storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned dp, unsigned lv = 0);
 
        /** Allocates storage for the texture.  The contents are initially
        undefined.  If storage has already been allocated, does nothing. */
index b9a8f5dd02b8cacb73f63df7bce028a5920035cd..ee8c71c5b0399e88f7c15b375878cf64623a4472 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,21 +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(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
        {
@@ -156,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) ? 0 : 1);
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, l);
+       }
        else if(w!=size || h!=size)
                throw incompatible_data("TextureCube::image");
 
index ea1c8296190ade9703121238771c9277baba3d04..0882a5e31004c3cef3e129b65ff24a2671e88fc0 100644 (file)
@@ -50,6 +50,7 @@ public:
 
 private:
        unsigned size;
+       unsigned levels;
        unsigned allocated;
 
        static TextureCubeFace face_order[6];
@@ -59,9 +60,12 @@ private:
 public:
        TextureCube();
 
-       /** Defines storage structure for the texture.  Must be called before an
-       image can be uploaded.  Once storage is defined, it can't be changed. */
-       void storage(PixelFormat fmt, unsigned size);
+       /** Defines storage structure for the texture.  If lv is zero, the number
+       of mipmap levels is automatically determined from storage dimensions.
+
+       Must be called before an image can be uploaded.  Once storage is defined,
+       it can't be changed. */
+       void storage(PixelFormat fmt, unsigned size, unsigned lv = 0);
 
        /** Allocates storage for the cube faces.  The contents are initially
        undefined.  If storage has already been allocated, does nothing. */