summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
9e92164)
There can't bee a huge number of stages in a program so a fixed-size
array takes less space than a vector. It also allows accessing ids of
specific stages if needed.
static Require _req(ARB_shader_objects);
id = glCreateProgram();
static Require _req(ARB_shader_objects);
id = glCreateProgram();
+ fill(stage_ids, stage_ids+MAX_STAGES, 0);
module = 0;
transient = 0;
linked = false;
module = 0;
transient = 0;
linked = false;
- 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)
{
glDeleteProgram(id);
}
void Program::add_stages(const Module &mod, const map<string, int> &spec_values)
{
throw invalid_operation("Program::add_stages");
switch(mod.get_format())
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;
- 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");
}
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;
glAttachShader(id, stage_id);
return stage_id;
unsigned stage_id = 0;
switch(*i)
{
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");
}
default: throw invalid_operation("Program::add_glsl_stages");
}
module = &mod;
const vector<SpirVModule::EntryPoint> &entry_points = mod.get_entry_points();
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)
{
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");
-
- 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");
}
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();
}
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;
if(!spec_values.empty() && !transient)
transient = new TransientData;
}
vector<SpirVModule::EntryPoint>::const_iterator j=entry_points.begin();
}
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
}
#pragma GCC diagnostic push
unsigned shader_id = shader.steal_id();
if(!shader_id)
throw invalid_argument("Program::attach_shader");
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);
}
compile_glsl_stage(shader_id);
}
throw invalid_operation("Program::link");
uniforms.clear();
throw invalid_operation("Program::link");
uniforms.clear();
+ enum Stage
+ {
+ VERTEX,
+ GEOMETRY,
+ FRAGMENT,
+ MAX_STAGES
+ };
+
struct TransientData
{
std::map<std::string, unsigned> textures;
struct TransientData
{
std::map<std::string, unsigned> textures;
- std::vector<unsigned> stage_ids;
+ unsigned stage_ids[MAX_STAGES];
const Module *module;
TransientData *transient;
bool linked;
const Module *module;
TransientData *transient;
bool linked;
void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
private:
void add_stages(const Module &, const std::map<std::string, int> & = std::map<std::string, int>());
private:
- unsigned add_stage(GLenum);
+ bool has_stages() const;
+ unsigned add_stage(Stage);
void add_glsl_stages(const GlslModule &, const std::map<std::string, int> &);
void compile_glsl_stage(unsigned);
void add_spirv_stages(const SpirVModule &, const std::map<std::string, int> &);
void add_glsl_stages(const GlslModule &, const std::map<std::string, int> &);
void compile_glsl_stage(unsigned);
void add_spirv_stages(const SpirVModule &, const std::map<std::string, int> &);