]> git.tdb.fi Git - libs/gl.git/commitdiff
Account for mipmap levels when computing texture data sizes
authorMikko Rasa <tdb@tdb.fi>
Wed, 10 Nov 2021 18:31:17 +0000 (20:31 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 10 Nov 2021 18:31:17 +0000 (20:31 +0200)
This isn't 100% accurate, but good enough for resource management.

source/backends/opengl/texture1d_backend.cpp
source/backends/opengl/texture2d_backend.cpp
source/backends/opengl/texture2darray_backend.cpp
source/backends/opengl/texture2darray_backend.h
source/backends/opengl/texture3d_backend.cpp
source/backends/opengl/texturecube_backend.cpp

index a270468b141883437f46d51de02a54888f69ee6f..b8424872b37c3ee960801bfda52eddd4f4185611 100644 (file)
@@ -64,8 +64,17 @@ void OpenGLTexture1D::sub_image(unsigned level, int x, unsigned wd, const void *
 
 size_t OpenGLTexture1D::get_data_size() const
 {
+       if(!id)
+               return 0;
+
        unsigned width = static_cast<const Texture1D *>(this)->width;
-       return id ? width*get_pixel_size(storage_fmt) : 0;
+       unsigned levels = static_cast<const Texture1D *>(this)->levels;
+
+       size_t level_size = width*get_pixel_size(storage_fmt);
+       size_t total_size = level_size;
+       for(unsigned i=0; i<levels; ++i, level_size>>=2)
+               total_size += level_size;
+       return total_size;
 }
 
 } // namespace GL
index 72962117bc9967d98228735063b5189e10368dfd..2f396df4180978ec52821ec59247ed5a76293be4 100644 (file)
@@ -102,9 +102,18 @@ Resource::AsyncLoader *OpenGLTexture2D::load(IO::Seekable &io, const Resources *
 
 uint64_t OpenGLTexture2D::get_data_size() const
 {
+       if(!id)
+               return 0;
+
        unsigned width = static_cast<const Texture2D *>(this)->width;
        unsigned height = static_cast<const Texture2D *>(this)->height;
-       return id ? width*height*get_pixel_size(format) : 0;
+       unsigned levels = static_cast<const Texture2D *>(this)->levels;
+
+       size_t level_size = width*height*get_pixel_size(format);
+       size_t total_size = level_size;
+       for(unsigned i=0; i<levels; ++i, level_size>>=2)
+               total_size += level_size;
+       return total_size;
 }
 
 void OpenGLTexture2D::unload()
index 610229ba4acf96f16cb7d4cf92761ab21c5c128f..edca615d91a8440ca7cabfa3fca8b7db235a9a0e 100644 (file)
@@ -10,5 +10,17 @@ OpenGLTexture2DArray::OpenGLTexture2DArray():
        static Require _req(EXT_texture_array);
 }
 
+size_t OpenGLTexture2DArray::get_data_size() const
+{
+       if(!id)
+               return 0;
+
+       size_t level_size = width*height*get_pixel_size(format);
+       size_t total_size = level_size;
+       for(unsigned i=0; i<levels; ++i, level_size>>=2)
+               total_size += level_size*depth;
+       return total_size;
+}
+
 } // namespace GL
 } // namespace Msp
index 42a33fc0979cbcdb846256be1f26bb58e2029684..68a85386dd1185bb253ccdcd7afb1b77ce4bdc8b 100644 (file)
@@ -10,6 +10,9 @@ class OpenGLTexture2DArray: public Texture3D
 {
 protected:
        OpenGLTexture2DArray();
+
+public:
+       virtual std::size_t get_data_size() const;
 };
 
 using Texture2DArrayBackend = OpenGLTexture2DArray;
index 0796472a041de543d67239591928d393fbc97678..961dd7e721766596f439312c45d11348e4eaebc7 100644 (file)
@@ -76,10 +76,19 @@ bool OpenGLTexture3D::is_array() const
 
 size_t OpenGLTexture3D::get_data_size() const
 {
+       if(!id)
+               return 0;
+
        unsigned width = static_cast<const Texture3D *>(this)->width;
        unsigned height = static_cast<const Texture3D *>(this)->height;
        unsigned depth = static_cast<const Texture3D *>(this)->depth;
-       return id ? width*height*depth*get_pixel_size(storage_fmt) : 0;
+       unsigned levels = static_cast<const Texture3D *>(this)->levels;
+
+       size_t level_size = width*height*depth*get_pixel_size(format);
+       size_t total_size = level_size;
+       for(unsigned i=0; i<levels; ++i, level_size>>=2)
+               total_size += level_size;
+       return total_size;
 }
 
 } // namespace GL
index 2b9e09a9c90ecb9a0f27ce48b37e1b9ae747de97..1cc3f8377f23eaf3027d8e54caeb58f83dbefd7b 100644 (file)
@@ -77,8 +77,17 @@ void OpenGLTextureCube::sub_image(unsigned face, unsigned level, int x, int y, u
 
 size_t OpenGLTextureCube::get_data_size() const
 {
+       if(!id)
+               return 0;
+
        unsigned size = static_cast<const TextureCube *>(this)->size;
-       return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
+       unsigned levels = static_cast<const TextureCube *>(this)->levels;
+
+       size_t level_size = size*size*get_pixel_size(storage_fmt);
+       size_t total_size = level_size;
+       for(unsigned i=0; i<levels; ++i, level_size>>=2)
+               total_size += level_size;
+       return total_size;
 }