]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Remove the specialized setters from UniformBlock
[libs/gl.git] / source / programdata.cpp
1 #include "color.h"
2 #include "error.h"
3 #include "extension.h"
4 #include "matrix.h"
5 #include "program.h"
6 #include "programdata.h"
7 #include "uniform.h"
8 #include "uniformblock.h"
9 #include "vector.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GL {
15
16 ProgramData::ProgramData():
17         modified(false)
18 {
19         static RequireExtension _ext("GL_ARB_shader_objects");
20 }
21
22 // Blocks are intentionally left uncopied
23 ProgramData::ProgramData(const ProgramData &other):
24         uniforms(other.uniforms),
25         modified(false)
26 {
27         for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
28                 i->second = i->second->clone();
29 }
30
31 ProgramData::~ProgramData()
32 {
33         for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
34                 delete i->second;
35         for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
36                 delete i->second.block;
37 }
38
39 void ProgramData::uniform(const string &name, Uniform *uni)
40 {
41         UniformMap::iterator i = uniforms.find(name);
42         if(i!=uniforms.end())
43         {
44                 delete i->second;
45                 i->second = uni;
46         }
47         else
48                 uniforms[name] = uni;
49
50         modified = true;
51 }
52
53 void ProgramData::uniform(const string &name, int v)
54 {
55         uniform(name, new Uniform1i(v));
56 }
57
58 void ProgramData::uniform(const string &name, float v)
59 {
60         uniform(name, new Uniform1f(v));
61 }
62
63 void ProgramData::uniform(const string &name, float v0, float v1)
64 {
65         float va[2] = { v0, v1 };
66         uniform2(name, va);
67 }
68
69 void ProgramData::uniform2(const string &name, const float *v)
70 {
71         uniform(name, new Uniform2f(v));
72 }
73
74 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
75 {
76         float va[3] = { v0, v1, v2 };
77         uniform3(name, va);
78 }
79
80 void ProgramData::uniform(const string &name, const Vector3 &v)
81 {
82         uniform(name, v.x, v.y, v.z);
83 }
84
85 void ProgramData::uniform3(const string &name, const float *v)
86 {
87         uniform(name, new Uniform3f(v));
88 }
89
90 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
91 {
92         float va[4] = { v0, v1, v2, v3 };
93         uniform4(name, va);
94 }
95
96 void ProgramData::uniform(const string &name, const Vector4 &v)
97 {
98         uniform(name, v.x, v.y, v.z, v.w);
99 }
100
101 void ProgramData::uniform(const string &name, const Color &c)
102 {
103         uniform(name, c.r, c.g, c.b, c.a);
104 }
105
106 void ProgramData::uniform4(const string &name, const float *v)
107 {
108         uniform(name, new Uniform4f(v));
109 }
110
111 void ProgramData::uniform_matrix4(const string &name, const float *v)
112 {
113         uniform(name, new UniformMatrix4x4f(v));
114 }
115
116 void ProgramData::uniform_matrix4(const string &name, const Matrix &m)
117 {
118         float v[16];
119         copy(m.data(), m.data()+16, v);
120         uniform_matrix4(name, v);
121 }
122
123 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
124 {
125         uniform(name, new UniformArray<Uniform1f>(n, v));
126 }
127
128 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
129 {
130         uniform(name, new UniformArray<Uniform2f>(n, v));
131 }
132
133 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
134 {
135         uniform(name, new UniformArray<Uniform3f>(n, v));
136 }
137
138 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
139 {
140         uniform(name, new UniformArray<Uniform4f>(n, v));
141 }
142
143 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
144 {
145         uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
146 }
147
148 const UniformBlock &ProgramData::get_block(const Program &prog) const
149 {
150         if(modified)
151         {
152                 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
153                         i->second.dirty = true;
154                 modified = false;
155         }
156
157         unsigned layout_hash = prog.get_uniform_layout_hash();
158         map<unsigned, Block>::iterator i = blocks.find(layout_hash);
159         if(i==blocks.end())
160         {
161                 i = blocks.insert(BlockMap::value_type(layout_hash, Block())).first;
162                 i->second.dirty = true;
163                 i->second.block = new UniformBlock;
164         }
165
166         UniformBlock &block = *i->second.block;
167         if(i->second.dirty)
168         {
169                 for(UniformMap::const_iterator j=uniforms.begin(); j!=uniforms.end(); ++j)
170                 {
171                         int loc = prog.get_uniform_location(j->first);
172                         if(loc>=0)
173                                 block.attach(loc, *j->second);
174                 }
175                 i->second.dirty = false;
176         }
177
178         return block;
179 }
180
181 void ProgramData::apply() const
182 {
183         const Program *prog = Program::current();
184         if(!prog)
185                 throw invalid_operation("ProgramData::apply");
186
187         const UniformBlock &block = get_block(*prog);
188         block.apply(-1);
189 }
190
191
192 ProgramData::Block::Block():
193         dirty(false),
194         block(0)
195 { }
196
197
198 ProgramData::Loader::Loader(ProgramData &pd):
199         DataFile::ObjectLoader<ProgramData>(pd)
200 {
201         add("uniform1i", &Loader::uniform1i);
202         add("uniform1f", &Loader::uniform1f);
203         add("uniform2f", &Loader::uniform2f);
204         add("uniform3f", &Loader::uniform3f);
205         add("uniform4f", &Loader::uniform4f);
206 }
207
208 void ProgramData::Loader::uniform1i(const string &n, int v)
209 {
210         obj.uniform(n, v);
211 }
212
213 void ProgramData::Loader::uniform1f(const string &n, float v)
214 {
215         obj.uniform(n, v);
216 }
217
218 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
219 {
220         obj.uniform(n, v0, v1);
221 }
222
223 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
224 {
225         obj.uniform(n, v0, v1, v2);
226 }
227
228 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
229 {
230         obj.uniform(n, v0, v1, v2, v3);
231 }
232
233 } // namespace GL
234 } // namespace Msp