Test uniform buffer format

--HG--
branch : opengl3
hg/feature/opengl3
kaetemi 10 years ago
parent 8bdb4e577b
commit 5ef419bce3

@ -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;

@ -22,27 +22,118 @@
#include <nel/misc/debug.h>
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[] = {

@ -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
@ -111,7 +90,7 @@ public:
? 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);
@ -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,
};
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);
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

Loading…
Cancel
Save