]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/texture_backend.cpp
Initial implementation of Vulkan backend
[libs/gl.git] / source / backends / vulkan / texture_backend.cpp
1 #include "device.h"
2 #include "error.h"
3 #include "texture.h"
4 #include "texture_backend.h"
5 #include "vulkan.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 VulkanTexture::VulkanTexture(unsigned t):
13         device(Device::get_current()),
14         view_type(t)
15 { }
16
17 VulkanTexture::VulkanTexture(VulkanTexture &&other):
18         device(other.device),
19         handle(other.handle),
20         view_handle(other.view_handle),
21         view_type(other.view_type),
22         debug_name(move(other.debug_name))
23 {
24         other.handle = 0;
25         other.view_handle = 0;
26 }
27
28 VulkanTexture::~VulkanTexture()
29 {
30         if(view_handle)
31                 device.get_destroy_queue().destroy(view_handle);
32 }
33
34 void VulkanTexture::allocate()
35 {
36         const Texture &self = *static_cast<const Texture *>(this);
37         const VulkanFunctions &vk = device.get_functions();
38
39         if(!handle)
40                 throw logic_error("Texture image allocation is unimplemented");
41
42         VkImageViewCreateInfo view_info = { };
43         view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
44         view_info.image = handle_cast<::VkImage>(handle);
45         view_info.viewType = static_cast<VkImageViewType>(view_type);
46         view_info.format = static_cast<VkFormat>(get_vulkan_pixelformat(self.storage_fmt));
47
48         const unsigned *swizzle_order = get_vulkan_swizzle(self.swizzle);
49         view_info.components.r = static_cast<VkComponentSwizzle>(swizzle_order[0]);
50         view_info.components.g = static_cast<VkComponentSwizzle>(swizzle_order[1]);
51         view_info.components.b = static_cast<VkComponentSwizzle>(swizzle_order[2]);
52         view_info.components.a = static_cast<VkComponentSwizzle>(swizzle_order[3]);
53
54         view_info.subresourceRange.aspectMask = get_vulkan_aspect(get_components(self.storage_fmt));
55         view_info.subresourceRange.baseMipLevel = 0;
56         view_info.subresourceRange.levelCount = 1;
57         view_info.subresourceRange.baseArrayLayer = 0;
58         view_info.subresourceRange.layerCount = 1;
59
60         vk.CreateImageView(view_info, view_handle);
61
62         if(!debug_name.empty())
63                 set_vulkan_object_names();
64 }
65
66 void VulkanTexture::generate_mipmap()
67 {
68         throw logic_error("VulkanTexture::generate_mipmap is unimplemented");
69 }
70
71 void VulkanTexture::set_debug_name(const string &name)
72 {
73 #ifdef DEBUG
74         debug_name = name;
75         if(handle)
76                 set_vulkan_object_names();
77 #else
78         (void)name;
79 #endif
80 }
81
82 void VulkanTexture::set_vulkan_object_names() const
83 {
84 #ifdef DEBUG
85         const VulkanFunctions &vk = device.get_functions();
86
87         string view_name = debug_name+"/view";
88         VkDebugUtilsObjectNameInfoEXT name_info = { };
89         name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
90         name_info.objectType = VK_OBJECT_TYPE_IMAGE_VIEW;
91         name_info.objectHandle = reinterpret_cast<uint64_t>(view_handle);
92         name_info.pObjectName = view_name.c_str();
93         vk.SetDebugUtilsObjectName(name_info);
94 #endif
95 }
96
97 } // namespace GL
98 } // namespace Msp