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);
+ 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");
}
- finalize(mod, transient);
+ finalize(*final_module, transient);
- if(mod.get_format()==Module::SPIR_V)
+ if(final_module->get_format()==Module::SPIR_V)
{
- collect_uniforms(static_cast<const SpirVModule &>(mod));
- collect_attributes(static_cast<const SpirVModule &>(mod));
- collect_builtins(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);
}
finalize_uniforms();
+ reflect_data.update_used_bindings();
+
for(const ReflectData::UniformInfo &u: reflect_data.uniforms)
require_type(u.type);
for(const ReflectData::AttributeInfo &a: reflect_data.attributes)
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.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;
if(v.storage==SpirVModule::PUSH_CONSTANT)
info.bind_point = ReflectData::PUSH_CONSTANT;
else
{
- info.bind_point = v.binding;
- info.descriptor_set = v.descriptor_set;
+ 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>());
+ 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.descriptor_set = v.descriptor_set;
+ 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();
}
{
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;
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;
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);