Swizzle updates to handle non-POD types (e.g. hvec3) correctly

This commit is contained in:
athile 2011-09-20 22:21:15 -04:00
parent f09aa1c177
commit 6022ff616e
5 changed files with 80 additions and 29 deletions

View File

@ -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<Type*>(_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 <typename Type, typename Class, int N, int E0, int E1, int E2, int E3>
@ -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<Type*>(_buffer))[i]; }
char _buffer[sizeof(Type) * N];
};
//! Internal class for implementing swizzle operators
@ -85,7 +93,7 @@ namespace detail
struct swizzle2 : public swizzle_base<T,P,2,E0,E1,0,0,(E0 == E1)>
{
using swizzle_base<T,P,2,E0,E1,0,0,(E0 == E1)>::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<T,P,2,E0,E1,E2,0,1>
{
using swizzle_base<T,P,2,E0,E1,E2,0,1>::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<T,P,2,E0,E1,E2,E3,1>
{
using swizzle_base<T,P,2,E0,E1,E2,E3,1>::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<T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>
{
using swizzle_base<T,P,3,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>::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<T,P,2,E0,E1,0,0,(E0==E1)>
{
using swizzle_base<T,P,2,E0,E1,0,0,(E0==E1)>::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<T,P,3,E0,E1,E2,E3,1>
{
using swizzle_base<T,P,3,E0,E1,E2,E3,1>::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<T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)>
{
using swizzle_base<T,P,4,E0,E1,E2,E3,(E0==E1||E0==E2||E0==E3||E1==E2||E1==E3||E2==E3)>::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<T,P,2,E0,E1,0,0,(E0==E1)>
{
using swizzle_base<T,P,2,E0,E1,0,0,(E0==E1)>::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<T,P,4,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>
{
using swizzle_base<T,P,4,E0,E1,E2,0,(E0==E1||E0==E2||E1==E2)>::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

View File

@ -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<value_type>,x,y)
_GLM_SWIZZLE2_2_MEMBERS(value_type,glm::detail::tvec2<value_type>,r,g)
_GLM_SWIZZLE2_2_MEMBERS(value_type,glm::detail::tvec2<value_type>,s,t)
@ -61,6 +57,10 @@ namespace detail
_GLM_SWIZZLE2_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,x,y)
_GLM_SWIZZLE2_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,r,g)
_GLM_SWIZZLE2_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,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;};

View File

@ -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<value_type>,x,y,z)
_GLM_SWIZZLE3_2_MEMBERS(value_type,glm::detail::tvec2<value_type>,r,g,b)
_GLM_SWIZZLE3_2_MEMBERS(value_type,glm::detail::tvec2<value_type>,s,t,p)
@ -61,6 +57,10 @@ namespace detail
_GLM_SWIZZLE3_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,x,y,z)
_GLM_SWIZZLE3_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,r,g,b)
_GLM_SWIZZLE3_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,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;};

View File

@ -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<value_type>,x,y,z,w)
_GLM_SWIZZLE4_2_MEMBERS(value_type,glm::detail::tvec2<value_type>,r,g,b,a)
_GLM_SWIZZLE4_2_MEMBERS(value_type,glm::detail::tvec2<value_type>,s,t,p,q)
@ -61,6 +57,10 @@ namespace detail
_GLM_SWIZZLE4_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,x,y,z,w)
_GLM_SWIZZLE4_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,r,g,b,a)
_GLM_SWIZZLE4_4_MEMBERS(value_type,glm::detail::tvec4<value_type>,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;};

View File

@ -8,6 +8,7 @@
///////////////////////////////////////////////////////////////////////////////////////////////////
#include <glm/glm.hpp>
#include <glm/gtc/half_float.hpp>
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;
}