]> git.tdb.fi Git - libs/gl.git/commitdiff
Add some utility functions for textures
authorMikko Rasa <tdb@tdb.fi>
Thu, 27 Oct 2016 22:49:59 +0000 (01:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 27 Oct 2016 22:49:59 +0000 (01:49 +0300)
source/resources.cpp
source/texture.cpp
source/texture.h
source/texture1d.cpp
source/texture1d.h
source/texture2d.cpp
source/texture2d.h
source/texture3d.cpp
source/texture3d.h
source/texturecube.cpp
source/texturecube.h

index 32709e30e2ebf521bd6bcf69eeb262069f517e4f..68b8fd5bdded8aa12489d7bda38c42a301e4c0f0 100644 (file)
@@ -88,8 +88,7 @@ Texture2D *Resources::create_texture2d(const string &name)
 
                RefPtr<GL::Texture2D> tex = new GL::Texture2D(resource_manager);
 
-               if(default_tex_filter==NEAREST_MIPMAP_NEAREST || default_tex_filter==NEAREST_MIPMAP_LINEAR ||
-                       default_tex_filter==LINEAR_MIPMAP_NEAREST || default_tex_filter==LINEAR_MIPMAP_LINEAR)
+               if(is_mipmapped(default_tex_filter))
                {
                        tex->set_generate_mipmap(true);
                        tex->set_mag_filter(LINEAR);
index fd14458553417d2b5d2b0dc71d036e2434f9efbf..b528ca30f5b6ccaee0ac81c0e78e86226f345a1a 100644 (file)
@@ -364,5 +364,12 @@ void Texture::Loader::wrap_t(TextureWrap w)
        obj.set_wrap_t(w);
 }
 
+
+bool is_mipmapped(TextureFilter filter)
+{
+       return (filter==NEAREST_MIPMAP_NEAREST || filter==NEAREST_MIPMAP_LINEAR ||
+               filter==LINEAR_MIPMAP_NEAREST || filter==LINEAR_MIPMAP_LINEAR);
+}
+
 } // namespace GL
 } // namespace Msp
index 44e126570b741a28e4cdd332523bf3d2b7436c0e..23f3bb2539ab5f9e9c544ccb00b73cc601e48f35 100644 (file)
@@ -188,6 +188,9 @@ public:
        virtual UInt64 get_data_size() const { return 0; }
 };
 
+
+bool is_mipmapped(TextureFilter);
+
 } // namespace GL
 } // namespace Msp
 
index 78c9ed0c54209a067c51516126b797289ddb82d4..0dc85586cbc2e76c328077ca9b666b96c16873e9 100644 (file)
@@ -55,8 +55,7 @@ void Texture1D::image(unsigned level, PixelFormat fmt, DataType type, const void
        if(gen_mipmap && level==0)
        {
                auto_generate_mipmap();
-               for(; w; w>>=1, ++level) ;
-               allocated |= (1<<level)-1;
+               allocated |= (1<<get_n_levels())-1;
        }
 }
 
@@ -86,7 +85,14 @@ void Texture1D::image(const Graphics::Image &img, bool srgb)
        image(0, fmt, UNSIGNED_BYTE, img.get_data());
 }
 
-unsigned Texture1D::get_level_size(unsigned level)
+unsigned Texture1D::get_n_levels() const
+{
+       unsigned n = 0;
+       for(unsigned s=width; s; s>>=1, ++n) ;
+       return n;
+}
+
+unsigned Texture1D::get_level_size(unsigned level) const
 {
        return width>>level;
 }
index 0ab9c6f74734974e2716a7e8843b1e535e40a7d3..d75167b1f3215b2402c25c0825f47fa8886c2ed3 100644 (file)
@@ -37,7 +37,8 @@ public:
        unsigned get_width() const { return width; }
 
 private:
-       unsigned get_level_size(unsigned);
+       unsigned get_n_levels() const;
+       unsigned get_level_size(unsigned) const;
 
 public:
        virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
index 4d3dac037291137856d28a77926e9517ac9475de..95f9f790507292797573931f9c241b6ae877be6e 100644 (file)
@@ -85,8 +85,7 @@ void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void
        if(gen_mipmap && level==0)
        {
                auto_generate_mipmap();
-               for(; (w || h); w>>=1, h>>=1, ++level) ;
-               allocated |= (1<<level)-1;
+               allocated |= (1<<get_n_levels())-1;
        }
 }
 
@@ -122,7 +121,14 @@ void Texture2D::image(const Graphics::Image &img, bool srgb, bool from_buffer)
        image(0, fmt, UNSIGNED_BYTE, from_buffer ? 0 : img.get_data());
 }
 
-void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
+unsigned Texture2D::get_n_levels() const
+{
+       unsigned n = 0;
+       for(unsigned s=max(width, height); s; s>>=1, ++n) ;
+       return n;
+}
+
+void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
 {
        w >>= level;
        h >>= level;
index 221fbd7b76a28aee38d95382a2b797e34fd5d24c..9647ad3ed55c688a86c156dd11e3daacba687b9d 100644 (file)
@@ -75,7 +75,8 @@ public:
        unsigned get_height() const { return height; }
 
 private:
-       void get_level_size(unsigned, unsigned &, unsigned &);
+       unsigned get_n_levels() const;
+       void get_level_size(unsigned, unsigned &, unsigned &) const;
 
 public:
        virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
index 5c9f009c0d5eb6d798a06b9a64fd783341430e38..12fc856b8415d4e9cc18ac95c2a9c548a79cf79f 100644 (file)
@@ -74,8 +74,7 @@ void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void
        if(gen_mipmap && level==0)
        {
                auto_generate_mipmap();
-               for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
-               allocated |= (1<<level)-1;
+               allocated |= (1<<get_n_levels())-1;
        }
 }
 
@@ -169,7 +168,17 @@ void Texture3D::image(const Graphics::Image &img, bool srgb)
        image(0, fmt, UNSIGNED_BYTE, img.get_data());
 }
 
-void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
+unsigned Texture3D::get_n_levels() const
+{
+       unsigned s = max(width, height);
+       if(target!=GL_TEXTURE_2D_ARRAY)
+               s = max(s, depth);
+       unsigned n = 0;
+       for(; s; s>>=1, ++n) ;
+       return n;
+}
+
+void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d) const
 {
        w >>= level;
        h >>= level;
index 55a0e3dbe8ec4ce214bdc56f5532655160965b58..57f3a6f8589e9d1ea9c986785b477524880b0a76 100644 (file)
@@ -85,7 +85,8 @@ public:
        unsigned get_height() const { return height; }
        unsigned get_depth() const { return depth; }
 protected:
-       void get_level_size(unsigned, unsigned &, unsigned &, unsigned &);
+       unsigned get_n_levels() const;
+       void get_level_size(unsigned, unsigned &, unsigned &, unsigned &) const;
 
 public:
        virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
index 26402695880dc12eba847ccb443fa8cb90fbb02c..65b1845eaf9ca5f1bb82f9886ada4ed0eed46472 100644 (file)
@@ -75,8 +75,7 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, D
        {
                // TODO Only do this once all faces are created
                auto_generate_mipmap();
-               for(; s; s>>=1, ++level) ;
-               allocated |= (1<<level)-1;
+               allocated |= (1<<get_n_levels())-1;
        }
 }
 
@@ -136,7 +135,14 @@ void TextureCube::image(const Graphics::Image &img, bool srgb)
                image(enumerate_faces(i), 0, fmt, UNSIGNED_BYTE, cdata+i*face_size);
 }
 
-unsigned TextureCube::get_level_size(unsigned level)
+unsigned TextureCube::get_n_levels() const
+{
+       unsigned n = 0;
+       for(unsigned s=size; s; s>>=1, ++n) ;
+       return n;
+}
+
+unsigned TextureCube::get_level_size(unsigned level) const
 {
        return size>>level;
 }
index fd4670469fd1ab0500b607a35e723fe7e9205b65..62f1189aa541426ff86d0cdcb5593604155a2dea 100644 (file)
@@ -85,7 +85,8 @@ public:
 
        unsigned get_size() const { return size; }
 private:
-       unsigned get_level_size(unsigned);
+       unsigned get_n_levels() const;
+       unsigned get_level_size(unsigned) const;
 
 public:
        /** Translates indices into face constants.  Valid indices are between 0