static Require _req(ARB_shader_objects);
id = glCreateProgram();
+ fill(stage_ids, stage_ids+MAX_STAGES, 0);
module = 0;
transient = 0;
linked = false;
Program::~Program()
{
- for(vector<unsigned>::iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i)
- glDeleteShader(*i);
+ for(unsigned i=0; i<MAX_STAGES; ++i)
+ if(stage_ids[i])
+ glDeleteShader(stage_ids[i]);
glDeleteProgram(id);
}
void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
{
- if(!stage_ids.empty())
+ if(has_stages())
throw invalid_operation("Program::add_stages");
switch(mod.get_format())
}
}
-unsigned Program::add_stage(GLenum type)
+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 GL_VERTEX_SHADER: { static Require _req(ARB_vertex_shader); } break;
- case GL_GEOMETRY_SHADER: { static Require _req(ARB_geometry_shader4); } break;
- case GL_FRAGMENT_SHADER: { static Require _req(ARB_fragment_shader); } break;
+ 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");
}
- unsigned stage_id = glCreateShader(type);
- stage_ids.push_back(stage_id);
+ 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;
unsigned stage_id = 0;
switch(*i)
{
- case SL::Stage::VERTEX: stage_id = add_stage(GL_VERTEX_SHADER); break;
- case SL::Stage::GEOMETRY: stage_id = add_stage(GL_GEOMETRY_SHADER); break;
- case SL::Stage::FRAGMENT: stage_id = add_stage(GL_FRAGMENT_SHADER); break;
+ 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");
}
module = &mod;
const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
- std::set<SpirVModule::Stage> stages;
+ 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)
{
- if(stages.count(i->stage))
- throw invalid_argument("Program::add_spirv_stages");
-
+ unsigned stage_id = 0;
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;
+ 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");
}
- stages.insert(i->stage);
+ used_stage_ids[n_stages++] = stage_id;
}
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);
+ 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;
}
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]);
+ 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
unsigned shader_id = shader.steal_id();
if(!shader_id)
throw invalid_argument("Program::attach_shader");
- stage_ids.push_back(shader_id);
+
+ 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::link()
{
- if(stage_ids.empty())
+ if(!has_stages())
throw invalid_operation("Program::link");
uniforms.clear();