]> git.tdb.fi Git - libs/gl.git/commitdiff
Support loading raw texture data from an external file
authorMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2021 10:17:24 +0000 (12:17 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 2 Nov 2021 13:11:16 +0000 (15:11 +0200)
This enables async loading of pixel formats not supported by standard
image files.

source/backends/opengl/texture2d_backend.cpp
source/core/texture.cpp
source/core/texture.h

index 9efc4351e65b47e8c6a08dda88d5cb3d344e4565..578cefe390f0a2ec2bfa4bdb15e73426fef43822 100644 (file)
@@ -1,3 +1,4 @@
+#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>
@@ -7,6 +8,8 @@
 #include "texture2d.h"
 #include "texture2d_backend.h"
 
+using namespace std;
+
 namespace Msp {
 namespace GL {
 
@@ -19,6 +22,7 @@ private:
        char *mapped_address = 0;
        Graphics::Image image;
        Graphics::ImageLoader *img_loader = 0;
+       DataFile::RawData *raw_data = 0;
        unsigned n_bytes = 0;
        int phase = 0;
 
@@ -105,15 +109,27 @@ void OpenGLTexture2D::unload()
 
 OpenGLTexture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
        texture(t),
-       io(i),
-       img_loader(Graphics::ImageLoader::open_io(io))
-{ }
+       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()
 {
        if(mapped_address)
                pixel_buffer.unmap();
        delete img_loader;
+       delete raw_data;
 }
 
 bool OpenGLTexture2D::AsyncLoader::needs_sync() const
@@ -125,8 +141,13 @@ bool OpenGLTexture2D::AsyncLoader::process()
 {
        if(phase==0)
        {
-               image.load_headers(*img_loader);
-               n_bytes = image.get_stride()*image.get_height();
+               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)
        {
@@ -134,7 +155,12 @@ bool OpenGLTexture2D::AsyncLoader::process()
                mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
        }
        else if(phase==2)
-               image.load_into(*img_loader, mapped_address);
+       {
+               if(raw_data)
+                       raw_data->load_into(mapped_address);
+               else
+                       image.load_into(*img_loader, mapped_address);
+       }
        else if(phase==3)
        {
                mapped_address = 0;
@@ -147,10 +173,13 @@ bool OpenGLTexture2D::AsyncLoader::process()
                if(!texture.id)
                        texture.create();
 
-               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, w, h, pixel_buffer, 0);
+               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);
 
                if(texture.auto_gen_mipmap)
                        texture.generate_mipmap();
index 175e76a2365688c8aef1f30592759d8addbefab1..5f21229fa8abc2d80c3e0029364bb519bd961bee 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/datafile/rawdata.h>
 #include <msp/io/memory.h>
 #include "error.h"
 #include "resourcemanager.h"
@@ -87,6 +88,7 @@ Texture::Loader::Loader(Texture &t, Collection *c):
        CollectionObjectLoader<Texture>(t, c),
        levels(0)
 {
+       add("external_data", &Loader::external_data);
        add("external_image", &Loader::external_image, false);
        add("external_image_srgb", &Loader::external_image, true);
        add("generate_mipmap", &Loader::generate_mipmap);
@@ -109,6 +111,19 @@ void Texture::Loader::load_external_image(Graphics::Image &img, const string &fn
        img.load_io(*io);
 }
 
+void Texture::Loader::external_data(const string &fn)
+{
+       if(obj.manager)
+               obj.manager->set_resource_location(obj, get_collection(), fn);
+       else
+       {
+               DataFile::RawData rd;
+               rd.open_file(get_collection(), fn);
+               rd.load();
+               obj.image(0, rd.get_data());
+       }
+}
+
 void Texture::Loader::external_image(bool srgb, const string &fn)
 {
        obj.use_srgb_format = srgb;
index 8db5dad83e0a90e6aa1bcde5a641f487a5e88355..639b15f5b711c820e8ff448f6d634078e2040ded 100644 (file)
@@ -49,6 +49,7 @@ protected:
                void load_external_image(Graphics::Image &, const std::string &);
 
        private:
+               void external_data(const std::string &);
                void external_image(bool, const std::string &);
                void generate_mipmap(bool);
                void image_data(const std::string &);