]> git.tdb.fi Git - libs/gl.git/blob - source/uniformblock.cpp
Add some files which got forgotten in earlier commits
[libs/gl.git] / source / uniformblock.cpp
1 #include <stdexcept>
2 #include <msp/gl/extensions/arb_shader_objects.h>
3 #include <msp/gl/extensions/arb_uniform_buffer_object.h>
4 #include "buffer.h"
5 #include "color.h"
6 #include "error.h"
7 #include "matrix.h"
8 #include "uniform.h"
9 #include "uniformblock.h"
10 #include "vector.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GL {
16
17 UniformBlock::UniformBlock():
18         size(0),
19         buf_range(0)
20 {
21         static Require _req(ARB_shader_objects);
22 }
23
24 UniformBlock::UniformBlock(unsigned s):
25         size(s),
26         buf_range(0)
27 {
28         static Require _req(ARB_uniform_buffer_object);
29
30         if(!size)
31                 throw invalid_argument("UniformBlock::UniformBlock");
32         data.resize(size);
33 }
34
35 UniformBlock::~UniformBlock()
36 {
37         delete buf_range;
38 }
39
40 unsigned UniformBlock::get_alignment() const
41 {
42         return BufferRange::get_uniform_buffer_alignment();
43 }
44
45 void UniformBlock::location_changed(Buffer *buf, unsigned off, unsigned) const
46 {
47         delete buf_range;
48         buf_range = new BufferRange(*buf, off, size);
49 }
50
51 void UniformBlock::attach(int index, const Uniform &uni)
52 {
53         if(size)
54                 throw invalid_operation("UniformBlock::attach");
55
56         uniforms[index] = &uni;
57 }
58
59 void UniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
60 {
61         if(size)
62         {
63                 uni.store(info, &data[info.location]);
64                 dirty = true;
65         }
66         else
67                 uniforms[info.location] = &uni;
68 }
69
70 void UniformBlock::apply(int index) const
71 {
72         if((index>=0) != (size>0))
73                 throw invalid_operation("UniformBlock::apply");
74
75         if(size)
76         {
77                 if(!get_buffer())
78                         throw invalid_operation("UniformBlock::apply");
79
80                 refresh();
81                 buf_range->bind_to(UNIFORM_BUFFER, index);
82         }
83         else
84         {
85                 for(map<int, const Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
86                         i->second->apply(i->first);
87         }
88 }
89
90 } // namespace GL
91 } // namespace Msp