]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/framebuffer_backend.cpp
52857351c3e3237bd72425764ef4d073f4b8767f
[libs/gl.git] / source / backends / vulkan / framebuffer_backend.cpp
1 #include <msp/strings/format.h>
2 #include "device.h"
3 #include "framebuffer.h"
4 #include "framebuffer_backend.h"
5 #include "vulkan.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 VulkanFramebuffer::VulkanFramebuffer(bool):
13         device(Device::get_current())
14 { }
15
16 VulkanFramebuffer::VulkanFramebuffer(VulkanFramebuffer &&other):
17         device(other.device),
18         handle(other.handle),
19         debug_name(move(other.debug_name))
20 {
21         other.handle = 0;
22 }
23
24 VulkanFramebuffer::~VulkanFramebuffer()
25 {
26         if(handle)
27                 device.get_destroy_queue().destroy(handle);
28 }
29
30 bool VulkanFramebuffer::is_format_supported(const FrameFormat &fmt)
31 {
32         const VulkanFunctions &vk = device.get_functions();
33         for(FrameAttachment a: fmt)
34         {
35                 PixelFormat pf = get_attachment_pixelformat(a);
36                 PixelComponents comp = get_components(pf);
37                 VkFormatProperties props;
38                 vk.GetPhysicalDeviceFormatProperties(static_cast<VkFormat>(get_vulkan_pixelformat(pf)), props);
39                 if(comp==DEPTH_COMPONENT || comp==STENCIL_INDEX)
40                 {
41                         if(!(props.optimalTilingFeatures&VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT))
42                                 return false;
43                 }
44                 else if(!(props.optimalTilingFeatures&VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BLEND_BIT))
45                         return false;
46         }
47
48         return true;
49 }
50
51 const Texture *VulkanFramebuffer::get_attachment(unsigned i) const
52 {
53         return static_cast<const Framebuffer *>(this)->attachments[i].tex;
54 }
55
56 void VulkanFramebuffer::update(unsigned) const
57 {
58         const Framebuffer &self = *static_cast<const Framebuffer *>(this);
59         const VulkanFunctions &vk = device.get_functions();
60
61         if(handle)
62                 device.get_destroy_queue().destroy(handle);
63
64         VkImageView vk_attachments[FrameFormat::MAX_ATTACHMENTS] = { };
65         unsigned i = 0;
66         for(const Framebuffer::Attachment &a: self.attachments)
67         {
68                 if(a.tex->view_type!=VK_IMAGE_VIEW_TYPE_2D || a.level || a.layer)
69                         throw logic_error("Unimplemented texture type in VulkanFramebuffer::update");
70                 vk_attachments[i++] = a.tex->view_handle;
71         }
72
73         VkRenderPass render_pass = device.get_pipeline_cache().get_render_pass(self.format, false, false, false);
74
75         VkFramebufferCreateInfo framebuffer_info = { };
76         framebuffer_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
77         framebuffer_info.renderPass = handle_cast<::VkRenderPass>(render_pass);
78         framebuffer_info.attachmentCount = self.format.size();
79         framebuffer_info.pAttachments = handle_cast<::VkImageView *>(vk_attachments);
80         framebuffer_info.width = self.width;
81         framebuffer_info.height = self.height;
82         framebuffer_info.layers = 1;
83
84         vk.CreateFramebuffer(framebuffer_info, handle);
85
86         if(!debug_name.empty())
87                 set_vulkan_object_name();
88 }
89
90 void VulkanFramebuffer::set_debug_name(const string &name)
91 {
92 #ifdef DEBUG
93         debug_name = name;
94         if(handle)
95                 set_vulkan_object_name();
96 #else
97         (void)name;
98 #endif
99 }
100
101 void VulkanFramebuffer::set_vulkan_object_name() const
102 {
103 #ifdef DEBUG
104         const VulkanFunctions &vk = device.get_functions();
105
106         VkDebugUtilsObjectNameInfoEXT name_info = { };
107         name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
108         name_info.objectType = VK_OBJECT_TYPE_FRAMEBUFFER;
109         name_info.objectHandle = reinterpret_cast<uint64_t>(handle);
110         name_info.pObjectName = debug_name.c_str();
111         vk.SetDebugUtilsObjectName(name_info);
112 #endif
113 }
114
115 } // namespace GL
116 } // namespace Msp