]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/opengl/texture2d_backend.cpp
Refactor Texture2D::AsyncLoader to use sub_image_async
[libs/gl.git] / source / backends / opengl / texture2d_backend.cpp
index 72962117bc9967d98228735063b5189e10368dfd..187d16934990668fb40132463ebded6cd0f387e0 100644 (file)
@@ -18,8 +18,7 @@ class OpenGLTexture2D::AsyncLoader: public Resource::AsyncLoader
 private:
        Texture2D &texture;
        IO::Seekable &io;
-       Buffer pixel_buffer;
-       char *mapped_address = 0;
+       Texture2D::AsyncTransfer transfer;
        Graphics::Image image;
        Graphics::ImageLoader *img_loader = 0;
        DataFile::RawData *raw_data = 0;
@@ -41,9 +40,7 @@ OpenGLTexture2D::OpenGLTexture2D():
 
 void OpenGLTexture2D::allocate()
 {
-       unsigned width = static_cast<const Texture2D *>(this)->width;
-       unsigned height = static_cast<const Texture2D *>(this)->height;
-       unsigned levels = static_cast<const Texture2D *>(this)->levels;
+       const Texture2D &self = *static_cast<const Texture2D *>(this);
 
        if(!id)
                create();
@@ -52,22 +49,22 @@ void OpenGLTexture2D::allocate()
        if(ARB_texture_storage)
        {
                if(ARB_direct_state_access)
-                       glTextureStorage2D(id, levels, gl_fmt, width, height);
+                       glTextureStorage2D(id, n_levels, gl_fmt, self.width, self.height);
                else
                {
                        bind_scratch();
-                       glTexStorage2D(target, levels, gl_fmt, width, height);
+                       glTexStorage2D(target, n_levels, gl_fmt, self.width, self.height);
                }
        }
        else
        {
                bind_scratch();
-               glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, levels-1);
+               glTexParameteri(target, GL_TEXTURE_MAX_LEVEL, n_levels-1);
                GLenum comp = get_gl_components(get_components(storage_fmt));
                GLenum type = get_gl_type(get_component_type(storage_fmt));
-               for(unsigned i=0; i<levels; ++i)
+               for(unsigned i=0; i<n_levels; ++i)
                {
-                       auto lv_size = static_cast<const Texture2D *>(this)->get_level_size(i);
+                       auto lv_size = self.get_level_size(i);
                        glTexImage2D(target, i, gl_fmt, lv_size.x, lv_size.y, 0, comp, type, 0);
                }
        }
@@ -88,13 +85,6 @@ void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsig
        }
 }
 
-void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, const Buffer &buffer, unsigned offset)
-{
-       glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer.id);
-       sub_image(level, x, y, wd, ht, reinterpret_cast<void *>(offset));
-       glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
-}
-
 Resource::AsyncLoader *OpenGLTexture2D::load(IO::Seekable &io, const Resources *)
 {
        return new AsyncLoader(static_cast<Texture2D &>(*this), io);
@@ -102,9 +92,16 @@ Resource::AsyncLoader *OpenGLTexture2D::load(IO::Seekable &io, const Resources *
 
 uint64_t OpenGLTexture2D::get_data_size() const
 {
-       unsigned width = static_cast<const Texture2D *>(this)->width;
-       unsigned height = static_cast<const Texture2D *>(this)->height;
-       return id ? width*height*get_pixel_size(format) : 0;
+       if(!id)
+               return 0;
+
+       const Texture2D &self = *static_cast<const Texture2D *>(this);
+
+       size_t level_size = self.width*self.height*get_pixel_size(format);
+       size_t total_size = level_size;
+       for(unsigned i=0; i<n_levels; ++i, level_size>>=2)
+               total_size += level_size;
+       return total_size;
 }
 
 void OpenGLTexture2D::unload()
@@ -114,6 +111,47 @@ void OpenGLTexture2D::unload()
 }
 
 
+OpenGLTexture2D::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other):
+       pixel_buffer(other.pixel_buffer)
+{
+       other.pixel_buffer = 0;
+}
+
+OpenGLTexture2D::AsyncTransfer &OpenGLTexture2D::AsyncTransfer::operator=(AsyncTransfer &&other)
+{
+       delete pixel_buffer;
+       pixel_buffer = other.pixel_buffer;
+       other.pixel_buffer = 0;
+
+       return *this;
+}
+
+OpenGLTexture2D::AsyncTransfer::~AsyncTransfer()
+{
+       delete pixel_buffer;
+}
+
+void *OpenGLTexture2D::AsyncTransfer::allocate()
+{
+       const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
+
+       pixel_buffer = new Buffer;
+       pixel_buffer->storage(self.data_size, STREAMING);
+       return pixel_buffer->map();
+}
+
+void OpenGLTexture2D::AsyncTransfer::finalize()
+{
+       const Texture2D::AsyncTransfer &self = *static_cast<const Texture2D::AsyncTransfer *>(this);
+
+       pixel_buffer->unmap();
+
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixel_buffer->id);
+       self.texture->OpenGLTexture2D::sub_image(self.level, self.x, self.y, self.width, self.height, 0);
+       glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
+}
+
+
 OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
        texture(t),
        io(i)
@@ -133,8 +171,6 @@ OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
 
 OpenGLTexture2D::AsyncLoader::~AsyncLoader()
 {
-       if(mapped_address)
-               pixel_buffer.unmap();
        delete img_loader;
        delete raw_data;
 }
@@ -158,35 +194,25 @@ bool OpenGLTexture2D::AsyncLoader::process()
        }
        else if(phase==1)
        {
-               pixel_buffer.storage(n_bytes);
-               mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
+               if(img_loader)
+               {
+                       unsigned w = image.get_width();
+                       unsigned h = image.get_height();
+                       texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h);
+               }
+
+               transfer = texture.sub_image_async(0, 0, 0, texture.width, texture.height);
        }
        else if(phase==2)
        {
                if(raw_data)
-                       raw_data->load_into(mapped_address);
+                       raw_data->load_into(transfer.get_address());
                else
-                       image.load_into(*img_loader, mapped_address);
+                       image.load_into(*img_loader, transfer.get_address());
        }
        else if(phase==3)
        {
-               mapped_address = 0;
-               if(!pixel_buffer.unmap())
-               {
-                       phase = 1;
-                       return false;
-               }
-
-               if(!texture.id)
-                       texture.create();
-
-               if(img_loader)
-               {
-                       unsigned w = image.get_width();
-                       unsigned h = image.get_height();
-                       texture.storage(pixelformat_from_image(image, texture.use_srgb_format), w, h);
-               }
-               texture.OpenGLTexture2D::sub_image(0, 0, 0, texture.width, texture.height, pixel_buffer, 0);
+               transfer = Texture2D::AsyncTransfer();
 
                if(texture.auto_gen_mipmap)
                        texture.generate_mipmap();