6 #include "programdata.h"
8 #include "uniformblock.h"
16 ProgramData::ProgramData():
19 static RequireExtension _ext("GL_ARB_shader_objects");
22 // Blocks are intentionally left uncopied
23 ProgramData::ProgramData(const ProgramData &other):
24 uniforms(other.uniforms),
27 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
28 i->second = i->second->clone();
31 ProgramData::~ProgramData()
33 for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
35 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
36 delete i->second.block;
39 void ProgramData::uniform(const string &name, Uniform *uni)
41 UniformMap::iterator i = uniforms.find(name);
53 void ProgramData::uniform(const string &name, int v)
55 uniform(name, new Uniform1i(v));
58 void ProgramData::uniform(const string &name, float v)
60 uniform(name, new Uniform1f(v));
63 void ProgramData::uniform(const string &name, float v0, float v1)
65 float va[2] = { v0, v1 };
69 void ProgramData::uniform2(const string &name, const float *v)
71 uniform(name, new Uniform2f(v));
74 void ProgramData::uniform(const string &name, float v0, float v1, float v2)
76 float va[3] = { v0, v1, v2 };
80 void ProgramData::uniform(const string &name, const Vector3 &v)
82 uniform(name, v.x, v.y, v.z);
85 void ProgramData::uniform3(const string &name, const float *v)
87 uniform(name, new Uniform3f(v));
90 void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
92 float va[4] = { v0, v1, v2, v3 };
96 void ProgramData::uniform(const string &name, const Vector4 &v)
98 uniform(name, v.x, v.y, v.z, v.w);
101 void ProgramData::uniform(const string &name, const Color &c)
103 uniform(name, c.r, c.g, c.b, c.a);
106 void ProgramData::uniform4(const string &name, const float *v)
108 uniform(name, new Uniform4f(v));
111 void ProgramData::uniform_matrix4(const string &name, const float *v)
113 uniform(name, new UniformMatrix4x4f(v));
116 void ProgramData::uniform_matrix4(const string &name, const Matrix &m)
119 copy(m.data(), m.data()+16, v);
120 uniform_matrix4(name, v);
123 void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
125 uniform(name, new UniformArray<Uniform1f>(n, v));
128 void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
130 uniform(name, new UniformArray<Uniform2f>(n, v));
133 void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
135 uniform(name, new UniformArray<Uniform3f>(n, v));
138 void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
140 uniform(name, new UniformArray<Uniform4f>(n, v));
143 void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
145 uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
148 const UniformBlock &ProgramData::get_block(const Program &prog) const
152 for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
153 i->second.dirty = true;
157 unsigned layout_hash = prog.get_uniform_layout_hash();
158 map<unsigned, Block>::iterator i = blocks.find(layout_hash);
161 i = blocks.insert(BlockMap::value_type(layout_hash, Block())).first;
162 i->second.dirty = true;
163 i->second.block = new UniformBlock;
166 UniformBlock &block = *i->second.block;
169 for(UniformMap::const_iterator j=uniforms.begin(); j!=uniforms.end(); ++j)
171 int loc = prog.get_uniform_location(j->first);
173 block.attach(loc, *j->second);
175 i->second.dirty = false;
181 void ProgramData::apply() const
183 const Program *prog = Program::current();
185 throw invalid_operation("ProgramData::apply");
187 const UniformBlock &block = get_block(*prog);
192 ProgramData::Block::Block():
198 ProgramData::Loader::Loader(ProgramData &pd):
199 DataFile::ObjectLoader<ProgramData>(pd)
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);
208 void ProgramData::Loader::uniform1i(const string &n, int v)
213 void ProgramData::Loader::uniform1f(const string &n, float v)
218 void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
220 obj.uniform(n, v0, v1);
223 void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
225 obj.uniform(n, v0, v1, v2);
228 void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
230 obj.uniform(n, v0, v1, v2, v3);