X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Funiformblock.cpp;h=ebaed2a6872d6067d19113fc0f9e51436bbf1045;hb=4f24fdd97b1845ebfcd74913fbf076faf7984902;hp=25511d251e2361e56b67285b86b7e8add1f14585;hpb=492325880c47450f50dcdebdcb336b37230ef867;p=libs%2Fgl.git diff --git a/source/core/uniformblock.cpp b/source/core/uniformblock.cpp index 25511d25..ebaed2a6 100644 --- a/source/core/uniformblock.cpp +++ b/source/core/uniformblock.cpp @@ -1,93 +1,93 @@ -#include -#include -#include -#include "buffer.h" -#include "color.h" -#include "error.h" -#include "matrix.h" -#include "uniform.h" +#include +#include "device.h" #include "uniformblock.h" -#include "vector.h" using namespace std; namespace Msp { namespace GL { -DefaultUniformBlock::DefaultUniformBlock() -{ - static Require _req(ARB_shader_objects); -} +UniformBlock::UniformBlock(const ReflectData::UniformBlockInfo &info): + UniformBlockBackend(info.bind_point>=0), + data(info.data_size) +{ } -void DefaultUniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni) +size_t UniformBlock::get_alignment() const { - if(info.block->bind_point>=0) - throw invalid_argument("DefaultUniformBlock::attach"); - - uniforms[info.location] = &uni; + return Device::get_current().get_info().limits.uniform_buffer_alignment; } -void DefaultUniformBlock::attach(int index, const Uniform &uni) +void UniformBlock::store(const ReflectData::UniformInfo &info, size_t array_size, const void *value) { - uniforms[index] = &uni; -} - -void DefaultUniformBlock::apply(int index) const -{ - if(index>=0) - throw invalid_argument("DefaultUniformBlock::apply"); - - for(map::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i) - i->second->apply(i->first); -} - - -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"); -} + array_size = min(array_size, max(info.array_size, 1U)); + + size_t store_offset; + bool packed; + if(info.block->bind_point==ReflectData::DEFAULT_BLOCK) + { + if(info.location<0) + return; + + store_offset = info.location*16; + packed = true; + } + else + { + store_offset = 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; + } + + char *store_ptr = data.data()+store_offset; + const char *value_ptr = static_cast(value); + if(packed) + { + 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; icreate_range(off, size); -} - -void BufferBackedUniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni) -{ - if(info.block->bind_point<0) - throw invalid_argument("BufferBackedUniformBlock::attach"); - - uni.store(info, &data[info.offset]); dirty = true; } -void BufferBackedUniformBlock::apply(int index) const +void UniformBlock::check_store_range(size_t offs, size_t size) { - if(index<0) - throw invalid_argument("BufferBackedUniformBlock::apply"); - if(!get_buffer()) - throw invalid_operation("UniformBlock::apply"); - - refresh(); - buf_range->bind_to(UNIFORM_BUFFER, index); +#ifdef DEBUG + if(offs>data.size() || offs+size>data.size()) + throw out_of_range("UniformBlock::store"); +#endif } } // namespace GL