]> git.tdb.fi Git - libs/gl.git/blob - source/programdata.cpp
Rewrite the Uniform classes as templates
[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 const UniformBlock &ProgramData::get_block(const Program &prog) const
124 {
125         if(modified)
126         {
127                 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
128                         i->second.dirty = true;
129                 modified = false;
130         }
131
132         unsigned layout_hash = prog.get_uniform_layout_hash();
133         map<unsigned, Block>::iterator i = blocks.find(layout_hash);
134         if(i==blocks.end())
135         {
136                 i = blocks.insert(BlockMap::value_type(layout_hash, Block())).first;
137                 i->second.dirty = true;
138                 i->second.block = new UniformBlock;
139         }
140
141         UniformBlock &block = *i->second.block;
142         if(i->second.dirty)
143         {
144                 for(UniformMap::const_iterator j=uniforms.begin(); j!=uniforms.end(); ++j)
145                 {
146                         int loc = prog.get_uniform_location(j->first);
147                         if(loc>=0)
148                                 block.uniform(loc, *j->second);
149                 }
150                 i->second.dirty = false;
151         }
152
153         return block;
154 }
155
156 void ProgramData::apply() const
157 {
158         const Program *prog = Program::current();
159         if(!prog)
160                 throw invalid_operation("ProgramData::apply");
161
162         const UniformBlock &block = get_block(*prog);
163         block.apply(-1);
164 }
165
166
167 ProgramData::Block::Block():
168         dirty(false),
169         block(0)
170 { }
171
172
173 ProgramData::Loader::Loader(ProgramData &pd):
174         DataFile::ObjectLoader<ProgramData>(pd)
175 {
176         add("uniform1i", &Loader::uniform1i);
177         add("uniform1f", &Loader::uniform1f);
178         add("uniform2f", &Loader::uniform2f);
179         add("uniform3f", &Loader::uniform3f);
180         add("uniform4f", &Loader::uniform4f);
181 }
182
183 void ProgramData::Loader::uniform1i(const string &n, int v)
184 {
185         obj.uniform(n, v);
186 }
187
188 void ProgramData::Loader::uniform1f(const string &n, float v)
189 {
190         obj.uniform(n, v);
191 }
192
193 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
194 {
195         obj.uniform(n, v0, v1);
196 }
197
198 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
199 {
200         obj.uniform(n, v0, v1, v2);
201 }
202
203 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
204 {
205         obj.uniform(n, v0, v1, v2, v3);
206 }
207
208 } // namespace GL
209 } // namespace Msp