From 260ecf71f02d4b3397e4f6c80d96a7c4ba473185 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 1 Nov 2021 14:42:49 +0200 Subject: [PATCH] Move the whole-texture image call and raw data loading to base class This reduces duplication in the loading code and promotes uniform behaviour across the different texture classes. --- source/core/texture.cpp | 9 +++++++++ source/core/texture.h | 6 ++++++ source/core/texture1d.cpp | 6 ------ source/core/texture1d.h | 6 +----- source/core/texture2d.cpp | 8 -------- source/core/texture2d.h | 6 +----- source/core/texture2dmultisample.cpp | 5 +++++ source/core/texture2dmultisample.h | 1 + source/core/texture3d.cpp | 6 ------ source/core/texture3d.h | 1 - source/core/texturecube.cpp | 13 +++++++++---- source/core/texturecube.h | 2 ++ 12 files changed, 34 insertions(+), 35 deletions(-) diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 78053386..175e76a2 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -92,6 +92,7 @@ Texture::Loader::Loader(Texture &t, Collection *c): add("generate_mipmap", &Loader::generate_mipmap); add("image_data", &Loader::image_data); add("mipmap_levels", &Loader::mipmap_levels); + add("raw_data", &Loader::raw_data); } void Texture::Loader::finish() @@ -143,5 +144,13 @@ void Texture::Loader::mipmap_levels(unsigned l) levels = l; } +void Texture::Loader::raw_data(const string &data) +{ + if(obj.manager) + obj.set_manager(0); + + obj.image(0, data.data()); +} + } // namespace GL } // namespace Msp diff --git a/source/core/texture.h b/source/core/texture.h index ff56e85c..8db5dad8 100644 --- a/source/core/texture.h +++ b/source/core/texture.h @@ -53,6 +53,7 @@ protected: void generate_mipmap(bool); void image_data(const std::string &); void mipmap_levels(unsigned); + void raw_data(const std::string &); }; public: @@ -91,6 +92,11 @@ public: using TextureBackend::generate_mipmap; + /** Replaces contents of an entire mipmap level. Allocated storage must + exist. The image data is interpreted according to the storage format and + must have size matching the selected mipmap level. */ + virtual void image(unsigned level, const void *) = 0; + /** Loads an image into the texture from a file. */ virtual void load_image(const std::string &, unsigned = 0); diff --git a/source/core/texture1d.cpp b/source/core/texture1d.cpp index ae4347e3..7d357ff2 100644 --- a/source/core/texture1d.cpp +++ b/source/core/texture1d.cpp @@ -82,16 +82,10 @@ Texture1D::Loader::Loader(Texture1D &t, Collection &c): void Texture1D::Loader::init() { - add("raw_data", &Loader::raw_data); add("storage", &Loader::storage); add("storage", &Loader::storage_levels); } -void Texture1D::Loader::raw_data(const string &data) -{ - obj.image(0, data.data()); -} - void Texture1D::Loader::storage(PixelFormat fmt, unsigned w) { obj.storage(fmt, w); diff --git a/source/core/texture1d.h b/source/core/texture1d.h index f99d3ef0..efd0517d 100644 --- a/source/core/texture1d.h +++ b/source/core/texture1d.h @@ -23,7 +23,6 @@ public: private: void init(); - void raw_data(const std::string &); void storage(PixelFormat, unsigned); void storage_levels(PixelFormat, unsigned, unsigned); }; @@ -38,10 +37,7 @@ public: cannot be changed once set. */ void storage(PixelFormat, unsigned wd, unsigned lv = 0); - /** Replaces contents of an entire mipmap level. Allocated storage must - exist. The image data is interpreted according to the storage format and - must have size matching the selected mipmap level. */ - void image(unsigned level, const void *); + virtual void image(unsigned level, const void *); /** Replaces a range of texels in the texture. Allocated storage must exist. The image data is interpreted according to the storage format and diff --git a/source/core/texture2d.cpp b/source/core/texture2d.cpp index 41f4ab11..9b1c16ea 100644 --- a/source/core/texture2d.cpp +++ b/source/core/texture2d.cpp @@ -99,18 +99,10 @@ Texture2D::Loader::Loader(Texture2D &t, Collection &c): void Texture2D::Loader::init() { - add("raw_data", &Loader::raw_data); add("storage", &Loader::storage); add("storage", &Loader::storage_levels); } -void Texture2D::Loader::raw_data(const string &data) -{ - if(obj.manager) - obj.set_manager(0); - obj.image(0, data.data()); -} - void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h) { obj.storage(fmt, w, h); diff --git a/source/core/texture2d.h b/source/core/texture2d.h index 1c750bd1..61a3f29f 100644 --- a/source/core/texture2d.h +++ b/source/core/texture2d.h @@ -24,7 +24,6 @@ public: private: void init(); - void raw_data(const std::string &); void storage(PixelFormat, unsigned, unsigned); void storage_levels(PixelFormat, unsigned, unsigned, unsigned); }; @@ -42,10 +41,7 @@ public: cannot be changed once set. */ void storage(PixelFormat, unsigned wd, unsigned ht, unsigned lv = 0); - /** Replaces contents of an entire mipmap level. Allocated storage must - exist. The image data is interpreted according to the storage format and - must have size matching the selected mipmap level. */ - virtual void image(unsigned level, const void *); + void image(unsigned level, const void *) override; /** Replaces a rectangular region of the texture. Allocated storage must exist. The image data is interpreted according to the storage format and diff --git a/source/core/texture2dmultisample.cpp b/source/core/texture2dmultisample.cpp index 1c0106d7..ce35bdb7 100644 --- a/source/core/texture2dmultisample.cpp +++ b/source/core/texture2dmultisample.cpp @@ -28,6 +28,11 @@ void Texture2DMultisample::storage(PixelFormat fmt, unsigned wd, unsigned ht, un allocate(); } +void Texture2DMultisample::image(unsigned, const void *) +{ + throw invalid_operation("Texture2DMultisample::image"); +} + void Texture2DMultisample::image(const Graphics::Image &, unsigned) { throw invalid_operation("Texture2DMultisample::image"); diff --git a/source/core/texture2dmultisample.h b/source/core/texture2dmultisample.h index 82ab0818..20c0f864 100644 --- a/source/core/texture2dmultisample.h +++ b/source/core/texture2dmultisample.h @@ -29,6 +29,7 @@ public: for the texture. */ void storage(PixelFormat, unsigned wd, unsigned ht, unsigned sm); + virtual void image(unsigned, const void *); virtual void image(const Graphics::Image &, unsigned = 0); unsigned get_width() const { return width; } diff --git a/source/core/texture3d.cpp b/source/core/texture3d.cpp index 0125dd09..c901556a 100644 --- a/source/core/texture3d.cpp +++ b/source/core/texture3d.cpp @@ -111,16 +111,10 @@ Texture3D::Loader::Loader(Texture3D &t, Collection &c): void Texture3D::Loader::init() { - add("raw_data", &Loader::raw_data); add("storage", &Loader::storage); add("storage", &Loader::storage_levels); } -void Texture3D::Loader::raw_data(const string &data) -{ - obj.image(0, data.data()); -} - void Texture3D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned d) { obj.storage(fmt, w, h, d); diff --git a/source/core/texture3d.h b/source/core/texture3d.h index 1fbcf4ee..c1bf8ed8 100644 --- a/source/core/texture3d.h +++ b/source/core/texture3d.h @@ -24,7 +24,6 @@ public: private: void init(); - void raw_data(const std::string &); void storage(PixelFormat, unsigned, unsigned, unsigned); void storage_levels(PixelFormat, unsigned, unsigned, unsigned, unsigned); }; diff --git a/source/core/texturecube.cpp b/source/core/texturecube.cpp index ff2b2875..0e0a1fc9 100644 --- a/source/core/texturecube.cpp +++ b/source/core/texturecube.cpp @@ -49,6 +49,14 @@ void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv) allocate(); } +void TextureCube::image(unsigned level, const void *data) +{ + const char *pixels = static_cast(data); + unsigned face_size = size*size*get_pixel_size(storage_fmt); + for(unsigned i=0; i<6; ++i) + image(static_cast(i), level, pixels+i*face_size); +} + void TextureCube::image(TextureCubeFace face, unsigned level, const void *data) { unsigned lsz = get_level_size(level); @@ -90,10 +98,7 @@ void TextureCube::image(const Graphics::Image &img, unsigned lv) else if(w!=size || h!=size) throw incompatible_data("TextureCube::image"); - const char *pixels = reinterpret_cast(img.get_pixels()); - unsigned face_size = img.get_stride()*size; - for(unsigned i=0; i<6; ++i) - image(static_cast(i), 0, pixels+i*face_size); + image(0, img.get_pixels()); } unsigned TextureCube::get_n_levels() const diff --git a/source/core/texturecube.h b/source/core/texturecube.h index 0f753c75..eef377e3 100644 --- a/source/core/texturecube.h +++ b/source/core/texturecube.h @@ -60,6 +60,8 @@ public: cannot be changed once set. */ void storage(PixelFormat, unsigned size, unsigned lv = 0); + virtual void image(unsigned, const void *); + /** Replaces contents of a single face. Allocated storage must exist. The image data is interpreted according to the storage format and must have size matching the selected mipmap level. */ -- 2.45.2