]> git.tdb.fi Git - libs/gl.git/commitdiff
Move Texture2D::AsyncLoader back to the common part
authorMikko Rasa <tdb@tdb.fi>
Tue, 28 Dec 2021 14:09:59 +0000 (16:09 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 28 Dec 2021 14:09:59 +0000 (16:09 +0200)
The backend-specific parts are now handled by AsyncTransfer.

source/backends/opengl/texture2d_backend.cpp
source/backends/opengl/texture2d_backend.h
source/backends/vulkan/texture2d_backend.cpp
source/backends/vulkan/texture2d_backend.h
source/core/texture2d.cpp
source/core/texture2d.h

index 187d16934990668fb40132463ebded6cd0f387e0..a4e3cad64c737d78a840b15930542e7cf7e6838f 100644 (file)
@@ -1,8 +1,6 @@
-#include <msp/datafile/rawdata.h>
 #include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/arb_texture_storage.h>
 #include <msp/gl/extensions/arb_vertex_buffer_object.h>
-#include <msp/graphics/imageloader.h>
 #include "buffer.h"
 #include "gl.h"
 #include "texture2d.h"
@@ -13,27 +11,6 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-class OpenGLTexture2D::AsyncLoader: public Resource::AsyncLoader
-{
-private:
-       Texture2D &texture;
-       IO::Seekable &io;
-       Texture2D::AsyncTransfer transfer;
-       Graphics::Image image;
-       Graphics::ImageLoader *img_loader = 0;
-       DataFile::RawData *raw_data = 0;
-       unsigned n_bytes = 0;
-       int phase = 0;
-
-public:
-       AsyncLoader(Texture2D &, IO::Seekable &);
-       ~AsyncLoader();
-
-       virtual bool needs_sync() const;
-       virtual bool process();
-};
-
-
 OpenGLTexture2D::OpenGLTexture2D():
        Texture(GL_TEXTURE_2D)
 { }
@@ -85,11 +62,6 @@ void OpenGLTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsig
        }
 }
 
-Resource::AsyncLoader *OpenGLTexture2D::load(IO::Seekable &io, const Resources *)
-{
-       return new AsyncLoader(static_cast<Texture2D &>(*this), io);
-}
-
 uint64_t OpenGLTexture2D::get_data_size() const
 {
        if(!id)
@@ -151,76 +123,5 @@ void OpenGLTexture2D::AsyncTransfer::finalize()
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
 }
 
-
-OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
-       texture(t),
-       io(i)
-{
-       char magic[4] = { };
-       io.read(magic, 4);
-       io.seek(0, IO::S_BEG);
-
-       if(DataFile::RawData::detect_signature(string(magic, 4)))
-       {
-               raw_data = new DataFile::RawData;
-               raw_data->open_io(io, "async");
-       }
-       else
-               img_loader = Graphics::ImageLoader::open_io(io);
-}
-
-OpenGLTexture2D::AsyncLoader::~AsyncLoader()
-{
-       delete img_loader;
-       delete raw_data;
-}
-
-bool OpenGLTexture2D::AsyncLoader::needs_sync() const
-{
-       return phase%2;
-}
-
-bool OpenGLTexture2D::AsyncLoader::process()
-{
-       if(phase==0)
-       {
-               if(raw_data)
-                       n_bytes = raw_data->get_size();
-               else
-               {
-                       image.load_headers(*img_loader);
-                       n_bytes = image.get_stride()*image.get_height();
-               }
-       }
-       else if(phase==1)
-       {
-               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(transfer.get_address());
-               else
-                       image.load_into(*img_loader, transfer.get_address());
-       }
-       else if(phase==3)
-       {
-               transfer = Texture2D::AsyncTransfer();
-
-               if(texture.auto_gen_mipmap)
-                       texture.generate_mipmap();
-       }
-
-       ++phase;
-       return phase>3;
-}
-
 } // namespace GL
 } // namespace Msp
index f9b5abfede4560ab55ca4e77a54e68b03e59296f..9d469f2be5950fe6149dea23081738fcf62e1828 100644 (file)
@@ -25,15 +25,12 @@ protected:
                void finalize();
        };
 
-       class AsyncLoader;
-
        OpenGLTexture2D();
 
        void allocate();
        void sub_image(unsigned, int, int, unsigned, unsigned, const void *);
 
 public:
-       virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
        virtual std::size_t get_data_size() const;
        virtual void unload();
 };
index 7c6b55f1ee94dfb0652ef76ea8adb7e75675fbbf..1ab49b888a63a5eeeebb7cc626ebc21d9a243050 100644 (file)
@@ -40,11 +40,6 @@ void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b)
        blit.dstOffsets[1] = { static_cast<int>(dst_size.x), static_cast<int>(dst_size.y), 1 };
 }
 
-Resource::AsyncLoader *VulkanTexture2D::load(IO::Seekable &, const Resources *)
-{
-       throw logic_error("Texture2D::load is unimplemented");
-}
-
 uint64_t VulkanTexture2D::get_data_size() const
 {
        return 0;
index 7686a9e0e548645c1e9b0bd0fa42ba7c11be4b86..139f8bef417bf03509425127f11668c3f0bbd8f9 100644 (file)
@@ -27,7 +27,6 @@ protected:
        virtual void fill_mipmap_blit(unsigned, void *);
 
 public:
-       virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
        virtual std::size_t get_data_size() const;
        virtual void unload();
 };
index 74071535e73f41e768f053cd5041479fe80353c3..a0ba9515e68babe3387b9e2af1278087b1d6d3f9 100644 (file)
@@ -1,3 +1,5 @@
+#include <msp/datafile/rawdata.h>
+#include <msp/graphics/imageloader.h>
 #include "error.h"
 #include "texture2d.h"
 
@@ -6,6 +8,27 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+class Texture2D::AsyncLoader: public Resource::AsyncLoader
+{
+private:
+       Texture2D &texture;
+       IO::Seekable &io;
+       Texture2D::AsyncTransfer transfer;
+       Graphics::Image image;
+       Graphics::ImageLoader *img_loader = 0;
+       DataFile::RawData *raw_data = 0;
+       unsigned n_bytes = 0;
+       int phase = 0;
+
+public:
+       AsyncLoader(Texture2D &, IO::Seekable &);
+       ~AsyncLoader();
+
+       virtual bool needs_sync() const;
+       virtual bool process();
+};
+
+
 Texture2D::~Texture2D()
 {
        set_manager(0);
@@ -72,6 +95,11 @@ LinAl::Vector<unsigned, 2> Texture2D::get_level_size(unsigned level) const
        return LinAl::Vector<unsigned, 2>(w, h);
 }
 
+Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
+{
+       return new AsyncLoader(static_cast<Texture2D &>(*this), io);
+}
+
 
 Texture2D::Loader::Loader(Texture2D &t):
        DataFile::DerivedObjectLoader<Texture2D, Texture::Loader>(t)
@@ -156,5 +184,76 @@ Texture2D::AsyncTransfer::~AsyncTransfer()
                finalize();
 }
 
+
+Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
+       texture(t),
+       io(i)
+{
+       char magic[4] = { };
+       io.read(magic, 4);
+       io.seek(0, IO::S_BEG);
+
+       if(DataFile::RawData::detect_signature(string(magic, 4)))
+       {
+               raw_data = new DataFile::RawData;
+               raw_data->open_io(io, "async");
+       }
+       else
+               img_loader = Graphics::ImageLoader::open_io(io);
+}
+
+Texture2D::AsyncLoader::~AsyncLoader()
+{
+       delete img_loader;
+       delete raw_data;
+}
+
+bool Texture2D::AsyncLoader::needs_sync() const
+{
+       return phase%2;
+}
+
+bool Texture2D::AsyncLoader::process()
+{
+       if(phase==0)
+       {
+               if(raw_data)
+                       n_bytes = raw_data->get_size();
+               else
+               {
+                       image.load_headers(*img_loader);
+                       n_bytes = image.get_stride()*image.get_height();
+               }
+       }
+       else if(phase==1)
+       {
+               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(transfer.get_address());
+               else
+                       image.load_into(*img_loader, transfer.get_address());
+       }
+       else if(phase==3)
+       {
+               transfer = Texture2D::AsyncTransfer();
+
+               if(texture.auto_gen_mipmap)
+                       texture.generate_mipmap();
+       }
+
+       ++phase;
+       return phase>3;
+}
+
 } // namespace GL
 } // namespace Msp
index 02cdade57e30e1b19cc771b2a0df4030d3b0a3fe..b13b8b3ca3bd8c274c04056d00999bbd26707b4e 100644 (file)
@@ -61,6 +61,8 @@ public:
        };
 
 private:
+       class AsyncLoader;
+
        unsigned width = 0;
        unsigned height = 0;
 
@@ -91,6 +93,9 @@ public:
 private:
        unsigned get_n_levels() const;
        LinAl::Vector<unsigned, 2> get_level_size(unsigned) const;
+
+public:
+       virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
 };
 
 } // namespace GL