]> git.tdb.fi Git - libs/gl.git/commitdiff
Convert RGB pixel data to RGBA when staging
authorMikko Rasa <tdb@tdb.fi>
Wed, 1 Dec 2021 08:54:07 +0000 (10:54 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 1 Dec 2021 08:54:07 +0000 (10:54 +0200)
source/backends/vulkan/texture1d_backend.cpp
source/backends/vulkan/texture2d_backend.cpp
source/backends/vulkan/texture3d_backend.cpp
source/backends/vulkan/texture_backend.cpp
source/backends/vulkan/texture_backend.h
source/backends/vulkan/texturecube_backend.cpp

index 1bf60f701d720733df4f9bbe8dd1236d330cc594..e27f371bdc33c75608303d5926198acc49840cd1 100644 (file)
@@ -49,8 +49,7 @@ void VulkanTexture1D::sub_image(unsigned level, int x, unsigned wd, const void *
                        vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
                });
 
-       const char *src = static_cast<const char *>(data);
-       copy(src, src+data_size, static_cast<char *>(staging));
+       stage_pixels(staging, data, wd);
 }
 
 void VulkanTexture1D::generate_mipmap()
index e40dfe4fedf3003287503f195f4b2426cc86cd37..3b94e766dc21ff4be2ec2b88ad4c49d7d5c7c1bf 100644 (file)
@@ -50,8 +50,7 @@ void VulkanTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsig
                        vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
                });
 
-       const char *src = static_cast<const char *>(data);
-       copy(src, src+data_size, static_cast<char *>(staging));
+       stage_pixels(staging, data, wd*ht);
 }
 
 void VulkanTexture2D::generate_mipmap()
index 86653ef2b04796f4148050b11780e16884fc932e..ee3490a77263e43e25ddb6d967e2d839926897ab 100644 (file)
@@ -55,8 +55,7 @@ void VulkanTexture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd
                        vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
                });
 
-       const char *src = static_cast<const char *>(data);
-       copy(src, src+data_size, static_cast<char *>(staging));
+       stage_pixels(staging, data, wd*ht*dp);
 }
 
 void VulkanTexture3D::generate_mipmap()
index 168d6adb0501d69a501b83e8bb726a860fd8e978..dd19c63664b88658fe06917798742bbcbb4fd2f5 100644 (file)
@@ -100,6 +100,43 @@ void VulkanTexture::allocate()
                set_vulkan_object_names();
 }
 
+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_levels(unsigned n_levels)
 {
        TransferQueue &tq = device.get_transfer_queue();
index 9d0435000f45578c8815bd428869e98b9cc056d8..dad8834b2940487d1c2b0bbc3676b5e633b26b5a 100644 (file)
@@ -30,6 +30,8 @@ protected:
        virtual void fill_image_info(void *) const = 0;
        void require_swizzle() { }
 
+       void stage_pixels(void *, const void *, size_t);
+
        virtual void generate_mipmap() = 0;
        void generate_mipmap_levels(unsigned);
        virtual void fill_mipmap_blit(unsigned, void *) = 0;
index 4d7d0baf14d053c97c3e8ddda9eabd5cdb28b7d9..6a4e598c0c103b0ece427cbcd34bc07faa330f09 100644 (file)
@@ -52,8 +52,7 @@ void VulkanTextureCube::sub_image(unsigned face, unsigned level, int x, int y, u
                        vk.CmdCopyBufferToImage(cmd_buf, staging_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &region);
                });
 
-       const char *src = static_cast<const char *>(data);
-       copy(src, src+data_size, static_cast<char *>(staging));
+       stage_pixels(staging, data, wd*ht);
 }
 
 void VulkanTextureCube::generate_mipmap()