]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/program_backend.cpp
Mask out the descriptor set part of bind point when creating layouts
[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         stage_flags(other.stage_flags),
23         creation_info(move(other.creation_info)),
24         desc_set_layout_handles(move(other.desc_set_layout_handles)),
25         layout_handle(other.layout_handle),
26         debug_name(move(other.debug_name))
27 {
28         other.desc_set_layout_handles.clear();
29         other.layout_handle = 0;
30 }
31
32 VulkanProgram::~VulkanProgram()
33 {
34         const VulkanFunctions &vk = device.get_functions();
35
36         if(layout_handle)
37                 vk.DestroyPipelineLayout(layout_handle);
38         for(VkDescriptorSetLayout d: desc_set_layout_handles)
39                 vk.DestroyDescriptorSetLayout(d);
40 }
41
42 bool VulkanProgram::has_stages() const
43 {
44         return n_stages;
45 }
46
47 void VulkanProgram::add_glsl_stages(const GlslModule &, const map<string, int> &)
48 {
49         throw invalid_operation("VulkanProgram::add_glsl_stages");
50 }
51
52 void VulkanProgram::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
53 {
54         const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
55
56         n_stages = entry_points.size();
57         size_t entry_names_size = 0;
58         for(const SpirVModule::EntryPoint &e: entry_points)
59                 entry_names_size += e.name.size()+1;
60
61         StructureBuilder sb(creation_info, 5);
62         VkPipelineShaderStageCreateInfo *&stage_infos = sb.add<VkPipelineShaderStageCreateInfo>(n_stages);
63         char *&name_table = sb.add<char>(entry_names_size);
64         VkSpecializationInfo *&spec_info = sb.add<VkSpecializationInfo>();
65         VkSpecializationMapEntry *&spec_map = sb.add<VkSpecializationMapEntry>(spec_values.size());
66         int *&spec_data = sb.add<int>(spec_values.size());
67
68         unsigned i = 0;
69         for(const SpirVModule::Constant &c: mod.get_spec_constants())
70         {
71                 auto j = spec_values.find(c.name);
72                 if(j!=spec_values.end())
73                 {
74                         spec_map[i].constantID = c.constant_id;
75                         spec_map[i].offset = i*sizeof(int);
76                         spec_map[i].size = sizeof(int);
77                         spec_data[i] = j->second;
78                         ++i;
79                 }
80         }
81
82         spec_info->mapEntryCount = i;
83         spec_info->pMapEntries = spec_map;
84         spec_info->dataSize = spec_values.size()*sizeof(int);
85         spec_info->pData = spec_data;
86
87         char *name_ptr = name_table;
88         i = 0;
89         for(const SpirVModule::EntryPoint &e: entry_points)
90         {
91                 unsigned stage_bit = get_vulkan_stage(e.stage);
92                 stage_flags |= stage_bit;
93
94                 stage_infos[i].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
95                 stage_infos[i].stage = static_cast<VkShaderStageFlagBits>(stage_bit);
96                 stage_infos[i].module = handle_cast<::VkShaderModule>(mod.handle);
97                 strcpy(name_ptr, e.name.c_str());
98                 stage_infos[i].pName = name_ptr;
99                 name_ptr += e.name.size()+1;
100                 stage_infos[i].pSpecializationInfo = spec_info;
101                 ++i;
102         }
103
104 #if DEBUG
105         if(!debug_name.empty())
106                 if(SpirVModule *spirv = static_cast<Program *>(this)->specialized_spirv)
107                         spirv->set_debug_name(debug_name);
108 #endif
109 }
110
111 void VulkanProgram::finalize_uniforms()
112 {
113         const VulkanFunctions &vk = device.get_functions();
114         const ReflectData &rd = static_cast<const Program *>(this)->reflect_data;
115
116         auto i = find_member(rd.uniform_blocks, static_cast<int>(ReflectData::PUSH_CONSTANT), &ReflectData::UniformBlockInfo::bind_point);
117         const ReflectData::UniformBlockInfo *push_const_block = (i!=rd.uniform_blocks.end() ? &*i : 0);
118
119         desc_set_layout_handles.resize(rd.n_descriptor_sets);
120         for(unsigned j=0; j<rd.n_descriptor_sets; ++j)
121         {
122                 std::vector<VkDescriptorSetLayoutBinding> bindings;
123                 for(const ReflectData::UniformBlockInfo &b: rd.uniform_blocks)
124                         if(b.bind_point>=0 && static_cast<unsigned>(b.bind_point>>20)==j)
125                         {
126                                 bindings.emplace_back();
127                                 VkDescriptorSetLayoutBinding &binding = bindings.back();
128                                 binding.binding = b.bind_point&0xFFFFF;
129                                 binding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
130                                 binding.descriptorCount = 1;
131                                 binding.stageFlags = VK_SHADER_STAGE_ALL;
132                                 binding.pImmutableSamplers = 0;
133                         }
134
135                 for(const ReflectData::UniformInfo &u: rd.uniforms)
136                         if(u.binding>=0 && static_cast<unsigned>(u.binding>>20)==j && is_image(u.type))
137                         {
138                                 bindings.emplace_back();
139                                 VkDescriptorSetLayoutBinding &binding = bindings.back();
140                                 binding.binding = u.binding&0xFFFFF;
141                                 binding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
142                                 binding.descriptorCount = 1;
143                                 binding.stageFlags = VK_SHADER_STAGE_ALL;
144                                 binding.pImmutableSamplers = 0;
145                         }
146
147                 VkDescriptorSetLayoutCreateInfo set_layout_info = { };
148                 set_layout_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
149                 set_layout_info.bindingCount = bindings.size();
150                 set_layout_info.pBindings = bindings.data();
151
152                 vk.CreateDescriptorSetLayout(set_layout_info, desc_set_layout_handles[j]);
153         }
154
155         VkPushConstantRange push_const_range = { };
156         push_const_range.stageFlags = stage_flags;
157         push_const_range.offset = 0;
158         push_const_range.size = (push_const_block ? push_const_block->data_size : 0);
159
160         VkPipelineLayoutCreateInfo layout_info = { };
161         layout_info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
162         layout_info.setLayoutCount = rd.n_descriptor_sets;
163         layout_info.pSetLayouts = handle_cast<::VkDescriptorSetLayout *>(desc_set_layout_handles.data());
164         layout_info.pushConstantRangeCount = (push_const_block ? 1 : 0);
165         layout_info.pPushConstantRanges = &push_const_range;
166
167         vk.CreatePipelineLayout(layout_info, layout_handle);
168 }
169
170 void VulkanProgram::set_debug_name(const string &name)
171 {
172 #ifdef DEBUG
173         debug_name = name;
174         if(SpirVModule *spirv = static_cast<Program *>(this)->specialized_spirv)
175                 spirv->set_debug_name(debug_name);
176 #else
177         (void)name;
178 #endif
179 }
180
181 } // namespace GL
182 } // namespace Msp