From: Mikko Rasa Date: Tue, 28 Dec 2021 20:11:03 +0000 (+0200) Subject: Convert pixel components if necessary when async loading textures X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=3f2ce5d1ba85bc0c03e6718cc41c2dac5e959d75;p=libs%2Fgl.git Convert pixel components if necessary when async loading textures --- diff --git a/source/backends/vulkan/texture_backend.cpp b/source/backends/vulkan/texture_backend.cpp index 217299bf..4e2e8224 100644 --- a/source/backends/vulkan/texture_backend.cpp +++ b/source/backends/vulkan/texture_backend.cpp @@ -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(this); - - if(self.swizzle==RGBA_TO_RGB) - { - const uint32_t *src = static_cast(data); - uint32_t *dst = static_cast(staging); - size_t i = 0; - for(; i+3>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(src); - for(; i(data); - size_t data_size = count*get_pixel_size(self.storage_fmt); - copy(src, src+data_size, static_cast(staging)); - } -} - void VulkanTexture::generate_mipmap() { unsigned n_levels = static_cast(this)->n_levels; diff --git a/source/backends/vulkan/texture_backend.h b/source/backends/vulkan/texture_backend.h index f7ea99b0..c977e82e 100644 --- a/source/backends/vulkan/texture_backend.h +++ b/source/backends/vulkan/texture_backend.h @@ -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; diff --git a/source/core/texture.cpp b/source/core/texture.cpp index 0d7a8151..a65715e7 100644 --- a/source/core/texture.cpp +++ b/source/core/texture.cpp @@ -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(data); + uint32_t *dst = static_cast(staging); + size_t i = 0; + for(; i+3>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(src); + for(; i(data); + size_t data_size = count*get_pixel_size(storage_fmt); + copy(src, src+data_size, static_cast(staging)); + } +} + void Texture::load_image(const string &fn, unsigned lv) { Graphics::Image img; diff --git a/source/core/texture.h b/source/core/texture.h index eb5275c6..96b17b89 100644 --- a/source/core/texture.h +++ b/source/core/texture.h @@ -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; } diff --git a/source/core/texture2d.cpp b/source/core/texture2d.cpp index a0ba9515..e38c99de 100644 --- a/source/core/texture2d.cpp +++ b/source/core/texture2d.cpp @@ -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());