X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Funiformblock.cpp;fp=source%2Fcore%2Funiformblock.cpp;h=cc8945cf2093358d7df19ccebfe5e92ee9fffa61;hb=7aaec9a70b8d7733429bec043f8e33e02956f266;hp=0000000000000000000000000000000000000000;hpb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;p=libs%2Fgl.git diff --git a/source/core/uniformblock.cpp b/source/core/uniformblock.cpp new file mode 100644 index 00000000..cc8945cf --- /dev/null +++ b/source/core/uniformblock.cpp @@ -0,0 +1,91 @@ +#include +#include +#include +#include "buffer.h" +#include "color.h" +#include "error.h" +#include "matrix.h" +#include "uniform.h" +#include "uniformblock.h" +#include "vector.h" + +using namespace std; + +namespace Msp { +namespace GL { + +UniformBlock::UniformBlock(): + size(0), + buf_range(0) +{ + static Require _req(ARB_shader_objects); +} + +UniformBlock::UniformBlock(unsigned s): + size(s), + buf_range(0) +{ + static Require _req(ARB_uniform_buffer_object); + + if(!size) + throw invalid_argument("UniformBlock::UniformBlock"); + data.resize(size); +} + +UniformBlock::~UniformBlock() +{ + delete buf_range; +} + +unsigned UniformBlock::get_alignment() const +{ + return BufferRange::get_uniform_buffer_alignment(); +} + +void UniformBlock::location_changed(Buffer *buf, unsigned off, unsigned) const +{ + delete buf_range; + buf_range = new BufferRange(*buf, off, size); +} + +void UniformBlock::attach(int index, const Uniform &uni) +{ + if(size) + throw invalid_operation("UniformBlock::attach"); + + uniforms[index] = &uni; +} + +void UniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni) +{ + if(size) + { + uni.store(info, &data[info.location]); + dirty = true; + } + else + uniforms[info.location] = &uni; +} + +void UniformBlock::apply(int index) const +{ + if((index>=0) != (size>0)) + throw invalid_operation("UniformBlock::apply"); + + if(size) + { + if(!get_buffer()) + throw invalid_operation("UniformBlock::apply"); + + refresh(); + buf_range->bind_to(UNIFORM_BUFFER, index); + } + else + { + for(map::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i) + i->second->apply(i->first); + } +} + +} // namespace GL +} // namespace Msp