]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texturecube.cpp
Require texture data to be uploaded in a format matching the storage
[libs/gl.git] / source / texturecube.cpp
index b0887ca85b1e4fca0e88769da87e4621f0d3af54..824ff2cc14d29743bf3e8adadd4709936a5f712d 100644 (file)
@@ -58,7 +58,7 @@ void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
        if(sz==0)
                throw invalid_argument("TextureCube::storage");
 
-       set_internal_format(fmt);
+       set_format(fmt);
        size = sz;
        levels = get_n_levels();
        if(lv>0)
@@ -77,20 +77,18 @@ void TextureCube::allocate(unsigned level)
        if(ARB_texture_storage)
        {
                BindRestore _bind(this);
-               glTexStorage2D(target, levels, ifmt, size, size);
+               glTexStorage2D(target, levels, storage_fmt, size, size);
                apply_swizzle();
                allocated |= (1<<levels)-1;
        }
        else
        {
-               PixelComponents comp = get_components(ifmt);
-               DataType type = get_component_type(ifmt);
                for(unsigned i=0; i<6; ++i)
-                       image(enumerate_faces(i), level, comp, type, 0);
+                       image(enumerate_faces(i), level, 0);
        }
 }
 
-void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
+void TextureCube::image(TextureCubeFace face, unsigned level, const void *data)
 {
        if(size==0)
                throw invalid_operation("TextureCube::image");
@@ -100,7 +98,7 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents co
                throw out_of_range("TextureCube::image");
 
        if(ARB_texture_storage)
-               return sub_image(face, level, 0, 0, s, s, comp, type, data);
+               return sub_image(face, level, 0, 0, s, s, data);
 
        BindRestore _bind(this);
 
@@ -109,7 +107,10 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents co
                glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
                apply_swizzle();
        }
-       glTexImage2D(face, level, ifmt, s, s, 0, get_upload_components(comp), type, data);
+
+       PixelComponents comp = get_components(storage_fmt);
+       DataType type = get_component_type(storage_fmt);
+       glTexImage2D(face, level, storage_fmt, s, s, 0, comp, type, data);
 
        if(level==0)
        {
@@ -128,13 +129,20 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents co
        {
                for(unsigned i=0; i<6; ++i)
                        if(enumerate_faces(i)!=face)
-                               glTexImage2D(enumerate_faces(i), level, ifmt, s, s, 0, comp, type, 0);
+                               glTexImage2D(enumerate_faces(i), level, storage_fmt, s, s, 0, comp, type, 0);
 
                allocated |= 64<<level;
        }
 }
 
-void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelComponents comp, DataType type, const void *data)
+void TextureCube::image(TextureCubeFace face, unsigned level, PixelComponents comp, DataType type, const void *data)
+{
+       if(comp!=get_components(format) || type!=get_component_type(format))
+               throw incompatible_data("TextureCube::image");
+       image(face, level, data);
+}
+
+void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, const void *data)
 {
        if(size==0)
                throw invalid_operation("TextureCube::sub_image");
@@ -142,12 +150,21 @@ void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y,
        BindRestore _bind(this);
        allocate(level);
 
-       glTexSubImage2D(face, level, x, y, wd, ht, get_upload_components(comp), type, data);
+       PixelComponents comp = get_components(storage_fmt);
+       DataType type = get_component_type(storage_fmt);
+       glTexSubImage2D(face, level, x, y, wd, ht, comp, type, data);
 
        if(auto_gen_mipmap && level==0)
                generate_mipmap();
 }
 
+void TextureCube::sub_image(TextureCubeFace face, 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("TextureCube::subimage");
+       sub_image(face, level, x, y, wd, ht, data);
+}
+
 void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
 {
        unsigned w = img.get_width();
@@ -166,7 +183,7 @@ void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool s
        PixelStore pstore = PixelStore::from_image(img);
        BindRestore _bind_ps(pstore);
 
-       image(face, 0, get_components(fmt), get_component_type(fmt), img.get_data());
+       image(face, 0, img.get_data());
 }
 
 void TextureCube::image(const Graphics::Image &img, unsigned lv, bool srgb)
@@ -179,10 +196,8 @@ void TextureCube::image(const Graphics::Image &img, unsigned lv, bool srgb)
        h /= 6;
 
        PixelFormat fmt = pixelformat_from_image(img);
-       PixelComponents comp = get_components(fmt);
-       DataType type = get_component_type(fmt);
        if(size==0)
-               storage(make_pixelformat(comp, type, srgb), w, lv);
+               storage(make_pixelformat(get_components(fmt), get_component_type(fmt), srgb), w, lv);
        else if(w!=size || h!=size)
                throw incompatible_data("TextureCube::image");
 
@@ -192,7 +207,7 @@ void TextureCube::image(const Graphics::Image &img, unsigned lv, bool srgb)
        const char *cdata = reinterpret_cast<const char *>(img.get_data());
        unsigned face_size = img.get_stride()*size;
        for(unsigned i=0; i<6; ++i)
-               image(enumerate_faces(i), 0, comp, type, cdata+i*face_size);
+               image(enumerate_faces(i), 0, cdata+i*face_size);
 }
 
 unsigned TextureCube::get_n_levels() const
@@ -255,7 +270,7 @@ Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsig
 
 UInt64 TextureCube::get_data_size() const
 {
-       return id ? size*size*6*get_pixel_size(ifmt) : 0;
+       return id ? size*size*6*get_pixel_size(storage_fmt) : 0;
 }
 
 
@@ -300,7 +315,7 @@ void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
 
 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
 {
-       obj.image(face, 0, get_components(obj.ifmt), get_component_type(obj.ifmt), data.data());
+       obj.image(face, 0, data.data());
 }
 
 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)