1 #include <msp/core/hash.h>
5 #include "framebuffer.h"
6 #include "pipelinecache.h"
7 #include "pipelinestate.h"
8 #include "stenciltest.h"
16 PipelineCache::PipelineCache(Device &d):
19 const VulkanFunctions &vk = device.get_functions();
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;
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;
33 vk.CreateDescriptorPool(pool_info, descriptor_pool);
36 PipelineCache::~PipelineCache()
38 const VulkanFunctions &vk = device.get_functions();
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);
47 VkRenderPass PipelineCache::get_render_pass(const FrameFormat &format, bool clear, bool discard, bool to_present)
49 const VulkanFunctions &vk = device.get_functions();
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);
55 auto j = render_passes.find(key);
56 if(j!=render_passes.end())
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;
64 VkSampleCountFlagBits vk_samples = static_cast<VkSampleCountFlagBits>(get_vulkan_samples(format.get_samples()));
67 unsigned color_count = 0;
68 for(FrameAttachment a: format)
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);
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;
82 if(subpass_layout==VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL)
84 color_refs[color_count].attachment = i;
85 color_refs[color_count].layout = subpass_layout;
90 depth_stencil_ref.attachment = i;
91 depth_stencil_ref.layout = subpass_layout;
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;
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;
110 VkRenderPass render_pass;
111 vk.CreateRenderPass(render_pass_info, render_pass);
113 render_passes.insert(make_pair(key, render_pass));
118 VkPipeline PipelineCache::get_pipeline(const PipelineState &ps)
120 const VulkanFunctions &vk = device.get_functions();
122 uint64_t key = ps.compute_hash();
123 auto i = pipelines.find(key);
124 if(i!=pipelines.end())
128 ps.fill_creation_info(buffer);
129 const VkGraphicsPipelineCreateInfo *creation_info = reinterpret_cast<const VkGraphicsPipelineCreateInfo *>(buffer.data());
132 vk.CreateGraphicsPipelines(0, 1, creation_info, &pipeline);
134 pipelines.insert(make_pair(key, pipeline));
139 VkDescriptorSet PipelineCache::get_descriptor_set(const PipelineState &ps, unsigned index)
141 const VulkanFunctions &vk = device.get_functions();
143 uint64_t key = ps.compute_descriptor_set_hash(index);
144 auto i = descriptor_sets.find(key);
145 if(i!=descriptor_sets.end())
148 VkDescriptorSetLayout layout = ps.get_descriptor_set_layout(index);
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);
156 VkDescriptorSet desc_set;
157 vk.AllocateDescriptorSets(alloc_info, &desc_set);
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);
165 vk.UpdateDescriptorSets(n_writes, writes, 0, 0);
167 descriptor_sets.insert(make_pair(key, desc_set));