2 #include <msp/core/algorithm.h>
6 #include "program_backend.h"
7 #include "structurebuilder.h"
15 VulkanProgram::VulkanProgram():
16 device(Device::get_current())
19 VulkanProgram::VulkanProgram(VulkanProgram &&other):
21 n_stages(other.n_stages),
22 creation_info(move(other.creation_info)),
23 desc_set_layout_handles(move(other.desc_set_layout_handles)),
24 layout_handle(other.layout_handle)
26 other.desc_set_layout_handles.clear();
27 other.layout_handle = 0;
30 VulkanProgram::~VulkanProgram()
32 const VulkanFunctions &vk = device.get_functions();
35 vk.DestroyPipelineLayout(layout_handle);
36 for(VkDescriptorSetLayout d: desc_set_layout_handles)
37 vk.DestroyDescriptorSetLayout(d);
40 bool VulkanProgram::has_stages() const
45 void VulkanProgram::add_glsl_stages(const GlslModule &, const map<string, int> &, TransientData &)
47 throw invalid_operation("VulkanProgram::add_glsl_stages");
50 void VulkanProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
52 const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
54 n_stages = entry_points.size();
55 size_t entry_names_size = 0;
56 for(const SpirVModule::EntryPoint &e: entry_points)
57 entry_names_size += e.name.size()+1;
59 StructureBuilder sb(creation_info, 5);
60 VkPipelineShaderStageCreateInfo *&stage_infos = sb.add<VkPipelineShaderStageCreateInfo>(n_stages);
61 char *&name_table = sb.add<char>(entry_names_size);
62 VkSpecializationInfo *&spec_info = sb.add<VkSpecializationInfo>();
63 VkSpecializationMapEntry *&spec_map = sb.add<VkSpecializationMapEntry>(spec_values.size());
64 int *&spec_data = sb.add<int>(spec_values.size());
67 for(const SpirVModule::Constant &c: mod.get_spec_constants())
69 auto j = spec_values.find(c.name);
70 if(j!=spec_values.end())
72 spec_map[i].constantID = c.constant_id;
73 spec_map[i].offset = i*sizeof(int);
74 spec_map[i].size = sizeof(int);
75 spec_data[i] = j->second;
80 spec_info->mapEntryCount = i;
81 spec_info->pMapEntries = spec_map;
82 spec_info->dataSize = spec_values.size()*sizeof(int);
83 spec_info->pData = spec_data;
85 char *name_ptr = name_table;
87 for(const SpirVModule::EntryPoint &e: entry_points)
89 stage_infos[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
90 stage_infos[i].stage = static_cast<VkShaderStageFlagBits>(get_vulkan_stage(e.stage));
91 stage_infos[i].module = handle_cast<::VkShaderModule>(mod.handle);
92 strcpy(name_ptr, e.name.c_str());
93 stage_infos[i].pName = name_ptr;
94 name_ptr += e.name.size()+1;
95 stage_infos[i].pSpecializationInfo = spec_info;
100 void VulkanProgram::finalize_uniforms()
102 const VulkanFunctions &vk = device.get_functions();
103 const ReflectData &rd = static_cast<const Program *>(this)->reflect_data;
105 auto i = find_member(rd.uniform_blocks, static_cast<int>(ReflectData::PUSH_CONSTANT), &ReflectData::UniformBlockInfo::bind_point);
106 const ReflectData::UniformBlockInfo *push_const_block = (i!=rd.uniform_blocks.end() ? &*i : 0);
108 desc_set_layout_handles.resize(rd.n_descriptor_sets);
109 for(unsigned j=0; j<rd.n_descriptor_sets; ++j)
111 std::vector<VkDescriptorSetLayoutBinding> bindings;
112 for(const ReflectData::UniformBlockInfo &b: rd.uniform_blocks)
113 if(b.bind_point>=0 && static_cast<unsigned>(b.bind_point>>20)==j)
115 bindings.emplace_back();
116 VkDescriptorSetLayoutBinding &binding = bindings.back();
117 binding.binding = b.bind_point;
118 binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
119 binding.descriptorCount = 1;
120 binding.stageFlags = VK_SHADER_STAGE_ALL;
121 binding.pImmutableSamplers = 0;
124 for(const ReflectData::UniformInfo &u: rd.uniforms)
125 if(u.binding>=0 && static_cast<unsigned>(u.binding>>20)==j && is_image(u.type))
127 bindings.emplace_back();
128 VkDescriptorSetLayoutBinding &binding = bindings.back();
129 binding.binding = u.binding;
130 binding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
131 binding.descriptorCount = 1;
132 binding.stageFlags = VK_SHADER_STAGE_ALL;
133 binding.pImmutableSamplers = 0;
136 VkDescriptorSetLayoutCreateInfo set_layout_info = { };
137 set_layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
138 set_layout_info.bindingCount = bindings.size();
139 set_layout_info.pBindings = bindings.data();
141 vk.CreateDescriptorSetLayout(set_layout_info, desc_set_layout_handles[j]);
144 VkPushConstantRange push_const_range = { };
145 push_const_range.stageFlags = VK_SHADER_STAGE_ALL;
146 push_const_range.offset = 0;
147 push_const_range.size = (push_const_block ? push_const_block->data_size : 0);
149 VkPipelineLayoutCreateInfo layout_info = { };
150 layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
151 layout_info.setLayoutCount = rd.n_descriptor_sets;
152 layout_info.pSetLayouts = handle_cast<::VkDescriptorSetLayout *>(desc_set_layout_handles.data());
153 layout_info.pushConstantRangeCount = (push_const_block ? 1 : 0);
154 layout_info.pPushConstantRanges = &push_const_range;
156 vk.CreatePipelineLayout(layout_info, layout_handle);