]> git.tdb.fi Git - libs/gl.git/commitdiff
Convert pixel components if necessary when async loading textures
authorMikko Rasa <tdb@tdb.fi>
Tue, 28 Dec 2021 20:11:03 +0000 (22:11 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 28 Dec 2021 20:11:03 +0000 (22:11 +0200)
source/backends/vulkan/texture_backend.cpp
source/backends/vulkan/texture_backend.h
source/core/texture.cpp
source/core/texture.h
source/core/texture2d.cpp

index 217299bf26d2ffe878572b03b2a57d752f757bf5..4e2e82249dde43a951ecfb82741a92442d00072c 100644 (file)
@@ -134,43 +134,6 @@ void VulkanTexture::create_mip_views() const
        }
 }
 
-void VulkanTexture::stage_pixels(void *staging, const void *data, size_t count)
-{
-       const Texture &self = *static_cast<const Texture *>(this);
-
-       if(self.swizzle==RGBA_TO_RGB)
-       {
-               const uint32_t *src = static_cast<const uint32_t *>(data);
-               uint32_t *dst = static_cast<uint32_t *>(staging);
-               size_t i = 0;
-               for(; i+3<count; i+=4)
-               {
-                       dst[0] = src[0]|0xFF000000;
-                       dst[1] = (src[0]>>24)|(src[1]<<8)|0xFF000000;
-                       dst[2] = (src[1]>>16)|(src[2]<<16)|0xFF000000;
-                       dst[3] = (src[2]>>8)|0xFF000000;
-                       src += 3;
-                       dst += 4;
-               }
-
-               if(i<count)
-               {
-                       const uint8_t *src_bytes = reinterpret_cast<const uint8_t *>(src);
-                       for(; i<count; ++i)
-                       {
-                               *dst++ = src_bytes[0]|(src_bytes[1]<<8)|(src_bytes[2]<<16)|0xFF000000;
-                               src_bytes += 3;
-                       }
-               }
-       }
-       else
-       {
-               const char *src = static_cast<const char *>(data);
-               size_t data_size = count*get_pixel_size(self.storage_fmt);
-               copy(src, src+data_size, static_cast<char *>(staging));
-       }
-}
-
 void VulkanTexture::generate_mipmap()
 {
        unsigned n_levels = static_cast<const Texture *>(this)->n_levels;
index f7ea99b0ecd4c13b605c0bc0bfc54a5b75fe1edf..c977e82e287f120fb13727e32c7c956617573804 100644 (file)
@@ -33,8 +33,6 @@ protected:
        void create_mip_views() const;
        void require_swizzle() { }
 
-       void stage_pixels(void *, const void *, size_t);
-
        void generate_mipmap();
        virtual void fill_mipmap_blit(unsigned, void *) = 0;
 
index 0d7a81512e7f73f5c0a49659b4feddc16739c582..a65715e716a26c6fbc283655c37c0382fc795010 100644 (file)
@@ -41,6 +41,41 @@ unsigned Texture::count_levels(unsigned size)
        return n;
 }
 
+void Texture::stage_pixels(void *staging, const void *data, size_t count)
+{
+       if(swizzle==RGBA_TO_RGB)
+       {
+               const uint32_t *src = static_cast<const uint32_t *>(data);
+               uint32_t *dst = static_cast<uint32_t *>(staging);
+               size_t i = 0;
+               for(; i+3<count; i+=4)
+               {
+                       dst[0] = src[0]|0xFF000000;
+                       dst[1] = (src[0]>>24)|(src[1]<<8)|0xFF000000;
+                       dst[2] = (src[1]>>16)|(src[2]<<16)|0xFF000000;
+                       dst[3] = (src[2]>>8)|0xFF000000;
+                       src += 3;
+                       dst += 4;
+               }
+
+               if(i<count)
+               {
+                       const uint8_t *src_bytes = reinterpret_cast<const uint8_t *>(src);
+                       for(; i<count; ++i)
+                       {
+                               *dst++ = src_bytes[0]|(src_bytes[1]<<8)|(src_bytes[2]<<16)|0xFF000000;
+                               src_bytes += 3;
+                       }
+               }
+       }
+       else
+       {
+               const char *src = static_cast<const char *>(data);
+               size_t data_size = count*get_pixel_size(storage_fmt);
+               copy(src, src+data_size, static_cast<char *>(staging));
+       }
+}
+
 void Texture::load_image(const string &fn, unsigned lv)
 {
        Graphics::Image img;
index eb5275c6e4fd71e60b6d0af04d179a229a94a56a..96b17b8910752e0f0f46f7466b2acf7d014e2746 100644 (file)
@@ -82,6 +82,8 @@ protected:
        void set_format(PixelFormat);
        static unsigned count_levels(unsigned);
 
+       void stage_pixels(void *, const void *, std::size_t);
+
 public:
        PixelFormat get_format() const { return format; }
 
index a0ba9515e68babe3387b9e2af1278087b1d6d3f9..e38c99deb85e2dda60603b89e97405a013c763cd 100644 (file)
@@ -238,7 +238,22 @@ bool Texture2D::AsyncLoader::process()
        }
        else if(phase==2)
        {
-               if(raw_data)
+               if(texture.swizzle==RGBA_TO_RGB)
+               {
+                       const void *data;
+                       if(raw_data)
+                       {
+                               raw_data->load();
+                               data = raw_data->get_data();
+                       }
+                       else
+                       {
+                               image.load(*img_loader);
+                               data = image.get_pixels();
+                       }
+                       texture.stage_pixels(transfer.get_address(), data, texture.width*texture.height);
+               }
+               else if(raw_data)
                        raw_data->load_into(transfer.get_address());
                else
                        image.load_into(*img_loader, transfer.get_address());