From c21d4de686e7abd51b0a779af00f06ce459e18d6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 26 Aug 2012 15:06:15 +0300 Subject: [PATCH] Align uniform blocks properly --- source/buffer.cpp | 6 ++++++ source/buffer.h | 1 + source/bufferable.cpp | 3 +++ source/bufferable.h | 1 + source/uniformblock.cpp | 5 +++++ source/uniformblock.h | 1 + 6 files changed, 17 insertions(+) diff --git a/source/buffer.cpp b/source/buffer.cpp index f0926874..f3b40da6 100644 --- a/source/buffer.cpp +++ b/source/buffer.cpp @@ -176,5 +176,11 @@ unsigned BufferRange::get_n_uniform_buffer_bindings() return count; } +unsigned BufferRange::get_uniform_buffer_alignment() +{ + static unsigned align = get_i(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT); + return align; +} + } // namespace GL } // namespace Msp diff --git a/source/buffer.h b/source/buffer.h index 6f0a9708..206cec8d 100644 --- a/source/buffer.h +++ b/source/buffer.h @@ -135,6 +135,7 @@ private: public: static unsigned get_n_uniform_buffer_bindings(); + static unsigned get_uniform_buffer_alignment(); }; } // namespace GL diff --git a/source/bufferable.cpp b/source/bufferable.cpp index 12e0aef7..abd10b3a 100644 --- a/source/bufferable.cpp +++ b/source/bufferable.cpp @@ -58,6 +58,9 @@ void Bufferable::update_buffer_offset() if(prev_in_buffer) offset = prev_in_buffer->buffer_offset+prev_in_buffer->get_data_size(); + unsigned align = get_alignment(); + offset += align-1; + offset -= offset%align; if(offset!=buffer_offset) { buffer_offset = offset; diff --git a/source/bufferable.h b/source/bufferable.h index fa81554d..f5ae043c 100644 --- a/source/bufferable.h +++ b/source/bufferable.h @@ -25,6 +25,7 @@ private: protected: virtual unsigned get_data_size() const = 0; + virtual unsigned get_alignment() const { return 1; } void update_buffer_offset(); virtual void offset_changed() { } diff --git a/source/uniformblock.cpp b/source/uniformblock.cpp index 6ea0d320..a87a6a42 100644 --- a/source/uniformblock.cpp +++ b/source/uniformblock.cpp @@ -27,6 +27,11 @@ UniformBlock::~UniformBlock() delete buf_range; } +unsigned UniformBlock::get_alignment() const +{ + return BufferRange::get_uniform_buffer_alignment(); +} + void UniformBlock::offset_changed() { delete buf_range; diff --git a/source/uniformblock.h b/source/uniformblock.h index 66b3c7e0..12b24de3 100644 --- a/source/uniformblock.h +++ b/source/uniformblock.h @@ -33,6 +33,7 @@ public: private: virtual unsigned get_data_size() const { return size; } + virtual unsigned get_alignment() const; virtual void offset_changed(); virtual void upload_data() const; -- 2.45.2