From 6022ff616e4856d10b5c5946f710dd6bbc9f139f Mon Sep 17 00:00:00 2001 From: athile Date: Tue, 20 Sep 2011 22:21:15 -0400 Subject: [PATCH] Swizzle updates to handle non-POD types (e.g. hvec3) correctly --- glm/core/_swizzle.hpp | 36 +++++++++++++++----------- glm/core/type_vec2.hpp | 8 +++--- glm/core/type_vec3.hpp | 8 +++--- glm/core/type_vec4.hpp | 8 +++--- test/core/core_type_vec3.cpp | 49 +++++++++++++++++++++++++++++++++--- 5 files changed, 80 insertions(+), 29 deletions(-) diff --git a/glm/core/_swizzle.hpp b/glm/core/_swizzle.hpp index 3b154c47..e5dcf492 100644 --- a/glm/core/_swizzle.hpp +++ b/glm/core/_swizzle.hpp @@ -53,7 +53,7 @@ namespace detail for (int i = 0; i < N; ++i) t[i] = that[i]; for (int i = 0; i < N; ++i) - e[offset_dst[i]] = t[i]; + elem(offset_dst[i]) = t[i]; return *this; } @@ -63,12 +63,18 @@ namespace detail static const int offset_dst[4] = { E0, E1, E2, E3 }; for (int i = 0; i < N; ++i) - e[offset_dst[i]] = t; + elem(offset_dst[i]) = t; return *this; } - Type e[N]; + protected: + Type& elem (size_t i) { return (reinterpret_cast(_buffer))[i]; } + + // Use an opaque buffer to *ensure* the compiler doesn't call a constructor. + // Otherwise, a vec4 containg all swizzles might end up with 1000s of + // constructor calls + char _buffer[sizeof(Type) * N]; }; template @@ -76,8 +82,10 @@ namespace detail { struct Stub {}; swizzle_base& operator= (const Stub& that) {} - - Type e[N]; + + protected: + Type& elem (size_t i) { return (reinterpret_cast(_buffer))[i]; } + char _buffer[sizeof(Type) * N]; }; //! Internal class for implementing swizzle operators @@ -85,7 +93,7 @@ namespace detail struct swizzle2 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1]); } + operator P () { return P(this->elem(E0), this->elem(E1)); } }; //! Internal class for implementing swizzle operators @@ -93,7 +101,7 @@ namespace detail struct swizzle2_3 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1], this->e[E2]); } + operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } }; //! Internal class for implementing swizzle operators @@ -101,7 +109,7 @@ namespace detail struct swizzle2_4 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1], this->e[E2], this->e[E3]); } + operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } }; //! Internal class for implementing swizzle operators @@ -109,7 +117,7 @@ namespace detail struct swizzle3 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1], this->e[E2]); } + operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } }; //! Internal class for implementing swizzle operators @@ -117,7 +125,7 @@ namespace detail struct swizzle3_2 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1]); } + operator P () { return P(this->elem(E0), this->elem(E1)); } }; //! Internal class for implementing swizzle operators @@ -125,7 +133,7 @@ namespace detail struct swizzle3_4 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1], this->e[E2], this->e[E3]); } + operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } }; //! Internal class for implementing swizzle operators @@ -133,7 +141,7 @@ namespace detail struct swizzle4 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1], this->e[E2], this->e[E3]); } + operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2), this->elem(E3)); } }; //! Internal class for implementing swizzle operators @@ -141,7 +149,7 @@ namespace detail struct swizzle4_2 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1]); } + operator P () { return P(this->elem(E0), this->elem(E1)); } }; @@ -150,7 +158,7 @@ namespace detail struct swizzle4_3 : public swizzle_base { using swizzle_base::operator=; - operator P () { return P(this->e[E0], this->e[E1], this->e[E2]); } + operator P () { return P(this->elem(E0), this->elem(E1), this->elem(E2)); } }; }//namespace detail diff --git a/glm/core/type_vec2.hpp b/glm/core/type_vec2.hpp index 0286bdd9..3695becd 100644 --- a/glm/core/type_vec2.hpp +++ b/glm/core/type_vec2.hpp @@ -48,10 +48,6 @@ namespace detail # elif(GLM_COMPONENT == GLM_COMPONENT_MS_EXT) union { - struct{value_type x, y;}; - struct{value_type r, g;}; - struct{value_type s, t;}; - _GLM_SWIZZLE2_2_MEMBERS(value_type,glm::detail::tvec2,x,y) _GLM_SWIZZLE2_2_MEMBERS(value_type,glm::detail::tvec2,r,g) _GLM_SWIZZLE2_2_MEMBERS(value_type,glm::detail::tvec2,s,t) @@ -61,6 +57,10 @@ namespace detail _GLM_SWIZZLE2_4_MEMBERS(value_type,glm::detail::tvec4,x,y) _GLM_SWIZZLE2_4_MEMBERS(value_type,glm::detail::tvec4,r,g) _GLM_SWIZZLE2_4_MEMBERS(value_type,glm::detail::tvec4,s,t) + + struct{value_type r, g;}; + struct{value_type s, t;}; + struct{value_type x, y;}; }; # else//(GLM_COMPONENT == GLM_COMPONENT_GLSL_NAMES) union {value_type x, r, s;}; diff --git a/glm/core/type_vec3.hpp b/glm/core/type_vec3.hpp index b6991a46..6c4acf01 100644 --- a/glm/core/type_vec3.hpp +++ b/glm/core/type_vec3.hpp @@ -48,10 +48,6 @@ namespace detail # elif(GLM_COMPONENT == GLM_COMPONENT_MS_EXT) union { - struct{value_type x, y, z;}; - struct{value_type r, g, b;}; - struct{value_type s, t, p;}; - _GLM_SWIZZLE3_2_MEMBERS(value_type,glm::detail::tvec2,x,y,z) _GLM_SWIZZLE3_2_MEMBERS(value_type,glm::detail::tvec2,r,g,b) _GLM_SWIZZLE3_2_MEMBERS(value_type,glm::detail::tvec2,s,t,p) @@ -61,6 +57,10 @@ namespace detail _GLM_SWIZZLE3_4_MEMBERS(value_type,glm::detail::tvec4,x,y,z) _GLM_SWIZZLE3_4_MEMBERS(value_type,glm::detail::tvec4,r,g,b) _GLM_SWIZZLE3_4_MEMBERS(value_type,glm::detail::tvec4,s,t,p) + + struct{value_type r, g, b;}; + struct{value_type s, t, p;}; + struct{value_type x, y, z;}; }; # else//(GLM_COMPONENT == GLM_COMPONENT_GLSL_NAMES) union {value_type x, r, s;}; diff --git a/glm/core/type_vec4.hpp b/glm/core/type_vec4.hpp index ae004dc7..898faab0 100644 --- a/glm/core/type_vec4.hpp +++ b/glm/core/type_vec4.hpp @@ -48,10 +48,6 @@ namespace detail # elif(GLM_COMPONENT == GLM_COMPONENT_MS_EXT) union { - struct{value_type x, y, z, w;}; - struct{value_type r, g, b, a;}; - struct{value_type s, t, p, q;}; - _GLM_SWIZZLE4_2_MEMBERS(value_type,glm::detail::tvec2,x,y,z,w) _GLM_SWIZZLE4_2_MEMBERS(value_type,glm::detail::tvec2,r,g,b,a) _GLM_SWIZZLE4_2_MEMBERS(value_type,glm::detail::tvec2,s,t,p,q) @@ -61,6 +57,10 @@ namespace detail _GLM_SWIZZLE4_4_MEMBERS(value_type,glm::detail::tvec4,x,y,z,w) _GLM_SWIZZLE4_4_MEMBERS(value_type,glm::detail::tvec4,r,g,b,a) _GLM_SWIZZLE4_4_MEMBERS(value_type,glm::detail::tvec4,s,t,p,q) + + struct{value_type r, g, b, a;}; + struct{value_type s, t, p, q;}; + struct{value_type x, y, z, w;}; }; # else//(GLM_COMPONENT == GLM_COMPONENT_GLSL_NAMES) union {value_type x, r, s;}; diff --git a/test/core/core_type_vec3.cpp b/test/core/core_type_vec3.cpp index 573d1777..0334859a 100644 --- a/test/core/core_type_vec3.cpp +++ b/test/core/core_type_vec3.cpp @@ -8,6 +8,7 @@ /////////////////////////////////////////////////////////////////////////////////////////////////// #include +#include static int test_vec3_operators() { @@ -91,18 +92,17 @@ int test_vec3_swizzle3_2() v.zx = u; Error += (v.x == 2.0f && v.y == 1.0f && v.z == 1.0f) ? 0 : 1; v.zy = u; Error += (v.x == 2.0f && v.y == 2.0f && v.z == 1.0f) ? 0 : 1; //v.zz = u; //Illegal - + return Error; } - int test_vec3_swizzle3_3() { int Error = 0; glm::vec3 v(1, 2, 3); glm::vec3 u; - + u = v; Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; @@ -126,6 +126,48 @@ int test_vec3_swizzle3_3() Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; u.pts = v; Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + + return Error; +} + +int test_vec3_swizzle_half() +{ + int Error = 0; + + glm::half a1(1); + glm::half b1(2); + glm::half c1(3); + glm::hvec3 v(a1, b1, c1); + glm::hvec3 u; + + float c = v.x; + float d = v.y; + u = v; + + float a = u.x; + float b = u.y; + Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + + /*u = v.xyz; + Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + u = v.zyx; + Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + u.zyx = v; + Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + + u = v.rgb; + Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + u = v.bgr; + Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + u.bgr = v; + Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + + u = v.stp; + Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1; + u = v.pts; + Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1; + u.pts = v; + Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;*/ return Error; } @@ -138,6 +180,7 @@ int main() Error += test_vec3_size(); Error += test_vec3_swizzle3_2(); Error += test_vec3_swizzle3_3(); + Error += test_vec3_swizzle_half(); return Error; }