-#include "color.h"
-#include "error.h"
-#include "extension.h"
-#include "matrix.h"
-#include "program.h"
-#include "programdata.h"
-#include "uniform.h"
-#include "uniformblock.h"
-#include "vector.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-ProgramData::ProgramData():
- modified(false)
-{
- static RequireExtension _ext("GL_ARB_shader_objects");
-}
-
-// Blocks are intentionally left uncopied
-ProgramData::ProgramData(const ProgramData &other):
- uniforms(other.uniforms),
- modified(false)
-{
- for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- i->second = i->second->clone();
-}
-
-ProgramData::~ProgramData()
-{
- for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- delete i->second;
- for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
- delete i->second.block;
-}
-
-void ProgramData::uniform(const string &name, Uniform *uni)
-{
- UniformMap::iterator i = uniforms.find(name);
- if(i!=uniforms.end())
- {
- delete i->second;
- i->second = uni;
- }
- else
- uniforms[name] = uni;
-
- modified = true;
-}
-
-void ProgramData::uniform(const string &name, int v)
-{
- uniform(name, new Uniform1i(v));
-}
-
-void ProgramData::uniform(const string &name, float v)
-{
- uniform(name, new Uniform1f(v));
-}
-
-void ProgramData::uniform(const string &name, float v0, float v1)
-{
- uniform(name, new Uniform2f(v0, v1));
-}
-
-void ProgramData::uniform2(const string &name, const float *v)
-{
- uniform(name, v[0], v[1]);
-}
-
-void ProgramData::uniform(const string &name, float v0, float v1, float v2)
-{
- uniform(name, new Uniform3f(v0, v1, v2));
-}
-
-void ProgramData::uniform(const string &name, const Vector3 &v)
-{
- uniform(name, v.x, v.y, v.z);
-}
-
-void ProgramData::uniform3(const string &name, const float *v)
-{
- uniform(name, v[0], v[1], v[2]);
-}
-
-void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
-{
- uniform(name, new Uniform4f(v0, v1, v2, v3));
-}
-
-void ProgramData::uniform(const string &name, const Vector4 &v)
-{
- uniform(name, v.x, v.y, v.z, v.w);
-}
-
-void ProgramData::uniform(const string &name, const Color &c)
-{
- uniform(name, c.r, c.g, c.b, c.a);
-}
-
-void ProgramData::uniform4(const string &name, const float *v)
-{
- uniform(name, v[0], v[1], v[2], v[3]);
-}
-
-void ProgramData::uniform_matrix4(const string &name, const float *v)
-{
- uniform(name, new UniformMatrix4x4f(v));
-}
-
-void ProgramData::uniform_matrix4(const string &name, const Matrix &m)
-{
- float v[16];
- copy(m.data(), m.data()+16, v);
- uniform_matrix4(name, v);
-}
-
-const UniformBlock &ProgramData::get_block(const Program &prog) const
-{
- if(modified)
- {
- for(BlockMap::iterator i=blocks.begin(); i!=blocks.end(); ++i)
- i->second.dirty = true;
- modified = false;
- }
-
- unsigned layout_hash = prog.get_uniform_layout_hash();
- map<unsigned, Block>::iterator i = blocks.find(layout_hash);
- if(i==blocks.end())
- {
- i = blocks.insert(BlockMap::value_type(layout_hash, Block())).first;
- i->second.dirty = true;
- i->second.block = new UniformBlock;
- }
-
- UniformBlock &block = *i->second.block;
- if(i->second.dirty)
- {
- for(UniformMap::const_iterator j=uniforms.begin(); j!=uniforms.end(); ++j)
- {
- int loc = prog.get_uniform_location(j->first);
- if(loc>=0)
- block.uniform(loc, *j->second);
- }
- i->second.dirty = false;
- }
-
- return block;
-}
-
-void ProgramData::apply() const
-{
- const Program *prog = Program::current();
- if(!prog)
- throw invalid_operation("ProgramData::apply");
-
- const UniformBlock &block = get_block(*prog);
- block.apply(-1);
-}
-
-
-ProgramData::Block::Block():
- dirty(false),
- block(0)
-{ }
-
-
-ProgramData::Loader::Loader(ProgramData &pd):
- DataFile::ObjectLoader<ProgramData>(pd)
-{
- add("uniform1i", &Loader::uniform1i);
- add("uniform1f", &Loader::uniform1f);
- add("uniform2f", &Loader::uniform2f);
- add("uniform3f", &Loader::uniform3f);
- add("uniform4f", &Loader::uniform4f);
-}
-
-void ProgramData::Loader::uniform1i(const string &n, int v)
-{
- obj.uniform(n, v);
-}
-
-void ProgramData::Loader::uniform1f(const string &n, float v)
-{
- obj.uniform(n, v);
-}
-
-void ProgramData::Loader::uniform2f(const string &n, float v0, float v1)
-{
- obj.uniform(n, v0, v1);
-}
-
-void ProgramData::Loader::uniform3f(const string &n, float v0, float v1, float v2)
-{
- obj.uniform(n, v0, v1, v2);
-}
-
-void ProgramData::Loader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
-{
- obj.uniform(n, v0, v1, v2, v3);
-}
-
-} // namespace GL
-} // namespace Msp