From 65a2f9dad7f5b2273f1bcd7cef57141bb93097c9 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Sun, 29 Mar 2015 11:04:11 +0200 Subject: [PATCH] Move uniform buffer format builder to NL3D --HG-- branch : opengl3 --- .../include/nel/3d/uniform_buffer_format.h | 116 ++++++++++++++ .../opengl3/driver_opengl_uniform_buffer.cpp | 92 +---------- .../opengl3/driver_opengl_uniform_buffer.h | 102 +----------- code/nel/src/3d/uniform_buffer_format.cpp | 149 ++++++++++++++++++ 4 files changed, 268 insertions(+), 191 deletions(-) create mode 100644 code/nel/include/nel/3d/uniform_buffer_format.h create mode 100644 code/nel/src/3d/uniform_buffer_format.cpp diff --git a/code/nel/include/nel/3d/uniform_buffer_format.h b/code/nel/include/nel/3d/uniform_buffer_format.h new file mode 100644 index 000000000..060bf269b --- /dev/null +++ b/code/nel/include/nel/3d/uniform_buffer_format.h @@ -0,0 +1,116 @@ +/* + +Copyright (C) 2015 Jan Boon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef NL_UNIFORM_BUFFER_FORMAT_H +#define NL_UNIFORM_BUFFER_FORMAT_H + +#include +#include + +namespace NL3D { + +// Uniform buffer format generation following glsl std140 rules +class CUniformBufferFormat +{ +public: + // When changing, update + // - s_TypeAlignment + // - s_TypeSize + // - NL3D::NLDRIVERGL3::s_TypeKeyword + enum TType + { + Float, // float + FloatVec2, // CVector2D + FloatVec3, + FloatVec4, // CVector + SInt, // sint32 + SIntVec2, + SIntVec3, + SIntVec4, + UInt, // uint32 + UIntVec2, + UIntVec3, + UIntVec4, + Bool, + BoolVec2, + BoolVec3, + BoolVec4, + FloatMat2, + FloatMat3, + FloatMat4, // CMatrix + FloatMat2x3, + FloatMat2x4, + FloatMat3x2, + FloatMat3x4, + FloatMat4x2, + FloatMat4x3, + }; + + struct CEntry + { + NLMISC::TStringId Name; + TType Type; + sint Offset; + sint Count; + + inline sint stride() const + { + return Count == 1 + ? s_TypeSize[Type] + : ((s_TypeSize[Type] + 15) & ~0xF); + } + inline sint size() const + { + return stride() * Count; + } + }; + + // Push a variable. Returns the byte offset in uniform buffer + // Note: Does not check for duplicate names. However, names must be unique + sint push(const std::string &name, TType type, sint count = 1); + + inline const CEntry &get(sint i) const { return m_Entries[i]; } + inline size_t size() const { return m_Entries.size(); } + inline void clear() { m_Entries.clear(); } + +private: + static const sint s_TypeAlignment[]; + static const sint s_TypeSize[]; + + typedef std::vector TEntries; + TEntries m_Entries; + +}; + +void testUniformBufferFormat(CUniformBufferFormat &ubf); + +} /* namespace NL3D */ + +#endif /* #ifndef NL_UNIFORM_BUFFER_FORMAT_H */ + +/* end of file */ diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.cpp index 722093baf..9c0ef51e9 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.cpp @@ -14,6 +14,7 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . +#include "stdopengl.h" #include "driver_opengl.h" #include "driver_opengl_uniform_buffer.h" @@ -22,97 +23,6 @@ #include namespace NL3D { - -const sint CUniformBufferFormat::s_TypeAlignment[] = { - 4, // Float - 8, - 16, - 16, - 4, // SInt - 8, - 16, - 16, - 4, // UInt - 8, - 16, - 16, - 4, // Bool - 8, - 16, - 16, - 16, // FloatMat2 - 16, - 16, - 16, // FloatMat2x3 - 16, - 16, // FloatMat3x2 - 16, - 16, // FloatMat4x2 - 16, -}; - -const sint CUniformBufferFormat::s_TypeSize[] = { - 4, // Float - 8, - 12, - 16, - 4, // SInt - 8, - 12, - 16, - 4, // UInt - 8, - 12, - 16, - 4, // Bool - 8, - 12, - 16, - 16 + 16, // FloatMat2 - 16 + 16 + 16, // FloatMat3 - 16 + 16 + 16 + 16, // FloatMat4 - 16 + 16, // FloatMat2x3 - 16 + 16, // FloatMat2x4 - 16 + 16 + 16, // FloatMat3x2 - 16 + 16 + 16, // FloatMat3x4 - 16 + 16 + 16 + 16, // FloatMat4x2 - 16 + 16 + 16 + 16, // FloatMat4x3 -}; - -void testUniformBufferFormat(CUniformBufferFormat &ubf) -{ - sint offset; - offset = ubf.push("a", CUniformBufferFormat::Float); - nlassert(offset == 0); - offset = ubf.push("b", CUniformBufferFormat::FloatVec2); - nlassert(offset == 8); - offset = ubf.push("c", CUniformBufferFormat::FloatVec3); - nlassert(offset == 16); - offset = ubf.push("d", CUniformBufferFormat::FloatVec4); - nlassert(offset == 32); - offset = ubf.push("e", CUniformBufferFormat::FloatVec2); - nlassert(offset == 48); - offset = ubf.push("g", CUniformBufferFormat::Float); - nlassert(offset == 56); - offset = ubf.push("h", CUniformBufferFormat::Float, 2); - nlassert(offset == 64); - offset = ubf.push("i", CUniformBufferFormat::FloatMat2x3); - nlinfo("offset: %i", offset); - nlassert(offset == 96); - offset = ubf.push("j", CUniformBufferFormat::FloatVec3); - nlassert(offset == 128); - offset = ubf.push("k", CUniformBufferFormat::FloatVec2); - nlassert(offset == 144); - offset = ubf.push("l", CUniformBufferFormat::Float, 2); - nlassert(offset == 160); - offset = ubf.push("m", CUniformBufferFormat::FloatVec2); - nlassert(offset == 192); - offset = ubf.push("n", CUniformBufferFormat::FloatMat3, 2); - nlassert(offset == 208); - offset = ubf.push("o", CUniformBufferFormat::FloatVec3); - nlassert(offset == 304); -} - namespace NLDRIVERGL3 { const char *GLSLHeaderUniformBuffer = diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.h b/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.h index 63b0e109c..9025de051 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.h +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_uniform_buffer.h @@ -17,110 +17,12 @@ #ifndef NL_DRIVER_OPENGL_UNIFORM_BUFFER_H #define NL_DRIVER_OPENGL_UNIFORM_BUFFER_H -#include "nel/misc/types_nl.h" +#include -#include "nel/misc/string_mapper.h" +#include #define NL3D_GL3_UNIFORM_BUFFER_DEBUG 1 -namespace NL3D { - -// Uniform buffer format generation following glsl std140 rules -class CUniformBufferFormat -{ -public: - // When changing, update - // - s_TypeAlignment - // - s_TypeSize - // - NL3D::NLDRIVERGL3::s_TypeKeyword - enum TType - { - Float, // float - FloatVec2, // CVector2D - FloatVec3, - FloatVec4, // CVector - SInt, // sint32 - SIntVec2, - SIntVec3, - SIntVec4, - UInt, // uint32 - UIntVec2, - UIntVec3, - UIntVec4, - Bool, - BoolVec2, - BoolVec3, - BoolVec4, - FloatMat2, - FloatMat3, - FloatMat4, // CMatrix - FloatMat2x3, - FloatMat2x4, - FloatMat3x2, - FloatMat3x4, - FloatMat4x2, - FloatMat4x3, - }; - - struct CEntry - { - NLMISC::TStringId Name; - TType Type; - sint Offset; - sint Count; - - inline sint stride() const - { - return Count == 1 - ? s_TypeSize[Type] - : ((s_TypeSize[Type] + 15) & ~0xF); - } - inline sint size() const - { - return stride() * Count; - } - }; - - // Push a variable. Returns the byte offset in uniform buffer - // Note: Does not check for duplicate names. However, names must be unique - sint push(const std::string &name, TType type, sint count = 1) - { - nlassert(count > 0); - sint baseAlign = count == 1 - ? s_TypeAlignment[type] - : ((s_TypeAlignment[type] + 15) & ~0xF); - sint baseOffset = m_Entries.size() - ? m_Entries.back().Offset + m_Entries.back().size() - : 0; - sint alignOffset = baseOffset; - alignOffset += (baseAlign - 1); - alignOffset &= ~(baseAlign - 1); // Note: alignment MUST BE power of 2 for this to work - m_Entries.resize(m_Entries.size() + 1); - CEntry &entry = m_Entries.back(); - entry.Name = NLMISC::CStringMapper::map(name); - entry.Type = type; - entry.Offset = alignOffset; - entry.Count = count; - return alignOffset; - } - - inline const CEntry &get(sint i) const { return m_Entries[i]; } - inline size_t size() const { return m_Entries.size(); } - inline void clear() { m_Entries.clear(); } - -private: - static const sint s_TypeAlignment[]; - static const sint s_TypeSize[]; - - typedef std::vector TEntries; - TEntries m_Entries; - -}; - -void testUniformBufferFormat(CUniformBufferFormat &ubf); - -} - namespace NL3D { namespace NLDRIVERGL3 { diff --git a/code/nel/src/3d/uniform_buffer_format.cpp b/code/nel/src/3d/uniform_buffer_format.cpp new file mode 100644 index 000000000..f85cd40bc --- /dev/null +++ b/code/nel/src/3d/uniform_buffer_format.cpp @@ -0,0 +1,149 @@ +/* + +Copyright (C) 2015 Jan Boon +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +*/ + +#include "std3d.h" +#include + +#include + +namespace NL3D { + +const sint CUniformBufferFormat::s_TypeAlignment[] = { + 4, // Float + 8, + 16, + 16, + 4, // SInt + 8, + 16, + 16, + 4, // UInt + 8, + 16, + 16, + 4, // Bool + 8, + 16, + 16, + 16, // FloatMat2 + 16, + 16, + 16, // FloatMat2x3 + 16, + 16, // FloatMat3x2 + 16, + 16, // FloatMat4x2 + 16, +}; + +const sint CUniformBufferFormat::s_TypeSize[] = { + 4, // Float + 8, + 12, + 16, + 4, // SInt + 8, + 12, + 16, + 4, // UInt + 8, + 12, + 16, + 4, // Bool + 8, + 12, + 16, + 16 + 16, // FloatMat2 + 16 + 16 + 16, // FloatMat3 + 16 + 16 + 16 + 16, // FloatMat4 + 16 + 16, // FloatMat2x3 + 16 + 16, // FloatMat2x4 + 16 + 16 + 16, // FloatMat3x2 + 16 + 16 + 16, // FloatMat3x4 + 16 + 16 + 16 + 16, // FloatMat4x2 + 16 + 16 + 16 + 16, // FloatMat4x3 +}; + +sint CUniformBufferFormat::push(const std::string &name, TType type, sint count) +{ + nlassert(count > 0); + sint baseAlign = count == 1 + ? s_TypeAlignment[type] + : ((s_TypeAlignment[type] + 15) & ~0xF); + sint baseOffset = m_Entries.size() + ? m_Entries.back().Offset + m_Entries.back().size() + : 0; + sint alignOffset = baseOffset; + alignOffset += (baseAlign - 1); + alignOffset &= ~(baseAlign - 1); // Note: alignment MUST BE power of 2 for this to work + m_Entries.resize(m_Entries.size() + 1); + CEntry &entry = m_Entries.back(); + entry.Name = NLMISC::CStringMapper::map(name); + entry.Type = type; + entry.Offset = alignOffset; + entry.Count = count; + return alignOffset; +} + +void testUniformBufferFormat(CUniformBufferFormat &ubf) +{ + sint offset; + offset = ubf.push("a", CUniformBufferFormat::Float); + nlassert(offset == 0); + offset = ubf.push("b", CUniformBufferFormat::FloatVec2); + nlassert(offset == 8); + offset = ubf.push("c", CUniformBufferFormat::FloatVec3); + nlassert(offset == 16); + offset = ubf.push("d", CUniformBufferFormat::FloatVec4); + nlassert(offset == 32); + offset = ubf.push("e", CUniformBufferFormat::FloatVec2); + nlassert(offset == 48); + offset = ubf.push("g", CUniformBufferFormat::Float); + nlassert(offset == 56); + offset = ubf.push("h", CUniformBufferFormat::Float, 2); + nlassert(offset == 64); + offset = ubf.push("i", CUniformBufferFormat::FloatMat2x3); + nlinfo("offset: %i", offset); + nlassert(offset == 96); + offset = ubf.push("j", CUniformBufferFormat::FloatVec3); + nlassert(offset == 128); + offset = ubf.push("k", CUniformBufferFormat::FloatVec2); + nlassert(offset == 144); + offset = ubf.push("l", CUniformBufferFormat::Float, 2); + nlassert(offset == 160); + offset = ubf.push("m", CUniformBufferFormat::FloatVec2); + nlassert(offset == 192); + offset = ubf.push("n", CUniformBufferFormat::FloatMat3, 2); + nlassert(offset == 208); + offset = ubf.push("o", CUniformBufferFormat::FloatVec3); + nlassert(offset == 304); +} + +} /* namespace NL3D */ + +/* end of file */