]> git.tdb.fi Git - libs/gl.git/blob - source/core/uniformblock.cpp
Store implementation limits in a central struct
[libs/gl.git] / source / core / 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 "deviceinfo.h"
7 #include "error.h"
8 #include "matrix.h"
9 #include "uniform.h"
10 #include "uniformblock.h"
11 #include "vector.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GL {
17
18 DefaultUniformBlock::DefaultUniformBlock()
19 {
20         static Require _req(ARB_shader_objects);
21 }
22
23 void DefaultUniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
24 {
25         if(info.block->bind_point>=0)
26                 throw invalid_argument("DefaultUniformBlock::attach");
27
28         attach(info.location, uni);
29 }
30
31 void DefaultUniformBlock::attach(int index, const Uniform &uni)
32 {
33         if(index<0)
34                 return;
35
36         if(static_cast<unsigned>(index)>=uniforms.size())
37                 uniforms.resize(index+1, 0);
38         uniforms[index] = &uni;
39 }
40
41 void DefaultUniformBlock::apply(int index) const
42 {
43         if(index>=0)
44                 throw invalid_argument("DefaultUniformBlock::apply");
45
46         for(unsigned i=0; i<uniforms.size(); ++i)
47                 if(uniforms[i])
48                         uniforms[i]->apply(i);
49 }
50
51
52 BufferBackedUniformBlock::BufferBackedUniformBlock(unsigned s):
53         size(s),
54         data(size),
55         buf_range(0)
56 {
57         static Require _req(ARB_shader_objects);
58         static Require _req2(ARB_uniform_buffer_object);
59
60         if(!size)
61                 throw invalid_argument("BufferBackedUniformBlock::BufferBackedUniformBlock");
62 }
63
64 BufferBackedUniformBlock::~BufferBackedUniformBlock()
65 {
66         delete buf_range;
67 }
68
69 unsigned BufferBackedUniformBlock::get_alignment() const
70 {
71         return Limits::get_global().uniform_buffer_alignment;
72 }
73
74 void BufferBackedUniformBlock::location_changed(Buffer *buf, unsigned off, unsigned) const
75 {
76         delete buf_range;
77         buf_range = buf->create_range(off, size);
78 }
79
80 void BufferBackedUniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
81 {
82         if(info.block->bind_point<0)
83                 throw invalid_argument("BufferBackedUniformBlock::attach");
84
85         uni.store(info, &data[info.offset]);
86         dirty = true;
87 }
88
89 void BufferBackedUniformBlock::apply(int index) const
90 {
91         if(index<0)
92                 throw invalid_argument("BufferBackedUniformBlock::apply");
93         if(!get_buffer())
94                 throw invalid_operation("UniformBlock::apply");
95
96         refresh();
97         buf_range->bind_to(UNIFORM_BUFFER, index);
98 }
99
100 } // namespace GL
101 } // namespace Msp