]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/uniformblock.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / uniformblock.cpp
index b06ddf926a0f3a34159ed9a2b7921a1ad708d018..952fd2fde0f736bfab861938a0682a411d1e5389 100644 (file)
@@ -1,7 +1,5 @@
 #include <algorithm>
-#include <msp/gl/extensions/arb_shader_objects.h>
-#include <msp/gl/extensions/arb_uniform_buffer_object.h>
-#include "deviceinfo.h"
+#include "device.h"
 #include "uniformblock.h"
 
 using namespace std;
@@ -10,35 +8,32 @@ namespace Msp {
 namespace GL {
 
 UniformBlock::UniformBlock(const ReflectData::UniformBlockInfo &info):
+       UniformBlockBackend(info.bind_point>=0),
        data(info.data_size)
-{
-       static Require _req(ARB_shader_objects);
-       if(info.bind_point>=0)
-               static Require _req2(ARB_uniform_buffer_object);
-}
+{ }
 
-unsigned UniformBlock::get_alignment() const
+size_t UniformBlock::get_alignment() const
 {
-       return DeviceInfo::get_global().limits.uniform_buffer_alignment;
+       return Device::get_current().get_info().limits.uniform_buffer_alignment;
 }
 
-void UniformBlock::store(const ReflectData::UniformInfo &info, unsigned array_size, const void *value)
+void UniformBlock::store(const ReflectData::UniformInfo &info, size_t array_size, const void *value)
 {
-       array_size = min(array_size, max(info.array_size, 1U));
+       array_size = min(array_size, max<size_t>(info.array_size, 1U));
 
-       char *store_ptr;
+       size_t store_offset;
        bool packed;
-       if(info.block->bind_point<0)
+       if(info.block->bind_point==ReflectData::DEFAULT_BLOCK)
        {
                if(info.location<0)
                        return;
 
-               store_ptr = data.data()+info.location*16;
+               store_offset = info.location*16;
                packed = true;
        }
        else
        {
-               store_ptr = data.data()+info.offset;
+               store_offset = info.offset;
                if(array_size!=1 && info.array_stride!=get_type_size(info.type))
                        packed = false;
                else if(is_matrix(info.type))
@@ -47,16 +42,19 @@ void UniformBlock::store(const ReflectData::UniformInfo &info, unsigned array_si
                        packed = true;
        }
 
+       char *store_ptr = data.data()+store_offset;
        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);
+               size_t value_size = array_size*get_type_size(info.type);
+               check_store_range(store_offset, value_size);
+               copy(value_ptr, value_ptr+value_size, 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;
+               check_store_range(store_offset, (array_size-1)*info.array_stride+(cols-1)*info.matrix_stride+col_size);
                for(unsigned i=0; i<array_size; ++i)
                {
                        char *elem_ptr = store_ptr;
@@ -72,6 +70,7 @@ void UniformBlock::store(const ReflectData::UniformInfo &info, unsigned array_si
        else
        {
                unsigned elem_size = get_type_size(info.type);
+               check_store_range(store_offset, (array_size-1)*info.array_stride+elem_size);
                for(unsigned i=0; i<array_size; ++i)
                {
                        copy(value_ptr, value_ptr+elem_size, store_ptr);
@@ -80,7 +79,15 @@ void UniformBlock::store(const ReflectData::UniformInfo &info, unsigned array_si
                }
        }
 
-       dirty = true;
+       mark_dirty();
+}
+
+void UniformBlock::check_store_range(size_t offs, size_t size)
+{
+#ifdef DEBUG
+       if(offs>data.size() || offs+size>data.size())
+               throw out_of_range("UniformBlock::store");
+#endif
 }
 
 } // namespace GL