]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/program_backend.cpp
Initial implementation of Vulkan backend
[libs/gl.git] / source / backends / vulkan / program_backend.cpp
1 #include <cstring>
2 #include <msp/core/algorithm.h>
3 #include "device.h"
4 #include "error.h"
5 #include "program.h"
6 #include "program_backend.h"
7 #include "structurebuilder.h"
8 #include "vulkan.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 VulkanProgram::VulkanProgram():
16         device(Device::get_current())
17 { }
18
19 VulkanProgram::VulkanProgram(VulkanProgram &&other):
20         device(other.device),
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)
25 {
26         other.desc_set_layout_handles.clear();
27         other.layout_handle = 0;
28 }
29
30 VulkanProgram::~VulkanProgram()
31 {
32         const VulkanFunctions &vk = device.get_functions();
33
34         if(layout_handle)
35                 vk.DestroyPipelineLayout(layout_handle);
36         for(VkDescriptorSetLayout d: desc_set_layout_handles)
37                 vk.DestroyDescriptorSetLayout(d);
38 }
39
40 bool VulkanProgram::has_stages() const
41 {
42         return n_stages;
43 }
44
45 void VulkanProgram::add_glsl_stages(const GlslModule &, const map<string, int> &, TransientData &)
46 {
47         throw invalid_operation("VulkanProgram::add_glsl_stages");
48 }
49
50 void VulkanProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
51 {
52         const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
53
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;
58
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());
65
66         unsigned i = 0;
67         for(const SpirVModule::Constant &c: mod.get_spec_constants())
68         {
69                 auto j = spec_values.find(c.name);
70                 if(j!=spec_values.end())
71                 {
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;
76                         ++i;
77                 }
78         }
79
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;
84
85         char *name_ptr = name_table;
86         i = 0;
87         for(const SpirVModule::EntryPoint &e: entry_points)
88         {
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;
96                 ++i;
97         }
98 }
99
100 void VulkanProgram::finalize_uniforms()
101 {
102         const VulkanFunctions &vk = device.get_functions();
103         const ReflectData &rd = static_cast<const Program *>(this)->reflect_data;
104
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);
107
108         desc_set_layout_handles.resize(rd.n_descriptor_sets);
109         for(unsigned j=0; j<rd.n_descriptor_sets; ++j)
110         {
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)
114                         {
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;
122                         }
123
124                 for(const ReflectData::UniformInfo &u: rd.uniforms)
125                         if(u.binding>=0 && static_cast<unsigned>(u.binding>>20)==j && is_image(u.type))
126                         {
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;
134                         }
135
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();
140
141                 vk.CreateDescriptorSetLayout(set_layout_info, desc_set_layout_handles[j]);
142         }
143
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);
148
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;
155
156         vk.CreatePipelineLayout(layout_info, layout_handle);
157 }
158
159 } // namespace GL
160 } // namespace Msp