X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Funiformblock.cpp;h=8f85b87f0ba1573e8749b7f8c405382da8201360;hb=729a477b47e97aea41f3f0b5db551f02bf70d1ee;hp=25511d251e2361e56b67285b86b7e8add1f14585;hpb=492325880c47450f50dcdebdcb336b37230ef867;p=libs%2Fgl.git diff --git a/source/core/uniformblock.cpp b/source/core/uniformblock.cpp index 25511d25..8f85b87f 100644 --- a/source/core/uniformblock.cpp +++ b/source/core/uniformblock.cpp @@ -1,11 +1,11 @@ -#include +#include #include #include #include "buffer.h" #include "color.h" +#include "deviceinfo.h" #include "error.h" #include "matrix.h" -#include "uniform.h" #include "uniformblock.h" #include "vector.h" @@ -14,81 +14,79 @@ using namespace std; namespace Msp { namespace GL { -DefaultUniformBlock::DefaultUniformBlock() +UniformBlock::UniformBlock(const ReflectData::UniformBlockInfo &info): + data(info.data_size) { static Require _req(ARB_shader_objects); + if(info.bind_point>=0) + static Require _req2(ARB_uniform_buffer_object); } -void DefaultUniformBlock::attach(const Program::UniformInfo &info, const Uniform &uni) +unsigned UniformBlock::get_alignment() const { - if(info.block->bind_point>=0) - throw invalid_argument("DefaultUniformBlock::attach"); - - uniforms[info.location] = &uni; -} - -void DefaultUniformBlock::attach(int index, const Uniform &uni) -{ - 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"); + return Limits::get_global().uniform_buffer_alignment; } -BufferBackedUniformBlock::~BufferBackedUniformBlock() +void UniformBlock::store(const ReflectData::UniformInfo &info, unsigned array_size, const void *value) { - delete buf_range; -} + array_size = min(array_size, max(info.array_size, 1U)); -unsigned BufferBackedUniformBlock::get_alignment() const -{ - return BufferRange::get_uniform_buffer_alignment(); -} - -void BufferBackedUniformBlock::location_changed(Buffer *buf, unsigned off, unsigned) const -{ - delete buf_range; - buf_range = buf->create_range(off, size); -} - -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(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; ibind_to(UNIFORM_BUFFER, index); -} - } // namespace GL } // namespace Msp