]> git.tdb.fi Git - libs/gl.git/blob - source/uniformblock.cpp
Initialize buf_range as null
[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 void UniformBlock::attach(int index, const Uniform &uni)
26 {
27         uniforms[index] = &uni;
28 }
29
30 void UniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
31 {
32         uniforms[info.location] = &uni;
33         if(buffer)
34         {
35                 uni.store(info, &data[info.location]);
36                 dirty = true;
37         }
38 }
39
40 void UniformBlock::apply(int index) const
41 {
42         if((index>=0) != (buffer!=0))
43                 throw invalid_operation("UniformBlock::apply");
44
45         if(buffer)
46         {
47                 if(dirty)
48                 {
49                         update_buffer_data();
50                         if(!buf_range)
51                                 buf_range = new BufferRange(*buffer, buffer_offset, size);
52                 }
53                 buf_range->bind_to(UNIFORM_BUFFER, index);
54         }
55         else
56         {
57                 for(map<int, const Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
58                         i->second->apply(i->first);
59         }
60 }
61
62 } // namespace GL
63 } // namespace Msp