From: Mikko Rasa Date: Wed, 10 Nov 2021 18:31:17 +0000 (+0200) Subject: Account for mipmap levels when computing texture data sizes X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=e2a707feb62c5df69884cca34d099dd2f7b7e4e2 Account for mipmap levels when computing texture data sizes This isn't 100% accurate, but good enough for resource management. --- diff --git a/source/backends/opengl/texture1d_backend.cpp b/source/backends/opengl/texture1d_backend.cpp index a270468b..b8424872 100644 --- a/source/backends/opengl/texture1d_backend.cpp +++ b/source/backends/opengl/texture1d_backend.cpp @@ -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(this)->width; - return id ? width*get_pixel_size(storage_fmt) : 0; + unsigned levels = static_cast(this)->levels; + + size_t level_size = width*get_pixel_size(storage_fmt); + size_t total_size = level_size; + for(unsigned i=0; i>=2) + total_size += level_size; + return total_size; } } // namespace GL diff --git a/source/backends/opengl/texture2d_backend.cpp b/source/backends/opengl/texture2d_backend.cpp index 72962117..2f396df4 100644 --- a/source/backends/opengl/texture2d_backend.cpp +++ b/source/backends/opengl/texture2d_backend.cpp @@ -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(this)->width; unsigned height = static_cast(this)->height; - return id ? width*height*get_pixel_size(format) : 0; + unsigned levels = static_cast(this)->levels; + + size_t level_size = width*height*get_pixel_size(format); + size_t total_size = level_size; + for(unsigned i=0; i>=2) + total_size += level_size; + return total_size; } void OpenGLTexture2D::unload() diff --git a/source/backends/opengl/texture2darray_backend.cpp b/source/backends/opengl/texture2darray_backend.cpp index 610229ba..edca615d 100644 --- a/source/backends/opengl/texture2darray_backend.cpp +++ b/source/backends/opengl/texture2darray_backend.cpp @@ -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>=2) + total_size += level_size*depth; + return total_size; +} + } // namespace GL } // namespace Msp diff --git a/source/backends/opengl/texture2darray_backend.h b/source/backends/opengl/texture2darray_backend.h index 42a33fc0..68a85386 100644 --- a/source/backends/opengl/texture2darray_backend.h +++ b/source/backends/opengl/texture2darray_backend.h @@ -10,6 +10,9 @@ class OpenGLTexture2DArray: public Texture3D { protected: OpenGLTexture2DArray(); + +public: + virtual std::size_t get_data_size() const; }; using Texture2DArrayBackend = OpenGLTexture2DArray; diff --git a/source/backends/opengl/texture3d_backend.cpp b/source/backends/opengl/texture3d_backend.cpp index 0796472a..961dd7e7 100644 --- a/source/backends/opengl/texture3d_backend.cpp +++ b/source/backends/opengl/texture3d_backend.cpp @@ -76,10 +76,19 @@ bool OpenGLTexture3D::is_array() const size_t OpenGLTexture3D::get_data_size() const { + if(!id) + return 0; + unsigned width = static_cast(this)->width; unsigned height = static_cast(this)->height; unsigned depth = static_cast(this)->depth; - return id ? width*height*depth*get_pixel_size(storage_fmt) : 0; + unsigned levels = static_cast(this)->levels; + + size_t level_size = width*height*depth*get_pixel_size(format); + size_t total_size = level_size; + for(unsigned i=0; i>=2) + total_size += level_size; + return total_size; } } // namespace GL diff --git a/source/backends/opengl/texturecube_backend.cpp b/source/backends/opengl/texturecube_backend.cpp index 2b9e09a9..1cc3f837 100644 --- a/source/backends/opengl/texturecube_backend.cpp +++ b/source/backends/opengl/texturecube_backend.cpp @@ -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(this)->size; - return id ? size*size*6*get_pixel_size(storage_fmt) : 0; + unsigned levels = static_cast(this)->levels; + + size_t level_size = size*size*get_pixel_size(storage_fmt); + size_t total_size = level_size; + for(unsigned i=0; i>=2) + total_size += level_size; + return total_size; }