]> git.tdb.fi Git - libs/gl.git/blob - source/uniformblock.cpp
Improvements to Bufferable
[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 unsigned UniformBlock::get_alignment() const
31 {
32         return BufferRange::get_uniform_buffer_alignment();
33 }
34
35 void UniformBlock::offset_changed()
36 {
37         delete buf_range;
38         buf_range = 0;
39 }
40
41 void UniformBlock::upload_data() const
42 {
43         if(!buf_range)
44                 buf_range = new BufferRange(*get_buffer(), get_offset(), size);
45         buf_range->data(&data[0]);
46 }
47
48 void UniformBlock::attach(int index, const Uniform &uni)
49 {
50         uniforms[index] = &uni;
51 }
52
53 void UniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
54 {
55         uniforms[info.location] = &uni;
56         if(get_buffer())
57         {
58                 uni.store(info, &data[info.location]);
59                 dirty = true;
60         }
61 }
62
63 void UniformBlock::apply(int index) const
64 {
65         if((index>=0) != (get_buffer()!=0))
66                 throw invalid_operation("UniformBlock::apply");
67
68         if(get_buffer())
69         {
70                 if(dirty)
71                         update_buffer();
72                 buf_range->bind_to(UNIFORM_BUFFER, index);
73         }
74         else
75         {
76                 for(map<int, const Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
77                         i->second->apply(i->first);
78         }
79 }
80
81 } // namespace GL
82 } // namespace Msp