]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/pipelinecache.cpp
Only do layout transitions in render pass for swapchain images
[libs/gl.git] / source / backends / vulkan / pipelinecache.cpp
1 #include <msp/core/hash.h>
2 #include "blend.h"
3 #include "depthtest.h"
4 #include "device.h"
5 #include "framebuffer.h"
6 #include "pipelinecache.h"
7 #include "pipelinestate.h"
8 #include "stenciltest.h"
9 #include "vulkan.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 PipelineCache::PipelineCache(Device &d):
17         device(d)
18 {
19         const VulkanFunctions &vk = device.get_functions();
20
21         VkDescriptorPoolSize pool_sizes[2] = { };
22         pool_sizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
23         pool_sizes[0].descriptorCount = 10000;
24         pool_sizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
25         pool_sizes[1].descriptorCount = 10000;
26
27         VkDescriptorPoolCreateInfo pool_info = { };
28         pool_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
29         pool_info.maxSets = 10000;
30         pool_info.poolSizeCount = 2;
31         pool_info.pPoolSizes = pool_sizes;
32
33         vk.CreateDescriptorPool(pool_info, descriptor_pool);
34 }
35
36 PipelineCache::~PipelineCache()
37 {
38         const VulkanFunctions &vk = device.get_functions();
39
40         for(const auto &kvp: render_passes)
41                 vk.DestroyRenderPass(kvp.second);
42         for(const auto &kvp: pipelines)
43                 vk.DestroyPipeline(kvp.second);
44         vk.DestroyDescriptorPool(descriptor_pool);
45 }
46
47 VkRenderPass PipelineCache::get_render_pass(const FrameFormat &format, bool clear, bool discard, bool to_present)
48 {
49         const VulkanFunctions &vk = device.get_functions();
50
51         uint64_t key = hash<64>(static_cast<uint8_t>(clear | (discard*2) | (to_present*4)));
52         for(FrameAttachment a: format)
53                 key = hash_update<64>(key, a);
54
55         auto j = render_passes.find(key);
56         if(j!=render_passes.end())
57                 return j->second;
58
59         VkAttachmentDescription attachments[FrameFormat::MAX_ATTACHMENTS] = { };
60         VkAttachmentReference color_refs[FrameFormat::MAX_ATTACHMENTS] = { };
61         VkAttachmentReference depth_stencil_ref = { };
62         depth_stencil_ref.attachment = VK_ATTACHMENT_UNUSED;
63
64         VkSampleCountFlagBits vk_samples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
65
66         unsigned i = 0;
67         unsigned color_count = 0;
68         for(FrameAttachment a: format)
69         {
70                 VkImageLayout subpass_layout = static_cast<VkImageLayout>(get_vulkan_attachment_layout(get_components(get_attachment_pixelformat(a))));
71                 VkImageLayout external_layout = (to_present ? VK_IMAGE_LAYOUT_PRESENT_SRC_KHR : subpass_layout);
72
73                 attachments[i].format = static_cast<VkFormat>(get_vulkan_pixelformat(get_attachment_pixelformat(a)));
74                 attachments[i].samples = vk_samples;
75                 attachments[i].loadOp = (clear ? discard ? VK_ATTACHMENT_LOAD_OP_DONT_CARE : VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
76                 attachments[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
77                 attachments[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
78                 attachments[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
79                 attachments[i].initialLayout = (clear ? VK_IMAGE_LAYOUT_UNDEFINED : external_layout);
80                 attachments[i].finalLayout = external_layout;
81
82                 if(subpass_layout==VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)
83                 {
84                         color_refs[color_count].attachment = i;
85                         color_refs[color_count].layout = subpass_layout;
86                         ++color_count;
87                 }
88                 else
89                 {
90                         depth_stencil_ref.attachment = i;
91                         depth_stencil_ref.layout = subpass_layout;
92                 }
93
94                 ++i;
95         }
96
97         VkSubpassDescription subpass = { };
98         subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
99         subpass.colorAttachmentCount = color_count;
100         subpass.pColorAttachments = color_refs;
101         subpass.pDepthStencilAttachment = &depth_stencil_ref;
102
103         VkRenderPassCreateInfo render_pass_info = { };
104         render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
105         render_pass_info.attachmentCount = format.size();
106         render_pass_info.pAttachments = attachments;
107         render_pass_info.subpassCount = 1;
108         render_pass_info.pSubpasses = &subpass;
109
110         VkRenderPass render_pass;
111         vk.CreateRenderPass(render_pass_info, render_pass);
112
113         render_passes.insert(make_pair(key, render_pass));
114
115         return render_pass;
116 }
117
118 VkPipeline PipelineCache::get_pipeline(const PipelineState &ps)
119 {
120         const VulkanFunctions &vk = device.get_functions();
121
122         uint64_t key = ps.compute_hash();
123         auto i = pipelines.find(key);
124         if(i!=pipelines.end())
125                 return i->second;
126
127         vector<char> buffer;
128         ps.fill_creation_info(buffer);
129         const VkGraphicsPipelineCreateInfo *creation_info = reinterpret_cast<const VkGraphicsPipelineCreateInfo *>(buffer.data());
130
131         VkPipeline pipeline;
132         vk.CreateGraphicsPipelines(0, 1, creation_info, &pipeline);
133
134         pipelines.insert(make_pair(key, pipeline));
135
136         return pipeline;
137 }
138
139 VkDescriptorSet PipelineCache::get_descriptor_set(const PipelineState &ps, unsigned index)
140 {
141         const VulkanFunctions &vk = device.get_functions();
142
143         uint64_t key = ps.compute_descriptor_set_hash(index);
144         auto i = descriptor_sets.find(key);
145         if(i!=descriptor_sets.end())
146                 return i->second;
147
148         VkDescriptorSetLayout layout = ps.get_descriptor_set_layout(index);
149
150         VkDescriptorSetAllocateInfo alloc_info = { };
151         alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
152         alloc_info.descriptorPool = handle_cast<::VkDescriptorPool>(descriptor_pool);
153         alloc_info.descriptorSetCount = 1;
154         alloc_info.pSetLayouts = handle_cast<::VkDescriptorSetLayout *>(&layout);
155
156         VkDescriptorSet desc_set;
157         vk.AllocateDescriptorSets(alloc_info, &desc_set);
158
159         vector<char> buffer;
160         unsigned n_writes = ps.fill_descriptor_writes(index, buffer);
161         VkWriteDescriptorSet *writes = reinterpret_cast<VkWriteDescriptorSet *>(buffer.data());
162         for(unsigned j=0; j<n_writes; ++j)
163                 writes[j].dstSet = handle_cast<::VkDescriptorSet>(desc_set);
164
165         vk.UpdateDescriptorSets(n_writes, writes, 0, 0);
166
167         descriptor_sets.insert(make_pair(key, desc_set));
168
169         return desc_set;
170 }
171
172 } // namespace GL
173 } // namespace Msp