From: Mikko Rasa Date: Wed, 21 Apr 2021 13:39:40 +0000 (+0300) Subject: Use template functions for setting vector and matrix uniforms X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=4124807fcacedc8317bd109f056d48e077d0c12f;p=libs%2Fgl.git Use template functions for setting vector and matrix uniforms These work automatically for all sizes. Type-safe overloads for arrays were also added for convenience. --- diff --git a/source/render/programdata.cpp b/source/render/programdata.cpp index 0f641a4a..d934b487 100644 --- a/source/render/programdata.cpp +++ b/source/render/programdata.cpp @@ -237,11 +237,6 @@ void ProgramData::uniform(Tag tag, float v0, float v1, float v2) uniform3(tag, va); } -void ProgramData::uniform(Tag tag, const Vector3 &v) -{ - uniform(tag, v.x, v.y, v.z); -} - void ProgramData::uniform3(Tag tag, const int *v) { uniform(tag, v); @@ -264,11 +259,6 @@ void ProgramData::uniform(Tag tag, float v0, float v1, float v2, float v3) uniform4(tag, va); } -void ProgramData::uniform(Tag tag, const Vector4 &v) -{ - uniform(tag, v.x, v.y, v.z, v.w); -} - void ProgramData::uniform(Tag tag, const Color &c) { uniform(tag, c.r, c.g, c.b, c.a); @@ -284,81 +274,41 @@ void ProgramData::uniform4(Tag tag, const float *v) uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix2(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix2(Tag tag, const float *v) { uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix3x2(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix3x2(Tag tag, const float *v) { uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix4x2(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix4x2(Tag tag, const float *v) { uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix2x3(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix2x3(Tag tag, const float *v) { uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix3(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix3(Tag tag, const float *v) { uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix4x3(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix4x3(Tag tag, const float *v) { uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix2x4(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix2x4(Tag tag, const float *v) { uniform(tag, v); } -void ProgramData::uniform(Tag tag, const LinAl::Matrix &m) -{ - uniform_matrix3x4(tag, &m(0, 0)); -} - void ProgramData::uniform_matrix3x4(Tag tag, const float *v) { uniform(tag, v); @@ -374,6 +324,16 @@ void ProgramData::uniform_matrix4(Tag tag, const float *v) uniform(tag, v); } +void ProgramData::uniform_array(Tag tag, unsigned n, const int *v) +{ + uniform_array(tag, n, v); +} + +void ProgramData::uniform_array(Tag tag, unsigned n, const float *v) +{ + uniform_array(tag, n, v); +} + void ProgramData::uniform1_array(Tag tag, unsigned n, const int *v) { uniform_array(tag, n, v); diff --git a/source/render/programdata.h b/source/render/programdata.h index a90d868e..f600f025 100644 --- a/source/render/programdata.h +++ b/source/render/programdata.h @@ -8,6 +8,7 @@ #include "matrix.h" #include "program.h" #include "tag.h" +#include "uniform.h" #include "vector.h" namespace Msp { @@ -22,7 +23,6 @@ public: class Buffer; class BufferBackedUniformBlock; -class Uniform; class UniformBlock; struct Color; @@ -183,33 +183,25 @@ public: void uniform2(Tag, const float *); void uniform(Tag, int, int, int); void uniform(Tag, float, float, float); - void uniform(Tag, const Vector3 &); void uniform3(Tag, const int *); void uniform3(Tag, const float *); void uniform(Tag, int, int, int, int); void uniform(Tag, float, float, float, float); - void uniform(Tag, const Vector4 &); void uniform(Tag, const Color &); void uniform4(Tag, const int *); void uniform4(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix2(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix3x2(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix4x2(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix2x3(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix3(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix4x3(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix2x4(Tag, const float *); - void uniform(Tag, const LinAl::Matrix &); void uniform_matrix3x4(Tag, const float *); void uniform(Tag, const Matrix &); void uniform_matrix4(Tag, const float *); + void uniform_array(Tag, unsigned, const int *); + void uniform_array(Tag, unsigned, const float *); void uniform1_array(Tag, unsigned, const int *); void uniform1_array(Tag, unsigned, const float *); void uniform2_array(Tag, unsigned, const int *); @@ -227,6 +219,19 @@ public: void uniform_matrix2x4_array(Tag, unsigned, const float *); void uniform_matrix3x4_array(Tag, unsigned, const float *); void uniform_matrix4_array(Tag, unsigned, const float *); + + template + void uniform(Tag, const LinAl::Vector &); + + template + void uniform(Tag, const LinAl::Matrix &); + + template + void uniform_array(Tag, unsigned, const LinAl::Vector *); + + template + void uniform_array(Tag, unsigned, const LinAl::Matrix *); + void remove_uniform(Tag); std::vector get_uniform_tags() const; @@ -245,6 +250,22 @@ public: void apply() const; }; +template +void ProgramData::uniform(Tag tag, const LinAl::Vector &v) +{ uniform >(tag, &v.x); } + +template +void ProgramData::uniform(Tag tag, const LinAl::Matrix &v) +{ uniform >(tag, &v(0, 0)); } + +template +void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Vector *v) +{ uniform_array >(tag, n, &v[0].x); } + +template +void ProgramData::uniform_array(Tag tag, unsigned n, const LinAl::Matrix *v) +{ uniform_array >(tag, n, &v[0](0, 0)); } + } // namespace GL } // namespace Msp diff --git a/source/render/renderer.cpp b/source/render/renderer.cpp index 03653edc..15e4437f 100644 --- a/source/render/renderer.cpp +++ b/source/render/renderer.cpp @@ -370,7 +370,7 @@ void Renderer::apply_state() standard_shdata.uniform("eye_obj_matrix", state->modelview_matrix); LinAl::SquareMatrix nm = state->modelview_matrix.block<3, 3>(0, 0); nm = transpose(invert(nm)); - standard_shdata.uniform_matrix3("eye_obj_normal_matrix", &nm(0, 0)); + standard_shdata.uniform("eye_obj_normal_matrix", nm); changed = (changed&~MATRIX)|STANDARD_SHDATA; }