]> git.tdb.fi Git - libs/gl.git/commitdiff
Refactor get_level_size in various texture classes
authorMikko Rasa <tdb@tdb.fi>
Fri, 13 Aug 2021 09:59:20 +0000 (12:59 +0300)
committerMikko Rasa <tdb@tdb.fi>
Fri, 13 Aug 2021 10:27:18 +0000 (13:27 +0300)
Also add a few argument validity checks to functions

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

index d472527c92322f4d05b0fe39640f7f91905133a9..71c736f04da4ecb7971d22461434169445614f6b 100644 (file)
@@ -64,6 +64,8 @@ void Texture1D::image(unsigned level, const void *data)
 {
        if(width==0)
                throw invalid_operation("Texture1D::image");
+       if(level>=levels)
+               throw out_of_range("Texture1D::image");
 
        unsigned w = get_level_size(level);
 
@@ -100,7 +102,9 @@ void Texture1D::image(unsigned level, PixelComponents comp, DataType type, const
 void Texture1D::sub_image(unsigned level, int x, unsigned wd, const void *data)
 {
        if(width==0)
-               throw invalid_operation("Texture3D::image");
+               throw invalid_operation("Texture1D::sub_image");
+       if(level>=levels)
+               throw out_of_range("Texture1D::sub_image");
 
        Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
        allocate(level);
index 1e9ec90344e242608caa55501268a08775a54aa2..78f8c9e983034f34eadec903c5c53ffb4ffd6c5d 100644 (file)
@@ -93,13 +93,13 @@ void Texture2D::image(unsigned level, const void *data)
 {
        if(width==0 || height==0)
                throw invalid_operation("Texture2D::image");
+       if(level>=levels)
+               throw out_of_range("Texture2D::image");
 
-       unsigned w = width;
-       unsigned h = height;
-       get_level_size(level, w, h);
+       LinAl::Vector<unsigned, 2> size = get_level_size(level);
 
        if(ARB_texture_storage)
-               return sub_image(level, 0, 0, w, h, data);
+               return sub_image(level, 0, 0, size.x, size.y, data);
 
        BindRestore _bind(this);
 
@@ -111,7 +111,7 @@ void Texture2D::image(unsigned level, const void *data)
 
        PixelComponents comp = get_components(storage_fmt);
        GLenum type = get_gl_type(get_component_type(storage_fmt));
-       glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
+       glTexImage2D(target, level, storage_fmt, size.x, size.y, 0, comp, type, data);
 
        allocated |= 1<<level;
        if(auto_gen_mipmap && level==0)
@@ -132,6 +132,8 @@ void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht
 {
        if(width==0 || height==0)
                throw invalid_operation("Texture2D::sub_image");
+       if(level>=levels)
+               throw out_of_range("Texture2D::sub_image");
 
        Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
        allocate(level);
@@ -179,15 +181,17 @@ unsigned Texture2D::get_n_levels() const
        return n;
 }
 
-void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
+LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
 {
-       w >>= level;
-       h >>= level;
+       unsigned w = width>>level;
+       unsigned h = height>>level;
 
        if(!w && h)
                w = 1;
        else if(!h && w)
                h = 1;
+
+       return LinAl::Vector<unsigned, 2>(w, h);
 }
 
 Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
index 7553b6b703c944a2144e989bf605d41b9ae2295c..877555c0f5ba43d4cdefb5a78af34225cc2e4756 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <string>
 #include <msp/graphics/image.h>
+#include <msp/linal/vector.h>
 #include "texture.h"
 
 namespace Msp {
@@ -86,7 +87,7 @@ public:
 
 private:
        unsigned get_n_levels() const;
-       void get_level_size(unsigned, unsigned &, unsigned &) const;
+       LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
 
 public:
        virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
index 1ecb4cf2be69e1b34a28f1f8b8cea643b84e2325..1928d4e3503c6f68f2446125ffe4b423c96ac5fc 100644 (file)
@@ -17,12 +17,11 @@ Texture2DArray::Texture2DArray():
 
 void Texture2DArray::layer_image(unsigned level, unsigned z, const void *data)
 {
-       unsigned w = get_width();
-       unsigned h = get_height();
-       unsigned d = get_depth();
-       get_level_size(level, w, h, d);
+       if(level>=levels || z>=depth)
+               throw out_of_range("Texture2DArray::layer_image");
 
-       sub_image(level, 0, 0, z, w, h, 1, data);
+       LinAl::Vector<unsigned, 3> size = get_level_size(level);
+       sub_image(level, 0, 0, z, size.x, size.y, 1, data);
 }
 
 void Texture2DArray::layer_image(unsigned level, unsigned z, PixelComponents comp, DataType type, const void *data)
index a268857d4996cd9bc08d98d9d5c8918bc7d25a3c..f6268d697d6d60c00735c6230e6ff6c701eb8b7c 100644 (file)
@@ -80,14 +80,13 @@ void Texture3D::image(unsigned level, const void *data)
 {
        if(width==0 || height==0 || depth==0)
                throw invalid_operation("Texture3D::image");
+       if(level>=levels)
+               throw out_of_range("Texture3D::image");
 
-       unsigned w = width;
-       unsigned h = height;
-       unsigned d = depth;
-       get_level_size(level, w, h, d);
+       LinAl::Vector<unsigned, 3> size = get_level_size(level);
 
        if(ARB_texture_storage)
-               return sub_image(level, 0, 0, 0, w, h, d, data);
+               return sub_image(level, 0, 0, 0, size.x, size.y, size.z, data);
 
        BindRestore _bind(this);
 
@@ -99,7 +98,7 @@ void Texture3D::image(unsigned level, const void *data)
 
        PixelComponents comp = get_components(storage_fmt);
        GLenum type = get_gl_type(get_component_type(storage_fmt));
-       glTexImage3D(target, level, storage_fmt, width, height, depth, 0, comp, type, data);
+       glTexImage3D(target, level, storage_fmt, size.x, size.y, size.z, 0, comp, type, data);
 
        allocated |= 1<<level;
        if(auto_gen_mipmap && level==0)
@@ -119,7 +118,9 @@ void Texture3D::image(unsigned level, PixelComponents comp, DataType type, const
 void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, const void *data)
 {
        if(width==0 || height==0 || depth==0)
-               throw invalid_operation("Texture3D::image");
+               throw invalid_operation("Texture3D::sub_image");
+       if(level>=levels)
+               throw out_of_range("Texture3D::sub_image");
 
        Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
        allocate(level);
@@ -171,10 +172,11 @@ unsigned Texture3D::get_n_levels() const
        return n;
 }
 
-void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
+LinAl::Vector<unsigned, 3> Texture3D::get_level_size(unsigned level) const
 {
-       w >>= level;
-       h >>= level;
+       unsigned w = width>>level;
+       unsigned h = height>>level;
+       unsigned d = depth;
        if(target!=GL_TEXTURE_2D_ARRAY)
                d >>= level;
 
@@ -184,6 +186,8 @@ void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigne
                h = 1;
        if(!d && (w || h))
                d = 1;
+
+       return LinAl::Vector<unsigned, 3>(w, h, d);
 }
 
 UInt64 Texture3D::get_data_size() const
index 1f50e056d8cb286d8e99b4c739f83e5f09c777bd..df52bf66ee8daeb1e1a879440c63ed52ab799833 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_TEXTURE3D_H_
 
 #include <string>
+#include <msp/linal/vector.h>
 #include "texture.h"
 
 namespace Msp {
@@ -27,14 +28,13 @@ public:
                void storage_levels(PixelFormat, unsigned, unsigned, unsigned, unsigned);
        };
 
-private:
+protected:
        unsigned width;
        unsigned height;
        unsigned depth;
        unsigned levels;
        unsigned allocated;
 
-protected:
        Texture3D(GLenum);
 public:
        Texture3D();
@@ -83,7 +83,7 @@ public:
        unsigned get_depth() const { return depth; }
 protected:
        unsigned get_n_levels() const;
-       void get_level_size(unsigned, unsigned &, unsigned &, unsigned &) const;
+       LinAl::Vector<unsigned, 3> get_level_size(unsigned) const;
 
 public:
        virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
index 93faa3703ab993ee29e05710347c56be82c3bca2..543bb9d40ec00e954ce894d9699e86c364405dd8 100644 (file)
@@ -103,10 +103,10 @@ void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
 {
        if(size==0)
                throw invalid_operation("TextureCube::image");
+       if(level>=levels)
+               throw out_of_range("TextureCube::image");
 
        unsigned s = get_level_size(level);
-       if(s==0)
-               throw out_of_range("TextureCube::image");
 
        if(ARB_texture_storage)
                return sub_image(face, level, 0, 0, s, s, data);
@@ -157,6 +157,8 @@ void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y,
 {
        if(size==0)
                throw invalid_operation("TextureCube::sub_image");
+       if(level>=levels)
+               throw out_of_range("TextureCube::sub_image");
 
        Conditional<BindRestore> _bind(!ARB_direct_state_acess, this);
        allocate(level);