features.material = true;
features.reflection = true;
GL::Program shprog(features);
- GL::ProgramData shdata(shprog);
+ GL::ProgramData shdata;
shdata.uniform("environment", 0);
shdata.uniform("reflectivity", 0.5f);
mat.set_specular(GL::Color(0.45, 0.5, 0.4));
mat.set_shininess(50);
vector<GL::Program *> programs;
- vector<GL::ProgramData *> progdata;
for(unsigned i=0; i<12; ++i)
{
GL::Program::StandardFeatures feat;
programs.back()->bind_attribute(4, "tangent");
programs.back()->bind_attribute(5, "binormal");
programs.back()->link();
- progdata.push_back(new GL::ProgramData(*programs.back()));
- progdata.back()->uniform("normalmap", 1);
}
- else
- progdata.push_back(0);
}
+ GL::ProgramData progdata;
+ progdata.uniform("normalmap", 1);
+
GL::Lighting lighting;
GL::Light light;
light.set_position(GL::Vector4(0, 2, 3, 0));
for(unsigned i=0; i<12; ++i)
{
GL::MatrixStack::Push push(renderer.matrix_stack());
- renderer.set_shader(programs[i], progdata[i]);
+ renderer.set_shader(programs[i], &progdata);
renderer.matrix_stack() *= GL::Matrix::translation(-3.3+(i%4)*2.2, 0, -3.5+(i/4)*3.0);
renderer.matrix_stack() *= GL::Matrix::rotation(angle, 0, 0, 1);
mesh.draw(renderer);
AmbientOcclusion::AmbientOcclusion(unsigned w, unsigned h, float depth_ratio):
occlude_shader(vertex_shader, occlude_fs),
- occlude_shdata(occlude_shader),
combine_shader(vertex_shader, combine_fs),
- combine_shdata(combine_shader),
quad(VERTEX2)
{
occlusion.storage(GL::RGB, w, h);
shdata(0)
{
if(const Technique *tech = object.get_technique())
- {
- // XXX Should create separate ProgramData for each pass
- const RenderPass &pass = tech->get_pass(Tag());
- if(const Program *shprog = pass.get_shader_program())
- shdata = new ProgramData(*shprog);
- }
+ if(tech->has_shaders())
+ shdata = new ProgramData;
}
void AnimatedObject::set_matrix(const Matrix &m)
Bloom::Bloom(unsigned w, unsigned h):
blur_shader(blur_vs, blur_fs),
- blur_shdata_common(blur_shader),
- blur_shdata_x(blur_shader),
- blur_shdata_y(blur_shader),
combine_shader(combine_vs, combine_fs),
- combine_shdata(combine_shader),
quad(VERTEX2)
{
- int loc = blur_shader.get_uniform_location("delta");
- blur_shdata_x.uniform(loc, 1.0f/w, 0.0f);
- blur_shdata_y.uniform(loc, 0.0f, 1.0f/h);
+ blur_shdata[0].uniform("delta", 1.0f/w, 0.0f);
+ blur_shdata[1].uniform("delta", 0.0f, 1.0f/h);
blur_shdata_common.uniform("source", 0);
for(unsigned i=0; i<2; ++i)
{
Bind bind_tex(i ? tex[0] : src);
fbo.attach(COLOR_ATTACHMENT0, tex[i], 0);
- (i ? blur_shdata_y : blur_shdata_x).apply();
+ blur_shdata[i].apply();
quad.draw();
}
}
Texture2D tex[2];
Program blur_shader;
ProgramData blur_shdata_common;
- ProgramData blur_shdata_x;
- ProgramData blur_shdata_y;
+ ProgramData blur_shdata[2];
Program combine_shader;
ProgramData combine_shdata;
Texturing combine_texturing;
#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(const Program &p):
- program(p)
+ProgramData::ProgramData():
+ modified(false)
{
static RequireExtension _ext("GL_ARB_shader_objects");
}
+// Blocks are intentionally left uncopied
ProgramData::ProgramData(const ProgramData &other):
- program(other.program),
- data(other.data)
+ uniforms(other.uniforms),
+ modified(false)
{
- for(map<int, Uniform *>::iterator i=data.begin(); i!=data.end(); ++i)
+ for(UniformMap::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
i->second = i->second->clone();
}
ProgramData::~ProgramData()
{
- for(map<int, Uniform *>::iterator i=data.begin(); i!=data.end(); ++i)
+ 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(int index, Uniform *uni)
+void ProgramData::uniform(const string &name, Uniform *uni)
{
- map<int, Uniform *>::iterator i = data.find(index);
- if(i!=data.end())
+ UniformMap::iterator i = uniforms.find(name);
+ if(i!=uniforms.end())
{
delete i->second;
i->second = uni;
}
else
- data[index] = uni;
-}
-
-void ProgramData::uniform(int index, int v)
-{
- if(index>=0)
- uniform(index, new Uniform1i(v));
-}
-
-void ProgramData::uniform(int index, float v)
-{
- if(index>=0)
- uniform(index, new Uniform1f(v));
-}
-
-void ProgramData::uniform(int index, float v0, float v1)
-{
- if(index>=0)
- uniform(index, new Uniform2f(v0, v1));
-}
-
-void ProgramData::uniform2(int index, const float *v)
-{
- uniform(index, v[0], v[1]);
-}
-
-void ProgramData::uniform(int index, float v0, float v1, float v2)
-{
- if(index>=0)
- uniform(index, new Uniform3f(v0, v1, v2));
-}
-
-void ProgramData::uniform(int index, const Vector3 &v)
-{
- uniform(index, v.x, v.y, v.z);
-}
-
-void ProgramData::uniform3(int index, const float *v)
-{
- uniform(index, v[0], v[1], v[2]);
-}
-
-void ProgramData::uniform(int index, float v0, float v1, float v2, float v3)
-{
- if(index>=0)
- uniform(index, new Uniform4f(v0, v1, v2, v3));
-}
-
-void ProgramData::uniform(int index, const Vector4 &v)
-{
- uniform(index, v.x, v.y, v.z, v.w);
-}
-
-void ProgramData::uniform(int index, const Color &c)
-{
- uniform(index, c.r, c.g, c.b, c.a);
-}
-
-void ProgramData::uniform4(int index, const float *v)
-{
- uniform(index, v[0], v[1], v[2], v[3]);
-}
+ uniforms[name] = uni;
-void ProgramData::uniform_matrix4(int index, const float *v)
-{
- if(index>=0)
- uniform(index, new UniformMatrix4x4f(v));
-}
-
-void ProgramData::uniform_matrix4(int index, const Matrix &m)
-{
- if(index>=0)
- {
- float v[16];
- copy(m.data(), m.data()+16, v);
- uniform_matrix4(index, v);
- }
+ modified = true;
}
void ProgramData::uniform(const string &name, int v)
{
- uniform(program.get_uniform_location(name), v);
+ uniform(name, new Uniform1i(v));
}
void ProgramData::uniform(const string &name, float v)
{
- uniform(program.get_uniform_location(name), v);
+ uniform(name, new Uniform1f(v));
}
void ProgramData::uniform(const string &name, float v0, float v1)
{
- uniform(program.get_uniform_location(name), v0, v1);
+ uniform(name, new Uniform2f(v0, v1));
}
void ProgramData::uniform2(const string &name, const float *v)
{
- uniform2(program.get_uniform_location(name), v);
+ uniform(name, v[0], v[1]);
}
void ProgramData::uniform(const string &name, float v0, float v1, float v2)
{
- uniform(program.get_uniform_location(name), v0, v1, v2);
+ uniform(name, new Uniform3f(v0, v1, v2));
}
void ProgramData::uniform(const string &name, const Vector3 &v)
{
- uniform(program.get_uniform_location(name), v);
+ uniform(name, v.x, v.y, v.z);
}
void ProgramData::uniform3(const string &name, const float *v)
{
- uniform3(program.get_uniform_location(name), v);
+ uniform(name, v[0], v[1], v[2]);
}
void ProgramData::uniform(const string &name, float v0, float v1, float v2, float v3)
{
- uniform(program.get_uniform_location(name), v0, v1, v2, v3);
+ uniform(name, new Uniform4f(v0, v1, v2, v3));
}
void ProgramData::uniform(const string &name, const Vector4 &v)
{
- uniform(program.get_uniform_location(name), 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)
{
- uniform4(program.get_uniform_location(name), v);
+ uniform(name, v[0], v[1], v[2], v[3]);
}
void ProgramData::uniform_matrix4(const string &name, const float *v)
{
- uniform_matrix4(program.get_uniform_location(name), v);
+ uniform(name, new UniformMatrix4x4f(v));
}
void ProgramData::uniform_matrix4(const string &name, const Matrix &m)
{
- uniform_matrix4(program.get_uniform_location(name), 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
{
- for(map<int, Uniform *>::const_iterator i=data.begin(); i!=data.end(); ++i)
- i->second->apply(i->first);
+ 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)
{
class Matrix;
class Program;
class Uniform;
+class UniformBlock;
class Vector3;
class Vector4;
};
private:
- const Program &program;
- std::map<int, Uniform *> data;
+ struct Block
+ {
+ bool dirty;
+ UniformBlock *block;
+
+ Block();
+ };
+
+ typedef std::map<std::string, Uniform *> UniformMap;
+ typedef std::map<unsigned, Block> BlockMap;
+
+ UniformMap uniforms;
+ mutable BlockMap blocks;
+ mutable bool modified;
ProgramData &operator=(const ProgramData &);
public:
- explicit ProgramData(const Program &);
+ ProgramData();
ProgramData(const ProgramData &);
~ProgramData();
private:
- void uniform(int, Uniform *);
+ void uniform(const std::string &, Uniform *);
public:
- void uniform(int, int);
- void uniform(int, float);
- void uniform(int, float, float);
- void uniform2(int, const float *);
- void uniform(int, float, float, float);
- void uniform(int, const Vector3 &);
- void uniform3(int, const float *);
- void uniform(int, float, float, float, float);
- void uniform(int, const Vector4 &);
- void uniform(int, const Color &);
- void uniform4(int, const float *);
- void uniform_matrix4(int, const float *);
- void uniform_matrix4(int, const Matrix &);
-
void uniform(const std::string &, int);
void uniform(const std::string &, float);
void uniform(const std::string &, float, float);
void uniform_matrix4(const std::string &, const float *);
void uniform_matrix4(const std::string &, const Matrix &);
+ const UniformBlock &get_block(const Program &) const;
+
void apply() const;
};
if(!obj.shprog)
throw invalid_operation("RenderPass::Loader::uniforms");
if(!obj.shdata)
- obj.shdata = new ProgramData(*obj.shprog);
+ obj.shdata = new ProgramData;
load_sub(*obj.shdata);
}
--- /dev/null
+#include <stdexcept>
+#include "color.h"
+#include "matrix.h"
+#include "uniform.h"
+#include "uniformblock.h"
+#include "vector.h"
+
+using namespace std;
+
+namespace Msp {
+namespace GL {
+
+UniformBlock::~UniformBlock()
+{
+ for(map<int, Uniform *>::iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ delete i->second;
+}
+
+void UniformBlock::uniform(int index, Uniform *uni)
+{
+ map<int, Uniform *>::iterator i = uniforms.find(index);
+ if(i!=uniforms.end())
+ {
+ delete i->second;
+ i->second = uni;
+ }
+ else
+ uniforms[index] = uni;
+}
+
+void UniformBlock::uniform(int index, const Uniform &uni)
+{
+ uniform(index, uni.clone());
+}
+
+void UniformBlock::uniform(int index, int v)
+{
+ uniform(index, new Uniform1i(v));
+}
+
+void UniformBlock::uniform(int index, float v)
+{
+ uniform(index, new Uniform1f(v));
+}
+
+void UniformBlock::uniform(int index, float v0, float v1)
+{
+ uniform(index, new Uniform2f(v0, v1));
+}
+
+void UniformBlock::uniform2(int index, const float *v)
+{
+ uniform(index, v[0], v[1]);
+}
+
+void UniformBlock::uniform(int index, float v0, float v1, float v2)
+{
+ uniform(index, new Uniform3f(v0, v1, v2));
+}
+
+void UniformBlock::uniform(int index, const Vector3 &v)
+{
+ uniform(index, v.x, v.y, v.z);
+}
+
+void UniformBlock::uniform3(int index, const float *v)
+{
+ uniform(index, v[0], v[1], v[2]);
+}
+
+void UniformBlock::uniform(int index, float v0, float v1, float v2, float v3)
+{
+ uniform(index, new Uniform4f(v0, v1, v2, v3));
+}
+
+void UniformBlock::uniform(int index, const Vector4 &v)
+{
+ uniform(index, v.x, v.y, v.z, v.w);
+}
+
+void UniformBlock::uniform(int index, const Color &c)
+{
+ uniform(index, c.r, c.g, c.b, c.a);
+}
+
+void UniformBlock::uniform4(int index, const float *v)
+{
+ uniform(index, v[0], v[1], v[2], v[3]);
+}
+
+void UniformBlock::uniform_matrix4(int index, const float *v)
+{
+ uniform(index, new UniformMatrix4x4f(v));
+}
+
+void UniformBlock::uniform_matrix4(int index, const Matrix &m)
+{
+ float v[16];
+ copy(m.data(), m.data()+16, v);
+ uniform_matrix4(index, v);
+}
+
+void UniformBlock::apply(int index) const
+{
+ if(index>=0)
+ throw logic_error("GL_ARB_uniform_buffer_object support not implemented yet");
+
+ for(map<int, Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+ i->second->apply(i->first);
+}
+
+} // namespace GL
+} // namespace Msp
--- /dev/null
+#ifndef MSP_GL_UNIFORMBLOCK_H_
+#define MSP_GL_UNIFORMBLOCK_H_
+
+#include <map>
+
+namespace Msp {
+namespace GL {
+
+class Color;
+class Matrix;
+class Uniform;
+class Vector3;
+class Vector4;
+
+class UniformBlock
+{
+private:
+ std::map<int, Uniform *> uniforms;
+
+ UniformBlock(const UniformBlock &);
+ UniformBlock &operator=(const UniformBlock &);
+public:
+ UniformBlock() { }
+ ~UniformBlock();
+
+private:
+ void uniform(int, Uniform *);
+public:
+ void uniform(int, const Uniform &);
+ void uniform(int, int);
+ void uniform(int, float);
+ void uniform(int, float, float);
+ void uniform2(int, const float *);
+ void uniform(int, float, float, float);
+ void uniform(int, const Vector3 &);
+ void uniform3(int, const float *);
+ void uniform(int, float, float, float, float);
+ void uniform(int, const Vector4 &);
+ void uniform(int, const Color &);
+ void uniform4(int, const float *);
+ void uniform_matrix4(int, const float *);
+ void uniform_matrix4(int, const Matrix &);
+
+ void apply(int) const;
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif