From 2b7f8e45e75bec30c1ea27fc0efd8286f67adc3f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 13 Aug 2021 12:59:20 +0300 Subject: [PATCH] Refactor get_level_size in various texture classes Also add a few argument validity checks to functions --- source/core/texture1d.cpp | 6 +++++- source/core/texture2d.cpp | 20 ++++++++++++-------- source/core/texture2d.h | 3 ++- source/core/texture2darray.cpp | 9 ++++----- source/core/texture3d.cpp | 24 ++++++++++++++---------- source/core/texture3d.h | 6 +++--- source/core/texturecube.cpp | 6 ++++-- 7 files changed, 44 insertions(+), 30 deletions(-) diff --git a/source/core/texture1d.cpp b/source/core/texture1d.cpp index d472527c..71c736f0 100644 --- a/source/core/texture1d.cpp +++ b/source/core/texture1d.cpp @@ -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 _bind(!ARB_direct_state_access, this); allocate(level); diff --git a/source/core/texture2d.cpp b/source/core/texture2d.cpp index 1e9ec903..78f8c9e9 100644 --- a/source/core/texture2d.cpp +++ b/source/core/texture2d.cpp @@ -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 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<=levels) + throw out_of_range("Texture2D::sub_image"); Conditional _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 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(w, h); } Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *) diff --git a/source/core/texture2d.h b/source/core/texture2d.h index 7553b6b7..877555c0 100644 --- a/source/core/texture2d.h +++ b/source/core/texture2d.h @@ -3,6 +3,7 @@ #include #include +#include #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 get_level_size(unsigned) const; public: virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0); diff --git a/source/core/texture2darray.cpp b/source/core/texture2darray.cpp index 1ecb4cf2..1928d4e3 100644 --- a/source/core/texture2darray.cpp +++ b/source/core/texture2darray.cpp @@ -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 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) diff --git a/source/core/texture3d.cpp b/source/core/texture3d.cpp index a268857d..f6268d69 100644 --- a/source/core/texture3d.cpp +++ b/source/core/texture3d.cpp @@ -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 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<=levels) + throw out_of_range("Texture3D::sub_image"); Conditional _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 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(w, h, d); } UInt64 Texture3D::get_data_size() const diff --git a/source/core/texture3d.h b/source/core/texture3d.h index 1f50e056..df52bf66 100644 --- a/source/core/texture3d.h +++ b/source/core/texture3d.h @@ -2,6 +2,7 @@ #define MSP_GL_TEXTURE3D_H_ #include +#include #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 get_level_size(unsigned) const; public: virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; } diff --git a/source/core/texturecube.cpp b/source/core/texturecube.cpp index 93faa370..543bb9d4 100644 --- a/source/core/texturecube.cpp +++ b/source/core/texturecube.cpp @@ -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 _bind(!ARB_direct_state_acess, this); allocate(level); -- 2.43.0