]> git.tdb.fi Git - libs/gl.git/blob - source/program.cpp
Memory allocation changes
[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/strings/format.h>
6 #include "arb_shader_objects.h"
7 #include "arb_uniform_buffer_object.h"
8 #include "arb_vertex_shader.h"
9 #include "buffer.h"
10 #include "error.h"
11 #include "program.h"
12 #include "shader.h"
13
14 using namespace std;
15
16 namespace Msp {
17 namespace GL {
18
19 Program::Program()
20 {
21         init();
22 }
23
24 Program::Program(const ProgramBuilder::StandardFeatures &features)
25 {
26         init();
27
28         ProgramBuilder builder(features);
29         builder.add_shaders(*this);
30         if(!features.transform)
31                 link();
32 }
33
34 Program::Program(const string &vert, const string &frag)
35 {
36         init();
37
38         attach_shader_owned(new VertexShader(vert));
39         attach_shader_owned(new FragmentShader(frag));
40         link();
41 }
42
43 void Program::init()
44 {
45         static Require _req(ARB_shader_objects);
46
47         linked = false;
48         id = glCreateProgram();
49 }
50
51 Program::~Program()
52 {
53         for(ShaderList::iterator i=owned_data.begin(); i!=owned_data.end(); ++i)
54                 delete *i;
55         glDeleteProgram(id);
56 }
57
58 void Program::attach_shader(Shader &shader)
59 {
60         if(find(shaders.begin(), shaders.end(), &shader)==shaders.end())
61         {
62                 glAttachShader(id, shader.get_id());
63                 shaders.push_back(&shader);
64         }
65 }
66
67 void Program::attach_shader_owned(Shader *shader)
68 {
69         attach_shader(*shader);
70         if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end())
71                 owned_data.push_back(shader);
72 }
73
74 void Program::detach_shader(Shader &shader)
75 {
76         ShaderList::iterator i = remove(shaders.begin(), shaders.end(), &shader);
77         if(i!=shaders.end())
78         {
79                 shaders.erase(i, shaders.end());
80                 glDetachShader(id, shader.get_id());
81         }
82 }
83
84 void Program::bind_attribute(unsigned index, const string &name)
85 {
86         static Require _req(ARB_vertex_shader);
87         glBindAttribLocation(id, index, name.c_str());
88 }
89
90 void Program::link()
91 {
92         for(ShaderList::iterator i=shaders.begin(); i!=shaders.end(); ++i)
93                 if(!(*i)->is_compiled())
94                         (*i)->compile();
95
96         uniforms.clear();
97
98         glLinkProgram(id);
99         int value;
100         glGetProgramiv(id, GL_LINK_STATUS, &value);
101         if(!(linked = value))
102                 throw compile_error(get_info_log());
103
104         glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &value);
105         unsigned count = value;
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, 128, &len, &size, &type, name);
114                 if(len && strncmp(name, "gl_", 3))
115                 {
116                         UniformInfo &info = uniforms[name];
117                         info.block = 0;
118                         info.name = name;
119                         info.size = size;
120                         info.array_stride = 0;
121                         info.matrix_stride = 0;
122                         info.type = type;
123                         uniforms_by_index[i] = &info;
124                 }
125         }
126
127         if(ARB_uniform_buffer_object)
128         {
129                 glGetProgramiv(id, GL_ACTIVE_UNIFORM_BLOCKS, &value);
130                 count = value;
131                 for(unsigned i=0; i<count; ++i)
132                 {
133                         char name[128];
134                         int len;
135                         glGetActiveUniformBlockName(id, i, 128, &len, name);
136                         UniformBlockInfo &info = uniform_blocks[name];
137                         info.name = name;
138
139                         glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_DATA_SIZE, &value);
140                         info.data_size = value;
141
142                         glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS, &value);
143                         vector<int> indices(value);
144                         glGetActiveUniformBlockiv(id, i, GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES, &indices[0]);
145                         for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
146                         {
147                                 if(!uniforms_by_index[*j])
148                                         throw logic_error("Program::link");
149                                 info.uniforms.push_back(uniforms_by_index[*j]);
150                                 uniforms_by_index[*j]->block = &info;
151                         }
152
153                         vector<unsigned> indices2(indices.begin(), indices.end());
154                         vector<int> values(indices.size());
155                         glGetActiveUniformsiv(id, indices.size(), &indices2[0], GL_UNIFORM_OFFSET, &values[0]);
156                         for(unsigned j=0; j<indices.size(); ++j)
157                                 uniforms_by_index[indices[j]]->location = values[j];
158
159                         indices2.clear();
160                         for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
161                                 if(uniforms_by_index[*j]->size>1)
162                                         indices2.push_back(*j);
163                         if(!indices2.empty())
164                         {
165                                 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_ARRAY_STRIDE, &values[0]);
166                                 for(unsigned j=0; j<indices2.size(); ++j)
167                                         uniforms_by_index[indices[j]]->array_stride = values[j];
168                         }
169
170                         indices2.clear();
171                         for(vector<int>::iterator j=indices.begin(); j!=indices.end(); ++j)
172                         {
173                                 GLenum t = uniforms_by_index[*j]->type;
174                                 if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 ||
175                                         t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 ||
176                                         t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3)
177                                         indices2.push_back(*j);
178                         }
179                         if(!indices2.empty())
180                         {
181                                 glGetActiveUniformsiv(id, indices2.size(), &indices2[0], GL_UNIFORM_MATRIX_STRIDE, &values[0]);
182                                 for(unsigned j=0; j<indices2.size(); ++j)
183                                         uniforms_by_index[indices[j]]->matrix_stride = values[j];
184                         }
185
186                         sort(info.uniforms.begin(), info.uniforms.end(), uniform_location_compare);
187                         info.layout_hash = compute_layout_hash(info.uniforms);
188                         info.bind_point = info.layout_hash%BufferRange::get_n_uniform_buffer_bindings();
189                         glUniformBlockBinding(id, i, info.bind_point);
190                 }
191         }
192
193         vector<const UniformInfo *> blockless_uniforms;
194         for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
195                 if(!i->second.block)
196                 {
197                         i->second.location = glGetUniformLocation(id, i->second.name.c_str());
198                         blockless_uniforms.push_back(&i->second);
199                 }
200
201         uniform_layout_hash = compute_layout_hash(blockless_uniforms);
202 }
203
204 unsigned Program::compute_layout_hash(const vector<const UniformInfo *> &uniforms)
205 {
206         string layout_descriptor;
207         for(vector<const UniformInfo *>::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i)
208                 layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->size);
209         return hash32(layout_descriptor);
210 }
211
212 bool Program::uniform_location_compare(const UniformInfo *uni1, const UniformInfo *uni2)
213 {
214         return uni1->location<uni2->location;
215 }
216
217 string Program::get_info_log() const
218 {
219         GLsizei len = 0;
220         glGetProgramiv(id, GL_INFO_LOG_LENGTH, &len);
221         char *buf = new char[len+1];
222         glGetProgramInfoLog(id, len+1, &len, buf);
223         string log(buf, len);
224         delete[] buf;
225         return log;
226 }
227
228 const Program::UniformBlockInfo &Program::get_uniform_block_info(const string &name) const
229 {
230         return get_item(uniform_blocks, name);
231 }
232
233 const Program::UniformInfo &Program::get_uniform_info(const string &name) const
234 {
235         return get_item(uniforms, name);
236 }
237
238 int Program::get_uniform_location(const string &n) const
239 {
240         UniformMap::const_iterator i = uniforms.find(n);
241         if(i==uniforms.end())
242         {
243                 if(n[n.size()-1]==']')
244                 {
245                         string::size_type open_bracket = n.rfind('[');
246                         if(open_bracket!=string::npos)
247                         {
248                                 /* The requested name looks like an array.  glGetActiveUniform only
249                                 gives us the first element of the array, so try to look that up and
250                                 add an offset. */
251                                 unsigned offset = lexical_cast<unsigned>(n.substr(open_bracket+1, n.size()-2-open_bracket));
252                                 i = uniforms.find(n.substr(0, open_bracket)+"[0]");
253                                 if(i!=uniforms.end() && !i->second.block && offset<i->second.size)
254                                         return i->second.location+offset;
255                         }
256                 }
257                 return -1;
258         }
259
260         return i->second.block ? -1 : i->second.location;
261 }
262
263 void Program::bind() const
264 {
265         if(!linked)
266                 throw invalid_operation("Program::bind");
267
268         if(!set_current(this))
269                 return;
270
271         glUseProgram(id);
272 }
273
274 void Program::unbind()
275 {
276         if(!set_current(0))
277                 return;
278
279         glUseProgram(0);
280 }
281
282
283 Program::Loader::Loader(Program &p):
284         DataFile::ObjectLoader<Program>(p)
285 {
286         add("attribute",       &Loader::attribute);
287         add("fragment_shader", &Loader::fragment_shader);
288         add("standard",        &Loader::standard);
289         add("vertex_shader",   &Loader::vertex_shader);
290 }
291
292 void Program::Loader::finish()
293 {
294         obj.link();
295 }
296
297 void Program::Loader::attribute(unsigned i, const string &n)
298 {
299         obj.bind_attribute(i, n);
300 }
301
302 void Program::Loader::fragment_shader(const string &src)
303 {
304         obj.attach_shader_owned(new FragmentShader(src));
305 }
306
307 void Program::Loader::standard()
308 {
309         ProgramBuilder::StandardFeatures feat;
310         load_sub(feat);
311         ProgramBuilder builder(feat);
312         builder.add_shaders(obj);
313 }
314
315 void Program::Loader::vertex_shader(const string &src)
316 {
317         obj.attach_shader_owned(new VertexShader(src));
318 }
319
320 } // namespace GL
321 } // namespace Msp