#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);
require_type(i->second.type);
}
-void Program::require_type(GLenum t)
-{
- switch(t)
- {
- case GL_FLOAT_MAT2x3:
- case GL_FLOAT_MAT2x4:
- case GL_FLOAT_MAT3x2:
- case GL_FLOAT_MAT3x4:
- case GL_FLOAT_MAT4x2:
- case GL_FLOAT_MAT4x3:
- { static Require _req(NV_non_square_matrices); }
- break;
- }
-}
-
void Program::query_uniforms()
{
unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
name[len-3] = 0;
UniformInfo &info = uniforms[name];
- info.block = 0;
info.name = name;
- info.size = size;
- info.array_stride = 0;
- info.matrix_stride = 0;
- info.type = type;
+ info.array_size = size;
+ info.type = from_gl_type(type);
uniforms_by_index[i] = &info;
}
}
query_uniform_blocks(uniforms_by_index);
UniformBlockInfo &default_block = uniform_blocks[string()];
- default_block.data_size = 0;
- default_block.bind_point = -1;
for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
if(!i->second.block)
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)
indices2.clear();
for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
- if(uniforms_by_index[*j]->size>1)
+ if(uniforms_by_index[*j]->array_size>1)
indices2.push_back(*j);
if(!indices2.empty())
{
indices2.clear();
for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
{
- GLenum t = uniforms_by_index[*j]->type;
- if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
- t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
- t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
+ DataType t = uniforms_by_index[*j]->type;
+ if(is_matrix(t))
indices2.push_back(*j);
}
if(!indices2.empty())
AttributeInfo &info = attributes[name];
info.name = name;
info.location = glGetAttribLocation(id, name);
- info.size = size;
- info.type = type;
+ info.array_size = size;
+ info.type = from_gl_type(type);
}
}
}
+void Program::collect_uniforms()
+{
+ const SpirVModule &mod = static_cast<const SpirVModule &>(*module);
+
+ UniformBlockInfo &default_block = uniform_blocks[string()];
+
+ 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.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;
for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
- layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->size);
+ layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->array_size);
return hash32(layout_descriptor);
}
}
+Program::UniformInfo::UniformInfo():
+ block(0),
+ location(0),
+ array_size(0),
+ array_stride(0),
+ matrix_stride(0),
+ type(VOID)
+{ }
+
+
+Program::UniformBlockInfo::UniformBlockInfo():
+ data_size(0),
+ bind_point(-1),
+ layout_hash(0)
+{ }
+
+
+Program::AttributeInfo::AttributeInfo():
+ location(-1),
+ array_size(0),
+ type(VOID)
+{ }
+
+
Program::Loader::Loader(Program &p, Collection &c):
DataFile::CollectionObjectLoader<Program>(p, &c)
{