]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/uniformblock.cpp
Move backend information into Device
[libs/gl.git] / source / core / uniformblock.cpp
index f114ea584d04b7ff35ef550348febd4f9c38585d..adadf0561c8051c54d06c2ac43a3a3608e6539dd 100644 (file)
-#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 <algorithm>
+#include "device.h"
 #include "uniformblock.h"
-#include "vector.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
-DefaultUniformBlock::DefaultUniformBlock()
-{
-       static Require _req(ARB_shader_objects);
-}
-
-void DefaultUniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
-{
-       if(info.block->bind_point>=0)
-               throw invalid_argument("DefaultUniformBlock::attach");
-
-       attach(info.location, uni);
-}
-
-void DefaultUniformBlock::attach(int index, const Uniform &uni)
-{
-       if(index<0)
-               return;
-
-       if(static_cast<unsigned>(index)>=uniforms.size())
-               uniforms.resize(index+1, 0);
-       uniforms[index] = &uni;
-}
-
-void DefaultUniformBlock::apply(int index) const
-{
-       if(index>=0)
-               throw invalid_argument("DefaultUniformBlock::apply");
-
-       for(unsigned i=0; i<uniforms.size(); ++i)
-               if(uniforms[i])
-                       uniforms[i]->apply(i);
-}
+UniformBlock::UniformBlock(const ReflectData::UniformBlockInfo &info):
+       UniformBlockBackend(info.bind_point>=0),
+       data(info.data_size)
+{ }
 
-
-BufferBackedUniformBlock::BufferBackedUniformBlock(unsigned s):
-       size(s),
-       data(size),
-       buf_range(0)
-{
-       static Require _req(ARB_shader_objects);
-       static Require _req2(ARB_uniform_buffer_object);
-
-       if(!size)
-               throw invalid_argument("BufferBackedUniformBlock::BufferBackedUniformBlock");
-}
-
-BufferBackedUniformBlock::~BufferBackedUniformBlock()
-{
-       delete buf_range;
-}
-
-unsigned BufferBackedUniformBlock::get_alignment() const
+size_t UniformBlock::get_alignment() const
 {
-       return BufferRange::get_uniform_buffer_alignment();
+       return Device::get_current().get_info().limits.uniform_buffer_alignment;
 }
 
-void BufferBackedUniformBlock::location_changed(Buffer *buf, unsigned off, unsigned) const
+void UniformBlock::store(const ReflectData::UniformInfo &info, size_t array_size, const void *value)
 {
-       delete buf_range;
-       buf_range = buf->create_range(off, size);
-}
+       array_size = min(array_size, max<size_t>(info.array_size, 1U));
 
-void BufferBackedUniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni)
-{
+       char *store_ptr;
+       bool packed;
        if(info.block->bind_point<0)
-               throw invalid_argument("BufferBackedUniformBlock::attach");
+       {
+               if(info.location<0)
+                       return;
+
+               store_ptr = data.data()+info.location*16;
+               packed = true;
+       }
+       else
+       {
+               store_ptr = data.data()+info.offset;
+               if(array_size!=1 && info.array_stride!=get_type_size(info.type))
+                       packed = false;
+               else if(is_matrix(info.type))
+                       packed = (info.matrix_stride==get_type_size(get_matrix_column_type(info.type)));
+               else
+                       packed = true;
+       }
+
+       const char *value_ptr = static_cast<const char *>(value);
+       if(packed)
+       {
+               const char *data_end = value_ptr+array_size*get_type_size(info.type);
+               copy(value_ptr, data_end, store_ptr);
+       }
+       else if(is_matrix(info.type))
+       {
+               unsigned col_size = get_type_size(get_matrix_column_type(info.type));
+               unsigned cols = get_type_size(info.type)/col_size;
+               for(unsigned i=0; i<array_size; ++i)
+               {
+                       char *elem_ptr = store_ptr;
+                       for(unsigned j=0; j<cols; ++j)
+                       {
+                               copy(value_ptr, value_ptr+col_size, elem_ptr);
+                               value_ptr += col_size;
+                               elem_ptr += info.matrix_stride;
+                       }
+                       store_ptr += info.array_stride;
+               }
+       }
+       else
+       {
+               unsigned elem_size = get_type_size(info.type);
+               for(unsigned i=0; i<array_size; ++i)
+               {
+                       copy(value_ptr, value_ptr+elem_size, store_ptr);
+                       value_ptr += elem_size;
+                       store_ptr += info.array_stride;
+               }
+       }
 
-       uni.store(info, &data[info.offset]);
        dirty = true;
 }
 
-void BufferBackedUniformBlock::apply(int index) const
-{
-       if(index<0)
-               throw invalid_argument("BufferBackedUniformBlock::apply");
-       if(!get_buffer())
-               throw invalid_operation("UniformBlock::apply");
-
-       refresh();
-       buf_range->bind_to(UNIFORM_BUFFER, index);
-}
-
 } // namespace GL
 } // namespace Msp