]> git.tdb.fi Git - libs/gl.git/blobdiff - source/backends/vulkan/texture_backend.cpp
Store the number of mipmap levels in the Texture base class
[libs/gl.git] / source / backends / vulkan / texture_backend.cpp
index dd19c63664b88658fe06917798742bbcbb4fd2f5..ec6d504732f9f0a13055eff1f42086d2fe4858a7 100644 (file)
@@ -49,7 +49,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,7 +73,7 @@ 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);
        }
 
        VkImageViewCreateInfo view_info = { };
@@ -137,15 +137,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);
+                       [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 +168,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);
 }