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/gl/extensions/nv_non_square_matrices.h>
11 #include <msp/io/print.h>
12 #include <msp/strings/format.h>
17 #include "programcompiler.h"
18 #include "resources.h"
31 Program::Program(const ProgramBuilder::StandardFeatures &features)
35 ProgramBuilder builder(features);
36 builder.add_shaders(*this);
40 Program::Program(const std::string &source)
44 ProgramCompiler compiler;
45 if(source.find(';')==string::npos && source.size()>5 && !source.compare(source.size()-5, 5, ".glsl"))
47 if(RefPtr<IO::Seekable> io = Resources::get_builtins().open(source))
48 compiler.compile(*io, source);
50 throw IO::file_not_found(source);
53 compiler.compile(source);
54 compiler.add_shaders(*this);
58 Program::Program(const string &vert, const string &frag)
62 attach_shader_owned(new VertexShader(vert));
63 attach_shader_owned(new FragmentShader(frag));
69 static Require _req(ARB_shader_objects);
72 id = glCreateProgram();
77 for(ShaderList::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
82 void Program::attach_shader(Shader &shader)
84 if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
86 glAttachShader(id, shader.get_id());
87 shaders.push_back(&shader);
91 void Program::attach_shader_owned(Shader *shader)
93 attach_shader(*shader);
94 if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end())
95 owned_data.push_back(shader);
98 void Program::detach_shader(Shader &shader)
100 ShaderList::iterator i = remove(shaders.begin(), shaders.end(), &shader);
103 shaders.erase(i, shaders.end());
104 glDetachShader(id, shader.get_id());
108 void Program::bind_attribute(unsigned index, const string &name)
110 static Require _req(ARB_vertex_shader);
111 glBindAttribLocation(id, index, name.c_str());
114 void Program::bind_attribute(VertexComponent comp, const string &name)
116 bind_attribute(get_component_type(comp), name);
119 void Program::bind_fragment_data(unsigned index, const string &name)
121 static Require _req(EXT_gpu_shader4);
122 glBindFragDataLocation(id, index, name.c_str());
127 for(ShaderList::iterator i=shaders.begin(); i!=shaders.end(); ++i)
128 if(!(*i)->is_compiled())
134 linked = get_program_i(id, GL_LINK_STATUS);
136 throw compile_error(get_info_log());
139 std::string info_log = get_info_log();
140 if(!info_log.empty())
141 IO::print("Program link info log:\n%s", info_log);
148 void Program::query_uniforms()
150 unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
151 vector<UniformInfo *> uniforms_by_index(count);
152 for(unsigned i=0; i<count; ++i)
158 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
159 if(len && strncmp(name, "gl_", 3))
161 /* Some implementations report the first element of a uniform array,
162 others report just the name of the array itself. */
163 if(len>3 && !strcmp(name+len-3, "[0]"))
166 UniformInfo &info = uniforms[name];
170 info.array_stride = 0;
171 info.matrix_stride = 0;
173 uniforms_by_index[i] = &info;
177 if(ARB_uniform_buffer_object)
178 query_uniform_blocks(uniforms_by_index);
180 UniformBlockInfo &default_block = uniform_blocks[string()];
181 default_block.data_size = 0;
182 default_block.bind_point = -1;
184 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
187 i->second.location = glGetUniformLocation(id, i->second.name.c_str());
188 i->second.block = &default_block;
189 default_block.uniforms.push_back(&i->second);
192 default_block.layout_hash = compute_layout_hash(default_block.uniforms);
194 string layout_descriptor;
195 for(UniformBlockMap::const_iterator i=uniform_blocks.begin(); i!=uniform_blocks.end(); ++i)
196 layout_descriptor += format("%d:%x\n", i->second.bind_point, i->second.layout_hash);
197 uniform_layout_hash = hash32(layout_descriptor);
200 void Program::query_uniform_blocks(const vector<UniformInfo *> &uniforms_by_index)
202 uniform_blocks.clear();
204 std::set<unsigned> used_bind_points;
205 unsigned count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
206 for(unsigned i=0; i<count; ++i)
210 glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
211 UniformBlockInfo &info = uniform_blocks[name];
215 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
216 info.data_size = value;
218 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
219 vector<int> indices(value);
220 glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
221 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
223 if(!uniforms_by_index[*j])
224 throw logic_error("Program::link");
225 info.uniforms.push_back(uniforms_by_index[*j]);
226 uniforms_by_index[*j]->block = &info;
229 vector<unsigned> indices2(indices.begin(), indices.end());
230 vector<int> values(indices.size());
231 glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
232 for(unsigned j=0; j<indices.size(); ++j)
233 uniforms_by_index[indices[j]]->location = values[j];
236 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
237 if(uniforms_by_index[*j]->size>1)
238 indices2.push_back(*j);
239 if(!indices2.empty())
241 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
242 for(unsigned j=0; j<indices2.size(); ++j)
243 uniforms_by_index[indices2[j]]->array_stride = values[j];
247 for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
249 GLenum t = uniforms_by_index[*j]->type;
250 if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
251 t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
252 t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
253 indices2.push_back(*j);
255 if(!indices2.empty())
257 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
258 for(unsigned j=0; j<indices2.size(); ++j)
259 uniforms_by_index[indices2[j]]->matrix_stride = values[j];
262 sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
263 info.layout_hash = compute_layout_hash(info.uniforms);
264 unsigned n_bindings = BufferRange::get_n_uniform_buffer_bindings();
265 info.bind_point = info.layout_hash%n_bindings;
266 while(used_bind_points.count(info.bind_point))
267 info.bind_point = (info.bind_point+1)%n_bindings;
268 glUniformBlockBinding(id, i, info.bind_point);
269 used_bind_points.insert(info.bind_point);
273 void Program::query_attributes()
275 unsigned count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
276 for(unsigned i=0; i<count; ++i)
282 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
283 if(len && strncmp(name, "gl_", 3))
285 if(len>3 && !strcmp(name+len-3, "[0]"))
288 AttributeInfo &info = attributes[name];
290 info.location = glGetAttribLocation(id, name);
297 Program::LayoutHash Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
299 string layout_descriptor;
300 for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
301 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->size);
302 return hash32(layout_descriptor);
305 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
307 return uni1->location<uni2->location;
310 string Program::get_info_log() const
312 GLsizei len = get_program_i(id, GL_INFO_LOG_LENGTH);
313 char *buf = new char[len+1];
314 glGetProgramInfoLog(id, len+1, &len, buf);
315 string log(buf, len);
320 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
322 return get_item(uniform_blocks, name);
325 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
327 return get_item(uniforms, name);
330 int Program::get_uniform_location(const string &n) const
332 if(n[n.size()-1]==']')
333 throw invalid_argument("Program::get_uniform_location");
335 UniformMap::const_iterator i = uniforms.find(n);
336 if(i==uniforms.end())
339 return i->second.block->bind_point<0 ? i->second.location : -1;
342 const Program::AttributeInfo &Program::get_attribute_info(const string &name) const
344 return get_item(attributes, name);
347 int Program::get_attribute_location(const string &n) const
349 if(n[n.size()-1]==']')
350 throw invalid_argument("Program::get_attribute_location");
352 AttributeMap::const_iterator i = attributes.find(n);
353 return i!=attributes.end() ? i->second.location : -1;
356 void Program::bind() const
359 throw invalid_operation("Program::bind");
361 if(!set_current(this))
367 void Program::unbind()
376 Program::Loader::Loader(Program &p):
377 DataFile::ObjectLoader<Program>(p)
379 add("attribute", &Loader::attribute);
380 add("fragment_shader", &Loader::fragment_shader);
381 add("geometry_shader", &Loader::geometry_shader);
382 add("standard", &Loader::standard);
383 add("vertex_shader", &Loader::vertex_shader);
386 void Program::Loader::finish()
391 void Program::Loader::attribute(unsigned i, const string &n)
393 obj.bind_attribute(i, n);
396 void Program::Loader::fragment_shader(const string &src)
398 obj.attach_shader_owned(new FragmentShader(src));
401 void Program::Loader::geometry_shader(const string &src)
403 obj.attach_shader_owned(new GeometryShader(src));
406 void Program::Loader::standard()
408 ProgramBuilder::StandardFeatures feat;
410 ProgramBuilder builder(feat);
411 builder.add_shaders(obj);
414 void Program::Loader::vertex_shader(const string &src)
416 obj.attach_shader_owned(new VertexShader(src));