3 #include "synchronizer.h"
5 #include "texture_backend.h"
13 VulkanTexture::VulkanTexture(unsigned t):
14 device(Device::get_current()),
18 VulkanTexture::VulkanTexture(VulkanTexture &&other):
21 view_handle(other.view_handle),
22 memory_id(other.memory_id),
23 view_type(other.view_type),
24 debug_name(move(other.debug_name))
27 other.view_handle = 0;
31 VulkanTexture::~VulkanTexture()
33 DestroyQueue &dq = device.get_destroy_queue();
36 dq.destroy(view_handle);
38 dq.destroy(handle, memory_id);
41 void VulkanTexture::allocate()
43 const Texture &self = *static_cast<const Texture *>(this);
44 const VulkanFunctions &vk = device.get_functions();
46 VkImageCreateInfo image_info = { };
47 image_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
48 image_info.format = static_cast<VkFormat>(get_vulkan_pixelformat(self.storage_fmt));
49 image_info.extent.width = 1;
50 image_info.extent.height = 1;
51 image_info.extent.depth = 1;
52 image_info.mipLevels = 1;
53 image_info.arrayLayers = 1;
54 image_info.samples = VK_SAMPLE_COUNT_1_BIT;
55 image_info.tiling = VK_IMAGE_TILING_OPTIMAL;
56 image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
57 image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
59 image_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
60 PixelComponents comp = get_components(self.storage_fmt);
61 if(comp==DEPTH_COMPONENT || comp==STENCIL_INDEX)
62 image_info.usage |= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
64 image_info.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
66 fill_image_info(&image_info);
68 /* SwapChainTexture may have already provided the image. Create_info is
69 filled anyway because some of its fields are used for view_info. */
72 vk.CreateImage(image_info, handle);
73 memory_id = device.get_allocator().allocate(handle, DEVICE_MEMORY);
75 // Trigger a layout transition if the image is used before uploading data.
76 change_layout(0, -1, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, true);
79 VkImageViewCreateInfo view_info = { };
80 view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
81 view_info.image = handle_cast<::VkImage>(handle);
82 view_info.viewType = static_cast<VkImageViewType>(view_type);
83 view_info.format = image_info.format;
85 const unsigned *swizzle_order = get_vulkan_swizzle(self.swizzle);
86 view_info.components.r = static_cast<VkComponentSwizzle>(swizzle_order[0]);
87 view_info.components.g = static_cast<VkComponentSwizzle>(swizzle_order[1]);
88 view_info.components.b = static_cast<VkComponentSwizzle>(swizzle_order[2]);
89 view_info.components.a = static_cast<VkComponentSwizzle>(swizzle_order[3]);
91 view_info.subresourceRange.aspectMask = get_vulkan_aspect(get_components(self.storage_fmt));
92 view_info.subresourceRange.baseMipLevel = 0;
93 view_info.subresourceRange.levelCount = image_info.mipLevels;
94 view_info.subresourceRange.baseArrayLayer = 0;
95 view_info.subresourceRange.layerCount = image_info.arrayLayers;
97 vk.CreateImageView(view_info, view_handle);
99 if(!debug_name.empty())
100 set_vulkan_object_names();
103 void VulkanTexture::generate_mipmap()
105 throw logic_error("VulkanTexture::generate_mipmap is unimplemented");
108 void VulkanTexture::change_layout(unsigned n_levels, int level, unsigned layout, bool discard) const
110 unsigned aspect = get_vulkan_aspect(get_components(static_cast<const Texture *>(this)->storage_fmt));
112 device.get_synchronizer().split_image_mipmap(handle, aspect, n_levels);
113 device.get_synchronizer().change_image_layout(handle, aspect, level, layout, discard);
116 void VulkanTexture::set_debug_name(const string &name)
121 set_vulkan_object_names();
127 void VulkanTexture::set_vulkan_object_names() const
130 const VulkanFunctions &vk = device.get_functions();
132 VkDebugUtilsObjectNameInfoEXT name_info = { };
133 name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
134 name_info.objectType = VK_OBJECT_TYPE_IMAGE;
135 name_info.objectHandle = reinterpret_cast<uint64_t>(handle);
136 name_info.pObjectName = debug_name.c_str();
137 vk.SetDebugUtilsObjectName(name_info);
139 string view_name = debug_name+"/view";
140 name_info.objectType = VK_OBJECT_TYPE_IMAGE_VIEW;
141 name_info.objectHandle = reinterpret_cast<uint64_t>(view_handle);
142 name_info.pObjectName = view_name.c_str();
143 vk.SetDebugUtilsObjectName(name_info);