]> git.tdb.fi Git - libs/gl.git/blob - source/uniformblock.cpp
Construct uniform buffers for named uniform blocks
[libs/gl.git] / source / uniformblock.cpp
1 #include <stdexcept>
2 #include "buffer.h"
3 #include "color.h"
4 #include "error.h"
5 #include "matrix.h"
6 #include "uniform.h"
7 #include "uniformblock.h"
8 #include "vector.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GL {
14
15 UniformBlock::UniformBlock()
16 { }
17
18 UniformBlock::UniformBlock(unsigned s):
19         size(s),
20         data(size)
21 { }
22
23 void UniformBlock::attach(int index, const Uniform &uni)
24 {
25         uniforms[index] = &uni;
26 }
27
28 void UniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
29 {
30         uniforms[info.location] = &uni;
31         if(buffer)
32         {
33                 uni.store(info, &data[info.location]);
34                 dirty = true;
35         }
36 }
37
38 void UniformBlock::apply(int index) const
39 {
40         if((index>=0) != (buffer!=0))
41                 throw invalid_operation("UniformBlock::apply");
42
43         if(buffer)
44         {
45                 if(dirty)
46                 {
47                         update_buffer_data();
48                         if(!buf_range)
49                                 buf_range = new BufferRange(*buffer, buffer_offset, size);
50                 }
51                 buf_range->bind_to(UNIFORM_BUFFER, index);
52         }
53         else
54         {
55                 for(map<int, const Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
56                         i->second->apply(i->first);
57         }
58 }
59
60 } // namespace GL
61 } // namespace Msp