]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/texture_backend.cpp
Refactor TransferQueue to require explicit finalization of transfers
[libs/gl.git] / source / backends / vulkan / texture_backend.cpp
index dd19c63664b88658fe06917798742bbcbb4fd2f5..217299bf26d2ffe878572b03b2a57d752f757bf5 100644 (file)
@@ -1,3 +1,4 @@
+#include <msp/strings/format.h>
 #include "device.h"
 #include "error.h"
 #include "synchronizer.h"
@@ -34,6 +35,11 @@ VulkanTexture::~VulkanTexture()
 
        if(view_handle)
                dq.destroy(view_handle);
+       if(mip_view_handles.size()>1)
+       {
+               for(VkImageView i: mip_view_handles)
+                       dq.destroy(i);
+       }
        if(handle)
                dq.destroy(handle, memory_id);
 }
@@ -49,7 +55,7 @@ void VulkanTexture::allocate()
        image_info.extent.width = 1;
        image_info.extent.height = 1;
        image_info.extent.depth = 1;
-       image_info.mipLevels = 1;
+       image_info.mipLevels = self.n_levels;
        image_info.arrayLayers = 1;
        image_info.samples = VK_SAMPLE_COUNT_1_BIT;
        image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
@@ -73,14 +79,25 @@ void VulkanTexture::allocate()
                memory_id = device.get_allocator().allocate(handle, DEVICE_MEMORY);
 
                // Trigger a layout transition if the image is used before uploading data.
-               change_layout(0, -1, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, true);
+               change_layout(-1, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, true);
        }
 
+       view_handle = create_view(-1);
+
+       if(!debug_name.empty())
+               set_vulkan_object_names();
+}
+
+VkImageView VulkanTexture::create_view(int level) const
+{
+       const Texture &self = *static_cast<const Texture *>(this);
+       const VulkanFunctions &vk = device.get_functions();
+
        VkImageViewCreateInfo view_info = { };
        view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
        view_info.image = handle_cast<::VkImage>(handle);
        view_info.viewType = static_cast<VkImageViewType>(view_type);
-       view_info.format = image_info.format;
+       view_info.format = static_cast<VkFormat>(get_vulkan_pixelformat(self.storage_fmt));
 
        const unsigned *swizzle_order = get_vulkan_swizzle(self.swizzle);
        view_info.components.r = static_cast<VkComponentSwizzle>(swizzle_order[0]);
@@ -89,15 +106,32 @@ void VulkanTexture::allocate()
        view_info.components.a = static_cast<VkComponentSwizzle>(swizzle_order[3]);
 
        view_info.subresourceRange.aspectMask = get_vulkan_aspect(get_components(self.storage_fmt));
-       view_info.subresourceRange.baseMipLevel = 0;
-       view_info.subresourceRange.levelCount = image_info.mipLevels;
+       view_info.subresourceRange.baseMipLevel = max(level, 0);
+       view_info.subresourceRange.levelCount = (level<0 ? VK_REMAINING_MIP_LEVELS : 1);
        view_info.subresourceRange.baseArrayLayer = 0;
-       view_info.subresourceRange.layerCount = image_info.arrayLayers;
+       view_info.subresourceRange.layerCount = VK_REMAINING_ARRAY_LAYERS;
 
-       vk.CreateImageView(view_info, view_handle);
+       VkImageView view;
+       vk.CreateImageView(view_info, view);
 
-       if(!debug_name.empty())
-               set_vulkan_object_names();
+       return view;
+}
+
+void VulkanTexture::create_mip_views() const
+{
+       const Texture &self = *static_cast<const Texture *>(this);
+
+       if(!mip_view_handles.empty())
+               return;
+               
+       mip_view_handles.resize(self.n_levels);
+       if(self.n_levels==1)
+               mip_view_handles[0] = view_handle;
+       else
+       {
+               for(unsigned i=0; i<self.n_levels; ++i)
+                       mip_view_handles[i] = create_view(i);
+       }
 }
 
 void VulkanTexture::stage_pixels(void *staging, const void *data, size_t count)
@@ -137,15 +171,17 @@ void VulkanTexture::stage_pixels(void *staging, const void *data, size_t count)
        }
 }
 
-void VulkanTexture::generate_mipmap_levels(unsigned n_levels)
+void VulkanTexture::generate_mipmap()
 {
+       unsigned n_levels = static_cast<const Texture *>(this)->n_levels;
+
        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);
+               tq.prepare_transfer(this, true,
+                       [this, i](){
+                               change_layout(i, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, false);
+                               change_layout(i+1, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, true);
                        },
                        [this, i](VkCommandBuffer cmd_buf, VkBuffer, size_t){
                                const VulkanFunctions &vk = device.get_functions();
@@ -166,11 +202,13 @@ void VulkanTexture::generate_mipmap_levels(unsigned n_levels)
        }
 }
 
-void VulkanTexture::change_layout(unsigned n_levels, int level, unsigned layout, bool discard) const
+void VulkanTexture::change_layout(int level, unsigned layout, bool discard) const
 {
-       unsigned aspect = get_vulkan_aspect(get_components(static_cast<const Texture *>(this)->storage_fmt));
-       if(n_levels>0)
-               device.get_synchronizer().split_image_mipmap(handle, aspect, n_levels);
+       const Texture &self = *static_cast<const Texture *>(this);
+
+       unsigned aspect = get_vulkan_aspect(get_components(self.storage_fmt));
+       if(level>=0)
+               device.get_synchronizer().split_image_mipmap(handle, aspect, self.n_levels);
        device.get_synchronizer().change_image_layout(handle, aspect, level, layout, discard);
 }
 
@@ -202,6 +240,17 @@ void VulkanTexture::set_vulkan_object_names() const
        name_info.objectHandle = reinterpret_cast<uint64_t>(view_handle);
        name_info.pObjectName = view_name.c_str();
        vk.SetDebugUtilsObjectName(name_info);
+
+       if(mip_view_handles.size()>1)
+       {
+               for(unsigned i=0; i<mip_view_handles.size(); ++i)
+               {
+                       view_name = format("%s/mip%d.view", debug_name, i);
+                       name_info.objectHandle = reinterpret_cast<uint64_t>(mip_view_handles[i]);
+                       name_info.pObjectName = view_name.c_str();
+                       vk.SetDebugUtilsObjectName(name_info);
+               }
+       }
 #endif
 }