]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/pipelinecache.cpp
Initial implementation of Vulkan backend
[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 is_cleared, bool to_present)
48 {
49         const VulkanFunctions &vk = device.get_functions();
50
51         uint64_t key = hash<64>(static_cast<uint8_t>(is_cleared | (to_present*2)));
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                 attachments[i].format = static_cast<VkFormat>(get_vulkan_pixelformat(get_attachment_pixelformat(a)));
71                 attachments[i].samples = vk_samples;
72                 attachments[i].loadOp = (is_cleared ? VK_ATTACHMENT_LOAD_OP_CLEAR : VK_ATTACHMENT_LOAD_OP_LOAD);
73                 attachments[i].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
74                 attachments[i].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
75                 attachments[i].stencilStoreOp = VK_ATTACHMENT_STORE_OP_STORE;
76                 attachments[i].initialLayout = (is_cleared ? VK_IMAGE_LAYOUT_UNDEFINED : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
77                 attachments[i].finalLayout = (to_present ? VK_IMAGE_LAYOUT_PRESENT_SRC_KHR : VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
78
79                 unsigned attach_pt = get_attach_point(a);
80                 if(attach_pt==get_attach_point(COLOR_ATTACHMENT))
81                 {
82                         color_refs[color_count].attachment = i;
83                         color_refs[color_count].layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
84                         ++color_count;
85                 }
86                 else if(attach_pt==get_attach_point(DEPTH_ATTACHMENT))
87                 {
88                         depth_stencil_ref.attachment = i;
89                         depth_stencil_ref.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
90                 }
91
92                 ++i;
93         }
94
95         VkSubpassDescription subpass = { };
96         subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
97         subpass.colorAttachmentCount = color_count;
98         subpass.pColorAttachments = color_refs;
99         subpass.pDepthStencilAttachment = &depth_stencil_ref;
100
101         VkRenderPassCreateInfo render_pass_info = { };
102         render_pass_info.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
103         render_pass_info.attachmentCount = format.size();
104         render_pass_info.pAttachments = attachments;
105         render_pass_info.subpassCount = 1;
106         render_pass_info.pSubpasses = &subpass;
107
108         VkRenderPass render_pass;
109         vk.CreateRenderPass(render_pass_info, render_pass);
110
111         render_passes.insert(make_pair(key, render_pass));
112
113         return render_pass;
114 }
115
116 VkPipeline PipelineCache::get_pipeline(const PipelineState &ps)
117 {
118         const VulkanFunctions &vk = device.get_functions();
119
120         uint64_t key = ps.compute_hash();
121         auto i = pipelines.find(key);
122         if(i!=pipelines.end())
123                 return i->second;
124
125         vector<char> buffer;
126         ps.fill_creation_info(buffer);
127         const VkGraphicsPipelineCreateInfo *creation_info = reinterpret_cast<const VkGraphicsPipelineCreateInfo *>(buffer.data());
128
129         VkPipeline pipeline;
130         vk.CreateGraphicsPipelines(0, 1, creation_info, &pipeline);
131
132         pipelines.insert(make_pair(key, pipeline));
133
134         return pipeline;
135 }
136
137 VkDescriptorSet PipelineCache::get_descriptor_set(const PipelineState &ps, unsigned index)
138 {
139         const VulkanFunctions &vk = device.get_functions();
140
141         uint64_t key = ps.compute_descriptor_set_hash(index);
142         auto i = descriptor_sets.find(key);
143         if(i!=descriptor_sets.end())
144                 return i->second;
145
146         VkDescriptorSetLayout layout = ps.get_descriptor_set_layout(index);
147
148         VkDescriptorSetAllocateInfo alloc_info = { };
149         alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
150         alloc_info.descriptorPool = handle_cast<::VkDescriptorPool>(descriptor_pool);
151         alloc_info.descriptorSetCount = 1;
152         alloc_info.pSetLayouts = handle_cast<::VkDescriptorSetLayout *>(&layout);
153
154         VkDescriptorSet desc_set;
155         vk.AllocateDescriptorSets(alloc_info, &desc_set);
156
157         vector<char> buffer;
158         unsigned n_writes = ps.fill_descriptor_writes(index, buffer);
159         VkWriteDescriptorSet *writes = reinterpret_cast<VkWriteDescriptorSet *>(buffer.data());
160         for(unsigned j=0; j<n_writes; ++j)
161                 writes[j].dstSet = handle_cast<::VkDescriptorSet>(desc_set);
162
163         vk.UpdateDescriptorSets(n_writes, writes, 0, 0);
164
165         descriptor_sets.insert(make_pair(key, desc_set));
166
167         return desc_set;
168 }
169
170 } // namespace GL
171 } // namespace Msp