]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/uniformblock.cpp
Rearrange soucre files into subdirectories
[libs/gl.git] / source / core / uniformblock.cpp
diff --git a/source/core/uniformblock.cpp b/source/core/uniformblock.cpp
new file mode 100644 (file)
index 0000000..cc8945c
--- /dev/null
@@ -0,0 +1,91 @@
+#include <stdexcept>
+#include <msp/gl/extensions/arb_shader_objects.h>
+#include <msp/gl/extensions/arb_uniform_buffer_object.h>
+#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<int, const Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
+                       i->second->apply(i->first);
+       }
+}
+
+} // namespace GL
+} // namespace Msp