]> git.tdb.fi Git - libs/gl.git/blobdiff - source/uniformblock.cpp
Remove an XXX because I no longer remember how to reproduce the issue
[libs/gl.git] / source / uniformblock.cpp
index 2ff00fa221295ada6f8578cc89649b6936c70afe..be30ce5b101420364b5bcba45722f00842e16099 100644 (file)
@@ -1,5 +1,9 @@
 #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"
@@ -10,18 +14,89 @@ 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::offset_changed()
+{
+       delete buf_range;
+       buf_range = 0;
+}
+
+void UniformBlock::upload_data(char *target) const
+{
+       if(!buf_range)
+               buf_range = new BufferRange(*get_buffer(), get_offset(), size);
+
+       if(target)
+               copy(data.begin(), data.end(), target);
+       else
+               buf_range->data(&data[0]);
+}
+
 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)
-               throw logic_error("GL_ARB_uniform_buffer_object support not implemented yet");
+       if((index>=0) != (size>0))
+               throw invalid_operation("UniformBlock::apply");
+
+       if(size)
+       {
+               if(!get_buffer())
+                       throw invalid_operation("UniformBlock::apply");
 
-       for(map<int, const Uniform *>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
-               i->second->apply(i->first);
+               if(dirty)
+                       update_buffer();
+               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