]> git.tdb.fi Git - libs/gl.git/blob - source/uniformblock.cpp
Alter the working logic of Bufferable to avoid some problems
[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         buf_range(0)
17 { }
18
19 UniformBlock::UniformBlock(unsigned s):
20         size(s),
21         data(size),
22         buf_range(0)
23 { }
24
25 UniformBlock::~UniformBlock()
26 {
27         delete buf_range;
28 }
29
30 void UniformBlock::offset_changed()
31 {
32         delete buf_range;
33         buf_range = 0;
34 }
35
36 void UniformBlock::upload_data() const
37 {
38         if(!buf_range)
39                 buf_range = new BufferRange(*buffer, buffer_offset, size);
40         buf_range->data(&data[0]);
41 }
42
43 void UniformBlock::attach(int index, const Uniform &uni)
44 {
45         uniforms[index] = &uni;
46 }
47
48 void UniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
49 {
50         uniforms[info.location] = &uni;
51         if(buffer)
52         {
53                 uni.store(info, &data[info.location]);
54                 dirty = true;
55         }
56 }
57
58 void UniformBlock::apply(int index) const
59 {
60         if((index>=0) != (buffer!=0))
61                 throw invalid_operation("UniformBlock::apply");
62
63         if(buffer)
64         {
65                 if(dirty)
66                         update_buffer_data();
67                 buf_range->bind_to(UNIFORM_BUFFER, index);
68         }
69         else
70         {
71                 for(map<int, const Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
72                         i->second->apply(i->first);
73         }
74 }
75
76 } // namespace GL
77 } // namespace Msp