4 #include <msp/core/hash.h>
5 #include <msp/core/maputils.h>
6 #include <msp/gl/extensions/arb_shader_objects.h>
7 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
8 #include <msp/gl/extensions/arb_vertex_shader.h>
9 #include <msp/gl/extensions/ext_gpu_shader4.h>
10 #include <msp/strings/format.h>
27 Program::Program(const ProgramBuilder::StandardFeatures &features)
31 ProgramBuilder builder(features);
32 builder.add_shaders(*this);
36 Program::Program(const string &vert, const string &frag)
40 attach_shader_owned(new VertexShader(vert));
41 attach_shader_owned(new FragmentShader(frag));
47 static Require _req(ARB_shader_objects);
50 id = glCreateProgram();
55 for(ShaderList::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
60 void Program::attach_shader(Shader &shader)
62 if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
64 glAttachShader(id, shader.get_id());
65 shaders.push_back(&shader);
69 void Program::attach_shader_owned(Shader *shader)
71 attach_shader(*shader);
72 if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end())
73 owned_data.push_back(shader);
76 void Program::detach_shader(Shader &shader)
78 ShaderList::iterator i = remove(shaders.begin(), shaders.end(), &shader);
81 shaders.erase(i, shaders.end());
82 glDetachShader(id, shader.get_id());
86 void Program::bind_attribute(unsigned index, const string &name)
88 static Require _req(ARB_vertex_shader);
89 glBindAttribLocation(id, index, name.c_str());
92 void Program::bind_attribute(VertexComponent comp, const string &name)
94 bind_attribute(get_component_type(comp), name);
97 void Program::bind_fragment_data(unsigned index, const string &name)
99 static Require _req(EXT_gpu_shader4);
100 glBindFragDataLocation(id, index, name.c_str());
105 for(ShaderList::iterator i=shaders.begin(); i!=shaders.end(); ++i)
106 if(!(*i)->is_compiled())
113 linked = get_program_i(id, GL_LINK_STATUS);
115 throw compile_error(get_info_log());
117 unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
118 vector<UniformInfo *> uniforms_by_index(count);
119 for(unsigned i=0; i<count; ++i)
125 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
126 if(len && strncmp(name, "gl_", 3))
128 /* Some implementations report the first element of a uniform array,
129 others report just the name of the array itself. */
130 if(len>3 && !strcmp(name+len-3, "[0]"))
133 UniformInfo &info = uniforms[name];
137 info.array_stride = 0;
138 info.matrix_stride = 0;
140 uniforms_by_index[i] = &info;
146 count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
147 for(unsigned i=0; i<count; ++i)
153 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
154 if(len && !strncmp(name, "gl_", 3))
158 if(ARB_uniform_buffer_object)
160 std::set<unsigned> used_bind_points;
161 count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
162 for(unsigned i=0; i<count; ++i)
166 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
167 UniformBlockInfo &info = uniform_blocks[name];
171 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
172 info.data_size = value;
174 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
175 vector<int> indices(value);
176 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
177 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
179 if(!uniforms_by_index[*j])
180 throw logic_error("Program::link");
181 info.uniforms.push_back(uniforms_by_index[*j]);
182 uniforms_by_index[*j]->block = &info;
185 vector<unsigned> indices2(indices.begin(), indices.end());
186 vector<int> values(indices.size());
187 glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
188 for(unsigned j=0; j<indices.size(); ++j)
189 uniforms_by_index[indices[j]]->location = values[j];
192 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
193 if(uniforms_by_index[*j]->size>1)
194 indices2.push_back(*j);
195 if(!indices2.empty())
197 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
198 for(unsigned j=0; j<indices2.size(); ++j)
199 uniforms_by_index[indices2[j]]->array_stride = values[j];
203 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
205 GLenum t = uniforms_by_index[*j]->type;
206 if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
207 t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
208 t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
209 indices2.push_back(*j);
211 if(!indices2.empty())
213 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
214 for(unsigned j=0; j<indices2.size(); ++j)
215 uniforms_by_index[indices2[j]]->matrix_stride = values[j];
218 sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
219 info.layout_hash = compute_layout_hash(info.uniforms);
220 unsigned n_bindings = BufferRange::get_n_uniform_buffer_bindings();
221 info.bind_point = info.layout_hash%n_bindings;
222 while(used_bind_points.count(info.bind_point))
223 info.bind_point = (info.bind_point+1)%n_bindings;
224 glUniformBlockBinding(id, i, info.bind_point);
225 used_bind_points.insert(info.bind_point);
229 vector<const UniformInfo *> blockless_uniforms;
230 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
233 i->second.location = glGetUniformLocation(id, i->second.name.c_str());
234 blockless_uniforms.push_back(&i->second);
237 uniform_layout_hash = compute_layout_hash(blockless_uniforms);
240 unsigned Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
242 string layout_descriptor;
243 for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
244 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->size);
245 return hash32(layout_descriptor);
248 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
250 return uni1->location<uni2->location;
253 string Program::get_info_log() const
255 GLsizei len = get_program_i(id, GL_INFO_LOG_LENGTH);
256 char *buf = new char[len+1];
257 glGetProgramInfoLog(id, len+1, &len, buf);
258 string log(buf, len);
263 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
265 return get_item(uniform_blocks, name);
268 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
270 return get_item(uniforms, name);
273 int Program::get_uniform_location(const string &n) const
275 UniformMap::const_iterator i = uniforms.find(n);
276 if(i==uniforms.end())
278 if(n[n.size()-1]==']')
280 string::size_type open_bracket = n.rfind('[');
281 if(open_bracket!=string::npos)
283 /* The requested name looks like an array. glGetActiveUniform only
284 gives us the first element of the array, so try to look that up and
286 unsigned offset = lexical_cast<unsigned>(n.substr(open_bracket+1, n.size()-2-open_bracket));
287 i = uniforms.find(n.substr(0, open_bracket));
288 if(i!=uniforms.end() && !i->second.block && offset<i->second.size)
289 return i->second.location+offset;
295 return i->second.block ? -1 : i->second.location;
298 void Program::bind() const
301 throw invalid_operation("Program::bind");
303 if(!set_current(this))
309 void Program::unbind()
318 Program::Loader::Loader(Program &p):
319 DataFile::ObjectLoader<Program>(p)
321 add("attribute", &Loader::attribute);
322 add("fragment_shader", &Loader::fragment_shader);
323 add("standard", &Loader::standard);
324 add("vertex_shader", &Loader::vertex_shader);
327 void Program::Loader::finish()
332 void Program::Loader::attribute(unsigned i, const string &n)
334 obj.bind_attribute(i, n);
337 void Program::Loader::fragment_shader(const string &src)
339 obj.attach_shader_owned(new FragmentShader(src));
342 void Program::Loader::standard()
344 ProgramBuilder::StandardFeatures feat;
346 ProgramBuilder builder(feat);
347 builder.add_shaders(obj);
350 void Program::Loader::vertex_shader(const string &src)
352 obj.attach_shader_owned(new VertexShader(src));