]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture2d.cpp
Async load texture data directly into the pixel buffer
[libs/gl.git] / source / texture2d.cpp
index 311a3e4569704c92b99703c4a0473f508778c9e1..0e76128c7bc5666da5828f11fe98f5398ecaf380 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/core/raii.h>
 #include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/arb_texture_storage.h>
+#include <msp/graphics/imageloader.h>
 #include "bindable.h"
 #include "buffer.h"
 #include "error.h"
@@ -21,11 +22,13 @@ private:
        Buffer pixel_buffer;
        char *mapped_address;
        Graphics::Image image;
+       Graphics::ImageLoader *img_loader;
        unsigned n_bytes;
        int phase;
 
 public:
        AsyncLoader(Texture2D &, IO::Seekable &);
+       ~AsyncLoader();
 
        virtual bool needs_sync() const;
        virtual bool process();
@@ -246,9 +249,17 @@ Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
        io(i),
        pixel_buffer(PIXEL_UNPACK_BUFFER),
        mapped_address(0),
+       img_loader(Graphics::ImageLoader::open_io(io)),
        phase(0)
 { }
 
+Texture2D::AsyncLoader::~AsyncLoader()
+{
+       if(mapped_address)
+               pixel_buffer.unmap();
+       delete img_loader;
+}
+
 bool Texture2D::AsyncLoader::needs_sync() const
 {
        return phase%2;
@@ -258,9 +269,7 @@ bool Texture2D::AsyncLoader::process()
 {
        if(phase==0)
        {
-               /* TODO Enhance the ImageLoader system so that the image can be loaded
-               directly to the buffer */
-               image.load_io(io);
+               image.load_headers(*img_loader);
                n_bytes = image.get_stride()*image.get_height();
        }
        else if(phase==1)
@@ -269,13 +278,11 @@ bool Texture2D::AsyncLoader::process()
                mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
        }
        else if(phase==2)
-       {
-               const char *data = reinterpret_cast<const char *>(image.get_pixels());
-               copy(data, data+n_bytes, mapped_address);
-       }
+               image.load_into(*img_loader, mapped_address);
        else if(phase==3)
        {
                Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
+               mapped_address = 0;
                if(!pixel_buffer.unmap())
                {
                        phase = 1;