- 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");
- }
-}
-
-bool Program::has_stages() const
-{
- for(unsigned i=0; i<MAX_STAGES; ++i)
- if(stage_ids[i])
- return true;
- return false;
-}
-
-unsigned Program::add_stage(Stage type)
-{
- GLenum gl_type;
- switch(type)
- {
- case VERTEX: { static Require _req(ARB_vertex_shader); gl_type = GL_VERTEX_SHADER; } break;
- case GEOMETRY: { static Require _req(ARB_geometry_shader4); gl_type = GL_GEOMETRY_SHADER; } break;
- case FRAGMENT: { static Require _req(ARB_fragment_shader); gl_type = GL_FRAGMENT_SHADER; } break;
- default: throw invalid_argument("Program::add_stage");
- }
-
- if(stage_ids[type])
- throw invalid_operation("Program::add_stage");
-
- unsigned stage_id = glCreateShader(gl_type);
- stage_ids[type] = stage_id;
- glAttachShader(id, stage_id);
-
- return stage_id;
-}
-
-void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values)
-{
- module = &mod;
-
- SL::Compiler compiler;
- compiler.set_source(mod.get_prepared_source(), "<module>");
- compiler.specialize(spec_values);
- compiler.compile(SL::Compiler::PROGRAM);
-#ifdef DEBUG
- string diagnostics = compiler.get_diagnostics();
- if(!diagnostics.empty())
- IO::print("Program diagnostics:\n%s\n", diagnostics);
-#endif
-
- vector<SL::Stage::Type> stages = compiler.get_stages();
- for(vector<SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
- {
- unsigned stage_id = 0;
- switch(*i)
- {
- case SL::Stage::VERTEX: stage_id = add_stage(VERTEX); break;
- case SL::Stage::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
- case SL::Stage::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
- default: throw invalid_operation("Program::add_glsl_stages");
- }
-
- string stage_src = compiler.get_stage_glsl(*i);
- const char *src_ptr = stage_src.data();
- int src_len = stage_src.size();
- glShaderSource(stage_id, 1, &src_ptr, &src_len);
-
- if(*i==SL::Stage::VERTEX)
- {
- const map<string, unsigned> &attribs = compiler.get_vertex_attributes();
- for(map<string, unsigned>::const_iterator j=attribs.begin(); j!=attribs.end(); ++j)
- glBindAttribLocation(id, j->second, j->first.c_str());
- }
-
- if(*i==SL::Stage::FRAGMENT && EXT_gpu_shader4)
- {
- const map<string, unsigned> &frag_outs = compiler.get_fragment_outputs();
- for(map<string, unsigned>::const_iterator j=frag_outs.begin(); j!=frag_outs.end(); ++j)
- glBindFragDataLocation(id, j->second, j->first.c_str());
- }
-
- compile_glsl_stage(stage_id);
- }
-
- if(!transient)
- transient = new TransientData;
- transient->textures = compiler.get_texture_bindings();
- transient->blocks = compiler.get_uniform_block_bindings();
-}
-
-void Program::compile_glsl_stage(unsigned stage_id)
-{
- glCompileShader(stage_id);
- bool compiled = get_shader_i(stage_id, GL_COMPILE_STATUS);
-
- GLsizei info_log_len = get_shader_i(stage_id, GL_INFO_LOG_LENGTH);
- string info_log(info_log_len+1, 0);
- glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]);
- info_log.erase(info_log_len);
- if(module && module->get_format()==Module::GLSL)
- info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
-
- if(!compiled)
- throw compile_error(info_log);
-#ifdef DEBUG
- if(!info_log.empty())
- IO::print("Shader compile info log:\n%s", info_log);
-#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();
- unsigned n_stages = 0;
- unsigned used_stage_ids[MAX_STAGES];
- for(vector<SpirVModule::EntryPoint>::const_iterator i=entry_points.begin(); i!=entry_points.end(); ++i)
- {
- unsigned stage_id = 0;
- switch(i->stage)
- {
- case SpirVModule::VERTEX: stage_id = add_stage(VERTEX); break;
- case SpirVModule::GEOMETRY: stage_id = add_stage(GEOMETRY); break;
- case SpirVModule::FRAGMENT: stage_id = add_stage(FRAGMENT); break;
- default: throw invalid_operation("Program::add_spirv_stages");
- }
-
- used_stage_ids[n_stages++] = stage_id;
- }
-
- const vector<UInt32> &code = mod.get_code();
- glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
-
- if(!spec_values.empty() && !transient)
- transient = new TransientData;
-
- const vector<SpirVModule::Constant> &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::Constant>::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);
- transient->spec_values[i->constant_id] = j->second;
- }
- }
-
- vector<SpirVModule::EntryPoint>::const_iterator j=entry_points.begin();
- for(unsigned i=0; i<MAX_STAGES; ++i)
- if(stage_ids[i])
- glSpecializeShader(stage_ids[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)
-{
- unsigned shader_id = shader.steal_id();
- if(!shader_id)
- throw invalid_argument("Program::attach_shader");
-
- int type;
- glGetShaderiv(shader_id, GL_SHADER_TYPE, &type);
- switch(type)
- {
- case GL_VERTEX_SHADER: stage_ids[VERTEX] = shader_id; break;
- case GL_GEOMETRY_SHADER: stage_ids[GEOMETRY] = shader_id; break;
- case GL_FRAGMENT_SHADER: stage_ids[FRAGMENT] = shader_id; break;
- }
-
- glAttachShader(id, shader_id);
- compile_glsl_stage(shader_id);
-}
-
-void Program::attach_shader_owned(Shader *shader)
-{
- attach_shader(*shader);
- delete shader;
-}
-
-void Program::detach_shader(Shader &)
-{
-}
-
-const vector<Shader *> &Program::get_attached_shaders() const
-{
- static vector<Shader *> dummy;
- return dummy;
-}
-
-void Program::bind_attribute(unsigned index, const string &name)
-{
- static Require _req(ARB_vertex_shader);
- glBindAttribLocation(id, index, name.c_str());
-}
-
-void Program::bind_attribute(VertexAttribute attr, const string &name)
-{
- bind_attribute(get_attribute_semantic(attr), name);
-}
-
-void Program::bind_fragment_data(unsigned index, const string &name)
-{
- static Require _req(EXT_gpu_shader4);
- glBindFragDataLocation(id, index, name.c_str());
-}
-#pragma GCC diagnostic pop
-
-void Program::link()
-{
- if(!has_stages())
- throw invalid_operation("Program::link");
-
- uniforms.clear();
- uniform_blocks.clear();
- attributes.clear();
-
- glLinkProgram(id);
- linked = get_program_i(id, GL_LINK_STATUS);
-
- GLsizei info_log_len = get_program_i(id, GL_INFO_LOG_LENGTH);
- string info_log(info_log_len+1, 0);
- glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]);
- info_log.erase(info_log_len);
- if(module && module->get_format()==Module::GLSL)
- info_log = static_cast<const GlslModule *>(module)->get_source_map().translate_errors(info_log);
-
- if(!linked)
- throw compile_error(info_log);
-#ifdef DEBUG
- if(!info_log.empty())
- IO::print("Program link info log:\n%s", info_log);
-#endif
-
- if(module->get_format()==Module::GLSL)
- {
- query_uniforms();
- query_attributes();
- if(transient)
- {
- for(unsigned i=0; i<uniform_blocks.size(); ++i)
- {
- map<string, unsigned>::const_iterator j = transient->blocks.find(uniform_blocks[i].name);
- if(j!=transient->blocks.end())
- {
- glUniformBlockBinding(id, i, j->second);
- uniform_blocks[i].bind_point = j->second;
- }
- }
-
- Conditional<BindRestore> _bind(!ARB_separate_shader_objects, this);
- for(map<string, unsigned>::const_iterator i=transient->textures.begin(); i!=transient->textures.end(); ++i)
- {
- int location = get_uniform_location(i->first);
- if(location>=0)
- {
- if(ARB_separate_shader_objects)
- glProgramUniform1i(id, location, i->second);
- else
- glUniform1i(location, i->second);
- }
- }
- }
- }
- else if(module->get_format()==Module::SPIR_V)
- {
- collect_uniforms();
- collect_attributes();
- }
-
- delete transient;
- transient = 0;
-
- for(vector<UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- require_type(i->type);
- for(vector<AttributeInfo>::const_iterator i=attributes.begin(); i!=attributes.end(); ++i)
- require_type(i->type);
-}