]> git.tdb.fi Git - libs/gl.git/blob - source/program.cpp
Use wrappers for single-value glGet* calls
[libs/gl.git] / source / program.cpp
1 #include <algorithm>
2 #include <cstring>
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>
9 #include "buffer.h"
10 #include "error.h"
11 #include "misc.h"
12 #include "program.h"
13 #include "shader.h"
14
15 using namespace std;
16
17 namespace Msp {
18 namespace GL {
19
20 Program::Program()
21 {
22         init();
23 }
24
25 Program::Program(const ProgramBuilder::StandardFeatures &features)
26 {
27         init();
28
29         ProgramBuilder builder(features);
30         builder.add_shaders(*this);
31         if(!features.transform)
32                 link();
33 }
34
35 Program::Program(const string &vert, const string &frag)
36 {
37         init();
38
39         attach_shader_owned(new VertexShader(vert));
40         attach_shader_owned(new FragmentShader(frag));
41         link();
42 }
43
44 void Program::init()
45 {
46         static Require _req(ARB_shader_objects);
47
48         linked = false;
49         id = glCreateProgram();
50 }
51
52 Program::~Program()
53 {
54         for(ShaderList::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
55                 delete *i;
56         glDeleteProgram(id);
57 }
58
59 void Program::attach_shader(Shader &shader)
60 {
61         if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
62         {
63                 glAttachShader(id, shader.get_id());
64                 shaders.push_back(&shader);
65         }
66 }
67
68 void Program::attach_shader_owned(Shader *shader)
69 {
70         attach_shader(*shader);
71         if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end())
72                 owned_data.push_back(shader);
73 }
74
75 void Program::detach_shader(Shader &shader)
76 {
77         ShaderList::iterator i = remove(shaders.begin(), shaders.end(), &shader);
78         if(i!=shaders.end())
79         {
80                 shaders.erase(i, shaders.end());
81                 glDetachShader(id, shader.get_id());
82         }
83 }
84
85 void Program::bind_attribute(unsigned index, const string &name)
86 {
87         static Require _req(ARB_vertex_shader);
88         glBindAttribLocation(id, index, name.c_str());
89 }
90
91 void Program::link()
92 {
93         for(ShaderList::iterator i=shaders.begin(); i!=shaders.end(); ++i)
94                 if(!(*i)->is_compiled())
95                         (*i)->compile();
96
97         uniforms.clear();
98         legacy_vars = false;
99
100         glLinkProgram(id);
101         linked = get_program_i(id, GL_LINK_STATUS);
102         if(!linked)
103                 throw compile_error(get_info_log());
104
105         unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
106         vector<UniformInfo *> uniforms_by_index(count);
107         for(unsigned i=0; i<count; ++i)
108         {
109                 char name[128];
110                 int len = 0;
111                 int size;
112                 GLenum type;
113                 glGetActiveUniform(id, i, sizeof(name), &len, &size, &type, name);
114                 if(len && strncmp(name, "gl_", 3))
115                 {
116                         /* Some implementations report the first element of a uniform array,
117                         others report just the name of an array. */
118                         if(len>3 && !strcmp(name+len-3, "[0]"))
119                                 name[len-3] = 0;
120
121                         UniformInfo &info = uniforms[name];
122                         info.block = 0;
123                         info.name = name;
124                         info.size = size;
125                         info.array_stride = 0;
126                         info.matrix_stride = 0;
127                         info.type = type;
128                         uniforms_by_index[i] = &info;
129                 }
130                 else
131                         legacy_vars = true;
132         }
133
134         count = get_program_i(id, GL_ACTIVE_ATTRIBUTES);
135         for(unsigned i=0; i<count; ++i)
136         {
137                 char name[128];
138                 int len = 0;
139                 int size;
140                 GLenum type;
141                 glGetActiveAttrib(id, i, sizeof(name), &len, &size, &type, name);
142                 if(len && !strncmp(name, "gl_", 3))
143                         legacy_vars = true;
144         }
145
146         if(ARB_uniform_buffer_object)
147         {
148                 count = get_program_i(id, GL_ACTIVE_UNIFORM_BLOCKS);
149                 for(unsigned i=0; i<count; ++i)
150                 {
151                         char name[128];
152                         int len;
153                         glGetActiveUniformBlockName(id, i, sizeof(name), &len, name);
154                         UniformBlockInfo &info = uniform_blocks[name];
155                         info.name = name;
156
157                         int value;
158                         glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
159                         info.data_size = value;
160
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)
165                         {
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;
170                         }
171
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];
177
178                         indices2.clear();
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())
183                         {
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];
187                         }
188
189                         indices2.clear();
190                         for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
191                         {
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);
197                         }
198                         if(!indices2.empty())
199                         {
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];
203                         }
204
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);
209                 }
210         }
211
212         vector<const UniformInfo *> blockless_uniforms;
213         for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
214                 if(!i->second.block)
215                 {
216                         i->second.location = glGetUniformLocation(id, i->second.name.c_str());
217                         blockless_uniforms.push_back(&i->second);
218                 }
219
220         uniform_layout_hash = compute_layout_hash(blockless_uniforms);
221 }
222
223 unsigned Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
224 {
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);
229 }
230
231 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
232 {
233         return uni1->location<uni2->location;
234 }
235
236 string Program::get_info_log() const
237 {
238         GLsizei len = get_program_i(id, GL_INFO_LOG_LENGTH);
239         char *buf = new char[len+1];
240         glGetProgramInfoLog(id, len+1, &len, buf);
241         string log(buf, len);
242         delete[] buf;
243         return log;
244 }
245
246 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
247 {
248         return get_item(uniform_blocks, name);
249 }
250
251 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
252 {
253         return get_item(uniforms, name);
254 }
255
256 int Program::get_uniform_location(const string &n) const
257 {
258         UniformMap::const_iterator i = uniforms.find(n);
259         if(i==uniforms.end())
260         {
261                 if(n[n.size()-1]==']')
262                 {
263                         string::size_type open_bracket = n.rfind('[');
264                         if(open_bracket!=string::npos)
265                         {
266                                 /* The requested name looks like an array.  glGetActiveUniform only
267                                 gives us the first element of the array, so try to look that up and
268                                 add an offset. */
269                                 unsigned offset = lexical_cast<unsigned>(n.substr(open_bracket+1, n.size()-2-open_bracket));
270                                 i = uniforms.find(n.substr(0, open_bracket));
271                                 if(i!=uniforms.end() && !i->second.block && offset<i->second.size)
272                                         return i->second.location+offset;
273                         }
274                 }
275                 return -1;
276         }
277
278         return i->second.block ? -1 : i->second.location;
279 }
280
281 void Program::bind() const
282 {
283         if(!linked)
284                 throw invalid_operation("Program::bind");
285
286         if(!set_current(this))
287                 return;
288
289         glUseProgram(id);
290 }
291
292 void Program::unbind()
293 {
294         if(!set_current(0))
295                 return;
296
297         glUseProgram(0);
298 }
299
300
301 Program::Loader::Loader(Program &p):
302         DataFile::ObjectLoader<Program>(p)
303 {
304         add("attribute",       &Loader::attribute);
305         add("fragment_shader", &Loader::fragment_shader);
306         add("standard",        &Loader::standard);
307         add("vertex_shader",   &Loader::vertex_shader);
308 }
309
310 void Program::Loader::finish()
311 {
312         obj.link();
313 }
314
315 void Program::Loader::attribute(unsigned i, const string &n)
316 {
317         obj.bind_attribute(i, n);
318 }
319
320 void Program::Loader::fragment_shader(const string &src)
321 {
322         obj.attach_shader_owned(new FragmentShader(src));
323 }
324
325 void Program::Loader::standard()
326 {
327         ProgramBuilder::StandardFeatures feat;
328         load_sub(feat);
329         ProgramBuilder builder(feat);
330         builder.add_shaders(obj);
331 }
332
333 void Program::Loader::vertex_shader(const string &src)
334 {
335         obj.attach_shader_owned(new VertexShader(src));
336 }
337
338 } // namespace GL
339 } // namespace Msp