-}
-
-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);
-
-#ifdef DEBUG
- if(!debug_name.empty() && KHR_debug)
- set_stage_debug_name(stage_id, type);
-#endif
-
- return stage_id;
-}
-
-void Program::add_glsl_stages(const GlslModule &mod, const map<string, int> &spec_values, TransientData &transient)
-{
- 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();
- if(stages.empty())
- throw invalid_argument("Program::add_glsl_stages");
-
- for(SL::Stage::Type st: stages)
- {
- unsigned stage_id = 0;
- switch(st)
- {
- 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(st);
- const char *src_ptr = stage_src.data();
- int src_len = stage_src.size();
- glShaderSource(stage_id, 1, &src_ptr, &src_len);
-
- if(st==SL::Stage::VERTEX)
- {
- for(const auto &kvp: compiler.get_vertex_attributes())
- glBindAttribLocation(id, kvp.second, kvp.first.c_str());
- }
-
- if(st==SL::Stage::FRAGMENT && EXT_gpu_shader4)
- {
- for(const auto &kvp: compiler.get_fragment_outputs())
- glBindFragDataLocation(id, kvp.second, kvp.first.c_str());
- }
-
- compile_glsl_stage(mod, stage_id);
- }
-
- transient.textures = compiler.get_texture_bindings();
- transient.blocks = compiler.get_uniform_block_bindings();
-}
-
-void Program::compile_glsl_stage(const GlslModule &mod, unsigned stage_id)
-{
- glCompileShader(stage_id);
- int status = 0;
- glGetShaderiv(stage_id, GL_COMPILE_STATUS, &status);
-
- int info_log_len = 0;
- glGetShaderiv(stage_id, GL_INFO_LOG_LENGTH, &info_log_len);
- 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);
- info_log = mod.get_source_map().translate_errors(info_log);
-
- if(!status)
- 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, TransientData &transient)
-{
- static Require _req(ARB_gl_spirv);
- static Require _req2(ARB_ES2_compatibility);
-
- unsigned n_stages = 0;
- unsigned used_stage_ids[MAX_STAGES];
- for(const SpirVModule::EntryPoint &e: mod.get_entry_points())
- {
- unsigned stage_id = 0;
- switch(e.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;
- }
-
- if(!n_stages)
- throw invalid_argument("Program::add_spirv_stages");
-
- const vector<uint32_t> &code = mod.get_code();
- glShaderBinary(n_stages, used_stage_ids, GL_SHADER_BINARY_FORMAT_SPIR_V, &code[0], code.size()*4);
-
- 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(const SpirVModule::Constant &c: spec_consts)
- {
- auto i = spec_values.find(c.name);
- if(i!=spec_values.end())
- {
- spec_id_array.push_back(c.constant_id);
- spec_value_array.push_back(i->second);
- transient.spec_values[c.constant_id] = i->second;
- }
- }
-
- auto j = mod.get_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]);
-}
-
-void Program::finalize(const Module &mod, const TransientData &transient)
-{
- reflect_data = ReflectData();
-
- glLinkProgram(id);
- int status = 0;
- glGetProgramiv(id, GL_LINK_STATUS, &status);
- linked = status;
-
- int info_log_len = 0;
- glGetProgramiv(id, GL_INFO_LOG_LENGTH, &info_log_len);
- 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(mod.get_format()==Module::GLSL)
- info_log = static_cast<const GlslModule &>(mod).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(mod.get_format()==Module::GLSL)
- {
- query_uniforms();
- query_attributes();
- for(unsigned i=0; i<reflect_data.uniform_blocks.size(); ++i)
- {
- auto j = transient.blocks.find(reflect_data.uniform_blocks[i].name);
- if(j!=transient.blocks.end())
- {
- glUniformBlockBinding(id, i, j->second);
- reflect_data.uniform_blocks[i].bind_point = j->second;
- }
- }