add_stages(mod, spec_values);
}
+Program::Program(Program &&other):
+ ProgramBackend(move(other)),
+ reflect_data(move(other.reflect_data)),
+ specialized_spirv(other.specialized_spirv)
+{
+ other.specialized_spirv = 0;
+}
+
+Program::~Program()
+{
+ delete specialized_spirv;
+}
+
void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
{
if(has_stages())
throw invalid_operation("Program::add_stages");
+ reflect_data = ReflectData();
+ const Module *final_module = &mod;
+
TransientData transient;
switch(mod.get_format())
{
add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values, transient);
break;
case Module::SPIR_V:
- add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values, transient);
+ if(!spec_values.empty())
+ {
+ specialized_spirv = static_cast<const SpirVModule &>(mod).specialize(spec_values);
+ final_module = specialized_spirv;
+ }
+ add_spirv_stages(*static_cast<const SpirVModule *>(final_module), spec_values);
break;
default:
throw invalid_argument("Program::add_stages");
}
- reflect_data = ReflectData();
-
- finalize(mod);
+ finalize(*final_module, transient);
- if(mod.get_format()==Module::GLSL)
+ if(final_module->get_format()==Module::SPIR_V)
{
- query_uniforms();
- query_attributes();
- apply_bindings(transient);
- }
- else if(mod.get_format()==Module::SPIR_V)
- {
- collect_uniforms(static_cast<const SpirVModule &>(mod), transient.spec_values);
- collect_attributes(static_cast<const SpirVModule &>(mod));
+ const SpirVModule &spirv_mod = *static_cast<const SpirVModule *>(final_module);
+ collect_uniforms(spirv_mod);
+ collect_attributes(spirv_mod);
+ collect_builtins(spirv_mod);
}
- for(ReflectData::UniformBlockInfo &b: reflect_data.uniform_blocks)
- if(!b.data_size && !b.uniforms.empty())
- {
- const ReflectData::UniformInfo &uni = *b.uniforms.back();
- b.data_size = uni.location*16+uni.array_size*get_type_size(uni.type);
- }
+ finalize_uniforms();
+
+ for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
+ if(u.binding>=0)
+ reflect_data.used_bindings.push_back(u.binding);
+ for(const ReflectData::UniformBlockInfo &b: reflect_data.uniform_blocks)
+ reflect_data.used_bindings.push_back(b.bind_point);
+ sort(reflect_data.used_bindings);
for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
require_type(u.type);
require_type(a.type);
}
-void Program::collect_uniforms(const SpirVModule &mod, const map<unsigned, int> &spec_values)
+void Program::collect_uniforms(const SpirVModule &mod)
{
// Prepare the default block
- reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
+ reflect_data.uniform_blocks.emplace_back();
vector<vector<string> > block_uniform_names(1);
+ unsigned n_descriptor_sets = 0;
for(const SpirVModule::Variable &v: mod.get_variables())
{
- if(v.storage==SpirVModule::UNIFORM && v.struct_type)
+ if((v.storage==SpirVModule::UNIFORM || v.storage==SpirVModule::PUSH_CONSTANT) && v.struct_type)
{
- reflect_data.uniform_blocks.push_back(ReflectData::UniformBlockInfo());
+ reflect_data.uniform_blocks.emplace_back();
ReflectData::UniformBlockInfo &info = reflect_data.uniform_blocks.back();
info.name = v.struct_type->name;
- info.bind_point = v.binding;
+ if(v.storage==SpirVModule::PUSH_CONSTANT)
+ info.bind_point = ReflectData::PUSH_CONSTANT;
+ else
+ {
+ if(v.binding>=0)
+ info.bind_point = v.binding | (v.descriptor_set<<20);
+ else
+ info.bind_point = ReflectData::DEFAULT_BLOCK;
+ n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
+ }
info.data_size = v.struct_type->size;
string prefix;
if(!v.name.empty())
prefix = v.struct_type->name+".";
- block_uniform_names.push_back(vector<string>());
- collect_block_uniforms(*v.struct_type, prefix, 0, spec_values, block_uniform_names.back());
+ block_uniform_names.emplace_back();
+ collect_block_uniforms(*v.struct_type, prefix, 0, block_uniform_names.back());
}
- else if(v.storage==SpirVModule::UNIFORM_CONSTANT && v.location>=0)
+ else if(v.storage==SpirVModule::UNIFORM_CONSTANT && (v.location>=0 || v.binding>=0))
{
block_uniform_names[0].push_back(v.name);
- reflect_data.uniforms.push_back(ReflectData::UniformInfo());
+ reflect_data.uniforms.emplace_back();
ReflectData::UniformInfo &info = reflect_data.uniforms.back();
info.name = v.name;
info.tag = v.name;
info.location = v.location;
- info.binding = v.binding;
- info.array_size = v.array_size;
+ if(v.binding>=0)
+ info.binding = v.binding | (v.descriptor_set<<20);
+ n_descriptor_sets = max(n_descriptor_sets, v.descriptor_set+1);
+ info.array_size = max(v.array_size, 1U);
info.type = v.type;
}
}
sort_member(reflect_data.uniforms, &ReflectData::UniformInfo::tag);
+ if(block_uniform_names.front().empty())
+ {
+ reflect_data.uniform_blocks.erase(reflect_data.uniform_blocks.begin());
+ block_uniform_names.erase(block_uniform_names.begin());
+ }
+
for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
{
ReflectData::UniformBlockInfo &block = reflect_data.uniform_blocks[i];
block.update_layout_hash();
}
+ reflect_data.n_descriptor_sets = n_descriptor_sets;
reflect_data.update_layout_hash();
}
-void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, const map<unsigned, int> &spec_values, vector<string> &uniform_names)
+void Program::collect_block_uniforms(const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset, vector<string> &uniform_names)
{
for(const SpirVModule::StructMember &m: strct.members)
{
unsigned offset = base_offset+m.offset;
if(m.struct_type)
{
- unsigned array_size = m.array_size;
- if(m.array_size_spec)
+ if(m.array_size)
{
- array_size = m.array_size_spec->i_value;
- auto j = spec_values.find(m.array_size_spec->constant_id);
- if(j!=spec_values.end())
- array_size = j->second;
- }
-
- if(array_size)
- {
- for(unsigned j=0; j<array_size; ++j, offset+=m.array_stride)
- collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, spec_values, uniform_names);
+ for(unsigned j=0; j<m.array_size; ++j, offset+=m.array_stride)
+ collect_block_uniforms(*m.struct_type, format("%s%s[%d].", prefix, m.name, j), offset, uniform_names);
}
else
- collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, spec_values, uniform_names);
+ collect_block_uniforms(*m.struct_type, prefix+m.name+".", offset, uniform_names);
}
else
{
string name = prefix+m.name;
uniform_names.push_back(name);
- reflect_data.uniforms.push_back(ReflectData::UniformInfo());
+ reflect_data.uniforms.emplace_back();
ReflectData::UniformInfo &info = reflect_data.uniforms.back();
info.name = name;
info.tag = name;
info.offset = offset;
- info.array_size = m.array_size;
+ info.array_size = max(m.array_size, 1U);
info.array_stride = m.array_stride;
info.matrix_stride = m.matrix_stride;
info.type = m.type;
for(const SpirVModule::Variable *v: e.globals)
if(v->storage==SpirVModule::INPUT)
{
- reflect_data.attributes.push_back(ReflectData::AttributeInfo());
+ reflect_data.attributes.emplace_back();
ReflectData::AttributeInfo &info = reflect_data.attributes.back();
info.name = v->name;
info.location = v->location;
}
}
+void Program::collect_builtins(const SpirVModule &mod)
+{
+ for(const SpirVModule::Variable &v: mod.get_variables())
+ if(v.storage==SpirVModule::OUTPUT && v.struct_type)
+ collect_builtins(*v.struct_type);
+}
+
+void Program::collect_builtins(const SpirVModule::Structure &strct)
+{
+ for(const SpirVModule::StructMember &m: strct.members)
+ if(m.builtin==SpirVModule::CLIP_DISTANCE)
+ reflect_data.n_clip_distances = m.array_size;
+}
+
const ReflectData::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
{
auto i = find_member(reflect_data.uniform_blocks, name, &ReflectData::UniformBlockInfo::name);
return i!=reflect_data.uniforms.end() && i->tag==tag ? i->binding : -1;
}
+bool Program::uses_binding(int binding) const
+{
+ auto i = lower_bound(reflect_data.used_bindings, binding);
+ return i!=reflect_data.used_bindings.end() && *i==binding;
+}
+
const ReflectData::AttributeInfo &Program::get_attribute_info(const string &name) const
{
auto i = lower_bound_member(reflect_data.attributes, name, &ReflectData::AttributeInfo::name);