#include <set>
#include <msp/core/hash.h>
#include <msp/core/maputils.h>
+#include <msp/gl/extensions/arb_es2_compatibility.h>
#include <msp/gl/extensions/arb_fragment_shader.h>
+#include <msp/gl/extensions/arb_gl_spirv.h>
#include <msp/gl/extensions/arb_geometry_shader4.h>
#include <msp/gl/extensions/arb_shader_objects.h>
#include <msp/gl/extensions/arb_uniform_buffer_object.h>
#include "buffer.h"
#include "error.h"
#include "misc.h"
-#include "module.h"
#include "program.h"
#include "resources.h"
#include "shader.h"
switch(mod.get_format())
{
case Module::GLSL: return add_glsl_stages(static_cast<const GlslModule &>(mod), spec_values);
+ case Module::SPIR_V: return add_spirv_stages(static_cast<const SpirVModule &>(mod), spec_values);
default: throw invalid_argument("Program::add_stages");
}
}
#endif
}
+void Program::add_spirv_stages(const SpirVModule &mod, const map<string, int> &spec_values)
+{
+ static Require _req(ARB_gl_spirv);
+ static Require _req2(ARB_ES2_compatibility);
+
+ module = &mod;
+
+ const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
+ std::set<SpirVModule::Stage> stages;
+ for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
+ {
+ if(stages.count(i->stage))
+ throw invalid_argument("Program::add_spirv_stages");
+
+ switch(i->stage)
+ {
+ case SpirVModule::VERTEX: add_stage(GL_VERTEX_SHADER); break;
+ case SpirVModule::GEOMETRY: add_stage(GL_GEOMETRY_SHADER); break;
+ case SpirVModule::FRAGMENT: add_stage(GL_FRAGMENT_SHADER); break;
+ default: throw invalid_operation("Program::add_spirv_stages");
+ }
+
+ stages.insert(i->stage);
+ }
+
+ const vector<UInt32> &code = mod.get_code();
+ glShaderBinary(stage_ids.size(), &stage_ids[0], GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
+
+ const vector<SpirVModule::SpecConstant> &spec_consts = mod.get_spec_constants();
+ vector<unsigned> spec_id_array;
+ vector<unsigned> spec_value_array;
+ spec_id_array.reserve(spec_consts.size());
+ spec_value_array.reserve(spec_consts.size());
+ for(vector<SpirVModule::SpecConstant>::const_iterator i=spec_consts.begin(); i!=spec_consts.end(); ++i)
+ {
+ map<string, int>::const_iterator j = spec_values.find(i->name);
+ if(j!=spec_values.end())
+ {
+ spec_id_array.push_back(i->constant_id);
+ spec_value_array.push_back(j->second);
+ }
+ }
+
+ vector<SpirVModule::EntryPoint>::const_iterator j=entry_points.begin();
+ for(vector<unsigned>::const_iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i, ++j)
+ glSpecializeShader(*i, j->name.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]);
+}
+
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
void Program::attach_shader(Shader &shader)
IO::print("Program link info log:\n%s", info_log);
#endif
- query_uniforms();
- query_attributes();
+ if(module->get_format()==Module::GLSL)
+ {
+ query_uniforms();
+ query_attributes();
+ }
+ else if(module->get_format()==Module::SPIR_V)
+ {
+ collect_uniforms();
+ collect_attributes();
+ }
for(UniformMap::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
require_type(i->second.type);
default_block.layout_hash = compute_layout_hash(default_block.uniforms);
- string layout_descriptor;
- for(UniformBlockMap::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
- layout_descriptor += format("%d:%x\n", i->second.bind_point, i->second.layout_hash);
- uniform_layout_hash = hash32(layout_descriptor);
+ update_layout_hash();
}
void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
}
}
+void Program::collect_uniforms()
+{
+ const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
+
+ UniformBlockInfo &default_block = uniform_blocks[string()];
+ default_block.data_size = 0;
+ default_block.bind_point = -1;
+
+ const vector<SpirVModule::Variable> &variables = mod.get_variables();
+ for(vector<SpirVModule::Variable>::const_iterator i=variables.begin(); i!=variables.end(); ++i)
+ {
+ if(i->storage==SpirVModule::UNIFORM && i->struct_type)
+ {
+ UniformBlockInfo &info = uniform_blocks[i->struct_type->name];
+ info.name = i->struct_type->name;
+ info.bind_point = i->binding;
+ info.data_size = i->struct_type->size;
+
+ string prefix;
+ if(!i->name.empty())
+ prefix = i->struct_type->name+".";
+ collect_block_uniforms(info, *i->struct_type, prefix, 0);
+
+ info.layout_hash = compute_layout_hash(info.uniforms);
+ }
+ else if(i->storage==SpirVModule::UNIFORM_CONSTANT && i->location>=0)
+ {
+ UniformInfo &info = uniforms[i->name];
+ info.name = i->name;
+ info.block = &default_block;
+ info.location = i->location;
+ info.array_size = i->array_size;
+ info.array_stride = 0;
+ info.matrix_stride = 0;
+ info.type = i->type;
+ default_block.uniforms.push_back(&info);
+ }
+ }
+
+ default_block.layout_hash = compute_layout_hash(default_block.uniforms);
+
+ update_layout_hash();
+}
+
+void Program::collect_block_uniforms(UniformBlockInfo &block, const SpirVModule::Structure &strct, const string &prefix, unsigned base_offset)
+{
+ for(vector<SpirVModule::StructMember>::const_iterator i=strct.members.begin(); i!=strct.members.end(); ++i)
+ {
+ if(i->struct_type)
+ {
+ if(i->array_size)
+ {
+ for(unsigned j=0; j<i->array_size; ++j)
+ collect_block_uniforms(block, *i->struct_type, format("%s%s[%d].", prefix, i->name, j), base_offset+i->offset+i->array_stride*j);
+ }
+ else
+ collect_block_uniforms(block, *i->struct_type, prefix+i->name+".", base_offset+i->offset);
+ }
+ else
+ {
+ string name = prefix+i->name;
+ UniformInfo &info = uniforms[name];
+ info.name = name;
+ info.block = █
+ info.location = i->offset;
+ info.array_size = i->array_size;
+ info.array_stride = i->array_stride;
+ info.matrix_stride = i->matrix_stride;
+ info.type = i->type;
+ block.uniforms.push_back(&info);
+ }
+ }
+}
+
+void Program::collect_attributes()
+{
+ const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
+
+ const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
+ for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
+ if(i->stage==SpirVModule::VERTEX && i->name=="main")
+ {
+ for(vector<const SpirVModule::Variable *>::const_iterator j=i->globals.begin(); j!=i->globals.end(); ++j)
+ if((*j)->storage==SpirVModule::INPUT)
+ {
+ AttributeInfo &info = attributes[(*j)->name];
+ info.location = (*j)->location;
+ info.array_size = (*j)->array_size;
+ info.type = (*j)->type;
+ }
+ }
+}
+
+void Program::update_layout_hash()
+{
+ string layout_descriptor;
+ for(UniformBlockMap::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
+ layout_descriptor += format("%d:%x\n", i->second.bind_point, i->second.layout_hash);
+ uniform_layout_hash = hash32(layout_descriptor);
+}
+
Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
{
string layout_descriptor;