From 7b0a38db12c3d1aacb59520a3f4baa16d9ec0048 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 1 Dec 2021 10:54:07 +0200 Subject: [PATCH] Convert RGB pixel data to RGBA when staging --- source/backends/vulkan/texture1d_backend.cpp | 3 +- source/backends/vulkan/texture2d_backend.cpp | 3 +- source/backends/vulkan/texture3d_backend.cpp | 3 +- source/backends/vulkan/texture_backend.cpp | 37 +++++++++++++++++++ source/backends/vulkan/texture_backend.h | 2 + .../backends/vulkan/texturecube_backend.cpp | 3 +- 6 files changed, 43 insertions(+), 8 deletions(-) diff --git a/source/backends/vulkan/texture1d_backend.cpp b/source/backends/vulkan/texture1d_backend.cpp index 1bf60f70..e27f371b 100644 --- a/source/backends/vulkan/texture1d_backend.cpp +++ b/source/backends/vulkan/texture1d_backend.cpp @@ -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, ®ion); }); - const char *src = static_cast(data); - copy(src, src+data_size, static_cast(staging)); + stage_pixels(staging, data, wd); } void VulkanTexture1D::generate_mipmap() diff --git a/source/backends/vulkan/texture2d_backend.cpp b/source/backends/vulkan/texture2d_backend.cpp index e40dfe4f..3b94e766 100644 --- a/source/backends/vulkan/texture2d_backend.cpp +++ b/source/backends/vulkan/texture2d_backend.cpp @@ -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, ®ion); }); - const char *src = static_cast(data); - copy(src, src+data_size, static_cast(staging)); + stage_pixels(staging, data, wd*ht); } void VulkanTexture2D::generate_mipmap() diff --git a/source/backends/vulkan/texture3d_backend.cpp b/source/backends/vulkan/texture3d_backend.cpp index 86653ef2..ee3490a7 100644 --- a/source/backends/vulkan/texture3d_backend.cpp +++ b/source/backends/vulkan/texture3d_backend.cpp @@ -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, ®ion); }); - const char *src = static_cast(data); - copy(src, src+data_size, static_cast(staging)); + stage_pixels(staging, data, wd*ht*dp); } void VulkanTexture3D::generate_mipmap() diff --git a/source/backends/vulkan/texture_backend.cpp b/source/backends/vulkan/texture_backend.cpp index 168d6adb..dd19c636 100644 --- a/source/backends/vulkan/texture_backend.cpp +++ b/source/backends/vulkan/texture_backend.cpp @@ -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(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_levels(unsigned n_levels) { TransferQueue &tq = device.get_transfer_queue(); diff --git a/source/backends/vulkan/texture_backend.h b/source/backends/vulkan/texture_backend.h index 9d043500..dad8834b 100644 --- a/source/backends/vulkan/texture_backend.h +++ b/source/backends/vulkan/texture_backend.h @@ -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; diff --git a/source/backends/vulkan/texturecube_backend.cpp b/source/backends/vulkan/texturecube_backend.cpp index 4d7d0baf..6a4e598c 100644 --- a/source/backends/vulkan/texturecube_backend.cpp +++ b/source/backends/vulkan/texturecube_backend.cpp @@ -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, ®ion); }); - const char *src = static_cast(data); - copy(src, src+data_size, static_cast(staging)); + stage_pixels(staging, data, wd*ht); } void VulkanTextureCube::generate_mipmap() -- 2.45.2