3 #include <msp/core/hash.h>
4 #include <msp/core/maputils.h>
5 #include <msp/gl/extensions/arb_shader_objects.h>
6 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
7 #include <msp/gl/extensions/arb_vertex_shader.h>
8 #include <msp/strings/format.h>
24 Program::Program(const ProgramBuilder::StandardFeatures &features)
28 ProgramBuilder builder(features);
29 builder.add_shaders(*this);
30 if(!features.transform)
34 Program::Program(const string &vert, const string &frag)
38 attach_shader_owned(new VertexShader(vert));
39 attach_shader_owned(new FragmentShader(frag));
45 static Require _req(ARB_shader_objects);
48 id = glCreateProgram();
53 for(ShaderList::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
58 void Program::attach_shader(Shader &shader)
60 if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
62 glAttachShader(id, shader.get_id());
63 shaders.push_back(&shader);
67 void Program::attach_shader_owned(Shader *shader)
69 attach_shader(*shader);
70 if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end())
71 owned_data.push_back(shader);
74 void Program::detach_shader(Shader &shader)
76 ShaderList::iterator i = remove(shaders.begin(), shaders.end(), &shader);
79 shaders.erase(i, shaders.end());
80 glDetachShader(id, shader.get_id());
84 void Program::bind_attribute(unsigned index, const string &name)
86 static Require _req(ARB_vertex_shader);
87 glBindAttribLocation(id, index, name.c_str());
92 for(ShaderList::iterator i=shaders.begin(); i!=shaders.end(); ++i)
93 if(!(*i)->is_compiled())
101 glGetProgramiv(id, GL_LINK_STATUS, &value);
102 if(!(linked = value))
103 throw compile_error(get_info_log());
106 glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &count);
107 vector<UniformInfo *> uniforms_by_index(count);
108 for(int i=0; i<count; ++i)
114 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
115 if(len && strncmp(name, "gl_", 3))
117 /* Some implementations report the first element of a uniform array,
118 others report just the name of an array. */
119 if(len>3 && !strcmp(name+len-3, "[0]"))
122 UniformInfo &info = uniforms[name];
126 info.array_stride = 0;
127 info.matrix_stride = 0;
129 uniforms_by_index[i] = &info;
135 glGetProgramiv(id, GL_ACTIVE_ATTRIBUTES, &count);
136 for(int i=0; i<count; ++i)
142 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
143 if(len && !strncmp(name, "gl_", 3))
147 if(ARB_uniform_buffer_object)
149 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, &count);
150 for(int i=0; i<count; ++i)
154 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
155 UniformBlockInfo &info = uniform_blocks[name];
158 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
159 info.data_size = value;
161 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
162 vector<int> indices(value);
163 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
164 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
166 if(!uniforms_by_index[*j])
167 throw logic_error("Program::link");
168 info.uniforms.push_back(uniforms_by_index[*j]);
169 uniforms_by_index[*j]->block = &info;
172 vector<unsigned> indices2(indices.begin(), indices.end());
173 vector<int> values(indices.size());
174 glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
175 for(unsigned j=0; j<indices.size(); ++j)
176 uniforms_by_index[indices[j]]->location = values[j];
179 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
180 if(uniforms_by_index[*j]->size>1)
181 indices2.push_back(*j);
182 if(!indices2.empty())
184 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
185 for(unsigned j=0; j<indices2.size(); ++j)
186 uniforms_by_index[indices[j]]->array_stride = values[j];
190 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
192 GLenum t = uniforms_by_index[*j]->type;
193 if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
194 t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
195 t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
196 indices2.push_back(*j);
198 if(!indices2.empty())
200 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
201 for(unsigned j=0; j<indices2.size(); ++j)
202 uniforms_by_index[indices[j]]->matrix_stride = values[j];
205 sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
206 info.layout_hash = compute_layout_hash(info.uniforms);
207 info.bind_point = info.layout_hash%BufferRange::get_n_uniform_buffer_bindings();
208 glUniformBlockBinding(id, i, info.bind_point);
212 vector<const UniformInfo *> blockless_uniforms;
213 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
216 i->second.location = glGetUniformLocation(id, i->second.name.c_str());
217 blockless_uniforms.push_back(&i->second);
220 uniform_layout_hash = compute_layout_hash(blockless_uniforms);
223 unsigned Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
225 string layout_descriptor;
226 for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
227 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->size);
228 return hash32(layout_descriptor);
231 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
233 return uni1->location<uni2->location;
236 string Program::get_info_log() const
239 glGetProgramiv(id, GL_INFO_LOG_LENGTH, &len);
240 char *buf = new char[len+1];
241 glGetProgramInfoLog(id, len+1, &len, buf);
242 string log(buf, len);
247 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
249 return get_item(uniform_blocks, name);
252 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
254 return get_item(uniforms, name);
257 int Program::get_uniform_location(const string &n) const
259 UniformMap::const_iterator i = uniforms.find(n);
260 if(i==uniforms.end())
262 if(n[n.size()-1]==']')
264 string::size_type open_bracket = n.rfind('[');
265 if(open_bracket!=string::npos)
267 /* The requested name looks like an array. glGetActiveUniform only
268 gives us the first element of the array, so try to look that up and
270 unsigned offset = lexical_cast<unsigned>(n.substr(open_bracket+1, n.size()-2-open_bracket));
271 i = uniforms.find(n.substr(0, open_bracket));
272 if(i!=uniforms.end() && !i->second.block && offset<i->second.size)
273 return i->second.location+offset;
279 return i->second.block ? -1 : i->second.location;
282 void Program::bind() const
285 throw invalid_operation("Program::bind");
287 if(!set_current(this))
293 void Program::unbind()
302 Program::Loader::Loader(Program &p):
303 DataFile::ObjectLoader<Program>(p)
305 add("attribute", &Loader::attribute);
306 add("fragment_shader", &Loader::fragment_shader);
307 add("standard", &Loader::standard);
308 add("vertex_shader", &Loader::vertex_shader);
311 void Program::Loader::finish()
316 void Program::Loader::attribute(unsigned i, const string &n)
318 obj.bind_attribute(i, n);
321 void Program::Loader::fragment_shader(const string &src)
323 obj.attach_shader_owned(new FragmentShader(src));
326 void Program::Loader::standard()
328 ProgramBuilder::StandardFeatures feat;
330 ProgramBuilder builder(feat);
331 builder.add_shaders(obj);
334 void Program::Loader::vertex_shader(const string &src)
336 obj.attach_shader_owned(new VertexShader(src));