]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texturecube.cpp
Be less eager to optimize constant conditions in loops
[libs/gl.git] / source / texturecube.cpp
index d5453c182e128b13e201cb87d7b738e38caefd00..03e65167d73fee7a1d89ba9ea3cea7ef2c68d81d 100644 (file)
@@ -1,4 +1,6 @@
+#include <msp/datafile/collection.h>
 #include <msp/gl/extensions/arb_texture_cube_map.h>
+#include <msp/gl/extensions/arb_texture_storage.h>
 #include <msp/io/memory.h>
 #include <msp/strings/format.h>
 #include "bindable.h"
@@ -11,6 +13,16 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+TextureCubeFace TextureCube::face_order[6] =
+{
+       POSITIVE_X,
+       NEGATIVE_X,
+       POSITIVE_Y,
+       NEGATIVE_Y,
+       POSITIVE_Z,
+       NEGATIVE_Z
+};
+
 Vector3 TextureCube::directions[6] =
 {
        Vector3(1, 0, 0),
@@ -21,6 +33,16 @@ Vector3 TextureCube::directions[6] =
        Vector3(0, 0, -1)
 };
 
+unsigned TextureCube::orientations[12] =
+{
+       5, 3,
+       4, 3,
+       0, 4,
+       0, 5,
+       0, 3,
+       1, 3
+};
+
 TextureCube::TextureCube():
        Texture(GL_TEXTURE_CUBE_MAP),
        size(0),
@@ -29,25 +51,42 @@ TextureCube::TextureCube():
        static Require _req(ARB_texture_cube_map);
 }
 
-void TextureCube::storage(PixelFormat fmt, unsigned sz)
+void TextureCube::storage(PixelFormat fmt, unsigned sz, unsigned lv)
 {
        if(size>0)
                throw invalid_operation("TextureCube::storage");
        if(sz==0)
                throw invalid_argument("TextureCube::storage");
-       require_pixelformat(fmt);
 
-       ifmt = fmt;
+       set_internal_format(fmt);
        size = sz;
+       levels = get_n_levels();
+       if(lv>0)
+               levels = min(levels, lv);
 }
 
 void TextureCube::allocate(unsigned level)
 {
+       if(size==0)
+               throw invalid_operation("TextureCube::allocate");
+       if(level>=levels)
+               throw invalid_argument("TextureCube::allocate");
        if(allocated&(1<<level))
                return;
 
-       for(unsigned i=0; i<6; ++i)
-               image(enumerate_faces(i), level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
+       if(ARB_texture_storage)
+       {
+               BindRestore _bind(this);
+               glTexStorage2D(target, levels, ifmt, size, size);
+               allocated |= (1<<levels)-1;
+       }
+       else
+       {
+               PixelFormat base_fmt = get_base_pixelformat(ifmt);
+               DataType type = get_alloc_type(base_fmt);
+               for(unsigned i=0; i<6; ++i)
+                       image(enumerate_faces(i), level, base_fmt, type, 0);
+       }
 }
 
 void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, DataType type, const void *data)
@@ -59,29 +98,47 @@ void TextureCube::image(TextureCubeFace face, unsigned level, PixelFormat fmt, D
        if(s==0)
                throw out_of_range("TextureCube::image");
 
+       if(ARB_texture_storage)
+               return sub_image(face, level, 0, 0, s, s, fmt, type, data);
+
        BindRestore _bind(this);
-       glTexImage2D(face, level, ifmt, s, s, 0, fmt, type, data);
+       glTexImage2D(face, level, ifmt, s, s, 0, get_upload_format(fmt), type, data);
 
        // XXX Allocation should be tracked per-face, but we'll run out of bits
        allocated |= 1<<level;
        if(gen_mipmap && level==0)
        {
-               for(; s; s>>=1, ++level) ;
-               allocated |= (1<<level)-1;
+               // TODO Only do this once all faces are created
+               auto_generate_mipmap();
+               allocated |= (1<<get_n_levels())-1;
        }
 }
 
-void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
+void TextureCube::sub_image(TextureCubeFace face, unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
+{
+       if(size==0)
+               throw invalid_operation("TextureCube::sub_image");
+
+       BindRestore _bind(this);
+       allocate(level);
+
+       glTexSubImage2D(face, level, x, y, wd, ht, get_upload_format(fmt), type, data);
+
+       if(gen_mipmap && level==0)
+               auto_generate_mipmap();
+}
+
+void TextureCube::image(TextureCubeFace face, const Graphics::Image &img, bool srgb)
 {
        unsigned w = img.get_width();
        unsigned h = img.get_height();
        PixelFormat fmt = pixelformat_from_graphics(img.get_format());
        if(size==0)
        {
-               if(w==h)
-                       storage(storage_pixelformat_from_graphics(img.get_format()), w);
-               else
+               if(w!=h)
                        throw incompatible_data("TextureCube::image");
+
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w);
        }
        else if(w!=size || h!=size)
                throw incompatible_data("TextureCube::image");
@@ -92,65 +149,79 @@ void TextureCube::image(TextureCubeFace face, const Graphics::Image &img)
        image(face, 0, fmt, UNSIGNED_BYTE, img.get_data());
 }
 
-unsigned TextureCube::get_level_size(unsigned level)
+void TextureCube::image(const Graphics::Image &img, bool srgb)
+{
+       unsigned w = img.get_width();
+       unsigned h = img.get_height();
+
+       if(h!=w*6)
+               throw incompatible_data("TextureCube::image");
+       h /= 6;
+
+       PixelFormat fmt = pixelformat_from_graphics(img.get_format());
+       if(size==0)
+       {
+               unsigned l = (is_mipmapped(min_filter) ? mipmap_levels ? mipmap_levels : 0 : 1);
+               storage(storage_pixelformat_from_graphics(img.get_format(), srgb), w, l);
+       }
+       else if(w!=size || h!=size)
+               throw incompatible_data("TextureCube::image");
+
+       PixelStore pstore = PixelStore::from_image(img);
+       BindRestore _bind_ps(pstore);
+
+       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, fmt, UNSIGNED_BYTE, cdata+i*face_size);
+}
+
+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;
 }
 
 TextureCubeFace TextureCube::enumerate_faces(unsigned i)
 {
-       switch(i)
-       {
-       case 0: return POSITIVE_X;
-       case 1: return NEGATIVE_X;
-       case 2: return POSITIVE_Y;
-       case 3: return NEGATIVE_Y;
-       case 4: return POSITIVE_Z;
-       case 5: return NEGATIVE_Z;
-       default: throw out_of_range("TextureCube::enumerate_faces");
-       }
+       if(i>=6)
+               throw out_of_range("TextureCube::enumerate_faces");
+       return face_order[i];
 }
 
-const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
+unsigned TextureCube::get_face_index(TextureCubeFace face)
 {
        switch(face)
        {
-       case POSITIVE_X: return directions[0];
-       case NEGATIVE_X: return directions[1];
-       case POSITIVE_Y: return directions[2];
-       case NEGATIVE_Y: return directions[3];
-       case POSITIVE_Z: return directions[4];
-       case NEGATIVE_Z: return directions[5];
-       default: throw invalid_argument("TextureCube::get_face_direction");
+       case POSITIVE_X: return 0;
+       case NEGATIVE_X: return 1;
+       case POSITIVE_Y: return 2;
+       case NEGATIVE_Y: return 3;
+       case POSITIVE_Z: return 4;
+       case NEGATIVE_Z: return 5;
+       default: throw invalid_argument("TextureCube::get_face_index");
        }
 }
 
+const Vector3 &TextureCube::get_face_direction(TextureCubeFace face)
+{
+       return directions[get_face_index(face)];
+}
+
 const Vector3 &TextureCube::get_s_direction(TextureCubeFace face)
 {
-       switch(face)
-       {
-       case POSITIVE_X: return directions[5];
-       case NEGATIVE_X: return directions[4];
-       case POSITIVE_Y: return directions[0];
-       case NEGATIVE_Y: return directions[0];
-       case POSITIVE_Z: return directions[0];
-       case NEGATIVE_Z: return directions[1];
-       default: throw invalid_argument("TextureCube::get_s_direction");
-       }
+       return directions[orientations[get_face_index(face)*2]];
 }
 
 const Vector3 &TextureCube::get_t_direction(TextureCubeFace face)
 {
-       switch(face)
-       {
-       case POSITIVE_X: return directions[3];
-       case NEGATIVE_X: return directions[3];
-       case POSITIVE_Y: return directions[4];
-       case NEGATIVE_Y: return directions[5];
-       case POSITIVE_Z: return directions[3];
-       case NEGATIVE_Z: return directions[3];
-       default: throw invalid_argument("TextureCube::get_t_direction");
-       }
+       return directions[orientations[get_face_index(face)*2+1]];
 }
 
 Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsigned v)
@@ -163,22 +234,48 @@ Vector3 TextureCube::get_texel_direction(TextureCubeFace face, unsigned u, unsig
        return fv+s*sv+t*tv;
 }
 
+UInt64 TextureCube::get_data_size() const
+{
+       return id ? size*size*6*get_pixel_size(ifmt) : 0;
+}
+
 
 TextureCube::Loader::Loader(TextureCube &t):
        DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t)
 {
+       init();
+}
+
+TextureCube::Loader::Loader(TextureCube &t, Collection &c):
+       DataFile::DerivedObjectLoader<TextureCube, Texture::Loader>(t, c)
+{
+       init();
+}
+
+void TextureCube::Loader::init()
+{
+       add("external_image", &Loader::external_image);
        add("image_data", &Loader::image_data);
        add("raw_data", &Loader::raw_data);
        add("storage", &Loader::storage);
 }
 
+void TextureCube::Loader::external_image(TextureCubeFace face, const string &fn)
+{
+       Graphics::Image img;
+       RefPtr<IO::Seekable> io = get_collection().open_raw(fn);
+       img.load_io(*io);
+
+       obj.image(face, img, srgb);
+}
+
 void TextureCube::Loader::image_data(TextureCubeFace face, const string &data)
 {
        Graphics::Image img;
        IO::Memory mem(data.data(), data.size());
        img.load_io(mem);
 
-       obj.image(face, img);
+       obj.image(face, img, srgb);
 }
 
 void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
@@ -188,6 +285,8 @@ void TextureCube::Loader::raw_data(TextureCubeFace face, const string &data)
 
 void TextureCube::Loader::storage(PixelFormat fmt, unsigned s)
 {
+       if(srgb)
+               fmt = get_srgb_pixelformat(fmt);
        obj.storage(fmt, s);
 }