- 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);
-
-#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)
-{
- 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(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(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);
- 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);
- if(module && module->get_format()==Module::GLSL)
- info_log = static_cast<const GlslModule *>(module)->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)
-{
- static Require _req(ARB_gl_spirv);
- static Require _req2(ARB_ES2_compatibility);
-
- module = &mod;
-
- 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;
- }
-
- 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);