X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fbackends%2Fvulkan%2Ftexture_backend.cpp;h=06c4a622341955b1064539afe11b369c0620b9e6;hp=ff1afad9b1130933136f4970992f700ce1245765;hb=a16145549dc87c3b12671f797bd77b14bcc7786b;hpb=bbdf52425b736a59d01dda215458c3a1c9bdb320 diff --git a/source/backends/vulkan/texture_backend.cpp b/source/backends/vulkan/texture_backend.cpp index ff1afad9..06c4a622 100644 --- a/source/backends/vulkan/texture_backend.cpp +++ b/source/backends/vulkan/texture_backend.cpp @@ -1,5 +1,6 @@ #include "device.h" #include "error.h" +#include "synchronizer.h" #include "texture.h" #include "texture_backend.h" #include "vulkan.h" @@ -18,17 +19,23 @@ VulkanTexture::VulkanTexture(VulkanTexture &&other): device(other.device), handle(other.handle), view_handle(other.view_handle), + memory_id(other.memory_id), view_type(other.view_type), debug_name(move(other.debug_name)) { other.handle = 0; other.view_handle = 0; + other.memory_id = 0; } VulkanTexture::~VulkanTexture() { + DestroyQueue &dq = device.get_destroy_queue(); + if(view_handle) - device.get_destroy_queue().destroy(view_handle); + dq.destroy(view_handle); + if(handle) + dq.destroy(handle, memory_id); } void VulkanTexture::allocate() @@ -36,14 +43,44 @@ void VulkanTexture::allocate() const Texture &self = *static_cast(this); const VulkanFunctions &vk = device.get_functions(); + VkImageCreateInfo image_info = { }; + image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; + image_info.format = static_cast(get_vulkan_pixelformat(self.storage_fmt)); + image_info.extent.width = 1; + image_info.extent.height = 1; + image_info.extent.depth = 1; + image_info.mipLevels = 1; + image_info.arrayLayers = 1; + image_info.samples = VK_SAMPLE_COUNT_1_BIT; + image_info.tiling = VK_IMAGE_TILING_OPTIMAL; + 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; + PixelComponents comp = get_components(self.storage_fmt); + if(comp==DEPTH_COMPONENT || comp==STENCIL_INDEX) + image_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT; + else + image_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; + + fill_image_info(&image_info); + + /* SwapChainTexture may have already provided the image. Create_info is + filled anyway because some of its fields are used for view_info. */ if(!handle) - throw logic_error("Texture image allocation is unimplemented"); + { + vk.CreateImage(image_info, handle); + memory_id = device.get_allocator().allocate(handle, DEVICE_MEMORY); + + // Trigger a layout transition if the image is used before uploading data. + synchronize(-1, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, true); + } 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(view_type); - view_info.format = static_cast(get_vulkan_pixelformat(self.storage_fmt)); + view_info.format = image_info.format; const unsigned *swizzle_order = get_vulkan_swizzle(self.swizzle); view_info.components.r = static_cast(swizzle_order[0]); @@ -53,9 +90,9 @@ void VulkanTexture::allocate() view_info.subresourceRange.aspectMask = get_vulkan_aspect(get_components(self.storage_fmt)); view_info.subresourceRange.baseMipLevel = 0; - view_info.subresourceRange.levelCount = 1; + view_info.subresourceRange.levelCount = image_info.mipLevels; view_info.subresourceRange.baseArrayLayer = 0; - view_info.subresourceRange.layerCount = 1; + view_info.subresourceRange.layerCount = image_info.arrayLayers; vk.CreateImageView(view_info, view_handle); @@ -68,6 +105,12 @@ void VulkanTexture::generate_mipmap() throw logic_error("VulkanTexture::generate_mipmap is unimplemented"); } +void VulkanTexture::synchronize(int layer, unsigned layout, bool discard) const +{ + unsigned aspect = get_vulkan_aspect(get_components(static_cast(this)->storage_fmt)); + device.get_synchronizer().access(handle, aspect, layer, layout, discard); +} + void VulkanTexture::set_debug_name(const string &name) { #ifdef DEBUG @@ -84,9 +127,14 @@ void VulkanTexture::set_vulkan_object_names() const #ifdef DEBUG const VulkanFunctions &vk = device.get_functions(); - string view_name = debug_name+"/view"; VkDebugUtilsObjectNameInfoEXT name_info = { }; name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT; + name_info.objectType = VK_OBJECT_TYPE_IMAGE; + name_info.objectHandle = reinterpret_cast(handle); + name_info.pObjectName = debug_name.c_str(); + vk.SetDebugUtilsObjectName(name_info); + + string view_name = debug_name+"/view"; name_info.objectType = VK_OBJECT_TYPE_IMAGE_VIEW; name_info.objectHandle = reinterpret_cast(view_handle); name_info.pObjectName = view_name.c_str();