]> git.tdb.fi Git - libs/gl.git/commitdiff
Implement mipmap generation for the Vulkan backend
authorMikko Rasa <tdb@tdb.fi>
Wed, 1 Dec 2021 08:31:43 +0000 (10:31 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 1 Dec 2021 08:31:52 +0000 (10:31 +0200)
14 files changed:
source/backends/vulkan/texture1d_backend.cpp
source/backends/vulkan/texture1d_backend.h
source/backends/vulkan/texture2d_backend.cpp
source/backends/vulkan/texture2d_backend.h
source/backends/vulkan/texture2dmultisample_backend.cpp
source/backends/vulkan/texture2dmultisample_backend.h
source/backends/vulkan/texture3d_backend.cpp
source/backends/vulkan/texture3d_backend.h
source/backends/vulkan/texture_backend.cpp
source/backends/vulkan/texture_backend.h
source/backends/vulkan/texturecube_backend.cpp
source/backends/vulkan/texturecube_backend.h
source/backends/vulkan/vulkan.cpp
source/backends/vulkan/vulkan.h

index e28ec4236cd9ae1b9758c9b77478830c43dbf214..d66732bf6b7c62056058b38abf7cfa6e9d01e74d 100644 (file)
@@ -53,6 +53,23 @@ void VulkanTexture1D::sub_image(unsigned level, int x, unsigned wd, const void *
        copy(src, src+data_size, static_cast<char *>(staging));
 }
 
+void VulkanTexture1D::generate_mipmap()
+{
+       generate_mipmap_levels(static_cast<const Texture1D *>(this)->levels);
+}
+
+void VulkanTexture1D::fill_mipmap_blit(unsigned level, void *b)
+{
+       const Texture1D &self = *static_cast<const Texture1D *>(this);
+       VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
+
+       int src_size = self.get_level_size(level);
+       int dst_size = self.get_level_size(level+1);
+
+       blit.srcOffsets[1] = { src_size, 1, 1 };
+       blit.dstOffsets[1] = { dst_size, 1, 1 };
+}
+
 size_t VulkanTexture1D::get_data_size() const
 {
        return 0;
index 424d262a7e271de1586fa493980056d5d11b6f7a..675da1b2c94cf43e7111748cab43215f42275b5a 100644 (file)
@@ -13,6 +13,8 @@ protected:
 
        virtual void fill_image_info(void *) const;
        void sub_image(unsigned, int, unsigned, const void *);
+       virtual void generate_mipmap();
+       virtual void fill_mipmap_blit(unsigned, void *);
 
 public:
        virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
index 71fb90cdbd4d90eec9bbbbcc91d2b06950dfa596..518f4c2ea54d552e0d50113f8c75a7f813d6e51c 100644 (file)
@@ -54,6 +54,23 @@ void VulkanTexture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsig
        copy(src, src+data_size, static_cast<char *>(staging));
 }
 
+void VulkanTexture2D::generate_mipmap()
+{
+       generate_mipmap_levels(static_cast<const Texture2D *>(this)->levels);
+}
+
+void VulkanTexture2D::fill_mipmap_blit(unsigned level, void *b)
+{
+       const Texture2D &self = *static_cast<const Texture2D *>(this);
+       VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
+
+       auto src_size = self.get_level_size(level);
+       auto dst_size = self.get_level_size(level+1);
+
+       blit.srcOffsets[1] = { static_cast<int>(src_size.x), static_cast<int>(src_size.y), 1 };
+       blit.dstOffsets[1] = { static_cast<int>(dst_size.x), static_cast<int>(dst_size.y), 1 };
+}
+
 Resource::AsyncLoader *VulkanTexture2D::load(IO::Seekable &, const Resources *)
 {
        throw logic_error("Texture2D::load is unimplemented");
index 1a5377ed668b29fc8db8c94fa1ee77bcb9e9b02f..cfd5ff3d447a84d14f282e4c57ec01f13b2c815c 100644 (file)
@@ -13,6 +13,8 @@ protected:
 
        virtual void fill_image_info(void *) const;
        void sub_image(unsigned, int, int, unsigned, unsigned, const void *);
+       virtual void generate_mipmap();
+       virtual void fill_mipmap_blit(unsigned, void *);
 
 public:
        virtual Resource::AsyncLoader *load(IO::Seekable &, const Resources * = 0);
index 4a81273b7d614e483946a4a358037d532b00f536..2e39c6538088f5fee62180cd13add61636e6d2a9 100644 (file)
@@ -1,3 +1,4 @@
+#include "error.h"
 #include "frameformat.h"
 #include "texture2dmultisample.h"
 #include "texture2dmultisample_backend.h"
@@ -23,6 +24,11 @@ void VulkanTexture2DMultisample::fill_image_info(void *ii) const
        image_info->samples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(self.samples));
 }
 
+void VulkanTexture2DMultisample::generate_mipmap()
+{
+       throw invalid_operation("VulkanTexture2DMultisample::generate_mipmap");
+}
+
 size_t VulkanTexture2DMultisample::get_data_size() const
 {
        return 0;
index d20e33bccf3cd116ce93c4b71a3f672a22378769..392fb0fb03fd679af1e032eafe2123eabde943e3 100644 (file)
@@ -12,6 +12,8 @@ protected:
        VulkanTexture2DMultisample();
 
        virtual void fill_image_info(void *) const;
+       virtual void generate_mipmap();
+       virtual void fill_mipmap_blit(unsigned, void *) { }
 
 public:
        virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
index 581206c671bf40498344d59a2387eb1c68fe1817..44a25904161306afbb5ab52a9f24a420bb57edef 100644 (file)
@@ -59,6 +59,25 @@ void VulkanTexture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd
        copy(src, src+data_size, static_cast<char *>(staging));
 }
 
+void VulkanTexture3D::generate_mipmap()
+{
+       generate_mipmap_levels(static_cast<const Texture3D *>(this)->levels);
+}
+
+void VulkanTexture3D::fill_mipmap_blit(unsigned level, void *b)
+{
+       const Texture3D &self = *static_cast<const Texture3D *>(this);
+       VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
+
+       LinAl::Vector<int, 3> src_size = self.get_level_size(level);
+       LinAl::Vector<int, 3> dst_size = self.get_level_size(level+1);
+
+       blit.srcSubresource.layerCount = (is_array() ? self.depth : 1);
+       blit.dstSubresource.layerCount = blit.srcSubresource.layerCount;
+       blit.srcOffsets[1] = { src_size.x, src_size.y, (is_array() ? 1 : src_size.z) };
+       blit.dstOffsets[1] = { dst_size.x, dst_size.y, (is_array() ? 1 : dst_size.z) };
+}
+
 bool VulkanTexture3D::is_array() const
 {
        return view_type==VK_IMAGE_VIEW_TYPE_2D_ARRAY;
index c5bdeb57188897362590e71ea8b1f58a756d036d..e1c8f8f5469deeb20aad8c4d3f1d065828b80a69 100644 (file)
@@ -14,6 +14,8 @@ protected:
 
        virtual void fill_image_info(void *) const;
        void sub_image(unsigned, int, int, int, unsigned, unsigned, unsigned, const void *);
+       virtual void generate_mipmap();
+       virtual void fill_mipmap_blit(unsigned, void *);
 
        bool is_array() const;
 
index f1e8b85bab162e32926db81f78ced507752e5d04..168d6adb0501d69a501b83e8bb726a860fd8e978 100644 (file)
@@ -56,7 +56,7 @@ void VulkanTexture::allocate()
        image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
        image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
 
-       image_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
+       image_info.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT|VK_IMAGE_USAGE_TRANSFER_DST_BIT|VK_IMAGE_USAGE_SAMPLED_BIT;
        PixelComponents comp = get_components(self.storage_fmt);
        if(comp==DEPTH_COMPONENT || comp==STENCIL_INDEX)
                image_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
@@ -100,9 +100,33 @@ void VulkanTexture::allocate()
                set_vulkan_object_names();
 }
 
-void VulkanTexture::generate_mipmap()
+void VulkanTexture::generate_mipmap_levels(unsigned n_levels)
 {
-       throw logic_error("VulkanTexture::generate_mipmap is unimplemented");
+       TransferQueue &tq = device.get_transfer_queue();
+       for(unsigned i=0; i+1<n_levels; ++i)
+       {
+               tq.prepare_transfer(this, true, 0,
+                       [this, n_levels, i](){
+                               change_layout((i==0 ? n_levels : 0), i, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, false);
+                               change_layout(0, i+1, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, true);
+                       },
+                       [this, i](VkCommandBuffer cmd_buf, VkBuffer, size_t){
+                               const VulkanFunctions &vk = device.get_functions();
+
+                               VkImageBlit region = { };
+                               region.srcSubresource.aspectMask = get_vulkan_aspect(get_components(static_cast<const Texture *>(this)->storage_fmt));
+                               region.srcSubresource.mipLevel = i;
+                               region.srcSubresource.baseArrayLayer = 0;
+                               region.srcSubresource.layerCount = 1;
+                               region.dstSubresource = region.srcSubresource;
+                               ++region.dstSubresource.mipLevel;
+
+                               fill_mipmap_blit(i, &region);
+
+                               vk.CmdBlitImage(cmd_buf, handle, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
+                                       1, &region, VK_FILTER_LINEAR);
+                       });
+       }
 }
 
 void VulkanTexture::change_layout(unsigned n_levels, int level, unsigned layout, bool discard) const
index 123c6e4b278efd1d9f3325c351737241800f9d49..9d0435000f45578c8815bd428869e98b9cc056d8 100644 (file)
@@ -30,7 +30,9 @@ protected:
        virtual void fill_image_info(void *) const = 0;
        void require_swizzle() { }
 
-       void generate_mipmap();
+       virtual void generate_mipmap() = 0;
+       void generate_mipmap_levels(unsigned);
+       virtual void fill_mipmap_blit(unsigned, void *) = 0;
 
        void change_layout(unsigned, int, unsigned, bool) const;
 
index 5b7bd368760f257698765aeff638ce8a427188ff..98f000447c8a2f3aef69cacdfe20e929719d73dc 100644 (file)
@@ -56,6 +56,25 @@ void VulkanTextureCube::sub_image(unsigned face, unsigned level, int x, int y, u
        copy(src, src+data_size, static_cast<char *>(staging));
 }
 
+void VulkanTextureCube::generate_mipmap()
+{
+       generate_mipmap_levels(static_cast<const TextureCube *>(this)->levels);
+}
+
+void VulkanTextureCube::fill_mipmap_blit(unsigned level, void *b)
+{
+       const TextureCube &self = *static_cast<const TextureCube *>(this);
+       VkImageBlit &blit = *static_cast<VkImageBlit *>(b);
+
+       int src_size = self.get_level_size(level);
+       int dst_size = self.get_level_size(level+1);
+
+       blit.srcSubresource.layerCount = 6;
+       blit.dstSubresource.layerCount = 6;
+       blit.srcOffsets[1] = { src_size, src_size, 1 };
+       blit.dstOffsets[1] = { dst_size, dst_size, 1 };
+}
+
 size_t VulkanTextureCube::get_data_size() const
 {
        return 0;
index 2e8d936fe3ad0bc04dd8dcb6e72c500ab35cdd55..d77308972ecb246f531957a6b028ea8690c46cd3 100644 (file)
@@ -13,6 +13,8 @@ protected:
 
        virtual void fill_image_info(void *) const;
        void sub_image(unsigned, unsigned, int, int, unsigned, unsigned, const void *);
+       virtual void generate_mipmap();
+       virtual void fill_mipmap_blit(unsigned, void *);
 
 public:
        virtual AsyncLoader *load(IO::Seekable &, const Resources * = 0) { return 0; }
index b4033af903c0324688976c2d52cb2e4cedce25a3..1f697f6637bfc2a2e7a148af03cd9901abade92c 100644 (file)
@@ -77,6 +77,7 @@ VulkanFunctions::VulkanFunctions(const Graphics::VulkanContext &c):
        // 19
        vkCmdCopyBuffer(context.get_function<PFN_vkCmdCopyBuffer>("vkCmdCopyBuffer")),
        vkCmdCopyBufferToImage(context.get_function<PFN_vkCmdCopyBufferToImage>("vkCmdCopyBufferToImage")),
+       vkCmdBlitImage(context.get_function<PFN_vkCmdBlitImage>("vkCmdBlitImage")),
        // 20
        vkCmdBindIndexBuffer(context.get_function<PFN_vkCmdBindIndexBuffer>("vkCmdBindIndexBuffer")),
        vkCmdDrawIndexed(context.get_function<PFN_vkCmdDrawIndexed>("vkCmdDrawIndexed")),
index b4b084bd4f4b6928ea4ae1d1775331ae62181691..60de01d18ba103006275f0605793dc2b2851306a 100644 (file)
@@ -155,6 +155,7 @@ private:
        PFN_vkCmdPushConstants vkCmdPushConstants = 0;  // 14.2.10
        PFN_vkCmdCopyBuffer vkCmdCopyBuffer = 0;  // 19.2
        PFN_vkCmdCopyBufferToImage vkCmdCopyBufferToImage = 0;  // 19.4
+       PFN_vkCmdBlitImage vkCmdBlitImage = 0;  // 19.5
        PFN_vkCmdBindIndexBuffer vkCmdBindIndexBuffer = 0;  // 20.3
        PFN_vkCmdDrawIndexed vkCmdDrawIndexed = 0;  // 20.3
        PFN_vkCmdBindVertexBuffers vkCmdBindVertexBuffers = 0;  // 21.2
@@ -356,6 +357,9 @@ public:
        void CmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage, VkImageLayout dstImageLayout, std::uint32_t regionCount, const VkBufferImageCopy *pRegions) const
        { vkCmdCopyBufferToImage(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkBuffer>(srcBuffer), handle_cast<::VkImage>(dstImage), dstImageLayout, regionCount, pRegions); }
 
+       void CmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageBlit *pRegions, VkFilter filter) const
+       { vkCmdBlitImage(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkImage>(srcImage), srcImageLayout, handle_cast<::VkImage>(dstImage), dstImageLayout, regionCount, pRegions, filter); }
+
        // Chapter 20: Drawing Commands
        void CmdBindIndexBuffer(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, VkIndexType indexType) const
        { vkCmdBindIndexBuffer(handle_cast<::VkCommandBuffer>(commandBuffer), handle_cast<::VkBuffer>(buffer), offset, indexType); }