]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture2d.cpp
Require texture data to be uploaded in a format matching the storage
[libs/gl.git] / source / texture2d.cpp
index 8846a555d6437f6554c46e316a6f40be0b1c9728..fa92f1c97e52b3c980485827280def8e1c0057c2 100644 (file)
@@ -53,7 +53,7 @@ void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, unsigned lv)
        if(wd==0 || ht==0)
                throw invalid_argument("Texture2D::storage");
 
-       set_internal_format(fmt);
+       set_format(fmt);
        width = wd;
        height = ht;
        levels = get_n_levels();
@@ -74,17 +74,17 @@ void Texture2D::allocate(unsigned level)
        {
                Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
                if(ARB_direct_state_access)
-                       glTextureStorage2D(id, levels, ifmt, width, height);
+                       glTextureStorage2D(id, levels, storage_fmt, width, height);
                else
-                       glTexStorage2D(target, levels, ifmt, width, height);
+                       glTexStorage2D(target, levels, storage_fmt, width, height);
                apply_swizzle();
                allocated |= (1<<levels)-1;
        }
        else
-               image(level, get_components(ifmt), get_component_type(ifmt), 0);
+               image(level, 0);
 }
 
-void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
+void Texture2D::image(unsigned level, const void *data)
 {
        if(width==0 || height==0)
                throw invalid_operation("Texture2D::image");
@@ -94,7 +94,7 @@ void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const
        get_level_size(level, w, h);
 
        if(ARB_texture_storage)
-               return sub_image(level, 0, 0, w, h, comp, type, data);
+               return sub_image(level, 0, 0, w, h, data);
 
        BindRestore _bind(this);
 
@@ -103,7 +103,10 @@ void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const
                glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
                apply_swizzle();
        }
-       glTexImage2D(target, level, ifmt, w, h, 0, get_upload_components(comp), type, data);
+
+       PixelComponents comp = get_components(storage_fmt);
+       DataType type = get_component_type(storage_fmt);
+       glTexImage2D(target, level, storage_fmt, w, h, 0, comp, type, data);
 
        allocated |= 1<<level;
        if(auto_gen_mipmap && level==0)
@@ -113,7 +116,14 @@ void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const
        }
 }
 
-void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
+void Texture2D::image(unsigned level, PixelComponents comp, DataType type, const void *data)
+{
+       if(comp!=get_components(format) || type!=get_component_type(format))
+               throw incompatible_data("Texture2D::image");
+       image(level, data);
+}
+
+void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
 {
        if(width==0 || height==0)
                throw invalid_operation("Texture2D::sub_image");
@@ -121,7 +131,8 @@ void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht
        Conditional<BindRestore> _bind(!ARB_direct_state_access, this);
        allocate(level);
 
-       comp = get_upload_components(comp);
+       PixelComponents comp = get_components(storage_fmt);
+       DataType type = get_component_type(storage_fmt);
        if(ARB_direct_state_access)
                glTextureSubImage2D(id, level, x, y, wd, ht, comp, type, data);
        else
@@ -131,6 +142,13 @@ void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht
                generate_mipmap();
 }
 
+void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
+{
+       if(comp!=get_components(format) || type!=get_component_type(format))
+               throw incompatible_data("Texture2D::sub_image");
+       sub_image(level, x, y, wd, ht, data);
+}
+
 void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb)
 {
        image(img, lv, srgb, false);
@@ -141,17 +159,15 @@ void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb, bool f
        unsigned w = img.get_width();
        unsigned h = img.get_height();
        PixelFormat fmt = pixelformat_from_image(img);
-       PixelComponents comp = get_components(fmt);
-       DataType type = get_component_type(fmt);
        if(width==0)
-               storage(make_pixelformat(comp, type, srgb), w, h, lv);
+               storage(make_pixelformat(get_components(fmt), get_component_type(fmt), srgb), w, h, lv);
        else if(w!=width || h!=height || (lv && lv!=levels))
                throw incompatible_data("Texture2D::image");
 
        PixelStore pstore = PixelStore::from_image(img);
        BindRestore _bind_ps(pstore);
 
-       image(0, comp, type, from_buffer ? 0 : img.get_data());
+       image(0, from_buffer ? 0 : img.get_data());
 }
 
 unsigned Texture2D::get_n_levels() const
@@ -182,7 +198,7 @@ Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
 
 UInt64 Texture2D::get_data_size() const
 {
-       return id ? width*height*get_pixel_size(ifmt) : 0;
+       return id ? width*height*get_pixel_size(format) : 0;
 }
 
 void Texture2D::unload()
@@ -215,7 +231,7 @@ void Texture2D::Loader::init()
 
 void Texture2D::Loader::raw_data(const string &data)
 {
-       obj.image(0, get_components(obj.ifmt), get_component_type(obj.ifmt), data.data());
+       obj.image(0, data.data());
 }
 
 void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h)