From 5ef419bce3cf3fcd7d9802841b15a8b5e2d45e46 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Sun, 29 Mar 2015 11:04:11 +0200 Subject: [PATCH] Test uniform buffer format --HG-- branch : opengl3 --- .../src/3d/driver/opengl3/driver_opengl.cpp | 12 ++ .../opengl3/driver_opengl_uniform_buffer.cpp | 123 +++++++++++++-- .../opengl3/driver_opengl_uniform_buffer.h | 146 ++++-------------- 3 files changed, 150 insertions(+), 131 deletions(-) diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl.cpp index ece7eca31..8ce454286 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl.cpp @@ -32,6 +32,7 @@ #include "nel/misc/dynloadlib.h" #include "driver_opengl_vertex_buffer.h" #include "driver_glsl_shader_generator.h" +#include "driver_opengl_uniform_buffer.h" using namespace std; @@ -150,6 +151,17 @@ CDriverGL3::CDriverGL3() { H_AUTO_OGL(CDriverGL3_CDriverGL) +#if NL3D_GL3_UNIFORM_BUFFER_DEBUG + // Test UniformBufferFormat + { + CUniformBufferFormat ubf; + testUniformBufferFormat(ubf); + std::stringstream ss; + generateUniformBufferGLSL(ss, ubf, NL_BUILTIN_MATERIAL_BINDING); + nlinfo("%s", ss.str().c_str()); + } +#endif + #if defined(NL_OS_WINDOWS) _PBuffer = NULL; 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 dd9b84f31..722093baf 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 @@ -22,27 +22,118 @@ #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 = - "#define NL_BUILTIN_CAMERA_BIND " NL_MACRO_TO_STR(NL_BUILTIN_CAMERA_BIND) "\n" - "#define NL_BUILTIN_MODEL_BIND " NL_MACRO_TO_STR(NL_BUILTIN_MODEL_BIND) "\n" - "#define NL_BUILTIN_MATERIAL_BIND " NL_MACRO_TO_STR(NL_BUILTIN_MATERIAL_BIND) "\n" - "#define NL_USER_ENV_BIND " NL_MACRO_TO_STR(NL_USER_ENV_BIND) "\n" - "#define NL_USER_VERTEX_PROGRAM_BIND " NL_MACRO_TO_STR(NL_USER_VERTEX_PROGRAM_BIND) "\n" - "#define NL_USER_GEOMETRY_PROGRAM_BIND " NL_MACRO_TO_STR(NL_USER_GEOMETRY_PROGRAM_BIND) "\n" - "#define NL_USER_PIXEL_PROGRAM_BIND " NL_MACRO_TO_STR(NL_USER_PIXEL_PROGRAM_BIND) "\n" - "#define NL_USER_MATERIAL_BIND " NL_MACRO_TO_STR(NL_USER_MATERIAL_BIND) "\n"; + "#define NL_BUILTIN_CAMERA_BINDING " NL_MACRO_TO_STR(NL_BUILTIN_CAMERA_BIND) "\n" + "#define NL_BUILTIN_MODEL_BINDING " NL_MACRO_TO_STR(NL_BUILTIN_MODEL_BIND) "\n" + "#define NL_BUILTIN_MATERIAL_BINDING " NL_MACRO_TO_STR(NL_BUILTIN_MATERIAL_BIND) "\n" + "#define NL_USER_ENV_BINDING " NL_MACRO_TO_STR(NL_USER_ENV_BIND) "\n" + "#define NL_USER_VERTEX_PROGRAM_BINDING " NL_MACRO_TO_STR(NL_USER_VERTEX_PROGRAM_BIND) "\n" + "#define NL_USER_GEOMETRY_PROGRAM_BINDING " NL_MACRO_TO_STR(NL_USER_GEOMETRY_PROGRAM_BIND) "\n" + "#define NL_USER_PIXEL_PROGRAM_BINDING " NL_MACRO_TO_STR(NL_USER_PIXEL_PROGRAM_BIND) "\n" + "#define NL_USER_MATERIAL_BINDING " NL_MACRO_TO_STR(NL_USER_MATERIAL_BIND) "\n"; static const char *s_UniformBufferBindDefine[] = { - "NL_BUILTIN_CAMERA_BIND", - "NL_BUILTIN_MODEL_BIND", - "NL_BUILTIN_MATERIAL_BIND", - "NL_USER_ENV_BIND", - "NL_USER_VERTEX_PROGRAM_BIND", - "NL_USER_GEOMETRY_PROGRAM_BIND", - "NL_USER_PIXEL_PROGRAM_BIND", - "NL_USER_MATERIAL_BIND", + "NL_BUILTIN_CAMERA_BINDING", + "NL_BUILTIN_MODEL_BINDING", + "NL_BUILTIN_MATERIAL_BINDING", + "NL_USER_ENV_BINDING", + "NL_USER_VERTEX_PROGRAM_BINDING", + "NL_USER_GEOMETRY_PROGRAM_BINDING", + "NL_USER_PIXEL_PROGRAM_BINDING", + "NL_USER_MATERIAL_BINDING", }; static const char *s_UniformBufferName[] = { 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 a44e7c7b0..63b0e109c 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 @@ -19,31 +19,10 @@ #include "nel/misc/types_nl.h" -namespace NL3D { -namespace NLDRIVERGL3 { - -// NOTE: It is completely safe to reorder these indices. -// When changing, update: -// - GLSLHeaderUniformBuffer -// - s_UniformBufferBindDefine -// - s_UniformBufferBindName -// Always use the defines. -#define NL_BUILTIN_CAMERA_BIND 0 // Builtin uniform buffer bound by driver, set by camera transformation -#define NL_BUILTIN_MODEL_BIND 1 // Builtin uniform buffer bound by driver, set by model transformation -#define NL_BUILTIN_MATERIAL_BIND 2 // Builtin uniform buffer bound by material -#define NL_USER_ENV_BIND 3 // User-specified uniform buffer bound by user -#define NL_USER_VERTEX_PROGRAM_BIND 4 // User-specified uniform buffer bound by vertex program -#define NL_USER_GEOMETRY_PROGRAM_BIND 5 // User-specified uniform buffer bound by geometry program -#define NL_USER_PIXEL_PROGRAM_BIND 6 // User-specified uniform buffer bound by pixel program -#define NL_USER_MATERIAL_BIND 7 // User-specified uniform buffer bound by material - -extern const char *GLSLHeaderUniformBuffer; - -} // NLDRIVERGL3 -} // NL3D - #include "nel/misc/string_mapper.h" +#define NL3D_GL3_UNIFORM_BUFFER_DEBUG 1 + namespace NL3D { // Uniform buffer format generation following glsl std140 rules @@ -107,11 +86,11 @@ public: sint push(const std::string &name, TType type, sint count = 1) { nlassert(count > 0); - sint baseAlign = count == 1 - ? s_TypeAlignment[type] + sint baseAlign = count == 1 + ? s_TypeAlignment[type] : ((s_TypeAlignment[type] + 15) & ~0xF); sint baseOffset = m_Entries.size() - ? m_Entries.back().Offset + m_Entries.back().stride() + ? m_Entries.back().Offset + m_Entries.back().size() : 0; sint alignOffset = baseOffset; alignOffset += (baseAlign - 1); @@ -128,7 +107,7 @@ public: 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[]; @@ -138,97 +117,34 @@ private: }; -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, -}; +void testUniformBufferFormat(CUniformBufferFormat &ubf); -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); - 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 { +namespace NLDRIVERGL3 { + +// NOTE: It is completely safe to reorder these indices. +// When changing, update: +// - GLSLHeaderUniformBuffer +// - s_UniformBufferBindDefine +// - s_UniformBufferBindName +// Always use the defines. +#define NL_BUILTIN_CAMERA_BINDING 0 // Builtin uniform buffer bound by driver, set by camera transformation +#define NL_BUILTIN_MODEL_BINDING 1 // Builtin uniform buffer bound by driver, set by model transformation +#define NL_BUILTIN_MATERIAL_BINDING 2 // Builtin uniform buffer bound by material +#define NL_USER_ENV_BINDING 3 // User-specified uniform buffer bound by user +#define NL_USER_VERTEX_PROGRAM_BINDING 4 // User-specified uniform buffer bound by vertex program +#define NL_USER_GEOMETRY_PROGRAM_BINDING 5 // User-specified uniform buffer bound by geometry program +#define NL_USER_PIXEL_PROGRAM_BINDING 6 // User-specified uniform buffer bound by pixel program +#define NL_USER_MATERIAL_BINDING 7 // User-specified uniform buffer bound by material + +extern const char *GLSLHeaderUniformBuffer; + +void generateUniformBufferGLSL(std::stringstream &ss, const CUniformBufferFormat &ubf, sint binding); + +} // NLDRIVERGL3 +} // NL3D #endif // NL_DRIVER_OPENGL_UNIFORM_BUFFER_H